src/video/x11/SDL_x11window.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 09 Aug 2013 23:13:52 -0700
changeset 7617 918703f5cff8
parent 7566 b14d00c30095
child 7632 d6a8fa507a45
permissions -rw-r--r--
Fixed bug 2007 - SDL_SetWindowMinimumSize() not implemented on X11 (thanks Rainer!)
slouken@1951
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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
*/
slouken@1951
    21
#include "SDL_config.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
slouken@6188
    37
#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
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@6467
    43
#include "SDL_assert.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
#define _NET_WM_STATE_TOGGLE    2l
slouken@3077
    48
icculus@6467
    49
static Bool isMapNotify(Display *dpy, XEvent *ev, XPointer win)
icculus@6467
    50
{
icculus@6467
    51
    return ev->type == MapNotify && ev->xmap.window == *((Window*)win);
icculus@6467
    52
}
icculus@6467
    53
static Bool isUnmapNotify(Display *dpy, XEvent *ev, XPointer win)
icculus@6467
    54
{
icculus@6467
    55
    return ev->type == UnmapNotify && ev->xunmap.window == *((Window*)win);
icculus@6467
    56
}
icculus@6467
    57
static Bool isConfigureNotify(Display *dpy, XEvent *ev, XPointer win)
icculus@6467
    58
{
slouken@6480
    59
    return ev->type == ConfigureNotify && ev->xconfigure.window == *((Window*)win);
slouken@6480
    60
}
icculus@6467
    61
slouken@4518
    62
static SDL_bool
icculus@6467
    63
X11_IsWindowLegacyFullscreen(_THIS, SDL_Window * window)
slouken@4518
    64
{
icculus@6467
    65
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@6467
    66
    return (data->fswindow != 0);
slouken@4518
    67
}
slouken@4518
    68
slouken@4522
    69
static SDL_bool
slouken@4522
    70
X11_IsWindowMapped(_THIS, SDL_Window * window)
slouken@4522
    71
{
slouken@4522
    72
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@4522
    73
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@4522
    74
    XWindowAttributes attr;
slouken@4522
    75
slouken@4522
    76
    XGetWindowAttributes(videodata->display, data->xwindow, &attr);
slouken@4522
    77
    if (attr.map_state != IsUnmapped) {
slouken@4522
    78
        return SDL_TRUE;
slouken@4522
    79
    } else {
slouken@4522
    80
        return SDL_FALSE;
slouken@4522
    81
    }
slouken@4522
    82
}
slouken@4522
    83
slouken@6540
    84
#if 0
slouken@6481
    85
static SDL_bool
slouken@6481
    86
X11_IsActionAllowed(SDL_Window *window, Atom action)
slouken@4522
    87
{
slouken@6481
    88
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@6481
    89
    Atom _NET_WM_ALLOWED_ACTIONS = data->videodata->_NET_WM_ALLOWED_ACTIONS;
slouken@6481
    90
    Atom type;
slouken@6481
    91
    Display *display = data->videodata->display;
slouken@6481
    92
    int form;
slouken@6481
    93
    unsigned long remain;
slouken@6481
    94
    unsigned long len, i;
slouken@6481
    95
    Atom *list;
slouken@6481
    96
    SDL_bool ret = SDL_FALSE;
slouken@6481
    97
slouken@6481
    98
    if (XGetWindowProperty(display, data->xwindow, _NET_WM_ALLOWED_ACTIONS, 0, 1024, False, XA_ATOM, &type, &form, &len, &remain, (unsigned char **)&list) == Success)
slouken@6481
    99
    {
slouken@6481
   100
        for (i=0; i<len; ++i)
slouken@6481
   101
        {
slouken@6481
   102
            if (list[i] == action) {
slouken@6481
   103
                ret = SDL_TRUE;
slouken@6481
   104
                break;
slouken@6481
   105
            }
slouken@6481
   106
        }
slouken@6481
   107
        XFree(list);
slouken@6481
   108
    }
slouken@6481
   109
    return ret;
slouken@6481
   110
}
slouken@6540
   111
#endif /* 0 */
slouken@6481
   112
slouken@6487
   113
void
slouken@6487
   114
X11_SetNetWMState(_THIS, Window xwindow, Uint32 flags)
slouken@6481
   115
{
slouken@6481
   116
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@6487
   117
    Display *display = videodata->display;
slouken@6487
   118
    Atom _NET_WM_STATE = videodata->_NET_WM_STATE;
slouken@6486
   119
    /*Atom _NET_WM_STATE_HIDDEN = videodata->_NET_WM_STATE_HIDDEN;*/
slouken@6481
   120
    Atom _NET_WM_STATE_FOCUSED = videodata->_NET_WM_STATE_FOCUSED;
slouken@6481
   121
    Atom _NET_WM_STATE_MAXIMIZED_VERT = videodata->_NET_WM_STATE_MAXIMIZED_VERT;
slouken@6481
   122
    Atom _NET_WM_STATE_MAXIMIZED_HORZ = videodata->_NET_WM_STATE_MAXIMIZED_HORZ;
slouken@6481
   123
    Atom _NET_WM_STATE_FULLSCREEN = videodata->_NET_WM_STATE_FULLSCREEN;
slouken@6487
   124
    Atom atoms[5];
slouken@4522
   125
    int count = 0;
slouken@4522
   126
slouken@6486
   127
    /* The window manager sets this property, we shouldn't set it.
slouken@6486
   128
       If we did, this would indicate to the window manager that we don't
slouken@6486
   129
       actually want to be mapped during XMapRaised(), which would be bad.
slouken@6486
   130
     *
slouken@6481
   131
    if (flags & SDL_WINDOW_HIDDEN) {
slouken@6481
   132
        atoms[count++] = _NET_WM_STATE_HIDDEN;
slouken@4522
   133
    }
slouken@6486
   134
    */
slouken@6481
   135
    if (flags & SDL_WINDOW_INPUT_FOCUS) {
slouken@6481
   136
        atoms[count++] = _NET_WM_STATE_FOCUSED;
slouken@6481
   137
    }
slouken@6481
   138
    if (flags & SDL_WINDOW_MAXIMIZED) {
slouken@6481
   139
        atoms[count++] = _NET_WM_STATE_MAXIMIZED_VERT;
slouken@6481
   140
        atoms[count++] = _NET_WM_STATE_MAXIMIZED_HORZ;
slouken@6481
   141
    }
slouken@6481
   142
    if (flags & SDL_WINDOW_FULLSCREEN) {
slouken@6481
   143
        atoms[count++] = _NET_WM_STATE_FULLSCREEN;
slouken@4522
   144
    }
slouken@6487
   145
    if (count > 0) {
slouken@6487
   146
        XChangeProperty(display, xwindow, _NET_WM_STATE, XA_ATOM, 32,
slouken@6487
   147
                        PropModeReplace, (unsigned char *)atoms, count);
slouken@6487
   148
    } else {
slouken@6487
   149
        XDeleteProperty(display, xwindow, _NET_WM_STATE);
slouken@6487
   150
    }
slouken@4522
   151
}
slouken@4522
   152
slouken@6481
   153
Uint32
slouken@6487
   154
X11_GetNetWMState(_THIS, Window xwindow)
slouken@6481
   155
{
slouken@6481
   156
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@6487
   157
    Display *display = videodata->display;
slouken@6481
   158
    Atom _NET_WM_STATE = videodata->_NET_WM_STATE;
slouken@6481
   159
    Atom _NET_WM_STATE_HIDDEN = videodata->_NET_WM_STATE_HIDDEN;
slouken@6481
   160
    Atom _NET_WM_STATE_FOCUSED = videodata->_NET_WM_STATE_FOCUSED;
slouken@6481
   161
    Atom _NET_WM_STATE_MAXIMIZED_VERT = videodata->_NET_WM_STATE_MAXIMIZED_VERT;
slouken@6481
   162
    Atom _NET_WM_STATE_MAXIMIZED_HORZ = videodata->_NET_WM_STATE_MAXIMIZED_HORZ;
slouken@6481
   163
    Atom _NET_WM_STATE_FULLSCREEN = videodata->_NET_WM_STATE_FULLSCREEN;
slouken@6481
   164
    Atom actualType;
slouken@6481
   165
    int actualFormat;
slouken@6481
   166
    unsigned long i, numItems, bytesAfter;
slouken@6481
   167
    unsigned char *propertyValue = NULL;
slouken@6481
   168
    long maxLength = 1024;
slouken@6481
   169
    Uint32 flags = 0;
slouken@6481
   170
slouken@6487
   171
    if (XGetWindowProperty(display, xwindow, _NET_WM_STATE,
slouken@6481
   172
                           0l, maxLength, False, XA_ATOM, &actualType,
slouken@6481
   173
                           &actualFormat, &numItems, &bytesAfter,
slouken@6481
   174
                           &propertyValue) == Success) {
slouken@6481
   175
        Atom *atoms = (Atom *) propertyValue;
slouken@6481
   176
        int maximized = 0;
slouken@6481
   177
        int fullscreen = 0;
slouken@6481
   178
slouken@6481
   179
        for (i = 0; i < numItems; ++i) {
slouken@6481
   180
            if (atoms[i] == _NET_WM_STATE_HIDDEN) {
slouken@6486
   181
                flags |= SDL_WINDOW_HIDDEN;
slouken@6481
   182
            } else if (atoms[i] == _NET_WM_STATE_FOCUSED) {
slouken@6481
   183
                flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@6481
   184
            } else if (atoms[i] == _NET_WM_STATE_MAXIMIZED_VERT) {
slouken@6481
   185
                maximized |= 1;
slouken@6481
   186
            } else if (atoms[i] == _NET_WM_STATE_MAXIMIZED_HORZ) {
slouken@6481
   187
                maximized |= 2;
slouken@6481
   188
            } else if ( atoms[i] == _NET_WM_STATE_FULLSCREEN) {
slouken@6481
   189
                fullscreen = 1;
slouken@6481
   190
            }
slouken@6481
   191
        }
slouken@6481
   192
        if (maximized == 3) {
slouken@6481
   193
            flags |= SDL_WINDOW_MAXIMIZED;
slouken@6481
   194
        }  else if (fullscreen == 1) {
slouken@6481
   195
            flags |= SDL_WINDOW_FULLSCREEN;
slouken@6481
   196
        }
slouken@6481
   197
        XFree(propertyValue);
slouken@6481
   198
    }
slouken@6481
   199
slouken@6484
   200
    /* FIXME, check the size hints for resizable */
slouken@6484
   201
    /*flags |= SDL_WINDOW_RESIZABLE;*/
slouken@6481
   202
slouken@6481
   203
    return flags;
slouken@6481
   204
}
slouken@6481
   205
