src/video/wayland/SDL_waylandwindow.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 15 Apr 2018 17:42:09 -0400
changeset 11944 295cf9910d75
parent 11848 2f157c1ca383
child 12033 bc702c5a7c3a
permissions -rw-r--r--
wayland: zxdg_shell_v6 needs a configure event before using a surface at all.

Fixes Bugzilla #4109.
Fixes Bugzilla #4119.
gabomdq@8062
     1
/*
gabomdq@8062
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 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@8104
    22
#include "../../SDL_internal.h"
gabomdq@8062
    23
icculus@8116
    24
#if SDL_VIDEO_DRIVER_WAYLAND && SDL_VIDEO_OPENGL_EGL
icculus@8116
    25
gabomdq@8062
    26
#include "../SDL_sysvideo.h"
gabomdq@8062
    27
#include "../../events/SDL_windowevents_c.h"
gabomdq@8062
    28
#include "../SDL_egl_c.h"
slouken@10304
    29
#include "SDL_waylandevents_c.h"
gabomdq@8062
    30
#include "SDL_waylandwindow.h"
gabomdq@8062
    31
#include "SDL_waylandvideo.h"
gabomdq@8082
    32
#include "SDL_waylandtouch.h"
arnej@9362
    33
#include "SDL_waylanddyn.h"
m@10614
    34
#include "SDL_hints.h"
gabomdq@8062
    35
icculus@11848
    36
#include "xdg-shell-unstable-v6-client-protocol.h"
icculus@11848
    37
icculus@11848
    38
/* On modern desktops, we probably will use the xdg-shell protocol instead
icculus@11848
    39
   of wl_shell, but wl_shell might be useful on older Wayland installs that
icculus@11848
    40
   don't have the newer protocol, or embedded things that don't have a full
icculus@11848
    41
   window manager. */
icculus@11848
    42
gabomdq@8062
    43
static void
icculus@11848
    44
handle_ping_wl_shell_surface(void *data, struct wl_shell_surface *shell_surface,
gabomdq@8062
    45
            uint32_t serial)
gabomdq@8062
    46
{
gabomdq@8062
    47
    wl_shell_surface_pong(shell_surface, serial);
gabomdq@8062
    48
}
gabomdq@8062
    49
gabomdq@8062
    50
static void
icculus@11848
    51
handle_configure_wl_shell_surface(void *data, struct wl_shell_surface *shell_surface,
gabomdq@8062
    52
                 uint32_t edges, int32_t width, int32_t height)
gabomdq@8062
    53
{
gabomdq@8766
    54
    SDL_WindowData *wind = (SDL_WindowData *)data;
gabomdq@8766
    55
    SDL_Window *window = wind->sdlwindow;
gabomdq@8766
    56
    struct wl_region *region;
gabomdq@8766
    57
slouken@10492
    58
    /* wl_shell_surface spec states that this is a suggestion.
slouken@10492
    59
       Ignore if less than or greater than max/min size. */
slouken@10492
    60
slouken@10492
    61
    if (width == 0 || height == 0) {
slouken@10492
    62
        return;
slouken@10492
    63
    }
slouken@10492
    64
slouken@10492
    65
    if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
slouken@10492
    66
        if ((window->flags & SDL_WINDOW_RESIZABLE)) {
slouken@10492
    67
            if (window->max_w > 0) {
slouken@10492
    68
                width = SDL_min(width, window->max_w);
slouken@10492
    69
            } 
slouken@10492
    70
            width = SDL_max(width, window->min_w);
slouken@10492
    71
slouken@10492
    72
            if (window->max_h > 0) {
slouken@10492
    73
                height = SDL_min(height, window->max_h);
slouken@10492
    74
            }
slouken@10492
    75
            height = SDL_max(height, window->min_h);
slouken@10492
    76
        } else {
slouken@10492
    77
            return;
slouken@10492
    78
        }
slouken@10492
    79
    }
slouken@10492
    80
slouken@10492
    81
    if (width == window->w && height == window->h) {
slouken@10492
    82
        return;
slouken@10492
    83
    }
slouken@10492
    84
gabomdq@8766
    85
    window->w = width;
gabomdq@8766
    86
    window->h = height;
gabomdq@8766
    87
    WAYLAND_wl_egl_window_resize(wind->egl_window, window->w, window->h, 0, 0);
