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