src/video/x11/SDL_x11window.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 27 Sep 2012 00:53:37 -0400
changeset 6466 ebe165c00fab
parent 6462 5e09ac1aba80
child 6467 ec5a04e921d4
permissions -rwxr-xr-x
On Compiz (etc?), _NET_WM_ACTION_FULLSCREEN needs the window to be resizable.

Thanks to Edward Rudd for the patch!
slouken@1951
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@1951
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1951
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1951
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1951
    20
*/
slouken@1951
    21
#include "SDL_config.h"
slouken@1951
    22
slouken@5481
    23
#if SDL_VIDEO_DRIVER_X11
slouken@5481
    24
slouken@1951
    25
#include "../SDL_sysvideo.h"
slouken@4465
    26
#include "../SDL_pixels_c.h"
slouken@1951
    27
#include "../../events/SDL_keyboard_c.h"
slouken@2940
    28
#include "../../events/SDL_mouse_c.h"
slouken@1951
    29
slouken@1951
    30
#include "SDL_x11video.h"
slouken@2940
    31
#include "SDL_x11mouse.h"
eligottlieb@4782
    32
#include "SDL_x11shape.h"
dimitris@6316
    33
#include "SDL_x11xinput2.h"
slouken@1951
    34
slouken@6188
    35
#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
slouken@3161
    36
#include "SDL_x11opengles.h"
slouken@3161
    37
#endif
slouken@3161
    38
slouken@4465
    39
#include "SDL_timer.h"
slouken@3241
    40
#include "SDL_syswm.h"
slouken@3241
    41
slouken@3077
    42
#define _NET_WM_STATE_REMOVE    0l
slouken@3077
    43
#define _NET_WM_STATE_ADD       1l
slouken@3077
    44
#define _NET_WM_STATE_TOGGLE    2l
slouken@3077
    45
slouken@4518
    46
static SDL_bool
slouken@4522
    47
X11_IsWindowOldFullscreen(_THIS, SDL_Window * window)
slouken@4518
    48
{
slouken@4522
    49
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@4518
    50
slouken@4518
    51
    /* ICCCM2.0-compliant window managers can handle fullscreen windows */
slouken@4518
    52
    if ((window->flags & SDL_WINDOW_FULLSCREEN) && !videodata->net_wm) {
slouken@4518
    53
        return SDL_TRUE;
slouken@4518
    54
    } else {
slouken@4518
    55
        return SDL_FALSE;
slouken@4518
    56
    }
slouken@4518
    57
}
slouken@4518
    58
slouken@4522
    59
static SDL_bool
slouken@4522
    60
X11_IsWindowMapped(_THIS, SDL_Window * window)
slouken@4522
    61
{
slouken@4522
    62
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@4522
    63
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@4522
    64
    XWindowAttributes attr;
slouken@4522
    65
slouken@4522
    66
    XGetWindowAttributes(videodata->display, data->xwindow, &attr);
slouken@4522
    67
    if (attr.map_state != IsUnmapped) {
slouken@4522
    68
        return SDL_TRUE;
slouken@4522
    69
    } else {
slouken@4522
    70
        return SDL_FALSE;
slouken@4522
    71
    }
slouken@4522
    72
}
slouken@4522
    73
slouken@4522
    74
static int
slouken@4522
    75
X11_GetWMStateProperty(_THIS, SDL_Window * window, Atom atoms[3])
slouken@4522
    76
{
slouken@4522
    77
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@4522
    78
    int count = 0;
slouken@4522
    79
slouken@4522
    80
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@4522
    81
        atoms[count++] = data->_NET_WM_STATE_FULLSCREEN;
slouken@4522
    82
    }
slouken@4522
    83
    if (window->flags & SDL_WINDOW_MAXIMIZED) {
slouken@4522
    84
        atoms[count++] = data->_NET_WM_STATE_MAXIMIZED_VERT;
slouken@4522
    85
        atoms[count++] = data->_NET_WM_STATE_MAXIMIZED_HORZ;
slouken@4522
    86
    }
slouken@4522
    87
    return count;
slouken@4522
    88
}
slouken@4522
    89
slouken@1951
    90
static int
slouken@1951
    91
SetupWindowData(_THIS, SDL_Window * window, Window w, BOOL created)
slouken@1951
    92
{
slouken@1951
    93
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1951
    94
    SDL_WindowData *data;
slouken@1951
    95
    int numwindows = videodata->numwindows;
bob@2324
    96
    int windowlistlength = videodata->windowlistlength;
slouken@1951
    97
    SDL_WindowData **windowlist = videodata->windowlist;
slouken@1951
    98
slouken@1951
    99
    /* Allocate the window data */
bob@2323
   100
    data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
slouken@1951
   101
    if (!data) {
slouken@1951
   102
        SDL_OutOfMemory();
slouken@1951
   103
        return -1;
slouken@1951
   104
    }
slouken@3685
   105
    data->window = window;
slouken@3685
   106
    data->xwindow = w;
slouken@1951
   107
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   108
    if (SDL_X11_HAVE_UTF8) {
slouken@1951
   109
        data->ic =
slouken@1951
   110
            pXCreateIC(videodata->im, XNClientWindow, w, XNFocusWindow, w,
slouken@1951
   111
                       XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
slouken@1951
   112
                       XNResourceName, videodata->classname, XNResourceClass,
slouken@1951
   113
                       videodata->classname, NULL);
slouken@1951
   114
    }
slouken@1951
   115
#endif
slouken@1951
   116
    data->created = created;
slouken@1951
   117
    data->videodata = videodata;
slouken@1951
   118
bob@2323
   119
    /* Associate the data with the window */
bob@2324
   120
bob@2324
   121
    if (numwindows < windowlistlength) {
bob@2324
   122
        windowlist[numwindows] = data;
bob@2324
   123
        videodata->numwindows++;
bob@2324
   124
    } else {
bob@2324
   125
        windowlist =
bob@2324
   126
            (SDL_WindowData **) SDL_realloc(windowlist,
bob@2324
   127
                                            (numwindows +
bob@2324
   128
                                             1) * sizeof(*windowlist));
bob@2324
   129
        if (!windowlist) {
bob@2324
   130
            SDL_OutOfMemory();
bob@2324
   131
            SDL_free(data);
bob@2324
   132
            return -1;
bob@2323
   133
        }
bob@2324
   134
        windowlist[numwindows] = data;
bob@2324
   135
        videodata->numwindows++;
bob@2324
   136
        videodata->windowlistlength++;
bob@2324
   137
        videodata->windowlist = windowlist;
bob@2323
   138
    }
bob@2323
   139
slouken@1951
   140
    /* Fill in the SDL window with the window data */
slouken@1951
   141
    {
slouken@1951
   142
        XWindowAttributes attrib;
slouken@1951
   143
slouken@1951
   144
        XGetWindowAttributes(data->videodata->display, w, &attrib);
slouken@1951
   145
        window->x = attrib.x;
slouken@1951
   146
        window->y = attrib.y;
slouken@1951
   147
        window->w = attrib.width;
slouken@1951
   148
        window->h = attrib.height;
slouken@1951
   149
        if (attrib.map_state != IsUnmapped) {
slouken@1951
   150
            window->flags |= SDL_WINDOW_SHOWN;
slouken@1951
   151
        } else {
slouken@1951
   152
            window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1951
   153
        }
slouken@5182
   154
        data->visual = attrib.visual;
slouken@5466
   155
        data->colormap = attrib.colormap;
slouken@1951
   156
    }
slouken@3077
   157
slouken@3077
   158
    {
slouken@4518
   159
        Atom _NET_WM_STATE = data->videodata->_NET_WM_STATE;
slouken@4518
   160
        Atom _NET_WM_STATE_MAXIMIZED_VERT = data->videodata->_NET_WM_STATE_MAXIMIZED_VERT;
slouken@4518
   161
        Atom _NET_WM_STATE_MAXIMIZED_HORZ = data->videodata->_NET_WM_STATE_MAXIMIZED_HORZ;
slouken@4518
   162
        Atom _NET_WM_STATE_FULLSCREEN = data->videodata->_NET_WM_STATE_FULLSCREEN;
slouken@3077
   163
        Atom actualType;
slouken@3077
   164
        int actualFormat;
slouken@3077
   165
        unsigned long i, numItems, bytesAfter;
slouken@3077
   166
        unsigned char *propertyValue = NULL;
slouken@3077
   167
        long maxLength = 1024;
slouken@3077
   168
slouken@3077
   169
        if (XGetWindowProperty(data->videodata->display, w, _NET_WM_STATE,
slouken@3078
   170
                               0l, maxLength, False, XA_ATOM, &actualType,
slouken@3078
   171
                               &actualFormat, &numItems, &bytesAfter,
slouken@3078
   172
                               &propertyValue) == Success) {
slouken@3078
   173
            Atom *atoms = (Atom *) propertyValue;
slouken@3077
   174
            int maximized = 0;
slouken@4518
   175
            int fullscreen = 0;
slouken@3077
   176
slouken@3077
   177
            for (i = 0; i < numItems; ++i) {
slouken@3077
   178
                if (atoms[i] == _NET_WM_STATE_MAXIMIZED_VERT) {
slouken@3077
   179
                    maximized |= 1;
slouken@3077
   180
                } else if (atoms[i] == _NET_WM_STATE_MAXIMIZED_HORZ) {
slouken@3077
   181
                    maximized |= 2;
slouken@4518
   182
                } else if ( atoms[i] == _NET_WM_STATE_FULLSCREEN) {
slouken@4518
   183
                    fullscreen = 1;
slouken@3077
   184
                }
slouken@3077
   185
            }
slouken@3077
   186
            if (maximized == 3) {
slouken@3077
   187
                window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@4518
   188
            }  else if (fullscreen == 1) {
slouken@4518
   189
                window->flags |= SDL_WINDOW_FULLSCREEN;
slouken@3077
   190
            }
slouken@3077
   191
            XFree(propertyValue);
slouken@3077
   192
        }
slouken@3077
   193
    }
slouken@3077
   194
icculus@6024
   195
    {
icculus@6024
   196
        Window FocalWindow;
icculus@6024
   197
        int RevertTo=0;
icculus@6024
   198
        XGetInputFocus(data->videodata->display, &FocalWindow, &RevertTo);
icculus@6024
   199
        if (FocalWindow==w)
icculus@6024
   200
        {
icculus@6024
   201
            window->flags |= SDL_WINDOW_INPUT_FOCUS;
icculus@6024
   202
            SDL_SetKeyboardFocus(data->window);
icculus@6024
   203
        }
icculus@6024
   204
icculus@6024
   205
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
icculus@6024
   206
            /* Tell x11 to clip mouse */
icculus@6024
   207
        }
icculus@6024
   208
    }
