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