src/video/x11/SDL_x11window.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 09 Feb 2009 05:32:12 +0000
changeset 3057 089a77aebb7d
parent 3052 b7197d7e8566
child 3077 9b58f26ede81
permissions -rw-r--r--
Added test program for SDL_CreateWindowFrom()
Make sure OpenGL library is loaded before working with OpenGL windows,
even those created with SDL_CreateWindowFrom()
slouken@1951
     1
/*
slouken@1951
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@1951
     4
slouken@1951
     5
    This library is free software; you can redistribute it and/or
slouken@1951
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1951
     7
    License as published by the Free Software Foundation; either
slouken@1951
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1951
     9
slouken@1951
    10
    This library is distributed in the hope that it will be useful,
slouken@1951
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1951
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1951
    13
    Lesser General Public License for more details.
slouken@1951
    14
slouken@1951
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1951
    16
    License along with this library; if not, write to the Free Software
slouken@1951
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1951
    18
slouken@1951
    19
    Sam Lantinga
slouken@1951
    20
    slouken@libsdl.org
slouken@1951
    21
*/
slouken@1951
    22
#include "SDL_config.h"
slouken@1951
    23
slouken@1951
    24
#include "SDL_syswm.h"
slouken@1951
    25
#include "../SDL_sysvideo.h"
slouken@1951
    26
#include "../../events/SDL_keyboard_c.h"
slouken@2940
    27
#include "../../events/SDL_mouse_c.h"
slouken@1951
    28
slouken@1951
    29
#include "SDL_x11video.h"
slouken@2940
    30
#include "SDL_x11mouse.h"
bob@3009
    31
#include "SDL_x11gamma.h"
bob@2185
    32
#include "../Xext/extensions/StdCmap.h"
slouken@1951
    33
slouken@2931
    34
static void
slouken@2931
    35
X11_GetDisplaySize(_THIS, SDL_Window * window, int *w, int *h)
slouken@2931
    36
{
slouken@2931
    37
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@2931
    38
    SDL_DisplayData *displaydata =
slouken@2931
    39
        (SDL_DisplayData *) SDL_GetDisplayFromWindow(window)->driverdata;
slouken@2931
    40
    XWindowAttributes attr;
slouken@2931
    41
slouken@2931
    42
    XGetWindowAttributes(data->display, RootWindow(data->display,
slouken@2931
    43
                                                   displaydata->screen),
slouken@2931
    44
                         &attr);
slouken@2931
    45
    if (w) {
slouken@2931
    46
        *w = attr.width;
slouken@2931
    47
    }
slouken@2931
    48
    if (h) {
slouken@2931
    49
        *h = attr.height;
slouken@2931
    50
    }
slouken@2931
    51
}
slouken@2931
    52
slouken@1951
    53
static int
slouken@1951
    54
SetupWindowData(_THIS, SDL_Window * window, Window w, BOOL created)
slouken@1951
    55
{
slouken@1951
    56
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1951
    57
    SDL_WindowData *data;
slouken@1951
    58
    int numwindows = videodata->numwindows;
bob@2324
    59
    int windowlistlength = videodata->windowlistlength;
slouken@1951
    60
    SDL_WindowData **windowlist = videodata->windowlist;
bob@2323
    61
    int index;
slouken@1951
    62
slouken@1951
    63
    /* Allocate the window data */
bob@2323
    64
    data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
slouken@1951
    65
    if (!data) {
slouken@1951
    66
        SDL_OutOfMemory();
slouken@1951
    67
        return -1;
slouken@1951
    68
    }
slouken@1951
    69
    data->windowID = window->id;
slouken@1951
    70
    data->window = w;
slouken@1951
    71
#ifdef X_HAVE_UTF8_STRING
slouken@1951
    72
    if (SDL_X11_HAVE_UTF8) {
slouken@1951
    73
        data->ic =
slouken@1951
    74
            pXCreateIC(videodata->im, XNClientWindow, w, XNFocusWindow, w,
slouken@1951
    75
                       XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
slouken@1951
    76
                       XNResourceName, videodata->classname, XNResourceClass,
slouken@1951
    77
                       videodata->classname, NULL);
slouken@1951
    78
    }
slouken@1951
    79
#endif
slouken@1951
    80
    data->created = created;
slouken@1951
    81
    data->videodata = videodata;
slouken@1951
    82
bob@2323
    83
    /* Associate the data with the window */
bob@2324
    84
bob@2324
    85
    if (numwindows < windowlistlength) {
bob@2324
    86
        windowlist[numwindows] = data;
bob@2324
    87
        videodata->numwindows++;
bob@2324
    88
    } else {
bob@2324
    89
        windowlist =
bob@2324
    90
            (SDL_WindowData **) SDL_realloc(windowlist,
bob@2324
    91
                                            (numwindows +
bob@2324
    92
                                             1) * sizeof(*windowlist));
bob@2324
    93
        if (!windowlist) {
bob@2324
    94
            SDL_OutOfMemory();
bob@2324
    95
            SDL_free(data);
bob@2324
    96
            return -1;
bob@2323
    97
        }
bob@2324
    98
        windowlist[numwindows] = data;
bob@2324
    99
        videodata->numwindows++;
bob@2324
   100
        videodata->windowlistlength++;
bob@2324
   101
        videodata->windowlist = windowlist;
bob@2323
   102
    }
bob@2323
   103
slouken@1951
   104
    /* Fill in the SDL window with the window data */
slouken@1951
   105
    {
slouken@1951
   106
        XWindowAttributes attrib;
slouken@1951
   107
slouken@1951
   108
        XGetWindowAttributes(data->videodata->display, w, &attrib);
slouken@1951
   109
        window->x = attrib.x;
slouken@1951
   110
        window->y = attrib.y;
slouken@1951
   111
        window->w = attrib.width;
slouken@1951
   112
        window->h = attrib.height;
slouken@1951
   113
        if (attrib.map_state != IsUnmapped) {
slouken@1951
   114
            window->flags |= SDL_WINDOW_SHOWN;
slouken@1951
   115
        } else {
slouken@1951
   116
            window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1951
   117
        }
slouken@1951
   118
    }
slouken@1951
   119
    /* FIXME: How can I tell?
slouken@1951
   120
       {
slouken@1951
   121
       DWORD style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1951
   122
       if (style & WS_VISIBLE) {
slouken@1951
   123
       if (style & (WS_BORDER | WS_THICKFRAME)) {
slouken@1951
   124
       window->flags &= ~SDL_WINDOW_BORDERLESS;
slouken@1951
   125
       } else {
slouken@1951
   126
       window->flags |= SDL_WINDOW_BORDERLESS;
slouken@1951
   127
       }
slouken@1951
   128
       if (style & WS_THICKFRAME) {
slouken@1951
   129
       window->flags |= SDL_WINDOW_RESIZABLE;
slouken@1951
   130
       } else {
slouken@1951
   131
       window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@1951
   132
       }
slouken@1951
   133
       if (style & WS_MAXIMIZE) {
slouken@1951
   134
       window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1951
   135
       } else {
slouken@1951
   136
       window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@1951
   137
       }
slouken@1951
   138
       if (style & WS_MINIMIZE) {
slouken@1951
   139
       window->flags |= SDL_WINDOW_MINIMIZED;
slouken@1951
   140
       } else {
slouken@1951
   141
       window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@1951
   142
       }
slouken@1951
   143
       }
slouken@1951
   144
       if (GetFocus() == hwnd) {
slouken@1951
   145
       int index = data->videodata->keyboard;
slouken@1951
   146
       window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@1951
   147
       SDL_SetKeyboardFocus(index, data->windowID);
slouken@1951
   148
slouken@1951
   149
       if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1951
   150
       RECT rect;
slouken@1951
   151
       GetClientRect(hwnd, &rect);
slouken@1951
   152
       ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1951
   153
       ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1951
   154
       ClipCursor(&rect);
slouken@1951
   155
       }
slouken@1951
   156
       }
slouken@1951
   157
     */
slouken@1951
   158
slouken@1951
   159
    /* All done! */
slouken@1951
   160
    window->driverdata = data;
slouken@1951
   161
    return 0;
slouken@1951
   162
}
slouken@1951
   163
