src/video/x11/SDL_x11window.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 26 May 2015 06:27:46 -0700
changeset 9619 b94b6d0bff0f
parent 9614 c9c61d66cfa0
child 9683 aa4e4768c6c1
permissions -rw-r--r--
Updated the copyright year to 2015
slouken@1951
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9619
     3
  Copyright (C) 1997-2015 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
*/
icculus@8093
    21
#include "../../SDL_internal.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
icculus@7827
    64
X11_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
icculus@7827
    68
    while (!X11_XCheckIfEvent(display, event_return, predicate, arg)) {
slouken@7857
    69
        if (SDL_TICKS_PASSED(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
icculus@7827
    91
    X11_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
icculus@7827
   113
    if (X11_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
        }
icculus@7827
   122
        X11_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;
gabomdq@7678
   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
icculus@7827
   144
       actually want to be mapped during X11_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) {
icculus@7827
   161
        X11_XChangeProperty(display, xwindow, _NET_WM_STATE, XA_ATOM, 32,
slouken@6487
   162
                        PropModeReplace, (unsigned char *)atoms, count);
slouken@6487
   163
    } else {
icculus@7827
   164
        X11_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
icculus@7827
   186
    if (X11_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
        }
icculus@7827
   212
        X11_XFree(propertyValue);
slouken@6481
   213
    }
slouken@6481
   214
slouken@6484
   215
    /* FIXME, check the size hints for resizable */
gabomdq@7678
   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 =
icculus@7827
   240
            X11_XCreateIC(videodata->im, XNClientWindow, w, XNFocusWindow, w,
slouken@1951
   241
                       XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
slouken@8707
   242
                       NULL);
slouken@1951
   243
    }
slouken@1951
   244
#endif
slouken@1951
   245
    data->created = created;
slouken@1951
   246
    data->videodata = videodata;
slouken@1951
   247
bob@2323
   248
    /* Associate the data with the window */
bob@2324
   249
bob@2324
   250
    if (numwindows < windowlistlength) {
bob@2324
   251
        windowlist[numwindows] = data;
bob@2324
   252
        videodata->numwindows++;
bob@2324
   253
    } else {
bob@2324
   254
        windowlist =
bob@2324
   255
            (SDL_WindowData **) SDL_realloc(windowlist,
bob@2324
   256
                                            (numwindows +
bob@2324
   257
                                             1) * sizeof(*windowlist));
bob@2324
   258
        if (!windowlist) {
bob@2324
   259
            SDL_free(data);
icculus@7037
   260
            return SDL_OutOfMemory();
bob@2323
   261
        }
bob@2324
   262
        windowlist[numwindows] = data;
bob@2324
   263
        videodata->numwindows++;
bob@2324
   264
        videodata->windowlistlength++;
bob@2324
   265
        videodata->windowlist = windowlist;
bob@2323
   266
    }
bob@2323
   267
slouken@1951
   268
    /* Fill in the SDL window with the window data */
slouken@1951
   269
    {
slouken@1951
   270
        XWindowAttributes attrib;
slouken@1951
   271
icculus@7827
   272
        X11_XGetWindowAttributes(data->videodata->display, w, &attrib);
slouken@1951
   273
        window->x = attrib.x;
slouken@1951
   274
        window->y = attrib.y;
slouken@1951
   275
        window->w = attrib.width;
slouken@1951
   276
        window->h = attrib.height;
slouken@1951
   277
        if (attrib.map_state != IsUnmapped) {
slouken@1951
   278
            window->flags |= SDL_WINDOW_SHOWN;
slouken@1951
   279
        } else {
slouken@1951
   280
            window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1951
   281
        }
slouken@5182
   282
        data->visual = attrib.visual;
slouken@5466
   283
        data->colormap = attrib.colormap;
slouken@1951
   284
    }
slouken@3077
   285
slouken@6487
   286
    window->flags |= X11_GetNetWMState(_this, w);
slouken@3077
   287
icculus@6024
   288
    {
icculus@6024
   289
        Window FocalWindow;
icculus@6024
   290
        int RevertTo=0;
icculus@7827
   291
        X11_XGetInputFocus(data->videodata->display, &FocalWindow, &RevertTo);
icculus@6024
   292
        if (FocalWindow==w)
icculus@6024
   293
        {
icculus@6024
   294
            window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@6481
   295
        }
slouken@6481
   296
slouken@6481
   297
        if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
icculus@6024
   298
            SDL_SetKeyboardFocus(data->window);
icculus@6024
   299
        }
icculus@6024
   300
icculus@6024
   301
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
icculus@6024
   302
            /* Tell x11 to clip mouse */
icculus@6024
   303
        }
icculus@6024
   304
    }
icculus@6024
   305
slouken@1951
   306
    /* All done! */
slouken@6487
   307
    window->driverdata = data;
slouken@1951
   308
    return 0;
slouken@1951
   309
}
slouken@1951
   310
icculus@6422
   311
static void
icculus@6422
   312
SetWindowBordered(Display *display, int screen, Window window, SDL_bool border)
icculus@6422
   313
{
icculus@6422
   314
    /*
icculus@6422
   315
     * this code used to check for KWM_WIN_DECORATION, but KDE hasn't
icculus@6422
   316
     *  supported it for years and years. It now respects _MOTIF_WM_HINTS.
icculus@6422
   317
     *  Gnome is similar: just use the Motif atom.
icculus@6422
   318
     */
icculus@6422
   319
icculus@7827
   320
    Atom WM_HINTS = X11_XInternAtom(display, "_MOTIF_WM_HINTS", True);
icculus@6422
   321
    if (WM_HINTS != None) {
icculus@6422
   322
        /* Hints used by Motif compliant window managers */
icculus@6422
   323
        struct
icculus@6422
   324
        {
icculus@6422
   325
            unsigned long flags;
icculus@6422
   326
            unsigned long functions;
icculus@6422
   327
            unsigned long decorations;
icculus@6422
   328
            long input_mode;
icculus@6422
   329
            unsigned long status;
icculus@6422
   330
        } MWMHints = {
icculus@6422
   331
            (1L << 1), 0, border ? 1 : 0, 0, 0
icculus@6422
   332
        };
icculus@6422
   333
icculus@7827
   334
        X11_XChangeProperty(display, window, WM_HINTS, WM_HINTS, 32,
icculus@6422
   335
                        PropModeReplace, (unsigned char *) &MWMHints,
urkle@9460
   336
                        sizeof(MWMHints) / sizeof(long));
icculus@6422
   337
    } else {  /* set the transient hints instead, if necessary */
icculus@7827
   338
        X11_XSetTransientForHint(display, window, RootWindow(display, screen));
icculus@6422
   339
    }
icculus@6422
   340
}
icculus@6422
   341
slouken@1951
   342
int
slouken@1951
   343
X11_CreateWindow(_THIS, SDL_Window * window)
slouken@1951
   344
{
slouken@1951
   345
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   346
    SDL_DisplayData *displaydata =
slouken@5246
   347
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@7683
   348
    SDL_WindowData *windowdata;
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@8785
   364
    long fevent = 0;
slouken@4518
   365
gabomdq@7659
   366
#if SDL_VIDEO_OPENGL_GLX || SDL_VIDEO_OPENGL_EGL
pgriffais@7737
   367
    if ((window->flags & SDL_WINDOW_OPENGL) &&
pgriffais@7737
   368
        !SDL_getenv("SDL_VIDEO_X11_VISUALID")) {
gabomdq@7723
   369
        XVisualInfo *vinfo = NULL;
slouken@1952
   370
gabomdq@7659
   371
#if SDL_VIDEO_OPENGL_EGL
gabomdq@7808
   372
        if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES 
gabomdq@7808
   373
#if SDL_VIDEO_OPENGL_GLX            
gabomdq@7808
   374
            && ( !_this->gl_data || ! _this->gl_data->HAS_GLX_EXT_create_context_es2_profile )
gabomdq@7808
   375
#endif
slouken@8780
   376
        ) {
slouken@6369
   377
            vinfo = X11_GLES_GetVisual(_this, display, screen);
slouken@6369
   378
        } else
slouken@6369
   379
#endif
slouken@6369
   380
        {
slouken@6370
   381
#if SDL_VIDEO_OPENGL_GLX
slouken@6369
   382
            vinfo = X11_GL_GetVisual(_this, display, screen);
slouken@6370
   383
#endif
slouken@1952
   384
        }
gabomdq@7723
   385
slouken@3161
   386
        if (!vinfo) {
slouken@3161
   387
            return -1;
slouken@3161
   388
        }
slouken@3161
   389
        visual = vinfo->visual;
slouken@3161
   390
        depth = vinfo->depth;
icculus@7827
   391
        X11_XFree(vinfo);
slouken@3161
   392
    } else
