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