os_osx.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /**************************************************************************/
  2. /* os_osx.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 OS_OSX_H
  31. #define OS_OSX_H
  32. #define BitMap _QDBitMap // Suppress deprecated QuickDraw definition.
  33. #include "core/os/input.h"
  34. #include "crash_handler_osx.h"
  35. #include "drivers/coreaudio/audio_driver_coreaudio.h"
  36. #include "drivers/coremidi/midi_driver_coremidi.h"
  37. #include "drivers/unix/os_unix.h"
  38. #include "joypad_osx.h"
  39. #include "main/input_default.h"
  40. #include "power_osx.h"
  41. #include "servers/audio_server.h"
  42. #include "servers/visual/rasterizer.h"
  43. #include "servers/visual/visual_server_wrap_mt.h"
  44. #include "servers/visual_server.h"
  45. #include <AppKit/AppKit.h>
  46. #include <AppKit/NSCursor.h>
  47. #include <ApplicationServices/ApplicationServices.h>
  48. #include <CoreVideo/CoreVideo.h>
  49. #undef BitMap
  50. #undef CursorShape
  51. class OS_OSX : public OS_Unix {
  52. public:
  53. struct KeyEvent {
  54. unsigned int osx_state;
  55. bool pressed;
  56. bool echo;
  57. bool raw;
  58. uint32_t scancode;
  59. uint32_t physical_scancode;
  60. uint32_t unicode;
  61. };
  62. struct WarpEvent {
  63. NSTimeInterval timestamp;
  64. NSPoint delta;
  65. };
  66. List<WarpEvent> warp_events;
  67. NSTimeInterval last_warp = 0;
  68. bool ignore_warp = false;
  69. bool last_pen_inverted = false;
  70. Vector<KeyEvent> key_event_buffer;
  71. int key_event_pos;
  72. bool force_quit;
  73. bool is_resizing = false;
  74. // rasterizer seems to no longer be given to visual server, its using GLES3 directly?
  75. //Rasterizer *rasterizer;
  76. VisualServer *visual_server;
  77. List<String> args;
  78. MainLoop *main_loop;
  79. #ifdef COREAUDIO_ENABLED
  80. AudioDriverCoreAudio audio_driver;
  81. #endif
  82. #ifdef COREMIDI_ENABLED
  83. MIDIDriverCoreMidi midi_driver;
  84. #endif
  85. InputDefault *input;
  86. JoypadOSX *joypad_osx;
  87. /* objc */
  88. CGEventSourceRef eventSource;
  89. void process_events();
  90. void process_key_events();
  91. void *framework;
  92. // pthread_key_t current;
  93. bool mouse_grab;
  94. Point2 mouse_pos;
  95. id delegate;
  96. id window_delegate;
  97. id window_object;
  98. id window_view;
  99. id autoreleasePool;
  100. id cursor;
  101. NSOpenGLPixelFormat *pixelFormat;
  102. NSOpenGLContext *context;
  103. NSOpenGLContext *context_offscreen;
  104. Vector<Vector2> mpath;
  105. bool layered_window;
  106. CursorShape cursor_shape;
  107. NSCursor *cursors[CURSOR_MAX];
  108. Map<CursorShape, Vector<Variant>> cursors_cache;
  109. MouseMode mouse_mode;
  110. String title;
  111. bool minimized;
  112. bool maximized;
  113. bool zoomed;
  114. bool resizable;
  115. bool window_focused;
  116. bool on_top;
  117. bool is_visible;
  118. Size2 window_size;
  119. Rect2 restore_rect;
  120. String open_with_filename;
  121. Point2 im_position;
  122. bool im_active;
  123. String im_text;
  124. Point2 im_selection;
  125. Size2 min_size;
  126. Size2 max_size;
  127. PowerOSX *power_manager;
  128. id tts = nullptr;
  129. CrashHandler crash_handler;
  130. void _update_window();
  131. int video_driver_index;
  132. virtual int get_current_video_driver() const;
  133. struct GlobalMenuItem {
  134. String label;
  135. Variant signal;
  136. Variant meta;
  137. GlobalMenuItem() {
  138. //NOP
  139. }
  140. GlobalMenuItem(const String &p_label, const Variant &p_signal, const Variant &p_meta) {
  141. label = p_label;
  142. signal = p_signal;
  143. meta = p_meta;
  144. }
  145. };
  146. Map<String, Vector<GlobalMenuItem>> global_menus;
  147. List<String> global_menus_order;
  148. void _update_global_menu();
  149. static void pre_wait_observer_cb(CFRunLoopObserverRef p_observer, CFRunLoopActivity p_activiy, void *p_context);
  150. protected:
  151. virtual void initialize_core();
  152. virtual Error initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver);
  153. virtual void finalize();
  154. virtual void set_main_loop(MainLoop *p_main_loop);
  155. virtual void delete_main_loop();
  156. public:
  157. static OS_OSX *singleton;
  158. virtual bool tts_is_speaking() const;
  159. virtual bool tts_is_paused() const;
  160. virtual Array tts_get_voices() const;
  161. 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);
  162. virtual void tts_pause();
  163. virtual void tts_resume();
  164. virtual void tts_stop();
  165. void global_menu_add_item(const String &p_menu, const String &p_label, const Variant &p_signal, const Variant &p_meta);
  166. void global_menu_add_separator(const String &p_menu);
  167. void global_menu_remove_item(const String &p_menu, int p_idx);
  168. void global_menu_clear(const String &p_menu);
  169. void wm_minimized(bool p_minimized);
  170. virtual String get_name() const;
  171. virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
  172. virtual Error open_dynamic_library(const String p_path, void *&p_library_handle, bool p_also_set_library_path = false);
  173. virtual void set_cursor_shape(CursorShape p_shape);
  174. virtual CursorShape get_cursor_shape() const;
  175. virtual void set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot);
  176. virtual void set_mouse_show(bool p_show);
  177. virtual void set_mouse_grab(bool p_grab);
  178. virtual bool is_mouse_grab_enabled() const;
  179. virtual void warp_mouse_position(const Point2 &p_to);
  180. virtual Point2 get_mouse_position() const;
  181. virtual int get_mouse_button_state() const;
  182. void update_real_mouse_position();
  183. virtual void set_window_title(const String &p_title);
  184. virtual void set_window_mouse_passthrough(const PoolVector2Array &p_region);
  185. virtual Size2 get_window_size() const;
  186. virtual Size2 get_real_window_size() const;
  187. virtual void set_native_icon(const String &p_filename);
  188. virtual void set_icon(const Ref<Image> &p_icon);
  189. virtual MainLoop *get_main_loop() const;
  190. virtual String get_config_path() const;
  191. virtual String get_data_path() const;
  192. virtual String get_cache_path() const;
  193. virtual String get_bundle_resource_dir() const;
  194. virtual String get_bundle_icon_path() const;
  195. virtual String get_godot_dir_name() const;
  196. virtual String get_system_dir(SystemDir p_dir, bool p_shared_storage = true) const;
  197. virtual bool can_draw() const;
  198. virtual void set_clipboard(const String &p_text);
  199. virtual String get_clipboard() const;
  200. virtual void release_rendering_thread();
  201. virtual void make_rendering_thread();
  202. virtual void swap_buffers();
  203. Error shell_open(String p_uri);
  204. void push_input(const Ref<InputEvent> &p_event);
  205. String get_locale() const;
  206. virtual void set_video_mode(const VideoMode &p_video_mode, int p_screen = 0);
  207. virtual VideoMode get_video_mode(int p_screen = 0) const;
  208. virtual void get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen = 0) const;
  209. virtual bool is_offscreen_gl_available() const;
  210. virtual void set_offscreen_gl_current(bool p_current);
  211. virtual String get_executable_path() const;
  212. virtual Error execute(const String &p_path, const List<String> &p_arguments, bool p_blocking = true, ProcessID *r_child_id = nullptr, String *r_pipe = nullptr, int *r_exitcode = nullptr, bool read_stderr = false, Mutex *p_pipe_mutex = nullptr, bool p_open_console = false);
  213. virtual LatinKeyboardVariant get_latin_keyboard_variant() const;
  214. virtual int keyboard_get_layout_count() const;
  215. virtual int keyboard_get_current_layout() const;
  216. virtual void keyboard_set_current_layout(int p_index);
  217. virtual String keyboard_get_layout_language(int p_index) const;
  218. virtual String keyboard_get_layout_name(int p_index) const;
  219. virtual uint32_t keyboard_get_scancode_from_physical(uint32_t p_scancode) const;
  220. virtual void move_window_to_foreground();
  221. virtual int get_screen_count() const;
  222. virtual int get_current_screen() const;
  223. virtual void set_current_screen(int p_screen);
  224. virtual Point2 get_screen_position(int p_screen = -1) const;
  225. virtual Size2 get_screen_size(int p_screen = -1) const;
  226. virtual int get_screen_dpi(int p_screen = -1) const;
  227. virtual float get_screen_scale(int p_screen = -1) const;
  228. virtual float get_screen_max_scale() const;
  229. virtual float get_screen_refresh_rate(int p_screen = -1) const;
  230. virtual Point2 get_window_position() const;
  231. virtual void set_window_position(const Point2 &p_position);
  232. virtual Size2 get_max_window_size() const;
  233. virtual Size2 get_min_window_size() const;
  234. virtual void set_min_window_size(const Size2 p_size);
  235. virtual void set_max_window_size(const Size2 p_size);
  236. virtual void set_window_size(const Size2 p_size);
  237. virtual void set_window_fullscreen(bool p_enabled);
  238. virtual bool is_window_fullscreen() const;
  239. virtual void set_window_resizable(bool p_enabled);
  240. virtual bool is_window_resizable() const;
  241. virtual void set_window_minimized(bool p_enabled);
  242. virtual bool is_window_minimized() const;
  243. virtual void set_window_maximized(bool p_enabled);
  244. virtual bool is_window_maximized() const;
  245. virtual void set_window_always_on_top(bool p_enabled);
  246. virtual bool is_window_always_on_top() const;
  247. virtual bool is_window_focused() const;
  248. virtual void request_attention();
  249. virtual String get_joy_guid(int p_device) const;
  250. virtual void set_borderless_window(bool p_borderless);
  251. virtual bool get_borderless_window();
  252. virtual bool get_window_per_pixel_transparency_enabled() const;
  253. virtual void set_window_per_pixel_transparency_enabled(bool p_enabled);
  254. virtual void set_ime_active(const bool p_active);
  255. virtual void set_ime_position(const Point2 &p_pos);
  256. virtual Point2 get_ime_selection() const;
  257. virtual String get_ime_text() const;
  258. virtual String get_unique_id() const;
  259. virtual String get_processor_name() const;
  260. virtual OS::PowerState get_power_state();
  261. virtual int get_power_seconds_left();
  262. virtual int get_power_percent_left();
  263. virtual bool _check_internal_feature_support(const String &p_feature);
  264. virtual void _set_use_vsync(bool p_enable);
  265. //virtual bool is_vsync_enabled() const;
  266. void run();
  267. void set_mouse_mode(MouseMode p_mode);
  268. MouseMode get_mouse_mode() const;
  269. void disable_crash_handler();
  270. bool is_disable_crash_handler() const;
  271. virtual Error move_to_trash(const String &p_path);
  272. void force_process_input();
  273. OS_OSX();
  274. private:
  275. Point2 get_native_screen_position(int p_screen) const;
  276. Point2 get_native_window_position() const;
  277. void set_native_window_position(const Point2 &p_position);
  278. Point2 get_screens_origin() const;
  279. };
  280. #endif // OS_OSX_H