slouken@1951
   206
static int
slouken@1951
   207
SetupWindowData(_THIS, SDL_Window * window, Window w, BOOL created)
slouken@1951
   208
{
slouken@1951
   209
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1951
   210
    SDL_WindowData *data;
slouken@1951
   211
    int numwindows = videodata->numwindows;
bob@2324
   212
    int windowlistlength = videodata->windowlistlength;
slouken@1951
   213
    SDL_WindowData **windowlist = videodata->windowlist;
slouken@1951
   214
slouken@1951
   215
    /* Allocate the window data */
bob@2323
   216
    data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
slouken@1951
   217
    if (!data) {
icculus@7037
   218
        return SDL_OutOfMemory();
slouken@1951
   219
    }
slouken@3685
   220
    data->window = window;
slouken@3685
   221
    data->xwindow = w;
slouken@1951
   222
#ifdef X_HAVE_UTF8_STRING
slouken@7007
   223
    if (SDL_X11_HAVE_UTF8 && videodata->im) {
slouken@1951
   224
        data->ic =
slouken@1951
   225
            pXCreateIC(videodata->im, XNClientWindow, w, XNFocusWindow, w,
slouken@1951
   226
                       XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
slouken@1951
   227
                       XNResourceName, videodata->classname, XNResourceClass,
slouken@1951
   228
                       videodata->classname, NULL);
slouken@1951
   229
    }
slouken@1951
   230
#endif
slouken@1951
   231
    data->created = created;
slouken@1951
   232
    data->videodata = videodata;
slouken@1951
   233
bob@2323
   234
    /* Associate the data with the window */
bob@2324
   235
bob@2324
   236
    if (numwindows < windowlistlength) {
bob@2324
   237
        windowlist[numwindows] = data;
bob@2324
   238
        videodata->numwindows++;
bob@2324
   239
    } else {
bob@2324
   240
        windowlist =
bob@2324
   241
            (SDL_WindowData **) SDL_realloc(windowlist,
bob@2324
   242
                                            (numwindows +
bob@2324
   243
                                             1) * sizeof(*windowlist));
bob@2324
   244
        if (!windowlist) {
bob@2324
   245
            SDL_free(data);
icculus@7037
   246
            return SDL_OutOfMemory();
bob@2323
   247
        }
bob@2324
   248
        windowlist[numwindows] = data;
bob@2324
   249
        videodata->numwindows++;
bob@2324
   250
        videodata->windowlistlength++;
bob@2324
   251
        videodata->windowlist = windowlist;
bob@2323
   252
    }
bob@2323
   253
slouken@1951
   254
    /* Fill in the SDL window with the window data */
slouken@1951
   255
    {
slouken@1951
   256
        XWindowAttributes attrib;
slouken@1951
   257
slouken@1951
   258
        XGetWindowAttributes(data->videodata->display, w, &attrib);
slouken@1951
   259
        window->x = attrib.x;
slouken@1951
   260
        window->y = attrib.y;
slouken@1951
   261
        window->w = attrib.width;
slouken@1951
   262
        window->h = attrib.height;
slouken@1951
   263
        if (attrib.map_state != IsUnmapped) {
slouken@1951
   264
            window->flags |= SDL_WINDOW_SHOWN;
slouken@1951
   265
        } else {
slouken@1951
   266
            window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1951
   267
        }
slouken@5182
   268
        data->visual = attrib.visual;
slouken@5466
   269
        data->colormap = attrib.colormap;
slouken@1951
   270
    }
slouken@3077
   271
slouken@6487
   272
    window->flags |= X11_GetNetWMState(_this, w);
slouken@3077
   273
icculus@6024
   274
    {
icculus@6024
   275
        Window FocalWindow;
icculus@6024
   276
        int RevertTo=0;
icculus@6024
   277
        XGetInputFocus(data->videodata->display, &FocalWindow, &RevertTo);
icculus@6024
   278
        if (FocalWindow==w)
icculus@6024
   279
        {
icculus@6024
   280
            window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@6481
   281
        }
slouken@6481
   282
slouken@6481
   283
        if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
icculus@6024
   284
            SDL_SetKeyboardFocus(data->window);
icculus@6024
   285
        }
icculus@6024
   286
icculus@6024
   287
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
icculus@6024
   288
            /* Tell x11 to clip mouse */
icculus@6024
   289
        }
icculus@6024
   290
    }
icculus@6024
   291
slouken@1951
   292
    /* All done! */
slouken@6487
   293
    window->driverdata = data;
slouken@1951
   294
    return 0;
slouken@1951
   295
}
slouken@1951
   296
icculus@6422
   297
static void
icculus@6422
   298
SetWindowBordered(Display *display, int screen, Window window, SDL_bool border)
icculus@6422
   299
{
icculus@6422
   300
    /*
icculus@6422
   301
     * this code used to check for KWM_WIN_DECORATION, but KDE hasn't
icculus@6422
   302
     *  supported it for years and years. It now respects _MOTIF_WM_HINTS.
icculus@6422
   303
     *  Gnome is similar: just use the Motif atom.
icculus@6422
   304
     */
icculus@6422
   305
icculus@6422
   306
    Atom WM_HINTS = XInternAtom(display, "_MOTIF_WM_HINTS", True);
icculus@6422
   307
    if (WM_HINTS != None) {
icculus@6422
   308
        /* Hints used by Motif compliant window managers */
icculus@6422
   309
        struct
icculus@6422
   310
        {
icculus@6422
   311
            unsigned long flags;
icculus@6422
   312
            unsigned long functions;
icculus@6422
   313
            unsigned long decorations;
icculus@6422
   314
            long input_mode;
icculus@6422
   315
            unsigned long status;
icculus@6422
   316
        } MWMHints = {
icculus@6422
   317
            (1L << 1), 0, border ? 1 : 0, 0, 0
icculus@6422
   318
        };
icculus@6422
   319
icculus@6422
   320
        XChangeProperty(display, window, WM_HINTS, WM_HINTS, 32,
icculus@6422
   321
                        PropModeReplace, (unsigned char *) &MWMHints,
icculus@6422
   322
                        sizeof(MWMHints) / 4);
icculus@6422
   323
    } else {  /* set the transient hints instead, if necessary */
icculus@6422
   324
        XSetTransientForHint(display, window, RootWindow(display, screen));
icculus@6422
   325
    }
icculus@6422
   326
}
icculus@6422
   327
slouken@1951
   328
int
slouken@1951
   329
X11_CreateWindow(_THIS, SDL_Window * window)
slouken@1951
   330
{
slouken@1951
   331
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   332
    SDL_DisplayData *displaydata =
slouken@5246
   333
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@4521
   334
    Display *display = data->display;
slouken@4521
   335
    int screen = displaydata->screen;
slouken@1951
   336
    Visual *visual;
slouken@1951
   337
    int depth;
slouken@1951
   338
    XSetWindowAttributes xattr;
slouken@1951
   339
    Window w;
urkle@6462
   340
    XSizeHints *sizehints;
urkle@6462
   341
    XWMHints *wmhints;
urkle@6462
   342
    XClassHint *classhints;
jstpierre@7490
   343
    const long _NET_WM_BYPASS_COMPOSITOR_HINT_ON = 1;
jstpierre@7490
   344
    Atom _NET_WM_BYPASS_COMPOSITOR;
slouken@4521
   345
    Atom _NET_WM_WINDOW_TYPE;
slouken@4521
   346
    Atom _NET_WM_WINDOW_TYPE_NORMAL;
mail@6167
   347
    Atom _NET_WM_PID;
slouken@7001
   348
    Atom XdndAware, xdnd_version = 5;
icculus@6045
   349
    Uint32 fevent = 0;
slouken@4518
   350
slouken@6369
   351
#if SDL_VIDEO_OPENGL_GLX || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
slouken@1951
   352
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@1952
   353
        XVisualInfo *vinfo;
slouken@1952
   354
slouken@7191
   355
#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
slouken@6370
   356
        if (_this->gl_config.use_egl == 1) {
slouken@6369
   357
            vinfo = X11_GLES_GetVisual(_this, display, screen);
slouken@6369
   358
        } else
slouken@6369
   359
#endif
slouken@6369
   360
        {
slouken@6370
   361
#if SDL_VIDEO_OPENGL_GLX
slouken@6369
   362
            vinfo = X11_GL_GetVisual(_this, display, screen);
slouken@6370
   363
#endif
slouken@1952
   364
        }
slouken@3161
   365
        if (!vinfo) {
slouken@3161
   366
            return -1;
slouken@3161
   367
        }
slouken@3161
   368
        visual = vinfo->visual;
slouken@3161
   369
        depth = vinfo->depth;
slouken@3161
   370
        XFree(vinfo);
slouken@3161
   371
    } else
slouken@3161
   372
#endif
slouken@1952
   373
    {
slouken@1951
   374
        visual = displaydata->visual;
slouken@1951
   375
        depth = displaydata->depth;
slouken@1951
   376
    }
slouken@1951
   377
slouken@5386
   378
    xattr.override_redirect = False;
slouken@1951
   379
    xattr.background_pixel = 0;
slouken@1951
   380
    xattr.border_pixel = 0;
slouken@5466
   381
