1 //========================================================================
2 // GLFW 3.3 Win32 - www.glfw.org
3 //------------------------------------------------------------------------
4 // Copyright (c) 2002-2006 Marcus Geelnard
5 // Copyright (c) 2006-2019 Camilla Löwy <elmindreda@glfw.org>
6 //
7 // This software is provided 'as-is', without any express or implied
8 // warranty. In no event will the authors be held liable for any damages
9 // arising from the use of this software.
10 //
11 // Permission is granted to anyone to use this software for any purpose,
12 // including commercial applications, and to alter it and redistribute it
13 // freely, subject to the following restrictions:
14 //
15 // 1. The origin of this software must not be misrepresented; you must not
16 // claim that you wrote the original software. If you use this software
17 // in a product, an acknowledgment in the product documentation would
18 // be appreciated but is not required.
19 //
20 // 2. Altered source versions must be plainly marked as such, and must not
21 // be misrepresented as being the original software.
22 //
23 // 3. This notice may not be removed or altered from any source
24 // distribution.
25 //
26 //========================================================================
27
28 #include "internal.h"
29
30 #include <stdlib.h>
31 #include <malloc.h>
32
33 static const GUID _glfw_GUID_DEVINTERFACE_HID =
34 {0x4d1e55b2,0xf16f,0x11cf,{0x88,0xcb,0x00,0x11,0x11,0x00,0x00,0x30}};
35
36 #define GUID_DEVINTERFACE_HID _glfw_GUID_DEVINTERFACE_HID
37
38 #if defined(_GLFW_USE_HYBRID_HPG) || defined(_GLFW_USE_OPTIMUS_HPG)
39
40 // Executables (but not DLLs) exporting this symbol with this value will be
41 // automatically directed to the high-performance GPU on Nvidia Optimus systems
42 // with up-to-date drivers
43 //
44 __declspec(dllexport) DWORD NvOptimusEnablement = 1;
45
46 // Executables (but not DLLs) exporting this symbol with this value will be
47 // automatically directed to the high-performance GPU on AMD PowerXpress systems
48 // with up-to-date drivers
49 //
50 __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
51
52 #endif // _GLFW_USE_HYBRID_HPG
53
54 #if defined(_GLFW_BUILD_DLL)
55
56 // GLFW DLL entry point
57 //
DllMain(HINSTANCE instance,DWORD reason,LPVOID reserved)58 BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
59 {
60 return TRUE;
61 }
62
63 #endif // _GLFW_BUILD_DLL
64
65 // Load necessary libraries (DLLs)
66 //
loadLibraries(void)67 static GLFWbool loadLibraries(void)
68 {
69 _glfw.win32.winmm.instance = LoadLibraryA("winmm.dll");
70 if (!_glfw.win32.winmm.instance)
71 {
72 _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
73 "Win32: Failed to load winmm.dll");
74 return GLFW_FALSE;
75 }
76
77 _glfw.win32.winmm.GetTime = (PFN_timeGetTime)
78 GetProcAddress(_glfw.win32.winmm.instance, "timeGetTime");
79
80 _glfw.win32.user32.instance = LoadLibraryA("user32.dll");
81 if (!_glfw.win32.user32.instance)
82 {
83 _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
84 "Win32: Failed to load user32.dll");
85 return GLFW_FALSE;
86 }
87
88 _glfw.win32.user32.SetProcessDPIAware_ = (PFN_SetProcessDPIAware)
89 GetProcAddress(_glfw.win32.user32.instance, "SetProcessDPIAware");
90 _glfw.win32.user32.ChangeWindowMessageFilterEx_ = (PFN_ChangeWindowMessageFilterEx)
91 GetProcAddress(_glfw.win32.user32.instance, "ChangeWindowMessageFilterEx");
92 _glfw.win32.user32.EnableNonClientDpiScaling_ = (PFN_EnableNonClientDpiScaling)
93 GetProcAddress(_glfw.win32.user32.instance, "EnableNonClientDpiScaling");
94 _glfw.win32.user32.SetProcessDpiAwarenessContext_ = (PFN_SetProcessDpiAwarenessContext)
95 GetProcAddress(_glfw.win32.user32.instance, "SetProcessDpiAwarenessContext");
96 _glfw.win32.user32.GetDpiForWindow_ = (PFN_GetDpiForWindow)
97 GetProcAddress(_glfw.win32.user32.instance, "GetDpiForWindow");
98 _glfw.win32.user32.AdjustWindowRectExForDpi_ = (PFN_AdjustWindowRectExForDpi)
99 GetProcAddress(_glfw.win32.user32.instance, "AdjustWindowRectExForDpi");
100
101 _glfw.win32.dinput8.instance = LoadLibraryA("dinput8.dll");
102 if (_glfw.win32.dinput8.instance)
103 {
104 _glfw.win32.dinput8.Create = (PFN_DirectInput8Create)
105 GetProcAddress(_glfw.win32.dinput8.instance, "DirectInput8Create");
106 }
107
108 {
109 int i;
110 const char* names[] =
111 {
112 "xinput1_4.dll",
113 "xinput1_3.dll",
114 "xinput9_1_0.dll",
115 "xinput1_2.dll",
116 "xinput1_1.dll",
117 NULL
118 };
119
120 for (i = 0; names[i]; i++)
121 {
122 _glfw.win32.xinput.instance = LoadLibraryA(names[i]);
123 if (_glfw.win32.xinput.instance)
124 {
125 _glfw.win32.xinput.GetCapabilities = (PFN_XInputGetCapabilities)
126 GetProcAddress(_glfw.win32.xinput.instance, "XInputGetCapabilities");
127 _glfw.win32.xinput.GetState = (PFN_XInputGetState)
128 GetProcAddress(_glfw.win32.xinput.instance, "XInputGetState");
129
130 break;
131 }
132 }
133 }
134
135 _glfw.win32.dwmapi.instance = LoadLibraryA("dwmapi.dll");
136 if (_glfw.win32.dwmapi.instance)
137 {
138 _glfw.win32.dwmapi.IsCompositionEnabled = (PFN_DwmIsCompositionEnabled)
139 GetProcAddress(_glfw.win32.dwmapi.instance, "DwmIsCompositionEnabled");
140 _glfw.win32.dwmapi.Flush = (PFN_DwmFlush)
141 GetProcAddress(_glfw.win32.dwmapi.instance, "DwmFlush");
142 _glfw.win32.dwmapi.EnableBlurBehindWindow = (PFN_DwmEnableBlurBehindWindow)
143 GetProcAddress(_glfw.win32.dwmapi.instance, "DwmEnableBlurBehindWindow");
144 }
145
146 _glfw.win32.shcore.instance = LoadLibraryA("shcore.dll");
147 if (_glfw.win32.shcore.instance)
148 {
149 _glfw.win32.shcore.SetProcessDpiAwareness_ = (PFN_SetProcessDpiAwareness)
150 GetProcAddress(_glfw.win32.shcore.instance, "SetProcessDpiAwareness");
151 _glfw.win32.shcore.GetDpiForMonitor_ = (PFN_GetDpiForMonitor)
152 GetProcAddress(_glfw.win32.shcore.instance, "GetDpiForMonitor");
153 }
154
155 _glfw.win32.ntdll.instance = LoadLibraryA("ntdll.dll");
156 if (_glfw.win32.ntdll.instance)
157 {
158 _glfw.win32.ntdll.RtlVerifyVersionInfo_ = (PFN_RtlVerifyVersionInfo)
159 GetProcAddress(_glfw.win32.ntdll.instance, "RtlVerifyVersionInfo");
160 }
161
162 return GLFW_TRUE;
163 }
164
165 // Unload used libraries (DLLs)
166 //
freeLibraries(void)167 static void freeLibraries(void)
168 {
169 if (_glfw.win32.xinput.instance)
170 FreeLibrary(_glfw.win32.xinput.instance);
171
172 if (_glfw.win32.dinput8.instance)
173 FreeLibrary(_glfw.win32.dinput8.instance);
174
175 if (_glfw.win32.winmm.instance)
176 FreeLibrary(_glfw.win32.winmm.instance);
177
178 if (_glfw.win32.user32.instance)
179 FreeLibrary(_glfw.win32.user32.instance);
180
181 if (_glfw.win32.dwmapi.instance)
182 FreeLibrary(_glfw.win32.dwmapi.instance);
183
184 if (_glfw.win32.shcore.instance)
185 FreeLibrary(_glfw.win32.shcore.instance);
186
187 if (_glfw.win32.ntdll.instance)
188 FreeLibrary(_glfw.win32.ntdll.instance);
189 }
190
191 // Create key code translation tables
192 //
createKeyTables(void)193 static void createKeyTables(void)
194 {
195 int scancode;
196
197 memset(_glfw.win32.keycodes, -1, sizeof(_glfw.win32.keycodes));
198 memset(_glfw.win32.scancodes, -1, sizeof(_glfw.win32.scancodes));
199
200 _glfw.win32.keycodes[0x00B] = GLFW_KEY_0;
201 _glfw.win32.keycodes[0x002] = GLFW_KEY_1;
202 _glfw.win32.keycodes[0x003] = GLFW_KEY_2;
203 _glfw.win32.keycodes[0x004] = GLFW_KEY_3;
204 _glfw.win32.keycodes[0x005] = GLFW_KEY_4;
205 _glfw.win32.keycodes[0x006] = GLFW_KEY_5;
206 _glfw.win32.keycodes[0x007] = GLFW_KEY_6;
207 _glfw.win32.keycodes[0x008] = GLFW_KEY_7;
208 _glfw.win32.keycodes[0x009] = GLFW_KEY_8;
209 _glfw.win32.keycodes[0x00A] = GLFW_KEY_9;
210 _glfw.win32.keycodes[0x01E] = GLFW_KEY_A;
211 _glfw.win32.keycodes[0x030] = GLFW_KEY_B;
212 _glfw.win32.keycodes[0x02E] = GLFW_KEY_C;
213 _glfw.win32.keycodes[0x020] = GLFW_KEY_D;
214 _glfw.win32.keycodes[0x012] = GLFW_KEY_E;
215 _glfw.win32.keycodes[0x021] = GLFW_KEY_F;
216 _glfw.win32.keycodes[0x022] = GLFW_KEY_G;
217 _glfw.win32.keycodes[0x023] = GLFW_KEY_H;
218 _glfw.win32.keycodes[0x017] = GLFW_KEY_I;
219 _glfw.win32.keycodes[0x024] = GLFW_KEY_J;
220 _glfw.win32.keycodes[0x025] = GLFW_KEY_K;
221 _glfw.win32.keycodes[0x026] = GLFW_KEY_L;
222 _glfw.win32.keycodes[0x032] = GLFW_KEY_M;
223 _glfw.win32.keycodes[0x031] = GLFW_KEY_N;
224 _glfw.win32.keycodes[0x018] = GLFW_KEY_O;
225 _glfw.win32.keycodes[0x019] = GLFW_KEY_P;
226 _glfw.win32.keycodes[0x010] = GLFW_KEY_Q;
227 _glfw.win32.keycodes[0x013] = GLFW_KEY_R;
228 _glfw.win32.keycodes[0x01F] = GLFW_KEY_S;
229 _glfw.win32.keycodes[0x014] = GLFW_KEY_T;
230 _glfw.win32.keycodes[0x016] = GLFW_KEY_U;
231 _glfw.win32.keycodes[0x02F] = GLFW_KEY_V;
232 _glfw.win32.keycodes[0x011] = GLFW_KEY_W;
233 _glfw.win32.keycodes[0x02D] = GLFW_KEY_X;
234 _glfw.win32.keycodes[0x015] = GLFW_KEY_Y;
235 _glfw.win32.keycodes[0x02C] = GLFW_KEY_Z;
236
237 _glfw.win32.keycodes[0x028] = GLFW_KEY_APOSTROPHE;
238 _glfw.win32.keycodes[0x02B] = GLFW_KEY_BACKSLASH;
239 _glfw.win32.keycodes[0x033] = GLFW_KEY_COMMA;
240 _glfw.win32.keycodes[0x00D] = GLFW_KEY_EQUAL;
241 _glfw.win32.keycodes[0x029] = GLFW_KEY_GRAVE_ACCENT;
242 _glfw.win32.keycodes[0x01A] = GLFW_KEY_LEFT_BRACKET;
243 _glfw.win32.keycodes[0x00C] = GLFW_KEY_MINUS;
244 _glfw.win32.keycodes[0x034] = GLFW_KEY_PERIOD;
245 _glfw.win32.keycodes[0x01B] = GLFW_KEY_RIGHT_BRACKET;
246 _glfw.win32.keycodes[0x027] = GLFW_KEY_SEMICOLON;
247 _glfw.win32.keycodes[0x035] = GLFW_KEY_SLASH;
248 _glfw.win32.keycodes[0x056] = GLFW_KEY_WORLD_2;
249
250 _glfw.win32.keycodes[0x00E] = GLFW_KEY_BACKSPACE;
251 _glfw.win32.keycodes[0x153] = GLFW_KEY_DELETE;
252 _glfw.win32.keycodes[0x14F] = GLFW_KEY_END;
253 _glfw.win32.keycodes[0x01C] = GLFW_KEY_ENTER;
254 _glfw.win32.keycodes[0x001] = GLFW_KEY_ESCAPE;
255 _glfw.win32.keycodes[0x147] = GLFW_KEY_HOME;
256 _glfw.win32.keycodes[0x152] = GLFW_KEY_INSERT;
257 _glfw.win32.keycodes[0x15D] = GLFW_KEY_MENU;
258 _glfw.win32.keycodes[0x151] = GLFW_KEY_PAGE_DOWN;
259 _glfw.win32.keycodes[0x149] = GLFW_KEY_PAGE_UP;
260 _glfw.win32.keycodes[0x045] = GLFW_KEY_PAUSE;
261 _glfw.win32.keycodes[0x146] = GLFW_KEY_PAUSE;
262 _glfw.win32.keycodes[0x039] = GLFW_KEY_SPACE;
263 _glfw.win32.keycodes[0x00F] = GLFW_KEY_TAB;
264 _glfw.win32.keycodes[0x03A] = GLFW_KEY_CAPS_LOCK;
265 _glfw.win32.keycodes[0x145] = GLFW_KEY_NUM_LOCK;
266 _glfw.win32.keycodes[0x046] = GLFW_KEY_SCROLL_LOCK;
267 _glfw.win32.keycodes[0x03B] = GLFW_KEY_F1;
268 _glfw.win32.keycodes[0x03C] = GLFW_KEY_F2;
269 _glfw.win32.keycodes[0x03D] = GLFW_KEY_F3;
270 _glfw.win32.keycodes[0x03E] = GLFW_KEY_F4;
271 _glfw.win32.keycodes[0x03F] = GLFW_KEY_F5;
272 _glfw.win32.keycodes[0x040] = GLFW_KEY_F6;
273 _glfw.win32.keycodes[0x041] = GLFW_KEY_F7;
274 _glfw.win32.keycodes[0x042] = GLFW_KEY_F8;
275 _glfw.win32.keycodes[0x043] = GLFW_KEY_F9;
276 _glfw.win32.keycodes[0x044] = GLFW_KEY_F10;
277 _glfw.win32.keycodes[0x057] = GLFW_KEY_F11;
278 _glfw.win32.keycodes[0x058] = GLFW_KEY_F12;
279 _glfw.win32.keycodes[0x064] = GLFW_KEY_F13;
280 _glfw.win32.keycodes[0x065] = GLFW_KEY_F14;
281 _glfw.win32.keycodes[0x066] = GLFW_KEY_F15;
282 _glfw.win32.keycodes[0x067] = GLFW_KEY_F16;
283 _glfw.win32.keycodes[0x068] = GLFW_KEY_F17;
284 _glfw.win32.keycodes[0x069] = GLFW_KEY_F18;
285 _glfw.win32.keycodes[0x06A] = GLFW_KEY_F19;
286 _glfw.win32.keycodes[0x06B] = GLFW_KEY_F20;
287 _glfw.win32.keycodes[0x06C] = GLFW_KEY_F21;
288 _glfw.win32.keycodes[0x06D] = GLFW_KEY_F22;
289 _glfw.win32.keycodes[0x06E] = GLFW_KEY_F23;
290 _glfw.win32.keycodes[0x076] = GLFW_KEY_F24;
291 _glfw.win32.keycodes[0x038] = GLFW_KEY_LEFT_ALT;
292 _glfw.win32.keycodes[0x01D] = GLFW_KEY_LEFT_CONTROL;
293 _glfw.win32.keycodes[0x02A] = GLFW_KEY_LEFT_SHIFT;
294 _glfw.win32.keycodes[0x15B] = GLFW_KEY_LEFT_SUPER;
295 _glfw.win32.keycodes[0x137] = GLFW_KEY_PRINT_SCREEN;
296 _glfw.win32.keycodes[0x138] = GLFW_KEY_RIGHT_ALT;
297 _glfw.win32.keycodes[0x11D] = GLFW_KEY_RIGHT_CONTROL;
298 _glfw.win32.keycodes[0x036] = GLFW_KEY_RIGHT_SHIFT;
299 _glfw.win32.keycodes[0x15C] = GLFW_KEY_RIGHT_SUPER;
300 _glfw.win32.keycodes[0x150] = GLFW_KEY_DOWN;
301 _glfw.win32.keycodes[0x14B] = GLFW_KEY_LEFT;
302 _glfw.win32.keycodes[0x14D] = GLFW_KEY_RIGHT;
303 _glfw.win32.keycodes[0x148] = GLFW_KEY_UP;
304
305 _glfw.win32.keycodes[0x052] = GLFW_KEY_KP_0;
306 _glfw.win32.keycodes[0x04F] = GLFW_KEY_KP_1;
307 _glfw.win32.keycodes[0x050] = GLFW_KEY_KP_2;
308 _glfw.win32.keycodes[0x051] = GLFW_KEY_KP_3;
309 _glfw.win32.keycodes[0x04B] = GLFW_KEY_KP_4;
310 _glfw.win32.keycodes[0x04C] = GLFW_KEY_KP_5;
311 _glfw.win32.keycodes[0x04D] = GLFW_KEY_KP_6;
312 _glfw.win32.keycodes[0x047] = GLFW_KEY_KP_7;
313 _glfw.win32.keycodes[0x048] = GLFW_KEY_KP_8;
314 _glfw.win32.keycodes[0x049] = GLFW_KEY_KP_9;
315 _glfw.win32.keycodes[0x04E] = GLFW_KEY_KP_ADD;
316 _glfw.win32.keycodes[0x053] = GLFW_KEY_KP_DECIMAL;
317 _glfw.win32.keycodes[0x135] = GLFW_KEY_KP_DIVIDE;
318 _glfw.win32.keycodes[0x11C] = GLFW_KEY_KP_ENTER;
319 _glfw.win32.keycodes[0x059] = GLFW_KEY_KP_EQUAL;
320 _glfw.win32.keycodes[0x037] = GLFW_KEY_KP_MULTIPLY;
321 _glfw.win32.keycodes[0x04A] = GLFW_KEY_KP_SUBTRACT;
322
323 for (scancode = 0; scancode < 512; scancode++)
324 {
325 if (_glfw.win32.keycodes[scancode] > 0)
326 _glfw.win32.scancodes[_glfw.win32.keycodes[scancode]] = scancode;
327 }
328 }
329
330 // Creates a dummy window for behind-the-scenes work
331 //
createHelperWindow(void)332 static GLFWbool createHelperWindow(void)
333 {
334 MSG msg;
335
336 _glfw.win32.helperWindowHandle =
337 CreateWindowExW(WS_EX_OVERLAPPEDWINDOW,
338 _GLFW_WNDCLASSNAME,
339 L"GLFW message window",
340 WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
341 0, 0, 1, 1,
342 NULL, NULL,
343 GetModuleHandleW(NULL),
344 NULL);
345
346 if (!_glfw.win32.helperWindowHandle)
347 {
348 _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
349 "Win32: Failed to create helper window");
350 return GLFW_FALSE;
351 }
352
353 // HACK: The command to the first ShowWindow call is ignored if the parent
354 // process passed along a STARTUPINFO, so clear that with a no-op call
355 ShowWindow(_glfw.win32.helperWindowHandle, SW_HIDE);
356
357 // Register for HID device notifications
358 {
359 DEV_BROADCAST_DEVICEINTERFACE_W dbi;
360 ZeroMemory(&dbi, sizeof(dbi));
361 dbi.dbcc_size = sizeof(dbi);
362 dbi.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
363 dbi.dbcc_classguid = GUID_DEVINTERFACE_HID;
364
365 _glfw.win32.deviceNotificationHandle =
366 RegisterDeviceNotificationW(_glfw.win32.helperWindowHandle,
367 (DEV_BROADCAST_HDR*) &dbi,
368 DEVICE_NOTIFY_WINDOW_HANDLE);
369 }
370
371 while (PeekMessageW(&msg, _glfw.win32.helperWindowHandle, 0, 0, PM_REMOVE))
372 {
373 TranslateMessage(&msg);
374 DispatchMessageW(&msg);
375 }
376
377 return GLFW_TRUE;
378 }
379
380
381 //////////////////////////////////////////////////////////////////////////
382 ////// GLFW internal API //////
383 //////////////////////////////////////////////////////////////////////////
384
385 // Returns a wide string version of the specified UTF-8 string
386 //
_glfwCreateWideStringFromUTF8Win32(const char * source)387 WCHAR* _glfwCreateWideStringFromUTF8Win32(const char* source)
388 {
389 WCHAR* target;
390 int count;
391
392 count = MultiByteToWideChar(CP_UTF8, 0, source, -1, NULL, 0);
393 if (!count)
394 {
395 _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
396 "Win32: Failed to convert string from UTF-8");
397 return NULL;
398 }
399
400 target = calloc(count, sizeof(WCHAR));
401
402 if (!MultiByteToWideChar(CP_UTF8, 0, source, -1, target, count))
403 {
404 _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
405 "Win32: Failed to convert string from UTF-8");
406 free(target);
407 return NULL;
408 }
409
410 return target;
411 }
412
413 // Returns a UTF-8 string version of the specified wide string
414 //
_glfwCreateUTF8FromWideStringWin32(const WCHAR * source)415 char* _glfwCreateUTF8FromWideStringWin32(const WCHAR* source)
416 {
417 char* target;
418 int size;
419
420 size = WideCharToMultiByte(CP_UTF8, 0, source, -1, NULL, 0, NULL, NULL);
421 if (!size)
422 {
423 _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
424 "Win32: Failed to convert string to UTF-8");
425 return NULL;
426 }
427
428 target = calloc(size, 1);
429
430 if (!WideCharToMultiByte(CP_UTF8, 0, source, -1, target, size, NULL, NULL))
431 {
432 _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
433 "Win32: Failed to convert string to UTF-8");
434 free(target);
435 return NULL;
436 }
437
438 return target;
439 }
440
441 // Reports the specified error, appending information about the last Win32 error
442 //
_glfwInputErrorWin32(int error,const char * description)443 void _glfwInputErrorWin32(int error, const char* description)
444 {
445 WCHAR buffer[_GLFW_MESSAGE_SIZE] = L"";
446 char message[_GLFW_MESSAGE_SIZE] = "";
447
448 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
449 FORMAT_MESSAGE_IGNORE_INSERTS |
450 FORMAT_MESSAGE_MAX_WIDTH_MASK,
451 NULL,
452 GetLastError() & 0xffff,
453 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
454 buffer,
455 sizeof(buffer) / sizeof(WCHAR),
456 NULL);
457 WideCharToMultiByte(CP_UTF8, 0, buffer, -1, message, sizeof(message), NULL, NULL);
458
459 _glfwInputError(error, "%s: %s", description, message);
460 }
461
462 // Updates key names according to the current keyboard layout
463 //
_glfwUpdateKeyNamesWin32(void)464 void _glfwUpdateKeyNamesWin32(void)
465 {
466 int key;
467 BYTE state[256] = {0};
468
469 memset(_glfw.win32.keynames, 0, sizeof(_glfw.win32.keynames));
470
471 for (key = GLFW_KEY_SPACE; key <= GLFW_KEY_LAST; key++)
472 {
473 UINT vk;
474 int scancode, length;
475 WCHAR chars[16];
476
477 scancode = _glfw.win32.scancodes[key];
478 if (scancode == -1)
479 continue;
480
481 if (key >= GLFW_KEY_KP_0 && key <= GLFW_KEY_KP_ADD)
482 {
483 const UINT vks[] = {
484 VK_NUMPAD0, VK_NUMPAD1, VK_NUMPAD2, VK_NUMPAD3,
485 VK_NUMPAD4, VK_NUMPAD5, VK_NUMPAD6, VK_NUMPAD7,
486 VK_NUMPAD8, VK_NUMPAD9, VK_DECIMAL, VK_DIVIDE,
487 VK_MULTIPLY, VK_SUBTRACT, VK_ADD
488 };
489
490 vk = vks[key - GLFW_KEY_KP_0];
491 }
492 else
493 vk = MapVirtualKey(scancode, MAPVK_VSC_TO_VK);
494
495 length = ToUnicode(vk, scancode, state,
496 chars, sizeof(chars) / sizeof(WCHAR),
497 0);
498
499 if (length == -1)
500 {
501 length = ToUnicode(vk, scancode, state,
502 chars, sizeof(chars) / sizeof(WCHAR),
503 0);
504 }
505
506 if (length < 1)
507 continue;
508
509 WideCharToMultiByte(CP_UTF8, 0, chars, 1,
510 _glfw.win32.keynames[key],
511 sizeof(_glfw.win32.keynames[key]),
512 NULL, NULL);
513 }
514 }
515
516 // Replacement for IsWindowsVersionOrGreater as MinGW lacks versionhelpers.h
517 //
_glfwIsWindowsVersionOrGreaterWin32(WORD major,WORD minor,WORD sp)518 BOOL _glfwIsWindowsVersionOrGreaterWin32(WORD major, WORD minor, WORD sp)
519 {
520 OSVERSIONINFOEXW osvi = { sizeof(osvi), major, minor, 0, 0, {0}, sp };
521 DWORD mask = VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR;
522 ULONGLONG cond = VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL);
523 cond = VerSetConditionMask(cond, VER_MINORVERSION, VER_GREATER_EQUAL);
524 cond = VerSetConditionMask(cond, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
525 // HACK: Use RtlVerifyVersionInfo instead of VerifyVersionInfoW as the
526 // latter lies unless the user knew to embedd a non-default manifest
527 // announcing support for Windows 10 via supportedOS GUID
528 return RtlVerifyVersionInfo(&osvi, mask, cond) == 0;
529 }
530
531 // Checks whether we are on at least the specified build of Windows 10
532 //
_glfwIsWindows10BuildOrGreaterWin32(WORD build)533 BOOL _glfwIsWindows10BuildOrGreaterWin32(WORD build)
534 {
535 OSVERSIONINFOEXW osvi = { sizeof(osvi), 10, 0, build };
536 DWORD mask = VER_MAJORVERSION | VER_MINORVERSION | VER_BUILDNUMBER;
537 ULONGLONG cond = VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL);
538 cond = VerSetConditionMask(cond, VER_MINORVERSION, VER_GREATER_EQUAL);
539 cond = VerSetConditionMask(cond, VER_BUILDNUMBER, VER_GREATER_EQUAL);
540 // HACK: Use RtlVerifyVersionInfo instead of VerifyVersionInfoW as the
541 // latter lies unless the user knew to embedd a non-default manifest
542 // announcing support for Windows 10 via supportedOS GUID
543 return RtlVerifyVersionInfo(&osvi, mask, cond) == 0;
544 }
545
546
547 //////////////////////////////////////////////////////////////////////////
548 ////// GLFW platform API //////
549 //////////////////////////////////////////////////////////////////////////
550
_glfwPlatformInit(void)551 int _glfwPlatformInit(void)
552 {
553 // To make SetForegroundWindow work as we want, we need to fiddle
554 // with the FOREGROUNDLOCKTIMEOUT system setting (we do this as early
555 // as possible in the hope of still being the foreground process)
556 SystemParametersInfoW(SPI_GETFOREGROUNDLOCKTIMEOUT, 0,
557 &_glfw.win32.foregroundLockTimeout, 0);
558 SystemParametersInfoW(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, UIntToPtr(0),
559 SPIF_SENDCHANGE);
560
561 if (!loadLibraries())
562 return GLFW_FALSE;
563
564 createKeyTables();
565 _glfwUpdateKeyNamesWin32();
566
567 if (_glfwIsWindows10CreatorsUpdateOrGreaterWin32())
568 SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
569 else if (IsWindows8Point1OrGreater())
570 SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
571 else if (IsWindowsVistaOrGreater())
572 SetProcessDPIAware();
573
574 if (!_glfwRegisterWindowClassWin32())
575 return GLFW_FALSE;
576
577 if (!createHelperWindow())
578 return GLFW_FALSE;
579
580 _glfwInitTimerWin32();
581 _glfwInitJoysticksWin32();
582
583 _glfwPollMonitorsWin32();
584 return GLFW_TRUE;
585 }
586
_glfwPlatformTerminate(void)587 void _glfwPlatformTerminate(void)
588 {
589 if (_glfw.win32.deviceNotificationHandle)
590 UnregisterDeviceNotification(_glfw.win32.deviceNotificationHandle);
591
592 if (_glfw.win32.helperWindowHandle)
593 DestroyWindow(_glfw.win32.helperWindowHandle);
594
595 _glfwUnregisterWindowClassWin32();
596
597 // Restore previous foreground lock timeout system setting
598 SystemParametersInfoW(SPI_SETFOREGROUNDLOCKTIMEOUT, 0,
599 UIntToPtr(_glfw.win32.foregroundLockTimeout),
600 SPIF_SENDCHANGE);
601
602 free(_glfw.win32.clipboardString);
603 free(_glfw.win32.rawInput);
604
605 _glfwTerminateWGL();
606 _glfwTerminateEGL();
607
608 _glfwTerminateJoysticksWin32();
609
610 freeLibraries();
611 }
612
_glfwPlatformGetVersionString(void)613 const char* _glfwPlatformGetVersionString(void)
614 {
615 return _GLFW_VERSION_NUMBER " Win32 WGL EGL OSMesa"
616 #if defined(__MINGW32__)
617 " MinGW"
618 #elif defined(_MSC_VER)
619 " VisualC"
620 #endif
621 #if defined(_GLFW_USE_HYBRID_HPG) || defined(_GLFW_USE_OPTIMUS_HPG)
622 " hybrid-GPU"
623 #endif
624 #if defined(_GLFW_BUILD_DLL)
625 " DLL"
626 #endif
627 ;
628 }
629
630