Skip to content

Latest commit

 

History

History
387 lines (313 loc) · 10.7 KB

SDL_waylandvideo.c

File metadata and controls

387 lines (313 loc) · 10.7 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
Simple DirectMedia Layer
Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
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.
*/
#include "../../SDL_internal.h"
#if SDL_VIDEO_DRIVER_WAYLAND
#include "SDL_video.h"
#include "SDL_mouse.h"
#include "SDL_stdinc.h"
#include "../../events/SDL_events_c.h"
#include "SDL_waylandvideo.h"
#include "SDL_waylandevents_c.h"
#include "SDL_waylandwindow.h"
#include "SDL_waylandopengles.h"
#include "SDL_waylandmouse.h"
#include "SDL_waylandtouch.h"
#include <fcntl.h>
#include <xkbcommon/xkbcommon.h>
#include "SDL_waylanddyn.h"
#include <wayland-util.h>
#define WAYLANDVID_DRIVER_NAME "wayland"
/* Initialization/Query functions */
static int
Wayland_VideoInit(_THIS);
static void
Wayland_GetDisplayModes(_THIS, SDL_VideoDisplay *sdl_display);
static int
Wayland_SetDisplayMode(_THIS, SDL_VideoDisplay *display, SDL_DisplayMode *mode);
static void
Wayland_VideoQuit(_THIS);
/* Wayland driver bootstrap functions */
static int
Wayland_Available(void)
{
struct wl_display *display = NULL;
if (SDL_WAYLAND_LoadSymbols()) {
display = WAYLAND_wl_display_connect(NULL);
if (display != NULL) {
WAYLAND_wl_display_disconnect(display);
}
SDL_WAYLAND_UnloadSymbols();
}
return (display != NULL);
}
static void
Wayland_DeleteDevice(SDL_VideoDevice *device)
{
SDL_free(device);
SDL_WAYLAND_UnloadSymbols();
}
static SDL_VideoDevice *
Wayland_CreateDevice(int devindex)
{
SDL_VideoDevice *device;
Mar 18, 2015
Mar 18, 2015
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
if (!SDL_WAYLAND_LoadSymbols()) {
return NULL;
}
/* Initialize all variables that we clean on shutdown */
device = SDL_calloc(1, sizeof(SDL_VideoDevice));
if (!device) {
SDL_WAYLAND_UnloadSymbols();
SDL_OutOfMemory();
return NULL;
}
/* Set the function pointers */
device->VideoInit = Wayland_VideoInit;
device->VideoQuit = Wayland_VideoQuit;
device->SetDisplayMode = Wayland_SetDisplayMode;
device->GetDisplayModes = Wayland_GetDisplayModes;
device->GetWindowWMInfo = Wayland_GetWindowWMInfo;
device->PumpEvents = Wayland_PumpEvents;
device->GL_SwapWindow = Wayland_GLES_SwapWindow;
device->GL_GetSwapInterval = Wayland_GLES_GetSwapInterval;
device->GL_SetSwapInterval = Wayland_GLES_SetSwapInterval;
device->GL_MakeCurrent = Wayland_GLES_MakeCurrent;
device->GL_CreateContext = Wayland_GLES_CreateContext;
device->GL_LoadLibrary = Wayland_GLES_LoadLibrary;
device->GL_UnloadLibrary = Wayland_GLES_UnloadLibrary;
device->GL_GetProcAddress = Wayland_GLES_GetProcAddress;
device->GL_DeleteContext = Wayland_GLES_DeleteContext;
device->CreateWindow = Wayland_CreateWindow;
device->ShowWindow = Wayland_ShowWindow;
device->SetWindowFullscreen = Wayland_SetWindowFullscreen;
device->SetWindowSize = Wayland_SetWindowSize;
device->DestroyWindow = Wayland_DestroyWindow;
device->free = Wayland_DeleteDevice;
return device;
}
VideoBootStrap Wayland_bootstrap = {
WAYLANDVID_DRIVER_NAME, "SDL Wayland video driver",
Wayland_Available, Wayland_CreateDevice
};
static void
display_handle_geometry(void *data,
struct wl_output *output,
int x, int y,
int physical_width,
int physical_height,
int subpixel,
const char *make,
const char *model,
int transform)
{
Mar 18, 2015
Mar 18, 2015
145
SDL_VideoDisplay *display = data;
Mar 18, 2015
Mar 18, 2015
147
148
display->name = strdup(model);
display->driverdata = output;
149
150
151
152
}
static void
display_handle_mode(void *data,
Mar 18, 2015
Mar 18, 2015
153
struct wl_output *output,
154
155
156
157
158
uint32_t flags,
int width,
int height,
int refresh)
{
Mar 18, 2015
Mar 18, 2015
159
SDL_VideoDisplay *display = data;
160
161
162
163
164
SDL_DisplayMode mode;
SDL_zero(mode);
mode.w = width;
mode.h = height;
Mar 18, 2015
Mar 18, 2015
165
166
mode.refresh_rate = refresh / 1000; // mHz to Hz
SDL_AddDisplayMode(display, &mode);
167
168
if (flags & WL_OUTPUT_MODE_CURRENT) {
Mar 18, 2015
Mar 18, 2015
169
170
display->current_mode = mode;
display->desktop_mode = mode;
Mar 18, 2015
Mar 18, 2015
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
static void
display_handle_done(void *data,
struct wl_output *output)
{
SDL_VideoDisplay *display = data;
SDL_AddVideoDisplay(display);
SDL_free(display->name);
SDL_free(display);
}
static void
display_handle_scale(void *data,
struct wl_output *output,
int32_t factor)
{
// TODO: do HiDPI stuff.
}
192
193
static const struct wl_output_listener output_listener = {
display_handle_geometry,
Mar 18, 2015
Mar 18, 2015
194
195
196
display_handle_mode,
display_handle_done,
display_handle_scale
Mar 18, 2015
Mar 18, 2015
200
Wayland_add_display(SDL_VideoData *d, uint32_t id)
Mar 18, 2015
Mar 18, 2015
202
203
204
205
206
207
SDL_VideoDisplay *display = SDL_malloc(sizeof *display);
if (!display) {
SDL_OutOfMemory();
return;
}
SDL_zero(*display);
Mar 18, 2015
Mar 18, 2015
209
210
211
212
213
struct wl_output *output = wl_registry_bind(d->registry, id, &wl_output_interface, 2);
if (!output) {
SDL_SetError("Failed to retrieve output.");
return;
}
Mar 18, 2015
Mar 18, 2015
215
216
wl_output_add_listener(output, &output_listener, display);
}
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
static void
windowmanager_hints(void *data, struct qt_windowmanager *qt_windowmanager,
int32_t show_is_fullscreen)
{
}
static void
windowmanager_quit(void *data, struct qt_windowmanager *qt_windowmanager)
{
SDL_SendQuit();
}
static const struct qt_windowmanager_listener windowmanager_listener = {
windowmanager_hints,
windowmanager_quit,
};
#endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */
static void
display_handle_global(void *data, struct wl_registry *registry, uint32_t id,
Mar 18, 2015
Mar 18, 2015
239
const char *interface, uint32_t version)
240
241
{
SDL_VideoData *d = data;
Mar 18, 2015
Mar 18, 2015
242
243
244
245
if (strcmp(interface, "wl_compositor") == 0) {
d->compositor = wl_registry_bind(d->registry, id, &wl_compositor_interface, 1);
} else if (strcmp(interface, "wl_output") == 0) {
Mar 18, 2015
Mar 18, 2015
246
Wayland_add_display(d, id);
247
248
249
250
251
252
253
254
} else if (strcmp(interface, "wl_seat") == 0) {
Wayland_display_add_input(d, id);
} else if (strcmp(interface, "wl_shell") == 0) {
d->shell = wl_registry_bind(d->registry, id, &wl_shell_interface, 1);
} else if (strcmp(interface, "wl_shm") == 0) {
d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
d->cursor_theme = WAYLAND_wl_cursor_theme_load(NULL, 32, d->shm);
d->default_cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
Mar 18, 2015
Mar 18, 2015
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
} else if (strcmp(interface, "qt_touch_extension") == 0) {
Wayland_touch_create(d, id);
} else if (strcmp(interface, "qt_surface_extension") == 0) {
d->surface_extension = wl_registry_bind(registry, id,
&qt_surface_extension_interface, 1);
} else if (strcmp(interface, "qt_windowmanager") == 0) {
d->windowmanager = wl_registry_bind(registry, id,
&qt_windowmanager_interface, 1);
qt_windowmanager_add_listener(d->windowmanager, &windowmanager_listener, d);
#endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */
}
}
static const struct wl_registry_listener registry_listener = {
Mar 18, 2015
Mar 18, 2015
271
display_handle_global
272
273
274
275
276
};
int
Wayland_VideoInit(_THIS)
{
Mar 18, 2015
Mar 18, 2015
277
SDL_VideoData *data = SDL_malloc(sizeof *data);
278
279
280
281
282
283
284
285
286
287
288
289
290
if (data == NULL)
return 0;
memset(data, 0, sizeof *data);
_this->driverdata = data;
data->display = WAYLAND_wl_display_connect(NULL);
if (data->display == NULL) {
SDL_SetError("Failed to connect to a Wayland display");
return 0;
}
data->registry = wl_display_get_registry(data->display);
Mar 18, 2015
Mar 18, 2015
291
if (data->registry == NULL) {
292
293
294
SDL_SetError("Failed to get the Wayland registry");
return 0;
}
Mar 18, 2015
Mar 18, 2015
295
296
297
wl_registry_add_listener(data->registry, &registry_listener, data);
Mar 18, 2015
Mar 18, 2015
298
299
300
301
302
// First roundtrip to receive all registry objects.
wl_display_roundtrip(data->display);
// Second roundtrip to receive all output events.
wl_display_roundtrip(data->display);
303
304
305
306
307
308
309
data->xkb_context = WAYLAND_xkb_context_new(0);
if (!data->xkb_context) {
SDL_SetError("Failed to create XKB context");
return 0;
}
Mar 18, 2015
Mar 18, 2015
310
Wayland_InitMouse();
311
312
313
314
315
316
317
318
319
WAYLAND_wl_display_flush(data->display);
return 0;
}
static void
Wayland_GetDisplayModes(_THIS, SDL_VideoDisplay *sdl_display)
{
Mar 18, 2015
Mar 18, 2015
320
321
// Nothing to do here, everything was already done in the wl_output
// callbacks.
322
323
324
325
326
}
static int
Wayland_SetDisplayMode(_THIS, SDL_VideoDisplay *display, SDL_DisplayMode *mode)
{
Mar 18, 2015
Mar 18, 2015
327
328
SDL_SetError("SetDisplayMode not (yet?) supported on Wayland.");
return -1;
329
330
331
332
333
334
}
void
Wayland_VideoQuit(_THIS)
{
SDL_VideoData *data = _this->driverdata;
Mar 18, 2015
Mar 18, 2015
335
int i;
336
337
338
Wayland_FiniMouse ();
Mar 18, 2015
Mar 18, 2015
339
340
341
342
343
for (i = 0; i < _this->num_displays; ++i) {
SDL_VideoDisplay *display = &_this->displays[i];
wl_output_destroy(display->driverdata);
display->driverdata = NULL;
}
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
Wayland_display_destroy_input(data);
if (data->xkb_context) {
WAYLAND_xkb_context_unref(data->xkb_context);
data->xkb_context = NULL;
}
#ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
if (data->windowmanager)
qt_windowmanager_destroy(data->windowmanager);
if (data->surface_extension)
qt_surface_extension_destroy(data->surface_extension);
Wayland_touch_destroy(data);
#endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */
if (data->shm)
wl_shm_destroy(data->shm);
if (data->cursor_theme)
WAYLAND_wl_cursor_theme_destroy(data->cursor_theme);
if (data->shell)
wl_shell_destroy(data->shell);
if (data->compositor)
wl_compositor_destroy(data->compositor);
Mar 18, 2015
Mar 18, 2015
373
374
375
if (data->registry)
wl_registry_destroy(data->registry);
376
377
378
379
380
381
382
383
384
385
386
387
if (data->display) {
WAYLAND_wl_display_flush(data->display);
WAYLAND_wl_display_disconnect(data->display);
}
free(data);
_this->driverdata = NULL;
}
#endif /* SDL_VIDEO_DRIVER_WAYLAND */
/* vi: set ts=4 sw=4 expandtab: */