src/video/x11/SDL_x11window.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 14 Aug 2017 10:28:47 -0700
changeset 11288 47a5a4999180
parent 11151 7cfe088fafba
child 11334 749cb40916f2
permissions -rw-r--r--
Fixed bug 2500 - X11: SDL tries (and fails) to hide foreign windows

Alvin

I'm interested in this bug as well. I have experienced it when trying to embed an SDL_Window into a FLTK application. To do this, I create a FLTK window (window inside a window - think video player) and then use SDL_CreateWindowFrom() on the inner most window's Xlib Window*. After which, I create a renderer.

In my situation I am using the FLTK GUI toolkit.

What I have experienced is that the SDL_CreateRender() will recreate the window in order to properly setup OpenGL capability. As part of this process, the window is hidden and a call is executed that waits indefinitely for an acknowledgement that the window was indeed unmapped. This is where my program hangs.

Please correct me if I am wrong, but should SDL2 not make Xlib calls that effect the Xlib Window in this situation (e.g. When SDL_CreateWindowFrom() is used)? The toolkit being used typically assumes responsibility and, I presume, tracks all Xlib Windows it creates.

On line src/video/SDL_video.c:1372 the comment associated with setting SDL_WINDOW_FOREIGN reads:

/* Can't destroy and re-create foreign windows, hrm */

Since I do not know the reason for hiding the window in the first place, the attached patch simply does not wait for a response when X11_XWithdrawWindow() and X11_XMapRaised() are issued by X11_HideWindow() and X11_ShowWindow(), respectively. I presume that the GUI toolkit (GTK, FLTK, etc.) has or will consume the acknowledging event as it is managing the Xlib Window (or it thinks it is).

I have tested the patch against hg d09d4b578e77 and I have successfully tested:
* Embedding the SDL_Window inside a FLTK application.
* Calling SDL_SetWindowSize() when FLTK resizes the window (e.g. dragging cursor on the edge of the window).
* Filling the renderer's default target blue and drawing a red fill square at the centre (exciting, I know!)
* Calling SDL_Quit() when the application terminates

I do not receive any Xlib erorr messages (BadWindow, etc.) in any of those situations.
slouken@1951
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 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@10723
    43
#include "SDL_log.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
icculus@6467
    48
static Bool isMapNotify(Display *dpy, XEvent *ev, XPointer win)
icculus@6467
    49
{
icculus@6467
    50
    return ev->type == MapNotify && ev->xmap.window == *((Window*)win);
icculus@6467
    51
}
icculus@6467
    52
static Bool isUnmapNotify(Display *dpy, XEvent *ev, XPointer win)
icculus@6467
    53
{
icculus@6467
    54
    return ev->type == UnmapNotify && ev->xunmap.window == *((Window*)win);
icculus@6467
    55
}
icculus@9838
    56
icculus@9838
    57
/*
icculus@6467
    58
static Bool isConfigureNotify(Display *dpy, XEvent *ev, XPointer win)
icculus@6467
    59
{
slouken@6480
    60
    return ev->type == ConfigureNotify && ev->xconfigure.window == *((Window*)win);
slouken@6480
    61
}
slouken@7632
    62
static Bool
icculus@7827
    63
X11_XIfEventTimeout(Display *display, XEvent *event_return, Bool (*predicate)(), XPointer arg, int timeoutMS)
slouken@7632
    64
{
slouken@7632
    65
    Uint32 start = SDL_GetTicks();
slouken@7632
    66
icculus@7827
    67
    while (!X11_XCheckIfEvent(display, event_return, predicate, arg)) {
slouken@7857
    68
        if (SDL_TICKS_PASSED(SDL_GetTicks(), start + timeoutMS)) {
slouken@7632
    69
            return False;
slouken@7632
    70
        }
slouken@7632
    71
    }
slouken@7632
    72
    return True;
slouken@7632
    73
}
slouken@7632
    74
*/
slouken@7632
    75
slouken@4518
    76
static SDL_bool
icculus@6467
    77
X11_IsWindowLegacyFullscreen(_THIS, SDL_Window * window)
slouken@4518
    78
{
icculus@6467
    79
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@6467
    80
    return (data->fswindow != 0);
slouken@4518
    81
}
slouken@4518
    82
slouken@4522
    83
static SDL_bool
slouken@4522
    84
X11_IsWindowMapped(_THIS, SDL_Window * window)
slouken@4522
    85
{
slouken@4522
    86
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@4522
    87
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@4522
    88
    XWindowAttributes attr;
slouken@4522
    89
icculus@7827
    90
    X11_XGetWindowAttributes(videodata->display, data->xwindow, &attr);
slouken@4522
    91
    if (attr.map_state != IsUnmapped) {
slouken@4522
    92
        return SDL_TRUE;
slouken@4522
    93
    } else {
slouken@4522
    94
        return SDL_FALSE;
slouken@4522
    95
    }
slouken@4522
    96
}
slouken@4522
    97
slouken@6540
    98
#if 0
slouken@6481
    99
static SDL_bool
slouken@6481
   100
X11_IsActionAllowed(SDL_Window *window, Atom action)
slouken@4522
   101
{
slouken@6481
   102
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@6481
   103
    Atom _NET_WM_ALLOWED_ACTIONS = data->videodata->_NET_WM_ALLOWED_ACTIONS;
slouken@6481
   104
    Atom type;
slouken@6481
   105
    Display *display = data->videodata->display;
slouken@6481
   106
    int form;
slouken@6481
   107
    unsigned long remain;
slouken@6481
   108
    unsigned long len, i;
slouken@6481
   109
    Atom *list;
slouken@6481
   110
    SDL_bool ret = SDL_FALSE;
slouken@6481
   111
icculus@7827
   112
    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
   113
    {
slouken@6481
   114
        for (i=0; i<len; ++i)
slouken@6481
   115
        {
slouken@6481
   116
            if (list[i] == action) {
slouken@6481
   117
                ret = SDL_TRUE;
slouken@6481
   118
                break;
slouken@6481
   119
            }
slouken@6481
   120
        }
icculus@7827
   121
        X11_XFree(list);
slouken@6481
   122
    }
slouken@6481
   123
    return ret;
slouken@6481
   124
}
slouken@6540
   125
#endif /* 0 */
slouken@6481
   126
slouken@6487
   127
void
slouken@6487
   128
X11_SetNetWMState(_THIS, Window xwindow, Uint32 flags)
slouken@6481
   129
{
slouken@6481
   130
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@6487
   131
    Display *display = videodata->display;
icculus@10010
   132
    /* !!! FIXME: just dereference videodata below instead of copying to locals. */
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;
icculus@10021
   139
    Atom _NET_WM_STATE_ABOVE = videodata->_NET_WM_STATE_ABOVE;
icculus@10021
   140
    Atom _NET_WM_STATE_SKIP_TASKBAR = videodata->_NET_WM_STATE_SKIP_TASKBAR;
icculus@10021
   141
    Atom _NET_WM_STATE_SKIP_PAGER = videodata->_NET_WM_STATE_SKIP_PAGER;
icculus@10021
   142
    Atom atoms[16];
slouken@4522
   143
    int count = 0;
slouken@4522
   144
slouken@6486
   145
    /* The window manager sets this property, we shouldn't set it.
slouken@6486
   146
       If we did, this would indicate to the window manager that we don't
icculus@7827
   147
       actually want to be mapped during X11_XMapRaised(), which would be bad.
slouken@6486
   148
     *
slouken@6481
   149
    if (flags & SDL_WINDOW_HIDDEN) {
slouken@6481
   150
        atoms[count++] = _NET_WM_STATE_HIDDEN;
slouken@4522
   151
    }
slouken@6486
   152
    */
icculus@10021
   153
icculus@10021
   154
    if (flags & SDL_WINDOW_ALWAYS_ON_TOP) {
icculus@10021
   155
        atoms[count++] = _NET_WM_STATE_ABOVE;
icculus@10021
   156
    }
icculus@10021
   157
    if (flags & SDL_WINDOW_SKIP_TASKBAR) {
icculus@10021
   158
        atoms[count++] = _NET_WM_STATE_SKIP_TASKBAR;
icculus@10021
   159
        atoms[count++] = _NET_WM_STATE_SKIP_PAGER;
icculus@10021
   160
    }
slouken@6481
   161
    if (flags & SDL_WINDOW_INPUT_FOCUS) {
slouken@6481
   162
        atoms[count++] = _NET_WM_STATE_FOCUSED;
slouken@6481
   163
    }
slouken@6481
   164
    if (flags & SDL_WINDOW_MAXIMIZED) {
slouken@6481
   165
        atoms[count++] = _NET_WM_STATE_MAXIMIZED_VERT;
slouken@6481
   166
        atoms[count++] = _NET_WM_STATE_MAXIMIZED_HORZ;
slouken@6481
   167
    }
slouken@6481
   168
    if (flags & SDL_WINDOW_FULLSCREEN) {
slouken@6481
   169
        atoms[count++] = _NET_WM_STATE_FULLSCREEN;
slouken@4522
   170
    }
icculus@10021
   171
icculus@10021
   172
    SDL_assert(count <= SDL_arraysize(atoms));
icculus@10021
   173
slouken@6487
   174
    if (count > 0) {
icculus@7827
   175
        X11_XChangeProperty(display, xwindow, _NET_WM_STATE, XA_ATOM, 32,
slouken@6487
   176
                        PropModeReplace, (unsigned char *)atoms, count);
slouken@6487
   177
    } else {
icculus@7827
   178
        X11_XDeleteProperty(display, xwindow, _NET_WM_STATE);
slouken@6487
   179
    }
slouken@4522
   180
}
slouken@4522
   181
slouken@6481
   182
Uint32
slouken@6487
   183