slouken@1951
   164
int
slouken@1951
   165
X11_CreateWindow(_THIS, SDL_Window * window)
slouken@1951
   166
{
slouken@1951
   167
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   168
    SDL_DisplayData *displaydata =
slouken@1952
   169
        (SDL_DisplayData *) SDL_GetDisplayFromWindow(window)->driverdata;
slouken@1951
   170
    Visual *visual;
slouken@1951
   171
    int depth;
slouken@1951
   172
    XSetWindowAttributes xattr;
slouken@1951
   173
    int x, y;
slouken@1951
   174
    Window w;
slouken@1951
   175
    XSizeHints *sizehints;
slouken@1951
   176
    XWMHints *wmhints;
slouken@1951
   177
    XClassHint *classhints;
slouken@1951
   178
slouken@1951
   179
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@1951
   180
/* FIXME
slouken@1951
   181
    if ( use_xinerama ) {
slouken@1951
   182
        x = xinerama_info.x_org;
slouken@1951
   183
        y = xinerama_info.y_org;
slouken@1951
   184
    }
slouken@1951
   185
*/
slouken@1951
   186
#endif
slouken@1952
   187
#ifdef SDL_VIDEO_OPENGL_GLX
slouken@1951
   188
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@1952
   189
        XVisualInfo *vinfo;
slouken@1952
   190
slouken@1952
   191
        vinfo = X11_GL_GetVisual(_this, data->display, displaydata->screen);
slouken@1952
   192
        if (!vinfo) {
slouken@1952
   193
            return -1;
slouken@1952
   194
        }
slouken@1952
   195
        visual = vinfo->visual;
slouken@1952
   196
        depth = vinfo->depth;
slouken@1952
   197
        XFree(vinfo);
slouken@1952
   198
    } else
slouken@1952
   199
#endif
slouken@1952
   200
    {
slouken@1951
   201
        visual = displaydata->visual;
slouken@1951
   202
        depth = displaydata->depth;
slouken@1951
   203
    }
slouken@1951
   204
slouken@1951
   205
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1951
   206
        xattr.override_redirect = True;
slouken@1951
   207
    } else {
slouken@1951
   208
        xattr.override_redirect = False;
slouken@1951
   209
    }
slouken@1951
   210
    xattr.background_pixel = 0;
slouken@1951
   211
    xattr.border_pixel = 0;
bob@2214
   212
