Skip to content

Latest commit

 

History

History
1679 lines (1469 loc) · 56.1 KB

pmwin.c

File metadata and controls

1679 lines (1469 loc) · 56.1 KB
 
Feb 26, 2018
Feb 26, 2018
1
2
3
4
5
6
7
8
/**
* 2ine; an OS/2 emulator for Linux.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
9
10
11
12
#include "os2native.h"
#include "pmwin.h"
#include "SDL.h"
Feb 27, 2018
Feb 27, 2018
13
14
#include "pmwin-lx.h"
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
// NOTE: PM reference manual says OS/2 generally ignores the HAB you pass
// to functions, instead getting that info from the current thread, but
// other IBM platforms might not do that, so programs should always pass
// a valid HAB to everything. Things like WinCreateWindow() probably rely
// on the current thread's HAB without requiring the API call to specify
// it in any case.
// So current approach: keep a pointer to the anchor block in the TIB, and
// the anchor block keeps a copy of it's HAB. When the anchor block is
// needed, we pull it from the TIB directly, and if it's in a function
// where a HAB is specified, we fail if the handles don't match.
// If I later find out that OS/2 doesn't even care even if you give it a
// bogus HAB, we can relax this check.
// !!! FIXME: You can send messages to HWNDs on a different thread, which
// !!! FIXME: means HWND values must be unique across threads. Technically,
// !!! FIXME: they have to be unique across processes, too. :/
#define FIRST_HWND_VALUE 10
#define FIRST_HPS_VALUE 10
// This is the keyname we use on an heavyweight SDL window to find our
// associated Window*.
#define WINDATA_WINDOWPTR_NAME "2ine_windowptr"
static SDL_atomic_t GHABCounter;
static SDL_atomic_t GHMQCounter;
typedef struct WindowClass
{
char *name;
PFNWP window_proc;
ULONG style;
ULONG data_len;
struct WindowClass *next;
} WindowClass;
// these get shared with all children, but they're owned by the topmost parent.
typedef struct
{
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;
} HeavyWeightWindow;
typedef struct Window
{
PFNWP window_proc;
HeavyWeightWindow heavy;
struct Window *parent;
struct Window *children;
struct Window *sibling;
struct Window *owner;
const char *window_class_name; // Class at creation time. styles and window procs might change later, though! This is just to make debugging easier.
char *text;
void *data;
size_t data_len;
ULONG id;
ULONG class_style;
ULONG style;
HWND hwnd;
LONG x;
LONG y;
LONG w;
LONG h;
Jul 10, 2018
Jul 10, 2018
81
BOOL enabled;
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
} Window;
typedef struct MessageQueueItem
{
QMSG qmsg;
struct MessageQueueItem *next;
} MessageQueueItem;
typedef struct
{
HMQ hmq;
MessageQueueItem *head;
MessageQueueItem *tail;
MessageQueueItem *free_pool;
} MessageQueue;
typedef struct
{
HPS hps;
Window *window;
// !!! FIXME: this is gonna be WAY more complicated.
} PresentationSpace;
typedef struct
{
HAB hab; // actual HAB value for this block, for comparison.
ERRORID last_error;
WindowClass *registered_classes;
MessageQueue message_queue;
Window *windows;
size_t windows_array_len;
PresentationSpace *pres_spaces;
size_t pres_spaces_array_len;
} AnchorBlock;
#define SET_WIN_ERROR_AND_RETURN(anchor, err, rc) { anchor->last_error = (err); return (rc); }
static Window desktop_window; // !!! FIXME: initialize and upkeep on this.
static inline AnchorBlock *getAnchorBlockNoHAB(void)
{
Feb 28, 2018
Feb 28, 2018
123
return (AnchorBlock *) LX_GETPOSTTIB()->anchor_block;
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
} // getAnchorBlockNoHAB
static AnchorBlock *getAnchorBlock(const HAB hab)
{
AnchorBlock *anchor = getAnchorBlockNoHAB();
if (!anchor) {
return NULL;
} else if (anchor->hab != hab) {
SET_WIN_ERROR_AND_RETURN(anchor, PMERR_INVALID_HAB, NULL);
}
return anchor;
} // getAnchorBlock
static Window *getWindowFromHWND(const AnchorBlock *anchor, const HWND hwnd)
{
if (hwnd >= FIRST_HWND_VALUE) {
const size_t idx = (size_t) (hwnd - FIRST_HWND_VALUE);
if (idx >= anchor->windows_array_len) {
return NULL;
}
Window *win = &anchor->windows[idx];
return (win->hwnd == hwnd) ? win : NULL;
} else if (hwnd == HWND_DESKTOP) {
return &desktop_window;
}
return NULL;
} // getWindowFromHWND
static PresentationSpace *getPresentationSpaceFromHPS(const AnchorBlock *anchor, const HPS hps)
{
if (hps >= FIRST_HPS_VALUE) {
const size_t idx = (size_t) (hps - FIRST_HPS_VALUE);
if (idx >= anchor->pres_spaces_array_len) {
return NULL;
}
PresentationSpace *ps = &anchor->pres_spaces[idx];
return (ps->hps == hps) ? ps : NULL;
}
return NULL;
} // getPresentationSpaceFromHPS
static const char *messageName(const ULONG msg)
{
switch (msg) {
#define MSGCASE(m) case m: return #m
MSGCASE(WM_NULL);
MSGCASE(WM_CREATE);
MSGCASE(WM_DESTROY);
MSGCASE(WM_ENABLE);
MSGCASE(WM_SHOW);
MSGCASE(WM_MOVE);
MSGCASE(WM_SIZE);
MSGCASE(WM_ADJUSTWINDOWPOS);
MSGCASE(WM_CALCVALIDRECTS);
MSGCASE(WM_SETWINDOWPARAMS);
MSGCASE(WM_QUERYWINDOWPARAMS);
MSGCASE(WM_HITTEST);
MSGCASE(WM_ACTIVATE);
MSGCASE(WM_SETFOCUS);
MSGCASE(WM_SETSELECTION);
MSGCASE(WM_PPAINT);
MSGCASE(WM_PSETFOCUS);
MSGCASE(WM_PSYSCOLORCHANGE);
MSGCASE(WM_PSIZE);
MSGCASE(WM_PACTIVATE);
MSGCASE(WM_PCONTROL);
MSGCASE(WM_COMMAND);
MSGCASE(WM_SYSCOMMAND);
MSGCASE(WM_HELP);
MSGCASE(WM_PAINT);
MSGCASE(WM_TIMER);
MSGCASE(WM_SEM1);
MSGCASE(WM_SEM2);
MSGCASE(WM_SEM3);
MSGCASE(WM_SEM4);
MSGCASE(WM_CLOSE);
MSGCASE(WM_QUIT);
MSGCASE(WM_SYSCOLORCHANGE);
MSGCASE(WM_SYSVALUECHANGED);
MSGCASE(WM_APPTERMINATENOTIFY);
MSGCASE(WM_PRESPARAMCHANGED);
MSGCASE(WM_CONTROL);
MSGCASE(WM_VSCROLL);
MSGCASE(WM_HSCROLL);
MSGCASE(WM_INITMENU);
MSGCASE(WM_MENUSELECT);
MSGCASE(WM_MENUEND);
MSGCASE(WM_DRAWITEM);
MSGCASE(WM_MEASUREITEM);
MSGCASE(WM_CONTROLPOINTER);
MSGCASE(WM_QUERYDLGCODE);
MSGCASE(WM_INITDLG);
MSGCASE(WM_SUBSTITUTESTRING);
MSGCASE(WM_MATCHMNEMONIC);
MSGCASE(WM_SAVEAPPLICATION);
MSGCASE(WM_FLASHWINDOW);
MSGCASE(WM_FORMATFRAME);
MSGCASE(WM_UPDATEFRAME);
MSGCASE(WM_FOCUSCHANGE);
MSGCASE(WM_SETBORDERSIZE);
MSGCASE(WM_TRACKFRAME);
MSGCASE(WM_MINMAXFRAME);
MSGCASE(WM_SETICON);
MSGCASE(WM_QUERYICON);
MSGCASE(WM_SETACCELTABLE);
MSGCASE(WM_QUERYACCELTABLE);
MSGCASE(WM_TRANSLATEACCEL);
MSGCASE(WM_QUERYTRACKINFO);
MSGCASE(WM_QUERYBORDERSIZE);
MSGCASE(WM_NEXTMENU);
MSGCASE(WM_ERASEBACKGROUND);
MSGCASE(WM_QUERYFRAMEINFO);
MSGCASE(WM_QUERYFOCUSCHAIN);
MSGCASE(WM_OWNERPOSCHANGE);
MSGCASE(WM_CALCFRAMERECT);
MSGCASE(WM_WINDOWPOSCHANGED);
MSGCASE(WM_ADJUSTFRAMEPOS);
MSGCASE(WM_QUERYFRAMECTLCOUNT);
MSGCASE(WM_QUERYHELPINFO);
MSGCASE(WM_SETHELPINFO);
MSGCASE(WM_ERROR);
MSGCASE(WM_REALIZEPALETTE);
MSGCASE(WM_RENDERFMT);
MSGCASE(WM_RENDERALLFMTS);
MSGCASE(WM_DESTROYCLIPBOARD);
MSGCASE(WM_PAINTCLIPBOARD);
MSGCASE(WM_SIZECLIPBOARD);
MSGCASE(WM_HSCROLLCLIPBOARD);
MSGCASE(WM_VSCROLLCLIPBOARD);
MSGCASE(WM_DRAWCLIPBOARD);
MSGCASE(WM_MOUSEMOVE);
MSGCASE(WM_BUTTON1DOWN);
MSGCASE(WM_BUTTON1UP);
MSGCASE(WM_BUTTON1DBLCLK);
MSGCASE(WM_BUTTON2DOWN);
MSGCASE(WM_BUTTON2UP);
MSGCASE(WM_BUTTON2DBLCLK);
MSGCASE(WM_BUTTON3DOWN);
MSGCASE(WM_BUTTON3UP);
MSGCASE(WM_BUTTON3DBLCLK);
MSGCASE(WM_CHAR);
MSGCASE(WM_VIOCHAR);
MSGCASE(WM_JOURNALNOTIFY);
MSGCASE(WM_MOUSEMAP);
MSGCASE(WM_VRNDISABLED);
MSGCASE(WM_VRNENABLED);
MSGCASE(WM_DDE_INITIATE);
MSGCASE(WM_DDE_REQUEST);
MSGCASE(WM_DDE_ACK);
MSGCASE(WM_DDE_DATA);
MSGCASE(WM_DDE_ADVISE);
MSGCASE(WM_DDE_UNADVISE);
MSGCASE(WM_DDE_POKE);
MSGCASE(WM_DDE_EXECUTE);
MSGCASE(WM_DDE_TERMINATE);
MSGCASE(WM_DDE_INITIATEACK);
MSGCASE(WM_QUERYCONVERTPOS);
MSGCASE(WM_MSGBOXINIT);
MSGCASE(WM_MSGBOXDISMISS);
MSGCASE(WM_CTLCOLORCHANGE);
MSGCASE(WM_QUERYCTLTYPE);
MSGCASE(WM_CHORD);
MSGCASE(WM_BUTTON1MOTIONSTART);
MSGCASE(WM_BUTTON1MOTIONEND);
MSGCASE(WM_BUTTON1CLICK);
MSGCASE(WM_BUTTON2MOTIONSTART);
MSGCASE(WM_BUTTON2MOTIONEND);
MSGCASE(WM_BUTTON2CLICK);
MSGCASE(WM_BUTTON3MOTIONSTART);
MSGCASE(WM_BUTTON3MOTIONEND);
MSGCASE(WM_BUTTON3CLICK);
MSGCASE(WM_BEGINDRAG);
MSGCASE(WM_ENDDRAG);
MSGCASE(WM_SINGLESELECT);
MSGCASE(WM_OPEN);
MSGCASE(WM_CONTEXTMENU);
MSGCASE(WM_CONTEXTHELP);
MSGCASE(WM_TEXTEDIT);
MSGCASE(WM_BEGINSELECT);
MSGCASE(WM_ENDSELECT);
MSGCASE(WM_PICKUP);
MSGCASE(WM_SEMANTICEVENT);
MSGCASE(WM_USER);
#undef MSGCASE
default: break;
}
return "???";
} // messageName
static WindowClass *findRegisteredClass(AnchorBlock *anchor, const char *classname)
{
const ULONG intclass = (ULONG) (size_t) classname;
if ((intclass >= 0xffff0001) && (intclass <= 0xffff0070)) {
// built-in class, like WC_BUTTON or whatever.
switch (intclass) {
// !!! FIXME: write me.
case WC_FRAME:
case WC_COMBOBOX:
case WC_BUTTON:
case WC_MENU:
case WC_STATIC:
case WC_ENTRYFIELD:
case WC_LISTBOX:
case WC_SCROLLBAR:
case WC_TITLEBAR:
case WC_MLE:
case WC_APPSTAT:
case WC_KBDSTAT:
case WC_PECIC:
case WC_DBE_KKPOPUP:
case WC_SPINBUTTON:
case WC_CONTAINER:
case WC_SLIDER:
case WC_VALUESET:
case WC_NOTEBOOK:
case WC_CIRCULARSLIDER:
default:
return NULL;
}
} else {
for (WindowClass *i = anchor->registered_classes; i; i = i->next) {
if (strcmp(i->name, classname) == 0) {
return i;
}
}
}
return NULL;
} // findRegisteredClass
static ULONG currentSystemTicks(void)
{
FIXME("this isn't correct"); // see notes in processSDLEvent() for details.
return SDL_GetTicks();
} // currentSystemTicks
HAB WinInitialize(ULONG flOptions)
{
Jul 10, 2018
Jul 10, 2018
362
TRACE_NATIVE("WinInitialize(%u)", (uint) flOptions);
363
364
365
366
367
if (flOptions != 0) {
return NULLHANDLE; // reserved; must be zero.
}
Feb 28, 2018
Feb 28, 2018
368
LxPostTIB *posttib = LX_GETPOSTTIB();
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
if (posttib->anchor_block != NULL) {
return NULLHANDLE; // fail if thread already has a HAB
}
if (!SDL_WasInit(SDL_INIT_VIDEO)) {
if (SDL_Init(SDL_INIT_VIDEO) == -1) {
fprintf(stderr, "SDL_INIT_VIDEO failed: %s\n", SDL_GetError());
return NULLHANDLE;
}
}
FIXME("OS/2 doesn't support multi-monitor"); // this might not be true, actually.
SDL_Rect desktoprect;
SDL_GetDisplayBounds(0, &desktoprect);
desktop_window.hwnd = HWND_DESKTOP;
desktop_window.w = desktoprect.w;
desktop_window.h = desktoprect.h;
AnchorBlock *anchor = calloc(1, sizeof (AnchorBlock));
if (anchor == NULL) {
return NULLHANDLE;
}
anchor->windows_array_len = 16;
anchor->windows = (Window *) malloc(sizeof (Window) * anchor->windows_array_len);
if (!anchor->windows) {
free(anchor);
return NULLHANDLE;
}
memset(anchor->windows, '\0', sizeof (Window) * anchor->windows_array_len);
anchor->pres_spaces_array_len = 16;
anchor->pres_spaces = (PresentationSpace *) malloc(sizeof (PresentationSpace) * anchor->pres_spaces_array_len);
if (!anchor->pres_spaces) {
free(anchor->windows);
free(anchor);
return NULLHANDLE;
}
memset(anchor->pres_spaces, '\0', sizeof (PresentationSpace) * anchor->pres_spaces_array_len);
const int ihab = SDL_AtomicAdd(&GHABCounter, 1) + 1;
if (ihab <= 0) { // this is clearly a pathological program.
free(anchor->pres_spaces);
free(anchor->windows);
free(anchor);
return NULLHANDLE;
}
const HAB hab = (HAB) ihab;
anchor->hab = hab;
posttib->anchor_block = anchor;
return hab;
} // WinInitialize
static MRESULT sendMessage(Window *win, ULONG msg, MPARAM mp1, MPARAM mp2)
{
PFNWP winproc = win->window_proc;
if (!winproc) {
winproc = WinDefWindowProc;
}
Jul 10, 2018
Jul 10, 2018
430
TRACE_EVENT("EVENT SEND { hwnd=%u, msg=%u (%s), mp1=%p, mp2=%p, proc=%p }", (uint) win->hwnd, (uint) msg, messageName(msg), mp1, mp2, winproc);
431
432
433
434
435
436
437
438
439
440
441
442
443
444
return winproc(win->hwnd, msg, mp1, mp2);
} // sendMessage
static BOOL destroyMessageQueue(AnchorBlock *anchor)
{
MessageQueueItem *next = NULL;
for (MessageQueueItem *i = anchor->message_queue.head; i; i = next) {
next = i->next;
free(i);
}
for (MessageQueueItem *i = anchor->message_queue.free_pool; i; i = next) {
next = i->next;
free(i);
}
Jul 10, 2018
Jul 10, 2018
445
TRACE_EVENT("HMQ %u destroyed", (uint) anchor->message_queue.hmq);
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
memset(&anchor->message_queue, '\0', sizeof (MessageQueue));
return TRUE;
} // destroyMessageQueue
static void destroyWindow(AnchorBlock *anchor, Window *win)
{
if (!win || (win->hwnd == NULLHANDLE)) {
return;
}
// !!! FIXME: hide window first, and optionally send WM_ACTIVATE message if losing focus.
// !!! FIXME: send WM_RENDERALLFMTS if we are clipboard owner and unrendered formats are in the clipboard.
// send WM_DESTROY. This is a simple notification, and cannot prevent destruction.
sendMessage(win, WM_DESTROY, 0, 0);
// Next, destroy all child windows.
Window *next = NULL;
for (Window *i = win->children; i != NULL; i = next) {
next = i->sibling;
destroyWindow(anchor, i);
}
// !!! FIXME: see notes in SDK reference about disassociating/destroying HPS objects.
// if this is a heavyweight window, destroy those parts.
if (win->parent == &desktop_window) {
SDL_DestroyTexture(win->heavy.texture);
SDL_DestroyRenderer(win->heavy.renderer);
SDL_DestroyWindow(win->heavy.window);
}
if (win->parent->children == win) {
win->parent->children = win->sibling;
} else {
for (Window *i = win->parent->children; i != NULL; i = i->sibling) {
assert(i != win);
if (i->sibling == win) {
i->sibling = win->sibling;
break;
}
}
}
free(win->text);
free(win->data);
memset(win, '\0', sizeof (*win));
} // destroyWindow
static void destroyPresentationSpace(AnchorBlock *anchor, PresentationSpace *ps)
{
if (!ps || (ps->hps == NULLHANDLE)) {
return;
}
FIXME("this will be more complicated later");
memset(ps, '\0', sizeof (*ps));
} // destroyPresentationSpace
BOOL WinTerminate(HAB hab)
{
Jul 10, 2018
Jul 10, 2018
505
TRACE_NATIVE("WinTerminate(%u)", (uint) hab);
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
AnchorBlock *anchor = getAnchorBlock(hab);
if (!anchor) {
return FALSE;
}
for (size_t i = 0; i < anchor->pres_spaces_array_len; i++) {
destroyPresentationSpace(anchor, &anchor->pres_spaces[i]);
}
free(anchor->pres_spaces);
for (size_t i = 0; i < anchor->windows_array_len; i++) {
destroyWindow(anchor, &anchor->windows[i]);
}
free(anchor->windows);
WindowClass *winclassnext = NULL;
for (WindowClass *i = anchor->registered_classes; i; i = winclassnext) {
winclassnext = i->next;
free(i->name);
free(i);
}
if (anchor->message_queue.hmq != NULLHANDLE) {
destroyMessageQueue(anchor);
}
free(anchor);
Feb 28, 2018
Feb 28, 2018
533
LX_GETPOSTTIB()->anchor_block = NULL;
534
535
536
537
538
539
540
541
SDL_Quit(); // !!! FIXME: does this reference count?
return TRUE;
} // WinTerminate
ERRORID WinGetLastError(HAB hab)
{
Jul 10, 2018
Jul 10, 2018
542
TRACE_NATIVE("WinGetLastError(%u)", (uint) hab);
543
544
545
546
547
548
549
550
551
552
AnchorBlock *anchor = getAnchorBlock(hab);
if (!anchor) {
return PMERR_INVALID_HAB;
}
const ERRORID retval = anchor->last_error;
SET_WIN_ERROR_AND_RETURN(anchor, NO_ERROR, retval);
} // WinGetLastError
HMQ WinCreateMsgQueue(HAB hab, LONG cmsg)
{
Jul 10, 2018
Jul 10, 2018
553
TRACE_NATIVE("WinCreateMsgQueue(%u, %d)", (uint) hab, (int) cmsg);
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
AnchorBlock *anchor = getAnchorBlock(hab);
if (!anchor) {
return NULLHANDLE;
} else if (anchor->message_queue.hmq != NULLHANDLE) {
SET_WIN_ERROR_AND_RETURN(anchor, PMERR_MSG_QUEUE_ALREADY_EXISTS, NULLHANDLE);
}
// as of OS/2 Warp 4.0, cmsg is ignored (the queue is always dynamically allocated), so we ignore it too.
assert(anchor->message_queue.head == NULL);
assert(anchor->message_queue.tail == NULL);
assert(anchor->message_queue.free_pool == NULL);
const int ihmq = SDL_AtomicAdd(&GHMQCounter, 1) + 1;
if (ihmq <= 0) { // this is clearly a pathological program.
return NULLHANDLE;
}
const HMQ hmq = (HMQ) ihmq;
anchor->message_queue.hmq = hmq;
Jul 10, 2018
Jul 10, 2018
574
TRACE_EVENT("HMQ %u created", (uint) hmq);
575
576
577
578
579
return hmq;
} // WinCreateMsgQueue
static ERRORID postMessage(AnchorBlock *anchor, const QMSG *qmsg)
{
Jul 10, 2018
Jul 10, 2018
580
TRACE_EVENT("EVENT POST { hwnd=%u, msg=%u (%s), mp1=%p, mp2=%p, time=%u, ptl={%d,%d}, reserved=%u }", (uint) qmsg->hwnd, (uint) qmsg->msg, messageName(qmsg->msg), qmsg->mp1, qmsg->mp2, (uint) qmsg->time, (int) qmsg->ptl.x, (int) qmsg->ptl.y, (uint) qmsg->reserved);
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
MessageQueueItem *item = anchor->message_queue.free_pool;
if (item) {
anchor->message_queue.free_pool = item->next;
} else {
item = (MessageQueueItem *) malloc(sizeof (*item));
if (item == NULL) {
return FALSE;
}
}
memcpy(&item->qmsg, qmsg, sizeof (*qmsg));
item->next = NULL;
assert(!anchor->message_queue.head == !anchor->message_queue.tail);
if (anchor->message_queue.tail) {
anchor->message_queue.tail->next = item;
} else {
anchor->message_queue.head = item;
}
anchor->message_queue.tail = item;
return TRUE;
} // postMessage
static inline Window *getWindowFromSDLWindow(SDL_Window *sdlwin)
{
return (Window *) sdlwin ? SDL_GetWindowData(sdlwin, WINDATA_WINDOWPTR_NAME) : NULL;
} // getWindowFromSDLWindow
#if 0
static Window *hittestWindow(SDL_Window *sdlwin, int x, int y)
{
if (!sdlwin) {
return NULL;
}
Window *win = getWindowFromSDLWindow(sdlwin);
if (!win) {
return NULL;
}
// (win) is currently the oldest parent, which is associated with the
// SDL_Window. We need to see which lightweight child window, if any,
// this event was meant for.
// y coordinates are flipped on OS/2. zero is the bottom of the window.
y = sdlwin->h - y;
// !!! FIXME WRITE ME
for (Window *i =
if (win->class_style & CS_HITTEST) {
sendMessage(win, WM_HITTEST,
}
} // findWindowFromSDL
#endif
static BOOL postTimestampedMsg(AnchorBlock *anchor, HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2, ULONG ticks)
{
const QMSG qmsg = { hwnd, msg, mp1, mp2, currentSystemTicks(), { 0, 0 }, 0 };
return postMessage(anchor, &qmsg);
} // postTimestampedMsg
#if 0
static BOOL postSimpleMsg(AnchorBlock *anchor, HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
{
return postTimestampedMsg(anchor, hwnd, msg, mp1, mp2, currentSystemTicks());
} // postSimpleMsg
#endif
static BOOL postExposedMessages(AnchorBlock *anchor, Window *win, const ULONG ticks)
{
FIXME("this should WinInvalidateRect() the windows, not send paint messages");
FIXME("this is a recursive hell"); // we need to start at the end of the sibling list, which is the bottom of the stack.
BOOL retval = FALSE;
if (win) {
retval |= postExposedMessages(anchor, win->sibling, ticks);
if (win->style & WS_VISIBLE) {
FIXME("for CS_SYNCPAINT classes, this should send the WM_PAINT message right now instead of posting");
retval |= postTimestampedMsg(anchor, win->hwnd, WM_PAINT, 0, 0, ticks);
retval |= postExposedMessages(anchor, win->children, ticks);
}
}
return retval;
} // postExposedMessages
static const char *sdlEventName(const SDL_EventType ev)
{
switch (ev) {
#define EVCASE(m) case m: return #m
EVCASE(SDL_QUIT);
EVCASE(SDL_APP_TERMINATING);
EVCASE(SDL_APP_LOWMEMORY);
EVCASE(SDL_APP_WILLENTERBACKGROUND);
EVCASE(SDL_APP_DIDENTERBACKGROUND);
EVCASE(SDL_APP_WILLENTERFOREGROUND);
EVCASE(SDL_APP_DIDENTERFOREGROUND);
EVCASE(SDL_WINDOWEVENT);
EVCASE(SDL_SYSWMEVENT);
EVCASE(SDL_KEYDOWN);
EVCASE(SDL_KEYUP);
EVCASE(SDL_TEXTEDITING);
EVCASE(SDL_TEXTINPUT);
EVCASE(SDL_KEYMAPCHANGED);
EVCASE(SDL_MOUSEMOTION);
EVCASE(SDL_MOUSEBUTTONDOWN);
EVCASE(SDL_MOUSEBUTTONUP);
EVCASE(SDL_MOUSEWHEEL);
EVCASE(SDL_JOYAXISMOTION);
EVCASE(SDL_JOYBALLMOTION);
EVCASE(SDL_JOYHATMOTION);
EVCASE(SDL_JOYBUTTONDOWN);
EVCASE(SDL_JOYBUTTONUP);
EVCASE(SDL_JOYDEVICEADDED);
EVCASE(SDL_JOYDEVICEREMOVED);
EVCASE(SDL_CONTROLLERAXISMOTION);
EVCASE(SDL_CONTROLLERBUTTONDOWN);
EVCASE(SDL_CONTROLLERBUTTONUP);
EVCASE(SDL_CONTROLLERDEVICEADDED);
EVCASE(SDL_CONTROLLERDEVICEREMOVED);
EVCASE(SDL_CONTROLLERDEVICEREMAPPED);
EVCASE(SDL_FINGERDOWN);
EVCASE(SDL_FINGERUP);
EVCASE(SDL_FINGERMOTION);
EVCASE(SDL_DOLLARGESTURE);
EVCASE(SDL_DOLLARRECORD);
EVCASE(SDL_MULTIGESTURE);
EVCASE(SDL_CLIPBOARDUPDATE);
EVCASE(SDL_DROPFILE);
EVCASE(SDL_DROPTEXT);
EVCASE(SDL_DROPBEGIN);
EVCASE(SDL_DROPCOMPLETE);
EVCASE(SDL_AUDIODEVICEADDED);
EVCASE(SDL_AUDIODEVICEREMOVED);
EVCASE(SDL_RENDER_TARGETS_RESET);
EVCASE(SDL_RENDER_DEVICE_RESET);
EVCASE(SDL_USEREVENT);
#undef EVCASE
default: break;
}
return "???";
} // sdlEventName
static const char *sdlWindowEventName(const SDL_WindowEventID ev)
{
switch (ev) {
#define EVCASE(m) case m: return #m
EVCASE(SDL_WINDOWEVENT_SHOWN);
EVCASE(SDL_WINDOWEVENT_HIDDEN);
EVCASE(SDL_WINDOWEVENT_EXPOSED);
EVCASE(SDL_WINDOWEVENT_MOVED);
EVCASE(SDL_WINDOWEVENT_RESIZED);
EVCASE(SDL_WINDOWEVENT_SIZE_CHANGED);
EVCASE(SDL_WINDOWEVENT_MINIMIZED);
EVCASE(SDL_WINDOWEVENT_MAXIMIZED);
EVCASE(SDL_WINDOWEVENT_RESTORED);
EVCASE(SDL_WINDOWEVENT_ENTER);
EVCASE(SDL_WINDOWEVENT_LEAVE);
EVCASE(SDL_WINDOWEVENT_FOCUS_GAINED);
EVCASE(SDL_WINDOWEVENT_FOCUS_LOST);
EVCASE(SDL_WINDOWEVENT_CLOSE);
EVCASE(SDL_WINDOWEVENT_TAKE_FOCUS);
EVCASE(SDL_WINDOWEVENT_HIT_TEST);
#undef EVCASE
default: break;
}
return "???";
} // sdlWindowEventName
static BOOL processSDLEvent(AnchorBlock *anchor, const SDL_Event *sdlevent)
{
// !!! FIXME: we just drop events if we run out of memory, etc.
// !!! FIXME: QMSG::time appears to be milliseconds since the system
// !!! FIXME: booted (presumably rolling over every 38 days). SDL's
// !!! FIXME: event timestamps (and SDL_GetTicks()) are milliseconds
// !!! FIXME: since SDL_Init(), which is more or less since app startup.
// !!! FIXME: We might need a way to map these, so they're consistent
// !!! FIXME: across processes.
// !!! FIXME: Also, it's well known that SDL event timestamps are when
// !!! FIXME: SDL gets the event from the OS, not the timestamp of when
// !!! FIXME: the OS generated it, which might be a problem.
/*
sdfsdf
HWND hwnd;
ULONG msg;
MPARAM mp1;
MPARAM mp2;
ULONG time;
POINTL ptl;
ULONG reserved;
sdfsdf
*/
//SDL_GetWindowFromID(window) sdfsdf
const ULONG ticks = (ULONG) sdlevent->common.timestamp;
Jul 10, 2018
Jul 10, 2018
783
TRACE_EVENT("EVENT SDL { type=0x%X (%s) }", (uint) sdlevent->type, sdlEventName(sdlevent->type));
784
785
786
787
788
789
switch (sdlevent->type) {
case SDL_QUIT:
return postTimestampedMsg(anchor, NULLHANDLE, WM_QUIT, 0, 0, ticks);
case SDL_WINDOWEVENT:
Jul 10, 2018
Jul 10, 2018
790
TRACE_EVENT("EVENT SDL WINDOW { type=0x%X (%s) }", (uint) sdlevent->window.event, sdlWindowEventName(sdlevent->window.event));
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
switch (sdlevent->window.event) {
case SDL_WINDOWEVENT_SHOWN:
case SDL_WINDOWEVENT_EXPOSED: {
// maybe this doesn't matter in a world of compositing window managers, but it would be nice to completely avoid sending WM_PAINT to child windows that otherwise don't apply.
// This only applies for heavyweight windows the OS is marking as exposed, not OS/2 apps marking OS/2 HWNDs as invalid for various reasons.
FIXME("SDL doesn't report invalid subregions, just that a window needs repainting");
Window *win = getWindowFromSDLWindow(SDL_GetWindowFromID(sdlevent->window.windowID));
if (!win) {
return FALSE;
}
return postExposedMessages(anchor, win, ticks);
}
default: break; // not (currently) supported.
}
return FALSE;
#if 0
case SDL_WINDOWEVENT_SHOWN:
case SDL_WINDOWEVENT_HIDDEN:
case SDL_WINDOWEVENT_MOVED, /**< Window has been moved to data1, data2
*/
case SDL_WINDOWEVENT_RESIZED, /**< Window has been resized to data1xdata2 */
case SDL_WINDOWEVENT_SIZE_CHANGED, /**< The window size has changed, either as
a result of an API call or through the
system or user changing the window size. */
case SDL_WINDOWEVENT_MINIMIZED, /**< Window has been minimized */
case SDL_WINDOWEVENT_MAXIMIZED, /**< Window has been maximized */
case SDL_WINDOWEVENT_RESTORED, /**< Window has been restored to normal size
and position */
case SDL_WINDOWEVENT_ENTER, /**< Window has gained mouse focus */
case SDL_WINDOWEVENT_LEAVE, /**< Window has lost mouse focus */
case SDL_WINDOWEVENT_FOCUS_GAINED, /**< Window has gained keyboard focus */
case SDL_WINDOWEVENT_FOCUS_LOST, /**< Window has lost keyboard focus */
case SDL_WINDOWEVENT_CLOSE, /**< The window manager requests that the window be closed */
case SDL_WINDOWEVENT_TAKE_FOCUS, /**< Window is being offered a focus (should SetWindowInputFocus() on itself or a subwindow, or ignore) */
//case SDL_WINDOWEVENT_HIT_TEST /**< Window had a hit test that wasn't SDL_HITTEST_NORMAL. */
// SDL_MOUSEMOTION = 0x400, /**< Mouse moved */
// SDL_MOUSEBUTTONDOWN, /**< Mouse button pressed */
// SDL_MOUSEBUTTONUP, /**< Mouse button released */
// SDL_MOUSEWHEEL, /**< Mouse wheel motion */
// SDL_KEYDOWN = 0x300, /**< Key pressed */
// SDL_KEYUP, /**< Key released */
// SDL_TEXTINPUT, /**< Keyboard text input */
// SDL_CLIPBOARDUPDATE = 0x900, /**< The clipboard changed */
// SDL_DROPFILE = 0x1000, /**< The system requests a file open */
// SDL_DROPTEXT, /**< text/plain drag-and-drop event */
// SDL_DROPBEGIN, /**< A new set of drops is beginning (NULL filename) */
// SDL_DROPCOMPLETE, /**< Current set of drops is now complete (NULL filename) */
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
if (sdlevent->button.clicks == 1) {}
#endif
default: return FALSE; // not (currently) supported.
}
assert(!"shouldn't hit this");
return TRUE;
} // processSDLEvent
static void pumpEvents(AnchorBlock *anchor)
{
SDL_Event sdlevent;
BOOL queued = FALSE;
// run through anything pending. If we don't post anything to the OS/2
// event queue, we need to block until we do, though, so then we'll use
// SDL_WaitEvent() in hopes that someday it's fully waitable.
while (SDL_PollEvent(&sdlevent)) {
queued |= processSDLEvent(anchor, &sdlevent);
}
while (!queued) { // block until stuff shows up.
while (!SDL_WaitEvent(&sdlevent)) {
SDL_Delay(10); // dunno what else to do here...
}
queued |= processSDLEvent(anchor, &sdlevent);
while (SDL_PollEvent(&sdlevent)) { // catch anything else pending.
queued |= processSDLEvent(anchor, &sdlevent);
}
}
} // waitForMessage
static BOOL skipMessage(const PQMSG qmsg, HWND hwndFilter, ULONG msgFilterFirst, ULONG msgFilterLast)
{
if (hwndFilter && (qmsg->hwnd != hwndFilter)) {
return TRUE;
} else if (msgFilterFirst || msgFilterLast) {
const ULONG msg = qmsg->msg;
if (msgFilterFirst > msgFilterLast) { // reject if between first and last.
if ((msg >= msgFilterLast) && (msg <= msgFilterFirst)) return TRUE;
} else { // reject if not between first and last.
if ((msg < msgFilterFirst) || (msg > msgFilterLast)) return TRUE;
}
}
return FALSE;
} // skipMessage
BOOL WinGetMsg(HAB hab, PQMSG pqmsg, HWND hwndFilter, ULONG msgFilterFirst, ULONG msgFilterLast)
{
Jul 10, 2018
Jul 10, 2018
900
TRACE_NATIVE("WinGetMsg(%u, %p, %u, %u, %u)", (uint) hab, pqmsg, (uint) hwndFilter, (uint) msgFilterFirst, (uint) msgFilterLast);
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
const BOOL bIsFiltering = (hwndFilter || msgFilterFirst || msgFilterLast);
AnchorBlock *anchor = getAnchorBlock(hab);
if (!anchor) {
return FALSE;
} else if (hwndFilter) {
FIXME("fail with PMERR_INVALID_HWND if this is a bogus HWND.");
}
while (1) {
MessageQueueItem *prev = NULL;
MessageQueueItem *i = anchor->message_queue.head;
if (bIsFiltering) {
for (; i != NULL; prev = i, i = i->next) {
if (!skipMessage(&i->qmsg, hwndFilter, msgFilterFirst, msgFilterLast)) {
break;
}
}
}
// Found an event? Remove from queue, return that info.
if (i != NULL) {
memcpy(pqmsg, &i->qmsg, sizeof (QMSG));
Jul 10, 2018
Jul 10, 2018
924
TRACE_EVENT("EVENT GET { hwnd=%u, msg=%u (%s), mp1=%p, mp2=%p, time=%u, ptl={%d,%d}, reserved=%u }", (uint) pqmsg->hwnd, (uint) pqmsg->msg, messageName(pqmsg->msg), pqmsg->mp1, pqmsg->mp2, (uint) pqmsg->time, (int) pqmsg->ptl.x, (int) pqmsg->ptl.y, (uint) pqmsg->reserved);
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
if (prev) {
prev->next = i->next;
} else {
anchor->message_queue.head = i->next;
}
if (!anchor->message_queue.head) {
assert(anchor->message_queue.tail == i);
anchor->message_queue.tail = NULL;
}
i->next = anchor->message_queue.free_pool;
anchor->message_queue.free_pool = i;
return (pqmsg->msg != WM_QUIT) ? TRUE : FALSE;
}
pumpEvents(anchor); // may block.
}
assert(!"shouldn't hit this.");
return TRUE; // shouldn't hit this.
} // WinGetMsg
MRESULT WinDispatchMsg(HAB hab, PQMSG pqmsg)
{
Jul 10, 2018
Jul 10, 2018
952
TRACE_NATIVE("WinDispatchMsg(%u, %p)", (uint) hab, pqmsg);
953
954
955
956
957
958
959
960
961
962
963
964
AnchorBlock *anchor = getAnchorBlock(hab);
if (anchor) {
Window *win = getWindowFromHWND(anchor, pqmsg->hwnd);
if (win) {
return sendMessage(win, pqmsg->msg, pqmsg->mp1, pqmsg->mp2);
}
}
return 0;
} // WinDispatchMsg
BOOL WinDestroyMsgQueue(HMQ hmq)
{
Jul 10, 2018
Jul 10, 2018
965
TRACE_NATIVE("WinDestroyMsgQueue(%u)", (uint) hmq);
966
967
968
969
970
971
972
973
974
975
976
977
AnchorBlock *anchor = getAnchorBlockNoHAB();
if (!anchor) {
return FALSE;
} else if ((hmq == NULLHANDLE) || (anchor->message_queue.hmq != hmq)) {
SET_WIN_ERROR_AND_RETURN(anchor, PMERR_INVALID_HMQ, FALSE);
}
return destroyMessageQueue(anchor);
} // WinDestroyMsgQueue
BOOL WinRegisterClass(HAB hab, PSZ pszClassName, PFNWP pfnWndProc, ULONG flStyle, ULONG cbWindowData)
{
Jul 10, 2018
Jul 10, 2018
978
TRACE_NATIVE("WinRegisterClass(%u, '%s', %p, %u, %u)", (uint) hab, pszClassName, pfnWndProc, (uint) flStyle, (uint) cbWindowData);
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
AnchorBlock *anchor = getAnchorBlock(hab);
if (!anchor) {
return FALSE;
}
WindowClass *winclass = findRegisteredClass(anchor, pszClassName);
if (!winclass) { // PM docs say this call can replace existing class.
winclass = (WindowClass *) malloc(sizeof (WindowClass));
if (!winclass) {
SET_WIN_ERROR_AND_RETURN(anchor, PMERR_HEAP_OUT_OF_MEMORY, FALSE);
} else if ((winclass->name = strdup(pszClassName)) == NULL) {
free(winclass);
SET_WIN_ERROR_AND_RETURN(anchor, PMERR_HEAP_OUT_OF_MEMORY, FALSE);
}
winclass->next = anchor->registered_classes;
anchor->registered_classes = winclass;
}
winclass->window_proc = pfnWndProc;
winclass->style = flStyle;
winclass->data_len = cbWindowData;