-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
SDL_windowsevents.c
2050 lines (1796 loc) · 75 KB
/
SDL_windowsevents.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
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
783
784
785
786
787
788
789
790
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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
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
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Simple DirectMedia Layer
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
#ifdef SDL_VIDEO_DRIVER_WINDOWS
#include "SDL_windowsvideo.h"
#include "SDL_windowsshape.h"
#include "SDL_vkeys.h"
#include "../../events/SDL_events_c.h"
#include "../../events/SDL_touch_c.h"
#include "../../events/scancodes_windows.h"
#include "../../main/SDL_main_callbacks.h"
/* Dropfile support */
#include <shellapi.h>
/* For GET_X_LPARAM, GET_Y_LPARAM. */
#include <windowsx.h>
/* For WM_TABLET_QUERYSYSTEMGESTURESTATUS et. al. */
#ifdef HAVE_TPCSHRD_H
#include <tpcshrd.h>
#endif /* HAVE_TPCSHRD_H */
/* #define WMMSG_DEBUG */
#ifdef WMMSG_DEBUG
#include <stdio.h>
#include "wmmsg.h"
#endif
#ifdef __GDK__
#include "../../core/gdk/SDL_gdk.h"
#endif
/* #define HIGHDPI_DEBUG */
/* Masks for processing the windows KEYDOWN and KEYUP messages */
#define REPEATED_KEYMASK (1 << 30)
#define EXTENDED_KEYMASK (1 << 24)
#define VK_ENTER 10 /* Keypad Enter ... no VKEY defined? */
#ifndef VK_OEM_NEC_EQUAL
#define VK_OEM_NEC_EQUAL 0x92
#endif
/* Make sure XBUTTON stuff is defined that isn't in older Platform SDKs... */
#ifndef WM_XBUTTONDOWN
#define WM_XBUTTONDOWN 0x020B
#endif
#ifndef WM_XBUTTONUP
#define WM_XBUTTONUP 0x020C
#endif
#ifndef GET_XBUTTON_WPARAM
#define GET_XBUTTON_WPARAM(w) (HIWORD(w))
#endif
#ifndef WM_INPUT
#define WM_INPUT 0x00ff
#endif
#ifndef WM_TOUCH
#define WM_TOUCH 0x0240
#endif
#ifndef WM_MOUSEHWHEEL
#define WM_MOUSEHWHEEL 0x020E
#endif
#ifndef WM_POINTERUPDATE
#define WM_POINTERUPDATE 0x0245
#endif
#ifndef WM_UNICHAR
#define WM_UNICHAR 0x0109
#endif
#ifndef WM_DPICHANGED
#define WM_DPICHANGED 0x02E0
#endif
#ifndef WM_GETDPISCALEDSIZE
#define WM_GETDPISCALEDSIZE 0x02E4
#endif
#ifndef TOUCHEVENTF_PEN
#define TOUCHEVENTF_PEN 0x0040
#endif
#ifndef IS_HIGH_SURROGATE
#define IS_HIGH_SURROGATE(x) (((x) >= 0xd800) && ((x) <= 0xdbff))
#endif
#ifndef IS_LOW_SURROGATE
#define IS_LOW_SURROGATE(x) (((x) >= 0xdc00) && ((x) <= 0xdfff))
#endif
#ifndef IS_SURROGATE_PAIR
#define IS_SURROGATE_PAIR(h, l) (IS_HIGH_SURROGATE(h) && IS_LOW_SURROGATE(l))
#endif
#ifndef USER_TIMER_MINIMUM
#define USER_TIMER_MINIMUM 0x0000000A
#endif
/* Used to compare Windows message timestamps */
#define SDL_TICKS_PASSED(A, B) ((Sint32)((B) - (A)) <= 0)
static SDL_bool SDL_processing_messages;
static DWORD message_tick;
static Uint64 timestamp_offset;
static void WIN_SetMessageTick(DWORD tick)
{
if (message_tick) {
if (tick < message_tick && timestamp_offset) {
/* The tick counter rolled over, bump our offset */
timestamp_offset += SDL_MS_TO_NS(0x100000000LL);
}
}
message_tick = tick;
}
static Uint64 WIN_GetEventTimestamp()
{
Uint64 timestamp, now;
if (!SDL_processing_messages) {
/* message_tick isn't valid, just use the current time */
return 0;
}
now = SDL_GetTicksNS();
timestamp = SDL_MS_TO_NS(message_tick);
if (!timestamp_offset) {
timestamp_offset = (now - timestamp);
}
timestamp += timestamp_offset;
if (timestamp > now) {
timestamp_offset -= (timestamp - now);
timestamp = now;
}
return timestamp;
}
static SDL_Scancode VKeytoScancodeFallback(WPARAM vkey)
{
switch (vkey) {
case VK_LEFT:
return SDL_SCANCODE_LEFT;
case VK_UP:
return SDL_SCANCODE_UP;
case VK_RIGHT:
return SDL_SCANCODE_RIGHT;
case VK_DOWN:
return SDL_SCANCODE_DOWN;
default:
return SDL_SCANCODE_UNKNOWN;
}
}
static SDL_Scancode VKeytoScancode(WPARAM vkey)
{
switch (vkey) {
case VK_MODECHANGE:
return SDL_SCANCODE_MODE;
case VK_SELECT:
return SDL_SCANCODE_SELECT;
case VK_EXECUTE:
return SDL_SCANCODE_EXECUTE;
case VK_HELP:
return SDL_SCANCODE_HELP;
case VK_PAUSE:
return SDL_SCANCODE_PAUSE;
case VK_NUMLOCK:
return SDL_SCANCODE_NUMLOCKCLEAR;
case VK_F13:
return SDL_SCANCODE_F13;
case VK_F14:
return SDL_SCANCODE_F14;
case VK_F15:
return SDL_SCANCODE_F15;
case VK_F16:
return SDL_SCANCODE_F16;
case VK_F17:
return SDL_SCANCODE_F17;
case VK_F18:
return SDL_SCANCODE_F18;
case VK_F19:
return SDL_SCANCODE_F19;
case VK_F20:
return SDL_SCANCODE_F20;
case VK_F21:
return SDL_SCANCODE_F21;
case VK_F22:
return SDL_SCANCODE_F22;
case VK_F23:
return SDL_SCANCODE_F23;
case VK_F24:
return SDL_SCANCODE_F24;
case VK_OEM_NEC_EQUAL:
return SDL_SCANCODE_KP_EQUALS;
case VK_BROWSER_BACK:
return SDL_SCANCODE_AC_BACK;
case VK_BROWSER_FORWARD:
return SDL_SCANCODE_AC_FORWARD;
case VK_BROWSER_REFRESH:
return SDL_SCANCODE_AC_REFRESH;
case VK_BROWSER_STOP:
return SDL_SCANCODE_AC_STOP;
case VK_BROWSER_SEARCH:
return SDL_SCANCODE_AC_SEARCH;
case VK_BROWSER_FAVORITES:
return SDL_SCANCODE_AC_BOOKMARKS;
case VK_BROWSER_HOME:
return SDL_SCANCODE_AC_HOME;
case VK_VOLUME_MUTE:
return SDL_SCANCODE_AUDIOMUTE;
case VK_VOLUME_DOWN:
return SDL_SCANCODE_VOLUMEDOWN;
case VK_VOLUME_UP:
return SDL_SCANCODE_VOLUMEUP;
case VK_MEDIA_NEXT_TRACK:
return SDL_SCANCODE_AUDIONEXT;
case VK_MEDIA_PREV_TRACK:
return SDL_SCANCODE_AUDIOPREV;
case VK_MEDIA_STOP:
return SDL_SCANCODE_AUDIOSTOP;
case VK_MEDIA_PLAY_PAUSE:
return SDL_SCANCODE_AUDIOPLAY;
case VK_LAUNCH_MAIL:
return SDL_SCANCODE_MAIL;
case VK_LAUNCH_MEDIA_SELECT:
return SDL_SCANCODE_MEDIASELECT;
case VK_OEM_102:
return SDL_SCANCODE_NONUSBACKSLASH;
case VK_ATTN:
return SDL_SCANCODE_SYSREQ;
case VK_CRSEL:
return SDL_SCANCODE_CRSEL;
case VK_EXSEL:
return SDL_SCANCODE_EXSEL;
case VK_OEM_CLEAR:
return SDL_SCANCODE_CLEAR;
case VK_LAUNCH_APP1:
return SDL_SCANCODE_APP1;
case VK_LAUNCH_APP2:
return SDL_SCANCODE_APP2;
default:
return SDL_SCANCODE_UNKNOWN;
}
}
static SDL_Scancode WindowsScanCodeToSDLScanCode(LPARAM lParam, WPARAM wParam)
{
SDL_Scancode code;
int nScanCode = (lParam >> 16) & 0xFF;
SDL_bool bIsExtended = (lParam & (1 << 24)) != 0;
code = VKeytoScancode(wParam);
if (code == SDL_SCANCODE_UNKNOWN && nScanCode <= 127) {
code = windows_scancode_table[nScanCode];
if (bIsExtended) {
switch (code) {
case SDL_SCANCODE_RETURN:
code = SDL_SCANCODE_KP_ENTER;
break;
case SDL_SCANCODE_LALT:
code = SDL_SCANCODE_RALT;
break;
case SDL_SCANCODE_LCTRL:
code = SDL_SCANCODE_RCTRL;
break;
case SDL_SCANCODE_SLASH:
code = SDL_SCANCODE_KP_DIVIDE;
break;
case SDL_SCANCODE_CAPSLOCK:
code = SDL_SCANCODE_KP_PLUS;
break;
default:
break;
}
} else {
switch (code) {
case SDL_SCANCODE_HOME:
code = SDL_SCANCODE_KP_7;
break;
case SDL_SCANCODE_UP:
code = SDL_SCANCODE_KP_8;
break;
case SDL_SCANCODE_PAGEUP:
code = SDL_SCANCODE_KP_9;
break;
case SDL_SCANCODE_LEFT:
code = SDL_SCANCODE_KP_4;
break;
case SDL_SCANCODE_RIGHT:
code = SDL_SCANCODE_KP_6;
break;
case SDL_SCANCODE_END:
code = SDL_SCANCODE_KP_1;
break;
case SDL_SCANCODE_DOWN:
code = SDL_SCANCODE_KP_2;
break;
case SDL_SCANCODE_PAGEDOWN:
code = SDL_SCANCODE_KP_3;
break;
case SDL_SCANCODE_INSERT:
code = SDL_SCANCODE_KP_0;
break;
case SDL_SCANCODE_DELETE:
code = SDL_SCANCODE_KP_PERIOD;
break;
case SDL_SCANCODE_PRINTSCREEN:
code = SDL_SCANCODE_KP_MULTIPLY;
break;
default:
break;
}
}
}
/* The on-screen keyboard can generate VK_LEFT and VK_RIGHT events without a scancode
* value set, however we cannot simply map these in VKeytoScancode() or we will be
* incorrectly handling the arrow keys on the number pad when NumLock is disabled
* (which also generate VK_LEFT, VK_RIGHT, etc in that scenario). Instead, we'll only
* map them if none of the above special number pad mappings applied. */
if (code == SDL_SCANCODE_UNKNOWN) {
code = VKeytoScancodeFallback(wParam);
}
return code;
}
#if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
static SDL_bool WIN_ShouldIgnoreFocusClick(SDL_WindowData *data)
{
return !SDL_WINDOW_IS_POPUP(data->window) &&
!SDL_GetHintBoolean(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, SDL_FALSE);
}
static void WIN_CheckWParamMouseButton(SDL_bool bwParamMousePressed, Uint32 mouseFlags, SDL_bool bSwapButtons, SDL_WindowData *data, Uint8 button, SDL_MouseID mouseID)
{
if (bSwapButtons) {
if (button == SDL_BUTTON_LEFT) {
button = SDL_BUTTON_RIGHT;
} else if (button == SDL_BUTTON_RIGHT) {
button = SDL_BUTTON_LEFT;
}
}
if (data->focus_click_pending & SDL_BUTTON(button)) {
/* Ignore the button click for activation */
if (!bwParamMousePressed) {
data->focus_click_pending &= ~SDL_BUTTON(button);
WIN_UpdateClipCursor(data->window);
}
if (WIN_ShouldIgnoreFocusClick(data)) {
return;
}
}
if (bwParamMousePressed && !(mouseFlags & SDL_BUTTON(button))) {
SDL_SendMouseButton(WIN_GetEventTimestamp(), data->window, mouseID, SDL_PRESSED, button);
} else if (!bwParamMousePressed && (mouseFlags & SDL_BUTTON(button))) {
SDL_SendMouseButton(WIN_GetEventTimestamp(), data->window, mouseID, SDL_RELEASED, button);
}
}
/*
* Some windows systems fail to send a WM_LBUTTONDOWN sometimes, but each mouse move contains the current button state also
* so this function reconciles our view of the world with the current buttons reported by windows
*/
static void WIN_CheckWParamMouseButtons(WPARAM wParam, SDL_WindowData *data, SDL_MouseID mouseID)
{
if (wParam != data->mouse_button_flags) {
Uint32 mouseFlags = SDL_GetMouseState(NULL, NULL);
/* WM_LBUTTONDOWN and friends handle button swapping for us. No need to check SM_SWAPBUTTON here. */
WIN_CheckWParamMouseButton((wParam & MK_LBUTTON), mouseFlags, SDL_FALSE, data, SDL_BUTTON_LEFT, mouseID);
WIN_CheckWParamMouseButton((wParam & MK_MBUTTON), mouseFlags, SDL_FALSE, data, SDL_BUTTON_MIDDLE, mouseID);
WIN_CheckWParamMouseButton((wParam & MK_RBUTTON), mouseFlags, SDL_FALSE, data, SDL_BUTTON_RIGHT, mouseID);
WIN_CheckWParamMouseButton((wParam & MK_XBUTTON1), mouseFlags, SDL_FALSE, data, SDL_BUTTON_X1, mouseID);
WIN_CheckWParamMouseButton((wParam & MK_XBUTTON2), mouseFlags, SDL_FALSE, data, SDL_BUTTON_X2, mouseID);
data->mouse_button_flags = wParam;
}
}
static void WIN_CheckRawMouseButtons(ULONG rawButtons, SDL_WindowData *data, SDL_MouseID mouseID)
{
// Add a flag to distinguish raw mouse buttons from wParam above
rawButtons |= 0x8000000;
if (rawButtons != data->mouse_button_flags) {
Uint32 mouseFlags = SDL_GetMouseState(NULL, NULL);
SDL_bool swapButtons = GetSystemMetrics(SM_SWAPBUTTON) != 0;
if (rawButtons & RI_MOUSE_BUTTON_1_DOWN) {
WIN_CheckWParamMouseButton((rawButtons & RI_MOUSE_BUTTON_1_DOWN), mouseFlags, swapButtons, data, SDL_BUTTON_LEFT, mouseID);
}
if (rawButtons & RI_MOUSE_BUTTON_1_UP) {
WIN_CheckWParamMouseButton(!(rawButtons & RI_MOUSE_BUTTON_1_UP), mouseFlags, swapButtons, data, SDL_BUTTON_LEFT, mouseID);
}
if (rawButtons & RI_MOUSE_BUTTON_2_DOWN) {
WIN_CheckWParamMouseButton((rawButtons & RI_MOUSE_BUTTON_2_DOWN), mouseFlags, swapButtons, data, SDL_BUTTON_RIGHT, mouseID);
}
if (rawButtons & RI_MOUSE_BUTTON_2_UP) {
WIN_CheckWParamMouseButton(!(rawButtons & RI_MOUSE_BUTTON_2_UP), mouseFlags, swapButtons, data, SDL_BUTTON_RIGHT, mouseID);
}
if (rawButtons & RI_MOUSE_BUTTON_3_DOWN) {
WIN_CheckWParamMouseButton((rawButtons & RI_MOUSE_BUTTON_3_DOWN), mouseFlags, swapButtons, data, SDL_BUTTON_MIDDLE, mouseID);
}
if (rawButtons & RI_MOUSE_BUTTON_3_UP) {
WIN_CheckWParamMouseButton(!(rawButtons & RI_MOUSE_BUTTON_3_UP), mouseFlags, swapButtons, data, SDL_BUTTON_MIDDLE, mouseID);
}
if (rawButtons & RI_MOUSE_BUTTON_4_DOWN) {
WIN_CheckWParamMouseButton((rawButtons & RI_MOUSE_BUTTON_4_DOWN), mouseFlags, swapButtons, data, SDL_BUTTON_X1, mouseID);
}
if (rawButtons & RI_MOUSE_BUTTON_4_UP) {
WIN_CheckWParamMouseButton(!(rawButtons & RI_MOUSE_BUTTON_4_UP), mouseFlags, swapButtons, data, SDL_BUTTON_X1, mouseID);
}
if (rawButtons & RI_MOUSE_BUTTON_5_DOWN) {
WIN_CheckWParamMouseButton((rawButtons & RI_MOUSE_BUTTON_5_DOWN), mouseFlags, swapButtons, data, SDL_BUTTON_X2, mouseID);
}
if (rawButtons & RI_MOUSE_BUTTON_5_UP) {
WIN_CheckWParamMouseButton(!(rawButtons & RI_MOUSE_BUTTON_5_UP), mouseFlags, swapButtons, data, SDL_BUTTON_X2, mouseID);
}
data->mouse_button_flags = rawButtons;
}
}
static void WIN_CheckAsyncMouseRelease(SDL_WindowData *data)
{
Uint32 mouseFlags;
SHORT keyState;
SDL_bool swapButtons;
/* mouse buttons may have changed state here, we need to resync them,
but we will get a WM_MOUSEMOVE right away which will fix things up if in non raw mode also
*/
mouseFlags = SDL_GetMouseState(NULL, NULL);
swapButtons = GetSystemMetrics(SM_SWAPBUTTON) != 0;
keyState = GetAsyncKeyState(VK_LBUTTON);
if (!(keyState & 0x8000)) {
WIN_CheckWParamMouseButton(SDL_FALSE, mouseFlags, swapButtons, data, SDL_BUTTON_LEFT, 0);
}
keyState = GetAsyncKeyState(VK_RBUTTON);
if (!(keyState & 0x8000)) {
WIN_CheckWParamMouseButton(SDL_FALSE, mouseFlags, swapButtons, data, SDL_BUTTON_RIGHT, 0);
}
keyState = GetAsyncKeyState(VK_MBUTTON);
if (!(keyState & 0x8000)) {
WIN_CheckWParamMouseButton(SDL_FALSE, mouseFlags, swapButtons, data, SDL_BUTTON_MIDDLE, 0);
}
keyState = GetAsyncKeyState(VK_XBUTTON1);
if (!(keyState & 0x8000)) {
WIN_CheckWParamMouseButton(SDL_FALSE, mouseFlags, swapButtons, data, SDL_BUTTON_X1, 0);
}
keyState = GetAsyncKeyState(VK_XBUTTON2);
if (!(keyState & 0x8000)) {
WIN_CheckWParamMouseButton(SDL_FALSE, mouseFlags, swapButtons, data, SDL_BUTTON_X2, 0);
}
data->mouse_button_flags = (WPARAM)-1;
}
static void WIN_UpdateFocus(SDL_Window *window, SDL_bool expect_focus)
{
SDL_WindowData *data = window->driverdata;
HWND hwnd = data->hwnd;
SDL_bool had_focus = (SDL_GetKeyboardFocus() == window);
SDL_bool has_focus = (GetForegroundWindow() == hwnd);
if (had_focus == has_focus || has_focus != expect_focus) {
return;
}
if (has_focus) {
POINT cursorPos;
SDL_bool swapButtons = GetSystemMetrics(SM_SWAPBUTTON) != 0;
if (GetAsyncKeyState(VK_LBUTTON)) {
data->focus_click_pending |= !swapButtons ? SDL_BUTTON_LMASK : SDL_BUTTON_RMASK;
}
if (GetAsyncKeyState(VK_RBUTTON)) {
data->focus_click_pending |= !swapButtons ? SDL_BUTTON_RMASK : SDL_BUTTON_LMASK;
}
if (GetAsyncKeyState(VK_MBUTTON)) {
data->focus_click_pending |= SDL_BUTTON_MMASK;
}
if (GetAsyncKeyState(VK_XBUTTON1)) {
data->focus_click_pending |= SDL_BUTTON_X1MASK;
}
if (GetAsyncKeyState(VK_XBUTTON2)) {
data->focus_click_pending |= SDL_BUTTON_X2MASK;
}
SDL_SetKeyboardFocus(data->keyboard_focus ? data->keyboard_focus : window);
/* In relative mode we are guaranteed to have mouse focus if we have keyboard focus */
if (!SDL_GetMouse()->relative_mode) {
GetCursorPos(&cursorPos);
ScreenToClient(hwnd, &cursorPos);
SDL_SendMouseMotion(WIN_GetEventTimestamp(), window, 0, 0, (float)cursorPos.x, (float)cursorPos.y);
}
WIN_CheckAsyncMouseRelease(data);
WIN_UpdateClipCursor(window);
/*
* FIXME: Update keyboard state
*/
WIN_CheckClipboardUpdate(data->videodata);
SDL_ToggleModState(SDL_KMOD_CAPS, (GetKeyState(VK_CAPITAL) & 0x0001) ? SDL_TRUE : SDL_FALSE);
SDL_ToggleModState(SDL_KMOD_NUM, (GetKeyState(VK_NUMLOCK) & 0x0001) ? SDL_TRUE : SDL_FALSE);
SDL_ToggleModState(SDL_KMOD_SCROLL, (GetKeyState(VK_SCROLL) & 0x0001) ? SDL_TRUE : SDL_FALSE);
WIN_UpdateWindowICCProfile(data->window, SDL_TRUE);
} else {
RECT rect;
data->in_window_deactivation = SDL_TRUE;
SDL_SetKeyboardFocus(NULL);
/* In relative mode we are guaranteed to not have mouse focus if we don't have keyboard focus */
if (SDL_GetMouse()->relative_mode) {
SDL_SetMouseFocus(NULL);
}
WIN_ResetDeadKeys();
if (GetClipCursor(&rect) && SDL_memcmp(&rect, &data->cursor_clipped_rect, sizeof(rect)) == 0) {
ClipCursor(NULL);
SDL_zero(data->cursor_clipped_rect);
}
data->in_window_deactivation = SDL_FALSE;
}
}
#endif /*!defined(__XBOXONE__) && !defined(__XBOXSERIES__)*/
static BOOL WIN_ConvertUTF32toUTF8(UINT32 codepoint, char *text)
{
if (codepoint <= 0x7F) {
text[0] = (char)codepoint;
text[1] = '\0';
} else if (codepoint <= 0x7FF) {
text[0] = 0xC0 | (char)((codepoint >> 6) & 0x1F);
text[1] = 0x80 | (char)(codepoint & 0x3F);
text[2] = '\0';
} else if (codepoint <= 0xFFFF) {
text[0] = 0xE0 | (char)((codepoint >> 12) & 0x0F);
text[1] = 0x80 | (char)((codepoint >> 6) & 0x3F);
text[2] = 0x80 | (char)(codepoint & 0x3F);
text[3] = '\0';
} else if (codepoint <= 0x10FFFF) {
text[0] = 0xF0 | (char)((codepoint >> 18) & 0x0F);
text[1] = 0x80 | (char)((codepoint >> 12) & 0x3F);
text[2] = 0x80 | (char)((codepoint >> 6) & 0x3F);
text[3] = 0x80 | (char)(codepoint & 0x3F);
text[4] = '\0';
} else {
return SDL_FALSE;
}
return SDL_TRUE;
}
static BOOL WIN_ConvertUTF16toUTF8(UINT32 high_surrogate, UINT32 low_surrogate, char *text)
{
const UINT32 SURROGATE_OFFSET = 0x10000U - (0xD800 << 10) - 0xDC00;
const UINT32 codepoint = (high_surrogate << 10) + low_surrogate + SURROGATE_OFFSET;
return WIN_ConvertUTF32toUTF8(codepoint, text);
}
static SDL_bool ShouldGenerateWindowCloseOnAltF4(void)
{
return !SDL_GetHintBoolean(SDL_HINT_WINDOWS_NO_CLOSE_ON_ALT_F4, SDL_FALSE);
}
#if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
/* We want to generate mouse events from mouse and pen, and touch events from touchscreens */
#define MI_WP_SIGNATURE 0xFF515700
#define MI_WP_SIGNATURE_MASK 0xFFFFFF00
#define IsTouchEvent(dw) ((dw)&MI_WP_SIGNATURE_MASK) == MI_WP_SIGNATURE
typedef enum
{
SDL_MOUSE_EVENT_SOURCE_UNKNOWN,
SDL_MOUSE_EVENT_SOURCE_MOUSE,
SDL_MOUSE_EVENT_SOURCE_TOUCH,
SDL_MOUSE_EVENT_SOURCE_PEN,
} SDL_MOUSE_EVENT_SOURCE;
static SDL_MOUSE_EVENT_SOURCE GetMouseMessageSource()
{
LPARAM extrainfo = GetMessageExtraInfo();
/* Mouse data (ignoring synthetic mouse events generated for touchscreens) */
/* Versions below Vista will set the low 7 bits to the Mouse ID and don't use bit 7:
Check bits 8-32 for the signature (which will indicate a Tablet PC Pen or Touch Device).
Only check bit 7 when Vista and up(Cleared=Pen, Set=Touch(which we need to filter out)),
when the signature is set. The Mouse ID will be zero for an actual mouse. */
if (IsTouchEvent(extrainfo)) {
if (extrainfo & 0x80) {
return SDL_MOUSE_EVENT_SOURCE_TOUCH;
} else {
return SDL_MOUSE_EVENT_SOURCE_PEN;
}
}
return SDL_MOUSE_EVENT_SOURCE_MOUSE;
}
#endif /*!defined(__XBOXONE__) && !defined(__XBOXSERIES__)*/
static SDL_WindowData *WIN_GetWindowDataFromHWND(HWND hwnd)
{
SDL_VideoDevice *_this = SDL_GetVideoDevice();
SDL_Window *window;
if (_this) {
for (window = _this->windows; window; window = window->next) {
SDL_WindowData *data = window->driverdata;
if (data && data->hwnd == hwnd) {
return data;
}
}
}
return NULL;
}
#if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
LRESULT CALLBACK
WIN_KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
KBDLLHOOKSTRUCT *hookData = (KBDLLHOOKSTRUCT *)lParam;
SDL_VideoData *data = SDL_GetVideoDevice()->driverdata;
SDL_Scancode scanCode;
if (nCode < 0 || nCode != HC_ACTION) {
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
switch (hookData->vkCode) {
case VK_LWIN:
scanCode = SDL_SCANCODE_LGUI;
break;
case VK_RWIN:
scanCode = SDL_SCANCODE_RGUI;
break;
case VK_LMENU:
scanCode = SDL_SCANCODE_LALT;
break;
case VK_RMENU:
scanCode = SDL_SCANCODE_RALT;
break;
case VK_LCONTROL:
scanCode = SDL_SCANCODE_LCTRL;
break;
case VK_RCONTROL:
scanCode = SDL_SCANCODE_RCTRL;
break;
/* These are required to intercept Alt+Tab and Alt+Esc on Windows 7 */
case VK_TAB:
scanCode = SDL_SCANCODE_TAB;
break;
case VK_ESCAPE:
scanCode = SDL_SCANCODE_ESCAPE;
break;
default:
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
if (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) {
SDL_SendKeyboardKey(0, SDL_PRESSED, scanCode);
} else {
SDL_SendKeyboardKey(0, SDL_RELEASED, scanCode);
/* If the key was down prior to our hook being installed, allow the
key up message to pass normally the first time. This ensures other
windows have a consistent view of the key state, and avoids keys
being stuck down in those windows if they are down when the grab
happens and raised while grabbed. */
if (hookData->vkCode <= 0xFF && data->pre_hook_key_state[hookData->vkCode]) {
data->pre_hook_key_state[hookData->vkCode] = 0;
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
}
return 1;
}
#endif /*!defined(__XBOXONE__) && !defined(__XBOXSERIES__)*/
LRESULT CALLBACK
WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
SDL_WindowData *data;
LRESULT returnCode = -1;
/* Get the window data for the window */
data = WIN_GetWindowDataFromHWND(hwnd);
#if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
if (data == NULL) {
/* Fallback */
data = (SDL_WindowData *)GetProp(hwnd, TEXT("SDL_WindowData"));
}
#endif
if (data == NULL) {
return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);
}
#ifdef WMMSG_DEBUG
{
char message[1024];
if (msg > MAX_WMMSG) {
SDL_snprintf(message, sizeof(message), "Received windows message: %p UNKNOWN (%d) -- 0x%x, 0x%x\r\n", hwnd, msg, wParam, lParam);
} else {
SDL_snprintf(message, sizeof(message), "Received windows message: %p %s -- 0x%x, 0x%x\r\n", hwnd, wmtab[msg], wParam, lParam);
}
OutputDebugStringA(message);
}
#endif /* WMMSG_DEBUG */
#if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
if (IME_HandleMessage(hwnd, msg, wParam, &lParam, data->videodata)) {
return 0;
}
#endif
switch (msg) {
case WM_SHOWWINDOW:
{
if (wParam) {
SDL_SendWindowEvent(data->window, SDL_EVENT_WINDOW_SHOWN, 0, 0);
} else {
SDL_SendWindowEvent(data->window, SDL_EVENT_WINDOW_HIDDEN, 0, 0);
}
} break;
#if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
case WM_NCACTIVATE:
{
/* Don't immediately clip the cursor in case we're clicking minimize/maximize buttons */
data->skip_update_clipcursor = SDL_TRUE;
/* Update the focus here, since it's possible to get WM_ACTIVATE and WM_SETFOCUS without
actually being the foreground window, but this appears to get called in all cases where
the global foreground window changes to and from this window. */
WIN_UpdateFocus(data->window, !!wParam);
} break;
case WM_ACTIVATE:
{
/* Update the focus in case we changed focus to a child window and then away from the application */
WIN_UpdateFocus(data->window, !!LOWORD(wParam));
} break;
case WM_MOUSEACTIVATE:
{
if (SDL_WINDOW_IS_POPUP(data->window)) {
return MA_NOACTIVATE;
}
} break;
case WM_SETFOCUS:
{
/* Update the focus in case it's changing between top-level windows in the same application */
WIN_UpdateFocus(data->window, SDL_TRUE);
} break;
case WM_KILLFOCUS:
case WM_ENTERIDLE:
{
/* Update the focus in case it's changing between top-level windows in the same application */
WIN_UpdateFocus(data->window, SDL_FALSE);
} break;
case WM_POINTERUPDATE:
{
data->last_pointer_update = lParam;
break;
}
case WM_MOUSEMOVE:
{
SDL_Mouse *mouse = SDL_GetMouse();
if (!data->mouse_tracked) {
TRACKMOUSEEVENT trackMouseEvent;
trackMouseEvent.cbSize = sizeof(TRACKMOUSEEVENT);
trackMouseEvent.dwFlags = TME_LEAVE;
trackMouseEvent.hwndTrack = data->hwnd;
if (TrackMouseEvent(&trackMouseEvent)) {
data->mouse_tracked = SDL_TRUE;
}
}
if (!mouse->relative_mode || mouse->relative_mode_warp) {
/* Only generate mouse events for real mouse */
if (GetMouseMessageSource() != SDL_MOUSE_EVENT_SOURCE_TOUCH &&
lParam != data->last_pointer_update) {
SDL_SendMouseMotion(WIN_GetEventTimestamp(), data->window, 0, 0, (float)GET_X_LPARAM(lParam), (float)GET_Y_LPARAM(lParam));
}
}
} break;
case WM_LBUTTONUP:
case WM_RBUTTONUP:
case WM_MBUTTONUP:
case WM_XBUTTONUP:
case WM_LBUTTONDOWN:
case WM_LBUTTONDBLCLK:
case WM_RBUTTONDOWN:
case WM_RBUTTONDBLCLK:
case WM_MBUTTONDOWN:
case WM_MBUTTONDBLCLK:
case WM_XBUTTONDOWN:
case WM_XBUTTONDBLCLK:
{
SDL_Mouse *mouse = SDL_GetMouse();
if (!mouse->relative_mode || mouse->relative_mode_warp) {
if (GetMouseMessageSource() != SDL_MOUSE_EVENT_SOURCE_TOUCH &&
lParam != data->last_pointer_update) {
WIN_CheckWParamMouseButtons(wParam, data, 0);
}
}
} break;
case WM_INPUT:
{
SDL_Mouse *mouse = SDL_GetMouse();
HRAWINPUT hRawInput = (HRAWINPUT)lParam;
RAWINPUT inp;
UINT size = sizeof(inp);
/* We only use raw mouse input in relative mode */
if (!mouse->relative_mode || mouse->relative_mode_warp) {
break;
}
/* Relative mouse motion is delivered to the window with keyboard focus */
if (data->window != SDL_GetKeyboardFocus()) {
break;
}
GetRawInputData(hRawInput, RID_INPUT, &inp, &size, sizeof(RAWINPUTHEADER));
/* Mouse data (ignoring synthetic mouse events generated for touchscreens) */
if (inp.header.dwType == RIM_TYPEMOUSE) {
SDL_MouseID mouseID;
RAWMOUSE *rawmouse;
if (SDL_GetNumTouchDevices() > 0 &&
(GetMouseMessageSource() == SDL_MOUSE_EVENT_SOURCE_TOUCH || (GetMessageExtraInfo() & 0x82) == 0x82)) {
break;
}
/* We do all of our mouse state checking against mouse ID 0
* We would only use the actual hDevice if we were tracking
* all mouse motion independently, and never using mouse ID 0.
*/
mouseID = 0; /* (SDL_MouseID)(uintptr_t)inp.header.hDevice; */
rawmouse = &inp.data.mouse;
if ((rawmouse->usFlags & 0x01) == MOUSE_MOVE_RELATIVE) {
SDL_SendMouseMotion(WIN_GetEventTimestamp(), data->window, mouseID, 1, (float)rawmouse->lLastX, (float)rawmouse->lLastY);
} else if (rawmouse->lLastX || rawmouse->lLastY) {
/* This is absolute motion, either using a tablet or mouse over RDP
Notes on how RDP appears to work, as of Windows 10 2004:
- SetCursorPos() calls are cached, with multiple calls coalesced into a single call that's sent to the RDP client. If the last call to SetCursorPos() has the same value as the last one that was sent to the client, it appears to be ignored and not sent. This means that we need to jitter the SetCursorPos() position slightly in order for the recentering to work correctly.
- User mouse motion is coalesced with SetCursorPos(), so the WM_INPUT positions we see will not necessarily match the position we requested with SetCursorPos().
- SetCursorPos() outside of the bounds of the focus window appears not to do anything.
- SetCursorPos() while the cursor is NULL doesn't do anything
We handle this by creating a safe area within the application window, and when the mouse leaves that safe area, we warp back to the opposite side. Any single motion > 50% of the safe area is assumed to be a warp and ignored.
*/
SDL_bool remote_desktop = GetSystemMetrics(SM_REMOTESESSION) ? SDL_TRUE : SDL_FALSE;
SDL_bool virtual_desktop = (rawmouse->usFlags & MOUSE_VIRTUAL_DESKTOP) ? SDL_TRUE : SDL_FALSE;
SDL_bool normalized_coordinates = !(rawmouse->usFlags & 0x40) ? SDL_TRUE : SDL_FALSE;
int w = GetSystemMetrics(virtual_desktop ? SM_CXVIRTUALSCREEN : SM_CXSCREEN);
int h = GetSystemMetrics(virtual_desktop ? SM_CYVIRTUALSCREEN : SM_CYSCREEN);
int x = normalized_coordinates ? (int)(((float)rawmouse->lLastX / 65535.0f) * w) : (int)rawmouse->lLastX;
int y = normalized_coordinates ? (int)(((float)rawmouse->lLastY / 65535.0f) * h) : (int)rawmouse->lLastY;
int relX, relY;
/* Calculate relative motion */
if (data->last_raw_mouse_position.x == 0 && data->last_raw_mouse_position.y == 0) {
data->last_raw_mouse_position.x = x;
data->last_raw_mouse_position.y = y;
}
relX = x - data->last_raw_mouse_position.x;
relY = y - data->last_raw_mouse_position.y;
if (remote_desktop) {
if (!data->in_title_click && !data->focus_click_pending) {
static int wobble;
float floatX = (float)x / w;
float floatY = (float)y / h;
/* See if the mouse is at the edge of the screen, or in the RDP title bar area */
if (floatX <= 0.01f || floatX >= 0.99f || floatY <= 0.01f || floatY >= 0.99f || y < 32) {
/* Wobble the cursor position so it's not ignored if the last warp didn't have any effect */
RECT rect = data->cursor_clipped_rect;
int warpX = rect.left + ((rect.right - rect.left) / 2) + wobble;
int warpY = rect.top + ((rect.bottom - rect.top) / 2);
WIN_SetCursorPos(warpX, warpY);
++wobble;
if (wobble > 1) {
wobble = -1;
}
} else {
/* Send relative motion if we didn't warp last frame (had good position data)
We also sometimes get large deltas due to coalesced mouse motion and warping,
so ignore those.
*/
const int MAX_RELATIVE_MOTION = (h / 6);
if (SDL_abs(relX) < MAX_RELATIVE_MOTION &&
SDL_abs(relY) < MAX_RELATIVE_MOTION) {
SDL_SendMouseMotion(WIN_GetEventTimestamp(), data->window, mouseID, 1, (float)relX, (float)relY);
}
}
}
} else {
const int MAXIMUM_TABLET_RELATIVE_MOTION = 32;
if (SDL_abs(relX) > MAXIMUM_TABLET_RELATIVE_MOTION ||
SDL_abs(relY) > MAXIMUM_TABLET_RELATIVE_MOTION) {
/* Ignore this motion, probably a pen lift and drop */
} else {
SDL_SendMouseMotion(WIN_GetEventTimestamp(), data->window, mouseID, 1, (float)relX, (float)relY);
}
}
data->last_raw_mouse_position.x = x;
data->last_raw_mouse_position.y = y;
}
WIN_CheckRawMouseButtons(rawmouse->usButtonFlags, data, mouseID);
}
} break;
case WM_MOUSEWHEEL:
case WM_MOUSEHWHEEL:
{
short amount = GET_WHEEL_DELTA_WPARAM(wParam);
float fAmount = (float)amount / WHEEL_DELTA;
if (msg == WM_MOUSEWHEEL) {
SDL_SendMouseWheel(WIN_GetEventTimestamp(), data->window, 0, 0.0f, fAmount, SDL_MOUSEWHEEL_NORMAL);
} else {
SDL_SendMouseWheel(WIN_GetEventTimestamp(), data->window, 0, fAmount, 0.0f, SDL_MOUSEWHEEL_NORMAL);
}
} break;
case WM_MOUSELEAVE:
if (!(data->window->flags & SDL_WINDOW_MOUSE_CAPTURE)) {
if (SDL_GetMouseFocus() == data->window && !SDL_GetMouse()->relative_mode && !IsIconic(hwnd)) {
SDL_Mouse *mouse;
POINT cursorPos;
GetCursorPos(&cursorPos);
ScreenToClient(hwnd, &cursorPos);
mouse = SDL_GetMouse();
if (!mouse->was_touch_mouse_events) { /* we're not a touch handler causing a mouse leave? */
SDL_SendMouseMotion(WIN_GetEventTimestamp(), data->window, 0, 0, (float)cursorPos.x, (float)cursorPos.y);
} else { /* touch handling? */
mouse->was_touch_mouse_events = SDL_FALSE; /* not anymore */
if (mouse->touch_mouse_events) { /* convert touch to mouse events */
SDL_SendMouseMotion(WIN_GetEventTimestamp(), data->window, SDL_TOUCH_MOUSEID, 0, (float)cursorPos.x, (float)cursorPos.y);
} else { /* normal handling */
SDL_SendMouseMotion(WIN_GetEventTimestamp(), data->window, 0, 0, (float)cursorPos.x, (float)cursorPos.y);
}
}
}
if (!SDL_GetMouse()->relative_mode) {
/* When WM_MOUSELEAVE is fired we can be assured that the cursor has left the window */
SDL_SetMouseFocus(NULL);
}