X11_GetNetWMState(_THIS, Window xwindow)
slouken@6481
   184
{
slouken@6481
   185
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@6487
   186
    Display *display = videodata->display;
slouken@6481
   187
    Atom _NET_WM_STATE = videodata->_NET_WM_STATE;
slouken@6481
   188
    Atom _NET_WM_STATE_HIDDEN = videodata->_NET_WM_STATE_HIDDEN;
slouken@6481
   189
    Atom _NET_WM_STATE_FOCUSED = videodata->_NET_WM_STATE_FOCUSED;
slouken@6481
   190
    Atom _NET_WM_STATE_MAXIMIZED_VERT = videodata->_NET_WM_STATE_MAXIMIZED_VERT;
slouken@6481
   191
    Atom _NET_WM_STATE_MAXIMIZED_HORZ = videodata->_NET_WM_STATE_MAXIMIZED_HORZ;
slouken@6481
   192
    Atom _NET_WM_STATE_FULLSCREEN = videodata->_NET_WM_STATE_FULLSCREEN;
slouken@6481
   193
    Atom actualType;
slouken@6481
   194
    int actualFormat;
slouken@6481
   195
    unsigned long i, numItems, bytesAfter;
slouken@6481
   196
    unsigned char *propertyValue = NULL;
slouken@6481
   197
    long maxLength = 1024;
slouken@6481
   198
    Uint32 flags = 0;
slouken@6481
   199
icculus@7827
   200
    if (X11_XGetWindowProperty(display, xwindow, _NET_WM_STATE,
slouken@6481
   201
                           0l, maxLength, False, XA_ATOM, &actualType,
slouken@6481
   202
                           &actualFormat, &numItems, &bytesAfter,
slouken@6481
   203
                           &propertyValue) == Success) {
slouken@6481
   204
        Atom *atoms = (Atom *) propertyValue;
slouken@6481
   205
        int maximized = 0;
slouken@6481
   206
        int fullscreen = 0;
slouken@6481
   207
slouken@6481
   208
        for (i = 0; i < numItems; ++i) {
slouken@6481
   209
            if (atoms[i] == _NET_WM_STATE_HIDDEN) {
slouken@6486
   210
                flags |= SDL_WINDOW_HIDDEN;
slouken@6481
   211
            } else if (atoms[i] == _NET_WM_STATE_FOCUSED) {
slouken@6481
   212
                flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@6481
   213
            } else if (atoms[i] == _NET_WM_STATE_MAXIMIZED_VERT) {
slouken@6481
   214
                maximized |= 1;
slouken@6481
   215
            } else if (atoms[i] == _NET_WM_STATE_MAXIMIZED_HORZ) {
slouken@6481
   216
                maximized |= 2;
slouken@6481
   217
            } else if ( atoms[i] == _NET_WM_STATE_FULLSCREEN) {
slouken@6481
   218
                fullscreen = 1;
slouken@6481
   219
            }
slouken@6481
   220
        }
slouken@6481
   221
        if (maximized == 3) {
slouken@6481
   222
            flags |= SDL_WINDOW_MAXIMIZED;
icculus@10017
   223
        }
icculus@10017
   224
icculus@10017
   225
        if (fullscreen == 1) {
slouken@6481
   226
            flags |= SDL_WINDOW_FULLSCREEN;
slouken@6481
   227
        }
slouken@11151
   228
slouken@11151
   229
        /* If the window is unmapped, numItems will be zero and _NET_WM_STATE_HIDDEN
slouken@11151
   230
         * will not be set. Do an additional check to see if the window is unmapped
slouken@11151
   231
         * and mark it as SDL_WINDOW_HIDDEN if it is.
slouken@11151
   232
         */
slouken@11151
   233
        {
slouken@11151
   234
            XWindowAttributes attr;
slouken@11151
   235
            SDL_memset(&attr,0,sizeof(attr));
slouken@11151
   236
            X11_XGetWindowAttributes(videodata->display, xwindow, &attr);
slouken@11151
   237
            if (attr.map_state == IsUnmapped) {
slouken@11151
   238
                flags |= SDL_WINDOW_HIDDEN;
slouken@11151
   239
            }
slouken@11151
   240
        }
icculus@7827
   241
        X11_XFree(propertyValue);
slouken@6481
   242
    }
slouken@6481
   243
slouken@6484
   244
    /* FIXME, check the size hints for resizable */
gabomdq@7678
   245
    /* flags |= SDL_WINDOW_RESIZABLE; */
slouken@6481
   246
slouken@6481
   247
    return flags;
slouken@6481
   248
}
slouken@6481
   249
slouken@1951
   250
static int
slouken@1951
   251
SetupWindowData(_THIS, SDL_Window * window, Window w, BOOL created)
slouken@1951
   252
{
slouken@1951
   253
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1951
   254
    SDL_WindowData *data;
slouken@1951
   255
    int numwindows = videodata->numwindows;
bob@2324
   256
    int windowlistlength = videodata->windowlistlength;
slouken@1951
   257
    SDL_WindowData **windowlist = videodata->windowlist;
slouken@1951
   258
slouken@1951
   259
    /* Allocate the window data */
bob@2323
   260
    data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
slouken@1951
   261
    if (!data) {
icculus@7037
   262
        return SDL_OutOfMemory();
slouken@1951
   263
    }
slouken@3685
   264
    data->window = window;
slouken@3685
   265
    data->xwindow = w;
slouken@1951
   266
#ifdef X_HAVE_UTF8_STRING
slouken@7007
   267
    if (SDL_X11_HAVE_UTF8 && videodata->im) {
slouken@1951
   268
        data->ic =
icculus@7827
   269
            X11_XCreateIC(videodata->im, XNClientWindow, w, XNFocusWindow, w,
slouken@1951
   270
                       XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
slouken@8707
   271
                       NULL);
slouken@1951
   272
    }
slouken@1951
   273
#endif
slouken@1951
   274
    data->created = created;
slouken@1951
   275
    data->videodata = videodata;
slouken@1951
   276
bob@2323
   277
    /* Associate the data with the window */
bob@2324
   278
bob@2324
   279
    if (numwindows < windowlistlength) {
bob@2324
   280
        windowlist[numwindows] = data;
bob@2324
   281
        videodata->numwindows++;
bob@2324
   282
    } else {
bob@2324
   283
        windowlist =
bob@2324
   284
            (SDL_WindowData **) SDL_realloc(windowlist,
bob@2324
   285
                                            (numwindows +
bob@2324
   286
                                             1) * sizeof(*windowlist));
bob@2324
   287
        if (!windowlist) {
bob@2324
   288
            SDL_free(data);
icculus@7037
   289
            return SDL_OutOfMemory();
bob@2323
   290
        }
bob@2324
   291
        windowlist[numwindows] = data;
bob@2324
   292
        videodata->numwindows++;
bob@2324
   293
        videodata->windowlistlength++;
bob@2324
   294
        videodata->windowlist = windowlist;
bob@2323
   295
    }
bob@2323
   296
slouken@1951
   297
    /* Fill in the SDL window with the window data */
slouken@1951
   298
    {
slouken@1951
   299
        XWindowAttributes attrib;
slouken@1951
   300
icculus@7827
   301
        X11_XGetWindowAttributes(data->videodata->display, w, &attrib);
slouken@1951
   302
        window->x = attrib.x;
slouken@1951
   303
        window->y = attrib.y;
slouken@1951
   304
        window->w = attrib.width;
slouken@1951
   305
        window->h = attrib.height;
slouken@1951
   306
        if (attrib.map_state != IsUnmapped) {
slouken@1951
   307
            window->flags |= SDL_WINDOW_SHOWN;
slouken@1951
   308
        } else {
slouken@1951
   309
            window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1951
   310
        }
slouken@5182
   311
        data->visual = attrib.visual;
slouken@5466
   312
        data->colormap = attrib.colormap;
slouken@1951
   313
    }
slouken@3077
   314
slouken@6487
   315
    window->flags |= X11_GetNetWMState(_this, w);
slouken@3077
   316
icculus@6024
   317
    {
icculus@6024
   318
        Window FocalWindow;
icculus@6024
   319
        int RevertTo=0;
icculus@7827
   320
        X11_XGetInputFocus(data->videodata->display, &FocalWindow, &RevertTo);
icculus@6024
   321
        if (FocalWindow==w)
icculus@6024
   322
        {
icculus@6024
   323
            window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@6481
   324
        }
slouken@6481
   325
slouken@6481
   326
        if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
icculus@6024
   327
            SDL_SetKeyboardFocus(data->window);
icculus@6024
   328
        }
icculus@6024
   329
icculus@6024
   330
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
icculus@6024
   331
            /* Tell x11 to clip mouse */
icculus@6024
   332
        }
icculus@6024
   333
    }
icculus@6024
   334
slouken@1951
   335
    /* All done! */
slouken@6487
   336
    window->driverdata = data;
slouken@1951
   337
    return 0;
slouken@1951
   338
}
slouken@1951
   339
icculus@6422
   340
static void
icculus@6422
   341
SetWindowBordered(Display *display, int screen, Window window, SDL_bool border)
icculus@6422
   342
{
icculus@6422
   343
    /*
icculus@6422
   344
     * this code used to check for KWM_WIN_DECORATION, but KDE hasn't
icculus@6422
   345
     *  supported it for years and years. It now respects _MOTIF_WM_HINTS.
icculus@6422
   346
     *  Gnome is similar: just use the Motif atom.
icculus@6422
   347
     */
icculus@6422
   348
icculus@7827
   349
    Atom WM_HINTS = X11_XInternAtom(display, "_MOTIF_WM_HINTS", True);
icculus@6422
   350
    if (WM_HINTS != None) {
icculus@6422
   351
        /* Hints used by Motif compliant window managers */
icculus@6422
   352
        struct
icculus@6422
   353
        {
icculus@6422
   354
            unsigned long flags;
icculus@6422
   355
            unsigned long functions;
icculus@6422
   356
            unsigned long decorations;
icculus@6422
   357
            long input_mode;
icculus@6422
   358
            unsigned long status;
icculus@6422
   359
        } MWMHints = {
icculus@6422
   360
            (1L << 1), 0, border ? 1 : 0, 0, 0
icculus@6422
   361
        };
icculus@6422
   362
icculus@7827
   363
        X11_XChangeProperty(display, window, WM_HINTS, WM_HINTS, 32,
icculus@6422
   364
                        PropModeReplace, (unsigned char *) &MWMHints,
urkle@9460
   365
                        sizeof(MWMHints) / sizeof(long));
icculus@6422
   366
    } else {  /* set the transient hints instead, if necessary */
icculus@7827
   367
        X11_XSetTransientForHint(display, window, RootWindow(display, screen));
icculus@6422
   368
    }
icculus@6422
   369
}
icculus@6422
   370
slouken@1951
   371
int
slouken@1951
   372
X11_CreateWindow(_THIS, SDL_Window * window)
slouken@1951
   373
{
slouken@1951
   374
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   375
    SDL_DisplayData *displaydata =
slouken@5246
   376
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@7683
   377
    SDL_WindowData *windowdata;
slouken@4521
   378
    Display *display = data->display;
slouken@4521
   379
    int screen = displaydata->screen;
slouken@1951
   380
    Visual *visual;
slouken@1951
   381
    int depth;
slouken@1951
   382
    XSetWindowAttributes xattr;
slouken@1951
   383
    Window w;
urkle@6462
   384
    XSizeHints *sizehints;
urkle@6462
   385
    XWMHints *wmhints;
urkle@6462
   386
    XClassHint *classhints;
jstpierre@7490
   387
    Atom _NET_WM_BYPASS_COMPOSITOR;
slouken@4521
   388
    Atom _NET_WM_WINDOW_TYPE;
icculus@10021
   389
    Atom wintype;
icculus@10021
   390
    const char *wintype_name = NULL;
icculus@11127
   391
    long compositor = 1;
mail@6167
   392
    Atom _NET_WM_PID;
slouken@7001
   393
    Atom XdndAware, xdnd_version = 5;
icculus@8785
   394
    long fevent = 0;
slouken@4518
   395
gabomdq@7659
   396
#if SDL_VIDEO_OPENGL_GLX || SDL_VIDEO_OPENGL_EGL
pgriffais@7737
   397
    if ((window->flags & SDL_WINDOW_OPENGL) &&
pgriffais@7737
   398
        !SDL_getenv("SDL_VIDEO_X11_VISUALID")) {
gabomdq@7723
   399
        XVisualInfo *vinfo = NULL;
slouken@1952
   400
gabomdq@7659
   401
#if SDL_VIDEO_OPENGL_EGL
gabomdq@7808
   402
        if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES 
gabomdq@7808
   403
#if SDL_VIDEO_OPENGL_GLX            
slouken@10806
   404
            && ( !_this->gl_data || X11_GL_UseEGL(_this) )
gabomdq@7808
   405
#endif
slouken@8780
   406
        ) {
slouken@6369
   407
            vinfo = X11_GLES_GetVisual(_this, display, screen);
slouken@6369
   408
        } else
slouken@6369
   409
#endif
slouken@6369
   410
        {
slouken@6370
   411
#if SDL_VIDEO_OPENGL_GLX
slouken@6369
   412
            vinfo = X11_GL_GetVisual(_this, display, screen);
slouken@6370
   413
#endif
slouken@1952
   414
        }
gabomdq@7723
   415
slouken@3161
   416
        if (!vinfo) {
slouken@3161
   417
            return -1;
slouken@3161
   418
        }
slouken@3161
   419
        visual = vinfo->visual;
slouken@3161
   420
        depth = vinfo->depth;
icculus@7827
   421
        X11_XFree(vinfo);
slouken@3161
   422
    } else
slouken@3161
   423
#endif
slouken@1952
   424
    {
slouken@1951
   425
        visual = displaydata->visual;
slouken@1951
   426
        depth = displaydata->depth;
slouken@1951
   427
    }
slouken@1951
   428
