src/video/x11/SDL_x11window.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 10 Feb 2011 11:39:08 -0800
changeset 5243 3a8a452b49f0
parent 5183 227d81a4b66c
child 5246 58265e606e4e
permissions -rw-r--r--
Removed gamma support since it wasn't widely used and not well supported.
slouken@1951
     1
/*
slouken@1951
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 Sam Lantinga
slouken@1951
     4
slouken@1951
     5
    This library is free software; you can redistribute it and/or
slouken@1951
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1951
     7
    License as published by the Free Software Foundation; either
slouken@1951
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1951
     9
slouken@1951
    10
    This library is distributed in the hope that it will be useful,
slouken@1951
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1951
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1951
    13
    Lesser General Public License for more details.
slouken@1951
    14
slouken@1951
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1951
    16
    License along with this library; if not, write to the Free Software
slouken@1951
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1951
    18
slouken@1951
    19
    Sam Lantinga
slouken@1951
    20
    slouken@libsdl.org
slouken@1951
    21
*/
slouken@1951
    22
#include "SDL_config.h"
slouken@1951
    23
slouken@1951
    24
#include "../SDL_sysvideo.h"
slouken@4465
    25
#include "../SDL_pixels_c.h"
slouken@1951
    26
#include "../../events/SDL_keyboard_c.h"
slouken@2940
    27
#include "../../events/SDL_mouse_c.h"
slouken@1951
    28
slouken@1951
    29
#include "SDL_x11video.h"
slouken@2940
    30
#include "SDL_x11mouse.h"
eligottlieb@4782
    31
#include "SDL_x11shape.h"
slouken@1951
    32
slouken@3161
    33
#ifdef SDL_VIDEO_DRIVER_PANDORA
slouken@3161
    34
#include "SDL_x11opengles.h"
slouken@3161
    35
#endif
slouken@3161
    36
slouken@4465
    37
#include "SDL_timer.h"
slouken@3241
    38
#include "SDL_syswm.h"
slouken@3241
    39
slouken@3077
    40
#define _NET_WM_STATE_REMOVE    0l
slouken@3077
    41
#define _NET_WM_STATE_ADD       1l
slouken@3077
    42
#define _NET_WM_STATE_TOGGLE    2l
slouken@3077
    43
slouken@4518
    44
static SDL_bool
slouken@4522
    45
X11_IsWindowOldFullscreen(_THIS, SDL_Window * window)
slouken@4518
    46
{
slouken@4522
    47
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@4518
    48
slouken@4518
    49
    /* ICCCM2.0-compliant window managers can handle fullscreen windows */
slouken@4518
    50
    if ((window->flags & SDL_WINDOW_FULLSCREEN) && !videodata->net_wm) {
slouken@4518
    51
        return SDL_TRUE;
slouken@4518
    52
    } else {
slouken@4518
    53
        return SDL_FALSE;
slouken@4518
    54
    }
slouken@4518
    55
}
slouken@4518
    56
slouken@4522
    57
static SDL_bool
slouken@4522
    58
X11_IsWindowMapped(_THIS, SDL_Window * window)
slouken@4522
    59
{
slouken@4522
    60
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@4522
    61
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@4522
    62
    XWindowAttributes attr;
slouken@4522
    63
slouken@4522
    64
    XGetWindowAttributes(videodata->display, data->xwindow, &attr);
slouken@4522
    65
    if (attr.map_state != IsUnmapped) {
slouken@4522
    66
        return SDL_TRUE;
slouken@4522
    67
    } else {
slouken@4522
    68
        return SDL_FALSE;
slouken@4522
    69
    }
slouken@4522
    70
}
slouken@4522
    71
slouken@4522
    72
static int
slouken@4522
    73
X11_GetWMStateProperty(_THIS, SDL_Window * window, Atom atoms[3])
slouken@4522
    74
{
slouken@4522
    75
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@4522
    76
    int count = 0;
slouken@4522
    77
slouken@4522
    78
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@4522
    79
        atoms[count++] = data->_NET_WM_STATE_FULLSCREEN;
slouken@4522
    80
    }
slouken@4522
    81
    if (window->flags & SDL_WINDOW_MAXIMIZED) {
slouken@4522
    82
        atoms[count++] = data->_NET_WM_STATE_MAXIMIZED_VERT;
slouken@4522
    83
        atoms[count++] = data->_NET_WM_STATE_MAXIMIZED_HORZ;
slouken@4522
    84
    }
slouken@4522
    85
    return count;
slouken@4522
    86
}
slouken@4522
    87
slouken@2931
    88
static void
slouken@2931
    89
X11_GetDisplaySize(_THIS, SDL_Window * window, int *w, int *h)
slouken@2931
    90
{
slouken@2931
    91
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@2931
    92
    SDL_DisplayData *displaydata =
slouken@3685
    93
        (SDL_DisplayData *) window->display->driverdata;
slouken@2931
    94
    XWindowAttributes attr;
slouken@2931
    95
slouken@4628
    96
    XGetWindowAttributes(data->display, RootWindow(data->display, displaydata->screen), &attr);
slouken@4628
    97
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@4628
    98
        /* The bounds when this window is visible is the fullscreen mode */
slouken@4628
    99
        SDL_DisplayMode fullscreen_mode;
slouken@4628
   100
        if (SDL_GetWindowDisplayMode(window, &fullscreen_mode) == 0) {
slouken@4628
   101
            attr.width = fullscreen_mode.w;
slouken@4628
   102
            attr.height = fullscreen_mode.h;
slouken@4628
   103
        }
slouken@4628
   104
    }
slouken@2931
   105
    if (w) {
slouken@2931
   106
        *w = attr.width;
slouken@2931
   107
    }
slouken@2931
   108
    if (h) {
slouken@2931
   109
        *h = attr.height;
slouken@2931
   110
    }
slouken@2931
   111
}
slouken@2931
   112
slouken@1951
   113
static int
slouken@1951
   114
