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