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