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_window.c (74337B)


      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 <limits.h>
     33 #include <stdlib.h>
     34 #include <malloc.h>
     35 #include <string.h>
     36 #include <windowsx.h>
     37 #include <shellapi.h>
     38 
     39 // Returns the window style for the specified window
     40 //
     41 static DWORD getWindowStyle(const _GLFWwindow* window)
     42 {
     43     DWORD style = WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
     44 
     45     if (window->monitor)
     46         style |= WS_POPUP;
     47     else
     48     {
     49         style |= WS_SYSMENU | WS_MINIMIZEBOX;
     50 
     51         if (window->decorated)
     52         {
     53             style |= WS_CAPTION;
     54 
     55             if (window->resizable)
     56                 style |= WS_MAXIMIZEBOX | WS_THICKFRAME;
     57         }
     58         else
     59             style |= WS_POPUP;
     60     }
     61 
     62     return style;
     63 }
     64 
     65 // Returns the extended window style for the specified window
     66 //
     67 static DWORD getWindowExStyle(const _GLFWwindow* window)
     68 {
     69     DWORD style = WS_EX_APPWINDOW;
     70 
     71     if (window->monitor || window->floating)
     72         style |= WS_EX_TOPMOST;
     73 
     74     return style;
     75 }
     76 
     77 // Returns the image whose area most closely matches the desired one
     78 //
     79 static const GLFWimage* chooseImage(int count, const GLFWimage* images,
     80                                     int width, int height)
     81 {
     82     int i, leastDiff = INT_MAX;
     83     const GLFWimage* closest = NULL;
     84 
     85     for (i = 0;  i < count;  i++)
     86     {
     87         const int currDiff = abs(images[i].width * images[i].height -
     88                                  width * height);
     89         if (currDiff < leastDiff)
     90         {
     91             closest = images + i;
     92             leastDiff = currDiff;
     93         }
     94     }
     95 
     96     return closest;
     97 }
     98 
     99 // Creates an RGBA icon or cursor
    100 //
    101 static HICON createIcon(const GLFWimage* image,
    102                         int xhot, int yhot, GLFWbool icon)
    103 {
    104     int i;
    105     HDC dc;
    106     HICON handle;
    107     HBITMAP color, mask;
    108     BITMAPV5HEADER bi;
    109     ICONINFO ii;
    110     unsigned char* target = NULL;
    111     unsigned char* source = image->pixels;
    112 
    113     ZeroMemory(&bi, sizeof(bi));
    114     bi.bV5Size        = sizeof(bi);
    115     bi.bV5Width       = image->width;
    116     bi.bV5Height      = -image->height;
    117     bi.bV5Planes      = 1;
    118     bi.bV5BitCount    = 32;
    119     bi.bV5Compression = BI_BITFIELDS;
    120     bi.bV5RedMask     = 0x00ff0000;
    121     bi.bV5GreenMask   = 0x0000ff00;
    122     bi.bV5BlueMask    = 0x000000ff;
    123     bi.bV5AlphaMask   = 0xff000000;
    124 
    125     dc = GetDC(NULL);
    126     color = CreateDIBSection(dc,
    127                              (BITMAPINFO*) &bi,
    128                              DIB_RGB_COLORS,
    129                              (void**) &target,
    130                              NULL,
    131                              (DWORD) 0);
    132     ReleaseDC(NULL, dc);
    133 
    134     if (!color)
    135     {
    136         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
    137                              "Win32: Failed to create RGBA bitmap");
    138         return NULL;
    139     }
    140 
    141     mask = CreateBitmap(image->width, image->height, 1, 1, NULL);
    142     if (!mask)
    143     {
    144         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
    145                              "Win32: Failed to create mask bitmap");
    146         DeleteObject(color);
    147         return NULL;
    148     }
    149 
    150     for (i = 0;  i < image->width * image->height;  i++)
    151     {
    152         target[0] = source[2];
    153         target[1] = source[1];
    154         target[2] = source[0];
    155         target[3] = source[3];
    156         target += 4;
    157         source += 4;
    158     }
    159 
    160     ZeroMemory(&ii, sizeof(ii));
    161     ii.fIcon    = icon;
    162     ii.xHotspot = xhot;
    163     ii.yHotspot = yhot;
    164     ii.hbmMask  = mask;
    165     ii.hbmColor = color;
    166 
    167     handle = CreateIconIndirect(&ii);
    168 
    169     DeleteObject(color);
    170     DeleteObject(mask);
    171 
    172     if (!handle)
    173     {
    174         if (icon)
    175         {
    176             _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
    177                                  "Win32: Failed to create icon");
    178         }
    179         else
    180         {
    181             _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
    182                                  "Win32: Failed to create cursor");
    183         }
    184     }
    185 
    186     return handle;
    187 }
    188 
    189 // Translate content area size to full window size according to styles and DPI
    190 //
    191 static void getFullWindowSize(DWORD style, DWORD exStyle,
    192                               int contentWidth, int contentHeight,
    193                               int* fullWidth, int* fullHeight,
    194                               UINT dpi)
    195 {
    196     RECT rect = { 0, 0, contentWidth, contentHeight };
    197 
    198     if (_glfwIsWindows10AnniversaryUpdateOrGreaterWin32())
    199         AdjustWindowRectExForDpi(&rect, style, FALSE, exStyle, dpi);
    200     else
    201         AdjustWindowRectEx(&rect, style, FALSE, exStyle);
    202 
    203     *fullWidth = rect.right - rect.left;
    204     *fullHeight = rect.bottom - rect.top;
    205 }
    206 
    207 // Enforce the content area aspect ratio based on which edge is being dragged
    208 //
    209 static void applyAspectRatio(_GLFWwindow* window, int edge, RECT* area)
    210 {
    211     int xoff, yoff;
    212     UINT dpi = USER_DEFAULT_SCREEN_DPI;
    213     const float ratio = (float) window->numer / (float) window->denom;
    214 
    215     if (_glfwIsWindows10AnniversaryUpdateOrGreaterWin32())
    216         dpi = GetDpiForWindow(window->win32.handle);
    217 
    218     getFullWindowSize(getWindowStyle(window), getWindowExStyle(window),
    219                       0, 0, &xoff, &yoff, dpi);
    220 
    221     if (edge == WMSZ_LEFT  || edge == WMSZ_BOTTOMLEFT ||
    222         edge == WMSZ_RIGHT || edge == WMSZ_BOTTOMRIGHT)
    223     {
    224         area->bottom = area->top + yoff +
    225             (int) ((area->right - area->left - xoff) / ratio);
    226     }
    227     else if (edge == WMSZ_TOPLEFT || edge == WMSZ_TOPRIGHT)
    228     {
    229         area->top = area->bottom - yoff -
    230             (int) ((area->right - area->left - xoff) / ratio);
    231     }
    232     else if (edge == WMSZ_TOP || edge == WMSZ_BOTTOM)
    233     {
    234         area->right = area->left + xoff +
    235             (int) ((area->bottom - area->top - yoff) * ratio);
    236     }
    237 }
    238 
    239 // Updates the cursor image according to its cursor mode
    240 //
    241 static void updateCursorImage(_GLFWwindow* window)
    242 {
    243     if (window->cursorMode == GLFW_CURSOR_NORMAL)
    244     {
    245         if (window->cursor)
    246             SetCursor(window->cursor->win32.handle);
    247         else
    248             SetCursor(LoadCursorW(NULL, IDC_ARROW));
    249     }
    250     else
    251         SetCursor(NULL);
    252 }
    253 
    254 // Updates the cursor clip rect
    255 //
    256 static void updateClipRect(_GLFWwindow* window)
    257 {
    258     if (window)
    259     {
    260         RECT clipRect;
    261         GetClientRect(window->win32.handle, &clipRect);
    262         ClientToScreen(window->win32.handle, (POINT*) &clipRect.left);
    263         ClientToScreen(window->win32.handle, (POINT*) &clipRect.right);
    264         ClipCursor(&clipRect);
    265     }
    266     else
    267         ClipCursor(NULL);
    268 }
    269 
    270 // Enables WM_INPUT messages for the mouse for the specified window
    271 //
    272 static void enableRawMouseMotion(_GLFWwindow* window)
    273 {
    274     const RAWINPUTDEVICE rid = { 0x01, 0x02, 0, window->win32.handle };
    275 
    276     if (!RegisterRawInputDevices(&rid, 1, sizeof(rid)))
    277     {
    278         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
    279                              "Win32: Failed to register raw input device");
    280     }
    281 }
    282 
    283 // Disables WM_INPUT messages for the mouse
    284 //
    285 static void disableRawMouseMotion(_GLFWwindow* window)
    286 {
    287     const RAWINPUTDEVICE rid = { 0x01, 0x02, RIDEV_REMOVE, NULL };
    288 
    289     if (!RegisterRawInputDevices(&rid, 1, sizeof(rid)))
    290     {
    291         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
    292                              "Win32: Failed to remove raw input device");
    293     }
    294 }
    295 
    296 // Apply disabled cursor mode to a focused window
    297 //
    298 static void disableCursor(_GLFWwindow* window)
    299 {
    300     _glfw.win32.disabledCursorWindow = window;
    301     _glfwPlatformGetCursorPos(window,
    302                               &_glfw.win32.restoreCursorPosX,
    303                               &_glfw.win32.restoreCursorPosY);
    304     updateCursorImage(window);
    305     _glfwCenterCursorInContentArea(window);
    306     updateClipRect(window);
    307 
    308     if (window->rawMouseMotion)
    309         enableRawMouseMotion(window);
    310 }
    311 
    312 // Exit disabled cursor mode for the specified window
    313 //
    314 static void enableCursor(_GLFWwindow* window)
    315 {
    316     if (window->rawMouseMotion)
    317         disableRawMouseMotion(window);
    318 
    319     _glfw.win32.disabledCursorWindow = NULL;
    320     updateClipRect(NULL);
    321     _glfwPlatformSetCursorPos(window,
    322                               _glfw.win32.restoreCursorPosX,
    323                               _glfw.win32.restoreCursorPosY);
    324     updateCursorImage(window);
    325 }
    326 
    327 // Returns whether the cursor is in the content area of the specified window
    328 //
    329 static GLFWbool cursorInContentArea(_GLFWwindow* window)
    330 {
    331     RECT area;
    332     POINT pos;
    333 
    334     if (!GetCursorPos(&pos))
    335         return GLFW_FALSE;
    336 
    337     if (WindowFromPoint(pos) != window->win32.handle)
    338         return GLFW_FALSE;
    339 
    340     GetClientRect(window->win32.handle, &area);
    341     ClientToScreen(window->win32.handle, (POINT*) &area.left);
    342     ClientToScreen(window->win32.handle, (POINT*) &area.right);
    343 
    344     return PtInRect(&area, pos);
    345 }
    346 
    347 // Update native window styles to match attributes
    348 //
    349 static void updateWindowStyles(const _GLFWwindow* window)
    350 {
    351     RECT rect;
    352     DWORD style = GetWindowLongW(window->win32.handle, GWL_STYLE);
    353     style &= ~(WS_OVERLAPPEDWINDOW | WS_POPUP);
    354     style |= getWindowStyle(window);
    355 
    356     GetClientRect(window->win32.handle, &rect);
    357 
    358     if (_glfwIsWindows10AnniversaryUpdateOrGreaterWin32())
    359     {
    360         AdjustWindowRectExForDpi(&rect, style, FALSE,
    361                                  getWindowExStyle(window),
    362                                  GetDpiForWindow(window->win32.handle));
    363     }
    364     else
    365         AdjustWindowRectEx(&rect, style, FALSE, getWindowExStyle(window));
    366 
    367     ClientToScreen(window->win32.handle, (POINT*) &rect.left);
    368     ClientToScreen(window->win32.handle, (POINT*) &rect.right);
    369     SetWindowLongW(window->win32.handle, GWL_STYLE, style);
    370     SetWindowPos(window->win32.handle, HWND_TOP,
    371                  rect.left, rect.top,
    372                  rect.right - rect.left, rect.bottom - rect.top,
    373                  SWP_FRAMECHANGED | SWP_NOACTIVATE | SWP_NOZORDER);
    374 }
    375 
    376 // Update window framebuffer transparency
    377 //
    378 static void updateFramebufferTransparency(const _GLFWwindow* window)
    379 {
    380     BOOL composition, opaque;
    381     DWORD color;
    382 
    383     if (!IsWindowsVistaOrGreater())
    384         return;
    385 
    386     if (FAILED(DwmIsCompositionEnabled(&composition)) || !composition)
    387        return;
    388 
    389     if (IsWindows8OrGreater() ||
    390         (SUCCEEDED(DwmGetColorizationColor(&color, &opaque)) && !opaque))
    391     {
    392         HRGN region = CreateRectRgn(0, 0, -1, -1);
    393         DWM_BLURBEHIND bb = {0};
    394         bb.dwFlags = DWM_BB_ENABLE | DWM_BB_BLURREGION;
    395         bb.hRgnBlur = region;
    396         bb.fEnable = TRUE;
    397 
    398         DwmEnableBlurBehindWindow(window->win32.handle, &bb);
    399         DeleteObject(region);
    400     }
    401     else
    402     {
    403         // HACK: Disable framebuffer transparency on Windows 7 when the
    404         //       colorization color is opaque, because otherwise the window
    405         //       contents is blended additively with the previous frame instead
    406         //       of replacing it
    407         DWM_BLURBEHIND bb = {0};
    408         bb.dwFlags = DWM_BB_ENABLE;
    409         DwmEnableBlurBehindWindow(window->win32.handle, &bb);
    410     }
    411 }
    412 
    413 // Retrieves and translates modifier keys
    414 //
    415 static int getKeyMods(void)
    416 {
    417     int mods = 0;
    418 
    419     if (GetKeyState(VK_SHIFT) & 0x8000)
    420         mods |= GLFW_MOD_SHIFT;
    421     if (GetKeyState(VK_CONTROL) & 0x8000)
    422         mods |= GLFW_MOD_CONTROL;
    423     if (GetKeyState(VK_MENU) & 0x8000)
    424         mods |= GLFW_MOD_ALT;
    425     if ((GetKeyState(VK_LWIN) | GetKeyState(VK_RWIN)) & 0x8000)
    426         mods |= GLFW_MOD_SUPER;
    427     if (GetKeyState(VK_CAPITAL) & 1)
    428         mods |= GLFW_MOD_CAPS_LOCK;
    429     if (GetKeyState(VK_NUMLOCK) & 1)
    430         mods |= GLFW_MOD_NUM_LOCK;
    431 
    432     return mods;
    433 }
    434 
    435 static void fitToMonitor(_GLFWwindow* window)
    436 {
    437     MONITORINFO mi = { sizeof(mi) };
    438     GetMonitorInfoW(window->monitor->win32.handle, &mi);
    439     SetWindowPos(window->win32.handle, HWND_TOPMOST,
    440                  mi.rcMonitor.left,
    441                  mi.rcMonitor.top,
    442                  mi.rcMonitor.right - mi.rcMonitor.left,
    443                  mi.rcMonitor.bottom - mi.rcMonitor.top,
    444                  SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOCOPYBITS);
    445 }
    446 
    447 // Make the specified window and its video mode active on its monitor
    448 //
    449 static void acquireMonitor(_GLFWwindow* window)
    450 {
    451     if (!_glfw.win32.acquiredMonitorCount)
    452     {
    453         SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED);
    454 
    455         // HACK: When mouse trails are enabled the cursor becomes invisible when
    456         //       the OpenGL ICD switches to page flipping
    457         if (IsWindowsXPOrGreater())
    458         {
    459             SystemParametersInfoW(SPI_GETMOUSETRAILS, 0, &_glfw.win32.mouseTrailSize, 0);
    460             SystemParametersInfoW(SPI_SETMOUSETRAILS, 0, 0, 0);
    461         }
    462     }
    463 
    464     if (!window->monitor->window)
    465         _glfw.win32.acquiredMonitorCount++;
    466 
    467     _glfwSetVideoModeWin32(window->monitor, &window->videoMode);
    468     _glfwInputMonitorWindow(window->monitor, window);
    469 }
    470 
    471 // Remove the window and restore the original video mode
    472 //
    473 static void releaseMonitor(_GLFWwindow* window)
    474 {
    475     if (window->monitor->window != window)
    476         return;
    477 
    478     _glfw.win32.acquiredMonitorCount--;
    479     if (!_glfw.win32.acquiredMonitorCount)
    480     {
    481         SetThreadExecutionState(ES_CONTINUOUS);
    482 
    483         // HACK: Restore mouse trail length saved in acquireMonitor
    484         if (IsWindowsXPOrGreater())
    485             SystemParametersInfoW(SPI_SETMOUSETRAILS, _glfw.win32.mouseTrailSize, 0, 0);
    486     }
    487 
    488     _glfwInputMonitorWindow(window->monitor, NULL);
    489     _glfwRestoreVideoModeWin32(window->monitor);
    490 }
    491 
    492 // Manually maximize the window, for when SW_MAXIMIZE cannot be used
    493 //
    494 static void maximizeWindowManually(_GLFWwindow* window)
    495 {
    496     RECT rect;
    497     DWORD style;
    498     MONITORINFO mi = { sizeof(mi) };
    499 
    500     GetMonitorInfoW(MonitorFromWindow(window->win32.handle,
    501                                       MONITOR_DEFAULTTONEAREST), &mi);
    502 
    503     rect = mi.rcWork;
    504 
    505     if (window->maxwidth != GLFW_DONT_CARE && window->maxheight != GLFW_DONT_CARE)
    506     {
    507         rect.right = _glfw_min(rect.right, rect.left + window->maxwidth);
    508         rect.bottom = _glfw_min(rect.bottom, rect.top + window->maxheight);
    509     }
    510 
    511     style = GetWindowLongW(window->win32.handle, GWL_STYLE);
    512     style |= WS_MAXIMIZE;
    513     SetWindowLongW(window->win32.handle, GWL_STYLE, style);
    514 
    515     if (window->decorated)
    516     {
    517         const DWORD exStyle = GetWindowLongW(window->win32.handle, GWL_EXSTYLE);
    518 
    519         if (_glfwIsWindows10AnniversaryUpdateOrGreaterWin32())
    520         {
    521             const UINT dpi = GetDpiForWindow(window->win32.handle);
    522             AdjustWindowRectExForDpi(&rect, style, FALSE, exStyle, dpi);
    523             OffsetRect(&rect, 0, GetSystemMetricsForDpi(SM_CYCAPTION, dpi));
    524         }
    525         else
    526         {
    527             AdjustWindowRectEx(&rect, style, FALSE, exStyle);
    528             OffsetRect(&rect, 0, GetSystemMetrics(SM_CYCAPTION));
    529         }
    530 
    531         rect.bottom = _glfw_min(rect.bottom, mi.rcWork.bottom);
    532     }
    533 
    534     SetWindowPos(window->win32.handle, HWND_TOP,
    535                  rect.left,
    536                  rect.top,
    537                  rect.right - rect.left,
    538                  rect.bottom - rect.top,
    539                  SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED);
    540 }
    541 
    542 // Window callback function (handles window messages)
    543 //
    544 static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg,
    545                                    WPARAM wParam, LPARAM lParam)
    546 {
    547     _GLFWwindow* window = GetPropW(hWnd, L"GLFW");
    548     if (!window)
    549     {
    550         // This is the message handling for the hidden helper window
    551         // and for a regular window during its initial creation
    552 
    553         switch (uMsg)
    554         {
    555             case WM_NCCREATE:
    556             {
    557                 if (_glfwIsWindows10AnniversaryUpdateOrGreaterWin32())
    558                 {
    559                     const CREATESTRUCTW* cs = (const CREATESTRUCTW*) lParam;
    560                     const _GLFWwndconfig* wndconfig = cs->lpCreateParams;
    561 
    562                     // On per-monitor DPI aware V1 systems, only enable
    563                     // non-client scaling for windows that scale the client area
    564                     // We need WM_GETDPISCALEDSIZE from V2 to keep the client
    565                     // area static when the non-client area is scaled
    566                     if (wndconfig && wndconfig->scaleToMonitor)
    567                         EnableNonClientDpiScaling(hWnd);
    568                 }
    569 
    570                 break;
    571             }
    572 
    573             case WM_DISPLAYCHANGE:
    574                 _glfwPollMonitorsWin32();
    575                 break;
    576 
    577             case WM_DEVICECHANGE:
    578             {
    579                 if (wParam == DBT_DEVICEARRIVAL)
    580                 {
    581                     DEV_BROADCAST_HDR* dbh = (DEV_BROADCAST_HDR*) lParam;
    582                     if (dbh && dbh->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
    583                         _glfwDetectJoystickConnectionWin32();
    584                 }
    585                 else if (wParam == DBT_DEVICEREMOVECOMPLETE)
    586                 {
    587                     DEV_BROADCAST_HDR* dbh = (DEV_BROADCAST_HDR*) lParam;
    588                     if (dbh && dbh->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
    589                         _glfwDetectJoystickDisconnectionWin32();
    590                 }
    591 
    592                 break;
    593             }
    594         }
    595 
    596         return DefWindowProcW(hWnd, uMsg, wParam, lParam);
    597     }
    598 
    599     switch (uMsg)
    600     {
    601         case WM_MOUSEACTIVATE:
    602         {
    603             // HACK: Postpone cursor disabling when the window was activated by
    604             //       clicking a caption button
    605             if (HIWORD(lParam) == WM_LBUTTONDOWN)
    606             {
    607                 if (LOWORD(lParam) != HTCLIENT)
    608                     window->win32.frameAction = GLFW_TRUE;
    609             }
    610 
    611             break;
    612         }
    613 
    614         case WM_CAPTURECHANGED:
    615         {
    616             // HACK: Disable the cursor once the caption button action has been
    617             //       completed or cancelled
    618             if (lParam == 0 && window->win32.frameAction)
    619             {
    620                 if (window->cursorMode == GLFW_CURSOR_DISABLED)
    621                     disableCursor(window);
    622 
    623                 window->win32.frameAction = GLFW_FALSE;
    624             }
    625 
    626             break;
    627         }
    628 
    629         case WM_SETFOCUS:
    630         {
    631             _glfwInputWindowFocus(window, GLFW_TRUE);
    632 
    633             // HACK: Do not disable cursor while the user is interacting with
    634             //       a caption button
    635             if (window->win32.frameAction)
    636                 break;
    637 
    638             if (window->cursorMode == GLFW_CURSOR_DISABLED)
    639                 disableCursor(window);
    640 
    641             return 0;
    642         }
    643 
    644         case WM_KILLFOCUS:
    645         {
    646             if (window->cursorMode == GLFW_CURSOR_DISABLED)
    647                 enableCursor(window);
    648 
    649             if (window->monitor && window->autoIconify)
    650                 _glfwPlatformIconifyWindow(window);
    651 
    652             _glfwInputWindowFocus(window, GLFW_FALSE);
    653             return 0;
    654         }
    655 
    656         case WM_SYSCOMMAND:
    657         {
    658             switch (wParam & 0xfff0)
    659             {
    660                 case SC_SCREENSAVE:
    661                 case SC_MONITORPOWER:
    662                 {
    663                     if (window->monitor)
    664                     {
    665                         // We are running in full screen mode, so disallow
    666                         // screen saver and screen blanking
    667                         return 0;
    668                     }
    669                     else
    670                         break;
    671                 }
    672 
    673                 // User trying to access application menu using ALT?
    674                 case SC_KEYMENU:
    675                     return 0;
    676             }
    677             break;
    678         }
    679 
    680         case WM_CLOSE:
    681         {
    682             _glfwInputWindowCloseRequest(window);
    683             return 0;
    684         }
    685 
    686         case WM_INPUTLANGCHANGE:
    687         {
    688             _glfwUpdateKeyNamesWin32();
    689             break;
    690         }
    691 
    692         case WM_CHAR:
    693         case WM_SYSCHAR:
    694         {
    695             if (wParam >= 0xd800 && wParam <= 0xdbff)
    696                 window->win32.highSurrogate = (WCHAR) wParam;
    697             else
    698             {
    699                 uint32_t codepoint = 0;
    700 
    701                 if (wParam >= 0xdc00 && wParam <= 0xdfff)
    702                 {
    703                     if (window->win32.highSurrogate)
    704                     {
    705                         codepoint += (window->win32.highSurrogate - 0xd800) << 10;
    706                         codepoint += (WCHAR) wParam - 0xdc00;
    707                         codepoint += 0x10000;
    708                     }
    709                 }
    710                 else
    711                     codepoint = (WCHAR) wParam;
    712 
    713                 window->win32.highSurrogate = 0;
    714                 _glfwInputChar(window, codepoint, getKeyMods(), uMsg != WM_SYSCHAR);
    715             }
    716 
    717             return 0;
    718         }
    719 
    720         case WM_UNICHAR:
    721         {
    722             if (wParam == UNICODE_NOCHAR)
    723             {
    724                 // WM_UNICHAR is not sent by Windows, but is sent by some
    725                 // third-party input method engine
    726                 // Returning TRUE here announces support for this message
    727                 return TRUE;
    728             }
    729 
    730             _glfwInputChar(window, (uint32_t) wParam, getKeyMods(), GLFW_TRUE);
    731             return 0;
    732         }
    733 
    734         case WM_KEYDOWN:
    735         case WM_SYSKEYDOWN:
    736         case WM_KEYUP:
    737         case WM_SYSKEYUP:
    738         {
    739             int key, scancode;
    740             const int action = (HIWORD(lParam) & KF_UP) ? GLFW_RELEASE : GLFW_PRESS;
    741             const int mods = getKeyMods();
    742 
    743             scancode = (HIWORD(lParam) & (KF_EXTENDED | 0xff));
    744             if (!scancode)
    745             {
    746                 // NOTE: Some synthetic key messages have a scancode of zero
    747                 // HACK: Map the virtual key back to a usable scancode
    748                 scancode = MapVirtualKeyW((UINT) wParam, MAPVK_VK_TO_VSC);
    749             }
    750 
    751             // HACK: Alt+PrtSc has a different scancode than just PrtSc
    752             if (scancode == 0x54)
    753                 scancode = 0x137;
    754 
    755             // HACK: Ctrl+Pause has a different scancode than just Pause
    756             if (scancode == 0x146)
    757                 scancode = 0x45;
    758 
    759             // HACK: CJK IME sets the extended bit for right Shift
    760             if (scancode == 0x136)
    761                 scancode = 0x36;
    762 
    763             key = _glfw.win32.keycodes[scancode];
    764 
    765             // The Ctrl keys require special handling
    766             if (wParam == VK_CONTROL)
    767             {
    768                 if (HIWORD(lParam) & KF_EXTENDED)
    769                 {
    770                     // Right side keys have the extended key bit set
    771                     key = GLFW_KEY_RIGHT_CONTROL;
    772                 }
    773                 else
    774                 {
    775                     // NOTE: Alt Gr sends Left Ctrl followed by Right Alt
    776                     // HACK: We only want one event for Alt Gr, so if we detect
    777                     //       this sequence we discard this Left Ctrl message now
    778                     //       and later report Right Alt normally
    779                     MSG next;
    780                     const DWORD time = GetMessageTime();
    781 
    782                     if (PeekMessageW(&next, NULL, 0, 0, PM_NOREMOVE))
    783                     {
    784                         if (next.message == WM_KEYDOWN ||
    785                             next.message == WM_SYSKEYDOWN ||
    786                             next.message == WM_KEYUP ||
    787                             next.message == WM_SYSKEYUP)
    788                         {
    789                             if (next.wParam == VK_MENU &&
    790                                 (HIWORD(next.lParam) & KF_EXTENDED) &&
    791                                 next.time == time)
    792                             {
    793                                 // Next message is Right Alt down so discard this
    794                                 break;
    795                             }
    796                         }
    797                     }
    798 
    799                     // This is a regular Left Ctrl message
    800                     key = GLFW_KEY_LEFT_CONTROL;
    801                 }
    802             }
    803             else if (wParam == VK_PROCESSKEY)
    804             {
    805                 // IME notifies that keys have been filtered by setting the
    806                 // virtual key-code to VK_PROCESSKEY
    807                 break;
    808             }
    809 
    810             if (action == GLFW_RELEASE && wParam == VK_SHIFT)
    811             {
    812                 // HACK: Release both Shift keys on Shift up event, as when both
    813                 //       are pressed the first release does not emit any event
    814                 // NOTE: The other half of this is in _glfwPlatformPollEvents
    815                 _glfwInputKey(window, GLFW_KEY_LEFT_SHIFT, scancode, action, mods);
    816                 _glfwInputKey(window, GLFW_KEY_RIGHT_SHIFT, scancode, action, mods);
    817             }
    818             else if (wParam == VK_SNAPSHOT)
    819             {
    820                 // HACK: Key down is not reported for the Print Screen key
    821                 _glfwInputKey(window, key, scancode, GLFW_PRESS, mods);
    822                 _glfwInputKey(window, key, scancode, GLFW_RELEASE, mods);
    823             }
    824             else
    825                 _glfwInputKey(window, key, scancode, action, mods);
    826 
    827             break;
    828         }
    829 
    830         case WM_LBUTTONDOWN:
    831         case WM_RBUTTONDOWN:
    832         case WM_MBUTTONDOWN:
    833         case WM_XBUTTONDOWN:
    834         case WM_LBUTTONUP:
    835         case WM_RBUTTONUP:
    836         case WM_MBUTTONUP:
    837         case WM_XBUTTONUP:
    838         {
    839             int i, button, action;
    840 
    841             if (uMsg == WM_LBUTTONDOWN || uMsg == WM_LBUTTONUP)
    842                 button = GLFW_MOUSE_BUTTON_LEFT;
    843             else if (uMsg == WM_RBUTTONDOWN || uMsg == WM_RBUTTONUP)
    844                 button = GLFW_MOUSE_BUTTON_RIGHT;
    845             else if (uMsg == WM_MBUTTONDOWN || uMsg == WM_MBUTTONUP)
    846                 button = GLFW_MOUSE_BUTTON_MIDDLE;
    847             else if (GET_XBUTTON_WPARAM(wParam) == XBUTTON1)
    848                 button = GLFW_MOUSE_BUTTON_4;
    849             else
    850                 button = GLFW_MOUSE_BUTTON_5;
    851 
    852             if (uMsg == WM_LBUTTONDOWN || uMsg == WM_RBUTTONDOWN ||
    853                 uMsg == WM_MBUTTONDOWN || uMsg == WM_XBUTTONDOWN)
    854             {
    855                 action = GLFW_PRESS;
    856             }
    857             else
    858                 action = GLFW_RELEASE;
    859 
    860             for (i = 0;  i <= GLFW_MOUSE_BUTTON_LAST;  i++)
    861             {
    862                 if (window->mouseButtons[i] == GLFW_PRESS)
    863                     break;
    864             }
    865 
    866             if (i > GLFW_MOUSE_BUTTON_LAST)
    867                 SetCapture(hWnd);
    868 
    869             _glfwInputMouseClick(window, button, action, getKeyMods());
    870 
    871             for (i = 0;  i <= GLFW_MOUSE_BUTTON_LAST;  i++)
    872             {
    873                 if (window->mouseButtons[i] == GLFW_PRESS)
    874                     break;
    875             }
    876 
    877             if (i > GLFW_MOUSE_BUTTON_LAST)
    878                 ReleaseCapture();
    879 
    880             if (uMsg == WM_XBUTTONDOWN || uMsg == WM_XBUTTONUP)
    881                 return TRUE;
    882 
    883             return 0;
    884         }
    885 
    886         case WM_MOUSEMOVE:
    887         {
    888             const int x = GET_X_LPARAM(lParam);
    889             const int y = GET_Y_LPARAM(lParam);
    890 
    891             if (!window->win32.cursorTracked)
    892             {
    893                 TRACKMOUSEEVENT tme;
    894                 ZeroMemory(&tme, sizeof(tme));
    895                 tme.cbSize = sizeof(tme);
    896                 tme.dwFlags = TME_LEAVE;
    897                 tme.hwndTrack = window->win32.handle;
    898                 TrackMouseEvent(&tme);
    899 
    900                 window->win32.cursorTracked = GLFW_TRUE;
    901                 _glfwInputCursorEnter(window, GLFW_TRUE);
    902             }
    903 
    904             if (window->cursorMode == GLFW_CURSOR_DISABLED)
    905             {
    906                 const int dx = x - window->win32.lastCursorPosX;
    907                 const int dy = y - window->win32.lastCursorPosY;
    908 
    909                 if (_glfw.win32.disabledCursorWindow != window)
    910                     break;
    911                 if (window->rawMouseMotion)
    912                     break;
    913 
    914                 _glfwInputCursorPos(window,
    915                                     window->virtualCursorPosX + dx,
    916                                     window->virtualCursorPosY + dy);
    917             }
    918             else
    919                 _glfwInputCursorPos(window, x, y);
    920 
    921             window->win32.lastCursorPosX = x;
    922             window->win32.lastCursorPosY = y;
    923 
    924             return 0;
    925         }
    926 
    927         case WM_INPUT:
    928         {
    929             UINT size = 0;
    930             HRAWINPUT ri = (HRAWINPUT) lParam;
    931             RAWINPUT* data = NULL;
    932             int dx, dy;
    933 
    934             if (_glfw.win32.disabledCursorWindow != window)
    935                 break;
    936             if (!window->rawMouseMotion)
    937                 break;
    938 
    939             GetRawInputData(ri, RID_INPUT, NULL, &size, sizeof(RAWINPUTHEADER));
    940             if (size > (UINT) _glfw.win32.rawInputSize)
    941             {
    942                 free(_glfw.win32.rawInput);
    943                 _glfw.win32.rawInput = calloc(size, 1);
    944                 _glfw.win32.rawInputSize = size;
    945             }
    946 
    947             size = _glfw.win32.rawInputSize;
    948             if (GetRawInputData(ri, RID_INPUT,
    949                                 _glfw.win32.rawInput, &size,
    950                                 sizeof(RAWINPUTHEADER)) == (UINT) -1)
    951             {
    952                 _glfwInputError(GLFW_PLATFORM_ERROR,
    953                                 "Win32: Failed to retrieve raw input data");
    954                 break;
    955             }
    956 
    957             data = _glfw.win32.rawInput;
    958             if (data->data.mouse.usFlags & MOUSE_MOVE_ABSOLUTE)
    959             {
    960                 dx = data->data.mouse.lLastX - window->win32.lastCursorPosX;
    961                 dy = data->data.mouse.lLastY - window->win32.lastCursorPosY;
    962             }
    963             else
    964             {
    965                 dx = data->data.mouse.lLastX;
    966                 dy = data->data.mouse.lLastY;
    967             }
    968 
    969             _glfwInputCursorPos(window,
    970                                 window->virtualCursorPosX + dx,
    971                                 window->virtualCursorPosY + dy);
    972 
    973             window->win32.lastCursorPosX += dx;
    974             window->win32.lastCursorPosY += dy;
    975             break;
    976         }
    977 
    978         case WM_MOUSELEAVE:
    979         {
    980             window->win32.cursorTracked = GLFW_FALSE;
    981             _glfwInputCursorEnter(window, GLFW_FALSE);
    982             return 0;
    983         }
    984 
    985         case WM_MOUSEWHEEL:
    986         {
    987             _glfwInputScroll(window, 0.0, (SHORT) HIWORD(wParam) / (double) WHEEL_DELTA);
    988             return 0;
    989         }
    990 
    991         case WM_MOUSEHWHEEL:
    992         {
    993             // This message is only sent on Windows Vista and later
    994             // NOTE: The X-axis is inverted for consistency with macOS and X11
    995             _glfwInputScroll(window, -((SHORT) HIWORD(wParam) / (double) WHEEL_DELTA), 0.0);
    996             return 0;
    997         }
    998 
    999         case WM_ENTERSIZEMOVE:
   1000         case WM_ENTERMENULOOP:
   1001         {
   1002             if (window->win32.frameAction)
   1003                 break;
   1004 
   1005             // HACK: Enable the cursor while the user is moving or
   1006             //       resizing the window or using the window menu
   1007             if (window->cursorMode == GLFW_CURSOR_DISABLED)
   1008                 enableCursor(window);
   1009 
   1010             break;
   1011         }
   1012 
   1013         case WM_EXITSIZEMOVE:
   1014         case WM_EXITMENULOOP:
   1015         {
   1016             if (window->win32.frameAction)
   1017                 break;
   1018 
   1019             // HACK: Disable the cursor once the user is done moving or
   1020             //       resizing the window or using the menu
   1021             if (window->cursorMode == GLFW_CURSOR_DISABLED)
   1022                 disableCursor(window);
   1023 
   1024             break;
   1025         }
   1026 
   1027         case WM_SIZE:
   1028         {
   1029             const int width = LOWORD(lParam);
   1030             const int height = HIWORD(lParam);
   1031             const GLFWbool iconified = wParam == SIZE_MINIMIZED;
   1032             const GLFWbool maximized = wParam == SIZE_MAXIMIZED ||
   1033                                        (window->win32.maximized &&
   1034                                         wParam != SIZE_RESTORED);
   1035 
   1036             if (_glfw.win32.disabledCursorWindow == window)
   1037                 updateClipRect(window);
   1038 
   1039             if (window->win32.iconified != iconified)
   1040                 _glfwInputWindowIconify(window, iconified);
   1041 
   1042             if (window->win32.maximized != maximized)
   1043                 _glfwInputWindowMaximize(window, maximized);
   1044 
   1045             if (width != window->win32.width || height != window->win32.height)
   1046             {
   1047                 window->win32.width = width;
   1048                 window->win32.height = height;
   1049 
   1050                 _glfwInputFramebufferSize(window, width, height);
   1051                 _glfwInputWindowSize(window, width, height);
   1052             }
   1053 
   1054             if (window->monitor && window->win32.iconified != iconified)
   1055             {
   1056                 if (iconified)
   1057                     releaseMonitor(window);
   1058                 else
   1059                 {
   1060                     acquireMonitor(window);
   1061                     fitToMonitor(window);
   1062                 }
   1063             }
   1064 
   1065             window->win32.iconified = iconified;
   1066             window->win32.maximized = maximized;
   1067             return 0;
   1068         }
   1069 
   1070         case WM_MOVE:
   1071         {
   1072             if (_glfw.win32.disabledCursorWindow == window)
   1073                 updateClipRect(window);
   1074 
   1075             // NOTE: This cannot use LOWORD/HIWORD recommended by MSDN, as
   1076             // those macros do not handle negative window positions correctly
   1077             _glfwInputWindowPos(window,
   1078                                 GET_X_LPARAM(lParam),
   1079                                 GET_Y_LPARAM(lParam));
   1080             return 0;
   1081         }
   1082 
   1083         case WM_SIZING:
   1084         {
   1085             if (window->numer == GLFW_DONT_CARE ||
   1086                 window->denom == GLFW_DONT_CARE)
   1087             {
   1088                 break;
   1089             }
   1090 
   1091             applyAspectRatio(window, (int) wParam, (RECT*) lParam);
   1092             return TRUE;
   1093         }
   1094 
   1095         case WM_GETMINMAXINFO:
   1096         {
   1097             int xoff, yoff;
   1098             UINT dpi = USER_DEFAULT_SCREEN_DPI;
   1099             MINMAXINFO* mmi = (MINMAXINFO*) lParam;
   1100 
   1101             if (window->monitor)
   1102                 break;
   1103 
   1104             if (_glfwIsWindows10AnniversaryUpdateOrGreaterWin32())
   1105                 dpi = GetDpiForWindow(window->win32.handle);
   1106 
   1107             getFullWindowSize(getWindowStyle(window), getWindowExStyle(window),
   1108                               0, 0, &xoff, &yoff, dpi);
   1109 
   1110             if (window->minwidth != GLFW_DONT_CARE &&
   1111                 window->minheight != GLFW_DONT_CARE)
   1112             {
   1113                 mmi->ptMinTrackSize.x = window->minwidth + xoff;
   1114                 mmi->ptMinTrackSize.y = window->minheight + yoff;
   1115             }
   1116 
   1117             if (window->maxwidth != GLFW_DONT_CARE &&
   1118                 window->maxheight != GLFW_DONT_CARE)
   1119             {
   1120                 mmi->ptMaxTrackSize.x = window->maxwidth + xoff;
   1121                 mmi->ptMaxTrackSize.y = window->maxheight + yoff;
   1122             }
   1123 
   1124             if (!window->decorated)
   1125             {
   1126                 MONITORINFO mi;
   1127                 const HMONITOR mh = MonitorFromWindow(window->win32.handle,
   1128                                                       MONITOR_DEFAULTTONEAREST);
   1129 
   1130                 ZeroMemory(&mi, sizeof(mi));
   1131                 mi.cbSize = sizeof(mi);
   1132                 GetMonitorInfoW(mh, &mi);
   1133 
   1134                 mmi->ptMaxPosition.x = mi.rcWork.left - mi.rcMonitor.left;
   1135                 mmi->ptMaxPosition.y = mi.rcWork.top - mi.rcMonitor.top;
   1136                 mmi->ptMaxSize.x = mi.rcWork.right - mi.rcWork.left;
   1137                 mmi->ptMaxSize.y = mi.rcWork.bottom - mi.rcWork.top;
   1138             }
   1139 
   1140             return 0;
   1141         }
   1142 
   1143         case WM_PAINT:
   1144         {
   1145             _glfwInputWindowDamage(window);
   1146             break;
   1147         }
   1148 
   1149         case WM_ERASEBKGND:
   1150         {
   1151             return TRUE;
   1152         }
   1153 
   1154         case WM_NCACTIVATE:
   1155         case WM_NCPAINT:
   1156         {
   1157             // Prevent title bar from being drawn after restoring a minimized
   1158             // undecorated window
   1159             if (!window->decorated)
   1160                 return TRUE;
   1161 
   1162             break;
   1163         }
   1164 
   1165         case WM_DWMCOMPOSITIONCHANGED:
   1166         case WM_DWMCOLORIZATIONCOLORCHANGED:
   1167         {
   1168             if (window->win32.transparent)
   1169                 updateFramebufferTransparency(window);
   1170             return 0;
   1171         }
   1172 
   1173         case WM_GETDPISCALEDSIZE:
   1174         {
   1175             if (window->win32.scaleToMonitor)
   1176                 break;
   1177 
   1178             // Adjust the window size to keep the content area size constant
   1179             if (_glfwIsWindows10CreatorsUpdateOrGreaterWin32())
   1180             {
   1181                 RECT source = {0}, target = {0};
   1182                 SIZE* size = (SIZE*) lParam;
   1183 
   1184                 AdjustWindowRectExForDpi(&source, getWindowStyle(window),
   1185                                          FALSE, getWindowExStyle(window),
   1186                                          GetDpiForWindow(window->win32.handle));
   1187                 AdjustWindowRectExForDpi(&target, getWindowStyle(window),
   1188                                          FALSE, getWindowExStyle(window),
   1189                                          LOWORD(wParam));
   1190 
   1191                 size->cx += (target.right - target.left) -
   1192                             (source.right - source.left);
   1193                 size->cy += (target.bottom - target.top) -
   1194                             (source.bottom - source.top);
   1195                 return TRUE;
   1196             }
   1197 
   1198             break;
   1199         }
   1200 
   1201         case WM_DPICHANGED:
   1202         {
   1203             const float xscale = HIWORD(wParam) / (float) USER_DEFAULT_SCREEN_DPI;
   1204             const float yscale = LOWORD(wParam) / (float) USER_DEFAULT_SCREEN_DPI;
   1205 
   1206             // Resize windowed mode windows that either permit rescaling or that
   1207             // need it to compensate for non-client area scaling
   1208             if (!window->monitor &&
   1209                 (window->win32.scaleToMonitor ||
   1210                  _glfwIsWindows10CreatorsUpdateOrGreaterWin32()))
   1211             {
   1212                 RECT* suggested = (RECT*) lParam;
   1213                 SetWindowPos(window->win32.handle, HWND_TOP,
   1214                              suggested->left,
   1215                              suggested->top,
   1216                              suggested->right - suggested->left,
   1217                              suggested->bottom - suggested->top,
   1218                              SWP_NOACTIVATE | SWP_NOZORDER);
   1219             }
   1220 
   1221             _glfwInputWindowContentScale(window, xscale, yscale);
   1222             break;
   1223         }
   1224 
   1225         case WM_SETCURSOR:
   1226         {
   1227             if (LOWORD(lParam) == HTCLIENT)
   1228             {
   1229                 updateCursorImage(window);
   1230                 return TRUE;
   1231             }
   1232 
   1233             break;
   1234         }
   1235 
   1236         case WM_DROPFILES:
   1237         {
   1238             HDROP drop = (HDROP) wParam;
   1239             POINT pt;
   1240             int i;
   1241 
   1242             const int count = DragQueryFileW(drop, 0xffffffff, NULL, 0);
   1243             char** paths = calloc(count, sizeof(char*));
   1244 
   1245             // Move the mouse to the position of the drop
   1246             DragQueryPoint(drop, &pt);
   1247             _glfwInputCursorPos(window, pt.x, pt.y);
   1248 
   1249             for (i = 0;  i < count;  i++)
   1250             {
   1251                 const UINT length = DragQueryFileW(drop, i, NULL, 0);
   1252                 WCHAR* buffer = calloc((size_t) length + 1, sizeof(WCHAR));
   1253 
   1254                 DragQueryFileW(drop, i, buffer, length + 1);
   1255                 paths[i] = _glfwCreateUTF8FromWideStringWin32(buffer);
   1256 
   1257                 free(buffer);
   1258             }
   1259 
   1260             _glfwInputDrop(window, count, (const char**) paths);
   1261 
   1262             for (i = 0;  i < count;  i++)
   1263                 free(paths[i]);
   1264             free(paths);
   1265 
   1266             DragFinish(drop);
   1267             return 0;
   1268         }
   1269     }
   1270 
   1271     return DefWindowProcW(hWnd, uMsg, wParam, lParam);
   1272 }
   1273 
   1274 // Creates the GLFW window
   1275 //
   1276 static int createNativeWindow(_GLFWwindow* window,
   1277                               const _GLFWwndconfig* wndconfig,
   1278                               const _GLFWfbconfig* fbconfig)
   1279 {
   1280     int xpos, ypos, fullWidth, fullHeight;
   1281     WCHAR* wideTitle;
   1282     DWORD style = getWindowStyle(window);
   1283     DWORD exStyle = getWindowExStyle(window);
   1284 
   1285     if (window->monitor)
   1286     {
   1287         MONITORINFO mi = { sizeof(mi) };
   1288         GetMonitorInfoW(window->monitor->win32.handle, &mi);
   1289 
   1290         // NOTE: This window placement is temporary and approximate, as the
   1291         //       correct position and size cannot be known until the monitor
   1292         //       video mode has been picked in _glfwSetVideoModeWin32
   1293         xpos = mi.rcMonitor.left;
   1294         ypos = mi.rcMonitor.top;
   1295         fullWidth  = mi.rcMonitor.right - mi.rcMonitor.left;
   1296         fullHeight = mi.rcMonitor.bottom - mi.rcMonitor.top;
   1297     }
   1298     else
   1299     {
   1300         xpos = CW_USEDEFAULT;
   1301         ypos = CW_USEDEFAULT;
   1302 
   1303         window->win32.maximized = wndconfig->maximized;
   1304         if (wndconfig->maximized)
   1305             style |= WS_MAXIMIZE;
   1306 
   1307         getFullWindowSize(style, exStyle,
   1308                           wndconfig->width, wndconfig->height,
   1309                           &fullWidth, &fullHeight,
   1310                           USER_DEFAULT_SCREEN_DPI);
   1311     }
   1312 
   1313     wideTitle = _glfwCreateWideStringFromUTF8Win32(wndconfig->title);
   1314     if (!wideTitle)
   1315         return GLFW_FALSE;
   1316 
   1317     window->win32.handle = CreateWindowExW(exStyle,
   1318                                            _GLFW_WNDCLASSNAME,
   1319                                            wideTitle,
   1320                                            style,
   1321                                            xpos, ypos,
   1322                                            fullWidth, fullHeight,
   1323                                            NULL, // No parent window
   1324                                            NULL, // No window menu
   1325                                            _glfw.win32.instance,
   1326                                            (LPVOID) wndconfig);
   1327 
   1328     free(wideTitle);
   1329 
   1330     if (!window->win32.handle)
   1331     {
   1332         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
   1333                              "Win32: Failed to create window");
   1334         return GLFW_FALSE;
   1335     }
   1336 
   1337     SetPropW(window->win32.handle, L"GLFW", window);
   1338 
   1339     if (IsWindows7OrGreater())
   1340     {
   1341         ChangeWindowMessageFilterEx(window->win32.handle,
   1342                                     WM_DROPFILES, MSGFLT_ALLOW, NULL);
   1343         ChangeWindowMessageFilterEx(window->win32.handle,
   1344                                     WM_COPYDATA, MSGFLT_ALLOW, NULL);
   1345         ChangeWindowMessageFilterEx(window->win32.handle,
   1346                                     WM_COPYGLOBALDATA, MSGFLT_ALLOW, NULL);
   1347     }
   1348 
   1349     window->win32.scaleToMonitor = wndconfig->scaleToMonitor;
   1350 
   1351     if (!window->monitor)
   1352     {
   1353         RECT rect = { 0, 0, wndconfig->width, wndconfig->height };
   1354         WINDOWPLACEMENT wp = { sizeof(wp) };
   1355         const HMONITOR mh = MonitorFromWindow(window->win32.handle,
   1356                                               MONITOR_DEFAULTTONEAREST);
   1357 
   1358         // Adjust window rect to account for DPI scaling of the window frame and
   1359         // (if enabled) DPI scaling of the content area
   1360         // This cannot be done until we know what monitor the window was placed on
   1361         // Only update the restored window rect as the window may be maximized
   1362 
   1363         if (wndconfig->scaleToMonitor)
   1364         {
   1365             float xscale, yscale;
   1366             _glfwGetMonitorContentScaleWin32(mh, &xscale, &yscale);
   1367 
   1368             if (xscale > 0.f && yscale > 0.f)
   1369             {
   1370                 rect.right = (int) (rect.right * xscale);
   1371                 rect.bottom = (int) (rect.bottom * yscale);
   1372             }
   1373         }
   1374 
   1375         if (_glfwIsWindows10AnniversaryUpdateOrGreaterWin32())
   1376         {
   1377             AdjustWindowRectExForDpi(&rect, style, FALSE, exStyle,
   1378                                      GetDpiForWindow(window->win32.handle));
   1379         }
   1380         else
   1381             AdjustWindowRectEx(&rect, style, FALSE, exStyle);
   1382 
   1383         GetWindowPlacement(window->win32.handle, &wp);
   1384         OffsetRect(&rect,
   1385                    wp.rcNormalPosition.left - rect.left,
   1386                    wp.rcNormalPosition.top - rect.top);
   1387 
   1388         wp.rcNormalPosition = rect;
   1389         wp.showCmd = SW_HIDE;
   1390         SetWindowPlacement(window->win32.handle, &wp);
   1391 
   1392         // Adjust rect of maximized undecorated window, because by default Windows will
   1393         // make such a window cover the whole monitor instead of its workarea
   1394 
   1395         if (wndconfig->maximized && !wndconfig->decorated)
   1396         {
   1397             MONITORINFO mi = { sizeof(mi) };
   1398             GetMonitorInfoW(mh, &mi);
   1399 
   1400             SetWindowPos(window->win32.handle, HWND_TOP,
   1401                          mi.rcWork.left,
   1402                          mi.rcWork.top,
   1403                          mi.rcWork.right - mi.rcWork.left,
   1404                          mi.rcWork.bottom - mi.rcWork.top,
   1405                          SWP_NOACTIVATE | SWP_NOZORDER);
   1406         }
   1407     }
   1408 
   1409     DragAcceptFiles(window->win32.handle, TRUE);
   1410 
   1411     if (fbconfig->transparent)
   1412     {
   1413         updateFramebufferTransparency(window);
   1414         window->win32.transparent = GLFW_TRUE;
   1415     }
   1416 
   1417     _glfwPlatformGetWindowSize(window, &window->win32.width, &window->win32.height);
   1418 
   1419     return GLFW_TRUE;
   1420 }
   1421 
   1422 
   1423 //////////////////////////////////////////////////////////////////////////
   1424 //////                       GLFW internal API                      //////
   1425 //////////////////////////////////////////////////////////////////////////
   1426 
   1427 // Registers the GLFW window class
   1428 //
   1429 GLFWbool _glfwRegisterWindowClassWin32(void)
   1430 {
   1431     WNDCLASSEXW wc;
   1432 
   1433     ZeroMemory(&wc, sizeof(wc));
   1434     wc.cbSize        = sizeof(wc);
   1435     wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
   1436     wc.lpfnWndProc   = windowProc;
   1437     wc.hInstance     = _glfw.win32.instance;
   1438     wc.hCursor       = LoadCursorW(NULL, IDC_ARROW);
   1439     wc.lpszClassName = _GLFW_WNDCLASSNAME;
   1440 
   1441     // Load user-provided icon if available
   1442     wc.hIcon = LoadImageW(GetModuleHandleW(NULL),
   1443                           L"GLFW_ICON", IMAGE_ICON,
   1444                           0, 0, LR_DEFAULTSIZE | LR_SHARED);
   1445     if (!wc.hIcon)
   1446     {
   1447         // No user-provided icon found, load default icon
   1448         wc.hIcon = LoadImageW(NULL,
   1449                               IDI_APPLICATION, IMAGE_ICON,
   1450                               0, 0, LR_DEFAULTSIZE | LR_SHARED);
   1451     }
   1452 
   1453     if (!RegisterClassExW(&wc))
   1454     {
   1455         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
   1456                              "Win32: Failed to register window class");
   1457         return GLFW_FALSE;
   1458     }
   1459 
   1460     return GLFW_TRUE;
   1461 }
   1462 
   1463 // Unregisters the GLFW window class
   1464 //
   1465 void _glfwUnregisterWindowClassWin32(void)
   1466 {
   1467     UnregisterClassW(_GLFW_WNDCLASSNAME, _glfw.win32.instance);
   1468 }
   1469 
   1470 
   1471 //////////////////////////////////////////////////////////////////////////
   1472 //////                       GLFW platform API                      //////
   1473 //////////////////////////////////////////////////////////////////////////
   1474 
   1475 int _glfwPlatformCreateWindow(_GLFWwindow* window,
   1476                               const _GLFWwndconfig* wndconfig,
   1477                               const _GLFWctxconfig* ctxconfig,
   1478                               const _GLFWfbconfig* fbconfig)
   1479 {
   1480     if (!createNativeWindow(window, wndconfig, fbconfig))
   1481         return GLFW_FALSE;
   1482 
   1483     if (ctxconfig->client != GLFW_NO_API)
   1484     {
   1485         if (ctxconfig->source == GLFW_NATIVE_CONTEXT_API)
   1486         {
   1487             if (!_glfwInitWGL())
   1488                 return GLFW_FALSE;
   1489             if (!_glfwCreateContextWGL(window, ctxconfig, fbconfig))
   1490                 return GLFW_FALSE;
   1491         }
   1492         else if (ctxconfig->source == GLFW_EGL_CONTEXT_API)
   1493         {
   1494             if (!_glfwInitEGL())
   1495                 return GLFW_FALSE;
   1496             if (!_glfwCreateContextEGL(window, ctxconfig, fbconfig))
   1497                 return GLFW_FALSE;
   1498         }
   1499         else if (ctxconfig->source == GLFW_OSMESA_CONTEXT_API)
   1500         {
   1501             if (!_glfwInitOSMesa())
   1502                 return GLFW_FALSE;
   1503             if (!_glfwCreateContextOSMesa(window, ctxconfig, fbconfig))
   1504                 return GLFW_FALSE;
   1505         }
   1506 
   1507         if (!_glfwRefreshContextAttribs(window, ctxconfig))
   1508             return GLFW_FALSE;
   1509     }
   1510 
   1511     if (window->monitor)
   1512     {
   1513         _glfwPlatformShowWindow(window);
   1514         _glfwPlatformFocusWindow(window);
   1515         acquireMonitor(window);
   1516         fitToMonitor(window);
   1517 
   1518         if (wndconfig->centerCursor)
   1519             _glfwCenterCursorInContentArea(window);
   1520     }
   1521     else
   1522     {
   1523         if (wndconfig->visible)
   1524         {
   1525             _glfwPlatformShowWindow(window);
   1526             if (wndconfig->focused)
   1527                 _glfwPlatformFocusWindow(window);
   1528         }
   1529     }
   1530 
   1531     return GLFW_TRUE;
   1532 }
   1533 
   1534 void _glfwPlatformDestroyWindow(_GLFWwindow* window)
   1535 {
   1536     if (window->monitor)
   1537         releaseMonitor(window);
   1538 
   1539     if (window->context.destroy)
   1540         window->context.destroy(window);
   1541 
   1542     if (_glfw.win32.disabledCursorWindow == window)
   1543         _glfw.win32.disabledCursorWindow = NULL;
   1544 
   1545     if (window->win32.handle)
   1546     {
   1547         RemovePropW(window->win32.handle, L"GLFW");
   1548         DestroyWindow(window->win32.handle);
   1549         window->win32.handle = NULL;
   1550     }
   1551 
   1552     if (window->win32.bigIcon)
   1553         DestroyIcon(window->win32.bigIcon);
   1554 
   1555     if (window->win32.smallIcon)
   1556         DestroyIcon(window->win32.smallIcon);
   1557 }
   1558 
   1559 void _glfwPlatformSetWindowTitle(_GLFWwindow* window, const char* title)
   1560 {
   1561     WCHAR* wideTitle = _glfwCreateWideStringFromUTF8Win32(title);
   1562     if (!wideTitle)
   1563         return;
   1564 
   1565     SetWindowTextW(window->win32.handle, wideTitle);
   1566     free(wideTitle);
   1567 }
   1568 
   1569 void _glfwPlatformSetWindowIcon(_GLFWwindow* window,
   1570                                 int count, const GLFWimage* images)
   1571 {
   1572     HICON bigIcon = NULL, smallIcon = NULL;
   1573 
   1574     if (count)
   1575     {
   1576         const GLFWimage* bigImage = chooseImage(count, images,
   1577                                                 GetSystemMetrics(SM_CXICON),
   1578                                                 GetSystemMetrics(SM_CYICON));
   1579         const GLFWimage* smallImage = chooseImage(count, images,
   1580                                                   GetSystemMetrics(SM_CXSMICON),
   1581                                                   GetSystemMetrics(SM_CYSMICON));
   1582 
   1583         bigIcon = createIcon(bigImage, 0, 0, GLFW_TRUE);
   1584         smallIcon = createIcon(smallImage, 0, 0, GLFW_TRUE);
   1585     }
   1586     else
   1587     {
   1588         bigIcon = (HICON) GetClassLongPtrW(window->win32.handle, GCLP_HICON);
   1589         smallIcon = (HICON) GetClassLongPtrW(window->win32.handle, GCLP_HICONSM);
   1590     }
   1591 
   1592     SendMessageW(window->win32.handle, WM_SETICON, ICON_BIG, (LPARAM) bigIcon);
   1593     SendMessageW(window->win32.handle, WM_SETICON, ICON_SMALL, (LPARAM) smallIcon);
   1594 
   1595     if (window->win32.bigIcon)
   1596         DestroyIcon(window->win32.bigIcon);
   1597 
   1598     if (window->win32.smallIcon)
   1599         DestroyIcon(window->win32.smallIcon);
   1600 
   1601     if (count)
   1602     {
   1603         window->win32.bigIcon = bigIcon;
   1604         window->win32.smallIcon = smallIcon;
   1605     }
   1606 }
   1607 
   1608 void _glfwPlatformGetWindowPos(_GLFWwindow* window, int* xpos, int* ypos)
   1609 {
   1610     POINT pos = { 0, 0 };
   1611     ClientToScreen(window->win32.handle, &pos);
   1612 
   1613     if (xpos)
   1614         *xpos = pos.x;
   1615     if (ypos)
   1616         *ypos = pos.y;
   1617 }
   1618 
   1619 void _glfwPlatformSetWindowPos(_GLFWwindow* window, int xpos, int ypos)
   1620 {
   1621     RECT rect = { xpos, ypos, xpos, ypos };
   1622 
   1623     if (_glfwIsWindows10AnniversaryUpdateOrGreaterWin32())
   1624     {
   1625         AdjustWindowRectExForDpi(&rect, getWindowStyle(window),
   1626                                  FALSE, getWindowExStyle(window),
   1627                                  GetDpiForWindow(window->win32.handle));
   1628     }
   1629     else
   1630     {
   1631         AdjustWindowRectEx(&rect, getWindowStyle(window),
   1632                            FALSE, getWindowExStyle(window));
   1633     }
   1634 
   1635     SetWindowPos(window->win32.handle, NULL, rect.left, rect.top, 0, 0,
   1636                  SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE);
   1637 }
   1638 
   1639 void _glfwPlatformGetWindowSize(_GLFWwindow* window, int* width, int* height)
   1640 {
   1641     RECT area;
   1642     GetClientRect(window->win32.handle, &area);
   1643 
   1644     if (width)
   1645         *width = area.right;
   1646     if (height)
   1647         *height = area.bottom;
   1648 }
   1649 
   1650 void _glfwPlatformSetWindowSize(_GLFWwindow* window, int width, int height)
   1651 {
   1652     if (window->monitor)
   1653     {
   1654         if (window->monitor->window == window)
   1655         {
   1656             acquireMonitor(window);
   1657             fitToMonitor(window);
   1658         }
   1659     }
   1660     else
   1661     {
   1662         RECT rect = { 0, 0, width, height };
   1663 
   1664         if (_glfwIsWindows10AnniversaryUpdateOrGreaterWin32())
   1665         {
   1666             AdjustWindowRectExForDpi(&rect, getWindowStyle(window),
   1667                                      FALSE, getWindowExStyle(window),
   1668                                      GetDpiForWindow(window->win32.handle));
   1669         }
   1670         else
   1671         {
   1672             AdjustWindowRectEx(&rect, getWindowStyle(window),
   1673                                FALSE, getWindowExStyle(window));
   1674         }
   1675 
   1676         SetWindowPos(window->win32.handle, HWND_TOP,
   1677                      0, 0, rect.right - rect.left, rect.bottom - rect.top,
   1678                      SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOMOVE | SWP_NOZORDER);
   1679     }
   1680 }
   1681 
   1682 void _glfwPlatformSetWindowSizeLimits(_GLFWwindow* window,
   1683                                       int minwidth, int minheight,
   1684                                       int maxwidth, int maxheight)
   1685 {
   1686     RECT area;
   1687 
   1688     if ((minwidth == GLFW_DONT_CARE || minheight == GLFW_DONT_CARE) &&
   1689         (maxwidth == GLFW_DONT_CARE || maxheight == GLFW_DONT_CARE))
   1690     {
   1691         return;
   1692     }
   1693 
   1694     GetWindowRect(window->win32.handle, &area);
   1695     MoveWindow(window->win32.handle,
   1696                area.left, area.top,
   1697                area.right - area.left,
   1698                area.bottom - area.top, TRUE);
   1699 }
   1700 
   1701 void _glfwPlatformSetWindowAspectRatio(_GLFWwindow* window, int numer, int denom)
   1702 {
   1703     RECT area;
   1704 
   1705     if (numer == GLFW_DONT_CARE || denom == GLFW_DONT_CARE)
   1706         return;
   1707 
   1708     GetWindowRect(window->win32.handle, &area);
   1709     applyAspectRatio(window, WMSZ_BOTTOMRIGHT, &area);
   1710     MoveWindow(window->win32.handle,
   1711                area.left, area.top,
   1712                area.right - area.left,
   1713                area.bottom - area.top, TRUE);
   1714 }
   1715 
   1716 void _glfwPlatformGetFramebufferSize(_GLFWwindow* window, int* width, int* height)
   1717 {
   1718     _glfwPlatformGetWindowSize(window, width, height);
   1719 }
   1720 
   1721 void _glfwPlatformGetWindowFrameSize(_GLFWwindow* window,
   1722                                      int* left, int* top,
   1723                                      int* right, int* bottom)
   1724 {
   1725     RECT rect;
   1726     int width, height;
   1727 
   1728     _glfwPlatformGetWindowSize(window, &width, &height);
   1729     SetRect(&rect, 0, 0, width, height);
   1730 
   1731     if (_glfwIsWindows10AnniversaryUpdateOrGreaterWin32())
   1732     {
   1733         AdjustWindowRectExForDpi(&rect, getWindowStyle(window),
   1734                                  FALSE, getWindowExStyle(window),
   1735                                  GetDpiForWindow(window->win32.handle));
   1736     }
   1737     else
   1738     {
   1739         AdjustWindowRectEx(&rect, getWindowStyle(window),
   1740                            FALSE, getWindowExStyle(window));
   1741     }
   1742 
   1743     if (left)
   1744         *left = -rect.left;
   1745     if (top)
   1746         *top = -rect.top;
   1747     if (right)
   1748         *right = rect.right - width;
   1749     if (bottom)
   1750         *bottom = rect.bottom - height;
   1751 }
   1752 
   1753 void _glfwPlatformGetWindowContentScale(_GLFWwindow* window,
   1754                                         float* xscale, float* yscale)
   1755 {
   1756     const HANDLE handle = MonitorFromWindow(window->win32.handle,
   1757                                             MONITOR_DEFAULTTONEAREST);
   1758     _glfwGetMonitorContentScaleWin32(handle, xscale, yscale);
   1759 }
   1760 
   1761 void _glfwPlatformIconifyWindow(_GLFWwindow* window)
   1762 {
   1763     ShowWindow(window->win32.handle, SW_MINIMIZE);
   1764 }
   1765 
   1766 void _glfwPlatformRestoreWindow(_GLFWwindow* window)
   1767 {
   1768     ShowWindow(window->win32.handle, SW_RESTORE);
   1769 }
   1770 
   1771 void _glfwPlatformMaximizeWindow(_GLFWwindow* window)
   1772 {
   1773     if (IsWindowVisible(window->win32.handle))
   1774         ShowWindow(window->win32.handle, SW_MAXIMIZE);
   1775     else
   1776         maximizeWindowManually(window);
   1777 }
   1778 
   1779 void _glfwPlatformShowWindow(_GLFWwindow* window)
   1780 {
   1781     ShowWindow(window->win32.handle, SW_SHOWNA);
   1782 }
   1783 
   1784 void _glfwPlatformHideWindow(_GLFWwindow* window)
   1785 {
   1786     ShowWindow(window->win32.handle, SW_HIDE);
   1787 }
   1788 
   1789 void _glfwPlatformRequestWindowAttention(_GLFWwindow* window)
   1790 {
   1791     FlashWindow(window->win32.handle, TRUE);
   1792 }
   1793 
   1794 void _glfwPlatformFocusWindow(_GLFWwindow* window)
   1795 {
   1796     BringWindowToTop(window->win32.handle);
   1797     SetForegroundWindow(window->win32.handle);
   1798     SetFocus(window->win32.handle);
   1799 }
   1800 
   1801 void _glfwPlatformSetWindowMonitor(_GLFWwindow* window,
   1802                                    _GLFWmonitor* monitor,
   1803                                    int xpos, int ypos,
   1804                                    int width, int height,
   1805                                    int refreshRate)
   1806 {
   1807     if (window->monitor == monitor)
   1808     {
   1809         if (monitor)
   1810         {
   1811             if (monitor->window == window)
   1812             {
   1813                 acquireMonitor(window);
   1814                 fitToMonitor(window);
   1815             }
   1816         }
   1817         else
   1818         {
   1819             RECT rect = { xpos, ypos, xpos + width, ypos + height };
   1820 
   1821             if (_glfwIsWindows10AnniversaryUpdateOrGreaterWin32())
   1822             {
   1823                 AdjustWindowRectExForDpi(&rect, getWindowStyle(window),
   1824                                          FALSE, getWindowExStyle(window),
   1825                                          GetDpiForWindow(window->win32.handle));
   1826             }
   1827             else
   1828             {
   1829                 AdjustWindowRectEx(&rect, getWindowStyle(window),
   1830                                    FALSE, getWindowExStyle(window));
   1831             }
   1832 
   1833             SetWindowPos(window->win32.handle, HWND_TOP,
   1834                          rect.left, rect.top,
   1835                          rect.right - rect.left, rect.bottom - rect.top,
   1836                          SWP_NOCOPYBITS | SWP_NOACTIVATE | SWP_NOZORDER);
   1837         }
   1838 
   1839         return;
   1840     }
   1841 
   1842     if (window->monitor)
   1843         releaseMonitor(window);
   1844 
   1845     _glfwInputWindowMonitor(window, monitor);
   1846 
   1847     if (window->monitor)
   1848     {
   1849         MONITORINFO mi = { sizeof(mi) };
   1850         UINT flags = SWP_SHOWWINDOW | SWP_NOACTIVATE | SWP_NOCOPYBITS;
   1851 
   1852         if (window->decorated)
   1853         {
   1854             DWORD style = GetWindowLongW(window->win32.handle, GWL_STYLE);
   1855             style &= ~WS_OVERLAPPEDWINDOW;
   1856             style |= getWindowStyle(window);
   1857             SetWindowLongW(window->win32.handle, GWL_STYLE, style);
   1858             flags |= SWP_FRAMECHANGED;
   1859         }
   1860 
   1861         acquireMonitor(window);
   1862 
   1863         GetMonitorInfoW(window->monitor->win32.handle, &mi);
   1864         SetWindowPos(window->win32.handle, HWND_TOPMOST,
   1865                      mi.rcMonitor.left,
   1866                      mi.rcMonitor.top,
   1867                      mi.rcMonitor.right - mi.rcMonitor.left,
   1868                      mi.rcMonitor.bottom - mi.rcMonitor.top,
   1869                      flags);
   1870     }
   1871     else
   1872     {
   1873         HWND after;
   1874         RECT rect = { xpos, ypos, xpos + width, ypos + height };
   1875         DWORD style = GetWindowLongW(window->win32.handle, GWL_STYLE);
   1876         UINT flags = SWP_NOACTIVATE | SWP_NOCOPYBITS;
   1877 
   1878         if (window->decorated)
   1879         {
   1880             style &= ~WS_POPUP;
   1881             style |= getWindowStyle(window);
   1882             SetWindowLongW(window->win32.handle, GWL_STYLE, style);
   1883 
   1884             flags |= SWP_FRAMECHANGED;
   1885         }
   1886 
   1887         if (window->floating)
   1888             after = HWND_TOPMOST;
   1889         else
   1890             after = HWND_NOTOPMOST;
   1891 
   1892         if (_glfwIsWindows10AnniversaryUpdateOrGreaterWin32())
   1893         {
   1894             AdjustWindowRectExForDpi(&rect, getWindowStyle(window),
   1895                                      FALSE, getWindowExStyle(window),
   1896                                      GetDpiForWindow(window->win32.handle));
   1897         }
   1898         else
   1899         {
   1900             AdjustWindowRectEx(&rect, getWindowStyle(window),
   1901                                FALSE, getWindowExStyle(window));
   1902         }
   1903 
   1904         SetWindowPos(window->win32.handle, after,
   1905                      rect.left, rect.top,
   1906                      rect.right - rect.left, rect.bottom - rect.top,
   1907                      flags);
   1908     }
   1909 }
   1910 
   1911 int _glfwPlatformWindowFocused(_GLFWwindow* window)
   1912 {
   1913     return window->win32.handle == GetActiveWindow();
   1914 }
   1915 
   1916 int _glfwPlatformWindowIconified(_GLFWwindow* window)
   1917 {
   1918     return IsIconic(window->win32.handle);
   1919 }
   1920 
   1921 int _glfwPlatformWindowVisible(_GLFWwindow* window)
   1922 {
   1923     return IsWindowVisible(window->win32.handle);
   1924 }
   1925 
   1926 int _glfwPlatformWindowMaximized(_GLFWwindow* window)
   1927 {
   1928     return IsZoomed(window->win32.handle);
   1929 }
   1930 
   1931 int _glfwPlatformWindowHovered(_GLFWwindow* window)
   1932 {
   1933     return cursorInContentArea(window);
   1934 }
   1935 
   1936 int _glfwPlatformFramebufferTransparent(_GLFWwindow* window)
   1937 {
   1938     BOOL composition, opaque;
   1939     DWORD color;
   1940 
   1941     if (!window->win32.transparent)
   1942         return GLFW_FALSE;
   1943 
   1944     if (!IsWindowsVistaOrGreater())
   1945         return GLFW_FALSE;
   1946 
   1947     if (FAILED(DwmIsCompositionEnabled(&composition)) || !composition)
   1948         return GLFW_FALSE;
   1949 
   1950     if (!IsWindows8OrGreater())
   1951     {
   1952         // HACK: Disable framebuffer transparency on Windows 7 when the
   1953         //       colorization color is opaque, because otherwise the window
   1954         //       contents is blended additively with the previous frame instead
   1955         //       of replacing it
   1956         if (FAILED(DwmGetColorizationColor(&color, &opaque)) || opaque)
   1957             return GLFW_FALSE;
   1958     }
   1959 
   1960     return GLFW_TRUE;
   1961 }
   1962 
   1963 void _glfwPlatformSetWindowResizable(_GLFWwindow* window, GLFWbool enabled)
   1964 {
   1965     updateWindowStyles(window);
   1966 }
   1967 
   1968 void _glfwPlatformSetWindowDecorated(_GLFWwindow* window, GLFWbool enabled)
   1969 {
   1970     updateWindowStyles(window);
   1971 }
   1972 
   1973 void _glfwPlatformSetWindowFloating(_GLFWwindow* window, GLFWbool enabled)
   1974 {
   1975     const HWND after = enabled ? HWND_TOPMOST : HWND_NOTOPMOST;
   1976     SetWindowPos(window->win32.handle, after, 0, 0, 0, 0,
   1977                  SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
   1978 }
   1979 
   1980 float _glfwPlatformGetWindowOpacity(_GLFWwindow* window)
   1981 {
   1982     BYTE alpha;
   1983     DWORD flags;
   1984 
   1985     if ((GetWindowLongW(window->win32.handle, GWL_EXSTYLE) & WS_EX_LAYERED) &&
   1986         GetLayeredWindowAttributes(window->win32.handle, NULL, &alpha, &flags))
   1987     {
   1988         if (flags & LWA_ALPHA)
   1989             return alpha / 255.f;
   1990     }
   1991 
   1992     return 1.f;
   1993 }
   1994 
   1995 void _glfwPlatformSetWindowOpacity(_GLFWwindow* window, float opacity)
   1996 {
   1997     if (opacity < 1.f)
   1998     {
   1999         const BYTE alpha = (BYTE) (255 * opacity);
   2000         DWORD style = GetWindowLongW(window->win32.handle, GWL_EXSTYLE);
   2001         style |= WS_EX_LAYERED;
   2002         SetWindowLongW(window->win32.handle, GWL_EXSTYLE, style);
   2003         SetLayeredWindowAttributes(window->win32.handle, 0, alpha, LWA_ALPHA);
   2004     }
   2005     else
   2006     {
   2007         DWORD style = GetWindowLongW(window->win32.handle, GWL_EXSTYLE);
   2008         style &= ~WS_EX_LAYERED;
   2009         SetWindowLongW(window->win32.handle, GWL_EXSTYLE, style);
   2010     }
   2011 }
   2012 
   2013 void _glfwPlatformSetRawMouseMotion(_GLFWwindow *window, GLFWbool enabled)
   2014 {
   2015     if (_glfw.win32.disabledCursorWindow != window)
   2016         return;
   2017 
   2018     if (enabled)
   2019         enableRawMouseMotion(window);
   2020     else
   2021         disableRawMouseMotion(window);
   2022 }
   2023 
   2024 GLFWbool _glfwPlatformRawMouseMotionSupported(void)
   2025 {
   2026     return GLFW_TRUE;
   2027 }
   2028 
   2029 void _glfwPlatformPollEvents(void)
   2030 {
   2031     MSG msg;
   2032     HWND handle;
   2033     _GLFWwindow* window;
   2034 
   2035     while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
   2036     {
   2037         if (msg.message == WM_QUIT)
   2038         {
   2039             // NOTE: While GLFW does not itself post WM_QUIT, other processes
   2040             //       may post it to this one, for example Task Manager
   2041             // HACK: Treat WM_QUIT as a close on all windows
   2042 
   2043             window = _glfw.windowListHead;
   2044             while (window)
   2045             {
   2046                 _glfwInputWindowCloseRequest(window);
   2047                 window = window->next;
   2048             }
   2049         }
   2050         else
   2051         {
   2052             TranslateMessage(&msg);
   2053             DispatchMessageW(&msg);
   2054         }
   2055     }
   2056 
   2057     // HACK: Release modifier keys that the system did not emit KEYUP for
   2058     // NOTE: Shift keys on Windows tend to "stick" when both are pressed as
   2059     //       no key up message is generated by the first key release
   2060     // NOTE: Windows key is not reported as released by the Win+V hotkey
   2061     //       Other Win hotkeys are handled implicitly by _glfwInputWindowFocus
   2062     //       because they change the input focus
   2063     // NOTE: The other half of this is in the WM_*KEY* handler in windowProc
   2064     handle = GetActiveWindow();
   2065     if (handle)
   2066     {
   2067         window = GetPropW(handle, L"GLFW");
   2068         if (window)
   2069         {
   2070             int i;
   2071             const int keys[4][2] =
   2072             {
   2073                 { VK_LSHIFT, GLFW_KEY_LEFT_SHIFT },
   2074                 { VK_RSHIFT, GLFW_KEY_RIGHT_SHIFT },
   2075                 { VK_LWIN, GLFW_KEY_LEFT_SUPER },
   2076                 { VK_RWIN, GLFW_KEY_RIGHT_SUPER }
   2077             };
   2078 
   2079             for (i = 0;  i < 4;  i++)
   2080             {
   2081                 const int vk = keys[i][0];
   2082                 const int key = keys[i][1];
   2083                 const int scancode = _glfw.win32.scancodes[key];
   2084 
   2085                 if ((GetKeyState(vk) & 0x8000))
   2086                     continue;
   2087                 if (window->keys[key] != GLFW_PRESS)
   2088                     continue;
   2089 
   2090                 _glfwInputKey(window, key, scancode, GLFW_RELEASE, getKeyMods());
   2091             }
   2092         }
   2093     }
   2094 
   2095     window = _glfw.win32.disabledCursorWindow;
   2096     if (window)
   2097     {
   2098         int width, height;
   2099         _glfwPlatformGetWindowSize(window, &width, &height);
   2100 
   2101         // NOTE: Re-center the cursor only if it has moved since the last call,
   2102         //       to avoid breaking glfwWaitEvents with WM_MOUSEMOVE
   2103         if (window->win32.lastCursorPosX != width / 2 ||
   2104             window->win32.lastCursorPosY != height / 2)
   2105         {
   2106             _glfwPlatformSetCursorPos(window, width / 2, height / 2);
   2107         }
   2108     }
   2109 }
   2110 
   2111 void _glfwPlatformWaitEvents(void)
   2112 {
   2113     WaitMessage();
   2114 
   2115     _glfwPlatformPollEvents();
   2116 }
   2117 
   2118 void _glfwPlatformWaitEventsTimeout(double timeout)
   2119 {
   2120     MsgWaitForMultipleObjects(0, NULL, FALSE, (DWORD) (timeout * 1e3), QS_ALLEVENTS);
   2121 
   2122     _glfwPlatformPollEvents();
   2123 }
   2124 
   2125 void _glfwPlatformPostEmptyEvent(void)
   2126 {
   2127     PostMessageW(_glfw.win32.helperWindowHandle, WM_NULL, 0, 0);
   2128 }
   2129 
   2130 void _glfwPlatformGetCursorPos(_GLFWwindow* window, double* xpos, double* ypos)
   2131 {
   2132     POINT pos;
   2133 
   2134     if (GetCursorPos(&pos))
   2135     {
   2136         ScreenToClient(window->win32.handle, &pos);
   2137 
   2138         if (xpos)
   2139             *xpos = pos.x;
   2140         if (ypos)
   2141             *ypos = pos.y;
   2142     }
   2143 }
   2144 
   2145 void _glfwPlatformSetCursorPos(_GLFWwindow* window, double xpos, double ypos)
   2146 {
   2147     POINT pos = { (int) xpos, (int) ypos };
   2148 
   2149     // Store the new position so it can be recognized later
   2150     window->win32.lastCursorPosX = pos.x;
   2151     window->win32.lastCursorPosY = pos.y;
   2152 
   2153     ClientToScreen(window->win32.handle, &pos);
   2154     SetCursorPos(pos.x, pos.y);
   2155 }
   2156 
   2157 void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode)
   2158 {
   2159     if (mode == GLFW_CURSOR_DISABLED)
   2160     {
   2161         if (_glfwPlatformWindowFocused(window))
   2162             disableCursor(window);
   2163     }
   2164     else if (_glfw.win32.disabledCursorWindow == window)
   2165         enableCursor(window);
   2166     else if (cursorInContentArea(window))
   2167         updateCursorImage(window);
   2168 }
   2169 
   2170 const char* _glfwPlatformGetScancodeName(int scancode)
   2171 {
   2172     if (scancode < 0 || scancode > (KF_EXTENDED | 0xff) ||
   2173         _glfw.win32.keycodes[scancode] == GLFW_KEY_UNKNOWN)
   2174     {
   2175         _glfwInputError(GLFW_INVALID_VALUE, "Invalid scancode %i", scancode);
   2176         return NULL;
   2177     }
   2178 
   2179     return _glfw.win32.keynames[_glfw.win32.keycodes[scancode]];
   2180 }
   2181 
   2182 int _glfwPlatformGetKeyScancode(int key)
   2183 {
   2184     return _glfw.win32.scancodes[key];
   2185 }
   2186 
   2187 int _glfwPlatformCreateCursor(_GLFWcursor* cursor,
   2188                               const GLFWimage* image,
   2189                               int xhot, int yhot)
   2190 {
   2191     cursor->win32.handle = (HCURSOR) createIcon(image, xhot, yhot, GLFW_FALSE);
   2192     if (!cursor->win32.handle)
   2193         return GLFW_FALSE;
   2194 
   2195     return GLFW_TRUE;
   2196 }
   2197 
   2198 int _glfwPlatformCreateStandardCursor(_GLFWcursor* cursor, int shape)
   2199 {
   2200     int id = 0;
   2201 
   2202     if (shape == GLFW_ARROW_CURSOR)
   2203         id = OCR_NORMAL;
   2204     else if (shape == GLFW_IBEAM_CURSOR)
   2205         id = OCR_IBEAM;
   2206     else if (shape == GLFW_CROSSHAIR_CURSOR)
   2207         id = OCR_CROSS;
   2208     else if (shape == GLFW_HAND_CURSOR)
   2209         id = OCR_HAND;
   2210     else if (shape == GLFW_HRESIZE_CURSOR)
   2211         id = OCR_SIZEWE;
   2212     else if (shape == GLFW_VRESIZE_CURSOR)
   2213         id = OCR_SIZENS;
   2214     else
   2215         return GLFW_FALSE;
   2216 
   2217     cursor->win32.handle = LoadImageW(NULL,
   2218                                       MAKEINTRESOURCEW(id), IMAGE_CURSOR, 0, 0,
   2219                                       LR_DEFAULTSIZE | LR_SHARED);
   2220     if (!cursor->win32.handle)
   2221     {
   2222         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
   2223                              "Win32: Failed to create standard cursor");
   2224         return GLFW_FALSE;
   2225     }
   2226 
   2227     return GLFW_TRUE;
   2228 }
   2229 
   2230 void _glfwPlatformDestroyCursor(_GLFWcursor* cursor)
   2231 {
   2232     if (cursor->win32.handle)
   2233         DestroyIcon((HICON) cursor->win32.handle);
   2234 }
   2235 
   2236 void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor)
   2237 {
   2238     if (cursorInContentArea(window))
   2239         updateCursorImage(window);
   2240 }
   2241 
   2242 void _glfwPlatformSetClipboardString(const char* string)
   2243 {
   2244     int characterCount;
   2245     HANDLE object;
   2246     WCHAR* buffer;
   2247 
   2248     characterCount = MultiByteToWideChar(CP_UTF8, 0, string, -1, NULL, 0);
   2249     if (!characterCount)
   2250         return;
   2251 
   2252     object = GlobalAlloc(GMEM_MOVEABLE, characterCount * sizeof(WCHAR));
   2253     if (!object)
   2254     {
   2255         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
   2256                              "Win32: Failed to allocate global handle for clipboard");
   2257         return;
   2258     }
   2259 
   2260     buffer = GlobalLock(object);
   2261     if (!buffer)
   2262     {
   2263         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
   2264                              "Win32: Failed to lock global handle");
   2265         GlobalFree(object);
   2266         return;
   2267     }
   2268 
   2269     MultiByteToWideChar(CP_UTF8, 0, string, -1, buffer, characterCount);
   2270     GlobalUnlock(object);
   2271 
   2272     if (!OpenClipboard(_glfw.win32.helperWindowHandle))
   2273     {
   2274         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
   2275                              "Win32: Failed to open clipboard");
   2276         GlobalFree(object);
   2277         return;
   2278     }
   2279 
   2280     EmptyClipboard();
   2281     SetClipboardData(CF_UNICODETEXT, object);
   2282     CloseClipboard();
   2283 }
   2284 
   2285 const char* _glfwPlatformGetClipboardString(void)
   2286 {
   2287     HANDLE object;
   2288     WCHAR* buffer;
   2289 
   2290     if (!OpenClipboard(_glfw.win32.helperWindowHandle))
   2291     {
   2292         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
   2293                              "Win32: Failed to open clipboard");
   2294         return NULL;
   2295     }
   2296 
   2297     object = GetClipboardData(CF_UNICODETEXT);
   2298     if (!object)
   2299     {
   2300         _glfwInputErrorWin32(GLFW_FORMAT_UNAVAILABLE,
   2301                              "Win32: Failed to convert clipboard to string");
   2302         CloseClipboard();
   2303         return NULL;
   2304     }
   2305 
   2306     buffer = GlobalLock(object);
   2307     if (!buffer)
   2308     {
   2309         _glfwInputErrorWin32(GLFW_PLATFORM_ERROR,
   2310                              "Win32: Failed to lock global handle");
   2311         CloseClipboard();
   2312         return NULL;
   2313     }
   2314 
   2315     free(_glfw.win32.clipboardString);
   2316     _glfw.win32.clipboardString = _glfwCreateUTF8FromWideStringWin32(buffer);
   2317 
   2318     GlobalUnlock(object);
   2319     CloseClipboard();
   2320 
   2321     return _glfw.win32.clipboardString;
   2322 }
   2323 
   2324 void _glfwPlatformGetRequiredInstanceExtensions(char** extensions)
   2325 {
   2326     if (!_glfw.vk.KHR_surface || !_glfw.vk.KHR_win32_surface)
   2327         return;
   2328 
   2329     extensions[0] = "VK_KHR_surface";
   2330     extensions[1] = "VK_KHR_win32_surface";
   2331 }
   2332 
   2333 int _glfwPlatformGetPhysicalDevicePresentationSupport(VkInstance instance,
   2334                                                       VkPhysicalDevice device,
   2335                                                       uint32_t queuefamily)
   2336 {
   2337     PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR
   2338         vkGetPhysicalDeviceWin32PresentationSupportKHR =
   2339         (PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR)
   2340         vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceWin32PresentationSupportKHR");
   2341     if (!vkGetPhysicalDeviceWin32PresentationSupportKHR)
   2342     {
   2343         _glfwInputError(GLFW_API_UNAVAILABLE,
   2344                         "Win32: Vulkan instance missing VK_KHR_win32_surface extension");
   2345         return GLFW_FALSE;
   2346     }
   2347 
   2348     return vkGetPhysicalDeviceWin32PresentationSupportKHR(device, queuefamily);
   2349 }
   2350 
   2351 VkResult _glfwPlatformCreateWindowSurface(VkInstance instance,
   2352                                           _GLFWwindow* window,
   2353                                           const VkAllocationCallbacks* allocator,
   2354                                           VkSurfaceKHR* surface)
   2355 {
   2356     VkResult err;
   2357     VkWin32SurfaceCreateInfoKHR sci;
   2358     PFN_vkCreateWin32SurfaceKHR vkCreateWin32SurfaceKHR;
   2359 
   2360     vkCreateWin32SurfaceKHR = (PFN_vkCreateWin32SurfaceKHR)
   2361         vkGetInstanceProcAddr(instance, "vkCreateWin32SurfaceKHR");
   2362     if (!vkCreateWin32SurfaceKHR)
   2363     {
   2364         _glfwInputError(GLFW_API_UNAVAILABLE,
   2365                         "Win32: Vulkan instance missing VK_KHR_win32_surface extension");
   2366         return VK_ERROR_EXTENSION_NOT_PRESENT;
   2367     }
   2368 
   2369     memset(&sci, 0, sizeof(sci));
   2370     sci.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
   2371     sci.hinstance = _glfw.win32.instance;
   2372     sci.hwnd = window->win32.handle;
   2373 
   2374     err = vkCreateWin32SurfaceKHR(instance, &sci, allocator, surface);
   2375     if (err)
   2376     {
   2377         _glfwInputError(GLFW_PLATFORM_ERROR,
   2378                         "Win32: Failed to create Vulkan surface: %s",
   2379                         _glfwGetVulkanResultString(err));
   2380     }
   2381 
   2382     return err;
   2383 }
   2384 
   2385 
   2386 //////////////////////////////////////////////////////////////////////////
   2387 //////                        GLFW native API                       //////
   2388 //////////////////////////////////////////////////////////////////////////
   2389 
   2390 GLFWAPI HWND glfwGetWin32Window(GLFWwindow* handle)
   2391 {
   2392     _GLFWwindow* window = (_GLFWwindow*) handle;
   2393     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
   2394     return window->win32.handle;
   2395 }
   2396