src/video/x11/SDL_x11window.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 21 Feb 2011 22:52:31 -0800
changeset 5386 762e67f81fc7
parent 5305 75f5ff92ad08
child 5466 006883d5fa51
permissions -rw-r--r--
Simplified the X11 window creation
slouken@1951
     1
/*
slouken@1951
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 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@5246
    93
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->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@5246
   262
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->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
    Window w;
slouken@1951
   269
    XSizeHints *sizehints;
slouken@1951
   270
    XWMHints *wmhints;
slouken@1951
   271
    XClassHint *classhints;
slouken@4521
   272
    Atom _NET_WM_WINDOW_TYPE;
slouken@4521
   273
    Atom _NET_WM_WINDOW_TYPE_NORMAL;
slouken@4522
   274
    int wmstate_count;
slouken@4522
   275
    Atom wmstate_atoms[3];
slouken@4518
   276
slouken@1951
   277
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@1951
   278
/* FIXME
slouken@1951
   279
    if ( use_xinerama ) {
slouken@1951
   280
        x = xinerama_info.x_org;
slouken@1951
   281
        y = xinerama_info.y_org;
slouken@1951
   282
    }
slouken@1951
   283
*/
slouken@1951
   284
#endif
slouken@5088
   285
#if SDL_VIDEO_OPENGL_GLX
slouken@1951
   286
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@1952
   287
        XVisualInfo *vinfo;
slouken@1952
   288
slouken@4521
   289
        vinfo = X11_GL_GetVisual(_this, display, screen);
slouken@1952
   290
        if (!vinfo) {
slouken@1952
   291
            return -1;
slouken@1952
   292
        }
slouken@1952
   293
        visual = vinfo->visual;
slouken@1952
   294
        depth = vinfo->depth;
slouken@1952
   295
        XFree(vinfo);
slouken@1952
   296
    } else
slouken@1952
   297
#endif
slouken@3161
   298
#ifdef SDL_VIDEO_DRIVER_PANDORA
slouken@3161
   299
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@3161
   300
        XVisualInfo *vinfo;
slouken@3161
   301
slouken@4521
   302
        vinfo = X11_GLES_GetVisual(_this, display, screen);
slouken@3161
   303
        if (!vinfo) {
slouken@3161
   304
            return -1;
slouken@3161
   305
        }
slouken@3161
   306
        visual = vinfo->visual;
slouken@3161
   307
        depth = vinfo->depth;
slouken@3161
   308
        XFree(vinfo);
slouken@3161
   309
    } else
slouken@3161
   310
#endif
slouken@1952
   311
    {
slouken@1951
   312
        visual = displaydata->visual;
slouken@1951
   313
        depth = displaydata->depth;
slouken@1951
   314
    }
slouken@1951
   315
slouken@5386
   316
    xattr.override_redirect = False;
slouken@1951
   317
    xattr.background_pixel = 0;
slouken@1951
   318
    xattr.border_pixel = 0;
slouken@5243
   319
    xattr.colormap = XCreateColormap(display, RootWindow(display, screen), visual, AllocNone);
slouken@1951
   320
slouken@5386
   321
    w = XCreateWindow(display, RootWindow(display, screen),
slouken@5386
   322
                      window->x, window->y, window->w, window->h,
slouken@5386
   323
                      0, depth, InputOutput, visual,
slouken@1951
   324
                      (CWOverrideRedirect | CWBackPixel | CWBorderPixel |
slouken@1951
   325
                       CWColormap), &xattr);
slouken@1952
   326
    if (!w) {
slouken@1952
   327
        SDL_SetError("Couldn't create window");
slouken@1952
   328
        return -1;
slouken@1952
   329
    }
slouken@3161
   330
#if SDL_VIDEO_DRIVER_PANDORA
slouken@3161
   331
    /* Create the GLES window surface */
slouken@3161
   332
    _this->gles_data->egl_surface =
slouken@3161
   333
        _this->gles_data->eglCreateWindowSurface(_this->gles_data->
slouken@3161
   334
                                                 egl_display,
slouken@3161
   335
                                                 _this->gles_data->egl_config,
slouken@3161
   336
                                                 (NativeWindowType) w, NULL);
slouken@3161
   337
slouken@3161
   338
    if (_this->gles_data->egl_surface == EGL_NO_SURFACE) {
slouken@3161
   339
        SDL_SetError("Could not create GLES window surface");
slouken@3161
   340
        return -1;
slouken@3161
   341
    }
slouken@3161
   342
#endif
slouken@1951
   343
slouken@1951
   344
    sizehints = XAllocSizeHints();
slouken@1951
   345
    if (sizehints) {
slouken@5386
   346
        if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
slouken@1951
   347
            sizehints->min_width = sizehints->max_width = window->w;
slouken@1951
   348
            sizehints->min_height = sizehints->max_height = window->h;
slouken@3225
   349
            sizehints->flags = PMaxSize | PMinSize;
slouken@1951
   350
        }