gabomdq@8766
    88
gabomdq@8766
    89
    region = wl_compositor_create_region(wind->waylandData->compositor);
gabomdq@8766
    90
    wl_region_add(region, 0, 0, window->w, window->h);
gabomdq@8766
    91
    wl_surface_set_opaque_region(wind->surface, region);
gabomdq@8766
    92
    wl_region_destroy(region);
gabomdq@8766
    93
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, window->w, window->h);
gabomdq@8062
    94
}
gabomdq@8062
    95
gabomdq@8062
    96
static void
icculus@11848
    97
handle_popup_done_wl_shell_surface(void *data, struct wl_shell_surface *shell_surface)
gabomdq@8062
    98
{
gabomdq@8062
    99
}
gabomdq@8062
   100
icculus@11848
   101
static const struct wl_shell_surface_listener shell_surface_listener_wl = {
icculus@11848
   102
    handle_ping_wl_shell_surface,
icculus@11848
   103
    handle_configure_wl_shell_surface,
icculus@11848
   104
    handle_popup_done_wl_shell_surface
gabomdq@8062
   105
};
gabomdq@8062
   106
icculus@11848
   107
icculus@11848
   108
icculus@11848
   109
icculus@11848
   110
static void
icculus@11848
   111
handle_configure_zxdg_shell_surface(void *data, struct zxdg_surface_v6 *zxdg, uint32_t serial)
icculus@11848
   112
{
icculus@11848
   113
    SDL_WindowData *wind = (SDL_WindowData *)data;
icculus@11848
   114
    SDL_Window *window = wind->sdlwindow;
icculus@11848
   115
    struct wl_region *region;
icculus@11944
   116
icculus@11944
   117
    wind->shell_surface.zxdg.initial_configure_seen = SDL_TRUE;
icculus@11944
   118
icculus@11848
   119
    WAYLAND_wl_egl_window_resize(wind->egl_window, window->w, window->h, 0, 0);
icculus@11848
   120
icculus@11848
   121
    region = wl_compositor_create_region(wind->waylandData->compositor);
icculus@11848
   122
    wl_region_add(region, 0, 0, window->w, window->h);
icculus@11848
   123
    wl_surface_set_opaque_region(wind->surface, region);
icculus@11848
   124
    wl_region_destroy(region);
icculus@11848
   125
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, window->w, window->h);
icculus@11848
   126
    zxdg_surface_v6_ack_configure(zxdg, serial);
icculus@11848
   127
}
icculus@11848
   128
icculus@11848
   129
static const struct zxdg_surface_v6_listener shell_surface_listener_zxdg = {
icculus@11848
   130
    handle_configure_zxdg_shell_surface
icculus@11848
   131
};
icculus@11848
   132
icculus@11848
   133
icculus@11848
   134
static void
icculus@11848
   135
handle_configure_zxdg_toplevel(void *data,
icculus@11848
   136
			  struct zxdg_toplevel_v6 *zxdg_toplevel_v6,
icculus@11848
   137
			  int32_t width,
icculus@11848
   138
			  int32_t height,
icculus@11848
   139
			  struct wl_array *states)
icculus@11848
   140
{
icculus@11848
   141
    SDL_WindowData *wind = (SDL_WindowData *)data;
icculus@11848
   142
    SDL_Window *window = wind->sdlwindow;
icculus@11848
   143
icculus@11848
   144
    /* wl_shell_surface spec states that this is a suggestion.
icculus@11848
   145
       Ignore if less than or greater than max/min size. */
icculus@11848
   146
icculus@11848
   147
    if (width == 0 || height == 0) {
icculus@11848
   148
        return;
icculus@11848
   149
    }
icculus@11848
   150
icculus@11848
   151
    if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
icculus@11848
   152
        if ((window->flags & SDL_WINDOW_RESIZABLE)) {
icculus@11848
   153
            if (window->max_w > 0) {
icculus@11848
   154
                width = SDL_min(width, window->max_w);
icculus@11848
   155
            } 
icculus@11848
   156
            width = SDL_max(width, window->min_w);
icculus@11848
   157
icculus@11848
   158
            if (window->max_h > 0) {
icculus@11848
   159
                height = SDL_min(height, window->max_h);
icculus@11848
   160
            }
icculus@11848
   161
            height = SDL_max(height, window->min_h);
icculus@11848
   162
        } else {
icculus@11848
   163
            return;
icculus@11848
   164
        }
icculus@11848
   165
    }