icculus@10021
   429
    xattr.override_redirect = ((window->flags & SDL_WINDOW_TOOLTIP) || (window->flags & SDL_WINDOW_POPUP_MENU)) ? True : False;
slouken@7750
   430
    xattr.background_pixmap = None;
slouken@1951
   431
    xattr.border_pixel = 0;
slouken@5466
   432
slouken@5466
   433
    if (visual->class == DirectColor) {
slouken@5466
   434
        XColor *colorcells;
slouken@5466
   435
        int i;
slouken@5466
   436
        int ncolors;
slouken@5466
   437
        int rmax, gmax, bmax;
slouken@5466
   438
        int rmask, gmask, bmask;
slouken@5466
   439
        int rshift, gshift, bshift;
slouken@5466
   440
slouken@5466
   441
        xattr.colormap =
icculus@7827
   442
            X11_XCreateColormap(display, RootWindow(display, screen),
slouken@5466
   443
                            visual, AllocAll);
slouken@5466
   444
slouken@5466
   445
        /* If we can't create a colormap, then we must die */
slouken@5466
   446
        if (!xattr.colormap) {
icculus@7037
   447
            return SDL_SetError("Could not create writable colormap");
slouken@5466
   448
        }
slouken@5466
   449
slouken@5466
   450
        /* OK, we got a colormap, now fill it in as best as we can */
slouken@5466
   451
        colorcells = SDL_malloc(visual->map_entries * sizeof(XColor));
slouken@5466
   452
        if (!colorcells) {
icculus@7037
   453
            return SDL_OutOfMemory();
slouken@5466
   454
        }
slouken@5466
   455
        ncolors = visual->map_entries;
slouken@5466
   456
        rmax = 0xffff;
slouken@5466
   457
        gmax = 0xffff;
slouken@5466
   458
        bmax = 0xffff;
slouken@5466
   459
slouken@5466
   460
        rshift = 0;
slouken@5466
   461
        rmask = visual->red_mask;
slouken@5466
   462
        while (0 == (rmask & 1)) {
slouken@5466
   463
            rshift++;
slouken@5466
   464
            rmask >>= 1;
slouken@5466
   465
        }
slouken@5466
   466
slouken@5466
   467
        gshift = 0;
slouken@5466
   468
        gmask = visual->green_mask;
slouken@5466
   469
        while (0 == (gmask & 1)) {
slouken@5466
   470
            gshift++;
slouken@5466
   471
            gmask >>= 1;
slouken@5466
   472
        }
slouken@5466
   473
slouken@5466
   474
        bshift = 0;
slouken@5466
   475
        bmask = visual->blue_mask;
slouken@5466
   476
        while (0 == (bmask & 1)) {
slouken@5466
   477
            bshift++;
slouken@5466
   478
            bmask >>= 1;
slouken@5466
   479
        }
slouken@5466
   480
slouken@5466
   481
        /* build the color table pixel values */
slouken@5466
   482
        for (i = 0; i < ncolors; i++) {
slouken@5466
   483
            Uint32 red = (rmax * i) / (ncolors - 1);
slouken@5466
   484
            Uint32 green = (gmax * i) / (ncolors - 1);
slouken@5466
   485
            Uint32 blue = (bmax * i) / (ncolors - 1);
slouken@5466
   486
slouken@5466
   487
            Uint32 rbits = (rmask * i) / (ncolors - 1);
slouken@5466
   488
            Uint32 gbits = (gmask * i) / (ncolors - 1);
slouken@5466
   489
            Uint32 bbits = (bmask * i) / (ncolors - 1);
slouken@5466
   490
slouken@5466
   491
            Uint32 pix =
slouken@5466
   492
                (rbits << rshift) | (gbits << gshift) | (bbits << bshift);
slouken@5466
   493
slouken@5466
   494
            colorcells[i].pixel = pix;
slouken@5466
   495
slouken@5466
   496
            colorcells[i].red = red;
slouken@5466
   497
            colorcells[i].green = green;
slouken@5466
   498
            colorcells[i].blue = blue;
slouken@5466
   499
slouken@5466
   500
            colorcells[i].flags = DoRed | DoGreen | DoBlue;
slouken@5466
   501
        }
slouken@5466
   502
icculus@7827
   503
        X11_XStoreColors(display, xattr.colormap, colorcells, ncolors);
slouken@5466
   504
slouken@5466
   505
        SDL_free(colorcells);
slouken@5466
   506
    } else {
slouken@5466
   507
        xattr.colormap =
icculus@7827
   508
            X11_XCreateColormap(display, RootWindow(display, screen),
slouken@5466
   509
                            visual, AllocNone);
slouken@5466
   510
    }
slouken@1951
   511
icculus@7827
   512
    w = X11_XCreateWindow(display, RootWindow(display, screen),
slouken@5386
   513
                      window->x, window->y, window->w, window->h,
slouken@5386
   514
                      0, depth, InputOutput, visual,
slouken@7750
   515
                      (CWOverrideRedirect | CWBackPixmap | CWBorderPixel |
slouken@1951
   516
                       CWColormap), &xattr);
slouken@1952
   517
    if (!w) {
icculus@7037
   518
        return SDL_SetError("Couldn't create window");
slouken@1952
   519
    }
slouken@1951
   520
icculus@6422
   521
    SetWindowBordered(display, screen, w,
icculus@6422
   522
                      (window->flags & SDL_WINDOW_BORDERLESS) == 0);
slouken@1951
   523
icculus@7827
   524
    sizehints = X11_XAllocSizeHints();
mail@6167
   525
    /* Setup the normal size hints */
urkle@6462
   526
    sizehints->flags = 0;
mail@6167
   527
    if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
urkle@6462
   528
        sizehints->min_width = sizehints->max_width = window->w;
urkle@6462
   529
        sizehints->min_height = sizehints->max_height = window->h;
urkle@6462
   530
        sizehints->flags |= (PMaxSize | PMinSize);
slouken@1951
   531
    }
urkle@6462
   532
    sizehints->x = window->x;
urkle@6462
   533
    sizehints->y = window->y;
urkle@6462
   534
    sizehints->flags |= USPosition;
slouken@1951
   535
mail@6167
   536
    /* Setup the input hints so we get keyboard input */
icculus@7827
   537
    wmhints = X11_XAllocWMHints();
urkle@6462
   538
    wmhints->input = True;
icculus@10023
   539
    wmhints->window_group = data->window_group;
icculus@10023
   540
    wmhints->flags = InputHint | WindowGroupHint;
mail@6167
   541
mail@6167
   542
    /* Setup the class hints so we can get an icon (AfterStep) */
icculus@7827
   543
    classhints = X11_XAllocClassHint();
urkle@6462
   544
    classhints->res_name = data->classname;
urkle@6462
   545
    classhints->res_class = data->classname;
mail@6167
   546
mail@6167
   547
    /* Set the size, input and class hints, and define WM_CLIENT_MACHINE and WM_LOCALE_NAME */
icculus@7827
   548
    X11_XSetWMProperties(display, w, NULL, NULL, NULL, 0, sizehints, wmhints, classhints);
mail@6167
   549
icculus@7827
   550
    X11_XFree(sizehints);
icculus@7827
   551
    X11_XFree(wmhints);
icculus@7827
   552
    X11_XFree(classhints);
mail@6167
   553
    /* Set the PID related to the window for the given hostname, if possible */
mail@6167
   554
    if (data->pid > 0) {
icculus@10014
   555
        long pid = (long) data->pid;
icculus@7827
   556
        _NET_WM_PID = X11_XInternAtom(display, "_NET_WM_PID", False);
icculus@7827
   557
        X11_XChangeProperty(display, w, _NET_WM_PID, XA_CARDINAL, 32, PropModeReplace,
icculus@10014
   558
                        (unsigned char *) &pid, 1);
slouken@1951
   559
    }
slouken@1951
   560
slouken@4522
   561
    /* Set the window manager state */
slouken@6487
   562
    X11_SetNetWMState(_this, w, window->flags);
slouken@4518
   563
icculus@10021
   564
    compositor = 2;  /* don't disable compositing except for "normal" windows */
icculus@10021
   565
icculus@10021
   566
    if (window->flags & SDL_WINDOW_UTILITY) {
icculus@10021
   567
        wintype_name = "_NET_WM_WINDOW_TYPE_UTILITY";
icculus@10021
   568
    } else if (window->flags & SDL_WINDOW_TOOLTIP) {
icculus@10021
   569
        wintype_name = "_NET_WM_WINDOW_TYPE_TOOLTIP";
icculus@10021
   570
    } else if (window->flags & SDL_WINDOW_POPUP_MENU) {
icculus@10021
   571
        wintype_name = "_NET_WM_WINDOW_TYPE_POPUP_MENU";
icculus@10021
   572
    } else {
icculus@10021
   573
        wintype_name = "_NET_WM_WINDOW_TYPE_NORMAL";
icculus@10021
   574
        compositor = 1;  /* disable compositing for "normal" windows */
icculus@10021
   575
    }
icculus@10021
   576
icculus@10021
   577
    /* Let the window manager know what type of window we are. */
icculus@7827
   578
    _NET_WM_WINDOW_TYPE = X11_XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
icculus@10021
   579
    wintype = X11_XInternAtom(display, wintype_name, False);
icculus@7827
   580
    X11_XChangeProperty(display, w, _NET_WM_WINDOW_TYPE, XA_ATOM, 32,
icculus@10021
   581
                    PropModeReplace, (unsigned char *)&wintype, 1);
slouken@4521
   582
icculus@7827
   583
    _NET_WM_BYPASS_COMPOSITOR = X11_XInternAtom(display, "_NET_WM_BYPASS_COMPOSITOR", False);
icculus@7827
   584
    X11_XChangeProperty(display, w, _NET_WM_BYPASS_COMPOSITOR, XA_CARDINAL, 32,
jstpierre@7490
   585
                    PropModeReplace,
icculus@10021
   586
                    (unsigned char *)&compositor, 1);
slouken@7191
   587
icculus@6638
   588
    {
icculus@10029
   589
        Atom protocols[3];
slouken@9899
   590
        int proto_count = 0;
slouken@9899
   591
icculus@10029
   592
        protocols[proto_count++] = data->WM_DELETE_WINDOW; /* Allow window to be deleted by the WM */
icculus@10029
   593
        protocols[proto_count++] = data->WM_TAKE_FOCUS; /* Since we will want to set input focus explicitly */
icculus@10029
   594
slouken@9899
   595
        /* Default to using ping if there is no hint */
slouken@10499
   596
        if (SDL_GetHintBoolean(SDL_HINT_VIDEO_X11_NET_WM_PING, SDL_TRUE)) {
icculus@10029
   597
            protocols[proto_count++] = data->_NET_WM_PING; /* Respond so WM knows we're alive */
slouken@9899
   598
        }
slouken@9899
   599
slouken@9899
   600
        SDL_assert(proto_count <= sizeof(protocols) / sizeof(protocols[0]));
slouken@9899
   601
slouken@9899
   602
        X11_XSetWMProtocols(display, w, protocols, proto_count);
icculus@6638
   603
    }
slouken@1951
   604
slouken@1951
   605
    if (SetupWindowData(_this, window, w, SDL_TRUE) < 0) {
icculus@7827
   606
        X11_XDestroyWindow(display, w);
slouken@1951
   607
        return -1;
slouken@1951
   608
    }
slouken@7683
   609
    windowdata = (SDL_WindowData *) window->driverdata;
slouken@5386
   610
gabomdq@7659
   611
