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