slouken@2945
   213
    if (visual->class == PseudoColor) {
bob@3044
   214
        printf("asking for PseudoColor\n");
bob@3044
   215
bob@3044
   216
        Status status;
bob@2214
   217
        XStandardColormap cmap;
bob@2214
   218
        XColor *colorcells;
bob@2214
   219
        Colormap colormap;
slouken@3052
   220
        Sint32 pix;
bob@3044
   221
        Sint32 ncolors;
bob@3044
   222
        Sint32 nbits;
bob@3044
   223
        Sint32 rmax, gmax, bmax;
slouken@3052
   224
        Sint32 rwidth, gwidth, bwidth;
bob@3044
   225
        Sint32 rmask, gmask, bmask;
bob@3044
   226
        Sint32 rshift, gshift, bshift;
slouken@3052
   227
        Sint32 r, g, b;
bob@2185
   228
bob@3044
   229
        /* Is the colormap we need already registered in SDL? */
bob@2214
   230
        if (colormap =
bob@3044
   231
            X11_LookupColormap(data->display,
bob@3044
   232
                               displaydata->screen, visual->visualid)) {
bob@2214
   233
            xattr.colormap = colormap;
bob@3044
   234
/*             printf("found existing colormap\n"); */
bob@2214
   235
        } else {
bob@3044
   236
            /* The colormap is not known to SDL so we will create it */
bob@3044
   237
            colormap = XCreateColormap(data->display,
bob@3044
   238
                                       RootWindow(data->display,
bob@3044
   239
                                                  displaydata->screen),
bob@3044
   240
                                       visual, AllocAll);
bob@3044
   241
/*             printf("colormap = %x\n", colormap); */
bob@3044
   242
bob@3044
   243
            /* If we can't create a colormap, then we must die */
bob@3044
   244
            if (!colormap) {
bob@3044
   245
                SDL_SetError
bob@3044
   246
                    ("Couldn't create window: Could not create writable colormap");
bob@3044
   247
                return -1;
bob@2185
   248
            }
bob@2213
   249
bob@3044
   250
            /* OK, we got a colormap, now fill it in as best as we can */
bob@2214
   251
bob@2214
   252
            colorcells = SDL_malloc(visual->map_entries * sizeof(XColor));
bob@2214
   253
            if (NULL == colorcells) {
bob@2214
   254
                SDL_SetError("out of memory in X11_CreateWindow");
bob@2185
   255
                return -1;
bob@2185
   256
            }
bob@3044
   257
bob@2214
   258
            ncolors = visual->map_entries;
slouken@3052
   259
            nbits = visual->bits_per_rgb;
bob@2214
   260
bob@3044
   261
/* 	    printf("ncolors = %d nbits = %d\n", ncolors, nbits); */
bob@3044
   262
slouken@3052
   263
            /* what if ncolors != (1 << nbits)? That can happen on a
slouken@3052
   264
               true PseudoColor display.  I'm assuming that we will
slouken@3052
   265
               always have ncolors == (1 << nbits) */
bob@3044
   266
slouken@3052
   267
            /* I'm making a lot of assumptions here. */
bob@3044
   268
slouken@3052
   269
            /* Compute the width of each field. If there is one extra
slouken@3052
   270
               bit, give it to green. If there are two extra bits give
slouken@3052
   271
               them to red and greed.  We can get extra bits when the
slouken@3052
   272
               number of bits per pixel is not a multiple of 3. For
slouken@3052
   273
               example when we have 16 bits per pixel and need a 5/6/5
slouken@3052
   274
               layout for the RGB fields */
slouken@3052
   275
slouken@3052
   276
            rwidth = (nbits / 3) + (((nbits % 3) == 2) ? 1 : 0);
slouken@3052
   277
            gwidth = (nbits / 3) + (((nbits % 3) >= 1) ? 1 : 0);
slouken@3052
   278
            bwidth = (nbits / 3);
bob@3044
   279
bob@3044
   280
            rshift = gwidth + bwidth;
bob@3044
   281
            gshift = bwidth;
bob@3044
   282
            bshift = 0;
bob@3044
   283
bob@3044
   284
            rmax = 1 << rwidth;
bob@3044
   285
            gmax = 1 << gwidth;
bob@3044
   286
            bmax = 1 << bwidth;
bob@3044
   287
bob@3044
   288
            rmask = rmax - 1;
bob@3044
   289
            gmask = gmax - 1;
bob@3044
   290
            bmask = bmax - 1;
bob@3044
   291
bob@3044
   292
/*             printf("red   mask = %4x shift = %4d width = %d\n", rmask, rshift, rwidth); */
bob@3044
   293
/*             printf("green mask = %4x shift = %4d width = %d\n", gmask, gshift, gwidth); */
bob@3044
   294
/*             printf("blue  mask = %4x shift = %4d width = %d\n", bmask, bshift, bwidth); */
bob@2214
   295
bob@2214
   296
            /* build the color table pixel values */
slouken@3052
   297
            pix = 0;
slouken@3052
   298
            for (r = 0; r < rmax; r++) {
slouken@3052
   299
                for (g = 0; g < gmax; g++) {
slouken@3052
   300
                    for (b = 0; b < bmax; b++) {
slouken@3052
   301
                        colorcells[pix].pixel =
slouken@3052
   302
                            (r << rshift) | (g << gshift) | (b << bshift);
slouken@3052
   303
                        colorcells[pix].red = (0xffff * r) / rmask;
slouken@3052
   304
                        colorcells[pix].green = (0xffff * g) / gmask;
slouken@3052
   305
                        colorcells[pix].blue = (0xffff * b) / bmask;
bob@3044
   306
/* 		  printf("%4x:%4x [%4x %4x %4x]\n",  */
bob@3044
   307
/* 			 pix,  */
bob@3044
   308
/* 			 colorcells[pix].pixel, */
bob@3044
   309
/* 			 colorcells[pix].red, */
bob@3044
   310
/* 			 colorcells[pix].green, */
bob@3044
   311
/* 			 colorcells[pix].blue); */
slouken@3052
   312
                        pix++;
slouken@3052
   313
                    }
slouken@3052
   314
                }
slouken@3052
   315
            }
bob@2214
   316
bob@3044
   317
/*             status = */
bob@3044
   318
/*                 XStoreColors(data->display, colormap, colorcells, ncolors); */
bob@2214
   319
bob@2214
   320
            xattr.colormap = colormap;
bob@3044
   321
            X11_TrackColormap(data->display, displaydata->screen,
bob@3044
   322
                              colormap, visual, NULL);
bob@3044
   323
bob@3009
   324
            SDL_free(colorcells);
bob@2185
   325
        }
slouken@2945
   326
    } else if (visual->class == DirectColor) {
bob@3009
   327
        Status status;
bob@3009
   328
        XStandardColormap cmap;
bob@3009
   329
        XColor *colorcells;
bob@3009
   330
        Colormap colormap;
bob@3009
   331
        int i;
bob@3009
   332
        int ncolors;
bob@3009
   333
        int rmax, gmax, bmax;
bob@3009
   334
        int rmask, gmask, bmask;
bob@3009
   335
        int rshift, gshift, bshift;
bob@3009
   336
bob@3009
   337
        /* Is the colormap we need already registered in SDL? */
bob@3009
   338
        if (colormap =
bob@3009
   339
            X11_LookupColormap(data->display,
bob@3009
   340
                               displaydata->screen, visual->visualid)) {
bob@3009
   341
            xattr.colormap = colormap;
bob@3009
   342
/*             printf("found existing colormap\n"); */
bob@3009
   343
        } else {
bob@3009
   344
            /* The colormap is not known to SDL so we will create it */
bob@3009
   345
            colormap = XCreateColormap(data->display,
bob@3009
   346
                                       RootWindow(data->display,
bob@3009
   347
                                                  displaydata->screen),
bob@3009
   348
                                       visual, AllocAll);
bob@3009
   349
/*             printf("colormap = %x\n", colormap); */
bob@3009
   350
bob@3009
   351
            /* If we can't create a colormap, then we must die */
bob@3009
   352
            if (!colormap) {
bob@3009
   353
                SDL_SetError
bob@3044
   354
                    ("Couldn't create window: Could not create writable colormap");
bob@3009
   355
                return -1;
bob@3009
   356
            }
bob@3009
   357
bob@3009
   358
            /* OK, we got a colormap, now fill it in as best as we can */
bob@3009
   359
bob@3009
   360
            colorcells = SDL_malloc(visual->map_entries * sizeof(XColor));
bob@3009
   361
            if (NULL == colorcells) {
bob@3009
   362
                SDL_SetError("out of memory in X11_CreateWindow");
bob@3009
   363
                return -1;
bob@3009
   364
            }
bob@3009
   365
            ncolors = visual->map_entries;
bob@3009
   366
            rmax = 0xffff;
bob@3009
   367
            gmax = 0xffff;
bob@3009
   368
            bmax = 0xffff;
bob@3009
   369
bob@3009
   370
            rshift = 0;
bob@3009
   371
            rmask = visual->red_mask;
bob@3009
   372
            while (0 == (rmask & 1)) {
bob@3009
   373
                rshift++;
bob@3009
   374
                rmask >>= 1;
bob@3009
   375
            }
bob@3009
   376
bob@3009
   377
/*             printf("rmask = %4x rshift = %4d\n", rmask, rshift); */
bob@3009
   378
bob@3009
   379
            gshift = 0;
bob@3009
   380
            gmask = visual->green_mask;
bob@3009
   381
            while (0 == (gmask & 1)) {
bob@3009
   382
                gshift++;
bob@3009
   383
                gmask >>= 1;
bob@3009
   384
            }
bob@3009
   385
bob@3009
   386
/*             printf("gmask = %4x gshift = %4d\n", gmask, gshift); */
bob@3009
   387
bob@3009
   388
            bshift = 0;
bob@3009
   389
            bmask = visual->blue_mask;
bob@3009
   390
            while (0 == (bmask & 1)) {
bob@3009
   391
                bshift++;
bob@3009
   392
                bmask >>= 1;
bob@3009
   393
            }
bob@3009
   394
bob@3009
   395
/*             printf("bmask = %4x bshift = %4d\n", bmask, bshift); */
bob@3009
   396
bob@3009
   397
            /* build the color table pixel values */
bob@3009
   398
            for (i = 0; i < ncolors; i++) {
bob@3009
   399
                Uint32 red = (rmax * i) / (ncolors - 1);
bob@3009
   400
                Uint32 green = (gmax * i) / (ncolors - 1);
bob@3009
   401
                Uint32 blue = (bmax * i) / (ncolors - 1);
bob@3009
   402
bob@3009
   403
                Uint32 rbits = (rmask * i) / (ncolors - 1);
bob@3009
   404
                Uint32 gbits = (gmask * i) / (ncolors - 1);
bob@3009
   405
                Uint32 bbits = (bmask * i) / (ncolors - 1);
bob@3009
   406
bob@3009
   407
                Uint32 pix =
bob@3009
   408
                    (rbits << rshift) | (gbits << gshift) | (bbits << bshift);
bob@3009
   409
bob@3009
   410
                colorcells[i].pixel = pix;
bob@3009
   411
bob@3009
   412
                colorcells[i].red = red;
bob@3009
   413
                colorcells[i].green = green;
bob@3009
   414
                colorcells[i].blue = blue;
bob@3009
   415
bob@3009
   416
                colorcells[i].flags = DoRed | DoGreen | DoBlue;
bob@3009
   417
/* 		printf("%2d:%4x [%4x %4x %4x]\n", i, pix, red, green, blue); */
bob@3009
   418
            }
bob@3009
   419
bob@3009
   420
            status =
bob@3009
   421
                XStoreColors(data->display, colormap, colorcells, ncolors);
bob@3009
   422
bob@3009
   423
            xattr.colormap = colormap;
bob@3009
   424
            X11_TrackColormap(data->display, displaydata->screen,
bob@3009
   425
                              colormap, visual, colorcells);
bob@3009
   426
bob@3009
   427
            SDL_free(colorcells);
bob@3009
   428
        }
slouken@1951
   429
    } else {
slouken@1951
   430
        xattr.colormap =
slouken@1951
   431
            XCreateColormap(data->display,
slouken@1951
   432
                            RootWindow(data->display, displaydata->screen),
slouken@1951
   433
                            visual, AllocNone);
slouken@1951
   434
    }
