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