win32_platform.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. //========================================================================
  2. // GLFW 3.4 Win32 - www.glfw.org
  3. //------------------------------------------------------------------------
  4. // Copyright (c) 2002-2006 Marcus Geelnard
  5. // Copyright (c) 2006-2019 Camilla Löwy <[email protected]>
  6. //
  7. // This software is provided 'as-is', without any express or implied
  8. // warranty. In no event will the authors be held liable for any damages
  9. // arising from the use of this software.
  10. //
  11. // Permission is granted to anyone to use this software for any purpose,
  12. // including commercial applications, and to alter it and redistribute it
  13. // freely, subject to the following restrictions:
  14. //
  15. // 1. The origin of this software must not be misrepresented; you must not
  16. // claim that you wrote the original software. If you use this software
  17. // in a product, an acknowledgment in the product documentation would
  18. // be appreciated but is not required.
  19. //
  20. // 2. Altered source versions must be plainly marked as such, and must not
  21. // be misrepresented as being the original software.
  22. //
  23. // 3. This notice may not be removed or altered from any source
  24. // distribution.
  25. //
  26. //========================================================================
  27. // We don't need all the fancy stuff
  28. #ifndef NOMINMAX
  29. #define NOMINMAX
  30. #endif
  31. #ifndef VC_EXTRALEAN
  32. #define VC_EXTRALEAN
  33. #endif
  34. #ifndef WIN32_LEAN_AND_MEAN
  35. #define WIN32_LEAN_AND_MEAN
  36. #endif
  37. // This is a workaround for the fact that glfw3.h needs to export APIENTRY (for
  38. // example to allow applications to correctly declare a GL_ARB_debug_output
  39. // callback) but windows.h assumes no one will define APIENTRY before it does
  40. // #undef APIENTRY
  41. // GLFW on Windows is Unicode only and does not work in MBCS mode
  42. #ifndef UNICODE
  43. #define UNICODE
  44. #endif
  45. // GLFW requires Windows XP or later
  46. #if WINVER < 0x0501
  47. #undef WINVER
  48. #define WINVER 0x0501
  49. #endif
  50. #if _WIN32_WINNT < 0x0501
  51. #undef _WIN32_WINNT
  52. #define _WIN32_WINNT 0x0501
  53. #endif
  54. // GLFW uses DirectInput8 interfaces
  55. #define DIRECTINPUT_VERSION 0x0800
  56. // GLFW uses OEM cursor resources
  57. #define OEMRESOURCE
  58. #include <wctype.h>
  59. #include <windows.h>
  60. #include <dinput.h>
  61. #include <xinput.h>
  62. #include <dbt.h>
  63. // HACK: Define macros that some windows.h variants don't
  64. #ifndef WM_MOUSEHWHEEL
  65. #define WM_MOUSEHWHEEL 0x020E
  66. #endif
  67. #ifndef WM_DWMCOMPOSITIONCHANGED
  68. #define WM_DWMCOMPOSITIONCHANGED 0x031E
  69. #endif
  70. #ifndef WM_COPYGLOBALDATA
  71. #define WM_COPYGLOBALDATA 0x0049
  72. #endif
  73. #ifndef WM_UNICHAR
  74. #define WM_UNICHAR 0x0109
  75. #endif
  76. #ifndef UNICODE_NOCHAR
  77. #define UNICODE_NOCHAR 0xFFFF
  78. #endif
  79. #ifndef WM_DPICHANGED
  80. #define WM_DPICHANGED 0x02E0
  81. #endif
  82. #ifndef GET_XBUTTON_WPARAM
  83. #define GET_XBUTTON_WPARAM(w) (HIWORD(w))
  84. #endif
  85. #ifndef EDS_ROTATEDMODE
  86. #define EDS_ROTATEDMODE 0x00000004
  87. #endif
  88. #ifndef DISPLAY_DEVICE_ACTIVE
  89. #define DISPLAY_DEVICE_ACTIVE 0x00000001
  90. #endif
  91. #ifndef _WIN32_WINNT_WINBLUE
  92. #define _WIN32_WINNT_WINBLUE 0x0602
  93. #endif
  94. #ifndef _WIN32_WINNT_WIN8
  95. #define _WIN32_WINNT_WIN8 0x0602
  96. #endif
  97. #ifndef WM_GETDPISCALEDSIZE
  98. #define WM_GETDPISCALEDSIZE 0x02e4
  99. #endif
  100. #ifndef USER_DEFAULT_SCREEN_DPI
  101. #define USER_DEFAULT_SCREEN_DPI 96
  102. #endif
  103. #ifndef OCR_HAND
  104. #define OCR_HAND 32649
  105. #endif
  106. #if WINVER < 0x0601
  107. typedef struct
  108. {
  109. DWORD cbSize;
  110. DWORD ExtStatus;
  111. } CHANGEFILTERSTRUCT;
  112. #ifndef MSGFLT_ALLOW
  113. #define MSGFLT_ALLOW 1
  114. #endif
  115. #endif /*Windows 7*/
  116. #if WINVER < 0x0600
  117. #define DWM_BB_ENABLE 0x00000001
  118. #define DWM_BB_BLURREGION 0x00000002
  119. typedef struct
  120. {
  121. DWORD dwFlags;
  122. BOOL fEnable;
  123. HRGN hRgnBlur;
  124. BOOL fTransitionOnMaximized;
  125. } DWM_BLURBEHIND;
  126. #else
  127. #include <dwmapi.h>
  128. #endif /*Windows Vista*/
  129. #ifndef DPI_ENUMS_DECLARED
  130. typedef enum
  131. {
  132. PROCESS_DPI_UNAWARE = 0,
  133. PROCESS_SYSTEM_DPI_AWARE = 1,
  134. PROCESS_PER_MONITOR_DPI_AWARE = 2
  135. } PROCESS_DPI_AWARENESS;
  136. typedef enum
  137. {
  138. MDT_EFFECTIVE_DPI = 0,
  139. MDT_ANGULAR_DPI = 1,
  140. MDT_RAW_DPI = 2,
  141. MDT_DEFAULT = MDT_EFFECTIVE_DPI
  142. } MONITOR_DPI_TYPE;
  143. #endif /*DPI_ENUMS_DECLARED*/
  144. #ifndef DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2
  145. #define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((HANDLE) -4)
  146. #endif /*DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2*/
  147. // HACK: Define versionhelpers.h functions manually as MinGW lacks the header
  148. #define IsWindowsXPOrGreater() \
  149. _glfwIsWindowsVersionOrGreaterWin32(HIBYTE(_WIN32_WINNT_WINXP), \
  150. LOBYTE(_WIN32_WINNT_WINXP), 0)
  151. #define IsWindowsVistaOrGreater() \
  152. _glfwIsWindowsVersionOrGreaterWin32(HIBYTE(_WIN32_WINNT_VISTA), \
  153. LOBYTE(_WIN32_WINNT_VISTA), 0)
  154. #define IsWindows7OrGreater() \
  155. _glfwIsWindowsVersionOrGreaterWin32(HIBYTE(_WIN32_WINNT_WIN7), \
  156. LOBYTE(_WIN32_WINNT_WIN7), 0)
  157. #define IsWindows8OrGreater() \
  158. _glfwIsWindowsVersionOrGreaterWin32(HIBYTE(_WIN32_WINNT_WIN8), \
  159. LOBYTE(_WIN32_WINNT_WIN8), 0)
  160. #define IsWindows8Point1OrGreater() \
  161. _glfwIsWindowsVersionOrGreaterWin32(HIBYTE(_WIN32_WINNT_WINBLUE), \
  162. LOBYTE(_WIN32_WINNT_WINBLUE), 0)
  163. #define _glfwIsWindows10AnniversaryUpdateOrGreaterWin32() \
  164. _glfwIsWindows10BuildOrGreaterWin32(14393)
  165. #define _glfwIsWindows10CreatorsUpdateOrGreaterWin32() \
  166. _glfwIsWindows10BuildOrGreaterWin32(15063)
  167. // HACK: Define macros that some xinput.h variants don't
  168. #ifndef XINPUT_CAPS_WIRELESS
  169. #define XINPUT_CAPS_WIRELESS 0x0002
  170. #endif
  171. #ifndef XINPUT_DEVSUBTYPE_WHEEL
  172. #define XINPUT_DEVSUBTYPE_WHEEL 0x02
  173. #endif
  174. #ifndef XINPUT_DEVSUBTYPE_ARCADE_STICK
  175. #define XINPUT_DEVSUBTYPE_ARCADE_STICK 0x03
  176. #endif
  177. #ifndef XINPUT_DEVSUBTYPE_FLIGHT_STICK
  178. #define XINPUT_DEVSUBTYPE_FLIGHT_STICK 0x04
  179. #endif
  180. #ifndef XINPUT_DEVSUBTYPE_DANCE_PAD
  181. #define XINPUT_DEVSUBTYPE_DANCE_PAD 0x05
  182. #endif
  183. #ifndef XINPUT_DEVSUBTYPE_GUITAR
  184. #define XINPUT_DEVSUBTYPE_GUITAR 0x06
  185. #endif
  186. #ifndef XINPUT_DEVSUBTYPE_DRUM_KIT
  187. #define XINPUT_DEVSUBTYPE_DRUM_KIT 0x08
  188. #endif
  189. #ifndef XINPUT_DEVSUBTYPE_ARCADE_PAD
  190. #define XINPUT_DEVSUBTYPE_ARCADE_PAD 0x13
  191. #endif
  192. #ifndef XUSER_MAX_COUNT
  193. #define XUSER_MAX_COUNT 4
  194. #endif
  195. // HACK: Define macros that some dinput.h variants don't
  196. #ifndef DIDFT_OPTIONAL
  197. #define DIDFT_OPTIONAL 0x80000000
  198. #endif
  199. // winmm.dll function pointer typedefs
  200. typedef DWORD (WINAPI * PFN_timeGetTime)(void);
  201. #define timeGetTime _glfw.win32.winmm.GetTime
  202. // xinput.dll function pointer typedefs
  203. typedef DWORD (WINAPI * PFN_XInputGetCapabilities)(DWORD,DWORD,XINPUT_CAPABILITIES*);
  204. typedef DWORD (WINAPI * PFN_XInputGetState)(DWORD,XINPUT_STATE*);
  205. #define XInputGetCapabilities _glfw.win32.xinput.GetCapabilities
  206. #define XInputGetState _glfw.win32.xinput.GetState
  207. // dinput8.dll function pointer typedefs
  208. typedef HRESULT (WINAPI * PFN_DirectInput8Create)(HINSTANCE,DWORD,REFIID,LPVOID*,LPUNKNOWN);
  209. #define DirectInput8Create _glfw.win32.dinput8.Create
  210. // user32.dll function pointer typedefs
  211. typedef BOOL (WINAPI * PFN_SetProcessDPIAware)(void);
  212. typedef BOOL (WINAPI * PFN_ChangeWindowMessageFilterEx)(HWND,UINT,DWORD,CHANGEFILTERSTRUCT*);
  213. typedef BOOL (WINAPI * PFN_EnableNonClientDpiScaling)(HWND);
  214. typedef BOOL (WINAPI * PFN_SetProcessDpiAwarenessContext)(HANDLE);
  215. typedef UINT (WINAPI * PFN_GetDpiForWindow)(HWND);
  216. typedef BOOL (WINAPI * PFN_AdjustWindowRectExForDpi)(LPRECT,DWORD,BOOL,DWORD,UINT);
  217. #define SetProcessDPIAware _glfw.win32.user32.SetProcessDPIAware_
  218. #define ChangeWindowMessageFilterEx _glfw.win32.user32.ChangeWindowMessageFilterEx_
  219. #define EnableNonClientDpiScaling _glfw.win32.user32.EnableNonClientDpiScaling_
  220. #define SetProcessDpiAwarenessContext _glfw.win32.user32.SetProcessDpiAwarenessContext_
  221. #define GetDpiForWindow _glfw.win32.user32.GetDpiForWindow_
  222. #define AdjustWindowRectExForDpi _glfw.win32.user32.AdjustWindowRectExForDpi_
  223. // dwmapi.dll function pointer typedefs
  224. typedef HRESULT (WINAPI * PFN_DwmIsCompositionEnabled)(BOOL*);
  225. typedef HRESULT (WINAPI * PFN_DwmFlush)(VOID);
  226. typedef HRESULT(WINAPI * PFN_DwmEnableBlurBehindWindow)(HWND,const DWM_BLURBEHIND*);
  227. #define DwmIsCompositionEnabled _glfw.win32.dwmapi.IsCompositionEnabled
  228. #define DwmFlush _glfw.win32.dwmapi.Flush
  229. #define DwmEnableBlurBehindWindow _glfw.win32.dwmapi.EnableBlurBehindWindow
  230. // shcore.dll function pointer typedefs
  231. typedef HRESULT (WINAPI * PFN_SetProcessDpiAwareness)(PROCESS_DPI_AWARENESS);
  232. typedef HRESULT (WINAPI * PFN_GetDpiForMonitor)(HMONITOR,MONITOR_DPI_TYPE,UINT*,UINT*);
  233. #define SetProcessDpiAwareness _glfw.win32.shcore.SetProcessDpiAwareness_
  234. #define GetDpiForMonitor _glfw.win32.shcore.GetDpiForMonitor_
  235. // ntdll.dll function pointer typedefs
  236. typedef LONG (WINAPI * PFN_RtlVerifyVersionInfo)(OSVERSIONINFOEXW*,ULONG,ULONGLONG);
  237. #define RtlVerifyVersionInfo _glfw.win32.ntdll.RtlVerifyVersionInfo_
  238. typedef VkFlags VkWin32SurfaceCreateFlagsKHR;
  239. typedef struct VkWin32SurfaceCreateInfoKHR
  240. {
  241. VkStructureType sType;
  242. const void* pNext;
  243. VkWin32SurfaceCreateFlagsKHR flags;
  244. HINSTANCE hinstance;
  245. HWND hwnd;
  246. } VkWin32SurfaceCreateInfoKHR;
  247. typedef VkResult (APIENTRY *PFN_vkCreateWin32SurfaceKHR)(VkInstance,const VkWin32SurfaceCreateInfoKHR*,const VkAllocationCallbacks*,VkSurfaceKHR*);
  248. typedef VkBool32 (APIENTRY *PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR)(VkPhysicalDevice,uint32_t);
  249. #include "win32_joystick.h"
  250. #include "wgl_context.h"
  251. #include "egl_context.h"
  252. #include "osmesa_context.h"
  253. #if !defined(_GLFW_WNDCLASSNAME)
  254. #define _GLFW_WNDCLASSNAME L"GLFW30"
  255. #endif
  256. #define _glfw_dlopen(name) LoadLibraryA(name)
  257. #define _glfw_dlclose(handle) FreeLibrary((HMODULE) handle)
  258. #define _glfw_dlsym(handle, name) GetProcAddress((HMODULE) handle, name)
  259. #define _GLFW_EGL_NATIVE_WINDOW ((EGLNativeWindowType) window->win32.handle)
  260. #define _GLFW_EGL_NATIVE_DISPLAY EGL_DEFAULT_DISPLAY
  261. #define _GLFW_PLATFORM_WINDOW_STATE _GLFWwindowWin32 win32
  262. #define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE _GLFWlibraryWin32 win32
  263. #define _GLFW_PLATFORM_LIBRARY_TIMER_STATE _GLFWtimerWin32 win32
  264. #define _GLFW_PLATFORM_MONITOR_STATE _GLFWmonitorWin32 win32
  265. #define _GLFW_PLATFORM_CURSOR_STATE _GLFWcursorWin32 win32
  266. #define _GLFW_PLATFORM_TLS_STATE _GLFWtlsWin32 win32
  267. #define _GLFW_PLATFORM_MUTEX_STATE _GLFWmutexWin32 win32
  268. // Win32-specific per-window data
  269. //
  270. typedef struct _GLFWwindowWin32
  271. {
  272. HWND handle;
  273. HICON bigIcon;
  274. HICON smallIcon;
  275. GLFWbool cursorTracked;
  276. GLFWbool frameAction;
  277. GLFWbool iconified;
  278. GLFWbool maximized;
  279. // Whether to enable framebuffer transparency on DWM
  280. GLFWbool transparent;
  281. GLFWbool scaleToMonitor;
  282. GLFWbool keymenu;
  283. // The last received cursor position, regardless of source
  284. int lastCursorPosX, lastCursorPosY;
  285. } _GLFWwindowWin32;
  286. // Win32-specific global data
  287. //
  288. typedef struct _GLFWlibraryWin32
  289. {
  290. HWND helperWindowHandle;
  291. HDEVNOTIFY deviceNotificationHandle;
  292. DWORD foregroundLockTimeout;
  293. int acquiredMonitorCount;
  294. char* clipboardString;
  295. short int keycodes[512];
  296. short int scancodes[GLFW_KEY_LAST + 1];
  297. char keynames[GLFW_KEY_LAST + 1][5];
  298. // Where to place the cursor when re-enabled
  299. double restoreCursorPosX, restoreCursorPosY;
  300. // The window whose disabled cursor mode is active
  301. _GLFWwindow* disabledCursorWindow;
  302. RAWINPUT* rawInput;
  303. int rawInputSize;
  304. UINT mouseTrailSize;
  305. struct {
  306. HINSTANCE instance;
  307. PFN_timeGetTime GetTime;
  308. } winmm;
  309. struct {
  310. HINSTANCE instance;
  311. PFN_DirectInput8Create Create;
  312. IDirectInput8W* api;
  313. } dinput8;
  314. struct {
  315. HINSTANCE instance;
  316. PFN_XInputGetCapabilities GetCapabilities;
  317. PFN_XInputGetState GetState;
  318. } xinput;
  319. struct {
  320. HINSTANCE instance;
  321. PFN_SetProcessDPIAware SetProcessDPIAware_;
  322. PFN_ChangeWindowMessageFilterEx ChangeWindowMessageFilterEx_;
  323. PFN_EnableNonClientDpiScaling EnableNonClientDpiScaling_;
  324. PFN_SetProcessDpiAwarenessContext SetProcessDpiAwarenessContext_;
  325. PFN_GetDpiForWindow GetDpiForWindow_;
  326. PFN_AdjustWindowRectExForDpi AdjustWindowRectExForDpi_;
  327. } user32;
  328. struct {
  329. HINSTANCE instance;
  330. PFN_DwmIsCompositionEnabled IsCompositionEnabled;
  331. PFN_DwmFlush Flush;
  332. PFN_DwmEnableBlurBehindWindow EnableBlurBehindWindow;
  333. } dwmapi;
  334. struct {
  335. HINSTANCE instance;
  336. PFN_SetProcessDpiAwareness SetProcessDpiAwareness_;
  337. PFN_GetDpiForMonitor GetDpiForMonitor_;
  338. } shcore;
  339. struct {
  340. HINSTANCE instance;
  341. PFN_RtlVerifyVersionInfo RtlVerifyVersionInfo_;
  342. } ntdll;
  343. } _GLFWlibraryWin32;
  344. // Win32-specific per-monitor data
  345. //
  346. typedef struct _GLFWmonitorWin32
  347. {
  348. HMONITOR handle;
  349. // This size matches the static size of DISPLAY_DEVICE.DeviceName
  350. WCHAR adapterName[32];
  351. WCHAR displayName[32];
  352. char publicAdapterName[32];
  353. char publicDisplayName[32];
  354. GLFWbool modesPruned;
  355. GLFWbool modeChanged;
  356. } _GLFWmonitorWin32;
  357. // Win32-specific per-cursor data
  358. //
  359. typedef struct _GLFWcursorWin32
  360. {
  361. HCURSOR handle;
  362. } _GLFWcursorWin32;
  363. // Win32-specific global timer data
  364. //
  365. typedef struct _GLFWtimerWin32
  366. {
  367. GLFWbool hasPC;
  368. uint64_t frequency;
  369. } _GLFWtimerWin32;
  370. // Win32-specific thread local storage data
  371. //
  372. typedef struct _GLFWtlsWin32
  373. {
  374. GLFWbool allocated;
  375. DWORD index;
  376. } _GLFWtlsWin32;
  377. // Win32-specific mutex data
  378. //
  379. typedef struct _GLFWmutexWin32
  380. {
  381. GLFWbool allocated;
  382. CRITICAL_SECTION section;
  383. } _GLFWmutexWin32;
  384. GLFWbool _glfwRegisterWindowClassWin32(void);
  385. void _glfwUnregisterWindowClassWin32(void);
  386. WCHAR* _glfwCreateWideStringFromUTF8Win32(const char* source);
  387. char* _glfwCreateUTF8FromWideStringWin32(const WCHAR* source);
  388. BOOL _glfwIsWindowsVersionOrGreaterWin32(WORD major, WORD minor, WORD sp);
  389. BOOL _glfwIsWindows10BuildOrGreaterWin32(WORD build);
  390. void _glfwInputErrorWin32(int error, const char* description);
  391. void _glfwUpdateKeyNamesWin32(void);
  392. void _glfwInitTimerWin32(void);
  393. void _glfwPollMonitorsWin32(void);
  394. void _glfwSetVideoModeWin32(_GLFWmonitor* monitor, const GLFWvidmode* desired);
  395. void _glfwRestoreVideoModeWin32(_GLFWmonitor* monitor);
  396. void _glfwGetMonitorContentScaleWin32(HMONITOR handle, float* xscale, float* yscale);