src/video/wayland/SDL_waylandwindow.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_sysvideo.h"
gabomdq@8062
    25
#include "../../events/SDL_windowevents_c.h"
gabomdq@8062
    26
#include "../SDL_egl_c.h"
gabomdq@8062
    27
#include "SDL_waylandwindow.h"
gabomdq@8062
    28
#include "SDL_waylandvideo.h"
gabomdq@8062
    29
gabomdq@8062
    30
static void
gabomdq@8062
    31
handle_ping(void *data, struct wl_shell_surface *shell_surface,
gabomdq@8062
    32
            uint32_t serial)
gabomdq@8062
    33
{
gabomdq@8062
    34
    wl_shell_surface_pong(shell_surface, serial);
gabomdq@8062
    35
}
gabomdq@8062
    36
gabomdq@8062
    37
static void
gabomdq@8062
    38
handle_configure(void *data, struct wl_shell_surface *shell_surface,
gabomdq@8062
    39
                 uint32_t edges, int32_t width, int32_t height)
gabomdq@8062
    40
{
gabomdq@8062
    41
}
gabomdq@8062
    42
gabomdq@8062
    43
static void
gabomdq@8062
    44
handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
gabomdq@8062
    45
{
gabomdq@8062
    46
}
gabomdq@8062
    47
gabomdq@8062
    48
static const struct wl_shell_surface_listener shell_surface_listener = {
gabomdq@8062
    49
    handle_ping,
gabomdq@8062
    50
    handle_configure,
gabomdq@8062
    51
    handle_popup_done
gabomdq@8062
    52
};
gabomdq@8062
    53
gabomdq@8062
    54
SDL_bool
gabomdq@8062
    55
Wayland_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
gabomdq@8062
    56
{
gabomdq@8062
    57
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
gabomdq@8062
    58
gabomdq@8062
    59
    info->info.wl.display = data->waylandData->display;
gabomdq@8062
    60
    info->info.wl.surface = data->surface;
gabomdq@8062
    61
    info->info.wl.shell_surface = data->shell_surface;
gabomdq@8062
    62
    info->subsystem = SDL_SYSWM_WAYLAND;
gabomdq@8062
    63
gabomdq@8062
    64
    return SDL_TRUE;
gabomdq@8062
    65
}
gabomdq@8062
    66
gabomdq@8062
    67
void Wayland_ShowWindow(_THIS, SDL_Window *window)
gabomdq@8062
    68
{
gabomdq@8062
    69
    SDL_WindowData *wind = window->driverdata;
gabomdq@8062
    70
gabomdq@8062
    71
    if (window->flags & SDL_WINDOW_FULLSCREEN)
gabomdq@8062
    72
        wl_shell_surface_set_fullscreen(wind->shell_surface,
gabomdq@8062
    73
                                        WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
gabomdq@8062
    74
                                        0, NULL);
gabomdq@8062
    75
    else
gabomdq@8062
    76
        wl_shell_surface_set_toplevel(wind->shell_surface);
gabomdq@8062
    77
gabomdq@8062
    78
    wayland_schedule_write(_this->driverdata);
gabomdq@8062
    79
}
gabomdq@8062
    80
gabomdq@8062
    81
void
gabomdq@8062
    82
Wayland_SetWindowFullscreen(_THIS, SDL_Window * window,
gabomdq@8062
    83
                            SDL_VideoDisplay * _display, SDL_bool fullscreen)
gabomdq@8062
    84
{
gabomdq@8062
    85
    SDL_WindowData *wind = window->driverdata;
gabomdq@8062
    86
gabomdq@8062
    87
    if (fullscreen)
gabomdq@8062
    88
        wl_shell_surface_set_fullscreen(wind->shell_surface,
gabomdq@8062
    89
                                        WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE,
gabomdq@8062
    90
                                        0, NULL);
gabomdq@8062
    91
    else
gabomdq@8062
    92
        wl_shell_surface_set_toplevel(wind->shell_surface);
gabomdq@8062
    93
gabomdq@8062
    94
    wayland_schedule_write(_this->driverdata);
gabomdq@8062
    95
}
gabomdq@8062
    96
gabomdq@8062
    97
int Wayland_CreateWindow(_THIS, SDL_Window *window)
gabomdq@8062
    98
{
gabomdq@8062
    99
    SDL_WindowData *data;
gabomdq@8062
   100
    SDL_VideoData *c;
gabomdq@8062
   101
    struct wl_region *region;
gabomdq@8062
   102
gabomdq@8062
   103
    data = calloc(1, sizeof *data);
gabomdq@8062
   104
    if (data == NULL)
gabomdq@8062
   105
        return 0;
gabomdq@8062
   106
gabomdq@8062
   107
    c = _this->driverdata;
gabomdq@8062
   108
    window->driverdata = data;
gabomdq@8062
   109
gabomdq@8062
   110
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
gabomdq@8062
   111
        SDL_GL_LoadLibrary(NULL);
gabomdq@8062
   112
        window->flags |= SDL_WINDOW_OPENGL;
gabomdq@8062
   113
    }