slouken@3161
   393
#endif
slouken@1952
   394
    {
slouken@1951
   395
        visual = displaydata->visual;
slouken@1951
   396
        depth = displaydata->depth;
slouken@1951
   397
    }
slouken@1951
   398
slouken@5386
   399
    xattr.override_redirect = False;
slouken@7750
   400
    xattr.background_pixmap = None;
slouken@1951
   401
    xattr.border_pixel = 0;
slouken@5466
   402
slouken@5466
   403
    if (visual->class == DirectColor) {
slouken@5466
   404
        XColor *colorcells;
slouken@5466
   405
        int i;
slouken@5466
   406
        int ncolors;
slouken@5466
   407
        int rmax, gmax, bmax;
slouken@5466
   408
        int rmask, gmask, bmask;
slouken@5466
   409
        int rshift, gshift, bshift;
slouken@5466
   410
slouken@5466
   411
        xattr.colormap =
icculus@7827
   412
            X11_XCreateColormap(display, RootWindow(display, screen),
slouken@5466
   413
                            visual, AllocAll);
slouken@5466
   414
slouken@5466
   415
        /* If we can't create a colormap, then we must die */
slouken@5466
   416
        if (!xattr.colormap) {
icculus@7037
   417
            return SDL_SetError("Could not create writable colormap");
slouken@5466
   418
        }
slouken@5466
   419
slouken@5466
   420
        /* OK, we got a colormap, now fill it in as best as we can */
slouken@5466
   421
        colorcells = SDL_malloc(visual->map_entries * sizeof(XColor));
slouken@5466
   422
        if (!colorcells) {
icculus@7037
   423
            return SDL_OutOfMemory();
slouken@5466
   424
        }
slouken@5466
   425
        ncolors = visual->map_entries;
slouken@5466
   426
        rmax = 0xffff;
slouken@5466
   427
        gmax = 0xffff;
slouken@5466
   428
        bmax = 0xffff;
slouken@5466
   429
slouken@5466
   430
        rshift = 0;
slouken@5466
   431
        rmask = visual->red_mask;
slouken@5466
   432
        while (0 == (rmask & 1)) {
slouken@5466
   433
            rshift++;
slouken@5466
   434
            rmask >>= 1;
slouken@5466
   435
        }
slouken@5466
   436
slouken@5466
   437
        gshift = 0;
slouken@5466
   438
        gmask = visual->green_mask;
slouken@5466
   439
        while (0 == (gmask & 1)) {
slouken@5466
   440
            gshift++;
slouken@5466
   441
            gmask >>= 1;
slouken@5466
   442
        }
slouken@5466
   443
slouken@5466
   444
        bshift = 0;
slouken@5466
   445
        bmask = visual->blue_mask;
slouken@5466
   446
        while (0 == (bmask & 1)) {
slouken@5466
   447
            bshift++;
slouken@5466
   448
            bmask >>= 1;
slouken@5466
   449
        }
slouken@5466
   450
slouken@5466
   451
        /* build the color table pixel values */
slouken@5466
   452
        for (i = 0; i < ncolors; i++) {
slouken@5466
   453
            Uint32 red = (rmax * i) / (ncolors - 1);
slouken@5466
   454
            Uint32 green = (gmax * i) / (ncolors - 1);
slouken@5466
   455
            Uint32 blue = (bmax * i) / (ncolors - 1);
slouken@5466
   456
slouken@5466
   457
            Uint32 rbits = (rmask * i) / (ncolors - 1);
slouken@5466
   458
            Uint32 gbits = (gmask * i) / (ncolors - 1);
slouken@5466
   459
            Uint32 bbits = (bmask * i) / (ncolors - 1);
slouken@5466
   460
slouken@5466
   461
            Uint32 pix =
slouken@5466
   462
                (rbits << rshift) | (gbits << gshift) | (bbits << bshift);
slouken@5466
   463
slouken@5466
   464
            colorcells[i].pixel = pix;
slouken@5466
   465
slouken@5466
   466
            colorcells[i].red = red;
slouken@5466
   467
            colorcells[i].green = green;
slouken@5466
   468
            colorcells[i].blue = blue;
slouken@5466
   469
slouken@5466
   470
            colorcells[i].flags = DoRed | DoGreen | DoBlue;
slouken@5466
   471
        }
slouken@5466
   472
icculus@7827
   473
        X11_XStoreColors(display, xattr.colormap, colorcells, ncolors);
slouken@5466
   474
slouken@5466
   475
        SDL_free(colorcells);
slouken@5466
   476
    } else {
slouken@5466
   477
        xattr.colormap =
icculus@7827
   478
            X11_XCreateColormap(display, RootWindow(display, screen),
slouken@5466
   479
                            visual, AllocNone);
slouken@5466
   480
    }
slouken@1951
   481
icculus@7827
   482
    w = X11_XCreateWindow(display, RootWindow(display, screen),
slouken@5386
   483
                      window->x, window->y, window->w, window->h,
slouken@5386
   484
                      0, depth, InputOutput, visual,
slouken@7750
   485
                      (CWOverrideRedirect | CWBackPixmap | CWBorderPixel |
slouken@1951
   486
                       CWColormap), &xattr);
slouken@1952
   487
    if (!w) {
icculus@7037
   488
        return SDL_SetError("Couldn't create window");
slouken@1952
   489
    }
slouken@1951
   490
icculus@6422
   491
    SetWindowBordered(display, screen, w,
icculus@6422
   492
                      (window->flags & SDL_WINDOW_BORDERLESS) == 0);
slouken@1951
   493
icculus@7827
   494
    sizehints = X11_XAllocSizeHints();
mail@6167
   495
    /* Setup the normal size hints */
urkle@6462
   496
    sizehints->flags = 0;
mail@6167
   497
    if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
urkle@6462
   498
        sizehints->min_width = sizehints->max_width = window->w;
urkle@6462
   499
        sizehints->min_height = sizehints->max_height = window->h;
urkle@6462
   500
        sizehints->flags |= (PMaxSize | PMinSize);
slouken@1951
   501
    }
urkle@6462
   502
    sizehints->x = window->x;
urkle@6462
   503
    sizehints->y = window->y;
urkle@6462
   504
    sizehints->flags |= USPosition;
slouken@1951
   505
mail@6167
   506
    /* Setup the input hints so we get keyboard input */
icculus@7827
   507
    wmhints = X11_XAllocWMHints();
urkle@6462
   508
    wmhints->input = True;
urkle@6462
   509
    wmhints->flags = InputHint;
mail@6167
   510
mail@6167
   511
    /* Setup the class hints so we can get an icon (AfterStep) */
icculus@7827
   512
    classhints = X11_XAllocClassHint();
urkle@6462
   513
    classhints->res_name = data->classname;
urkle@6462
   514
    classhints->res_class = data->classname;
mail@6167
   515
mail@6167
   516
    /* Set the size, input and class hints, and define WM_CLIENT_MACHINE and WM_LOCALE_NAME */
icculus@7827
   517
    X11_XSetWMProperties(display, w, NULL, NULL, NULL, 0, sizehints, wmhints, classhints);
mail@6167
   518
icculus@7827
   519
    X11_XFree(sizehints);
icculus@7827
   520
    X11_XFree(wmhints);
icculus@7827
   521
    X11_XFree(classhints);
mail@6167
   522
    /* Set the PID related to the window for the given hostname, if possible */
mail@6167
   523
    if (data->pid > 0) {
icculus@7827
   524
        _NET_WM_PID = X11_XInternAtom(display, "_NET_WM_PID", False);
icculus@7827
   525
        X11_XChangeProperty(display, w, _NET_WM_PID, XA_CARDINAL, 32, PropModeReplace,
mail@6167
   526
                        (unsigned char *)&data->pid, 1);
slouken@1951
   527
    }
slouken@1951
   528
slouken@4522
   529
    /* Set the window manager state */
slouken@6487
   530
    X11_SetNetWMState(_this, w, window->flags);
slouken@4518
   531
slouken@4521
   532
    /* Let the window manager know we're a "normal" window */
icculus@7827
   533
    _NET_WM_WINDOW_TYPE = X11_XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
icculus@7827
   534
    _NET_WM_WINDOW_TYPE_NORMAL = X11_XInternAtom(display, "_NET_WM_WINDOW_TYPE_NORMAL", False);
icculus@7827
   535
    X11_XChangeProperty(display, w, _NET_WM_WINDOW_TYPE, XA_ATOM, 32,
slouken@4521
   536
                    PropModeReplace,
slouken@4521
   537
                    (unsigned char *)&_NET_WM_WINDOW_TYPE_NORMAL, 1);
