DSCF

.co.uk

PC32

A ColorForth compiler and editor for x86-64
git clone https://dscf.co.uk/clone/PC32.git
Log | Files | Refs | README

win32_init.c (23700B)


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