slouken@1951
   435
slouken@2876
   436
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@2876
   437
        || window->x == SDL_WINDOWPOS_CENTERED) {
slouken@2931
   438
        X11_GetDisplaySize(_this, window, &x, NULL);
slouken@2931
   439
        x = (x - window->w) / 2;
slouken@1951
   440
    } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
slouken@1951
   441
        x = 0;
slouken@1951
   442
    } else {
slouken@1951
   443
        x = window->x;
slouken@1951
   444
    }
slouken@2876
   445
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@2876
   446
        || window->y == SDL_WINDOWPOS_CENTERED) {
slouken@2931
   447
        X11_GetDisplaySize(_this, window, NULL, &y);
slouken@2931
   448
        y = (y - window->h) / 2;
slouken@1951
   449
    } else if (window->y == SDL_WINDOWPOS_UNDEFINED) {
slouken@1951
   450
        y = 0;
slouken@1951
   451
    } else {
slouken@1951
   452
        y = window->y;
slouken@1951
   453
    }
slouken@1951
   454
slouken@1951
   455
    w = XCreateWindow(data->display,
slouken@1951
   456
                      RootWindow(data->display, displaydata->screen), x, y,
slouken@1951
   457
                      window->w, window->h, 0, depth, InputOutput, visual,
slouken@1951
   458
                      (CWOverrideRedirect | CWBackPixel | CWBorderPixel |
slouken@1951
   459
                       CWColormap), &xattr);
slouken@1952
   460
    if (!w) {
slouken@1952
   461
        SDL_SetError("Couldn't create window");
slouken@1952
   462
        return -1;
slouken@1952
   463
    }
slouken@1951
   464
slouken@1951
   465
    sizehints = XAllocSizeHints();
slouken@1951
   466
    if (sizehints) {
slouken@2876
   467
        if ((window->flags & SDL_WINDOW_RESIZABLE)
slouken@2876
   468
            && !(window->flags & SDL_WINDOW_FULLSCREEN)) {
slouken@1951
   469
            sizehints->min_width = 32;
slouken@1951
   470
            sizehints->min_height = 32;
slouken@1951
   471
            sizehints->max_height = 4096;
slouken@1951
   472
            sizehints->max_width = 4096;
slouken@1951
   473
        } else {
slouken@1951
   474
            sizehints->min_width = sizehints->max_width = window->w;
slouken@1951
   475
            sizehints->min_height = sizehints->max_height = window->h;
slouken@1951
   476
        }
slouken@1951
   477
        sizehints->flags = PMaxSize | PMinSize;
slouken@1951
   478
        if (!(window->flags & SDL_WINDOW_FULLSCREEN)
slouken@1951
   479
            && window->x != SDL_WINDOWPOS_UNDEFINED
slouken@1951
   480
            && window->y != SDL_WINDOWPOS_UNDEFINED) {
slouken@1951
   481
            sizehints->x = x;
slouken@1951
   482
            sizehints->y = y;
slouken@1951
   483
            sizehints->flags |= USPosition;
slouken@1951
   484
        }
slouken@1951
   485
        XSetWMNormalHints(data->display, w, sizehints);
slouken@1951
   486
        XFree(sizehints);
slouken@1951
   487
    }