slouken@5386
   351
        sizehints->x = window->x;
slouken@5386
   352
        sizehints->y = window->y;
slouken@5386
   353
        sizehints->flags |= USPosition;
slouken@4521
   354
        XSetWMNormalHints(display, w, sizehints);
slouken@1951
   355
        XFree(sizehints);
slouken@1951
   356
    }
slouken@1951
   357
slouken@5386
   358
    if (window->flags & SDL_WINDOW_BORDERLESS) {
slouken@1951
   359
        SDL_bool set;
slouken@1951
   360
        Atom WM_HINTS;
slouken@1951
   361
slouken@1951
   362
        /* We haven't modified the window manager hints yet */
slouken@1951
   363
        set = SDL_FALSE;
slouken@1951
   364
slouken@1951
   365
        /* First try to set MWM hints */
slouken@4521
   366
        WM_HINTS = XInternAtom(display, "_MOTIF_WM_HINTS", True);
slouken@1951
   367
        if (WM_HINTS != None) {
slouken@1951
   368
            /* Hints used by Motif compliant window managers */
slouken@1951
   369
            struct
slouken@1951
   370
            {
slouken@1951
   371
                unsigned long flags;
slouken@1951
   372
                unsigned long functions;
slouken@1951
   373
                unsigned long decorations;
slouken@1951
   374
                long input_mode;
slouken@1951
   375
                unsigned long status;
slouken@1951
   376
            } MWMHints = {
slouken@1951
   377
            (1L << 1), 0, 0, 0, 0};
slouken@1951
   378
slouken@4521
   379
            XChangeProperty(display, w, WM_HINTS, WM_HINTS, 32,
slouken@1951
   380
                            PropModeReplace, (unsigned char *) &MWMHints,
slouken@4521
   381
                            sizeof(MWMHints) / 4);
slouken@1951
   382
            set = SDL_TRUE;
slouken@1951
   383
        }
slouken@1951
   384
        /* Now try to set KWM hints */
slouken@4521
   385
        WM_HINTS = XInternAtom(display, "KWM_WIN_DECORATION", True);
slouken@1951
   386
        if (WM_HINTS != None) {
slouken@1951
   387
            long KWMHints = 0;
slouken@1951
   388
slouken@4521
   389
            XChangeProperty(display, w, WM_HINTS, WM_HINTS, 32,
slouken@1951
   390
                            PropModeReplace,
slouken@1951
   391
                            (unsigned char *) &KWMHints,
slouken@4521
   392
                            sizeof(KWMHints) / 4);
slouken@1951
   393
            set = SDL_TRUE;
slouken@1951
   394
        }
slouken@1951
   395
        /* Now try to set GNOME hints */
slouken@4521
   396
        WM_HINTS = XInternAtom(display, "_WIN_HINTS", True);
slouken@1951
   397
        if (WM_HINTS != None) {
slouken@1951
   398
            long GNOMEHints = 0;
slouken@1951
   399
slouken@4521
   400
            XChangeProperty(display, w, WM_HINTS, WM_HINTS, 32,
slouken@1951
   401
                            PropModeReplace,
slouken@1951
   402
                            (unsigned char *) &GNOMEHints,
slouken@4521
   403
                            sizeof(GNOMEHints) / 4);
slouken@1951
   404
            set = SDL_TRUE;
slouken@1951
   405
        }
slouken@1951
   406
        /* Finally set the transient hints if necessary */
slouken@1951
   407
        if (!set) {
slouken@4521
   408
            XSetTransientForHint(display, w, RootWindow(display, screen));
slouken@1951
   409
        }
slouken@1951
   410
    } else {
slouken@1951
   411
        SDL_bool set;
slouken@1951
   412
        Atom WM_HINTS;
slouken@1951
   413
slouken@1951
   414
        /* We haven't modified the window manager hints yet */
slouken@1951
   415
        set = SDL_FALSE;
slouken@1951
   416
slouken@1951
   417
        /* First try to unset MWM hints */
slouken@4521
   418
        WM_HINTS = XInternAtom(display, "_MOTIF_WM_HINTS", True);
slouken@1951
   419
        if (WM_HINTS != None) {
slouken@4521
   420
            XDeleteProperty(display, w, WM_HINTS);
slouken@1951
   421
            set = SDL_TRUE;
slouken@1951
   422
        }
slouken@1951
   423
        /* Now try to unset KWM hints */
slouken@4521
   424
        WM_HINTS = XInternAtom(display, "KWM_WIN_DECORATION", True);
slouken@1951
   425
        if (WM_HINTS != None) {
slouken@4521
   426
            XDeleteProperty(display, w, WM_HINTS);
slouken@1951
   427
            set = SDL_TRUE;
slouken@1951
   428
        }
slouken@1951
   429
        /* Now try to unset GNOME hints */
slouken@4521
   430
        WM_HINTS = XInternAtom(display, "_WIN_HINTS", True);
slouken@1951
   431
        if (WM_HINTS != None) {
slouken@4521
   432
            XDeleteProperty(display, w, WM_HINTS);
slouken@1951
   433
            set = SDL_TRUE;
slouken@1951
   434
        }
slouken@1951
   435
        /* Finally unset the transient hints if necessary */
slouken@1951
   436
        if (!set) {
slouken@4555
   437
            XDeleteProperty(display, w, XA_WM_TRANSIENT_FOR);
slouken@1951
   438
        }
slouken@1951
   439
    }
