src/video/x11/SDL_x11window.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Mar 2011 08:49:20 -0800
changeset 5466 006883d5fa51
parent 5386 762e67f81fc7
child 5478 6b65ff3cee62
permissions -rw-r--r--
Gamma support is back!

New API functions:
SDL_SetWindowBrightness()
SDL_GetWindowBrightness()
SDL_SetWindowGammaRamp()
SDL_GetWindowGammaRamp()
SDL_CalculateGammaRamp()
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@5466
   178
        data->colormap = attrib.colormap;
slouken@1951
   179
    }
slouken@3077
   180
slouken@3077
   181
    {
slouken@4518
   182
        Atom _NET_WM_STATE = data->videodata->_NET_WM_STATE;
slouken@4518
   183
        Atom _NET_WM_STATE_MAXIMIZED_VERT = data->videodata->_NET_WM_STATE_MAXIMIZED_VERT;
slouken@4518
   184
        Atom _NET_WM_STATE_MAXIMIZED_HORZ = data->videodata->_NET_WM_STATE_MAXIMIZED_HORZ;
slouken@4518
   185
        Atom _NET_WM_STATE_FULLSCREEN = data->videodata->_NET_WM_STATE_FULLSCREEN;
slouken@3077
   186
        Atom actualType;
slouken@3077
   187
        int actualFormat;
slouken@3077
   188
        unsigned long i, numItems, bytesAfter;
slouken@3077
   189
        unsigned char *propertyValue = NULL;
slouken@3077
   190
        long maxLength = 1024;
slouken@3077
   191
slouken@3077
   192
        if (XGetWindowProperty(data->videodata->display, w, _NET_WM_STATE,
slouken@3078
   193
                               0l, maxLength, False, XA_ATOM, &actualType,
slouken@3078
   194
                               &actualFormat, &numItems, &bytesAfter,
slouken@3078
   195
                               &propertyValue) == Success) {
slouken@3078
   196
            Atom *atoms = (Atom *) propertyValue;
slouken@3077
   197
            int maximized = 0;
slouken@4518
   198
            int fullscreen = 0;
slouken@3077
   199
slouken@3077
   200
            for (i = 0; i < numItems; ++i) {
slouken@3077
   201
                if (atoms[i] == _NET_WM_STATE_MAXIMIZED_VERT) {
slouken@3077
   202
                    maximized |= 1;
slouken@3077
   203
                } else if (atoms[i] == _NET_WM_STATE_MAXIMIZED_HORZ) {
slouken@3077
   204
                    maximized |= 2;
slouken@4518
   205
                } else if ( atoms[i] == _NET_WM_STATE_FULLSCREEN) {
slouken@4518
   206
                    fullscreen = 1;
slouken@3077
   207
                }
slouken@3077
   208
            }
slouken@3077
   209
            if (maximized == 3) {
slouken@3077
   210
                window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@4518
   211
            }  else if (fullscreen == 1) {
slouken@4518
   212
                window->flags |= SDL_WINDOW_FULLSCREEN;
slouken@3077
   213
            }
slouken@3077
   214
            XFree(propertyValue);
slouken@3077
   215
        }
slouken@3077
   216
    }
slouken@3077
   217
slouken@1951
   218
    /* FIXME: How can I tell?
slouken@1951
   219
       {
slouken@1951
   220
       DWORD style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1951
   221
       if (style & WS_VISIBLE) {
slouken@1951
   222
       if (style & (WS_BORDER | WS_THICKFRAME)) {
slouken@1951
   223
       window->flags &= ~SDL_WINDOW_BORDERLESS;
slouken@1951
   224
       } else {
slouken@1951
   225
       window->flags |= SDL_WINDOW_BORDERLESS;
slouken@1951
   226
       }
slouken@1951
   227
       if (style & WS_THICKFRAME) {
slouken@1951
   228
       window->flags |= SDL_WINDOW_RESIZABLE;
slouken@1951
   229
       } else {
slouken@1951
   230
       window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@1951
   231
       }
slouken@1951
   232
       if (style & WS_MINIMIZE) {
slouken@1951
   233
       window->flags |= SDL_WINDOW_MINIMIZED;
slouken@1951
   234
       } else {
slouken@1951
   235
       window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@1951
   236
       }
slouken@1951
   237
       }
slouken@1951
   238
       if (GetFocus() == hwnd) {
slouken@1951
   239
       int index = data->videodata->keyboard;
slouken@1951
   240
       window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@3685
   241
       SDL_SetKeyboardFocus(index, data->window);
slouken@1951
   242
slouken@1951
   243
       if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1951
   244
       RECT rect;
slouken@1951
   245
       GetClientRect(hwnd, &rect);
slouken@1951
   246
       ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1951
   247
       ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1951
   248
       ClipCursor(&rect);
slouken@1951
   249
       }
slouken@1951
   250
       }
slouken@1951
   251
     */
slouken@1951
   252
slouken@1951
   253
    /* All done! */
slouken@1951
   254
    window->driverdata = data;
slouken@1951
   255
    return 0;
slouken@1951
   256
}
slouken@1951
   257
slouken@1951
   258
int
slouken@1951
   259
X11_CreateWindow(_THIS, SDL_Window * window)
slouken@1951
   260
{
slouken@1951
   261
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   262
    SDL_DisplayData *displaydata =
slouken@5246
   263
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@4521
   264
    Display *display = data->display;
slouken@4521
   265
    int screen = displaydata->screen;
slouken@1951
   266
    Visual *visual;
slouken@1951
   267
    int depth;
slouken@1951
   268
    XSetWindowAttributes xattr;
slouken@1951
   269
    Window w;
slouken@1951
   270
    XSizeHints *sizehints;
slouken@1951
   271
    XWMHints *wmhints;
slouken@1951
   272
    XClassHint *classhints;
slouken@4521
   273
    Atom _NET_WM_WINDOW_TYPE;
slouken@4521
   274
    Atom _NET_WM_WINDOW_TYPE_NORMAL;
slouken@4522
   275
    int wmstate_count;
slouken@4522
   276
    Atom wmstate_atoms[3];
slouken@4518
   277
slouken@1951
   278
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@1951
   279
/* FIXME
slouken@1951
   280
    if ( use_xinerama ) {
slouken@1951
   281
        x = xinerama_info.x_org;
slouken@1951
   282
        y = xinerama_info.y_org;
slouken@1951
   283
    }
slouken@1951
   284
*/
slouken@1951
   285
#endif
slouken@5088
   286
#if SDL_VIDEO_OPENGL_GLX
slouken@1951
   287
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@1952
   288
        XVisualInfo *vinfo;
slouken@1952
   289
slouken@4521
   290
        vinfo = X11_GL_GetVisual(_this, display, screen);
slouken@1952
   291
        if (!vinfo) {
slouken@1952
   292
            return -1;
slouken@1952
   293
        }
slouken@1952
   294
        visual = vinfo->visual;
slouken@1952
   295
        depth = vinfo->depth;
slouken@1952
   296
        XFree(vinfo);
slouken@1952
   297
    } else
