src/video/x11/SDL_x11window.c
author Ryan C. Gordon
Mon, 17 Feb 2020 15:02:37 -0500
changeset 13534 24e8a27fa627
parent 13533 867352e06641
child 13535 dbcccb065928
permissions -rw-r--r--
x11: Don't delay an extra 10ms if we were just going to break out of the loop.
slouken@1951
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@13422
     3
  Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
slouken@1951
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1951
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1951
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1951
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@1951
    22
slouken@5481
    23
#if SDL_VIDEO_DRIVER_X11
slouken@5481
    24
slouken@7523
    25
#include "SDL_assert.h"
slouken@6662
    26
#include "SDL_hints.h"
slouken@1951
    27
#include "../SDL_sysvideo.h"
slouken@4465
    28
#include "../SDL_pixels_c.h"
slouken@1951
    29
#include "../../events/SDL_keyboard_c.h"
slouken@2940
    30
#include "../../events/SDL_mouse_c.h"
slouken@1951
    31
slouken@1951
    32
#include "SDL_x11video.h"
slouken@2940
    33
#include "SDL_x11mouse.h"
eligottlieb@4782
    34
#include "SDL_x11shape.h"
dimitris@6316
    35
#include "SDL_x11xinput2.h"
slouken@1951
    36
gabomdq@7659
    37
#if SDL_VIDEO_OPENGL_EGL
slouken@3161
    38
#include "SDL_x11opengles.h"
slouken@3161
    39
#endif
slouken@3161
    40
slouken@4465
    41
#include "SDL_timer.h"
slouken@3241
    42
#include "SDL_syswm.h"
icculus@10723
    43
#include "SDL_log.h"
slouken@3241
    44
slouken@3077
    45
#define _NET_WM_STATE_REMOVE    0l
slouken@3077
    46
#define _NET_WM_STATE_ADD       1l
slouken@3077
    47
icculus@6467
    48
static Bool isMapNotify(Display *dpy, XEvent *ev, XPointer win)
icculus@6467
    49
{
icculus@6467
    50
    return ev->type == MapNotify && ev->xmap.window == *((Window*)win);
icculus@6467
    51
}
icculus@6467
    52
static Bool isUnmapNotify(Display *dpy, XEvent *ev, XPointer win)
icculus@6467
    53
{
icculus@6467
    54
    return ev->type == UnmapNotify && ev->xunmap.window == *((Window*)win);
icculus@6467
    55
}
icculus@9838
    56
icculus@9838
    57
/*
icculus@6467
    58
static Bool isConfigureNotify(Display *dpy, XEvent *ev, XPointer win)
slouken@4518
    59
{
slouken@6480
    60
    return ev->type == ConfigureNotify && ev->xconfigure.window == *((Window*)win);
slouken@6480
    61
}
slouken@7632
    62
static Bool
icculus@7827
    63
X11_XIfEventTimeout(Display *display, XEvent *event_return, Bool (*predicate)(), XPointer arg, int timeoutMS)
slouken@7632
    64
{
slouken@7632
    65
    Uint32 start = SDL_GetTicks();
slouken@7632
    66
icculus@7827
    67
    while (!X11_XCheckIfEvent(display, event_return, predicate, arg)) {
slouken@7857
    68
        if (SDL_TICKS_PASSED(SDL_GetTicks(), start + timeoutMS)) {
slouken@7632
    69
            return False;
slouken@7632
    70
        }
slouken@7632
    71
    }
slouken@7632
    72
    return True;
slouken@7632
    73
}
slouken@7632
    74
*/
slouken@7632
    75
icculus@6467
    76
static SDL_bool
icculus@6467
    77
X11_IsWindowLegacyFullscreen(_THIS, SDL_Window * window)
icculus@6467
    78
{
icculus@6467
    79
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@6467
    80
    return (data->fswindow != 0);
slouken@4518
    81
}
slouken@4518
    82
slouken@4522
    83
static SDL_bool
slouken@4522
    84
X11_IsWindowMapped(_THIS, SDL_Window * window)
slouken@4522
    85
{
slouken@4522
    86
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@4522
    87
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@4522
    88
    XWindowAttributes attr;
slouken@4522
    89
icculus@7827
    90
    X11_XGetWindowAttributes(videodata->display, data->xwindow, &attr);
slouken@4522
    91
    if (attr.map_state != IsUnmapped) {
slouken@4522
    92
        return SDL_TRUE;
slouken@4522
    93
    } else {
slouken@4522
    94
        return SDL_FALSE;
slouken@4522
    95
    }
slouken@4522
    96
}
slouken@4522
    97
slouken@6540
    98
#if 0
slouken@6481
    99
static SDL_bool
slouken@6481
   100
X11_IsActionAllowed(SDL_Window *window, Atom action)
slouken@4522
   101
{
slouken@6481
   102
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@6481
   103
    Atom _NET_WM_ALLOWED_ACTIONS = data->videodata->_NET_WM_ALLOWED_ACTIONS;
slouken@6481
   104
    Atom type;
slouken@6481
   105
    Display *display = data->videodata->display;
slouken@6481
   106
    int form;
slouken@6481
   107
    unsigned long remain;
slouken@6481
   108
    unsigned long len, i;
slouken@6481
   109
    Atom *list;
slouken@6481
   110
    SDL_bool ret = SDL_FALSE;
slouken@6481
   111
icculus@7827
   112
    if (X11_XGetWindowProperty(display, data->xwindow, _NET_WM_ALLOWED_ACTIONS, 0, 1024, False, XA_ATOM, &type, &form, &len, &remain, (unsigned char **)&list) == Success)
slouken@6481
   113
    {
slouken@6481
   114
        for (i=0; i<len; ++i)
slouken@6481
   115
        {
slouken@6481
   116
            if (list[i] == action) {
slouken@6481
   117
                ret = SDL_TRUE;
slouken@6481
   118
                break;
slouken@6481
   119
            }
slouken@6481
   120
        }
icculus@7827
   121
        X11_XFree(list);
slouken@6481
   122
    }
slouken@6481
   123
    return ret;
slouken@6481
   124
}
slouken@6540
   125
#endif /* 0 */
slouken@6481
   126
slouken@6487
   127
void
slouken@6487
   128
X11_SetNetWMState(_THIS, Window xwindow, Uint32 flags)
slouken@6481
   129
{
slouken@6481
   130
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@6487
   131
    Display *display = videodata->display;
icculus@10010
   132
    /* !!! FIXME: just dereference videodata below instead of copying to locals. */
slouken@6487
   133
    Atom _NET_WM_STATE = videodata->_NET_WM_STATE;
gabomdq@7678
   134
    /* Atom _NET_WM_STATE_HIDDEN = videodata->_NET_WM_STATE_HIDDEN; */
slouken@6481
   135
    Atom _NET_WM_STATE_FOCUSED = videodata->_NET_WM_STATE_FOCUSED;
slouken@6481
   136
    Atom _NET_WM_STATE_MAXIMIZED_VERT = videodata->_NET_WM_STATE_MAXIMIZED_VERT;
slouken@6481
   137
    Atom _NET_WM_STATE_MAXIMIZED_HORZ = videodata->_NET_WM_STATE_MAXIMIZED_HORZ;
slouken@6481
   138
    Atom _NET_WM_STATE_FULLSCREEN = videodata->_NET_WM_STATE_FULLSCREEN;
icculus@10021
   139
    Atom _NET_WM_STATE_ABOVE = videodata->_NET_WM_STATE_ABOVE;
icculus@10021
   140
    Atom _NET_WM_STATE_SKIP_TASKBAR = videodata->_NET_WM_STATE_SKIP_TASKBAR;
icculus@10021
   141
    Atom _NET_WM_STATE_SKIP_PAGER = videodata->_NET_WM_STATE_SKIP_PAGER;
icculus@10021
   142
    Atom atoms[16];
slouken@4522
   143
    int count = 0;
slouken@4522
   144
slouken@6486
   145
    /* The window manager sets this property, we shouldn't set it.
slouken@6486
   146
       If we did, this would indicate to the window manager that we don't
icculus@7827
   147
       actually want to be mapped during X11_XMapRaised(), which would be bad.
slouken@6486
   148
     *
slouken@6481
   149
    if (flags & SDL_WINDOW_HIDDEN) {
slouken@6481
   150
        atoms[count++] = _NET_WM_STATE_HIDDEN;
slouken@6481
   151
    }
slouken@6486
   152
    */
icculus@10021
   153
icculus@10021
   154
    if (flags & SDL_WINDOW_ALWAYS_ON_TOP) {
icculus@10021
   155
        atoms[count++] = _NET_WM_STATE_ABOVE;
icculus@10021
   156
    }
icculus@10021
   157
    if (flags & SDL_WINDOW_SKIP_TASKBAR) {
icculus@10021
   158
        atoms[count++] = _NET_WM_STATE_SKIP_TASKBAR;
icculus@10021
   159
        atoms[count++] = _NET_WM_STATE_SKIP_PAGER;
icculus@10021
   160
    }
slouken@6481
   161
    if (flags & SDL_WINDOW_INPUT_FOCUS) {
slouken@6481
   162
        atoms[count++] = _NET_WM_STATE_FOCUSED;
slouken@4522
   163
    }
slouken@6481
   164
    if (flags & SDL_WINDOW_MAXIMIZED) {
slouken@6481
   165
        atoms[count++] = _NET_WM_STATE_MAXIMIZED_VERT;
slouken@6481
   166
        atoms[count++] = _NET_WM_STATE_MAXIMIZED_HORZ;
slouken@6481
   167
    }
slouken@6481
   168
    if (flags & SDL_WINDOW_FULLSCREEN) {
slouken@6481
   169
        atoms[count++] = _NET_WM_STATE_FULLSCREEN;
slouken@4522
   170
    }
icculus@10021
   171
icculus@10021
   172
    SDL_assert(count <= SDL_arraysize(atoms));
icculus@10021
   173
slouken@6487
   174
    if (count > 0) {
icculus@7827
   175
        X11_XChangeProperty(display, xwindow, _NET_WM_STATE, XA_ATOM, 32,
slouken@6487
   176
                        PropModeReplace, (unsigned char *)atoms, count);
slouken@6487
   177
    } else {
icculus@7827
   178
        X11_XDeleteProperty(display, xwindow, _NET_WM_STATE);
slouken@6487
   179
    }
slouken@4522
   180
}
slouken@4522
   181
slouken@6481
   182
Uint32
slouken@6487
   183
