File: events.c

package info (click to toggle)
biew 5.2.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,808 kB
  • ctags: 5,528
  • sloc: ansic: 38,763; makefile: 389; pascal: 245
file content (165 lines) | stat: -rw-r--r-- 4,904 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/** 
 * @namespace   biew
 * @file        events.c
 * @brief       This file contains console event handler of BIEW project.
 * @version     -
 * @remark      this source file is part of Binary vIEW project (BIEW).
 *              The Binary vIEW (BIEW) is copyright (C) 1995 Nick Kurshev.
 *              All rights reserved. This software is redistributable under the
 *              licence given in the file "Licence.en" ("Licence.ru" in russian
 *              translation) distributed in the BIEW archive.
 * @note        Requires POSIX compatible development system
 *
 * @author      Nick Kurshev
 * @since       1995
 * @note        Development, fixes and improvements
**/
#include <limits.h>
#include <string.h>

#include "bconsole.h"
#include "biewlib/kbd_code.h"
#include "biewlib/biewlib.h"

static int KB_Buff[64];
static size_t KB_freq = 0;

int  __FASTCALL__ IsKbdTerminate( void )
{
  int ch = 0;
  if(__kbdTestKey(0)) ch = __kbdGetKey(0);
  return ch == KE_ESCAPE;
}

static tAbsCoord mx,my;

static int __NEAR__ __FASTCALL__ getPromptKey(int flag)
{
 int ret;
 if(mx <= 7) ret = KE_F(1);
 else
   if(mx <= 15) ret = KE_F(2);
   else
     if(mx <= 23) ret = KE_F(3);
     else
       if(mx <= 31) ret = KE_F(4);
       else
         if(mx <= 39) ret = KE_F(5);
         else
           if(mx <= 47) ret = KE_F(6);
           else
             if(mx <= 55) ret = KE_F(7);
             else
               if(mx <= 63) ret = KE_F(8);
               else
                 if(mx <= 71) ret = KE_F(9);
                 else         ret = KE_F(10);
 if(flag & KS_SHIFT) ret += KE_SHIFT_F(1) - KE_F(1);
 else
   if(flag & KS_ALT) ret += KE_ALT_F(1) - KE_F(1);
   else
     if(flag & KS_CTRL) ret += KE_CTL_F(1) - KE_F(1);
 return ret;
}

extern unsigned long biew_kbdFlags;

static int __NEAR__ __FASTCALL__ __GetEvent( void (*prompt)(void) ,TWindow *win)
{
 static unsigned char oFlag = UCHAR_MAX;
 static void (* oprompt)(void) = 0;
 unsigned char Flag;
 int key;
 /** Unconditional repaint prompt */
 Flag = __kbdGetShiftsKey();
 if(Flag != oFlag || oprompt !=prompt)
 {
    oFlag = Flag; oprompt = prompt;
    if(prompt) (*prompt)();
 }
 while(1)
 {
     key = __kbdGetKey(biew_kbdFlags);
     switch( key )
     {
       case KE_SHIFTKEYS:
             Flag = __kbdGetShiftsKey();
             if(Flag != oFlag || oprompt !=prompt)
             {
               oFlag = Flag; oprompt = prompt;
               if(prompt) (*prompt)();
             }
             break;
       case KE_MOUSE:
             __MsGetPos(&mx,&my);
             if((__MsGetBtns() & MS_LEFTPRESS) == MS_LEFTPRESS)
             {
                  if(my == (unsigned)(tvioHeight - 1))
                  {
                    while(1)
                    {
                      if((__MsGetBtns() & MS_LEFTPRESS) != MS_LEFTPRESS) break;
                      __MsGetPos(&mx,&my);
                    }
                    if(my != (unsigned)(tvioHeight - 1)) continue;
                    return getPromptKey(__kbdGetShiftsKey());
                  }
                  else
                  {
                    tAbsCoord X1,Y1,X2,Y2;
                    unsigned wdh,hght;
                    if(win)
                    {
                      twGetWinPos(win,&X1,&Y1,&X2,&Y2);
                      X1--; Y1--; X2--; Y2--;
                    }
                    else
                    {
                      X1 = 0; X2 = tvioWidth; Y1 = 1; Y2 = tvioHeight - 1;
                    }
                    wdh = X2 - X1;
                    hght = Y2 - Y1;
                    if(win && (mx < X1 || mx > X2 || my < Y1 || my > Y2))
                    {
                       while(1)
                       {
                          if((__MsGetBtns() & MS_LEFTPRESS) != MS_LEFTPRESS) break;
                       }
                       return KE_ESCAPE;
                    }
                    if(my <= Y2 && my >= Y1 + 3*hght/4) return KE_DOWNARROW;
                    else
                      if(my >= Y1 && my <= Y1 + hght/4) return KE_UPARROW;
                      else
                        if(mx <= X2 && mx >= X1 + 3*wdh/4) return KE_RIGHTARROW;
                        else
                          if(mx >= X1 && mx <= X1 + wdh/4) return KE_LEFTARROW;
                  }
              }
        default: return key;
     }
 }
 return key;
}

static void __NEAR__ __FASTCALL__ __GetEventQue(void (*prompt)(void), TWindow *win)
{
  int key;
  key = __GetEvent(prompt,win);
  if(KB_freq < sizeof(KB_Buff)/sizeof(int)) KB_Buff[KB_freq++] = key;
}

int __FASTCALL__ GetEvent( void (*prompt)(void) ,TWindow * win)
{
  int key;
  while(!KB_freq) __GetEventQue(prompt,win);
  key = KB_Buff[0];
  --KB_freq;
  if(KB_freq) memmove(KB_Buff,&KB_Buff[1],KB_freq-1);
  return key;
}

void __FASTCALL__ PostEvent(int code)
{
  if(KB_freq < sizeof(KB_Buff)/sizeof(int)) KB_Buff[KB_freq++] = code;
}