slouken@1951
   440
slouken@1951
   441
    /* Set the input hints so we get keyboard input */
slouken@1951
   442
    wmhints = XAllocWMHints();
slouken@1951
   443
    if (wmhints) {
slouken@1951
   444
        wmhints->input = True;
slouken@1956
   445
        wmhints->flags = InputHint;
slouken@4521
   446
        XSetWMHints(display, w, wmhints);
slouken@1951
   447
        XFree(wmhints);
slouken@1951
   448
    }
slouken@1951
   449
slouken@1951
   450
    /* Set the class hints so we can get an icon (AfterStep) */
slouken@1951
   451
    classhints = XAllocClassHint();
slouken@1951
   452
    if (classhints != NULL) {
slouken@1951
   453
        classhints->res_name = data->classname;
slouken@1951
   454
        classhints->res_class = data->classname;
slouken@4521
   455
        XSetClassHint(display, w, classhints);
slouken@1951
   456
        XFree(classhints);
slouken@1951
   457
    }
slouken@1951
   458
slouken@4522
   459
    /* Set the window manager state */
slouken@4522
   460
    wmstate_count = X11_GetWMStateProperty(_this, window, wmstate_atoms);
slouken@4522
   461
    if (wmstate_count > 0) {
slouken@4522
   462
        XChangeProperty(display, w, data->_NET_WM_STATE, XA_ATOM, 32,
slouken@4522
   463
                        PropModeReplace,
slouken@4522
   464
                        (unsigned char *)wmstate_atoms, wmstate_count);
slouken@4522
   465
    } else {
slouken@4522
   466
        XDeleteProperty(display, w, data->_NET_WM_STATE);
slouken@4518
   467
    }
slouken@4518
   468
slouken@4521
   469
    /* Let the window manager know we're a "normal" window */
slouken@4521
   470
    _NET_WM_WINDOW_TYPE = XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
slouken@4521
   471
    _NET_WM_WINDOW_TYPE_NORMAL = XInternAtom(display, "_NET_WM_WINDOW_TYPE_NORMAL", False);
slouken@4521
   472
    XChangeProperty(display, w, _NET_WM_WINDOW_TYPE, XA_ATOM, 32,
slouken@4521
   473
                    PropModeReplace,
slouken@4521
   474
                    (unsigned char *)&_NET_WM_WINDOW_TYPE_NORMAL, 1);
slouken@4521
   475
slouken@1951
   476
    /* Allow the window to be deleted by the window manager */
slouken@4521
   477
    XSetWMProtocols(display, w, &data->WM_DELETE_WINDOW, 1);
slouken@1951
   478
slouken@1951
   479
    if (SetupWindowData(_this, window, w, SDL_TRUE) < 0) {
slouken@4521
   480
        XDestroyWindow(display, w);
slouken@1951
   481
        return -1;
slouken@1951
   482
    }
slouken@5386
   483
bob@2325
   484
#ifdef X_HAVE_UTF8_STRING
bob@2325
   485
    {
bob@2325
   486
        Uint32 fevent = 0;
bob@2325
   487
        pXGetICValues(((SDL_WindowData *) window->driverdata)->ic,
bob@2325
   488
                      XNFilterEvents, &fevent, NULL);
slouken@4521
   489
        XSelectInput(display, w,
bob@2325
   490
                     (FocusChangeMask | EnterWindowMask | LeaveWindowMask |
bob@2325
   491
                      ExposureMask | ButtonPressMask | ButtonReleaseMask |
bob@2325
   492
                      PointerMotionMask | KeyPressMask | KeyReleaseMask |
bob@2325
   493
                      PropertyChangeMask | StructureNotifyMask |
bob@2325
   494
                      KeymapStateMask | fevent));
bob@2325
   495
    }
bob@2325
   496
#else
kazeuser@2718
   497
    {
slouken@4521
   498
        XSelectInput(display, w,
slouken@2725
   499
                     (FocusChangeMask | EnterWindowMask | LeaveWindowMask |
slouken@2725
   500
                      ExposureMask | ButtonPressMask | ButtonReleaseMask |
slouken@2725
   501
                      PointerMotionMask | KeyPressMask | KeyReleaseMask |
slouken@2725
   502
                      PropertyChangeMask | StructureNotifyMask |
slouken@2725
   503
                      KeymapStateMask));
kazeuser@2718
   504
    }
bob@2325
   505