slouken@1951
   488
slouken@2876
   489
    if (window->flags & (SDL_WINDOW_BORDERLESS | SDL_WINDOW_FULLSCREEN)) {
slouken@1951
   490
        SDL_bool set;
slouken@1951
   491
        Atom WM_HINTS;
slouken@1951
   492
slouken@1951
   493
        /* We haven't modified the window manager hints yet */
slouken@1951
   494
        set = SDL_FALSE;
slouken@1951
   495
slouken@1951
   496
        /* First try to set MWM hints */
slouken@1951
   497
        WM_HINTS = XInternAtom(data->display, "_MOTIF_WM_HINTS", True);
slouken@1951
   498
        if (WM_HINTS != None) {
slouken@1951
   499
            /* Hints used by Motif compliant window managers */
slouken@1951
   500
            struct
slouken@1951
   501
            {
slouken@1951
   502
                unsigned long flags;
slouken@1951
   503
                unsigned long functions;
slouken@1951
   504
                unsigned long decorations;
slouken@1951
   505
                long input_mode;
slouken@1951
   506
                unsigned long status;
slouken@1951
   507
            } MWMHints = {
slouken@1951
   508
            (1L << 1), 0, 0, 0, 0};
slouken@1951
   509
slouken@1951
   510
            XChangeProperty(data->display, w, WM_HINTS, WM_HINTS, 32,
slouken@1951
   511
                            PropModeReplace, (unsigned char *) &MWMHints,
slouken@1951
   512
                            sizeof(MWMHints) / sizeof(long));
slouken@1951
   513
            set = SDL_TRUE;
slouken@1951
   514
        }
slouken@1951
   515
        /* Now try to set KWM hints */
slouken@1951
   516
        WM_HINTS = XInternAtom(data->display, "KWM_WIN_DECORATION", True);
slouken@1951
   517
        if (WM_HINTS != None) {
slouken@1951
   518
            long KWMHints = 0;
slouken@1951
   519
slouken@1951
   520
            XChangeProperty(data->display, w,
slouken@1951
   521
                            WM_HINTS, WM_HINTS, 32,
slouken@1951
   522
                            PropModeReplace,
slouken@1951
   523
                            (unsigned char *) &KWMHints,
slouken@1951
   524
                            sizeof(KWMHints) / sizeof(long));
slouken@1951
   525
            set = SDL_TRUE;
slouken@1951
   526
        }
slouken@1951
   527
        /* Now try to set GNOME hints */
slouken@1951
   528
        WM_HINTS = XInternAtom(data->display, "_WIN_HINTS", True);
slouken@1951
   529
        if (WM_HINTS != None) {
slouken@1951
   530
            long GNOMEHints = 0;
slouken@1951
   531
slouken@1951
   532
            XChangeProperty(data->display, w,
slouken@1951
   533
                            WM_HINTS, WM_HINTS, 32,
slouken@1951
   534
                            PropModeReplace,
slouken@1951
   535
                            (unsigned char *) &GNOMEHints,
slouken@1951
   536
                            sizeof(GNOMEHints) / sizeof(long));
slouken@1951
   537
            set = SDL_TRUE;
slouken@1951
   538
        }
slouken@1951
   539
        /* Finally set the transient hints if necessary */
slouken@1951
   540
        if (!set) {
slouken@1951
   541
            XSetTransientForHint(data->display, w,
slouken@1951
   542
                                 RootWindow(data->display,
slouken@1951
   543
                                            displaydata->screen));
slouken@1951
   544
        }
slouken@1951
   545
    } else {
slouken@1951
   546
        SDL_bool set;
slouken@1951
   547
        Atom WM_HINTS;
slouken@1951
   548
slouken@1951
   549
        /* We haven't modified the window manager hints yet */
slouken@1951
   550
        set = SDL_FALSE;
slouken@1951
   551
slouken@1951
   552
        /* First try to unset MWM hints */
slouken@1951
   553
        WM_HINTS = XInternAtom(data->display, "_MOTIF_WM_HINTS", True);
slouken@1951
   554
        if (WM_HINTS != None) {
slouken@1951
   555
            XDeleteProperty(data->display, w, WM_HINTS);
slouken@1951
   556
            set = SDL_TRUE;
slouken@1951
   557
        }
slouken@1951
   558
        /* Now try to unset KWM hints */
slouken@1951
   559
        WM_HINTS = XInternAtom(data->display, "KWM_WIN_DECORATION", True);
slouken@1951
   560
        if (WM_HINTS != None) {
slouken@1951
   561
            XDeleteProperty(data->display, w, WM_HINTS);
slouken@1951
   562
            set = SDL_TRUE;
slouken@1951
   563
        }
slouken@1951
   564
        /* Now try to unset GNOME hints */
slouken@1951
   565
        WM_HINTS = XInternAtom(data->display, "_WIN_HINTS", True);
slouken@1951
   566
        if (WM_HINTS != None) {
slouken@1951
   567
            XDeleteProperty(data->display, w, WM_HINTS);
slouken@1951
   568
            set = SDL_TRUE;
slouken@1951
   569
        }
slouken@1951
   570
        /* Finally unset the transient hints if necessary */
slouken@1951
   571
        if (!set) {
slouken@1951
   572
            /* NOTE: Does this work? */
slouken@1951
   573
            XSetTransientForHint(data->display, w, None);
slouken@1951
   574
        }
slouken@1951
   575
    }
slouken@1951
   576
slouken@1951
   577
    /* Tell KDE to keep fullscreen windows on top */
slouken@1951
   578
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1951
   579
        XEvent ev;
slouken@1951
   580
        long mask;
slouken@1951
   581
slouken@1951
   582
        SDL_zero(ev);
slouken@1951
   583
        ev.xclient.type = ClientMessage;