icculus@11848
   166
icculus@11848
   167
    if (width == window->w && height == window->h) {
icculus@11848
   168
        return;
icculus@11848
   169
    }
icculus@11848
   170
icculus@11848
   171
    window->w = width;
icculus@11848
   172
    window->h = height;
icculus@11848
   173
}
icculus@11848
   174
icculus@11848
   175
static void
icculus@11848
   176
handle_close_zxdg_toplevel(void *data, struct zxdg_toplevel_v6 *zxdg_toplevel_v6)
icculus@11848
   177
{
icculus@11848
   178
    SDL_WindowData *window = (SDL_WindowData *)data;
icculus@11848
   179
    SDL_SendWindowEvent(window->sdlwindow, SDL_WINDOWEVENT_CLOSE, 0, 0);
icculus@11848
   180
}
icculus@11848
   181
icculus@11848
   182
static const struct zxdg_toplevel_v6_listener toplevel_listener_zxdg = {
icculus@11848
   183
    handle_configure_zxdg_toplevel,
icculus@11848
   184
    handle_close_zxdg_toplevel
icculus@11848
   185
};
icculus@11848
   186
icculus@11848
   187
gabomdq@8082
   188
#ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
gabomdq@8082
   189
static void
gabomdq@8082
   190
handle_onscreen_visibility(void *data,
gabomdq@8082
   191
        struct qt_extended_surface *qt_extended_surface, int32_t visible)
gabomdq@8082
   192
{
gabomdq@8082
   193
}
gabomdq@8082
   194
gabomdq@8082
   195
static void
gabomdq@8082
   196
handle_set_generic_property(void *data,
gabomdq@8082
   197
        struct qt_extended_surface *qt_extended_surface, const char *name,
gabomdq@8082
   198
        struct wl_array *value)
gabomdq@8082
   199
{
gabomdq@8082
   200
}
gabomdq@8082
   201
gabomdq@8082
   202
static void
gabomdq@8082
   203
handle_close(void *data, struct qt_extended_surface *qt_extended_surface)
gabomdq@8082
   204
{
gabomdq@8082
   205
    SDL_WindowData *window = (SDL_WindowData *)data;
gabomdq@8082
   206
    SDL_SendWindowEvent(window->sdlwindow, SDL_WINDOWEVENT_CLOSE, 0, 0);
gabomdq@8082
   207
}
gabomdq@8082
   208
gabomdq@8082
   209
static const struct qt_extended_surface_listener extended_surface_listener = {
gabomdq@8082
   210
    handle_onscreen_visibility,
gabomdq@8082
   211
    handle_set_generic_property,
gabomdq@8082
   212
    handle_close,
gabomdq@8082
   213
};
gabomdq@8082
   214
#endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */
gabomdq@8082
   215
gabomdq@8062
   216
SDL_bool
gabomdq@8062
   217
Wayland_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
gabomdq@8062
   218
{
gabomdq@8062
   219
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@11088
   220
    const Uint32 version = ((((Uint32) info->version.major) * 1000000) +
icculus@11088
   221
                            (((Uint32) info->version.minor) * 10000) +
icculus@11088
   222
                            (((Uint32) info->version.patch)));
icculus@11088
   223
icculus@11088
   224
    /* Before 2.0.6, it was possible to build an SDL with Wayland support
icculus@11088
   225
       (SDL_SysWMinfo will be large enough to hold Wayland info), but build
icculus@11088
   226
       your app against SDL headers that didn't have Wayland support
icculus@11088
   227
       (SDL_SysWMinfo could be smaller than Wayland needs. This would lead
icculus@11088
   228
       to an app properly using SDL_GetWindowWMInfo() but we'd accidentally
icculus@11088
   229
       overflow memory on the stack or heap. To protect against this, we've
icculus@11088
   230
       padded out the struct unconditionally in the headers and Wayland will
icculus@11088
   231
       just return an error for older apps using this function. Those apps
icculus@11088
   232
       will need to be recompiled against newer headers or not use Wayland,
icculus@11088
   233
       maybe by forcing SDL_VIDEODRIVER=x11. */
icculus@11088
   234
    if (version < 2000006) {
icculus@11088
   235
        info->subsystem = SDL_SYSWM_UNKNOWN;
philipp@11092
   236
        SDL_SetError("Version must be 2.0.6 or newer");
icculus@11088
   237
        return SDL_FALSE;
icculus@11088
   238
    }
gabomdq@8062
   239
gabomdq@8062
   240
    info->info.wl.display = data->waylandData->display;
gabomdq@8062
   241
    info->info.wl.surface = data->surface;
icculus@11848
   242
    info->info.wl.shell_surface = data->shell_surface.wl;
gabomdq@8062
   243
    info->subsystem = SDL_SYSWM_WAYLAND;
gabomdq@8062
   244
gabomdq@8062
   245
    return SDL_TRUE;
gabomdq@8062
   246
}
gabomdq@8062
   247
