wl_platform.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. //========================================================================
  2. // GLFW 3.4 Wayland - www.glfw.org
  3. //------------------------------------------------------------------------
  4. // Copyright (c) 2014 Jonas Ådahl <jadahl@gmail.com>
  5. //
  6. // This software is provided 'as-is', without any express or implied
  7. // warranty. In no event will the authors be held liable for any damages
  8. // arising from the use of this software.
  9. //
  10. // Permission is granted to anyone to use this software for any purpose,
  11. // including commercial applications, and to alter it and redistribute it
  12. // freely, subject to the following restrictions:
  13. //
  14. // 1. The origin of this software must not be misrepresented; you must not
  15. // claim that you wrote the original software. If you use this software
  16. // in a product, an acknowledgment in the product documentation would
  17. // be appreciated but is not required.
  18. //
  19. // 2. Altered source versions must be plainly marked as such, and must not
  20. // be misrepresented as being the original software.
  21. //
  22. // 3. This notice may not be removed or altered from any source
  23. // distribution.
  24. //
  25. //========================================================================
  26. #include <wayland-client.h>
  27. #include <dlfcn.h>
  28. #include <poll.h>
  29. typedef VkFlags VkWaylandSurfaceCreateFlagsKHR;
  30. typedef struct VkWaylandSurfaceCreateInfoKHR
  31. {
  32. VkStructureType sType;
  33. const void* pNext;
  34. VkWaylandSurfaceCreateFlagsKHR flags;
  35. struct wl_display* display;
  36. struct wl_surface* surface;
  37. } VkWaylandSurfaceCreateInfoKHR;
  38. typedef VkResult (APIENTRY *PFN_vkCreateWaylandSurfaceKHR)(VkInstance,const VkWaylandSurfaceCreateInfoKHR*,const VkAllocationCallbacks*,VkSurfaceKHR*);
  39. typedef VkBool32 (APIENTRY *PFN_vkGetPhysicalDeviceWaylandPresentationSupportKHR)(VkPhysicalDevice,uint32_t,struct wl_display*);
  40. #include "posix_thread.h"
  41. #ifdef __linux__
  42. #include "linux_joystick.h"
  43. #else
  44. #include "null_joystick.h"
  45. #endif
  46. #include "backend_utils.h"
  47. #include "xkb_glfw.h"
  48. #include "wl_cursors.h"
  49. #include "wayland-xdg-shell-client-protocol.h"
  50. #include "wayland-xdg-decoration-unstable-v1-client-protocol.h"
  51. #include "wayland-relative-pointer-unstable-v1-client-protocol.h"
  52. #include "wayland-pointer-constraints-unstable-v1-client-protocol.h"
  53. #include "wayland-primary-selection-unstable-v1-client-protocol.h"
  54. #include "wayland-primary-selection-unstable-v1-client-protocol.h"
  55. #include "wayland-xdg-activation-v1-client-protocol.h"
  56. #include "wayland-cursor-shape-v1-client-protocol.h"
  57. #include "wayland-fractional-scale-v1-client-protocol.h"
  58. #include "wayland-viewporter-client-protocol.h"
  59. #include "wayland-kwin-blur-v1-client-protocol.h"
  60. #include "wayland-wlr-layer-shell-unstable-v1-client-protocol.h"
  61. #include "wayland-single-pixel-buffer-v1-client-protocol.h"
  62. #include "wayland-idle-inhibit-unstable-v1-client-protocol.h"
  63. #define _glfw_dlopen(name) dlopen(name, RTLD_LAZY | RTLD_LOCAL)
  64. #define _glfw_dlclose(handle) dlclose(handle)
  65. #define _glfw_dlsym(handle, name) dlsym(handle, name)
  66. #define _GLFW_PLATFORM_WINDOW_STATE _GLFWwindowWayland wl
  67. #define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE _GLFWlibraryWayland wl
  68. #define _GLFW_PLATFORM_MONITOR_STATE _GLFWmonitorWayland wl
  69. #define _GLFW_PLATFORM_CURSOR_STATE _GLFWcursorWayland wl
  70. #define _GLFW_PLATFORM_CONTEXT_STATE
  71. #define _GLFW_PLATFORM_LIBRARY_CONTEXT_STATE
  72. typedef struct wl_cursor_theme* (* PFN_wl_cursor_theme_load)(const char*, int, struct wl_shm*);
  73. typedef void (* PFN_wl_cursor_theme_destroy)(struct wl_cursor_theme*);
  74. typedef struct wl_cursor* (* PFN_wl_cursor_theme_get_cursor)(struct wl_cursor_theme*, const char*);
  75. typedef struct wl_buffer* (* PFN_wl_cursor_image_get_buffer)(struct wl_cursor_image*);
  76. #define wl_cursor_theme_load _glfw.wl.cursor.theme_load
  77. #define wl_cursor_theme_destroy _glfw.wl.cursor.theme_destroy
  78. #define wl_cursor_theme_get_cursor _glfw.wl.cursor.theme_get_cursor
  79. #define wl_cursor_image_get_buffer _glfw.wl.cursor.image_get_buffer
  80. typedef struct wl_egl_window* (* PFN_wl_egl_window_create)(struct wl_surface*, int, int);
  81. typedef void (* PFN_wl_egl_window_destroy)(struct wl_egl_window*);
  82. typedef void (* PFN_wl_egl_window_resize)(struct wl_egl_window*, int, int, int, int);
  83. #define wl_egl_window_create _glfw.wl.egl.window_create
  84. #define wl_egl_window_destroy _glfw.wl.egl.window_destroy
  85. #define wl_egl_window_resize _glfw.wl.egl.window_resize
  86. typedef enum _GLFWCSDSurface
  87. {
  88. CENTRAL_WINDOW, CSD_titlebar, CSD_shadow_top, CSD_shadow_left, CSD_shadow_bottom, CSD_shadow_right,
  89. CSD_shadow_upper_left, CSD_shadow_upper_right, CSD_shadow_lower_left, CSD_shadow_lower_right,
  90. } _GLFWCSDSurface;
  91. typedef struct _GLFWWaylandBufferPair {
  92. struct wl_buffer *a, *b, *front, *back;
  93. struct { uint8_t *a, *b, *front, *back; } data;
  94. bool has_pending_update;
  95. size_t size_in_bytes, width, height, viewport_width, viewport_height, stride;
  96. bool a_needs_to_be_destroyed, b_needs_to_be_destroyed;
  97. } _GLFWWaylandBufferPair;
  98. typedef struct _GLFWWaylandCSDSurface {
  99. struct wl_surface *surface;
  100. struct wl_subsurface *subsurface;
  101. struct wp_viewport *wp_viewport;
  102. _GLFWWaylandBufferPair buffer;
  103. int x, y;
  104. } _GLFWWaylandCSDSurface;
  105. typedef enum WaylandWindowState {
  106. TOPLEVEL_STATE_NONE = 0,
  107. TOPLEVEL_STATE_MAXIMIZED = 1,
  108. TOPLEVEL_STATE_FULLSCREEN = 2,
  109. TOPLEVEL_STATE_RESIZING = 4,
  110. TOPLEVEL_STATE_ACTIVATED = 8,
  111. TOPLEVEL_STATE_TILED_LEFT = 16,
  112. TOPLEVEL_STATE_TILED_RIGHT = 32,
  113. TOPLEVEL_STATE_TILED_TOP = 64,
  114. TOPLEVEL_STATE_TILED_BOTTOM = 128,
  115. TOPLEVEL_STATE_SUSPENDED = 256,
  116. } WaylandWindowState;
  117. typedef struct glfw_wl_xdg_activation_request {
  118. GLFWid window_id;
  119. GLFWactivationcallback callback;
  120. void *callback_data;
  121. uintptr_t request_id;
  122. void *token;
  123. } glfw_wl_xdg_activation_request;
  124. static const WaylandWindowState TOPLEVEL_STATE_DOCKED = TOPLEVEL_STATE_MAXIMIZED | TOPLEVEL_STATE_FULLSCREEN | TOPLEVEL_STATE_TILED_TOP | TOPLEVEL_STATE_TILED_LEFT | TOPLEVEL_STATE_TILED_RIGHT | TOPLEVEL_STATE_TILED_BOTTOM;
  125. enum WaylandWindowPendingState {
  126. PENDING_STATE_TOPLEVEL = 1,
  127. PENDING_STATE_DECORATION = 2
  128. };
  129. enum _GLFWWaylandAxisEvent {
  130. AXIS_EVENT_UNKNOWN = 0,
  131. AXIS_EVENT_CONTINUOUS = 1,
  132. AXIS_EVENT_DISCRETE = 2,
  133. AXIS_EVENT_VALUE120 = 3
  134. };
  135. // Wayland-specific per-window data
  136. //
  137. typedef struct _GLFWwindowWayland
  138. {
  139. int width, height;
  140. bool visible;
  141. bool hovered;
  142. bool transparent;
  143. struct wl_surface* surface;
  144. bool waiting_for_swap_to_commit;
  145. struct wl_egl_window* native;
  146. struct wl_callback* callback;
  147. struct {
  148. struct xdg_surface* surface;
  149. struct xdg_toplevel* toplevel;
  150. struct zxdg_toplevel_decoration_v1* decoration;
  151. struct { int width, height; } top_level_bounds;
  152. } xdg;
  153. struct wp_fractional_scale_v1 *wp_fractional_scale_v1;
  154. struct wp_viewport *wp_viewport;
  155. struct org_kde_kwin_blur *org_kde_kwin_blur;
  156. bool has_blur, expect_scale_from_compositor, window_fully_created;
  157. struct {
  158. bool surface_configured, fractional_scale_received, preferred_scale_received;
  159. } once;
  160. struct wl_buffer *temp_buffer_used_during_window_creation;
  161. struct {
  162. GLFWLayerShellConfig config;
  163. struct zwlr_layer_surface_v1* zwlr_layer_surface_v1;
  164. } layer_shell;
  165. /* information about axis events on current frame */
  166. struct
  167. {
  168. struct {
  169. enum _GLFWWaylandAxisEvent x_axis_type;
  170. float x;
  171. enum _GLFWWaylandAxisEvent y_axis_type;
  172. float y;
  173. } discrete, continuous;
  174. /* Event timestamp in nanoseconds */
  175. monotonic_t timestamp_ns;
  176. } pointer_curr_axis_info;
  177. _GLFWcursor* currentCursor;
  178. double cursorPosX, cursorPosY, allCursorPosX, allCursorPosY;
  179. char* title;
  180. char appId[256];
  181. // We need to track the monitors the window spans on to calculate the
  182. // optimal scaling factor.
  183. struct { uint32_t deduced, preferred; } integer_scale;
  184. uint32_t fractional_scale;
  185. bool initial_scale_notified;
  186. _GLFWmonitor** monitors;
  187. int monitorsCount;
  188. int monitorsSize;
  189. struct {
  190. struct zwp_relative_pointer_v1* relativePointer;
  191. struct zwp_locked_pointer_v1* lockedPointer;
  192. } pointerLock;
  193. struct {
  194. bool serverSide, buffer_destroyed, titlebar_needs_update, dragging;
  195. _GLFWCSDSurface focus;
  196. _GLFWWaylandCSDSurface titlebar, shadow_left, shadow_right, shadow_top, shadow_bottom, shadow_upper_left, shadow_upper_right, shadow_lower_left, shadow_lower_right;
  197. struct {
  198. uint8_t *data;
  199. size_t size;
  200. } mapping;
  201. struct {
  202. int width, height;
  203. bool focused;
  204. double fscale;
  205. WaylandWindowState toplevel_states;
  206. } for_window_state;
  207. struct {
  208. unsigned int width, top, horizontal, vertical, visible_titlebar_height;
  209. } metrics;
  210. struct {
  211. int32_t x, y, width, height;
  212. } geometry;
  213. struct {
  214. bool hovered;
  215. int width, left;
  216. } minimize, maximize, close;
  217. struct {
  218. uint32_t *data;
  219. size_t for_decoration_size, stride, segments, corner_size;
  220. } shadow_tile;
  221. monotonic_t last_click_on_top_decoration_at;
  222. uint32_t titlebar_color;
  223. bool use_custom_titlebar_color;
  224. } decorations;
  225. struct {
  226. unsigned long long id;
  227. void(*callback)(unsigned long long id);
  228. struct wl_callback *current_wl_callback;
  229. } frameCallbackData;
  230. struct {
  231. int32_t width, height;
  232. } user_requested_content_size;
  233. struct {
  234. bool minimize, maximize, fullscreen, window_menu;
  235. } wm_capabilities;
  236. bool maximize_on_first_show;
  237. uint32_t pending_state;
  238. struct {
  239. int width, height;
  240. WaylandWindowState toplevel_states;
  241. uint32_t decoration_mode;
  242. } current, pending;
  243. } _GLFWwindowWayland;
  244. typedef enum _GLFWWaylandOfferType
  245. {
  246. EXPIRED,
  247. CLIPBOARD,
  248. DRAG_AND_DROP,
  249. PRIMARY_SELECTION
  250. }_GLFWWaylandOfferType ;
  251. typedef struct _GLFWWaylandDataOffer
  252. {
  253. void *id;
  254. _GLFWWaylandOfferType offer_type;
  255. size_t idx;
  256. bool is_self_offer;
  257. bool is_primary;
  258. const char *mime_for_drop;
  259. uint32_t source_actions;
  260. uint32_t dnd_action;
  261. struct wl_surface *surface;
  262. const char **mimes;
  263. size_t mimes_capacity, mimes_count;
  264. } _GLFWWaylandDataOffer;
  265. // Wayland-specific global data
  266. //
  267. typedef struct _GLFWlibraryWayland
  268. {
  269. struct wl_display* display;
  270. struct wl_registry* registry;
  271. struct wl_compositor* compositor;
  272. struct wl_subcompositor* subcompositor;
  273. struct wl_shm* shm;
  274. struct wl_seat* seat;
  275. struct wl_pointer* pointer;
  276. struct wl_keyboard* keyboard;
  277. struct wl_data_device_manager* dataDeviceManager;
  278. struct wl_data_device* dataDevice;
  279. struct xdg_wm_base* wmBase;
  280. int xdg_wm_base_version;
  281. struct zxdg_decoration_manager_v1* decorationManager;
  282. struct zwp_relative_pointer_manager_v1* relativePointerManager;
  283. struct zwp_pointer_constraints_v1* pointerConstraints;
  284. struct wl_data_source* dataSourceForClipboard;
  285. struct zwp_primary_selection_device_manager_v1* primarySelectionDeviceManager;
  286. struct zwp_primary_selection_device_v1* primarySelectionDevice;
  287. struct zwp_primary_selection_source_v1* dataSourceForPrimarySelection;
  288. struct xdg_activation_v1* xdg_activation_v1;
  289. struct wp_cursor_shape_manager_v1* wp_cursor_shape_manager_v1;
  290. struct wp_cursor_shape_device_v1* wp_cursor_shape_device_v1;
  291. struct wp_fractional_scale_manager_v1 *wp_fractional_scale_manager_v1;
  292. struct wp_viewporter *wp_viewporter;
  293. struct org_kde_kwin_blur_manager *org_kde_kwin_blur_manager;
  294. struct zwlr_layer_shell_v1* zwlr_layer_shell_v1; uint32_t zwlr_layer_shell_v1_version;
  295. struct wp_single_pixel_buffer_manager_v1 *wp_single_pixel_buffer_manager_v1;
  296. struct zwp_idle_inhibit_manager_v1* idle_inhibit_manager;
  297. int compositorVersion;
  298. int seatVersion;
  299. struct wl_surface* cursorSurface;
  300. GLFWCursorShape cursorPreviousShape;
  301. uint32_t serial, input_serial, pointer_serial, pointer_enter_serial, keyboard_enter_serial;
  302. int32_t keyboardRepeatRate;
  303. monotonic_t keyboardRepeatDelay;
  304. struct {
  305. uint32_t key;
  306. id_type keyRepeatTimer;
  307. GLFWid keyboardFocusId;
  308. } keyRepeatInfo;
  309. id_type cursorAnimationTimer;
  310. _GLFWXKBData xkb;
  311. _GLFWDBUSData dbus;
  312. _GLFWwindow* pointerFocus;
  313. GLFWid keyboardFocusId;
  314. struct {
  315. void* handle;
  316. PFN_wl_cursor_theme_load theme_load;
  317. PFN_wl_cursor_theme_destroy theme_destroy;
  318. PFN_wl_cursor_theme_get_cursor theme_get_cursor;
  319. PFN_wl_cursor_image_get_buffer image_get_buffer;
  320. } cursor;
  321. struct {
  322. void* handle;
  323. PFN_wl_egl_window_create window_create;
  324. PFN_wl_egl_window_destroy window_destroy;
  325. PFN_wl_egl_window_resize window_resize;
  326. } egl;
  327. struct {
  328. glfw_wl_xdg_activation_request *array;
  329. size_t capacity, sz;
  330. } activation_requests;
  331. EventLoopData eventLoopData;
  332. size_t dataOffersCounter;
  333. _GLFWWaylandDataOffer dataOffers[8];
  334. bool has_preferred_buffer_scale;
  335. } _GLFWlibraryWayland;
  336. // Wayland-specific per-monitor data
  337. //
  338. typedef struct _GLFWmonitorWayland
  339. {
  340. struct wl_output* output;
  341. uint32_t name;
  342. char friendly_name[64], description[64];
  343. int currentMode;
  344. int x;
  345. int y;
  346. int scale;
  347. } _GLFWmonitorWayland;
  348. // Wayland-specific per-cursor data
  349. //
  350. typedef struct _GLFWcursorWayland
  351. {
  352. struct wl_cursor* cursor;
  353. struct wl_buffer* buffer;
  354. int width, height;
  355. int xhot, yhot;
  356. unsigned int currentImage;
  357. /** The scale of the cursor, or 0 if the cursor should be loaded late, or -1 if the cursor variable itself is unused. */
  358. int scale;
  359. /** Cursor shape stored to allow late cursor loading in setCursorImage. */
  360. GLFWCursorShape shape;
  361. } _GLFWcursorWayland;
  362. void _glfwAddOutputWayland(uint32_t name, uint32_t version);
  363. void _glfwWaylandBeforeBufferSwap(_GLFWwindow *window);
  364. void _glfwWaylandAfterBufferSwap(_GLFWwindow *window);
  365. void _glfwSetupWaylandDataDevice(void);
  366. void _glfwSetupWaylandPrimarySelectionDevice(void);
  367. double _glfwWaylandWindowScale(_GLFWwindow*);
  368. int _glfwWaylandIntegerWindowScale(_GLFWwindow*);
  369. void animateCursorImage(id_type timer_id, void *data);
  370. struct wl_cursor* _glfwLoadCursor(GLFWCursorShape, struct wl_cursor_theme*);
  371. void destroy_data_offer(_GLFWWaylandDataOffer*);
  372. typedef struct wayland_cursor_shape {
  373. int which; const char *name;
  374. } wayland_cursor_shape;
  375. wayland_cursor_shape
  376. glfw_cursor_shape_to_wayland_cursor_shape(GLFWCursorShape g);