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