X11_GetNetWMState(_THIS, Window xwindow)
slouken@6481
   184
{
slouken@6481
   185
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@6487
   186
    Display *display = videodata->display;
slouken@6481
   187
    Atom _NET_WM_STATE = videodata->_NET_WM_STATE;
slouken@6481
   188
    Atom _NET_WM_STATE_HIDDEN = videodata->_NET_WM_STATE_HIDDEN;
slouken@6481
   189
    Atom _NET_WM_STATE_FOCUSED = videodata->_NET_WM_STATE_FOCUSED;
slouken@6481
   190
    Atom _NET_WM_STATE_MAXIMIZED_VERT = videodata->_NET_WM_STATE_MAXIMIZED_VERT;
slouken@6481
   191
    Atom _NET_WM_STATE_MAXIMIZED_HORZ = videodata->_NET_WM_STATE_MAXIMIZED_HORZ;
slouken@6481
   192
    Atom _NET_WM_STATE_FULLSCREEN = videodata->_NET_WM_STATE_FULLSCREEN;
slouken@6481
   193
    Atom actualType;
slouken@6481
   194
    int actualFormat;
slouken@6481
   195
    unsigned long i, numItems, bytesAfter;
slouken@6481
   196
    unsigned char *propertyValue = NULL;
slouken@6481
   197
    long maxLength = 1024;
slouken@6481
   198
    Uint32 flags = 0;
slouken@6481
   199
icculus@7827
   200
    if (X11_XGetWindowProperty(display, xwindow, _NET_WM_STATE,
slouken@6481
   201
                           0l, maxLength, False, XA_ATOM, &actualType,
slouken@6481
   202
                           &actualFormat, &numItems, &bytesAfter,
slouken@6481
   203
                           &propertyValue) == Success) {
slouken@6481
   204
        Atom *atoms = (Atom *) propertyValue;
slouken@6481
   205
        int maximized = 0;
slouken@6481
   206
        int fullscreen = 0;
slouken@6481
   207
slouken@6481
   208
        for (i = 0; i < numItems; ++i) {
slouken@6481
   209
            if (atoms[i] == _NET_WM_STATE_HIDDEN) {
slouken@6486
   210
                flags |= SDL_WINDOW_HIDDEN;
slouken@6481
   211
            } else if (atoms[i] == _NET_WM_STATE_FOCUSED) {
slouken@6481
   212
                flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@6481
   213
            } else if (atoms[i] == _NET_WM_STATE_MAXIMIZED_VERT) {
slouken@6481
   214
                maximized |= 1;
slouken@6481
   215
            } else if (atoms[i] == _NET_WM_STATE_MAXIMIZED_HORZ) {
slouken@6481
   216
                maximized |= 2;
slouken@6481
   217
            } else if ( atoms[i] == _NET_WM_STATE_FULLSCREEN) {
slouken@6481
   218
                fullscreen = 1;
slouken@6481
   219
            }
slouken@6481
   220
        }
slouken@6481
   221
        if (maximized == 3) {
slouken@6481
   222
            flags |= SDL_WINDOW_MAXIMIZED;
icculus@10017
   223
        }
icculus@10017
   224
icculus@10017
   225
        if (fullscreen == 1) {
slouken@6481
   226
            flags |= SDL_WINDOW_FULLSCREEN;
slouken@6481
   227
        }
slouken@11151
   228
slouken@11151
   229
        /* If the window is unmapped, numItems will be zero and _NET_WM_STATE_HIDDEN
slouken@11151
   230
         * will not be set. Do an additional check to see if the window is unmapped
slouken@11151
   231
         * and mark it as SDL_WINDOW_HIDDEN if it is.
slouken@11151
   232
         */
slouken@11151
   233
        {
slouken@11151
   234
            XWindowAttributes attr;
slouken@11151
   235
            SDL_memset(&attr,0,sizeof(attr));
slouken@11151
   236
            X11_XGetWindowAttributes(videodata->display, xwindow, &attr);
slouken@11151
   237
            if (attr.map_state == IsUnmapped) {
slouken@11151
   238
                flags |= SDL_WINDOW_HIDDEN;
slouken@11151
   239
            }
slouken@11151
   240
        }
icculus@7827
   241
        X11_XFree(propertyValue);
slouken@6481
   242
    }
slouken@6481
   243
slouken@6484
   244
    /* FIXME, check the size hints for resizable */
gabomdq@7678
   245
    /* flags |= SDL_WINDOW_RESIZABLE; */
slouken@6481
   246
slouken@6481
   247
    return flags;
slouken@6481
   248
}
slouken@6481
   249
slouken@1951
   250
static int
slouken@1951
   251
SetupWindowData(_THIS, SDL_Window * window, Window w, BOOL created)
slouken@1951
   252
{
slouken@1951
   253
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1951
   254
    SDL_WindowData *data;
slouken@1951
   255
    int numwindows = videodata->numwindows;
bob@2324
   256
    int windowlistlength = videodata->windowlistlength;
slouken@1951
   257
    SDL_WindowData **windowlist = videodata->windowlist;
slouken@1951
   258
slouken@1951
   259
    /* Allocate the window data */
bob@2323
   260
    data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
slouken@1951
   261
    if (!data) {
icculus@7037
   262
        return SDL_OutOfMemory();
slouken@1951
   263
    }
slouken@3685
   264
    data->window = window;
slouken@3685
   265
    data->xwindow = w;
slouken@1951
   266
#ifdef X_HAVE_UTF8_STRING
slouken@7007
   267
    if (SDL_X11_HAVE_UTF8 && videodata->im) {
slouken@1951
   268
        data->ic =
icculus@7827
   269
            X11_XCreateIC(videodata->im, XNClientWindow, w, XNFocusWindow, w,
slouken@1951
   270
                       XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
slouken@8707
   271
                       NULL);
slouken@1951
   272
    }
slouken@1951
   273
#endif
slouken@1951
   274
    data->created = created;
slouken@1951
   275
    data->videodata = videodata;
slouken@1951
   276
bob@2323
   277
    /* Associate the data with the window */
bob@2324
   278
bob@2324
   279
    if (numwindows < windowlistlength) {
bob@2324
   280
        windowlist[numwindows] = data;
bob@2324
   281
        videodata->numwindows++;
bob@2324
   282
    } else {
bob@2324
   283
        windowlist =
bob@2324
   284
            (SDL_WindowData **) SDL_realloc(windowlist,
bob@2324
   285
                                            (numwindows +
bob@2324
   286
                                             1) * sizeof(*windowlist));
bob@2324
   287
        if (!windowlist) {
bob@2324
   288
            SDL_free(data);
icculus@7037
   289
            return SDL_OutOfMemory();
bob@2323
   290
        }
bob@2324
   291
        windowlist[numwindows] = data;
bob@2324
   292
        videodata->numwindows++;
bob@2324
   293
        videodata->windowlistlength++;
bob@2324
   294
        videodata->windowlist = windowlist;
bob@2323
   295
    }
bob@2323
   296
slouken@1951
   297
    /* Fill in the SDL window with the window data */
slouken@1951
   298
    {
slouken@1951
   299
        XWindowAttributes attrib;
slouken@1951
   300
icculus@7827
   301
        X11_XGetWindowAttributes(data->videodata->display, w, &attrib);
slouken@1951
   302
        window->x = attrib.x;
slouken@1951
   303
        window->y = attrib.y;
slouken@1951
   304
        window->w = attrib.width;
slouken@1951
   305
        window->h = attrib.height;
slouken@1951
   306
        if (attrib.map_state != IsUnmapped) {
slouken@1951
   307
            window->flags |= SDL_WINDOW_SHOWN;
slouken@1951
   308
        } else {
slouken@1951
   309
            window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1951
   310
        }
slouken@5182
   311
        data->visual = attrib.visual;
slouken@5466
   312
        data->colormap = attrib.colormap;
slouken@1951
   313
    }
slouken@3077
   314
slouken@6487
   315
    window->flags |= X11_GetNetWMState(_this, w);
slouken@3077
   316
icculus@6024
   317
    {
icculus@6024
   318
        Window FocalWindow;
icculus@6024
   319
        int RevertTo=0;
icculus@7827
   320
        X11_XGetInputFocus(data->videodata->display, &FocalWindow, &RevertTo);
icculus@6024
   321
        if (FocalWindow==w)
icculus@6024
   322
        {
icculus@6024
   323
            window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@6481
   324
        }
slouken@6481
   325
slouken@6481
   326
        if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
icculus@6024
   327
            SDL_SetKeyboardFocus(data->window);
icculus@6024
   328
        }
icculus@6024
   329
icculus@6024
   330
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
icculus@6024
   331
            /* Tell x11 to clip mouse */
icculus@6024
   332
        }
icculus@6024
   333
    }
icculus@6024
   334
slouken@1951
   335
    /* All done! */
slouken@6487
   336
    window->driverdata = data;
slouken@1951
   337
    return 0;
slouken@1951
   338
}
slouken@1951
   339
icculus@6422
   340
static void
icculus@6422
   341
SetWindowBordered(Display *display, int screen, Window window, SDL_bool border)
icculus@6422
   342
{
icculus@6422
   343
    /*
icculus@6422
   344
     * this code used to check for KWM_WIN_DECORATION, but KDE hasn't
icculus@6422
   345
     *  supported it for years and years. It now respects _MOTIF_WM_HINTS.
icculus@6422
   346
     *  Gnome is similar: just use the Motif atom.
icculus@6422
   347
     */
icculus@6422
   348
icculus@7827
   349
    Atom WM_HINTS = X11_XInternAtom(display, "_MOTIF_WM_HINTS", True);
icculus@6422
   350
    if (WM_HINTS != None) {
icculus@6422
   351
        /* Hints used by Motif compliant window managers */
icculus@6422
   352
        struct
icculus@6422
   353
        {
icculus@6422
   354
            unsigned long flags;
icculus@6422
   355
            unsigned long functions;
icculus@6422
   356
            unsigned long decorations;
icculus@6422
   357
            long input_mode;
icculus@6422
   358
            unsigned long status;
icculus@6422
   359
        } MWMHints = {
icculus@6422
   360
            (1L << 1), 0, border ? 1 : 0, 0, 0
icculus@6422
   361
        };
icculus@6422
   362
icculus@7827
   363
        X11_XChangeProperty(display, window, WM_HINTS, WM_HINTS, 32,
icculus@6422
   364
                        PropModeReplace, (unsigned char *) &MWMHints,
urkle@9460
   365
                        sizeof(MWMHints) / sizeof(long));
icculus@6422
   366
    } else {  /* set the transient hints instead, if necessary */
icculus@7827
   367
        X11_XSetTransientForHint(display, window, RootWindow(display, screen));
icculus@6422
   368
    }
icculus@6422
   369
}
icculus@6422
   370
slouken@1951
   371
int
slouken@1951
   372