#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
gabomdq@7723
   612
    if ((window->flags & SDL_WINDOW_OPENGL) && 
gabomdq@7808
   613
        _this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES
gabomdq@7808
   614
#if SDL_VIDEO_OPENGL_GLX            
slouken@10806
   615
        && ( !_this->gl_data || X11_GL_UseEGL(_this) )
gabomdq@7808
   616
#endif  
gabomdq@7808
   617
    ) {
gabomdq@7960
   618
#if SDL_VIDEO_OPENGL_EGL  
gabomdq@7659
   619
        if (!_this->egl_data) {
icculus@7827
   620
            X11_XDestroyWindow(display, w);
gabomdq@7659
   621
            return -1;
gabomdq@7659
   622
        }
gabomdq@7659
   623
gabomdq@7659
   624
        /* Create the GLES window surface */
slouken@7683
   625
        windowdata->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) w);
gabomdq@7659
   626
slouken@7683
   627
        if (windowdata->egl_surface == EGL_NO_SURFACE) {
icculus@7827
   628
            X11_XDestroyWindow(display, w);
gabomdq@7659
   629
            return SDL_SetError("Could not create GLES window surface");
gabomdq@7659
   630
        }
gabomdq@7960
   631
#else
slouken@10806
   632
        return SDL_SetError("Could not create GLES window surface (EGL support not configured)");
gabomdq@7960
   633
#endif /* SDL_VIDEO_OPENGL_EGL */
gabomdq@7659
   634
    }
gabomdq@7659
   635
#endif
gabomdq@7659
   636
    
gabomdq@7659
   637
bob@2325
   638
#ifdef X_HAVE_UTF8_STRING
slouken@7683
   639
    if (SDL_X11_HAVE_UTF8 && windowdata->ic) {
icculus@7827
   640
        X11_XGetICValues(windowdata->ic, XNFilterEvents, &fevent, NULL);
kazeuser@2718
   641
    }
bob@2325
   642
#endif
bob@2325
   643
dimitris@6316
   644
    X11_Xinput2SelectTouch(_this, window);
dimitris@6316
   645
icculus@7827
   646
    X11_XSelectInput(display, w,
icculus@6045
   647
                 (FocusChangeMask | EnterWindowMask | LeaveWindowMask |
icculus@6045
   648
                 ExposureMask | ButtonPressMask | ButtonReleaseMask |
icculus@6045
   649
                 PointerMotionMask | KeyPressMask | KeyReleaseMask |
icculus@6045
   650
                 PropertyChangeMask | StructureNotifyMask |
icculus@6045
   651
                 KeymapStateMask | fevent));
icculus@6045
   652
icculus@7827
   653
    XdndAware = X11_XInternAtom(display, "XdndAware", False);
icculus@7827
   654
    X11_XChangeProperty(display, w, XdndAware, XA_ATOM, 32,
slouken@7001
   655
                 PropModeReplace,
slouken@7191
   656
                 (unsigned char*)&xdnd_version, 1);
slouken@7001
   657
icculus@7827
   658
    X11_XFlush(display);
slouken@5031
   659
slouken@1951
   660
    return 0;
slouken@1951
   661
}
slouken@1951
   662
slouken@1951
   663
int
bob@2324
   664
X11_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1951
   665
{
slouken@1951
   666
    Window w = (Window) data;
slouken@1951
   667
slouken@4518
   668
    window->title = X11_GetWindowTitle(_this, w);
slouken@1951
   669
slouken@1951
   670
    if (SetupWindowData(_this, window, w, SDL_FALSE) < 0) {
slouken@1951
   671
        return -1;
slouken@1951
   672
    }
slouken@1951
   673
    return 0;
slouken@1951
   674
}
slouken@1951
   675
slouken@4518
   676
char *
slouken@4518
   677
X11_GetWindowTitle(_THIS, Window xwindow)
slouken@4518
   678
{
slouken@4518
   679
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@4518
   680
    Display *display = data->display;
slouken@4518
   681
    int status, real_format;
slouken@4518
   682
    Atom real_type;
slouken@4518
   683
    unsigned long items_read, items_left;
slouken@4518
   684
    unsigned char *propdata;
slouken@4518
   685
    char *title = NULL;
slouken@4518
   686
icculus@7827
   687
    status = X11_XGetWindowProperty(display, xwindow, data->_NET_WM_NAME,
slouken@4518
   688
                0L, 8192L, False, data->UTF8_STRING, &real_type, &real_format,
slouken@4518
   689
                &items_read, &items_left, &propdata);
icculus@6023
   690
    if (status == Success && propdata) {
slouken@4518
   691
        title = SDL_strdup(SDL_static_cast(char*, propdata));
icculus@7827
   692
        X11_XFree(propdata);
slouken@4518
   693
    } else {
icculus@7827
   694
        status = X11_XGetWindowProperty(display, xwindow, XA_WM_NAME,
slouken@4518
   695
                    0L, 8192L, False, XA_STRING, &real_type, &real_format,
slouken@4518
   696
                    &items_read, &items_left, &propdata);
icculus@6023
   697
        if (status == Success && propdata) {
slouken@4518
   698
            title = SDL_iconv_string("UTF-8", "", SDL_static_cast(char*, propdata), items_read+1);
slouken@9117
   699
            X11_XFree(propdata);
slouken@4518
   700
        } else {
slouken@4518
   701
            title = SDL_strdup("");
slouken@4518
   702
        }
slouken@4518
   703
    }
slouken@4518
   704
    return title;
slouken@4518
   705
}
slouken@4518
   706
slouken@1951
   707
void
slouken@1951
   708
X11_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1951
   709
{
slouken@1951
   710
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   711
    Display *display = data->videodata->display;
icculus@9479
   712
    XTextProperty titleprop;
slouken@1951
   713
    Status status;
icculus@9474
   714
    const char *title = window->title ? window->title : "";
icculus@9474
   715
    char *title_locale = NULL;
slouken@1951
   716
slouken@1951
   717
#ifdef X_HAVE_UTF8_STRING
slouken@4518
   718
    Atom _NET_WM_NAME = data->videodata->_NET_WM_NAME;
slouken@1951
   719
#endif
slouken@1951
   720
icculus@9474
   721
    title_locale = SDL_iconv_utf8_locale(title);
icculus@9474
   722
    if (!title_locale) {
icculus@9474
   723
        SDL_OutOfMemory();
icculus@9474
   724
        return;
icculus@9474
   725
    }
icculus@9474
   726
icculus@9474
   727
    status = X11_XStringListToTextProperty(&title_locale, 1, &titleprop);
icculus@9474
   728
    SDL_free(title_locale);
icculus@9474
   729
    if (status) {
icculus@9474
   730
        X11_XSetTextProperty(display, data->xwindow, &titleprop, XA_WM_NAME);
icculus@9474
   731
        X11_XFree(titleprop.value);
icculus@9474
   732
    }
icculus@9474
   733
#ifdef X_HAVE_UTF8_STRING
icculus@9474
   734
    if (SDL_X11_HAVE_UTF8) {
icculus@9474
   735
        status = X11_Xutf8TextListToTextProperty(display, (char **) &title, 1,
icculus@9474
   736
                                            XUTF8StringStyle, &titleprop);
icculus@9474
   737
        if (status == Success) {
icculus@9474
   738
            X11_XSetTextProperty(display, data->xwindow, &titleprop,
icculus@9474
   739
                                 _NET_WM_NAME);
icculus@7827
   740
            X11_XFree(titleprop.value);
slouken@1951
   741
        }
icculus@9474
   742
    }
slouken@1951
   743
#endif
icculus@9474
   744
icculus@7827
   745
    X11_XFlush(display);
slouken@1951
   746
}
slouken@1951
   747
slouken@1951
   748
void
slouken@2967
   749
X11_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slouken@2967
   750
{
slouken@2967
   751
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@2967
   752
    Display *display = data->videodata->display;
slouken@4518
   753
    Atom _NET_WM_ICON = data->videodata->_NET_WM_ICON;
slouken@2967
   754
slouken@2967
   755
    if (icon) {
slouken@2967
   756
        int propsize;
slouken@4528
   757
        long *propdata;
slouken@2967
   758
slouken@2967
   759
        /* Set the _NET_WM_ICON property */
slouken@7523
   760
        SDL_assert(icon->format->format == SDL_PIXELFORMAT_ARGB8888);
slouken@2990
   761
        propsize = 2 + (icon->w * icon->h);
slouken@4566
   762
        propdata = SDL_malloc(propsize * sizeof(long));
slouken@2967
   763
        if (propdata) {
slouken@4528
   764
            int x, y;
slouken@4528
   765
            Uint32 *src;
slouken@4528
   766
            long *dst;
slouken@4528
   767
slouken@2967
   768
            propdata[0] = icon->w;
slouken@2967
   769
            propdata[1] = icon->h;
slouken@4528
   770
            dst = &propdata[2];
slouken@4528
   771
            for (y = 0; y < icon->h; ++y) {
slouken@7523
   772
                src = (Uint32*)((Uint8*)icon->pixels + y * icon->pitch);
slouken@4528
   773
                for (x = 0; x < icon->w; ++x) {
slouken@4528
   774
                    *dst++ = *src++;
slouken@4528
   775
                }
slouken@4528
   776
            }
icculus@7827
   777
            X11_XChangeProperty(display, data->xwindow, _NET_WM_ICON, XA_CARDINAL,
slouken@2990
   778
                            32, PropModeReplace, (unsigned char *) propdata,
slouken@2990
   779
                            propsize);
slouken@2967
   780
        }
icculus@6562
   781
        SDL_free(propdata);
slouken@2967
   782
    } else {
icculus@7827
   783
        X11_XDeleteProperty(display, data->xwindow, _NET_WM_ICON);
slouken@2967
   784
    }
icculus@7827
   785
    X11_XFlush(display);
slouken@2967
   786
}
slouken@2967
   787
slouken@2967
   788
void
slouken@1951
   789
X11_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1951
   790
{
slouken@1951
   791
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   792
    Display *display = data->videodata->display;
slouken@1951
   793
icculus@10103
   794
    X11_XMoveWindow(display, data->xwindow, window->x - data->border_left, window->y - data->border_top);
icculus@7827
   795
    X11_XFlush(display);
slouken@1951
   796
}
slouken@1951
   797
slouken@1951
   798
void
slouken@7617
   799
X11_SetWindowMinimumSize(_THIS, SDL_Window * window)
slouken@7617
   800
{
slouken@7617
   801
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7617
   802
    Display *display = data->videodata->display;
slouken@7617
   803
slouken@7617
   804
    if (window->flags & SDL_WINDOW_RESIZABLE) {
icculus@7827
   805
         XSizeHints *sizehints = X11_XAllocSizeHints();
slouken@7617
   806
         long userhints;
slouken@7617
   807
icculus@7827
   808
         X11_XGetWMNormalHints(display, data->xwindow, sizehints, &userhints);
slouken@7617
   809
slouken@7617
   810
         sizehints->min_width = window->min_w;
slouken@7617
   811
         sizehints->min_height = window->min_h;
slouken@7617
   812
         sizehints->flags |= PMinSize;
slouken@7617
   813
icculus@7827
   814
         X11_XSetWMNormalHints(display, data->xwindow, sizehints);
slouken@7617
   815
icculus@7827
   816
         X11_XFree(sizehints);
slouken@7617
   817
slouken@7617
   818
        /* See comment in X11_SetWindowSize. */
icculus@7827
   819
        X11_XResizeWindow(display, data->xwindow, window->w, window->h);
icculus@10103
   820
        X11_XMoveWindow(display, data->xwindow, window->x - data->border_left, window->y - data->border_top);
icculus@7827
   821
        X11_XRaiseWindow(display, data->xwindow);
slouken@7617
   822
    }
slouken@7617
   823
icculus@7827
   824
    X11_XFlush(display);
slouken@7617
   825
}
slouken@7617
   826
slouken@7617
   827
void
slouken@7617
   828