slouken@5466
   382
    if (visual->class == DirectColor) {
slouken@5466
   383
        XColor *colorcells;
slouken@5466
   384
        int i;
slouken@5466
   385
        int ncolors;
slouken@5466
   386
        int rmax, gmax, bmax;
slouken@5466
   387
        int rmask, gmask, bmask;
slouken@5466
   388
        int rshift, gshift, bshift;
slouken@5466
   389
slouken@5466
   390
        xattr.colormap =
slouken@5466
   391
            XCreateColormap(display, RootWindow(display, screen),
slouken@5466
   392
                            visual, AllocAll);
slouken@5466
   393
slouken@5466
   394
        /* If we can't create a colormap, then we must die */
slouken@5466
   395
        if (!xattr.colormap) {
icculus@7037
   396
            return SDL_SetError("Could not create writable colormap");
slouken@5466
   397
        }
slouken@5466
   398
slouken@5466
   399
        /* OK, we got a colormap, now fill it in as best as we can */
slouken@5466
   400
        colorcells = SDL_malloc(visual->map_entries * sizeof(XColor));
slouken@5466
   401
        if (!colorcells) {
icculus@7037
   402
            return SDL_OutOfMemory();
slouken@5466
   403
        }
slouken@5466
   404
        ncolors = visual->map_entries;
slouken@5466
   405
        rmax = 0xffff;
slouken@5466
   406
        gmax = 0xffff;
slouken@5466
   407
        bmax = 0xffff;
slouken@5466
   408
slouken@5466
   409
        rshift = 0;
slouken@5466
   410
        rmask = visual->red_mask;
slouken@5466
   411
        while (0 == (rmask & 1)) {
slouken@5466
   412
            rshift++;
slouken@5466
   413
            rmask >>= 1;
slouken@5466
   414
        }
slouken@5466
   415
slouken@5466
   416
        gshift = 0;
slouken@5466
   417
        gmask = visual->green_mask;
slouken@5466
   418
        while (0 == (gmask & 1)) {
slouken@5466
   419
            gshift++;
slouken@5466
   420
            gmask >>= 1;
slouken@5466
   421
        }
slouken@5466
   422
slouken@5466
   423
        bshift = 0;
slouken@5466
   424
        bmask = visual->blue_mask;
slouken@5466
   425
        while (0 == (bmask & 1)) {
slouken@5466
   426
            bshift++;
slouken@5466
   427
            bmask >>= 1;
slouken@5466
   428
        }
slouken@5466
   429
slouken@5466
   430
        /* build the color table pixel values */
slouken@5466
   431
        for (i = 0; i < ncolors; i++) {
slouken@5466
   432
            Uint32 red = (rmax * i) / (ncolors - 1);
slouken@5466
   433
            Uint32 green = (gmax * i) / (ncolors - 1);
slouken@5466
   434
            Uint32 blue = (bmax * i) / (ncolors - 1);
slouken@5466
   435
slouken@5466
   436
            Uint32 rbits = (rmask * i) / (ncolors - 1);
slouken@5466
   437
            Uint32 gbits = (gmask * i) / (ncolors - 1);
slouken@5466
   438
            Uint32 bbits = (bmask * i) / (ncolors - 1);
slouken@5466
   439
slouken@5466
   440
            Uint32 pix =
slouken@5466
   441
                (rbits << rshift) | (gbits << gshift) | (bbits << bshift);
slouken@5466
   442
slouken@5466
   443
            colorcells[i].pixel = pix;
slouken@5466
   444
slouken@5466
   445
            colorcells[i].red = red;
slouken@5466
   446
            colorcells[i].green = green;
slouken@5466
   447
            colorcells[i].blue = blue;
slouken@5466
   448
slouken@5466
   449
            colorcells[i].flags = DoRed | DoGreen | DoBlue;
slouken@5466
   450
        }
slouken@5466
   451
slouken@5466
   452
        XStoreColors(display, xattr.colormap, colorcells, ncolors);
slouken@5466
   453
slouken@5466
   454
        SDL_free(colorcells);
slouken@5466
   455
    } else {
slouken@5466
   456
        xattr.colormap =
slouken@5466
   457
            XCreateColormap(display, RootWindow(display, screen),
slouken@5466
   458
                            visual, AllocNone);
slouken@5466
   459
    }
slouken@1951
   460
slouken@5386
   461
    w = XCreateWindow(display, RootWindow(display, screen),
slouken@5386
   462
                      window->x, window->y, window->w, window->h,
slouken@5386
   463
                      0, depth, InputOutput, visual,
slouken@1951
   464
                      (CWOverrideRedirect | CWBackPixel | CWBorderPixel |
slouken@1951
   465
                       CWColormap), &xattr);
slouken@1952
   466
    if (!w) {
icculus@7037
   467
        return SDL_SetError("Couldn't create window");
slouken@1952
   468
    }
slouken@6188
   469
#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
slouken@6373
   470
    if ((window->flags & SDL_WINDOW_OPENGL) && (_this->gl_config.use_egl == 1)) {
slouken@6373
   471
        if (!_this->gles_data) {
slouken@6373
   472
            XDestroyWindow(display, w);
slouken@6373
   473
            return -1;
slouken@6373
   474
        }
slouken@6373
   475
slouken@6188
   476
        /* Create the GLES window surface */
slouken@6188
   477
        _this->gles_data->egl_surface =
slouken@6188
   478
            _this->gles_data->eglCreateWindowSurface(_this->gles_data->
slouken@3161
   479
                                                 egl_display,
slouken@3161
   480
                                                 _this->gles_data->egl_config,
slouken@3161
   481
                                                 (NativeWindowType) w, NULL);
slouken@3161
   482
slouken@6188
   483
        if (_this->gles_data->egl_surface == EGL_NO_SURFACE) {
slouken@6373
   484
            XDestroyWindow(display, w);
icculus@7037
   485
            return SDL_SetError("Could not create GLES window surface");
slouken@6188
   486
        }
slouken@3161
   487
    }
slouken@3161
   488
#endif
slouken@1951
   489
icculus@6422
   490
    SetWindowBordered(display, screen, w,
icculus@6422
   491
                      (window->flags & SDL_WINDOW_BORDERLESS) == 0);
slouken@1951
   492
urkle@6462
   493
    sizehints = XAllocSizeHints();
mail@6167
   494
    /* Setup the normal size hints */
urkle@6462
   495
    sizehints->flags = 0;
mail@6167
   496
    if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
urkle@6462
   497
        sizehints->min_width = sizehints->max_width = window->w;
urkle@6462
   498
        sizehints->min_height = sizehints->max_height = window->h;
urkle@6462
   499
        sizehints->flags |= (PMaxSize | PMinSize);
slouken@1951
   500
    }
urkle@6462
   501
    sizehints->x = window->x;
urkle@6462
   502
    sizehints->y = window->y;
urkle@6462
   503
    sizehints->flags |= USPosition;
slouken@1951
   504
mail@6167
   505
    /* Setup the input hints so we get keyboard input */
urkle@6462
   506
    wmhints = XAllocWMHints();
urkle@6462
   507
    wmhints->input = True;
urkle@6462
   508
    wmhints->flags = InputHint;
mail@6167
   509
mail@6167
   510
    /* Setup the class hints so we can get an icon (AfterStep) */
urkle@6462
   511
    classhints = XAllocClassHint();
urkle@6462
   512
    classhints->res_name = data->classname;
urkle@6462
   513
    classhints->res_class = data->classname;
mail@6167
   514
mail@6167
   515
    /* Set the size, input and class hints, and define WM_CLIENT_MACHINE and WM_LOCALE_NAME */
urkle@6462
   516
    XSetWMProperties(display, w, NULL, NULL, NULL, 0, sizehints, wmhints, classhints);
mail@6167
   517
urkle@6462
   518
    XFree(sizehints);
urkle@6462
   519
    XFree(wmhints);
urkle@6462
   520
    XFree(classhints);
mail@6167
   521
    /* Set the PID related to the window for the given hostname, if possible */
mail@6167
   522
    if (data->pid > 0) {
mail@6167
   523
        _NET_WM_PID = XInternAtom(display, "_NET_WM_PID", False);
mail@6167
   524
        XChangeProperty(display, w, _NET_WM_PID, XA_CARDINAL, 32, PropModeReplace,
mail@6167
   525
                        (unsigned char *)&data->pid, 1);
slouken@1951
   526
    }
slouken@1951
   527
slouken@4522
   528
    /* Set the window manager state */
slouken@6487
   529
    X11_SetNetWMState(_this, w, window->flags);
slouken@4518
   530
slouken@4521
   531
    /* Let the window manager know we're a "normal" window */
slouken@4521
   532
    _NET_WM_WINDOW_TYPE = XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
slouken@4521
   533
    _NET_WM_WINDOW_TYPE_NORMAL = XInternAtom(display, "_NET_WM_WINDOW_TYPE_NORMAL", False);
slouken@4521
   534
    XChangeProperty(display, w, _NET_WM_WINDOW_TYPE, XA_ATOM, 32,
slouken@4521
   535
                    PropModeReplace,
slouken@4521
   536
                    (unsigned char *)&_NET_WM_WINDOW_TYPE_NORMAL, 1);
slouken@4521
   537
jstpierre@7490
   538
    _NET_WM_BYPASS_COMPOSITOR = XInternAtom(display, "_NET_WM_BYPASS_COMPOSITOR", False);
jstpierre@7490
   539
    XChangeProperty(display, w, _NET_WM_BYPASS_COMPOSITOR, XA_CARDINAL, 32,
jstpierre@7490
   540
                    PropModeReplace,
jstpierre@7490
   541
                    (unsigned char *)&_NET_WM_BYPASS_COMPOSITOR_HINT_ON, 1);
slouken@7191
   542
icculus@6638
   543
    {
icculus@6638
   544
        Atom protocols[] = {
icculus@6638
   545
            data->WM_DELETE_WINDOW, /* Allow window to be deleted by the WM */
icculus@6638
   546
            data->_NET_WM_PING, /* Respond so WM knows we're alive */
icculus@6638
   547
        };
icculus@6638
   548
        XSetWMProtocols(display, w, protocols, sizeof (protocols) / sizeof (protocols[0]));
icculus@6638
   549
    }