slouken@4521
   538
icculus@7827
   539
    _NET_WM_BYPASS_COMPOSITOR = X11_XInternAtom(display, "_NET_WM_BYPASS_COMPOSITOR", False);
icculus@7827
   540
    X11_XChangeProperty(display, w, _NET_WM_BYPASS_COMPOSITOR, XA_CARDINAL, 32,
jstpierre@7490
   541
                    PropModeReplace,
jstpierre@7490
   542
                    (unsigned char *)&_NET_WM_BYPASS_COMPOSITOR_HINT_ON, 1);
slouken@7191
   543
icculus@6638
   544
    {
icculus@6638
   545
        Atom protocols[] = {
icculus@6638
   546
            data->WM_DELETE_WINDOW, /* Allow window to be deleted by the WM */
icculus@6638
   547
            data->_NET_WM_PING, /* Respond so WM knows we're alive */
icculus@6638
   548
        };
icculus@7827
   549
        X11_XSetWMProtocols(display, w, protocols, sizeof (protocols) / sizeof (protocols[0]));
icculus@6638
   550
    }
slouken@1951
   551
slouken@1951
   552
    if (SetupWindowData(_this, window, w, SDL_TRUE) < 0) {
icculus@7827
   553
        X11_XDestroyWindow(display, w);
slouken@1951
   554
        return -1;
slouken@1951
   555
    }
slouken@7683
   556
    windowdata = (SDL_WindowData *) window->driverdata;
slouken@5386
   557
gabomdq@7659
   558
#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
gabomdq@7723
   559
    if ((window->flags & SDL_WINDOW_OPENGL) && 
gabomdq@7808
   560
        _this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES
gabomdq@7808
   561
#if SDL_VIDEO_OPENGL_GLX            
gabomdq@7808
   562
        && ( !_this->gl_data || ! _this->gl_data->HAS_GLX_EXT_create_context_es2_profile )
gabomdq@7808
   563
#endif  
gabomdq@7808
   564
    ) {
gabomdq@7960
   565
#if SDL_VIDEO_OPENGL_EGL  
gabomdq@7659
   566
        if (!_this->egl_data) {
icculus@7827
   567
            X11_XDestroyWindow(display, w);
gabomdq@7659
   568
            return -1;
gabomdq@7659
   569
        }
gabomdq@7659
   570
gabomdq@7659
   571
        /* Create the GLES window surface */
slouken@7683
   572
        windowdata->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) w);
gabomdq@7659
   573
slouken@7683
   574
        if (windowdata->egl_surface == EGL_NO_SURFACE) {
icculus@7827
   575
            X11_XDestroyWindow(display, w);
gabomdq@7659
   576
            return SDL_SetError("Could not create GLES window surface");
gabomdq@7659
   577
        }
gabomdq@7960
   578
#else
gabomdq@7960
   579
        return SDL_SetError("Could not create GLES window surface (no EGL support available)");
gabomdq@7960
   580
#endif /* SDL_VIDEO_OPENGL_EGL */
gabomdq@7659
   581
    }
gabomdq@7659
   582
#endif
gabomdq@7659
   583
    
gabomdq@7659
   584
bob@2325
   585
#ifdef X_HAVE_UTF8_STRING
slouken@7683
   586
    if (SDL_X11_HAVE_UTF8 && windowdata->ic) {
icculus@7827
   587
        X11_XGetICValues(windowdata->ic, XNFilterEvents, &fevent, NULL);
kazeuser@2718
   588
    }
bob@2325
   589
#endif
bob@2325
   590
dimitris@6316
   591
    X11_Xinput2SelectTouch(_this, window);
dimitris@6316
   592
icculus@7827
   593
    X11_XSelectInput(display, w,
icculus@6045
   594
                 (FocusChangeMask | EnterWindowMask | LeaveWindowMask |
icculus@6045
   595
                 ExposureMask | ButtonPressMask | ButtonReleaseMask |
icculus@6045
   596
                 PointerMotionMask | KeyPressMask | KeyReleaseMask |
icculus@6045
   597
                 PropertyChangeMask | StructureNotifyMask |
icculus@6045
   598
                 KeymapStateMask | fevent));
icculus@6045
   599
icculus@7827
   600
    XdndAware = X11_XInternAtom(display, "XdndAware", False);
icculus@7827
   601
    X11_XChangeProperty(display, w, XdndAware, XA_ATOM, 32,
slouken@7001
   602
                 PropModeReplace,
slouken@7191
   603
                 (unsigned char*)&xdnd_version, 1);
slouken@7001
   604
icculus@7827
   605
    X11_XFlush(display);
slouken@5031
   606
slouken@1951
   607
    return 0;
slouken@1951
   608
}
slouken@1951
   609
slouken@1951
   610
int
bob@2324
   611
X11_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1951
   612
{
slouken@1951
   613
    Window w = (Window) data;
slouken@1951
   614
slouken@4518
   615
    window->title = X11_GetWindowTitle(_this, w);
slouken@1951
   616
slouken@1951
   617
    if (SetupWindowData(_this, window, w, SDL_FALSE) < 0) {
slouken@1951
   618
        return -1;
slouken@1951
   619
    }
slouken@1951
   620
    return 0;
slouken@1951
   621
}
slouken@1951
   622
slouken@4518
   623
char *
slouken@4518
   624
X11_GetWindowTitle(_THIS, Window xwindow)
slouken@4518
   625
{
slouken@4518
   626
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@4518
   627
    Display *display = data->display;
slouken@4518
   628
    int status, real_format;
slouken@4518
   629
    Atom real_type;
slouken@4518
   630
    unsigned long items_read, items_left;
slouken@4518
   631
    unsigned char *propdata;
slouken@4518
   632
    char *title = NULL;
slouken@4518
   633
icculus@7827
   634
    status = X11_XGetWindowProperty(display, xwindow, data->_NET_WM_NAME,
slouken@4518
   635
                0L, 8192L, False, data->UTF8_STRING, &real_type, &real_format,
slouken@4518
   636
                &items_read, &items_left, &propdata);
icculus@6023
   637
    if (status == Success && propdata) {
slouken@4518
   638
        title = SDL_strdup(SDL_static_cast(char*, propdata));
icculus@7827
   639
        X11_XFree(propdata);
slouken@4518
   640
    } else {
icculus@7827
   641
        status = X11_XGetWindowProperty(display, xwindow, XA_WM_NAME,
slouken@4518
   642
                    0L, 8192L, False, XA_STRING, &real_type, &real_format,
slouken@4518
   643
                    &items_read, &items_left, &propdata);
icculus@6023
   644
        if (status == Success && propdata) {
slouken@4518
   645
            title = SDL_iconv_string("UTF-8", "", SDL_static_cast(char*, propdata), items_read+1);
slouken@9117
   646
            X11_XFree(propdata);
slouken@4518
   647
        } else {
slouken@4518
   648
            title = SDL_strdup("");
slouken@4518
   649
        }
slouken@4518
   650
    }
slouken@4518
   651
    return title;
slouken@4518
   652
}
slouken@4518
   653
slouken@1951
   654
void
slouken@1951
   655
X11_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1951
   656
{
slouken@1951
   657
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   658
    Display *display = data->videodata->display;
icculus@9479
   659
    XTextProperty titleprop;
slouken@1951
   660
    Status status;
icculus@9474
   661
    const char *title = window->title ? window->title : "";
icculus@9474
   662
    char *title_locale = NULL;
slouken@1951
   663
slouken@1951
   664
#ifdef X_HAVE_UTF8_STRING
slouken@4518
   665
    Atom _NET_WM_NAME = data->videodata->_NET_WM_NAME;
slouken@1951
   666
#endif
slouken@1951
   667
icculus@9474
   668
    title_locale = SDL_iconv_utf8_locale(title);
icculus@9474
   669
    if (!title_locale) {
icculus@9474
   670
        SDL_OutOfMemory();
icculus@9474
   671
        return;
icculus@9474
   672
    }
icculus@9474
   673
icculus@9474
   674
    status = X11_XStringListToTextProperty(&title_locale, 1, &titleprop);
icculus@9474
   675
    SDL_free(title_locale);
icculus@9474
   676
    if (status) {
icculus@9474
   677
        X11_XSetTextProperty(display, data->xwindow, &titleprop, XA_WM_NAME);
icculus@9474
   678
        X11_XFree(titleprop.value);
icculus@9474
   679
    }
icculus@9474
   680
#ifdef X_HAVE_UTF8_STRING
icculus@9474
   681
    if (SDL_X11_HAVE_UTF8) {
icculus@9474
   682
        status = X11_Xutf8TextListToTextProperty(display, (char **) &title, 1,
icculus@9474
   683
                                            XUTF8StringStyle, &titleprop);
icculus@9474
   684
        if (status == Success) {
icculus@9474
   685
            X11_XSetTextProperty(display, data->xwindow, &titleprop,
icculus@9474
   686
                                 _NET_WM_NAME);
icculus@7827
   687
            X11_XFree(titleprop.value);
slouken@1951
   688
        }
icculus@9474
   689
    }
slouken@1951
   690
#endif
icculus@9474
   691
icculus@7827
   692
    X11_XFlush(display);
slouken@1951
   693
}
slouken@1951
   694