SetupWindowData(_THIS, SDL_Window * window, Window w, BOOL created)
slouken@1951
   115
{
slouken@1951
   116
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1951
   117
    SDL_WindowData *data;
slouken@1951
   118
    int numwindows = videodata->numwindows;
bob@2324
   119
    int windowlistlength = videodata->windowlistlength;
slouken@1951
   120
    SDL_WindowData **windowlist = videodata->windowlist;
slouken@1951
   121
slouken@1951
   122
    /* Allocate the window data */
bob@2323
   123
    data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
slouken@1951
   124
    if (!data) {
slouken@1951
   125
        SDL_OutOfMemory();
slouken@1951
   126
        return -1;
slouken@1951
   127
    }
slouken@3685
   128
    data->window = window;
slouken@3685
   129
    data->xwindow = w;
slouken@1951
   130
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   131
    if (SDL_X11_HAVE_UTF8) {
slouken@1951
   132
        data->ic =
slouken@1951
   133
            pXCreateIC(videodata->im, XNClientWindow, w, XNFocusWindow, w,
slouken@1951
   134
                       XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
slouken@1951
   135
                       XNResourceName, videodata->classname, XNResourceClass,
slouken@1951
   136
                       videodata->classname, NULL);
slouken@1951
   137
    }
slouken@1951
   138
#endif
slouken@1951
   139
    data->created = created;
slouken@1951
   140
    data->videodata = videodata;
slouken@1951
   141
bob@2323
   142
    /* Associate the data with the window */
bob@2324
   143
bob@2324
   144
    if (numwindows < windowlistlength) {
bob@2324
   145
        windowlist[numwindows] = data;
bob@2324
   146
        videodata->numwindows++;
bob@2324
   147
    } else {
bob@2324
   148
        windowlist =
bob@2324
   149
            (SDL_WindowData **) SDL_realloc(windowlist,
bob@2324
   150
                                            (numwindows +
bob@2324
   151
                                             1) * sizeof(*windowlist));
bob@2324
   152
        if (!windowlist) {
bob@2324
   153
            SDL_OutOfMemory();
bob@2324
   154
            SDL_free(data);
bob@2324
   155
            return -1;
bob@2323
   156
        }
bob@2324
   157
        windowlist[numwindows] = data;
bob@2324
   158
        videodata->numwindows++;
bob@2324
   159
        videodata->windowlistlength++;
bob@2324
   160
        videodata->windowlist = windowlist;
bob@2323
   161
    }
bob@2323
   162
slouken@1951
   163
    /* Fill in the SDL window with the window data */
slouken@1951
   164
    {
slouken@1951
   165
        XWindowAttributes attrib;
slouken@1951
   166
slouken@1951
   167
        XGetWindowAttributes(data->videodata->display, w, &attrib);
slouken@1951
   168
        window->x = attrib.x;
slouken@1951
   169
        window->y = attrib.y;
slouken@1951
   170
        window->w = attrib.width;
slouken@1951
   171
        window->h = attrib.height;
slouken@1951
   172
        if (attrib.map_state != IsUnmapped) {
slouken@1951
   173
            window->flags |= SDL_WINDOW_SHOWN;
slouken@1951
   174
        } else {
slouken@1951
   175
            window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1951
   176
        }
slouken@5182
   177
        data->visual = attrib.visual;
slouken@1951
   178
    }
slouken@3077
   179
slouken@3077
   180
    {
slouken@4518
   181
        Atom _NET_WM_STATE = data->videodata->_NET_WM_STATE;
slouken@4518
   182
        Atom _NET_WM_STATE_MAXIMIZED_VERT = data->videodata->_NET_WM_STATE_MAXIMIZED_VERT;
slouken@4518
   183
        Atom _NET_WM_STATE_MAXIMIZED_HORZ = data->videodata->_NET_WM_STATE_MAXIMIZED_HORZ;
slouken@4518
   184
        Atom _NET_WM_STATE_FULLSCREEN = data->videodata->_NET_WM_STATE_FULLSCREEN;
slouken@3077
   185
        Atom actualType;
slouken@3077
   186
        int actualFormat;
slouken@3077
   187
        unsigned long i, numItems, bytesAfter;
slouken@3077
   188
        unsigned char *propertyValue = NULL;
slouken@3077
   189
        long maxLength = 1024;
slouken@3077
   190
slouken@3077
   191
        if (XGetWindowProperty(data->videodata->display, w, _NET_WM_STATE,
slouken@3078
   192
                               0l, maxLength, False, XA_ATOM, &actualType,
slouken@3078
   193
                               &actualFormat, &numItems, &bytesAfter,
slouken@3078
   194
                               &propertyValue) == Success) {
slouken@3078
   195
            Atom *atoms = (Atom *) propertyValue;
slouken@3077
   196
            int maximized = 0;
slouken@4518
   197
            int fullscreen = 0;
slouken@3077
   198
slouken@3077
   199
            for (i = 0; i < numItems; ++i) {
slouken@3077
   200
                if (atoms[i] == _NET_WM_STATE_MAXIMIZED_VERT) {
slouken@3077
   201
                    maximized |= 1;
slouken@3077
   202
                } else if (atoms[i] == _NET_WM_STATE_MAXIMIZED_HORZ) {
slouken@3077
   203
                    maximized |= 2;
slouken@4518
   204
                } else if ( atoms[i] == _NET_WM_STATE_FULLSCREEN) {
slouken@4518
   205
                    fullscreen = 1;
slouken@3077
   206
                }
slouken@3077
   207
            }
slouken@3077
   208
            if (maximized == 3) {
slouken@3077
   209
                window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@4518
   210
            }  else if (fullscreen == 1) {
slouken@4518
   211
                window->flags |= SDL_WINDOW_FULLSCREEN;
slouken@3077
   212
            }
slouken@3077
   213
            XFree(propertyValue);
slouken@3077
   214
        }
slouken@3077
   215
    }
slouken@3077
   216
slouken@1951
   217
    /* FIXME: How can I tell?
slouken@1951
   218
       {
slouken@1951
   219
       DWORD style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1951
   220
       if (style & WS_VISIBLE) {
slouken@1951
   221
       if (style & (WS_BORDER | WS_THICKFRAME)) {
slouken@1951
   222
       window->flags &= ~SDL_WINDOW_BORDERLESS;
slouken@1951
   223
       } else {
slouken@1951
   224
       window->flags |= SDL_WINDOW_BORDERLESS;
slouken@1951
   225
       }
slouken@1951
   226
       if (style & WS_THICKFRAME) {
slouken@1951
   227
       window->flags |= SDL_WINDOW_RESIZABLE;
slouken@1951
   228
       } else {
slouken@1951
   229
       window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@1951
   230
       }
slouken@1951
   231
       if (style & WS_MINIMIZE) {
slouken@1951
   232
       window->flags |= SDL_WINDOW_MINIMIZED;
slouken@1951
   233
       } else {
slouken@1951
   234
       window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@1951
   235
       }
slouken@1951
   236
       }
slouken@1951
   237
       if (GetFocus() == hwnd) {
slouken@1951
   238
       int index = data->videodata->keyboard;
slouken@1951
   239
       window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@3685
   240
       SDL_SetKeyboardFocus(index, data->window);
slouken@1951
   241
slouken@1951
   242
       if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1951
   243
       RECT rect;
slouken@1951
   244
       GetClientRect(hwnd, &rect);
slouken@1951
   245
       ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1951
   246
       ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1951
   247
       ClipCursor(&rect);
slouken@1951
   248
       }
slouken@1951
   249
       }
slouken@1951
   250
     */
slouken@1951
   251
slouken@1951
   252
    /* All done! */
slouken@1951
   253
    window->driverdata = data;
slouken@1951
   254
    return 0;
slouken@1951
   255
}
slouken@1951
   256
