os_uwp.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*************************************************************************/
  2. /* os_uwp.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_UWP_H
  31. #define OS_UWP_H
  32. #include "context_egl_uwp.h"
  33. #include "core/math/transform_2d.h"
  34. #include "core/os/input.h"
  35. #include "core/os/os.h"
  36. #include "core/ustring.h"
  37. #include "drivers/xaudio2/audio_driver_xaudio2.h"
  38. #include "joypad_uwp.h"
  39. #include "main/input_default.h"
  40. #include "power_uwp.h"
  41. #include "servers/audio_server.h"
  42. #include "servers/visual/rasterizer.h"
  43. #include "servers/visual_server.h"
  44. #include <fcntl.h>
  45. #include <io.h>
  46. #include <stdio.h>
  47. #define WIN32_LEAN_AND_MEAN
  48. #include <windows.h>
  49. class OS_UWP : public OS {
  50. public:
  51. struct KeyEvent {
  52. enum MessageType {
  53. KEY_EVENT_MESSAGE,
  54. CHAR_EVENT_MESSAGE
  55. };
  56. bool alt, shift, control;
  57. MessageType type;
  58. bool pressed;
  59. unsigned int scancode;
  60. unsigned int physical_scancode;
  61. unsigned int unicode;
  62. bool echo;
  63. CorePhysicalKeyStatus status;
  64. };
  65. private:
  66. enum {
  67. JOYPADS_MAX = 8,
  68. JOY_AXIS_COUNT = 6,
  69. MAX_JOY_AXIS = 32768, // I've no idea
  70. KEY_EVENT_BUFFER_SIZE = 512
  71. };
  72. FILE *stdo;
  73. KeyEvent key_event_buffer[KEY_EVENT_BUFFER_SIZE];
  74. int key_event_pos;
  75. uint64_t ticks_start;
  76. uint64_t ticks_per_second;
  77. bool minimized;
  78. bool old_invalid;
  79. bool outside;
  80. int old_x, old_y;
  81. Point2i center;
  82. VisualServer *visual_server;
  83. int pressrc;
  84. ContextEGL_UWP *gl_context;
  85. Windows::UI::Core::CoreWindow ^ window;
  86. VideoMode video_mode;
  87. int video_driver_index;
  88. MainLoop *main_loop;
  89. AudioDriverXAudio2 audio_driver;
  90. PowerUWP *power_manager;
  91. MouseMode mouse_mode;
  92. bool alt_mem;
  93. bool gr_mem;
  94. bool shift_mem;
  95. bool control_mem;
  96. bool meta_mem;
  97. bool force_quit;
  98. uint32_t last_button_state;
  99. CursorShape cursor_shape;
  100. InputDefault *input;
  101. JoypadUWP ^ joypad;
  102. Windows::System::Display::DisplayRequest ^ display_request;
  103. void _post_dpad(DWORD p_dpad, int p_device, bool p_pressed);
  104. void _drag_event(int idx, UINT uMsg, WPARAM wParam, LPARAM lParam);
  105. void _touch_event(int idx, UINT uMsg, WPARAM wParam, LPARAM lParam);
  106. ref class ManagedType {
  107. public:
  108. property bool alert_close_handle;
  109. property Platform::String ^ clipboard;
  110. void alert_close(Windows::UI::Popups::IUICommand ^ command);
  111. void on_clipboard_changed(Platform::Object ^ sender, Platform::Object ^ ev);
  112. void update_clipboard();
  113. void on_accelerometer_reading_changed(Windows::Devices::Sensors::Accelerometer ^ sender, Windows::Devices::Sensors::AccelerometerReadingChangedEventArgs ^ args);
  114. void on_magnetometer_reading_changed(Windows::Devices::Sensors::Magnetometer ^ sender, Windows::Devices::Sensors::MagnetometerReadingChangedEventArgs ^ args);
  115. void on_gyroscope_reading_changed(Windows::Devices::Sensors::Gyrometer ^ sender, Windows::Devices::Sensors::GyrometerReadingChangedEventArgs ^ args);
  116. /** clang-format breaks this, it does not understand this token. */
  117. /* clang-format off */
  118. internal:
  119. ManagedType() { alert_close_handle = false; }
  120. property OS_UWP* os;
  121. /* clang-format on */
  122. };
  123. ManagedType ^ managed_object;
  124. Windows::Devices::Sensors::Accelerometer ^ accelerometer;
  125. Windows::Devices::Sensors::Magnetometer ^ magnetometer;
  126. Windows::Devices::Sensors::Gyrometer ^ gyrometer;
  127. // functions used by main to initialize/deinitialize the OS
  128. protected:
  129. virtual int get_video_driver_count() const;
  130. virtual int get_current_video_driver() const;
  131. virtual void initialize_core();
  132. virtual Error initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver);
  133. virtual void set_main_loop(MainLoop *p_main_loop);
  134. virtual void delete_main_loop();
  135. virtual void finalize();
  136. virtual void finalize_core();
  137. void process_events();
  138. void process_key_events();
  139. public:
  140. // Event to send to the app wrapper
  141. HANDLE mouse_mode_changed;
  142. virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
  143. String get_stdin_string(bool p_block);
  144. void set_mouse_mode(MouseMode p_mode);
  145. MouseMode get_mouse_mode() const;
  146. virtual Point2 get_mouse_position() const;
  147. virtual int get_mouse_button_state() const;
  148. virtual void set_window_title(const String &p_title);
  149. virtual void set_video_mode(const VideoMode &p_video_mode, int p_screen = 0);
  150. virtual VideoMode get_video_mode(int p_screen = 0) const;
  151. virtual void get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen = 0) const;
  152. virtual Size2 get_window_size() const;
  153. virtual void set_window_size(const Size2 p_size);
  154. virtual void set_window_fullscreen(bool p_enabled);
  155. virtual bool is_window_fullscreen() const;
  156. virtual void set_keep_screen_on(bool p_enabled);
  157. virtual MainLoop *get_main_loop() const;
  158. virtual String get_name() const;
  159. virtual Date get_date(bool utc) const;
  160. virtual Time get_time(bool utc) const;
  161. virtual TimeZoneInfo get_time_zone_info() const;
  162. virtual uint64_t get_unix_time() const;
  163. virtual bool can_draw() const;
  164. virtual Error set_cwd(const String &p_cwd);
  165. virtual void delay_usec(uint32_t p_usec) const;
  166. virtual uint64_t get_ticks_usec() const;
  167. 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);
  168. virtual Error kill(const ProcessID &p_pid);
  169. virtual bool has_environment(const String &p_var) const;
  170. virtual String get_environment(const String &p_var) const;
  171. virtual bool set_environment(const String &p_var, const String &p_value) const;
  172. virtual void set_clipboard(const String &p_text);
  173. virtual String get_clipboard() const;
  174. void set_cursor_shape(CursorShape p_shape);
  175. CursorShape get_cursor_shape() const;
  176. virtual void set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot);
  177. void set_icon(const Ref<Image> &p_icon);
  178. virtual String get_executable_path() const;
  179. virtual String get_locale() const;
  180. virtual void move_window_to_foreground();
  181. virtual String get_user_data_dir() const;
  182. virtual bool _check_internal_feature_support(const String &p_feature);
  183. void set_window(Windows::UI::Core::CoreWindow ^ p_window);
  184. void screen_size_changed();
  185. virtual void release_rendering_thread();
  186. virtual void make_rendering_thread();
  187. virtual void swap_buffers();
  188. virtual bool has_touchscreen_ui_hint() const;
  189. virtual bool has_virtual_keyboard() const;
  190. virtual void show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect = Rect2(), bool p_multiline = false, int p_max_input_length = -1, int p_cursor_start = -1, int p_cursor_end = -1);
  191. virtual void hide_virtual_keyboard();
  192. virtual Error open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path = false);
  193. virtual Error close_dynamic_library(void *p_library_handle);
  194. virtual Error get_dynamic_library_symbol_handle(void *p_library_handle, const String p_name, void *&p_symbol_handle, bool p_optional = false);
  195. virtual Error shell_open(String p_uri);
  196. void run();
  197. virtual bool get_swap_ok_cancel() { return true; }
  198. void input_event(const Ref<InputEvent> &p_event);
  199. virtual OS::PowerState get_power_state();
  200. virtual int get_power_seconds_left();
  201. virtual int get_power_percent_left();
  202. void queue_key_event(KeyEvent &p_event);
  203. OS_UWP();
  204. ~OS_UWP();
  205. };
  206. #endif