wl_platform.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. //========================================================================
  2. // GLFW 3.4 Wayland - www.glfw.org
  3. //------------------------------------------------------------------------
  4. // Copyright (c) 2014 Jonas Ådahl <[email protected]>
  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 <xkbcommon/xkbcommon.h>
  28. #ifdef HAVE_XKBCOMMON_COMPOSE_H
  29. #include <xkbcommon/xkbcommon-compose.h>
  30. #endif
  31. #include <dlfcn.h>
  32. typedef VkFlags VkWaylandSurfaceCreateFlagsKHR;
  33. typedef struct VkWaylandSurfaceCreateInfoKHR
  34. {
  35. VkStructureType sType;
  36. const void* pNext;
  37. VkWaylandSurfaceCreateFlagsKHR flags;
  38. struct wl_display* display;
  39. struct wl_surface* surface;
  40. } VkWaylandSurfaceCreateInfoKHR;
  41. typedef VkResult (APIENTRY *PFN_vkCreateWaylandSurfaceKHR)(VkInstance,const VkWaylandSurfaceCreateInfoKHR*,const VkAllocationCallbacks*,VkSurfaceKHR*);
  42. typedef VkBool32 (APIENTRY *PFN_vkGetPhysicalDeviceWaylandPresentationSupportKHR)(VkPhysicalDevice,uint32_t,struct wl_display*);
  43. #include "posix_thread.h"
  44. #include "posix_time.h"
  45. #ifdef __linux__
  46. #include "linux_joystick.h"
  47. #else
  48. #include "null_joystick.h"
  49. #endif
  50. #include "xkb_unicode.h"
  51. #include "egl_context.h"
  52. #include "osmesa_context.h"
  53. #include "wayland-xdg-shell-client-protocol.h"
  54. #include "wayland-xdg-decoration-client-protocol.h"
  55. #include "wayland-viewporter-client-protocol.h"
  56. #include "wayland-relative-pointer-unstable-v1-client-protocol.h"
  57. #include "wayland-pointer-constraints-unstable-v1-client-protocol.h"
  58. #include "wayland-idle-inhibit-unstable-v1-client-protocol.h"
  59. #define _glfw_dlopen(name) dlopen(name, RTLD_LAZY | RTLD_LOCAL)
  60. #define _glfw_dlclose(handle) dlclose(handle)
  61. #define _glfw_dlsym(handle, name) dlsym(handle, name)
  62. #define _GLFW_EGL_NATIVE_WINDOW ((EGLNativeWindowType) window->wl.native)
  63. #define _GLFW_EGL_NATIVE_DISPLAY ((EGLNativeDisplayType) _glfw.wl.display)
  64. #define _GLFW_PLATFORM_WINDOW_STATE _GLFWwindowWayland wl
  65. #define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE _GLFWlibraryWayland wl
  66. #define _GLFW_PLATFORM_MONITOR_STATE _GLFWmonitorWayland wl
  67. #define _GLFW_PLATFORM_CURSOR_STATE _GLFWcursorWayland wl
  68. #define _GLFW_PLATFORM_CONTEXT_STATE struct { int dummyContext; }
  69. #define _GLFW_PLATFORM_LIBRARY_CONTEXT_STATE struct { int dummyLibraryContext; }
  70. struct wl_cursor_image {
  71. uint32_t width;
  72. uint32_t height;
  73. uint32_t hotspot_x;
  74. uint32_t hotspot_y;
  75. uint32_t delay;
  76. };
  77. struct wl_cursor {
  78. unsigned int image_count;
  79. struct wl_cursor_image** images;
  80. char* name;
  81. };
  82. typedef struct wl_cursor_theme* (* PFN_wl_cursor_theme_load)(const char*, int, struct wl_shm*);
  83. typedef void (* PFN_wl_cursor_theme_destroy)(struct wl_cursor_theme*);
  84. typedef struct wl_cursor* (* PFN_wl_cursor_theme_get_cursor)(struct wl_cursor_theme*, const char*);
  85. typedef struct wl_buffer* (* PFN_wl_cursor_image_get_buffer)(struct wl_cursor_image*);
  86. #define wl_cursor_theme_load _glfw.wl.cursor.theme_load
  87. #define wl_cursor_theme_destroy _glfw.wl.cursor.theme_destroy
  88. #define wl_cursor_theme_get_cursor _glfw.wl.cursor.theme_get_cursor
  89. #define wl_cursor_image_get_buffer _glfw.wl.cursor.image_get_buffer
  90. typedef struct wl_egl_window* (* PFN_wl_egl_window_create)(struct wl_surface*, int, int);
  91. typedef void (* PFN_wl_egl_window_destroy)(struct wl_egl_window*);
  92. typedef void (* PFN_wl_egl_window_resize)(struct wl_egl_window*, int, int, int, int);
  93. #define wl_egl_window_create _glfw.wl.egl.window_create
  94. #define wl_egl_window_destroy _glfw.wl.egl.window_destroy
  95. #define wl_egl_window_resize _glfw.wl.egl.window_resize
  96. typedef struct xkb_context* (* PFN_xkb_context_new)(enum xkb_context_flags);
  97. typedef void (* PFN_xkb_context_unref)(struct xkb_context*);
  98. typedef struct xkb_keymap* (* PFN_xkb_keymap_new_from_string)(struct xkb_context*, const char*, enum xkb_keymap_format, enum xkb_keymap_compile_flags);
  99. typedef void (* PFN_xkb_keymap_unref)(struct xkb_keymap*);
  100. typedef xkb_mod_index_t (* PFN_xkb_keymap_mod_get_index)(struct xkb_keymap*, const char*);
  101. typedef int (* PFN_xkb_keymap_key_repeats)(struct xkb_keymap*, xkb_keycode_t);
  102. typedef struct xkb_state* (* PFN_xkb_state_new)(struct xkb_keymap*);
  103. typedef void (* PFN_xkb_state_unref)(struct xkb_state*);
  104. typedef int (* PFN_xkb_state_key_get_syms)(struct xkb_state*, xkb_keycode_t, const xkb_keysym_t**);
  105. typedef enum xkb_state_component (* PFN_xkb_state_update_mask)(struct xkb_state*, xkb_mod_mask_t, xkb_mod_mask_t, xkb_mod_mask_t, xkb_layout_index_t, xkb_layout_index_t, xkb_layout_index_t);
  106. typedef xkb_mod_mask_t (* PFN_xkb_state_serialize_mods)(struct xkb_state*, enum xkb_state_component);
  107. #define xkb_context_new _glfw.wl.xkb.context_new
  108. #define xkb_context_unref _glfw.wl.xkb.context_unref
  109. #define xkb_keymap_new_from_string _glfw.wl.xkb.keymap_new_from_string
  110. #define xkb_keymap_unref _glfw.wl.xkb.keymap_unref
  111. #define xkb_keymap_mod_get_index _glfw.wl.xkb.keymap_mod_get_index
  112. #define xkb_keymap_key_repeats _glfw.wl.xkb.keymap_key_repeats
  113. #define xkb_state_new _glfw.wl.xkb.state_new
  114. #define xkb_state_unref _glfw.wl.xkb.state_unref
  115. #define xkb_state_key_get_syms _glfw.wl.xkb.state_key_get_syms
  116. #define xkb_state_update_mask _glfw.wl.xkb.state_update_mask
  117. #define xkb_state_serialize_mods _glfw.wl.xkb.state_serialize_mods
  118. #ifdef HAVE_XKBCOMMON_COMPOSE_H
  119. typedef struct xkb_compose_table* (* PFN_xkb_compose_table_new_from_locale)(struct xkb_context*, const char*, enum xkb_compose_compile_flags);
  120. typedef void (* PFN_xkb_compose_table_unref)(struct xkb_compose_table*);
  121. typedef struct xkb_compose_state* (* PFN_xkb_compose_state_new)(struct xkb_compose_table*, enum xkb_compose_state_flags);
  122. typedef void (* PFN_xkb_compose_state_unref)(struct xkb_compose_state*);
  123. typedef enum xkb_compose_feed_result (* PFN_xkb_compose_state_feed)(struct xkb_compose_state*, xkb_keysym_t);
  124. typedef enum xkb_compose_status (* PFN_xkb_compose_state_get_status)(struct xkb_compose_state*);
  125. typedef xkb_keysym_t (* PFN_xkb_compose_state_get_one_sym)(struct xkb_compose_state*);
  126. #define xkb_compose_table_new_from_locale _glfw.wl.xkb.compose_table_new_from_locale
  127. #define xkb_compose_table_unref _glfw.wl.xkb.compose_table_unref
  128. #define xkb_compose_state_new _glfw.wl.xkb.compose_state_new
  129. #define xkb_compose_state_unref _glfw.wl.xkb.compose_state_unref
  130. #define xkb_compose_state_feed _glfw.wl.xkb.compose_state_feed
  131. #define xkb_compose_state_get_status _glfw.wl.xkb.compose_state_get_status
  132. #define xkb_compose_state_get_one_sym _glfw.wl.xkb.compose_state_get_one_sym
  133. #endif
  134. #define _GLFW_DECORATION_WIDTH 4
  135. #define _GLFW_DECORATION_TOP 24
  136. #define _GLFW_DECORATION_VERTICAL (_GLFW_DECORATION_TOP + _GLFW_DECORATION_WIDTH)
  137. #define _GLFW_DECORATION_HORIZONTAL (2 * _GLFW_DECORATION_WIDTH)
  138. typedef enum _GLFWdecorationSideWayland
  139. {
  140. mainWindow,
  141. topDecoration,
  142. leftDecoration,
  143. rightDecoration,
  144. bottomDecoration,
  145. } _GLFWdecorationSideWayland;
  146. typedef struct _GLFWdecorationWayland
  147. {
  148. struct wl_surface* surface;
  149. struct wl_subsurface* subsurface;
  150. struct wp_viewport* viewport;
  151. } _GLFWdecorationWayland;
  152. // Wayland-specific per-window data
  153. //
  154. typedef struct _GLFWwindowWayland
  155. {
  156. int width, height;
  157. GLFWbool visible;
  158. GLFWbool maximized;
  159. GLFWbool hovered;
  160. GLFWbool transparent;
  161. struct wl_surface* surface;
  162. struct wl_egl_window* native;
  163. struct wl_callback* callback;
  164. struct {
  165. struct xdg_surface* surface;
  166. struct xdg_toplevel* toplevel;
  167. struct zxdg_toplevel_decoration_v1* decoration;
  168. } xdg;
  169. _GLFWcursor* currentCursor;
  170. double cursorPosX, cursorPosY;
  171. char* title;
  172. // We need to track the monitors the window spans on to calculate the
  173. // optimal scaling factor.
  174. int scale;
  175. _GLFWmonitor** monitors;
  176. int monitorsCount;
  177. int monitorsSize;
  178. struct {
  179. struct zwp_relative_pointer_v1* relativePointer;
  180. struct zwp_locked_pointer_v1* lockedPointer;
  181. } pointerLock;
  182. struct zwp_idle_inhibitor_v1* idleInhibitor;
  183. GLFWbool wasFullscreen;
  184. struct {
  185. GLFWbool serverSide;
  186. struct wl_buffer* buffer;
  187. _GLFWdecorationWayland top, left, right, bottom;
  188. int focus;
  189. } decorations;
  190. } _GLFWwindowWayland;
  191. // Wayland-specific global data
  192. //
  193. typedef struct _GLFWlibraryWayland
  194. {
  195. struct wl_display* display;
  196. struct wl_registry* registry;
  197. struct wl_compositor* compositor;
  198. struct wl_subcompositor* subcompositor;
  199. struct wl_shm* shm;
  200. struct wl_seat* seat;
  201. struct wl_pointer* pointer;
  202. struct wl_keyboard* keyboard;
  203. struct wl_data_device_manager* dataDeviceManager;
  204. struct wl_data_device* dataDevice;
  205. struct wl_data_offer* dataOffer;
  206. struct wl_data_source* dataSource;
  207. struct xdg_wm_base* wmBase;
  208. struct zxdg_decoration_manager_v1* decorationManager;
  209. struct wp_viewporter* viewporter;
  210. struct zwp_relative_pointer_manager_v1* relativePointerManager;
  211. struct zwp_pointer_constraints_v1* pointerConstraints;
  212. struct zwp_idle_inhibit_manager_v1* idleInhibitManager;
  213. int compositorVersion;
  214. int seatVersion;
  215. struct wl_cursor_theme* cursorTheme;
  216. struct wl_cursor_theme* cursorThemeHiDPI;
  217. struct wl_surface* cursorSurface;
  218. const char* cursorPreviousName;
  219. int cursorTimerfd;
  220. uint32_t serial;
  221. int32_t keyboardRepeatRate;
  222. int32_t keyboardRepeatDelay;
  223. int keyboardLastKey;
  224. int keyboardLastScancode;
  225. char* clipboardString;
  226. size_t clipboardSize;
  227. char* clipboardSendString;
  228. size_t clipboardSendSize;
  229. int timerfd;
  230. short int keycodes[256];
  231. short int scancodes[GLFW_KEY_LAST + 1];
  232. struct {
  233. void* handle;
  234. struct xkb_context* context;
  235. struct xkb_keymap* keymap;
  236. struct xkb_state* state;
  237. #ifdef HAVE_XKBCOMMON_COMPOSE_H
  238. struct xkb_compose_state* composeState;
  239. #endif
  240. xkb_mod_mask_t controlMask;
  241. xkb_mod_mask_t altMask;
  242. xkb_mod_mask_t shiftMask;
  243. xkb_mod_mask_t superMask;
  244. xkb_mod_mask_t capsLockMask;
  245. xkb_mod_mask_t numLockMask;
  246. unsigned int modifiers;
  247. PFN_xkb_context_new context_new;
  248. PFN_xkb_context_unref context_unref;
  249. PFN_xkb_keymap_new_from_string keymap_new_from_string;
  250. PFN_xkb_keymap_unref keymap_unref;
  251. PFN_xkb_keymap_mod_get_index keymap_mod_get_index;
  252. PFN_xkb_keymap_key_repeats keymap_key_repeats;
  253. PFN_xkb_state_new state_new;
  254. PFN_xkb_state_unref state_unref;
  255. PFN_xkb_state_key_get_syms state_key_get_syms;
  256. PFN_xkb_state_update_mask state_update_mask;
  257. PFN_xkb_state_serialize_mods state_serialize_mods;
  258. #ifdef HAVE_XKBCOMMON_COMPOSE_H
  259. PFN_xkb_compose_table_new_from_locale compose_table_new_from_locale;
  260. PFN_xkb_compose_table_unref compose_table_unref;
  261. PFN_xkb_compose_state_new compose_state_new;
  262. PFN_xkb_compose_state_unref compose_state_unref;
  263. PFN_xkb_compose_state_feed compose_state_feed;
  264. PFN_xkb_compose_state_get_status compose_state_get_status;
  265. PFN_xkb_compose_state_get_one_sym compose_state_get_one_sym;
  266. #endif
  267. } xkb;
  268. _GLFWwindow* pointerFocus;
  269. _GLFWwindow* keyboardFocus;
  270. struct {
  271. void* handle;
  272. PFN_wl_cursor_theme_load theme_load;
  273. PFN_wl_cursor_theme_destroy theme_destroy;
  274. PFN_wl_cursor_theme_get_cursor theme_get_cursor;
  275. PFN_wl_cursor_image_get_buffer image_get_buffer;
  276. } cursor;
  277. struct {
  278. void* handle;
  279. PFN_wl_egl_window_create window_create;
  280. PFN_wl_egl_window_destroy window_destroy;
  281. PFN_wl_egl_window_resize window_resize;
  282. } egl;
  283. } _GLFWlibraryWayland;
  284. // Wayland-specific per-monitor data
  285. //
  286. typedef struct _GLFWmonitorWayland
  287. {
  288. struct wl_output* output;
  289. uint32_t name;
  290. int currentMode;
  291. int x;
  292. int y;
  293. int scale;
  294. } _GLFWmonitorWayland;
  295. // Wayland-specific per-cursor data
  296. //
  297. typedef struct _GLFWcursorWayland
  298. {
  299. struct wl_cursor* cursor;
  300. struct wl_cursor* cursorHiDPI;
  301. struct wl_buffer* buffer;
  302. int width, height;
  303. int xhot, yhot;
  304. int currentImage;
  305. } _GLFWcursorWayland;
  306. void _glfwAddOutputWayland(uint32_t name, uint32_t version);