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