icculus@6024
   209
slouken@1951
   210
    /* FIXME: How can I tell?
slouken@1951
   211
       {
slouken@1951
   212
       DWORD style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1951
   213
       if (style & WS_VISIBLE) {
slouken@1951
   214
       if (style & (WS_BORDER | WS_THICKFRAME)) {
slouken@1951
   215
       window->flags &= ~SDL_WINDOW_BORDERLESS;
slouken@1951
   216
       } else {
slouken@1951
   217
       window->flags |= SDL_WINDOW_BORDERLESS;
slouken@1951
   218
       }
slouken@1951
   219
       if (style & WS_THICKFRAME) {
slouken@1951
   220
       window->flags |= SDL_WINDOW_RESIZABLE;
slouken@1951
   221
       } else {
slouken@1951
   222
       window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@1951
   223
       }
slouken@1951
   224
       if (style & WS_MINIMIZE) {
slouken@1951
   225
       window->flags |= SDL_WINDOW_MINIMIZED;
slouken@1951
   226
       } else {
slouken@1951
   227
       window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@1951
   228
       }
slouken@1951
   229
       }
slouken@1951
   230
       if (GetFocus() == hwnd) {
slouken@1951
   231
       int index = data->videodata->keyboard;
slouken@1951
   232
       window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@3685
   233
       SDL_SetKeyboardFocus(index, data->window);
slouken@1951
   234
slouken@1951
   235
       if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1951
   236
       RECT rect;
slouken@1951
   237
       GetClientRect(hwnd, &rect);
slouken@1951
   238
       ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1951
   239
       ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1951
   240
       ClipCursor(&rect);
slouken@1951
   241
       }
slouken@1951
   242
       }
slouken@1951
   243
     */
slouken@1951
   244
slouken@1951
   245
    /* All done! */
slouken@1951
   246
    window->driverdata = data;
slouken@1951
   247
    return 0;
slouken@1951
   248
}
slouken@1951
   249
icculus@6422
   250
static void
icculus@6422
   251
SetWindowBordered(Display *display, int screen, Window window, SDL_bool border)
icculus@6422
   252
{
icculus@6422
   253
    /*
icculus@6422
   254
     * this code used to check for KWM_WIN_DECORATION, but KDE hasn't
icculus@6422
   255
     *  supported it for years and years. It now respects _MOTIF_WM_HINTS.
icculus@6422
   256
     *  Gnome is similar: just use the Motif atom.
icculus@6422
   257
     */
icculus@6422
   258
icculus@6422
   259
    Atom WM_HINTS = XInternAtom(display, "_MOTIF_WM_HINTS", True);
icculus@6422
   260
    if (WM_HINTS != None) {
icculus@6422
   261
        /* Hints used by Motif compliant window managers */
icculus@6422
   262
        struct
icculus@6422
   263
        {
icculus@6422
   264
            unsigned long flags;
icculus@6422
   265
            unsigned long functions;
icculus@6422
   266
            unsigned long decorations;
icculus@6422
   267
            long input_mode;
icculus@6422
   268
            unsigned long status;
icculus@6422
   269
        } MWMHints = {
icculus@6422
   270
            (1L << 1), 0, border ? 1 : 0, 0, 0
icculus@6422
   271
        };
icculus@6422
   272
icculus@6422
   273
        XChangeProperty(display, window, WM_HINTS, WM_HINTS, 32,
icculus@6422
   274
                        PropModeReplace, (unsigned char *) &MWMHints,
icculus@6422
   275
                        sizeof(MWMHints) / 4);
icculus@6422
   276
    } else {  /* set the transient hints instead, if necessary */
icculus@6422
   277
        XSetTransientForHint(display, window, RootWindow(display, screen));
icculus@6422
   278
    }
icculus@6422
   279
}
icculus@6422
   280
slouken@1951
   281
int
slouken@1951
   282
X11_CreateWindow(_THIS, SDL_Window * window)
slouken@1951
   283
{
slouken@1951
   284
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   285
    SDL_DisplayData *displaydata =
slouken@5246
   286
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@4521
   287
    Display *display = data->display;
slouken@4521
   288
    int screen = displaydata->screen;
slouken@1951
   289
    Visual *visual;
slouken@1951
   290
    int depth;
slouken@1951
   291
    XSetWindowAttributes xattr;
slouken@1951
   292
    Window w;
urkle@6462
   293
    XSizeHints *sizehints;
urkle@6462
   294
    XWMHints *wmhints;
urkle@6462
   295
    XClassHint *classhints;
slouken@4521
   296
    Atom _NET_WM_WINDOW_TYPE;
slouken@4521
   297
    Atom _NET_WM_WINDOW_TYPE_NORMAL;
mail@6167
   298
    Atom _NET_WM_PID;
slouken@4522
   299
    int wmstate_count;
slouken@4522
   300
    Atom wmstate_atoms[3];
icculus@6045
   301
    Uint32 fevent = 0;
slouken@4518
   302
slouken@6369
   303
#if SDL_VIDEO_OPENGL_GLX || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
slouken@1951
   304
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@1952
   305
        XVisualInfo *vinfo;
slouken@1952
   306
slouken@6370
   307
#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2        
slouken@6370
   308
        if (_this->gl_config.use_egl == 1) {
slouken@6369
   309
            vinfo = X11_GLES_GetVisual(_this, display, screen);
slouken@6369
   310
        } else
slouken@6369
   311
#endif
slouken@6369
   312
        {
slouken@6370
   313
#if SDL_VIDEO_OPENGL_GLX
slouken@6369
   314
            vinfo = X11_GL_GetVisual(_this, display, screen);
slouken@6370
   315
#endif
slouken@1952
   316
        }
slouken@3161
   317
        if (!vinfo) {
slouken@3161
   318
            return -1;
slouken@3161
   319
        }
slouken@3161
   320
        visual = vinfo->visual;
slouken@3161
   321
        depth = vinfo->depth;
slouken@3161
   322
        XFree(vinfo);
slouken@3161
   323
    } else
