display_server_windows.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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. #ifndef DISPLAY_SERVER_WINDOWS_H
  31. #define DISPLAY_SERVER_WINDOWS_H
  32. #include "crash_handler_windows.h"
  33. #include "joypad_windows.h"
  34. #include "key_mapping_windows.h"
  35. #include "tts_windows.h"
  36. #include "core/config/project_settings.h"
  37. #include "core/input/input.h"
  38. #include "core/io/image.h"
  39. #include "core/os/os.h"
  40. #include "drivers/wasapi/audio_driver_wasapi.h"
  41. #include "drivers/winmidi/midi_driver_winmidi.h"
  42. #include "servers/audio_server.h"
  43. #include "servers/display_server.h"
  44. #include "servers/rendering/renderer_compositor.h"
  45. #include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
  46. #include "servers/rendering_server.h"
  47. #ifdef XAUDIO2_ENABLED
  48. #include "drivers/xaudio2/audio_driver_xaudio2.h"
  49. #endif
  50. #if defined(RD_ENABLED)
  51. #include "servers/rendering/rendering_device.h"
  52. #endif
  53. #if defined(GLES3_ENABLED)
  54. #include "gl_manager_windows_angle.h"
  55. #include "gl_manager_windows_native.h"
  56. #endif // GLES3_ENABLED
  57. #include "native_menu_windows.h"
  58. #include <io.h>
  59. #include <stdio.h>
  60. #define WIN32_LEAN_AND_MEAN
  61. #include <windows.h>
  62. #include <windowsx.h>
  63. // WinTab API
  64. #define WT_PACKET 0x7FF0
  65. #define WT_PROXIMITY 0x7FF5
  66. #define WT_INFOCHANGE 0x7FF6
  67. #define WT_CSRCHANGE 0x7FF7
  68. #define WTI_DEFSYSCTX 4
  69. #define WTI_DEVICES 100
  70. #define DVC_NPRESSURE 15
  71. #define DVC_TPRESSURE 16
  72. #define DVC_ORIENTATION 17
  73. #define DVC_ROTATION 18
  74. #define CXO_MESSAGES 0x0004
  75. #define PK_STATUS 0x0002
  76. #define PK_NORMAL_PRESSURE 0x0400
  77. #define PK_TANGENT_PRESSURE 0x0800
  78. #define PK_ORIENTATION 0x1000
  79. #define TPS_INVERT 0x0010 /* 1.1 */
  80. typedef struct tagLOGCONTEXTW {
  81. WCHAR lcName[40];
  82. UINT lcOptions;
  83. UINT lcStatus;
  84. UINT lcLocks;
  85. UINT lcMsgBase;
  86. UINT lcDevice;
  87. UINT lcPktRate;
  88. DWORD lcPktData;
  89. DWORD lcPktMode;
  90. DWORD lcMoveMask;
  91. DWORD lcBtnDnMask;
  92. DWORD lcBtnUpMask;
  93. LONG lcInOrgX;
  94. LONG lcInOrgY;
  95. LONG lcInOrgZ;
  96. LONG lcInExtX;
  97. LONG lcInExtY;
  98. LONG lcInExtZ;
  99. LONG lcOutOrgX;
  100. LONG lcOutOrgY;
  101. LONG lcOutOrgZ;
  102. LONG lcOutExtX;
  103. LONG lcOutExtY;
  104. LONG lcOutExtZ;
  105. DWORD lcSensX;
  106. DWORD lcSensY;
  107. DWORD lcSensZ;
  108. BOOL lcSysMode;
  109. int lcSysOrgX;
  110. int lcSysOrgY;
  111. int lcSysExtX;
  112. int lcSysExtY;
  113. DWORD lcSysSensX;
  114. DWORD lcSysSensY;
  115. } LOGCONTEXTW;
  116. typedef struct tagAXIS {
  117. LONG axMin;
  118. LONG axMax;
  119. UINT axUnits;
  120. DWORD axResolution;
  121. } AXIS;
  122. typedef struct tagORIENTATION {
  123. int orAzimuth;
  124. int orAltitude;
  125. int orTwist;
  126. } ORIENTATION;
  127. typedef struct tagPACKET {
  128. int pkStatus;
  129. int pkNormalPressure;
  130. int pkTangentPressure;
  131. ORIENTATION pkOrientation;
  132. } PACKET;
  133. typedef HANDLE(WINAPI *WTOpenPtr)(HWND p_window, LOGCONTEXTW *p_ctx, BOOL p_enable);
  134. typedef BOOL(WINAPI *WTClosePtr)(HANDLE p_ctx);
  135. typedef UINT(WINAPI *WTInfoPtr)(UINT p_category, UINT p_index, LPVOID p_output);
  136. typedef BOOL(WINAPI *WTPacketPtr)(HANDLE p_ctx, UINT p_param, LPVOID p_packets);
  137. typedef BOOL(WINAPI *WTEnablePtr)(HANDLE p_ctx, BOOL p_enable);
  138. enum PreferredAppMode {
  139. APPMODE_DEFAULT = 0,
  140. APPMODE_ALLOWDARK = 1,
  141. APPMODE_FORCEDARK = 2,
  142. APPMODE_FORCELIGHT = 3,
  143. APPMODE_MAX = 4
  144. };
  145. typedef const char *(CDECL *WineGetVersionPtr)(void);
  146. typedef bool(WINAPI *ShouldAppsUseDarkModePtr)();
  147. typedef DWORD(WINAPI *GetImmersiveColorFromColorSetExPtr)(UINT dwImmersiveColorSet, UINT dwImmersiveColorType, bool bIgnoreHighContrast, UINT dwHighContrastCacheMode);
  148. typedef int(WINAPI *GetImmersiveColorTypeFromNamePtr)(const WCHAR *name);
  149. typedef int(WINAPI *GetImmersiveUserColorSetPreferencePtr)(bool bForceCheckRegistry, bool bSkipCheckOnFail);
  150. typedef HRESULT(WINAPI *RtlGetVersionPtr)(OSVERSIONINFOW *lpVersionInformation);
  151. typedef bool(WINAPI *AllowDarkModeForAppPtr)(bool darkMode);
  152. typedef PreferredAppMode(WINAPI *SetPreferredAppModePtr)(PreferredAppMode appMode);
  153. typedef void(WINAPI *RefreshImmersiveColorPolicyStatePtr)();
  154. typedef void(WINAPI *FlushMenuThemesPtr)();
  155. // Windows Ink API
  156. #ifndef POINTER_STRUCTURES
  157. #define POINTER_STRUCTURES
  158. typedef DWORD POINTER_INPUT_TYPE;
  159. typedef UINT32 POINTER_FLAGS;
  160. typedef UINT32 PEN_FLAGS;
  161. typedef UINT32 PEN_MASK;
  162. #ifndef PEN_FLAG_INVERTED
  163. #define PEN_FLAG_INVERTED 0x00000002
  164. #endif
  165. #ifndef PEN_FLAG_ERASER
  166. #define PEN_FLAG_ERASER 0x00000004
  167. #endif
  168. #ifndef PEN_MASK_PRESSURE
  169. #define PEN_MASK_PRESSURE 0x00000001
  170. #endif
  171. #ifndef PEN_MASK_TILT_X
  172. #define PEN_MASK_TILT_X 0x00000004
  173. #endif
  174. #ifndef PEN_MASK_TILT_Y
  175. #define PEN_MASK_TILT_Y 0x00000008
  176. #endif
  177. #ifndef POINTER_MESSAGE_FLAG_FIRSTBUTTON
  178. #define POINTER_MESSAGE_FLAG_FIRSTBUTTON 0x00000010
  179. #endif
  180. #ifndef POINTER_MESSAGE_FLAG_SECONDBUTTON
  181. #define POINTER_MESSAGE_FLAG_SECONDBUTTON 0x00000020
  182. #endif
  183. #ifndef POINTER_MESSAGE_FLAG_THIRDBUTTON
  184. #define POINTER_MESSAGE_FLAG_THIRDBUTTON 0x00000040
  185. #endif
  186. #ifndef POINTER_MESSAGE_FLAG_FOURTHBUTTON
  187. #define POINTER_MESSAGE_FLAG_FOURTHBUTTON 0x00000080
  188. #endif
  189. #ifndef POINTER_MESSAGE_FLAG_FIFTHBUTTON
  190. #define POINTER_MESSAGE_FLAG_FIFTHBUTTON 0x00000100
  191. #endif
  192. #ifndef IS_POINTER_FLAG_SET_WPARAM
  193. #define IS_POINTER_FLAG_SET_WPARAM(wParam, flag) (((DWORD)HIWORD(wParam) & (flag)) == (flag))
  194. #endif
  195. #ifndef IS_POINTER_FIRSTBUTTON_WPARAM
  196. #define IS_POINTER_FIRSTBUTTON_WPARAM(wParam) IS_POINTER_FLAG_SET_WPARAM(wParam, POINTER_MESSAGE_FLAG_FIRSTBUTTON)
  197. #endif
  198. #ifndef IS_POINTER_SECONDBUTTON_WPARAM
  199. #define IS_POINTER_SECONDBUTTON_WPARAM(wParam) IS_POINTER_FLAG_SET_WPARAM(wParam, POINTER_MESSAGE_FLAG_SECONDBUTTON)
  200. #endif
  201. #ifndef IS_POINTER_THIRDBUTTON_WPARAM
  202. #define IS_POINTER_THIRDBUTTON_WPARAM(wParam) IS_POINTER_FLAG_SET_WPARAM(wParam, POINTER_MESSAGE_FLAG_THIRDBUTTON)
  203. #endif
  204. #ifndef IS_POINTER_FOURTHBUTTON_WPARAM
  205. #define IS_POINTER_FOURTHBUTTON_WPARAM(wParam) IS_POINTER_FLAG_SET_WPARAM(wParam, POINTER_MESSAGE_FLAG_FOURTHBUTTON)
  206. #endif
  207. #ifndef IS_POINTER_FIFTHBUTTON_WPARAM
  208. #define IS_POINTER_FIFTHBUTTON_WPARAM(wParam) IS_POINTER_FLAG_SET_WPARAM(wParam, POINTER_MESSAGE_FLAG_FIFTHBUTTON)
  209. #endif
  210. #ifndef GET_POINTERID_WPARAM
  211. #define GET_POINTERID_WPARAM(wParam) (LOWORD(wParam))
  212. #endif
  213. #if WINVER < 0x0602
  214. enum tagPOINTER_INPUT_TYPE {
  215. PT_POINTER = 0x00000001,
  216. PT_TOUCH = 0x00000002,
  217. PT_PEN = 0x00000003,
  218. PT_MOUSE = 0x00000004,
  219. PT_TOUCHPAD = 0x00000005
  220. };
  221. typedef enum tagPOINTER_BUTTON_CHANGE_TYPE {
  222. POINTER_CHANGE_NONE,
  223. POINTER_CHANGE_FIRSTBUTTON_DOWN,
  224. POINTER_CHANGE_FIRSTBUTTON_UP,
  225. POINTER_CHANGE_SECONDBUTTON_DOWN,
  226. POINTER_CHANGE_SECONDBUTTON_UP,
  227. POINTER_CHANGE_THIRDBUTTON_DOWN,
  228. POINTER_CHANGE_THIRDBUTTON_UP,
  229. POINTER_CHANGE_FOURTHBUTTON_DOWN,
  230. POINTER_CHANGE_FOURTHBUTTON_UP,
  231. POINTER_CHANGE_FIFTHBUTTON_DOWN,
  232. POINTER_CHANGE_FIFTHBUTTON_UP,
  233. } POINTER_BUTTON_CHANGE_TYPE;
  234. typedef struct tagPOINTER_INFO {
  235. POINTER_INPUT_TYPE pointerType;
  236. UINT32 pointerId;
  237. UINT32 frameId;
  238. POINTER_FLAGS pointerFlags;
  239. HANDLE sourceDevice;
  240. HWND hwndTarget;
  241. POINT ptPixelLocation;
  242. POINT ptHimetricLocation;
  243. POINT ptPixelLocationRaw;
  244. POINT ptHimetricLocationRaw;
  245. DWORD dwTime;
  246. UINT32 historyCount;
  247. INT32 InputData;
  248. DWORD dwKeyStates;
  249. UINT64 PerformanceCount;
  250. POINTER_BUTTON_CHANGE_TYPE ButtonChangeType;
  251. } POINTER_INFO;
  252. typedef struct tagPOINTER_PEN_INFO {
  253. POINTER_INFO pointerInfo;
  254. PEN_FLAGS penFlags;
  255. PEN_MASK penMask;
  256. UINT32 pressure;
  257. UINT32 rotation;
  258. INT32 tiltX;
  259. INT32 tiltY;
  260. } POINTER_PEN_INFO;
  261. #endif
  262. #endif //POINTER_STRUCTURES
  263. #ifndef WM_POINTERUPDATE
  264. #define WM_POINTERUPDATE 0x0245
  265. #endif
  266. #ifndef WM_POINTERENTER
  267. #define WM_POINTERENTER 0x0249
  268. #endif
  269. #ifndef WM_POINTERLEAVE
  270. #define WM_POINTERLEAVE 0x024A
  271. #endif
  272. #ifndef WM_POINTERDOWN
  273. #define WM_POINTERDOWN 0x0246
  274. #endif
  275. #ifndef WM_POINTERUP
  276. #define WM_POINTERUP 0x0247
  277. #endif
  278. typedef BOOL(WINAPI *GetPointerTypePtr)(uint32_t p_id, POINTER_INPUT_TYPE *p_type);
  279. typedef BOOL(WINAPI *GetPointerPenInfoPtr)(uint32_t p_id, POINTER_PEN_INFO *p_pen_info);
  280. typedef BOOL(WINAPI *LogicalToPhysicalPointForPerMonitorDPIPtr)(HWND hwnd, LPPOINT lpPoint);
  281. typedef BOOL(WINAPI *PhysicalToLogicalPointForPerMonitorDPIPtr)(HWND hwnd, LPPOINT lpPoint);
  282. typedef HRESULT(WINAPI *SHLoadIndirectStringPtr)(PCWSTR pszSource, PWSTR pszOutBuf, UINT cchOutBuf, void **ppvReserved);
  283. typedef struct {
  284. BYTE bWidth; // Width, in pixels, of the image
  285. BYTE bHeight; // Height, in pixels, of the image
  286. BYTE bColorCount; // Number of colors in image (0 if >=8bpp)
  287. BYTE bReserved; // Reserved ( must be 0)
  288. WORD wPlanes; // Color Planes
  289. WORD wBitCount; // Bits per pixel
  290. DWORD dwBytesInRes; // How many bytes in this resource?
  291. DWORD dwImageOffset; // Where in the file is this image?
  292. } ICONDIRENTRY, *LPICONDIRENTRY;
  293. typedef struct {
  294. WORD idReserved; // Reserved (must be 0)
  295. WORD idType; // Resource Type (1 for icons)
  296. WORD idCount; // How many images?
  297. ICONDIRENTRY idEntries[1]; // An entry for each image (idCount of 'em)
  298. } ICONDIR, *LPICONDIR;
  299. typedef enum _SHC_PROCESS_DPI_AWARENESS {
  300. SHC_PROCESS_DPI_UNAWARE = 0,
  301. SHC_PROCESS_SYSTEM_DPI_AWARE = 1,
  302. SHC_PROCESS_PER_MONITOR_DPI_AWARE = 2,
  303. } SHC_PROCESS_DPI_AWARENESS;
  304. #ifndef WS_EX_NOREDIRECTIONBITMAP
  305. #define WS_EX_NOREDIRECTIONBITMAP 0x00200000L
  306. #endif
  307. class DropTargetWindows;
  308. #ifndef WDA_EXCLUDEFROMCAPTURE
  309. #define WDA_EXCLUDEFROMCAPTURE 0x00000011
  310. #endif
  311. class DisplayServerWindows : public DisplayServer {
  312. // No need to register with GDCLASS, it's platform-specific and nothing is added.
  313. friend class DropTargetWindows;
  314. _THREAD_SAFE_CLASS_
  315. // UXTheme API
  316. static bool dark_title_available;
  317. static bool use_legacy_dark_mode_before_20H1;
  318. static bool ux_theme_available;
  319. static ShouldAppsUseDarkModePtr ShouldAppsUseDarkMode;
  320. static GetImmersiveColorFromColorSetExPtr GetImmersiveColorFromColorSetEx;
  321. static GetImmersiveColorTypeFromNamePtr GetImmersiveColorTypeFromName;
  322. static GetImmersiveUserColorSetPreferencePtr GetImmersiveUserColorSetPreference;
  323. // WinTab API
  324. static bool wintab_available;
  325. static WTOpenPtr wintab_WTOpen;
  326. static WTClosePtr wintab_WTClose;
  327. static WTInfoPtr wintab_WTInfo;
  328. static WTPacketPtr wintab_WTPacket;
  329. static WTEnablePtr wintab_WTEnable;
  330. // Windows Ink API
  331. static bool winink_available;
  332. static GetPointerTypePtr win8p_GetPointerType;
  333. static GetPointerPenInfoPtr win8p_GetPointerPenInfo;
  334. // DPI conversion API
  335. static LogicalToPhysicalPointForPerMonitorDPIPtr win81p_LogicalToPhysicalPointForPerMonitorDPI;
  336. static PhysicalToLogicalPointForPerMonitorDPIPtr win81p_PhysicalToLogicalPointForPerMonitorDPI;
  337. // Shell API
  338. static SHLoadIndirectStringPtr load_indirect_string;
  339. void _update_tablet_ctx(const String &p_old_driver, const String &p_new_driver);
  340. String tablet_driver;
  341. Vector<String> tablet_drivers;
  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. OSVERSIONINFOW 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. HANDLE power_request;
  385. TTS_Windows *tts = nullptr;
  386. NativeMenuWindows *native_menu = nullptr;
  387. struct WindowData {
  388. HWND hWnd;
  389. Vector<Vector2> mpath;
  390. bool create_completed = false;
  391. bool pre_fs_valid = false;
  392. RECT pre_fs_rect;
  393. bool maximized = false;
  394. bool maximized_fs = false;
  395. bool minimized = false;
  396. bool fullscreen = false;
  397. bool multiwindow_fs = false;
  398. bool borderless = false;
  399. bool resizable = true;
  400. bool window_focused = false;
  401. int activate_state = 0;
  402. bool was_maximized_pre_fs = false;
  403. bool was_fullscreen_pre_min = false;
  404. bool always_on_top = false;
  405. bool no_focus = false;
  406. bool exclusive = false;
  407. bool context_created = false;
  408. bool mpass = false;
  409. bool sharp_corners = false;
  410. bool hide_from_capture = false;
  411. // Used to transfer data between events using timer.
  412. WPARAM saved_wparam;
  413. LPARAM saved_lparam;
  414. // Timers.
  415. uint32_t move_timer_id = 0U;
  416. uint32_t activate_timer_id = 0U;
  417. HANDLE wtctx;
  418. LOGCONTEXTW wtlc;
  419. int min_pressure;
  420. int max_pressure;
  421. bool tilt_supported;
  422. bool pen_inverted = false;
  423. bool block_mm = false;
  424. int last_pressure_update;
  425. float last_pressure;
  426. Vector2 last_tilt;
  427. bool last_pen_inverted = false;
  428. Size2 min_size;
  429. Size2 max_size;
  430. int width = 0, height = 0;
  431. Size2 window_rect;
  432. Point2 last_pos;
  433. ObjectID instance_id;
  434. // IME
  435. HIMC im_himc;
  436. Vector2 im_position;
  437. bool ime_active = false;
  438. bool ime_in_progress = false;
  439. bool ime_suppress_next_keyup = false;
  440. bool layered_window = false;
  441. Callable rect_changed_callback;
  442. Callable event_callback;
  443. Callable input_event_callback;
  444. Callable input_text_callback;
  445. Callable drop_files_callback;
  446. // OLE API
  447. DropTargetWindows *drop_target = nullptr;
  448. WindowID transient_parent = INVALID_WINDOW_ID;
  449. HashSet<WindowID> transient_children;
  450. bool is_popup = false;
  451. Rect2i parent_safe_rect;
  452. bool initialized = false;
  453. };
  454. JoypadWindows *joypad = nullptr;
  455. HHOOK mouse_monitor = nullptr;
  456. List<WindowID> popup_list;
  457. uint64_t time_since_popup = 0;
  458. Ref<Image> icon;
  459. 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);
  460. WindowID window_id_counter = MAIN_WINDOW_ID;
  461. RBMap<WindowID, WindowData> windows;
  462. WindowID last_focused_window = INVALID_WINDOW_ID;
  463. WindowID last_mouse_button_down_window = INVALID_WINDOW_ID;
  464. HCURSOR hCursor;
  465. WNDPROC user_proc = nullptr;
  466. struct IndicatorData {
  467. RID menu_rid;
  468. Callable callback;
  469. };
  470. IndicatorID indicator_id_counter = 0;
  471. HashMap<IndicatorID, IndicatorData> indicators;
  472. struct FileDialogData {
  473. HWND hwnd_owner = 0;
  474. Rect2i wrect;
  475. String appid;
  476. String title;
  477. String current_directory;
  478. String root;
  479. String filename;
  480. bool show_hidden = false;
  481. DisplayServer::FileDialogMode mode = FileDialogMode::FILE_DIALOG_MODE_OPEN_ANY;
  482. Vector<String> filters;
  483. TypedArray<Dictionary> options;
  484. WindowID window_id = DisplayServer::INVALID_WINDOW_ID;
  485. Callable callback;
  486. bool options_in_cb = false;
  487. Thread listener_thread;
  488. SafeFlag close_requested;
  489. SafeFlag finished;
  490. };
  491. Mutex file_dialog_mutex;
  492. List<FileDialogData *> file_dialogs;
  493. HashMap<HWND, FileDialogData *> file_dialog_wnd;
  494. struct FileDialogCallback {
  495. Callable callback;
  496. Variant status;
  497. Variant files;
  498. Variant index;
  499. Variant options;
  500. bool opt_in_cb = false;
  501. };
  502. List<FileDialogCallback> pending_cbs;
  503. void process_file_dialog_callbacks();
  504. static void _thread_fd_monitor(void *p_ud);
  505. HashMap<int64_t, MouseButton> pointer_prev_button;
  506. HashMap<int64_t, MouseButton> pointer_button;
  507. HashMap<int64_t, LONG> pointer_down_time;
  508. HashMap<int64_t, Vector2> pointer_last_pos;
  509. void _send_window_event(const WindowData &wd, WindowEvent p_event);
  510. 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_minimized, bool p_maximized, bool p_maximized_fs, bool p_no_activate_focus, DWORD &r_style, DWORD &r_style_ex);
  511. MouseMode mouse_mode;
  512. int restore_mouse_trails = 0;
  513. bool use_raw_input = false;
  514. bool drop_events = false;
  515. bool in_dispatch_input_event = false;
  516. WNDCLASSEXW wc;
  517. HBRUSH window_bkg_brush = nullptr;
  518. uint32_t window_bkg_brush_color = 0;
  519. HCURSOR cursors[CURSOR_MAX] = { nullptr };
  520. CursorShape cursor_shape = CursorShape::CURSOR_ARROW;
  521. RBMap<CursorShape, Vector<Variant>> cursors_cache;
  522. Callable system_theme_changed;
  523. void _drag_event(WindowID p_window, float p_x, float p_y, int idx);
  524. void _touch_event(WindowID p_window, bool p_pressed, float p_x, float p_y, int idx);
  525. bool _is_always_on_top_recursive(WindowID p_window) const;
  526. void _update_window_style(WindowID p_window, bool p_repaint = true);
  527. void _update_window_mouse_passthrough(WindowID p_window);
  528. void _update_real_mouse_position(WindowID p_window);
  529. void _set_mouse_mode_impl(MouseMode p_mode);
  530. WindowID _get_focused_window_or_popup() const;
  531. void _register_raw_input_devices(WindowID p_target_window);
  532. void _process_activate_event(WindowID p_window_id);
  533. void _process_key_events();
  534. static void _dispatch_input_events(const Ref<InputEvent> &p_event);
  535. void _dispatch_input_event(const Ref<InputEvent> &p_event);
  536. LRESULT _handle_early_window_message(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  537. Point2i _get_screens_origin() const;
  538. enum class WinKeyModifierMask {
  539. ALT_GR = (1 << 1),
  540. SHIFT = (1 << 2),
  541. ALT = (1 << 3),
  542. META = (1 << 4),
  543. CTRL = (1 << 5),
  544. };
  545. BitField<WinKeyModifierMask> _get_mods() const;
  546. 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);
  547. String _get_keyboard_layout_display_name(const String &p_klid) const;
  548. String _get_klid(HKL p_hkl) const;
  549. public:
  550. LRESULT WndProcFileDialog(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  551. LRESULT WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  552. LRESULT MouseProc(int code, WPARAM wParam, LPARAM lParam);
  553. void popup_open(WindowID p_window);
  554. void popup_close(WindowID p_window);
  555. virtual bool has_feature(Feature p_feature) const override;
  556. virtual String get_name() const override;
  557. virtual bool tts_is_speaking() const override;
  558. virtual bool tts_is_paused() const override;
  559. virtual TypedArray<Dictionary> tts_get_voices() const override;
  560. 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;
  561. virtual void tts_pause() override;
  562. virtual void tts_resume() override;
  563. virtual void tts_stop() override;
  564. virtual bool is_dark_mode_supported() const override;
  565. virtual bool is_dark_mode() const override;
  566. virtual Color get_accent_color() const override;
  567. virtual Color get_base_color() const override;
  568. virtual void set_system_theme_change_callback(const Callable &p_callable) override;
  569. 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) override;
  570. 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) override;
  571. virtual void beep() const override;
  572. virtual void mouse_set_mode(MouseMode p_mode) override;
  573. virtual MouseMode mouse_get_mode() const override;
  574. virtual void warp_mouse(const Point2i &p_position) override;
  575. virtual Point2i mouse_get_position() const override;
  576. virtual BitField<MouseButtonMask> mouse_get_button_state() const override;
  577. virtual void clipboard_set(const String &p_text) override;
  578. virtual String clipboard_get() const override;
  579. virtual Ref<Image> clipboard_get_image() const override;
  580. virtual bool clipboard_has() const override;
  581. virtual bool clipboard_has_image() const override;
  582. virtual int get_screen_count() const override;
  583. virtual int get_primary_screen() const override;
  584. virtual int get_keyboard_focus_screen() const override;
  585. virtual Point2i screen_get_position(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  586. virtual Size2i screen_get_size(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  587. virtual Rect2i screen_get_usable_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  588. virtual int screen_get_dpi(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  589. virtual float screen_get_refresh_rate(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  590. virtual Color screen_get_pixel(const Point2i &p_position) const override;
  591. virtual Ref<Image> screen_get_image(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  592. virtual Ref<Image> screen_get_image_rect(const Rect2i &p_rect) const override;
  593. virtual void screen_set_keep_on(bool p_enable) override; //disable screensaver
  594. virtual bool screen_is_kept_on() const override;
  595. virtual Vector<DisplayServer::WindowID> get_window_list() const override;
  596. 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;
  597. virtual void show_window(WindowID p_window) override;
  598. virtual void delete_sub_window(WindowID p_window) override;
  599. virtual WindowID window_get_active_popup() const override;
  600. virtual void window_set_popup_safe_rect(WindowID p_window, const Rect2i &p_rect) override;
  601. virtual Rect2i window_get_popup_safe_rect(WindowID p_window) const override;
  602. virtual int64_t window_get_native_handle(HandleType p_handle_type, WindowID p_window = MAIN_WINDOW_ID) const override;
  603. virtual WindowID get_window_at_screen_position(const Point2i &p_position) const override;
  604. virtual void window_attach_instance_id(ObjectID p_instance, WindowID p_window = MAIN_WINDOW_ID) override;
  605. virtual ObjectID window_get_attached_instance_id(WindowID p_window = MAIN_WINDOW_ID) const override;
  606. virtual void gl_window_make_current(DisplayServer::WindowID p_window_id) override;
  607. virtual void window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  608. virtual void window_set_window_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  609. virtual void window_set_input_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  610. virtual void window_set_input_text_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  611. virtual void window_set_drop_files_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  612. virtual void window_set_title(const String &p_title, WindowID p_window = MAIN_WINDOW_ID) override;
  613. virtual Size2i window_get_title_size(const String &p_title, WindowID p_window = MAIN_WINDOW_ID) const override;
  614. virtual void window_set_mouse_passthrough(const Vector<Vector2> &p_region, WindowID p_window = MAIN_WINDOW_ID) override;
  615. virtual int window_get_current_screen(WindowID p_window = MAIN_WINDOW_ID) const override;
  616. virtual void window_set_current_screen(int p_screen, WindowID p_window = MAIN_WINDOW_ID) override;
  617. virtual Point2i window_get_position(WindowID p_window = MAIN_WINDOW_ID) const override;
  618. virtual Point2i window_get_position_with_decorations(WindowID p_window = MAIN_WINDOW_ID) const override;
  619. virtual void window_set_position(const Point2i &p_position, WindowID p_window = MAIN_WINDOW_ID) override;
  620. virtual void window_set_transient(WindowID p_window, WindowID p_parent) override;
  621. virtual void window_set_exclusive(WindowID p_window, bool p_exclusive) override;
  622. virtual void window_set_max_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
  623. virtual Size2i window_get_max_size(WindowID p_window = MAIN_WINDOW_ID) const override;
  624. virtual void window_set_min_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
  625. virtual Size2i window_get_min_size(WindowID p_window = MAIN_WINDOW_ID) const override;
  626. virtual void window_set_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
  627. virtual Size2i window_get_size(WindowID p_window = MAIN_WINDOW_ID) const override;
  628. virtual Size2i window_get_size_with_decorations(WindowID p_window = MAIN_WINDOW_ID) const override;
  629. virtual void window_set_mode(WindowMode p_mode, WindowID p_window = MAIN_WINDOW_ID) override;
  630. virtual WindowMode window_get_mode(WindowID p_window = MAIN_WINDOW_ID) const override;
  631. virtual bool window_is_maximize_allowed(WindowID p_window = MAIN_WINDOW_ID) const override;
  632. virtual void window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window = MAIN_WINDOW_ID) override;
  633. virtual bool window_get_flag(WindowFlags p_flag, WindowID p_window = MAIN_WINDOW_ID) const override;
  634. virtual void window_request_attention(WindowID p_window = MAIN_WINDOW_ID) override;
  635. virtual void window_move_to_foreground(WindowID p_window = MAIN_WINDOW_ID) override;
  636. virtual bool window_is_focused(WindowID p_window = MAIN_WINDOW_ID) const override;
  637. virtual WindowID get_focused_window() const override;
  638. virtual bool window_can_draw(WindowID p_window = MAIN_WINDOW_ID) const override;
  639. virtual bool can_any_window_draw() const override;
  640. virtual void window_set_ime_active(const bool p_active, WindowID p_window = MAIN_WINDOW_ID) override;
  641. virtual void window_set_ime_position(const Point2i &p_pos, WindowID p_window = MAIN_WINDOW_ID) override;
  642. virtual Point2i ime_get_selection() const override;
  643. virtual String ime_get_text() const override;
  644. virtual void window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window = MAIN_WINDOW_ID) override;
  645. virtual DisplayServer::VSyncMode window_get_vsync_mode(WindowID p_vsync_mode) const override;
  646. virtual void cursor_set_shape(CursorShape p_shape) override;
  647. virtual CursorShape cursor_get_shape() const override;
  648. virtual void cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape = CURSOR_ARROW, const Vector2 &p_hotspot = Vector2()) override;
  649. virtual bool get_swap_cancel_ok() override;
  650. virtual void enable_for_stealing_focus(OS::ProcessID pid) override;
  651. virtual Error dialog_show(String p_title, String p_description, Vector<String> p_buttons, const Callable &p_callback) override;
  652. virtual Error dialog_input_text(String p_title, String p_description, String p_partial, const Callable &p_callback) override;
  653. virtual int keyboard_get_layout_count() const override;
  654. virtual int keyboard_get_current_layout() const override;
  655. virtual void keyboard_set_current_layout(int p_index) override;
  656. virtual String keyboard_get_layout_language(int p_index) const override;
  657. virtual String keyboard_get_layout_name(int p_index) const override;
  658. virtual Key keyboard_get_keycode_from_physical(Key p_keycode) const override;
  659. virtual Key keyboard_get_label_from_physical(Key p_keycode) const override;
  660. virtual int tablet_get_driver_count() const override;
  661. virtual String tablet_get_driver_name(int p_driver) const override;
  662. virtual String tablet_get_current_driver() const override;
  663. virtual void tablet_set_current_driver(const String &p_driver) override;
  664. virtual void process_events() override;
  665. virtual void force_process_and_drop_events() override;
  666. virtual void release_rendering_thread() override;
  667. virtual void swap_buffers() override;
  668. virtual void set_native_icon(const String &p_filename) override;
  669. virtual void set_icon(const Ref<Image> &p_icon) override;
  670. virtual IndicatorID create_status_indicator(const Ref<Texture2D> &p_icon, const String &p_tooltip, const Callable &p_callback) override;
  671. virtual void status_indicator_set_icon(IndicatorID p_id, const Ref<Texture2D> &p_icon) override;
  672. virtual void status_indicator_set_tooltip(IndicatorID p_id, const String &p_tooltip) override;
  673. virtual void status_indicator_set_menu(IndicatorID p_id, const RID &p_rid) override;
  674. virtual void status_indicator_set_callback(IndicatorID p_id, const Callable &p_callback) override;
  675. virtual Rect2 status_indicator_get_rect(IndicatorID p_id) const override;
  676. virtual void delete_status_indicator(IndicatorID p_id) override;
  677. virtual void set_context(Context p_context) override;
  678. virtual bool is_window_transparency_available() const override;
  679. 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, Error &r_error);
  680. static Vector<String> get_rendering_drivers_func();
  681. static void register_windows_driver();
  682. 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, Error &r_error);
  683. ~DisplayServerWindows();
  684. };
  685. #endif // DISPLAY_SERVER_WINDOWS_H