win32_init.c 23 KB

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