slouken@1952
   298
#endif
slouken@3161
   299
#ifdef SDL_VIDEO_DRIVER_PANDORA
slouken@3161
   300
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@3161
   301
        XVisualInfo *vinfo;
slouken@3161
   302
slouken@4521
   303
        vinfo = X11_GLES_GetVisual(_this, display, screen);
slouken@3161
   304
        if (!vinfo) {
slouken@3161
   305
            return -1;
slouken@3161
   306
        }
slouken@3161
   307
        visual = vinfo->visual;
slouken@3161
   308
        depth = vinfo->depth;
slouken@3161
   309
        XFree(vinfo);
slouken@3161
   310
    } else
slouken@3161
   311
#endif
slouken@1952
   312
    {
slouken@1951
   313
        visual = displaydata->visual;
slouken@1951
   314
        depth = displaydata->depth;
slouken@1951
   315
    }
slouken@1951
   316
slouken@5386
   317
    xattr.override_redirect = False;
slouken@1951
   318
    xattr.background_pixel = 0;
slouken@1951
   319
    xattr.border_pixel = 0;
slouken@5466
   320
slouken@5466
   321
    if (visual->class == DirectColor) {
slouken@5466
   322
        Status status;
slouken@5466
   323
        XColor *colorcells;
slouken@5466
   324
        int i;
slouken@5466
   325
        int ncolors;
slouken@5466
   326
        int rmax, gmax, bmax;
slouken@5466
   327
        int rmask, gmask, bmask;
slouken@5466
   328
        int rshift, gshift, bshift;
slouken@5466
   329
slouken@5466
   330
        xattr.colormap =
slouken@5466
   331
            XCreateColormap(display, RootWindow(display, screen),
slouken@5466
   332
                            visual, AllocAll);
slouken@5466
   333
slouken@5466
   334
        /* If we can't create a colormap, then we must die */
slouken@5466
   335
        if (!xattr.colormap) {
slouken@5466
   336
            SDL_SetError("Could not create writable colormap");
slouken@5466
   337
            return -1;
slouken@5466
   338
        }
slouken@5466
   339
slouken@5466
   340
        /* OK, we got a colormap, now fill it in as best as we can */
slouken@5466
   341
        colorcells = SDL_malloc(visual->map_entries * sizeof(XColor));
slouken@5466
   342
        if (!colorcells) {
slouken@5466
   343
            SDL_OutOfMemory();
slouken@5466
   344
            return -1;
slouken@5466
   345
        }
slouken@5466
   346
        ncolors = visual->map_entries;
slouken@5466
   347
        rmax = 0xffff;
slouken@5466
   348
        gmax = 0xffff;
slouken@5466
   349
        bmax = 0xffff;
slouken@5466
   350
slouken@5466
   351
        rshift = 0;
slouken@5466
   352
        rmask = visual->red_mask;
slouken@5466
   353
        while (0 == (rmask & 1)) {
slouken@5466
   354
            rshift++;
slouken@5466
   355
            rmask >>= 1;
slouken@5466
   356
        }
slouken@5466
   357
slouken@5466
   358
        gshift = 0;
slouken@5466
   359
        gmask = visual->green_mask;
slouken@5466
   360
        while (0 == (gmask & 1)) {
slouken@5466
   361
            gshift++;
slouken@5466
   362
            gmask >>= 1;
slouken@5466
   363
        }
slouken@5466
   364
slouken@5466
   365
        bshift = 0;
slouken@5466
   366
        bmask = visual->blue_mask;
slouken@5466
   367
        while (0 == (bmask & 1)) {
slouken@5466
   368
            bshift++;
slouken@5466
   369
            bmask >>= 1;
slouken@5466
   370
        }
slouken@5466
   371
slouken@5466
   372
        /* build the color table pixel values */
slouken@5466
   373
        for (i = 0; i < ncolors; i++) {
slouken@5466
   374
            Uint32 red = (rmax * i) / (ncolors - 1);
slouken@5466
   375
            Uint32 green = (gmax * i) / (ncolors - 1);
slouken@5466
   376
            Uint32 blue = (bmax * i) / (ncolors - 1);
slouken@5466
   377
slouken@5466
   378
            Uint32 rbits = (rmask * i) / (ncolors - 1);
slouken@5466
   379
            Uint32 gbits = (gmask * i) / (ncolors - 1);
slouken@5466
   380
            Uint32 bbits = (bmask * i) / (ncolors - 1);
slouken@5466
   381
slouken@5466
   382
            Uint32 pix =
slouken@5466
   383
                (rbits << rshift) | (gbits << gshift) | (bbits << bshift);
slouken@5466
   384
slouken@5466
   385
            colorcells[i].pixel = pix;
slouken@5466
   386
slouken@5466
   387
            colorcells[i].red = red;
slouken@5466
   388
            colorcells[i].green = green;
slouken@5466
   389
            colorcells[i].blue = blue;
slouken@5466
   390
slouken@5466
   391
            colorcells[i].flags = DoRed | DoGreen | DoBlue;
slouken@5466
   392
        }
slouken@5466
   393
slouken@5466
   394
        XStoreColors(display, xattr.colormap, colorcells, ncolors);
slouken@5466
   395
slouken@5466
   396
        SDL_free(colorcells);
slouken@5466
   397
    } else {
slouken@5466
   398
        xattr.colormap =
slouken@5466
   399
            XCreateColormap(display, RootWindow(display, screen),
slouken@5466
   400
                            visual, AllocNone);
slouken@5466
   401
    }
slouken@1951
   402
slouken@5386
   403
    w = XCreateWindow(display, RootWindow(display, screen),
slouken@5386
   404
                      window->x, window->y, window->w, window->h,
slouken@5386
   405
                      0, depth, InputOutput, visual,
slouken@1951
   406
                      (CWOverrideRedirect | CWBackPixel | CWBorderPixel |
slouken@1951
   407
                       CWColormap), &xattr);
slouken@1952
   408
    if (!w) {
slouken@1952
   409
        SDL_SetError("Couldn't create window");
slouken@1952
   410
        return -1;
slouken@1952
   411
    }
slouken@3161
   412
