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