display_server_x11.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*************************************************************************/
  2. /* display_server_x11.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 DISPLAY_SERVER_X11_H
  31. #define DISPLAY_SERVER_X11_H
  32. #ifdef X11_ENABLED
  33. #include "servers/display_server.h"
  34. #include "core/input/input.h"
  35. #include "drivers/alsa/audio_driver_alsa.h"
  36. #include "drivers/alsamidi/midi_driver_alsamidi.h"
  37. #include "drivers/pulseaudio/audio_driver_pulseaudio.h"
  38. #include "drivers/unix/os_unix.h"
  39. #include "joypad_linux.h"
  40. #include "servers/audio_server.h"
  41. #include "servers/rendering/rasterizer.h"
  42. #include "servers/rendering_server.h"
  43. #if defined(OPENGL_ENABLED)
  44. #include "context_gl_x11.h"
  45. #endif
  46. #if defined(VULKAN_ENABLED)
  47. #include "drivers/vulkan/rendering_device_vulkan.h"
  48. #include "platform/linuxbsd/vulkan_context_x11.h"
  49. #endif
  50. #include <X11/Xcursor/Xcursor.h>
  51. #include <X11/Xlib.h>
  52. #include <X11/extensions/XInput2.h>
  53. #include <X11/extensions/Xrandr.h>
  54. #include <X11/keysym.h>
  55. // Hints for X11 fullscreen
  56. typedef struct {
  57. unsigned long flags;
  58. unsigned long functions;
  59. unsigned long decorations;
  60. long inputMode;
  61. unsigned long status;
  62. } Hints;
  63. typedef struct _xrr_monitor_info {
  64. Atom name;
  65. Bool primary;
  66. Bool automatic;
  67. int noutput;
  68. int x;
  69. int y;
  70. int width;
  71. int height;
  72. int mwidth;
  73. int mheight;
  74. RROutput *outputs;
  75. } xrr_monitor_info;
  76. #undef CursorShape
  77. class DisplayServerX11 : public DisplayServer {
  78. //No need to register, it's platform-specific and nothing is added
  79. //GDCLASS(DisplayServerX11, DisplayServer)
  80. _THREAD_SAFE_CLASS_
  81. Atom wm_delete;
  82. Atom xdnd_enter;
  83. Atom xdnd_position;
  84. Atom xdnd_status;
  85. Atom xdnd_action_copy;
  86. Atom xdnd_drop;
  87. Atom xdnd_finished;
  88. Atom xdnd_selection;
  89. Atom xdnd_aware;
  90. Atom requested;
  91. int xdnd_version;
  92. #if defined(OPENGL_ENABLED)
  93. ContextGL_X11 *context_gles2;
  94. #endif
  95. #if defined(VULKAN_ENABLED)
  96. VulkanContextX11 *context_vulkan;
  97. RenderingDeviceVulkan *rendering_device_vulkan;
  98. #endif
  99. struct WindowData {
  100. Window x11_window;
  101. ::XIC xic;
  102. Size2i min_size;
  103. Size2i max_size;
  104. Point2i position;
  105. Size2i size;
  106. Point2i im_position;
  107. bool im_active = false;
  108. Callable rect_changed_callback;
  109. Callable event_callback;
  110. Callable input_event_callback;
  111. Callable input_text_callback;
  112. Callable drop_files_callback;
  113. WindowID transient_parent = INVALID_WINDOW_ID;
  114. Set<WindowID> transient_children;
  115. ObjectID instance_id;
  116. //better to guess on the fly, given WM can change it
  117. //WindowMode mode;
  118. bool fullscreen = false; //OS can't exit from this mode
  119. bool on_top = false;
  120. bool borderless = false;
  121. bool resize_disabled = false;
  122. Vector2i last_position_before_fs;
  123. };
  124. Map<WindowID, WindowData> windows;
  125. WindowID window_id_counter = MAIN_WINDOW_ID;
  126. WindowID _create_window(WindowMode p_mode, uint32_t p_flags, const Rect2i &p_rect);
  127. String internal_clipboard;
  128. Window xdnd_source_window;
  129. ::Display *x11_display;
  130. char *xmbstring;
  131. int xmblen;
  132. unsigned long last_timestamp;
  133. ::Time last_keyrelease_time;
  134. ::XIM xim;
  135. ::XIMStyle xim_style;
  136. static void _xim_destroy_callback(::XIM im, ::XPointer client_data,
  137. ::XPointer call_data);
  138. Point2i last_mouse_pos;
  139. bool last_mouse_pos_valid;
  140. Point2i last_click_pos;
  141. uint64_t last_click_ms;
  142. int last_click_button_index;
  143. uint32_t last_button_state;
  144. struct {
  145. int opcode;
  146. Vector<int> touch_devices;
  147. Map<int, Vector2> absolute_devices;
  148. Map<int, Vector2> pen_pressure_range;
  149. Map<int, Vector2> pen_tilt_x_range;
  150. Map<int, Vector2> pen_tilt_y_range;
  151. XIEventMask all_event_mask;
  152. Map<int, Vector2> state;
  153. double pressure;
  154. bool pressure_supported;
  155. Vector2 tilt;
  156. Vector2 mouse_pos_to_filter;
  157. Vector2 relative_motion;
  158. Vector2 raw_pos;
  159. Vector2 old_raw_pos;
  160. ::Time last_relative_time;
  161. } xi;
  162. bool _refresh_device_info();
  163. unsigned int _get_mouse_button_state(unsigned int p_x11_button, int p_x11_type);
  164. void _get_key_modifier_state(unsigned int p_x11_state, Ref<InputEventWithModifiers> state);
  165. void _flush_mouse_motion();
  166. MouseMode mouse_mode;
  167. Point2i center;
  168. void _handle_key_event(WindowID p_window, XKeyEvent *p_event, bool p_echo = false);
  169. bool minimized;
  170. bool window_has_focus;
  171. bool do_mouse_warp;
  172. const char *cursor_theme;
  173. int cursor_size;
  174. XcursorImage *img[CURSOR_MAX];
  175. Cursor cursors[CURSOR_MAX];
  176. Cursor null_cursor;
  177. CursorShape current_cursor;
  178. Map<CursorShape, Vector<Variant>> cursors_cache;
  179. bool layered_window;
  180. String rendering_driver;
  181. bool window_focused;
  182. //void set_wm_border(bool p_enabled);
  183. void set_wm_fullscreen(bool p_enabled);
  184. void set_wm_above(bool p_enabled);
  185. typedef xrr_monitor_info *(*xrr_get_monitors_t)(Display *dpy, Window window, Bool get_active, int *nmonitors);
  186. typedef void (*xrr_free_monitors_t)(xrr_monitor_info *monitors);
  187. xrr_get_monitors_t xrr_get_monitors;
  188. xrr_free_monitors_t xrr_free_monitors;
  189. void *xrandr_handle;
  190. Bool xrandr_ext_ok;
  191. struct Property {
  192. unsigned char *data;
  193. int format, nitems;
  194. Atom type;
  195. };
  196. static Property _read_property(Display *p_display, Window p_window, Atom p_property);
  197. void _update_real_mouse_position(const WindowData &wd);
  198. void _set_wm_fullscreen(WindowID p_window, bool p_enabled);
  199. void _set_wm_maximized(WindowID p_window, bool p_enabled);
  200. void _update_context(WindowData &wd);
  201. Context context = CONTEXT_ENGINE;
  202. void _send_window_event(const WindowData &wd, WindowEvent p_event);
  203. static void _dispatch_input_events(const Ref<InputEvent> &p_event);
  204. void _dispatch_input_event(const Ref<InputEvent> &p_event);
  205. protected:
  206. void _window_changed(XEvent *event);
  207. public:
  208. virtual bool has_feature(Feature p_feature) const;
  209. virtual String get_name() const;
  210. virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
  211. virtual void mouse_set_mode(MouseMode p_mode);
  212. virtual MouseMode mouse_get_mode() const;
  213. virtual void mouse_warp_to_position(const Point2i &p_to);
  214. virtual Point2i mouse_get_position() const;
  215. virtual Point2i mouse_get_absolute_position() const;
  216. virtual int mouse_get_button_state() const;
  217. virtual void clipboard_set(const String &p_text);
  218. virtual String clipboard_get() const;
  219. virtual int get_screen_count() const;
  220. virtual Point2i screen_get_position(int p_screen = SCREEN_OF_MAIN_WINDOW) const;
  221. virtual Size2i screen_get_size(int p_screen = SCREEN_OF_MAIN_WINDOW) const;
  222. virtual Rect2i screen_get_usable_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const;
  223. virtual int screen_get_dpi(int p_screen = SCREEN_OF_MAIN_WINDOW) const;
  224. virtual bool screen_is_touchscreen(int p_screen = SCREEN_OF_MAIN_WINDOW) const;
  225. virtual Vector<DisplayServer::WindowID> get_window_list() const;
  226. virtual WindowID create_sub_window(WindowMode p_mode, uint32_t p_flags, const Rect2i &p_rect = Rect2i());
  227. virtual void delete_sub_window(WindowID p_id);
  228. virtual WindowID get_window_at_screen_position(const Point2i &p_position) const;
  229. virtual void window_attach_instance_id(ObjectID p_instance, WindowID p_window = MAIN_WINDOW_ID);
  230. virtual ObjectID window_get_attached_instance_id(WindowID p_window = MAIN_WINDOW_ID) const;
  231. virtual void window_set_title(const String &p_title, WindowID p_window = MAIN_WINDOW_ID);
  232. virtual void window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID);
  233. virtual void window_set_window_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID);
  234. virtual void window_set_input_event_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID);
  235. virtual void window_set_input_text_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID);
  236. virtual void window_set_drop_files_callback(const Callable &p_callable, WindowID p_window = MAIN_WINDOW_ID);
  237. virtual int window_get_current_screen(WindowID p_window = MAIN_WINDOW_ID) const;
  238. virtual void window_set_current_screen(int p_screen, WindowID p_window = MAIN_WINDOW_ID);
  239. virtual Point2i window_get_position(WindowID p_window = MAIN_WINDOW_ID) const;
  240. virtual void window_set_position(const Point2i &p_position, WindowID p_window = MAIN_WINDOW_ID);
  241. virtual void window_set_max_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID);
  242. virtual Size2i window_get_max_size(WindowID p_window = MAIN_WINDOW_ID) const;
  243. virtual void window_set_transient(WindowID p_window, WindowID p_parent);
  244. virtual void window_set_min_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID);
  245. virtual Size2i window_get_min_size(WindowID p_window = MAIN_WINDOW_ID) const;
  246. virtual void window_set_size(const Size2i p_size, WindowID p_window = MAIN_WINDOW_ID);
  247. virtual Size2i window_get_size(WindowID p_window = MAIN_WINDOW_ID) const;
  248. virtual Size2i window_get_real_size(WindowID p_window = MAIN_WINDOW_ID) const;
  249. virtual void window_set_mode(WindowMode p_mode, WindowID p_window = MAIN_WINDOW_ID);
  250. virtual WindowMode window_get_mode(WindowID p_window = MAIN_WINDOW_ID) const;
  251. virtual bool window_is_maximize_allowed(WindowID p_window = MAIN_WINDOW_ID) const;
  252. virtual void window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window = MAIN_WINDOW_ID);
  253. virtual bool window_get_flag(WindowFlags p_flag, WindowID p_window = MAIN_WINDOW_ID) const;
  254. virtual void window_request_attention(WindowID p_window = MAIN_WINDOW_ID);
  255. virtual void window_move_to_foreground(WindowID p_window = MAIN_WINDOW_ID);
  256. virtual bool window_can_draw(WindowID p_window = MAIN_WINDOW_ID) const;
  257. virtual bool can_any_window_draw() const;
  258. virtual void window_set_ime_active(const bool p_active, WindowID p_window = MAIN_WINDOW_ID);
  259. virtual void window_set_ime_position(const Point2i &p_pos, WindowID p_window = MAIN_WINDOW_ID);
  260. virtual void cursor_set_shape(CursorShape p_shape);
  261. virtual CursorShape cursor_get_shape() const;
  262. virtual void cursor_set_custom_image(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot);
  263. virtual LatinKeyboardVariant get_latin_keyboard_variant() const;
  264. virtual void process_events();
  265. virtual void release_rendering_thread();
  266. virtual void make_rendering_thread();
  267. virtual void swap_buffers();
  268. virtual void set_context(Context p_context);
  269. virtual void set_native_icon(const String &p_filename);
  270. virtual void set_icon(const Ref<Image> &p_icon);
  271. static DisplayServer *create_func(const String &p_rendering_driver, WindowMode p_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error);
  272. static Vector<String> get_rendering_drivers_func();
  273. static void register_x11_driver();
  274. DisplayServerX11(const String &p_rendering_driver, WindowMode p_mode, uint32_t p_flags, const Vector2i &p_resolution, Error &r_error);
  275. ~DisplayServerX11();
  276. };
  277. #endif // X11 enabled
  278. #endif // DISPLAY_SERVER_X11_H