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