X11_CreateWindow(_THIS, SDL_Window * window)
slouken@1951
   373
{
slouken@1951
   374
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   375
    SDL_DisplayData *displaydata =
slouken@5246
   376
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@7683
   377
    SDL_WindowData *windowdata;
slouken@4521
   378
    Display *display = data->display;
slouken@4521
   379
    int screen = displaydata->screen;
slouken@1951
   380
    Visual *visual;
slouken@1951
   381
    int depth;
slouken@1951
   382
    XSetWindowAttributes xattr;
slouken@1951
   383
    Window w;
urkle@6462
   384
    XSizeHints *sizehints;
urkle@6462
   385
    XWMHints *wmhints;
urkle@6462
   386
    XClassHint *classhints;
jstpierre@7490
   387
    Atom _NET_WM_BYPASS_COMPOSITOR;
slouken@4521
   388
    Atom _NET_WM_WINDOW_TYPE;
icculus@10021
   389
    Atom wintype;
icculus@10021
   390
    const char *wintype_name = NULL;
icculus@11127
   391
    long compositor = 1;
mail@6167
   392
    Atom _NET_WM_PID;
icculus@8785
   393
    long fevent = 0;
slouken@4518
   394
gabomdq@7659
   395
#if SDL_VIDEO_OPENGL_GLX || SDL_VIDEO_OPENGL_EGL
slouken@13057
   396
    const char *forced_visual_id = SDL_GetHint(SDL_HINT_VIDEO_X11_WINDOW_VISUALID);
slouken@13057
   397
slouken@13057
   398
    if (forced_visual_id != NULL && forced_visual_id[0] != '\0')
slouken@13057
   399
    {
slouken@13057
   400
        XVisualInfo *vi, template;
slouken@13057
   401
        int nvis;
slouken@13057
   402
slouken@13057
   403
        SDL_zero(template);
slouken@13057
   404
        template.visualid = SDL_strtol(forced_visual_id, NULL, 0);
slouken@13057
   405
        vi = X11_XGetVisualInfo(display, VisualIDMask, &template, &nvis);
slouken@13057
   406
        if (vi) {
slouken@13057
   407
            visual = vi->visual;
slouken@13057
   408
            depth = vi->depth;
slouken@13057
   409
            X11_XFree(vi);
slouken@13057
   410
        }
slouken@13057
   411
        else
slouken@13057
   412
        {
slouken@13057
   413
            return -1;
slouken@13057
   414
        }
slouken@13057
   415
    }
slouken@13057
   416
    else if ((window->flags & SDL_WINDOW_OPENGL) &&
pgriffais@7737
   417
        !SDL_getenv("SDL_VIDEO_X11_VISUALID")) {
gabomdq@7723
   418
        XVisualInfo *vinfo = NULL;
slouken@1952
   419
gabomdq@7659
   420
#if SDL_VIDEO_OPENGL_EGL
slouken@13475
   421
        if (((_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) ||
slouken@13475
   422
             SDL_GetHintBoolean(SDL_HINT_VIDEO_X11_FORCE_EGL, SDL_FALSE))
gabomdq@7808
   423
#if SDL_VIDEO_OPENGL_GLX            
slouken@10806
   424
            && ( !_this->gl_data || X11_GL_UseEGL(_this) )
gabomdq@7808
   425
#endif
slouken@8780
   426
        ) {
slouken@6369
   427
            vinfo = X11_GLES_GetVisual(_this, display, screen);
slouken@6369
   428
        } else
slouken@6369
   429
#endif
slouken@6369
   430
        {
slouken@6370
   431
#if SDL_VIDEO_OPENGL_GLX
slouken@6369
   432
            vinfo = X11_GL_GetVisual(_this, display, screen);
slouken@6370
   433
#endif
slouken@1952
   434
        }
gabomdq@7723
   435
slouken@3161
   436
        if (!vinfo) {
slouken@3161
   437
            return -1;
slouken@3161
   438
        }
slouken@3161
   439
        visual = vinfo->visual;
slouken@3161
   440
        depth = vinfo->depth;
icculus@7827
   441
        X11_XFree(vinfo);
slouken@3161
   442
    } else
slouken@3161
   443
#endif
slouken@1952
   444
    {
slouken@1951
   445
        visual = displaydata->visual;
slouken@1951
   446
        depth = displaydata->depth;
slouken@1951
   447
    }
slouken@1951
   448
icculus@10021
   449
    xattr.override_redirect = ((window->flags & SDL_WINDOW_TOOLTIP) || (window->flags & SDL_WINDOW_POPUP_MENU)) ? True : False;
slouken@7750
   450
    xattr.background_pixmap = None;
slouken@1951
   451
    xattr.border_pixel = 0;
slouken@5466
   452
slouken@5466
   453
    if (visual->class == DirectColor) {
slouken@5466
   454
        XColor *colorcells;
slouken@5466
   455
        int i;
slouken@5466
   456
        int ncolors;
slouken@5466
   457
        int rmax, gmax, bmax;
slouken@5466
   458
        int rmask, gmask, bmask;
slouken@5466
   459
        int rshift, gshift, bshift;
slouken@5466
   460
slouken@5466
   461
        xattr.colormap =
icculus@7827
   462
            X11_XCreateColormap(display, RootWindow(display, screen),
slouken@5466
   463
                            visual, AllocAll);
slouken@5466
   464
slouken@5466
   465
        /* If we can't create a colormap, then we must die */
slouken@5466
   466
        if (!xattr.colormap) {
icculus@7037
   467
            return SDL_SetError("Could not create writable colormap");
slouken@5466
   468
        }
slouken@5466
   469
slouken@5466
   470
        /* OK, we got a colormap, now fill it in as best as we can */
slouken@5466
   471
        colorcells = SDL_malloc(visual->map_entries * sizeof(XColor));
slouken@5466
   472
        if (!colorcells) {
icculus@7037
   473
            return SDL_OutOfMemory();
slouken@5466
   474
        }
slouken@5466
   475
        ncolors = visual->map_entries;
slouken@5466
   476
        rmax = 0xffff;
slouken@5466
   477
        gmax = 0xffff;
slouken@5466
   478
        bmax = 0xffff;
slouken@5466
   479
slouken@5466
   480
        rshift = 0;
slouken@5466
   481
        rmask = visual->red_mask;
slouken@5466
   482
        while (0 == (rmask & 1)) {
slouken@5466
   483
            rshift++;
slouken@5466
   484
            rmask >>= 1;
slouken@5466
   485
        }
slouken@5466
   486
slouken@5466
   487
        gshift = 0;
slouken@5466
   488
        gmask = visual->green_mask;
slouken@5466
   489
        while (0 == (gmask & 1)) {
slouken@5466
   490
            gshift++;
slouken@5466
   491
            gmask >>= 1;
slouken@5466
   492
        }
slouken@5466
   493
slouken@5466
   494
        bshift = 0;
slouken@5466
   495
        bmask = visual->blue_mask;
slouken@5466
   496
        while (0 == (bmask & 1)) {
slouken@5466
   497
            bshift++;
slouken@5466
   498
            bmask >>= 1;
slouken@5466
   499
        }
slouken@5466
   500
slouken@5466
   501
        /* build the color table pixel values */
slouken@5466
   502
        for (i = 0; i < ncolors; i++) {
slouken@5466
   503
            Uint32 red = (rmax * i) / (ncolors - 1);
slouken@5466
   504
            Uint32 green = (gmax * i) / (ncolors - 1);
slouken@5466
   505
            Uint32 blue = (bmax * i) / (ncolors - 1);
slouken@5466
   506
slouken@5466
   507
            Uint32 rbits = (rmask * i) / (ncolors - 1);
slouken@5466
   508
            Uint32 gbits = (gmask * i) / (ncolors - 1);
slouken@5466
   509
            Uint32 bbits = (bmask * i) / (ncolors - 1);
slouken@5466
   510
slouken@5466
   511
            Uint32 pix =
slouken@5466
   512
                (rbits << rshift) | (gbits << gshift) | (bbits << bshift);
slouken@5466
   513
slouken@5466
   514
            colorcells[i].pixel = pix;
slouken@5466
   515
slouken@5466
   516
            colorcells[i].red = red;
slouken@5466
   517
            colorcells[i].green = green;
slouken@5466
   518
            colorcells[i].blue = blue;
slouken@5466
   519
slouken@5466
   520
            colorcells[i].flags = DoRed | DoGreen | DoBlue;
slouken@5466
   521
        }
slouken@5466
   522
icculus@7827
   523
        X11_XStoreColors(display, xattr.colormap, colorcells, ncolors);
slouken@5466
   524
slouken@5466
   525
        SDL_free(colorcells);
slouken@5466
   526
    } else {
slouken@5466
   527
        xattr.colormap =
icculus@7827
   528
            X11_XCreateColormap(display, RootWindow(display, screen),
slouken@5466
   529
                            visual, AllocNone);
slouken@5466
   530
    }
slouken@1951
   531
icculus@7827
   532
    w = X11_XCreateWindow(display, RootWindow(display, screen),
slouken@5386
   533
                      window->x, window->y, window->w, window->h,
slouken@5386
   534
                      0, depth, InputOutput, visual,
slouken@7750
   535
                      (CWOverrideRedirect | CWBackPixmap | CWBorderPixel |
slouken@1951
   536
                       CWColormap), &xattr);
slouken@1952
   537
    if (!w) {
icculus@7037
   538
        return SDL_SetError("Couldn't create window");
slouken@1952
   539
    }
slouken@1951
   540
icculus@6422
   541
    SetWindowBordered(display, screen, w,
icculus@6422
   542
                      (window->flags & SDL_WINDOW_BORDERLESS) == 0);
slouken@1951
   543
icculus@7827
   544
    sizehints = X11_XAllocSizeHints();
mail@6167
   545
    /* Setup the normal size hints */
urkle@6462
   546
    sizehints->flags = 0;
mail@6167
   547
    if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
urkle@6462
   548
        sizehints->min_width = sizehints->max_width = window->w;
urkle@6462
   549
        sizehints->min_height = sizehints->max_height = window->h;
urkle@6462
   550
        sizehints->flags |= (PMaxSize | PMinSize);
slouken@1951
   551
    }
urkle@6462
   552
    sizehints->x = window->x;
urkle@6462
   553
    sizehints->y = window->y;
urkle@6462
   554
    sizehints->flags |= USPosition;
slouken@1951
   555
mail@6167
   556
    /* Setup the input hints so we get keyboard input */
icculus@7827
   557
    wmhints = X11_XAllocWMHints();
urkle@6462
   558
    wmhints->input = True;
icculus@10023
   559
    wmhints->window_group = data->window_group;
icculus@10023
   560
    wmhints->flags = InputHint | WindowGroupHint;
mail@6167
   561
mail@6167
   562
    /* Setup the class hints so we can get an icon (AfterStep) */
icculus@7827
   563
    classhints = X11_XAllocClassHint();
urkle@6462
   564
    classhints->res_name = data->classname;
urkle@6462
   565
    classhints->res_class = data->classname;
mail@6167
   566
mail@6167
   567
    /* Set the size, input and class hints, and define WM_CLIENT_MACHINE and WM_LOCALE_NAME */
icculus@7827
   568
    X11_XSetWMProperties(display, w, NULL, NULL, NULL, 0, sizehints, wmhints, classhints);
mail@6167
   569
icculus@7827
   570
    X11_XFree(sizehints);
icculus@7827
   571
    X11_XFree(wmhints);
icculus@7827
   572
    X11_XFree(classhints);
mail@6167
   573
    /* Set the PID related to the window for the given hostname, if possible */
mail@6167
   574
    if (data->pid > 0) {
icculus@10014
   575
        long pid = (long) data->pid;
icculus@7827
   576
        _NET_WM_PID = X11_XInternAtom(display, "_NET_WM_PID", False);
icculus@7827
   577
        X11_XChangeProperty(display, w, _NET_WM_PID, XA_CARDINAL, 32, PropModeReplace,
icculus@10014
   578
                        (unsigned char *) &pid, 1);
slouken@1951
   579
    }
slouken@1951
   580
slouken@4522
   581
    /* Set the window manager state */
slouken@6487
   582
    X11_SetNetWMState(_this, w, window->flags);
slouken@4518
   583
icculus@10021
   584
    compositor = 2;  /* don't disable compositing except for "normal" windows */
icculus@10021
   585
icculus@10021
   586
    if (window->flags & SDL_WINDOW_UTILITY) {
icculus@10021
   587
        wintype_name = "_NET_WM_WINDOW_TYPE_UTILITY";
icculus@10021
   588
    } else if (window->flags & SDL_WINDOW_TOOLTIP) {
icculus@10021
   589
        wintype_name = "_NET_WM_WINDOW_TYPE_TOOLTIP";
icculus@10021
   590
    } else if (window->flags & SDL_WINDOW_POPUP_MENU) {
icculus@10021
   591
        wintype_name = "_NET_WM_WINDOW_TYPE_POPUP_MENU";
icculus@10021
   592
    } else {
icculus@10021
   593
        wintype_name = "_NET_WM_WINDOW_TYPE_NORMAL";
icculus@10021
   594
        compositor = 1;  /* disable compositing for "normal" windows */
icculus@10021
   595
    }
icculus@10021
   596
icculus@10021
   597
    /* Let the window manager know what type of window we are. */
icculus@7827
   598
    _NET_WM_WINDOW_TYPE = X11_XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
icculus@10021
   599
    wintype = X11_XInternAtom(display, wintype_name, False);
icculus@7827
   600
    X11_XChangeProperty(display, w, _NET_WM_WINDOW_TYPE, XA_ATOM, 32,
icculus@10021
   601
                    PropModeReplace, (unsigned char *)&wintype, 1);
slouken@11876
   602
    if (SDL_GetHintBoolean(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, SDL_TRUE)) {
slouken@11876
   603
        _NET_WM_BYPASS_COMPOSITOR = X11_XInternAtom(display, "_NET_WM_BYPASS_COMPOSITOR", False);
slouken@11876
   604
        X11_XChangeProperty(display, w, _NET_WM_BYPASS_COMPOSITOR, XA_CARDINAL, 32,
slouken@11876
   605
                        PropModeReplace,
slouken@11876
   606
                        (unsigned char *)&compositor, 1);
slouken@11876
   607
    }
slouken@7191
   608
icculus@6638
   609
    {
icculus@10029
   610
        Atom protocols[3];
slouken@9899
   611
        int proto_count = 0;
slouken@9899
   612
icculus@10029
   613
        protocols[proto_count++] = data->WM_DELETE_WINDOW; /* Allow window to be deleted by the WM */
icculus@10029
   614
        protocols[proto_count++] = data->WM_TAKE_FOCUS; /* Since we will want to set input focus explicitly */
icculus@10029
   615
slouken@9899
   616
        /* Default to using ping if there is no hint */
slouken@10499
   617
        if (SDL_GetHintBoolean(SDL_HINT_VIDEO_X11_NET_WM_PING, SDL_TRUE)) {
icculus@10029
   618
            protocols[proto_count++] = data->_NET_WM_PING; /* Respond so WM knows we're alive */
slouken@9899
   619
        }
slouken@9899
   620
slouken@9899
   621
        SDL_assert(proto_count <= sizeof(protocols) / sizeof(protocols[0]));
slouken@9899
   622
slouken@9899
   623
        X11_XSetWMProtocols(display, w, protocols, proto_count);
icculus@6638
   624
    }
slouken@1951
   625
slouken@1951
   626
    if (SetupWindowData(_this, window, w, SDL_TRUE) < 0) {
icculus@7827
   627
        X11_XDestroyWindow(display, w);
slouken@1951
   628
        return -1;
slouken@1951
   629
    }
slouken@7683
   630
    windowdata = (SDL_WindowData *) window->driverdata;
slouken@5386
   631
slouken@13475
   632
