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