#endif
bob@2325
   506
slouken@5031
   507
    XFlush(display);
slouken@5031
   508
slouken@1951
   509
    return 0;
slouken@1951
   510
}
slouken@1951
   511
slouken@1951
   512
int
bob@2324
   513
X11_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1951
   514
{
slouken@1951
   515
    Window w = (Window) data;
slouken@1951
   516
slouken@4518
   517
    window->title = X11_GetWindowTitle(_this, w);
slouken@1951
   518
slouken@1951
   519
    if (SetupWindowData(_this, window, w, SDL_FALSE) < 0) {
slouken@1951
   520
        return -1;
slouken@1951
   521
    }
slouken@1951
   522
    return 0;
slouken@1951
   523
}
slouken@1951
   524
slouken@4518
   525
char *
slouken@4518
   526
X11_GetWindowTitle(_THIS, Window xwindow)
slouken@4518
   527
{
slouken@4518
   528
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@4518
   529
    Display *display = data->display;
slouken@4518
   530
    int status, real_format;
slouken@4518
   531
    Atom real_type;
slouken@4518
   532
    unsigned long items_read, items_left;
slouken@4518
   533
    unsigned char *propdata;
slouken@4518
   534
    char *title = NULL;
slouken@4518
   535
slouken@4518
   536
    status = XGetWindowProperty(display, xwindow, data->_NET_WM_NAME,
slouken@4518
   537
                0L, 8192L, False, data->UTF8_STRING, &real_type, &real_format,
slouken@4518
   538
                &items_read, &items_left, &propdata);
slouken@4518
   539
    if (status == Success) {
slouken@4518
   540
        title = SDL_strdup(SDL_static_cast(char*, propdata));
slouken@4518
   541
        XFree(propdata);
slouken@4518
   542
    } else {
slouken@4518
   543
        status = XGetWindowProperty(display, xwindow, XA_WM_NAME,
slouken@4518
   544
                    0L, 8192L, False, XA_STRING, &real_type, &real_format,
slouken@4518
   545
                    &items_read, &items_left, &propdata);
slouken@4518
   546
        if (status == Success) {
slouken@4518
   547
            title = SDL_iconv_string("UTF-8", "", SDL_static_cast(char*, propdata), items_read+1);
slouken@4518
   548
        } else {
slouken@4518
   549
            title = SDL_strdup("");
slouken@4518
   550
        }
slouken@4518
   551
    }
slouken@4518
   552
    return title;
slouken@4518
   553
}
slouken@4518
   554
slouken@1951
   555
void
slouken@1951
   556
X11_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1951
   557
{
slouken@1951
   558
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   559
    Display *display = data->videodata->display;
slouken@1951
   560
    XTextProperty titleprop, iconprop;
slouken@1951
   561
    Status status;
slouken@1951
   562
    const char *title = window->title;
slouken@1951
   563
    const char *icon = NULL;
slouken@1951
   564
slouken@1951
   565
#ifdef X_HAVE_UTF8_STRING
slouken@4518
   566
    Atom _NET_WM_NAME = data->videodata->_NET_WM_NAME;
slouken@4518
   567
    Atom _NET_WM_ICON_NAME = data->videodata->_NET_WM_ICON_NAME;
slouken@1951
   568
#endif
slouken@1951
   569
slouken@1951
   570
    if (title != NULL) {
slouken@2143
   571
        char *title_locale = SDL_iconv_utf8_locale(title);
slouken@2143
   572
        if (!title_locale) {
slouken@1951
   573
            SDL_OutOfMemory();
slouken@1951
   574
            return;
slouken@1951
   575
        }
slouken@2143
   576
        status = XStringListToTextProperty(&title_locale, 1, &titleprop);
slouken@2143
   577
        SDL_free(title_locale);
slouken@1951
   578
        if (status) {
slouken@3685
   579
            XSetTextProperty(display, data->xwindow, &titleprop, XA_WM_NAME);
slouken@1951
   580
            XFree(titleprop.value);
slouken@1951
   581
        }
slouken@1951
   582
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   583
        if (SDL_X11_HAVE_UTF8) {
slouken@1951
   584
            status =
slouken@1951
   585
                Xutf8TextListToTextProperty(display, (char **) &title, 1,
slouken@1951
   586
                                            XUTF8StringStyle, &titleprop);
slouken@1951
   587
            if (status == Success) {
slouken@3685
   588
                XSetTextProperty(display, data->xwindow, &titleprop,
slouken@1951
   589
                                 _NET_WM_NAME);
slouken@1951
   590
                XFree(titleprop.value);
slouken@1951
   591
            }
slouken@1951
   592
        }
slouken@1951
   593
#endif
slouken@1951
   594
    }
slouken@1951
   595
    if (icon != NULL) {
slouken@2143
   596
        char *icon_locale = SDL_iconv_utf8_locale(icon);
slouken@2143
   597
        if (!icon_locale) {
slouken@1951
   598
            SDL_OutOfMemory();
slouken@1951
   599
            return;
slouken@1951
   600
        }
slouken@2143
   601
        status = XStringListToTextProperty(&icon_locale, 1, &iconprop);
slouken@2143
   602
        SDL_free(icon_locale);
slouken@1951
   603
        if (status) {
slouken@3685
   604
            XSetTextProperty(display, data->xwindow, &iconprop,
slouken@1951
   605
                             XA_WM_ICON_NAME);
slouken@1951
   606
            XFree(iconprop.value);
slouken@1951
   607
        }
slouken@1951
   608
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   609
        if (SDL_X11_HAVE_UTF8) {
slouken@1951
   610
            status =
slouken@1951
   611
                Xutf8TextListToTextProperty(display, (char **) &icon, 1,
slouken@1951
   612
                                            XUTF8StringStyle, &iconprop);
slouken@1951
   613
            if (status == Success) {
slouken@3685
   614
                XSetTextProperty(display, data->xwindow, &iconprop,
slouken@1951
   615
                                 _NET_WM_ICON_NAME);
slouken@1951
   616
                XFree(iconprop.value);
slouken@1951
   617
            }
slouken@1951
   618
        }
slouken@1951
   619
#endif
slouken@1951
   620
    }
slouken@5031
   621
    XFlush(display);
slouken@1951
   622
}
slouken@1951
   623
