os_windows.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*************************************************************************/
  2. /* os_windows.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 OS_WINDOWS_H
  31. #define OS_WINDOWS_H
  32. #include "context_gl_windows.h"
  33. #include "core/os/input.h"
  34. #include "core/os/os.h"
  35. #include "core/project_settings.h"
  36. #include "crash_handler_windows.h"
  37. #include "drivers/unix/ip_unix.h"
  38. #include "drivers/wasapi/audio_driver_wasapi.h"
  39. #include "drivers/winmidi/midi_driver_winmidi.h"
  40. #include "key_mapping_windows.h"
  41. #include "main/input_default.h"
  42. #include "power_windows.h"
  43. #include "servers/audio_server.h"
  44. #include "servers/visual/rasterizer.h"
  45. #include "servers/visual_server.h"
  46. #ifdef XAUDIO2_ENABLED
  47. #include "drivers/xaudio2/audio_driver_xaudio2.h"
  48. #endif
  49. #include <dwmapi.h>
  50. #include <fcntl.h>
  51. #include <io.h>
  52. #include <shellapi.h>
  53. #include <stdio.h>
  54. #define WIN32_LEAN_AND_MEAN
  55. #include <windows.h>
  56. #include <windowsx.h>
  57. // WinTab API
  58. #define WT_PACKET 0x7FF0
  59. #define WT_PROXIMITY 0x7FF5
  60. #define WT_INFOCHANGE 0x7FF6
  61. #define WT_CSRCHANGE 0x7FF7
  62. #define WTI_DEFSYSCTX 4
  63. #define WTI_DEVICES 100
  64. #define DVC_NPRESSURE 15
  65. #define DVC_TPRESSURE 16
  66. #define DVC_ORIENTATION 17
  67. #define DVC_ROTATION 18
  68. #define CXO_MESSAGES 0x0004
  69. #define PK_NORMAL_PRESSURE 0x0400
  70. #define PK_TANGENT_PRESSURE 0x0800
  71. #define PK_ORIENTATION 0x1000
  72. typedef struct tagLOGCONTEXTW {
  73. WCHAR lcName[40];
  74. UINT lcOptions;
  75. UINT lcStatus;
  76. UINT lcLocks;
  77. UINT lcMsgBase;
  78. UINT lcDevice;
  79. UINT lcPktRate;
  80. DWORD lcPktData;
  81. DWORD lcPktMode;
  82. DWORD lcMoveMask;
  83. DWORD lcBtnDnMask;
  84. DWORD lcBtnUpMask;
  85. LONG lcInOrgX;
  86. LONG lcInOrgY;
  87. LONG lcInOrgZ;
  88. LONG lcInExtX;
  89. LONG lcInExtY;
  90. LONG lcInExtZ;
  91. LONG lcOutOrgX;
  92. LONG lcOutOrgY;
  93. LONG lcOutOrgZ;
  94. LONG lcOutExtX;
  95. LONG lcOutExtY;
  96. LONG lcOutExtZ;
  97. DWORD lcSensX;
  98. DWORD lcSensY;
  99. DWORD lcSensZ;
  100. BOOL lcSysMode;
  101. int lcSysOrgX;
  102. int lcSysOrgY;
  103. int lcSysExtX;
  104. int lcSysExtY;
  105. DWORD lcSysSensX;
  106. DWORD lcSysSensY;
  107. } LOGCONTEXTW;
  108. typedef struct tagAXIS {
  109. LONG axMin;
  110. LONG axMax;
  111. UINT axUnits;
  112. DWORD axResolution;
  113. } AXIS;
  114. typedef struct tagORIENTATION {
  115. int orAzimuth;
  116. int orAltitude;
  117. int orTwist;
  118. } ORIENTATION;
  119. typedef struct tagPACKET {
  120. int pkNormalPressure;
  121. int pkTangentPressure;
  122. ORIENTATION pkOrientation;
  123. } PACKET;
  124. typedef HANDLE(WINAPI *WTOpenPtr)(HWND p_window, LOGCONTEXTW *p_ctx, BOOL p_enable);
  125. typedef BOOL(WINAPI *WTClosePtr)(HANDLE p_ctx);
  126. typedef UINT(WINAPI *WTInfoPtr)(UINT p_category, UINT p_index, LPVOID p_output);
  127. typedef BOOL(WINAPI *WTPacketPtr)(HANDLE p_ctx, UINT p_param, LPVOID p_packets);
  128. typedef BOOL(WINAPI *WTEnablePtr)(HANDLE p_ctx, BOOL p_enable);
  129. // Windows Ink API
  130. #ifndef POINTER_STRUCTURES
  131. #define POINTER_STRUCTURES
  132. typedef DWORD POINTER_INPUT_TYPE;
  133. typedef UINT32 POINTER_FLAGS;
  134. typedef UINT32 PEN_FLAGS;
  135. typedef UINT32 PEN_MASK;
  136. #ifndef PEN_MASK_PRESSURE
  137. #define PEN_MASK_PRESSURE 0x00000001
  138. #endif
  139. #ifndef PEN_MASK_TILT_X
  140. #define PEN_MASK_TILT_X 0x00000004
  141. #endif
  142. #ifndef PEN_MASK_TILT_Y
  143. #define PEN_MASK_TILT_Y 0x00000008
  144. #endif
  145. #ifndef POINTER_MESSAGE_FLAG_FIRSTBUTTON
  146. #define POINTER_MESSAGE_FLAG_FIRSTBUTTON 0x00000010
  147. #endif
  148. enum tagPOINTER_INPUT_TYPE {
  149. PT_POINTER = 0x00000001,
  150. PT_TOUCH = 0x00000002,
  151. PT_PEN = 0x00000003,
  152. PT_MOUSE = 0x00000004,
  153. PT_TOUCHPAD = 0x00000005
  154. };
  155. typedef enum tagPOINTER_BUTTON_CHANGE_TYPE {
  156. POINTER_CHANGE_NONE,
  157. POINTER_CHANGE_FIRSTBUTTON_DOWN,
  158. POINTER_CHANGE_FIRSTBUTTON_UP,
  159. POINTER_CHANGE_SECONDBUTTON_DOWN,
  160. POINTER_CHANGE_SECONDBUTTON_UP,
  161. POINTER_CHANGE_THIRDBUTTON_DOWN,
  162. POINTER_CHANGE_THIRDBUTTON_UP,
  163. POINTER_CHANGE_FOURTHBUTTON_DOWN,
  164. POINTER_CHANGE_FOURTHBUTTON_UP,
  165. POINTER_CHANGE_FIFTHBUTTON_DOWN,
  166. POINTER_CHANGE_FIFTHBUTTON_UP,
  167. } POINTER_BUTTON_CHANGE_TYPE;
  168. typedef struct tagPOINTER_INFO {
  169. POINTER_INPUT_TYPE pointerType;
  170. UINT32 pointerId;
  171. UINT32 frameId;
  172. POINTER_FLAGS pointerFlags;
  173. HANDLE sourceDevice;
  174. HWND hwndTarget;
  175. POINT ptPixelLocation;
  176. POINT ptHimetricLocation;
  177. POINT ptPixelLocationRaw;
  178. POINT ptHimetricLocationRaw;
  179. DWORD dwTime;
  180. UINT32 historyCount;
  181. INT32 InputData;
  182. DWORD dwKeyStates;
  183. UINT64 PerformanceCount;
  184. POINTER_BUTTON_CHANGE_TYPE ButtonChangeType;
  185. } POINTER_INFO;
  186. typedef struct tagPOINTER_PEN_INFO {
  187. POINTER_INFO pointerInfo;
  188. PEN_FLAGS penFlags;
  189. PEN_MASK penMask;
  190. UINT32 pressure;
  191. UINT32 rotation;
  192. INT32 tiltX;
  193. INT32 tiltY;
  194. } POINTER_PEN_INFO;
  195. #endif
  196. #ifndef WM_POINTERUPDATE
  197. #define WM_POINTERUPDATE 0x0245
  198. #endif
  199. #ifndef WM_POINTERENTER
  200. #define WM_POINTERENTER 0x0249
  201. #endif
  202. #ifndef WM_POINTERLEAVE
  203. #define WM_POINTERLEAVE 0x024A
  204. #endif
  205. typedef BOOL(WINAPI *GetPointerTypePtr)(uint32_t p_id, POINTER_INPUT_TYPE *p_type);
  206. typedef BOOL(WINAPI *GetPointerPenInfoPtr)(uint32_t p_id, POINTER_PEN_INFO *p_pen_info);
  207. typedef struct {
  208. BYTE bWidth; // Width, in pixels, of the image
  209. BYTE bHeight; // Height, in pixels, of the image
  210. BYTE bColorCount; // Number of colors in image (0 if >=8bpp)
  211. BYTE bReserved; // Reserved ( must be 0)
  212. WORD wPlanes; // Color Planes
  213. WORD wBitCount; // Bits per pixel
  214. DWORD dwBytesInRes; // How many bytes in this resource?
  215. DWORD dwImageOffset; // Where in the file is this image?
  216. } ICONDIRENTRY, *LPICONDIRENTRY;
  217. typedef struct {
  218. WORD idReserved; // Reserved (must be 0)
  219. WORD idType; // Resource Type (1 for icons)
  220. WORD idCount; // How many images?
  221. ICONDIRENTRY idEntries[1]; // An entry for each image (idCount of 'em)
  222. } ICONDIR, *LPICONDIR;
  223. class JoypadWindows;
  224. class OS_Windows : public OS {
  225. String tablet_driver;
  226. Vector<String> tablet_drivers;
  227. // WinTab API
  228. static bool wintab_available;
  229. static WTOpenPtr wintab_WTOpen;
  230. static WTClosePtr wintab_WTClose;
  231. static WTInfoPtr wintab_WTInfo;
  232. static WTPacketPtr wintab_WTPacket;
  233. static WTEnablePtr wintab_WTEnable;
  234. // Windows Ink API
  235. static bool winink_available;
  236. static GetPointerTypePtr win8p_GetPointerType;
  237. static GetPointerPenInfoPtr win8p_GetPointerPenInfo;
  238. HANDLE wtctx;
  239. LOGCONTEXTW wtlc;
  240. int min_pressure;
  241. int max_pressure;
  242. bool tilt_supported;
  243. bool block_mm = false;
  244. int last_pressure_update;
  245. float last_pressure;
  246. Vector2 last_tilt;
  247. enum {
  248. KEY_EVENT_BUFFER_SIZE = 512
  249. };
  250. #ifdef STDOUT_FILE
  251. FILE *stdo;
  252. #endif
  253. struct KeyEvent {
  254. bool alt, shift, control, meta;
  255. UINT uMsg;
  256. WPARAM wParam;
  257. LPARAM lParam;
  258. };
  259. KeyEvent key_event_buffer[KEY_EVENT_BUFFER_SIZE];
  260. int key_event_pos;
  261. uint64_t ticks_start;
  262. uint64_t ticks_per_second;
  263. bool old_invalid;
  264. bool outside;
  265. int old_x, old_y;
  266. Point2i center;
  267. #if defined(OPENGL_ENABLED)
  268. ContextGL_Windows *gl_context;
  269. #endif
  270. VisualServer *visual_server;
  271. int pressrc;
  272. HINSTANCE hInstance; // Holds The Instance Of The Application
  273. HWND hWnd;
  274. Vector<Vector2> mpath;
  275. Point2 last_pos;
  276. bool layered_window;
  277. uint32_t move_timer_id;
  278. HCURSOR hCursor;
  279. Size2 min_size;
  280. Size2 max_size;
  281. Size2 window_rect;
  282. VideoMode video_mode;
  283. bool preserve_window_size = false;
  284. MainLoop *main_loop;
  285. WNDPROC user_proc;
  286. // IME
  287. HIMC im_himc;
  288. Vector2 im_position;
  289. MouseMode mouse_mode;
  290. int restore_mouse_trails;
  291. bool alt_mem;
  292. bool gr_mem;
  293. bool shift_mem;
  294. bool control_mem;
  295. bool meta_mem;
  296. bool force_quit;
  297. bool window_has_focus;
  298. uint32_t last_button_state;
  299. bool use_raw_input;
  300. bool drop_events;
  301. HCURSOR cursors[CURSOR_MAX] = { NULL };
  302. CursorShape cursor_shape;
  303. Map<CursorShape, Vector<Variant>> cursors_cache;
  304. InputDefault *input;
  305. JoypadWindows *joypad;
  306. Map<int, Vector2> touch_state;
  307. PowerWindows *power_manager;
  308. int video_driver_index;
  309. #ifdef WASAPI_ENABLED
  310. AudioDriverWASAPI driver_wasapi;
  311. #endif
  312. #ifdef XAUDIO2_ENABLED
  313. AudioDriverXAudio2 driver_xaudio2;
  314. #endif
  315. #ifdef WINMIDI_ENABLED
  316. MIDIDriverWinMidi driver_midi;
  317. #endif
  318. CrashHandler crash_handler;
  319. bool _is_win11_terminal() const;
  320. void _drag_event(float p_x, float p_y, int idx);
  321. void _touch_event(bool p_pressed, float p_x, float p_y, int idx);
  322. void _update_window_style(bool p_repaint = true, bool p_maximized = false);
  323. void _update_window_mouse_passthrough();
  324. void _set_mouse_mode_impl(MouseMode p_mode);
  325. // functions used by main to initialize/deinitialize the OS
  326. protected:
  327. virtual int get_current_video_driver() const;
  328. virtual void initialize_core();
  329. virtual Error initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver);
  330. virtual void set_main_loop(MainLoop *p_main_loop);
  331. virtual void delete_main_loop();
  332. virtual void finalize();
  333. virtual void finalize_core();
  334. void process_events();
  335. void process_key_events();
  336. String _quote_command_line_argument(const String &p_text) const;
  337. struct ProcessInfo {
  338. STARTUPINFO si;
  339. PROCESS_INFORMATION pi;
  340. };
  341. Map<ProcessID, ProcessInfo> *process_map;
  342. bool pre_fs_valid;
  343. RECT pre_fs_rect;
  344. bool maximized;
  345. bool minimized;
  346. bool borderless;
  347. bool window_focused;
  348. bool console_visible;
  349. bool was_maximized;
  350. public:
  351. LRESULT WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  352. virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
  353. String get_stdin_string(bool p_block);
  354. void set_mouse_mode(MouseMode p_mode);
  355. MouseMode get_mouse_mode() const;
  356. virtual void warp_mouse_position(const Point2 &p_to);
  357. virtual Point2 get_mouse_position() const;
  358. void update_real_mouse_position();
  359. virtual int get_mouse_button_state() const;
  360. virtual void set_window_title(const String &p_title);
  361. virtual void set_window_mouse_passthrough(const PoolVector2Array &p_region);
  362. virtual void set_video_mode(const VideoMode &p_video_mode, int p_screen = 0);
  363. virtual VideoMode get_video_mode(int p_screen = 0) const;
  364. virtual void get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen = 0) const;
  365. virtual int get_tablet_driver_count() const;
  366. virtual String get_tablet_driver_name(int p_driver) const;
  367. virtual String get_current_tablet_driver() const;
  368. virtual void set_current_tablet_driver(const String &p_driver);
  369. virtual int get_screen_count() const;
  370. virtual int get_current_screen() const;
  371. virtual void set_current_screen(int p_screen);
  372. virtual Point2 get_screen_position(int p_screen = -1) const;
  373. virtual Size2 get_screen_size(int p_screen = -1) const;
  374. virtual int get_screen_dpi(int p_screen = -1) const;
  375. virtual Point2 get_window_position() const;
  376. virtual void set_window_position(const Point2 &p_position);
  377. virtual Size2 get_window_size() const;
  378. virtual Size2 get_real_window_size() const;
  379. virtual Size2 get_max_window_size() const;
  380. virtual Size2 get_min_window_size() const;
  381. virtual void set_min_window_size(const Size2 p_size);
  382. virtual void set_max_window_size(const Size2 p_size);
  383. virtual void set_window_size(const Size2 p_size);
  384. virtual void set_window_fullscreen(bool p_enabled);
  385. virtual bool is_window_fullscreen() const;
  386. virtual void set_window_resizable(bool p_enabled);
  387. virtual bool is_window_resizable() const;
  388. virtual void set_window_minimized(bool p_enabled);
  389. virtual bool is_window_minimized() const;
  390. virtual void set_window_maximized(bool p_enabled);
  391. virtual bool is_window_maximized() const;
  392. virtual void set_window_always_on_top(bool p_enabled);
  393. virtual bool is_window_always_on_top() const;
  394. virtual bool is_window_focused() const;
  395. virtual void set_console_visible(bool p_enabled);
  396. virtual bool is_console_visible() const;
  397. virtual void request_attention();
  398. virtual void *get_native_handle(int p_handle_type);
  399. virtual void set_borderless_window(bool p_borderless);
  400. virtual bool get_borderless_window();
  401. virtual bool get_window_per_pixel_transparency_enabled() const;
  402. virtual void set_window_per_pixel_transparency_enabled(bool p_enabled);
  403. virtual Error open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path = false);
  404. virtual Error close_dynamic_library(void *p_library_handle);
  405. virtual Error get_dynamic_library_symbol_handle(void *p_library_handle, const String p_name, void *&p_symbol_handle, bool p_optional = false);
  406. virtual MainLoop *get_main_loop() const;
  407. virtual String get_name() const;
  408. virtual Date get_date(bool utc) const;
  409. virtual Time get_time(bool utc) const;
  410. virtual TimeZoneInfo get_time_zone_info() const;
  411. virtual uint64_t get_unix_time() const;
  412. virtual uint64_t get_system_time_secs() const;
  413. virtual uint64_t get_system_time_msecs() const;
  414. virtual bool can_draw() const;
  415. virtual Error set_cwd(const String &p_cwd);
  416. virtual void delay_usec(uint32_t p_usec) const;
  417. virtual uint64_t get_ticks_usec() const;
  418. virtual Error execute(const String &p_path, const List<String> &p_arguments, bool p_blocking = true, ProcessID *r_child_id = NULL, String *r_pipe = NULL, int *r_exitcode = NULL, bool read_stderr = false, Mutex *p_pipe_mutex = NULL);
  419. virtual Error kill(const ProcessID &p_pid);
  420. virtual int get_process_id() const;
  421. virtual bool has_environment(const String &p_var) const;
  422. virtual String get_environment(const String &p_var) const;
  423. virtual bool set_environment(const String &p_var, const String &p_value) const;
  424. virtual void set_clipboard(const String &p_text);
  425. virtual String get_clipboard() const;
  426. void set_cursor_shape(CursorShape p_shape);
  427. CursorShape get_cursor_shape() const;
  428. virtual void set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot);
  429. void GetMaskBitmaps(HBITMAP hSourceBitmap, COLORREF clrTransparent, OUT HBITMAP &hAndMaskBitmap, OUT HBITMAP &hXorMaskBitmap);
  430. void set_native_icon(const String &p_filename);
  431. void set_icon(const Ref<Image> &p_icon);
  432. virtual String get_executable_path() const;
  433. virtual String get_locale() const;
  434. virtual int get_processor_count() const;
  435. virtual LatinKeyboardVariant get_latin_keyboard_variant() const;
  436. virtual int keyboard_get_layout_count() const;
  437. virtual int keyboard_get_current_layout() const;
  438. virtual void keyboard_set_current_layout(int p_index);
  439. virtual String keyboard_get_layout_language(int p_index) const;
  440. virtual String keyboard_get_layout_name(int p_index) const;
  441. virtual void enable_for_stealing_focus(ProcessID pid);
  442. virtual void move_window_to_foreground();
  443. virtual String get_config_path() const;
  444. virtual String get_data_path() const;
  445. virtual String get_cache_path() const;
  446. virtual String get_godot_dir_name() const;
  447. virtual String get_system_dir(SystemDir p_dir, bool p_shared_storage = true) const;
  448. virtual String get_user_data_dir() const;
  449. virtual String get_unique_id() const;
  450. virtual void set_ime_active(const bool p_active);
  451. virtual void set_ime_position(const Point2 &p_pos);
  452. virtual void release_rendering_thread();
  453. virtual void make_rendering_thread();
  454. virtual void swap_buffers();
  455. virtual Error shell_open(String p_uri);
  456. void run();
  457. virtual bool get_swap_ok_cancel() { return true; }
  458. virtual bool is_joy_known(int p_device);
  459. virtual String get_joy_guid(int p_device) const;
  460. virtual void _set_use_vsync(bool p_enable);
  461. //virtual bool is_vsync_enabled() const;
  462. virtual OS::PowerState get_power_state();
  463. virtual int get_power_seconds_left();
  464. virtual int get_power_percent_left();
  465. virtual bool _check_internal_feature_support(const String &p_feature);
  466. void disable_crash_handler();
  467. bool is_disable_crash_handler() const;
  468. virtual void initialize_debugging();
  469. void force_process_input();
  470. virtual Error move_to_trash(const String &p_path);
  471. virtual void process_and_drop_events();
  472. OS_Windows(HINSTANCE _hInstance);
  473. ~OS_Windows();
  474. };
  475. #endif