src/video/x11/SDL_x11window.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 05 Apr 2011 09:47:34 -0700
changeset 5531 477f550f6efd
parent 5481 22dfc3958dc3
child 5535 96594ac5fd1a
permissions -rw-r--r--
Fixed setting programmatically setting the size of a window on X11 for non-resizable windows.

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