wl_platform.h 14.3 KB
Newer Older
1
//========================================================================
Camilla Berglund's avatar
Camilla Berglund committed
2
// GLFW 3.3 Wayland - www.glfw.org
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//------------------------------------------------------------------------
// Copyright (c) 2014 Jonas Ådahl <jadahl@gmail.com>
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
//    claim that you wrote the original software. If you use this software
//    in a product, an acknowledgment in the product documentation would
//    be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
//    be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
//    distribution.
//
//========================================================================

27
#include <wayland-client.h>
Jonas Ådahl's avatar
Jonas Ådahl committed
28
#include <xkbcommon/xkbcommon.h>
29
#ifdef HAVE_XKBCOMMON_COMPOSE_H
30
#include <xkbcommon/xkbcommon-compose.h>
31
#endif
Camilla Berglund's avatar
Camilla Berglund committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <dlfcn.h>

typedef VkFlags VkWaylandSurfaceCreateFlagsKHR;

typedef struct VkWaylandSurfaceCreateInfoKHR
{
    VkStructureType                 sType;
    const void*                     pNext;
    VkWaylandSurfaceCreateFlagsKHR  flags;
    struct wl_display*              display;
    struct wl_surface*              surface;
} VkWaylandSurfaceCreateInfoKHR;

typedef VkResult (APIENTRY *PFN_vkCreateWaylandSurfaceKHR)(VkInstance,const VkWaylandSurfaceCreateInfoKHR*,const VkAllocationCallbacks*,VkSurfaceKHR*);
typedef VkBool32 (APIENTRY *PFN_vkGetPhysicalDeviceWaylandPresentationSupportKHR)(VkPhysicalDevice,uint32_t,struct wl_display*);
47

Camilla Löwy's avatar
Camilla Löwy committed
48
#include "posix_thread.h"
Camilla Berglund's avatar
Camilla Berglund committed
49
#include "posix_time.h"
50
#ifdef __linux__
Camilla Berglund's avatar
Camilla Berglund committed
51
#include "linux_joystick.h"
52
53
54
#else
#include "null_joystick.h"
#endif
Camilla Berglund's avatar
Camilla Berglund committed
55
#include "xkb_unicode.h"
56
#include "egl_context.h"
57
#include "osmesa_context.h"
58

59
#include "wayland-xdg-shell-client-protocol.h"
60
#include "wayland-xdg-decoration-client-protocol.h"
61
#include "wayland-viewporter-client-protocol.h"
62
63
#include "wayland-relative-pointer-unstable-v1-client-protocol.h"
#include "wayland-pointer-constraints-unstable-v1-client-protocol.h"
64
#include "wayland-idle-inhibit-unstable-v1-client-protocol.h"
65

Camilla Berglund's avatar
Camilla Berglund committed
66
67
68
69
#define _glfw_dlopen(name) dlopen(name, RTLD_LAZY | RTLD_LOCAL)
#define _glfw_dlclose(handle) dlclose(handle)
#define _glfw_dlsym(handle, name) dlsym(handle, name)

70
71
#define _GLFW_EGL_NATIVE_WINDOW         ((EGLNativeWindowType) window->wl.native)
#define _GLFW_EGL_NATIVE_DISPLAY        ((EGLNativeDisplayType) _glfw.wl.display)
72

73
74
75
#define _GLFW_PLATFORM_WINDOW_STATE         _GLFWwindowWayland  wl
#define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE _GLFWlibraryWayland wl
#define _GLFW_PLATFORM_MONITOR_STATE        _GLFWmonitorWayland wl
76
#define _GLFW_PLATFORM_CURSOR_STATE         _GLFWcursorWayland  wl
77

Corentin Wallez's avatar
Corentin Wallez committed
78
79
#define _GLFW_PLATFORM_CONTEXT_STATE         struct { int dummyContext; }
#define _GLFW_PLATFORM_LIBRARY_CONTEXT_STATE struct { int dummyLibraryContext; }
80
#define _GLFW_PLATFORM_USER_CONTEXT_STATE    struct { int dummyUserContext; }
81

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
struct wl_cursor_image {
    uint32_t width;
    uint32_t height;
    uint32_t hotspot_x;
    uint32_t hotspot_y;
    uint32_t delay;
};
struct wl_cursor {
    unsigned int image_count;
    struct wl_cursor_image** images;
    char* name;
};
typedef struct wl_cursor_theme* (* PFN_wl_cursor_theme_load)(const char*, int, struct wl_shm*);
typedef void (* PFN_wl_cursor_theme_destroy)(struct wl_cursor_theme*);
typedef struct wl_cursor* (* PFN_wl_cursor_theme_get_cursor)(struct wl_cursor_theme*, const char*);
typedef struct wl_buffer* (* PFN_wl_cursor_image_get_buffer)(struct wl_cursor_image*);
#define wl_cursor_theme_load _glfw.wl.cursor.theme_load
#define wl_cursor_theme_destroy _glfw.wl.cursor.theme_destroy
#define wl_cursor_theme_get_cursor _glfw.wl.cursor.theme_get_cursor
#define wl_cursor_image_get_buffer _glfw.wl.cursor.image_get_buffer