slouken@1951
   257
int
slouken@1951
   258
X11_CreateWindow(_THIS, SDL_Window * window)
slouken@1951
   259
{
slouken@1951
   260
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   261
    SDL_DisplayData *displaydata =
slouken@3685
   262
        (SDL_DisplayData *) window->display->driverdata;
slouken@4521
   263
    Display *display = data->display;
slouken@4521
   264
    int screen = displaydata->screen;
slouken@1951
   265
    Visual *visual;
slouken@1951
   266
    int depth;
slouken@1951
   267
    XSetWindowAttributes xattr;
slouken@1951
   268
    int x, y;
slouken@1951
   269
    Window w;
slouken@1951
   270
    XSizeHints *sizehints;
slouken@1951
   271
    XWMHints *wmhints;
slouken@1951
   272
    XClassHint *classhints;
slouken@4518
   273
    SDL_bool oldstyle_fullscreen;
slouken@4521
   274
    Atom _NET_WM_WINDOW_TYPE;
slouken@4521
   275
    Atom _NET_WM_WINDOW_TYPE_NORMAL;
slouken@4522
   276
    int wmstate_count;
slouken@4522
   277
    Atom wmstate_atoms[3];
slouken@4518
   278
slouken@4518
   279
    /* ICCCM2.0-compliant window managers can handle fullscreen windows */
slouken@4522
   280
    oldstyle_fullscreen = X11_IsWindowOldFullscreen(_this, window);
slouken@1951
   281
slouken@1951
   282
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@1951
   283
/* FIXME
slouken@1951
   284
    if ( use_xinerama ) {
slouken@1951
   285
        x = xinerama_info.x_org;
slouken@1951
   286
        y = xinerama_info.y_org;
slouken@1951
   287
    }
slouken@1951
   288
*/
slouken@1951
   289
#endif
slouken@5088
   290
#if SDL_VIDEO_OPENGL_GLX
slouken@1951
   291
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@1952
   292
        XVisualInfo *vinfo;
slouken@1952
   293
slouken@4521
   294
        vinfo = X11_GL_GetVisual(_this, display, screen);
slouken@1952
   295
        if (!vinfo) {
slouken@1952
   296
            return -1;
slouken@1952
   297
        }
slouken@1952
   298
        visual = vinfo->visual;
slouken@1952
   299
        depth = vinfo->depth;
slouken@1952
   300
        XFree(vinfo);
slouken@1952
   301
    } else
slouken@1952
   302
#endif
slouken@3161
   303
#ifdef SDL_VIDEO_DRIVER_PANDORA
slouken@3161
   304
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@3161
   305
        XVisualInfo *vinfo;
slouken@3161
   306
slouken@4521
   307
        vinfo = X11_GLES_GetVisual(_this, display, screen);
slouken@3161
   308
        if (!vinfo) {
slouken@3161
   309
            return -1;
slouken@3161
   310
        }
slouken@3161
   311
        visual = vinfo->visual;
slouken@3161
   312
        depth = vinfo->depth;
slouken@3161
   313
        XFree(vinfo);
slouken@3161
   314
    } else
slouken@3161
   315
#endif
slouken@1952
   316
    {
slouken@1951
   317
        visual = displaydata->visual;
slouken@1951
   318
        depth = displaydata->depth;
slouken@1951
   319
    }
slouken@1951
   320
slouken@4518
   321
    if (oldstyle_fullscreen) {
slouken@1951
   322
        xattr.override_redirect = True;
slouken@1951
   323
    } else {
slouken@1951
   324
        xattr.override_redirect = False;
slouken@1951
   325
    }
slouken@1951
   326
    xattr.background_pixel = 0;
slouken@1951
   327
    xattr.border_pixel = 0;
slouken@5243
   328
    xattr.colormap = XCreateColormap(display, RootWindow(display, screen), visual, AllocNone);
slouken@1951
   329
slouken@4518
   330
    if (oldstyle_fullscreen
slouken@2876
   331
        || window->x == SDL_WINDOWPOS_CENTERED) {
slouken@2931
   332
        X11_GetDisplaySize(_this, window, &x, NULL);
slouken@2931
   333
        x = (x - window->w) / 2;
slouken@1951
   334
    } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
slouken@1951
   335
        x = 0;
slouken@1951
   336
    } else {
slouken@1951
   337
        x = window->x;
slouken@1951
   338
    }
slouken@4518
   339
    if (oldstyle_fullscreen
slouken@2876
   340
        || window->y == SDL_WINDOWPOS_CENTERED) {
slouken@2931
   341
        X11_GetDisplaySize(_this, window, NULL, &y);
slouken@2931
   342
        y = (y - window->h) / 2;
slouken@1951
   343
    } else if (window->y == SDL_WINDOWPOS_UNDEFINED) {
slouken@1951
   344
        y = 0;
slouken@1951
   345
    } else {
slouken@1951
   346
        y = window->y;
slouken@1951
   347
    }
slouken@1951
   348
slouken@4521
   349
    w = XCreateWindow(display, RootWindow(display, screen), x, y,
slouken@1951
   350
                      window->w, window->h, 0, depth, InputOutput, visual,
slouken@1951
   351
                      (CWOverrideRedirect | CWBackPixel | CWBorderPixel |
slouken@1951
   352
                       CWColormap), &xattr);
slouken@1952
   353
    if (!w) {
slouken@1952
   354
        SDL_SetError("Couldn't create window");
slouken@1952
   355
        return -1;
slouken@1952
   356
    }
slouken@3161
   357
#if SDL_VIDEO_DRIVER_PANDORA
slouken@3161
   358
    /* Create the GLES window surface */
slouken@3161
   359
    _this->gles_data->egl_surface =
slouken@3161
   360
        _this->gles_data->eglCreateWindowSurface(_this->gles_data->
slouken@3161
   361
                                                 egl_display,
slouken@3161
   362
                                                 _this->gles_data->egl_config,
slouken@3161
   363
                                                 (NativeWindowType) w, NULL);