#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2 || SDL_VIDEO_OPENGL_EGL
gabomdq@7723
   633
    if ((window->flags & SDL_WINDOW_OPENGL) && 
slouken@13475
   634
        ((_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) ||
slouken@13475
   635
         SDL_GetHintBoolean(SDL_HINT_VIDEO_X11_FORCE_EGL, SDL_FALSE))
gabomdq@7808
   636
#if SDL_VIDEO_OPENGL_GLX            
slouken@10806
   637
        && ( !_this->gl_data || X11_GL_UseEGL(_this) )
gabomdq@7808
   638
#endif  
gabomdq@7808
   639
    ) {
gabomdq@7960
   640
#if SDL_VIDEO_OPENGL_EGL  
gabomdq@7659
   641
        if (!_this->egl_data) {
gabomdq@7659
   642
            return -1;
gabomdq@7659
   643
        }
gabomdq@7659
   644
gabomdq@7659
   645
        /* Create the GLES window surface */
slouken@7683
   646
        windowdata->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) w);
gabomdq@7659
   647
slouken@7683
   648
        if (windowdata->egl_surface == EGL_NO_SURFACE) {
gabomdq@7659
   649
            return SDL_SetError("Could not create GLES window surface");
gabomdq@7659
   650
        }
gabomdq@7960
   651
#else
slouken@10806
   652
        return SDL_SetError("Could not create GLES window surface (EGL support not configured)");
gabomdq@7960
   653
#endif /* SDL_VIDEO_OPENGL_EGL */
gabomdq@7659
   654
    }
gabomdq@7659
   655
#endif
gabomdq@7659
   656
    
gabomdq@7659
   657
bob@2325
   658
#ifdef X_HAVE_UTF8_STRING
slouken@7683
   659
    if (SDL_X11_HAVE_UTF8 && windowdata->ic) {
icculus@7827
   660
        X11_XGetICValues(windowdata->ic, XNFilterEvents, &fevent, NULL);
kazeuser@2718
   661
    }
bob@2325
   662
#endif
bob@2325
   663
dimitris@6316
   664
    X11_Xinput2SelectTouch(_this, window);
dimitris@6316
   665
icculus@7827
   666
    X11_XSelectInput(display, w,
icculus@6045
   667
                 (FocusChangeMask | EnterWindowMask | LeaveWindowMask |
icculus@6045
   668
                 ExposureMask | ButtonPressMask | ButtonReleaseMask |
icculus@6045
   669
                 PointerMotionMask | KeyPressMask | KeyReleaseMask |
icculus@6045
   670
                 PropertyChangeMask | StructureNotifyMask |
icculus@6045
   671
                 KeymapStateMask | fevent));
icculus@6045
   672
icculus@7827
   673
    X11_XFlush(display);
slouken@5031
   674
slouken@1951
   675
    return 0;
slouken@1951
   676
}
slouken@1951
   677
slouken@1951
   678
int
bob@2324
   679
X11_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1951
   680
{
slouken@1951
   681
    Window w = (Window) data;
slouken@1951
   682
slouken@4518
   683
    window->title = X11_GetWindowTitle(_this, w);
slouken@1951
   684
slouken@1951
   685
    if (SetupWindowData(_this, window, w, SDL_FALSE) < 0) {
slouken@1951
   686
        return -1;
slouken@1951
   687
    }
slouken@1951
   688
    return 0;
slouken@1951
   689
}
slouken@1951
   690
slouken@4518
   691
char *
slouken@4518
   692
X11_GetWindowTitle(_THIS, Window xwindow)
slouken@4518
   693
{
slouken@4518
   694
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@4518
   695
    Display *display = data->display;
slouken@4518
   696
    int status, real_format;
slouken@4518
   697
    Atom real_type;
slouken@4518
   698
    unsigned long items_read, items_left;
slouken@4518
   699
    unsigned char *propdata;
slouken@4518
   700
    char *title = NULL;
slouken@4518
   701
icculus@7827
   702
    status = X11_XGetWindowProperty(display, xwindow, data->_NET_WM_NAME,
slouken@4518
   703
                0L, 8192L, False, data->UTF8_STRING, &real_type, &real_format,
slouken@4518
   704
                &items_read, &items_left, &propdata);
icculus@6023
   705
    if (status == Success && propdata) {
slouken@4518
   706
        title = SDL_strdup(SDL_static_cast(char*, propdata));
icculus@7827
   707
        X11_XFree(propdata);
slouken@4518
   708
    } else {
icculus@7827
   709
        status = X11_XGetWindowProperty(display, xwindow, XA_WM_NAME,
slouken@4518
   710
                    0L, 8192L, False, XA_STRING, &real_type, &real_format,
slouken@4518
   711
                    &items_read, &items_left, &propdata);
icculus@6023
   712
        if (status == Success && propdata) {
slouken@4518
   713
            title = SDL_iconv_string("UTF-8", "", SDL_static_cast(char*, propdata), items_read+1);
slouken@9117
   714
            X11_XFree(propdata);
slouken@4518
   715
        } else {
slouken@4518
   716
            title = SDL_strdup("");
slouken@4518
   717
        }
slouken@4518
   718
    }
slouken@4518
   719
    return title;
slouken@4518
   720
}
slouken@4518
   721
slouken@1951
   722
void
slouken@1951
   723
X11_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1951
   724
{
slouken@1951
   725
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   726
    Display *display = data->videodata->display;
icculus@9479
   727
    XTextProperty titleprop;
slouken@1951
   728
    Status status;
icculus@9474
   729
    const char *title = window->title ? window->title : "";
icculus@9474
   730
    char *title_locale = NULL;
slouken@1951
   731
slouken@1951
   732
#ifdef X_HAVE_UTF8_STRING
slouken@4518
   733
    Atom _NET_WM_NAME = data->videodata->_NET_WM_NAME;
slouken@1951
   734
#endif
slouken@1951
   735
icculus@9474
   736
    title_locale = SDL_iconv_utf8_locale(title);
icculus@9474
   737
    if (!title_locale) {
icculus@9474
   738
        SDL_OutOfMemory();
icculus@9474
   739
        return;
icculus@9474
   740
    }
icculus@9474
   741
icculus@9474
   742
    status = X11_XStringListToTextProperty(&title_locale, 1, &titleprop);
icculus@9474
   743
    SDL_free(title_locale);
icculus@9474
   744
    if (status) {
icculus@9474
   745
        X11_XSetTextProperty(display, data->xwindow, &titleprop, XA_WM_NAME);
icculus@9474
   746
        X11_XFree(titleprop.value);
icculus@9474
   747
    }
icculus@9474
   748
#ifdef X_HAVE_UTF8_STRING
icculus@9474
   749
    if (SDL_X11_HAVE_UTF8) {
icculus@9474
   750
        status = X11_Xutf8TextListToTextProperty(display, (char **) &title, 1,
icculus@9474
   751
                                            XUTF8StringStyle, &titleprop);
icculus@9474
   752
        if (status == Success) {
icculus@9474
   753
            X11_XSetTextProperty(display, data->xwindow, &titleprop,
icculus@9474
   754
                                 _NET_WM_NAME);
icculus@7827
   755
            X11_XFree(titleprop.value);
slouken@1951
   756
        }
icculus@9474
   757
    }
slouken@1951
   758
#endif
icculus@9474
   759
icculus@7827
   760
    X11_XFlush(display);
slouken@1951
   761
}
slouken@1951
   762
slouken@1951
   763
void
slouken@2967
   764
X11_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slouken@2967
   765
{
slouken@2967
   766
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@2967
   767
    Display *display = data->videodata->display;
slouken@4518
   768
    Atom _NET_WM_ICON = data->videodata->_NET_WM_ICON;
slouken@2967
   769
slouken@2967
   770
    if (icon) {
slouken@2967
   771
        int propsize;
slouken@4528
   772
        long *propdata;
slouken@2967
   773
slouken@2967
   774
        /* Set the _NET_WM_ICON property */
slouken@7523
   775
        SDL_assert(icon->format->format == SDL_PIXELFORMAT_ARGB8888);
slouken@2990
   776
        propsize = 2 + (icon->w * icon->h);
slouken@4566
   777
        propdata = SDL_malloc(propsize * sizeof(long));
slouken@2967
   778
        if (propdata) {
slouken@4528
   779
            int x, y;
slouken@4528
   780
            Uint32 *src;
slouken@4528
   781
            long *dst;
slouken@4528
   782
slouken@2967
   783
            propdata[0] = icon->w;
slouken@2967
   784
            propdata[1] = icon->h;
slouken@4528
   785
            dst = &propdata[2];
slouken@4528
   786
            for (y = 0; y < icon->h; ++y) {
slouken@7523
   787
                src = (Uint32*)((Uint8*)icon->pixels + y * icon->pitch);
slouken@4528
   788
                for (x = 0; x < icon->w; ++x) {
slouken@4528
   789
                    *dst++ = *src++;
slouken@4528
   790
                }
slouken@4528
   791
            }
icculus@7827
   792
            X11_XChangeProperty(display, data->xwindow, _NET_WM_ICON, XA_CARDINAL,
slouken@2990
   793
                            32, PropModeReplace, (unsigned char *) propdata,
slouken@2990
   794
                            propsize);
slouken@2967
   795
        }
icculus@6562
   796
        SDL_free(propdata);
slouken@2967
   797
    } else {
icculus@7827
   798
        X11_XDeleteProperty(display, data->xwindow, _NET_WM_ICON);
slouken@2967
   799
    }
icculus@7827
   800
    X11_XFlush(display);
slouken@2967
   801
}
slouken@2967
   802
slouken@2967
   803
void
slouken@1951
   804
X11_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1951
   805
{
slouken@1951
   806
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   807
    Display *display = data->videodata->display;
icculus@13191
   808
    unsigned int childCount;
icculus@13191
   809
    Window childReturn, root, parent;
icculus@13191
   810
    Window* children;
icculus@13191
   811
    XWindowAttributes attrs;
icculus@13519
   812
    int orig_x, orig_y;
icculus@13519
   813
    Uint32 timeout;
icculus@13519
   814
icculus@13519
   815
    X11_XSync(display, False);
icculus@13519
   816
    X11_XQueryTree(display, data->xwindow, &root, &parent, &children, &childCount);
icculus@13519
   817
    X11_XGetWindowAttributes(display, data->xwindow, &attrs);
icculus@13519
   818
    X11_XTranslateCoordinates(display, parent, DefaultRootWindow(display),
icculus@13519
   819
                              attrs.x, attrs.y, &orig_x, &orig_y, &childReturn);
slouken@1951
   820
icculus@13191
   821
    /*Attempt to move the window*/
icculus@10103
   822
    X11_XMoveWindow(display, data->xwindow, window->x - data->border_left, window->y - data->border_top);
icculus@13191
   823
icculus@13519
   824
    /* Wait a brief time to see if the window manager decided to let this move happen.
icculus@13519
   825
       If the window changes at all, even to an unexpected value, we break out. */
icculus@13519
   826
    timeout = SDL_GetTicks() + 100;
icculus@13534
   827
    while (SDL_TRUE) {
icculus@13519
   828
        int x, y;
icculus@13519
   829
        X11_XSync(display, False);
icculus@13519
   830
        X11_XGetWindowAttributes(display, data->xwindow, &attrs);
icculus@13519
   831
        X11_XTranslateCoordinates(display, parent, DefaultRootWindow(display),
icculus@13519
   832
                                  attrs.x, attrs.y, &x, &y, &childReturn);
icculus@13519
   833
icculus@13519
   834
        if ((x != orig_x) || (y != orig_y)) {
icculus@13519
   835
            window->x = x;
icculus@13519
   836
            window->y = y;
icculus@13519
   837
            break;  /* window moved, time to go. */
icculus@13533
   838
        } else if ((x == window->x) && (y == window->y)) {
icculus@13533
   839
            break;  /* we're at the place we wanted to be anyhow, drop out. */
icculus@13519
   840
        }
icculus@13519
   841
icculus@13534
   842
        if (SDL_TICKS_PASSED(SDL_GetTicks(), timeout)) {
icculus@13534
   843
            break;
icculus@13534
   844
        }
icculus@13534
   845
icculus@13519
   846
        SDL_Delay(10);
icculus@13534
   847
    }
slouken@1951
   848
}
slouken@1951
   849
slouken@1951
   850
void
slouken@7617
   851
