wl_platform.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. #define _glfw_dlopen(name) dlopen(name, RTLD_LAZY | RTLD_LOCAL)
  63. #define _glfw_dlclose(handle) dlclose(handle)
  64. #define _glfw_dlsym(handle, name) dlsym(handle, name)
  65. #define _GLFW_PLATFORM_WINDOW_STATE _GLFWwindowWayland wl
  66. #define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE _GLFWlibraryWayland wl
  67. #define _GLFW_PLATFORM_MONITOR_STATE _GLFWmonitorWayland wl
  68. #define _GLFW_PLATFORM_CURSOR_STATE _GLFWcursorWayland wl
  69. #define _GLFW_PLATFORM_CONTEXT_STATE
  70. #define _GLFW_PLATFORM_LIBRARY_CONTEXT_STATE
  71. typedef struct wl_cursor_theme* (* PFN_wl_cursor_theme_load)(const char*, int, struct wl_shm*);
  72. typedef void (* PFN_wl_cursor_theme_destroy)(struct wl_cursor_theme*);
  73. typedef struct wl_cursor* (* PFN_wl_cursor_theme_get_cursor)(struct wl_cursor_theme*, const char*);
  74. typedef struct wl_buffer* (* PFN_wl_cursor_image_get_buffer)(struct wl_cursor_image*);
  75. #define wl_cursor_theme_load _glfw.wl.cursor.theme_load
  76. #define wl_cursor_theme_destroy _glfw.wl.cursor.theme_destroy
  77. #define wl_cursor_theme_get_cursor _glfw.wl.cursor.theme_get_cursor
  78. #define wl_cursor_image_get_buffer _glfw.wl.cursor.image_get_buffer
  79. typedef struct wl_egl_window* (* PFN_wl_egl_window_create)(struct wl_surface*, int, int);
  80. typedef void (* PFN_wl_egl_window_destroy)(struct wl_egl_window*);
  81. typedef void (* PFN_wl_egl_window_resize)(struct wl_egl_window*, int, int, int, int);
  82. #define wl_egl_window_create _glfw.wl.egl.window_create
  83. #define wl_egl_window_destroy _glfw.wl.egl.window_destroy
  84. #define wl_egl_window_resize _glfw.wl.egl.window_resize
  85. typedef enum _GLFWCSDSurface
  86. {
  87. CENTRAL_WINDOW, CSD_titlebar, CSD_shadow_top, CSD_shadow_left, CSD_shadow_bottom, CSD_shadow_right,
  88. CSD_shadow_upper_left, CSD_shadow_upper_right, CSD_shadow_lower_left, CSD_shadow_lower_right,
  89. } _GLFWCSDSurface;
  90. typedef struct _GLFWWaylandBufferPair {
  91. struct wl_buffer *a, *b, *front, *back;
  92. struct { uint8_t *a, *b, *front, *back; } data;
  93. bool has_pending_update;
  94. size_t size_in_bytes, width, height, viewport_width, viewport_height, stride;
  95. bool a_needs_to_be_destroyed, b_needs_to_be_destroyed;
  96. } _GLFWWaylandBufferPair;
  97. typedef struct _GLFWWaylandCSDSurface {
  98. struct wl_surface *surface;
  99. struct wl_subsurface *subsurface;
  100. struct wp_viewport *wp_viewport;
  101. _GLFWWaylandBufferPair buffer;
  102. int x, y;
  103. } _GLFWWaylandCSDSurface;
  104. typedef enum WaylandWindowState {
  105. TOPLEVEL_STATE_NONE = 0,
  106. TOPLEVEL_STATE_MAXIMIZED = 1,
  107. TOPLEVEL_STATE_FULLSCREEN = 2,
  108. TOPLEVEL_STATE_RESIZING = 4,
  109. TOPLEVEL_STATE_ACTIVATED = 8,
  110. TOPLEVEL_STATE_TILED_LEFT = 16,
  111. TOPLEVEL_STATE_TILED_RIGHT = 32,
  112. TOPLEVEL_STATE_TILED_TOP = 64,
  113. TOPLEVEL_STATE_TILED_BOTTOM = 128,
  114. TOPLEVEL_STATE_SUSPENDED = 256,
  115. } WaylandWindowState;
  116. typedef struct glfw_wl_xdg_activation_request {
  117. GLFWid window_id;
  118. GLFWactivationcallback callback;
  119. void *callback_data;
  120. uintptr_t request_id;
  121. void *token;
  122. } glfw_wl_xdg_activation_request;
  123. 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;
  124. enum WaylandWindowPendingState {
  125. PENDING_STATE_TOPLEVEL = 1,
  126. PENDING_STATE_DECORATION = 2
  127. };
  128. enum _GLFWWaylandAxisEvent {
  129. AXIS_EVENT_UNKNOWN = 0,
  130. AXIS_EVENT_CONTINUOUS = 1,
  131. AXIS_EVENT_DISCRETE = 2,
  132. AXIS_EVENT_VALUE120 = 3
  133. };
  134. // Wayland-specific per-window data
  135. //
  136. typedef struct _GLFWwindowWayland
  137. {
  138. int width, height;
  139. bool visible;
  140. bool hovered;
  141. bool transparent;
  142. struct wl_surface* surface;
  143. bool waiting_for_swap_to_commit;
  144. struct wl_egl_window* native;
  145. struct wl_callback* callback;
  146. struct {
  147. struct xdg_surface* surface;
  148. struct xdg_toplevel* toplevel;
  149. struct zxdg_toplevel_decoration_v1* decoration;
  150. struct { int width, height; } top_level_bounds;
  151. } xdg;
  152. struct wp_fractional_scale_v1 *wp_fractional_scale_v1;
  153. struct wp_viewport *wp_viewport;
  154. struct org_kde_kwin_blur *org_kde_kwin_blur;
  155. bool has_blur, expect_scale_from_compositor, window_fully_created;
  156. struct {
  157. bool surface_configured, fractional_scale_received, preferred_scale_received;
  158. } once;
  159. struct wl_buffer *temp_buffer_used_during_window_creation;
  160. struct {
  161. GLFWLayerShellConfig config;
  162. struct zwlr_layer_surface_v1* zwlr_layer_surface_v1;
  163. } layer_shell;
  164. /* information about axis events on current frame */
  165. struct
  166. {
  167. struct {
  168. enum _GLFWWaylandAxisEvent x_axis_type;
  169. float x;
  170. enum _GLFWWaylandAxisEvent y_axis_type;
  171. float y;
  172. } discrete, continuous;
  173. /* Event timestamp in nanoseconds */
  174. monotonic_t timestamp_ns;
  175. } pointer_curr_axis_info;
  176. _GLFWcursor* currentCursor;
  177. double cursorPosX, cursorPosY, allCursorPosX, allCursorPosY;
  178. char* title;
  179. char appId[256];
  180. // We need to track the monitors the window spans on to calculate the
  181. // optimal scaling factor.
  182. struct { uint32_t deduced, preferred; } integer_scale;
  183. uint32_t fractional_scale;
  184. bool initial_scale_notified;
  185. _GLFWmonitor** monitors;
  186. int monitorsCount;
  187. int monitorsSize;
  188. struct {
  189. struct zwp_relative_pointer_v1* relativePointer;
  190. struct zwp_locked_pointer_v1* lockedPointer;
  191. } pointerLock;
  192. struct {
  193. bool serverSide, buffer_destroyed, titlebar_needs_update;
  194. _GLFWCSDSurface focus;
  195. _GLFWWaylandCSDSurface titlebar, shadow_left, shadow_right, shadow_top, shadow_bottom, shadow_upper_left, shadow_upper_right, shadow_lower_left, shadow_lower_right;
  196. struct {
  197. uint8_t *data;
  198. size_t size;
  199. } mapping;
  200. struct {
  201. int width, height;
  202. bool focused, needs_shadow;
  203. double fscale;
  204. WaylandWindowState toplevel_states;
  205. } for_window_state;
  206. struct {
  207. unsigned int width, top, horizontal, vertical, visible_titlebar_height;
  208. } metrics;
  209. struct {
  210. int32_t x, y, width, height;
  211. } geometry;
  212. struct {
  213. bool hovered;
  214. int width, left;
  215. } minimize, maximize, close;
  216. struct {
  217. uint32_t *data;
  218. size_t for_decoration_size, stride, segments, corner_size;
  219. } shadow_tile;
  220. monotonic_t last_click_on_top_decoration_at;
  221. uint32_t titlebar_color;
  222. bool use_custom_titlebar_color;
  223. } decorations;
  224. struct {
  225. unsigned long long id;
  226. void(*callback)(unsigned long long id);
  227. struct wl_callback *current_wl_callback;
  228. } frameCallbackData;
  229. struct {
  230. int32_t width, height;
  231. } user_requested_content_size;
  232. struct {
  233. bool minimize, maximize, fullscreen, window_menu;
  234. } wm_capabilities;
  235. bool maximize_on_first_show;
  236. uint32_t pending_state;
  237. struct {
  238. int width, height;
  239. WaylandWindowState toplevel_states;
  240. uint32_t decoration_mode;
  241. } current, pending;
  242. } _GLFWwindowWayland;
  243. typedef enum _GLFWWaylandOfferType
  244. {
  245. EXPIRED,
  246. CLIPBOARD,
  247. DRAG_AND_DROP,
  248. PRIMARY_SELECTION
  249. }_GLFWWaylandOfferType ;
  250. typedef struct _GLFWWaylandDataOffer
  251. {
  252. void *id;
  253. _GLFWWaylandOfferType offer_type;
  254. size_t idx;
  255. bool is_self_offer;
  256. bool is_primary;
  257. const char *mime_for_drop;
  258. uint32_t source_actions;
  259. uint32_t dnd_action;
  260. struct wl_surface *surface;
  261. const char **mimes;
  262. size_t mimes_capacity, mimes_count;
  263. } _GLFWWaylandDataOffer;
  264. // Wayland-specific global data
  265. //
  266. typedef struct _GLFWlibraryWayland
  267. {
  268. struct wl_display* display;
  269. struct wl_registry* registry;
  270. struct wl_compositor* compositor;
  271. struct wl_subcompositor* subcompositor;
  272. struct wl_shm* shm;
  273. struct wl_seat* seat;
  274. struct wl_pointer* pointer;
  275. struct wl_keyboard* keyboard;
  276. struct wl_data_device_manager* dataDeviceManager;
  277. struct wl_data_device* dataDevice;
  278. struct xdg_wm_base* wmBase;
  279. int xdg_wm_base_version;
  280. struct zxdg_decoration_manager_v1* decorationManager;
  281. struct zwp_relative_pointer_manager_v1* relativePointerManager;
  282. struct zwp_pointer_constraints_v1* pointerConstraints;
  283. struct wl_data_source* dataSourceForClipboard;
  284. struct zwp_primary_selection_device_manager_v1* primarySelectionDeviceManager;
  285. struct zwp_primary_selection_device_v1* primarySelectionDevice;
  286. struct zwp_primary_selection_source_v1* dataSourceForPrimarySelection;
  287. struct xdg_activation_v1* xdg_activation_v1;
  288. struct wp_cursor_shape_manager_v1* wp_cursor_shape_manager_v1;
  289. struct wp_cursor_shape_device_v1* wp_cursor_shape_device_v1;
  290. struct wp_fractional_scale_manager_v1 *wp_fractional_scale_manager_v1;
  291. struct wp_viewporter *wp_viewporter;
  292. struct org_kde_kwin_blur_manager *org_kde_kwin_blur_manager;
  293. struct zwlr_layer_shell_v1* zwlr_layer_shell_v1; uint32_t zwlr_layer_shell_v1_version;
  294. struct wp_single_pixel_buffer_manager_v1 *wp_single_pixel_buffer_manager_v1;
  295. int compositorVersion;
  296. int seatVersion;
  297. struct wl_surface* cursorSurface;
  298. GLFWCursorShape cursorPreviousShape;
  299. uint32_t serial, input_serial, pointer_serial, pointer_enter_serial, keyboard_enter_serial;
  300. int32_t keyboardRepeatRate;
  301. monotonic_t keyboardRepeatDelay;
  302. struct {
  303. uint32_t key;
  304. id_type keyRepeatTimer;
  305. GLFWid keyboardFocusId;
  306. } keyRepeatInfo;
  307. id_type cursorAnimationTimer;
  308. _GLFWXKBData xkb;
  309. _GLFWDBUSData dbus;
  310. _GLFWwindow* pointerFocus;
  311. GLFWid keyboardFocusId;
  312. struct {
  313. void* handle;
  314. PFN_wl_cursor_theme_load theme_load;
  315. PFN_wl_cursor_theme_destroy theme_destroy;
  316. PFN_wl_cursor_theme_get_cursor theme_get_cursor;
  317. PFN_wl_cursor_image_get_buffer image_get_buffer;
  318. } cursor;
  319. struct {
  320. void* handle;
  321. PFN_wl_egl_window_create window_create;
  322. PFN_wl_egl_window_destroy window_destroy;
  323. PFN_wl_egl_window_resize window_resize;
  324. } egl;
  325. struct {
  326. glfw_wl_xdg_activation_request *array;
  327. size_t capacity, sz;
  328. } activation_requests;
  329. EventLoopData eventLoopData;
  330. size_t dataOffersCounter;
  331. _GLFWWaylandDataOffer dataOffers[8];
  332. bool has_preferred_buffer_scale;
  333. } _GLFWlibraryWayland;
  334. // Wayland-specific per-monitor data
  335. //
  336. typedef struct _GLFWmonitorWayland
  337. {
  338. struct wl_output* output;
  339. uint32_t name;
  340. char friendly_name[64], description[64];
  341. int currentMode;
  342. int x;
  343. int y;
  344. int scale;
  345. } _GLFWmonitorWayland;
  346. // Wayland-specific per-cursor data
  347. //
  348. typedef struct _GLFWcursorWayland
  349. {
  350. struct wl_cursor* cursor;
  351. struct wl_buffer* buffer;
  352. int width, height;
  353. int xhot, yhot;
  354. unsigned int currentImage;
  355. /** The scale of the cursor, or 0 if the cursor should be loaded late, or -1 if the cursor variable itself is unused. */
  356. int scale;
  357. /** Cursor shape stored to allow late cursor loading in setCursorImage. */
  358. GLFWCursorShape shape;
  359. } _GLFWcursorWayland;
  360. void _glfwAddOutputWayland(uint32_t name, uint32_t version);
  361. void _glfwWaylandBeforeBufferSwap(_GLFWwindow *window);
  362. void _glfwWaylandAfterBufferSwap(_GLFWwindow *window);
  363. void _glfwSetupWaylandDataDevice(void);
  364. void _glfwSetupWaylandPrimarySelectionDevice(void);
  365. double _glfwWaylandWindowScale(_GLFWwindow*);
  366. int _glfwWaylandIntegerWindowScale(_GLFWwindow*);
  367. void animateCursorImage(id_type timer_id, void *data);
  368. struct wl_cursor* _glfwLoadCursor(GLFWCursorShape, struct wl_cursor_theme*);
  369. void destroy_data_offer(_GLFWWaylandDataOffer*);
  370. typedef struct wayland_cursor_shape {
  371. int which; const char *name;
  372. } wayland_cursor_shape;
  373. wayland_cursor_shape
  374. glfw_cursor_shape_to_wayland_cursor_shape(GLFWCursorShape g);