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