slouken@1951
   695
void
slouken@2967
   696
X11_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slouken@2967
   697
{
slouken@2967
   698
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@2967
   699
    Display *display = data->videodata->display;
slouken@4518
   700
    Atom _NET_WM_ICON = data->videodata->_NET_WM_ICON;
slouken@2967
   701
slouken@2967
   702
    if (icon) {
slouken@2967
   703
        int propsize;
slouken@4528
   704
        long *propdata;
slouken@2967
   705
slouken@2967
   706
        /* Set the _NET_WM_ICON property */
slouken@7523
   707
        SDL_assert(icon->format->format == SDL_PIXELFORMAT_ARGB8888);
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@7523
   719
                src = (Uint32*)((Uint8*)icon->pixels + y * icon->pitch);
slouken@4528
   720
                for (x = 0; x < icon->w; ++x) {
slouken@4528
   721
                    *dst++ = *src++;
slouken@4528
   722
                }
slouken@4528
   723
            }
icculus@7827
   724
            X11_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
    } else {
icculus@7827
   730
        X11_XDeleteProperty(display, data->xwindow, _NET_WM_ICON);
slouken@2967
   731
    }
icculus@7827
   732
    X11_XFlush(display);
slouken@2967
   733
}
slouken@2967
   734
slouken@2967
   735
void
slouken@1951
   736
X11_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1951
   737
{
slouken@1951
   738
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   739
    Display *display = data->videodata->display;
slouken@1951
   740
icculus@7827
   741
    X11_XMoveWindow(display, data->xwindow, window->x, window->y);
icculus@7827
   742
    X11_XFlush(display);
slouken@1951
   743
}
slouken@1951
   744
slouken@1951
   745
void
slouken@7617
   746
X11_SetWindowMinimumSize(_THIS, SDL_Window * window)
slouken@7617
   747
{
slouken@7617
   748
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7617
   749
    Display *display = data->videodata->display;
slouken@7617
   750
slouken@7617
   751
    if (window->flags & SDL_WINDOW_RESIZABLE) {
icculus@7827
   752
         XSizeHints *sizehints = X11_XAllocSizeHints();
slouken@7617
   753
         long userhints;
slouken@7617
   754
icculus@7827
   755
         X11_XGetWMNormalHints(display, data->xwindow, sizehints, &userhints);
slouken@7617
   756
slouken@7617
   757
         sizehints->min_width = window->min_w;
slouken@7617
   758
         sizehints->min_height = window->min_h;
slouken@7617
   759
         sizehints->flags |= PMinSize;
slouken@7617
   760
icculus@7827
   761
         X11_XSetWMNormalHints(display, data->xwindow, sizehints);
slouken@7617
   762
icculus@7827
   763
         X11_XFree(sizehints);
slouken@7617
   764
slouken@7617
   765
        /* See comment in X11_SetWindowSize. */
icculus@7827
   766
        X11_XResizeWindow(display, data->xwindow, window->w, window->h);
icculus@7827
   767
        X11_XMoveWindow(display, data->xwindow, window->x, window->y);
icculus@7827
   768
        X11_XRaiseWindow(display, data->xwindow);
slouken@7617
   769
    }
slouken@7617
   770
icculus@7827
   771
    X11_XFlush(display);
slouken@7617
   772
}
slouken@7617
   773
slouken@7617
   774
void
slouken@7617
   775
X11_SetWindowMaximumSize(_THIS, SDL_Window * window)
slouken@7617
   776
{
slouken@7617
   777
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7617
   778
    Display *display = data->videodata->display;
slouken@7617
   779
slouken@7617
   780
    if (window->flags & SDL_WINDOW_RESIZABLE) {
icculus@7827
   781
         XSizeHints *sizehints = X11_XAllocSizeHints();
slouken@7617
   782
         long userhints;
slouken@7617
   783
icculus@7827
   784
         X11_XGetWMNormalHints(display, data->xwindow, sizehints, &userhints);
slouken@7617
   785
slouken@7617
   786
         sizehints->max_width = window->max_w;
slouken@7617
   787
         sizehints->max_height = window->max_h;
slouken@7617
   788
         sizehints->flags |= PMaxSize;
slouken@7617
   789
icculus@7827
   790
         X11_XSetWMNormalHints(display, data->xwindow, sizehints);
slouken@7617
   791
icculus@7827
   792
         X11_XFree(sizehints);
slouken@7617
   793
slouken@7617
   794
        /* See comment in X11_SetWindowSize. */
icculus@7827
   795
        X11_XResizeWindow(display, data->xwindow, window->w, window->h);
icculus@7827
   796
        X11_XMoveWindow(display, data->xwindow, window->x, window->y);
icculus@7827
   797
        X11_XRaiseWindow(display, data->xwindow);
slouken@7617
   798
    }
slouken@7617
   799
icculus@7827
   800
    X11_XFlush(display);
slouken@7617
   801
}
slouken@7617
   802
slouken@7617
   803
void
slouken@1951
   804
X11_SetWindowSize(_THIS, SDL_Window * window)
slouken@1951
   805
{
slouken@1951
   806
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   807
    Display *display = data->videodata->display;
slouken@1951
   808
slouken@6481
   809
    if (SDL_IsShapedWindow(window)) {
eligottlieb@4782
   810
        X11_ResizeWindowShape(window);
slouken@6481
   811
    }
slouken@5531
   812
    if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
icculus@7827
   813
         /* Apparently, if the X11 Window is set to a 'non-resizable' window, you cannot resize it using the X11_XResizeWindow, thus
gabomdq@7677
   814
            we must set the size hints to adjust the window size. */
icculus@7827
   815
         XSizeHints *sizehints = X11_XAllocSizeHints();
slouken@5531
   816
         long userhints;
slouken@5531
   817
icculus@7827
   818
         X11_XGetWMNormalHints(display, data->xwindow, sizehints, &userhints);
slouken@5531
   819
slouken@6343
   820
         sizehints->min_width = sizehints->max_width = window->w;
slouken@5531
   821
         sizehints->min_height = sizehints->max_height = window->h;
slouken@7617
   822
         sizehints->flags |= PMinSize | PMaxSize;
slouken@5531
   823
icculus@7827
   824
         X11_XSetWMNormalHints(display, data->xwindow, sizehints);
slouken@5531
   825
icculus@7827
   826
         X11_XFree(sizehints);
slouken@6541
   827
slouken@6541
   828
        /* From Pierre-Loup:
jorgen@6899
   829
           WMs each have their little quirks with that.  When you change the
jorgen@6899
   830
           size hints, they get a ConfigureNotify event with the
jorgen@6899
   831
           WM_NORMAL_SIZE_HINTS Atom.  They all save the hints then, but they
jorgen@6899
   832
           don't all resize the window right away to enforce the new hints.
slouken@6541
   833
jorgen@6899
   834
           Some of them resize only after:
jorgen@6899
   835
            - A user-initiated move or resize
jorgen@6899
   836
            - A code-initiated move or resize
jorgen@6899
   837
            - Hiding & showing window (Unmap & map)
slouken@6541
   838
jorgen@6899
   839
           The following move & resize seems to help a lot of WMs that didn't
jorgen@6899
   840
           properly update after the hints were changed. We don't do a
jorgen@6899
   841
           hide/show, because there are supposedly subtle problems with doing so
jorgen@6899
   842
           and transitioning from windowed to fullscreen in Unity.
jorgen@6899
   843
         */
icculus@7827
   844
        X11_XResizeWindow(display, data->xwindow, window->w, window->h);
icculus@7827
   845
        X11_XMoveWindow(display, data->xwindow, window->x, window->y);
icculus@7827
   846
        X11_XRaiseWindow(display, data->xwindow);
slouken@6481
   847
    } else {
icculus@7827
   848
        X11_XResizeWindow(display, data->xwindow, window->w, window->h);
slouken@6481
   849
    }
jorgen@6899
   850
icculus@7827
   851
    X11_XFlush(display);
slouken@1951
   852
}
slouken@1951
   853
icculus@6422
   854
void
icculus@6422
   855