#if SDL_VIDEO_DRIVER_PANDORA
slouken@3161
   413
    /* Create the GLES window surface */
slouken@3161
   414
    _this->gles_data->egl_surface =
slouken@3161
   415
        _this->gles_data->eglCreateWindowSurface(_this->gles_data->
slouken@3161
   416
                                                 egl_display,
slouken@3161
   417
                                                 _this->gles_data->egl_config,
slouken@3161
   418
                                                 (NativeWindowType) w, NULL);
slouken@3161
   419
slouken@3161
   420
    if (_this->gles_data->egl_surface == EGL_NO_SURFACE) {
slouken@3161
   421
        SDL_SetError("Could not create GLES window surface");
slouken@3161
   422
        return -1;
slouken@3161
   423
    }
slouken@3161
   424
#endif
slouken@1951
   425
slouken@1951
   426
    sizehints = XAllocSizeHints();
slouken@1951
   427
    if (sizehints) {
slouken@5386
   428
        if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
slouken@1951
   429
            sizehints->min_width = sizehints->max_width = window->w;
slouken@1951
   430
            sizehints->min_height = sizehints->max_height = window->h;
slouken@3225
   431
            sizehints->flags = PMaxSize | PMinSize;
slouken@1951
   432
        }
slouken@5386
   433
        sizehints->x = window->x;
slouken@5386
   434
        sizehints->y = window->y;
slouken@5386
   435
        sizehints->flags |= USPosition;
slouken@4521
   436
        XSetWMNormalHints(display, w, sizehints);
slouken@1951
   437
        XFree(sizehints);
slouken@1951
   438
    }
slouken@1951
   439
slouken@5386
   440
    if (window->flags & SDL_WINDOW_BORDERLESS) {
slouken@1951
   441
        SDL_bool set;
slouken@1951
   442
        Atom WM_HINTS;
slouken@1951
   443
slouken@1951
   444
        /* We haven't modified the window manager hints yet */
slouken@1951
   445
        set = SDL_FALSE;
slouken@1951
   446
slouken@1951
   447
        /* First try to set MWM hints */
slouken@4521
   448
        WM_HINTS = XInternAtom(display, "_MOTIF_WM_HINTS", True);
slouken@1951
   449
        if (WM_HINTS != None) {
slouken@1951
   450
            /* Hints used by Motif compliant window managers */
slouken@1951
   451
            struct
slouken@1951
   452
            {
slouken@1951
   453
                unsigned long flags;
slouken@1951
   454
                unsigned long functions;
slouken@1951
   455
                unsigned long decorations;
slouken@1951
   456
                long input_mode;
slouken@1951
   457
                unsigned long status;
slouken@1951
   458
            } MWMHints = {
slouken@1951
   459
            (1L << 1), 0, 0, 0, 0};
slouken@1951
   460
slouken@4521
   461
            XChangeProperty(display, w, WM_HINTS, WM_HINTS, 32,
slouken@1951
   462
                            PropModeReplace, (unsigned char *) &MWMHints,
slouken@4521
   463
                            sizeof(MWMHints) / 4);
slouken@1951
   464
            set = SDL_TRUE;
slouken@1951
   465
        }
slouken@1951
   466
        /* Now try to set KWM hints */
slouken@4521
   467
        WM_HINTS = XInternAtom(display, "KWM_WIN_DECORATION", True);
slouken@1951
   468
        if (WM_HINTS != None) {
slouken@1951
   469
            long KWMHints = 0;
slouken@1951
   470
slouken@4521
   471
            XChangeProperty(display, w, WM_HINTS, WM_HINTS, 32,
slouken@1951
   472
                            PropModeReplace,
slouken@1951
   473
                            (unsigned char *) &KWMHints,
slouken@4521
   474
                            sizeof(KWMHints) / 4);
slouken@1951
   475
            set = SDL_TRUE;
slouken@1951
   476
        }
slouken@1951
   477
        /* Now try to set GNOME hints */
slouken@4521
   478
        WM_HINTS = XInternAtom(display, "_WIN_HINTS", True);
slouken@1951
   479
        if (WM_HINTS != None) {
slouken@1951
   480
            long GNOMEHints = 0;
slouken@1951
   481
slouken@4521
   482
            XChangeProperty(display, w, WM_HINTS, WM_HINTS, 32,
slouken@1951
   483
                            PropModeReplace,
slouken@1951
   484
                            (unsigned char *) &GNOMEHints,
slouken@4521
   485
                            sizeof(GNOMEHints) / 4);
slouken@1951
   486
            set = SDL_TRUE;
slouken@1951
   487
        }
slouken@1951
   488
        /* Finally set the transient hints if necessary */
slouken@1951
   489
        if (!set) {
slouken@4521
   490
            XSetTransientForHint(display, w, RootWindow(display, screen));
slouken@1951
   491
        }
slouken@1951
   492
    } else {
slouken@1951
   493
        SDL_bool set;
slouken@1951
   494
        Atom WM_HINTS;
slouken@1951
   495
slouken@1951
   496
        /* We haven't modified the window manager hints yet */
slouken@1951
   497
        set = SDL_FALSE;
slouken@1951
   498
slouken@1951
   499
        /* First try to unset MWM hints */
slouken@4521
   500
        WM_HINTS = XInternAtom(display, "_MOTIF_WM_HINTS", True);
slouken@1951
   501
        if (WM_HINTS != None) {
slouken@4521
   502
            XDeleteProperty(display, w, WM_HINTS);
slouken@1951
   503
            set = SDL_TRUE;
slouken@1951
   504
        }
slouken@1951
   505
        /* Now try to unset KWM hints */
slouken@4521
   506
        WM_HINTS = XInternAtom(display, "KWM_WIN_DECORATION", True);
slouken@1951
   507
        if (WM_HINTS != None) {
slouken@4521
   508
            XDeleteProperty(display, w, WM_HINTS);
slouken@1951
   509
            set = SDL_TRUE;
slouken@1951
   510
        }
slouken@1951
   511
        /* Now try to unset GNOME hints */
slouken@4521
   512
        WM_HINTS = XInternAtom(display, "_WIN_HINTS", True);
slouken@1951
   513
        if (WM_HINTS != None) {
slouken@4521
   514
            XDeleteProperty(display, w, WM_HINTS);
slouken@1951
   515
            set = SDL_TRUE;
slouken@1951
   516
        }
slouken@1951
   517
        /* Finally unset the transient hints if necessary */
slouken@1951
   518
        if (!set) {
slouken@4555
   519
            XDeleteProperty(display, w, XA_WM_TRANSIENT_FOR);
slouken@1951
   520
        }
slouken@1951
   521
    }