X11_SetWindowMaximumSize(_THIS, SDL_Window * window)
slouken@7617
   829
{
slouken@7617
   830
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7617
   831
    Display *display = data->videodata->display;
slouken@7617
   832
slouken@7617
   833
    if (window->flags & SDL_WINDOW_RESIZABLE) {
icculus@7827
   834
         XSizeHints *sizehints = X11_XAllocSizeHints();
slouken@7617
   835
         long userhints;
slouken@7617
   836
icculus@7827
   837
         X11_XGetWMNormalHints(display, data->xwindow, sizehints, &userhints);
slouken@7617
   838
slouken@7617
   839
         sizehints->max_width = window->max_w;
slouken@7617
   840
         sizehints->max_height = window->max_h;
slouken@7617
   841
         sizehints->flags |= PMaxSize;
slouken@7617
   842
icculus@7827
   843
         X11_XSetWMNormalHints(display, data->xwindow, sizehints);
slouken@7617
   844
icculus@7827
   845
         X11_XFree(sizehints);
slouken@7617
   846
slouken@7617
   847
        /* See comment in X11_SetWindowSize. */
icculus@7827
   848
        X11_XResizeWindow(display, data->xwindow, window->w, window->h);
icculus@10103
   849
        X11_XMoveWindow(display, data->xwindow, window->x - data->border_left, window->y - data->border_top);
icculus@7827
   850
        X11_XRaiseWindow(display, data->xwindow);
slouken@7617
   851
    }
slouken@7617
   852
icculus@7827
   853
    X11_XFlush(display);
slouken@7617
   854
}
slouken@7617
   855
slouken@7617
   856
void
slouken@1951
   857
X11_SetWindowSize(_THIS, SDL_Window * window)
slouken@1951
   858
{
slouken@1951
   859
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   860
    Display *display = data->videodata->display;
slouken@1951
   861
slouken@6481
   862
    if (SDL_IsShapedWindow(window)) {
eligottlieb@4782
   863
        X11_ResizeWindowShape(window);
slouken@6481
   864
    }
slouken@5531
   865
    if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
icculus@7827
   866
         /* Apparently, if the X11 Window is set to a 'non-resizable' window, you cannot resize it using the X11_XResizeWindow, thus
gabomdq@7677
   867
            we must set the size hints to adjust the window size. */
icculus@7827
   868
         XSizeHints *sizehints = X11_XAllocSizeHints();
slouken@5531
   869
         long userhints;
slouken@5531
   870
icculus@7827
   871
         X11_XGetWMNormalHints(display, data->xwindow, sizehints, &userhints);
slouken@5531
   872
slouken@6343
   873
         sizehints->min_width = sizehints->max_width = window->w;
slouken@5531
   874
         sizehints->min_height = sizehints->max_height = window->h;
slouken@7617
   875
         sizehints->flags |= PMinSize | PMaxSize;
slouken@5531
   876
icculus@7827
   877
         X11_XSetWMNormalHints(display, data->xwindow, sizehints);
slouken@5531
   878
icculus@7827
   879
         X11_XFree(sizehints);
slouken@6541
   880
slouken@6541
   881
        /* From Pierre-Loup:
jorgen@6899
   882
           WMs each have their little quirks with that.  When you change the
jorgen@6899
   883
           size hints, they get a ConfigureNotify event with the
jorgen@6899
   884
           WM_NORMAL_SIZE_HINTS Atom.  They all save the hints then, but they
jorgen@6899
   885
           don't all resize the window right away to enforce the new hints.
slouken@6541
   886
jorgen@6899
   887
           Some of them resize only after:
jorgen@6899
   888
            - A user-initiated move or resize
jorgen@6899
   889
            - A code-initiated move or resize
jorgen@6899
   890
            - Hiding & showing window (Unmap & map)
slouken@6541
   891
jorgen@6899
   892
           The following move & resize seems to help a lot of WMs that didn't
jorgen@6899
   893
           properly update after the hints were changed. We don't do a
jorgen@6899
   894
           hide/show, because there are supposedly subtle problems with doing so
jorgen@6899
   895
           and transitioning from windowed to fullscreen in Unity.
jorgen@6899
   896
         */
icculus@7827
   897
        X11_XResizeWindow(display, data->xwindow, window->w, window->h);
icculus@10103
   898
        X11_XMoveWindow(display, data->xwindow, window->x - data->border_left, window->y - data->border_top);
icculus@7827
   899
        X11_XRaiseWindow(display, data->xwindow);
slouken@6481
   900
    } else {
icculus@7827
   901
        X11_XResizeWindow(display, data->xwindow, window->w, window->h);
slouken@6481
   902
    }
jorgen@6899
   903
icculus@7827
   904
    X11_XFlush(display);
slouken@1951
   905
}
slouken@1951
   906
icculus@10024
   907
int
icculus@10024
   908
X11_GetWindowBordersSize(_THIS, SDL_Window * window, int *top, int *left, int *bottom, int *right)
icculus@10024
   909
{
icculus@10024
   910
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
icculus@10024
   911
icculus@10102
   912
    *left = data->border_left;
icculus@10102
   913
    *right = data->border_right;
icculus@10102
   914
    *top = data->border_top;
icculus@10102
   915
    *bottom = data->border_bottom;
icculus@10024
   916
icculus@10102
   917
    return 0;
icculus@10024
   918
}
icculus@10024
   919
icculus@10025
   920
int
icculus@10025
   921
X11_SetWindowOpacity(_THIS, SDL_Window * window, float opacity)
icculus@10025
   922
{
icculus@10025
   923
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@10025
   924
    Display *display = data->videodata->display;
icculus@10025
   925
    Atom _NET_WM_WINDOW_OPACITY = data->videodata->_NET_WM_WINDOW_OPACITY;
icculus@10025
   926
icculus@10025
   927
    if (opacity == 1.0f) {
icculus@10025
   928
        X11_XDeleteProperty(display, data->xwindow, _NET_WM_WINDOW_OPACITY);
icculus@10025
   929
    } else  {
icculus@10025
   930
        const Uint32 FullyOpaque = 0xFFFFFFFF;
icculus@10025
   931
        const long alpha = (long) ((double)opacity * (double)FullyOpaque);
icculus@10025
   932
        X11_XChangeProperty(display, data->xwindow, _NET_WM_WINDOW_OPACITY, XA_CARDINAL, 32,
icculus@10025
   933
            PropModeReplace, (unsigned char *)&alpha, 1);
icculus@10025
   934
    }
icculus@10025
   935
icculus@10025
   936
    return 0;
icculus@10025
   937
}
icculus@10025
   938
icculus@10027
   939
int 
icculus@10027
   940
X11_SetWindowModalFor(_THIS, SDL_Window * modal_window, SDL_Window * parent_window) {
icculus@10027
   941
    SDL_WindowData *data = (SDL_WindowData *) modal_window->driverdata;
icculus@10027
   942
    SDL_WindowData *parent_data = (SDL_WindowData *) parent_window->driverdata;
icculus@10027
   943
    Display *display = data->videodata->display;
icculus@10027
   944
icculus@10027
   945
    X11_XSetTransientForHint(display, data->xwindow, parent_data->xwindow);
icculus@10027
   946
    return 0;
icculus@10027
   947
}
icculus@10027
   948
icculus@10026
   949
int
icculus@10026
   950
X11_SetWindowInputFocus(_THIS, SDL_Window * window) 
icculus@10026
   951
{
icculus@10026
   952
    if (X11_IsWindowMapped(_this, window)) {
icculus@10026
   953
        SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@10026
   954
        Display *display = data->videodata->display;
icculus@10026
   955
        X11_XSetInputFocus(display, data->xwindow, RevertToNone, CurrentTime);
icculus@10026
   956
        X11_XFlush(display);
icculus@10026
   957
        return 0;
icculus@10026
   958
    }
icculus@10026
   959
    return -1;
icculus@10026
   960
}
icculus@10026
   961
icculus@6422
   962
void
icculus@6422
   963
X11_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
icculus@6422
   964
{
icculus@6423
   965
    const SDL_bool focused = ((window->flags & SDL_WINDOW_INPUT_FOCUS) != 0);
icculus@6423
   966
    const SDL_bool visible = ((window->flags & SDL_WINDOW_HIDDEN) == 0);
icculus@6422
   967
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@6422
   968
    SDL_DisplayData *displaydata =
icculus@6422
   969
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
icculus@6422
   970
    Display *display = data->videodata->display;
icculus@6422
   971
    XEvent event;
icculus@6422
   972
icculus@6422
   973
    SetWindowBordered(display, displaydata->screen, data->xwindow, bordered);
icculus@7827
   974
    X11_XFlush(display);
icculus@6422
   975
icculus@6423
   976
    if (visible) {
icculus@6423
   977
        XWindowAttributes attr;
icculus@6423
   978
        do {
icculus@7827
   979
            X11_XSync(display, False);
icculus@7827
   980
            X11_XGetWindowAttributes(display, data->xwindow, &attr);
icculus@6423
   981
        } while (attr.map_state != IsViewable);
icculus@6423
   982
icculus@6423
   983
        if (focused) {
icculus@7827
   984
            X11_XSetInputFocus(display, data->xwindow, RevertToParent, CurrentTime);
icculus@6423
   985
        }
icculus@6423
   986
    }
icculus@6423
   987
icculus@6422
   988
    /* make sure these don't make it to the real event queue if they fired here. */
icculus@7827
   989
    X11_XSync(display, False);
icculus@7827
   990
    X11_XCheckIfEvent(display, &event, &isUnmapNotify, (XPointer)&data->xwindow);
icculus@7827
   991
    X11_XCheckIfEvent(display, &event, &isMapNotify, (XPointer)&data->xwindow);
icculus@6422
   992
}
slouken@6336
   993
slouken@1951
   994
void
icculus@10385
   995
X11_SetWindowResizable(_THIS, SDL_Window * window, SDL_bool resizable)
icculus@10385
   996
{
icculus@10385
   997
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@10385
   998
    Display *display = data->videodata->display;
icculus@10385
   999
icculus@10385
  1000
    XSizeHints *sizehints = X11_XAllocSizeHints();
icculus@10385
  1001
    long userhints;
icculus@10385
  1002
icculus@10385
  1003
    X11_XGetWMNormalHints(display, data->xwindow, sizehints, &userhints);
icculus@10385
  1004
icculus@10385
  1005
    if (resizable) {
icculus@10385
  1006
        /* FIXME: Is there a better way to get max window size from X? -flibit */
icculus@10385
  1007
        const int maxsize = 0x7FFFFFFF;
icculus@10385
  1008
        sizehints->min_width = window->min_w;
icculus@10385
  1009
        sizehints->min_height = window->min_h;
icculus@10385
  1010
        sizehints->max_width = (window->max_w == 0) ? maxsize : window->max_w;
icculus@10385
  1011
        sizehints->max_height = (window->max_h == 0) ? maxsize : window->max_h;
icculus@10385
  1012
    } else {
icculus@10385
  1013
        sizehints->min_width = window->w;
icculus@10385
  1014
        sizehints->min_height = window->h;
icculus@10385
  1015
        sizehints->max_width = window->w;
icculus@10385
  1016
        sizehints->max_height = window->h;
icculus@10385
  1017
    }
icculus@10385
  1018
    sizehints->flags |= PMinSize | PMaxSize;
icculus@10385
  1019
icculus@10385
  1020
    X11_XSetWMNormalHints(display, data->xwindow, sizehints);
icculus@10385
  1021
icculus@10385
  1022
    X11_XFree(sizehints);
icculus@10385
  1023
icculus@10385
  1024
    /* See comment in X11_SetWindowSize. */
icculus@10385
  1025
    X11_XResizeWindow(display, data->xwindow, window->w, window->h);
icculus@10385
  1026
    X11_XMoveWindow(display, data->xwindow, window->x - data->border_left, window->y - data->border_top);
icculus@10385
  1027
    X11_XRaiseWindow(display, data->xwindow);
icculus@10385
  1028
icculus@10385
  1029
    X11_XFlush(display);
icculus@10385
  1030
}
icculus@10385
  1031
icculus@10385
  1032