X11_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
icculus@6422
   856
{
icculus@6423
   857
    const SDL_bool focused = ((window->flags & SDL_WINDOW_INPUT_FOCUS) != 0);
icculus@6423
   858
    const SDL_bool visible = ((window->flags & SDL_WINDOW_HIDDEN) == 0);
icculus@6422
   859
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@6422
   860
    SDL_DisplayData *displaydata =
icculus@6422
   861
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
icculus@6422
   862
    Display *display = data->videodata->display;
icculus@6422
   863
    XEvent event;
icculus@6422
   864
icculus@6422
   865
    SetWindowBordered(display, displaydata->screen, data->xwindow, bordered);
icculus@7827
   866
    X11_XFlush(display);
icculus@7827
   867
    X11_XIfEvent(display, &event, &isConfigureNotify, (XPointer)&data->xwindow);
icculus@6422
   868
icculus@6423
   869
    if (visible) {
icculus@6423
   870
        XWindowAttributes attr;
icculus@6423
   871
        do {
icculus@7827
   872
            X11_XSync(display, False);
icculus@7827
   873
            X11_XGetWindowAttributes(display, data->xwindow, &attr);
icculus@6423
   874
        } while (attr.map_state != IsViewable);
icculus@6423
   875
icculus@6423
   876
        if (focused) {
icculus@7827
   877
            X11_XSetInputFocus(display, data->xwindow, RevertToParent, CurrentTime);
icculus@6423
   878
        }
icculus@6423
   879
    }
icculus@6423
   880
icculus@6422
   881
    /* make sure these don't make it to the real event queue if they fired here. */
icculus@7827
   882
    X11_XSync(display, False);
icculus@7827
   883
    X11_XCheckIfEvent(display, &event, &isUnmapNotify, (XPointer)&data->xwindow);
icculus@7827
   884
    X11_XCheckIfEvent(display, &event, &isMapNotify, (XPointer)&data->xwindow);
icculus@6422
   885
}
slouken@6336
   886
slouken@1951
   887
void
slouken@1951
   888
X11_ShowWindow(_THIS, SDL_Window * window)
slouken@1951
   889
{
slouken@1951
   890
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   891
    Display *display = data->videodata->display;
slouken@6336
   892
    XEvent event;
slouken@1951
   893
slouken@6483
   894
    if (!X11_IsWindowMapped(_this, window)) {
icculus@7827
   895
        X11_XMapRaised(display, data->xwindow);
slouken@6483
   896
        /* Blocking wait for "MapNotify" event.
icculus@7827
   897
         * We use X11_XIfEvent because pXWindowEvent takes a mask rather than a type,
slouken@6483
   898
         * and XCheckTypedWindowEvent doesn't block */
icculus@7827
   899
        X11_XIfEvent(display, &event, &isMapNotify, (XPointer)&data->xwindow);
icculus@7827
   900
        X11_XFlush(display);
slouken@6483
   901
    }
slouken@1951
   902
}
slouken@1951
   903
slouken@1951
   904
void
slouken@1951
   905
X11_HideWindow(_THIS, SDL_Window * window)
slouken@1951
   906
{
slouken@1951
   907
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7522
   908
    SDL_DisplayData *displaydata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@1951
   909
    Display *display = data->videodata->display;
slouken@6336
   910
    XEvent event;
slouken@1951
   911
slouken@6483
   912
    if (X11_IsWindowMapped(_this, window)) {
icculus@7827
   913
        X11_XWithdrawWindow(display, data->xwindow, displaydata->screen);
slouken@6483
   914
        /* Blocking wait for "UnmapNotify" event */
icculus@7827
   915
        X11_XIfEvent(display, &event, &isUnmapNotify, (XPointer)&data->xwindow);
icculus@7827
   916
        X11_XFlush(display);
slouken@6483
   917
    }
slouken@1951
   918
}
slouken@1951
   919
slouken@7741
   920
static void
slouken@7741
   921
SetWindowActive(_THIS, SDL_Window * window)
slouken@7741
   922
{
slouken@7741
   923
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7741
   924
    SDL_DisplayData *displaydata =
slouken@7741
   925
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@7741
   926
    Display *display = data->videodata->display;
slouken@7741
   927
    Atom _NET_ACTIVE_WINDOW = data->videodata->_NET_ACTIVE_WINDOW;
slouken@7741
   928
slouken@7741
   929
    if (X11_IsWindowMapped(_this, window)) {
slouken@7741
   930
        XEvent e;
slouken@7741
   931
slouken@7741
   932
        SDL_zero(e);
slouken@7741
   933
        e.xany.type = ClientMessage;
slouken@7741
   934
        e.xclient.message_type = _NET_ACTIVE_WINDOW;
slouken@7741
   935
        e.xclient.format = 32;
slouken@7741
   936
        e.xclient.window = data->xwindow;
slouken@7741
   937
        e.xclient.data.l[0] = 1;  /* source indication. 1 = application */
slouken@7741
   938
        e.xclient.data.l[1] = CurrentTime;
slouken@7741
   939
        e.xclient.data.l[2] = 0;
slouken@7741
   940
icculus@7827
   941
        X11_XSendEvent(display, RootWindow(display, displaydata->screen), 0,
slouken@7741
   942
                   SubstructureNotifyMask | SubstructureRedirectMask, &e);
slouken@7741
   943
icculus@7827
   944
        X11_XFlush(display);
slouken@7741
   945
    }
slouken@7741
   946
}
slouken@7741
   947
slouken@1951
   948
void
slouken@1951
   949
X11_RaiseWindow(_THIS, SDL_Window * window)
slouken@1951
   950
{
slouken@1951
   951
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   952
    Display *display = data->videodata->display;
slouken@1951
   953
icculus@7827
   954
    X11_XRaiseWindow(display, data->xwindow);
slouken@7741
   955
    SetWindowActive(_this, window);
icculus@7827
   956
    X11_XFlush(display);
slouken@1951
   957
}
slouken@1951
   958
slouken@3077
   959
static void
slouken@5302
   960
SetWindowMaximized(_THIS, SDL_Window * window, SDL_bool maximized)
slouken@3077
   961
{
slouken@3077
   962
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@3077
   963
    SDL_DisplayData *displaydata =
slouken@5246
   964
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@3077
   965
    Display *display = data->videodata->display;
slouken@4518
   966
    Atom _NET_WM_STATE = data->videodata->_NET_WM_STATE;
slouken@4518
   967
    Atom _NET_WM_STATE_MAXIMIZED_VERT = data->videodata->_NET_WM_STATE_MAXIMIZED_VERT;
slouken@4518
   968
    Atom _NET_WM_STATE_MAXIMIZED_HORZ = data->videodata->_NET_WM_STATE_MAXIMIZED_HORZ;
slouken@3077
   969
icculus@7566
   970
    if (maximized) {
icculus@7566
   971
        window->flags |= SDL_WINDOW_MAXIMIZED;
icculus@7566
   972
    } else {
icculus@7566
   973
        window->flags &= ~SDL_WINDOW_MAXIMIZED;
icculus@7566
   974
    }
icculus@7566
   975
slouken@4522
   976
    if (X11_IsWindowMapped(_this, window)) {
slouken@4522
   977
        XEvent e;
slouken@3077
   978
slouken@4524
   979
        SDL_zero(e);
slouken@4522
   980
        e.xany.type = ClientMessage;
slouken@4522
   981
        e.xclient.message_type = _NET_WM_STATE;
slouken@4522
   982
        e.xclient.format = 32;
slouken@4522
   983
        e.xclient.window = data->xwindow;
slouken@4522
   984
        e.xclient.data.l[0] =
slouken@4522
   985
            maximized ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
slouken@4522
   986
        e.xclient.data.l[1] = _NET_WM_STATE_MAXIMIZED_VERT;
slouken@4522
   987
        e.xclient.data.l[2] = _NET_WM_STATE_MAXIMIZED_HORZ;
slouken@4522
   988
        e.xclient.data.l[3] = 0l;
slouken@4522
   989
icculus@7827
   990
        X11_XSendEvent(display, RootWindow(display, displaydata->screen), 0,
slouken@4522
   991
                   SubstructureNotifyMask | SubstructureRedirectMask, &e);
slouken@4522
   992
    } else {
icculus@7566
   993
        X11_SetNetWMState(_this, data->xwindow, window->flags);
slouken@4522
   994
    }
icculus@7827
   995
    X11_XFlush(display);
slouken@3077
   996
}
slouken@3077
   997
slouken@1951
   998
void
slouken@1951
   999
X11_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1951
  1000
{
slouken@5302
  1001
    SetWindowMaximized(_this, window, SDL_TRUE);
slouken@1951
  1002
}
slouken@1951
  1003
slouken@1951
  1004
void
slouken@1951
  1005