slouken@1951
   550
slouken@1951
   551
    if (SetupWindowData(_this, window, w, SDL_TRUE) < 0) {
slouken@4521
   552
        XDestroyWindow(display, w);
slouken@1951
   553
        return -1;
slouken@1951
   554
    }
slouken@5386
   555
bob@2325
   556
#ifdef X_HAVE_UTF8_STRING
icculus@6045
   557
    if (SDL_X11_HAVE_UTF8) {
bob@2325
   558
        pXGetICValues(((SDL_WindowData *) window->driverdata)->ic,
bob@2325
   559
                      XNFilterEvents, &fevent, NULL);
kazeuser@2718
   560
    }
bob@2325
   561
#endif
bob@2325
   562
dimitris@6316
   563
    X11_Xinput2SelectTouch(_this, window);
dimitris@6316
   564
icculus@6045
   565
    XSelectInput(display, w,
icculus@6045
   566
                 (FocusChangeMask | EnterWindowMask | LeaveWindowMask |
icculus@6045
   567
                 ExposureMask | ButtonPressMask | ButtonReleaseMask |
icculus@6045
   568
                 PointerMotionMask | KeyPressMask | KeyReleaseMask |
icculus@6045
   569
                 PropertyChangeMask | StructureNotifyMask |
icculus@6045
   570
                 KeymapStateMask | fevent));
icculus@6045
   571
slouken@7001
   572
    XdndAware = XInternAtom(display, "XdndAware", False);
slouken@7001
   573
    XChangeProperty(display, w, XdndAware, XA_ATOM, 32,
slouken@7001
   574
                 PropModeReplace,
slouken@7191
   575
                 (unsigned char*)&xdnd_version, 1);
slouken@7001
   576
slouken@5031
   577
    XFlush(display);
slouken@5031
   578
slouken@1951
   579
    return 0;
slouken@1951
   580
}
slouken@1951
   581
slouken@1951
   582
int
bob@2324
   583
X11_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1951
   584
{
slouken@1951
   585
    Window w = (Window) data;
slouken@1951
   586
slouken@4518
   587
    window->title = X11_GetWindowTitle(_this, w);
slouken@1951
   588
slouken@1951
   589
    if (SetupWindowData(_this, window, w, SDL_FALSE) < 0) {
slouken@1951
   590
        return -1;
slouken@1951
   591
    }
slouken@1951
   592
    return 0;
slouken@1951
   593
}
slouken@1951
   594
slouken@4518
   595
char *
slouken@4518
   596
X11_GetWindowTitle(_THIS, Window xwindow)
slouken@4518
   597
{
slouken@4518
   598
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@4518
   599
    Display *display = data->display;
slouken@4518
   600
    int status, real_format;
slouken@4518
   601
    Atom real_type;
slouken@4518
   602
    unsigned long items_read, items_left;
slouken@4518
   603
    unsigned char *propdata;
slouken@4518
   604
    char *title = NULL;
slouken@4518
   605
slouken@4518
   606
    status = XGetWindowProperty(display, xwindow, data->_NET_WM_NAME,
slouken@4518
   607
                0L, 8192L, False, data->UTF8_STRING, &real_type, &real_format,
slouken@4518
   608
                &items_read, &items_left, &propdata);
icculus@6023
   609
    if (status == Success && propdata) {
slouken@4518
   610
        title = SDL_strdup(SDL_static_cast(char*, propdata));
slouken@4518
   611
        XFree(propdata);
slouken@4518
   612
    } else {
slouken@4518
   613
        status = XGetWindowProperty(display, xwindow, XA_WM_NAME,
slouken@4518
   614
                    0L, 8192L, False, XA_STRING, &real_type, &real_format,
slouken@4518
   615
                    &items_read, &items_left, &propdata);
icculus@6023
   616
        if (status == Success && propdata) {
slouken@4518
   617
            title = SDL_iconv_string("UTF-8", "", SDL_static_cast(char*, propdata), items_read+1);
slouken@4518
   618
        } else {
slouken@4518
   619
            title = SDL_strdup("");
slouken@4518
   620
        }
slouken@4518
   621
    }
slouken@4518
   622
    return title;
slouken@4518
   623
}
slouken@4518
   624
slouken@1951
   625
void
slouken@1951
   626
X11_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1951
   627
{
slouken@1951
   628
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   629
    Display *display = data->videodata->display;
slouken@1951
   630
    XTextProperty titleprop, iconprop;
slouken@1951
   631
    Status status;
slouken@1951
   632
    const char *title = window->title;
slouken@1951
   633
    const char *icon = NULL;
slouken@1951
   634
slouken@1951
   635
#ifdef X_HAVE_UTF8_STRING
slouken@4518
   636
    Atom _NET_WM_NAME = data->videodata->_NET_WM_NAME;
slouken@4518
   637
    Atom _NET_WM_ICON_NAME = data->videodata->_NET_WM_ICON_NAME;
slouken@1951
   638
#endif
slouken@1951
   639
slouken@1951
   640
    if (title != NULL) {
slouken@2143
   641
        char *title_locale = SDL_iconv_utf8_locale(title);
slouken@2143
   642
        if (!title_locale) {
slouken@1951
   643
            SDL_OutOfMemory();
slouken@1951
   644
            return;
slouken@1951
   645
        }
slouken@2143
   646
        status = XStringListToTextProperty(&title_locale, 1, &titleprop);
slouken@2143
   647
        SDL_free(title_locale);
slouken@1951
   648
        if (status) {
slouken@3685
   649
            XSetTextProperty(display, data->xwindow, &titleprop, XA_WM_NAME);
slouken@1951
   650
            XFree(titleprop.value);
slouken@1951
   651
        }
slouken@1951
   652
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   653
        if (SDL_X11_HAVE_UTF8) {
slouken@1951
   654
            status =
slouken@1951
   655
                Xutf8TextListToTextProperty(display, (char **) &title, 1,
slouken@1951
   656
                                            XUTF8StringStyle, &titleprop);
slouken@1951
   657
            if (status == Success) {
slouken@3685
   658
                XSetTextProperty(display, data->xwindow, &titleprop,
slouken@1951
   659
                                 _NET_WM_NAME);
slouken@1951
   660
                XFree(titleprop.value);
slouken@1951
   661
            }
slouken@1951
   662
        }
slouken@1951
   663
#endif
slouken@1951
   664
    }
slouken@1951
   665
    if (icon != NULL) {
slouken@2143
   666
        char *icon_locale = SDL_iconv_utf8_locale(icon);
slouken@2143
   667
        if (!icon_locale) {
slouken@1951
   668
            SDL_OutOfMemory();
slouken@1951
   669
            return;
slouken@1951
   670
        }
slouken@2143
   671
        status = XStringListToTextProperty(&icon_locale, 1, &iconprop);
slouken@2143
   672
        SDL_free(icon_locale);
slouken@1951
   673
        if (status) {
slouken@3685
   674
            XSetTextProperty(display, data->xwindow, &iconprop,
slouken@1951
   675
                             XA_WM_ICON_NAME);
slouken@1951
   676
            XFree(iconprop.value);
slouken@1951
   677
        }
slouken@1951
   678
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   679
        if (SDL_X11_HAVE_UTF8) {
slouken@1951
   680
            status =
slouken@1951
   681
                Xutf8TextListToTextProperty(display, (char **) &icon, 1,
slouken@1951
   682
                                            XUTF8StringStyle, &iconprop);
slouken@1951
   683
            if (status == Success) {
slouken@3685
   684
                XSetTextProperty(display, data->xwindow, &iconprop,
slouken@1951
   685
                                 _NET_WM_ICON_NAME);
slouken@1951
   686
                XFree(iconprop.value);
slouken@1951
   687
            }
slouken@1951
   688
        }
slouken@1951
   689
#endif
slouken@1951
   690
    }
slouken@5031
   691
    XFlush(display);
slouken@1951
   692
}
slouken@1951
   693
slouken@1951
   694
void
slouken@2967
   695
X11_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slouken@2967
   696
{
slouken@2967
   697
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@2967
   698
    Display *display = data->videodata->display;
slouken@4518
   699
    Atom _NET_WM_ICON = data->videodata->_NET_WM_ICON;
slouken@2967
   700
slouken@2967
   701
    if (icon) {
slouken@2967
   702
        int propsize;
slouken@4528
   703
        long *propdata;
slouken@2967
   704
slouken@2967
   705
        /* Set the _NET_WM_ICON property */
slouken@7523
   706
        SDL_assert(icon->format->format == SDL_PIXELFORMAT_ARGB8888);
slouken@2990
   707
        propsize = 2 + (icon->w * icon->h);
slouken@4566
   708
        propdata = SDL_malloc(propsize * sizeof(long));
slouken@2967
   709
        if (propdata) {
slouken@4528
   710
            int x, y;
slouken@4528
   711
            Uint32 *src;
slouken@4528
   712
            long *dst;
slouken@4528
   713
slouken@2967
   714
            propdata[0] = icon->w;
slouken@2967
   715
            propdata[1] = icon->h;
slouken@4528
   716
            dst = &propdata[2];
slouken@4528
   717
            for (y = 0; y < icon->h; ++y) {
slouken@7523
   718
                src = (Uint32*)((Uint8*)icon->pixels + y * icon->pitch);
slouken@4528
   719
                for (x = 0; x < icon->w; ++x) {
slouken@4528
   720
                    *dst++ = *src++;
slouken@4528
   721
                }
slouken@4528
   722
            }
slouken@3685
   723
            XChangeProperty(display, data->xwindow, _NET_WM_ICON, XA_CARDINAL,
slouken@2990
   724
                            32, PropModeReplace, (unsigned char *) propdata,
slouken@2990
   725
                            propsize);
slouken@2967
   726
        }
icculus@6562
   727
        SDL_free(propdata);
slouken@2967
   728
    } else {
slouken@3685
   729
        XDeleteProperty(display, data->xwindow, _NET_WM_ICON);
slouken@2967
   730
    }
slouken@5031
   731
    XFlush(display);
slouken@2967
   732
}
slouken@2967
   733
