Atlas - win32_init.c

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