slouken@1951
   522
slouken@1951
   523
    /* Set the input hints so we get keyboard input */
slouken@1951
   524
    wmhints = XAllocWMHints();
slouken@1951
   525
    if (wmhints) {
slouken@1951
   526
        wmhints->input = True;
slouken@1956
   527
        wmhints->flags = InputHint;
slouken@4521
   528
        XSetWMHints(display, w, wmhints);
slouken@1951
   529
        XFree(wmhints);
slouken@1951
   530
    }
slouken@1951
   531
slouken@1951
   532
    /* Set the class hints so we can get an icon (AfterStep) */
slouken@1951
   533
    classhints = XAllocClassHint();
slouken@1951
   534
    if (classhints != NULL) {
slouken@1951
   535
        classhints->res_name = data->classname;
slouken@1951
   536
        classhints->res_class = data->classname;
slouken@4521
   537
        XSetClassHint(display, w, classhints);
slouken@1951
   538
        XFree(classhints);
slouken@1951
   539
    }
slouken@1951
   540
slouken@4522
   541
    /* Set the window manager state */
slouken@4522
   542
    wmstate_count = X11_GetWMStateProperty(_this, window, wmstate_atoms);
slouken@4522
   543
    if (wmstate_count > 0) {
slouken@4522
   544
        XChangeProperty(display, w, data->_NET_WM_STATE, XA_ATOM, 32,
slouken@4522
   545
                        PropModeReplace,
slouken@4522
   546
                        (unsigned char *)wmstate_atoms, wmstate_count);
slouken@4522
   547
    } else {
slouken@4522
   548
        XDeleteProperty(display, w, data->_NET_WM_STATE);
slouken@4518
   549
    }
slouken@4518
   550
slouken@4521
   551
    /* Let the window manager know we're a "normal" window */
slouken@4521
   552
    _NET_WM_WINDOW_TYPE = XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
slouken@4521
   553
    _NET_WM_WINDOW_TYPE_NORMAL = XInternAtom(display, "_NET_WM_WINDOW_TYPE_NORMAL", False);
slouken@4521
   554
    XChangeProperty(display, w, _NET_WM_WINDOW_TYPE, XA_ATOM, 32,
slouken@4521
   555
                    PropModeReplace,
slouken@4521
   556
                    (unsigned char *)&_NET_WM_WINDOW_TYPE_NORMAL, 1);
slouken@4521
   557
slouken@1951
   558
    /* Allow the window to be deleted by the window manager */
slouken@4521
   559
    XSetWMProtocols(display, w, &data->WM_DELETE_WINDOW, 1);
slouken@1951
   560
slouken@1951
   561
    if (SetupWindowData(_this, window, w, SDL_TRUE) < 0) {
slouken@4521
   562
        XDestroyWindow(display, w);
slouken@1951
   563
        return -1;
slouken@1951
   564
    }
slouken@5386
   565
bob@2325
   566
#ifdef X_HAVE_UTF8_STRING
bob@2325
   567
    {
bob@2325
   568
        Uint32 fevent = 0;
bob@2325
   569
        pXGetICValues(((SDL_WindowData *) window->driverdata)->ic,
bob@2325
   570
                      XNFilterEvents, &fevent, NULL);
slouken@4521
   571
        XSelectInput(display, w,
bob@2325
   572
                     (FocusChangeMask | EnterWindowMask | LeaveWindowMask |
bob@2325
   573
                      ExposureMask | ButtonPressMask | ButtonReleaseMask |
bob@2325
   574
                      PointerMotionMask | KeyPressMask | KeyReleaseMask |
bob@2325
   575
                      PropertyChangeMask | StructureNotifyMask |
bob@2325
   576
                      KeymapStateMask | fevent));
bob@2325
   577
    }
bob@2325
   578
#else
kazeuser@2718
   579
    {
slouken@4521
   580
        XSelectInput(display, w,
slouken@2725
   581
                     (FocusChangeMask | EnterWindowMask | LeaveWindowMask |
slouken@2725
   582
                      ExposureMask | ButtonPressMask | ButtonReleaseMask |
slouken@2725
   583
                      PointerMotionMask | KeyPressMask | KeyReleaseMask |
slouken@2725
   584
                      PropertyChangeMask | StructureNotifyMask |
slouken@2725
   585
                      KeymapStateMask));
kazeuser@2718
   586
    }
bob@2325
   587
#endif
bob@2325
   588
slouken@5031
   589
    XFlush(display);
slouken@5031
   590
slouken@1951
   591
    return 0;
slouken@1951
   592
}
slouken@1951
   593
slouken@1951
   594
int
bob@2324
   595
X11_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1951
   596
{
slouken@1951
   597
    Window w = (Window) data;
slouken@1951
   598
slouken@4518
   599
    window->title = X11_GetWindowTitle(_this, w);
slouken@1951
   600
slouken@1951
   601
    if (SetupWindowData(_this, window, w, SDL_FALSE) < 0) {
slouken@1951
   602
        return -1;
slouken@1951
   603
    }
slouken@1951
   604
    return 0;
slouken@1951
   605
}
slouken@1951
   606
slouken@4518
   607
char *
slouken@4518
   608
X11_GetWindowTitle(_THIS, Window xwindow)
slouken@4518
   609
{
slouken@4518
   610
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@4518
   611
    Display *display = data->display;
slouken@4518
   612
    int status, real_format;
slouken@4518
   613
    Atom real_type;
slouken@4518
   614
    unsigned long items_read, items_left;
slouken@4518
   615
    unsigned char *propdata;
slouken@4518
   616
    char *title = NULL;
slouken@4518
   617
slouken@4518
   618
    status = XGetWindowProperty(display, xwindow, data->_NET_WM_NAME,
slouken@4518
   619
                0L, 8192L, False, data->UTF8_STRING, &real_type, &real_format,
slouken@4518
   620
                &items_read, &items_left, &propdata);
slouken@4518
   621
    if (status == Success) {
slouken@4518
   622
        title = SDL_strdup(SDL_static_cast(char*, propdata));
slouken@4518
   623
        XFree(propdata);
slouken@4518
   624
    } else {
slouken@4518
   625
        status = XGetWindowProperty(display, xwindow, XA_WM_NAME,
slouken@4518
   626
                    0L, 8192L, False, XA_STRING, &real_type, &real_format,
slouken@4518
   627
                    &items_read, &items_left, &propdata);
slouken@4518
   628
        if (status == Success) {
slouken@4518
   629
            title = SDL_iconv_string("UTF-8", "", SDL_static_cast(char*, propdata), items_read+1);
slouken@4518
   630
        } else {
slouken@4518
   631
            title = SDL_strdup("");
slouken@4518
   632
        }
slouken@4518
   633
    }
slouken@4518
   634
    return title;
slouken@4518
   635
}
slouken@4518
   636