icculus@9554
   248
int
icculus@9554
   249
Wayland_SetWindowHitTest(SDL_Window *window, SDL_bool enabled)
icculus@9554
   250
{
icculus@9554
   251
    return 0;  /* just succeed, the real work is done elsewhere. */
icculus@9554
   252
}
icculus@9554
   253
icculus@11848
   254
static void
icculus@11848
   255
SetFullscreen(_THIS, SDL_Window * window, struct wl_output *output)
icculus@11848
   256
{
icculus@11848
   257
    const SDL_VideoData *viddata = (const SDL_VideoData *) _this->driverdata;
icculus@11848
   258
    SDL_WindowData *wind = window->driverdata;
icculus@11848
   259
icculus@11848
   260
    if (viddata->shell.zxdg) {
icculus@11848
   261
        if (output) {
icculus@11848
   262
            zxdg_toplevel_v6_set_fullscreen(wind->shell_surface.zxdg.roleobj.toplevel, output);
icculus@11848
   263
        } else {
icculus@11848
   264
            zxdg_toplevel_v6_unset_fullscreen(wind->shell_surface.zxdg.roleobj.toplevel);
icculus@11848
   265
        }
icculus@11848
   266
    } else {
icculus@11848
   267
        if (output) {
icculus@11848
   268
            wl_shell_surface_set_fullscreen(wind->shell_surface.wl,
icculus@11848
   269
                                            WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT,
icculus@11848
   270
                                            0, output);
icculus@11848
   271
        } else {
icculus@11848
   272
            wl_shell_surface_set_toplevel(wind->shell_surface.wl);
icculus@11848
   273
        }
icculus@11848
   274
    }
icculus@11848
   275
icculus@11848
   276
    WAYLAND_wl_display_flush( ((SDL_VideoData*)_this->driverdata)->display );
icculus@11848
   277
}
icculus@11848
   278
gabomdq@8062
   279
void Wayland_ShowWindow(_THIS, SDL_Window *window)
gabomdq@8062
   280
{
icculus@11848
   281
    struct wl_output *output = (struct wl_output *) window->fullscreen_mode.driverdata;
icculus@11848
   282
    SetFullscreen(_this, window, (window->flags & SDL_WINDOW_FULLSCREEN) ? output : NULL);
gabomdq@8062
   283
}
gabomdq@8062
   284
m@10614
   285
#ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
slouken@11284
   286
static void SDLCALL
slouken@11284
   287
QtExtendedSurface_OnHintChanged(void *userdata, const char *name,
m@10614
   288
        const char *oldValue, const char *newValue)