slouken@1951
   584
        ev.xclient.window = RootWindow(data->display, displaydata->screen);
slouken@1951
   585
        ev.xclient.message_type =
slouken@1951
   586
            XInternAtom(data->display, "KWM_KEEP_ON_TOP", False);
slouken@1951
   587
        ev.xclient.format = 32;
slouken@1951
   588
        ev.xclient.data.l[0] = w;
slouken@1951
   589
        ev.xclient.data.l[1] = CurrentTime;
slouken@1951
   590
        XSendEvent(data->display,
slouken@1951
   591
                   RootWindow(data->display, displaydata->screen), False,
slouken@1951
   592
                   SubstructureRedirectMask, &ev);
slouken@1951
   593
    }
slouken@1951
   594
slouken@1951
   595
    /* Set the input hints so we get keyboard input */
slouken@1951
   596
    wmhints = XAllocWMHints();
slouken@1951
   597
    if (wmhints) {
slouken@1951
   598
        wmhints->input = True;
slouken@1956
   599
        wmhints->flags = InputHint;
slouken@1951
   600
        XSetWMHints(data->display, w, wmhints);
slouken@1951
   601
        XFree(wmhints);
slouken@1951
   602
    }
slouken@1951
   603
slouken@1951
   604
    /* Set the class hints so we can get an icon (AfterStep) */
slouken@1951
   605
    classhints = XAllocClassHint();
slouken@1951
   606
    if (classhints != NULL) {
slouken@1951
   607
        classhints->res_name = data->classname;
slouken@1951
   608
        classhints->res_class = data->classname;
slouken@1951
   609
        XSetClassHint(data->display, w, classhints);
slouken@1951
   610
        XFree(classhints);
slouken@1951
   611
    }
slouken@1951
   612
slouken@1951
   613
    /* Allow the window to be deleted by the window manager */
slouken@1951
   614
    XSetWMProtocols(data->display, w, &data->WM_DELETE_WINDOW, 1);
slouken@1951
   615
slouken@1951
   616
    if (SetupWindowData(_this, window, w, SDL_TRUE) < 0) {
slouken@1951
   617
        XDestroyWindow(data->display, w);
slouken@1951
   618
        return -1;
slouken@1951
   619
    }
bob@2325
   620
#ifdef X_HAVE_UTF8_STRING
bob@2325
   621
    {
bob@2325
   622
        Uint32 fevent = 0;
bob@2325
   623
        pXGetICValues(((SDL_WindowData *) window->driverdata)->ic,
bob@2325
   624
                      XNFilterEvents, &fevent, NULL);
bob@2325
   625
        XSelectInput(data->display, w,
bob@2325
   626
                     (FocusChangeMask | EnterWindowMask | LeaveWindowMask |
bob@2325
   627
                      ExposureMask | ButtonPressMask | ButtonReleaseMask |
bob@2325
   628
                      PointerMotionMask | KeyPressMask | KeyReleaseMask |
bob@2325
   629
                      PropertyChangeMask | StructureNotifyMask |
bob@2325
   630
                      KeymapStateMask | fevent));
bob@2325
   631
    }
bob@2325
   632
#else
kazeuser@2718
   633
    {
kazeuser@2718
   634
        XSelectInput(data->display, w,
slouken@2725
   635
                     (FocusChangeMask | EnterWindowMask | LeaveWindowMask |
slouken@2725
   636
                      ExposureMask | ButtonPressMask | ButtonReleaseMask |
slouken@2725
   637
                      PointerMotionMask | KeyPressMask | KeyReleaseMask |
slouken@2725
   638
                      PropertyChangeMask | StructureNotifyMask |
slouken@2725
   639
                      KeymapStateMask));
kazeuser@2718
   640
    }
bob@2325
   641
#endif
bob@2325
   642
slouken@2940
   643
#if SDL_VIDEO_DRIVER_X11_XINPUT
slouken@2710
   644
    /* we're informing the display what extension events we want to receive from it */
slouken@2940
   645
    {
slouken@2940
   646
        int i, j, n = 0;
slouken@2940
   647
        XEventClass xevents[256];
slouken@2940
   648
slouken@2940
   649
        for (i = 0; i < SDL_GetNumMice(); ++i) {
slouken@2940
   650
            SDL_Mouse *mouse;
slouken@2940
   651
            X11_MouseData *data;
slouken@2940
   652
slouken@2940
   653
            mouse = SDL_GetMouse(i);
slouken@2942
   654
            data = (X11_MouseData *) mouse->driverdata;
slouken@2940
   655
            if (!data) {
slouken@2940
   656
                continue;
slouken@2940
   657
            }
slouken@2940
   658
slouken@2940
   659
            for (j = 0; j < data->num_xevents; ++j) {
slouken@2940
   660
                xevents[n++] = data->xevents[j];
slouken@2940
   661
            }
slouken@2940
   662
        }
slouken@2940
   663
        if (n > 0) {
slouken@2940
   664
            XSelectExtensionEvent(data->display, w, xevents, n);
slouken@2940
   665
        }
slouken@2940
   666
    }
slouken@2940
   667
#endif
slouken@2710
   668
slouken@1951
   669
    return 0;
slouken@1951
   670
}
slouken@1951
   671
slouken@1951
   672
int
bob@2324
   673
X11_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1951
   674
{
slouken@1951
   675
    Window w = (Window) data;
slouken@1951
   676
slouken@1951
   677
    /* FIXME: Query the title from the existing window */
slouken@1951
   678
slouken@1951
   679
    if (SetupWindowData(_this, window, w, SDL_FALSE) < 0) {
slouken@1951
   680
        return -1;
slouken@1951
   681
    }
slouken@1951
   682
    return 0;
slouken@1951
   683
}
slouken@1951
   684
slouken@1951
   685
void
slouken@1951
   686