void
slouken@1951
  1033
X11_ShowWindow(_THIS, SDL_Window * window)
slouken@1951
  1034
{
slouken@1951
  1035
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
  1036
    Display *display = data->videodata->display;
slouken@6336
  1037
    XEvent event;
slouken@1951
  1038
slouken@6483
  1039
    if (!X11_IsWindowMapped(_this, window)) {
icculus@7827
  1040
        X11_XMapRaised(display, data->xwindow);
slouken@6483
  1041
        /* Blocking wait for "MapNotify" event.
icculus@7827
  1042
         * We use X11_XIfEvent because pXWindowEvent takes a mask rather than a type,
slouken@6483
  1043
         * and XCheckTypedWindowEvent doesn't block */
slouken@11288
  1044
        if(!(window->flags & SDL_WINDOW_FOREIGN))
slouken@11288
  1045
            X11_XIfEvent(display, &event, &isMapNotify, (XPointer)&data->xwindow);
icculus@7827
  1046
        X11_XFlush(display);
slouken@6483
  1047
    }
icculus@9683
  1048
slouken@9794
  1049
    if (!data->videodata->net_wm) {
icculus@9683
  1050
        /* no WM means no FocusIn event, which confuses us. Force it. */
icculus@9683
  1051
        X11_XSetInputFocus(display, data->xwindow, RevertToNone, CurrentTime);
icculus@9683
  1052
        X11_XFlush(display);
icculus@9683
  1053
    }
slouken@1951
  1054
}
slouken@1951
  1055
slouken@1951
  1056
void
slouken@1951
  1057
X11_HideWindow(_THIS, SDL_Window * window)
slouken@1951
  1058
{
slouken@1951
  1059
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7522
  1060
    SDL_DisplayData *displaydata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@1951
  1061
    Display *display = data->videodata->display;
slouken@6336
  1062
    XEvent event;
slouken@1951
  1063
slouken@6483
  1064
    if (X11_IsWindowMapped(_this, window)) {
icculus@7827
  1065
        X11_XWithdrawWindow(display, data->xwindow, displaydata->screen);
slouken@6483
  1066
        /* Blocking wait for "UnmapNotify" event */
slouken@11288
  1067
        if(!(window->flags & SDL_WINDOW_FOREIGN))
slouken@11288
  1068
            X11_XIfEvent(display, &event, &isUnmapNotify, (XPointer)&data->xwindow);
icculus@7827
  1069
        X11_XFlush(display);
slouken@6483
  1070
    }
slouken@1951
  1071
}
slouken@1951
  1072
slouken@7741
  1073
static void
slouken@7741
  1074
SetWindowActive(_THIS, SDL_Window * window)
slouken@7741
  1075
{
slouken@7741
  1076
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@7741
  1077
    SDL_DisplayData *displaydata =
slouken@7741
  1078
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@7741
  1079
    Display *display = data->videodata->display;
slouken@7741
  1080
    Atom _NET_ACTIVE_WINDOW = data->videodata->_NET_ACTIVE_WINDOW;
slouken@7741
  1081
slouken@7741
  1082
    if (X11_IsWindowMapped(_this, window)) {
icculus@10007
  1083
        XEvent e;
icculus@10007
  1084
icculus@10006
  1085
        /*printf("SDL Window %p: sending _NET_ACTIVE_WINDOW with timestamp %lu\n", window, data->user_time);*/
slouken@7741
  1086
slouken@7741
  1087
        SDL_zero(e);
slouken@7741
  1088
        e.xany.type = ClientMessage;
slouken@7741
  1089
        e.xclient.message_type = _NET_ACTIVE_WINDOW;
slouken@7741
  1090
        e.xclient.format = 32;
slouken@7741
  1091
        e.xclient.window = data->xwindow;
slouken@7741
  1092
        e.xclient.data.l[0] = 1;  /* source indication. 1 = application */
icculus@10006
  1093
        e.xclient.data.l[1] = data->user_time;
slouken@7741
  1094
        e.xclient.data.l[2] = 0;
slouken@7741
  1095
icculus@7827
  1096
        X11_XSendEvent(display, RootWindow(display, displaydata->screen), 0,
slouken@7741
  1097
                   SubstructureNotifyMask | SubstructureRedirectMask, &e);
slouken@7741
  1098
icculus@7827
  1099
        X11_XFlush(display);
slouken@7741
  1100
    }
slouken@7741
  1101
}
slouken@7741
  1102
slouken@1951
  1103
void
slouken@1951
  1104
X11_RaiseWindow(_THIS, SDL_Window * window)
slouken@1951
  1105
{
slouken@1951
  1106
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
  1107
    Display *display = data->videodata->display;
slouken@1951
  1108
icculus@7827
  1109
    X11_XRaiseWindow(display, data->xwindow);
slouken@7741
  1110
    SetWindowActive(_this, window);
icculus@7827
  1111
    X11_XFlush(display);
slouken@1951
  1112
}
slouken@1951
  1113
slouken@3077
  1114
static void
slouken@5302
  1115
SetWindowMaximized(_THIS, SDL_Window * window, SDL_bool maximized)
slouken@3077
  1116
{
slouken@3077
  1117
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@3077
  1118
    SDL_DisplayData *displaydata =
slouken@5246
  1119
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@3077
  1120
    Display *display = data->videodata->display;
slouken@4518
  1121
    Atom _NET_WM_STATE = data->videodata->_NET_WM_STATE;
slouken@4518
  1122
    Atom _NET_WM_STATE_MAXIMIZED_VERT = data->videodata->_NET_WM_STATE_MAXIMIZED_VERT;
slouken@4518
  1123
    Atom _NET_WM_STATE_MAXIMIZED_HORZ = data->videodata->_NET_WM_STATE_MAXIMIZED_HORZ;
slouken@3077
  1124
icculus@7566
  1125
    if (maximized) {
icculus@7566
  1126
        window->flags |= SDL_WINDOW_MAXIMIZED;
icculus@7566
  1127
    } else {
icculus@7566
  1128
        window->flags &= ~SDL_WINDOW_MAXIMIZED;
icculus@7566
  1129
    }
icculus@7566
  1130
slouken@4522
  1131
    if (X11_IsWindowMapped(_this, window)) {
slouken@4522
  1132
        XEvent e;
slouken@3077
  1133
slouken@4524
  1134
        SDL_zero(e);
slouken@4522
  1135
        e.xany.type = ClientMessage;
slouken@4522
  1136
        e.xclient.message_type = _NET_WM_STATE;
slouken@4522
  1137
        e.xclient.format = 32;
slouken@4522
  1138
        e.xclient.window = data->xwindow;
slouken@4522
  1139
        e.xclient.data.l[0] =
slouken@4522
  1140
            maximized ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
slouken@4522
  1141
        e.xclient.data.l[1] = _NET_WM_STATE_MAXIMIZED_VERT;
slouken@4522
  1142
        e.xclient.data.l[2] = _NET_WM_STATE_MAXIMIZED_HORZ;
slouken@4522
  1143
        e.xclient.data.l[3] = 0l;
slouken@4522
  1144
icculus@7827
  1145
        X11_XSendEvent(display, RootWindow(display, displaydata->screen), 0,
slouken@4522
  1146
                   SubstructureNotifyMask | SubstructureRedirectMask, &e);
slouken@4522
  1147
    } else {
icculus@7566
  1148
        X11_SetNetWMState(_this, data->xwindow, window->flags);
slouken@4522
  1149
    }
icculus@7827
  1150
    X11_XFlush(display);
slouken@3077
  1151
}
slouken@3077
  1152
slouken@1951
  1153
void
slouken@1951
  1154
X11_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1951
  1155
{
slouken@5302
  1156
    SetWindowMaximized(_this, window, SDL_TRUE);
slouken@1951
  1157
}
slouken@1951
  1158
slouken@1951
  1159
void
slouken@1951
  1160
X11_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1951
  1161
{
slouken@4518
  1162
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@4518
  1163
    SDL_DisplayData *displaydata =
slouken@5246
  1164
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@4518
  1165
    Display *display = data->videodata->display;
slouken@7191
  1166
icculus@7827
  1167
    X11_XIconifyWindow(display, data->xwindow, displaydata->screen);
icculus@7827
  1168
    X11_XFlush(display);
slouken@1951
  1169
}
slouken@1951
  1170
slouken@1951
  1171
void
slouken@1951
  1172
X11_RestoreWindow(_THIS, SDL_Window * window)
slouken@1951
  1173
{
slouken@5302
  1174
    SetWindowMaximized(_this, window, SDL_FALSE);
slouken@7741
  1175
    X11_ShowWindow(_this, window);
slouken@7191
  1176
    SetWindowActive(_this, window);
slouken@1951
  1177
}
slouken@1951
  1178
icculus@10011
  1179
/* This asks the Window Manager to handle fullscreen for us. This is the modern way. */
icculus@6467
  1180
static void
icculus@6467
  1181
X11_SetWindowFullscreenViaWM(_THIS, SDL_Window * window, SDL_VideoDisplay * _display, SDL_bool fullscreen)
slouken@5302
  1182
{
slouken@5302
  1183
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5305
  1184
    SDL_DisplayData *displaydata = (SDL_DisplayData *) _display->driverdata;
slouken@5302
  1185
    Display *display = data->videodata->display;
slouken@5302
  1186
    Atom _NET_WM_STATE = data->videodata->_NET_WM_STATE;
slouken@5302
  1187
    Atom _NET_WM_STATE_FULLSCREEN = data->videodata->_NET_WM_STATE_FULLSCREEN;
slouken@5302
  1188
slouken@5302
  1189
    if (X11_IsWindowMapped(_this, window)) {
slouken@5302
  1190
        XEvent e;
slouken@5302
  1191
slouken@6539
  1192
        if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
slouken@6539
  1193
            /* Compiz refuses fullscreen toggle if we're not resizable, so update the hints so we
slouken@6539
  1194
               can be resized to the fullscreen resolution (or reset so we're not resizable again) */
icculus@7827
  1195
            XSizeHints *sizehints = X11_XAllocSizeHints();
slouken@6539
  1196
            long flags = 0;
icculus@7827
  1197
            X11_XGetWMNormalHints(display, data->xwindow, sizehints, &flags);
slouken@6539
  1198
            /* set the resize flags on */
slouken@6539
  1199
            if (fullscreen) {
slouken@6539
  1200
                /* we are going fullscreen so turn the flags off */
slouken@6539
  1201
                sizehints->flags &= ~(PMinSize | PMaxSize);
slouken@6539
  1202
            } else {
slouken@6539
  1203
                /* Reset the min/max width height to make the window non-resizable again */
icculus@6466
  1204
                sizehints->flags |= PMinSize | PMaxSize;
slouken@6540
  1205
                sizehints->min_width = sizehints->max_width = window->windowed.w;
slouken@6540
  1206
                sizehints->min_height = sizehints->max_height = window->windowed.h;
icculus@6466
  1207
            }
icculus@7827
  1208
            X11_XSetWMNormalHints(display, data->xwindow, sizehints);
icculus@7827
  1209
            X11_XFree(sizehints);
icculus@6466
  1210
        }
slouken@6539
  1211
slouken@5302
  1212
        SDL_zero(e);
slouken@5302
  1213
        e.xany.type = ClientMessage;
slouken@5302
  1214
        e.xclient.message_type = _NET_WM_STATE;
slouken@5302
  1215
        e.xclient.format = 32;
slouken@5302
  1216
        e.xclient.window = data->xwindow;
slouken@5302
  1217
        e.xclient.data.l[0] =
slouken@5302
  1218
            fullscreen ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
slouken@5302
  1219
        e.xclient.data.l[1] = _NET_WM_STATE_FULLSCREEN;
slouken@5302
  1220
        e.xclient.data.l[3] = 0l;
slouken@5302
  1221
icculus@7827
  1222
        X11_XSendEvent(display, RootWindow(display, displaydata->screen), 0,
slouken@5302
  1223
                   SubstructureNotifyMask | SubstructureRedirectMask, &e);
icculus@10084
  1224
icculus@10084
  1225
        /* Fullscreen windows sometimes end up being marked maximized by
icculus@10084
  1226
            window managers. Force it back to how we expect it to be. */
icculus@10084
  1227
        if (!fullscreen && ((window->flags & SDL_WINDOW_MAXIMIZED) == 0)) {
icculus@10084
  1228
            SDL_zero(e);
icculus@10084
  1229
            e.xany.type = ClientMessage;
icculus@10084
  1230
            e.xclient.message_type = _NET_WM_STATE;
icculus@10084
  1231
            e.xclient.format = 32;
icculus@10084
  1232
            e.xclient.window = data->xwindow;
icculus@10084
  1233
            e.xclient.data.l[0] = _NET_WM_STATE_REMOVE;
icculus@10084
  1234
            e.xclient.data.l[1] = data->videodata->_NET_WM_STATE_MAXIMIZED_VERT;
icculus@10084
  1235
            e.xclient.data.l[2] = data->videodata->_NET_WM_STATE_MAXIMIZED_HORZ;
icculus@10084
  1236
            e.xclient.data.l[3] = 0l;
icculus@10084
  1237
            X11_XSendEvent(display, RootWindow(display, displaydata->screen), 0,
icculus@10084
  1238
                   SubstructureNotifyMask | SubstructureRedirectMask, &e);
icculus@10084
  1239
        }
slouken@5302
  1240
    } else {
slouken@6481
  1241
        Uint32 flags;
slouken@5302
  1242
slouken@6481
  1243
        flags = window->flags;
slouken@5302
  1244
        if (fullscreen) {
slouken@6481
  1245
            flags |= SDL_WINDOW_FULLSCREEN;
slouken@6481
  1246
        } else {
slouken@6481
  1247
            flags &= ~SDL_WINDOW_FULLSCREEN;
slouken@5302
  1248
        }
slouken@6487
  1249
        X11_SetNetWMState(_this, data->xwindow, flags);
slouken@5302
  1250
    }