m@10614
   289
{
m@10614
   290
    struct qt_extended_surface *qt_extended_surface = userdata;
m@10614
   291
m@10614
   292
    if (name == NULL) {
m@10614
   293
        return;
m@10614
   294
    }
m@10614
   295
m@10614
   296
    if (strcmp(name, SDL_HINT_QTWAYLAND_CONTENT_ORIENTATION) == 0) {
m@10614
   297
        int32_t orientation = QT_EXTENDED_SURFACE_ORIENTATION_PRIMARYORIENTATION;
m@10614
   298
m@10614
   299
        if (newValue != NULL) {
m@10614
   300
            if (strcmp(newValue, "portrait") == 0) {
m@10614
   301
                orientation = QT_EXTENDED_SURFACE_ORIENTATION_PORTRAITORIENTATION;
m@10614
   302
            } else if (strcmp(newValue, "landscape") == 0) {
m@10614
   303
                orientation = QT_EXTENDED_SURFACE_ORIENTATION_LANDSCAPEORIENTATION;
m@10614
   304
            } else if (strcmp(newValue, "inverted-portrait") == 0) {
m@10614
   305
                orientation = QT_EXTENDED_SURFACE_ORIENTATION_INVERTEDPORTRAITORIENTATION;
m@10614
   306
            } else if (strcmp(newValue, "inverted-landscape") == 0) {
m@10614
   307
                orientation = QT_EXTENDED_SURFACE_ORIENTATION_INVERTEDLANDSCAPEORIENTATION;
m@10614
   308
            }
m@10614
   309
        }
m@10614
   310
m@10614
   311
        qt_extended_surface_set_content_orientation(qt_extended_surface, orientation);
m@10614
   312
    } else if (strcmp(name, SDL_HINT_QTWAYLAND_WINDOW_FLAGS) == 0) {
m@10614
   313
        uint32_t flags = 0;
m@10614
   314
m@10614
   315
        if (newValue != NULL) {
m@10614
   316
            char *tmp = strdup(newValue);
m@10614
   317
            char *saveptr = NULL;
m@10614
   318
m@10614
   319
            char *flag = strtok_r(tmp, " ", &saveptr);
m@10614
   320
            while (flag) {
m@10614
   321
                if (strcmp(flag, "OverridesSystemGestures") == 0) {
m@10614
   322
                    flags |= QT_EXTENDED_SURFACE_WINDOWFLAG_OVERRIDESSYSTEMGESTURES;
m@10614
   323
                } else if (strcmp(flag, "StaysOnTop") == 0) {
m@10614
   324
                    flags |= QT_EXTENDED_SURFACE_WINDOWFLAG_STAYSONTOP;
m@10614
   325
                } else if (strcmp(flag, "BypassWindowManager") == 0) {
m@10614
   326
                    // See https://github.com/qtproject/qtwayland/commit/fb4267103d
m@10614
   327
                    flags |= 4 /* QT_EXTENDED_SURFACE_WINDOWFLAG_BYPASSWINDOWMANAGER */;
m@10614
   328
                }
m@10614
   329
m@10614
   330
                flag = strtok_r(NULL, " ", &saveptr);
m@10614
   331
            }
m@10614
   332
m@10614
   333
            free(tmp);
m@10614
   334
        }
m@10614
   335
m@10614
   336
        qt_extended_surface_set_window_flags(qt_extended_surface, flags);
m@10614
   337
    }
m@10614
   338
}
m@10614
   339
m@10614
   340
static void QtExtendedSurface_Subscribe(struct qt_extended_surface *surface, const char *name)
m@10614
   341
{
m@10614
   342
    SDL_AddHintCallback(name, QtExtendedSurface_OnHintChanged, surface);
m@10614
   343
}
m@10614
   344
m@10614
   345
static void QtExtendedSurface_Unsubscribe(struct qt_extended_surface *surface, const char *name)
m@10614
   346
{
m@10614
   347
    SDL_DelHintCallback(name, QtExtendedSurface_OnHintChanged, surface);
m@10614
   348
}
m@10614
   349
#endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */
m@10614
   350
gabomdq@8062
   351
void
gabomdq@8062
   352
Wayland_SetWindowFullscreen(_THIS, SDL_Window * window,
gabomdq@8062
   353
                            SDL_VideoDisplay * _display, SDL_bool fullscreen)
gabomdq@8062
   354
{
icculus@11848
   355
    struct wl_output *output = (struct wl_output *) _display->driverdata;
icculus@11848
   356
    SetFullscreen(_this, window, fullscreen ? output : NULL);
gabomdq@8062
   357
}
gabomdq@8062
   358
slouken@10492
   359
void
slouken@10492
   360
Wayland_RestoreWindow(_THIS, SDL_Window * window)
slouken@10492
   361
{
slouken@10492
   362
    SDL_WindowData *wind = window->driverdata;
icculus@11848
   363
    const SDL_VideoData *viddata = (const SDL_VideoData *) _this->driverdata;
slouken@10492
   364
icculus@11848
   365
    if (viddata->shell.zxdg) {
icculus@11848
   366
    } else {
icculus@11848
   367
        wl_shell_surface_set_toplevel(wind->shell_surface.wl);
icculus@11848
   368
    }
slouken@10492
   369
slouken@10492
   370
    WAYLAND_wl_display_flush( ((SDL_VideoData*)_this->driverdata)->display );
slouken@10492
   371
}
slouken@10492
   372