X11_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1951
   687
{
slouken@1951
   688
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   689
    Display *display = data->videodata->display;
slouken@1951
   690
    XTextProperty titleprop, iconprop;
slouken@1951
   691
    Status status;
slouken@1951
   692
    const char *title = window->title;
slouken@1951
   693
    const char *icon = NULL;
slouken@1951
   694
slouken@1951
   695
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   696
    Atom _NET_WM_NAME = 0;
slouken@1951
   697
    Atom _NET_WM_ICON_NAME = 0;
slouken@1951
   698
slouken@1951
   699
    /* Look up some useful Atoms */
slouken@1951
   700
    if (SDL_X11_HAVE_UTF8) {
slouken@1951
   701
        _NET_WM_NAME = XInternAtom(display, "_NET_WM_NAME", False);
slouken@1951
   702
        _NET_WM_ICON_NAME = XInternAtom(display, "_NET_WM_ICON_NAME", False);
slouken@1951
   703
    }
slouken@1951
   704
#endif
slouken@1951
   705
slouken@1951
   706
    if (title != NULL) {
slouken@2143
   707
        char *title_locale = SDL_iconv_utf8_locale(title);
slouken@2143
   708
        if (!title_locale) {
slouken@1951
   709
            SDL_OutOfMemory();
slouken@1951
   710
            return;
slouken@1951
   711
        }
slouken@2143
   712
        status = XStringListToTextProperty(&title_locale, 1, &titleprop);
slouken@2143
   713
        SDL_free(title_locale);
slouken@1951
   714
        if (status) {
slouken@1951
   715
            XSetTextProperty(display, data->window, &titleprop, XA_WM_NAME);
slouken@1951
   716
            XFree(titleprop.value);
slouken@1951
   717
        }
slouken@1951
   718
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   719
        if (SDL_X11_HAVE_UTF8) {
slouken@1951
   720
            status =
slouken@1951
   721
                Xutf8TextListToTextProperty(display, (char **) &title, 1,
slouken@1951
   722
                                            XUTF8StringStyle, &titleprop);
slouken@1951
   723
            if (status == Success) {
slouken@1951
   724
                XSetTextProperty(display, data->window, &titleprop,
slouken@1951
   725
                                 _NET_WM_NAME);
slouken@1951
   726
                XFree(titleprop.value);
slouken@1951
   727
            }
slouken@1951
   728
        }
slouken@1951
   729
#endif
slouken@1951
   730
    }
slouken@1951
   731
    if (icon != NULL) {
slouken@2143
   732
        char *icon_locale = SDL_iconv_utf8_locale(icon);
slouken@2143
   733
        if (!icon_locale) {
slouken@1951
   734
            SDL_OutOfMemory();
slouken@1951
   735
            return;
slouken@1951
   736
        }
slouken@2143
   737
        status = XStringListToTextProperty(&icon_locale, 1, &iconprop);
slouken@2143
   738
        SDL_free(icon_locale);
slouken@1951
   739
        if (status) {
slouken@1951
   740
            XSetTextProperty(display, data->window, &iconprop,
slouken@1951
   741
                             XA_WM_ICON_NAME);
slouken@1951
   742
            XFree(iconprop.value);
slouken@1951
   743
        }
slouken@1951
   744
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   745
        if (SDL_X11_HAVE_UTF8) {
slouken@1951
   746
            status =
slouken@1951
   747
                Xutf8TextListToTextProperty(display, (char **) &icon, 1,
slouken@1951
   748
                                            XUTF8StringStyle, &iconprop);
slouken@1951
   749
            if (status == Success) {
slouken@1951
   750
                XSetTextProperty(display, data->window, &iconprop,
slouken@1951
   751
                                 _NET_WM_ICON_NAME);
slouken@1951
   752
                XFree(iconprop.value);
slouken@1951
   753
            }
slouken@1951
   754
        }
slouken@1951
   755
#endif
slouken@1951
   756
    }
slouken@1951
   757
}
slouken@1951
   758
slouken@1951
   759
void
slouken@2967
   760
X11_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slouken@2967
   761
{
slouken@2967
   762
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@2967
   763
    Display *display = data->videodata->display;
slouken@2967
   764
    Atom _NET_WM_ICON = XInternAtom(display, "_NET_WM_ICON", False);
slouken@2967
   765
slouken@2967
   766
    if (icon) {
slouken@2967
   767
        SDL_PixelFormat format;
slouken@2967
   768
        SDL_Surface *surface;
slouken@2967
   769
        int propsize;
slouken@2967
   770
        Uint32 *propdata;
slouken@2967
   771
slouken@2967
   772
        /* Convert the icon to ARGB for modern window managers */
slouken@2990
   773
        SDL_InitFormat(&format, 32, 0x00FF0000, 0x0000FF00, 0x000000FF,
slouken@2990
   774
                       0xFF000000);
slouken@2967
   775
        surface = SDL_ConvertSurface(icon, &format, 0);
slouken@2967
   776
        if (!surface) {
slouken@2967
   777
            return;
slouken@2967
   778
        }
slouken@2967
   779
slouken@2967
   780
        /* Set the _NET_WM_ICON property */
slouken@2990
   781
        propsize = 2 + (icon->w * icon->h);
slouken@2967
   782
        propdata = SDL_malloc(propsize * sizeof(Uint32));
slouken@2967
   783
        if (propdata) {
slouken@2967
   784
            propdata[0] = icon->w;
slouken@2967
   785
            propdata[1] = icon->h;
slouken@2990
   786
            SDL_memcpy(&propdata[2], surface->pixels,
slouken@2990
   787
                       surface->h * surface->pitch);
slouken@2990
   788
            XChangeProperty(display, data->window, _NET_WM_ICON, XA_CARDINAL,
slouken@2990
   789
                            32, PropModeReplace, (unsigned char *) propdata,
slouken@2990
   790
                            propsize);
slouken@2967
   791
        }
slouken@2967
   792
        SDL_FreeSurface(surface);
slouken@2967
   793
    } else {
slouken@2967
   794
        XDeleteProperty(display, data->window, _NET_WM_ICON);
slouken@2967
   795
    }
slouken@2967
   796
}
slouken@2967
   797
slouken@2967
   798
void
slouken@1951
   799
X11_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1951
   800
{
slouken@1951
   801
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   802
    SDL_DisplayData *displaydata =
slouken@1952
   803
        (SDL_DisplayData *) SDL_GetDisplayFromWindow(window)->driverdata;
slouken@1951
   804
    Display *display = data->videodata->display;
slouken@2875
   805
    int x, y;
slouken@1951
   806
slouken@2876
   807
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@2876
   808
        || window->x == SDL_WINDOWPOS_CENTERED) {
slouken@2931
   809
        X11_GetDisplaySize(_this, window, &x, NULL);
slouken@2931
   810
        x = (x - window->w) / 2;
slouken@2875
   811
    } else {
slouken@2875
   812
        x = window->x;
slouken@2875
   813
    }
