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