103
104
105
106
107
108
109
typedef struct wl_egl_window* (* PFN_wl_egl_window_create)(struct wl_surface*, int, int);
typedef void (* PFN_wl_egl_window_destroy)(struct wl_egl_window*);
typedef void (* PFN_wl_egl_window_resize)(struct wl_egl_window*, int, int, int, int);
#define wl_egl_window_create _glfw.wl.egl.window_create
#define wl_egl_window_destroy _glfw.wl.egl.window_destroy
#define wl_egl_window_resize _glfw.wl.egl.window_resize

110
111
112
113
114
typedef struct xkb_context* (* PFN_xkb_context_new)(enum xkb_context_flags);
typedef void (* PFN_xkb_context_unref)(struct xkb_context*);
typedef struct xkb_keymap* (* PFN_xkb_keymap_new_from_string)(struct xkb_context*, const char*, enum xkb_keymap_format, enum xkb_keymap_compile_flags);
typedef void (* PFN_xkb_keymap_unref)(struct xkb_keymap*);
typedef xkb_mod_index_t (* PFN_xkb_keymap_mod_get_index)(struct xkb_keymap*, const char*);
115
typedef int (* PFN_xkb_keymap_key_repeats)(struct xkb_keymap*, xkb_keycode_t);
116
117
118
119
120
121
122
123
124
125
typedef struct xkb_state* (* PFN_xkb_state_new)(struct xkb_keymap*);
typedef void (* PFN_xkb_state_unref)(struct xkb_state*);
typedef int (* PFN_xkb_state_key_get_syms)(struct xkb_state*, xkb_keycode_t, const xkb_keysym_t**);
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);
typedef xkb_mod_mask_t (* PFN_xkb_state_serialize_mods)(struct xkb_state*, enum xkb_state_component);
#define xkb_context_new _glfw.wl.xkb.context_new
#define xkb_context_unref _glfw.wl.xkb.context_unref
#define xkb_keymap_new_from_string _glfw.wl.xkb.keymap_new_from_string
#define xkb_keymap_unref _glfw.wl.xkb.keymap_unref
#define xkb_keymap_mod_get_index _glfw.wl.xkb.keymap_mod_get_index
126
#define xkb_keymap_key_repeats _glfw.wl.xkb.keymap_key_repeats
127
128
129
130
131
#define xkb_state_new _glfw.wl.xkb.state_new
#define xkb_state_unref _glfw.wl.xkb.state_unref
#define xkb_state_key_get_syms _glfw.wl.xkb.state_key_get_syms
#define xkb_state_update_mask _glfw.wl.xkb.state_update_mask
#define xkb_state_serialize_mods _glfw.wl.xkb.state_serialize_mods
132
133
134
135
136
137
138
139
140

#ifdef HAVE_XKBCOMMON_COMPOSE_H
typedef struct xkb_compose_table* (* PFN_xkb_compose_table_new_from_locale)(struct xkb_context*, const char*, enum xkb_compose_compile_flags);
typedef void (* PFN_xkb_compose_table_unref)(struct xkb_compose_table*);
typedef struct xkb_compose_state* (* PFN_xkb_compose_state_new)(struct xkb_compose_table*, enum xkb_compose_state_flags);
typedef void (* PFN_xkb_compose_state_unref)(struct xkb_compose_state*);
typedef enum xkb_compose_feed_result (* PFN_xkb_compose_state_feed)(struct xkb_compose_state*, xkb_keysym_t);
typedef enum xkb_compose_status (* PFN_xkb_compose_state_get_status)(struct xkb_compose_state*);
typedef xkb_keysym_t (* PFN_xkb_compose_state_get_one_sym)(struct xkb_compose_state*);
141
142
143
144
145
146
147
#define xkb_compose_table_new_from_locale _glfw.wl.xkb.compose_table_new_from_locale
#define xkb_compose_table_unref _glfw.wl.xkb.compose_table_unref
#define xkb_compose_state_new _glfw.wl.xkb.compose_state_new
#define xkb_compose_state_unref _glfw.wl.xkb.compose_state_unref
#define xkb_compose_state_feed _glfw.wl.xkb.compose_state_feed
#define xkb_compose_state_get_status _glfw.wl.xkb.compose_state_get_status
#define xkb_compose_state_get_one_sym _glfw.wl.xkb.compose_state_get_one_sym
148
#endif
149

