-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
WindowImplWin32.cpp
1331 lines (1141 loc) · 48.7 KB
/
WindowImplWin32.cpp
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
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2024 Laurent Gomila (laurent@sfml-dev.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.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/JoystickImpl.hpp>
#include <SFML/Window/Win32/WindowImplWin32.hpp>
#include <SFML/Window/WindowEnums.hpp>
#include <SFML/System/Err.hpp>
#include <SFML/System/String.hpp>
#include <SFML/System/Utf.hpp>
// dbt.h is lowercase here, as a cross-compile on linux with mingw-w64
// expects lowercase, and a native compile on windows, whether via msvc
// or mingw-w64 addresses files in a case insensitive manner.
#include <dbt.h>
#include <ostream>
#include <vector>
#include <cstddef>
#include <cstring>
// MinGW lacks the definition of some Win32 constants
#ifndef XBUTTON1
#define XBUTTON1 0x0001
#endif
#ifndef XBUTTON2
#define XBUTTON2 0x0002
#endif
#ifndef WM_MOUSEHWHEEL
#define WM_MOUSEHWHEEL 0x020E
#endif
#ifndef MAPVK_VK_TO_VSC
#define MAPVK_VK_TO_VSC (0)
#endif
namespace
{
unsigned int windowCount = 0; // Windows owned by SFML
unsigned int handleCount = 0; // All window handles
const wchar_t* className = L"SFML_Window";
sf::priv::WindowImplWin32* fullscreenWindow = nullptr;
const GUID guidDevinterfaceHid = {0x4d1e55b2, 0xf16f, 0x11cf, {0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30}};
void setProcessDpiAware()
{
// Try SetProcessDpiAwareness first
HINSTANCE shCoreDll = LoadLibrary(L"Shcore.dll");
if (shCoreDll)
{
enum ProcessDpiAwareness
{
ProcessDpiUnaware = 0,
ProcessSystemDpiAware = 1,
ProcessPerMonitorDpiAware = 2
};
using SetProcessDpiAwarenessFuncType = HRESULT(WINAPI*)(ProcessDpiAwareness);
auto setProcessDpiAwarenessFunc = reinterpret_cast<SetProcessDpiAwarenessFuncType>(
reinterpret_cast<void*>(GetProcAddress(shCoreDll, "SetProcessDpiAwareness")));
if (setProcessDpiAwarenessFunc)
{
// We only check for E_INVALIDARG because we would get
// E_ACCESSDENIED if the DPI was already set previously
// and S_OK means the call was successful.
// We intentionally don't use Per Monitor V2 which can be
// enabled with SetProcessDpiAwarenessContext, because that
// would scale the title bar and thus change window size
// by default when moving the window between monitors.
if (setProcessDpiAwarenessFunc(ProcessPerMonitorDpiAware) == E_INVALIDARG)
{
sf::err() << "Failed to set process DPI awareness" << std::endl;
}
else
{
FreeLibrary(shCoreDll);
return;
}
}
FreeLibrary(shCoreDll);
}
// Fall back to SetProcessDPIAware if SetProcessDpiAwareness
// is not available on this system
HINSTANCE user32Dll = LoadLibrary(L"user32.dll");
if (user32Dll)
{
using SetProcessDPIAwareFuncType = BOOL(WINAPI*)();
auto setProcessDPIAwareFunc = reinterpret_cast<SetProcessDPIAwareFuncType>(
reinterpret_cast<void*>(GetProcAddress(user32Dll, "SetProcessDPIAware")));
if (setProcessDPIAwareFunc)
{
if (!setProcessDPIAwareFunc())
sf::err() << "Failed to set process DPI awareness" << std::endl;
}
FreeLibrary(user32Dll);
}
}
// Register a RAWINPUTDEVICE representing the mouse to receive raw
// mouse deltas using WM_INPUT
void initRawMouse()
{
const RAWINPUTDEVICE rawMouse{0x01, 0x02, 0, nullptr}; // HID usage: mouse device class, no flags, follow keyboard focus
if (RegisterRawInputDevices(&rawMouse, 1, sizeof(rawMouse)) != TRUE)
sf::err() << "Failed to initialize raw mouse input" << std::endl;
}
} // namespace
namespace sf::priv
{
////////////////////////////////////////////////////////////
WindowImplWin32::WindowImplWin32(WindowHandle handle) : m_handle(handle)
{
// Set that this process is DPI aware and can handle DPI scaling
setProcessDpiAware();
if (m_handle)
{
// If we're the first window handle, we only need to poll for joysticks when WM_DEVICECHANGE message is received
if (handleCount == 0)
{
JoystickImpl::setLazyUpdates(true);
initRawMouse();
}
++handleCount;
// We change the event procedure of the control (it is important to save the old one)
SetWindowLongPtrW(m_handle, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
m_callback = SetWindowLongPtrW(m_handle, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(&WindowImplWin32::globalOnEvent));
}
}
////////////////////////////////////////////////////////////
WindowImplWin32::WindowImplWin32(VideoMode mode,
const String& title,
std::uint32_t style,
State state,
const ContextSettings& /*settings*/) :
m_lastSize(mode.size),
m_fullscreen(state == State::Fullscreen),
m_cursorGrabbed(m_fullscreen)
{
// Set that this process is DPI aware and can handle DPI scaling
setProcessDpiAware();
// Register the window class at first call
if (windowCount == 0)
registerWindowClass();
// Compute position and size
HDC screenDC = GetDC(nullptr);
const int left = (GetDeviceCaps(screenDC, HORZRES) - static_cast<int>(mode.size.x)) / 2;
const int top = (GetDeviceCaps(screenDC, VERTRES) - static_cast<int>(mode.size.y)) / 2;
auto [width, height] = Vector2i(mode.size);
ReleaseDC(nullptr, screenDC);
// Choose the window style according to the Style parameter
DWORD win32Style = WS_VISIBLE;
if (style == Style::None)
{
win32Style |= WS_POPUP;
}
else
{
if (style & Style::Titlebar)
win32Style |= WS_CAPTION | WS_MINIMIZEBOX;
if (style & Style::Resize)
win32Style |= WS_THICKFRAME | WS_MAXIMIZEBOX;
if (style & Style::Close)
win32Style |= WS_SYSMENU;
}
// In windowed mode, adjust width and height so that window will have the requested client area
if (!m_fullscreen)
{
RECT rectangle = {0, 0, width, height};
AdjustWindowRect(&rectangle, win32Style, false);
width = rectangle.right - rectangle.left;
height = rectangle.bottom - rectangle.top;
}
// Create the window
m_handle = CreateWindowW(className,
title.toWideString().c_str(),
win32Style,
left,
top,
width,
height,
nullptr,
nullptr,
GetModuleHandle(nullptr),
this);
// Register to receive device interface change notifications (used for joystick connection handling)
DEV_BROADCAST_DEVICEINTERFACE deviceInterface =
{sizeof(DEV_BROADCAST_DEVICEINTERFACE), DBT_DEVTYP_DEVICEINTERFACE, 0, guidDevinterfaceHid, {0}};
RegisterDeviceNotification(m_handle, &deviceInterface, DEVICE_NOTIFY_WINDOW_HANDLE);
// If we're the first window handle, we only need to poll for joysticks when WM_DEVICECHANGE message is received
if (m_handle)
{
if (handleCount == 0)
{
JoystickImpl::setLazyUpdates(true);
initRawMouse();
}
++handleCount;
}
// By default, the OS limits the size of the window the the desktop size,
// we have to resize it after creation to apply the real size
setSize(mode.size);
// Switch to fullscreen if requested
if (m_fullscreen)
switchToFullscreen(mode);
// Increment window count
++windowCount;
}
////////////////////////////////////////////////////////////
WindowImplWin32::~WindowImplWin32()
{
// TODO should we restore the cursor shape and visibility?
// Destroy the custom icon, if any
if (m_icon)
DestroyIcon(m_icon);
// If it's the last window handle we have to poll for joysticks again
if (m_handle)
{
--handleCount;
if (handleCount == 0)
JoystickImpl::setLazyUpdates(false);
}
if (!m_callback)
{
// Destroy the window
if (m_handle)
DestroyWindow(m_handle);
// Decrement the window count
--windowCount;
// Unregister window class if we were the last window
if (windowCount == 0)
UnregisterClassW(className, GetModuleHandleW(nullptr));
}
else
{
// The window is external: remove the hook on its message callback
SetWindowLongPtrW(m_handle, GWLP_WNDPROC, m_callback);
}
}
////////////////////////////////////////////////////////////
WindowHandle WindowImplWin32::getNativeHandle() const
{
return m_handle;
}
////////////////////////////////////////////////////////////
void WindowImplWin32::processEvents()
{
// We process the window events only if we own it
if (!m_callback)
{
MSG message;
while (PeekMessageW(&message, nullptr, 0, 0, PM_REMOVE))
{
TranslateMessage(&message);
DispatchMessageW(&message);
}
}
}
////////////////////////////////////////////////////////////
Vector2i WindowImplWin32::getPosition() const
{
RECT rect;
GetWindowRect(m_handle, &rect);
return {rect.left, rect.top};
}
////////////////////////////////////////////////////////////
void WindowImplWin32::setPosition(Vector2i position)
{
SetWindowPos(m_handle, nullptr, position.x, position.y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
if (m_cursorGrabbed)
grabCursor(true);
}
////////////////////////////////////////////////////////////
Vector2u WindowImplWin32::getSize() const
{
RECT rect;
GetClientRect(m_handle, &rect);
return Vector2u(Vector2<LONG>(rect.right - rect.left, rect.bottom - rect.top));
}
////////////////////////////////////////////////////////////
void WindowImplWin32::setSize(Vector2u size)
{
const auto [width, height] = contentSizeToWindowSize(size);
SetWindowPos(m_handle, nullptr, 0, 0, width, height, SWP_NOMOVE | SWP_NOZORDER);
}
////////////////////////////////////////////////////////////
void WindowImplWin32::setTitle(const String& title)
{
SetWindowTextW(m_handle, title.toWideString().c_str());
}
////////////////////////////////////////////////////////////
void WindowImplWin32::setIcon(Vector2u size, const std::uint8_t* pixels)
{
// First destroy the previous one
if (m_icon)
DestroyIcon(m_icon);
// Windows wants BGRA pixels: swap red and blue channels
std::vector<std::uint8_t> iconPixels(size.x * size.y * 4);
for (std::size_t i = 0; i < iconPixels.size() / 4; ++i)
{
iconPixels[i * 4 + 0] = pixels[i * 4 + 2];
iconPixels[i * 4 + 1] = pixels[i * 4 + 1];
iconPixels[i * 4 + 2] = pixels[i * 4 + 0];
iconPixels[i * 4 + 3] = pixels[i * 4 + 3];
}
// Create the icon from the pixel array
m_icon = CreateIcon(GetModuleHandleW(nullptr),
static_cast<int>(size.x),
static_cast<int>(size.y),
1,
32,
nullptr,
iconPixels.data());
// Set it as both big and small icon of the window
if (m_icon)
{
SendMessageW(m_handle, WM_SETICON, ICON_BIG, reinterpret_cast<LPARAM>(m_icon));
SendMessageW(m_handle, WM_SETICON, ICON_SMALL, reinterpret_cast<LPARAM>(m_icon));
}
else
{
err() << "Failed to set the window's icon" << std::endl;
}
}
////////////////////////////////////////////////////////////
void WindowImplWin32::setVisible(bool visible)
{
ShowWindow(m_handle, visible ? SW_SHOW : SW_HIDE);
}
////////////////////////////////////////////////////////////
void WindowImplWin32::setMouseCursorVisible(bool visible)
{
m_cursorVisible = visible;
SetCursor(m_cursorVisible ? m_lastCursor : nullptr);
}
////////////////////////////////////////////////////////////
void WindowImplWin32::setMouseCursorGrabbed(bool grabbed)
{
m_cursorGrabbed = grabbed;
grabCursor(m_cursorGrabbed);
}
////////////////////////////////////////////////////////////
void WindowImplWin32::setMouseCursor(const CursorImpl& cursor)
{
m_lastCursor = static_cast<HCURSOR>(cursor.m_cursor);
SetCursor(m_cursorVisible ? m_lastCursor : nullptr);
}
////////////////////////////////////////////////////////////
void WindowImplWin32::setKeyRepeatEnabled(bool enabled)
{
m_keyRepeatEnabled = enabled;
}
////////////////////////////////////////////////////////////
void WindowImplWin32::requestFocus()
{
// Allow focus stealing only within the same process; compare PIDs of current and foreground window
DWORD thisPid = 0;
DWORD foregroundPid = 0;
GetWindowThreadProcessId(m_handle, &thisPid);
GetWindowThreadProcessId(GetForegroundWindow(), &foregroundPid);
if (thisPid == foregroundPid)
{
// The window requesting focus belongs to the same process as the current window: steal focus
SetForegroundWindow(m_handle);
}
else
{
// Different process: don't steal focus, but create a taskbar notification ("flash")
FLASHWINFO info;
info.cbSize = sizeof(info);
info.hwnd = m_handle;
info.dwFlags = FLASHW_TRAY;
info.dwTimeout = 0;
info.uCount = 3;
FlashWindowEx(&info);
}
}
////////////////////////////////////////////////////////////
bool WindowImplWin32::hasFocus() const
{
return m_handle == GetForegroundWindow();
}
////////////////////////////////////////////////////////////
void WindowImplWin32::registerWindowClass()
{
WNDCLASSW windowClass;
windowClass.style = 0;
windowClass.lpfnWndProc = &WindowImplWin32::globalOnEvent;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = GetModuleHandleW(nullptr);
windowClass.hIcon = nullptr;
windowClass.hCursor = nullptr;
windowClass.hbrBackground = nullptr;
windowClass.lpszMenuName = nullptr;
windowClass.lpszClassName = className;
RegisterClassW(&windowClass);
}
////////////////////////////////////////////////////////////
void WindowImplWin32::switchToFullscreen(const VideoMode& mode)
{
DEVMODE devMode;
devMode.dmSize = sizeof(devMode);
devMode.dmPelsWidth = mode.size.x;
devMode.dmPelsHeight = mode.size.y;
devMode.dmBitsPerPel = mode.bitsPerPixel;
devMode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;
// Apply fullscreen mode
if (ChangeDisplaySettingsW(&devMode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
{
err() << "Failed to change display mode for fullscreen" << std::endl;
return;
}
// Make the window flags compatible with fullscreen mode
SetWindowLongPtr(m_handle,
GWL_STYLE,
static_cast<LONG_PTR>(WS_POPUP) | static_cast<LONG_PTR>(WS_CLIPCHILDREN) |
static_cast<LONG_PTR>(WS_CLIPSIBLINGS));
SetWindowLongPtr(m_handle, GWL_EXSTYLE, WS_EX_APPWINDOW);
// Resize the window so that it fits the entire screen
SetWindowPos(m_handle, HWND_TOP, 0, 0, static_cast<int>(mode.size.x), static_cast<int>(mode.size.y), SWP_FRAMECHANGED);
ShowWindow(m_handle, SW_SHOW);
// Set "this" as the current fullscreen window
fullscreenWindow = this;
}
////////////////////////////////////////////////////////////
void WindowImplWin32::cleanup()
{
// Restore the previous video mode (in case we were running in fullscreen)
if (fullscreenWindow == this)
{
ChangeDisplaySettingsW(nullptr, 0);
fullscreenWindow = nullptr;
}
// Unhide the mouse cursor (in case it was hidden)
setMouseCursorVisible(true);
// No longer track the cursor
setTracking(false);
// No longer capture the cursor
ReleaseCapture();
}
////////////////////////////////////////////////////////////
void WindowImplWin32::setTracking(bool track)
{
TRACKMOUSEEVENT mouseEvent;
mouseEvent.cbSize = sizeof(TRACKMOUSEEVENT);
mouseEvent.dwFlags = track ? TME_LEAVE : TME_CANCEL;
mouseEvent.hwndTrack = m_handle;
mouseEvent.dwHoverTime = HOVER_DEFAULT;
TrackMouseEvent(&mouseEvent);
}
////////////////////////////////////////////////////////////
void WindowImplWin32::grabCursor(bool grabbed)
{
if (grabbed)
{
RECT rect;
GetClientRect(m_handle, &rect);
MapWindowPoints(m_handle, nullptr, reinterpret_cast<LPPOINT>(&rect), 2);
ClipCursor(&rect);
}
else
{
ClipCursor(nullptr);
}
}
////////////////////////////////////////////////////////////
Vector2i WindowImplWin32::contentSizeToWindowSize(Vector2u size)
{
// SetWindowPos wants the total size of the window (including title bar and borders) so we have to compute it
RECT rectangle = {0, 0, static_cast<long>(size.x), static_cast<long>(size.y)};
AdjustWindowRect(&rectangle, static_cast<DWORD>(GetWindowLongPtr(m_handle, GWL_STYLE)), false);
const auto width = rectangle.right - rectangle.left;
const auto height = rectangle.bottom - rectangle.top;
return {width, height};
}
////////////////////////////////////////////////////////////
Keyboard::Scancode WindowImplWin32::toScancode(WPARAM wParam, LPARAM lParam)
{
int code = (lParam & (0xFF << 16)) >> 16;
// Retrieve the scancode from the VirtualKey for synthetic key messages
if (code == 0)
{
code = static_cast<int>(MapVirtualKey(static_cast<UINT>(wParam), MAPVK_VK_TO_VSC));
}
// Windows scancodes
// Reference: https://msdn.microsoft.com/en-us/library/aa299374(v=vs.60).aspx
// clang-format off
switch (code)
{
case 1: return Keyboard::Scan::Escape;
case 2: return Keyboard::Scan::Num1;
case 3: return Keyboard::Scan::Num2;
case 4: return Keyboard::Scan::Num3;
case 5: return Keyboard::Scan::Num4;
case 6: return Keyboard::Scan::Num5;
case 7: return Keyboard::Scan::Num6;
case 8: return Keyboard::Scan::Num7;
case 9: return Keyboard::Scan::Num8;
case 10: return Keyboard::Scan::Num9;
case 11: return Keyboard::Scan::Num0;
case 12: return Keyboard::Scan::Hyphen;
case 13: return Keyboard::Scan::Equal;
case 14: return Keyboard::Scan::Backspace;
case 15: return Keyboard::Scan::Tab;
case 16: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::MediaPreviousTrack : Keyboard::Scan::Q;
case 17: return Keyboard::Scan::W;
case 18: return Keyboard::Scan::E;
case 19: return Keyboard::Scan::R;
case 20: return Keyboard::Scan::T;
case 21: return Keyboard::Scan::Y;
case 22: return Keyboard::Scan::U;
case 23: return Keyboard::Scan::I;
case 24: return Keyboard::Scan::O;
case 25: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::MediaNextTrack : Keyboard::Scan::P;
case 26: return Keyboard::Scan::LBracket;
case 27: return Keyboard::Scan::RBracket;
case 28: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::NumpadEnter : Keyboard::Scan::Enter;
case 29: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::RControl : Keyboard::Scan::LControl;
case 30: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::Select : Keyboard::Scan::A;
case 31: return Keyboard::Scan::S;
case 32: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::VolumeMute : Keyboard::Scan::D;
case 33: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::LaunchApplication1 : Keyboard::Scan::F;
case 34: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::MediaPlayPause : Keyboard::Scan::G;
case 35: return Keyboard::Scan::H;
case 36: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::MediaStop : Keyboard::Scan::J;
case 37: return Keyboard::Scan::K;
case 38: return Keyboard::Scan::L;
case 39: return Keyboard::Scan::Semicolon;
case 40: return Keyboard::Scan::Apostrophe;
case 41: return Keyboard::Scan::Grave;
case 42: return Keyboard::Scan::LShift;
case 43: return Keyboard::Scan::Backslash;
case 44: return Keyboard::Scan::Z;
case 45: return Keyboard::Scan::X;
case 46: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::VolumeDown : Keyboard::Scan::C;
case 47: return Keyboard::Scan::V;
case 48: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::VolumeUp : Keyboard::Scan::B;
case 49: return Keyboard::Scan::N;
case 50: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::HomePage : Keyboard::Scan::M;
case 51: return Keyboard::Scan::Comma;
case 52: return Keyboard::Scan::Period;
case 53: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::NumpadDivide : Keyboard::Scan::Slash;
case 54: return Keyboard::Scan::RShift;
case 55: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::PrintScreen : Keyboard::Scan::NumpadMultiply;
case 56: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::RAlt : Keyboard::Scan::LAlt;
case 57: return Keyboard::Scan::Space;
case 58: return Keyboard::Scan::CapsLock;
case 59: return Keyboard::Scan::F1;
case 60: return Keyboard::Scan::F2;
case 61: return Keyboard::Scan::F3;
case 62: return Keyboard::Scan::F4;
case 63: return Keyboard::Scan::F5;
case 64: return Keyboard::Scan::F6;
case 65: return Keyboard::Scan::F7;
case 66: return Keyboard::Scan::F8;
case 67: return Keyboard::Scan::F9;
case 68: return Keyboard::Scan::F10;
case 69: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::NumLock : Keyboard::Scan::Pause;
case 70: return Keyboard::Scan::ScrollLock;
case 71: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::Home : Keyboard::Scan::Numpad7;
case 72: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::Up : Keyboard::Scan::Numpad8;
case 73: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::PageUp : Keyboard::Scan::Numpad9;
case 74: return Keyboard::Scan::NumpadMinus;
case 75: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::Left : Keyboard::Scan::Numpad4;
case 76: return Keyboard::Scan::Numpad5;
case 77: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::Right : Keyboard::Scan::Numpad6;
case 78: return Keyboard::Scan::NumpadPlus;
case 79: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::End : Keyboard::Scan::Numpad1;
case 80: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::Down : Keyboard::Scan::Numpad2;
case 81: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::PageDown : Keyboard::Scan::Numpad3;
case 82: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::Insert : Keyboard::Scan::Numpad0;
case 83: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::Delete : Keyboard::Scan::NumpadDecimal;
case 86: return Keyboard::Scan::NonUsBackslash;
case 87: return Keyboard::Scan::F11;
case 88: return Keyboard::Scan::F12;
case 91: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::LSystem : Keyboard::Scan::Unknown;
case 92: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::RSystem : Keyboard::Scan::Unknown;
case 93: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::Menu : Keyboard::Scan::Unknown;
case 99: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::Help : Keyboard::Scan::Unknown;
case 100: return Keyboard::Scan::F13;
case 101: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::Search : Keyboard::Scan::F14;
case 102: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::Favorites : Keyboard::Scan::F15;
case 103: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::Refresh : Keyboard::Scan::F16;
case 104: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::Stop : Keyboard::Scan::F17;
case 105: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::Forward : Keyboard::Scan::F18;
case 106: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::Back : Keyboard::Scan::F19;
case 107: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::LaunchApplication1 : Keyboard::Scan::F20;
case 108: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::LaunchMail : Keyboard::Scan::F21;
case 109: return (HIWORD(lParam) & KF_EXTENDED) ? Keyboard::Scan::LaunchMediaSelect : Keyboard::Scan::F22;
case 110: return Keyboard::Scan::F23;
case 118: return Keyboard::Scan::F24;
default: return Keyboard::Scan::Unknown;
}
// clang-format on
}
////////////////////////////////////////////////////////////
void WindowImplWin32::processEvent(UINT message, WPARAM wParam, LPARAM lParam)
{
// Don't process any message until window is created
if (m_handle == nullptr)
return;
switch (message)
{
// Destroy event
case WM_DESTROY:
{
// Here we must cleanup resources !
cleanup();
break;
}
// Set cursor event
case WM_SETCURSOR:
{
// The mouse has moved, if the cursor is in our window we must refresh the cursor
if (LOWORD(lParam) == HTCLIENT)
{
SetCursor(m_cursorVisible ? m_lastCursor : nullptr);
}
break;
}
// Close event
case WM_CLOSE:
{
pushEvent(Event::Closed{});
break;
}
// Resize event
case WM_SIZE:
{
// Consider only events triggered by a maximize or a un-maximize
if (wParam != SIZE_MINIMIZED && !m_resizing && m_lastSize != getSize())
{
// Update the last handled size
m_lastSize = getSize();
// Push a resize event
pushEvent(Event::Resized{m_lastSize});
// Restore/update cursor grabbing
grabCursor(m_cursorGrabbed);
}
break;
}
// Start resizing
case WM_ENTERSIZEMOVE:
{
m_resizing = true;
grabCursor(false);
break;
}
// Stop resizing
case WM_EXITSIZEMOVE:
{
m_resizing = false;
// Ignore cases where the window has only been moved
if (m_lastSize != getSize())
{
// Update the last handled size
m_lastSize = getSize();
// Push a resize event
pushEvent(Event::Resized{m_lastSize});
}
// Restore/update cursor grabbing
grabCursor(m_cursorGrabbed);
break;
}
// Fix violations of minimum or maximum size
case WM_GETMINMAXINFO:
{
// We override the returned information to remove the default limit
// (the OS doesn't allow windows bigger than the desktop by default)
const auto maximumSize = contentSizeToWindowSize(getMaximumSize().value_or(Vector2u(50'000, 50'000)));
MINMAXINFO& minMaxInfo = *reinterpret_cast<PMINMAXINFO>(lParam);
minMaxInfo.ptMaxTrackSize.x = maximumSize.x;
minMaxInfo.ptMaxTrackSize.y = maximumSize.y;
if (getMaximumSize().has_value())
{
minMaxInfo.ptMaxSize.x = maximumSize.x;
minMaxInfo.ptMaxSize.y = maximumSize.y;
}
if (getMinimumSize().has_value())
{
const auto minimumSize = contentSizeToWindowSize(getMinimumSize().value());
minMaxInfo.ptMinTrackSize.x = minimumSize.x;
minMaxInfo.ptMinTrackSize.y = minimumSize.y;
}
break;
}
// Gain focus event
case WM_SETFOCUS:
{
// Restore cursor grabbing
grabCursor(m_cursorGrabbed);
pushEvent(Event::FocusGained{});
break;
}
// Lost focus event
case WM_KILLFOCUS:
{
// Ungrab the cursor
grabCursor(false);
pushEvent(Event::FocusLost{});
break;
}
// Text event
case WM_CHAR:
{
if (m_keyRepeatEnabled || ((lParam & (1 << 30)) == 0))
{
// Get the code of the typed character
auto character = static_cast<std::uint32_t>(wParam);
// Check if it is the first part of a surrogate pair, or a regular character
if ((character >= 0xD800) && (character <= 0xDBFF))
{
// First part of a surrogate pair: store it and wait for the second one
m_surrogate = static_cast<std::uint16_t>(character);
}
else
{
// Check if it is the second part of a surrogate pair, or a regular character
if ((character >= 0xDC00) && (character <= 0xDFFF))
{
// Convert the UTF-16 surrogate pair to a single UTF-32 value
std::uint16_t utf16[] = {m_surrogate, static_cast<std::uint16_t>(character)};
sf::Utf16::toUtf32(utf16, utf16 + 2, &character);
m_surrogate = 0;
}
// Send a TextEntered event
pushEvent(Event::TextEntered{character});
}
}
break;
}
// Keydown event
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
{
if (m_keyRepeatEnabled || ((HIWORD(lParam) & KF_REPEAT) == 0))
{
Event::KeyPressed event;
event.alt = HIWORD(GetKeyState(VK_MENU)) != 0;
event.control = HIWORD(GetKeyState(VK_CONTROL)) != 0;
event.shift = HIWORD(GetKeyState(VK_SHIFT)) != 0;
event.system = HIWORD(GetKeyState(VK_LWIN)) || HIWORD(GetKeyState(VK_RWIN));
event.code = virtualKeyCodeToSF(wParam, lParam);
event.scancode = toScancode(wParam, lParam);
pushEvent(event);
}
break;
}
// Keyup event
case WM_KEYUP:
case WM_SYSKEYUP:
{
Event::KeyReleased event;
event.alt = HIWORD(GetKeyState(VK_MENU)) != 0;
event.control = HIWORD(GetKeyState(VK_CONTROL)) != 0;
event.shift = HIWORD(GetKeyState(VK_SHIFT)) != 0;
event.system = HIWORD(GetKeyState(VK_LWIN)) || HIWORD(GetKeyState(VK_RWIN));
event.code = virtualKeyCodeToSF(wParam, lParam);
event.scancode = toScancode(wParam, lParam);
pushEvent(event);
break;
}
// Vertical mouse wheel event
case WM_MOUSEWHEEL:
{
// Mouse position is in screen coordinates, convert it to window coordinates
POINT position;
position.x = static_cast<std::int16_t>(LOWORD(lParam));
position.y = static_cast<std::int16_t>(HIWORD(lParam));
ScreenToClient(m_handle, &position);
auto delta = static_cast<std::int16_t>(HIWORD(wParam));
Event::MouseWheelScrolled event;
event.wheel = Mouse::Wheel::Vertical;
event.delta = static_cast<float>(delta) / 120.f;
event.position = {position.x, position.y};
pushEvent(event);
break;
}
// Horizontal mouse wheel event
case WM_MOUSEHWHEEL:
{
// Mouse position is in screen coordinates, convert it to window coordinates
POINT position;
position.x = static_cast<std::int16_t>(LOWORD(lParam));
position.y = static_cast<std::int16_t>(HIWORD(lParam));
ScreenToClient(m_handle, &position);
auto delta = static_cast<std::int16_t>(HIWORD(wParam));
Event::MouseWheelScrolled event;
event.wheel = Mouse::Wheel::Horizontal;
event.delta = -static_cast<float>(delta) / 120.f;
event.position = {position.x, position.y};
pushEvent(event);
break;
}
// Mouse left button down event
case WM_LBUTTONDOWN:
{
Event::MouseButtonPressed event;
event.button = Mouse::Button::Left;
event.position = {static_cast<std::int16_t>(LOWORD(lParam)), static_cast<std::int16_t>(HIWORD(lParam))};
pushEvent(event);
break;
}
// Mouse left button up event
case WM_LBUTTONUP:
{
Event::MouseButtonReleased event;
event.button = Mouse::Button::Left;
event.position = {static_cast<std::int16_t>(LOWORD(lParam)), static_cast<std::int16_t>(HIWORD(lParam))};
pushEvent(event);
break;
}
// Mouse right button down event
case WM_RBUTTONDOWN:
{
Event::MouseButtonPressed event;
event.button = Mouse::Button::Right;
event.position = {static_cast<std::int16_t>(LOWORD(lParam)), static_cast<std::int16_t>(HIWORD(lParam))};
pushEvent(event);
break;
}
// Mouse right button up event
case WM_RBUTTONUP:
{
Event::MouseButtonReleased event;
event.button = Mouse::Button::Right;
event.position = {static_cast<std::int16_t>(LOWORD(lParam)), static_cast<std::int16_t>(HIWORD(lParam))};
pushEvent(event);
break;
}
// Mouse wheel button down event
case WM_MBUTTONDOWN:
{
Event::MouseButtonPressed event;
event.button = Mouse::Button::Middle;
event.position = {static_cast<std::int16_t>(LOWORD(lParam)), static_cast<std::int16_t>(HIWORD(lParam))};
pushEvent(event);
break;