slouken@3161
   324
#endif
slouken@1952
   325
    {
slouken@1951
   326
        visual = displaydata->visual;
slouken@1951
   327
        depth = displaydata->depth;
slouken@1951
   328
    }
slouken@1951
   329
slouken@5386
   330
    xattr.override_redirect = False;
slouken@1951
   331
    xattr.background_pixel = 0;
slouken@1951
   332
    xattr.border_pixel = 0;
slouken@5466
   333
slouken@5466
   334
    if (visual->class == DirectColor) {
slouken@5466
   335
        XColor *colorcells;
slouken@5466
   336
        int i;
slouken@5466
   337
        int ncolors;
slouken@5466
   338
        int rmax, gmax, bmax;
slouken@5466
   339
        int rmask, gmask, bmask;
slouken@5466
   340
        int rshift, gshift, bshift;
slouken@5466
   341
slouken@5466
   342
        xattr.colormap =
slouken@5466
   343
            XCreateColormap(display, RootWindow(display, screen),
slouken@5466
   344
                            visual, AllocAll);
slouken@5466
   345
slouken@5466
   346
        /* If we can't create a colormap, then we must die */
slouken@5466
   347
        if (!xattr.colormap) {
slouken@5466
   348
            SDL_SetError("Could not create writable colormap");
slouken@5466
   349
            return -1;
slouken@5466
   350
        }
slouken@5466
   351
slouken@5466
   352
        /* OK, we got a colormap, now fill it in as best as we can */
slouken@5466
   353
        colorcells = SDL_malloc(visual->map_entries * sizeof(XColor));
slouken@5466
   354
        if (!colorcells) {
slouken@5466
   355
            SDL_OutOfMemory();
slouken@5466
   356
            return -1;
slouken@5466
   357
        }
slouken@5466
   358
        ncolors = visual->map_entries;
slouken@5466
   359
        rmax = 0xffff;
slouken@5466
   360
        gmax = 0xffff;
slouken@5466
   361
        bmax = 0xffff;
slouken@5466
   362
slouken@5466
   363
        rshift = 0;
slouken@5466
   364
        rmask = visual->red_mask;
slouken@5466
   365
        while (0 == (rmask & 1)) {
slouken@5466
   366
            rshift++;
slouken@5466
   367
            rmask >>= 1;
slouken@5466
   368
        }
slouken@5466
   369
slouken@5466
   370
        gshift = 0;
slouken@5466
   371
        gmask = visual->green_mask;
slouken@5466
   372
        while (0 == (gmask & 1)) {
slouken@5466
   373
            gshift++;
slouken@5466
   374
            gmask >>= 1;
slouken@5466
   375
        }
slouken@5466
   376
slouken@5466
   377
        bshift = 0;
slouken@5466
   378
        bmask = visual->blue_mask;
slouken@5466
   379
        while (0 == (bmask & 1)) {
slouken@5466
   380
            bshift++;
slouken@5466
   381
            bmask >>= 1;
slouken@5466
   382
        }
slouken@5466
   383
slouken@5466
   384
        /* build the color table pixel values */
slouken@5466
   385
        for (i = 0; i < ncolors; i++) {
slouken@5466
   386
            Uint32 red = (rmax * i) / (ncolors - 1);
slouken@5466
   387
            Uint32 green = (gmax * i) / (ncolors - 1);
slouken@5466
   388
            Uint32 blue = (bmax * i) / (ncolors - 1);
slouken@5466
   389
slouken@5466
   390
            Uint32 rbits = (rmask * i) / (ncolors - 1);
slouken@5466
   391
            Uint32 gbits = (gmask * i) / (ncolors - 1);
slouken@5466
   392
            Uint32 bbits = (bmask * i) / (ncolors - 1);
slouken@5466
   393
slouken@5466
   394
            Uint32 pix =
slouken@5466
   395
                (rbits << rshift) | (gbits << gshift) | (bbits << bshift);
slouken@5466
   396
slouken@5466
   397
            colorcells[i].pixel = pix;
slouken@5466
   398
slouken@5466
   399
            colorcells[i].red = red;
slouken@5466
   400
            colorcells[i].green = green;
slouken@5466
   401
            colorcells[i].blue = blue;
slouken@5466
   402
slouken@5466
   403
            colorcells[i].flags = DoRed | DoGreen | DoBlue;
slouken@5466
   404
        }
slouken@5466
   405
slouken@5466
   406
        XStoreColors(display, xattr.colormap, colorcells, ncolors);
slouken@5466
   407
slouken@5466
   408
        SDL_free(colorcells);
slouken@5466
   409
    } else {
slouken@5466
   410
        xattr.colormap =
slouken@5466
   411
            XCreateColormap(display, RootWindow(display, screen),
slouken@5466
   412
                            visual, AllocNone);
slouken@5466
   413
    }
slouken@1951
   414
slouken@5386
   415
    w = XCreateWindow(display, RootWindow(display, screen),
slouken@5386
   416
                      window->x, window->y, window->w, window->h,
slouken@5386
   417
                      0, depth, InputOutput, visual,
slouken@1951
   418
                      (CWOverrideRedirect | CWBackPixel | CWBorderPixel |
slouken@1951
   419
                       CWColormap), &xattr);
slouken@1952
   420
    if (!w) {
slouken@1952
   421
        SDL_SetError("Couldn't create window");
slouken@1952
   422
        return -1;
slouken@1952
   423
    }
slouken@6188
   424
#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
slouken@6373
   425
    if ((window->flags & SDL_WINDOW_OPENGL) && (_this->gl_config.use_egl == 1)) {
slouken@6373
   426
        if (!_this->gles_data) {
slouken@6373
   427
            XDestroyWindow(display, w);
slouken@6373
   428
            return -1;
slouken@6373
   429
        }
slouken@6373
   430
slouken@6188
   431
        /* Create the GLES window surface */
slouken@6188
   432
        _this->gles_data->egl_surface =
slouken@6188
   433
            _this->gles_data->eglCreateWindowSurface(_this->gles_data->
slouken@3161
   434
                                                 egl_display,
slouken@3161
   435
                                                 _this->gles_data->egl_config,
slouken@3161
   436
                                                 (NativeWindowType) w, NULL);
slouken@3161
   437
slouken@6188
   438
        if (_this->gles_data->egl_surface == EGL_NO_SURFACE) {
slouken@6188
   439
            SDL_SetError("Could not create GLES window surface");
slouken@6373
   440
            XDestroyWindow(display, w);
slouken@6188
   441
            return -1;
slouken@6188
   442
        }
slouken@3161
   443
    }
slouken@3161
   444
#endif
slouken@1951
   445
icculus@6422
   446
    SetWindowBordered(display, screen, w,
icculus@6422
   447
                      (window->flags & SDL_WINDOW_BORDERLESS) == 0);
slouken@1951
   448
urkle@6462
   449
    sizehints = XAllocSizeHints();
mail@6167
   450
    /* Setup the normal size hints */
urkle@6462
   451
    sizehints->flags = 0;
mail@6167
   452
    if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
