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