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