150
151
#define _GLFW_DECORATION_WIDTH 4
#define _GLFW_DECORATION_TOP 24
152
153
#define _GLFW_DECORATION_VERTICAL (_GLFW_DECORATION_TOP + _GLFW_DECORATION_WIDTH)
#define _GLFW_DECORATION_HORIZONTAL (2 * _GLFW_DECORATION_WIDTH)
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171

typedef enum _GLFWdecorationSideWayland
{
    mainWindow,
    topDecoration,
    leftDecoration,
    rightDecoration,
    bottomDecoration,

} _GLFWdecorationSideWayland;

typedef struct _GLFWdecorationWayland
{
    struct wl_surface*          surface;
    struct wl_subsurface*       subsurface;
    struct wp_viewport*         viewport;

} _GLFWdecorationWayland;
172

Camilla Berglund's avatar
Camilla Berglund committed
173
174
// Wayland-specific per-window data
//
175
176
177
typedef struct _GLFWwindowWayland
{
    int                         width, height;
178
    GLFWbool                    visible;
179
    GLFWbool                    maximized;
180
    GLFWbool                    hovered;
181
    GLFWbool                    transparent;
182
183
    struct wl_surface*          surface;
    struct wl_egl_window*       native;
184
    struct wl_shell_surface*    shellSurface;
185
    struct wl_callback*         callback;
186

187
188
189
    struct {
        struct xdg_surface*     surface;
        struct xdg_toplevel*    toplevel;
190
        struct zxdg_toplevel_decoration_v1* decoration;
191
192
    } xdg;

193
    _GLFWcursor*                currentCursor;
194
    double                      cursorPosX, cursorPosY;
195

196
197
    char*                       title;

198
199
200
201
202
203
    // We need to track the monitors the window spans on to calculate the
    // optimal scaling factor.
    int                         scale;
    _GLFWmonitor**              monitors;
    int                         monitorsCount;
    int                         monitorsSize;
Camilla Berglund's avatar
Camilla Berglund committed
204

205
206
207
208
    struct {
        struct zwp_relative_pointer_v1*    relativePointer;
        struct zwp_locked_pointer_v1*      lockedPointer;
    } pointerLock;
209
210
211

    struct zwp_idle_inhibitor_v1*          idleInhibitor;

212
    GLFWbool                    wasFullscreen;
213

214
    struct {
215
        GLFWbool                           serverSide;
216
217
218
219
220
        struct wl_buffer*                  buffer;
        _GLFWdecorationWayland             top, left, right, bottom;
        int                                focus;
    } decorations;

221
222
} _GLFWwindowWayland;