slouken@1951
   624
void
slouken@2967
   625
X11_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slouken@2967
   626
{
slouken@2967
   627
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@2967
   628
    Display *display = data->videodata->display;
slouken@4518
   629
    Atom _NET_WM_ICON = data->videodata->_NET_WM_ICON;
slouken@2967
   630
slouken@2967
   631
    if (icon) {
slouken@2967
   632
        SDL_PixelFormat format;
slouken@2967
   633
        SDL_Surface *surface;
slouken@2967
   634
        int propsize;
slouken@4528
   635
        long *propdata;
slouken@2967
   636
slouken@2967
   637
        /* Convert the icon to ARGB for modern window managers */
slouken@5288
   638
        SDL_InitFormat(&format, SDL_PIXELFORMAT_ARGB8888);
slouken@2967
   639
        surface = SDL_ConvertSurface(icon, &format, 0);
slouken@2967
   640
        if (!surface) {
slouken@2967
   641
            return;
slouken@2967
   642
        }
slouken@2967
   643
slouken@2967
   644
        /* Set the _NET_WM_ICON property */
slouken@2990
   645
        propsize = 2 + (icon->w * icon->h);
slouken@4566
   646
        propdata = SDL_malloc(propsize * sizeof(long));
slouken@2967
   647
        if (propdata) {
slouken@4528
   648
            int x, y;
slouken@4528
   649
            Uint32 *src;
slouken@4528
   650
            long *dst;
slouken@4528
   651
slouken@2967
   652
            propdata[0] = icon->w;
slouken@2967
   653
            propdata[1] = icon->h;
slouken@4528
   654
            dst = &propdata[2];
slouken@4528
   655
            for (y = 0; y < icon->h; ++y) {
slouken@4528
   656
                src = (Uint32*)((Uint8*)surface->pixels + y * surface->pitch);
slouken@4528
   657
                for (x = 0; x < icon->w; ++x) {
slouken@4528
   658
                    *dst++ = *src++;
slouken@4528
   659
                }
slouken@4528
   660
            }
slouken@3685
   661
            XChangeProperty(display, data->xwindow, _NET_WM_ICON, XA_CARDINAL,
slouken@2990
   662
                            32, PropModeReplace, (unsigned char *) propdata,
slouken@2990
   663
                            propsize);
slouken@2967
   664
        }
slouken@2967
   665
        SDL_FreeSurface(surface);
slouken@2967
   666
    } else {
slouken@3685
   667
        XDeleteProperty(display, data->xwindow, _NET_WM_ICON);
slouken@2967
   668
    }
slouken@5031
   669
    XFlush(display);
slouken@2967
   670
}
slouken@2967
   671
slouken@2967
   672
void
slouken@1951
   673
X11_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1951
   674
{
slouken@1951
   675
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   676
    Display *display = data->videodata->display;
slouken@4518
   677
    SDL_bool oldstyle_fullscreen;
slouken@2875
   678
    int x, y;
slouken@1951
   679
slouken@4518
   680
    /* ICCCM2.0-compliant window managers can handle fullscreen windows */
slouken@4522
   681
    oldstyle_fullscreen = X11_IsWindowOldFullscreen(_this, window);
slouken@4518
   682
slouken@4518
   683
    if (oldstyle_fullscreen
slouken@5246
   684
        || SDL_WINDOWPOS_ISCENTERED(window->x)) {
slouken@2931
   685
        X11_GetDisplaySize(_this, window, &x, NULL);
slouken@2931
   686
        x = (x - window->w) / 2;
slouken@2875
   687
    } else {
slouken@2875
   688
        x = window->x;
slouken@2875
   689
    }
slouken@4518
   690
    if (oldstyle_fullscreen
slouken@5246
   691
        || SDL_WINDOWPOS_ISCENTERED(window->y)) {
slouken@2931
   692
        X11_GetDisplaySize(_this, window, NULL, &y);
slouken@2931
   693
        y = (y - window->h) / 2;
slouken@2875
   694
    } else {
slouken@2875
   695
        y = window->y;
slouken@2875
   696
    }
slouken@3685
   697
    XMoveWindow(display, data->xwindow, x, y);
slouken@5031
   698
    XFlush(display);
slouken@1951
   699
}
slouken@1951
   700
