src/video/wayland/SDL_waylandvideo.c
author Gabriel Jacobo <gabomdq@gmail.com>
Sat, 14 Dec 2013 20:18:43 -0300
changeset 8062 4fc5f66d63cc
child 8082 5b83ad3f01ac
permissions -rw-r--r--
Wayland support

Based on the original port to Wayland by: Joel Teichroeb, Benjamin Franzke, Scott Moreau, et al.

Additional changes in this commit, done by me:

* Wayland uses the common EGL framework
* EGL can now create a desktop OpenGL context
* testgl2 loads GL functions dynamically, no need to link to libGL anymore
* Assorted fixes to the Wayland backend

Tested on the Weston Compositor (v1.0.5) that ships with Ubuntu 13.10,
running Weston under X. Tests ran: testrendercopyex (all backends), testgl2, testgles2,testintersections
gabomdq@8062
     1
/*
gabomdq@8062
     2
  Simple DirectMedia Layer
gabomdq@8062
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
gabomdq@8062
     4
gabomdq@8062
     5
  This software is provided 'as-is', without any express or implied
gabomdq@8062
     6
  warranty.  In no event will the authors be held liable for any damages
gabomdq@8062
     7
  arising from the use of this software.
gabomdq@8062
     8
gabomdq@8062
     9
  Permission is granted to anyone to use this software for any purpose,
gabomdq@8062
    10
  including commercial applications, and to alter it and redistribute it
gabomdq@8062
    11
  freely, subject to the following restrictions:
gabomdq@8062
    12
gabomdq@8062
    13
  1. The origin of this software must not be misrepresented; you must not
gabomdq@8062
    14
     claim that you wrote the original software. If you use this software
gabomdq@8062
    15
     in a product, an acknowledgment in the product documentation would be
gabomdq@8062
    16
     appreciated but is not required.
gabomdq@8062
    17
  2. Altered source versions must be plainly marked as such, and must not be
gabomdq@8062
    18
     misrepresented as being the original software.
gabomdq@8062
    19
  3. This notice may not be removed or altered from any source distribution.
gabomdq@8062
    20
*/
gabomdq@8062
    21
gabomdq@8062
    22
#include "SDL_config.h"
gabomdq@8062
    23
gabomdq@8062
    24
#include "SDL_video.h"
gabomdq@8062
    25
#include "SDL_mouse.h"
gabomdq@8062
    26
#include "../../events/SDL_events_c.h"
gabomdq@8062
    27
gabomdq@8062
    28
#include "SDL_waylandvideo.h"
gabomdq@8062
    29
#include "SDL_waylandevents_c.h"
gabomdq@8062
    30
#include "SDL_waylandwindow.h"
gabomdq@8062
    31
#include "SDL_waylandopengles.h"
gabomdq@8062
    32
#include "SDL_waylandmouse.h"
gabomdq@8062
    33
gabomdq@8062
    34
#include <fcntl.h>
gabomdq@8062
    35
#include <xkbcommon/xkbcommon.h>
gabomdq@8062
    36
gabomdq@8062
    37
#define WAYLANDVID_DRIVER_NAME "wayland"
gabomdq@8062
    38
gabomdq@8062
    39
struct wayland_mode {
gabomdq@8062
    40
    SDL_DisplayMode mode;
gabomdq@8062
    41
    struct wl_list link;
gabomdq@8062
    42
};
gabomdq@8062
    43
gabomdq@8062
    44
/* Initialization/Query functions */
gabomdq@8062
    45
static int
gabomdq@8062
    46
Wayland_VideoInit(_THIS);
gabomdq@8062
    47
gabomdq@8062
    48
static void
gabomdq@8062
    49
Wayland_GetDisplayModes(_THIS, SDL_VideoDisplay *sdl_display);
gabomdq@8062
    50
static int
gabomdq@8062
    51
Wayland_SetDisplayMode(_THIS, SDL_VideoDisplay *display, SDL_DisplayMode *mode);
gabomdq@8062
    52
gabomdq@8062
    53
static void
gabomdq@8062
    54
Wayland_VideoQuit(_THIS);
gabomdq@8062
    55
gabomdq@8062
    56
/* Wayland driver bootstrap functions */
gabomdq@8062
    57
static int
gabomdq@8062
    58
