src/video/wayland/SDL_waylandwindow.c
changeset 8062 4fc5f66d63cc
child 8082 5b83ad3f01ac
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/wayland/SDL_waylandwindow.c	Sat Dec 14 20:18:43 2013 -0300
     1.3 @@ -0,0 +1,192 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +#include "../SDL_sysvideo.h"
    1.28 +#include "../../events/SDL_windowevents_c.h"
    1.29 +#include "../SDL_egl_c.h"
    1.30 +#include "SDL_waylandwindow.h"
    1.31 +#include "SDL_waylandvideo.h"
    1.32 +
    1.33 +static void
    1.34 +handle_ping(void *data, struct wl_shell_surface *shell_surface,
    1.35 +            uint32_t serial)
    1.36 +{
    1.37 +    wl_shell_surface_pong(shell_surface, serial);
    1.38 +}
    1.39 +
    1.40 +static void
    1.41 +handle_configure(void *data, struct wl_shell_surface *shell_surface,
    1.42 +                 uint32_t edges, int32_t width, int32_t height)
    1.43 +{
    1.44 +}
    1.45 +
    1.46 +static void
    1.47 +handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
    1.48 +{
    1.49 +}
    1.50 +
    1.51 +static const struct wl_shell_surface_listener shell_surface_listener = {
    1.52 +    handle_ping,
    1.53 +    handle_configure,
    1.54 +    handle_popup_done
    1.55 +};
    1.56 +
    1.57 +SDL_bool
    1.58 +Wayland_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
    1.59 +{
    1.60 +    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
    1.61 +
    1.62 +    info->info.wl.display = data->waylandData->display;
    1.63 +    info->info.wl.surface = data->surface;
    1.64 +    info->info.wl.shell_surface = data->shell_surface;
    1.65 +    info->subsystem = SDL_SYSWM_WAYLAND;
    1.66 +
    1.67 +    return SDL_TRUE;
    1.68 +}
    1.69 +
    1.70 +void Wayland_ShowWindow(_THIS, SDL_Window *window)
    1.71 +{
    1.72 +    SDL_WindowData *wind = window->driverdata;
    1.73 +
    1.74 +    if (window->flags & SDL_WINDOW_FULLSCREEN)
    1.75 +        wl_shell_surface_set_fullscreen(wind->shell_surface,
    1.76 +                                        WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
    1.77 +                                        0, NULL);
    1.78 +    else
    1.79 +        wl_shell_surface_set_toplevel(wind->shell_surface);
    1.80 +
    1.81 +    wayland_schedule_write(_this->driverdata);
    1.82 +}
    1.83 +
    1.84 +void
    1.85 +Wayland_SetWindowFullscreen(_THIS, SDL_Window * window,
    1.86 +                            SDL_VideoDisplay * _display, SDL_bool fullscreen)
    1.87 +{
    1.88 +    SDL_WindowData *wind = window->driverdata;
    1.89 +
    1.90 +    if (fullscreen)
    1.91 +        wl_shell_surface_set_fullscreen(wind->shell_surface,
    1.92 +                                        WL_SHELL_SURFACE_FULLSCREEN_METHOD_SCALE,
    1.93 +                                        0, NULL);
    1.94 +    else
    1.95 +        wl_shell_surface_set_toplevel(wind->shell_surface);
    1.96 +
    1.97 +    wayland_schedule_write(_this->driverdata);
    1.98 +}
    1.99 +
   1.100 +int Wayland_CreateWindow(_THIS, SDL_Window *window)
   1.101 +{
   1.102 +    SDL_WindowData *data;
   1.103 +    SDL_VideoData *c;
   1.104 +    struct wl_region *region;
   1.105 +
   1.106 +    data = calloc(1, sizeof *data);
   1.107 +    if (data == NULL)
   1.108 +        return 0;
   1.109 +
   1.110 +    c = _this->driverdata;
   1.111 +    window->driverdata = data;
   1.112 +
   1.113 +    if (!(window->flags & SDL_WINDOW_OPENGL)) {
   1.114 +        SDL_GL_LoadLibrary(NULL);
   1.115 +        window->flags |= SDL_WINDOW_OPENGL;
   1.116 +    }
   1.117 +
   1.118 +    if (window->x == SDL_WINDOWPOS_UNDEFINED) {
   1.119 +        window->x = 0;
   1.120 +    }
   1.121 +    if (window->y == SDL_WINDOWPOS_UNDEFINED) {
   1.122 +        window->y = 0;
   1.123 +    }
   1.124 +
   1.125 +    data->waylandData = c;
   1.126 +    data->sdlwindow = window;
   1.127 +
   1.128 +    data->surface =
   1.129 +        wl_compositor_create_surface(c->compositor);
   1.130 +    wl_surface_set_user_data(data->surface, data);
   1.131 +    data->shell_surface = wl_shell_get_shell_surface(c->shell,
   1.132 +                                                     data->surface);
   1.133 +    data->egl_window = wl_egl_window_create(data->surface,
   1.134 +                                            window->w, window->h);
   1.135 +
   1.136 +    /* Create the GLES window surface */
   1.137 +    data->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) data->egl_window);
   1.138 +    
   1.139 +    if (data->egl_surface == EGL_NO_SURFACE) {
   1.140 +        SDL_SetError("failed to create a window surface");
   1.141 +        return -1;
   1.142 +    }
   1.143 +
   1.144 +    if (data->shell_surface) {
   1.145 +        wl_shell_surface_set_user_data(data->shell_surface, data);
   1.146 +        wl_shell_surface_add_listener(data->shell_surface,
   1.147 +                                      &shell_surface_listener, data);
   1.148 +    }
   1.149 +
   1.150 +    region = wl_compositor_create_region(c->compositor);
   1.151 +    wl_region_add(region, 0, 0, window->w, window->h);
   1.152 +    wl_surface_set_opaque_region(data->surface, region);
   1.153 +    wl_region_destroy(region);
   1.154 +
   1.155 +    wayland_schedule_write(c);
   1.156 +
   1.157 +    return 0;
   1.158 +}
   1.159 +
   1.160 +void Wayland_SetWindowSize(_THIS, SDL_Window * window)
   1.161 +{
   1.162 +    SDL_VideoData *data = _this->driverdata;
   1.163 +    SDL_WindowData *wind = window->driverdata;
   1.164 +    struct wl_region *region;
   1.165 +
   1.166 +    wl_egl_window_resize(wind->egl_window, window->w, window->h, 0, 0);
   1.167 +
   1.168 +    region = wl_compositor_create_region(data->compositor);
   1.169 +    wl_region_add(region, 0, 0, window->w, window->h);
   1.170 +    wl_surface_set_opaque_region(wind->surface, region);
   1.171 +    wl_region_destroy(region);
   1.172 +}
   1.173 +
   1.174 +void Wayland_DestroyWindow(_THIS, SDL_Window *window)
   1.175 +{
   1.176 +    SDL_VideoData *data = _this->driverdata;
   1.177 +    SDL_WindowData *wind = window->driverdata;
   1.178 +
   1.179 +    window->driverdata = NULL;
   1.180 +
   1.181 +    if (data) {
   1.182 +        SDL_EGL_DestroySurface(_this, wind->egl_surface);
   1.183 +        wl_egl_window_destroy(wind->egl_window);
   1.184 +
   1.185 +        if (wind->shell_surface)
   1.186 +            wl_shell_surface_destroy(wind->shell_surface);
   1.187 +
   1.188 +        wl_surface_destroy(wind->surface);
   1.189 +
   1.190 +        SDL_free(wind);
   1.191 +        wayland_schedule_write(data);
   1.192 +    }
   1.193 +}
   1.194 +
   1.195 +/* vi: set ts=4 sw=4 expandtab: */