src/video/wayland/SDL_waylandwindow.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 24 Jun 2018 22:42:36 -0700
changeset 12033 bc702c5a7c3a
parent 11944 295cf9910d75
child 12201 8bdc4d340419
child 12367 8256aaaafe54
permissions -rw-r--r--
wayland: Implemented xdg-wm-base support.

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