slouken@3161
   364
slouken@3161
   365
    if (_this->gles_data->egl_surface == EGL_NO_SURFACE) {
slouken@3161
   366
        SDL_SetError("Could not create GLES window surface");
slouken@3161
   367
        return -1;
slouken@3161
   368
    }
slouken@3161
   369
#endif
slouken@1951
   370
slouken@1951
   371
    sizehints = XAllocSizeHints();
slouken@1951
   372
    if (sizehints) {
slouken@3225
   373
        if (!(window->flags & SDL_WINDOW_RESIZABLE)
slouken@4518
   374
            || oldstyle_fullscreen) {
slouken@1951
   375
            sizehints->min_width = sizehints->max_width = window->w;
slouken@1951
   376
            sizehints->min_height = sizehints->max_height = window->h;
slouken@3225
   377
            sizehints->flags = PMaxSize | PMinSize;
slouken@1951
   378
        }
slouken@4518
   379
        if (!oldstyle_fullscreen
slouken@1951
   380
            && window->x != SDL_WINDOWPOS_UNDEFINED
slouken@1951
   381
            && window->y != SDL_WINDOWPOS_UNDEFINED) {
slouken@1951
   382
            sizehints->x = x;
slouken@1951
   383
            sizehints->y = y;
slouken@1951
   384
            sizehints->flags |= USPosition;
slouken@1951
   385
        }
slouken@4521
   386
        XSetWMNormalHints(display, w, sizehints);
slouken@1951
   387
        XFree(sizehints);
slouken@1951
   388
    }
slouken@1951
   389
slouken@4518
   390
    if ((window->flags & SDL_WINDOW_BORDERLESS) || oldstyle_fullscreen) {
slouken@1951
   391
        SDL_bool set;
slouken@1951
   392
        Atom WM_HINTS;
slouken@1951
   393
slouken@1951
   394
        /* We haven't modified the window manager hints yet */
slouken@1951
   395
        set = SDL_FALSE;
slouken@1951
   396
slouken@1951
   397
        /* First try to set MWM hints */
slouken@4521
   398
        WM_HINTS = XInternAtom(display, "_MOTIF_WM_HINTS", True);
slouken@1951
   399
        if (WM_HINTS != None) {
slouken@1951
   400
            /* Hints used by Motif compliant window managers */
slouken@1951
   401
            struct
slouken@1951
   402
            {
slouken@1951
   403
                unsigned long flags;
slouken@1951
   404
                unsigned long functions;
slouken@1951
   405
                unsigned long decorations;
slouken@1951
   406
                long input_mode;
slouken@1951
   407
                unsigned long status;
slouken@1951
   408
            } MWMHints = {
slouken@1951
   409
            (1L << 1), 0, 0, 0, 0};
slouken@1951
   410
slouken@4521
   411
            XChangeProperty(display, w, WM_HINTS, WM_HINTS, 32,
slouken@1951
   412
                            PropModeReplace, (unsigned char *) &MWMHints,
slouken@4521
   413
                            sizeof(MWMHints) / 4);
slouken@1951
   414
            set = SDL_TRUE;
slouken@1951
   415
        }
slouken@1951
   416
        /* Now try to set KWM hints */
slouken@4521
   417
        WM_HINTS = XInternAtom(display, "KWM_WIN_DECORATION", True);
slouken@1951
   418
        if (WM_HINTS != None) {
slouken@1951
   419
            long KWMHints = 0;
slouken@1951
   420
slouken@4521
   421
            XChangeProperty(display, w, WM_HINTS, WM_HINTS, 32,
slouken@1951
   422
                            PropModeReplace,
slouken@1951
   423
                            (unsigned char *) &KWMHints,
slouken@4521
   424
                            sizeof(KWMHints) / 4);
slouken@1951
   425
            set = SDL_TRUE;
slouken@1951
   426
        }
slouken@1951
   427
        /* Now try to set GNOME hints */
slouken@4521
   428
        WM_HINTS = XInternAtom(display, "_WIN_HINTS", True);
slouken@1951
   429
        if (WM_HINTS != None) {
slouken@1951
   430
            long GNOMEHints = 0;
slouken@1951
   431
slouken@4521
   432
            XChangeProperty(display, w, WM_HINTS, WM_HINTS, 32,
slouken@1951
   433
                            PropModeReplace,
slouken@1951
   434
                            (unsigned char *) &GNOMEHints,
slouken@4521
   435
                            sizeof(GNOMEHints) / 4);
slouken@1951
   436
            set = SDL_TRUE;
slouken@1951
   437
        }
slouken@1951
   438
        /* Finally set the transient hints if necessary */
slouken@1951
   439
        if (!set) {
slouken@4521
   440
            XSetTransientForHint(display, w, RootWindow(display, screen));
slouken@1951
   441
        }
slouken@1951
   442
    } else {
slouken@1951
   443
        SDL_bool set;
slouken@1951
   444
        Atom WM_HINTS;
slouken@1951
   445
slouken@1951
   446
        /* We haven't modified the window manager hints yet */
slouken@1951
   447
        set = SDL_FALSE;
slouken@1951
   448
slouken@1951
   449
        /* First try to unset MWM hints */
slouken@4521
   450
        WM_HINTS = XInternAtom(display, "_MOTIF_WM_HINTS", True);
slouken@1951
   451
        if (WM_HINTS != None) {
slouken@4521
   452
            XDeleteProperty(display, w, WM_HINTS);
slouken@1951
   453
            set = SDL_TRUE;
slouken@1951
   454
        }
slouken@1951
   455
        /* Now try to unset KWM hints */
slouken@4521
   456
        WM_HINTS = XInternAtom(display, "KWM_WIN_DECORATION", True);
slouken@1951
   457
        if (WM_HINTS != None) {
slouken@4521
   458
            XDeleteProperty(display, w, WM_HINTS);
slouken@1951
   459
            set = SDL_TRUE;
slouken@1951
   460
        }
slouken@1951
   461
        /* Now try to unset GNOME hints */
slouken@4521
   462
        WM_HINTS = XInternAtom(display, "_WIN_HINTS", True);
slouken@1951
   463
        if (WM_HINTS != None) {
slouken@4521
   464
            XDeleteProperty(display, w, WM_HINTS);
slouken@1951
   465
            set = SDL_TRUE;
slouken@1951
   466
        }
slouken@1951
   467
        /* Finally unset the transient hints if necessary */
slouken@1951
   468
        if (!set) {
slouken@4555
   469
            XDeleteProperty(display, w, XA_WM_TRANSIENT_FOR);
slouken@1951
   470
        }
slouken@1951
   471
    }