urkle@6462
   453
        sizehints->min_width = sizehints->max_width = window->w;
urkle@6462
   454
        sizehints->min_height = sizehints->max_height = window->h;
urkle@6462
   455
        sizehints->flags |= (PMaxSize | PMinSize);
slouken@1951
   456
    }
urkle@6462
   457
    sizehints->x = window->x;
urkle@6462
   458
    sizehints->y = window->y;
urkle@6462
   459
    sizehints->flags |= USPosition;
slouken@1951
   460
mail@6167
   461
    /* Setup the input hints so we get keyboard input */
urkle@6462
   462
    wmhints = XAllocWMHints();
urkle@6462
   463
    wmhints->input = True;
urkle@6462
   464
    wmhints->flags = InputHint;
mail@6167
   465
mail@6167
   466
    /* Setup the class hints so we can get an icon (AfterStep) */
urkle@6462
   467
    classhints = XAllocClassHint();
urkle@6462
   468
    classhints->res_name = data->classname;
urkle@6462
   469
    classhints->res_class = data->classname;
mail@6167
   470
mail@6167
   471
    /* Set the size, input and class hints, and define WM_CLIENT_MACHINE and WM_LOCALE_NAME */
urkle@6462
   472
    XSetWMProperties(display, w, NULL, NULL, NULL, 0, sizehints, wmhints, classhints);
mail@6167
   473
urkle@6462
   474
    XFree(sizehints);
urkle@6462
   475
    XFree(wmhints);
urkle@6462
   476
    XFree(classhints);
mail@6167
   477
    /* Set the PID related to the window for the given hostname, if possible */
mail@6167
   478
    if (data->pid > 0) {
mail@6167
   479
        _NET_WM_PID = XInternAtom(display, "_NET_WM_PID", False);
mail@6167
   480
        XChangeProperty(display, w, _NET_WM_PID, XA_CARDINAL, 32, PropModeReplace,
mail@6167
   481
                        (unsigned char *)&data->pid, 1);
slouken@1951
   482
    }
slouken@1951
   483
slouken@4522
   484
    /* Set the window manager state */
slouken@4522
   485
    wmstate_count = X11_GetWMStateProperty(_this, window, wmstate_atoms);
slouken@4522
   486
    if (wmstate_count > 0) {
slouken@4522
   487
        XChangeProperty(display, w, data->_NET_WM_STATE, XA_ATOM, 32,
slouken@4522
   488
                        PropModeReplace,
slouken@4522
   489
                        (unsigned char *)wmstate_atoms, wmstate_count);
slouken@4522
   490
    } else {
slouken@4522
   491
        XDeleteProperty(display, w, data->_NET_WM_STATE);
slouken@4518
   492
    }
slouken@4518
   493
slouken@4521
   494
    /* Let the window manager know we're a "normal" window */
slouken@4521
   495
    _NET_WM_WINDOW_TYPE = XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
slouken@4521
   496
    _NET_WM_WINDOW_TYPE_NORMAL = XInternAtom(display, "_NET_WM_WINDOW_TYPE_NORMAL", False);
slouken@4521
   497
    XChangeProperty(display, w, _NET_WM_WINDOW_TYPE, XA_ATOM, 32,
slouken@4521
   498
                    PropModeReplace,
slouken@4521
   499
                    (unsigned char *)&_NET_WM_WINDOW_TYPE_NORMAL, 1);
slouken@4521
   500
slouken@1951
   501
    /* Allow the window to be deleted by the window manager */
slouken@4521
   502
    XSetWMProtocols(display, w, &data->WM_DELETE_WINDOW, 1);
slouken@1951
   503
slouken@1951
   504
    if (SetupWindowData(_this, window, w, SDL_TRUE) < 0) {
slouken@4521
   505
        XDestroyWindow(display, w);
slouken@1951
   506
        return -1;
slouken@1951
   507
    }
slouken@5386
   508
bob@2325
   509
#ifdef X_HAVE_UTF8_STRING
icculus@6045
   510
    if (SDL_X11_HAVE_UTF8) {
bob@2325
   511
        pXGetICValues(((SDL_WindowData *) window->driverdata)->ic,
bob@2325
   512
                      XNFilterEvents, &fevent, NULL);
kazeuser@2718
   513
    }
bob@2325
   514
#endif
bob@2325
   515
dimitris@6316
   516
    X11_Xinput2SelectTouch(_this, window);
dimitris@6316
   517
icculus@6045
   518
    XSelectInput(display, w,
icculus@6045
   519
                 (FocusChangeMask | EnterWindowMask | LeaveWindowMask |
icculus@6045
   520
                 ExposureMask | ButtonPressMask | ButtonReleaseMask |
icculus@6045
   521
                 PointerMotionMask | KeyPressMask | KeyReleaseMask |
icculus@6045
   522
                 PropertyChangeMask | StructureNotifyMask |
icculus@6045
   523
                 KeymapStateMask | fevent));
icculus@6045
   524
slouken@5031
   525
    XFlush(display);
slouken@5031
   526
slouken@1951
   527
    return 0;
slouken@1951
   528
}
slouken@1951
   529
slouken@1951
   530
int
bob@2324
   531
X11_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1951
   532
{
slouken@1951
   533
    Window w = (Window) data;
slouken@1951
   534
slouken@4518
   535
    window->title = X11_GetWindowTitle(_this, w);
slouken@1951
   536
slouken@1951
   537
    if (SetupWindowData(_this, window, w, SDL_FALSE) < 0) {
slouken@1951
   538
        return -1;
slouken@1951
   539
    }
slouken@1951
   540
    return 0;
slouken@1951
   541
}
slouken@1951
   542
slouken@4518
   543
char *
slouken@4518
   544
X11_GetWindowTitle(_THIS, Window xwindow)
slouken@4518
   545
{
slouken@4518
   546
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@4518
   547
    Display *display = data->display;
slouken@4518
   548
    int status, real_format;
slouken@4518
   549
    Atom real_type;
slouken@4518
   550
    unsigned long items_read, items_left;
slouken@4518
   551
    unsigned char *propdata;
slouken@4518
   552
    char *title = NULL;
slouken@4518
   553
slouken@4518
   554
    status = XGetWindowProperty(display, xwindow, data->_NET_WM_NAME,
slouken@4518
   555
                0L, 8192L, False, data->UTF8_STRING, &real_type, &real_format,
slouken@4518
   556
                &items_read, &items_left, &propdata);
icculus@6023
   557
    if (status == Success && propdata) {
slouken@4518
   558
        title = SDL_strdup(SDL_static_cast(char*, propdata));
slouken@4518
   559
        XFree(propdata);
slouken@4518
   560
    } else {
slouken@4518
   561
        status = XGetWindowProperty(display, xwindow, XA_WM_NAME,
slouken@4518
   562
                    0L, 8192L, False, XA_STRING, &real_type, &real_format,
slouken@4518
   563
                    &items_read, &items_left, &propdata);
icculus@6023
   564
        if (status == Success && propdata) {
slouken@4518
   565
            title = SDL_iconv_string("UTF-8", "", SDL_static_cast(char*, propdata), items_read+1);
slouken@4518
   566
        } else {
slouken@4518
   567
            title = SDL_strdup("");
slouken@4518
   568
        }
slouken@4518
   569
    }
slouken@4518
   570
    return title;
slouken@4518
   571
}
slouken@4518
   572
slouken@1951
   573
void
slouken@1951
   574
