forked from alexfru/dflat20
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevents-unix.c
194 lines (186 loc) · 5.88 KB
/
events-unix.c
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* UNIX event generation */
#include <signal.h>
#include <termios.h>
#include <sys/select.h>
#include "dflat.h"
#include "unikey.h"
int mouse_x, mouse_y;
int mouse_button;
/* ------ collect mouse, clock, and keyboard events ----- */
void collect_events(void)
{
fd_set fdset;
struct timeval tv;
int e, n;
int mx, my, modkeys;
char buf[32];
FD_ZERO(&fdset);
FD_SET(0, &fdset);
tv.tv_sec = 0;
tv.tv_usec = 30000;
e = select(1, &fdset, NULL, NULL, &tv);
if (e < 0) return;
if (e == 0) return; //FIXME implement timeouts
if (FD_ISSET(0, &fdset)) {
if ((n = readansi(0, buf, sizeof(buf))) < 0)
return;
if ((e = ansi_to_unikey(buf, n)) != -1) { // FIXME UTF-8 unicode != -1
PostEvent(KEYBOARD, e, 0); // no sk
return;
}
if ((n = ansi_to_unimouse(buf, n, &mx, &my, &modkeys, &e)) != -1) {
if (mx >= SCREENWIDTH || my >= SCREENHEIGHT-1) return;
switch (n) {
case kMouseLeftDown:
PostEvent(LEFT_BUTTON, mx, my);
break;
case kMouseLeftDoubleClick:
PostEvent(DOUBLE_CLICK, mx, my);
break;
case kMouseRightDown:
PostEvent(RIGHT_BUTTON, mx, my);
break;
case kMouseLeftUp:
case kMouseRightUp:
PostEvent(BUTTON_RELEASED, mx, my);
break;
case kMouseMotion: /* only returned on ANSI 1003 */
break;
case kMouseLeftDrag:
case kMouseRightDrag:
PostEvent(MOUSE_MOVED, mx, my);
break;
case kMouseWheelUp:
PostEvent(KEYBOARD, (modkeys & kCtrl)? PGUP: UP, 0);
break;
case kMouseWheelDown:
PostEvent(KEYBOARD, (modkeys & kCtrl)? PGDN: DN, 0);
break;
}
mouse_x = mx;
mouse_y = my;
mouse_button = n;
return;
}
printf("unknown ANSI key %s\r\n", unikeyname(n));
}
#if 0
/* -------- test for a clock event (one/second) ------- */
if (timed_out(clocktimer)) {
/* ----- get the current time ----- */
time_t t = time(NULL);
now = localtime(&t);
hr = now->tm_hour > 12 ?
now->tm_hour - 12 :
now->tm_hour;
if (hr == 0)
hr = 12;
sprintf(time_string, "%2d:%02d", hr, now->tm_min);
strcpy(time_string+5, now->tm_hour > 11 ? "pm " : "am ");
/* ------- blink the : at one-second intervals ----- */
if (flipflop)
*(time_string+2) = ' ';
flipflop ^= TRUE;
/* -------- reset the timer -------- */
set_timer(clocktimer, 1);
/* -------- post the clock event -------- */
PostEvent(CLOCKTICK, FP_SEG(time_string), FP_OFF(time_string));
}
/* --------- keyboard events ---------- */
if ((sk = getshift()) != ShiftKeys) {
ShiftKeys = sk;
/* ---- the shift status changed ---- */
PostEvent(SHIFT_CHANGED, sk, 0);
if (sk & ALTKEY)
AltDown = TRUE;
}
/* ---- build keyboard events for key combinations that
BIOS doesn't report --------- */
if (sk & ALTKEY) {
if (keyportvalue == 14) {
AltDown = FALSE;
waitforkeyboard();
PostEvent(KEYBOARD, ALT_BS, sk);
}
if (keyportvalue == 83) {
AltDown = FALSE;
waitforkeyboard();
PostEvent(KEYBOARD, ALT_DEL, sk);
}
}
if (sk & CTRLKEY) {
AltDown = FALSE;
if (keyportvalue == 82) {
waitforkeyboard();
PostEvent(KEYBOARD, CTRL_INS, sk);
}
}
/* ----------- test for keystroke ------- */
if (keyhit()) {
static int cvt[] = {SHIFT_INS,END,DN,PGDN,BS,'5',
FWD,HOME,UP,PGUP};
int c = getkey();
AltDown = FALSE;
/* -------- convert numeric pad keys ------- */
if (sk & (LEFTSHIFT | RIGHTSHIFT)) {
if (c >= '0' && c <= '9')
c = cvt[c-'0'];
else if (c == '.' || c == DEL)
c = SHIFT_DEL;
else if (c == INS)
c = SHIFT_INS;
}
if (c != '\r' && (c < ' ' || c > 127))
clearBIOSbuffer();
/* ------ post the keyboard event ------ */
PostEvent(KEYBOARD, c, sk);
}
/* ------------ test for mouse events --------- */
if (button_releases()) {
/* ------- the button was released -------- */
AltDown = FALSE;
doubletimer = DOUBLETICKS;
PostEvent(BUTTON_RELEASED, mx, my);
disable_timer(delaytimer);
}
get_mouseposition(&mx, &my);
if (mx != px || my != py) {
px = mx;
py = my;
PostEvent(MOUSE_MOVED, mx, my);
}
if (rightbutton()) {
AltDown = FALSE;
PostEvent(RIGHT_BUTTON, mx, my);
}
if (leftbutton()) {
AltDown = FALSE;
if (mx == pmx && my == pmy) {
/* ---- same position as last left button ---- */
if (timer_running(doubletimer)) {
/* -- second click before double timeout -- */
disable_timer(doubletimer);
PostEvent(DOUBLE_CLICK, mx, my);
}
else if (!timer_running(delaytimer)) {
/* ---- button held down a while ---- */
delaytimer = lagdelay;
lagdelay = DELAYTICKS;
/* ---- post a typematic-like button ---- */
PostEvent(LEFT_BUTTON, mx, my);
}
}
else {
/* --------- new button press ------- */
disable_timer(doubletimer);
delaytimer = FIRSTDELAY;
lagdelay = DELAYTICKS;
PostEvent(LEFT_BUTTON, mx, my);
pmx = mx;
pmy = my;
}
}
else
lagdelay = FIRSTDELAY;
#endif
}