slouken@1951
   701
void
slouken@1951
   702
X11_SetWindowSize(_THIS, SDL_Window * window)
slouken@1951
   703
{
slouken@1951
   704
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   705
    Display *display = data->videodata->display;
slouken@1951
   706
slouken@4937
   707
    if (SDL_IsShapedWindow(window))
eligottlieb@4782
   708
        X11_ResizeWindowShape(window);
slouken@3685
   709
    XResizeWindow(display, data->xwindow, window->w, window->h);
slouken@5031
   710
    XFlush(display);
slouken@1951
   711
}
slouken@1951
   712
slouken@1951
   713
void
slouken@1951
   714
X11_ShowWindow(_THIS, SDL_Window * window)
slouken@1951
   715
{
slouken@1951
   716
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   717
    Display *display = data->videodata->display;
slouken@1951
   718
slouken@3685
   719
    XMapRaised(display, data->xwindow);
slouken@5031
   720
    XFlush(display);
slouken@1951
   721
}
slouken@1951
   722
slouken@1951
   723
void
slouken@1951
   724
X11_HideWindow(_THIS, SDL_Window * window)
slouken@1951
   725
{
slouken@1951
   726
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   727
    Display *display = data->videodata->display;
slouken@1951
   728
slouken@3685
   729
    XUnmapWindow(display, data->xwindow);
slouken@5031
   730
    XFlush(display);
slouken@1951
   731
}
slouken@1951
   732
slouken@1951
   733
void
slouken@1951
   734
X11_RaiseWindow(_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@3685
   739
    XRaiseWindow(display, data->xwindow);
slouken@5031
   740
    XFlush(display);
slouken@1951
   741
}
slouken@1951
   742
slouken@3077
   743
static void
slouken@5302
   744
SetWindowMaximized(_THIS, SDL_Window * window, SDL_bool maximized)
slouken@3077
   745
{
slouken@3077
   746
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@3077
   747
    SDL_DisplayData *displaydata =
slouken@5246
   748
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@3077
   749
    Display *display = data->videodata->display;
slouken@4518
   750
    Atom _NET_WM_STATE = data->videodata->_NET_WM_STATE;
slouken@4518
   751
    Atom _NET_WM_STATE_MAXIMIZED_VERT = data->videodata->_NET_WM_STATE_MAXIMIZED_VERT;
slouken@4518
   752
    Atom _NET_WM_STATE_MAXIMIZED_HORZ = data->videodata->_NET_WM_STATE_MAXIMIZED_HORZ;
slouken@4522
   753
    Atom _NET_WM_STATE_FULLSCREEN = data->videodata->_NET_WM_STATE_FULLSCREEN;
slouken@3077
   754
slouken@4522
   755
    if (X11_IsWindowMapped(_this, window)) {
slouken@4522
   756
        XEvent e;
slouken@3077
   757
slouken@4524
   758
        SDL_zero(e);
slouken@4522
   759
        e.xany.type = ClientMessage;
slouken@4522
   760
        e.xclient.message_type = _NET_WM_STATE;
slouken@4522
   761
        e.xclient.format = 32;
slouken@4522
   762
        e.xclient.window = data->xwindow;
slouken@4522
   763
        e.xclient.data.l[0] =
slouken@4522
   764
            maximized ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
slouken@4522
   765
        e.xclient.data.l[1] = _NET_WM_STATE_MAXIMIZED_VERT;
slouken@4522
   766
        e.xclient.data.l[2] = _NET_WM_STATE_MAXIMIZED_HORZ;
slouken@4522
   767
        e.xclient.data.l[3] = 0l;
slouken@4522
   768
slouken@4522
   769
        XSendEvent(display, RootWindow(display, displaydata->screen), 0,
slouken@4522
   770
                   SubstructureNotifyMask | SubstructureRedirectMask, &e);
slouken@4522
   771
    } else {
slouken@4522
   772
        int count = 0;
slouken@4522
   773
        Atom atoms[3];
slouken@4522
   774
slouken@4522
   775
        if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@4522
   776
            atoms[count++] = _NET_WM_STATE_FULLSCREEN;
slouken@4522
   777
        }
slouken@4522
   778
        if (maximized) {
slouken@4522
   779
            atoms[count++] = _NET_WM_STATE_MAXIMIZED_VERT;
slouken@4522
   780
            atoms[count++] = _NET_WM_STATE_MAXIMIZED_HORZ;
slouken@4522
   781
        }
slouken@4522
   782
        if (count > 0) {
slouken@4522
   783
            XChangeProperty(display, data->xwindow, _NET_WM_STATE, XA_ATOM, 32,
slouken@4522
   784
                            PropModeReplace, (unsigned char *)atoms, count);
slouken@4522
   785
        } else {
slouken@4522
   786
            XDeleteProperty(display, data->xwindow, _NET_WM_STATE);
slouken@4522
   787
        }
slouken@4522
   788
    }
