os_windows.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*************************************************************************/
  2. /* os_windows.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_win.h"
  33. #include "core/os/input.h"
  34. #include "core/os/os.h"
  35. #include "core/project_settings.h"
  36. #include "crash_handler_win.h"
  37. #include "drivers/rtaudio/audio_driver_rtaudio.h"
  38. #include "drivers/wasapi/audio_driver_wasapi.h"
  39. #include "drivers/winmidi/win_midi.h"
  40. #include "power_windows.h"
  41. #include "servers/audio_server.h"
  42. #include "servers/visual/rasterizer.h"
  43. #include "servers/visual_server.h"
  44. #ifdef XAUDIO2_ENABLED
  45. #include "drivers/xaudio2/audio_driver_xaudio2.h"
  46. #endif
  47. #include "drivers/unix/ip_unix.h"
  48. #include "key_mapping_win.h"
  49. #include "main/input_default.h"
  50. #include <fcntl.h>
  51. #include <io.h>
  52. #include <stdio.h>
  53. #include <windows.h>
  54. #include <windowsx.h>
  55. /**
  56. @author Juan Linietsky <reduzio@gmail.com>
  57. */
  58. class JoypadWindows;
  59. class OS_Windows : public OS {
  60. enum {
  61. KEY_EVENT_BUFFER_SIZE = 512
  62. };
  63. FILE *stdo;
  64. struct KeyEvent {
  65. bool alt, shift, control, meta;
  66. UINT uMsg;
  67. WPARAM wParam;
  68. LPARAM lParam;
  69. };
  70. KeyEvent key_event_buffer[KEY_EVENT_BUFFER_SIZE];
  71. int key_event_pos;
  72. uint64_t ticks_start;
  73. uint64_t ticks_per_second;
  74. bool old_invalid;
  75. bool outside;
  76. int old_x, old_y;
  77. Point2i center;
  78. #if defined(OPENGL_ENABLED)
  79. ContextGL_Win *gl_context;
  80. #endif
  81. VisualServer *visual_server;
  82. int pressrc;
  83. HDC hDC; // Private GDI Device Context
  84. HINSTANCE hInstance; // Holds The Instance Of The Application
  85. HWND hWnd;
  86. Point2 last_pos;
  87. HBITMAP hBitmap; //DIB section for layered window
  88. uint8_t *dib_data;
  89. Size2 dib_size;
  90. HDC hDC_dib;
  91. bool layered_window;
  92. uint32_t move_timer_id;
  93. HCURSOR hCursor;
  94. Size2 window_rect;
  95. VideoMode video_mode;
  96. bool preserve_window_size = false;
  97. MainLoop *main_loop;
  98. WNDPROC user_proc;
  99. // IME
  100. HIMC im_himc;
  101. Vector2 im_position;
  102. MouseMode mouse_mode;
  103. bool alt_mem;
  104. bool gr_mem;
  105. bool shift_mem;
  106. bool control_mem;
  107. bool meta_mem;
  108. bool force_quit;
  109. bool window_has_focus;
  110. uint32_t last_button_state;
  111. bool use_raw_input;
  112. HCURSOR cursors[CURSOR_MAX] = { NULL };
  113. CursorShape cursor_shape;
  114. InputDefault *input;
  115. JoypadWindows *joypad;
  116. Map<int, Vector2> touch_state;
  117. PowerWindows *power_manager;
  118. int video_driver_index;
  119. #ifdef WASAPI_ENABLED
  120. AudioDriverWASAPI driver_wasapi;
  121. #endif
  122. #ifdef RTAUDIO_ENABLED
  123. AudioDriverRtAudio driver_rtaudio;
  124. #endif
  125. #ifdef XAUDIO2_ENABLED
  126. AudioDriverXAudio2 driver_xaudio2;
  127. #endif
  128. #ifdef WINMIDI_ENABLED
  129. MIDIDriverWinMidi driver_midi;
  130. #endif
  131. CrashHandler crash_handler;
  132. void _drag_event(float p_x, float p_y, int idx);
  133. void _touch_event(bool p_pressed, float p_x, float p_y, int idx);
  134. void _update_window_style(bool repaint = true);
  135. void _set_mouse_mode_impl(MouseMode p_mode);
  136. // functions used by main to initialize/deinitialize the OS
  137. protected:
  138. virtual int get_current_video_driver() const;
  139. virtual void initialize_core();
  140. virtual Error initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver);
  141. virtual void set_main_loop(MainLoop *p_main_loop);
  142. virtual void delete_main_loop();
  143. virtual void finalize();
  144. virtual void finalize_core();
  145. void process_events();
  146. void process_key_events();
  147. struct ProcessInfo {
  148. STARTUPINFO si;
  149. PROCESS_INFORMATION pi;
  150. };
  151. Map<ProcessID, ProcessInfo> *process_map;
  152. bool pre_fs_valid;
  153. RECT pre_fs_rect;
  154. bool maximized;
  155. bool minimized;
  156. bool borderless;
  157. public:
  158. LRESULT WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  159. virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
  160. String get_stdin_string(bool p_block);
  161. void set_mouse_mode(MouseMode p_mode);
  162. MouseMode get_mouse_mode() const;
  163. virtual void warp_mouse_position(const Point2 &p_to);
  164. virtual Point2 get_mouse_position() const;
  165. virtual int get_mouse_button_state() const;
  166. virtual void set_window_title(const String &p_title);
  167. virtual void set_video_mode(const VideoMode &p_video_mode, int p_screen = 0);
  168. virtual VideoMode get_video_mode(int p_screen = 0) const;
  169. virtual void get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen = 0) const;
  170. virtual int get_screen_count() const;
  171. virtual int get_current_screen() const;
  172. virtual void set_current_screen(int p_screen);
  173. virtual Point2 get_screen_position(int p_screen = -1) const;
  174. virtual Size2 get_screen_size(int p_screen = -1) const;
  175. virtual int get_screen_dpi(int p_screen = -1) const;
  176. virtual Point2 get_window_position() const;
  177. virtual void set_window_position(const Point2 &p_position);
  178. virtual Size2 get_window_size() const;
  179. virtual Size2 get_real_window_size() const;
  180. virtual void set_window_size(const Size2 p_size);
  181. virtual void set_window_fullscreen(bool p_enabled);
  182. virtual bool is_window_fullscreen() const;
  183. virtual void set_window_resizable(bool p_enabled);
  184. virtual bool is_window_resizable() const;
  185. virtual void set_window_minimized(bool p_enabled);
  186. virtual bool is_window_minimized() const;
  187. virtual void set_window_maximized(bool p_enabled);
  188. virtual bool is_window_maximized() const;
  189. virtual void set_window_always_on_top(bool p_enabled);
  190. virtual bool is_window_always_on_top() const;
  191. virtual void request_attention();
  192. virtual void set_borderless_window(bool p_borderless);
  193. virtual bool get_borderless_window();
  194. virtual bool get_window_per_pixel_transparency_enabled() const;
  195. virtual void set_window_per_pixel_transparency_enabled(bool p_enabled);
  196. virtual uint8_t *get_layered_buffer_data();
  197. virtual Size2 get_layered_buffer_size();
  198. virtual void swap_layered_buffer();
  199. virtual Error open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path = false);
  200. virtual Error close_dynamic_library(void *p_library_handle);
  201. virtual Error get_dynamic_library_symbol_handle(void *p_library_handle, const String p_name, void *&p_symbol_handle, bool p_optional = false);
  202. virtual MainLoop *get_main_loop() const;
  203. virtual String get_name();
  204. virtual Date get_date(bool utc) const;
  205. virtual Time get_time(bool utc) const;
  206. virtual TimeZoneInfo get_time_zone_info() const;
  207. virtual uint64_t get_unix_time() const;
  208. virtual uint64_t get_system_time_secs() const;
  209. virtual uint64_t get_system_time_msecs() const;
  210. virtual bool can_draw() const;
  211. virtual Error set_cwd(const String &p_cwd);
  212. virtual void delay_usec(uint32_t p_usec) const;
  213. virtual uint64_t get_ticks_usec() const;
  214. virtual Error execute(const String &p_path, const List<String> &p_arguments, bool p_blocking, ProcessID *r_child_id = NULL, String *r_pipe = NULL, int *r_exitcode = NULL, bool read_stderr = false);
  215. virtual Error kill(const ProcessID &p_pid);
  216. virtual int get_process_id() const;
  217. virtual bool has_environment(const String &p_var) const;
  218. virtual String get_environment(const String &p_var) const;
  219. virtual void set_clipboard(const String &p_text);
  220. virtual String get_clipboard() const;
  221. void set_cursor_shape(CursorShape p_shape);
  222. virtual void set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot);
  223. void GetMaskBitmaps(HBITMAP hSourceBitmap, COLORREF clrTransparent, OUT HBITMAP &hAndMaskBitmap, OUT HBITMAP &hXorMaskBitmap);
  224. void set_icon(const Ref<Image> &p_icon);
  225. virtual String get_executable_path() const;
  226. virtual String get_locale() const;
  227. virtual int get_processor_count() const;
  228. virtual LatinKeyboardVariant get_latin_keyboard_variant() const;
  229. virtual void enable_for_stealing_focus(ProcessID pid);
  230. virtual void move_window_to_foreground();
  231. virtual String get_config_path() const;
  232. virtual String get_data_path() const;
  233. virtual String get_cache_path() const;
  234. virtual String get_godot_dir_name() const;
  235. virtual String get_system_dir(SystemDir p_dir) const;
  236. virtual String get_user_data_dir() const;
  237. virtual String get_unique_id() const;
  238. virtual void set_ime_active(const bool p_active);
  239. virtual void set_ime_position(const Point2 &p_pos);
  240. virtual void release_rendering_thread();
  241. virtual void make_rendering_thread();
  242. virtual void swap_buffers();
  243. virtual Error shell_open(String p_uri);
  244. void run();
  245. virtual bool get_swap_ok_cancel() { return true; }
  246. virtual bool is_joy_known(int p_device);
  247. virtual String get_joy_guid(int p_device) const;
  248. virtual void _set_use_vsync(bool p_enable);
  249. //virtual bool is_vsync_enabled() const;
  250. virtual OS::PowerState get_power_state();
  251. virtual int get_power_seconds_left();
  252. virtual int get_power_percent_left();
  253. virtual bool _check_internal_feature_support(const String &p_feature);
  254. void disable_crash_handler();
  255. bool is_disable_crash_handler() const;
  256. virtual void initialize_debugging();
  257. void force_process_input();
  258. virtual Error move_to_trash(const String &p_path);
  259. OS_Windows(HINSTANCE _hInstance);
  260. ~OS_Windows();
  261. };
  262. #endif