slouken@1951
   472
slouken@1951
   473
    /* Set the input hints so we get keyboard input */
slouken@1951
   474
    wmhints = XAllocWMHints();
slouken@1951
   475
    if (wmhints) {
slouken@1951
   476
        wmhints->input = True;
slouken@1956
   477
        wmhints->flags = InputHint;
slouken@4521
   478
        XSetWMHints(display, w, wmhints);
slouken@1951
   479
        XFree(wmhints);
slouken@1951
   480
    }
slouken@1951
   481
slouken@1951
   482
    /* Set the class hints so we can get an icon (AfterStep) */
slouken@1951
   483
    classhints = XAllocClassHint();
slouken@1951
   484
    if (classhints != NULL) {
slouken@1951
   485
        classhints->res_name = data->classname;
slouken@1951
   486
        classhints->res_class = data->classname;
slouken@4521
   487
        XSetClassHint(display, w, classhints);
slouken@1951
   488
        XFree(classhints);
slouken@1951
   489
    }
slouken@1951
   490
slouken@4522
   491
    /* Set the window manager state */
slouken@4522
   492
    wmstate_count = X11_GetWMStateProperty(_this, window, wmstate_atoms);
slouken@4522
   493
    if (wmstate_count > 0) {
slouken@4522
   494
        XChangeProperty(display, w, data->_NET_WM_STATE, XA_ATOM, 32,
slouken@4522
   495
                        PropModeReplace,
slouken@4522
   496
                        (unsigned char *)wmstate_atoms, wmstate_count);
slouken@4522
   497
    } else {
slouken@4522
   498
        XDeleteProperty(display, w, data->_NET_WM_STATE);
slouken@4518
   499
    }
slouken@4518
   500
slouken@4521
   501
    /* Let the window manager know we're a "normal" window */
slouken@4521
   502
    _NET_WM_WINDOW_TYPE = XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
slouken@4521
   503
    _NET_WM_WINDOW_TYPE_NORMAL = XInternAtom(display, "_NET_WM_WINDOW_TYPE_NORMAL", False);
slouken@4521
   504
    XChangeProperty(display, w, _NET_WM_WINDOW_TYPE, XA_ATOM, 32,
slouken@4521
   505
                    PropModeReplace,
slouken@4521
   506
                    (unsigned char *)&_NET_WM_WINDOW_TYPE_NORMAL, 1);
slouken@4521
   507
slouken@1951
   508
    /* Allow the window to be deleted by the window manager */
slouken@4521
   509
    XSetWMProtocols(display, w, &data->WM_DELETE_WINDOW, 1);
slouken@1951
   510
slouken@1951
   511
    if (SetupWindowData(_this, window, w, SDL_TRUE) < 0) {
slouken@4521
   512
        XDestroyWindow(display, w);
slouken@1951
   513
        return -1;
slouken@1951
   514
    }
bob@2325
   515
#ifdef X_HAVE_UTF8_STRING
bob@2325
   516
    {
bob@2325
   517
        Uint32 fevent = 0;
bob@2325
   518
        pXGetICValues(((SDL_WindowData *) window->driverdata)->ic,
bob@2325
   519
                      XNFilterEvents, &fevent, NULL);
slouken@4521
   520
        XSelectInput(display, w,
bob@2325
   521
                     (FocusChangeMask | EnterWindowMask | LeaveWindowMask |
bob@2325
   522
                      ExposureMask | ButtonPressMask | ButtonReleaseMask |
bob@2325
   523
                      PointerMotionMask | KeyPressMask | KeyReleaseMask |
bob@2325
   524
                      PropertyChangeMask | StructureNotifyMask |
bob@2325
   525
                      KeymapStateMask | fevent));
bob@2325
   526
    }
bob@2325
   527
#else
kazeuser@2718
   528
    {
slouken@4521
   529
        XSelectInput(display, w,
slouken@2725
   530
                     (FocusChangeMask | EnterWindowMask | LeaveWindowMask |
slouken@2725
   531
                      ExposureMask | ButtonPressMask | ButtonReleaseMask |
slouken@2725
   532
                      PointerMotionMask | KeyPressMask | KeyReleaseMask |
slouken@2725
   533
                      PropertyChangeMask | StructureNotifyMask |
slouken@2725
   534
                      KeymapStateMask));
kazeuser@2718
   535
    }
bob@2325
   536
#endif
bob@2325
   537
slouken@5031
   538
    XFlush(display);
slouken@5031
   539
slouken@1951
   540
    return 0;
slouken@1951
   541
}
slouken@1951
   542
slouken@1951
   543
int
bob@2324
   544
X11_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1951
   545
{
slouken@1951
   546
    Window w = (Window) data;
slouken@1951
   547
slouken@4518
   548
    window->title = X11_GetWindowTitle(_this, w);
slouken@1951
   549
slouken@1951
   550
    if (SetupWindowData(_this, window, w, SDL_FALSE) < 0) {
slouken@1951
   551
        return -1;
slouken@1951
   552
    }
slouken@1951
   553
    return 0;
slouken@1951
   554
}
slouken@1951
   555
slouken@4518
   556
char *
slouken@4518
   557
X11_GetWindowTitle(_THIS, Window xwindow)
slouken@4518
   558
{
slouken@4518
   559
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@4518
   560
    Display *display = data->display;
slouken@4518
   561
    int status, real_format;
slouken@4518
   562
    Atom real_type;
slouken@4518
   563
    unsigned long items_read, items_left;
slouken@4518
   564
    unsigned char *propdata;
slouken@4518
   565
    char *title = NULL;
slouken@4518
   566
slouken@4518
   567
    status = XGetWindowProperty(display, xwindow, data->_NET_WM_NAME,
slouken@4518
   568
                0L, 8192L, False, data->UTF8_STRING, &real_type, &real_format,
slouken@4518
   569
                &items_read, &items_left, &propdata);
slouken@4518
   570
    if (status == Success) {
slouken@4518
   571
        title = SDL_strdup(SDL_static_cast(char*, propdata));
slouken@4518
   572
        XFree(propdata);
slouken@4518
   573
    } else {
slouken@4518
   574
        status = XGetWindowProperty(display, xwindow, XA_WM_NAME,
slouken@4518
   575
                    0L, 8192L, False, XA_STRING, &real_type, &real_format,
slouken@4518
   576
                    &items_read, &items_left, &propdata);
slouken@4518
   577
        if (status == Success) {
slouken@4518
   578
            title = SDL_iconv_string("UTF-8", "", SDL_static_cast(char*, propdata), items_read+1);
slouken@4518
   579
        } else {
slouken@4518
   580
            title = SDL_strdup("");
slouken@4518
   581
        }
slouken@4518
   582
    }
slouken@4518
   583
    return title;
slouken@4518
   584
}
slouken@4518
   585