slouken@5031
   789
    XFlush(display);
slouken@3077
   790
}
slouken@3077
   791
slouken@1951
   792
void
slouken@1951
   793
X11_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1951
   794
{
slouken@5302
   795
    SetWindowMaximized(_this, window, SDL_TRUE);
slouken@1951
   796
}
slouken@1951
   797
slouken@1951
   798
void
slouken@1951
   799
X11_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1951
   800
{
slouken@4518
   801
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@4518
   802
    SDL_DisplayData *displaydata =
slouken@5246
   803
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@4518
   804
    Display *display = data->videodata->display;
slouken@4518
   805
 
slouken@4518
   806
    XIconifyWindow(display, data->xwindow, displaydata->screen);
slouken@5031
   807
    XFlush(display);
slouken@1951
   808
}
slouken@1951
   809
slouken@1951
   810
void
slouken@1951
   811
X11_RestoreWindow(_THIS, SDL_Window * window)
slouken@1951
   812
{
slouken@5302
   813
    SetWindowMaximized(_this, window, SDL_FALSE);
slouken@1951
   814
    X11_ShowWindow(_this, window);
slouken@1951
   815
}
slouken@1951
   816
slouken@5305
   817
void
slouken@5305
   818
X11_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * _display, SDL_bool fullscreen)
slouken@5302
   819
{
slouken@5302
   820
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5305
   821
    SDL_DisplayData *displaydata = (SDL_DisplayData *) _display->driverdata;
slouken@5302
   822
    Display *display = data->videodata->display;
slouken@5302
   823
    Atom _NET_WM_STATE = data->videodata->_NET_WM_STATE;
slouken@5302
   824
    Atom _NET_WM_STATE_MAXIMIZED_VERT = data->videodata->_NET_WM_STATE_MAXIMIZED_VERT;
slouken@5302
   825
    Atom _NET_WM_STATE_MAXIMIZED_HORZ = data->videodata->_NET_WM_STATE_MAXIMIZED_HORZ;
slouken@5302
   826
    Atom _NET_WM_STATE_FULLSCREEN = data->videodata->_NET_WM_STATE_FULLSCREEN;
slouken@5302
   827
slouken@5302
   828
    if (X11_IsWindowMapped(_this, window)) {
slouken@5302
   829
        XEvent e;
slouken@5302
   830
slouken@5302
   831
        SDL_zero(e);
slouken@5302
   832
        e.xany.type = ClientMessage;
slouken@5302
   833
        e.xclient.message_type = _NET_WM_STATE;
slouken@5302
   834
        e.xclient.format = 32;
slouken@5302
   835
        e.xclient.window = data->xwindow;
slouken@5302
   836
        e.xclient.data.l[0] =
slouken@5302
   837
            fullscreen ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
slouken@5302
   838
        e.xclient.data.l[1] = _NET_WM_STATE_FULLSCREEN;
slouken@5302
   839
        e.xclient.data.l[3] = 0l;
slouken@5302
   840
slouken@5302
   841
        XSendEvent(display, RootWindow(display, displaydata->screen), 0,
slouken@5302
   842
                   SubstructureNotifyMask | SubstructureRedirectMask, &e);
slouken@5302
   843
    } else {
slouken@5302
   844
        int count = 0;
slouken@5302
   845
        Atom atoms[3];
slouken@5302
   846
slouken@5302
   847
        if (fullscreen) {
slouken@5302
   848
            atoms[count++] = _NET_WM_STATE_FULLSCREEN;
slouken@5302
   849
        }
slouken@5302
   850
        if (window->flags & SDL_WINDOW_MAXIMIZED) {
slouken@5302
   851
            atoms[count++] = _NET_WM_STATE_MAXIMIZED_VERT;
slouken@5302
   852
            atoms[count++] = _NET_WM_STATE_MAXIMIZED_HORZ;
slouken@5302
   853
        }
slouken@5302
   854
        if (count > 0) {
slouken@5302
   855
            XChangeProperty(display, data->xwindow, _NET_WM_STATE, XA_ATOM, 32,
slouken@5302
   856
                            PropModeReplace, (unsigned char *)atoms, count);
slouken@5302
   857
        } else {
slouken@5302
   858
            XDeleteProperty(display, data->xwindow, _NET_WM_STATE);
slouken@5302
   859
        }
slouken@5302
   860
    }
slouken@5302
   861
    XFlush(display);
slouken@5302
   862
}
slouken@5302
   863