X11_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1951
  1006
{
slouken@4518
  1007
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@4518
  1008
    SDL_DisplayData *displaydata =
slouken@5246
  1009
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@4518
  1010
    Display *display = data->videodata->display;
slouken@7191
  1011
icculus@7827
  1012
    X11_XIconifyWindow(display, data->xwindow, displaydata->screen);
icculus@7827
  1013
    X11_XFlush(display);
slouken@1951
  1014
}
slouken@1951
  1015
slouken@1951
  1016
void
slouken@1951
  1017
X11_RestoreWindow(_THIS, SDL_Window * window)
slouken@1951
  1018
{
slouken@5302
  1019
    SetWindowMaximized(_this, window, SDL_FALSE);
slouken@7741
  1020
    X11_ShowWindow(_this, window);
slouken@7191
  1021
    SetWindowActive(_this, window);
slouken@1951
  1022
}
slouken@1951
  1023
icculus@6467
  1024
/* This asks the Window Manager to handle fullscreen for us. Most don't do it right, though. */
icculus@6467
  1025
static void
icculus@6467
  1026
X11_SetWindowFullscreenViaWM(_THIS, SDL_Window * window, SDL_VideoDisplay * _display, SDL_bool fullscreen)
slouken@5302
  1027
{
slouken@5302
  1028
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5305
  1029
    SDL_DisplayData *displaydata = (SDL_DisplayData *) _display->driverdata;
slouken@5302
  1030
    Display *display = data->videodata->display;
slouken@5302
  1031
    Atom _NET_WM_STATE = data->videodata->_NET_WM_STATE;
slouken@5302
  1032
    Atom _NET_WM_STATE_FULLSCREEN = data->videodata->_NET_WM_STATE_FULLSCREEN;
slouken@5302
  1033
slouken@5302
  1034
    if (X11_IsWindowMapped(_this, window)) {
slouken@5302
  1035
        XEvent e;
slouken@5302
  1036
slouken@6539
  1037
        if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
slouken@6539
  1038
            /* Compiz refuses fullscreen toggle if we're not resizable, so update the hints so we
slouken@6539
  1039
               can be resized to the fullscreen resolution (or reset so we're not resizable again) */
icculus@7827
  1040
            XSizeHints *sizehints = X11_XAllocSizeHints();
slouken@6539
  1041
            long flags = 0;
icculus@7827
  1042
            X11_XGetWMNormalHints(display, data->xwindow, sizehints, &flags);
slouken@6539
  1043
            /* set the resize flags on */
slouken@6539
  1044
            if (fullscreen) {
slouken@6539
  1045
                /* we are going fullscreen so turn the flags off */
slouken@6539
  1046
                sizehints->flags &= ~(PMinSize | PMaxSize);
slouken@6539
  1047
            } else {
slouken@6539
  1048
                /* Reset the min/max width height to make the window non-resizable again */
icculus@6466
  1049
                sizehints->flags |= PMinSize | PMaxSize;
slouken@6540
  1050
                sizehints->min_width = sizehints->max_width = window->windowed.w;
slouken@6540
  1051
                sizehints->min_height = sizehints->max_height = window->windowed.h;
icculus@6466
  1052
            }
icculus@7827
  1053
            X11_XSetWMNormalHints(display, data->xwindow, sizehints);
icculus@7827
  1054
            X11_XFree(sizehints);
icculus@6466
  1055
        }
slouken@6539
  1056
slouken@5302
  1057
        SDL_zero(e);
slouken@5302
  1058
        e.xany.type = ClientMessage;
slouken@5302
  1059
        e.xclient.message_type = _NET_WM_STATE;
slouken@5302
  1060
        e.xclient.format = 32;
slouken@5302
  1061
        e.xclient.window = data->xwindow;
slouken@5302
  1062
        e.xclient.data.l[0] =
slouken@5302
  1063
            fullscreen ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
slouken@5302
  1064
        e.xclient.data.l[1] = _NET_WM_STATE_FULLSCREEN;
slouken@5302
  1065
        e.xclient.data.l[3] = 0l;
slouken@5302
  1066
icculus@7827
  1067
        X11_XSendEvent(display, RootWindow(display, displaydata->screen), 0,
slouken@5302
  1068
                   SubstructureNotifyMask | SubstructureRedirectMask, &e);
slouken@5302
  1069
    } else {
slouken@6481
  1070
        Uint32 flags;
slouken@5302
  1071
slouken@6481
  1072
        flags = window->flags;
slouken@5302
  1073
        if (fullscreen) {
slouken@6481
  1074
            flags |= SDL_WINDOW_FULLSCREEN;
slouken@6481
  1075
        } else {
slouken@6481
  1076
            flags &= ~SDL_WINDOW_FULLSCREEN;
slouken@5302
  1077
        }
slouken@6487
  1078
        X11_SetNetWMState(_this, data->xwindow, flags);
slouken@5302
  1079
    }
mikesart@6568
  1080
slouken@6575
  1081
    if (data->visual->class == DirectColor) {
slouken@6575
  1082
        if ( fullscreen ) {
icculus@7827
  1083
            X11_XInstallColormap(display, data->colormap);
slouken@6575
  1084
        } else {
icculus@7827
  1085
            X11_XUninstallColormap(display, data->colormap);
slouken@6575
  1086
        }
slouken@6575
  1087
    }
mikesart@6568
  1088
icculus@7827
  1089
    X11_XFlush(display);
slouken@5302
  1090
}
slouken@5302
  1091
icculus@6467
  1092
/* This handles fullscreen itself, outside the Window Manager. */
icculus@6467
  1093
static void
icculus@6467
  1094
X11_BeginWindowFullscreenLegacy(_THIS, SDL_Window * window, SDL_VideoDisplay * _display)
icculus@6467
  1095
{
icculus@6467
  1096
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@6467
  1097
    SDL_DisplayData *displaydata = (SDL_DisplayData *) _display->driverdata;
icculus@6467
  1098
    Visual *visual = data->visual;
icculus@6467
  1099
    Display *display = data->videodata->display;
icculus@6467
  1100
    const int screen = displaydata->screen;
icculus@6467
  1101
    Window root = RootWindow(display, screen);
icculus@6467
  1102
    const int def_vis = (visual == DefaultVisual(display, screen));
icculus@6467
  1103
    unsigned long xattrmask = 0;
icculus@6467
  1104
    XSetWindowAttributes xattr;
icculus@6467
  1105
    XEvent ev;
slouken@6475
  1106
    SDL_Rect rect;
icculus@6467
  1107
icculus@6467
  1108
    if ( data->fswindow ) {
icculus@6467
  1109
        return;  /* already fullscreen, I hope. */
icculus@6467
  1110
    }
icculus@6467
  1111
slouken@6475
  1112
    X11_GetDisplayBounds(_this, _display, &rect);
slouken@6475
  1113
icculus@6467
  1114
    SDL_zero(xattr);
icculus@6467
  1115
    xattr.override_redirect = True;
icculus@6467
  1116
    xattrmask |= CWOverrideRedirect;
icculus@6467
  1117
    xattr.background_pixel = def_vis ? BlackPixel(display, screen) : 0;
icculus@6467
  1118
    xattrmask |= CWBackPixel;
icculus@6467
  1119
    xattr.border_pixel = 0;
icculus@6467
  1120
    xattrmask |= CWBorderPixel;
icculus@6467
  1121
    xattr.colormap = data->colormap;
icculus@6467
  1122
    xattrmask |= CWColormap;
icculus@6467
  1123
icculus@7827
  1124
    data->fswindow = X11_XCreateWindow(display, root,
slouken@6475
  1125
                                   rect.x, rect.y, rect.w, rect.h, 0,
icculus@6467
  1126
                                   displaydata->depth, InputOutput,
icculus@6467
  1127
                                   visual, xattrmask, &xattr);
icculus@6467
  1128
icculus@7827
  1129
    X11_XSelectInput(display, data->fswindow, StructureNotifyMask);
icculus@7827
  1130
    X11_XSetWindowBackground(display, data->fswindow, 0);
icculus@7827
  1131
    X11_XInstallColormap(display, data->colormap);
icculus@7827
  1132
    X11_XClearWindow(display, data->fswindow);
icculus@7827
  1133
    X11_XMapRaised(display, data->fswindow);
icculus@6467
  1134
slouken@6480
  1135
    /* Make sure the fswindow is in view by warping mouse to the corner */
icculus@7827
  1136
    X11_XUngrabPointer(display, CurrentTime);
icculus@7827
  1137
    X11_XWarpPointer(display, None, root, 0, 0, 0, 0, rect.x, rect.y);
icculus@6467
  1138
icculus@6467
  1139
    /* Wait to be mapped, filter Unmap event out if it arrives. */
icculus@7827
  1140
    X11_XIfEvent(display, &ev, &isMapNotify, (XPointer)&data->fswindow);
icculus@7827
  1141
    X11_XCheckIfEvent(display, &ev, &isUnmapNotify, (XPointer)&data->fswindow);
icculus@6467
  1142
icculus@6467
  1143
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
icculus@6467
  1144
    if ( displaydata->use_vidmode ) {
icculus@7827
  1145
        X11_XF86VidModeLockModeSwitch(display, screen, True);
icculus@6467
  1146
    }
icculus@6467
  1147
#endif
icculus@6467
  1148
icculus@6467
  1149
    SetWindowBordered(display, displaydata->screen, data->xwindow, SDL_FALSE);
icculus@6467
  1150
icculus@6467
  1151
    /* Center actual window within our cover-the-screen window. */
icculus@7827
  1152
    X11_XReparentWindow(display, data->xwindow, data->fswindow,
slouken@6501
  1153
                    (rect.w - window->w) / 2, (rect.h - window->h) / 2);
icculus@6467
  1154
slouken@7632
  1155
    /* Move the mouse to the upper left to make sure it's on-screen */
icculus@7827
  1156
    X11_XWarpPointer(display, None, root, 0, 0, 0, 0, rect.x, rect.y);
slouken@7632
  1157
slouken@6501
  1158
    /* Center mouse in the fullscreen window. */
slouken@6501
  1159
    rect.x += (rect.w / 2);
slouken@6501
  1160
    rect.y += (rect.h / 2);
icculus@7827
  1161
    X11_XWarpPointer(display, None, root, 0, 0, 0, 0, rect.x, rect.y);
icculus@6467
  1162
icculus@6467
  1163
    /* Wait to be mapped, filter Unmap event out if it arrives. */
icculus@7827
  1164
    X11_XIfEvent(display, &ev, &isMapNotify, (XPointer)&data->xwindow);
icculus@7827
  1165
    X11_XCheckIfEvent(display, &ev, &isUnmapNotify, (XPointer)&data->xwindow);
icculus@6467
  1166
slouken@6662
  1167
    SDL_UpdateWindowGrab(window);
icculus@6467
  1168
}
icculus@6467
  1169