X11_SetWindowMinimumSize(_THIS, SDL_Window * window)
slouken@7617
   852
{
slouken@7617
   853
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7617
   854
    Display *display = data->videodata->display;
slouken@7617
   855
slouken@7617
   856
    if (window->flags & SDL_WINDOW_RESIZABLE) {
icculus@7827
   857
         XSizeHints *sizehints = X11_XAllocSizeHints();
slouken@7617
   858
         long userhints;
slouken@7617
   859
icculus@7827
   860
         X11_XGetWMNormalHints(display, data->xwindow, sizehints, &userhints);
slouken@7617
   861
slouken@7617
   862
         sizehints->min_width = window->min_w;
slouken@7617
   863
         sizehints->min_height = window->min_h;
slouken@7617
   864
         sizehints->flags |= PMinSize;
slouken@7617
   865
icculus@7827
   866
         X11_XSetWMNormalHints(display, data->xwindow, sizehints);
slouken@7617
   867
icculus@7827
   868
         X11_XFree(sizehints);
slouken@7617
   869
slouken@7617
   870
        /* See comment in X11_SetWindowSize. */
icculus@7827
   871
        X11_XResizeWindow(display, data->xwindow, window->w, window->h);
icculus@10103
   872
        X11_XMoveWindow(display, data->xwindow, window->x - data->border_left, window->y - data->border_top);
icculus@7827
   873
        X11_XRaiseWindow(display, data->xwindow);
slouken@7617
   874
    }
slouken@7617
   875
icculus@7827
   876
    X11_XFlush(display);
slouken@7617
   877
}
slouken@7617
   878
slouken@7617
   879
void
slouken@7617
   880
X11_SetWindowMaximumSize(_THIS, SDL_Window * window)
slouken@7617
   881
{
slouken@7617
   882
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7617
   883
    Display *display = data->videodata->display;
slouken@7617
   884
slouken@7617
   885
    if (window->flags & SDL_WINDOW_RESIZABLE) {
icculus@7827
   886
         XSizeHints *sizehints = X11_XAllocSizeHints();
slouken@7617
   887
         long userhints;
slouken@7617
   888
icculus@7827
   889
         X11_XGetWMNormalHints(display, data->xwindow, sizehints, &userhints);
slouken@7617
   890
slouken@7617
   891
         sizehints->max_width = window->max_w;
slouken@7617
   892
         sizehints->max_height = window->max_h;
slouken@7617
   893
         sizehints->flags |= PMaxSize;
slouken@7617
   894
icculus@7827
   895
         X11_XSetWMNormalHints(display, data->xwindow, sizehints);
slouken@7617
   896
icculus@7827
   897
         X11_XFree(sizehints);
slouken@7617
   898
slouken@7617
   899
        /* See comment in X11_SetWindowSize. */
icculus@7827
   900
        X11_XResizeWindow(display, data->xwindow, window->w, window->h);
icculus@10103
   901
        X11_XMoveWindow(display, data->xwindow, window->x - data->border_left, window->y - data->border_top);
icculus@7827
   902
        X11_XRaiseWindow(display, data->xwindow);
slouken@7617
   903
    }
slouken@7617
   904
icculus@7827
   905
    X11_XFlush(display);
slouken@7617
   906
}
slouken@7617
   907
slouken@7617
   908
void
slouken@1951
   909
X11_SetWindowSize(_THIS, SDL_Window * window)
slouken@1951
   910
{
slouken@1951
   911
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   912
    Display *display = data->videodata->display;
slouken@1951
   913
slouken@6481
   914
    if (SDL_IsShapedWindow(window)) {
eligottlieb@4782
   915
        X11_ResizeWindowShape(window);
slouken@6481
   916
    }
slouken@5531
   917
    if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
icculus@7827
   918
         /* Apparently, if the X11 Window is set to a 'non-resizable' window, you cannot resize it using the X11_XResizeWindow, thus
gabomdq@7677
   919
            we must set the size hints to adjust the window size. */
icculus@7827
   920
         XSizeHints *sizehints = X11_XAllocSizeHints();
slouken@5531
   921
         long userhints;
slouken@5531
   922
icculus@7827
   923
         X11_XGetWMNormalHints(display, data->xwindow, sizehints, &userhints);
slouken@5531
   924
slouken@6343
   925
         sizehints->min_width = sizehints->max_width = window->w;
slouken@5531
   926
         sizehints->min_height = sizehints->max_height = window->h;
slouken@7617
   927
         sizehints->flags |= PMinSize | PMaxSize;
slouken@5531
   928
icculus@7827
   929
         X11_XSetWMNormalHints(display, data->xwindow, sizehints);
slouken@5531
   930
icculus@7827
   931
         X11_XFree(sizehints);
slouken@6541
   932
slouken@6541
   933
        /* From Pierre-Loup:
jorgen@6899
   934
           WMs each have their little quirks with that.  When you change the
jorgen@6899
   935
           size hints, they get a ConfigureNotify event with the
jorgen@6899
   936
           WM_NORMAL_SIZE_HINTS Atom.  They all save the hints then, but they
jorgen@6899
   937
           don't all resize the window right away to enforce the new hints.
slouken@6541
   938
jorgen@6899
   939
           Some of them resize only after:
jorgen@6899
   940
            - A user-initiated move or resize
jorgen@6899
   941
            - A code-initiated move or resize
jorgen@6899
   942
            - Hiding & showing window (Unmap & map)
slouken@6541
   943
jorgen@6899
   944
           The following move & resize seems to help a lot of WMs that didn't
jorgen@6899
   945
           properly update after the hints were changed. We don't do a
jorgen@6899
   946
           hide/show, because there are supposedly subtle problems with doing so
jorgen@6899
   947
           and transitioning from windowed to fullscreen in Unity.
jorgen@6899
   948
         */
icculus@7827
   949
        X11_XResizeWindow(display, data->xwindow, window->w, window->h);
icculus@10103
   950
        X11_XMoveWindow(display, data->xwindow, window->x - data->border_left, window->y - data->border_top);
icculus@7827
   951
        X11_XRaiseWindow(display, data->xwindow);
slouken@6481
   952
    } else {
icculus@7827
   953
        X11_XResizeWindow(display, data->xwindow, window->w, window->h);
slouken@6481
   954
    }
jorgen@6899
   955
icculus@7827
   956
    X11_XFlush(display);
slouken@1951
   957
}
slouken@1951
   958
icculus@10024
   959
int
icculus@10024
   960
X11_GetWindowBordersSize(_THIS, SDL_Window * window, int *top, int *left, int *bottom, int *right)
icculus@10024
   961
{
icculus@10024
   962
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
icculus@10024
   963
icculus@10102
   964
    *left = data->border_left;
icculus@10102
   965
    *right = data->border_right;
icculus@10102
   966
    *top = data->border_top;
icculus@10102
   967
    *bottom = data->border_bottom;
icculus@10024
   968
icculus@10102
   969
    return 0;
icculus@10024
   970
}
icculus@10024
   971
icculus@10025
   972
int
icculus@10025
   973
X11_SetWindowOpacity(_THIS, SDL_Window * window, float opacity)
icculus@10025
   974
{
icculus@10025
   975
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@10025
   976
    Display *display = data->videodata->display;
icculus@10025
   977
    Atom _NET_WM_WINDOW_OPACITY = data->videodata->_NET_WM_WINDOW_OPACITY;
icculus@10025
   978
icculus@10025
   979
    if (opacity == 1.0f) {
icculus@10025
   980
        X11_XDeleteProperty(display, data->xwindow, _NET_WM_WINDOW_OPACITY);
icculus@10025
   981
    } else  {
icculus@10025
   982
        const Uint32 FullyOpaque = 0xFFFFFFFF;
icculus@10025
   983
        const long alpha = (long) ((double)opacity * (double)FullyOpaque);
icculus@10025
   984
        X11_XChangeProperty(display, data->xwindow, _NET_WM_WINDOW_OPACITY, XA_CARDINAL, 32,
icculus@10025
   985
            PropModeReplace, (unsigned char *)&alpha, 1);
icculus@10025
   986
    }
icculus@10025
   987
icculus@10025
   988
    return 0;
icculus@10025
   989
}
icculus@10025
   990
icculus@10027
   991
int 
icculus@10027
   992
X11_SetWindowModalFor(_THIS, SDL_Window * modal_window, SDL_Window * parent_window) {
icculus@10027
   993
    SDL_WindowData *data = (SDL_WindowData *) modal_window->driverdata;
icculus@10027
   994
    SDL_WindowData *parent_data = (SDL_WindowData *) parent_window->driverdata;
icculus@10027
   995
    Display *display = data->videodata->display;
icculus@10027
   996
icculus@10027
   997
    X11_XSetTransientForHint(display, data->xwindow, parent_data->xwindow);
icculus@10027
   998
    return 0;
icculus@10027
   999
}
icculus@10027
  1000
icculus@10026
  1001
int
icculus@10026
  1002
X11_SetWindowInputFocus(_THIS, SDL_Window * window) 
icculus@10026
  1003
{
icculus@10026
  1004
    if (X11_IsWindowMapped(_this, window)) {
icculus@10026
  1005
        SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@10026
  1006
        Display *display = data->videodata->display;
icculus@10026
  1007
        X11_XSetInputFocus(display, data->xwindow, RevertToNone, CurrentTime);
icculus@10026
  1008
        X11_XFlush(display);
icculus@10026
  1009
        return 0;
icculus@10026
  1010
    }
icculus@10026
  1011
    return -1;
icculus@10026
  1012
}
icculus@10026
  1013
icculus@6422
  1014
void
icculus@6422
  1015
X11_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
icculus@6422
  1016
{
icculus@6423
  1017
    const SDL_bool focused = ((window->flags & SDL_WINDOW_INPUT_FOCUS) != 0);
icculus@6423
  1018
    const SDL_bool visible = ((window->flags & SDL_WINDOW_HIDDEN) == 0);
icculus@6422
  1019
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@6422
  1020
    SDL_DisplayData *displaydata =
icculus@6422
  1021
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
icculus@6422
  1022
    Display *display = data->videodata->display;
icculus@6422
  1023
    XEvent event;
icculus@6422
  1024
icculus@6422
  1025
    SetWindowBordered(display, displaydata->screen, data->xwindow, bordered);
icculus@7827
  1026
    X11_XFlush(display);
icculus@6422
  1027
icculus@6423
  1028
    if (visible) {
icculus@6423
  1029
        XWindowAttributes attr;
icculus@6423
  1030
        do {
icculus@7827
  1031
            X11_XSync(display, False);
icculus@7827
  1032
            X11_XGetWindowAttributes(display, data->xwindow, &attr);
icculus@6423
  1033
        } while (attr.map_state != IsViewable);
icculus@6423
  1034
icculus@6423
  1035
        if (focused) {
icculus@7827
  1036
            X11_XSetInputFocus(display, data->xwindow, RevertToParent, CurrentTime);
icculus@6423
  1037
        }
icculus@6423
  1038
    }
icculus@6423
  1039
icculus@6422
  1040
    /* make sure these don't make it to the real event queue if they fired here. */
icculus@7827
  1041
    X11_XSync(display, False);
icculus@7827
  1042
    X11_XCheckIfEvent(display, &event, &isUnmapNotify, (XPointer)&data->xwindow);
icculus@7827
  1043
    X11_XCheckIfEvent(display, &event, &isMapNotify, (XPointer)&data->xwindow);
icculus@6422
  1044
}
slouken@6336
  1045
slouken@1951
  1046
void
icculus@10385
  1047
X11_SetWindowResizable(_THIS, SDL_Window * window, SDL_bool resizable)
icculus@10385
  1048
{
icculus@10385
  1049
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@10385
  1050
    Display *display = data->videodata->display;
icculus@10385
  1051
icculus@10385
  1052
    XSizeHints *sizehints = X11_XAllocSizeHints();
icculus@10385
  1053
    long userhints;
icculus@10385
  1054
icculus@10385
  1055
    X11_XGetWMNormalHints(display, data->xwindow, sizehints, &userhints);
icculus@10385
  1056
icculus@10385
  1057
    if (resizable) {
icculus@10385
  1058
        /* FIXME: Is there a better way to get max window size from X? -flibit */
icculus@10385
  1059
        const int maxsize = 0x7FFFFFFF;
icculus@10385
  1060
        sizehints->min_width = window->min_w;
icculus@10385
  1061
        sizehints->min_height = window->min_h;
icculus@10385
  1062
        sizehints->max_width = (window->max_w == 0) ? maxsize : window->max_w;
icculus@10385
  1063
        sizehints->max_height = (window->max_h == 0) ? maxsize : window->max_h;
icculus@10385
  1064
    } else {
icculus@10385
  1065
        sizehints->min_width = window->w;
icculus@10385
  1066
        sizehints->min_height = window->h;
icculus@10385
  1067
        sizehints->max_width = window->w;
icculus@10385
  1068
        sizehints->max_height = window->h;
icculus@10385
  1069
    }
icculus@10385
  1070
    sizehints->flags |= PMinSize | PMaxSize;
icculus@10385
  1071
icculus@10385
  1072
    X11_XSetWMNormalHints(display, data->xwindow, sizehints);
icculus@10385
  1073
icculus@10385
  1074
    X11_XFree(sizehints);
icculus@10385
  1075
icculus@10385
  1076
    /* See comment in X11_SetWindowSize. */
icculus@10385
  1077
    X11_XResizeWindow(display, data->xwindow, window->w, window->h);
icculus@10385
  1078
    X11_XMoveWindow(display, data->xwindow, window->x - data->border_left, window->y - data->border_top);
icculus@10385
  1079
    X11_XRaiseWindow(display, data->xwindow);
icculus@10385
  1080
icculus@10385
  1081
    X11_XFlush(display);
icculus@10385
  1082
}
icculus@10385
  1083