Wayland_Available(void)
gabomdq@8062
    59
{
gabomdq@8062
    60
    struct wl_display *display = NULL;
gabomdq@8062
    61
gabomdq@8062
    62
    display = wl_display_connect(NULL);
gabomdq@8062
    63
    if (display != NULL) {
gabomdq@8062
    64
        wl_display_disconnect(display);
gabomdq@8062
    65
    }
gabomdq@8062
    66
gabomdq@8062
    67
    return (display != NULL);
gabomdq@8062
    68
}
gabomdq@8062
    69
gabomdq@8062
    70
static void
gabomdq@8062
    71
Wayland_DeleteDevice(SDL_VideoDevice *device)
gabomdq@8062
    72
{
gabomdq@8062
    73
    SDL_free(device);
gabomdq@8062
    74
}
gabomdq@8062
    75
gabomdq@8062
    76
static SDL_VideoDevice *
gabomdq@8062
    77
Wayland_CreateDevice(int devindex)
gabomdq@8062
    78
{
gabomdq@8062
    79
    SDL_VideoDevice *device;
gabomdq@8062
    80
gabomdq@8062
    81
    /* Initialize all variables that we clean on shutdown */
gabomdq@8062
    82
    device = SDL_calloc(1, sizeof(SDL_VideoDevice));
gabomdq@8062
    83
    if (!device) {
gabomdq@8062
    84
        SDL_OutOfMemory();
gabomdq@8062
    85
        return NULL;
gabomdq@8062
    86
    }
gabomdq@8062
    87
gabomdq@8062
    88
    /* Set the function pointers */
gabomdq@8062
    89
    device->VideoInit = Wayland_VideoInit;
gabomdq@8062
    90
    device->VideoQuit = Wayland_VideoQuit;
gabomdq@8062
    91
    device->SetDisplayMode = Wayland_SetDisplayMode;
gabomdq@8062
    92
    device->GetDisplayModes = Wayland_GetDisplayModes;
gabomdq@8062
    93
    device->GetWindowWMInfo = Wayland_GetWindowWMInfo;
gabomdq@8062
    94
gabomdq@8062
    95
    device->PumpEvents = Wayland_PumpEvents;
gabomdq@8062
    96
gabomdq@8062
    97
    device->GL_SwapWindow = Wayland_GLES_SwapWindow;
gabomdq@8062
    98
    device->GL_GetSwapInterval = Wayland_GLES_GetSwapInterval;
gabomdq@8062
    99
    device->GL_SetSwapInterval = Wayland_GLES_SetSwapInterval;
gabomdq@8062
   100
    device->GL_MakeCurrent = Wayland_GLES_MakeCurrent;
gabomdq@8062
   101
    device->GL_CreateContext = Wayland_GLES_CreateContext;
gabomdq@8062
   102
    device->GL_LoadLibrary = Wayland_GLES_LoadLibrary;
gabomdq@8062
   103
    device->GL_UnloadLibrary = Wayland_GLES_UnloadLibrary;
gabomdq@8062
   104
    device->GL_GetProcAddress = Wayland_GLES_GetProcAddress;
gabomdq@8062
   105
    device->GL_DeleteContext = Wayland_GLES_DeleteContext;
gabomdq@8062
   106
gabomdq@8062
   107
    device->CreateWindow = Wayland_CreateWindow;
gabomdq@8062
   108
    device->ShowWindow = Wayland_ShowWindow;
gabomdq@8062
   109
    device->SetWindowFullscreen = Wayland_SetWindowFullscreen;
gabomdq@8062
   110
    device->SetWindowSize = Wayland_SetWindowSize;
gabomdq@8062
   111
    device->DestroyWindow = Wayland_DestroyWindow;
gabomdq@8062
   112
gabomdq@8062
   113
    device->free = Wayland_DeleteDevice;
gabomdq@8062
   114
gabomdq@8062
   115
    return device;
gabomdq@8062
   116
}
gabomdq@8062
   117
gabomdq@8062
   118
VideoBootStrap Wayland_bootstrap = {
gabomdq@8062
   119
    WAYLANDVID_DRIVER_NAME, "SDL Wayland video driver",
gabomdq@8062
   120
    Wayland_Available, Wayland_CreateDevice
gabomdq@8062
   121
};
gabomdq@8062
   122
gabomdq@8062
   123
static void
gabomdq@8062
   124