icculus@6467
  1170
static void
icculus@6467
  1171
X11_EndWindowFullscreenLegacy(_THIS, SDL_Window * window, SDL_VideoDisplay * _display)
icculus@6467
  1172
{
icculus@6467
  1173
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@6467
  1174
    SDL_DisplayData *displaydata = (SDL_DisplayData *) _display->driverdata;
icculus@6467
  1175
    Display *display = data->videodata->display;
icculus@6467
  1176
    const int screen = displaydata->screen;
icculus@6467
  1177
    Window root = RootWindow(display, screen);
slouken@6480
  1178
    Window fswindow = data->fswindow;
icculus@6467
  1179
    XEvent ev;
icculus@6467
  1180
slouken@6480
  1181
    if (!data->fswindow) {
icculus@6467
  1182
        return;  /* already not fullscreen, I hope. */
slouken@6480
  1183
    }
icculus@6467
  1184
slouken@6480
  1185
    data->fswindow = None;
icculus@6467
  1186
icculus@6467
  1187
#if SDL_VIDEO_DRIVER_X11_VIDMODE
icculus@6467
  1188
    if ( displaydata->use_vidmode ) {
icculus@7827
  1189
        X11_XF86VidModeLockModeSwitch(display, screen, False);
icculus@6467
  1190
    }
icculus@6467
  1191
#endif
icculus@6467
  1192
slouken@6662
  1193
    SDL_UpdateWindowGrab(window);
icculus@6467
  1194
icculus@7827
  1195
    X11_XReparentWindow(display, data->xwindow, root, window->x, window->y);
slouken@6480
  1196
slouken@6480
  1197
    /* flush these events so they don't confuse normal event handling */
icculus@7827
  1198
    X11_XSync(display, False);
icculus@7827
  1199
    X11_XCheckIfEvent(display, &ev, &isMapNotify, (XPointer)&data->xwindow);
icculus@7827
  1200
    X11_XCheckIfEvent(display, &ev, &isUnmapNotify, (XPointer)&data->xwindow);
icculus@6467
  1201
icculus@6467
  1202
    SetWindowBordered(display, screen, data->xwindow,
icculus@6467
  1203
                      (window->flags & SDL_WINDOW_BORDERLESS) == 0);
icculus@6467
  1204
icculus@7827
  1205
    X11_XWithdrawWindow(display, fswindow, screen);
slouken@6480
  1206
slouken@6480
  1207
    /* Wait to be unmapped. */
icculus@7827
  1208
    X11_XIfEvent(display, &ev, &isUnmapNotify, (XPointer)&fswindow);
icculus@7827
  1209
    X11_XDestroyWindow(display, fswindow);
icculus@6467
  1210
}
icculus@6467
  1211
icculus@6467
  1212
icculus@6467
  1213
void
icculus@6467
  1214
X11_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * _display, SDL_bool fullscreen)
icculus@6467
  1215
{
icculus@6467
  1216
    /* !!! FIXME: SDL_Hint? */
icculus@6467
  1217
    SDL_bool legacy = SDL_FALSE;
icculus@6467
  1218
    const char *env = SDL_getenv("SDL_VIDEO_X11_LEGACY_FULLSCREEN");
icculus@6467
  1219
    if (env) {
icculus@6467
  1220
        legacy = SDL_atoi(env);
icculus@6467
  1221
    } else {
slouken@6473
  1222
        SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
icculus@6467
  1223
        SDL_DisplayData *displaydata = (SDL_DisplayData *) _display->driverdata;
icculus@6467
  1224
        if ( displaydata->use_vidmode ) {
icculus@6467
  1225
            legacy = SDL_TRUE;  /* the new stuff only works with XRandR. */
slouken@6473
  1226
        } else if ( !videodata->net_wm ) {
slouken@6473
  1227
            legacy = SDL_TRUE;  /* The window manager doesn't support it */
icculus@6467
  1228
        } else {
icculus@6467
  1229
            /* !!! FIXME: look at the window manager name, and blacklist certain ones? */
icculus@6467
  1230
            /* http://stackoverflow.com/questions/758648/find-the-name-of-the-x-window-manager */
icculus@6467
  1231
            legacy = SDL_FALSE;  /* try the new way. */
icculus@6467
  1232
        }
icculus@6467
  1233
    }
icculus@6467
  1234
icculus@6467
  1235
    if (legacy) {
icculus@6467
  1236
        if (fullscreen) {
icculus@6467
  1237
            X11_BeginWindowFullscreenLegacy(_this, window, _display);
icculus@6467
  1238
        } else {
icculus@6467
  1239
            X11_EndWindowFullscreenLegacy(_this, window, _display);
icculus@6467
  1240
        }
icculus@6467
  1241
    } else {
icculus@6467
  1242
        X11_SetWindowFullscreenViaWM(_this, window, _display, fullscreen);
icculus@6467
  1243
    }
icculus@6467
  1244
}
icculus@6467
  1245
icculus@6467
  1246
slouken@5466
  1247
int
slouken@5466
  1248