slouken@1951
   637
void
slouken@1951
   638
X11_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1951
   639
{
slouken@1951
   640
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   641
    Display *display = data->videodata->display;
slouken@1951
   642
    XTextProperty titleprop, iconprop;
slouken@1951
   643
    Status status;
slouken@1951
   644
    const char *title = window->title;
slouken@1951
   645
    const char *icon = NULL;
slouken@1951
   646
slouken@1951
   647
#ifdef X_HAVE_UTF8_STRING
slouken@4518
   648
    Atom _NET_WM_NAME = data->videodata->_NET_WM_NAME;
slouken@4518
   649
    Atom _NET_WM_ICON_NAME = data->videodata->_NET_WM_ICON_NAME;
slouken@1951
   650
#endif
slouken@1951
   651
slouken@1951
   652
    if (title != NULL) {
slouken@2143
   653
        char *title_locale = SDL_iconv_utf8_locale(title);
slouken@2143
   654
        if (!title_locale) {
slouken@1951
   655
            SDL_OutOfMemory();
slouken@1951
   656
            return;
slouken@1951
   657
        }
slouken@2143
   658
        status = XStringListToTextProperty(&title_locale, 1, &titleprop);
slouken@2143
   659
        SDL_free(title_locale);
slouken@1951
   660
        if (status) {
slouken@3685
   661
            XSetTextProperty(display, data->xwindow, &titleprop, XA_WM_NAME);
slouken@1951
   662
            XFree(titleprop.value);
slouken@1951
   663
        }
slouken@1951
   664
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   665
        if (SDL_X11_HAVE_UTF8) {
slouken@1951
   666
            status =
slouken@1951
   667
                Xutf8TextListToTextProperty(display, (char **) &title, 1,
slouken@1951
   668
                                            XUTF8StringStyle, &titleprop);
slouken@1951
   669
            if (status == Success) {
slouken@3685
   670
                XSetTextProperty(display, data->xwindow, &titleprop,
slouken@1951
   671
                                 _NET_WM_NAME);
slouken@1951
   672
                XFree(titleprop.value);
slouken@1951
   673
            }
slouken@1951
   674
        }
slouken@1951
   675
#endif
slouken@1951
   676
    }
slouken@1951
   677
    if (icon != NULL) {
slouken@2143
   678
        char *icon_locale = SDL_iconv_utf8_locale(icon);
slouken@2143
   679
        if (!icon_locale) {
slouken@1951
   680
            SDL_OutOfMemory();
slouken@1951
   681
            return;
slouken@1951
   682
        }
slouken@2143
   683
        status = XStringListToTextProperty(&icon_locale, 1, &iconprop);
slouken@2143
   684
        SDL_free(icon_locale);
slouken@1951
   685
        if (status) {
slouken@3685
   686
            XSetTextProperty(display, data->xwindow, &iconprop,
slouken@1951
   687
                             XA_WM_ICON_NAME);
slouken@1951
   688
            XFree(iconprop.value);
slouken@1951
   689
        }
slouken@1951
   690
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   691
        if (SDL_X11_HAVE_UTF8) {
slouken@1951
   692
            status =
slouken@1951
   693
                Xutf8TextListToTextProperty(display, (char **) &icon, 1,
slouken@1951
   694
                                            XUTF8StringStyle, &iconprop);
slouken@1951
   695
            if (status == Success) {
slouken@3685
   696
                XSetTextProperty(display, data->xwindow, &iconprop,
slouken@1951
   697
                                 _NET_WM_ICON_NAME);
slouken@1951
   698
                XFree(iconprop.value);
slouken@1951
   699
            }
slouken@1951
   700
        }
slouken@1951
   701
#endif
slouken@1951
   702
    }
slouken@5031
   703
    XFlush(display);
slouken@1951
   704
}
slouken@1951
   705
slouken@1951
   706
void
slouken@2967
   707
X11_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slouken@2967
   708
{
slouken@2967
   709
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@2967
   710
    Display *display = data->videodata->display;
slouken@4518
   711
    Atom _NET_WM_ICON = data->videodata->_NET_WM_ICON;
slouken@2967
   712
slouken@2967
   713
    if (icon) {
slouken@2967
   714
        SDL_PixelFormat format;
slouken@2967
   715
        SDL_Surface *surface;
slouken@2967
   716
        int propsize;
slouken@4528
   717
        long *propdata;
slouken@2967
   718
slouken@2967
   719
        /* Convert the icon to ARGB for modern window managers */
slouken@5288
   720
        SDL_InitFormat(&format, SDL_PIXELFORMAT_ARGB8888);
slouken@2967
   721
        surface = SDL_ConvertSurface(icon, &format, 0);
slouken@2967
   722
        if (!surface) {
slouken@2967
   723
            return;
slouken@2967
   724
        }
slouken@2967
   725
slouken@2967
   726
        /* Set the _NET_WM_ICON property */
slouken@2990
   727
        propsize = 2 + (icon->w * icon->h);
slouken@4566
   728
        propdata = SDL_malloc(propsize * sizeof(long));
slouken@2967
   729
        if (propdata) {
slouken@4528
   730
            int x, y;
slouken@4528
   731
            Uint32 *src;
slouken@4528
   732
            long *dst;
slouken@4528
   733
slouken@2967
   734
            propdata[0] = icon->w;
slouken@2967
   735
            propdata[1] = icon->h;
slouken@4528
   736
            dst = &propdata[2];
slouken@4528
   737
            for (y = 0; y < icon->h; ++y) {
slouken@4528
   738
                src = (Uint32*)((Uint8*)surface->pixels + y * surface->pitch);
slouken@4528
   739
                for (x = 0; x < icon->w; ++x) {
slouken@4528
   740
                    *dst++ = *src++;
slouken@4528
   741
                }
slouken@4528
   742
            }
slouken@3685
   743
            XChangeProperty(display, data->xwindow, _NET_WM_ICON, XA_CARDINAL,
slouken@2990
   744
                            32, PropModeReplace, (unsigned char *) propdata,
slouken@2990
   745
                            propsize);
slouken@2967
   746
        }
slouken@2967
   747
        SDL_FreeSurface(surface);
slouken@2967
   748
    } else {
slouken@3685
   749
        XDeleteProperty(display, data->xwindow, _NET_WM_ICON);
slouken@2967
   750
    }
slouken@5031
   751
    XFlush(display);
slouken@2967
   752
}
slouken@2967
   753