slouken@2967
   734
void
slouken@1951
   735
X11_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1951
   736
{
slouken@1951
   737
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   738
    Display *display = data->videodata->display;
slouken@1951
   739
slouken@5478
   740
    XMoveWindow(display, data->xwindow, window->x, window->y);
slouken@5031
   741
    XFlush(display);
slouken@1951
   742
}
slouken@1951
   743
slouken@1951
   744
void
slouken@7617
   745
X11_SetWindowMinimumSize(_THIS, SDL_Window * window)
slouken@7617
   746
{
slouken@7617
   747
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7617
   748
    Display *display = data->videodata->display;
slouken@7617
   749
slouken@7617
   750
    if (window->flags & SDL_WINDOW_RESIZABLE) {
slouken@7617
   751
         XSizeHints *sizehints = XAllocSizeHints();
slouken@7617
   752
         long userhints;
slouken@7617
   753
slouken@7617
   754
         XGetWMNormalHints(display, data->xwindow, sizehints, &userhints);
slouken@7617
   755
slouken@7617
   756
         sizehints->min_width = window->min_w;
slouken@7617
   757
         sizehints->min_height = window->min_h;
slouken@7617
   758
         sizehints->flags |= PMinSize;
slouken@7617
   759
slouken@7617
   760
         XSetWMNormalHints(display, data->xwindow, sizehints);
slouken@7617
   761
slouken@7617
   762
         XFree(sizehints);
slouken@7617
   763
slouken@7617
   764
        /* See comment in X11_SetWindowSize. */
slouken@7617
   765
        XResizeWindow(display, data->xwindow, window->w, window->h);
slouken@7617
   766
        XMoveWindow(display, data->xwindow, window->x, window->y);
slouken@7617
   767
        XRaiseWindow(display, data->xwindow);
slouken@7617
   768
    }
slouken@7617
   769
slouken@7617
   770
    XFlush(display);
slouken@7617
   771
}
slouken@7617
   772
slouken@7617
   773
void
slouken@7617
   774
X11_SetWindowMaximumSize(_THIS, SDL_Window * window)
slouken@7617
   775
{
slouken@7617
   776
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7617
   777
    Display *display = data->videodata->display;
slouken@7617
   778
slouken@7617
   779
    if (window->flags & SDL_WINDOW_RESIZABLE) {
slouken@7617
   780
         XSizeHints *sizehints = XAllocSizeHints();
slouken@7617
   781
         long userhints;
slouken@7617
   782
slouken@7617
   783
         XGetWMNormalHints(display, data->xwindow, sizehints, &userhints);
slouken@7617
   784
slouken@7617
   785
         sizehints->max_width = window->max_w;
slouken@7617
   786
         sizehints->max_height = window->max_h;
slouken@7617
   787
         sizehints->flags |= PMaxSize;
slouken@7617
   788
slouken@7617
   789
         XSetWMNormalHints(display, data->xwindow, sizehints);
slouken@7617
   790
slouken@7617
   791
         XFree(sizehints);
slouken@7617
   792
slouken@7617
   793
        /* See comment in X11_SetWindowSize. */
slouken@7617
   794
        XResizeWindow(display, data->xwindow, window->w, window->h);
slouken@7617
   795
        XMoveWindow(display, data->xwindow, window->x, window->y);
slouken@7617
   796
        XRaiseWindow(display, data->xwindow);
slouken@7617
   797
    }
slouken@7617
   798
slouken@7617
   799
    XFlush(display);
slouken@7617
   800
}
slouken@7617
   801
slouken@7617
   802
void
slouken@1951
   803
X11_SetWindowSize(_THIS, SDL_Window * window)
slouken@1951
   804
{
slouken@1951
   805
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   806
    Display *display = data->videodata->display;
slouken@1951
   807
slouken@6481
   808
    if (SDL_IsShapedWindow(window)) {
eligottlieb@4782
   809
        X11_ResizeWindowShape(window);
slouken@6481
   810
    }
slouken@5531
   811
    if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
slouken@5531
   812
         /* Apparently, if the X11 Window is set to a 'non-resizable' window, you cannot resize it using the XResizeWindow, thus
slouken@5531
   813
            we must set the size hints to adjust the window size.*/
slouken@5531
   814
         XSizeHints *sizehints = XAllocSizeHints();
slouken@5531
   815
         long userhints;
slouken@5531
   816
slouken@5531
   817
         XGetWMNormalHints(display, data->xwindow, sizehints, &userhints);
slouken@5531
   818
slouken@6343
   819
         sizehints->min_width = sizehints->max_width = window->w;
slouken@5531
   820
         sizehints->min_height = sizehints->max_height = window->h;
slouken@7617
   821
         sizehints->flags |= PMinSize | PMaxSize;
slouken@5531
   822
slouken@5531
   823
         XSetWMNormalHints(display, data->xwindow, sizehints);
slouken@5531
   824
slouken@5531
   825
         XFree(sizehints);
slouken@6541
   826
slouken@6541
   827
        /* From Pierre-Loup:
jorgen@6899
   828
           WMs each have their little quirks with that.  When you change the
jorgen@6899
   829
           size hints, they get a ConfigureNotify event with the
jorgen@6899
   830
           WM_NORMAL_SIZE_HINTS Atom.  They all save the hints then, but they
jorgen@6899
   831
           don't all resize the window right away to enforce the new hints.
slouken@6541
   832
jorgen@6899
   833
           Some of them resize only after:
jorgen@6899
   834
            - A user-initiated move or resize
jorgen@6899
   835
            - A code-initiated move or resize
jorgen@6899
   836
            - Hiding & showing window (Unmap & map)
slouken@6541
   837
jorgen@6899
   838
           The following move & resize seems to help a lot of WMs that didn't
jorgen@6899
   839
           properly update after the hints were changed. We don't do a
jorgen@6899
   840
           hide/show, because there are supposedly subtle problems with doing so
jorgen@6899
   841
           and transitioning from windowed to fullscreen in Unity.
jorgen@6899
   842
         */
jorgen@6899
   843
        XResizeWindow(display, data->xwindow, window->w, window->h);
jorgen@6899
   844
        XMoveWindow(display, data->xwindow, window->x, window->y);
slouken@6541
   845
        XRaiseWindow(display, data->xwindow);
slouken@6481
   846
    } else {
slouken@5531
   847
        XResizeWindow(display, data->xwindow, window->w, window->h);
slouken@6481
   848
    }
jorgen@6899
   849
slouken@5031
   850
    XFlush(display);
slouken@1951
   851
}
slouken@1951
   852
icculus@6422
   853
void
icculus@6422
   854
X11_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
icculus@6422
   855
{
icculus@6423
   856
    const SDL_bool focused = ((window->flags & SDL_WINDOW_INPUT_FOCUS) != 0);
icculus@6423
   857
    const SDL_bool visible = ((window->flags & SDL_WINDOW_HIDDEN) == 0);
icculus@6422
   858
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@6422
   859
    SDL_DisplayData *displaydata =
icculus@6422
   860
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
icculus@6422
   861
    Display *display = data->videodata->display;
icculus@6422
   862
    XEvent event;
icculus@6422
   863
icculus@6422
   864
    SetWindowBordered(display, displaydata->screen, data->xwindow, bordered);
icculus@6423
   865
    XFlush(display);
icculus@6422
   866
    XIfEvent(display, &event, &isConfigureNotify, (XPointer)&data->xwindow);
icculus@6422
   867
icculus@6423
   868
    if (visible) {
icculus@6423
   869
        XWindowAttributes attr;
icculus@6423
   870
        do {
icculus@6423
   871
            XSync(display, False);
icculus@6423
   872
            XGetWindowAttributes(display, data->xwindow, &attr);
icculus@6423
   873
        } while (attr.map_state != IsViewable);
icculus@6423
   874
icculus@6423
   875
        if (focused) {
icculus@6423
   876
            XSetInputFocus(display, data->xwindow, RevertToParent, CurrentTime);
icculus@6423
   877
        }
icculus@6423
   878
    }
icculus@6423
   879
icculus@6422
   880
    /* make sure these don't make it to the real event queue if they fired here. */
icculus@6423
   881
    XSync(display, False);
icculus@6423
   882
    XCheckIfEvent(display, &event, &isUnmapNotify, (XPointer)&data->xwindow);
icculus@6423
   883
    XCheckIfEvent(display, &event, &isMapNotify, (XPointer)&data->xwindow);
icculus@6422
   884
}
slouken@6336
   885
slouken@1951
   886
void
slouken@1951
   887
X11_ShowWindow(_THIS, SDL_Window * window)
slouken@1951
   888
{
slouken@1951
   889
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   890
    Display *display = data->videodata->display;
slouken@6336
   891
    XEvent event;
slouken@1951
   892
slouken@6483
   893
    if (!X11_IsWindowMapped(_this, window)) {
slouken@6483
   894
        XMapRaised(display, data->xwindow);
slouken@6483
   895
        /* Blocking wait for "MapNotify" event.
slouken@7191
   896
         * We use XIfEvent because XWindowEvent takes a mask rather than a type,
slouken@6483
   897
         * and XCheckTypedWindowEvent doesn't block */
slouken@6483
   898
        XIfEvent(display, &event, &isMapNotify, (XPointer)&data->xwindow);
slouken@6483
   899
        XFlush(display);
slouken@6483
   900
    }
slouken@1951
   901
}
slouken@1951
   902
slouken@1951
   903
void
slouken@1951
   904
