• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //========================================================================
2 // GLFW 3.2 Win32 - www.glfw.org
3 //------------------------------------------------------------------------
4 // Copyright (c) 2002-2006 Marcus Geelnard
5 // Copyright (c) 2006-2016 Camilla Berglund <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 #include <initguid.h>
34 DEFINE_GUID(GUID_DEVINTERFACE_HID,0x4d1e55b2,0xf16f,0x11cf,0x88,0xcb,0x00,0x11,0x11,0x00,0x00,0x30);
35 
36 #if defined(_GLFW_USE_HYBRID_HPG) || defined(_GLFW_USE_OPTIMUS_HPG)
37 
38 // Applications exporting this symbol with this value will be automatically
39 // directed to the high-performance GPU on Nvidia Optimus systems with
40 // up-to-date drivers
41 //
42 __declspec(dllexport) DWORD NvOptimusEnablement = 1;
43 
44 // Applications exporting this symbol with this value will be automatically
45 // directed to the high-performance GPU on AMD PowerXpress systems with
46 // up-to-date drivers
47 //
48 __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
49 
50 #endif // _GLFW_USE_HYBRID_HPG
51 
52 #if defined(_GLFW_BUILD_DLL)
53 
54 // GLFW DLL entry point
55 //
DllMain(HINSTANCE instance,DWORD reason,LPVOID reserved)56 BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
57 {
58     return TRUE;
59 }
60 
61 #endif // _GLFW_BUILD_DLL
62 
63 // Load necessary libraries (DLLs)
64 //
loadLibraries(void)65 static GLFWbool loadLibraries(void)
66 {
67     _glfw.win32.winmm.instance = LoadLibraryA("winmm.dll");
68     if (!_glfw.win32.winmm.instance)
69     {
70         _glfwInputError(GLFW_PLATFORM_ERROR, "Win32: Failed to load winmm.dll");
71         return GLFW_FALSE;
72     }
73 
74     _glfw.win32.winmm.timeGetTime = (TIMEGETTIME_T)
75         GetProcAddress(_glfw.win32.winmm.instance, "timeGetTime");
76 
77     _glfw.win32.user32.instance = LoadLibraryA("user32.dll");
78     if (!_glfw.win32.user32.instance)
79     {
80         _glfwInputError(GLFW_PLATFORM_ERROR, "Win32: Failed to load user32.dll");
81         return GLFW_FALSE;
82     }
83 
84     _glfw.win32.user32.SetProcessDPIAware = (SETPROCESSDPIAWARE_T)
85         GetProcAddress(_glfw.win32.user32.instance, "SetProcessDPIAware");
86     _glfw.win32.user32.ChangeWindowMessageFilterEx = (CHANGEWINDOWMESSAGEFILTEREX_T)
87         GetProcAddress(_glfw.win32.user32.instance, "ChangeWindowMessageFilterEx");
88 
89     _glfw.win32.dinput8.instance = LoadLibraryA("dinput8.dll");
90     if (_glfw.win32.dinput8.instance)
91     {
92         _glfw.win32.dinput8.DirectInput8Create = (DIRECTINPUT8CREATE_T)
93             GetProcAddress(_glfw.win32.dinput8.instance, "DirectInput8Create");
94     }
95 
96     {
97         int i;
98         const char* names[] =
99         {
100             "xinput1_4.dll",
101             "xinput1_3.dll",
102             "xinput9_1_0.dll",
103             "xinput1_2.dll",
104             "xinput1_1.dll",
105             NULL
106         };
107 
108         for (i = 0;  names[i];  i++)
109         {
110             _glfw.win32.xinput.instance = LoadLibraryA(names[i]);
111             if (_glfw.win32.xinput.instance)
112             {
113                 _glfw.win32.xinput.XInputGetCapabilities = (XINPUTGETCAPABILITIES_T)
114                     GetProcAddress(_glfw.win32.xinput.instance, "XInputGetCapabilities");
115                 _glfw.win32.xinput.XInputGetState = (XINPUTGETSTATE_T)
116                     GetProcAddress(_glfw.win32.xinput.instance, "XInputGetState");
117 
118                 break;
119             }
120         }
121     }
122 
123     _glfw.win32.dwmapi.instance = LoadLibraryA("dwmapi.dll");
124     if (_glfw.win32.dwmapi.instance)
125     {
126         _glfw.win32.dwmapi.DwmIsCompositionEnabled = (DWMISCOMPOSITIONENABLED_T)
127             GetProcAddress(_glfw.win32.dwmapi.instance, "DwmIsCompositionEnabled");
128         _glfw.win32.dwmapi.DwmFlush = (DWMFLUSH_T)
129             GetProcAddress(_glfw.win32.dwmapi.instance, "DwmFlush");
130     }
131 
132     _glfw.win32.shcore.instance = LoadLibraryA("shcore.dll");
133     if (_glfw.win32.shcore.instance)
134     {
135         _glfw.win32.shcore.SetProcessDpiAwareness = (SETPROCESSDPIAWARENESS_T)
136             GetProcAddress(_glfw.win32.shcore.instance, "SetProcessDpiAwareness");
137     }
138 
139     return GLFW_TRUE;
140 }
141 
142 // Unload used libraries (DLLs)
143 //
freeLibraries(void)144 static void freeLibraries(void)
145 {
146     if (_glfw.win32.xinput.instance)
147         FreeLibrary(_glfw.win32.xinput.instance);
148 
149     if (_glfw.win32.dinput8.instance)
150         FreeLibrary(_glfw.win32.dinput8.instance);
151 
152     if (_glfw.win32.winmm.instance)
153         FreeLibrary(_glfw.win32.winmm.instance);
154 
155     if (_glfw.win32.user32.instance)
156         FreeLibrary(_glfw.win32.user32.instance);
157 
158     if (_glfw.win32.dwmapi.instance)
159         FreeLibrary(_glfw.win32.dwmapi.instance);
160 
161     if (_glfw.win32.shcore.instance)
162         FreeLibrary(_glfw.win32.shcore.instance);
163 }
164 
165 // Create key code translation tables
166 //
createKeyTables(void)167 static void createKeyTables(void)
168 {
169     int scancode;
170 
171     memset(_glfw.win32.publicKeys, -1, sizeof(_glfw.win32.publicKeys));
172     memset(_glfw.win32.nativeKeys, -1, sizeof(_glfw.win32.nativeKeys));
173 
174     _glfw.win32.publicKeys[0x00B] = GLFW_KEY_0;
175     _glfw.win32.publicKeys[0x002] = GLFW_KEY_1;
176     _glfw.win32.publicKeys[0x003] = GLFW_KEY_2;
177     _glfw.win32.publicKeys[0x004] = GLFW_KEY_3;
178     _glfw.win32.publicKeys[0x005] = GLFW_KEY_4;
179     _glfw.win32.publicKeys[0x006] = GLFW_KEY_5;
180     _glfw.win32.publicKeys[0x007] = GLFW_KEY_6;
181     _glfw.win32.publicKeys[0x008] = GLFW_KEY_7;
182     _glfw.win32.publicKeys[0x009] = GLFW_KEY_8;
183     _glfw.win32.publicKeys[0x00A] = GLFW_KEY_9;
184     _glfw.win32.publicKeys[0x01E] = GLFW_KEY_A;
185     _glfw.win32.publicKeys[0x030] = GLFW_KEY_B;
186     _glfw.win32.publicKeys[0x02E] = GLFW_KEY_C;
187     _glfw.win32.publicKeys[0x020] = GLFW_KEY_D;
188     _glfw.win32.publicKeys[0x012] = GLFW_KEY_E;
189     _glfw.win32.publicKeys[0x021] = GLFW_KEY_F;
190     _glfw.win32.publicKeys[0x022] = GLFW_KEY_G;
191     _glfw.win32.publicKeys[0x023] = GLFW_KEY_H;
192     _glfw.win32.publicKeys[0x017] = GLFW_KEY_I;
193     _glfw.win32.publicKeys[0x024] = GLFW_KEY_J;
194     _glfw.win32.publicKeys[0x025] = GLFW_KEY_K;
195     _glfw.win32.publicKeys[0x026] = GLFW_KEY_L;
196     _glfw.win32.publicKeys[0x032] = GLFW_KEY_M;
197     _glfw.win32.publicKeys[0x031] = GLFW_KEY_N;
198     _glfw.win32.publicKeys[0x018] = GLFW_KEY_O;
199     _glfw.win32.publicKeys[0x019] = GLFW_KEY_P;
200     _glfw.win32.publicKeys[0x010] = GLFW_KEY_Q;
201     _glfw.win32.publicKeys[0x013] = GLFW_KEY_R;
202     _glfw.win32.publicKeys[0x01F] = GLFW_KEY_S;
203     _glfw.win32.publicKeys[0x014] = GLFW_KEY_T;
204     _glfw.win32.publicKeys[0x016] = GLFW_KEY_U;
205     _glfw.win32.publicKeys[0x02F] = GLFW_KEY_V;
206     _glfw.win32.publicKeys[0x011] = GLFW_KEY_W;
207     _glfw.win32.publicKeys[0x02D] = GLFW_KEY_X;
208     _glfw.win32.publicKeys[0x015] = GLFW_KEY_Y;
209     _glfw.win32.publicKeys[0x02C] = GLFW_KEY_Z;
210 
211     _glfw.win32.publicKeys[0x028] = GLFW_KEY_APOSTROPHE;
212     _glfw.win32.publicKeys[0x02B] = GLFW_KEY_BACKSLASH;
213     _glfw.win32.publicKeys[0x033] = GLFW_KEY_COMMA;
214     _glfw.win32.publicKeys[0x00D] = GLFW_KEY_EQUAL;
215     _glfw.win32.publicKeys[0x029] = GLFW_KEY_GRAVE_ACCENT;
216     _glfw.win32.publicKeys[0x01A] = GLFW_KEY_LEFT_BRACKET;
217     _glfw.win32.publicKeys[0x00C] = GLFW_KEY_MINUS;
218     _glfw.win32.publicKeys[0x034] = GLFW_KEY_PERIOD;
219     _glfw.win32.publicKeys[0x01B] = GLFW_KEY_RIGHT_BRACKET;
220     _glfw.win32.publicKeys[0x027] = GLFW_KEY_SEMICOLON;
221     _glfw.win32.publicKeys[0x035] = GLFW_KEY_SLASH;
222     _glfw.win32.publicKeys[0x056] = GLFW_KEY_WORLD_2;
223 
224     _glfw.win32.publicKeys[0x00E] = GLFW_KEY_BACKSPACE;
225     _glfw.win32.publicKeys[0x153] = GLFW_KEY_DELETE;
226     _glfw.win32.publicKeys[0x14F] = GLFW_KEY_END;
227     _glfw.win32.publicKeys[0x01C] = GLFW_KEY_ENTER;
228     _glfw.win32.publicKeys[0x001] = GLFW_KEY_ESCAPE;
229     _glfw.win32.publicKeys[0x147] = GLFW_KEY_HOME;
230     _glfw.win32.publicKeys[0x152] = GLFW_KEY_INSERT;
231     _glfw.win32.publicKeys[0x15D] = GLFW_KEY_MENU;
232     _glfw.win32.publicKeys[0x151] = GLFW_KEY_PAGE_DOWN;
233     _glfw.win32.publicKeys[0x149] = GLFW_KEY_PAGE_UP;
234     _glfw.win32.publicKeys[0x045] = GLFW_KEY_PAUSE;
235     _glfw.win32.publicKeys[0x146] = GLFW_KEY_PAUSE;
236     _glfw.win32.publicKeys[0x039] = GLFW_KEY_SPACE;
237     _glfw.win32.publicKeys[0x00F] = GLFW_KEY_TAB;
238     _glfw.win32.publicKeys[0x03A] = GLFW_KEY_CAPS_LOCK;
239     _glfw.win32.publicKeys[0x145] = GLFW_KEY_NUM_LOCK;
240     _glfw.win32.publicKeys[0x046] = GLFW_KEY_SCROLL_LOCK;
241     _glfw.win32.publicKeys[0x03B] = GLFW_KEY_F1;
242     _glfw.win32.publicKeys[0x03C] = GLFW_KEY_F2;
243     _glfw.win32.publicKeys[0x03D] = GLFW_KEY_F3;
244     _glfw.win32.publicKeys[0x03E] = GLFW_KEY_F4;
245     _glfw.win32.publicKeys[0x03F] = GLFW_KEY_F5;
246     _glfw.win32.publicKeys[0x040] = GLFW_KEY_F6;
247     _glfw.win32.publicKeys[0x041] = GLFW_KEY_F7;
248     _glfw.win32.publicKeys[0x042] = GLFW_KEY_F8;
249     _glfw.win32.publicKeys[0x043] = GLFW_KEY_F9;
250     _glfw.win32.publicKeys[0x044] = GLFW_KEY_F10;
251     _glfw.win32.publicKeys[0x057] = GLFW_KEY_F11;
252     _glfw.win32.publicKeys[0x058] = GLFW_KEY_F12;
253     _glfw.win32.publicKeys[0x064] = GLFW_KEY_F13;
254     _glfw.win32.publicKeys[0x065] = GLFW_KEY_F14;
255     _glfw.win32.publicKeys[0x066] = GLFW_KEY_F15;
256     _glfw.win32.publicKeys[0x067] = GLFW_KEY_F16;
257     _glfw.win32.publicKeys[0x068] = GLFW_KEY_F17;
258     _glfw.win32.publicKeys[0x069] = GLFW_KEY_F18;
259     _glfw.win32.publicKeys[0x06A] = GLFW_KEY_F19;
260     _glfw.win32.publicKeys[0x06B] = GLFW_KEY_F20;
261     _glfw.win32.publicKeys[0x06C] = GLFW_KEY_F21;
262     _glfw.win32.publicKeys[0x06D] = GLFW_KEY_F22;
263     _glfw.win32.publicKeys[0x06E] = GLFW_KEY_F23;
264     _glfw.win32.publicKeys[0x076] = GLFW_KEY_F24;
265     _glfw.win32.publicKeys[0x038] = GLFW_KEY_LEFT_ALT;
266     _glfw.win32.publicKeys[0x01D] = GLFW_KEY_LEFT_CONTROL;
267     _glfw.win32.publicKeys[0x02A] = GLFW_KEY_LEFT_SHIFT;
268     _glfw.win32.publicKeys[0x15B] = GLFW_KEY_LEFT_SUPER;
269     _glfw.win32.publicKeys[0x137] = GLFW_KEY_PRINT_SCREEN;
270     _glfw.win32.publicKeys[0x138] = GLFW_KEY_RIGHT_ALT;
271     _glfw.win32.publicKeys[0x11D] = GLFW_KEY_RIGHT_CONTROL;
272     _glfw.win32.publicKeys[0x036] = GLFW_KEY_RIGHT_SHIFT;
273     _glfw.win32.publicKeys[0x15C] = GLFW_KEY_RIGHT_SUPER;
274     _glfw.win32.publicKeys[0x150] = GLFW_KEY_DOWN;
275     _glfw.win32.publicKeys[0x14B] = GLFW_KEY_LEFT;
276     _glfw.win32.publicKeys[0x14D] = GLFW_KEY_RIGHT;
277     _glfw.win32.publicKeys[0x148] = GLFW_KEY_UP;
278 
279     _glfw.win32.publicKeys[0x052] = GLFW_KEY_KP_0;
280     _glfw.win32.publicKeys[0x04F] = GLFW_KEY_KP_1;
281     _glfw.win32.publicKeys[0x050] = GLFW_KEY_KP_2;
282     _glfw.win32.publicKeys[0x051] = GLFW_KEY_KP_3;
283     _glfw.win32.publicKeys[0x04B] = GLFW_KEY_KP_4;
284     _glfw.win32.publicKeys[0x04C] = GLFW_KEY_KP_5;
285     _glfw.win32.publicKeys[0x04D] = GLFW_KEY_KP_6;
286     _glfw.win32.publicKeys[0x047] = GLFW_KEY_KP_7;
287     _glfw.win32.publicKeys[0x048] = GLFW_KEY_KP_8;
288     _glfw.win32.publicKeys[0x049] = GLFW_KEY_KP_9;
289     _glfw.win32.publicKeys[0x04E] = GLFW_KEY_KP_ADD;
290     _glfw.win32.publicKeys[0x053] = GLFW_KEY_KP_DECIMAL;
291     _glfw.win32.publicKeys[0x135] = GLFW_KEY_KP_DIVIDE;
292     _glfw.win32.publicKeys[0x11C] = GLFW_KEY_KP_ENTER;
293     _glfw.win32.publicKeys[0x037] = GLFW_KEY_KP_MULTIPLY;
294     _glfw.win32.publicKeys[0x04A] = GLFW_KEY_KP_SUBTRACT;
295 
296     for (scancode = 0;  scancode < 512;  scancode++)
297     {
298         if (_glfw.win32.publicKeys[scancode] > 0)
299             _glfw.win32.nativeKeys[_glfw.win32.publicKeys[scancode]] = scancode;
300     }
301 }
302 
303 // Creates a dummy window for behind-the-scenes work
304 //
createHelperWindow(void)305 static HWND createHelperWindow(void)
306 {
307     HWND window = CreateWindowExW(WS_EX_OVERLAPPEDWINDOW,
308                                   _GLFW_WNDCLASSNAME,
309                                   L"GLFW helper window",
310                                   WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
311                                   0, 0, 1, 1,
312                                   HWND_MESSAGE, NULL,
313                                   GetModuleHandleW(NULL),
314                                   NULL);
315     if (!window)
316     {
317         _glfwInputError(GLFW_PLATFORM_ERROR,
318                         "Win32: Failed to create helper window");
319         return NULL;
320     }
321 
322     // HACK: The first call to ShowWindow is ignored if the parent process
323     //       passed along a STARTUPINFO, so clear that flag with a no-op call
324     ShowWindow(window, SW_HIDE);
325 
326     // Register for HID device notifications
327     {
328         DEV_BROADCAST_DEVICEINTERFACE_W dbi;
329         ZeroMemory(&dbi, sizeof(dbi));
330         dbi.dbcc_size = sizeof(dbi);
331         dbi.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
332         dbi.dbcc_classguid = GUID_DEVINTERFACE_HID;
333 
334         RegisterDeviceNotificationW(window,
335                                     (DEV_BROADCAST_HDR*) &dbi,
336                                     DEVICE_NOTIFY_WINDOW_HANDLE);
337     }
338 
339    return window;
340 }
341 
342 
343 //////////////////////////////////////////////////////////////////////////
344 //////                       GLFW internal API                      //////
345 //////////////////////////////////////////////////////////////////////////
346 
347 // Returns a wide string version of the specified UTF-8 string
348 //
_glfwCreateWideStringFromUTF8Win32(const char * source)349 WCHAR* _glfwCreateWideStringFromUTF8Win32(const char* source)
350 {
351     WCHAR* target;
352     int length;
353 
354     length = MultiByteToWideChar(CP_UTF8, 0, source, -1, NULL, 0);
355     if (!length)
356         return NULL;
357 
358     target = calloc(length, sizeof(WCHAR));
359 
360     if (!MultiByteToWideChar(CP_UTF8, 0, source, -1, target, length))
361     {
362         free(target);
363         return NULL;
364     }
365 
366     return target;
367 }
368 
369 // Returns a UTF-8 string version of the specified wide string
370 //
_glfwCreateUTF8FromWideStringWin32(const WCHAR * source)371 char* _glfwCreateUTF8FromWideStringWin32(const WCHAR* source)
372 {
373     char* target;
374     int length;
375 
376     length = WideCharToMultiByte(CP_UTF8, 0, source, -1, NULL, 0, NULL, NULL);
377     if (!length)
378         return NULL;
379 
380     target = calloc(length, 1);
381 
382     if (!WideCharToMultiByte(CP_UTF8, 0, source, -1, target, length, NULL, NULL))
383     {
384         free(target);
385         return NULL;
386     }
387 
388     return target;
389 }
390 
391 
392 //////////////////////////////////////////////////////////////////////////
393 //////                       GLFW platform API                      //////
394 //////////////////////////////////////////////////////////////////////////
395 
_glfwPlatformInit(void)396 int _glfwPlatformInit(void)
397 {
398     if (!_glfwInitThreadLocalStorageWin32())
399         return GLFW_FALSE;
400 
401     // To make SetForegroundWindow work as we want, we need to fiddle
402     // with the FOREGROUNDLOCKTIMEOUT system setting (we do this as early
403     // as possible in the hope of still being the foreground process)
404     SystemParametersInfoW(SPI_GETFOREGROUNDLOCKTIMEOUT, 0,
405                           &_glfw.win32.foregroundLockTimeout, 0);
406     SystemParametersInfoW(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, UIntToPtr(0),
407                           SPIF_SENDCHANGE);
408 
409     if (!loadLibraries())
410         return GLFW_FALSE;
411 
412     createKeyTables();
413 
414     if (_glfw_SetProcessDpiAwareness)
415         _glfw_SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
416     else if (_glfw_SetProcessDPIAware)
417         _glfw_SetProcessDPIAware();
418 
419     if (!_glfwRegisterWindowClassWin32())
420         return GLFW_FALSE;
421 
422     _glfw.win32.helperWindowHandle = createHelperWindow();
423     if (!_glfw.win32.helperWindowHandle)
424         return GLFW_FALSE;
425 
426     _glfwPlatformPollEvents();
427 
428     _glfwInitTimerWin32();
429     _glfwInitJoysticksWin32();
430 
431     return GLFW_TRUE;
432 }
433 
_glfwPlatformTerminate(void)434 void _glfwPlatformTerminate(void)
435 {
436     if (_glfw.win32.helperWindowHandle)
437         DestroyWindow(_glfw.win32.helperWindowHandle);
438 
439     _glfwUnregisterWindowClassWin32();
440 
441     // Restore previous foreground lock timeout system setting
442     SystemParametersInfoW(SPI_SETFOREGROUNDLOCKTIMEOUT, 0,
443                           UIntToPtr(_glfw.win32.foregroundLockTimeout),
444                           SPIF_SENDCHANGE);
445 
446     free(_glfw.win32.clipboardString);
447 
448     _glfwTerminateWGL();
449     _glfwTerminateEGL();
450 
451     _glfwTerminateJoysticksWin32();
452     _glfwTerminateThreadLocalStorageWin32();
453 
454     freeLibraries();
455 }
456 
_glfwPlatformGetVersionString(void)457 const char* _glfwPlatformGetVersionString(void)
458 {
459     return _GLFW_VERSION_NUMBER " Win32 WGL EGL"
460 #if defined(__MINGW32__)
461         " MinGW"
462 #elif defined(_MSC_VER)
463         " VisualC"
464 #endif
465 #if defined(_GLFW_USE_HYBRID_HPG) || defined(_GLFW_USE_OPTIMUS_HPG)
466         " hybrid-GPU"
467 #endif
468 #if defined(_GLFW_BUILD_DLL)
469         " DLL"
470 #endif
471         ;
472 }
473 
474