gabomdq@8062
   114
gabomdq@8062
   115
    if (window->x == SDL_WINDOWPOS_UNDEFINED) {
gabomdq@8062
   116
        window->x = 0;
gabomdq@8062
   117
    }
gabomdq@8062
   118
    if (window->y == SDL_WINDOWPOS_UNDEFINED) {
gabomdq@8062
   119
        window->y = 0;
gabomdq@8062
   120
    }
gabomdq@8062
   121
gabomdq@8062
   122
    data->waylandData = c;
gabomdq@8062
   123
    data->sdlwindow = window;
gabomdq@8062
   124
gabomdq@8062
   125
    data->surface =
gabomdq@8062
   126
        wl_compositor_create_surface(c->compositor);
gabomdq@8062
   127
    wl_surface_set_user_data(data->surface, data);
gabomdq@8062
   128
    data->shell_surface = wl_shell_get_shell_surface(c->shell,
gabomdq@8062
   129
                                                     data->surface);
gabomdq@8062
   130
    data->egl_window = wl_egl_window_create(data->surface,
gabomdq@8062
   131
                                            window->w, window->h);
gabomdq@8062
   132
gabomdq@8062
   133
    /* Create the GLES window surface */
gabomdq@8062
   134
    data->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) data->egl_window);
gabomdq@8062
   135
    
gabomdq@8062
   136
    if (data->egl_surface == EGL_NO_SURFACE) {
gabomdq@8062
   137
        SDL_SetError("failed to create a window surface");
gabomdq@8062
   138
        return -1;
gabomdq@8062
   139
    }
gabomdq@8062
   140
gabomdq@8062
   141
    if (data->shell_surface) {
gabomdq@8062
   142
        wl_shell_surface_set_user_data(data->shell_surface, data);
gabomdq@8062
   143
        wl_shell_surface_add_listener(data->shell_surface,
gabomdq@8062
   144
                                      &shell_surface_listener, data);
gabomdq@8062
   145
    }
gabomdq@8062
   146
gabomdq@8062
   147
    region = wl_compositor_create_region(c->compositor);
gabomdq@8062
   148
    wl_region_add(region, 0, 0, window->w, window->h);
gabomdq@8062
   149
    wl_surface_set_opaque_region(data->surface, region);
gabomdq@8062
   150
    wl_region_destroy(region);
gabomdq@8062
   151
gabomdq@8062
   152
    wayland_schedule_write(c);
gabomdq@8062
   153
gabomdq@8062
   154
    return 0;
gabomdq@8062
   155
}
gabomdq@8062
   156
gabomdq@8062
   157
void Wayland_SetWindowSize(_THIS, SDL_Window * window)
gabomdq@8062
   158
{
gabomdq@8062
   159
    SDL_VideoData *data = _this->driverdata;
gabomdq@8062
   160
    SDL_WindowData *wind = window->driverdata;
gabomdq@8062
   161
    struct wl_region *region;
gabomdq@8062
   162
gabomdq@8062
   163
    wl_egl_window_resize(wind->egl_window, window->w, window->h, 0, 0);
gabomdq@8062
   164
gabomdq@8062
   165
    region = wl_compositor_create_region(data->compositor);
gabomdq@8062
   166
    wl_region_add(region, 0, 0, window->w, window->h);
gabomdq@8062
   167
    wl_surface_set_opaque_region(wind->surface, region);
gabomdq@8062
   168
    wl_region_destroy(region);
gabomdq@8062
   169
}
gabomdq@8062
   170
gabomdq@8062
   171
void Wayland_DestroyWindow(_THIS, SDL_Window *window)
gabomdq@8062
   172
{
gabomdq@8062
   173
    SDL_VideoData *data = _this->driverdata;
gabomdq@8062
   174
    SDL_WindowData *wind = window->driverdata;
gabomdq@8062
   175
gabomdq@8062
   176
    window->driverdata = NULL;
gabomdq@8062
   177
gabomdq@8062
   178
    if (data) {
gabomdq@8062
   179
        SDL_EGL_DestroySurface(_this, wind->egl_surface);
gabomdq@8062
   180
        wl_egl_window_destroy(wind->egl_window);
gabomdq@8062
   181
gabomdq@8062
   182
        if (wind->shell_surface)
gabomdq@8062
   183
            wl_shell_surface_destroy(wind->shell_surface);
gabomdq@8062
   184
gabomdq@8062
   185
        wl_surface_destroy(wind->surface);
gabomdq@8062
   186
gabomdq@8062
   187
        SDL_free(wind);
gabomdq@8062
   188
        wayland_schedule_write(data);
gabomdq@8062
   189
    }
gabomdq@8062
   190
}
gabomdq@8062
   191
gabomdq@8062
   192
/* vi: set ts=4 sw=4 expandtab: */