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