slouken@2876
   814
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@2876
   815
        || window->y == SDL_WINDOWPOS_CENTERED) {
slouken@2931
   816
        X11_GetDisplaySize(_this, window, NULL, &y);
slouken@2931
   817
        y = (y - window->h) / 2;
slouken@2875
   818
    } else {
slouken@2875
   819
        y = window->y;
slouken@2875
   820
    }
slouken@2875
   821
    XMoveWindow(display, data->window, x, y);
slouken@1951
   822
}
slouken@1951
   823
slouken@1951
   824
void
slouken@1951
   825
X11_SetWindowSize(_THIS, SDL_Window * window)
slouken@1951
   826
{
slouken@1951
   827
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   828
    Display *display = data->videodata->display;
slouken@1951
   829
slouken@2931
   830
    XResizeWindow(display, data->window, window->w, window->h);
slouken@1951
   831
}
slouken@1951
   832
slouken@1951
   833
void
slouken@1951
   834
X11_ShowWindow(_THIS, SDL_Window * window)
slouken@1951
   835
{
slouken@1951
   836
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   837
    Display *display = data->videodata->display;
slouken@1951
   838
slouken@1951
   839
    XMapRaised(display, data->window);
slouken@1951
   840
}
slouken@1951
   841
slouken@1951
   842
void
slouken@1951
   843
X11_HideWindow(_THIS, SDL_Window * window)
slouken@1951
   844
{
slouken@1951
   845
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   846
    Display *display = data->videodata->display;
slouken@1951
   847
slouken@1951
   848
    XUnmapWindow(display, data->window);
slouken@1951
   849
}
slouken@1951
   850
slouken@1951
   851
void
slouken@1951
   852
X11_RaiseWindow(_THIS, SDL_Window * window)
slouken@1951
   853
{
slouken@1951
   854
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   855
    Display *display = data->videodata->display;
slouken@1951
   856
slouken@1951
   857
    XRaiseWindow(display, data->window);
slouken@1951
   858
}
slouken@1951
   859
slouken@1951
   860
void
slouken@1951
   861
X11_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1951
   862
{
slouken@1951
   863
    /* FIXME: is this even possible? */
slouken@1951
   864
}
slouken@1951
   865
slouken@1951
   866
void
slouken@1951
   867
X11_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1951
   868
{
slouken@1951
   869
    X11_HideWindow(_this, window);
slouken@1951
   870
}
slouken@1951
   871
slouken@1951
   872
void
slouken@1951
   873
X11_RestoreWindow(_THIS, SDL_Window * window)
slouken@1951
   874
{
slouken@1951
   875
    X11_ShowWindow(_this, window);
slouken@1951
   876
}
slouken@1951
   877
slouken@1951
   878
void
slouken@1951
   879
X11_SetWindowGrab(_THIS, SDL_Window * window)
slouken@1951
   880
{
slouken@2875
   881
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@2875
   882
    Display *display = data->videodata->display;
slouken@2875
   883
slouken@2876
   884
    if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
slouken@2876
   885
        && (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@2875
   886
        /* Try to grab the mouse */
slouken@2876
   887
        for (;;) {
slouken@2876
   888
            int result =
slouken@2876
   889
                XGrabPointer(display, data->window, True, 0, GrabModeAsync,
slouken@2876
   890
                             GrabModeAsync, data->window, None, CurrentTime);
slouken@2876
   891
            if (result == GrabSuccess) {
slouken@2875
   892
                break;
slouken@2875
   893
            }
slouken@2875
   894
            SDL_Delay(100);
slouken@2875
   895
        }
slouken@2875
   896
slouken@2875
   897
        /* Raise the window if we grab the mouse */
slouken@2875
   898
        XRaiseWindow(display, data->window);
slouken@2875
   899
slouken@2875
   900
        /* Now grab the keyboard */
slouken@2876
   901
        XGrabKeyboard(display, data->window, True, GrabModeAsync,
slouken@2876
   902
                      GrabModeAsync, CurrentTime);
slouken@2875
   903
    } else {
slouken@2875
   904
        XUngrabPointer(display, CurrentTime);
slouken@2875
   905
        XUngrabKeyboard(display, CurrentTime);
slouken@2875
   906
    }
slouken@1951
   907
}
slouken@1951
   908
slouken@1951
   909
void
slouken@1951
   910
X11_DestroyWindow(_THIS, SDL_Window * window)
slouken@1951
   911
{
slouken@1951
   912
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
bob@2323
   913
    window->driverdata = NULL;
slouken@1951
   914
slouken@1951
   915
    if (data) {
bob@2323
   916
        SDL_VideoData *videodata = (SDL_VideoData *) data->videodata;
bob@2323
   917
        Display *display = videodata->display;
bob@2323
   918
        int numwindows = videodata->numwindows;
bob@2323
   919
        SDL_WindowData **windowlist = videodata->windowlist;
bob@2323
   920
        int i;
bob@2323
   921
bob@2323
   922
        if (windowlist) {
bob@2324
   923
            for (i = 0; i < numwindows; ++i) {
bob@2324
   924
                if (windowlist[i] && (windowlist[i]->windowID == window->id)) {
bob@2324
   925
                    windowlist[i] = windowlist[numwindows - 1];
bob@2324
   926
                    windowlist[numwindows - 1] = NULL;
bob@2324
   927
                    videodata->numwindows--;
bob@2324
   928
                    break;
bob@2324
   929
                }
bob@2323
   930
            }
bob@2323
   931
        }
slouken@1951
   932
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   933
        if (data->ic) {
slouken@1951
   934
            XDestroyIC(data->ic);
slouken@1951
   935
        }
slouken@1951
   936
#endif
slouken@1951
   937
        if (data->created) {
slouken@1951
   938
            XDestroyWindow(display, data->window);
slouken@1951
   939
        }
slouken@1951
   940
        SDL_free(data);
slouken@1951
   941
    }
slouken@1951
   942
}
slouken@1951
   943
slouken@1951
   944
SDL_bool
slouken@1951
   945
X11_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1951
   946
{
slouken@1951
   947
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@1951
   948
        /* FIXME! */
slouken@1951
   949
        return SDL_TRUE;
slouken@1951
   950
    } else {
slouken@1951
   951
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1951
   952
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1951
   953
        return SDL_FALSE;
slouken@1951
   954
    }
slouken@1951
   955
}
slouken@1951
   956
slouken@1951
   957
/* vi: set ts=4 sw=4 expandtab: */