X11_HideWindow(_THIS, SDL_Window * window)
slouken@1951
   905
{
slouken@1951
   906
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7522
   907
    SDL_DisplayData *displaydata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@1951
   908
    Display *display = data->videodata->display;
slouken@6336
   909
    XEvent event;
slouken@1951
   910
slouken@6483
   911
    if (X11_IsWindowMapped(_this, window)) {
slouken@7522
   912
        XWithdrawWindow(display, data->xwindow, displaydata->screen);
slouken@6483
   913
        /* Blocking wait for "UnmapNotify" event */
slouken@7191
   914
        XIfEvent(display, &event, &isUnmapNotify, (XPointer)&data->xwindow);
slouken@6483
   915
        XFlush(display);
slouken@6483
   916
    }
slouken@1951
   917
}
slouken@1951
   918
slouken@1951
   919
void
slouken@1951
   920
X11_RaiseWindow(_THIS, SDL_Window * window)
slouken@1951
   921
{
slouken@1951
   922
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   923
    Display *display = data->videodata->display;
slouken@1951
   924
slouken@3685
   925
    XRaiseWindow(display, data->xwindow);
slouken@5031
   926
    XFlush(display);
slouken@1951
   927
}
slouken@1951
   928
slouken@3077
   929
static void
slouken@5302
   930
SetWindowMaximized(_THIS, SDL_Window * window, SDL_bool maximized)
slouken@3077
   931
{
slouken@3077
   932
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@3077
   933
    SDL_DisplayData *displaydata =
slouken@5246
   934
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@3077
   935
    Display *display = data->videodata->display;
slouken@4518
   936
    Atom _NET_WM_STATE = data->videodata->_NET_WM_STATE;
slouken@4518
   937
    Atom _NET_WM_STATE_MAXIMIZED_VERT = data->videodata->_NET_WM_STATE_MAXIMIZED_VERT;
slouken@4518
   938
    Atom _NET_WM_STATE_MAXIMIZED_HORZ = data->videodata->_NET_WM_STATE_MAXIMIZED_HORZ;
slouken@3077
   939
icculus@7566
   940
    if (maximized) {
icculus@7566
   941
        window->flags |= SDL_WINDOW_MAXIMIZED;
icculus@7566
   942
    } else {
icculus@7566
   943
        window->flags &= ~SDL_WINDOW_MAXIMIZED;
icculus@7566
   944
    }
icculus@7566
   945
slouken@4522
   946
    if (X11_IsWindowMapped(_this, window)) {
slouken@4522
   947
        XEvent e;
slouken@3077
   948
slouken@4524
   949
        SDL_zero(e);
slouken@4522
   950
        e.xany.type = ClientMessage;
slouken@4522
   951
        e.xclient.message_type = _NET_WM_STATE;
slouken@4522
   952
        e.xclient.format = 32;
slouken@4522
   953
        e.xclient.window = data->xwindow;
slouken@4522
   954
        e.xclient.data.l[0] =
slouken@4522
   955
            maximized ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
slouken@4522
   956
        e.xclient.data.l[1] = _NET_WM_STATE_MAXIMIZED_VERT;
slouken@4522
   957
        e.xclient.data.l[2] = _NET_WM_STATE_MAXIMIZED_HORZ;
slouken@4522
   958
        e.xclient.data.l[3] = 0l;
slouken@4522
   959
slouken@4522
   960
        XSendEvent(display, RootWindow(display, displaydata->screen), 0,
slouken@4522
   961
                   SubstructureNotifyMask | SubstructureRedirectMask, &e);
slouken@4522
   962
    } else {
icculus@7566
   963
        X11_SetNetWMState(_this, data->xwindow, window->flags);
slouken@4522
   964
    }
slouken@5031
   965
    XFlush(display);
slouken@3077
   966
}
slouken@3077
   967
slouken@1951
   968
void
slouken@1951
   969
X11_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1951
   970
{
slouken@5302
   971
    SetWindowMaximized(_this, window, SDL_TRUE);
slouken@1951
   972
}
slouken@1951
   973
slouken@1951
   974
void
slouken@1951
   975
X11_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1951
   976
{
slouken@4518
   977
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@4518
   978
    SDL_DisplayData *displaydata =
slouken@5246
   979
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@4518
   980
    Display *display = data->videodata->display;
slouken@7191
   981
slouken@4518
   982
    XIconifyWindow(display, data->xwindow, displaydata->screen);
slouken@5031
   983
    XFlush(display);
slouken@1951
   984
}
slouken@1951
   985
slouken@6970
   986
static void
slouken@6970
   987
SetWindowActive(_THIS, SDL_Window * window)
slouken@6970
   988
{
slouken@6970
   989
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@6970
   990
    SDL_DisplayData *displaydata =
slouken@6970
   991
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@6970
   992
    Display *display = data->videodata->display;
slouken@7191
   993
    Atom _NET_ACTIVE_WINDOW = data->videodata->_NET_ACTIVE_WINDOW;
slouken@6970
   994
slouken@6970
   995
    if (X11_IsWindowMapped(_this, window)) {
slouken@6970
   996
        XEvent e;
slouken@6970
   997
slouken@6970
   998
        SDL_zero(e);
slouken@6970
   999
        e.xany.type = ClientMessage;
slouken@6970
  1000
        e.xclient.message_type = _NET_ACTIVE_WINDOW;
slouken@6970
  1001
        e.xclient.format = 32;
slouken@6970
  1002
        e.xclient.window = data->xwindow;
slouken@6970
  1003
        e.xclient.data.l[0] = 1;  /* source indication. 1 = application */
slouken@7191
  1004
        e.xclient.data.l[1] = CurrentTime;
slouken@7191
  1005
        e.xclient.data.l[2] = 0;
slouken@6970
  1006
slouken@6970
  1007
        XSendEvent(display, RootWindow(display, displaydata->screen), 0,
slouken@6970
  1008
                   SubstructureNotifyMask | SubstructureRedirectMask, &e);
slouken@6970
  1009
slouken@7191
  1010
        XFlush(display);
slouken@6970
  1011
    }
slouken@6970
  1012
}
slouken@6970
  1013
slouken@1951
  1014
void
slouken@1951
  1015
X11_RestoreWindow(_THIS, SDL_Window * window)
slouken@1951
  1016
{
slouken@5302
  1017
    SetWindowMaximized(_this, window, SDL_FALSE);
slouken@7191
  1018
    SetWindowActive(_this, window);
slouken@1951
  1019
    X11_ShowWindow(_this, window);
slouken@1951
  1020
}
slouken@1951
  1021
icculus@6467
  1022
/* This asks the Window Manager to handle fullscreen for us. Most don't do it right, though. */
icculus@6467
  1023
static void
icculus@6467
  1024
X11_SetWindowFullscreenViaWM(_THIS, SDL_Window * window, SDL_VideoDisplay * _display, SDL_bool fullscreen)
slouken@5302
  1025
{
slouken@5302
  1026
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5305
  1027
    SDL_DisplayData *displaydata = (SDL_DisplayData *) _display->driverdata;
slouken@5302
  1028
    Display *display = data->videodata->display;
slouken@5302
  1029
    Atom _NET_WM_STATE = data->videodata->_NET_WM_STATE;
slouken@5302
  1030
    Atom _NET_WM_STATE_FULLSCREEN = data->videodata->_NET_WM_STATE_FULLSCREEN;
slouken@5302
  1031
slouken@5302
  1032
    if (X11_IsWindowMapped(_this, window)) {
slouken@5302
  1033
        XEvent e;
slouken@5302
  1034
slouken@6539
  1035
        if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
slouken@6539
  1036
            /* Compiz refuses fullscreen toggle if we're not resizable, so update the hints so we
slouken@6539
  1037
               can be resized to the fullscreen resolution (or reset so we're not resizable again) */
slouken@6539
  1038
            XSizeHints *sizehints = XAllocSizeHints();
slouken@6539
  1039
            long flags = 0;
slouken@6539
  1040
            XGetWMNormalHints(display, data->xwindow, sizehints, &flags);
slouken@6539
  1041
            /* set the resize flags on */
slouken@6539
  1042
            if (fullscreen) {
slouken@6539
  1043
                /* we are going fullscreen so turn the flags off */
slouken@6539
  1044
                sizehints->flags &= ~(PMinSize | PMaxSize);
slouken@6539
  1045
            } else {
slouken@6539
  1046
                /* Reset the min/max width height to make the window non-resizable again */
icculus@6466
  1047
                sizehints->flags |= PMinSize | PMaxSize;
slouken@6540
  1048
                sizehints->min_width = sizehints->max_width = window->windowed.w;
slouken@6540
  1049
                sizehints->min_height = sizehints->max_height = window->windowed.h;
icculus@6466
  1050
            }
slouken@6539
  1051
            XSetWMNormalHints(display, data->xwindow, sizehints);
slouken@6539
  1052
            XFree(sizehints);
icculus@6466
  1053
        }
slouken@6539
  1054
slouken@5302
  1055
        SDL_zero(e);
slouken@5302
  1056
        e.xany.type = ClientMessage;
slouken@5302
  1057
        e.xclient.message_type = _NET_WM_STATE;
slouken@5302
  1058
        e.xclient.format = 32;
slouken@5302
  1059
        e.xclient.window = data->xwindow;
slouken@5302
  1060
        e.xclient.data.l[0] =
slouken@5302
  1061
            fullscreen ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
slouken@5302
  1062
        e.xclient.data.l[1] = _NET_WM_STATE_FULLSCREEN;
slouken@5302
  1063
        e.xclient.data.l[3] = 0l;
slouken@5302
  1064
slouken@5302
  1065
        XSendEvent(display, RootWindow(display, displaydata->screen), 0,
slouken@5302
  1066
                   SubstructureNotifyMask | SubstructureRedirectMask, &e);
slouken@5302
  1067
    } else {
slouken@6481
  1068
        Uint32 flags;
slouken@5302
  1069
slouken@6481
  1070
        flags = window->flags;
slouken@5302
  1071
        if (fullscreen) {
slouken@6481
  1072
            flags |= SDL_WINDOW_FULLSCREEN;
slouken@6481
  1073
        } else {
slouken@6481
  1074
            flags &= ~SDL_WINDOW_FULLSCREEN;
slouken@5302
  1075
        }
slouken@6487
  1076
        X11_SetNetWMState(_this, data->xwindow, flags);
slouken@5302
  1077
    }