Camilla Berglund's avatar
Camilla Berglund committed
223
224
// Wayland-specific global data
//
225
226
227
228
229
typedef struct _GLFWlibraryWayland
{
    struct wl_display*          display;
    struct wl_registry*         registry;
    struct wl_compositor*       compositor;
230
    struct wl_subcompositor*    subcompositor;
231
    struct wl_shell*            shell;
232
    struct wl_shm*              shm;
233
234
235
    struct wl_seat*             seat;
    struct wl_pointer*          pointer;
    struct wl_keyboard*         keyboard;
236
237
238
    struct wl_data_device_manager*          dataDeviceManager;
    struct wl_data_device*      dataDevice;
    struct wl_data_offer*       dataOffer;
239
    struct wl_data_source*      dataSource;
240
    struct xdg_wm_base*         wmBase;
241
    struct zxdg_decoration_manager_v1*      decorationManager;
242
    struct wp_viewporter*       viewporter;
243
244
    struct zwp_relative_pointer_manager_v1* relativePointerManager;
    struct zwp_pointer_constraints_v1*      pointerConstraints;
245
    struct zwp_idle_inhibit_manager_v1*     idleInhibitManager;
246

247
    int                         compositorVersion;
248
    int                         seatVersion;
249

250
    struct wl_cursor_theme*     cursorTheme;
251
    struct wl_cursor_theme*     cursorThemeHiDPI;
252
    struct wl_surface*          cursorSurface;
253
    const char*                 cursorPreviousName;
254
    int                         cursorTimerfd;
255
    uint32_t                    serial;
256

257
258
    int32_t                     keyboardRepeatRate;
    int32_t                     keyboardRepeatDelay;
259
260
    int                         keyboardLastKey;
    int                         keyboardLastScancode;
261
262
    char*                       clipboardString;
    size_t                      clipboardSize;
263
264
    char*                       clipboardSendString;
    size_t                      clipboardSendSize;
265
    int                         timerfd;
Camilla Berglund's avatar
Cleanup    
Camilla Berglund committed
266
267
    short int                   keycodes[256];
    short int                   scancodes[GLFW_KEY_LAST + 1];
268

Jonas Ådahl's avatar
Jonas Ådahl committed
269
    struct {
270
        void*                   handle;
Jonas Ådahl's avatar
Jonas Ådahl committed
271
272
273
        struct xkb_context*     context;
        struct xkb_keymap*      keymap;
        struct xkb_state*       state;
274
275

#ifdef HAVE_XKBCOMMON_COMPOSE_H
276
        struct xkb_compose_state* composeState;
277
278
#endif

279
280
281
282
        xkb_mod_mask_t          controlMask;
        xkb_mod_mask_t          altMask;
        xkb_mod_mask_t          shiftMask;
        xkb_mod_mask_t          superMask;
283
284
        xkb_mod_mask_t          capsLockMask;
        xkb_mod_mask_t          numLockMask;
Jonas Ådahl's avatar
Jonas Ådahl committed
285
        unsigned int            modifiers;
286
287
288
289
290
291

        PFN_xkb_context_new context_new;
        PFN_xkb_context_unref context_unref;
        PFN_xkb_keymap_new_from_string keymap_new_from_string;
        PFN_xkb_keymap_unref keymap_unref;
        PFN_xkb_keymap_mod_get_index keymap_mod_get_index;
292
        PFN_xkb_keymap_key_repeats keymap_key_repeats;
293
294
295
296
297
        PFN_xkb_state_new state_new;
        PFN_xkb_state_unref state_unref;
        PFN_xkb_state_key_get_syms state_key_get_syms;
        PFN_xkb_state_update_mask state_update_mask;
        PFN_xkb_state_serialize_mods state_serialize_mods;
298
299

#ifdef HAVE_XKBCOMMON_COMPOSE_H
300
301
302
303
304
305
306
        PFN_xkb_compose_table_new_from_locale compose_table_new_from_locale;
        PFN_xkb_compose_table_unref compose_table_unref;
        PFN_xkb_compose_state_new compose_state_new;
        PFN_xkb_compose_state_unref compose_state_unref;
        PFN_xkb_compose_state_feed compose_state_feed;
        PFN_xkb_compose_state_get_status compose_state_get_status;
        PFN_xkb_compose_state_get_one_sym compose_state_get_one_sym;
307
#endif
Jonas Ådahl's avatar
Jonas Ådahl committed
308
309
    } xkb;

310
311
    _GLFWwindow*                pointerFocus;
    _GLFWwindow*                keyboardFocus;
Camilla Berglund's avatar
Camilla Berglund committed
312

313
314
315
316
317
318
319
320
321
    struct {
        void*                   handle;

        PFN_wl_cursor_theme_load theme_load;
        PFN_wl_cursor_theme_destroy theme_destroy;
        PFN_wl_cursor_theme_get_cursor theme_get_cursor;
        PFN_wl_cursor_image_get_buffer image_get_buffer;
    } cursor;

322
323
324
325
326
327
328
329
    struct {
        void*                   handle;

        PFN_wl_egl_window_create window_create;
        PFN_wl_egl_window_destroy window_destroy;
        PFN_wl_egl_window_resize window_resize;
    } egl;

330
331
} _GLFWlibraryWayland;

Camilla Berglund's avatar
Camilla Berglund committed
332
333
// Wayland-specific per-monitor data
//
334
335
336
typedef struct _GLFWmonitorWayland
{
    struct wl_output*           output;
337
    uint32_t                    name;
338
    int                         currentMode;
339
340
341

    int                         x;
    int                         y;
342
    int                         scale;
343

344
345
} _GLFWmonitorWayland;

Camilla Berglund's avatar
Camilla Berglund committed
346
347
// Wayland-specific per-cursor data
//
348
349
typedef struct _GLFWcursorWayland
{
350
    struct wl_cursor*           cursor;
351
    struct wl_cursor*           cursorHiDPI;
352
353
354
    struct wl_buffer*           buffer;
    int                         width, height;
    int                         xhot, yhot;
355
    int                         currentImage;
Camilla Berglund's avatar
Camilla Berglund committed
356
} _GLFWcursorWayland;
357
358


359
void _glfwAddOutputWayland(uint32_t name, uint32_t version);
360