icculus@10385
  1084
void
slouken@1951
  1085
X11_ShowWindow(_THIS, SDL_Window * window)
slouken@1951
  1086
{
slouken@1951
  1087
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
  1088
    Display *display = data->videodata->display;
slouken@6336
  1089
    XEvent event;
slouken@1951
  1090
slouken@6483
  1091
    if (!X11_IsWindowMapped(_this, window)) {
icculus@7827
  1092
        X11_XMapRaised(display, data->xwindow);
slouken@6483
  1093
        /* Blocking wait for "MapNotify" event.
icculus@7827
  1094
         * We use X11_XIfEvent because pXWindowEvent takes a mask rather than a type,
slouken@6483
  1095
         * and XCheckTypedWindowEvent doesn't block */
slouken@11288
  1096
        if(!(window->flags & SDL_WINDOW_FOREIGN))
slouken@11288
  1097
            X11_XIfEvent(display, &event, &isMapNotify, (XPointer)&data->xwindow);
icculus@7827
  1098
        X11_XFlush(display);
slouken@6483
  1099
    }
icculus@9683
  1100
slouken@9794
  1101
    if (!data->videodata->net_wm) {
icculus@9683
  1102
        /* no WM means no FocusIn event, which confuses us. Force it. */
icculus@9683
  1103
        X11_XSetInputFocus(display, data->xwindow, RevertToNone, CurrentTime);
icculus@9683
  1104
        X11_XFlush(display);
icculus@9683
  1105
    }
slouken@1951
  1106
}
slouken@1951
  1107
slouken@1951
  1108
void
slouken@1951
  1109
X11_HideWindow(_THIS, SDL_Window * window)
slouken@1951
  1110
{
slouken@1951
  1111
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7522
  1112
    SDL_DisplayData *displaydata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@1951
  1113
    Display *display = data->videodata->display;
slouken@6336
  1114
    XEvent event;
slouken@1951
  1115
slouken@6483
  1116
    if (X11_IsWindowMapped(_this, window)) {
icculus@7827
  1117
        X11_XWithdrawWindow(display, data->xwindow, displaydata->screen);
slouken@6483
  1118
        /* Blocking wait for "UnmapNotify" event */
slouken@11288
  1119
        if(!(window->flags & SDL_WINDOW_FOREIGN))
slouken@11288
  1120
            X11_XIfEvent(display, &event, &isUnmapNotify, (XPointer)&data->xwindow);
icculus@7827
  1121
        X11_XFlush(display);
slouken@6483
  1122
    }
slouken@1951
  1123
}
slouken@1951
  1124
slouken@7741
  1125
static void
slouken@7741
  1126
SetWindowActive(_THIS, SDL_Window * window)
slouken@7741
  1127
{
slouken@7741
  1128
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7741
  1129
    SDL_DisplayData *displaydata =
slouken@7741
  1130
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@7741
  1131
    Display *display = data->videodata->display;
slouken@7741
  1132
    Atom _NET_ACTIVE_WINDOW = data->videodata->_NET_ACTIVE_WINDOW;
slouken@7741
  1133
slouken@7741
  1134
    if (X11_IsWindowMapped(_this, window)) {
icculus@10007
  1135
        XEvent e;
icculus@10007
  1136
icculus@10006
  1137
        /*printf("SDL Window %p: sending _NET_ACTIVE_WINDOW with timestamp %lu\n", window, data->user_time);*/
slouken@7741
  1138
slouken@7741
  1139
        SDL_zero(e);
slouken@7741
  1140
        e.xany.type = ClientMessage;
slouken@7741
  1141
        e.xclient.message_type = _NET_ACTIVE_WINDOW;
slouken@7741
  1142
        e.xclient.format = 32;
slouken@7741
  1143
        e.xclient.window = data->xwindow;
slouken@7741
  1144
        e.xclient.data.l[0] = 1;  /* source indication. 1 = application */
icculus@10006
  1145
        e.xclient.data.l[1] = data->user_time;
slouken@7741
  1146
        e.xclient.data.l[2] = 0;
slouken@7741
  1147
icculus@7827
  1148
        X11_XSendEvent(display, RootWindow(display, displaydata->screen), 0,
slouken@7741
  1149
                   SubstructureNotifyMask | SubstructureRedirectMask, &e);
slouken@7741
  1150
icculus@7827
  1151
        X11_XFlush(display);
slouken@7741
  1152
    }
slouken@7741
  1153
}
slouken@7741
  1154
slouken@1951
  1155
void
slouken@1951
  1156
X11_RaiseWindow(_THIS, SDL_Window * window)
slouken@1951
  1157
{
slouken@1951
  1158
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
  1159
    Display *display = data->videodata->display;
slouken@1951
  1160
icculus@7827
  1161
    X11_XRaiseWindow(display, data->xwindow);
slouken@7741
  1162
    SetWindowActive(_this, window);
icculus@7827
  1163
    X11_XFlush(display);
slouken@1951
  1164
}
slouken@1951
  1165
slouken@3077
  1166
static void
slouken@5302
  1167
SetWindowMaximized(_THIS, SDL_Window * window, SDL_bool maximized)
slouken@3077
  1168
{
slouken@3077
  1169
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@3077
  1170
    SDL_DisplayData *displaydata =
slouken@5246
  1171
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@3077
  1172
    Display *display = data->videodata->display;
slouken@4518
  1173
    Atom _NET_WM_STATE = data->videodata->_NET_WM_STATE;
slouken@4518
  1174
    Atom _NET_WM_STATE_MAXIMIZED_VERT = data->videodata->_NET_WM_STATE_MAXIMIZED_VERT;
slouken@4518
  1175
    Atom _NET_WM_STATE_MAXIMIZED_HORZ = data->videodata->_NET_WM_STATE_MAXIMIZED_HORZ;
slouken@4522
  1176
icculus@7566
  1177
    if (maximized) {
icculus@7566
  1178
        window->flags |= SDL_WINDOW_MAXIMIZED;
icculus@7566
  1179
    } else {
icculus@7566
  1180
        window->flags &= ~SDL_WINDOW_MAXIMIZED;
icculus@7566
  1181
    }
icculus@7566
  1182
slouken@4522
  1183
    if (X11_IsWindowMapped(_this, window)) {
slouken@4522
  1184
        XEvent e;
slouken@4522
  1185
slouken@4524
  1186
        SDL_zero(e);
slouken@4522
  1187
        e.xany.type = ClientMessage;
slouken@4522
  1188
        e.xclient.message_type = _NET_WM_STATE;
slouken@4522
  1189
        e.xclient.format = 32;
slouken@4522
  1190
        e.xclient.window = data->xwindow;
slouken@4522
  1191
        e.xclient.data.l[0] =
slouken@4522
  1192
            maximized ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
slouken@4522
  1193
        e.xclient.data.l[1] = _NET_WM_STATE_MAXIMIZED_VERT;
slouken@4522
  1194
        e.xclient.data.l[2] = _NET_WM_STATE_MAXIMIZED_HORZ;
slouken@4522
  1195
        e.xclient.data.l[3] = 0l;
slouken@3077
  1196
icculus@7827
  1197
        X11_XSendEvent(display, RootWindow(display, displaydata->screen), 0,
slouken@4522
  1198
                   SubstructureNotifyMask | SubstructureRedirectMask, &e);
slouken@4522
  1199
    } else {
icculus@7566
  1200
        X11_SetNetWMState(_this, data->xwindow, window->flags);
slouken@4522
  1201
    }
icculus@7827
  1202
    X11_XFlush(display);
slouken@3077
  1203
}
slouken@3077
  1204
slouken@1951
  1205
void
slouken@1951
  1206
X11_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1951
  1207
{
slouken@5302
  1208
    SetWindowMaximized(_this, window, SDL_TRUE);
slouken@1951
  1209
}
slouken@1951
  1210
slouken@1951
  1211
void
slouken@1951
  1212
X11_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1951
  1213
{
slouken@4518
  1214
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@4518
  1215
    SDL_DisplayData *displaydata =
slouken@5246
  1216
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@4518
  1217
    Display *display = data->videodata->display;
slouken@7191
  1218
icculus@7827
  1219
    X11_XIconifyWindow(display, data->xwindow, displaydata->screen);
icculus@7827
  1220
    X11_XFlush(display);
slouken@1951
  1221
}
slouken@1951
  1222
slouken@1951
  1223
void
slouken@1951
  1224
X11_RestoreWindow(_THIS, SDL_Window * window)
slouken@1951
  1225
{
slouken@5302
  1226
    SetWindowMaximized(_this, window, SDL_FALSE);
slouken@7741
  1227
    X11_ShowWindow(_this, window);
slouken@7191
  1228
    SetWindowActive(_this, window);
slouken@1951
  1229
}
slouken@1951
  1230
icculus@10011
  1231
/* This asks the Window Manager to handle fullscreen for us. This is the modern way. */
icculus@6467
  1232
static void
icculus@6467
  1233
X11_SetWindowFullscreenViaWM(_THIS, SDL_Window * window, SDL_VideoDisplay * _display, SDL_bool fullscreen)
slouken@5302
  1234
{
slouken@5302
  1235
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5305
  1236
    SDL_DisplayData *displaydata = (SDL_DisplayData *) _display->driverdata;
slouken@5302
  1237
    Display *display = data->videodata->display;
slouken@5302
  1238
    Atom _NET_WM_STATE = data->videodata->_NET_WM_STATE;
slouken@5302
  1239
    Atom _NET_WM_STATE_FULLSCREEN = data->videodata->_NET_WM_STATE_FULLSCREEN;
slouken@5302
  1240
slouken@5302
  1241
    if (X11_IsWindowMapped(_this, window)) {
slouken@5302
  1242
        XEvent e;
slouken@5302
  1243
slouken@6539
  1244
        if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
slouken@6539
  1245
            /* Compiz refuses fullscreen toggle if we're not resizable, so update the hints so we
slouken@6539
  1246
               can be resized to the fullscreen resolution (or reset so we're not resizable again) */
icculus@7827
  1247
            XSizeHints *sizehints = X11_XAllocSizeHints();
slouken@6539
  1248
            long flags = 0;
icculus@7827
  1249
            X11_XGetWMNormalHints(display, data->xwindow, sizehints, &flags);
slouken@6539
  1250
            /* set the resize flags on */
slouken@6539
  1251
            if (fullscreen) {
slouken@6539
  1252
                /* we are going fullscreen so turn the flags off */
slouken@6539
  1253
                sizehints->flags &= ~(PMinSize | PMaxSize);
slouken@6539
  1254
            } else {
slouken@6539
  1255
                /* Reset the min/max width height to make the window non-resizable again */
icculus@6466
  1256
                sizehints->flags |= PMinSize | PMaxSize;
slouken@6540
  1257
                sizehints->min_width = sizehints->max_width = window->windowed.w;
slouken@6540
  1258
                sizehints->min_height = sizehints->max_height = window->windowed.h;
icculus@6466
  1259
            }
icculus@7827
  1260
            X11_XSetWMNormalHints(display, data->xwindow, sizehints);
icculus@7827
  1261
            X11_XFree(sizehints);
icculus@6466
  1262
        }
slouken@6539
  1263
slouken@5302
  1264
        SDL_zero(e);
slouken@5302
  1265
        e.xany.type = ClientMessage;
slouken@5302
  1266
        e.xclient.message_type = _NET_WM_STATE;
slouken@5302
  1267
        e.xclient.format = 32;
slouken@5302
  1268
        e.xclient.window = data->xwindow;
slouken@5302
  1269
        e.xclient.data.l[0] =
slouken@5302
  1270
            fullscreen ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
slouken@5302
  1271
        e.xclient.data.l[1] = _NET_WM_STATE_FULLSCREEN;
slouken@5302
  1272
        e.xclient.data.l[3] = 0l;
slouken@5302
  1273
icculus@7827
  1274
        X11_XSendEvent(display, RootWindow(display, displaydata->screen), 0,
slouken@5302
  1275
                   SubstructureNotifyMask | SubstructureRedirectMask, &e);
icculus@10084
  1276
icculus@10084
  1277
        /* Fullscreen windows sometimes end up being marked maximized by
icculus@10084
  1278
            window managers. Force it back to how we expect it to be. */
icculus@10084
  1279
        if (!fullscreen && ((window->flags & SDL_WINDOW_MAXIMIZED) == 0)) {
icculus@10084
  1280
            SDL_zero(e);
icculus@10084
  1281
            e.xany.type = ClientMessage;
icculus@10084
  1282
            e.xclient.message_type = _NET_WM_STATE;
icculus@10084
  1283
            e.xclient.format = 32;
icculus@10084
  1284
            e.xclient.window = data->xwindow;
icculus@10084
  1285
            e.xclient.data.l[0] = _NET_WM_STATE_REMOVE;
icculus@10084
  1286
            e.xclient.data.l[1] = data->videodata->_NET_WM_STATE_MAXIMIZED_VERT;
icculus@10084
  1287
            e.xclient.data.l[2] = data->videodata->_NET_WM_STATE_MAXIMIZED_HORZ;
icculus@10084
  1288
            e.xclient.data.l[3] = 0l;
icculus@10084
  1289
            X11_XSendEvent(display, RootWindow(display, displaydata->screen), 0,
icculus@10084
  1290
                   SubstructureNotifyMask | SubstructureRedirectMask, &e);
icculus@10084
  1291
        }
slouken@5302
  1292
    } else {
slouken@6481
  1293
        Uint32 flags;
slouken@5302
  1294
slouken@6481
  1295
        flags = window->flags;
slouken@5302
  1296
        if (fullscreen) {
slouken@6481
  1297
            flags |= SDL_WINDOW_FULLSCREEN;
slouken@6481
  1298
        } else {
slouken@6481
  1299
            flags &= ~SDL_WINDOW_FULLSCREEN;
slouken@5302
  1300
        }
slouken@6487
  1301
        X11_SetNetWMState(_this, data->xwindow, flags);
slouken@5302
  1302
    }
