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