forked from alexfru/dflat20
-
Notifications
You must be signed in to change notification settings - Fork 2
/
message.c
600 lines (566 loc) · 16.6 KB
/
message.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/* --------- message.c ---------- */
#include "dflat.h"
static int px = -1, py = -1;
static int pmx = -1, pmy = -1;
static int mx, my;
static int handshaking = 0;
BOOL AllocTesting = FALSE;
jmp_buf AllocError;
BOOL AltDown = FALSE;
/* ---------- event queue ---------- */
static struct events {
MESSAGE event;
int mx;
int my;
} EventQueue[MAXMESSAGES];
/* ---------- message queue --------- */
static struct msgs {
WINDOW wnd;
MESSAGE msg;
PARAM p1;
PARAM p2;
} MsgQueue[MAXMESSAGES];
static int EventQueueOnCtr;
static int EventQueueOffCtr;
static int EventQueueCtr;
static int MsgQueueOnCtr;
static int MsgQueueOffCtr;
static int MsgQueueCtr;
static int lagdelay = FIRSTDELAY;
static volatile int keyportvalue; /* for watching for key release */
WINDOW CaptureMouse;
WINDOW CaptureKeyboard;
static BOOL NoChildCaptureMouse;
static BOOL NoChildCaptureKeyboard;
static volatile int delaytimer = -1;
static volatile int clocktimer = -1;
char time_string[] = " ";
static WINDOW Cwnd;
static void StopMsg(void)
{
#ifndef BUILD_SMALL_DFLAT
ClearClipboard();
ClearDialogBoxes();
#endif
restorecursor();
unhidecursor();
hide_mousecursor();
}
/* ------------ initialize the message system --------- */
BOOL init_messages(void)
{
int cols, rows;
AllocTesting = TRUE;
if (setjmp(AllocError) != 0) {
StopMsg();
return FALSE;
}
tty_init(MouseTracking|CatchISig|ExitLastLine|FullBuffer);
if (tty_getsize(&cols, &rows) > 0) {
SCREENWIDTH = min(cols, MAXCOLS-1);
SCREENHEIGHT = rows - 1;
}
resetmouse();
set_mousetravel(0, SCREENWIDTH-1, 0, SCREENHEIGHT-1);
savecursor();
hidecursor();
px = py = -1;
pmx = pmy = -1;
mx = my = 0;
CaptureMouse = CaptureKeyboard = NULL;
NoChildCaptureMouse = FALSE;
NoChildCaptureKeyboard = FALSE;
MsgQueueOnCtr = MsgQueueOffCtr = MsgQueueCtr = 0;
EventQueueOnCtr = EventQueueOffCtr = EventQueueCtr = 0;
PostMessage(NULL,START,0,0);
lagdelay = FIRSTDELAY;
return TRUE;
}
/* ----- post an event and parameters to event queue ---- */
void PostEvent(MESSAGE event, int p1, int p2)
{
if (EventQueueCtr != MAXMESSAGES) {
EventQueue[EventQueueOnCtr].event = event;
EventQueue[EventQueueOnCtr].mx = p1;
EventQueue[EventQueueOnCtr].my = p2;
if (++EventQueueOnCtr == MAXMESSAGES)
EventQueueOnCtr = 0;
EventQueueCtr++;
}
}
#if MSDOS
/* ------ collect mouse, clock, and keyboard events ----- */
void near collect_events(void)
{
static int ShiftKeys = 0;
int sk;
struct tm *now;
static BOOL flipflop = FALSE;
int hr;
/* -------- 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
/* ----- post a message and parameters to msg queue ---- */
void PostMessage(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
{
if (MsgQueueCtr != MAXMESSAGES) {
MsgQueue[MsgQueueOnCtr].wnd = wnd;
MsgQueue[MsgQueueOnCtr].msg = msg;
MsgQueue[MsgQueueOnCtr].p1 = p1;
MsgQueue[MsgQueueOnCtr].p2 = p2;
if (++MsgQueueOnCtr == MAXMESSAGES)
MsgQueueOnCtr = 0;
MsgQueueCtr++;
}
}
/* --------- send a message to a window ----------- */
int SendMessage(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
{
int rtn = TRUE, x, y;
#ifdef INCLUDE_LOGGING
LogMessages(wnd, msg, p1, p2);
#endif
if (wnd != NULL)
switch (msg) {
case PAINT:
case BORDER:
/* ------- don't send these messages unless the
window is visible -------- */
if (isVisible(wnd))
rtn = (*wnd->wndproc)(wnd, msg, p1, p2);
break;
case RIGHT_BUTTON:
case LEFT_BUTTON:
case DOUBLE_CLICK:
case BUTTON_RELEASED:
/* --- don't send these messages unless the
window is visible or has captured the mouse -- */
if (isVisible(wnd) || wnd == CaptureMouse)
rtn = (*wnd->wndproc)(wnd, msg, p1, p2);
break;
case KEYBOARD:
case SHIFT_CHANGED:
/* ------- don't send these messages unless the
window is visible or has captured the keyboard -- */
if (!(isVisible(wnd) || wnd == CaptureKeyboard))
break;
default:
rtn = (*wnd->wndproc)(wnd, msg, p1, p2);
break;
}
/* ----- window processor returned true or the message was sent
to no window at all (NULL) ----- */
if (rtn != FALSE) {
/* --------- process messages that a window sends to the
system itself ---------- */
switch (msg) {
case STOP:
StopMsg();
break;
/* ------- clock messages --------- */
case CAPTURE_CLOCK:
if (Cwnd == NULL)
set_timer(clocktimer, 0);
wnd->PrevClock = Cwnd;
Cwnd = wnd;
break;
case RELEASE_CLOCK:
Cwnd = wnd->PrevClock;
if (Cwnd == NULL)
disable_timer(clocktimer);
break;
/* -------- keyboard messages ------- */
case KEYBOARD_CURSOR:
if (wnd == NULL)
cursor((int)p1, (int)p2);
else if (wnd == inFocus)
cursor(GetClientLeft(wnd)+(int)p1,
GetClientTop(wnd)+(int)p2);
break;
case CAPTURE_KEYBOARD:
if (p2)
((WINDOW)p2)->PrevKeyboard=CaptureKeyboard;
else
wnd->PrevKeyboard = CaptureKeyboard;
CaptureKeyboard = wnd;
NoChildCaptureKeyboard = (int)p1;
break;
case RELEASE_KEYBOARD:
if (wnd != NULL) {
if (CaptureKeyboard == wnd || (int)p1)
CaptureKeyboard = wnd->PrevKeyboard;
else {
WINDOW twnd = CaptureKeyboard;
while (twnd != NULL) {
if (twnd->PrevKeyboard == wnd) {
twnd->PrevKeyboard = wnd->PrevKeyboard;
break;
}
twnd = twnd->PrevKeyboard;
}
if (twnd == NULL)
CaptureKeyboard = NULL;
}
wnd->PrevKeyboard = NULL;
}
else
CaptureKeyboard = NULL;
NoChildCaptureKeyboard = FALSE;
break;
case CURRENT_KEYBOARD_CURSOR:
curr_cursor(&x, &y);
*(int*)p1 = x;
*(int*)p2 = y;
break;
case SAVE_CURSOR:
savecursor();
break;
case RESTORE_CURSOR:
restorecursor();
break;
case HIDE_CURSOR:
normalcursor();
hidecursor();
break;
case SHOW_CURSOR:
if (p1)
set_cursor_type(0x0106);
else
set_cursor_type(0x0607);
unhidecursor();
break;
case WAITKEYBOARD:
waitforkeyboard();
break;
/* -------- mouse messages -------- */
case RESET_MOUSE:
resetmouse();
set_mousetravel(0, SCREENWIDTH-1, 0, SCREENHEIGHT-1);
break;
case MOUSE_INSTALLED:
rtn = mouse_installed();
break;
case MOUSE_TRAVEL: {
RECT rc;
if (!p1) {
rc.lf = rc.tp = 0;
rc.rt = SCREENWIDTH-1;
rc.bt = SCREENHEIGHT-1;
}
else
rc = *(RECT *)p1;
set_mousetravel(rc.lf, rc.rt, rc.tp, rc.bt);
break;
}
case SHOW_MOUSE:
show_mousecursor();
break;
case HIDE_MOUSE:
hide_mousecursor();
break;
case MOUSE_CURSOR:
set_mouseposition((int)p1, (int)p2);
break;
case CURRENT_MOUSE_CURSOR:
get_mouseposition((int*)p1,(int*)p2);
break;
case WAITMOUSE:
waitformouse();
break;
case TESTMOUSE:
rtn = mousebuttons();
break;
case CAPTURE_MOUSE:
if (p2)
((WINDOW)p2)->PrevMouse = CaptureMouse;
else
wnd->PrevMouse = CaptureMouse;
CaptureMouse = wnd;
NoChildCaptureMouse = (int)p1;
break;
case RELEASE_MOUSE:
if (wnd != NULL) {
if (CaptureMouse == wnd || (int)p1)
CaptureMouse = wnd->PrevMouse;
else {
WINDOW twnd = CaptureMouse;
while (twnd != NULL) {
if (twnd->PrevMouse == wnd) {
twnd->PrevMouse = wnd->PrevMouse;
break;
}
twnd = twnd->PrevMouse;
}
if (twnd == NULL)
CaptureMouse = NULL;
}
wnd->PrevMouse = NULL;
}
else
CaptureMouse = NULL;
NoChildCaptureMouse = FALSE;
break;
default:
break;
}
}
return rtn;
}
static RECT VisibleRect(WINDOW wnd)
{
RECT rc = WindowRect(wnd);
if (!TestAttribute(wnd, NOCLIP)) {
WINDOW pwnd = GetParent(wnd);
if (!pwnd)
return rc;
RECT prc;
prc = ClientRect(pwnd);
while (pwnd != NULL) {
if (TestAttribute(pwnd, NOCLIP))
break;
rc = subRectangle(rc, prc);
if (!ValidRect(rc))
break;
if ((pwnd = GetParent(pwnd)) != NULL)
prc = ClientRect(pwnd);
}
}
return rc;
}
/* ----- find window that mouse coordinates are in --- */
static WINDOW inWindow(WINDOW wnd, int x, int y)
{
WINDOW Hit = NULL;
while (wnd != NULL) {
if (isVisible(wnd)) {
WINDOW wnd1;
RECT rc = VisibleRect(wnd);
if (InsideRect(x, y, rc))
Hit = wnd;
if ((wnd1 = inWindow(LastWindow(wnd), x, y)) != NULL)
Hit = wnd1;
if (Hit != NULL)
break;
}
wnd = PrevWindow(wnd);
}
return Hit;
}
static WINDOW MouseWindow(int x, int y)
{
/* ------ get the window in which a
mouse event occurred ------ */
WINDOW Mwnd = inWindow(ApplicationWindow, x, y);
/* ---- process mouse captures ----- */
if (CaptureMouse != NULL) {
if (NoChildCaptureMouse ||
Mwnd == NULL ||
!isAncestor(Mwnd, CaptureMouse))
Mwnd = CaptureMouse;
}
return Mwnd;
}
void handshake(void)
{
#if MSDOS
handshaking++;
dispatch_message();
--handshaking;
#endif
}
/* ---- dispatch messages to the message proc function ---- */
BOOL dispatch_message(void)
{
WINDOW Mwnd, Kwnd;
/* -------- collect mouse and keyboard events ------- */
collect_events();
/* --------- dequeue and process events -------- */
while (EventQueueCtr > 0) {
struct events ev;
ev = EventQueue[EventQueueOffCtr];
if (++EventQueueOffCtr == MAXMESSAGES)
EventQueueOffCtr = 0;
--EventQueueCtr;
/* ------ get the window in which a
keyboard event occurred ------ */
Kwnd = inFocus;
/* ---- process keyboard captures ----- */
if (CaptureKeyboard != NULL)
if (Kwnd == NULL ||
NoChildCaptureKeyboard ||
!isAncestor(Kwnd, CaptureKeyboard))
Kwnd = CaptureKeyboard;
/* -------- send mouse and keyboard messages to the
window that should get them -------- */
switch (ev.event) {
case SHIFT_CHANGED:
case KEYBOARD:
if (!handshaking)
SendMessage(Kwnd, ev.event, ev.mx, ev.my);
break;
case LEFT_BUTTON:
if (!handshaking) {
Mwnd = MouseWindow(ev.mx, ev.my);
if (!CaptureMouse ||
(!NoChildCaptureMouse &&
isAncestor(Mwnd, CaptureMouse)))
if (Mwnd != inFocus)
SendMessage(Mwnd, SETFOCUS, TRUE, 0);
SendMessage(Mwnd, LEFT_BUTTON, ev.mx, ev.my);
}
break;
case BUTTON_RELEASED:
case DOUBLE_CLICK:
case RIGHT_BUTTON:
if (handshaking)
break;
case MOUSE_MOVED:
Mwnd = MouseWindow(ev.mx, ev.my);
SendMessage(Mwnd, ev.event, ev.mx, ev.my);
break;
#if MSDOS // FIXME add MK_FP
case CLOCKTICK:
SendMessage(Cwnd, ev.event,
(PARAM) MK_FP(ev.mx, ev.my), 0);
break;
#endif
default:
break;
}
}
/* ------ dequeue and process messages ----- */
while (MsgQueueCtr > 0) {
struct msgs mq;
mq = MsgQueue[MsgQueueOffCtr];
if (++MsgQueueOffCtr == MAXMESSAGES)
MsgQueueOffCtr = 0;
--MsgQueueCtr;
SendMessage(mq.wnd, mq.msg, mq.p1, mq.p2);
if (mq.msg == ENDDIALOG)
return FALSE;
if (mq.msg == STOP) {
PostMessage(NULL, STOP, 0, 0);
return FALSE;
}
}
#if VIDEO_FB
convert_screen_to_ansi();
#endif
return TRUE;
}