X11_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1951
   575
{
slouken@1951
   576
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   577
    Display *display = data->videodata->display;
slouken@1951
   578
    XTextProperty titleprop, iconprop;
slouken@1951
   579
    Status status;
slouken@1951
   580
    const char *title = window->title;
slouken@1951
   581
    const char *icon = NULL;
slouken@1951
   582
slouken@1951
   583
#ifdef X_HAVE_UTF8_STRING
slouken@4518
   584
    Atom _NET_WM_NAME = data->videodata->_NET_WM_NAME;
slouken@4518
   585
    Atom _NET_WM_ICON_NAME = data->videodata->_NET_WM_ICON_NAME;
slouken@1951
   586
#endif
slouken@1951
   587
slouken@1951
   588
    if (title != NULL) {
slouken@2143
   589
        char *title_locale = SDL_iconv_utf8_locale(title);
slouken@2143
   590
        if (!title_locale) {
slouken@1951
   591
            SDL_OutOfMemory();
slouken@1951
   592
            return;
slouken@1951
   593
        }
slouken@2143
   594
        status = XStringListToTextProperty(&title_locale, 1, &titleprop);
slouken@2143
   595
        SDL_free(title_locale);
slouken@1951
   596
        if (status) {
slouken@3685
   597
            XSetTextProperty(display, data->xwindow, &titleprop, XA_WM_NAME);
slouken@1951
   598
            XFree(titleprop.value);
slouken@1951
   599
        }
slouken@1951
   600
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   601
        if (SDL_X11_HAVE_UTF8) {
slouken@1951
   602
            status =
slouken@1951
   603
                Xutf8TextListToTextProperty(display, (char **) &title, 1,
slouken@1951
   604
                                            XUTF8StringStyle, &titleprop);
slouken@1951
   605
            if (status == Success) {
slouken@3685
   606
                XSetTextProperty(display, data->xwindow, &titleprop,
slouken@1951
   607
                                 _NET_WM_NAME);
slouken@1951
   608
                XFree(titleprop.value);
slouken@1951
   609
            }
slouken@1951
   610
        }
slouken@1951
   611
#endif
slouken@1951
   612
    }
slouken@1951
   613
    if (icon != NULL) {
slouken@2143
   614
        char *icon_locale = SDL_iconv_utf8_locale(icon);
slouken@2143
   615
        if (!icon_locale) {
slouken@1951
   616
            SDL_OutOfMemory();
slouken@1951
   617
            return;
slouken@1951
   618
        }
slouken@2143
   619
        status = XStringListToTextProperty(&icon_locale, 1, &iconprop);
slouken@2143
   620
        SDL_free(icon_locale);
slouken@1951
   621
        if (status) {
slouken@3685
   622
            XSetTextProperty(display, data->xwindow, &iconprop,
slouken@1951
   623
                             XA_WM_ICON_NAME);
slouken@1951
   624
            XFree(iconprop.value);
slouken@1951
   625
        }
slouken@1951
   626
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   627
        if (SDL_X11_HAVE_UTF8) {
slouken@1951
   628
            status =
slouken@1951
   629
                Xutf8TextListToTextProperty(display, (char **) &icon, 1,
slouken@1951
   630
                                            XUTF8StringStyle, &iconprop);
slouken@1951
   631
            if (status == Success) {
slouken@3685
   632
                XSetTextProperty(display, data->xwindow, &iconprop,
slouken@1951
   633
                                 _NET_WM_ICON_NAME);
slouken@1951
   634
                XFree(iconprop.value);
slouken@1951
   635
            }
slouken@1951
   636
        }
slouken@1951
   637
#endif
slouken@1951
   638
    }
slouken@5031
   639
    XFlush(display);
slouken@1951
   640
}
slouken@1951
   641
slouken@1951
   642
void
slouken@2967
   643
X11_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slouken@2967
   644
{
slouken@2967
   645
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@2967
   646
    Display *display = data->videodata->display;
slouken@4518
   647
    Atom _NET_WM_ICON = data->videodata->_NET_WM_ICON;
slouken@2967
   648
slouken@2967
   649
    if (icon) {
slouken@2967
   650
        SDL_PixelFormat format;
slouken@2967
   651
        SDL_Surface *surface;
slouken@2967
   652
        int propsize;
slouken@4528
   653
        long *propdata;
slouken@2967
   654
slouken@2967
   655
        /* Convert the icon to ARGB for modern window managers */
slouken@5288
   656
        SDL_InitFormat(&format, SDL_PIXELFORMAT_ARGB8888);
slouken@2967
   657
        surface = SDL_ConvertSurface(icon, &format, 0);
slouken@2967
   658
        if (!surface) {
slouken@2967
   659
            return;
slouken@2967
   660
        }
slouken@2967
   661
slouken@2967
   662
        /* Set the _NET_WM_ICON property */
slouken@2990
   663
        propsize = 2 + (icon->w * icon->h);
slouken@4566
   664
        propdata = SDL_malloc(propsize * sizeof(long));
slouken@2967
   665
        if (propdata) {
slouken@4528
   666
            int x, y;
slouken@4528
   667
            Uint32 *src;
slouken@4528
   668
            long *dst;
slouken@4528
   669
slouken@2967
   670
            propdata[0] = icon->w;
slouken@2967
   671
            propdata[1] = icon->h;
slouken@4528
   672
            dst = &propdata[2];
slouken@4528
   673
            for (y = 0; y < icon->h; ++y) {
slouken@4528
   674
                src = (Uint32*)((Uint8*)surface->pixels + y * surface->pitch);
slouken@4528
   675
                for (x = 0; x < icon->w; ++x) {
slouken@4528
   676
                    *dst++ = *src++;
slouken@4528
   677
                }
slouken@4528
   678
            }
slouken@3685
   679
            XChangeProperty(display, data->xwindow, _NET_WM_ICON, XA_CARDINAL,
slouken@2990
   680
                            32, PropModeReplace, (unsigned char *) propdata,
slouken@2990
   681
                            propsize);
slouken@2967
   682
        }
slouken@2967
   683
        SDL_FreeSurface(surface);
slouken@2967
   684
    } else {
slouken@3685
   685
        XDeleteProperty(display, data->xwindow, _NET_WM_ICON);
slouken@2967
   686
    }
slouken@5031
   687
    XFlush(display);
slouken@2967
   688
}
slouken@2967
   689
slouken@2967
   690
void
slouken@1951
   691
X11_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1951
   692
{
slouken@1951
   693
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   694
    Display *display = data->videodata->display;
slouken@1951
   695
slouken@5478
   696
    XMoveWindow(display, data->xwindow, window->x, window->y);
slouken@5031
   697
    XFlush(display);
slouken@1951
   698
}
slouken@1951
   699
slouken@1951
   700
void
slouken@1951
   701
X11_SetWindowSize(_THIS, SDL_Window * window)
slouken@1951
   702
{
slouken@1951
   703
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   704
    Display *display = data->videodata->display;
slouken@1951
   705
slouken@4937
   706
    if (SDL_IsShapedWindow(window))
eligottlieb@4782
   707
        X11_ResizeWindowShape(window);
slouken@5531
   708
    if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
slouken@5531
   709
         /* Apparently, if the X11 Window is set to a 'non-resizable' window, you cannot resize it using the XResizeWindow, thus
slouken@5531
   710
            we must set the size hints to adjust the window size.*/
slouken@5531
   711
         XSizeHints *sizehints = XAllocSizeHints();
slouken@5531
   712
         long userhints;
slouken@5531
   713
slouken@5531
   714
         XGetWMNormalHints(display, data->xwindow, sizehints, &userhints);
slouken@5531
   715
slouken@6343
   716
         sizehints->min_width = sizehints->max_width = window->w;
slouken@5531
   717
         sizehints->min_height = sizehints->max_height = window->h;
slouken@5531
   718
slouken@5531
   719
         XSetWMNormalHints(display, data->xwindow, sizehints);
slouken@5531
   720
slouken@5531
   721
         XFree(sizehints);
slouken@5531
   722
    } else
slouken@5531
   723
        XResizeWindow(display, data->xwindow, window->w, window->h);
slouken@5031
   724
    XFlush(display);
slouken@1951
   725
}
slouken@1951
   726
slouken@6336
   727
static Bool isMapNotify(Display *dpy, XEvent *ev, XPointer win)
slouken@6336
   728
{
slouken@6336
   729
    return ev->type == MapNotify && ev->xmap.window == *((Window*)win);
slouken@6336
   730
}
slouken@6336
   731
