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