slouken@1951
   586
void
slouken@1951
   587
X11_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1951
   588
{
slouken@1951
   589
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   590
    Display *display = data->videodata->display;
slouken@1951
   591
    XTextProperty titleprop, iconprop;
slouken@1951
   592
    Status status;
slouken@1951
   593
    const char *title = window->title;
slouken@1951
   594
    const char *icon = NULL;
slouken@1951
   595
slouken@1951
   596
#ifdef X_HAVE_UTF8_STRING
slouken@4518
   597
    Atom _NET_WM_NAME = data->videodata->_NET_WM_NAME;
slouken@4518
   598
    Atom _NET_WM_ICON_NAME = data->videodata->_NET_WM_ICON_NAME;
slouken@1951
   599
#endif
slouken@1951
   600
slouken@1951
   601
    if (title != NULL) {
slouken@2143
   602
        char *title_locale = SDL_iconv_utf8_locale(title);
slouken@2143
   603
        if (!title_locale) {
slouken@1951
   604
            SDL_OutOfMemory();
slouken@1951
   605
            return;
slouken@1951
   606
        }
slouken@2143
   607
        status = XStringListToTextProperty(&title_locale, 1, &titleprop);
slouken@2143
   608
        SDL_free(title_locale);
slouken@1951
   609
        if (status) {
slouken@3685
   610
            XSetTextProperty(display, data->xwindow, &titleprop, XA_WM_NAME);
slouken@1951
   611
            XFree(titleprop.value);
slouken@1951
   612
        }
slouken@1951
   613
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   614
        if (SDL_X11_HAVE_UTF8) {
slouken@1951
   615
            status =
slouken@1951
   616
                Xutf8TextListToTextProperty(display, (char **) &title, 1,
slouken@1951
   617
                                            XUTF8StringStyle, &titleprop);
slouken@1951
   618
            if (status == Success) {
slouken@3685
   619
                XSetTextProperty(display, data->xwindow, &titleprop,
slouken@1951
   620
                                 _NET_WM_NAME);
slouken@1951
   621
                XFree(titleprop.value);
slouken@1951
   622
            }
slouken@1951
   623
        }
slouken@1951
   624
#endif
slouken@1951
   625
    }
slouken@1951
   626
    if (icon != NULL) {
slouken@2143
   627
        char *icon_locale = SDL_iconv_utf8_locale(icon);
slouken@2143
   628
        if (!icon_locale) {
slouken@1951
   629
            SDL_OutOfMemory();
slouken@1951
   630
            return;
slouken@1951
   631
        }
slouken@2143
   632
        status = XStringListToTextProperty(&icon_locale, 1, &iconprop);
slouken@2143
   633
        SDL_free(icon_locale);
slouken@1951
   634
        if (status) {
slouken@3685
   635
            XSetTextProperty(display, data->xwindow, &iconprop,
slouken@1951
   636
                             XA_WM_ICON_NAME);
slouken@1951
   637
            XFree(iconprop.value);
slouken@1951
   638
        }
slouken@1951
   639
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   640
        if (SDL_X11_HAVE_UTF8) {
slouken@1951
   641
            status =
slouken@1951
   642
                Xutf8TextListToTextProperty(display, (char **) &icon, 1,
slouken@1951
   643
                                            XUTF8StringStyle, &iconprop);
slouken@1951
   644
            if (status == Success) {
slouken@3685
   645
                XSetTextProperty(display, data->xwindow, &iconprop,
slouken@1951
   646
                                 _NET_WM_ICON_NAME);
slouken@1951
   647
                XFree(iconprop.value);
slouken@1951
   648
            }
slouken@1951
   649
        }
slouken@1951
   650
#endif
slouken@1951
   651
    }
slouken@5031
   652
    XFlush(display);
slouken@1951
   653
}
slouken@1951
   654
slouken@1951
   655
void
slouken@2967
   656
X11_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slouken@2967
   657
{
slouken@2967
   658
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@2967
   659
    Display *display = data->videodata->display;
slouken@4518
   660
    Atom _NET_WM_ICON = data->videodata->_NET_WM_ICON;
slouken@2967
   661
slouken@2967
   662
    if (icon) {
slouken@2967
   663
        SDL_PixelFormat format;
slouken@2967
   664
        SDL_Surface *surface;
slouken@2967
   665
        int propsize;
slouken@4528
   666
        long *propdata;
slouken@2967
   667
slouken@2967
   668
        /* Convert the icon to ARGB for modern window managers */
slouken@2990
   669
        SDL_InitFormat(&format, 32, 0x00FF0000, 0x0000FF00, 0x000000FF,
slouken@2990
   670
                       0xFF000000);
slouken@2967
   671
        surface = SDL_ConvertSurface(icon, &format, 0);
slouken@2967
   672
        if (!surface) {
slouken@2967
   673
            return;
slouken@2967
   674
        }
slouken@2967
   675
slouken@2967
   676
        /* Set the _NET_WM_ICON property */
slouken@2990
   677
        propsize = 2 + (icon->w * icon->h);
slouken@4566
   678
        propdata = SDL_malloc(propsize * sizeof(long));
slouken@2967
   679
        if (propdata) {
slouken@4528
   680
            int x, y;
slouken@4528
   681
            Uint32 *src;
slouken@4528
   682
            long *dst;
slouken@4528
   683
slouken@2967
   684
            propdata[0] = icon->w;
slouken@2967
   685
            propdata[1] = icon->h;
slouken@4528
   686
            dst = &propdata[2];
slouken@4528
   687
            for (y = 0; y < icon->h; ++y) {
slouken@4528
   688
                src = (Uint32*)((Uint8*)surface->pixels + y * surface->pitch);
slouken@4528
   689
                for (x = 0; x < icon->w; ++x) {
slouken@4528
   690
                    *dst++ = *src++;
slouken@4528
   691
                }
slouken@4528
   692
            }
slouken@3685
   693
            XChangeProperty(display, data->xwindow, _NET_WM_ICON, XA_CARDINAL,
slouken@2990
   694
                            32, PropModeReplace, (unsigned char *) propdata,
slouken@2990
   695
                            propsize);
slouken@2967
   696
        }
slouken@2967
   697
        SDL_FreeSurface(surface);
slouken@2967
   698
    } else {
slouken@3685
   699
        XDeleteProperty(display, data->xwindow, _NET_WM_ICON);
slouken@2967
   700
    }
slouken@5031
   701
    XFlush(display);
slouken@2967
   702
}
slouken@2967
   703