mikesart@6568
  1303
slouken@6575
  1304
    if (data->visual->class == DirectColor) {
slouken@6575
  1305
        if ( fullscreen ) {
icculus@7827
  1306
            X11_XInstallColormap(display, data->colormap);
slouken@6575
  1307
        } else {
icculus@7827
  1308
            X11_XUninstallColormap(display, data->colormap);
slouken@6575
  1309
        }
slouken@6575
  1310
    }
mikesart@6568
  1311
icculus@7827
  1312
    X11_XFlush(display);
slouken@5302
  1313
}
slouken@5302
  1314
icculus@6467
  1315
/* This handles fullscreen itself, outside the Window Manager. */
icculus@6467
  1316
static void
icculus@6467
  1317
X11_BeginWindowFullscreenLegacy(_THIS, SDL_Window * window, SDL_VideoDisplay * _display)
icculus@6467
  1318
{
icculus@6467
  1319
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@6467
  1320
    SDL_DisplayData *displaydata = (SDL_DisplayData *) _display->driverdata;
icculus@6467
  1321
    Visual *visual = data->visual;
icculus@6467
  1322
    Display *display = data->videodata->display;
icculus@6467
  1323
    const int screen = displaydata->screen;
icculus@6467
  1324
    Window root = RootWindow(display, screen);
icculus@6467
  1325
    const int def_vis = (visual == DefaultVisual(display, screen));
icculus@6467
  1326
    unsigned long xattrmask = 0;
icculus@6467
  1327
    XSetWindowAttributes xattr;
icculus@6467
  1328
    XEvent ev;
slouken@6475
  1329
    SDL_Rect rect;
icculus@6467
  1330
icculus@6467
  1331
    if ( data->fswindow ) {
icculus@6467
  1332
        return;  /* already fullscreen, I hope. */
icculus@6467
  1333
    }
icculus@6467
  1334
slouken@6475
  1335
    X11_GetDisplayBounds(_this, _display, &rect);
slouken@6475
  1336
icculus@6467
  1337
    SDL_zero(xattr);
icculus@6467
  1338
    xattr.override_redirect = True;
icculus@6467
  1339
    xattrmask |= CWOverrideRedirect;
icculus@6467
  1340
    xattr.background_pixel = def_vis ? BlackPixel(display, screen) : 0;
icculus@6467
  1341
    xattrmask |= CWBackPixel;
icculus@6467
  1342
    xattr.border_pixel = 0;
icculus@6467
  1343
    xattrmask |= CWBorderPixel;
icculus@6467
  1344
    xattr.colormap = data->colormap;
icculus@6467
  1345
    xattrmask |= CWColormap;
icculus@6467
  1346
icculus@7827
  1347
    data->fswindow = X11_XCreateWindow(display, root,
slouken@6475
  1348
                                   rect.x, rect.y, rect.w, rect.h, 0,
icculus@6467
  1349
                                   displaydata->depth, InputOutput,
icculus@6467
  1350
                                   visual, xattrmask, &xattr);
icculus@6467
  1351
icculus@7827
  1352
    X11_XSelectInput(display, data->fswindow, StructureNotifyMask);
icculus@7827
  1353
    X11_XSetWindowBackground(display, data->fswindow, 0);
icculus@7827
  1354
    X11_XInstallColormap(display, data->colormap);
icculus@7827
  1355
    X11_XClearWindow(display, data->fswindow);
icculus@7827
  1356
    X11_XMapRaised(display, data->fswindow);
icculus@6467
  1357
slouken@6480
  1358
    /* Make sure the fswindow is in view by warping mouse to the corner */
icculus@7827
  1359
    X11_XUngrabPointer(display, CurrentTime);
icculus@7827
  1360
    X11_XWarpPointer(display, None, root, 0, 0, 0, 0, rect.x, rect.y);
icculus@6467
  1361
icculus@6467
  1362
    /* Wait to be mapped, filter Unmap event out if it arrives. */
icculus@7827
  1363
    X11_XIfEvent(display, &ev, &isMapNotify, (XPointer)&data->fswindow);
icculus@7827
  1364
    X11_XCheckIfEvent(display, &ev, &isUnmapNotify, (XPointer)&data->fswindow);
icculus@6467
  1365
icculus@6467
  1366
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
icculus@6467
  1367
    if ( displaydata->use_vidmode ) {
icculus@7827
  1368
        X11_XF86VidModeLockModeSwitch(display, screen, True);
icculus@6467
  1369
    }
icculus@6467
  1370
#endif
icculus@6467
  1371
icculus@6467
  1372
    SetWindowBordered(display, displaydata->screen, data->xwindow, SDL_FALSE);
icculus@6467
  1373
icculus@6467
  1374
    /* Center actual window within our cover-the-screen window. */
icculus@7827
  1375
    X11_XReparentWindow(display, data->xwindow, data->fswindow,
slouken@6501
  1376
                    (rect.w - window->w) / 2, (rect.h - window->h) / 2);
icculus@6467
  1377
slouken@7632
  1378
    /* Move the mouse to the upper left to make sure it's on-screen */
icculus@7827
  1379
    X11_XWarpPointer(display, None, root, 0, 0, 0, 0, rect.x, rect.y);
slouken@7632
  1380
slouken@6501
  1381
    /* Center mouse in the fullscreen window. */
slouken@6501
  1382
    rect.x += (rect.w / 2);
slouken@6501
  1383
    rect.y += (rect.h / 2);
icculus@7827
  1384
    X11_XWarpPointer(display, None, root, 0, 0, 0, 0, rect.x, rect.y);
icculus@6467
  1385
icculus@6467
  1386
    /* Wait to be mapped, filter Unmap event out if it arrives. */
icculus@7827
  1387
    X11_XIfEvent(display, &ev, &isMapNotify, (XPointer)&data->xwindow);
icculus@7827
  1388
    X11_XCheckIfEvent(display, &ev, &isUnmapNotify, (XPointer)&data->xwindow);
icculus@6467
  1389
slouken@6662
  1390
    SDL_UpdateWindowGrab(window);
icculus@6467
  1391
}
icculus@6467
  1392
icculus@6467
  1393
static void
icculus@6467
  1394
X11_EndWindowFullscreenLegacy(_THIS, SDL_Window * window, SDL_VideoDisplay * _display)
icculus@6467
  1395
{
icculus@6467
  1396
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@6467
  1397
    SDL_DisplayData *displaydata = (SDL_DisplayData *) _display->driverdata;
icculus@6467
  1398
    Display *display = data->videodata->display;
icculus@6467
  1399
    const int screen = displaydata->screen;
icculus@6467
  1400
    Window root = RootWindow(display, screen);
slouken@6480
  1401
    Window fswindow = data->fswindow;
icculus@6467
  1402
    XEvent ev;
icculus@6467
  1403
slouken@6480
  1404
    if (!data->fswindow) {
icculus@6467
  1405
        return;  /* already not fullscreen, I hope. */
slouken@6480
  1406
    }
icculus@6467
  1407
slouken@6480
  1408
    data->fswindow = None;
icculus@6467
  1409
icculus@6467
  1410
#if SDL_VIDEO_DRIVER_X11_VIDMODE
icculus@6467
  1411
    if ( displaydata->use_vidmode ) {
icculus@7827
  1412
        X11_XF86VidModeLockModeSwitch(display, screen, False);
icculus@6467
  1413
    }
icculus@6467
  1414
#endif
icculus@6467
  1415
slouken@6662
  1416
    SDL_UpdateWindowGrab(window);
icculus@6467
  1417
icculus@7827
  1418
    X11_XReparentWindow(display, data->xwindow, root, window->x, window->y);
slouken@6480
  1419
slouken@6480
  1420
    /* flush these events so they don't confuse normal event handling */
icculus@7827
  1421
    X11_XSync(display, False);
icculus@7827
  1422
    X11_XCheckIfEvent(display, &ev, &isMapNotify, (XPointer)&data->xwindow);
icculus@7827
  1423
    X11_XCheckIfEvent(display, &ev, &isUnmapNotify, (XPointer)&data->xwindow);
icculus@6467
  1424
icculus@6467
  1425
    SetWindowBordered(display, screen, data->xwindow,
icculus@6467
  1426
                      (window->flags & SDL_WINDOW_BORDERLESS) == 0);
icculus@6467
  1427
icculus@7827
  1428
    X11_XWithdrawWindow(display, fswindow, screen);
slouken@6480
  1429
slouken@6480
  1430
    /* Wait to be unmapped. */
icculus@7827
  1431
    X11_XIfEvent(display, &ev, &isUnmapNotify, (XPointer)&fswindow);
icculus@7827
  1432
    X11_XDestroyWindow(display, fswindow);
icculus@6467
  1433
}
icculus@6467
  1434
icculus@6467
  1435
icculus@6467
  1436
void
icculus@6467
  1437
X11_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * _display, SDL_bool fullscreen)
icculus@6467
  1438
{
icculus@6467
  1439
    /* !!! FIXME: SDL_Hint? */
icculus@6467
  1440
    SDL_bool legacy = SDL_FALSE;
icculus@6467
  1441
    const char *env = SDL_getenv("SDL_VIDEO_X11_LEGACY_FULLSCREEN");
icculus@6467
  1442
    if (env) {
icculus@6467
  1443
        legacy = SDL_atoi(env);
icculus@6467
  1444
    } else {
slouken@6473
  1445
        SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
icculus@6467
  1446
        SDL_DisplayData *displaydata = (SDL_DisplayData *) _display->driverdata;
icculus@6467
  1447
        if ( displaydata->use_vidmode ) {
icculus@6467
  1448
            legacy = SDL_TRUE;  /* the new stuff only works with XRandR. */
slouken@6473
  1449
        } else if ( !videodata->net_wm ) {
slouken@6473
  1450
            legacy = SDL_TRUE;  /* The window manager doesn't support it */
icculus@6467
  1451
        } else {
icculus@6467
  1452
            /* !!! FIXME: look at the window manager name, and blacklist certain ones? */
icculus@6467
  1453
            /* http://stackoverflow.com/questions/758648/find-the-name-of-the-x-window-manager */
icculus@6467
  1454
            legacy = SDL_FALSE;  /* try the new way. */
icculus@6467
  1455
        }
icculus@6467
  1456
    }
icculus@6467
  1457
icculus@6467
  1458
    if (legacy) {
icculus@6467
  1459
        if (fullscreen) {
icculus@6467
  1460
            X11_BeginWindowFullscreenLegacy(_this, window, _display);
icculus@6467
  1461
        } else {
icculus@6467
  1462
            X11_EndWindowFullscreenLegacy(_this, window, _display);
icculus@6467
  1463
        }
icculus@6467
  1464
    } else {
icculus@6467
  1465
        X11_SetWindowFullscreenViaWM(_this, window, _display, fullscreen);
icculus@6467
  1466
    }
icculus@6467
  1467
}
icculus@6467
  1468
