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