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