os_x11.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*************************************************************************/
  2. /* os_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 OS_X11_H
  31. #define OS_X11_H
  32. #include "context_gl_x11.h"
  33. #include "core/os/input.h"
  34. #include "crash_handler_x11.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 "main/input_default.h"
  41. #include "power_x11.h"
  42. #include "servers/audio_server.h"
  43. #include "servers/visual/rasterizer.h"
  44. #include "servers/visual_server.h"
  45. //#include "servers/visual/visual_server_wrap_mt.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, Vector3> pen_devices;
  121. XIEventMask all_event_mask;
  122. XIEventMask all_master_event_mask;
  123. Map<int, Vector2> state;
  124. double pressure;
  125. Vector2 tilt;
  126. Vector2 mouse_pos_to_filter;
  127. Vector2 relative_motion;
  128. Vector2 raw_pos;
  129. Vector2 old_raw_pos;
  130. ::Time last_relative_time;
  131. } xi;
  132. bool refresh_device_info();
  133. unsigned int get_mouse_button_state(unsigned int p_x11_button, int p_x11_type);
  134. void get_key_modifier_state(unsigned int p_x11_state, Ref<InputEventWithModifiers> state);
  135. void flush_mouse_motion();
  136. MouseMode mouse_mode;
  137. Point2i center;
  138. void handle_key_event(XKeyEvent *p_event, bool p_echo = false);
  139. void process_xevents();
  140. virtual void delete_main_loop();
  141. bool force_quit;
  142. bool minimized;
  143. bool window_has_focus;
  144. bool do_mouse_warp;
  145. const char *cursor_theme;
  146. int cursor_size;
  147. XcursorImage *img[CURSOR_MAX];
  148. Cursor cursors[CURSOR_MAX];
  149. Cursor null_cursor;
  150. CursorShape current_cursor;
  151. Map<CursorShape, Vector<Variant> > cursors_cache;
  152. InputDefault *input;
  153. #ifdef JOYDEV_ENABLED
  154. JoypadLinux *joypad;
  155. #endif
  156. #ifdef ALSA_ENABLED
  157. AudioDriverALSA driver_alsa;
  158. #endif
  159. #ifdef ALSAMIDI_ENABLED
  160. MIDIDriverALSAMidi driver_alsamidi;
  161. #endif
  162. #ifdef PULSEAUDIO_ENABLED
  163. AudioDriverPulseAudio driver_pulseaudio;
  164. #endif
  165. PowerX11 *power_manager;
  166. bool layered_window;
  167. CrashHandler crash_handler;
  168. int video_driver_index;
  169. bool maximized;
  170. bool window_focused;
  171. //void set_wm_border(bool p_enabled);
  172. void set_wm_fullscreen(bool p_enabled);
  173. void set_wm_above(bool p_enabled);
  174. typedef xrr_monitor_info *(*xrr_get_monitors_t)(Display *dpy, Window window, Bool get_active, int *nmonitors);
  175. typedef void (*xrr_free_monitors_t)(xrr_monitor_info *monitors);
  176. xrr_get_monitors_t xrr_get_monitors;
  177. xrr_free_monitors_t xrr_free_monitors;
  178. void *xrandr_handle;
  179. Bool xrandr_ext_ok;
  180. protected:
  181. virtual int get_current_video_driver() const;
  182. virtual void initialize_core();
  183. virtual Error initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver);
  184. virtual void finalize();
  185. virtual void set_main_loop(MainLoop *p_main_loop);
  186. void _window_changed(XEvent *event);
  187. bool is_window_maximize_allowed();
  188. public:
  189. virtual String get_name() const;
  190. virtual void set_cursor_shape(CursorShape p_shape);
  191. virtual CursorShape get_cursor_shape() const;
  192. virtual void set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot);
  193. void set_mouse_mode(MouseMode p_mode);
  194. MouseMode get_mouse_mode() const;
  195. virtual void warp_mouse_position(const Point2 &p_to);
  196. virtual Point2 get_mouse_position() const;
  197. virtual int get_mouse_button_state() const;
  198. virtual void set_window_title(const String &p_title);
  199. virtual void set_icon(const Ref<Image> &p_icon);
  200. virtual MainLoop *get_main_loop() const;
  201. virtual bool can_draw() const;
  202. virtual void set_clipboard(const String &p_text);
  203. virtual String get_clipboard() const;
  204. virtual void release_rendering_thread();
  205. virtual void make_rendering_thread();
  206. virtual void swap_buffers();
  207. virtual String get_config_path() const;
  208. virtual String get_data_path() const;
  209. virtual String get_cache_path() const;
  210. virtual String get_system_dir(SystemDir p_dir) const;
  211. virtual Error shell_open(String p_uri);
  212. virtual void set_video_mode(const VideoMode &p_video_mode, int p_screen = 0);
  213. virtual VideoMode get_video_mode(int p_screen = 0) const;
  214. virtual void get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen = 0) const;
  215. virtual int get_screen_count() const;
  216. virtual int get_current_screen() const;
  217. virtual void set_current_screen(int p_screen);
  218. virtual Point2 get_screen_position(int p_screen = -1) const;
  219. virtual Size2 get_screen_size(int p_screen = -1) const;
  220. virtual int get_screen_dpi(int p_screen = -1) const;
  221. virtual Point2 get_window_position() const;
  222. virtual void set_window_position(const Point2 &p_position);
  223. virtual Size2 get_window_size() const;
  224. virtual Size2 get_real_window_size() const;
  225. virtual Size2 get_max_window_size() const;
  226. virtual Size2 get_min_window_size() const;
  227. virtual void set_min_window_size(const Size2 p_size);
  228. virtual void set_max_window_size(const Size2 p_size);
  229. virtual void set_window_size(const Size2 p_size);
  230. virtual void set_window_fullscreen(bool p_enabled);
  231. virtual bool is_window_fullscreen() const;
  232. virtual void set_window_resizable(bool p_enabled);
  233. virtual bool is_window_resizable() const;
  234. virtual void set_window_minimized(bool p_enabled);
  235. virtual bool is_window_minimized() const;
  236. virtual void set_window_maximized(bool p_enabled);
  237. virtual bool is_window_maximized() const;
  238. virtual void set_window_always_on_top(bool p_enabled);
  239. virtual bool is_window_always_on_top() const;
  240. virtual bool is_window_focused() const;
  241. virtual void request_attention();
  242. virtual void set_borderless_window(bool p_borderless);
  243. virtual bool get_borderless_window();
  244. virtual bool get_window_per_pixel_transparency_enabled() const;
  245. virtual void set_window_per_pixel_transparency_enabled(bool p_enabled);
  246. virtual void set_ime_active(const bool p_active);
  247. virtual void set_ime_position(const Point2 &p_pos);
  248. virtual String get_unique_id() const;
  249. virtual void move_window_to_foreground();
  250. virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
  251. virtual bool is_joy_known(int p_device);
  252. virtual String get_joy_guid(int p_device) const;
  253. virtual void set_context(int p_context);
  254. virtual void _set_use_vsync(bool p_enable);
  255. //virtual bool is_vsync_enabled() const;
  256. virtual OS::PowerState get_power_state();
  257. virtual int get_power_seconds_left();
  258. virtual int get_power_percent_left();
  259. virtual bool _check_internal_feature_support(const String &p_feature);
  260. virtual void force_process_input();
  261. void run();
  262. void disable_crash_handler();
  263. bool is_disable_crash_handler() const;
  264. virtual Error move_to_trash(const String &p_path);
  265. virtual LatinKeyboardVariant get_latin_keyboard_variant() const;
  266. void update_real_mouse_position();
  267. OS_X11();
  268. };
  269. #endif