slouken@2967
   754
void
slouken@1951
   755
X11_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1951
   756
{
slouken@1951
   757
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   758
    Display *display = data->videodata->display;
slouken@4518
   759
    SDL_bool oldstyle_fullscreen;
slouken@2875
   760
    int x, y;
slouken@1951
   761
slouken@4518
   762
    /* ICCCM2.0-compliant window managers can handle fullscreen windows */
slouken@4522
   763
    oldstyle_fullscreen = X11_IsWindowOldFullscreen(_this, window);
slouken@4518
   764
slouken@4518
   765
    if (oldstyle_fullscreen
slouken@5246
   766
        || SDL_WINDOWPOS_ISCENTERED(window->x)) {
slouken@2931
   767
        X11_GetDisplaySize(_this, window, &x, NULL);
slouken@2931
   768
        x = (x - window->w) / 2;
slouken@2875
   769
    } else {
slouken@2875
   770
        x = window->x;
slouken@2875
   771
    }
slouken@4518
   772
    if (oldstyle_fullscreen
slouken@5246
   773
        || SDL_WINDOWPOS_ISCENTERED(window->y)) {
slouken@2931
   774
        X11_GetDisplaySize(_this, window, NULL, &y);
slouken@2931
   775
        y = (y - window->h) / 2;
slouken@2875
   776
    } else {
slouken@2875
   777
        y = window->y;
slouken@2875
   778
    }
slouken@3685
   779
    XMoveWindow(display, data->xwindow, x, y);
slouken@5031
   780
    XFlush(display);
slouken@1951
   781
}
slouken@1951
   782
slouken@1951
   783
void
slouken@1951
   784
X11_SetWindowSize(_THIS, SDL_Window * window)
slouken@1951
   785
{
slouken@1951
   786
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   787
    Display *display = data->videodata->display;
slouken@1951
   788
slouken@4937
   789
    if (SDL_IsShapedWindow(window))
eligottlieb@4782
   790
        X11_ResizeWindowShape(window);
slouken@3685
   791
    XResizeWindow(display, data->xwindow, window->w, window->h);
slouken@5031
   792
    XFlush(display);
slouken@1951
   793
}
slouken@1951
   794
slouken@1951
   795
void
slouken@1951
   796
X11_ShowWindow(_THIS, SDL_Window * window)
slouken@1951
   797
{
slouken@1951
   798
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   799
    Display *display = data->videodata->display;
slouken@1951
   800
slouken@3685
   801
    XMapRaised(display, data->xwindow);
slouken@5031
   802
    XFlush(display);
slouken@1951
   803
}
slouken@1951
   804
slouken@1951
   805
void
slouken@1951
   806
X11_HideWindow(_THIS, SDL_Window * window)
slouken@1951
   807
{
slouken@1951
   808
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   809
    Display *display = data->videodata->display;
slouken@1951
   810
slouken@3685
   811
    XUnmapWindow(display, data->xwindow);
slouken@5031
   812
    XFlush(display);
slouken@1951
   813
}
slouken@1951
   814
slouken@1951
   815
void
slouken@1951
   816
X11_RaiseWindow(_THIS, SDL_Window * window)
slouken@1951
   817
{
slouken@1951
   818
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   819
    Display *display = data->videodata->display;
slouken@1951
   820
slouken@3685
   821
    XRaiseWindow(display, data->xwindow);
slouken@5031
   822
    XFlush(display);
slouken@1951
   823
}
slouken@1951
   824
slouken@3077
   825
static void
slouken@5302
   826
SetWindowMaximized(_THIS, SDL_Window * window, SDL_bool maximized)
slouken@3077
   827
{
slouken@3077
   828
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@3077
   829
    SDL_DisplayData *displaydata =
slouken@5246
   830
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@3077
   831
    Display *display = data->videodata->display;
slouken@4518
   832
    Atom _NET_WM_STATE = data->videodata->_NET_WM_STATE;
slouken@4518
   833
    Atom _NET_WM_STATE_MAXIMIZED_VERT = data->videodata->_NET_WM_STATE_MAXIMIZED_VERT;
slouken@4518
   834
    Atom _NET_WM_STATE_MAXIMIZED_HORZ = data->videodata->_NET_WM_STATE_MAXIMIZED_HORZ;
slouken@4522
   835
    Atom _NET_WM_STATE_FULLSCREEN = data->videodata->_NET_WM_STATE_FULLSCREEN;
slouken@3077
   836
slouken@4522
   837
    if (X11_IsWindowMapped(_this, window)) {
slouken@4522
   838
        XEvent e;
slouken@3077
   839
slouken@4524
   840
        SDL_zero(e);
slouken@4522
   841
        e.xany.type = ClientMessage;
slouken@4522
   842
        e.xclient.message_type = _NET_WM_STATE;
slouken@4522
   843
        e.xclient.format = 32;
slouken@4522
   844
        e.xclient.window = data->xwindow;
slouken@4522
   845
        e.xclient.data.l[0] =
slouken@4522
   846
            maximized ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
slouken@4522
   847
        e.xclient.data.l[1] = _NET_WM_STATE_MAXIMIZED_VERT;
slouken@4522
   848
        e.xclient.data.l[2] = _NET_WM_STATE_MAXIMIZED_HORZ;
slouken@4522
   849
        e.xclient.data.l[3] = 0l;
slouken@4522
   850
slouken@4522
   851
        XSendEvent(display, RootWindow(display, displaydata->screen), 0,
slouken@4522
   852
                   SubstructureNotifyMask | SubstructureRedirectMask, &e);
slouken@4522
   853
    } else {
slouken@4522
   854
        int count = 0;
slouken@4522
   855
        Atom atoms[3];
slouken@4522
   856
slouken@4522
   857
        if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@4522
   858
            atoms[count++] = _NET_WM_STATE_FULLSCREEN;
slouken@4522
   859
        }
slouken@4522
   860
        if (maximized) {
slouken@4522
   861
            atoms[count++] = _NET_WM_STATE_MAXIMIZED_VERT;
slouken@4522
   862
            atoms[count++] = _NET_WM_STATE_MAXIMIZED_HORZ;
slouken@4522
   863
        }
slouken@4522
   864
        if (count > 0) {
slouken@4522
   865
            XChangeProperty(display, data->xwindow, _NET_WM_STATE, XA_ATOM, 32,
slouken@4522
   866
                            PropModeReplace, (unsigned char *)atoms, count);
slouken@4522
   867
        } else {
slouken@4522
   868
            XDeleteProperty(display, data->xwindow, _NET_WM_STATE);
slouken@4522
   869
        }
slouken@4522
   870
    }
