2 Simple DirectMedia Layer
3 Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
22 #include "../../SDL_internal.h"
24 #if SDL_VIDEO_DRIVER_WAYLAND
26 #include "SDL_video.h"
27 #include "SDL_mouse.h"
28 #include "SDL_stdinc.h"
29 #include "../../events/SDL_events_c.h"
31 #include "SDL_waylandvideo.h"
32 #include "SDL_waylandevents_c.h"
33 #include "SDL_waylandwindow.h"
34 #include "SDL_waylandopengles.h"
35 #include "SDL_waylandmouse.h"
36 #include "SDL_waylandtouch.h"
39 #include <xkbcommon/xkbcommon.h>
41 #include "SDL_waylanddyn.h"
42 #include <wayland-util.h>
44 #define WAYLANDVID_DRIVER_NAME "wayland"
46 /* Initialization/Query functions */
48 Wayland_VideoInit(_THIS);
51 Wayland_GetDisplayModes(_THIS, SDL_VideoDisplay *sdl_display);
53 Wayland_SetDisplayMode(_THIS, SDL_VideoDisplay *display, SDL_DisplayMode *mode);
56 Wayland_VideoQuit(_THIS);
58 /* Wayland driver bootstrap functions */
60 Wayland_Available(void)
62 struct wl_display *display = NULL;
63 if (SDL_WAYLAND_LoadSymbols()) {
64 display = WAYLAND_wl_display_connect(NULL);
65 if (display != NULL) {
66 WAYLAND_wl_display_disconnect(display);
68 SDL_WAYLAND_UnloadSymbols();
71 return (display != NULL);
75 Wayland_DeleteDevice(SDL_VideoDevice *device)
78 SDL_WAYLAND_UnloadSymbols();
81 static SDL_VideoDevice *
82 Wayland_CreateDevice(int devindex)
84 SDL_VideoDevice *device;
86 if (!SDL_WAYLAND_LoadSymbols()) {
90 /* Initialize all variables that we clean on shutdown */
91 device = SDL_calloc(1, sizeof(SDL_VideoDevice));
93 SDL_WAYLAND_UnloadSymbols();
98 /* Set the function pointers */
99 device->VideoInit = Wayland_VideoInit;
100 device->VideoQuit = Wayland_VideoQuit;
101 device->SetDisplayMode = Wayland_SetDisplayMode;
102 device->GetDisplayModes = Wayland_GetDisplayModes;
103 device->GetWindowWMInfo = Wayland_GetWindowWMInfo;
105 device->PumpEvents = Wayland_PumpEvents;
107 device->GL_SwapWindow = Wayland_GLES_SwapWindow;
108 device->GL_GetSwapInterval = Wayland_GLES_GetSwapInterval;
109 device->GL_SetSwapInterval = Wayland_GLES_SetSwapInterval;
110 device->GL_MakeCurrent = Wayland_GLES_MakeCurrent;
111 device->GL_CreateContext = Wayland_GLES_CreateContext;
112 device->GL_LoadLibrary = Wayland_GLES_LoadLibrary;
113 device->GL_UnloadLibrary = Wayland_GLES_UnloadLibrary;
114 device->GL_GetProcAddress = Wayland_GLES_GetProcAddress;
115 device->GL_DeleteContext = Wayland_GLES_DeleteContext;
117 device->CreateWindow = Wayland_CreateWindow;
118 device->ShowWindow = Wayland_ShowWindow;
119 device->SetWindowFullscreen = Wayland_SetWindowFullscreen;
120 device->SetWindowSize = Wayland_SetWindowSize;
121 device->DestroyWindow = Wayland_DestroyWindow;
122 device->SetWindowHitTest = Wayland_SetWindowHitTest;
124 device->free = Wayland_DeleteDevice;
129 VideoBootStrap Wayland_bootstrap = {
130 WAYLANDVID_DRIVER_NAME, "SDL Wayland video driver",
131 Wayland_Available, Wayland_CreateDevice
135 display_handle_geometry(void *data,
136 struct wl_output *output,
146 SDL_VideoDisplay *display = data;
148 display->name = strdup(model);
149 display->driverdata = output;
153 display_handle_mode(void *data,
154 struct wl_output *output,
160 SDL_VideoDisplay *display = data;
161 SDL_DisplayMode mode;
166 mode.refresh_rate = refresh / 1000; // mHz to Hz
167 SDL_AddDisplayMode(display, &mode);
169 if (flags & WL_OUTPUT_MODE_CURRENT) {
170 display->current_mode = mode;
171 display->desktop_mode = mode;
176 display_handle_done(void *data,
177 struct wl_output *output)
179 SDL_VideoDisplay *display = data;
180 SDL_AddVideoDisplay(display);
181 SDL_free(display->name);
186 display_handle_scale(void *data,
187 struct wl_output *output,
190 // TODO: do HiDPI stuff.
193 static const struct wl_output_listener output_listener = {
194 display_handle_geometry,
201 Wayland_add_display(SDL_VideoData *d, uint32_t id)
203 struct wl_output *output;
204 SDL_VideoDisplay *display = SDL_malloc(sizeof *display);
211 output = wl_registry_bind(d->registry, id, &wl_output_interface, 2);
213 SDL_SetError("Failed to retrieve output.");
217 wl_output_add_listener(output, &output_listener, display);
220 #ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
222 windowmanager_hints(void *data, struct qt_windowmanager *qt_windowmanager,
223 int32_t show_is_fullscreen)
228 windowmanager_quit(void *data, struct qt_windowmanager *qt_windowmanager)
233 static const struct qt_windowmanager_listener windowmanager_listener = {
237 #endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */
240 display_handle_global(void *data, struct wl_registry *registry, uint32_t id,
241 const char *interface, uint32_t version)
243 SDL_VideoData *d = data;
245 if (strcmp(interface, "wl_compositor") == 0) {
246 d->compositor = wl_registry_bind(d->registry, id, &wl_compositor_interface, 1);
247 } else if (strcmp(interface, "wl_output") == 0) {
248 Wayland_add_display(d, id);
249 } else if (strcmp(interface, "wl_seat") == 0) {
250 Wayland_display_add_input(d, id);
251 } else if (strcmp(interface, "wl_shell") == 0) {
252 d->shell = wl_registry_bind(d->registry, id, &wl_shell_interface, 1);
253 } else if (strcmp(interface, "wl_shm") == 0) {
254 d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
255 d->cursor_theme = WAYLAND_wl_cursor_theme_load(NULL, 32, d->shm);
256 d->default_cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
258 #ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
259 } else if (strcmp(interface, "qt_touch_extension") == 0) {
260 Wayland_touch_create(d, id);
261 } else if (strcmp(interface, "qt_surface_extension") == 0) {
262 d->surface_extension = wl_registry_bind(registry, id,
263 &qt_surface_extension_interface, 1);
264 } else if (strcmp(interface, "qt_windowmanager") == 0) {
265 d->windowmanager = wl_registry_bind(registry, id,
266 &qt_windowmanager_interface, 1);
267 qt_windowmanager_add_listener(d->windowmanager, &windowmanager_listener, d);
268 #endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */
272 static const struct wl_registry_listener registry_listener = {
273 display_handle_global
277 Wayland_VideoInit(_THIS)
279 SDL_VideoData *data = SDL_malloc(sizeof *data);
281 return SDL_OutOfMemory();
282 memset(data, 0, sizeof *data);
284 _this->driverdata = data;
286 data->display = WAYLAND_wl_display_connect(NULL);
287 if (data->display == NULL) {
288 return SDL_SetError("Failed to connect to a Wayland display");
291 data->registry = wl_display_get_registry(data->display);
292 if (data->registry == NULL) {
293 return SDL_SetError("Failed to get the Wayland registry");
296 wl_registry_add_listener(data->registry, ®istry_listener, data);
298 // First roundtrip to receive all registry objects.
299 WAYLAND_wl_display_roundtrip(data->display);
301 // Second roundtrip to receive all output events.
302 WAYLAND_wl_display_roundtrip(data->display);
304 data->xkb_context = WAYLAND_xkb_context_new(0);
305 if (!data->xkb_context) {
306 return SDL_SetError("Failed to create XKB context");
311 WAYLAND_wl_display_flush(data->display);
317 Wayland_GetDisplayModes(_THIS, SDL_VideoDisplay *sdl_display)
319 // Nothing to do here, everything was already done in the wl_output
324 Wayland_SetDisplayMode(_THIS, SDL_VideoDisplay *display, SDL_DisplayMode *mode)
326 return SDL_Unsupported();
330 Wayland_VideoQuit(_THIS)
332 SDL_VideoData *data = _this->driverdata;
335 Wayland_FiniMouse ();
337 for (i = 0; i < _this->num_displays; ++i) {
338 SDL_VideoDisplay *display = &_this->displays[i];
339 wl_output_destroy(display->driverdata);
340 display->driverdata = NULL;
343 Wayland_display_destroy_input(data);
345 if (data->xkb_context) {
346 WAYLAND_xkb_context_unref(data->xkb_context);
347 data->xkb_context = NULL;
349 #ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
350 if (data->windowmanager)
351 qt_windowmanager_destroy(data->windowmanager);
353 if (data->surface_extension)
354 qt_surface_extension_destroy(data->surface_extension);
356 Wayland_touch_destroy(data);
357 #endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */
360 wl_shm_destroy(data->shm);
362 if (data->cursor_theme)
363 WAYLAND_wl_cursor_theme_destroy(data->cursor_theme);
366 wl_shell_destroy(data->shell);
368 if (data->compositor)
369 wl_compositor_destroy(data->compositor);
372 wl_registry_destroy(data->registry);
375 WAYLAND_wl_display_flush(data->display);
376 WAYLAND_wl_display_disconnect(data->display);
380 _this->driverdata = NULL;
383 #endif /* SDL_VIDEO_DRIVER_WAYLAND */
385 /* vi: set ts=4 sw=4 expandtab: */