icculus@6467
  1469
slouken@5466
  1470
int
slouken@5466
  1471
X11_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
slouken@5466
  1472
{
slouken@5466
  1473
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5466
  1474
    Display *display = data->videodata->display;
slouken@5466
  1475
    Visual *visual = data->visual;
slouken@5466
  1476
    Colormap colormap = data->colormap;
slouken@5466
  1477
    XColor *colorcells;
slouken@5466
  1478
    int ncolors;
slouken@5466
  1479
    int rmask, gmask, bmask;
slouken@5466
  1480
    int rshift, gshift, bshift;
icculus@5981
  1481
    int i;
slouken@5466
  1482
slouken@5466
  1483
    if (visual->class != DirectColor) {
icculus@7037
  1484
        return SDL_SetError("Window doesn't have DirectColor visual");
slouken@5466
  1485
    }
slouken@5466
  1486
slouken@5466
  1487
    ncolors = visual->map_entries;
slouken@5466
  1488
    colorcells = SDL_malloc(ncolors * sizeof(XColor));
slouken@5466
  1489
    if (!colorcells) {
icculus@7037
  1490
        return SDL_OutOfMemory();
slouken@5466
  1491
    }
slouken@5466
  1492
slouken@5466
  1493
    rshift = 0;
slouken@5466
  1494
    rmask = visual->red_mask;
slouken@5466
  1495
    while (0 == (rmask & 1)) {
slouken@5466
  1496
        rshift++;
slouken@5466
  1497
        rmask >>= 1;
slouken@5466
  1498
    }
slouken@5466
  1499
slouken@5466
  1500
    gshift = 0;
slouken@5466
  1501
    gmask = visual->green_mask;
slouken@5466
  1502
    while (0 == (gmask & 1)) {
slouken@5466
  1503
        gshift++;
slouken@5466
  1504
        gmask >>= 1;
slouken@5466
  1505
    }
slouken@5466
  1506
slouken@5466
  1507
    bshift = 0;
slouken@5466
  1508
    bmask = visual->blue_mask;
slouken@5466
  1509
    while (0 == (bmask & 1)) {
slouken@5466
  1510
        bshift++;
slouken@5466
  1511
        bmask >>= 1;
slouken@5466
  1512
    }
slouken@5466
  1513
slouken@5466
  1514
    /* build the color table pixel values */
slouken@5466
  1515
    for (i = 0; i < ncolors; i++) {
slouken@5466
  1516
        Uint32 rbits = (rmask * i) / (ncolors - 1);
slouken@5466
  1517
        Uint32 gbits = (gmask * i) / (ncolors - 1);
slouken@5466
  1518
        Uint32 bbits = (bmask * i) / (ncolors - 1);
slouken@5466
  1519
        Uint32 pix = (rbits << rshift) | (gbits << gshift) | (bbits << bshift);
slouken@5466
  1520
slouken@5466
  1521
        colorcells[i].pixel = pix;
slouken@5466
  1522
slouken@5466
  1523
        colorcells[i].red = ramp[(0 * 256) + i];
slouken@5466
  1524
        colorcells[i].green = ramp[(1 * 256) + i];
slouken@5466
  1525
        colorcells[i].blue = ramp[(2 * 256) + i];
slouken@5466
  1526
slouken@5466
  1527
        colorcells[i].flags = DoRed | DoGreen | DoBlue;
slouken@5466
  1528
    }
slouken@5466
  1529
icculus@7827
  1530
    X11_XStoreColors(display, colormap, colorcells, ncolors);
icculus@7827
  1531
    X11_XFlush(display);
slouken@5466
  1532
    SDL_free(colorcells);
slouken@5466
  1533
slouken@5466
  1534
    return 0;
slouken@5466
  1535
}
slouken@5466
  1536
slouken@5302
  1537
void
slouken@6662
  1538
X11_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
slouken@1951
  1539
{
slouken@2875
  1540
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@2875
  1541
    Display *display = data->videodata->display;
slouken@4518
  1542
    SDL_bool oldstyle_fullscreen;
slouken@6662
  1543
    SDL_bool grab_keyboard;
slouken@2875
  1544
slouken@6662
  1545
    /* ICCCM2.0-compliant window managers can handle fullscreen windows
slouken@6662
  1546
       If we're using XVidMode to change resolution we need to confine
slouken@6662
  1547
       the cursor so we don't pan around the virtual desktop.
slouken@6662
  1548
     */
icculus@6467
  1549
    oldstyle_fullscreen = X11_IsWindowLegacyFullscreen(_this, window);
slouken@4518
  1550
slouken@6662
  1551
    if (oldstyle_fullscreen || grabbed) {
slouken@2875
  1552
        /* Try to grab the mouse */
icculus@10723
  1553
        if (!data->videodata->broken_pointer_grab) {
icculus@11334
  1554
            const unsigned int mask = ButtonPressMask | ButtonReleaseMask | PointerMotionMask | FocusChangeMask;
icculus@10723
  1555
            int attempts;
icculus@10723
  1556
            int result;
icculus@10723
  1557
drewb@10964
  1558
            /* Try for up to 5000ms (5s) to grab. If it still fails, stop trying. */
drewb@10964
  1559
            for (attempts = 0; attempts < 100; attempts++) {
icculus@11334
  1560
                result = X11_XGrabPointer(display, data->xwindow, True, mask, GrabModeAsync,
icculus@10723
  1561
                                 GrabModeAsync, data->xwindow, None, CurrentTime);
icculus@10723
  1562
                if (result == GrabSuccess) {
icculus@10723
  1563
                    break;
icculus@10723
  1564
                }
icculus@10723
  1565
                SDL_Delay(50);
slouken@2875
  1566
            }
icculus@10723
  1567
icculus@10723
  1568
            if (result != GrabSuccess) {
icculus@10723
  1569
                SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO, "The X server refused to let us grab the mouse. You might experience input bugs.");
icculus@10723
  1570
                data->videodata->broken_pointer_grab = SDL_TRUE;  /* don't try again. */
icculus@10723
  1571
            }
slouken@2875
  1572
        }
slouken@2875
  1573
slouken@2875
  1574
        /* Raise the window if we grab the mouse */
icculus@7827
  1575
        X11_XRaiseWindow(display, data->xwindow);
slouken@2875
  1576
slouken@2875
  1577
        /* Now grab the keyboard */
slouken@10499
  1578
        if (SDL_GetHintBoolean(SDL_HINT_GRAB_KEYBOARD, SDL_FALSE)) {
slouken@6662
  1579
            grab_keyboard = SDL_TRUE;
slouken@6662
  1580
        } else {
slouken@6662
  1581
            /* We need to do this with the old style override_redirect
slouken@6662
  1582
               fullscreen window otherwise we won't get keyboard focus.
slouken@6662
  1583
            */
slouken@6662
  1584
            grab_keyboard = oldstyle_fullscreen;
slouken@6662
  1585
        }
slouken@6662
  1586
        if (grab_keyboard) {
icculus@7827
  1587
            X11_XGrabKeyboard(display, data->xwindow, True, GrabModeAsync,
slouken@6662
  1588
                          GrabModeAsync, CurrentTime);
slouken@6662
  1589
        }
slouken@2875
  1590
    } else {
icculus@7827
  1591
        X11_XUngrabPointer(display, CurrentTime);
icculus@7827
  1592
        X11_XUngrabKeyboard(display, CurrentTime);
slouken@2875
  1593
    }
icculus@7827
  1594
    X11_XSync(display, False);
slouken@1951
  1595
}
slouken@1951
  1596
slouken@1951
  1597
void
slouken@1951
  1598
X11_DestroyWindow(_THIS, SDL_Window * window)
slouken@1951
  1599
{
slouken@1951
  1600
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@8905
  1601
slouken@1951
  1602
    if (data) {
bob@2323
  1603
        SDL_VideoData *videodata = (SDL_VideoData *) data->videodata;
bob@2323
  1604
        Display *display = videodata->display;
bob@2323
  1605
        int numwindows = videodata->numwindows;
bob@2323
  1606
        SDL_WindowData **windowlist = videodata->windowlist;
bob@2323
  1607
        int i;
bob@2323
  1608
bob@2323
  1609
        if (windowlist) {
bob@2324
  1610
            for (i = 0; i < numwindows; ++i) {
slouken@3685
  1611
                if (windowlist[i] && (windowlist[i]->window == window)) {
bob@2324
  1612
                    windowlist[i] = windowlist[numwindows - 1];
bob@2324
  1613
                    windowlist[numwindows - 1] = NULL;
bob@2324
  1614
                    videodata->numwindows--;
bob@2324
  1615
                    break;
bob@2324
  1616
                }
bob@2323
  1617
            }
bob@2323
  1618
        }
slouken@1951
  1619
#ifdef X_HAVE_UTF8_STRING
slouken@1951
  1620
        if (data->ic) {
icculus@7827
  1621
            X11_XDestroyIC(data->ic);
slouken@1951
  1622
        }
slouken@1951
  1623
#endif
slouken@1951
  1624
        if (data->created) {
icculus@7827
  1625
            X11_XDestroyWindow(display, data->xwindow);
icculus@7827
  1626
            X11_XFlush(display);
slouken@1951
  1627
        }
slouken@1951
  1628
        SDL_free(data);
slouken@1951
  1629
    }
slouken@8978
  1630
    window->driverdata = NULL;
slouken@1951
  1631
}
slouken@1951
  1632
slouken@1951
  1633
SDL_bool
slouken@1951
  1634
X11_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1951
  1635
{
slouken@4510
  1636
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@4510
  1637
    Display *display = data->videodata->display;
slouken@4510
  1638
slouken@4510
  1639
    if (info->version.major == SDL_MAJOR_VERSION &&
slouken@4510
  1640
        info->version.minor == SDL_MINOR_VERSION) {
slouken@4510
  1641
        info->subsystem = SDL_SYSWM_X11;
slouken@5056
  1642
        info->info.x11.display = display;
slouken@5056
  1643
        info->info.x11.window = data->xwindow;
slouken@1951
  1644
        return SDL_TRUE;
slouken@1951
  1645
    } else {
philipp@10945
  1646
        SDL_SetError("Application not compiled with SDL %d.%d",
slouken@1951
  1647
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1951
  1648
        return SDL_FALSE;
slouken@1951
  1649
    }
slouken@1951
  1650
}
slouken@1951
  1651
rush@8932
  1652
int
icculus@8935
  1653
X11_SetWindowHitTest(SDL_Window *window, SDL_bool enabled)
rush@8932
  1654
{
icculus@8935
  1655
    return 0;  /* just succeed, the real work is done elsewhere. */
rush@8932
  1656
}
rush@8932
  1657
icculus@12070
  1658
void
icculus@12070
  1659
X11_AcceptDragAndDrop(SDL_Window * window, SDL_bool accept)
icculus@12070
  1660
{
icculus@12070
  1661
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@12070
  1662
    Display *display = data->videodata->display;
icculus@12070
  1663
    Atom XdndAware = X11_XInternAtom(display, "XdndAware", False);
icculus@12070
  1664
icculus@12070
  1665
    if (accept) {
icculus@12070
  1666
        Atom xdnd_version = 5;
icculus@12070
  1667
        X11_XChangeProperty(display, data->xwindow, XdndAware, XA_ATOM, 32,
icculus@12070
  1668
                     PropModeReplace, (unsigned char*)&xdnd_version, 1);
icculus@12070
  1669
    } else {
icculus@12070
  1670
        X11_XDeleteProperty(display, data->xwindow, XdndAware);
icculus@12070
  1671
    }
icculus@12070
  1672
}
icculus@12070
  1673
slouken@5481
  1674
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
  1675
slouken@1951
  1676
/* vi: set ts=4 sw=4 expandtab: */