slouken@10492
   373
void
slouken@10492
   374
Wayland_MaximizeWindow(_THIS, SDL_Window * window)
slouken@10492
   375
{
slouken@10492
   376
    SDL_WindowData *wind = window->driverdata;
icculus@11848
   377
    SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
slouken@10492
   378
icculus@11848
   379
    if (viddata->shell.zxdg) {
icculus@11848
   380
        zxdg_toplevel_v6_set_maximized(wind->shell_surface.zxdg.roleobj.toplevel);
icculus@11848
   381
    } else {
icculus@11848
   382
        wl_shell_surface_set_maximized(wind->shell_surface.wl, NULL);
icculus@11848
   383
    }
slouken@10492
   384
icculus@11848
   385
    WAYLAND_wl_display_flush( viddata->display );
slouken@10492
   386
}
slouken@10492
   387
gabomdq@8062
   388
int Wayland_CreateWindow(_THIS, SDL_Window *window)
gabomdq@8062
   389
{
gabomdq@8062
   390
    SDL_WindowData *data;
gabomdq@8062
   391
    SDL_VideoData *c;
gabomdq@8062
   392
    struct wl_region *region;
gabomdq@8062
   393
gabomdq@8062
   394
    data = calloc(1, sizeof *data);
gabomdq@8062
   395
    if (data == NULL)
philipp@9483
   396
        return SDL_OutOfMemory();
gabomdq@8062
   397
gabomdq@8062
   398
    c = _this->driverdata;
gabomdq@8062
   399
    window->driverdata = data;
gabomdq@8062
   400
gabomdq@8062
   401
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
gabomdq@8062
   402
        SDL_GL_LoadLibrary(NULL);
gabomdq@8062
   403
        window->flags |= SDL_WINDOW_OPENGL;
gabomdq@8062
   404
    }
gabomdq@8062
   405
gabomdq@8062
   406
    if (window->x == SDL_WINDOWPOS_UNDEFINED) {
gabomdq@8062
   407
        window->x = 0;
gabomdq@8062
   408
    }
gabomdq@8062
   409
    if (window->y == SDL_WINDOWPOS_UNDEFINED) {
gabomdq@8062
   410
        window->y = 0;
gabomdq@8062
   411
    }
gabomdq@8062
   412
gabomdq@8062
   413
    data->waylandData = c;
gabomdq@8062
   414
    data->sdlwindow = window;
gabomdq@8062
   415
gabomdq@8062
   416
    data->surface =
gabomdq@8062
   417
        wl_compositor_create_surface(c->compositor);
gabomdq@8062
   418
    wl_surface_set_user_data(data->surface, data);
icculus@11848
   419
icculus@11848
   420
    if (c->shell.zxdg) {
icculus@11848
   421
        data->shell_surface.zxdg.surface = zxdg_shell_v6_get_xdg_surface(c->shell.zxdg, data->surface);
icculus@11848
   422
        /* !!! FIXME: add popup role */
icculus@11848
   423
        data->shell_surface.zxdg.roleobj.toplevel = zxdg_surface_v6_get_toplevel(data->shell_surface.zxdg.surface);
icculus@11848
   424
        zxdg_toplevel_v6_add_listener(data->shell_surface.zxdg.roleobj.toplevel, &toplevel_listener_zxdg, data);
icculus@11848
   425
        zxdg_toplevel_v6_set_app_id(data->shell_surface.zxdg.roleobj.toplevel, c->classname);
icculus@11848
   426
    } else {
icculus@11848
   427
        data->shell_surface.wl = wl_shell_get_shell_surface(c->shell.wl, data->surface);
icculus@11848
   428
        wl_shell_surface_set_class(data->shell_surface.wl, c->classname);
icculus@11848
   429
    }
icculus@11848
   430
m@10614
   431
#ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
gabomdq@8082
   432
    if (c->surface_extension) {
gabomdq@8082
   433
        data->extended_surface = qt_surface_extension_get_extended_surface(
gabomdq@8082
   434
                c->surface_extension, data->surface);
m@10614
   435
m@10614
   436
        QtExtendedSurface_Subscribe(data->extended_surface, SDL_HINT_QTWAYLAND_CONTENT_ORIENTATION);
m@10614
   437
        QtExtendedSurface_Subscribe(data->extended_surface, SDL_HINT_QTWAYLAND_WINDOW_FLAGS);
gabomdq@8082
   438
    }
gabomdq@8082
   439
#endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */
m@8712
   440
gabomdq@8104
   441
    data->egl_window = WAYLAND_wl_egl_window_create(data->surface,
gabomdq@8062
   442
                                            window->w, window->h);
gabomdq@8062
   443
gabomdq@8062
   444
    /* Create the GLES window surface */
gabomdq@8062
   445
    data->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) data->egl_window);
gabomdq@8062
   446
    
gabomdq@8062
   447
    if (data->egl_surface == EGL_NO_SURFACE) {
philipp@9543
   448
        return SDL_SetError("failed to create a window surface");
gabomdq@8062
   449
    }
gabomdq@8062
   450
icculus@11848
   451
    if (c->shell.zxdg) {
icculus@11848
   452
        if (data->shell_surface.zxdg.surface) {
icculus@11848
   453
            zxdg_surface_v6_set_user_data(data->shell_surface.zxdg.surface, data);
icculus@11848
   454
            zxdg_surface_v6_add_listener(data->shell_surface.zxdg.surface, &shell_surface_listener_zxdg, data);
icculus@11848
   455
        }
icculus@11848
   456
    } else {
icculus@11848
   457
        if (data->shell_surface.wl) {
icculus@11848
   458
            wl_shell_surface_set_user_data(data->shell_surface.wl, data);
icculus@11848
   459
            wl_shell_surface_add_listener(data->shell_surface.wl, &shell_surface_listener_wl, data);
icculus@11848
   460
        }
gabomdq@8062
   461
    }
gabomdq@8062
   462
gabomdq@8082
   463
#ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
gabomdq@8082
   464
    if (data->extended_surface) {
gabomdq@8082
   465
        qt_extended_surface_set_user_data(data->extended_surface, data);
gabomdq@8082
   466
        qt_extended_surface_add_listener(data->extended_surface,
gabomdq@8082
   467
                                         &extended_surface_listener, data);
gabomdq@8082
   468
    }
gabomdq@8082
   469
#endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */
gabomdq@8082
   470
gabomdq@8062
   471
    region = wl_compositor_create_region(c->compositor);
gabomdq@8062
   472
    wl_region_add(region, 0, 0, window->w, window->h);
gabomdq@8062
   473
    wl_surface_set_opaque_region(data->surface, region);
gabomdq@8062
   474
    wl_region_destroy(region);
gabomdq@8062
   475
slouken@10304
   476
    if (c->relative_mouse_mode) {
slouken@10304
   477
        Wayland_input_lock_pointer(c->input);
slouken@10304
   478
    }
slouken@10304
   479
icculus@11848
   480
    wl_surface_commit(data->surface);
gabomdq@8104
   481
    WAYLAND_wl_display_flush(c->display);
gabomdq@8062
   482
icculus@11944
   483
    /* we have to wait until the surface gets a "configure" event, or
icculus@11944
   484
       use of this surface will fail. This is a new rule for xdg_shell. */
icculus@11944
   485
    if (c->shell.zxdg) {
icculus@11944
   486
        if (data->shell_surface.zxdg.surface) {
icculus@11944
   487
            while (!data->shell_surface.zxdg.initial_configure_seen) {
icculus@11944
   488
                WAYLAND_wl_display_flush(c->display);
icculus@11944
   489
                WAYLAND_wl_display_dispatch(c->display);
icculus@11944
   490
            }
icculus@11944
   491
        }
icculus@11944
   492
    }
icculus@11944
   493
gabomdq@8062
   494
    return 0;
gabomdq@8062
   495
}
gabomdq@8062
   496
gabomdq@8062
   497