mikesart@6568
  1078
slouken@6575
  1079
    if (data->visual->class == DirectColor) {
slouken@6575
  1080
        if ( fullscreen ) {
slouken@6575
  1081
            XInstallColormap(display, data->colormap);
slouken@6575
  1082
        } else {
slouken@6575
  1083
            XUninstallColormap(display, data->colormap);
slouken@6575
  1084
        }
slouken@6575
  1085
    }
mikesart@6568
  1086
slouken@5302
  1087
    XFlush(display);
slouken@5302
  1088
}
slouken@5302
  1089
icculus@6467
  1090
/* This handles fullscreen itself, outside the Window Manager. */
icculus@6467
  1091
static void
icculus@6467
  1092
X11_BeginWindowFullscreenLegacy(_THIS, SDL_Window * window, SDL_VideoDisplay * _display)
icculus@6467
  1093
{
icculus@6467
  1094
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@6467
  1095
    SDL_DisplayData *displaydata = (SDL_DisplayData *) _display->driverdata;
icculus@6467
  1096
    Visual *visual = data->visual;
icculus@6467
  1097
    Display *display = data->videodata->display;
icculus@6467
  1098
    const int screen = displaydata->screen;
icculus@6467
  1099
    Window root = RootWindow(display, screen);
icculus@6467
  1100
    const int def_vis = (visual == DefaultVisual(display, screen));
icculus@6467
  1101
    unsigned long xattrmask = 0;
icculus@6467
  1102
    XSetWindowAttributes xattr;
icculus@6467
  1103
    XEvent ev;
slouken@6475
  1104
    SDL_Rect rect;
icculus@6467
  1105
icculus@6467
  1106
    if ( data->fswindow ) {
icculus@6467
  1107
        return;  /* already fullscreen, I hope. */
icculus@6467
  1108
    }
icculus@6467
  1109
slouken@6475
  1110
    X11_GetDisplayBounds(_this, _display, &rect);
slouken@6475
  1111
icculus@6467
  1112
    SDL_zero(xattr);
icculus@6467
  1113
    xattr.override_redirect = True;
icculus@6467
  1114
    xattrmask |= CWOverrideRedirect;
icculus@6467
  1115
    xattr.background_pixel = def_vis ? BlackPixel(display, screen) : 0;
icculus@6467
  1116
    xattrmask |= CWBackPixel;
icculus@6467
  1117
    xattr.border_pixel = 0;
icculus@6467
  1118
    xattrmask |= CWBorderPixel;
icculus@6467
  1119
    xattr.colormap = data->colormap;
icculus@6467
  1120
    xattrmask |= CWColormap;
icculus@6467
  1121
slouken@6475
  1122
    data->fswindow = XCreateWindow(display, root,
slouken@6475
  1123
                                   rect.x, rect.y, rect.w, rect.h, 0,
icculus@6467
  1124
                                   displaydata->depth, InputOutput,
icculus@6467
  1125
                                   visual, xattrmask, &xattr);
icculus@6467
  1126
icculus@6467
  1127
    XSelectInput(display, data->fswindow, StructureNotifyMask);
icculus@6467
  1128
    XSetWindowBackground(display, data->fswindow, 0);
slouken@6500
  1129
    XInstallColormap(display, data->colormap);
icculus@6467
  1130
    XClearWindow(display, data->fswindow);
slouken@6480
  1131
    XMapRaised(display, data->fswindow);
icculus@6467
  1132
slouken@6480
  1133
    /* Make sure the fswindow is in view by warping mouse to the corner */
slouken@6500
  1134
    XUngrabPointer(display, CurrentTime);
slouken@6480
  1135
    XWarpPointer(display, None, root, 0, 0, 0, 0, rect.x, rect.y);
icculus@6467
  1136
icculus@6467
  1137
    /* Wait to be mapped, filter Unmap event out if it arrives. */
icculus@6467
  1138
    XIfEvent(display, &ev, &isMapNotify, (XPointer)&data->fswindow);
icculus@6467
  1139
    XCheckIfEvent(display, &ev, &isUnmapNotify, (XPointer)&data->fswindow);
icculus@6467
  1140
icculus@6467
  1141
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
icculus@6467
  1142
    if ( displaydata->use_vidmode ) {
icculus@6467
  1143
        XF86VidModeLockModeSwitch(display, screen, True);
icculus@6467
  1144
    }
icculus@6467
  1145
#endif
icculus@6467
  1146
icculus@6467
  1147
    SetWindowBordered(display, displaydata->screen, data->xwindow, SDL_FALSE);
icculus@6467
  1148
icculus@6467
  1149
    /* Center actual window within our cover-the-screen window. */
slouken@6501
  1150
    XReparentWindow(display, data->xwindow, data->fswindow,
slouken@6501
  1151
                    (rect.w - window->w) / 2, (rect.h - window->h) / 2);
icculus@6467
  1152
slouken@6501
  1153
    /* Center mouse in the fullscreen window. */
slouken@6501
  1154
    rect.x += (rect.w / 2);
slouken@6501
  1155
    rect.y += (rect.h / 2);
slouken@6475
  1156
    XWarpPointer(display, None, root, 0, 0, 0, 0, rect.x, rect.y);
icculus@6467
  1157
icculus@6467
  1158
    /* Wait to be mapped, filter Unmap event out if it arrives. */
icculus@6467
  1159
    XIfEvent(display, &ev, &isMapNotify, (XPointer)&data->xwindow);
slouken@6480
  1160
    XCheckIfEvent(display, &ev, &isUnmapNotify, (XPointer)&data->xwindow);
icculus@6467
  1161
slouken@6662
  1162
    SDL_UpdateWindowGrab(window);
icculus@6467
  1163
}
icculus@6467
  1164
icculus@6467
  1165
static void
icculus@6467
  1166
X11_EndWindowFullscreenLegacy(_THIS, SDL_Window * window, SDL_VideoDisplay * _display)
icculus@6467
  1167
{
icculus@6467
  1168
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@6467
  1169
    SDL_DisplayData *displaydata = (SDL_DisplayData *) _display->driverdata;
icculus@6467
  1170
    Display *display = data->videodata->display;
icculus@6467
  1171
    const int screen = displaydata->screen;
icculus@6467
  1172
    Window root = RootWindow(display, screen);
slouken@6480
  1173
    Window fswindow = data->fswindow;
icculus@6467
  1174
    XEvent ev;
icculus@6467
  1175
slouken@6480
  1176
    if (!data->fswindow) {
icculus@6467
  1177
        return;  /* already not fullscreen, I hope. */
slouken@6480
  1178
    }
icculus@6467
  1179
slouken@6480
  1180
    data->fswindow = None;
icculus@6467
  1181
icculus@6467
  1182
#if SDL_VIDEO_DRIVER_X11_VIDMODE
icculus@6467
  1183
    if ( displaydata->use_vidmode ) {
icculus@6467
  1184
        XF86VidModeLockModeSwitch(display, screen, False);
icculus@6467
  1185
    }
icculus@6467
  1186
#endif
icculus@6467
  1187
slouken@6662
  1188
    SDL_UpdateWindowGrab(window);
icculus@6467
  1189
slouken@6480
  1190
    XReparentWindow(display, data->xwindow, root, window->x, window->y);
slouken@6480
  1191
slouken@6480
  1192
    /* flush these events so they don't confuse normal event handling */
icculus@6467
  1193
    XIfEvent(display, &ev, &isUnmapNotify, (XPointer)&data->xwindow);
icculus@6467
  1194
    XIfEvent(display, &ev, &isMapNotify, (XPointer)&data->xwindow);
icculus@6467
  1195
icculus@6467
  1196
    SetWindowBordered(display, screen, data->xwindow,
icculus@6467
  1197
                      (window->flags & SDL_WINDOW_BORDERLESS) == 0);
icculus@6467
  1198
slouken@7522
  1199
    XWithdrawWindow(display, fswindow, screen);
slouken@6480
  1200
slouken@6480
  1201
    /* Wait to be unmapped. */
slouken@6480
  1202
    XIfEvent(display, &ev, &isUnmapNotify, (XPointer)&fswindow);
slouken@6480
  1203
    XDestroyWindow(display, fswindow);
icculus@6467
  1204
}
icculus@6467
  1205
icculus@6467
  1206
icculus@6467
  1207
void
icculus@6467
  1208
X11_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * _display, SDL_bool fullscreen)
icculus@6467
  1209
{
icculus@6467
  1210
    /* !!! FIXME: SDL_Hint? */
icculus@6467
  1211
    SDL_bool legacy = SDL_FALSE;
icculus@6467
  1212
    const char *env = SDL_getenv("SDL_VIDEO_X11_LEGACY_FULLSCREEN");
icculus@6467
  1213
    if (env) {
icculus@6467
  1214
        legacy = SDL_atoi(env);
icculus@6467
  1215
    } else {
slouken@6473
  1216
        SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
icculus@6467
  1217
        SDL_DisplayData *displaydata = (SDL_DisplayData *) _display->driverdata;
icculus@6467
  1218
        if ( displaydata->use_vidmode ) {
icculus@6467
  1219
            legacy = SDL_TRUE;  /* the new stuff only works with XRandR. */
slouken@6473
  1220
        } else if ( !videodata->net_wm ) {
slouken@6473
  1221
            legacy = SDL_TRUE;  /* The window manager doesn't support it */
icculus@6467
  1222
        } else {
icculus@6467
  1223
            /* !!! FIXME: look at the window manager name, and blacklist certain ones? */
icculus@6467
  1224
            /* http://stackoverflow.com/questions/758648/find-the-name-of-the-x-window-manager */
icculus@6467
  1225
            legacy = SDL_FALSE;  /* try the new way. */
icculus@6467
  1226
        }
icculus@6467
  1227
    }
icculus@6467
  1228
icculus@6467
  1229
    if (legacy) {
icculus@6467
  1230
        if (fullscreen) {
icculus@6467
  1231
            X11_BeginWindowFullscreenLegacy(_this, window, _display);
icculus@6467
  1232
        } else {
icculus@6467
  1233
            X11_EndWindowFullscreenLegacy(_this, window, _display);
icculus@6467
  1234
        }
icculus@6467
  1235
    } else {
icculus@6467
  1236
        X11_SetWindowFullscreenViaWM(_this, window, _display, fullscreen);
icculus@6467
  1237
    }
icculus@6467
  1238
}
icculus@6467
  1239
