display_server_windows.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. /**************************************************************************/
  2. /* display_server_windows.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "crash_handler_windows.h"
  32. #include "joypad_windows.h"
  33. #include "key_mapping_windows.h"
  34. #include "tts_windows.h"
  35. #include "core/config/project_settings.h"
  36. #include "core/input/input.h"
  37. #include "core/io/image.h"
  38. #include "core/os/os.h"
  39. #include "drivers/wasapi/audio_driver_wasapi.h"
  40. #include "drivers/winmidi/midi_driver_winmidi.h"
  41. #include "servers/audio_server.h"
  42. #include "servers/display_server.h"
  43. #include "servers/rendering/renderer_compositor.h"
  44. #include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
  45. #include "servers/rendering_server.h"
  46. #ifdef XAUDIO2_ENABLED
  47. #include "drivers/xaudio2/audio_driver_xaudio2.h"
  48. #endif
  49. #if defined(RD_ENABLED)
  50. #include "servers/rendering/rendering_device.h"
  51. #endif
  52. #if defined(GLES3_ENABLED)
  53. #include "gl_manager_windows_angle.h"
  54. #include "gl_manager_windows_native.h"
  55. #endif // GLES3_ENABLED
  56. #include "native_menu_windows.h"
  57. #include <io.h>
  58. #include <cstdio>
  59. #define WIN32_LEAN_AND_MEAN
  60. #include <windows.h>
  61. #include <windowsx.h>
  62. // WinTab API
  63. #define WT_PACKET 0x7FF0
  64. #define WT_PROXIMITY 0x7FF5
  65. #define WT_INFOCHANGE 0x7FF6
  66. #define WT_CSRCHANGE 0x7FF7
  67. #define WTI_DEFSYSCTX 4
  68. #define WTI_DEVICES 100
  69. #define DVC_NPRESSURE 15
  70. #define DVC_TPRESSURE 16
  71. #define DVC_ORIENTATION 17
  72. #define DVC_ROTATION 18
  73. #define CXO_MESSAGES 0x0004
  74. #define PK_STATUS 0x0002
  75. #define PK_NORMAL_PRESSURE 0x0400
  76. #define PK_TANGENT_PRESSURE 0x0800
  77. #define PK_ORIENTATION 0x1000
  78. #define TPS_INVERT 0x0010 /* 1.1 */
  79. typedef struct tagLOGCONTEXTW {
  80. WCHAR lcName[40];
  81. UINT lcOptions;
  82. UINT lcStatus;
  83. UINT lcLocks;
  84. UINT lcMsgBase;
  85. UINT lcDevice;
  86. UINT lcPktRate;
  87. DWORD lcPktData;
  88. DWORD lcPktMode;
  89. DWORD lcMoveMask;
  90. DWORD lcBtnDnMask;
  91. DWORD lcBtnUpMask;
  92. LONG lcInOrgX;
  93. LONG lcInOrgY;
  94. LONG lcInOrgZ;
  95. LONG lcInExtX;
  96. LONG lcInExtY;
  97. LONG lcInExtZ;
  98. LONG lcOutOrgX;
  99. LONG lcOutOrgY;
  100. LONG lcOutOrgZ;
  101. LONG lcOutExtX;
  102. LONG lcOutExtY;
  103. LONG lcOutExtZ;
  104. DWORD lcSensX;
  105. DWORD lcSensY;
  106. DWORD lcSensZ;
  107. BOOL lcSysMode;
  108. int lcSysOrgX;
  109. int lcSysOrgY;
  110. int lcSysExtX;
  111. int lcSysExtY;
  112. DWORD lcSysSensX;
  113. DWORD lcSysSensY;
  114. } LOGCONTEXTW;
  115. typedef struct tagAXIS {
  116. LONG axMin;
  117. LONG axMax;
  118. UINT axUnits;
  119. DWORD axResolution;
  120. } AXIS;
  121. typedef struct tagORIENTATION {
  122. int orAzimuth;
  123. int orAltitude;
  124. int orTwist;
  125. } ORIENTATION;
  126. typedef struct tagPACKET {
  127. int pkStatus;
  128. int pkNormalPressure;
  129. int pkTangentPressure;
  130. ORIENTATION pkOrientation;
  131. } PACKET;
  132. typedef HANDLE(WINAPI *WTOpenPtr)(HWND p_window, LOGCONTEXTW *p_ctx, BOOL p_enable);
  133. typedef BOOL(WINAPI *WTClosePtr)(HANDLE p_ctx);
  134. typedef UINT(WINAPI *WTInfoPtr)(UINT p_category, UINT p_index, LPVOID p_output);
  135. typedef BOOL(WINAPI *WTPacketPtr)(HANDLE p_ctx, UINT p_param, LPVOID p_packets);
  136. typedef BOOL(WINAPI *WTEnablePtr)(HANDLE p_ctx, BOOL p_enable);
  137. enum PreferredAppMode {
  138. APPMODE_DEFAULT = 0,
  139. APPMODE_ALLOWDARK = 1,
  140. APPMODE_FORCEDARK = 2,
  141. APPMODE_FORCELIGHT = 3,
  142. APPMODE_MAX = 4
  143. };
  144. typedef const char *(CDECL *WineGetVersionPtr)(void);
  145. typedef bool(WINAPI *ShouldAppsUseDarkModePtr)();
  146. typedef DWORD(WINAPI *GetImmersiveColorFromColorSetExPtr)(UINT dwImmersiveColorSet, UINT dwImmersiveColorType, bool bIgnoreHighContrast, UINT dwHighContrastCacheMode);
  147. typedef int(WINAPI *GetImmersiveColorTypeFromNamePtr)(const WCHAR *name);
  148. typedef int(WINAPI *GetImmersiveUserColorSetPreferencePtr)(bool bForceCheckRegistry, bool bSkipCheckOnFail);
  149. typedef HRESULT(WINAPI *RtlGetVersionPtr)(OSVERSIONINFOEXW *lpVersionInformation);
  150. typedef bool(WINAPI *AllowDarkModeForAppPtr)(bool darkMode);
  151. typedef PreferredAppMode(WINAPI *SetPreferredAppModePtr)(PreferredAppMode appMode);
  152. typedef void(WINAPI *RefreshImmersiveColorPolicyStatePtr)();
  153. typedef void(WINAPI *FlushMenuThemesPtr)();
  154. // Windows Ink API
  155. #ifndef POINTER_STRUCTURES
  156. #define POINTER_STRUCTURES
  157. typedef DWORD POINTER_INPUT_TYPE;
  158. typedef UINT32 POINTER_FLAGS;
  159. typedef UINT32 PEN_FLAGS;
  160. typedef UINT32 PEN_MASK;
  161. #ifndef PEN_FLAG_INVERTED
  162. #define PEN_FLAG_INVERTED 0x00000002
  163. #endif
  164. #ifndef PEN_FLAG_ERASER
  165. #define PEN_FLAG_ERASER 0x00000004
  166. #endif
  167. #ifndef PEN_MASK_PRESSURE
  168. #define PEN_MASK_PRESSURE 0x00000001
  169. #endif
  170. #ifndef PEN_MASK_TILT_X
  171. #define PEN_MASK_TILT_X 0x00000004
  172. #endif
  173. #ifndef PEN_MASK_TILT_Y
  174. #define PEN_MASK_TILT_Y 0x00000008
  175. #endif
  176. #ifndef POINTER_MESSAGE_FLAG_FIRSTBUTTON
  177. #define POINTER_MESSAGE_FLAG_FIRSTBUTTON 0x00000010
  178. #endif
  179. #ifndef POINTER_MESSAGE_FLAG_SECONDBUTTON
  180. #define POINTER_MESSAGE_FLAG_SECONDBUTTON 0x00000020
  181. #endif
  182. #ifndef POINTER_MESSAGE_FLAG_THIRDBUTTON
  183. #define POINTER_MESSAGE_FLAG_THIRDBUTTON 0x00000040
  184. #endif
  185. #ifndef POINTER_MESSAGE_FLAG_FOURTHBUTTON
  186. #define POINTER_MESSAGE_FLAG_FOURTHBUTTON 0x00000080
  187. #endif
  188. #ifndef POINTER_MESSAGE_FLAG_FIFTHBUTTON
  189. #define POINTER_MESSAGE_FLAG_FIFTHBUTTON 0x00000100
  190. #endif
  191. #ifndef IS_POINTER_FLAG_SET_WPARAM
  192. #define IS_POINTER_FLAG_SET_WPARAM(wParam, flag) (((DWORD)HIWORD(wParam) & (flag)) == (flag))
  193. #endif
  194. #ifndef IS_POINTER_FIRSTBUTTON_WPARAM
  195. #define IS_POINTER_FIRSTBUTTON_WPARAM(wParam) IS_POINTER_FLAG_SET_WPARAM(wParam, POINTER_MESSAGE_FLAG_FIRSTBUTTON)
  196. #endif
  197. #ifndef IS_POINTER_SECONDBUTTON_WPARAM
  198. #define IS_POINTER_SECONDBUTTON_WPARAM(wParam) IS_POINTER_FLAG_SET_WPARAM(wParam, POINTER_MESSAGE_FLAG_SECONDBUTTON)
  199. #endif
  200. #ifndef IS_POINTER_THIRDBUTTON_WPARAM
  201. #define IS_POINTER_THIRDBUTTON_WPARAM(wParam) IS_POINTER_FLAG_SET_WPARAM(wParam, POINTER_MESSAGE_FLAG_THIRDBUTTON)
  202. #endif
  203. #ifndef IS_POINTER_FOURTHBUTTON_WPARAM
  204. #define IS_POINTER_FOURTHBUTTON_WPARAM(wParam) IS_POINTER_FLAG_SET_WPARAM(wParam, POINTER_MESSAGE_FLAG_FOURTHBUTTON)
  205. #endif
  206. #ifndef IS_POINTER_FIFTHBUTTON_WPARAM
  207. #define IS_POINTER_FIFTHBUTTON_WPARAM(wParam) IS_POINTER_FLAG_SET_WPARAM(wParam, POINTER_MESSAGE_FLAG_FIFTHBUTTON)
  208. #endif
  209. #ifndef GET_POINTERID_WPARAM
  210. #define GET_POINTERID_WPARAM(wParam) (LOWORD(wParam))
  211. #endif
  212. #if WINVER < 0x0602
  213. enum tagPOINTER_INPUT_TYPE {
  214. PT_POINTER = 0x00000001,
  215. PT_TOUCH = 0x00000002,
  216. PT_PEN = 0x00000003,
  217. PT_MOUSE = 0x00000004,
  218. PT_TOUCHPAD = 0x00000005
  219. };
  220. typedef enum tagPOINTER_BUTTON_CHANGE_TYPE {
  221. POINTER_CHANGE_NONE,
  222. POINTER_CHANGE_FIRSTBUTTON_DOWN,
  223. POINTER_CHANGE_FIRSTBUTTON_UP,
  224. POINTER_CHANGE_SECONDBUTTON_DOWN,
  225. POINTER_CHANGE_SECONDBUTTON_UP,
  226. POINTER_CHANGE_THIRDBUTTON_DOWN,
  227. POINTER_CHANGE_THIRDBUTTON_UP,
  228. POINTER_CHANGE_FOURTHBUTTON_DOWN,
  229. POINTER_CHANGE_FOURTHBUTTON_UP,
  230. POINTER_CHANGE_FIFTHBUTTON_DOWN,
  231. POINTER_CHANGE_FIFTHBUTTON_UP,
  232. } POINTER_BUTTON_CHANGE_TYPE;
  233. typedef struct tagPOINTER_INFO {
  234. POINTER_INPUT_TYPE pointerType;
  235. UINT32 pointerId;
  236. UINT32 frameId;
  237. POINTER_FLAGS pointerFlags;
  238. HANDLE sourceDevice;
  239. HWND hwndTarget;
  240. POINT ptPixelLocation;
  241. POINT ptHimetricLocation;
  242. POINT ptPixelLocationRaw;
  243. POINT ptHimetricLocationRaw;
  244. DWORD dwTime;
  245. UINT32 historyCount;
  246. INT32 InputData;
  247. DWORD dwKeyStates;
  248. UINT64 PerformanceCount;
  249. POINTER_BUTTON_CHANGE_TYPE ButtonChangeType;
  250. } POINTER_INFO;
  251. typedef struct tagPOINTER_PEN_INFO {
  252. POINTER_INFO pointerInfo;
  253. PEN_FLAGS penFlags;
  254. PEN_MASK penMask;
  255. UINT32 pressure;
  256. UINT32 rotation;
  257. INT32 tiltX;
  258. INT32 tiltY;
  259. } POINTER_PEN_INFO;
  260. #endif
  261. #endif //POINTER_STRUCTURES
  262. #ifndef WM_POINTERUPDATE
  263. #define WM_POINTERUPDATE 0x0245
  264. #endif
  265. #ifndef WM_POINTERENTER
  266. #define WM_POINTERENTER 0x0249
  267. #endif
  268. #ifndef WM_POINTERLEAVE
  269. #define WM_POINTERLEAVE 0x024A
  270. #endif
  271. #ifndef WM_POINTERDOWN
  272. #define WM_POINTERDOWN 0x0246
  273. #endif
  274. #ifndef WM_POINTERUP
  275. #define WM_POINTERUP 0x0247
  276. #endif
  277. typedef BOOL(WINAPI *GetPointerTypePtr)(uint32_t p_id, POINTER_INPUT_TYPE *p_type);
  278. typedef BOOL(WINAPI *GetPointerPenInfoPtr)(uint32_t p_id, POINTER_PEN_INFO *p_pen_info);
  279. typedef BOOL(WINAPI *LogicalToPhysicalPointForPerMonitorDPIPtr)(HWND hwnd, LPPOINT lpPoint);
  280. typedef BOOL(WINAPI *PhysicalToLogicalPointForPerMonitorDPIPtr)(HWND hwnd, LPPOINT lpPoint);
  281. typedef HRESULT(WINAPI *SHLoadIndirectStringPtr)(PCWSTR pszSource, PWSTR pszOutBuf, UINT cchOutBuf, void **ppvReserved);
  282. typedef struct {
  283. BYTE bWidth; // Width, in pixels, of the image
  284. BYTE bHeight; // Height, in pixels, of the image
  285. BYTE bColorCount; // Number of colors in image (0 if >=8bpp)
  286. BYTE bReserved; // Reserved ( must be 0)
  287. WORD wPlanes; // Color Planes
  288. WORD wBitCount; // Bits per pixel
  289. DWORD dwBytesInRes; // How many bytes in this resource?
  290. DWORD dwImageOffset; // Where in the file is this image?
  291. } ICONDIRENTRY, *LPICONDIRENTRY;
  292. typedef struct {
  293. WORD idReserved; // Reserved (must be 0)
  294. WORD idType; // Resource Type (1 for icons)
  295. WORD idCount; // How many images?
  296. ICONDIRENTRY idEntries[1]; // An entry for each image (idCount of 'em)
  297. } ICONDIR, *LPICONDIR;
  298. typedef enum _SHC_PROCESS_DPI_AWARENESS {
  299. SHC_PROCESS_DPI_UNAWARE = 0,
  300. SHC_PROCESS_SYSTEM_DPI_AWARE = 1,
  301. SHC_PROCESS_PER_MONITOR_DPI_AWARE = 2,
  302. } SHC_PROCESS_DPI_AWARENESS;
  303. #ifndef WS_EX_NOREDIRECTIONBITMAP
  304. #define WS_EX_NOREDIRECTIONBITMAP 0x00200000L
  305. #endif
  306. class DropTargetWindows;
  307. #ifndef WDA_EXCLUDEFROMCAPTURE
  308. #define WDA_EXCLUDEFROMCAPTURE 0x00000011
  309. #endif
  310. class DisplayServerWindows : public DisplayServer {
  311. GDSOFTCLASS(DisplayServerWindows, DisplayServer);
  312. friend class DropTargetWindows;
  313. _THREAD_SAFE_CLASS_
  314. // UXTheme API
  315. static bool dark_title_available;
  316. static bool use_legacy_dark_mode_before_20H1;
  317. static bool ux_theme_available;
  318. static ShouldAppsUseDarkModePtr ShouldAppsUseDarkMode;
  319. static GetImmersiveColorFromColorSetExPtr GetImmersiveColorFromColorSetEx;
  320. static GetImmersiveColorTypeFromNamePtr GetImmersiveColorTypeFromName;
  321. static GetImmersiveUserColorSetPreferencePtr GetImmersiveUserColorSetPreference;
  322. // WinTab API
  323. static bool wintab_available;
  324. static WTOpenPtr wintab_WTOpen;
  325. static WTClosePtr wintab_WTClose;
  326. static WTInfoPtr wintab_WTInfo;
  327. static WTPacketPtr wintab_WTPacket;
  328. static WTEnablePtr wintab_WTEnable;
  329. // Windows Ink API
  330. static bool winink_available;
  331. static GetPointerTypePtr win8p_GetPointerType;
  332. static GetPointerPenInfoPtr win8p_GetPointerPenInfo;
  333. // DPI conversion API
  334. static LogicalToPhysicalPointForPerMonitorDPIPtr win81p_LogicalToPhysicalPointForPerMonitorDPI;
  335. static PhysicalToLogicalPointForPerMonitorDPIPtr win81p_PhysicalToLogicalPointForPerMonitorDPI;
  336. // Shell API
  337. static SHLoadIndirectStringPtr load_indirect_string;
  338. void _update_tablet_ctx(const String &p_old_driver, const String &p_new_driver);
  339. String tablet_driver;
  340. Vector<String> tablet_drivers;
  341. bool winink_disabled = false;
  342. enum DriverID {
  343. DRIVER_ID_COMPAT_OPENGL3 = 1 << 0,
  344. DRIVER_ID_COMPAT_ANGLE_D3D11 = 1 << 1,
  345. DRIVER_ID_RD_VULKAN = 1 << 2,
  346. DRIVER_ID_RD_D3D12 = 1 << 3,
  347. };
  348. static BitField<DriverID> tested_drivers;
  349. enum TimerID {
  350. TIMER_ID_MOVE_REDRAW = 1,
  351. TIMER_ID_WINDOW_ACTIVATION = 2,
  352. };
  353. OSVERSIONINFOEXW os_ver;
  354. enum {
  355. KEY_EVENT_BUFFER_SIZE = 512
  356. };
  357. struct KeyEvent {
  358. WindowID window_id;
  359. bool alt, shift, control, meta, altgr;
  360. UINT uMsg;
  361. WPARAM wParam;
  362. LPARAM lParam;
  363. };
  364. WindowID window_mouseover_id = INVALID_WINDOW_ID;
  365. KeyEvent key_event_buffer[KEY_EVENT_BUFFER_SIZE];
  366. int key_event_pos;
  367. bool old_invalid;
  368. int old_x, old_y;
  369. Point2i center;
  370. #if defined(GLES3_ENABLED)
  371. GLManagerANGLE_Windows *gl_manager_angle = nullptr;
  372. GLManagerNative_Windows *gl_manager_native = nullptr;
  373. #endif
  374. #if defined(RD_ENABLED)
  375. RenderingContextDriver *rendering_context = nullptr;
  376. RenderingDevice *rendering_device = nullptr;
  377. #endif
  378. RBMap<int, Vector2> touch_state;
  379. int pressrc;
  380. HINSTANCE hInstance; // Holds The Instance Of The Application
  381. String rendering_driver;
  382. bool app_focused = false;
  383. bool keep_screen_on = false;
  384. bool get_object_recieved = false;
  385. HANDLE power_request;
  386. TTS_Windows *tts = nullptr;
  387. NativeMenuWindows *native_menu = nullptr;
  388. struct WindowData {
  389. HWND hWnd;
  390. WindowID id;
  391. Vector<Vector2> mpath;
  392. bool create_completed = false;
  393. bool pre_fs_valid = false;
  394. RECT pre_fs_rect;
  395. bool maximized = false;
  396. bool maximized_fs = false;
  397. bool minimized = false;
  398. bool fullscreen = false;
  399. bool multiwindow_fs = false;
  400. bool borderless = false;
  401. bool resizable = true;
  402. bool no_min_btn = false;
  403. bool no_max_btn = false;
  404. bool window_focused = false;
  405. int activate_state = 0;
  406. bool was_maximized_pre_fs = false;
  407. bool was_fullscreen_pre_min = false;
  408. bool first_activation_done = false;
  409. bool was_maximized = false;
  410. bool always_on_top = false;
  411. bool no_focus = false;
  412. bool exclusive = false;
  413. bool context_created = false;
  414. bool mpass = false;
  415. bool sharp_corners = false;
  416. bool hide_from_capture = false;
  417. // Used to transfer data between events using timer.
  418. WPARAM saved_wparam;
  419. LPARAM saved_lparam;
  420. // Timers.
  421. uint32_t move_timer_id = 0U;
  422. uint32_t activate_timer_id = 0U;
  423. HANDLE wtctx;
  424. LOGCONTEXTW wtlc;
  425. int min_pressure;
  426. int max_pressure;
  427. bool tilt_supported;
  428. bool pen_inverted = false;
  429. bool block_mm = false;
  430. int last_pressure_update;
  431. float last_pressure;
  432. Vector2 last_tilt;
  433. bool last_pen_inverted = false;
  434. Size2 min_size;
  435. Size2 max_size;
  436. int width = 0, height = 0;
  437. Size2 window_rect;
  438. Point2 last_pos;
  439. ObjectID instance_id;
  440. // IME
  441. HIMC im_himc;
  442. Vector2 im_position;
  443. bool ime_active = false;
  444. bool ime_in_progress = false;
  445. bool ime_suppress_next_keyup = false;
  446. bool layered_window = false;
  447. Callable rect_changed_callback;
  448. Callable event_callback;
  449. Callable input_event_callback;
  450. Callable input_text_callback;
  451. Callable drop_files_callback;
  452. // OLE API
  453. DropTargetWindows *drop_target = nullptr;
  454. WindowID transient_parent = INVALID_WINDOW_ID;
  455. HashSet<WindowID> transient_children;
  456. bool is_popup = false;
  457. Rect2i parent_safe_rect;
  458. bool initialized = false;
  459. HWND parent_hwnd = 0;
  460. };
  461. JoypadWindows *joypad = nullptr;
  462. HHOOK mouse_monitor = nullptr;
  463. List<WindowID> popup_list;
  464. uint64_t time_since_popup = 0;
  465. Ref<Image> icon;
  466. WindowID _create_window(WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Rect2i &p_rect, bool p_exclusive, WindowID p_transient_parent, HWND p_parent_hwnd);
  467. WindowID window_id_counter = MAIN_WINDOW_ID;
  468. RBMap<WindowID, WindowData> windows;
  469. WindowID last_focused_window = INVALID_WINDOW_ID;
  470. WindowID last_mouse_button_down_window = INVALID_WINDOW_ID;
  471. HCURSOR hCursor;
  472. WNDPROC user_proc = nullptr;
  473. struct IndicatorData {
  474. RID menu_rid;
  475. Callable callback;
  476. };
  477. IndicatorID indicator_id_counter = 0;
  478. HashMap<IndicatorID, IndicatorData> indicators;
  479. struct FileDialogData {
  480. HWND hwnd_owner = 0;
  481. Rect2i wrect;
  482. String appid;
  483. String title;
  484. String current_directory;
  485. String root;
  486. String filename;
  487. bool show_hidden = false;
  488. DisplayServer::FileDialogMode mode = FileDialogMode::FILE_DIALOG_MODE_OPEN_ANY;
  489. Vector<String> filters;
  490. TypedArray<Dictionary> options;
  491. WindowID window_id = DisplayServer::INVALID_WINDOW_ID;
  492. Callable callback;
  493. bool options_in_cb = false;
  494. Thread listener_thread;
  495. SafeFlag close_requested;
  496. SafeFlag finished;
  497. };
  498. Mutex file_dialog_mutex;
  499. List<FileDialogData *> file_dialogs;
  500. HashMap<HWND, FileDialogData *> file_dialog_wnd;
  501. struct FileDialogCallback {
  502. Callable callback;
  503. Variant status;
  504. Variant files;
  505. Variant index;
  506. Variant options;
  507. bool opt_in_cb = false;
  508. };
  509. List<FileDialogCallback> pending_cbs;
  510. void process_file_dialog_callbacks();
  511. static void _thread_fd_monitor(void *p_ud);
  512. HashMap<int64_t, MouseButton> pointer_prev_button;
  513. HashMap<int64_t, MouseButton> pointer_button;
  514. HashMap<int64_t, LONG> pointer_down_time;
  515. HashMap<int64_t, Vector2> pointer_last_pos;
  516. void _send_window_event(const WindowData &wd, WindowEvent p_event);
  517. void _get_window_style(bool p_main_window, bool p_initialized, bool p_fullscreen, bool p_multiwindow_fs, bool p_borderless, bool p_resizable, bool p_no_min_btn, bool p_no_max_btn, bool p_minimized, bool p_maximized, bool p_maximized_fs, bool p_no_activate_focus, bool p_embed_child, DWORD &r_style, DWORD &r_style_ex);
  518. MouseMode mouse_mode;
  519. MouseMode mouse_mode_base = MOUSE_MODE_VISIBLE;
  520. MouseMode mouse_mode_override = MOUSE_MODE_VISIBLE;
  521. bool mouse_mode_override_enabled = false;
  522. void _mouse_update_mode();
  523. int restore_mouse_trails = 0;
  524. bool use_raw_input = false;
  525. bool drop_events = false;
  526. bool in_dispatch_input_event = false;
  527. WNDCLASSEXW wc;
  528. HBRUSH window_bkg_brush = nullptr;
  529. uint32_t window_bkg_brush_color = 0;
  530. HCURSOR cursors[CURSOR_MAX] = { nullptr };
  531. CursorShape cursor_shape = CursorShape::CURSOR_ARROW;
  532. RBMap<CursorShape, Vector<Variant>> cursors_cache;
  533. Callable system_theme_changed;
  534. void _drag_event(WindowID p_window, float p_x, float p_y, int idx);
  535. void _touch_event(WindowID p_window, bool p_pressed, float p_x, float p_y, int idx);
  536. bool _is_always_on_top_recursive(WindowID p_window) const;
  537. void _update_window_style(WindowID p_window, bool p_repaint = true);
  538. void _update_window_mouse_passthrough(WindowID p_window);
  539. void _update_real_mouse_position(WindowID p_window);
  540. void _set_mouse_mode_impl(MouseMode p_mode);
  541. WindowID _get_focused_window_or_popup() const;
  542. void _register_raw_input_devices(WindowID p_target_window);
  543. bool _has_moving_window() const;
  544. void _process_activate_event(WindowID p_window_id);
  545. void _process_key_events();
  546. static void _dispatch_input_events(const Ref<InputEvent> &p_event);
  547. void _dispatch_input_event(const Ref<InputEvent> &p_event);
  548. LRESULT _handle_early_window_message(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  549. Point2i _get_screens_origin() const;
  550. enum class WinKeyModifierMask {
  551. ALT_GR = (1 << 1),
  552. SHIFT = (1 << 2),
  553. ALT = (1 << 3),
  554. META = (1 << 4),
  555. CTRL = (1 << 5),
  556. };
  557. BitField<WinKeyModifierMask> _get_mods() const;
  558. Error _file_dialog_with_options_show(const String &p_title, const String &p_current_directory, const String &p_root, const String &p_filename, bool p_show_hidden, FileDialogMode p_mode, const Vector<String> &p_filters, const TypedArray<Dictionary> &p_options, const Callable &p_callback, bool p_options_in_cb, WindowID p_window_id);
  559. String _get_keyboard_layout_display_name(const String &p_klid) const;
  560. String _get_klid(HKL p_hkl) const;
  561. struct EmbeddedProcessData {
  562. HWND window_handle = 0;
  563. HWND parent_window_handle = 0;
  564. bool is_visible = false;
  565. };
  566. HashMap<OS::ProcessID, EmbeddedProcessData *> embedded_processes;
  567. HWND _find_window_from_process_id(OS::ProcessID p_pid, HWND p_current_hwnd);
  568. void initialize_tts() const;
  569. public:
  570. LRESULT WndProcFileDialog(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  571. LRESULT WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  572. LRESULT MouseProc(int code, WPARAM wParam, LPARAM lParam);
  573. void popup_open(WindowID p_window);
  574. void popup_close(WindowID p_window);
  575. virtual bool has_feature(Feature p_feature) const override;
  576. virtual String get_name() const override;
  577. virtual bool tts_is_speaking() const override;
  578. virtual bool tts_is_paused() const override;
  579. virtual TypedArray<Dictionary> tts_get_voices() const override;
  580. virtual void tts_speak(const String &p_text, const String &p_voice, int p_volume = 50, float p_pitch = 1.f, float p_rate = 1.f, int p_utterance_id = 0, bool p_interrupt = false) override;
  581. virtual void tts_pause() override;
  582. virtual void tts_resume() override;
  583. virtual void tts_stop() override;
  584. virtual bool is_dark_mode_supported() const override;
  585. virtual bool is_dark_mode() const override;
  586. virtual Color get_accent_color() const override;
  587. virtual Color get_base_color() const override;
  588. virtual void set_system_theme_change_callback(const Callable &p_callable) override;
  589. virtual Error file_dialog_show(const String &p_title, const String &p_current_directory, const String &p_filename, bool p_show_hidden, FileDialogMode p_mode, const Vector<String> &p_filters, const Callable &p_callback, WindowID p_window_id) override;
  590. virtual Error file_dialog_with_options_show(const String &p_title, const String &p_current_directory, const String &p_root, const String &p_filename, bool p_show_hidden, FileDialogMode p_mode, const Vector<String> &p_filters, const TypedArray<Dictionary> &p_options, const Callable &p_callback, WindowID p_window_id) override;
  591. virtual void beep() const override;
  592. virtual void mouse_set_mode(MouseMode p_mode) override;
  593. virtual MouseMode mouse_get_mode() const override;
  594. virtual void mouse_set_mode_override(MouseMode p_mode) override;
  595. virtual MouseMode mouse_get_mode_override() const override;
  596. virtual void mouse_set_mode_override_enabled(bool p_override_enabled) override;
  597. virtual bool mouse_is_mode_override_enabled() const override;
  598. virtual void warp_mouse(const Point2i &p_position) override;
  599. virtual Point2i mouse_get_position() const override;
  600. virtual BitField<MouseButtonMask> mouse_get_button_state() const override;
  601. virtual void clipboard_set(const String &p_text) override;
  602. virtual String clipboard_get() const override;
  603. virtual Ref<Image> clipboard_get_image() const override;
  604. virtual bool clipboard_has() const override;
  605. virtual bool clipboard_has_image() const override;
  606. virtual int get_screen_count() const override;
  607. virtual int get_primary_screen() const override;
  608. virtual int get_keyboard_focus_screen() const override;
  609. virtual Point2i screen_get_position(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  610. virtual Size2i screen_get_size(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  611. virtual Rect2i screen_get_usable_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  612. virtual int screen_get_dpi(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  613. virtual float screen_get_refresh_rate(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  614. virtual Color screen_get_pixel(const Point2i &p_position) const override;
  615. virtual Ref<Image> screen_get_image(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  616. virtual Ref<Image> screen_get_image_rect(const Rect2i &p_rect) const override;
  617. virtual void screen_set_keep_on(bool p_enable) override; //disable screensaver
  618. virtual bool screen_is_kept_on() const override;
  619. virtual Vector<DisplayServer::WindowID> get_window_list() const override;
  620. virtual WindowID create_sub_window(WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Rect2i &p_rect = Rect2i(), bool p_exclusive = false, WindowID p_transient_parent = INVALID_WINDOW_ID) override;
  621. virtual void show_window(WindowID p_window) override;
  622. virtual void delete_sub_window(WindowID p_window) override;
  623. virtual WindowID window_get_active_popup() const override;
  624. virtual void window_set_popup_safe_rect(WindowID p_window, const Rect2i &p_rect) override;
  625. virtual Rect2i window_get_popup_safe_rect(WindowID p_window) const override;
  626. virtual int64_t window_get_native_handle(HandleType p_handle_type, WindowID p_window = MAIN_WINDOW_ID) const override;
  627. virtual WindowID get_window_at_screen_position(const Point2i &p_position) const override;
  628. virtual void window_attach_instance_id(ObjectID p_instance, WindowID p_window = MAIN_WINDOW_ID) override;
  629. virtual ObjectID window_get_attached_instance_id(WindowID p_window = MAIN_WINDOW_ID) const override;
  630. virtual void gl_window_make_current(DisplayServer::WindowID p_window_id) override;
  631. virtual void window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  632. virtual void window_set_window_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  633. virtual void window_set_input_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  634. virtual void window_set_input_text_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  635. virtual void window_set_drop_files_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  636. virtual void window_set_title(const String &p_title, WindowID p_window = MAIN_WINDOW_ID) override;
  637. virtual Size2i window_get_title_size(const String &p_title, WindowID p_window = MAIN_WINDOW_ID) const override;
  638. virtual void window_set_mouse_passthrough(const Vector<Vector2> &p_region, WindowID p_window = MAIN_WINDOW_ID) override;
  639. virtual int window_get_current_screen(WindowID p_window = MAIN_WINDOW_ID) const override;
  640. virtual void window_set_current_screen(int p_screen, WindowID p_window = MAIN_WINDOW_ID) override;
  641. virtual Point2i window_get_position(WindowID p_window = MAIN_WINDOW_ID) const override;
  642. virtual Point2i window_get_position_with_decorations(WindowID p_window = MAIN_WINDOW_ID) const override;
  643. virtual void window_set_position(const Point2i &p_position, WindowID p_window = MAIN_WINDOW_ID) override;
  644. virtual void window_set_transient(WindowID p_window, WindowID p_parent) override;
  645. virtual void window_set_exclusive(WindowID p_window, bool p_exclusive) override;
  646. virtual void window_set_max_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
  647. virtual Size2i window_get_max_size(WindowID p_window = MAIN_WINDOW_ID) const override;
  648. virtual void window_set_min_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
  649. virtual Size2i window_get_min_size(WindowID p_window = MAIN_WINDOW_ID) const override;
  650. virtual void window_set_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
  651. virtual Size2i window_get_size(WindowID p_window = MAIN_WINDOW_ID) const override;
  652. virtual Size2i window_get_size_with_decorations(WindowID p_window = MAIN_WINDOW_ID) const override;
  653. virtual void window_set_mode(WindowMode p_mode, WindowID p_window = MAIN_WINDOW_ID) override;
  654. virtual WindowMode window_get_mode(WindowID p_window = MAIN_WINDOW_ID) const override;
  655. virtual bool window_is_maximize_allowed(WindowID p_window = MAIN_WINDOW_ID) const override;
  656. virtual void window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window = MAIN_WINDOW_ID) override;
  657. virtual bool window_get_flag(WindowFlags p_flag, WindowID p_window = MAIN_WINDOW_ID) const override;
  658. virtual void window_request_attention(WindowID p_window = MAIN_WINDOW_ID) override;
  659. virtual void window_move_to_foreground(WindowID p_window = MAIN_WINDOW_ID) override;
  660. virtual bool window_is_focused(WindowID p_window = MAIN_WINDOW_ID) const override;
  661. virtual WindowID get_focused_window() const override;
  662. virtual bool window_can_draw(WindowID p_window = MAIN_WINDOW_ID) const override;
  663. virtual bool can_any_window_draw() const override;
  664. virtual void window_set_ime_active(const bool p_active, WindowID p_window = MAIN_WINDOW_ID) override;
  665. virtual void window_set_ime_position(const Point2i &p_pos, WindowID p_window = MAIN_WINDOW_ID) override;
  666. virtual int accessibility_should_increase_contrast() const override;
  667. virtual int accessibility_should_reduce_animation() const override;
  668. virtual int accessibility_should_reduce_transparency() const override;
  669. virtual int accessibility_screen_reader_active() const override;
  670. virtual Point2i ime_get_selection() const override;
  671. virtual String ime_get_text() const override;
  672. virtual void window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window = MAIN_WINDOW_ID) override;
  673. virtual DisplayServer::VSyncMode window_get_vsync_mode(WindowID p_vsync_mode) const override;
  674. virtual void window_start_drag(WindowID p_window = MAIN_WINDOW_ID) override;
  675. virtual void window_start_resize(WindowResizeEdge p_edge, WindowID p_window = MAIN_WINDOW_ID) override;
  676. virtual void cursor_set_shape(CursorShape p_shape) override;
  677. virtual CursorShape cursor_get_shape() const override;
  678. virtual void cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape = CURSOR_ARROW, const Vector2 &p_hotspot = Vector2()) override;
  679. virtual bool get_swap_cancel_ok() override;
  680. virtual void enable_for_stealing_focus(OS::ProcessID pid) override;
  681. virtual Error embed_process(WindowID p_window, OS::ProcessID p_pid, const Rect2i &p_rect, bool p_visible, bool p_grab_focus) override;
  682. virtual Error request_close_embedded_process(OS::ProcessID p_pid) override;
  683. virtual Error remove_embedded_process(OS::ProcessID p_pid) override;
  684. virtual OS::ProcessID get_focused_process_id() override;
  685. virtual Error dialog_show(String p_title, String p_description, Vector<String> p_buttons, const Callable &p_callback) override;
  686. virtual Error dialog_input_text(String p_title, String p_description, String p_partial, const Callable &p_callback) override;
  687. virtual int keyboard_get_layout_count() const override;
  688. virtual int keyboard_get_current_layout() const override;
  689. virtual void keyboard_set_current_layout(int p_index) override;
  690. virtual String keyboard_get_layout_language(int p_index) const override;
  691. virtual String keyboard_get_layout_name(int p_index) const override;
  692. virtual Key keyboard_get_keycode_from_physical(Key p_keycode) const override;
  693. virtual Key keyboard_get_label_from_physical(Key p_keycode) const override;
  694. virtual void show_emoji_and_symbol_picker() const override;
  695. virtual int tablet_get_driver_count() const override;
  696. virtual String tablet_get_driver_name(int p_driver) const override;
  697. virtual String tablet_get_current_driver() const override;
  698. virtual void tablet_set_current_driver(const String &p_driver) override;
  699. virtual void process_events() override;
  700. virtual void force_process_and_drop_events() override;
  701. virtual void release_rendering_thread() override;
  702. virtual void swap_buffers() override;
  703. virtual void set_native_icon(const String &p_filename) override;
  704. virtual void set_icon(const Ref<Image> &p_icon) override;
  705. virtual IndicatorID create_status_indicator(const Ref<Texture2D> &p_icon, const String &p_tooltip, const Callable &p_callback) override;
  706. virtual void status_indicator_set_icon(IndicatorID p_id, const Ref<Texture2D> &p_icon) override;
  707. virtual void status_indicator_set_tooltip(IndicatorID p_id, const String &p_tooltip) override;
  708. virtual void status_indicator_set_menu(IndicatorID p_id, const RID &p_rid) override;
  709. virtual void status_indicator_set_callback(IndicatorID p_id, const Callable &p_callback) override;
  710. virtual Rect2 status_indicator_get_rect(IndicatorID p_id) const override;
  711. virtual void delete_status_indicator(IndicatorID p_id) override;
  712. virtual void set_context(Context p_context) override;
  713. virtual bool is_window_transparency_available() const override;
  714. static DisplayServer *create_func(const String &p_rendering_driver, WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, Context p_context, int64_t p_parent_window, Error &r_error);
  715. static Vector<String> get_rendering_drivers_func();
  716. static void register_windows_driver();
  717. DisplayServerWindows(const String &p_rendering_driver, WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, Context p_context, int64_t p_parent_window, Error &r_error);
  718. ~DisplayServerWindows();
  719. };