slouken@2967
   704
void
slouken@1951
   705
X11_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1951
   706
{
slouken@1951
   707
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   708
    Display *display = data->videodata->display;
slouken@4518
   709
    SDL_bool oldstyle_fullscreen;
slouken@2875
   710
    int x, y;
slouken@1951
   711
slouken@4518
   712
    /* ICCCM2.0-compliant window managers can handle fullscreen windows */
slouken@4522
   713
    oldstyle_fullscreen = X11_IsWindowOldFullscreen(_this, window);
slouken@4518
   714
slouken@4518
   715
    if (oldstyle_fullscreen
slouken@2876
   716
        || window->x == SDL_WINDOWPOS_CENTERED) {
slouken@2931
   717
        X11_GetDisplaySize(_this, window, &x, NULL);
slouken@2931
   718
        x = (x - window->w) / 2;
slouken@2875
   719
    } else {
slouken@2875
   720
        x = window->x;
slouken@2875
   721
    }
slouken@4518
   722
    if (oldstyle_fullscreen
slouken@2876
   723
        || window->y == SDL_WINDOWPOS_CENTERED) {
slouken@2931
   724
        X11_GetDisplaySize(_this, window, NULL, &y);
slouken@2931
   725
        y = (y - window->h) / 2;
slouken@2875
   726
    } else {
slouken@2875
   727
        y = window->y;
slouken@2875
   728
    }
slouken@3685
   729
    XMoveWindow(display, data->xwindow, x, y);
slouken@5031
   730
    XFlush(display);
slouken@1951
   731
}
slouken@1951
   732
slouken@1951
   733
void
slouken@1951
   734
X11_SetWindowSize(_THIS, SDL_Window * window)
slouken@1951
   735
{
slouken@1951
   736
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   737
    Display *display = data->videodata->display;
slouken@1951
   738
slouken@4937
   739
    if (SDL_IsShapedWindow(window))
eligottlieb@4782
   740
        X11_ResizeWindowShape(window);
slouken@3685
   741
    XResizeWindow(display, data->xwindow, window->w, window->h);
slouken@5031
   742
    XFlush(display);
slouken@1951
   743
}
slouken@1951
   744
slouken@1951
   745
void
slouken@1951
   746
X11_ShowWindow(_THIS, SDL_Window * window)
slouken@1951
   747
{
slouken@1951
   748
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   749
    Display *display = data->videodata->display;
slouken@1951
   750
slouken@3685
   751
    XMapRaised(display, data->xwindow);
slouken@5031
   752
    XFlush(display);
slouken@1951
   753
}
slouken@1951
   754
slouken@1951
   755
void
slouken@1951
   756
X11_HideWindow(_THIS, SDL_Window * window)
slouken@1951
   757
{
slouken@1951
   758
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   759
    Display *display = data->videodata->display;
slouken@1951
   760
slouken@3685
   761
    XUnmapWindow(display, data->xwindow);
slouken@5031
   762
    XFlush(display);
slouken@1951
   763
}
slouken@1951
   764
slouken@1951
   765
void
slouken@1951
   766
X11_RaiseWindow(_THIS, SDL_Window * window)
slouken@1951
   767
{
slouken@1951
   768
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   769
    Display *display = data->videodata->display;
slouken@1951
   770
slouken@3685
   771
    XRaiseWindow(display, data->xwindow);
slouken@5031
   772
    XFlush(display);
slouken@1951
   773
}
slouken@1951
   774
slouken@3077
   775
static void
slouken@3077
   776
X11_SetWindowMaximized(_THIS, SDL_Window * window, SDL_bool maximized)
slouken@3077
   777
{
slouken@3077
   778
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@3077
   779
    SDL_DisplayData *displaydata =
slouken@3685
   780
        (SDL_DisplayData *) window->display->driverdata;
slouken@3077
   781
    Display *display = data->videodata->display;
slouken@4518
   782
    Atom _NET_WM_STATE = data->videodata->_NET_WM_STATE;
slouken@4518
   783
    Atom _NET_WM_STATE_MAXIMIZED_VERT = data->videodata->_NET_WM_STATE_MAXIMIZED_VERT;
slouken@4518
   784
    Atom _NET_WM_STATE_MAXIMIZED_HORZ = data->videodata->_NET_WM_STATE_MAXIMIZED_HORZ;
slouken@4522
   785
    Atom _NET_WM_STATE_FULLSCREEN = data->videodata->_NET_WM_STATE_FULLSCREEN;
slouken@3077
   786
slouken@4522
   787
    if (X11_IsWindowMapped(_this, window)) {
slouken@4522
   788
        XEvent e;
slouken@3077
   789
slouken@4524
   790
        SDL_zero(e);
slouken@4522
   791
        e.xany.type = ClientMessage;
slouken@4522
   792
        e.xclient.message_type = _NET_WM_STATE;
slouken@4522
   793
        e.xclient.format = 32;
slouken@4522
   794
        e.xclient.window = data->xwindow;
slouken@4522
   795
        e.xclient.data.l[0] =
slouken@4522
   796
            maximized ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
slouken@4522
   797
        e.xclient.data.l[1] = _NET_WM_STATE_MAXIMIZED_VERT;
slouken@4522
   798
        e.xclient.data.l[2] = _NET_WM_STATE_MAXIMIZED_HORZ;
slouken@4522
   799
        e.xclient.data.l[3] = 0l;
slouken@4522
   800
slouken@4522
   801
        XSendEvent(display, RootWindow(display, displaydata->screen), 0,
slouken@4522
   802
                   SubstructureNotifyMask | SubstructureRedirectMask, &e);
slouken@4522
   803
    } else {
slouken@4522
   804
        int count = 0;
slouken@4522
   805
        Atom atoms[3];
slouken@4522
   806
slouken@4522
   807
        if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@4522
   808
            atoms[count++] = _NET_WM_STATE_FULLSCREEN;
slouken@4522
   809
        }
slouken@4522
   810
        if (maximized) {
slouken@4522
   811
            atoms[count++] = _NET_WM_STATE_MAXIMIZED_VERT;
slouken@4522
   812
            atoms[count++] = _NET_WM_STATE_MAXIMIZED_HORZ;
slouken@4522
   813
        }
slouken@4522
   814
        if (count > 0) {
slouken@4522
   815
            XChangeProperty(display, data->xwindow, _NET_WM_STATE, XA_ATOM, 32,
slouken@4522
   816
                            PropModeReplace, (unsigned char *)atoms, count);
slouken@4522
   817
        } else {
slouken@4522
   818
            XDeleteProperty(display, data->xwindow, _NET_WM_STATE);
slouken@4522
   819
        }
slouken@4522
   820
    }