wayland_add_mode(SDL_VideoData *d, SDL_DisplayMode m)
gabomdq@8062
   125
{
gabomdq@8062
   126
    struct wayland_mode *mode;
gabomdq@8062
   127
gabomdq@8062
   128
    /* Check for duplicate mode */
gabomdq@8062
   129
    wl_list_for_each(mode, &d->modes_list, link)
gabomdq@8062
   130
        if (mode->mode.w == m.w && mode->mode.h == m.h &&
gabomdq@8062
   131
	    mode->mode.refresh_rate == m.refresh_rate)
gabomdq@8062
   132
	    return;
gabomdq@8062
   133
gabomdq@8062
   134
    /* Add new mode to the list */
gabomdq@8062
   135
    mode = SDL_calloc(1, sizeof *mode);
gabomdq@8062
   136
gabomdq@8062
   137
    if (!mode)
gabomdq@8062
   138
	return;
gabomdq@8062
   139
gabomdq@8062
   140
    mode->mode = m;
gabomdq@8062
   141
    wl_list_insert(&d->modes_list, &mode->link);
gabomdq@8062
   142
}
gabomdq@8062
   143
gabomdq@8062
   144
static void
gabomdq@8062
   145
display_handle_geometry(void *data,
gabomdq@8062
   146
                        struct wl_output *output,
gabomdq@8062
   147
                        int x, int y,
gabomdq@8062
   148
                        int physical_width,
gabomdq@8062
   149
                        int physical_height,
gabomdq@8062
   150
                        int subpixel,
gabomdq@8062
   151
                        const char *make,
gabomdq@8062
   152
                        const char *model,
gabomdq@8062
   153
                        int transform)
gabomdq@8062
   154
gabomdq@8062
   155
{
gabomdq@8062
   156
    SDL_VideoData *d = data;
gabomdq@8062
   157
gabomdq@8062
   158
    d->screen_allocation.x = x;
gabomdq@8062
   159
    d->screen_allocation.y = y;
gabomdq@8062
   160
}
gabomdq@8062
   161
gabomdq@8062
   162
static void
gabomdq@8062
   163
display_handle_mode(void *data,
gabomdq@8062
   164
                    struct wl_output *wl_output,
gabomdq@8062
   165
                    uint32_t flags,
gabomdq@8062
   166
                    int width,
gabomdq@8062
   167
                    int height,
gabomdq@8062
   168
                    int refresh)
gabomdq@8062
   169
{
gabomdq@8062
   170
    SDL_VideoData *d = data;
gabomdq@8062
   171
    SDL_DisplayMode mode;
gabomdq@8062
   172
gabomdq@8062
   173
    SDL_zero(mode);
gabomdq@8062
   174
    mode.w = width;
gabomdq@8062
   175
    mode.h = height;
gabomdq@8062
   176
    mode.refresh_rate = refresh / 1000;
gabomdq@8062
   177
gabomdq@8062
   178
    wayland_add_mode(d, mode);
gabomdq@8062
   179
gabomdq@8062
   180
    if (flags & WL_OUTPUT_MODE_CURRENT) {
gabomdq@8062
   181
        d->screen_allocation.width = width;
gabomdq@8062
   182
        d->screen_allocation.height = height;
gabomdq@8062
   183
    }
gabomdq@8062
   184
}
gabomdq@8062
   185
gabomdq@8062
   186
static const struct wl_output_listener output_listener = {
gabomdq@8062
   187
    display_handle_geometry,
gabomdq@8062
   188
    display_handle_mode
gabomdq@8062
   189
};
gabomdq@8062
   190
gabomdq@8062
   191
static void
gabomdq@8062
   192
shm_handle_format(void *data,
gabomdq@8062
   193
                  struct wl_shm *shm,
gabomdq@8062
   194
                  uint32_t format)
gabomdq@8062
   195
{
gabomdq@8062
   196
    SDL_VideoData *d = data;
gabomdq@8062
   197
gabomdq@8062
   198
    d->shm_formats |= (1 << format);
gabomdq@8062
   199
}
gabomdq@8062
   200
gabomdq@8062
   201
static const struct wl_shm_listener shm_listener = {
gabomdq@8062
   202
    shm_handle_format
gabomdq@8062
   203
};
gabomdq@8062
   204
gabomdq@8062
   205
static void
gabomdq@8062
   206
display_handle_global(void *data, struct wl_registry *registry, uint32_t id,
gabomdq@8062
   207
					const char *interface, uint32_t version)