mikesart@6568
  1251
slouken@6575
  1252
    if (data->visual->class == DirectColor) {
slouken@6575
  1253
        if ( fullscreen ) {
icculus@7827
  1254
            X11_XInstallColormap(display, data->colormap);
slouken@6575
  1255
        } else {
icculus@7827
  1256
            X11_XUninstallColormap(display, data->colormap);
slouken@6575
  1257
        }
slouken@6575
  1258
    }
mikesart@6568
  1259
icculus@7827
  1260
    X11_XFlush(display);
slouken@5302
  1261
}
slouken@5302
  1262
icculus@6467
  1263
/* This handles fullscreen itself, outside the Window Manager. */
icculus@6467
  1264
static void
icculus@6467
  1265
X11_BeginWindowFullscreenLegacy(_THIS, SDL_Window * window, SDL_VideoDisplay * _display)
icculus@6467
  1266
{
icculus@6467
  1267
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@6467
  1268
    SDL_DisplayData *displaydata = (SDL_DisplayData *) _display->driverdata;
icculus@6467
  1269
    Visual *visual = data->visual;
icculus@6467
  1270
    Display *display = data->videodata->display;
icculus@6467
  1271
    const int screen = displaydata->screen;
icculus@6467
  1272
    Window root = RootWindow(display, screen);
icculus@6467
  1273
    const int def_vis = (visual == DefaultVisual(display, screen));
icculus@6467
  1274
    unsigned long xattrmask = 0;
icculus@6467
  1275
    XSetWindowAttributes xattr;
icculus@6467
  1276
    XEvent ev;
slouken@6475
  1277
    SDL_Rect rect;
icculus@6467
  1278
icculus@6467
  1279
    if ( data->fswindow ) {
icculus@6467
  1280
        return;  /* already fullscreen, I hope. */
icculus@6467
  1281
    }
icculus@6467
  1282
slouken@6475
  1283
    X11_GetDisplayBounds(_this, _display, &rect);
slouken@6475
  1284
icculus@6467
  1285
    SDL_zero(xattr);
icculus@6467
  1286
    xattr.override_redirect = True;
icculus@6467
  1287
    xattrmask |= CWOverrideRedirect;
icculus@6467
  1288
    xattr.background_pixel = def_vis ? BlackPixel(display, screen) : 0;
icculus@6467
  1289
    xattrmask |= CWBackPixel;
icculus@6467
  1290
    xattr.border_pixel = 0;
icculus@6467
  1291
    xattrmask |= CWBorderPixel;
icculus@6467
  1292
    xattr.colormap = data->colormap;
icculus@6467
  1293
    xattrmask |= CWColormap;
icculus@6467
  1294
icculus@7827
  1295
    data->fswindow = X11_XCreateWindow(display, root,
slouken@6475
  1296
                                   rect.x, rect.y, rect.w, rect.h, 0,
icculus@6467
  1297
                                   displaydata->depth, InputOutput,
icculus@6467
  1298
                                   visual, xattrmask, &xattr);
icculus@6467
  1299
icculus@7827
  1300
    X11_XSelectInput(display, data->fswindow, StructureNotifyMask);
icculus@7827
  1301
    X11_XSetWindowBackground(display, data->fswindow, 0);
icculus@7827
  1302
    X11_XInstallColormap(display, data->colormap);
icculus@7827
  1303
    X11_XClearWindow(display, data->fswindow);
icculus@7827
  1304
    X11_XMapRaised(display, data->fswindow);
icculus@6467
  1305
slouken@6480
  1306
    /* Make sure the fswindow is in view by warping mouse to the corner */
icculus@7827
  1307
    X11_XUngrabPointer(display, CurrentTime);
icculus@7827
  1308
    X11_XWarpPointer(display, None, root, 0, 0, 0, 0, rect.x, rect.y);
icculus@6467
  1309
icculus@6467
  1310
    /* Wait to be mapped, filter Unmap event out if it arrives. */
icculus@7827
  1311
    X11_XIfEvent(display, &ev, &isMapNotify, (XPointer)&data->fswindow);
icculus@7827
  1312
    X11_XCheckIfEvent(display, &ev, &isUnmapNotify, (XPointer)&data->fswindow);
icculus@6467
  1313
icculus@6467
  1314
#if SDL_VIDEO_DRIVER_X11_XVIDMODE
icculus@6467
  1315
    if ( displaydata->use_vidmode ) {
icculus@7827
  1316
        X11_XF86VidModeLockModeSwitch(display, screen, True);
icculus@6467
  1317
    }
icculus@6467
  1318
#endif
icculus@6467
  1319
icculus@6467
  1320
    SetWindowBordered(display, displaydata->screen, data->xwindow, SDL_FALSE);
icculus@6467
  1321
icculus@6467
  1322
    /* Center actual window within our cover-the-screen window. */
icculus@7827
  1323
    X11_XReparentWindow(display, data->xwindow, data->fswindow,
slouken@6501
  1324
                    (rect.w - window->w) / 2, (rect.h - window->h) / 2);
icculus@6467
  1325
slouken@7632
  1326
    /* Move the mouse to the upper left to make sure it's on-screen */
icculus@7827
  1327
    X11_XWarpPointer(display, None, root, 0, 0, 0, 0, rect.x, rect.y);
slouken@7632
  1328
slouken@6501
  1329
    /* Center mouse in the fullscreen window. */
slouken@6501
  1330
    rect.x += (rect.w / 2);
slouken@6501
  1331
    rect.y += (rect.h / 2);
icculus@7827
  1332
    X11_XWarpPointer(display, None, root, 0, 0, 0, 0, rect.x, rect.y);
icculus@6467
  1333
icculus@6467
  1334
    /* Wait to be mapped, filter Unmap event out if it arrives. */
icculus@7827
  1335
    X11_XIfEvent(display, &ev, &isMapNotify, (XPointer)&data->xwindow);
icculus@7827
  1336
    X11_XCheckIfEvent(display, &ev, &isUnmapNotify, (XPointer)&data->xwindow);
icculus@6467
  1337
slouken@6662
  1338
    SDL_UpdateWindowGrab(window);
icculus@6467
  1339
}
icculus@6467
  1340
icculus@6467
  1341
static void
icculus@6467
  1342
X11_EndWindowFullscreenLegacy(_THIS, SDL_Window * window, SDL_VideoDisplay * _display)
icculus@6467
  1343
{
icculus@6467
  1344
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@6467
  1345
    SDL_DisplayData *displaydata = (SDL_DisplayData *) _display->driverdata;
icculus@6467
  1346
    Display *display = data->videodata->display;
icculus@6467
  1347
    const int screen = displaydata->screen;
icculus@6467
  1348
    Window root = RootWindow(display, screen);
slouken@6480
  1349
    Window fswindow = data->fswindow;
icculus@6467
  1350
    XEvent ev;
icculus@6467
  1351
slouken@6480
  1352
    if (!data->fswindow) {
icculus@6467
  1353
        return;  /* already not fullscreen, I hope. */
slouken@6480
  1354
    }
icculus@6467
  1355
slouken@6480
  1356
    data->fswindow = None;
icculus@6467
  1357
icculus@6467
  1358
#if SDL_VIDEO_DRIVER_X11_VIDMODE
icculus@6467
  1359
    if ( displaydata->use_vidmode ) {
icculus@7827
  1360
        X11_XF86VidModeLockModeSwitch(display, screen, False);
icculus@6467
  1361
    }
icculus@6467
  1362
#endif
icculus@6467
  1363
slouken@6662
  1364
    SDL_UpdateWindowGrab(window);
icculus@6467
  1365
icculus@7827
  1366
    X11_XReparentWindow(display, data->xwindow, root, window->x, window->y);
slouken@6480
  1367
slouken@6480
  1368
    /* flush these events so they don't confuse normal event handling */
icculus@7827
  1369
    X11_XSync(display, False);
icculus@7827
  1370
    X11_XCheckIfEvent(display, &ev, &isMapNotify, (XPointer)&data->xwindow);
icculus@7827
  1371
    X11_XCheckIfEvent(display, &ev, &isUnmapNotify, (XPointer)&data->xwindow);
icculus@6467
  1372
icculus@6467
  1373
    SetWindowBordered(display, screen, data->xwindow,
icculus@6467
  1374
                      (window->flags & SDL_WINDOW_BORDERLESS) == 0);
icculus@6467
  1375
icculus@7827
  1376
    X11_XWithdrawWindow(display, fswindow, screen);
slouken@6480
  1377
slouken@6480
  1378
    /* Wait to be unmapped. */
icculus@7827
  1379
    X11_XIfEvent(display, &ev, &isUnmapNotify, (XPointer)&fswindow);
icculus@7827
  1380
    X11_XDestroyWindow(display, fswindow);
icculus@6467
  1381
}
icculus@6467
  1382
icculus@6467
  1383
icculus@6467
  1384
void
icculus@6467
  1385