void Wayland_SetWindowSize(_THIS, SDL_Window * window)
gabomdq@8062
   498
{
gabomdq@8062
   499
    SDL_VideoData *data = _this->driverdata;
gabomdq@8062
   500
    SDL_WindowData *wind = window->driverdata;
gabomdq@8062
   501
    struct wl_region *region;
gabomdq@8062
   502
gabomdq@8104
   503
    WAYLAND_wl_egl_window_resize(wind->egl_window, window->w, window->h, 0, 0);
gabomdq@8062
   504
gabomdq@8104
   505
    region =wl_compositor_create_region(data->compositor);
gabomdq@8062
   506
    wl_region_add(region, 0, 0, window->w, window->h);
gabomdq@8062
   507
    wl_surface_set_opaque_region(wind->surface, region);
gabomdq@8062
   508
    wl_region_destroy(region);
gabomdq@8062
   509
}
gabomdq@8062
   510
slouken@10492
   511
void Wayland_SetWindowTitle(_THIS, SDL_Window * window)
slouken@10492
   512
{
slouken@10492
   513
    SDL_WindowData *wind = window->driverdata;
icculus@11848
   514
    SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
slouken@10492
   515
    
slouken@10492
   516
    if (window->title != NULL) {
icculus@11848
   517
        if (viddata->shell.zxdg) {
icculus@11848
   518
            zxdg_toplevel_v6_set_title(wind->shell_surface.zxdg.roleobj.toplevel, window->title);
icculus@11848
   519
        } else {
icculus@11848
   520
            wl_shell_surface_set_title(wind->shell_surface.wl, window->title);
icculus@11848
   521
        }
slouken@10492
   522
    }
slouken@10492
   523
slouken@10492
   524
    WAYLAND_wl_display_flush( ((SDL_VideoData*)_this->driverdata)->display );
slouken@10492
   525
}
slouken@10492
   526
gabomdq@8062
   527
void Wayland_DestroyWindow(_THIS, SDL_Window *window)
gabomdq@8062
   528
{
gabomdq@8062
   529
    SDL_VideoData *data = _this->driverdata;
gabomdq@8062
   530
    SDL_WindowData *wind = window->driverdata;
gabomdq@8062
   531
gabomdq@8062
   532
    if (data) {
gabomdq@8062
   533
        SDL_EGL_DestroySurface(_this, wind->egl_surface);
gabomdq@8104
   534
        WAYLAND_wl_egl_window_destroy(wind->egl_window);
gabomdq@8062
   535
icculus@11848
   536
        if (data->shell.zxdg) {
icculus@11848
   537
            if (wind->shell_surface.zxdg.roleobj.toplevel) {
icculus@11848
   538
                zxdg_toplevel_v6_destroy(wind->shell_surface.zxdg.roleobj.toplevel);
icculus@11848
   539
            }
icculus@11848
   540
            if (wind->shell_surface.zxdg.surface) {
icculus@11848
   541
                zxdg_surface_v6_destroy(wind->shell_surface.zxdg.surface);
icculus@11848
   542
            }
icculus@11848
   543
        } else {
icculus@11848
   544
            if (wind->shell_surface.wl) {
icculus@11848
   545
                wl_shell_surface_destroy(wind->shell_surface.wl);
icculus@11848
   546
            }
icculus@11848
   547
        }
gabomdq@8062
   548
gabomdq@8082
   549
#ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
m@10614
   550
        if (wind->extended_surface) {
m@10614
   551
            QtExtendedSurface_Unsubscribe(wind->extended_surface, SDL_HINT_QTWAYLAND_CONTENT_ORIENTATION);
m@10614
   552
            QtExtendedSurface_Unsubscribe(wind->extended_surface, SDL_HINT_QTWAYLAND_WINDOW_FLAGS);
gabomdq@8082
   553
            qt_extended_surface_destroy(wind->extended_surface);
m@10614
   554
        }
gabomdq@8082
   555
#endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */
gabomdq@8062
   556
        wl_surface_destroy(wind->surface);
gabomdq@8062
   557
gabomdq@8062
   558
        SDL_free(wind);
gabomdq@8104
   559
        WAYLAND_wl_display_flush(data->display);
gabomdq@8062
   560
    }
slouken@8978
   561
    window->driverdata = NULL;
gabomdq@8062
   562
}
gabomdq@8062
   563
icculus@8116
   564
#endif /* SDL_VIDEO_DRIVER_WAYLAND && SDL_VIDEO_OPENGL_EGL */
icculus@8116
   565
gabomdq@8062
   566
/* vi: set ts=4 sw=4 expandtab: */