os_windows.h 17 KB

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