static Bool isUnmapNotify(Display *dpy, XEvent *ev, XPointer win)
slouken@6336
   732
{
slouken@6336
   733
    return ev->type == UnmapNotify && ev->xunmap.window == *((Window*)win);
slouken@6336
   734
}
icculus@6422
   735
static Bool isConfigureNotify(Display *dpy, XEvent *ev, XPointer win)
icculus@6422
   736
{
icculus@6422
   737
    return ev->type == ConfigureNotify && ev->xunmap.window == *((Window*)win);
icculus@6422
   738
}
icculus@6422
   739
icculus@6422
   740
void
icculus@6422
   741
X11_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
icculus@6422
   742
{
icculus@6423
   743
    const SDL_bool focused = ((window->flags & SDL_WINDOW_INPUT_FOCUS) != 0);
icculus@6423
   744
    const SDL_bool visible = ((window->flags & SDL_WINDOW_HIDDEN) == 0);
icculus@6422
   745
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@6422
   746
    SDL_DisplayData *displaydata =
icculus@6422
   747
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
icculus@6422
   748
    Display *display = data->videodata->display;
icculus@6422
   749
    XEvent event;
icculus@6422
   750
icculus@6422
   751
    SetWindowBordered(display, displaydata->screen, data->xwindow, bordered);
icculus@6423
   752
    XFlush(display);
icculus@6422
   753
    XIfEvent(display, &event, &isConfigureNotify, (XPointer)&data->xwindow);
icculus@6422
   754
icculus@6423
   755
    if (visible) {
icculus@6423
   756
        XWindowAttributes attr;
icculus@6423
   757
        do {
icculus@6423
   758
            XSync(display, False);
icculus@6423
   759
            XGetWindowAttributes(display, data->xwindow, &attr);
icculus@6423
   760
        } while (attr.map_state != IsViewable);
icculus@6423
   761
icculus@6423
   762
        if (focused) {
icculus@6423
   763
            XSetInputFocus(display, data->xwindow, RevertToParent, CurrentTime);
icculus@6423
   764
        }
icculus@6423
   765
    }
icculus@6423
   766
icculus@6422
   767
    /* make sure these don't make it to the real event queue if they fired here. */
icculus@6423
   768
    XSync(display, False);
icculus@6423
   769
    XCheckIfEvent(display, &event, &isUnmapNotify, (XPointer)&data->xwindow);
icculus@6423
   770
    XCheckIfEvent(display, &event, &isMapNotify, (XPointer)&data->xwindow);
icculus@6422
   771
}
slouken@6336
   772
slouken@1951
   773
void
slouken@1951
   774
X11_ShowWindow(_THIS, SDL_Window * window)
slouken@1951
   775
{
slouken@1951
   776
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   777
    Display *display = data->videodata->display;
slouken@6336
   778
    XEvent event;
slouken@1951
   779
slouken@3685
   780
    XMapRaised(display, data->xwindow);
slouken@6336
   781
    /* Blocking wait for "MapNotify" event.
slouken@6336
   782
     * We use XIfEvent because XWindowEvent takes a mask rather than a type, 
slouken@6336
   783
     * and XCheckTypedWindowEvent doesn't block */
slouken@6336
   784
    XIfEvent(display, &event, &isMapNotify, (XPointer)&data->xwindow);
slouken@5031
   785
    XFlush(display);
slouken@1951
   786
}
slouken@1951
   787
slouken@1951
   788
void
slouken@1951
   789
X11_HideWindow(_THIS, SDL_Window * window)
slouken@1951
   790
{
slouken@1951
   791
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   792
    Display *display = data->videodata->display;
slouken@6336
   793
    XEvent event;
slouken@1951
   794
slouken@3685
   795
    XUnmapWindow(display, data->xwindow);
slouken@6336
   796
    /* Blocking wait for "UnmapNotify" event */
slouken@6336
   797
    XIfEvent(display, &event, &isUnmapNotify, (XPointer)&data->xwindow);    
slouken@5031
   798
    XFlush(display);
slouken@1951
   799
}
slouken@1951
   800
slouken@1951
   801
void
slouken@1951
   802
X11_RaiseWindow(_THIS, SDL_Window * window)
slouken@1951
   803
{
slouken@1951
   804
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   805
    Display *display = data->videodata->display;
slouken@1951
   806
slouken@3685
   807
    XRaiseWindow(display, data->xwindow);
slouken@5031
   808
    XFlush(display);
slouken@1951
   809
}
slouken@1951
   810
slouken@3077
   811
static void
slouken@5302
   812
SetWindowMaximized(_THIS, SDL_Window * window, SDL_bool maximized)
slouken@3077
   813
{
slouken@3077
   814
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@3077
   815
    SDL_DisplayData *displaydata =
slouken@5246
   816
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@3077
   817
    Display *display = data->videodata->display;
slouken@4518
   818
    Atom _NET_WM_STATE = data->videodata->_NET_WM_STATE;
slouken@4518
   819
    Atom _NET_WM_STATE_MAXIMIZED_VERT = data->videodata->_NET_WM_STATE_MAXIMIZED_VERT;
slouken@4518
   820
    Atom _NET_WM_STATE_MAXIMIZED_HORZ = data->videodata->_NET_WM_STATE_MAXIMIZED_HORZ;
slouken@4522
   821
    Atom _NET_WM_STATE_FULLSCREEN = data->videodata->_NET_WM_STATE_FULLSCREEN;
slouken@3077
   822
slouken@4522
   823
    if (X11_IsWindowMapped(_this, window)) {
slouken@4522
   824
        XEvent e;
slouken@3077
   825
slouken@4524
   826
        SDL_zero(e);
slouken@4522
   827
        e.xany.type = ClientMessage;
slouken@4522
   828
        e.xclient.message_type = _NET_WM_STATE;
slouken@4522
   829
        e.xclient.format = 32;
slouken@4522
   830
        e.xclient.window = data->xwindow;
slouken@4522
   831
        e.xclient.data.l[0] =
slouken@4522
   832
            maximized ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
slouken@4522
   833
        e.xclient.data.l[1] = _NET_WM_STATE_MAXIMIZED_VERT;
slouken@4522
   834
        e.xclient.data.l[2] = _NET_WM_STATE_MAXIMIZED_HORZ;
slouken@4522
   835
        e.xclient.data.l[3] = 0l;
slouken@4522
   836
slouken@4522
   837
        XSendEvent(display, RootWindow(display, displaydata->screen), 0,
slouken@4522
   838
                   SubstructureNotifyMask | SubstructureRedirectMask, &e);
slouken@4522
   839
    } else {
slouken@4522
   840
        int count = 0;
slouken@4522
   841
        Atom atoms[3];
slouken@4522
   842
slouken@4522
   843
        if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@4522
   844
            atoms[count++] = _NET_WM_STATE_FULLSCREEN;
slouken@4522
   845
        }
slouken@4522
   846
        if (maximized) {
slouken@4522
   847
            atoms[count++] = _NET_WM_STATE_MAXIMIZED_VERT;
slouken@4522
   848
            atoms[count++] = _NET_WM_STATE_MAXIMIZED_HORZ;
slouken@4522
   849
        }
slouken@4522
   850
        if (count > 0) {
slouken@4522
   851
            XChangeProperty(display, data->xwindow, _NET_WM_STATE, XA_ATOM, 32,
slouken@4522
   852
                            PropModeReplace, (unsigned char *)atoms, count);
slouken@4522
   853
        } else {
slouken@4522
   854
            XDeleteProperty(display, data->xwindow, _NET_WM_STATE);
slouken@4522
   855
        }
slouken@4522
   856
    }
slouken@5031
   857
    XFlush(display);
slouken@3077
   858
}
slouken@3077
   859
slouken@1951
   860
void
slouken@1951
   861
X11_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1951
   862
{
slouken@5302
   863
    SetWindowMaximized(_this, window, SDL_TRUE);
slouken@1951
   864
}
slouken@1951
   865
