display_server_macos.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /**************************************************************************/
  2. /* display_server_macos.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 DISPLAY_SERVER_MACOS_H
  31. #define DISPLAY_SERVER_MACOS_H
  32. #include "core/input/input.h"
  33. #include "servers/display_server.h"
  34. #if defined(GLES3_ENABLED)
  35. #include "gl_manager_macos_angle.h"
  36. #include "gl_manager_macos_legacy.h"
  37. #endif // GLES3_ENABLED
  38. #include "native_menu_macos.h"
  39. #if defined(RD_ENABLED)
  40. #include "servers/rendering/rendering_device.h"
  41. #if defined(VULKAN_ENABLED)
  42. #include "rendering_context_driver_vulkan_macos.h"
  43. #endif // VULKAN_ENABLED
  44. #if defined(METAL_ENABLED)
  45. #include "drivers/metal/rendering_context_driver_metal.h"
  46. #endif
  47. #endif // RD_ENABLED
  48. #define BitMap _QDBitMap // Suppress deprecated QuickDraw definition.
  49. #import <AppKit/AppKit.h>
  50. #import <AppKit/NSCursor.h>
  51. #import <ApplicationServices/ApplicationServices.h>
  52. #import <CoreVideo/CoreVideo.h>
  53. #import <Foundation/Foundation.h>
  54. #import <IOKit/pwr_mgt/IOPMLib.h>
  55. #undef BitMap
  56. #undef CursorShape
  57. class DisplayServerMacOS : public DisplayServer {
  58. // No need to register with GDCLASS, it's platform-specific and nothing is added.
  59. _THREAD_SAFE_CLASS_
  60. public:
  61. struct KeyEvent {
  62. WindowID window_id = INVALID_WINDOW_ID;
  63. unsigned int macos_state = false;
  64. bool pressed = false;
  65. bool echo = false;
  66. bool raw = false;
  67. Key keycode = Key::NONE;
  68. Key physical_keycode = Key::NONE;
  69. Key key_label = Key::NONE;
  70. uint32_t unicode = 0;
  71. KeyLocation location = KeyLocation::UNSPECIFIED;
  72. };
  73. struct WindowData {
  74. id window_delegate;
  75. id window_object;
  76. id window_view;
  77. id window_button_view;
  78. Vector<Vector2> mpath;
  79. Point2i mouse_pos;
  80. Size2i min_size;
  81. Size2i max_size;
  82. Size2i size;
  83. Vector2i wb_offset = Vector2i(14, 14);
  84. NSRect last_frame_rect;
  85. bool im_active = false;
  86. Size2i im_position;
  87. Callable rect_changed_callback;
  88. Callable event_callback;
  89. Callable input_event_callback;
  90. Callable input_text_callback;
  91. Callable drop_files_callback;
  92. ObjectID instance_id;
  93. bool fs_transition = false;
  94. bool initial_size = true;
  95. WindowID transient_parent = INVALID_WINDOW_ID;
  96. bool exclusive = false;
  97. HashSet<WindowID> transient_children;
  98. bool layered_window = false;
  99. bool fullscreen = false;
  100. bool exclusive_fullscreen = false;
  101. bool on_top = false;
  102. bool borderless = false;
  103. bool resize_disabled = false;
  104. bool no_focus = false;
  105. bool is_popup = false;
  106. bool mpass = false;
  107. bool focused = false;
  108. bool is_visible = true;
  109. bool extend_to_title = false;
  110. Rect2i parent_safe_rect;
  111. };
  112. List<WindowID> popup_list;
  113. uint64_t time_since_popup = 0;
  114. private:
  115. #if defined(GLES3_ENABLED)
  116. GLManagerLegacy_MacOS *gl_manager_legacy = nullptr;
  117. GLManagerANGLE_MacOS *gl_manager_angle = nullptr;
  118. #endif
  119. #if defined(RD_ENABLED)
  120. RenderingContextDriver *rendering_context = nullptr;
  121. RenderingDevice *rendering_device = nullptr;
  122. #endif
  123. String rendering_driver;
  124. struct WarpEvent {
  125. NSTimeInterval timestamp;
  126. NSPoint delta;
  127. };
  128. List<WarpEvent> warp_events;
  129. NSTimeInterval last_warp = 0;
  130. bool ignore_warp = false;
  131. Vector<KeyEvent> key_event_buffer;
  132. int key_event_pos = 0;
  133. id tts = nullptr;
  134. id menu_delegate = nullptr;
  135. NativeMenuMacOS *native_menu = nullptr;
  136. Point2i im_selection;
  137. String im_text;
  138. CGEventSourceRef event_source;
  139. MouseMode mouse_mode = MOUSE_MODE_VISIBLE;
  140. bool drop_events = false;
  141. bool in_dispatch_input_event = false;
  142. struct LayoutInfo {
  143. String name;
  144. String code;
  145. };
  146. Vector<LayoutInfo> kbd_layouts;
  147. int current_layout = 0;
  148. bool keyboard_layout_dirty = true;
  149. WindowID window_mouseover_id = INVALID_WINDOW_ID;
  150. WindowID last_focused_window = INVALID_WINDOW_ID;
  151. WindowID window_id_counter = MAIN_WINDOW_ID;
  152. float display_max_scale = 1.f;
  153. Point2i origin;
  154. bool displays_arrangement_dirty = true;
  155. bool is_resizing = false;
  156. CursorShape cursor_shape = CURSOR_ARROW;
  157. NSCursor *cursors[CURSOR_MAX];
  158. HashMap<CursorShape, Vector<Variant>> cursors_cache;
  159. HashMap<WindowID, WindowData> windows;
  160. struct IndicatorData {
  161. id delegate;
  162. id item;
  163. };
  164. IndicatorID indicator_id_counter = 0;
  165. HashMap<IndicatorID, IndicatorData> indicators;
  166. IOPMAssertionID screen_keep_on_assertion = kIOPMNullAssertionID;
  167. Callable help_search_callback;
  168. Callable help_action_callback;
  169. struct MenuCall {
  170. Variant tag;
  171. Callable callback;
  172. };
  173. List<MenuCall> deferred_menu_calls;
  174. Callable system_theme_changed;
  175. WindowID _create_window(WindowMode p_mode, VSyncMode p_vsync_mode, const Rect2i &p_rect);
  176. void _update_window_style(WindowData p_wd);
  177. void _update_displays_arrangement();
  178. Point2i _get_native_screen_position(int p_screen) const;
  179. static void _displays_arrangement_changed(CGDirectDisplayID display_id, CGDisplayChangeSummaryFlags flags, void *user_info);
  180. static void _dispatch_input_events(const Ref<InputEvent> &p_event);
  181. void _dispatch_input_event(const Ref<InputEvent> &p_event);
  182. void _push_input(const Ref<InputEvent> &p_event);
  183. void _process_key_events();
  184. void _update_keyboard_layouts();
  185. static void _keyboard_layout_changed(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef user_info);
  186. static NSCursor *_cursor_from_selector(SEL p_selector, SEL p_fallback = nil);
  187. Error _file_dialog_with_options_show(const String &p_title, const String &p_current_directory, const String &p_root, const String &p_filename, bool p_show_hidden, FileDialogMode p_mode, const Vector<String> &p_filters, const TypedArray<Dictionary> &p_options, const Callable &p_callback, bool p_options_in_cb);
  188. public:
  189. void menu_callback(id p_sender);
  190. void emit_system_theme_changed();
  191. bool has_window(WindowID p_window) const;
  192. WindowData &get_window(WindowID p_window);
  193. NSImage *_convert_to_nsimg(Ref<Image> &p_image) const;
  194. Point2i _get_screens_origin() const;
  195. void set_menu_delegate(NSMenu *p_menu);
  196. void send_event(NSEvent *p_event);
  197. void send_window_event(const WindowData &p_wd, WindowEvent p_event);
  198. void release_pressed_events();
  199. void get_key_modifier_state(unsigned int p_macos_state, Ref<InputEventWithModifiers> r_state) const;
  200. void update_mouse_pos(WindowData &p_wd, NSPoint p_location_in_window);
  201. void push_to_key_event_buffer(const KeyEvent &p_event);
  202. void pop_last_key_event();
  203. void update_im_text(const Point2i &p_selection, const String &p_text);
  204. void set_last_focused_window(WindowID p_window);
  205. bool mouse_process_popups(bool p_close = false);
  206. void popup_open(WindowID p_window);
  207. void popup_close(WindowID p_window);
  208. void set_is_resizing(bool p_is_resizing);
  209. bool get_is_resizing() const;
  210. void reparent_check(WindowID p_window);
  211. WindowID _get_focused_window_or_popup() const;
  212. void mouse_enter_window(WindowID p_window);
  213. void mouse_exit_window(WindowID p_window);
  214. void update_presentation_mode();
  215. void window_destroy(WindowID p_window);
  216. void window_resize(WindowID p_window, int p_width, int p_height);
  217. void window_set_custom_window_buttons(WindowData &p_wd, bool p_enabled);
  218. void set_window_per_pixel_transparency_enabled(bool p_enabled, WindowID p_window);
  219. virtual bool has_feature(Feature p_feature) const override;
  220. virtual String get_name() const override;
  221. virtual void help_set_search_callbacks(const Callable &p_search_callback = Callable(), const Callable &p_action_callback = Callable()) override;
  222. Callable _help_get_search_callback() const;
  223. Callable _help_get_action_callback() const;
  224. virtual bool tts_is_speaking() const override;
  225. virtual bool tts_is_paused() const override;
  226. virtual TypedArray<Dictionary> tts_get_voices() const override;
  227. virtual void tts_speak(const String &p_text, const String &p_voice, int p_volume = 50, float p_pitch = 1.f, float p_rate = 1.f, int p_utterance_id = 0, bool p_interrupt = false) override;
  228. virtual void tts_pause() override;
  229. virtual void tts_resume() override;
  230. virtual void tts_stop() override;
  231. virtual bool is_dark_mode_supported() const override;
  232. virtual bool is_dark_mode() const override;
  233. virtual Color get_accent_color() const override;
  234. virtual Color get_base_color() const override;
  235. virtual void set_system_theme_change_callback(const Callable &p_callable) override;
  236. virtual Error dialog_show(String p_title, String p_description, Vector<String> p_buttons, const Callable &p_callback) override;
  237. virtual Error dialog_input_text(String p_title, String p_description, String p_partial, const Callable &p_callback) override;
  238. virtual Error file_dialog_show(const String &p_title, const String &p_current_directory, const String &p_filename, bool p_show_hidden, FileDialogMode p_mode, const Vector<String> &p_filters, const Callable &p_callback) override;
  239. virtual Error file_dialog_with_options_show(const String &p_title, const String &p_current_directory, const String &p_root, const String &p_filename, bool p_show_hidden, FileDialogMode p_mode, const Vector<String> &p_filters, const TypedArray<Dictionary> &p_options, const Callable &p_callback) override;
  240. virtual void mouse_set_mode(MouseMode p_mode) override;
  241. virtual MouseMode mouse_get_mode() const override;
  242. bool update_mouse_wrap(WindowData &p_wd, NSPoint &r_delta, NSPoint &r_mpos, NSTimeInterval p_timestamp);
  243. virtual void warp_mouse(const Point2i &p_position) override;
  244. virtual Point2i mouse_get_position() const override;
  245. virtual BitField<MouseButtonMask> mouse_get_button_state() const override;
  246. virtual void clipboard_set(const String &p_text) override;
  247. virtual String clipboard_get() const override;
  248. virtual Ref<Image> clipboard_get_image() const override;
  249. virtual bool clipboard_has() const override;
  250. virtual bool clipboard_has_image() const override;
  251. virtual int get_screen_count() const override;
  252. virtual int get_primary_screen() const override;
  253. virtual int get_keyboard_focus_screen() const override;
  254. virtual Point2i screen_get_position(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  255. virtual Size2i screen_get_size(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  256. virtual int screen_get_dpi(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  257. virtual float screen_get_scale(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  258. virtual float screen_get_max_scale() const override;
  259. virtual Rect2i screen_get_usable_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  260. virtual float screen_get_refresh_rate(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  261. virtual Color screen_get_pixel(const Point2i &p_position) const override;
  262. virtual Ref<Image> screen_get_image(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
  263. virtual void screen_set_keep_on(bool p_enable) override;
  264. virtual bool screen_is_kept_on() const override;
  265. virtual Vector<int> get_window_list() const override;
  266. virtual WindowID create_sub_window(WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Rect2i &p_rect = Rect2i(), bool p_exclusive = false, WindowID p_transient_parent = INVALID_WINDOW_ID) override;
  267. virtual void show_window(WindowID p_id) override;
  268. virtual void delete_sub_window(WindowID p_id) override;
  269. virtual WindowID window_get_active_popup() const override;
  270. virtual void window_set_popup_safe_rect(WindowID p_window, const Rect2i &p_rect) override;
  271. virtual Rect2i window_get_popup_safe_rect(WindowID p_window) const override;
  272. virtual void window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  273. virtual void window_set_window_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  274. virtual void window_set_input_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  275. virtual void window_set_input_text_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  276. virtual void window_set_drop_files_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID) override;
  277. virtual void window_set_title(const String &p_title, WindowID p_window = MAIN_WINDOW_ID) override;
  278. virtual Size2i window_get_title_size(const String &p_title, WindowID p_window) const override;
  279. virtual void window_set_mouse_passthrough(const Vector<Vector2> &p_region, WindowID p_window = MAIN_WINDOW_ID) override;
  280. virtual int window_get_current_screen(WindowID p_window = MAIN_WINDOW_ID) const override;
  281. virtual void window_set_current_screen(int p_screen, WindowID p_window = MAIN_WINDOW_ID) override;
  282. virtual Point2i window_get_position(WindowID p_window = MAIN_WINDOW_ID) const override;
  283. virtual Point2i window_get_position_with_decorations(WindowID p_window = MAIN_WINDOW_ID) const override;
  284. virtual void window_set_position(const Point2i &p_position, WindowID p_window = MAIN_WINDOW_ID) override;
  285. virtual void window_set_transient(WindowID p_window, WindowID p_parent) override;
  286. virtual void window_set_exclusive(WindowID p_window, bool p_exclusive) override;
  287. virtual void window_set_max_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
  288. virtual Size2i window_get_max_size(WindowID p_window = MAIN_WINDOW_ID) const override;
  289. virtual void window_set_min_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
  290. virtual Size2i window_get_min_size(WindowID p_window = MAIN_WINDOW_ID) const override;
  291. virtual void window_set_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID) override;
  292. virtual Size2i window_get_size(WindowID p_window = MAIN_WINDOW_ID) const override;
  293. virtual Size2i window_get_size_with_decorations(WindowID p_window = MAIN_WINDOW_ID) const override;
  294. virtual void window_set_mode(WindowMode p_mode, WindowID p_window = MAIN_WINDOW_ID) override;
  295. virtual WindowMode window_get_mode(WindowID p_window = MAIN_WINDOW_ID) const override;
  296. virtual bool window_is_maximize_allowed(WindowID p_window = MAIN_WINDOW_ID) const override;
  297. virtual void window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window = MAIN_WINDOW_ID) override;
  298. virtual bool window_get_flag(WindowFlags p_flag, WindowID p_window = MAIN_WINDOW_ID) const override;
  299. virtual void window_request_attention(WindowID p_window = MAIN_WINDOW_ID) override;
  300. virtual void window_move_to_foreground(WindowID p_window = MAIN_WINDOW_ID) override;
  301. virtual bool window_is_focused(WindowID p_window = MAIN_WINDOW_ID) const override;
  302. virtual WindowID get_focused_window() const override;
  303. virtual bool window_can_draw(WindowID p_window = MAIN_WINDOW_ID) const override;
  304. virtual bool can_any_window_draw() const override;
  305. virtual void window_set_ime_active(const bool p_active, WindowID p_window = MAIN_WINDOW_ID) override;
  306. virtual void window_set_ime_position(const Point2i &p_pos, WindowID p_window = MAIN_WINDOW_ID) override;
  307. virtual WindowID get_window_at_screen_position(const Point2i &p_position) const override;
  308. virtual int64_t window_get_native_handle(HandleType p_handle_type, WindowID p_window = MAIN_WINDOW_ID) const override;
  309. virtual void window_attach_instance_id(ObjectID p_instance, WindowID p_window = MAIN_WINDOW_ID) override;
  310. virtual ObjectID window_get_attached_instance_id(WindowID p_window = MAIN_WINDOW_ID) const override;
  311. virtual void gl_window_make_current(DisplayServer::WindowID p_window_id) override;
  312. virtual void window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window = MAIN_WINDOW_ID) override;
  313. virtual DisplayServer::VSyncMode window_get_vsync_mode(WindowID p_vsync_mode) const override;
  314. virtual bool window_maximize_on_title_dbl_click() const override;
  315. virtual bool window_minimize_on_title_dbl_click() const override;
  316. virtual void window_set_window_buttons_offset(const Vector2i &p_offset, WindowID p_window = MAIN_WINDOW_ID) override;
  317. virtual Vector3i window_get_safe_title_margins(WindowID p_window = MAIN_WINDOW_ID) const override;
  318. virtual Point2i ime_get_selection() const override;
  319. virtual String ime_get_text() const override;
  320. void cursor_update_shape();
  321. virtual void cursor_set_shape(CursorShape p_shape) override;
  322. virtual CursorShape cursor_get_shape() const override;
  323. virtual void cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape = CURSOR_ARROW, const Vector2 &p_hotspot = Vector2()) override;
  324. virtual bool get_swap_cancel_ok() override;
  325. virtual int keyboard_get_layout_count() const override;
  326. virtual int keyboard_get_current_layout() const override;
  327. virtual void keyboard_set_current_layout(int p_index) override;
  328. virtual String keyboard_get_layout_language(int p_index) const override;
  329. virtual String keyboard_get_layout_name(int p_index) const override;
  330. virtual Key keyboard_get_keycode_from_physical(Key p_keycode) const override;
  331. virtual Key keyboard_get_label_from_physical(Key p_keycode) const override;
  332. virtual void process_events() override;
  333. virtual void force_process_and_drop_events() override;
  334. virtual void release_rendering_thread() override;
  335. virtual void swap_buffers() override;
  336. virtual void set_native_icon(const String &p_filename) override;
  337. virtual void set_icon(const Ref<Image> &p_icon) override;
  338. virtual IndicatorID create_status_indicator(const Ref<Texture2D> &p_icon, const String &p_tooltip, const Callable &p_callback) override;
  339. virtual void status_indicator_set_icon(IndicatorID p_id, const Ref<Texture2D> &p_icon) override;
  340. virtual void status_indicator_set_tooltip(IndicatorID p_id, const String &p_tooltip) override;
  341. virtual void status_indicator_set_menu(IndicatorID p_id, const RID &p_menu_rid) override;
  342. virtual void status_indicator_set_callback(IndicatorID p_id, const Callable &p_callback) override;
  343. virtual Rect2 status_indicator_get_rect(IndicatorID p_id) const override;
  344. virtual void delete_status_indicator(IndicatorID p_id) override;
  345. virtual bool is_window_transparency_available() const override;
  346. static DisplayServer *create_func(const String &p_rendering_driver, WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, Context p_context, Error &r_error);
  347. static Vector<String> get_rendering_drivers_func();
  348. static void register_macos_driver();
  349. DisplayServerMacOS(const String &p_rendering_driver, WindowMode p_mode, VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, Context p_context, Error &r_error);
  350. ~DisplayServerMacOS();
  351. };
  352. #endif // DISPLAY_SERVER_MACOS_H