slouken@5031
   871
    XFlush(display);
slouken@3077
   872
}
slouken@3077
   873
slouken@1951
   874
void
slouken@1951
   875
X11_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1951
   876
{
slouken@5302
   877
    SetWindowMaximized(_this, window, SDL_TRUE);
slouken@1951
   878
}
slouken@1951
   879
slouken@1951
   880
void
slouken@1951
   881
X11_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1951
   882
{
slouken@4518
   883
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@4518
   884
    SDL_DisplayData *displaydata =
slouken@5246
   885
        (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
slouken@4518
   886
    Display *display = data->videodata->display;
slouken@4518
   887
 
slouken@4518
   888
    XIconifyWindow(display, data->xwindow, displaydata->screen);
slouken@5031
   889
    XFlush(display);
slouken@1951
   890
}
slouken@1951
   891
slouken@1951
   892
void
slouken@1951
   893
X11_RestoreWindow(_THIS, SDL_Window * window)
slouken@1951
   894
{
slouken@5302
   895
    SetWindowMaximized(_this, window, SDL_FALSE);
slouken@1951
   896
    X11_ShowWindow(_this, window);
slouken@1951
   897
}
slouken@1951
   898
slouken@5305
   899
void
slouken@5305
   900
X11_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * _display, SDL_bool fullscreen)
slouken@5302
   901
{
slouken@5302
   902
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5305
   903
    SDL_DisplayData *displaydata = (SDL_DisplayData *) _display->driverdata;
slouken@5302
   904
    Display *display = data->videodata->display;
slouken@5302
   905
    Atom _NET_WM_STATE = data->videodata->_NET_WM_STATE;
slouken@5302
   906
    Atom _NET_WM_STATE_MAXIMIZED_VERT = data->videodata->_NET_WM_STATE_MAXIMIZED_VERT;
slouken@5302
   907
    Atom _NET_WM_STATE_MAXIMIZED_HORZ = data->videodata->_NET_WM_STATE_MAXIMIZED_HORZ;
slouken@5302
   908
    Atom _NET_WM_STATE_FULLSCREEN = data->videodata->_NET_WM_STATE_FULLSCREEN;
slouken@5302
   909
slouken@5302
   910
    if (X11_IsWindowMapped(_this, window)) {
slouken@5302
   911
        XEvent e;
slouken@5302
   912
slouken@5302
   913
        SDL_zero(e);
slouken@5302
   914
        e.xany.type = ClientMessage;
slouken@5302
   915
        e.xclient.message_type = _NET_WM_STATE;
slouken@5302
   916
        e.xclient.format = 32;
slouken@5302
   917
        e.xclient.window = data->xwindow;
slouken@5302
   918
        e.xclient.data.l[0] =
slouken@5302
   919
            fullscreen ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
slouken@5302
   920
        e.xclient.data.l[1] = _NET_WM_STATE_FULLSCREEN;
slouken@5302
   921
        e.xclient.data.l[3] = 0l;
slouken@5302
   922
slouken@5302
   923
        XSendEvent(display, RootWindow(display, displaydata->screen), 0,
slouken@5302
   924
                   SubstructureNotifyMask | SubstructureRedirectMask, &e);
slouken@5302
   925
    } else {
slouken@5302
   926
        int count = 0;
slouken@5302
   927
        Atom atoms[3];
slouken@5302
   928
slouken@5302
   929
        if (fullscreen) {
slouken@5302
   930
            atoms[count++] = _NET_WM_STATE_FULLSCREEN;
slouken@5302
   931
        }
slouken@5302
   932
        if (window->flags & SDL_WINDOW_MAXIMIZED) {
slouken@5302
   933
            atoms[count++] = _NET_WM_STATE_MAXIMIZED_VERT;
slouken@5302
   934
            atoms[count++] = _NET_WM_STATE_MAXIMIZED_HORZ;
slouken@5302
   935
        }
slouken@5302
   936
        if (count > 0) {
slouken@5302
   937
            XChangeProperty(display, data->xwindow, _NET_WM_STATE, XA_ATOM, 32,
slouken@5302
   938
                            PropModeReplace, (unsigned char *)atoms, count);
slouken@5302
   939
        } else {
slouken@5302
   940
            XDeleteProperty(display, data->xwindow, _NET_WM_STATE);
slouken@5302
   941
        }
slouken@5302
   942
    }
slouken@5302
   943
    XFlush(display);
slouken@5302
   944
}
slouken@5302
   945
slouken@5466
   946
int
slouken@5466
   947
X11_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
slouken@5466
   948
{
slouken@5466
   949
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5466
   950
    Display *display = data->videodata->display;
slouken@5466
   951
    Visual *visual = data->visual;
slouken@5466
   952
    Colormap colormap = data->colormap;
slouken@5466
   953
    XColor *colorcells;
slouken@5466
   954
    int ncolors;
slouken@5466
   955
    int rmask, gmask, bmask;
slouken@5466
   956
    int rshift, gshift, bshift;
slouken@5466
   957
    int i, j;
slouken@5466
   958
slouken@5466
   959
    if (visual->class != DirectColor) {
slouken@5466
   960
        SDL_SetError("Window doesn't have DirectColor visual");
slouken@5466
   961
        return -1;
slouken@5466
   962
    }
slouken@5466
   963
slouken@5466
   964
    ncolors = visual->map_entries;
slouken@5466
   965
    colorcells = SDL_malloc(ncolors * sizeof(XColor));
slouken@5466
   966
    if (!colorcells) {
slouken@5466
   967
        SDL_OutOfMemory();
slouken@5466
   968
        return -1;
slouken@5466
   969
    }
slouken@5466
   970
slouken@5466
   971
    rshift = 0;
slouken@5466
   972
    rmask = visual->red_mask;
slouken@5466
   973
    while (0 == (rmask & 1)) {
slouken@5466
   974
        rshift++;
slouken@5466
   975
        rmask >>= 1;
slouken@5466
   976
    }
slouken@5466
   977
slouken@5466
   978
    gshift = 0;
slouken@5466
   979
    gmask = visual->green_mask;
slouken@5466
   980
    while (0 == (gmask & 1)) {
slouken@5466
   981
        gshift++;
slouken@5466
   982
        gmask >>= 1;
slouken@5466
   983
    }
slouken@5466
   984
slouken@5466
   985
    bshift = 0;
slouken@5466
   986
    bmask = visual->blue_mask;
slouken@5466
   987
    while (0 == (bmask & 1)) {
slouken@5466
   988
        bshift++;
slouken@5466
   989
        bmask >>= 1;
slouken@5466
   990
    }
slouken@5466
   991
slouken@5466
   992
    /* build the color table pixel values */
slouken@5466
   993
    for (i = 0; i < ncolors; i++) {
slouken@5466
   994
        Uint32 rbits = (rmask * i) / (ncolors - 1);
slouken@5466
   995
        Uint32 gbits = (gmask * i) / (ncolors - 1);
slouken@5466
   996
        Uint32 bbits = (bmask * i) / (ncolors - 1);
slouken@5466
   997
        Uint32 pix = (rbits << rshift) | (gbits << gshift) | (bbits << bshift);
slouken@5466
   998
slouken@5466
   999
        colorcells[i].pixel = pix;
slouken@5466
  1000
slouken@5466
  1001
        colorcells[i].red = ramp[(0 * 256) + i];
slouken@5466
  1002
        colorcells[i].green = ramp[(1 * 256) + i];
slouken@5466
  1003
        colorcells[i].blue = ramp[(2 * 256) + i];
slouken@5466
  1004
slouken@5466
  1005
        colorcells[i].flags = DoRed | DoGreen | DoBlue;
slouken@5466
  1006
    }
slouken@5466
  1007
slouken@5466
  1008
    XStoreColors(display, colormap, colorcells, ncolors);
slouken@5466
  1009
    XFlush(display);
slouken@5466
  1010
    SDL_free(colorcells);
slouken@5466
  1011
slouken@5466
  1012
    return 0;
slouken@5466
  1013
}
slouken@5466
  1014
