• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //========================================================================
2 // GLFW 3.5 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 #if defined(_GLFW_WIN32)
31 
32 #include <stdlib.h>
33 
34 static const GUID _glfw_GUID_DEVINTERFACE_HID =
35     {0x4d1e55b2,0xf16f,0x11cf,{0x88,0xcb,0x00,0x11,0x11,0x00,0x00,0x30}};
36 
37 #define GUID_DEVINTERFACE_HID _glfw_GUID_DEVINTERFACE_HID
38 
39 #if defined(_GLFW_USE_HYBRID_HPG) || defined(_GLFW_USE_OPTIMUS_HPG)
40 
41 #if defined(_GLFW_BUILD_DLL)
42  #pragma message("These symbols must be exported by the executable and have no effect in a DLL")
43 #endif
44 
45 // Executables (but not DLLs) exporting this symbol with this value will be
46 // automatically directed to the high-performance GPU on Nvidia Optimus systems
47 // with up-to-date drivers
48 //
49 __declspec(dllexport) DWORD NvOptimusEnablement = 1;
50 
51 // Executables (but not DLLs) exporting this symbol with this value will be
52 // automatically directed to the high-performance GPU on AMD PowerXpress systems
53 // with up-to-date drivers
54 //
55 __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
56 
57 #endif // _GLFW_USE_HYBRID_HPG
58 
59 #if defined(_GLFW_BUILD_DLL)
60 
61 // GLFW DLL entry point
62 //
DllMain(HINSTANCE instance,DWORD reason,LPVOID reserved)63 BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
64 {
65     return TRUE;
66 }
67 
68 #endif // _GLFW_BUILD_DLL
69 
70 // Load necessary libraries (DLLs)
71 //
loadLibraries(void)72 static GLFWbool loadLibraries(void)
73 {
74     if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
75                                 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
76                             (const WCHAR*) &_glfw,
77                             (HMODULE*) &_glfw.win32.instance))
78     {
79         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
80                              "Win32: Failed to retrieve own module handle");
81         return GLFW_FALSE;
82     }
83 
84     _glfw.win32.user32.instance = _glfwPlatformLoadModule("user32.dll");
85     if (!_glfw.win32.user32.instance)
86     {
87         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
88                              "Win32: Failed to load user32.dll");
89         return GLFW_FALSE;
90     }
91 
92     _glfw.win32.user32.SetProcessDPIAware_ = (PFN_SetProcessDPIAware)
93         _glfwPlatformGetModuleSymbol(_glfw.win32.user32.instance, "SetProcessDPIAware");
94     _glfw.win32.user32.ChangeWindowMessageFilterEx_ = (PFN_ChangeWindowMessageFilterEx)
95         _glfwPlatformGetModuleSymbol(_glfw.win32.user32.instance, "ChangeWindowMessageFilterEx");
96     _glfw.win32.user32.EnableNonClientDpiScaling_ = (PFN_EnableNonClientDpiScaling)
97         _glfwPlatformGetModuleSymbol(_glfw.win32.user32.instance, "EnableNonClientDpiScaling");
98     _glfw.win32.user32.SetProcessDpiAwarenessContext_ = (PFN_SetProcessDpiAwarenessContext)
99         _glfwPlatformGetModuleSymbol(_glfw.win32.user32.instance, "SetProcessDpiAwarenessContext");
100     _glfw.win32.user32.GetDpiForWindow_ = (PFN_GetDpiForWindow)
101         _glfwPlatformGetModuleSymbol(_glfw.win32.user32.instance, "GetDpiForWindow");
102     _glfw.win32.user32.AdjustWindowRectExForDpi_ = (PFN_AdjustWindowRectExForDpi)
103         _glfwPlatformGetModuleSymbol(_glfw.win32.user32.instance, "AdjustWindowRectExForDpi");
104     _glfw.win32.user32.GetSystemMetricsForDpi_ = (PFN_GetSystemMetricsForDpi)
105         _glfwPlatformGetModuleSymbol(_glfw.win32.user32.instance, "GetSystemMetricsForDpi");
106 
107     _glfw.win32.dinput8.instance = _glfwPlatformLoadModule("dinput8.dll");
108     if (_glfw.win32.dinput8.instance)
109     {
110         _glfw.win32.dinput8.Create = (PFN_DirectInput8Create)
111             _glfwPlatformGetModuleSymbol(_glfw.win32.dinput8.instance, "DirectInput8Create");
112     }
113 
114     {
115         int i;
116         const char* names[] =
117         {
118             "xinput1_4.dll",
119             "xinput1_3.dll",
120             "xinput9_1_0.dll",
121             "xinput1_2.dll",
122             "xinput1_1.dll",
123             NULL
124         };
125 
126         for (i = 0;  names[i];  i++)
127         {
128             _glfw.win32.xinput.instance = _glfwPlatformLoadModule(names[i]);
129             if (_glfw.win32.xinput.instance)
130             {
131                 _glfw.win32.xinput.GetCapabilities = (PFN_XInputGetCapabilities)
132                     _glfwPlatformGetModuleSymbol(_glfw.win32.xinput.instance, "XInputGetCapabilities");
133                 _glfw.win32.xinput.GetState = (PFN_XInputGetState)
134                     _glfwPlatformGetModuleSymbol(_glfw.win32.xinput.instance, "XInputGetState");
135 
136                 break;
137             }
138         }
139     }
140 
141     _glfw.win32.dwmapi.instance = _glfwPlatformLoadModule("dwmapi.dll");
142     if (_glfw.win32.dwmapi.instance)
143     {
144         _glfw.win32.dwmapi.IsCompositionEnabled = (PFN_DwmIsCompositionEnabled)
145             _glfwPlatformGetModuleSymbol(_glfw.win32.dwmapi.instance, "DwmIsCompositionEnabled");
146         _glfw.win32.dwmapi.Flush = (PFN_DwmFlush)
147             _glfwPlatformGetModuleSymbol(_glfw.win32.dwmapi.instance, "DwmFlush");
148         _glfw.win32.dwmapi.EnableBlurBehindWindow = (PFN_DwmEnableBlurBehindWindow)
149             _glfwPlatformGetModuleSymbol(_glfw.win32.dwmapi.instance, "DwmEnableBlurBehindWindow");
150         _glfw.win32.dwmapi.GetColorizationColor = (PFN_DwmGetColorizationColor)
151             _glfwPlatformGetModuleSymbol(_glfw.win32.dwmapi.instance, "DwmGetColorizationColor");
152     }
153 
154     _glfw.win32.shcore.instance = _glfwPlatformLoadModule("shcore.dll");
155     if (_glfw.win32.shcore.instance)
156     {
157         _glfw.win32.shcore.SetProcessDpiAwareness_ = (PFN_SetProcessDpiAwareness)
158             _glfwPlatformGetModuleSymbol(_glfw.win32.shcore.instance, "SetProcessDpiAwareness");
159         _glfw.win32.shcore.GetDpiForMonitor_ = (PFN_GetDpiForMonitor)
160             _glfwPlatformGetModuleSymbol(_glfw.win32.shcore.instance, "GetDpiForMonitor");
161     }
162 
163     _glfw.win32.ntdll.instance = _glfwPlatformLoadModule("ntdll.dll");
164     if (_glfw.win32.ntdll.instance)
165     {
166         _glfw.win32.ntdll.RtlVerifyVersionInfo_ = (PFN_RtlVerifyVersionInfo)
167             _glfwPlatformGetModuleSymbol(_glfw.win32.ntdll.instance, "RtlVerifyVersionInfo");
168     }
169 
170     return GLFW_TRUE;
171 }
172 
173 // Unload used libraries (DLLs)
174 //
freeLibraries(void)175 static void freeLibraries(void)
176 {
177     if (_glfw.win32.xinput.instance)
178         _glfwPlatformFreeModule(_glfw.win32.xinput.instance);
179 
180     if (_glfw.win32.dinput8.instance)
181         _glfwPlatformFreeModule(_glfw.win32.dinput8.instance);
182 
183     if (_glfw.win32.user32.instance)
184         _glfwPlatformFreeModule(_glfw.win32.user32.instance);
185 
186     if (_glfw.win32.dwmapi.instance)
187         _glfwPlatformFreeModule(_glfw.win32.dwmapi.instance);
188 
189     if (_glfw.win32.shcore.instance)
190         _glfwPlatformFreeModule(_glfw.win32.shcore.instance);
191 
192     if (_glfw.win32.ntdll.instance)
193         _glfwPlatformFreeModule(_glfw.win32.ntdll.instance);
194 }
195 
196 // Create key code translation tables
197 //
createKeyTables(void)198 static void createKeyTables(void)
199 {
200     int scancode;
201 
202     memset(_glfw.win32.keycodes, -1, sizeof(_glfw.win32.keycodes));
203     memset(_glfw.win32.scancodes, -1, sizeof(_glfw.win32.scancodes));
204 
205     _glfw.win32.keycodes[0x00B] = GLFW_KEY_0;
206     _glfw.win32.keycodes[0x002] = GLFW_KEY_1;
207     _glfw.win32.keycodes[0x003] = GLFW_KEY_2;
208     _glfw.win32.keycodes[0x004] = GLFW_KEY_3;
209     _glfw.win32.keycodes[0x005] = GLFW_KEY_4;
210     _glfw.win32.keycodes[0x006] = GLFW_KEY_5;
211     _glfw.win32.keycodes[0x007] = GLFW_KEY_6;
212     _glfw.win32.keycodes[0x008] = GLFW_KEY_7;
213     _glfw.win32.keycodes[0x009] = GLFW_KEY_8;
214     _glfw.win32.keycodes[0x00A] = GLFW_KEY_9;
215     _glfw.win32.keycodes[0x01E] = GLFW_KEY_A;
216     _glfw.win32.keycodes[0x030] = GLFW_KEY_B;
217     _glfw.win32.keycodes[0x02E] = GLFW_KEY_C;
218     _glfw.win32.keycodes[0x020] = GLFW_KEY_D;
219     _glfw.win32.keycodes[0x012] = GLFW_KEY_E;
220     _glfw.win32.keycodes[0x021] = GLFW_KEY_F;
221     _glfw.win32.keycodes[0x022] = GLFW_KEY_G;
222     _glfw.win32.keycodes[0x023] = GLFW_KEY_H;
223     _glfw.win32.keycodes[0x017] = GLFW_KEY_I;
224     _glfw.win32.keycodes[0x024] = GLFW_KEY_J;
225     _glfw.win32.keycodes[0x025] = GLFW_KEY_K;
226     _glfw.win32.keycodes[0x026] = GLFW_KEY_L;
227     _glfw.win32.keycodes[0x032] = GLFW_KEY_M;
228     _glfw.win32.keycodes[0x031] = GLFW_KEY_N;
229     _glfw.win32.keycodes[0x018] = GLFW_KEY_O;
230     _glfw.win32.keycodes[0x019] = GLFW_KEY_P;
231     _glfw.win32.keycodes[0x010] = GLFW_KEY_Q;
232     _glfw.win32.keycodes[0x013] = GLFW_KEY_R;
233     _glfw.win32.keycodes[0x01F] = GLFW_KEY_S;
234     _glfw.win32.keycodes[0x014] = GLFW_KEY_T;
235     _glfw.win32.keycodes[0x016] = GLFW_KEY_U;
236     _glfw.win32.keycodes[0x02F] = GLFW_KEY_V;
237     _glfw.win32.keycodes[0x011] = GLFW_KEY_W;
238     _glfw.win32.keycodes[0x02D] = GLFW_KEY_X;
239     _glfw.win32.keycodes[0x015] = GLFW_KEY_Y;
240     _glfw.win32.keycodes[0x02C] = GLFW_KEY_Z;
241 
242     _glfw.win32.keycodes[0x028] = GLFW_KEY_APOSTROPHE;
243     _glfw.win32.keycodes[0x02B] = GLFW_KEY_BACKSLASH;
244     _glfw.win32.keycodes[0x033] = GLFW_KEY_COMMA;
245     _glfw.win32.keycodes[0x00D] = GLFW_KEY_EQUAL;
246     _glfw.win32.keycodes[0x029] = GLFW_KEY_GRAVE_ACCENT;
247     _glfw.win32.keycodes[0x01A] = GLFW_KEY_LEFT_BRACKET;
248     _glfw.win32.keycodes[0x00C] = GLFW_KEY_MINUS;
249     _glfw.win32.keycodes[0x034] = GLFW_KEY_PERIOD;
250     _glfw.win32.keycodes[0x01B] = GLFW_KEY_RIGHT_BRACKET;
251     _glfw.win32.keycodes[0x027] = GLFW_KEY_SEMICOLON;
252     _glfw.win32.keycodes[0x035] = GLFW_KEY_SLASH;
253     _glfw.win32.keycodes[0x056] = GLFW_KEY_WORLD_2;
254 
255     _glfw.win32.keycodes[0x00E] = GLFW_KEY_BACKSPACE;
256     _glfw.win32.keycodes[0x153] = GLFW_KEY_DELETE;
257     _glfw.win32.keycodes[0x14F] = GLFW_KEY_END;
258     _glfw.win32.keycodes[0x01C] = GLFW_KEY_ENTER;
259     _glfw.win32.keycodes[0x001] = GLFW_KEY_ESCAPE;
260     _glfw.win32.keycodes[0x147] = GLFW_KEY_HOME;
261     _glfw.win32.keycodes[0x152] = GLFW_KEY_INSERT;
262     _glfw.win32.keycodes[0x15D] = GLFW_KEY_MENU;
263     _glfw.win32.keycodes[0x151] = GLFW_KEY_PAGE_DOWN;
264     _glfw.win32.keycodes[0x149] = GLFW_KEY_PAGE_UP;
265     _glfw.win32.keycodes[0x045] = GLFW_KEY_PAUSE;
266     _glfw.win32.keycodes[0x039] = GLFW_KEY_SPACE;
267     _glfw.win32.keycodes[0x00F] = GLFW_KEY_TAB;
268     _glfw.win32.keycodes[0x03A] = GLFW_KEY_CAPS_LOCK;
269     _glfw.win32.keycodes[0x145] = GLFW_KEY_NUM_LOCK;
270     _glfw.win32.keycodes[0x046] = GLFW_KEY_SCROLL_LOCK;
271     _glfw.win32.keycodes[0x03B] = GLFW_KEY_F1;
272     _glfw.win32.keycodes[0x03C] = GLFW_KEY_F2;
273     _glfw.win32.keycodes[0x03D] = GLFW_KEY_F3;
274     _glfw.win32.keycodes[0x03E] = GLFW_KEY_F4;
275     _glfw.win32.keycodes[0x03F] = GLFW_KEY_F5;
276     _glfw.win32.keycodes[0x040] = GLFW_KEY_F6;
277     _glfw.win32.keycodes[0x041] = GLFW_KEY_F7;
278     _glfw.win32.keycodes[0x042] = GLFW_KEY_F8;
279     _glfw.win32.keycodes[0x043] = GLFW_KEY_F9;
280     _glfw.win32.keycodes[0x044] = GLFW_KEY_F10;
281     _glfw.win32.keycodes[0x057] = GLFW_KEY_F11;
282     _glfw.win32.keycodes[0x058] = GLFW_KEY_F12;
283     _glfw.win32.keycodes[0x064] = GLFW_KEY_F13;
284     _glfw.win32.keycodes[0x065] = GLFW_KEY_F14;
285     _glfw.win32.keycodes[0x066] = GLFW_KEY_F15;
286     _glfw.win32.keycodes[0x067] = GLFW_KEY_F16;
287     _glfw.win32.keycodes[0x068] = GLFW_KEY_F17;
288     _glfw.win32.keycodes[0x069] = GLFW_KEY_F18;
289     _glfw.win32.keycodes[0x06A] = GLFW_KEY_F19;
290     _glfw.win32.keycodes[0x06B] = GLFW_KEY_F20;
291     _glfw.win32.keycodes[0x06C] = GLFW_KEY_F21;
292     _glfw.win32.keycodes[0x06D] = GLFW_KEY_F22;
293     _glfw.win32.keycodes[0x06E] = GLFW_KEY_F23;
294     _glfw.win32.keycodes[0x076] = GLFW_KEY_F24;
295     _glfw.win32.keycodes[0x038] = GLFW_KEY_LEFT_ALT;
296     _glfw.win32.keycodes[0x01D] = GLFW_KEY_LEFT_CONTROL;
297     _glfw.win32.keycodes[0x02A] = GLFW_KEY_LEFT_SHIFT;
298     _glfw.win32.keycodes[0x15B] = GLFW_KEY_LEFT_SUPER;
299     _glfw.win32.keycodes[0x137] = GLFW_KEY_PRINT_SCREEN;
300     _glfw.win32.keycodes[0x138] = GLFW_KEY_RIGHT_ALT;
301     _glfw.win32.keycodes[0x11D] = GLFW_KEY_RIGHT_CONTROL;
302     _glfw.win32.keycodes[0x036] = GLFW_KEY_RIGHT_SHIFT;
303     _glfw.win32.keycodes[0x15C] = GLFW_KEY_RIGHT_SUPER;
304     _glfw.win32.keycodes[0x150] = GLFW_KEY_DOWN;
305     _glfw.win32.keycodes[0x14B] = GLFW_KEY_LEFT;
306     _glfw.win32.keycodes[0x14D] = GLFW_KEY_RIGHT;
307     _glfw.win32.keycodes[0x148] = GLFW_KEY_UP;
308 
309     _glfw.win32.keycodes[0x052] = GLFW_KEY_KP_0;
310     _glfw.win32.keycodes[0x04F] = GLFW_KEY_KP_1;
311     _glfw.win32.keycodes[0x050] = GLFW_KEY_KP_2;
312     _glfw.win32.keycodes[0x051] = GLFW_KEY_KP_3;
313     _glfw.win32.keycodes[0x04B] = GLFW_KEY_KP_4;
314     _glfw.win32.keycodes[0x04C] = GLFW_KEY_KP_5;
315     _glfw.win32.keycodes[0x04D] = GLFW_KEY_KP_6;
316     _glfw.win32.keycodes[0x047] = GLFW_KEY_KP_7;
317     _glfw.win32.keycodes[0x048] = GLFW_KEY_KP_8;
318     _glfw.win32.keycodes[0x049] = GLFW_KEY_KP_9;
319     _glfw.win32.keycodes[0x04E] = GLFW_KEY_KP_ADD;
320     _glfw.win32.keycodes[0x053] = GLFW_KEY_KP_DECIMAL;
321     _glfw.win32.keycodes[0x135] = GLFW_KEY_KP_DIVIDE;
322     _glfw.win32.keycodes[0x11C] = GLFW_KEY_KP_ENTER;
323     _glfw.win32.keycodes[0x059] = GLFW_KEY_KP_EQUAL;
324     _glfw.win32.keycodes[0x037] = GLFW_KEY_KP_MULTIPLY;
325     _glfw.win32.keycodes[0x04A] = GLFW_KEY_KP_SUBTRACT;
326 
327     for (scancode = 0;  scancode < 512;  scancode++)
328     {
329         if (_glfw.win32.keycodes[scancode] > 0)
330             _glfw.win32.scancodes[_glfw.win32.keycodes[scancode]] = scancode;
331     }
332 }
333 
334 // Window procedure for the hidden helper window
335 //
helperWindowProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)336 static LRESULT CALLBACK helperWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
337 {
338     switch (uMsg)
339     {
340         case WM_DISPLAYCHANGE:
341             _glfwPollMonitorsWin32();
342             break;
343 
344         case WM_DEVICECHANGE:
345         {
346             if (!_glfw.joysticksInitialized)
347                 break;
348 
349             if (wParam == DBT_DEVICEARRIVAL)
350             {
351                 DEV_BROADCAST_HDR* dbh = (DEV_BROADCAST_HDR*) lParam;
352                 if (dbh && dbh->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
353                     _glfwDetectJoystickConnectionWin32();
354             }
355             else if (wParam == DBT_DEVICEREMOVECOMPLETE)
356             {
357                 DEV_BROADCAST_HDR* dbh = (DEV_BROADCAST_HDR*) lParam;
358                 if (dbh && dbh->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
359                     _glfwDetectJoystickDisconnectionWin32();
360             }
361 
362             break;
363         }
364     }
365 
366     return DefWindowProcW(hWnd, uMsg, wParam, lParam);
367 }
368 
369 // Creates a dummy window for behind-the-scenes work
370 //
createHelperWindow(void)371 static GLFWbool createHelperWindow(void)
372 {
373     MSG msg;
374     WNDCLASSEXW wc = { sizeof(wc) };
375 
376     wc.style         = CS_OWNDC;
377     wc.lpfnWndProc   = (WNDPROC) helperWindowProc;
378     wc.hInstance     = _glfw.win32.instance;
379     wc.lpszClassName = L"GLFW3 Helper";
380 
381     _glfw.win32.helperWindowClass = RegisterClassExW(&wc);
382     if (!_glfw.win32.helperWindowClass)
383     {
384         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
385                              "Win32: Failed to register helper window class");
386         return GLFW_FALSE;
387     }
388 
389     _glfw.win32.helperWindowHandle =
390         CreateWindowExW(WS_EX_OVERLAPPEDWINDOW,
391                         MAKEINTATOM(_glfw.win32.helperWindowClass),
392                         L"GLFW message window",
393                         WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
394                         0, 0, 1, 1,
395                         NULL, NULL,
396                         _glfw.win32.instance,
397                         NULL);
398 
399     if (!_glfw.win32.helperWindowHandle)
400     {
401         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
402                              "Win32: Failed to create helper window");
403         return GLFW_FALSE;
404     }
405 
406     // HACK: The command to the first ShowWindow call is ignored if the parent
407     //       process passed along a STARTUPINFO, so clear that with a no-op call
408     ShowWindow(_glfw.win32.helperWindowHandle, SW_HIDE);
409 
410     // Register for HID device notifications
411     {
412         DEV_BROADCAST_DEVICEINTERFACE_W dbi;
413         ZeroMemory(&dbi, sizeof(dbi));
414         dbi.dbcc_size = sizeof(dbi);
415         dbi.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
416         dbi.dbcc_classguid = GUID_DEVINTERFACE_HID;
417 
418         _glfw.win32.deviceNotificationHandle =
419             RegisterDeviceNotificationW(_glfw.win32.helperWindowHandle,
420                                         (DEV_BROADCAST_HDR*) &dbi,
421                                         DEVICE_NOTIFY_WINDOW_HANDLE);
422     }
423 
424     while (PeekMessageW(&msg, _glfw.win32.helperWindowHandle, 0, 0, PM_REMOVE))
425     {
426         TranslateMessage(&msg);
427         DispatchMessageW(&msg);
428     }
429 
430    return GLFW_TRUE;
431 }
432 
433 //////////////////////////////////////////////////////////////////////////
434 //////                       GLFW internal API                      //////
435 //////////////////////////////////////////////////////////////////////////
436 
437 // Returns a wide string version of the specified UTF-8 string
438 //
_glfwCreateWideStringFromUTF8Win32(const char * source)439 WCHAR* _glfwCreateWideStringFromUTF8Win32(const char* source)
440 {
441     WCHAR* target;
442     int count;
443 
444     count = MultiByteToWideChar(CP_UTF8, 0, source, -1, NULL, 0);
445     if (!count)
446     {
447         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
448                              "Win32: Failed to convert string from UTF-8");
449         return NULL;
450     }
451 
452     target = _glfw_calloc(count, sizeof(WCHAR));
453 
454     if (!MultiByteToWideChar(CP_UTF8, 0, source, -1, target, count))
455     {
456         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
457                              "Win32: Failed to convert string from UTF-8");
458         _glfw_free(target);
459         return NULL;
460     }
461 
462     return target;
463 }
464 
465 // Returns a UTF-8 string version of the specified wide string
466 //
_glfwCreateUTF8FromWideStringWin32(const WCHAR * source)467 char* _glfwCreateUTF8FromWideStringWin32(const WCHAR* source)
468 {
469     char* target;
470     int size;
471 
472     size = WideCharToMultiByte(CP_UTF8, 0, source, -1, NULL, 0, NULL, NULL);
473     if (!size)
474     {
475         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
476                              "Win32: Failed to convert string to UTF-8");
477         return NULL;
478     }
479 
480     target = _glfw_calloc(size, 1);
481 
482     if (!WideCharToMultiByte(CP_UTF8, 0, source, -1, target, size, NULL, NULL))
483     {
484         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
485                              "Win32: Failed to convert string to UTF-8");
486         _glfw_free(target);
487         return NULL;
488     }
489 
490     return target;
491 }
492 
493 // Reports the specified error, appending information about the last Win32 error
494 //
_glfwInputErrorWin32(int error,const char * description)495 void _glfwInputErrorWin32(int error, const char* description)
496 {
497     WCHAR buffer[_GLFW_MESSAGE_SIZE] = L"";
498     char message[_GLFW_MESSAGE_SIZE] = "";
499 
500     FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
501                        FORMAT_MESSAGE_IGNORE_INSERTS |
502                        FORMAT_MESSAGE_MAX_WIDTH_MASK,
503                    NULL,
504                    GetLastError() & 0xffff,
505                    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
506                    buffer,
507                    sizeof(buffer) / sizeof(WCHAR),
508                    NULL);
509     WideCharToMultiByte(CP_UTF8, 0, buffer, -1, message, sizeof(message), NULL, NULL);
510 
511     _glfwInputError(error, "%s: %s", description, message);
512 }
513 
514 // Updates key names according to the current keyboard layout
515 //
_glfwUpdateKeyNamesWin32(void)516 void _glfwUpdateKeyNamesWin32(void)
517 {
518     int key;
519     BYTE state[256] = {0};
520 
521     memset(_glfw.win32.keynames, 0, sizeof(_glfw.win32.keynames));
522 
523     for (key = GLFW_KEY_SPACE;  key <= GLFW_KEY_LAST;  key++)
524     {
525         UINT vk;
526         int scancode, length;
527         WCHAR chars[16];
528 
529         scancode = _glfw.win32.scancodes[key];
530         if (scancode == -1)
531             continue;
532 
533         if (key >= GLFW_KEY_KP_0 && key <= GLFW_KEY_KP_ADD)
534         {
535             const UINT vks[] = {
536                 VK_NUMPAD0,  VK_NUMPAD1,  VK_NUMPAD2, VK_NUMPAD3,
537                 VK_NUMPAD4,  VK_NUMPAD5,  VK_NUMPAD6, VK_NUMPAD7,
538                 VK_NUMPAD8,  VK_NUMPAD9,  VK_DECIMAL, VK_DIVIDE,
539                 VK_MULTIPLY, VK_SUBTRACT, VK_ADD
540             };
541 
542             vk = vks[key - GLFW_KEY_KP_0];
543         }
544         else
545             vk = MapVirtualKeyW(scancode, MAPVK_VSC_TO_VK);
546 
547         length = ToUnicode(vk, scancode, state,
548                            chars, sizeof(chars) / sizeof(WCHAR),
549                            0);
550 
551         if (length == -1)
552         {
553             // This is a dead key, so we need a second simulated key press
554             // to make it output its own character (usually a diacritic)
555             length = ToUnicode(vk, scancode, state,
556                                chars, sizeof(chars) / sizeof(WCHAR),
557                                0);
558         }
559 
560         if (length < 1)
561             continue;
562 
563         WideCharToMultiByte(CP_UTF8, 0, chars, 1,
564                             _glfw.win32.keynames[key],
565                             sizeof(_glfw.win32.keynames[key]),
566                             NULL, NULL);
567     }
568 }
569 
570 // Replacement for IsWindowsVersionOrGreater, as we cannot rely on the
571 // application having a correct embedded manifest
572 //
_glfwIsWindowsVersionOrGreaterWin32(WORD major,WORD minor,WORD sp)573 BOOL _glfwIsWindowsVersionOrGreaterWin32(WORD major, WORD minor, WORD sp)
574 {
575     OSVERSIONINFOEXW osvi = { sizeof(osvi), major, minor, 0, 0, {0}, sp };
576     DWORD mask = VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR;
577     ULONGLONG cond = VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL);
578     cond = VerSetConditionMask(cond, VER_MINORVERSION, VER_GREATER_EQUAL);
579     cond = VerSetConditionMask(cond, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
580     // HACK: Use RtlVerifyVersionInfo instead of VerifyVersionInfoW as the
581     //       latter lies unless the user knew to embed a non-default manifest
582     //       announcing support for Windows 10 via supportedOS GUID
583     return RtlVerifyVersionInfo(&osvi, mask, cond) == 0;
584 }
585 
586 // Checks whether we are on at least the specified build of Windows 10
587 //
_glfwIsWindows10BuildOrGreaterWin32(WORD build)588 BOOL _glfwIsWindows10BuildOrGreaterWin32(WORD build)
589 {
590     OSVERSIONINFOEXW osvi = { sizeof(osvi), 10, 0, build };
591     DWORD mask = VER_MAJORVERSION | VER_MINORVERSION | VER_BUILDNUMBER;
592     ULONGLONG cond = VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL);
593     cond = VerSetConditionMask(cond, VER_MINORVERSION, VER_GREATER_EQUAL);
594     cond = VerSetConditionMask(cond, VER_BUILDNUMBER, VER_GREATER_EQUAL);
595     // HACK: Use RtlVerifyVersionInfo instead of VerifyVersionInfoW as the
596     //       latter lies unless the user knew to embed a non-default manifest
597     //       announcing support for Windows 10 via supportedOS GUID
598     return RtlVerifyVersionInfo(&osvi, mask, cond) == 0;
599 }
600 
_glfwConnectWin32(int platformID,_GLFWplatform * platform)601 GLFWbool _glfwConnectWin32(int platformID, _GLFWplatform* platform)
602 {
603     const _GLFWplatform win32 =
604     {
605         .platformID = GLFW_PLATFORM_WIN32,
606         .init = _glfwInitWin32,
607         .terminate = _glfwTerminateWin32,
608         .getCursorPos = _glfwGetCursorPosWin32,
609         .setCursorPos = _glfwSetCursorPosWin32,
610         .setCursorMode = _glfwSetCursorModeWin32,
611         .setRawMouseMotion = _glfwSetRawMouseMotionWin32,
612         .rawMouseMotionSupported = _glfwRawMouseMotionSupportedWin32,
613         .createCursor = _glfwCreateCursorWin32,
614         .createStandardCursor = _glfwCreateStandardCursorWin32,
615         .destroyCursor = _glfwDestroyCursorWin32,
616         .setCursor = _glfwSetCursorWin32,
617         .getScancodeName = _glfwGetScancodeNameWin32,
618         .getKeyScancode = _glfwGetKeyScancodeWin32,
619         .setClipboardString = _glfwSetClipboardStringWin32,
620         .getClipboardString = _glfwGetClipboardStringWin32,
621         .initJoysticks = _glfwInitJoysticksWin32,
622         .terminateJoysticks = _glfwTerminateJoysticksWin32,
623         .pollJoystick = _glfwPollJoystickWin32,
624         .getMappingName = _glfwGetMappingNameWin32,
625         .updateGamepadGUID = _glfwUpdateGamepadGUIDWin32,
626         .freeMonitor = _glfwFreeMonitorWin32,
627         .getMonitorPos = _glfwGetMonitorPosWin32,
628         .getMonitorContentScale = _glfwGetMonitorContentScaleWin32,
629         .getMonitorWorkarea = _glfwGetMonitorWorkareaWin32,
630         .getVideoModes = _glfwGetVideoModesWin32,
631         .getVideoMode = _glfwGetVideoModeWin32,
632         .getGammaRamp = _glfwGetGammaRampWin32,
633         .setGammaRamp = _glfwSetGammaRampWin32,
634         .createWindow = _glfwCreateWindowWin32,
635         .destroyWindow = _glfwDestroyWindowWin32,
636         .setWindowTitle = _glfwSetWindowTitleWin32,
637         .setWindowIcon = _glfwSetWindowIconWin32,
638         .getWindowPos = _glfwGetWindowPosWin32,
639         .setWindowPos = _glfwSetWindowPosWin32,
640         .getWindowSize = _glfwGetWindowSizeWin32,
641         .setWindowSize = _glfwSetWindowSizeWin32,
642         .setWindowSizeLimits = _glfwSetWindowSizeLimitsWin32,
643         .setWindowAspectRatio = _glfwSetWindowAspectRatioWin32,
644         .getFramebufferSize = _glfwGetFramebufferSizeWin32,
645         .getWindowFrameSize = _glfwGetWindowFrameSizeWin32,
646         .getWindowContentScale = _glfwGetWindowContentScaleWin32,
647         .iconifyWindow = _glfwIconifyWindowWin32,
648         .restoreWindow = _glfwRestoreWindowWin32,
649         .maximizeWindow = _glfwMaximizeWindowWin32,
650         .showWindow = _glfwShowWindowWin32,
651         .hideWindow = _glfwHideWindowWin32,
652         .requestWindowAttention = _glfwRequestWindowAttentionWin32,
653         .focusWindow = _glfwFocusWindowWin32,
654         .setWindowMonitor = _glfwSetWindowMonitorWin32,
655         .windowFocused = _glfwWindowFocusedWin32,
656         .windowIconified = _glfwWindowIconifiedWin32,
657         .windowVisible = _glfwWindowVisibleWin32,
658         .windowMaximized = _glfwWindowMaximizedWin32,
659         .windowHovered = _glfwWindowHoveredWin32,
660         .framebufferTransparent = _glfwFramebufferTransparentWin32,
661         .getWindowOpacity = _glfwGetWindowOpacityWin32,
662         .setWindowResizable = _glfwSetWindowResizableWin32,
663         .setWindowDecorated = _glfwSetWindowDecoratedWin32,
664         .setWindowFloating = _glfwSetWindowFloatingWin32,
665         .setWindowOpacity = _glfwSetWindowOpacityWin32,
666         .setWindowMousePassthrough = _glfwSetWindowMousePassthroughWin32,
667         .pollEvents = _glfwPollEventsWin32,
668         .waitEvents = _glfwWaitEventsWin32,
669         .waitEventsTimeout = _glfwWaitEventsTimeoutWin32,
670         .postEmptyEvent = _glfwPostEmptyEventWin32,
671         .getEGLPlatform = _glfwGetEGLPlatformWin32,
672         .getEGLNativeDisplay = _glfwGetEGLNativeDisplayWin32,
673         .getEGLNativeWindow = _glfwGetEGLNativeWindowWin32,
674         .getRequiredInstanceExtensions = _glfwGetRequiredInstanceExtensionsWin32,
675         .getPhysicalDevicePresentationSupport = _glfwGetPhysicalDevicePresentationSupportWin32,
676         .createWindowSurface = _glfwCreateWindowSurfaceWin32
677     };
678 
679     *platform = win32;
680     return GLFW_TRUE;
681 }
682 
_glfwInitWin32(void)683 int _glfwInitWin32(void)
684 {
685     if (!loadLibraries())
686         return GLFW_FALSE;
687 
688     createKeyTables();
689     _glfwUpdateKeyNamesWin32();
690 
691     if (_glfwIsWindows10Version1703OrGreaterWin32())
692         SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
693     else if (IsWindows8Point1OrGreater())
694         SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
695     else if (IsWindowsVistaOrGreater())
696         SetProcessDPIAware();
697 
698     if (!createHelperWindow())
699         return GLFW_FALSE;
700 
701     _glfwPollMonitorsWin32();
702     return GLFW_TRUE;
703 }
704 
_glfwTerminateWin32(void)705 void _glfwTerminateWin32(void)
706 {
707     if (_glfw.win32.blankCursor)
708         DestroyIcon((HICON) _glfw.win32.blankCursor);
709 
710     if (_glfw.win32.deviceNotificationHandle)
711         UnregisterDeviceNotification(_glfw.win32.deviceNotificationHandle);
712 
713     if (_glfw.win32.helperWindowHandle)
714         DestroyWindow(_glfw.win32.helperWindowHandle);
715     if (_glfw.win32.helperWindowClass)
716         UnregisterClassW(MAKEINTATOM(_glfw.win32.helperWindowClass), _glfw.win32.instance);
717     if (_glfw.win32.mainWindowClass)
718         UnregisterClassW(MAKEINTATOM(_glfw.win32.mainWindowClass), _glfw.win32.instance);
719 
720     _glfw_free(_glfw.win32.clipboardString);
721     _glfw_free(_glfw.win32.rawInput);
722 
723     _glfwTerminateWGL();
724     _glfwTerminateEGL();
725     _glfwTerminateOSMesa();
726 
727     freeLibraries();
728 }
729 
730 #endif // _GLFW_WIN32
731 
732