gabomdq@8062
   208
{
gabomdq@8062
   209
    SDL_VideoData *d = data;
gabomdq@8062
   210
gabomdq@8062
   211
    if (strcmp(interface, "wl_compositor") == 0) {
gabomdq@8062
   212
        d->compositor = wl_registry_bind(d->registry, id, &wl_compositor_interface, 1);
gabomdq@8062
   213
    } else if (strcmp(interface, "wl_output") == 0) {
gabomdq@8062
   214
        d->output = wl_registry_bind(d->registry, id, &wl_output_interface, 1);
gabomdq@8062
   215
        wl_output_add_listener(d->output, &output_listener, d);
gabomdq@8062
   216
    } else if (strcmp(interface, "wl_seat") == 0) {
gabomdq@8062
   217
        Wayland_display_add_input(d, id);
gabomdq@8062
   218
    } else if (strcmp(interface, "wl_shell") == 0) {
gabomdq@8062
   219
        d->shell = wl_registry_bind(d->registry, id, &wl_shell_interface, 1);
gabomdq@8062
   220
    } else if (strcmp(interface, "wl_shm") == 0) {
gabomdq@8062
   221
        d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
gabomdq@8062
   222
        d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm);
gabomdq@8062
   223
        d->default_cursor = wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
gabomdq@8062
   224
        wl_shm_add_listener(d->shm, &shm_listener, d);
gabomdq@8062
   225
    }
gabomdq@8062
   226
}
gabomdq@8062
   227
gabomdq@8062
   228
static const struct wl_registry_listener registry_listener = {
gabomdq@8062
   229
	display_handle_global
gabomdq@8062
   230
};
gabomdq@8062
   231
gabomdq@8062
   232
int
gabomdq@8062
   233
Wayland_VideoInit(_THIS)
gabomdq@8062
   234
{
gabomdq@8062
   235
    SDL_VideoData *data;
gabomdq@8062
   236
gabomdq@8062
   237
    data = malloc(sizeof *data);
gabomdq@8062
   238
    if (data == NULL)
gabomdq@8062
   239
        return 0;
gabomdq@8062
   240
    memset(data, 0, sizeof *data);
gabomdq@8062
   241
gabomdq@8062
   242
    _this->driverdata = data;
gabomdq@8062
   243
gabomdq@8062
   244
    wl_list_init(&data->modes_list);
gabomdq@8062
   245
    
gabomdq@8062
   246
    data->display = wl_display_connect(NULL);
gabomdq@8062
   247
    if (data->display == NULL) {
gabomdq@8062
   248
        SDL_SetError("Failed to connect to a Wayland display");
gabomdq@8062
   249
        return 0;
gabomdq@8062
   250
    }
gabomdq@8062
   251
gabomdq@8062
   252
    data->registry = wl_display_get_registry(data->display);
gabomdq@8062
   253
    wl_registry_add_listener(data->registry, &registry_listener, data);
gabomdq@8062
   254
gabomdq@8062
   255
    while (data->screen_allocation.width == 0)
gabomdq@8062
   256
        wl_display_dispatch(data->display);
gabomdq@8062
   257
gabomdq@8062
   258
    data->xkb_context = xkb_context_new(0);
gabomdq@8062
   259
    if (!data->xkb_context) {
gabomdq@8062
   260
        SDL_SetError("Failed to create XKB context");
gabomdq@8062
   261
        return 0;
gabomdq@8062
   262
    }
gabomdq@8062
   263
gabomdq@8062
   264
    SDL_VideoDisplay display;
gabomdq@8062
   265
    SDL_DisplayMode mode;
gabomdq@8062
   266
gabomdq@8062
   267
    /* Use a fake 32-bpp desktop mode */
gabomdq@8062
   268
    mode.format = SDL_PIXELFORMAT_RGB888;
gabomdq@8062
   269
    mode.w = data->screen_allocation.width;
gabomdq@8062
   270
    mode.h = data->screen_allocation.height;
gabomdq@8062
   271
    mode.refresh_rate = 0;
gabomdq@8062
   272
    mode.driverdata = NULL;
gabomdq@8062
   273
    wayland_add_mode(data, mode);
gabomdq@8062
   274
    SDL_zero(display);
gabomdq@8062
   275
    display.desktop_mode = mode;
gabomdq@8062
   276
    display.current_mode = mode;
gabomdq@8062
   277
    display.driverdata = NULL;
gabomdq@8062
   278
    SDL_AddVideoDisplay(&display);
gabomdq@8062
   279
gabomdq@8062
   280
    Wayland_InitMouse ();
gabomdq@8062
   281
gabomdq@8062
   282
    wayland_schedule_write(data);
gabomdq@8062
   283
gabomdq@8062
   284
    return 0;
gabomdq@8062
   285
}
gabomdq@8062
   286
gabomdq@8062
   287
static void
gabomdq@8062
   288