X11_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * _display, SDL_bool fullscreen)
icculus@6467
  1386
{
icculus@6467
  1387
    /* !!! FIXME: SDL_Hint? */
icculus@6467
  1388
    SDL_bool legacy = SDL_FALSE;
icculus@6467
  1389
    const char *env = SDL_getenv("SDL_VIDEO_X11_LEGACY_FULLSCREEN");
icculus@6467
  1390
    if (env) {
icculus@6467
  1391
        legacy = SDL_atoi(env);
icculus@6467
  1392
    } else {
slouken@6473
  1393
        SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
icculus@6467
  1394
        SDL_DisplayData *displaydata = (SDL_DisplayData *) _display->driverdata;
icculus@6467
  1395
        if ( displaydata->use_vidmode ) {
icculus@6467
  1396
            legacy = SDL_TRUE;  /* the new stuff only works with XRandR. */
slouken@6473
  1397
        } else if ( !videodata->net_wm ) {
slouken@6473
  1398
            legacy = SDL_TRUE;  /* The window manager doesn't support it */
icculus@6467
  1399
        } else {
icculus@6467
  1400
            /* !!! FIXME: look at the window manager name, and blacklist certain ones? */
icculus@6467
  1401
            /* http://stackoverflow.com/questions/758648/find-the-name-of-the-x-window-manager */
icculus@6467
  1402
            legacy = SDL_FALSE;  /* try the new way. */
icculus@6467
  1403
        }
icculus@6467
  1404
    }
icculus@6467
  1405
icculus@6467
  1406
    if (legacy) {
icculus@6467
  1407
        if (fullscreen) {
icculus@6467
  1408
            X11_BeginWindowFullscreenLegacy(_this, window, _display);
icculus@6467
  1409
        } else {
icculus@6467
  1410
            X11_EndWindowFullscreenLegacy(_this, window, _display);
icculus@6467
  1411
        }
icculus@6467
  1412
    } else {
icculus@6467
  1413
        X11_SetWindowFullscreenViaWM(_this, window, _display, fullscreen);
icculus@6467
  1414
    }
icculus@6467
  1415
}
icculus@6467
  1416
icculus@6467
  1417
slouken@5466
  1418
int
slouken@5466
  1419
X11_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
slouken@5466
  1420
{
slouken@5466
  1421
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5466
  1422
    Display *display = data->videodata->display;
slouken@5466
  1423
    Visual *visual = data->visual;
slouken@5466
  1424
    Colormap colormap = data->colormap;
slouken@5466
  1425
    XColor *colorcells;
slouken@5466
  1426
    int ncolors;
slouken@5466
  1427
    int rmask, gmask, bmask;
slouken@5466
  1428
    int rshift, gshift, bshift;
icculus@5981
  1429
    int i;
slouken@5466
  1430
slouken@5466
  1431
    if (visual->class != DirectColor) {
icculus@7037
  1432
        return SDL_SetError("Window doesn't have DirectColor visual");
slouken@5466
  1433
    }
slouken@5466
  1434
slouken@5466
  1435
    ncolors = visual->map_entries;
slouken@5466
  1436
    colorcells = SDL_malloc(ncolors * sizeof(XColor));
slouken@5466
  1437
    if (!colorcells) {
icculus@7037
  1438
        return SDL_OutOfMemory();
slouken@5466
  1439
    }
slouken@5466
  1440
slouken@5466
  1441
    rshift = 0;
slouken@5466
  1442
    rmask = visual->red_mask;
slouken@5466
  1443
    while (0 == (rmask & 1)) {
slouken@5466
  1444
        rshift++;
slouken@5466
  1445
        rmask >>= 1;
slouken@5466
  1446
    }
slouken@5466
  1447
slouken@5466
  1448
    gshift = 0;
slouken@5466
  1449
    gmask = visual->green_mask;
slouken@5466
  1450
    while (0 == (gmask & 1)) {
slouken@5466
  1451
        gshift++;
slouken@5466
  1452
        gmask >>= 1;
slouken@5466
  1453
    }
slouken@5466
  1454
slouken@5466
  1455
    bshift = 0;
slouken@5466
  1456
    bmask = visual->blue_mask;
slouken@5466
  1457
    while (0 == (bmask & 1)) {
slouken@5466
  1458
        bshift++;
slouken@5466
  1459
        bmask >>= 1;
slouken@5466
  1460
    }
slouken@5466
  1461
slouken@5466
  1462
    /* build the color table pixel values */
slouken@5466
  1463
    for (i = 0; i < ncolors; i++) {
slouken@5466
  1464
        Uint32 rbits = (rmask * i) / (ncolors - 1);
slouken@5466
  1465
        Uint32 gbits = (gmask * i) / (ncolors - 1);
slouken@5466
  1466
        Uint32 bbits = (bmask * i) / (ncolors - 1);
slouken@5466
  1467
        Uint32 pix = (rbits << rshift) | (gbits << gshift) | (bbits << bshift);
slouken@5466
  1468
slouken@5466
  1469
        colorcells[i].pixel = pix;
slouken@5466
  1470
slouken@5466
  1471
        colorcells[i].red = ramp[(0 * 256) + i];
slouken@5466
  1472
        colorcells[i].green = ramp[(1 * 256) + i];
slouken@5466
  1473
        colorcells[i].blue = ramp[(2 * 256) + i];
slouken@5466
  1474
slouken@5466
  1475
        colorcells[i].flags = DoRed | DoGreen | DoBlue;
slouken@5466
  1476
    }
slouken@5466
  1477
icculus@7827
  1478
    X11_XStoreColors(display, colormap, colorcells, ncolors);
icculus@7827
  1479
    X11_XFlush(display);
slouken@5466
  1480
    SDL_free(colorcells);
slouken@5466
  1481
slouken@5466
  1482
    return 0;
slouken@5466
  1483
}
slouken@5466
  1484
slouken@5302
  1485
void
slouken@6662
  1486
X11_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
slouken@1951
  1487
{
slouken@2875
  1488
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@2875
  1489
    Display *display = data->videodata->display;
slouken@4518
  1490
    SDL_bool oldstyle_fullscreen;
slouken@6662
  1491
    SDL_bool grab_keyboard;
slouken@2875
  1492
slouken@6662
  1493
    /* ICCCM2.0-compliant window managers can handle fullscreen windows
slouken@6662
  1494
       If we're using XVidMode to change resolution we need to confine
slouken@6662
  1495
       the cursor so we don't pan around the virtual desktop.
slouken@6662
  1496
     */
icculus@6467
  1497
    oldstyle_fullscreen = X11_IsWindowLegacyFullscreen(_this, window);
slouken@4518
  1498
slouken@6662
  1499
    if (oldstyle_fullscreen || grabbed) {
slouken@2875
  1500
        /* Try to grab the mouse */
icculus@10723
  1501
        if (!data->videodata->broken_pointer_grab) {
icculus@10723
  1502
            int attempts;
icculus@10723
  1503
            int result;
icculus@10723
  1504
drewb@10964
  1505
            /* Try for up to 5000ms (5s) to grab. If it still fails, stop trying. */
drewb@10964
  1506
            for (attempts = 0; attempts < 100; attempts++) {
icculus@10723
  1507
                result = X11_XGrabPointer(display, data->xwindow, True, 0, GrabModeAsync,
icculus@10723
  1508
                                 GrabModeAsync, data->xwindow, None, CurrentTime);
icculus@10723
  1509
                if (result == GrabSuccess) {
icculus@10723
  1510
                    break;
icculus@10723
  1511
                }
icculus@10723
  1512
                SDL_Delay(50);
slouken@2875
  1513
            }
icculus@10723
  1514
icculus@10723
  1515
            if (result != GrabSuccess) {
icculus@10723
  1516
                SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO, "The X server refused to let us grab the mouse. You might experience input bugs.");
icculus@10723
  1517
                data->videodata->broken_pointer_grab = SDL_TRUE;  /* don't try again. */
icculus@10723
  1518
            }
slouken@2875
  1519
        }
slouken@2875
  1520
slouken@2875
  1521
        /* Raise the window if we grab the mouse */
icculus@7827
  1522
        X11_XRaiseWindow(display, data->xwindow);
slouken@2875
  1523
slouken@2875
  1524
        /* Now grab the keyboard */
slouken@10499
  1525
        if (SDL_GetHintBoolean(SDL_HINT_GRAB_KEYBOARD, SDL_FALSE)) {
slouken@6662
  1526
            grab_keyboard = SDL_TRUE;
slouken@6662
  1527
        } else {
slouken@6662
  1528
            /* We need to do this with the old style override_redirect
slouken@6662
  1529
               fullscreen window otherwise we won't get keyboard focus.
slouken@6662
  1530
            */
slouken@6662
  1531
            grab_keyboard = oldstyle_fullscreen;
slouken@6662
  1532
        }
slouken@6662
  1533
        if (grab_keyboard) {
icculus@7827
  1534
            X11_XGrabKeyboard(display, data->xwindow, True, GrabModeAsync,
slouken@6662
  1535
                          GrabModeAsync, CurrentTime);
slouken@6662
  1536
        }
slouken@2875
  1537
    } else {
icculus@7827
  1538
        X11_XUngrabPointer(display, CurrentTime);
icculus@7827
  1539
        X11_XUngrabKeyboard(display, CurrentTime);
slouken@2875
  1540
    }
icculus@7827
  1541
    X11_XSync(display, False);
slouken@1951
  1542
}
slouken@1951
  1543
slouken@1951
  1544
void
slouken@1951
  1545
X11_DestroyWindow(_THIS, SDL_Window * window)
slouken@1951
  1546
{
slouken@1951
  1547
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@8905
  1548
slouken@1951
  1549
    if (data) {
bob@2323
  1550
        SDL_VideoData *videodata = (SDL_VideoData *) data->videodata;
bob@2323
  1551
        Display *display = videodata->display;
bob@2323
  1552
        int numwindows = videodata->numwindows;
bob@2323
  1553
        SDL_WindowData **windowlist = videodata->windowlist;
bob@2323
  1554
        int i;
bob@2323
  1555
bob@2323
  1556
        if (windowlist) {
bob@2324
  1557
            for (i = 0; i < numwindows; ++i) {
slouken@3685
  1558
                if (windowlist[i] && (windowlist[i]->window == window)) {
bob@2324
  1559
                    windowlist[i] = windowlist[numwindows - 1];
bob@2324
  1560
                    windowlist[numwindows - 1] = NULL;
bob@2324
  1561
                    videodata->numwindows--;
bob@2324
  1562
                    break;
bob@2324
  1563
                }
bob@2323
  1564
            }
bob@2323
  1565
        }
slouken@1951
  1566
#ifdef X_HAVE_UTF8_STRING
slouken@1951
  1567
        if (data->ic) {
icculus@7827
  1568
            X11_XDestroyIC(data->ic);
slouken@1951
  1569
        }
slouken@1951
  1570
#endif
slouken@1951
  1571
        if (data->created) {
icculus@7827
  1572
            X11_XDestroyWindow(display, data->xwindow);
icculus@7827
  1573
            X11_XFlush(display);
slouken@1951
  1574
        }
slouken@1951
  1575
        SDL_free(data);
slouken@1951
  1576
    }
slouken@8978
  1577
    window->driverdata = NULL;
slouken@1951
  1578
}
slouken@1951
  1579
slouken@1951
  1580
SDL_bool
slouken@1951
  1581
X11_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1951
  1582
{
slouken@4510
  1583
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@4510
  1584
    Display *display = data->videodata->display;
slouken@4510
  1585
slouken@4510
  1586
    if (info->version.major == SDL_MAJOR_VERSION &&
slouken@4510
  1587
        info->version.minor == SDL_MINOR_VERSION) {
slouken@4510
  1588
        info->subsystem = SDL_SYSWM_X11;
slouken@5056
  1589
        info->info.x11.display = display;
slouken@5056
  1590
        info->info.x11.window = data->xwindow;
slouken@1951
  1591
        return SDL_TRUE;
slouken@1951
  1592
    } else {
philipp@10945
  1593
        SDL_SetError("Application not compiled with SDL %d.%d",
slouken@1951
  1594
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1951
  1595
        return SDL_FALSE;
slouken@1951
  1596
    }
slouken@1951
  1597
}
slouken@1951
  1598
rush@8932
  1599
int
icculus@8935
  1600
X11_SetWindowHitTest(SDL_Window *window, SDL_bool enabled)
rush@8932
  1601
{
icculus@8935
  1602
    return 0;  /* just succeed, the real work is done elsewhere. */
rush@8932
  1603
}
rush@8932
  1604
slouken@5481
  1605
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
  1606
slouken@1951
  1607
/* vi: set ts=4 sw=4 expandtab: */