slouken@5302
  1015
void
slouken@1951
  1016
X11_SetWindowGrab(_THIS, SDL_Window * window)
slouken@1951
  1017
{
slouken@2875
  1018
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@2875
  1019
    Display *display = data->videodata->display;
slouken@4518
  1020
    SDL_bool oldstyle_fullscreen;
slouken@2875
  1021
slouken@4518
  1022
    /* ICCCM2.0-compliant window managers can handle fullscreen windows */
slouken@4522
  1023
    oldstyle_fullscreen = X11_IsWindowOldFullscreen(_this, window);
slouken@4518
  1024
slouken@4518
  1025
    if (((window->flags & SDL_WINDOW_INPUT_GRABBED) || oldstyle_fullscreen)
slouken@2876
  1026
        && (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@2875
  1027
        /* Try to grab the mouse */
slouken@2876
  1028
        for (;;) {
slouken@2876
  1029
            int result =
slouken@3685
  1030
                XGrabPointer(display, data->xwindow, True, 0, GrabModeAsync,
slouken@3685
  1031
                             GrabModeAsync, data->xwindow, None, CurrentTime);
slouken@2876
  1032
            if (result == GrabSuccess) {
slouken@2875
  1033
                break;
slouken@2875
  1034
            }
slouken@2875
  1035
            SDL_Delay(100);
slouken@2875
  1036
        }
slouken@2875
  1037
slouken@2875
  1038
        /* Raise the window if we grab the mouse */
slouken@3685
  1039
        XRaiseWindow(display, data->xwindow);
slouken@2875
  1040
slouken@2875
  1041
        /* Now grab the keyboard */
slouken@3685
  1042
        XGrabKeyboard(display, data->xwindow, True, GrabModeAsync,
slouken@2876
  1043
                      GrabModeAsync, CurrentTime);
slouken@2875
  1044
    } else {
slouken@2875
  1045
        XUngrabPointer(display, CurrentTime);
slouken@2875
  1046
        XUngrabKeyboard(display, CurrentTime);
slouken@2875
  1047
    }
slouken@1951
  1048
}
slouken@1951
  1049
slouken@1951
  1050
void
slouken@1951
  1051
X11_DestroyWindow(_THIS, SDL_Window * window)
slouken@1951
  1052
{
slouken@1951
  1053
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
bob@2323
  1054
    window->driverdata = NULL;
slouken@1951
  1055
slouken@1951
  1056
    if (data) {
bob@2323
  1057
        SDL_VideoData *videodata = (SDL_VideoData *) data->videodata;
bob@2323
  1058
        Display *display = videodata->display;
bob@2323
  1059
        int numwindows = videodata->numwindows;
bob@2323
  1060
        SDL_WindowData **windowlist = videodata->windowlist;
bob@2323
  1061
        int i;
bob@2323
  1062
bob@2323
  1063
        if (windowlist) {
bob@2324
  1064
            for (i = 0; i < numwindows; ++i) {
slouken@3685
  1065
                if (windowlist[i] && (windowlist[i]->window == window)) {
bob@2324
  1066
                    windowlist[i] = windowlist[numwindows - 1];
bob@2324
  1067
                    windowlist[numwindows - 1] = NULL;
bob@2324
  1068
                    videodata->numwindows--;
bob@2324
  1069
                    break;
bob@2324
  1070
                }
bob@2323
  1071
            }
bob@2323
  1072
        }
slouken@1951
  1073
#ifdef X_HAVE_UTF8_STRING
slouken@1951
  1074
        if (data->ic) {
slouken@1951
  1075
            XDestroyIC(data->ic);
slouken@1951
  1076
        }
slouken@1951
  1077
#endif
slouken@1951
  1078
        if (data->created) {
slouken@3685
  1079
            XDestroyWindow(display, data->xwindow);
slouken@5031
  1080
            XFlush(display);
slouken@1951
  1081
        }
slouken@1951
  1082
        SDL_free(data);
slouken@1951
  1083
    }
slouken@1951
  1084
}
slouken@1951
  1085
slouken@1951
  1086
SDL_bool
slouken@1951
  1087
X11_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1951
  1088
{
slouken@4510
  1089
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@4510
  1090
    Display *display = data->videodata->display;
slouken@4510
  1091
slouken@4510
  1092
    if (info->version.major == SDL_MAJOR_VERSION &&
slouken@4510
  1093
        info->version.minor == SDL_MINOR_VERSION) {
slouken@4510
  1094
        info->subsystem = SDL_SYSWM_X11;
slouken@5056
  1095
        info->info.x11.display = display;
slouken@5056
  1096
        info->info.x11.window = data->xwindow;
slouken@1951
  1097
        return SDL_TRUE;
slouken@1951
  1098
    } else {
slouken@1951
  1099
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1951
  1100
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1951
  1101
        return SDL_FALSE;
slouken@1951
  1102
    }
slouken@1951
  1103
}
slouken@1951
  1104
slouken@1951
  1105
/* vi: set ts=4 sw=4 expandtab: */