slouken@5302
   864
void
slouken@1951
   865
X11_SetWindowGrab(_THIS, SDL_Window * window)
slouken@1951
   866
{
slouken@2875
   867
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@2875
   868
    Display *display = data->videodata->display;
slouken@4518
   869
    SDL_bool oldstyle_fullscreen;
slouken@2875
   870
slouken@4518
   871
    /* ICCCM2.0-compliant window managers can handle fullscreen windows */
slouken@4522
   872
    oldstyle_fullscreen = X11_IsWindowOldFullscreen(_this, window);
slouken@4518
   873
slouken@4518
   874
    if (((window->flags & SDL_WINDOW_INPUT_GRABBED) || oldstyle_fullscreen)
slouken@2876
   875
        && (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@2875
   876
        /* Try to grab the mouse */
slouken@2876
   877
        for (;;) {
slouken@2876
   878
            int result =
slouken@3685
   879
                XGrabPointer(display, data->xwindow, True, 0, GrabModeAsync,
slouken@3685
   880
                             GrabModeAsync, data->xwindow, None, CurrentTime);
slouken@2876
   881
            if (result == GrabSuccess) {
slouken@2875
   882
                break;
slouken@2875
   883
            }
slouken@2875
   884
            SDL_Delay(100);
slouken@2875
   885
        }
slouken@2875
   886
slouken@2875
   887
        /* Raise the window if we grab the mouse */
slouken@3685
   888
        XRaiseWindow(display, data->xwindow);
slouken@2875
   889
slouken@2875
   890
        /* Now grab the keyboard */
slouken@3685
   891
        XGrabKeyboard(display, data->xwindow, True, GrabModeAsync,
slouken@2876
   892
                      GrabModeAsync, CurrentTime);
slouken@2875
   893
    } else {
slouken@2875
   894
        XUngrabPointer(display, CurrentTime);
slouken@2875
   895
        XUngrabKeyboard(display, CurrentTime);
slouken@2875
   896
    }
slouken@1951
   897
}
slouken@1951
   898
slouken@1951
   899
void
slouken@1951
   900
X11_DestroyWindow(_THIS, SDL_Window * window)
slouken@1951
   901
{
slouken@1951
   902
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
bob@2323
   903
    window->driverdata = NULL;
slouken@1951
   904
slouken@1951
   905
    if (data) {
bob@2323
   906
        SDL_VideoData *videodata = (SDL_VideoData *) data->videodata;
bob@2323
   907
        Display *display = videodata->display;
bob@2323
   908
        int numwindows = videodata->numwindows;
bob@2323
   909
        SDL_WindowData **windowlist = videodata->windowlist;
bob@2323
   910
        int i;
bob@2323
   911
bob@2323
   912
        if (windowlist) {
bob@2324
   913
            for (i = 0; i < numwindows; ++i) {
slouken@3685
   914
                if (windowlist[i] && (windowlist[i]->window == window)) {
bob@2324
   915
                    windowlist[i] = windowlist[numwindows - 1];
bob@2324
   916
                    windowlist[numwindows - 1] = NULL;
bob@2324
   917
                    videodata->numwindows--;
bob@2324
   918
                    break;
bob@2324
   919
                }
bob@2323
   920
            }
bob@2323
   921
        }
slouken@1951
   922
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   923
        if (data->ic) {
slouken@1951
   924
            XDestroyIC(data->ic);
slouken@1951
   925
        }
slouken@1951
   926
#endif
slouken@1951
   927
        if (data->created) {
slouken@3685
   928
            XDestroyWindow(display, data->xwindow);
slouken@5031
   929
            XFlush(display);
slouken@1951
   930
        }
slouken@1951
   931
        SDL_free(data);
slouken@1951
   932
    }
slouken@1951
   933
}
slouken@1951
   934
slouken@1951
   935
SDL_bool
slouken@1951
   936
X11_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1951
   937
{
slouken@4510
   938
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@4510
   939
    Display *display = data->videodata->display;
slouken@4510
   940
slouken@4510
   941
    if (info->version.major == SDL_MAJOR_VERSION &&
slouken@4510
   942
        info->version.minor == SDL_MINOR_VERSION) {
slouken@4510
   943
        info->subsystem = SDL_SYSWM_X11;
slouken@5056
   944
        info->info.x11.display = display;
slouken@5056
   945
        info->info.x11.window = data->xwindow;
slouken@1951
   946
        return SDL_TRUE;
slouken@1951
   947
    } else {
slouken@1951
   948
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1951
   949
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1951
   950
        return SDL_FALSE;
slouken@1951
   951
    }
slouken@1951
   952
}
slouken@1951
   953
slouken@1951
   954
/* vi: set ts=4 sw=4 expandtab: */