slouken@5031
   821
    XFlush(display);
slouken@3077
   822
}
slouken@3077
   823
slouken@1951
   824
void
slouken@1951
   825
X11_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1951
   826
{
slouken@3077
   827
    X11_SetWindowMaximized(_this, window, SDL_TRUE);
slouken@1951
   828
}
slouken@1951
   829
slouken@1951
   830
void
slouken@1951
   831
X11_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1951
   832
{
slouken@4518
   833
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@4518
   834
    SDL_DisplayData *displaydata =
slouken@4518
   835
        (SDL_DisplayData *) window->display->driverdata;
slouken@4518
   836
    Display *display = data->videodata->display;
slouken@4518
   837
 
slouken@4518
   838
    XIconifyWindow(display, data->xwindow, displaydata->screen);
slouken@5031
   839
    XFlush(display);
slouken@1951
   840
}
slouken@1951
   841
slouken@1951
   842
void
slouken@1951
   843
X11_RestoreWindow(_THIS, SDL_Window * window)
slouken@1951
   844
{
slouken@3077
   845
    X11_SetWindowMaximized(_this, window, SDL_FALSE);
slouken@1951
   846
    X11_ShowWindow(_this, window);
slouken@1951
   847
}
slouken@1951
   848
slouken@1951
   849
void
slouken@1951
   850
X11_SetWindowGrab(_THIS, SDL_Window * window)
slouken@1951
   851
{
slouken@2875
   852
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@2875
   853
    Display *display = data->videodata->display;
slouken@4518
   854
    SDL_bool oldstyle_fullscreen;
slouken@2875
   855
slouken@4518
   856
    /* ICCCM2.0-compliant window managers can handle fullscreen windows */
slouken@4522
   857
    oldstyle_fullscreen = X11_IsWindowOldFullscreen(_this, window);
slouken@4518
   858
slouken@4518
   859
    if (((window->flags & SDL_WINDOW_INPUT_GRABBED) || oldstyle_fullscreen)
slouken@2876
   860
        && (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@2875
   861
        /* Try to grab the mouse */
slouken@2876
   862
        for (;;) {
slouken@2876
   863
            int result =
slouken@3685
   864
                XGrabPointer(display, data->xwindow, True, 0, GrabModeAsync,
slouken@3685
   865
                             GrabModeAsync, data->xwindow, None, CurrentTime);
slouken@2876
   866
            if (result == GrabSuccess) {
slouken@2875
   867
                break;
slouken@2875
   868
            }
slouken@2875
   869
            SDL_Delay(100);
slouken@2875
   870
        }
slouken@2875
   871
slouken@2875
   872
        /* Raise the window if we grab the mouse */
slouken@3685
   873
        XRaiseWindow(display, data->xwindow);
slouken@2875
   874
slouken@2875
   875
        /* Now grab the keyboard */
slouken@3685
   876
        XGrabKeyboard(display, data->xwindow, True, GrabModeAsync,
slouken@2876
   877
                      GrabModeAsync, CurrentTime);
slouken@2875
   878
    } else {
slouken@2875
   879
        XUngrabPointer(display, CurrentTime);
slouken@2875
   880
        XUngrabKeyboard(display, CurrentTime);
slouken@2875
   881
    }
slouken@1951
   882
}
slouken@1951
   883
slouken@1951
   884
void
slouken@1951
   885
X11_DestroyWindow(_THIS, SDL_Window * window)
slouken@1951
   886
{
slouken@1951
   887
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
bob@2323
   888
    window->driverdata = NULL;
slouken@1951
   889
slouken@1951
   890
    if (data) {
bob@2323
   891
        SDL_VideoData *videodata = (SDL_VideoData *) data->videodata;
bob@2323
   892
        Display *display = videodata->display;
bob@2323
   893
        int numwindows = videodata->numwindows;
bob@2323
   894
        SDL_WindowData **windowlist = videodata->windowlist;
bob@2323
   895
        int i;
bob@2323
   896
bob@2323
   897
        if (windowlist) {
bob@2324
   898
            for (i = 0; i < numwindows; ++i) {
slouken@3685
   899
                if (windowlist[i] && (windowlist[i]->window == window)) {
bob@2324
   900
                    windowlist[i] = windowlist[numwindows - 1];
bob@2324
   901
                    windowlist[numwindows - 1] = NULL;
bob@2324
   902
                    videodata->numwindows--;
bob@2324
   903
                    break;
bob@2324
   904
                }
bob@2323
   905
            }
bob@2323
   906
        }
slouken@1951
   907
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   908
        if (data->ic) {
slouken@1951
   909
            XDestroyIC(data->ic);
slouken@1951
   910
        }
slouken@1951
   911
#endif
slouken@1951
   912
        if (data->created) {
slouken@3685
   913
            XDestroyWindow(display, data->xwindow);
slouken@5031
   914
            XFlush(display);
slouken@1951
   915
        }
slouken@1951
   916
        SDL_free(data);
slouken@1951
   917
    }
slouken@1951
   918
}
slouken@1951
   919
slouken@1951
   920
SDL_bool
slouken@1951
   921
X11_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1951
   922
{
slouken@4510
   923
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@4510
   924
    Display *display = data->videodata->display;
slouken@4510
   925
slouken@4510
   926
    if (info->version.major == SDL_MAJOR_VERSION &&
slouken@4510
   927
        info->version.minor == SDL_MINOR_VERSION) {
slouken@4510
   928
        info->subsystem = SDL_SYSWM_X11;
slouken@5056
   929
        info->info.x11.display = display;
slouken@5056
   930
        info->info.x11.window = data->xwindow;
slouken@1951
   931
        return SDL_TRUE;
slouken@1951
   932
    } else {
slouken@1951
   933
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1951
   934
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1951
   935
        return SDL_FALSE;
slouken@1951
   936
    }
slouken@1951
   937
}
slouken@1951
   938
slouken@1951
   939
/* vi: set ts=4 sw=4 expandtab: */