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