Wayland_GetDisplayModes(_THIS, SDL_VideoDisplay *sdl_display)
gabomdq@8062
   289
{
gabomdq@8062
   290
    SDL_VideoData *data = _this->driverdata;
gabomdq@8062
   291
    SDL_DisplayMode mode;
gabomdq@8062
   292
    struct wayland_mode *m;
gabomdq@8062
   293
gabomdq@8062
   294
    Wayland_PumpEvents(_this);
gabomdq@8062
   295
gabomdq@8062
   296
    wl_list_for_each(m, &data->modes_list, link) {
gabomdq@8062
   297
        m->mode.format = SDL_PIXELFORMAT_RGB888;
gabomdq@8062
   298
        SDL_AddDisplayMode(sdl_display, &m->mode);
gabomdq@8062
   299
        m->mode.format = SDL_PIXELFORMAT_RGBA8888;
gabomdq@8062
   300
        SDL_AddDisplayMode(sdl_display, &m->mode);
gabomdq@8062
   301
    }
gabomdq@8062
   302
gabomdq@8062
   303
    mode.w = data->screen_allocation.width;
gabomdq@8062
   304
    mode.h = data->screen_allocation.height;
gabomdq@8062
   305
    mode.refresh_rate = 0;
gabomdq@8062
   306
    mode.driverdata = NULL;
gabomdq@8062
   307
gabomdq@8062
   308
    mode.format = SDL_PIXELFORMAT_RGB888;
gabomdq@8062
   309
    SDL_AddDisplayMode(sdl_display, &mode);
gabomdq@8062
   310
    mode.format = SDL_PIXELFORMAT_RGBA8888;
gabomdq@8062
   311
    SDL_AddDisplayMode(sdl_display, &mode);
gabomdq@8062
   312
}
gabomdq@8062
   313
gabomdq@8062
   314
static int
gabomdq@8062
   315
Wayland_SetDisplayMode(_THIS, SDL_VideoDisplay *display, SDL_DisplayMode *mode)
gabomdq@8062
   316
{
gabomdq@8062
   317
    return 0;
gabomdq@8062
   318
}
gabomdq@8062
   319
gabomdq@8062
   320
void
gabomdq@8062
   321
Wayland_VideoQuit(_THIS)
gabomdq@8062
   322
{
gabomdq@8062
   323
    SDL_VideoData *data = _this->driverdata;
gabomdq@8062
   324
    struct wayland_mode *t, *m;
gabomdq@8062
   325
gabomdq@8062
   326
    Wayland_FiniMouse ();
gabomdq@8062
   327
gabomdq@8062
   328
    if (data->output)
gabomdq@8062
   329
        wl_output_destroy(data->output);
gabomdq@8062
   330
gabomdq@8062
   331
    Wayland_display_destroy_input(data);
gabomdq@8062
   332
gabomdq@8062
   333
    if (data->xkb_context) {
gabomdq@8062
   334
        xkb_context_unref(data->xkb_context);
gabomdq@8062
   335
        data->xkb_context = NULL;
gabomdq@8062
   336
    }
gabomdq@8062
   337
gabomdq@8062
   338
    if (data->shm)
gabomdq@8062
   339
        wl_shm_destroy(data->shm);
gabomdq@8062
   340
gabomdq@8062
   341
    if (data->cursor_theme)
gabomdq@8062
   342
        wl_cursor_theme_destroy(data->cursor_theme);
gabomdq@8062
   343
gabomdq@8062
   344
    if (data->shell)
gabomdq@8062
   345
        wl_shell_destroy(data->shell);
gabomdq@8062
   346
gabomdq@8062
   347
    if (data->compositor)
gabomdq@8062
   348
        wl_compositor_destroy(data->compositor);
gabomdq@8062
   349
gabomdq@8062
   350
    if (data->display) {
gabomdq@8062
   351
        wl_display_flush(data->display);
gabomdq@8062
   352
        wl_display_disconnect(data->display);
gabomdq@8062
   353
    }
gabomdq@8062
   354
    
gabomdq@8062
   355
    wl_list_for_each_safe(m, t, &data->modes_list, link) {
gabomdq@8062
   356
        wl_list_remove(&m->link);
gabomdq@8062
   357
        free(m);
gabomdq@8062
   358
    }
gabomdq@8062
   359
gabomdq@8062
   360
gabomdq@8062
   361
    free(data);
gabomdq@8062
   362
    _this->driverdata = NULL;
gabomdq@8062
   363
}
gabomdq@8062
   364
gabomdq@8062
   365
/* vi: set ts=4 sw=4 expandtab: */