X11_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
slouken@5466
  1249
{
slouken@5466
  1250
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5466
  1251
    Display *display = data->videodata->display;
slouken@5466
  1252
    Visual *visual = data->visual;
slouken@5466
  1253
    Colormap colormap = data->colormap;
slouken@5466
  1254
    XColor *colorcells;
slouken@5466
  1255
    int ncolors;
slouken@5466
  1256
    int rmask, gmask, bmask;
slouken@5466
  1257
    int rshift, gshift, bshift;
icculus@5981
  1258
    int i;
slouken@5466
  1259
slouken@5466
  1260
    if (visual->class != DirectColor) {
icculus@7037
  1261
        return SDL_SetError("Window doesn't have DirectColor visual");
slouken@5466
  1262
    }
slouken@5466
  1263
slouken@5466
  1264
    ncolors = visual->map_entries;
slouken@5466
  1265
    colorcells = SDL_malloc(ncolors * sizeof(XColor));
slouken@5466
  1266
    if (!colorcells) {
icculus@7037
  1267
        return SDL_OutOfMemory();
slouken@5466
  1268
    }
slouken@5466
  1269
slouken@5466
  1270
    rshift = 0;
slouken@5466
  1271
    rmask = visual->red_mask;
slouken@5466
  1272
    while (0 == (rmask & 1)) {
slouken@5466
  1273
        rshift++;
slouken@5466
  1274
        rmask >>= 1;
slouken@5466
  1275
    }
slouken@5466
  1276
slouken@5466
  1277
    gshift = 0;
slouken@5466
  1278
    gmask = visual->green_mask;
slouken@5466
  1279
    while (0 == (gmask & 1)) {
slouken@5466
  1280
        gshift++;
slouken@5466
  1281
        gmask >>= 1;
slouken@5466
  1282
    }
slouken@5466
  1283
slouken@5466
  1284
    bshift = 0;
slouken@5466
  1285
    bmask = visual->blue_mask;
slouken@5466
  1286
    while (0 == (bmask & 1)) {
slouken@5466
  1287
        bshift++;
slouken@5466
  1288
        bmask >>= 1;
slouken@5466
  1289
    }
slouken@5466
  1290
slouken@5466
  1291
    /* build the color table pixel values */
slouken@5466
  1292
    for (i = 0; i < ncolors; i++) {
slouken@5466
  1293
        Uint32 rbits = (rmask * i) / (ncolors - 1);
slouken@5466
  1294
        Uint32 gbits = (gmask * i) / (ncolors - 1);
slouken@5466
  1295
        Uint32 bbits = (bmask * i) / (ncolors - 1);
slouken@5466
  1296
        Uint32 pix = (rbits << rshift) | (gbits << gshift) | (bbits << bshift);
slouken@5466
  1297
slouken@5466
  1298
        colorcells[i].pixel = pix;
slouken@5466
  1299
slouken@5466
  1300
        colorcells[i].red = ramp[(0 * 256) + i];
slouken@5466
  1301
        colorcells[i].green = ramp[(1 * 256) + i];
slouken@5466
  1302
        colorcells[i].blue = ramp[(2 * 256) + i];
slouken@5466
  1303
slouken@5466
  1304
        colorcells[i].flags = DoRed | DoGreen | DoBlue;
slouken@5466
  1305
    }
slouken@5466
  1306
icculus@7827
  1307
    X11_XStoreColors(display, colormap, colorcells, ncolors);
icculus@7827
  1308
    X11_XFlush(display);
slouken@5466
  1309
    SDL_free(colorcells);
slouken@5466
  1310
slouken@5466
  1311
    return 0;
slouken@5466
  1312
}
slouken@5466
  1313
slouken@5302
  1314
void
slouken@6662
  1315
X11_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
slouken@1951
  1316
{
slouken@2875
  1317
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@2875
  1318
    Display *display = data->videodata->display;
slouken@4518
  1319
    SDL_bool oldstyle_fullscreen;
slouken@6662
  1320
    SDL_bool grab_keyboard;
slouken@6662
  1321
    const char *hint;
slouken@2875
  1322
slouken@6662
  1323
    /* ICCCM2.0-compliant window managers can handle fullscreen windows
slouken@6662
  1324
       If we're using XVidMode to change resolution we need to confine
slouken@6662
  1325
       the cursor so we don't pan around the virtual desktop.
slouken@6662
  1326
     */
icculus@6467
  1327
    oldstyle_fullscreen = X11_IsWindowLegacyFullscreen(_this, window);
slouken@4518
  1328
slouken@6662
  1329
    if (oldstyle_fullscreen || grabbed) {
slouken@2875
  1330
        /* Try to grab the mouse */
slouken@2876
  1331
        for (;;) {
vluchits@9614
  1332
            const unsigned int mask = ButtonPressMask | ButtonReleaseMask 
vluchits@9614
  1333
                | PointerMotionMask | FocusChangeMask;
slouken@2876
  1334
            int result =
vluchits@9614
  1335
                X11_XGrabPointer(display, data->xwindow, False, mask, 
vluchits@9614
  1336
                             GrabModeAsync, GrabModeAsync, data->xwindow, 
vluchits@9614
  1337
                             None, CurrentTime);
slouken@2876
  1338
            if (result == GrabSuccess) {
slouken@2875
  1339
                break;
slouken@2875
  1340
            }
slouken@6662
  1341
            SDL_Delay(50);
slouken@2875
  1342
        }
slouken@2875
  1343
slouken@2875
  1344
        /* Raise the window if we grab the mouse */
icculus@7827
  1345
        X11_XRaiseWindow(display, data->xwindow);
slouken@2875
  1346
slouken@2875
  1347
        /* Now grab the keyboard */
slouken@6662
  1348
        hint = SDL_GetHint(SDL_HINT_GRAB_KEYBOARD);
slouken@6662
  1349
        if (hint && SDL_atoi(hint)) {
slouken@6662
  1350
            grab_keyboard = SDL_TRUE;
slouken@6662
  1351
        } else {
slouken@6662
  1352
            /* We need to do this with the old style override_redirect
slouken@6662
  1353
               fullscreen window otherwise we won't get keyboard focus.
slouken@6662
  1354
            */
slouken@6662
  1355
            grab_keyboard = oldstyle_fullscreen;
slouken@6662
  1356
        }
slouken@6662
  1357
        if (grab_keyboard) {
icculus@7827
  1358
            X11_XGrabKeyboard(display, data->xwindow, True, GrabModeAsync,
slouken@6662
  1359
                          GrabModeAsync, CurrentTime);
slouken@6662
  1360
        }
slouken@2875
  1361
    } else {
icculus@7827
  1362
        X11_XUngrabPointer(display, CurrentTime);
icculus@7827
  1363
        X11_XUngrabKeyboard(display, CurrentTime);
slouken@2875
  1364
    }
icculus@7827
  1365
    X11_XSync(display, False);
slouken@1951
  1366
}
slouken@1951
  1367
slouken@1951
  1368
void
slouken@1951
  1369
X11_DestroyWindow(_THIS, SDL_Window * window)
slouken@1951
  1370
{
slouken@1951
  1371
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@8905
  1372
slouken@1951
  1373
    if (data) {
bob@2323
  1374
        SDL_VideoData *videodata = (SDL_VideoData *) data->videodata;
bob@2323
  1375
        Display *display = videodata->display;
bob@2323
  1376
        int numwindows = videodata->numwindows;
bob@2323
  1377
        SDL_WindowData **windowlist = videodata->windowlist;
bob@2323
  1378
        int i;
bob@2323
  1379
bob@2323
  1380
        if (windowlist) {
bob@2324
  1381
            for (i = 0; i < numwindows; ++i) {
slouken@3685
  1382
                if (windowlist[i] && (windowlist[i]->window == window)) {
bob@2324
  1383
                    windowlist[i] = windowlist[numwindows - 1];
bob@2324
  1384
                    windowlist[numwindows - 1] = NULL;
bob@2324
  1385
                    videodata->numwindows--;
bob@2324
  1386
                    break;
bob@2324
  1387
                }
bob@2323
  1388
            }
bob@2323
  1389
        }
slouken@1951
  1390
#ifdef X_HAVE_UTF8_STRING
slouken@1951
  1391
        if (data->ic) {
icculus@7827
  1392
            X11_XDestroyIC(data->ic);
slouken@1951
  1393
        }
slouken@1951
  1394
#endif
slouken@1951
  1395
        if (data->created) {
icculus@7827
  1396
            X11_XDestroyWindow(display, data->xwindow);
icculus@7827
  1397
            X11_XFlush(display);
slouken@1951
  1398
        }
slouken@1951
  1399
        SDL_free(data);
slouken@1951
  1400
    }
slouken@8978
  1401
    window->driverdata = NULL;
slouken@1951
  1402
}
slouken@1951
  1403
slouken@1951
  1404
SDL_bool
slouken@1951
  1405
X11_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1951
  1406
{
slouken@4510
  1407
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@4510
  1408
    Display *display = data->videodata->display;
slouken@4510
  1409
slouken@4510
  1410
    if (info->version.major == SDL_MAJOR_VERSION &&
slouken@4510
  1411
        info->version.minor == SDL_MINOR_VERSION) {
slouken@4510
  1412
        info->subsystem = SDL_SYSWM_X11;
slouken@5056
  1413
        info->info.x11.display = display;
slouken@5056
  1414
        info->info.x11.window = data->xwindow;
slouken@1951
  1415
        return SDL_TRUE;
slouken@1951
  1416
    } else {
slouken@1951
  1417
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1951
  1418
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1951
  1419
        return SDL_FALSE;
slouken@1951
  1420
    }
slouken@1951
  1421
}
slouken@1951
  1422
rush@8932
  1423
int
icculus@8935
  1424
X11_SetWindowHitTest(SDL_Window *window, SDL_bool enabled)
rush@8932
  1425
{
icculus@8935
  1426
    return 0;  /* just succeed, the real work is done elsewhere. */
rush@8932
  1427
}
rush@8932
  1428
slouken@5481
  1429
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
  1430
slouken@1951
  1431
/* vi: set ts=4 sw=4 expandtab: */