slouken@1951
   866
void
slouken@1951
   867
X11_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1951
   868
{
slouken@4518
   869
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@4518
   870
    SDL_DisplayData *displaydata =
slouken@5246
   871
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@4518
   872
    Display *display = data->videodata->display;
slouken@4518
   873
 
slouken@4518
   874
    XIconifyWindow(display, data->xwindow, displaydata->screen);
slouken@5031
   875
    XFlush(display);
slouken@1951
   876
}
slouken@1951
   877
slouken@1951
   878
void
slouken@1951
   879
X11_RestoreWindow(_THIS, SDL_Window * window)
slouken@1951
   880
{
slouken@5302
   881
    SetWindowMaximized(_this, window, SDL_FALSE);
slouken@1951
   882
    X11_ShowWindow(_this, window);
slouken@1951
   883
}
slouken@1951
   884
icculus@6466
   885
static Bool
icculus@6466
   886
isActionAllowed(SDL_WindowData *data, Atom action)
icculus@6466
   887
{
icculus@6466
   888
    Atom _NET_WM_ALLOWED_ACTIONS = data->videodata->_NET_WM_ALLOWED_ACTIONS;
icculus@6466
   889
    Atom type;
icculus@6466
   890
    Display *display = data->videodata->display;
icculus@6466
   891
    int form;
icculus@6466
   892
    unsigned long remain;
icculus@6466
   893
    unsigned long len, i;
icculus@6466
   894
    Atom *list;
icculus@6466
   895
    Bool ret = False;
icculus@6466
   896
    if (XGetWindowProperty(display, data->xwindow, _NET_WM_ALLOWED_ACTIONS, 0, 1024, False, XA_ATOM, &type, &form, &len, &remain, (unsigned char **)&list) == Success)
icculus@6466
   897
    {
icculus@6466
   898
        for (i=0; i<len; ++i)
icculus@6466
   899
        {
icculus@6466
   900
            if (list[i] == action) {
icculus@6466
   901
                ret = True;
icculus@6466
   902
                break;
icculus@6466
   903
            }
icculus@6466
   904
        }
icculus@6466
   905
        XFree(list);
icculus@6466
   906
    }
icculus@6466
   907
    return ret;
icculus@6466
   908
}
icculus@6466
   909
slouken@5305
   910
void
slouken@5305
   911
X11_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * _display, SDL_bool fullscreen)
slouken@5302
   912
{
slouken@5302
   913
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5305
   914
    SDL_DisplayData *displaydata = (SDL_DisplayData *) _display->driverdata;
slouken@5302
   915
    Display *display = data->videodata->display;
slouken@5302
   916
    Atom _NET_WM_STATE = data->videodata->_NET_WM_STATE;
slouken@5302
   917
    Atom _NET_WM_STATE_MAXIMIZED_VERT = data->videodata->_NET_WM_STATE_MAXIMIZED_VERT;
slouken@5302
   918
    Atom _NET_WM_STATE_MAXIMIZED_HORZ = data->videodata->_NET_WM_STATE_MAXIMIZED_HORZ;
slouken@5302
   919
    Atom _NET_WM_STATE_FULLSCREEN = data->videodata->_NET_WM_STATE_FULLSCREEN;
slouken@5302
   920
slouken@5302
   921
    if (X11_IsWindowMapped(_this, window)) {
slouken@5302
   922
        XEvent e;
slouken@5302
   923
icculus@6466
   924
        if (isActionAllowed(data, data->videodata->_NET_WM_ACTION_FULLSCREEN) == False)
icculus@6466
   925
        {
icculus@6466
   926
            /* We aren't allowed to go into fullscreen mode... */
icculus@6466
   927
            if ((window->flags & SDL_WINDOW_RESIZABLE) == 0) {
icculus@6466
   928
                /* ...and we aren't resizable. Compiz refuses fullscreen toggle in this case. */
icculus@6466
   929
                XSizeHints *sizehints = XAllocSizeHints();
icculus@6466
   930
                long flags = 0;
icculus@6466
   931
                XGetWMNormalHints(display, data->xwindow, sizehints, &flags);
icculus@6466
   932
                /* set the resize flags on */
icculus@6466
   933
                sizehints->flags |= PMinSize | PMaxSize;
icculus@6466
   934
                if (fullscreen) {
icculus@6466
   935
                    /* we are going fullscreen so turn the flags off */
icculus@6466
   936
                    sizehints->flags ^= (PMinSize | PMaxSize);
icculus@6466
   937
                } else {
icculus@6466
   938
                    /* Reset the min/max width height to make the window non-resizable again */
icculus@6466
   939
                    sizehints->min_width = sizehints->max_width = window->w;
icculus@6466
   940
                    sizehints->min_height = sizehints->max_height = window->h;
icculus@6466
   941
                }
icculus@6466
   942
                XSetWMNormalHints(display, data->xwindow, sizehints);
icculus@6466
   943
                XFree(sizehints);
icculus@6466
   944
            }
icculus@6466
   945
        }
icculus@6466
   946
        
slouken@5302
   947
        SDL_zero(e);
slouken@5302
   948
        e.xany.type = ClientMessage;
slouken@5302
   949
        e.xclient.message_type = _NET_WM_STATE;
slouken@5302
   950
        e.xclient.format = 32;
slouken@5302
   951
        e.xclient.window = data->xwindow;
slouken@5302
   952
        e.xclient.data.l[0] =
slouken@5302
   953
            fullscreen ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
slouken@5302
   954
        e.xclient.data.l[1] = _NET_WM_STATE_FULLSCREEN;
slouken@5302
   955
        e.xclient.data.l[3] = 0l;
slouken@5302
   956
slouken@5302
   957
        XSendEvent(display, RootWindow(display, displaydata->screen), 0,
slouken@5302
   958
                   SubstructureNotifyMask | SubstructureRedirectMask, &e);
slouken@5302
   959
    } else {
slouken@5302
   960
        int count = 0;
slouken@5302
   961
        Atom atoms[3];
slouken@5302
   962
slouken@5302
   963
        if (fullscreen) {
slouken@5302
   964
            atoms[count++] = _NET_WM_STATE_FULLSCREEN;
slouken@5302
   965
        }
slouken@5302
   966
        if (window->flags & SDL_WINDOW_MAXIMIZED) {
slouken@5302
   967
            atoms[count++] = _NET_WM_STATE_MAXIMIZED_VERT;
slouken@5302
   968
            atoms[count++] = _NET_WM_STATE_MAXIMIZED_HORZ;
slouken@5302
   969
        }
slouken@5302
   970
        if (count > 0) {
slouken@5302
   971
            XChangeProperty(display, data->xwindow, _NET_WM_STATE, XA_ATOM, 32,
slouken@5302
   972
                            PropModeReplace, (unsigned char *)atoms, count);
slouken@5302
   973
        } else {
slouken@5302
   974
            XDeleteProperty(display, data->xwindow, _NET_WM_STATE);
slouken@5302
   975
        }
slouken@5302
   976
    }
slouken@5302
   977
    XFlush(display);
slouken@5302
   978
}
slouken@5302
   979
slouken@5466
   980
int
slouken@5466
   981
