os_x11.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*************************************************************************/
  2. /* os_x11.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_X11_H
  31. #define OS_X11_H
  32. #include "context_gl_x11.h"
  33. #include "core/local_vector.h"
  34. #include "core/os/input.h"
  35. #include "crash_handler_x11.h"
  36. #include "drivers/alsa/audio_driver_alsa.h"
  37. #include "drivers/alsamidi/midi_driver_alsamidi.h"
  38. #include "drivers/pulseaudio/audio_driver_pulseaudio.h"
  39. #include "drivers/unix/os_unix.h"
  40. #include "joypad_linux.h"
  41. #include "main/input_default.h"
  42. #include "power_x11.h"
  43. #include "servers/audio_server.h"
  44. #include "servers/visual/rasterizer.h"
  45. #include "servers/visual_server.h"
  46. #include <X11/Xcursor/Xcursor.h>
  47. #include <X11/Xlib.h>
  48. #include <X11/extensions/XInput2.h>
  49. #include <X11/extensions/Xrandr.h>
  50. #include <X11/keysym.h>
  51. // Hints for X11 fullscreen
  52. typedef struct {
  53. unsigned long flags;
  54. unsigned long functions;
  55. unsigned long decorations;
  56. long inputMode;
  57. unsigned long status;
  58. } Hints;
  59. typedef struct _xrr_monitor_info {
  60. Atom name;
  61. Bool primary;
  62. Bool automatic;
  63. int noutput;
  64. int x;
  65. int y;
  66. int width;
  67. int height;
  68. int mwidth;
  69. int mheight;
  70. RROutput *outputs;
  71. } xrr_monitor_info;
  72. #undef CursorShape
  73. class OS_X11 : public OS_Unix {
  74. Atom wm_delete;
  75. Atom xdnd_enter;
  76. Atom xdnd_position;
  77. Atom xdnd_status;
  78. Atom xdnd_action_copy;
  79. Atom xdnd_drop;
  80. Atom xdnd_finished;
  81. Atom xdnd_selection;
  82. Atom requested;
  83. int xdnd_version;
  84. #if defined(OPENGL_ENABLED)
  85. ContextGL_X11 *context_gl;
  86. #endif
  87. //Rasterizer *rasterizer;
  88. VisualServer *visual_server;
  89. VideoMode current_videomode;
  90. List<String> args;
  91. Window x11_window;
  92. Window xdnd_source_window;
  93. MainLoop *main_loop;
  94. ::Display *x11_display;
  95. char *xmbstring;
  96. int xmblen;
  97. unsigned long last_timestamp;
  98. ::Time last_keyrelease_time;
  99. ::XIC xic;
  100. ::XIM xim;
  101. ::XIMStyle xim_style;
  102. static void xim_destroy_callback(::XIM im, ::XPointer client_data,
  103. ::XPointer call_data);
  104. // IME
  105. bool im_active;
  106. Vector2 im_position;
  107. Vector2 last_position_before_fs;
  108. Size2 min_size;
  109. Size2 max_size;
  110. Point2 last_mouse_pos;
  111. bool last_mouse_pos_valid;
  112. Point2i last_click_pos;
  113. uint64_t last_click_ms;
  114. int last_click_button_index;
  115. uint32_t last_button_state;
  116. struct {
  117. int opcode;
  118. Vector<int> touch_devices;
  119. Map<int, Vector2> absolute_devices;
  120. Map<int, Vector2> pen_pressure_range;
  121. Map<int, Vector2> pen_tilt_x_range;
  122. Map<int, Vector2> pen_tilt_y_range;
  123. XIEventMask all_event_mask;
  124. XIEventMask all_master_event_mask;
  125. Map<int, Vector2> state;
  126. double pressure;
  127. bool pressure_supported;
  128. Vector2 tilt;
  129. Vector2 mouse_pos_to_filter;
  130. Vector2 relative_motion;
  131. Vector2 raw_pos;
  132. Vector2 old_raw_pos;
  133. ::Time last_relative_time;
  134. } xi;
  135. bool refresh_device_info();
  136. unsigned int get_mouse_button_state(unsigned int p_x11_button, int p_x11_type);
  137. void get_key_modifier_state(unsigned int p_x11_state, Ref<InputEventWithModifiers> state);
  138. void flush_mouse_motion();
  139. MouseMode mouse_mode;
  140. Point2i center;
  141. void _handle_key_event(XKeyEvent *p_event, LocalVector<XEvent> &p_events, uint32_t &p_event_index, bool p_echo = false);
  142. Atom _process_selection_request_target(Atom p_target, Window p_requestor, Atom p_property) const;
  143. void _handle_selection_request_event(XSelectionRequestEvent *p_event) const;
  144. String _get_clipboard_impl(Atom p_source, Window x11_window, Atom target) const;
  145. String _get_clipboard(Atom p_source, Window x11_window) const;
  146. void _clipboard_transfer_ownership(Atom p_source, Window x11_window) const;
  147. mutable Mutex events_mutex;
  148. Thread events_thread;
  149. bool events_thread_done = false;
  150. LocalVector<XEvent> polled_events;
  151. static void _poll_events_thread(void *ud);
  152. bool _wait_for_events() const;
  153. void _poll_events();
  154. void _check_pending_events(LocalVector<XEvent> &r_events);
  155. static Bool _predicate_all_events(Display *display, XEvent *event, XPointer arg);
  156. static Bool _predicate_clipboard_selection(Display *display, XEvent *event, XPointer arg);
  157. static Bool _predicate_clipboard_incr(Display *display, XEvent *event, XPointer arg);
  158. static Bool _predicate_clipboard_save_targets(Display *display, XEvent *event, XPointer arg);
  159. void process_xevents();
  160. virtual void delete_main_loop();
  161. bool force_quit;
  162. bool minimized;
  163. bool window_has_focus;
  164. bool do_mouse_warp;
  165. const char *cursor_theme;
  166. int cursor_size;
  167. XcursorImage *img[CURSOR_MAX];
  168. Cursor cursors[CURSOR_MAX];
  169. Cursor null_cursor;
  170. CursorShape current_cursor;
  171. Map<CursorShape, Vector<Variant>> cursors_cache;
  172. InputDefault *input;
  173. #ifdef JOYDEV_ENABLED
  174. JoypadLinux *joypad;
  175. #endif
  176. #ifdef ALSA_ENABLED
  177. AudioDriverALSA driver_alsa;
  178. #endif
  179. #ifdef ALSAMIDI_ENABLED
  180. MIDIDriverALSAMidi driver_alsamidi;
  181. #endif
  182. #ifdef PULSEAUDIO_ENABLED
  183. AudioDriverPulseAudio driver_pulseaudio;
  184. #endif
  185. PowerX11 *power_manager;
  186. bool layered_window;
  187. CrashHandler crash_handler;
  188. int video_driver_index;
  189. bool maximized;
  190. bool window_focused;
  191. //void set_wm_border(bool p_enabled);
  192. void set_wm_fullscreen(bool p_enabled);
  193. void set_wm_above(bool p_enabled);
  194. typedef xrr_monitor_info *(*xrr_get_monitors_t)(Display *dpy, Window window, Bool get_active, int *nmonitors);
  195. typedef void (*xrr_free_monitors_t)(xrr_monitor_info *monitors);
  196. xrr_get_monitors_t xrr_get_monitors;
  197. xrr_free_monitors_t xrr_free_monitors;
  198. void *xrandr_handle;
  199. Bool xrandr_ext_ok;
  200. protected:
  201. virtual int get_current_video_driver() const;
  202. virtual void initialize_core();
  203. virtual Error initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver);
  204. virtual void finalize();
  205. virtual void set_main_loop(MainLoop *p_main_loop);
  206. void _window_changed(XEvent *event);
  207. bool window_maximize_check(const char *p_atom_name) const;
  208. bool window_fullscreen_check() const;
  209. bool is_window_maximize_allowed() const;
  210. public:
  211. virtual String get_name() const;
  212. virtual void set_cursor_shape(CursorShape p_shape);
  213. virtual CursorShape get_cursor_shape() const;
  214. virtual void set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot);
  215. void set_mouse_mode(MouseMode p_mode);
  216. MouseMode get_mouse_mode() const;
  217. virtual void warp_mouse_position(const Point2 &p_to);
  218. virtual Point2 get_mouse_position() const;
  219. virtual int get_mouse_button_state() const;
  220. virtual void set_window_title(const String &p_title);
  221. virtual void set_window_mouse_passthrough(const PoolVector2Array &p_region);
  222. virtual void set_icon(const Ref<Image> &p_icon);
  223. virtual MainLoop *get_main_loop() const;
  224. virtual bool can_draw() const;
  225. virtual void set_clipboard(const String &p_text);
  226. virtual String get_clipboard() const;
  227. virtual void release_rendering_thread();
  228. virtual void make_rendering_thread();
  229. virtual void swap_buffers();
  230. virtual String get_config_path() const;
  231. virtual String get_data_path() const;
  232. virtual String get_cache_path() const;
  233. virtual String get_system_dir(SystemDir p_dir, bool p_shared_storage = true) const;
  234. virtual Error shell_open(String p_uri);
  235. virtual void set_video_mode(const VideoMode &p_video_mode, int p_screen = 0);
  236. virtual VideoMode get_video_mode(int p_screen = 0) const;
  237. virtual void get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen = 0) const;
  238. virtual int get_screen_count() const;
  239. virtual int get_current_screen() const;
  240. virtual void set_current_screen(int p_screen);
  241. virtual Point2 get_screen_position(int p_screen = -1) const;
  242. virtual Size2 get_screen_size(int p_screen = -1) const;
  243. virtual int get_screen_dpi(int p_screen = -1) const;
  244. virtual Point2 get_window_position() const;
  245. virtual void set_window_position(const Point2 &p_position);
  246. virtual Size2 get_window_size() const;
  247. virtual Size2 get_real_window_size() const;
  248. virtual Size2 get_max_window_size() const;
  249. virtual Size2 get_min_window_size() const;
  250. virtual void set_min_window_size(const Size2 p_size);
  251. virtual void set_max_window_size(const Size2 p_size);
  252. virtual void set_window_size(const Size2 p_size);
  253. virtual void set_window_fullscreen(bool p_enabled);
  254. virtual bool is_window_fullscreen() const;
  255. virtual void set_window_resizable(bool p_enabled);
  256. virtual bool is_window_resizable() const;
  257. virtual void set_window_minimized(bool p_enabled);
  258. virtual bool is_window_minimized() const;
  259. virtual void set_window_maximized(bool p_enabled);
  260. virtual bool is_window_maximized() const;
  261. virtual void set_window_always_on_top(bool p_enabled);
  262. virtual bool is_window_always_on_top() const;
  263. virtual bool is_window_focused() const;
  264. virtual void request_attention();
  265. virtual void *get_native_handle(int p_handle_type);
  266. virtual void set_borderless_window(bool p_borderless);
  267. virtual bool get_borderless_window();
  268. virtual bool get_window_per_pixel_transparency_enabled() const;
  269. virtual void set_window_per_pixel_transparency_enabled(bool p_enabled);
  270. virtual void set_ime_active(const bool p_active);
  271. virtual void set_ime_position(const Point2 &p_pos);
  272. virtual String get_unique_id() const;
  273. virtual void move_window_to_foreground();
  274. virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
  275. virtual bool is_joy_known(int p_device);
  276. virtual String get_joy_guid(int p_device) const;
  277. virtual void set_context(int p_context);
  278. virtual void _set_use_vsync(bool p_enable);
  279. //virtual bool is_vsync_enabled() const;
  280. virtual OS::PowerState get_power_state();
  281. virtual int get_power_seconds_left();
  282. virtual int get_power_percent_left();
  283. virtual bool _check_internal_feature_support(const String &p_feature);
  284. virtual void force_process_input();
  285. void run();
  286. void disable_crash_handler();
  287. bool is_disable_crash_handler() const;
  288. virtual Error move_to_trash(const String &p_path);
  289. virtual LatinKeyboardVariant get_latin_keyboard_variant() const;
  290. virtual int keyboard_get_layout_count() const;
  291. virtual int keyboard_get_current_layout() const;
  292. virtual void keyboard_set_current_layout(int p_index);
  293. virtual String keyboard_get_layout_language(int p_index) const;
  294. virtual String keyboard_get_layout_name(int p_index) const;
  295. void update_real_mouse_position();
  296. OS_X11();
  297. };
  298. #endif