icculus@6467
  1240
slouken@5466
  1241
int
slouken@5466
  1242
X11_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
slouken@5466
  1243
{
slouken@5466
  1244
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5466
  1245
    Display *display = data->videodata->display;
slouken@5466
  1246
    Visual *visual = data->visual;
slouken@5466
  1247
    Colormap colormap = data->colormap;
slouken@5466
  1248
    XColor *colorcells;
slouken@5466
  1249
    int ncolors;
slouken@5466
  1250
    int rmask, gmask, bmask;
slouken@5466
  1251
    int rshift, gshift, bshift;
icculus@5981
  1252
    int i;
slouken@5466
  1253
slouken@5466
  1254
    if (visual->class != DirectColor) {
icculus@7037
  1255
        return SDL_SetError("Window doesn't have DirectColor visual");
slouken@5466
  1256
    }
slouken@5466
  1257
slouken@5466
  1258
    ncolors = visual->map_entries;
slouken@5466
  1259
    colorcells = SDL_malloc(ncolors * sizeof(XColor));
slouken@5466
  1260
    if (!colorcells) {
icculus@7037
  1261
        return SDL_OutOfMemory();
slouken@5466
  1262
    }
slouken@5466
  1263
slouken@5466
  1264
    rshift = 0;
slouken@5466
  1265
    rmask = visual->red_mask;
slouken@5466
  1266
    while (0 == (rmask & 1)) {
slouken@5466
  1267
        rshift++;
slouken@5466
  1268
        rmask >>= 1;
slouken@5466
  1269
    }
slouken@5466
  1270
slouken@5466
  1271
    gshift = 0;
slouken@5466
  1272
    gmask = visual->green_mask;
slouken@5466
  1273
    while (0 == (gmask & 1)) {
slouken@5466
  1274
        gshift++;
slouken@5466
  1275
        gmask >>= 1;
slouken@5466
  1276
    }
slouken@5466
  1277
slouken@5466
  1278
    bshift = 0;
slouken@5466
  1279
    bmask = visual->blue_mask;
slouken@5466
  1280
    while (0 == (bmask & 1)) {
slouken@5466
  1281
        bshift++;
slouken@5466
  1282
        bmask >>= 1;
slouken@5466
  1283
    }
slouken@5466
  1284
slouken@5466
  1285
    /* build the color table pixel values */
slouken@5466
  1286
    for (i = 0; i < ncolors; i++) {
slouken@5466
  1287
        Uint32 rbits = (rmask * i) / (ncolors - 1);
slouken@5466
  1288
        Uint32 gbits = (gmask * i) / (ncolors - 1);
slouken@5466
  1289
        Uint32 bbits = (bmask * i) / (ncolors - 1);
slouken@5466
  1290
        Uint32 pix = (rbits << rshift) | (gbits << gshift) | (bbits << bshift);
slouken@5466
  1291
slouken@5466
  1292
        colorcells[i].pixel = pix;
slouken@5466
  1293
slouken@5466
  1294
        colorcells[i].red = ramp[(0 * 256) + i];
slouken@5466
  1295
        colorcells[i].green = ramp[(1 * 256) + i];
slouken@5466
  1296
        colorcells[i].blue = ramp[(2 * 256) + i];
slouken@5466
  1297
slouken@5466
  1298
        colorcells[i].flags = DoRed | DoGreen | DoBlue;
slouken@5466
  1299
    }
slouken@5466
  1300
slouken@5466
  1301
    XStoreColors(display, colormap, colorcells, ncolors);
slouken@5466
  1302
    XFlush(display);
slouken@5466
  1303
    SDL_free(colorcells);
slouken@5466
  1304
slouken@5466
  1305
    return 0;
slouken@5466
  1306
}
slouken@5466
  1307
slouken@5302
  1308
void
slouken@6662
  1309
X11_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
slouken@1951
  1310
{
slouken@2875
  1311
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@2875
  1312
    Display *display = data->videodata->display;
slouken@4518
  1313
    SDL_bool oldstyle_fullscreen;
slouken@6662
  1314
    SDL_bool grab_keyboard;
slouken@6662
  1315
    const char *hint;
slouken@2875
  1316
slouken@6662
  1317
    /* ICCCM2.0-compliant window managers can handle fullscreen windows
slouken@6662
  1318
       If we're using XVidMode to change resolution we need to confine
slouken@6662
  1319
       the cursor so we don't pan around the virtual desktop.
slouken@6662
  1320
     */
icculus@6467
  1321
    oldstyle_fullscreen = X11_IsWindowLegacyFullscreen(_this, window);
slouken@4518
  1322
slouken@6662
  1323
    if (oldstyle_fullscreen || grabbed) {
slouken@2875
  1324
        /* Try to grab the mouse */
slouken@2876
  1325
        for (;;) {
slouken@2876
  1326
            int result =
slouken@3685
  1327
                XGrabPointer(display, data->xwindow, True, 0, GrabModeAsync,
slouken@3685
  1328
                             GrabModeAsync, data->xwindow, None, CurrentTime);
slouken@2876
  1329
            if (result == GrabSuccess) {
slouken@2875
  1330
                break;
slouken@2875
  1331
            }
slouken@6662
  1332
            SDL_Delay(50);
slouken@2875
  1333
        }
slouken@2875
  1334
slouken@2875
  1335
        /* Raise the window if we grab the mouse */
slouken@3685
  1336
        XRaiseWindow(display, data->xwindow);
slouken@2875
  1337
slouken@2875
  1338
        /* Now grab the keyboard */
slouken@6662
  1339
        hint = SDL_GetHint(SDL_HINT_GRAB_KEYBOARD);
slouken@6662
  1340
        if (hint && SDL_atoi(hint)) {
slouken@6662
  1341
            grab_keyboard = SDL_TRUE;
slouken@6662
  1342
        } else {
slouken@6662
  1343
            /* We need to do this with the old style override_redirect
slouken@6662
  1344
               fullscreen window otherwise we won't get keyboard focus.
slouken@6662
  1345
            */
slouken@6662
  1346
            grab_keyboard = oldstyle_fullscreen;
slouken@6662
  1347
        }
slouken@6662
  1348
        if (grab_keyboard) {
slouken@6662
  1349
            XGrabKeyboard(display, data->xwindow, True, GrabModeAsync,
slouken@6662
  1350
                          GrabModeAsync, CurrentTime);
slouken@6662
  1351
        }
slouken@2875
  1352
    } else {
slouken@2875
  1353
        XUngrabPointer(display, CurrentTime);
slouken@2875
  1354
        XUngrabKeyboard(display, CurrentTime);
slouken@2875
  1355
    }
slouken@6500
  1356
    XSync(display, False);
slouken@1951
  1357
}
slouken@1951
  1358
slouken@1951
  1359
void
slouken@1951
  1360
X11_DestroyWindow(_THIS, SDL_Window * window)
slouken@1951
  1361
{
slouken@1951
  1362
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
bob@2323
  1363
    window->driverdata = NULL;
slouken@1951
  1364
slouken@1951
  1365
    if (data) {
bob@2323
  1366
        SDL_VideoData *videodata = (SDL_VideoData *) data->videodata;
bob@2323
  1367
        Display *display = videodata->display;
bob@2323
  1368
        int numwindows = videodata->numwindows;
bob@2323
  1369
        SDL_WindowData **windowlist = videodata->windowlist;
bob@2323
  1370
        int i;
bob@2323
  1371
bob@2323
  1372
        if (windowlist) {
bob@2324
  1373
            for (i = 0; i < numwindows; ++i) {
slouken@3685
  1374
                if (windowlist[i] && (windowlist[i]->window == window)) {
bob@2324
  1375
                    windowlist[i] = windowlist[numwindows - 1];
bob@2324
  1376
                    windowlist[numwindows - 1] = NULL;
bob@2324
  1377
                    videodata->numwindows--;
bob@2324
  1378
                    break;
bob@2324
  1379
                }
bob@2323
  1380
            }
bob@2323
  1381
        }
slouken@1951
  1382
#ifdef X_HAVE_UTF8_STRING
slouken@1951
  1383
        if (data->ic) {
slouken@1951
  1384
            XDestroyIC(data->ic);
slouken@1951
  1385
        }
slouken@1951
  1386
#endif
slouken@1951
  1387
        if (data->created) {
slouken@3685
  1388
            XDestroyWindow(display, data->xwindow);
slouken@5031
  1389
            XFlush(display);
slouken@1951
  1390
        }
slouken@1951
  1391
        SDL_free(data);
slouken@1951
  1392
    }
slouken@1951
  1393
}
slouken@1951
  1394
slouken@1951
  1395
SDL_bool
slouken@1951
  1396
X11_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1951
  1397
{
slouken@4510
  1398
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@4510
  1399
    Display *display = data->videodata->display;
slouken@4510
  1400
slouken@4510
  1401
    if (info->version.major == SDL_MAJOR_VERSION &&
slouken@4510
  1402
        info->version.minor == SDL_MINOR_VERSION) {
slouken@4510
  1403
        info->subsystem = SDL_SYSWM_X11;
slouken@5056
  1404
        info->info.x11.display = display;
slouken@5056
  1405
        info->info.x11.window = data->xwindow;
slouken@1951
  1406
        return SDL_TRUE;
slouken@1951
  1407
    } else {
slouken@1951
  1408
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1951
  1409
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1951
  1410
        return SDL_FALSE;
slouken@1951
  1411
    }
slouken@1951
  1412
}
slouken@1951
  1413
slouken@5481
  1414
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
  1415
slouken@1951
  1416
/* vi: set ts=4 sw=4 expandtab: */