X11_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
slouken@5466
   982
{
slouken@5466
   983
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5466
   984
    Display *display = data->videodata->display;
slouken@5466
   985
    Visual *visual = data->visual;
slouken@5466
   986
    Colormap colormap = data->colormap;
slouken@5466
   987
    XColor *colorcells;
slouken@5466
   988
    int ncolors;
slouken@5466
   989
    int rmask, gmask, bmask;
slouken@5466
   990
    int rshift, gshift, bshift;
icculus@5981
   991
    int i;
slouken@5466
   992
slouken@5466
   993
    if (visual->class != DirectColor) {
slouken@5466
   994
        SDL_SetError("Window doesn't have DirectColor visual");
slouken@5466
   995
        return -1;
slouken@5466
   996
    }
slouken@5466
   997
slouken@5466
   998
    ncolors = visual->map_entries;
slouken@5466
   999
    colorcells = SDL_malloc(ncolors * sizeof(XColor));
slouken@5466
  1000
    if (!colorcells) {
slouken@5466
  1001
        SDL_OutOfMemory();
slouken@5466
  1002
        return -1;
slouken@5466
  1003
    }
slouken@5466
  1004
slouken@5466
  1005
    rshift = 0;
slouken@5466
  1006
    rmask = visual->red_mask;
slouken@5466
  1007
    while (0 == (rmask & 1)) {
slouken@5466
  1008
        rshift++;
slouken@5466
  1009
        rmask >>= 1;
slouken@5466
  1010
    }
slouken@5466
  1011
slouken@5466
  1012
    gshift = 0;
slouken@5466
  1013
    gmask = visual->green_mask;
slouken@5466
  1014
    while (0 == (gmask & 1)) {
slouken@5466
  1015
        gshift++;
slouken@5466
  1016
        gmask >>= 1;
slouken@5466
  1017
    }
slouken@5466
  1018
slouken@5466
  1019
    bshift = 0;
slouken@5466
  1020
    bmask = visual->blue_mask;
slouken@5466
  1021
    while (0 == (bmask & 1)) {
slouken@5466
  1022
        bshift++;
slouken@5466
  1023
        bmask >>= 1;
slouken@5466
  1024
    }
slouken@5466
  1025
slouken@5466
  1026
    /* build the color table pixel values */
slouken@5466
  1027
    for (i = 0; i < ncolors; i++) {
slouken@5466
  1028
        Uint32 rbits = (rmask * i) / (ncolors - 1);
slouken@5466
  1029
        Uint32 gbits = (gmask * i) / (ncolors - 1);
slouken@5466
  1030
        Uint32 bbits = (bmask * i) / (ncolors - 1);
slouken@5466
  1031
        Uint32 pix = (rbits << rshift) | (gbits << gshift) | (bbits << bshift);
slouken@5466
  1032
slouken@5466
  1033
        colorcells[i].pixel = pix;
slouken@5466
  1034
slouken@5466
  1035
        colorcells[i].red = ramp[(0 * 256) + i];
slouken@5466
  1036
        colorcells[i].green = ramp[(1 * 256) + i];
slouken@5466
  1037
        colorcells[i].blue = ramp[(2 * 256) + i];
slouken@5466
  1038
slouken@5466
  1039
        colorcells[i].flags = DoRed | DoGreen | DoBlue;
slouken@5466
  1040
    }
slouken@5466
  1041
slouken@5466
  1042
    XStoreColors(display, colormap, colorcells, ncolors);
slouken@5466
  1043
    XFlush(display);
slouken@5466
  1044
    SDL_free(colorcells);
slouken@5466
  1045
slouken@5466
  1046
    return 0;
slouken@5466
  1047
}
slouken@5466
  1048
slouken@5302
  1049
void
slouken@1951
  1050
X11_SetWindowGrab(_THIS, SDL_Window * window)
slouken@1951
  1051
{
slouken@2875
  1052
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@2875
  1053
    Display *display = data->videodata->display;
slouken@4518
  1054
    SDL_bool oldstyle_fullscreen;
slouken@2875
  1055
slouken@4518
  1056
    /* ICCCM2.0-compliant window managers can handle fullscreen windows */
slouken@4522
  1057
    oldstyle_fullscreen = X11_IsWindowOldFullscreen(_this, window);
slouken@4518
  1058
slouken@4518
  1059
    if (((window->flags & SDL_WINDOW_INPUT_GRABBED) || oldstyle_fullscreen)
slouken@2876
  1060
        && (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@2875
  1061
        /* Try to grab the mouse */
slouken@2876
  1062
        for (;;) {
slouken@2876
  1063
            int result =
slouken@3685
  1064
                XGrabPointer(display, data->xwindow, True, 0, GrabModeAsync,
slouken@3685
  1065
                             GrabModeAsync, data->xwindow, None, CurrentTime);
slouken@2876
  1066
            if (result == GrabSuccess) {
slouken@2875
  1067
                break;
slouken@2875
  1068
            }
slouken@2875
  1069
            SDL_Delay(100);
slouken@2875
  1070
        }
slouken@2875
  1071
slouken@2875
  1072
        /* Raise the window if we grab the mouse */
slouken@3685
  1073
        XRaiseWindow(display, data->xwindow);
slouken@2875
  1074
slouken@2875
  1075
        /* Now grab the keyboard */
slouken@3685
  1076
        XGrabKeyboard(display, data->xwindow, True, GrabModeAsync,
slouken@2876
  1077
                      GrabModeAsync, CurrentTime);
slouken@2875
  1078
    } else {
slouken@2875
  1079
        XUngrabPointer(display, CurrentTime);
slouken@2875
  1080
        XUngrabKeyboard(display, CurrentTime);
slouken@2875
  1081
    }
slouken@1951
  1082
}
slouken@1951
  1083
slouken@1951
  1084
void
slouken@1951
  1085
X11_DestroyWindow(_THIS, SDL_Window * window)
slouken@1951
  1086
{
slouken@1951
  1087
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
bob@2323
  1088
    window->driverdata = NULL;
slouken@1951
  1089
slouken@1951
  1090
    if (data) {
bob@2323
  1091
        SDL_VideoData *videodata = (SDL_VideoData *) data->videodata;
bob@2323
  1092
        Display *display = videodata->display;
bob@2323
  1093
        int numwindows = videodata->numwindows;
bob@2323
  1094
        SDL_WindowData **windowlist = videodata->windowlist;
bob@2323
  1095
        int i;
bob@2323
  1096
bob@2323
  1097
        if (windowlist) {
bob@2324
  1098
            for (i = 0; i < numwindows; ++i) {
slouken@3685
  1099
                if (windowlist[i] && (windowlist[i]->window == window)) {
bob@2324
  1100
                    windowlist[i] = windowlist[numwindows - 1];
bob@2324
  1101
                    windowlist[numwindows - 1] = NULL;
bob@2324
  1102
                    videodata->numwindows--;
bob@2324
  1103
                    break;
bob@2324
  1104
                }
bob@2323
  1105
            }
bob@2323
  1106
        }
slouken@1951
  1107
#ifdef X_HAVE_UTF8_STRING
slouken@1951
  1108
        if (data->ic) {
slouken@1951
  1109
            XDestroyIC(data->ic);
slouken@1951
  1110
        }
slouken@1951
  1111
#endif
slouken@1951
  1112
        if (data->created) {
slouken@3685
  1113
            XDestroyWindow(display, data->xwindow);
slouken@5031
  1114
            XFlush(display);
slouken@1951
  1115
        }
slouken@1951
  1116
        SDL_free(data);
slouken@1951
  1117
    }
slouken@1951
  1118
}
slouken@1951
  1119
slouken@1951
  1120
SDL_bool
slouken@1951
  1121
X11_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1951
  1122
{
slouken@4510
  1123
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@4510
  1124
    Display *display = data->videodata->display;
slouken@4510
  1125
slouken@4510
  1126
    if (info->version.major == SDL_MAJOR_VERSION &&
slouken@4510
  1127
        info->version.minor == SDL_MINOR_VERSION) {
slouken@4510
  1128
        info->subsystem = SDL_SYSWM_X11;
slouken@5056
  1129
        info->info.x11.display = display;
slouken@5056
  1130
        info->info.x11.window = data->xwindow;
slouken@1951
  1131
        return SDL_TRUE;
slouken@1951
  1132
    } else {
slouken@1951
  1133
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1951
  1134
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1951
  1135
        return SDL_FALSE;
slouken@1951
  1136
    }
slouken@1951
  1137
}
slouken@1951
  1138
slouken@5481
  1139
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
  1140
slouken@1951
  1141
/* vi: set ts=4 sw=4 expandtab: */