os_windows.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*************************************************************************/
  2. /* os_windows.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 <fcntl.h>
  50. #include <io.h>
  51. #include <stdio.h>
  52. #include <windows.h>
  53. #include <windowsx.h>
  54. #ifndef POINTER_STRUCTURES
  55. #define POINTER_STRUCTURES
  56. typedef DWORD POINTER_INPUT_TYPE;
  57. typedef UINT32 POINTER_FLAGS;
  58. typedef UINT32 PEN_FLAGS;
  59. typedef UINT32 PEN_MASK;
  60. enum tagPOINTER_INPUT_TYPE {
  61. PT_POINTER = 0x00000001,
  62. PT_TOUCH = 0x00000002,
  63. PT_PEN = 0x00000003,
  64. PT_MOUSE = 0x00000004,
  65. PT_TOUCHPAD = 0x00000005
  66. };
  67. typedef enum tagPOINTER_BUTTON_CHANGE_TYPE {
  68. POINTER_CHANGE_NONE,
  69. POINTER_CHANGE_FIRSTBUTTON_DOWN,
  70. POINTER_CHANGE_FIRSTBUTTON_UP,
  71. POINTER_CHANGE_SECONDBUTTON_DOWN,
  72. POINTER_CHANGE_SECONDBUTTON_UP,
  73. POINTER_CHANGE_THIRDBUTTON_DOWN,
  74. POINTER_CHANGE_THIRDBUTTON_UP,
  75. POINTER_CHANGE_FOURTHBUTTON_DOWN,
  76. POINTER_CHANGE_FOURTHBUTTON_UP,
  77. POINTER_CHANGE_FIFTHBUTTON_DOWN,
  78. POINTER_CHANGE_FIFTHBUTTON_UP,
  79. } POINTER_BUTTON_CHANGE_TYPE;
  80. typedef struct tagPOINTER_INFO {
  81. POINTER_INPUT_TYPE pointerType;
  82. UINT32 pointerId;
  83. UINT32 frameId;
  84. POINTER_FLAGS pointerFlags;
  85. HANDLE sourceDevice;
  86. HWND hwndTarget;
  87. POINT ptPixelLocation;
  88. POINT ptHimetricLocation;
  89. POINT ptPixelLocationRaw;
  90. POINT ptHimetricLocationRaw;
  91. DWORD dwTime;
  92. UINT32 historyCount;
  93. INT32 InputData;
  94. DWORD dwKeyStates;
  95. UINT64 PerformanceCount;
  96. POINTER_BUTTON_CHANGE_TYPE ButtonChangeType;
  97. } POINTER_INFO;
  98. typedef struct tagPOINTER_PEN_INFO {
  99. POINTER_INFO pointerInfo;
  100. PEN_FLAGS penFlags;
  101. PEN_MASK penMask;
  102. UINT32 pressure;
  103. UINT32 rotation;
  104. INT32 tiltX;
  105. INT32 tiltY;
  106. } POINTER_PEN_INFO;
  107. #endif
  108. typedef BOOL(WINAPI *GetPointerTypePtr)(uint32_t p_id, POINTER_INPUT_TYPE *p_type);
  109. typedef BOOL(WINAPI *GetPointerPenInfoPtr)(uint32_t p_id, POINTER_PEN_INFO *p_pen_info);
  110. typedef struct {
  111. BYTE bWidth; // Width, in pixels, of the image
  112. BYTE bHeight; // Height, in pixels, of the image
  113. BYTE bColorCount; // Number of colors in image (0 if >=8bpp)
  114. BYTE bReserved; // Reserved ( must be 0)
  115. WORD wPlanes; // Color Planes
  116. WORD wBitCount; // Bits per pixel
  117. DWORD dwBytesInRes; // How many bytes in this resource?
  118. DWORD dwImageOffset; // Where in the file is this image?
  119. } ICONDIRENTRY, *LPICONDIRENTRY;
  120. typedef struct {
  121. WORD idReserved; // Reserved (must be 0)
  122. WORD idType; // Resource Type (1 for icons)
  123. WORD idCount; // How many images?
  124. ICONDIRENTRY idEntries[1]; // An entry for each image (idCount of 'em)
  125. } ICONDIR, *LPICONDIR;
  126. class JoypadWindows;
  127. class OS_Windows : public OS {
  128. static GetPointerTypePtr win8p_GetPointerType;
  129. static GetPointerPenInfoPtr win8p_GetPointerPenInfo;
  130. enum {
  131. KEY_EVENT_BUFFER_SIZE = 512
  132. };
  133. #ifdef STDOUT_FILE
  134. FILE *stdo;
  135. #endif
  136. struct KeyEvent {
  137. bool alt, shift, control, meta;
  138. UINT uMsg;
  139. WPARAM wParam;
  140. LPARAM lParam;
  141. };
  142. KeyEvent key_event_buffer[KEY_EVENT_BUFFER_SIZE];
  143. int key_event_pos;
  144. uint64_t ticks_start;
  145. uint64_t ticks_per_second;
  146. bool old_invalid;
  147. bool outside;
  148. int old_x, old_y;
  149. Point2i center;
  150. #if defined(OPENGL_ENABLED)
  151. ContextGL_Windows *gl_context;
  152. #endif
  153. VisualServer *visual_server;
  154. int pressrc;
  155. HINSTANCE hInstance; // Holds The Instance Of The Application
  156. HWND hWnd;
  157. Point2 last_pos;
  158. HBITMAP hBitmap; //DIB section for layered window
  159. uint8_t *dib_data;
  160. Size2 dib_size;
  161. HDC hDC_dib;
  162. bool layered_window;
  163. uint32_t move_timer_id;
  164. HCURSOR hCursor;
  165. Size2 min_size;
  166. Size2 max_size;
  167. Size2 window_rect;
  168. VideoMode video_mode;
  169. bool preserve_window_size = false;
  170. MainLoop *main_loop;
  171. WNDPROC user_proc;
  172. // IME
  173. HIMC im_himc;
  174. Vector2 im_position;
  175. MouseMode mouse_mode;
  176. bool alt_mem;
  177. bool gr_mem;
  178. bool shift_mem;
  179. bool control_mem;
  180. bool meta_mem;
  181. bool force_quit;
  182. bool window_has_focus;
  183. uint32_t last_button_state;
  184. bool use_raw_input;
  185. bool drop_events;
  186. HCURSOR cursors[CURSOR_MAX] = { NULL };
  187. CursorShape cursor_shape;
  188. Map<CursorShape, Vector<Variant> > cursors_cache;
  189. InputDefault *input;
  190. JoypadWindows *joypad;
  191. Map<int, Vector2> touch_state;
  192. PowerWindows *power_manager;
  193. int video_driver_index;
  194. #ifdef WASAPI_ENABLED
  195. AudioDriverWASAPI driver_wasapi;
  196. #endif
  197. #ifdef XAUDIO2_ENABLED
  198. AudioDriverXAudio2 driver_xaudio2;
  199. #endif
  200. #ifdef WINMIDI_ENABLED
  201. MIDIDriverWinMidi driver_midi;
  202. #endif
  203. CrashHandler crash_handler;
  204. void _drag_event(float p_x, float p_y, int idx);
  205. void _touch_event(bool p_pressed, float p_x, float p_y, int idx);
  206. void _update_window_style(bool p_repaint = true, bool p_maximized = false);
  207. void _set_mouse_mode_impl(MouseMode p_mode);
  208. // functions used by main to initialize/deinitialize the OS
  209. protected:
  210. virtual int get_current_video_driver() const;
  211. virtual void initialize_core();
  212. virtual Error initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver);
  213. virtual void set_main_loop(MainLoop *p_main_loop);
  214. virtual void delete_main_loop();
  215. virtual void finalize();
  216. virtual void finalize_core();
  217. void process_events();
  218. void process_key_events();
  219. struct ProcessInfo {
  220. STARTUPINFO si;
  221. PROCESS_INFORMATION pi;
  222. };
  223. Map<ProcessID, ProcessInfo> *process_map;
  224. bool pre_fs_valid;
  225. RECT pre_fs_rect;
  226. bool maximized;
  227. bool minimized;
  228. bool borderless;
  229. bool window_focused;
  230. bool console_visible;
  231. bool was_maximized;
  232. public:
  233. LRESULT WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  234. virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
  235. String get_stdin_string(bool p_block);
  236. void set_mouse_mode(MouseMode p_mode);
  237. MouseMode get_mouse_mode() const;
  238. virtual void warp_mouse_position(const Point2 &p_to);
  239. virtual Point2 get_mouse_position() const;
  240. void update_real_mouse_position();
  241. virtual int get_mouse_button_state() const;
  242. virtual void set_window_title(const String &p_title);
  243. virtual void set_video_mode(const VideoMode &p_video_mode, int p_screen = 0);
  244. virtual VideoMode get_video_mode(int p_screen = 0) const;
  245. virtual void get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen = 0) const;
  246. virtual int get_screen_count() const;
  247. virtual int get_current_screen() const;
  248. virtual void set_current_screen(int p_screen);
  249. virtual Point2 get_screen_position(int p_screen = -1) const;
  250. virtual Size2 get_screen_size(int p_screen = -1) const;
  251. virtual int get_screen_dpi(int p_screen = -1) const;
  252. virtual Point2 get_window_position() const;
  253. virtual void set_window_position(const Point2 &p_position);
  254. virtual Size2 get_window_size() const;
  255. virtual Size2 get_real_window_size() const;
  256. virtual Size2 get_max_window_size() const;
  257. virtual Size2 get_min_window_size() const;
  258. virtual void set_min_window_size(const Size2 p_size);
  259. virtual void set_max_window_size(const Size2 p_size);
  260. virtual void set_window_size(const Size2 p_size);
  261. virtual void set_window_fullscreen(bool p_enabled);
  262. virtual bool is_window_fullscreen() const;
  263. virtual void set_window_resizable(bool p_enabled);
  264. virtual bool is_window_resizable() const;
  265. virtual void set_window_minimized(bool p_enabled);
  266. virtual bool is_window_minimized() const;
  267. virtual void set_window_maximized(bool p_enabled);
  268. virtual bool is_window_maximized() const;
  269. virtual void set_window_always_on_top(bool p_enabled);
  270. virtual bool is_window_always_on_top() const;
  271. virtual bool is_window_focused() const;
  272. virtual void set_console_visible(bool p_enabled);
  273. virtual bool is_console_visible() const;
  274. virtual void request_attention();
  275. virtual void set_borderless_window(bool p_borderless);
  276. virtual bool get_borderless_window();
  277. virtual bool get_window_per_pixel_transparency_enabled() const;
  278. virtual void set_window_per_pixel_transparency_enabled(bool p_enabled);
  279. virtual uint8_t *get_layered_buffer_data();
  280. virtual Size2 get_layered_buffer_size();
  281. virtual void swap_layered_buffer();
  282. virtual Error open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path = false);
  283. virtual Error close_dynamic_library(void *p_library_handle);
  284. virtual Error get_dynamic_library_symbol_handle(void *p_library_handle, const String p_name, void *&p_symbol_handle, bool p_optional = false);
  285. virtual MainLoop *get_main_loop() const;
  286. virtual String get_name() const;
  287. virtual Date get_date(bool utc) const;
  288. virtual Time get_time(bool utc) const;
  289. virtual TimeZoneInfo get_time_zone_info() const;
  290. virtual uint64_t get_unix_time() const;
  291. virtual uint64_t get_system_time_secs() const;
  292. virtual uint64_t get_system_time_msecs() const;
  293. virtual bool can_draw() const;
  294. virtual Error set_cwd(const String &p_cwd);
  295. virtual void delay_usec(uint32_t p_usec) const;
  296. virtual uint64_t get_ticks_usec() const;
  297. 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);
  298. virtual Error kill(const ProcessID &p_pid);
  299. virtual int get_process_id() const;
  300. virtual bool has_environment(const String &p_var) const;
  301. virtual String get_environment(const String &p_var) const;
  302. virtual bool set_environment(const String &p_var, const String &p_value) const;
  303. virtual void set_clipboard(const String &p_text);
  304. virtual String get_clipboard() const;
  305. void set_cursor_shape(CursorShape p_shape);
  306. CursorShape get_cursor_shape() const;
  307. virtual void set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot);
  308. void GetMaskBitmaps(HBITMAP hSourceBitmap, COLORREF clrTransparent, OUT HBITMAP &hAndMaskBitmap, OUT HBITMAP &hXorMaskBitmap);
  309. void set_native_icon(const String &p_filename);
  310. void set_icon(const Ref<Image> &p_icon);
  311. virtual String get_executable_path() const;
  312. virtual String get_locale() const;
  313. virtual int get_processor_count() const;
  314. virtual LatinKeyboardVariant get_latin_keyboard_variant() const;
  315. virtual void enable_for_stealing_focus(ProcessID pid);
  316. virtual void move_window_to_foreground();
  317. virtual String get_config_path() const;
  318. virtual String get_data_path() const;
  319. virtual String get_cache_path() const;
  320. virtual String get_godot_dir_name() const;
  321. virtual String get_system_dir(SystemDir p_dir) const;
  322. virtual String get_user_data_dir() const;
  323. virtual String get_unique_id() const;
  324. virtual void set_ime_active(const bool p_active);
  325. virtual void set_ime_position(const Point2 &p_pos);
  326. virtual void release_rendering_thread();
  327. virtual void make_rendering_thread();
  328. virtual void swap_buffers();
  329. virtual Error shell_open(String p_uri);
  330. void run();
  331. virtual bool get_swap_ok_cancel() { return true; }
  332. virtual bool is_joy_known(int p_device);
  333. virtual String get_joy_guid(int p_device) const;
  334. virtual void _set_use_vsync(bool p_enable);
  335. //virtual bool is_vsync_enabled() const;
  336. virtual OS::PowerState get_power_state();
  337. virtual int get_power_seconds_left();
  338. virtual int get_power_percent_left();
  339. virtual bool _check_internal_feature_support(const String &p_feature);
  340. void disable_crash_handler();
  341. bool is_disable_crash_handler() const;
  342. virtual void initialize_debugging();
  343. void force_process_input();
  344. virtual Error move_to_trash(const String &p_path);
  345. virtual void process_and_drop_events();
  346. OS_Windows(HINSTANCE _hInstance);
  347. ~OS_Windows();
  348. };
  349. #endif