src/video/x11/SDL_x11window.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 17 Dec 2008 07:17:54 +0000
changeset 2875 91a7e08cd238
parent 2859 99210400e8b9
child 2876 3fcb0d447bcd
permissions -rw-r--r--
* Implemented X11 fullscreen input grab
* Progress towards being able to toggle in and out of fullscreen mode
slouken@1951
     1
/*
slouken@1951
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 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_syswm.h"
slouken@1951
    25
#include "../SDL_sysvideo.h"
slouken@1951
    26
#include "../../events/SDL_keyboard_c.h"
slouken@1951
    27
slouken@1951
    28
#include "SDL_x11video.h"
bob@2185
    29
#include "../Xext/extensions/StdCmap.h"
slouken@1951
    30
slouken@1951
    31
static int
slouken@1951
    32
SetupWindowData(_THIS, SDL_Window * window, Window w, BOOL created)
slouken@1951
    33
{
slouken@1951
    34
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1951
    35
    SDL_WindowData *data;
slouken@1951
    36
    int numwindows = videodata->numwindows;
bob@2324
    37
    int windowlistlength = videodata->windowlistlength;
slouken@1951
    38
    SDL_WindowData **windowlist = videodata->windowlist;
bob@2323
    39
    int index;
slouken@1951
    40
slouken@1951
    41
    /* Allocate the window data */
bob@2323
    42
    data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
slouken@1951
    43
    if (!data) {
slouken@1951
    44
        SDL_OutOfMemory();
slouken@1951
    45
        return -1;
slouken@1951
    46
    }
slouken@1951
    47
    data->windowID = window->id;
slouken@1951
    48
    data->window = w;
slouken@1951
    49
#ifdef X_HAVE_UTF8_STRING
slouken@1951
    50
    if (SDL_X11_HAVE_UTF8) {
slouken@1951
    51
        data->ic =
slouken@1951
    52
            pXCreateIC(videodata->im, XNClientWindow, w, XNFocusWindow, w,
slouken@1951
    53
                       XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
slouken@1951
    54
                       XNResourceName, videodata->classname, XNResourceClass,
slouken@1951
    55
                       videodata->classname, NULL);
slouken@1951
    56
    }
slouken@1951
    57
#endif
slouken@1951
    58
    data->created = created;
slouken@1951
    59
    data->videodata = videodata;
slouken@1951
    60
bob@2323
    61
    /* Associate the data with the window */
bob@2324
    62
bob@2324
    63
    if (numwindows < windowlistlength) {
bob@2324
    64
        windowlist[numwindows] = data;
bob@2324
    65
        videodata->numwindows++;
bob@2324
    66
    } else {
bob@2324
    67
        windowlist =
bob@2324
    68
            (SDL_WindowData **) SDL_realloc(windowlist,
bob@2324
    69
                                            (numwindows +
bob@2324
    70
                                             1) * sizeof(*windowlist));
bob@2324
    71
        if (!windowlist) {
bob@2324
    72
            SDL_OutOfMemory();
bob@2324
    73
            SDL_free(data);
bob@2324
    74
            return -1;
bob@2323
    75
        }
bob@2324
    76
        windowlist[numwindows] = data;
bob@2324
    77
        videodata->numwindows++;
bob@2324
    78
        videodata->windowlistlength++;
bob@2324
    79
        videodata->windowlist = windowlist;
bob@2323
    80
    }
bob@2323
    81
slouken@1951
    82
    /* Fill in the SDL window with the window data */
slouken@1951
    83
    {
slouken@1951
    84
        XWindowAttributes attrib;
slouken@1951
    85
slouken@1951
    86
        XGetWindowAttributes(data->videodata->display, w, &attrib);
slouken@1951
    87
        window->x = attrib.x;
slouken@1951
    88
        window->y = attrib.y;
slouken@1951
    89
        window->w = attrib.width;
slouken@1951
    90
        window->h = attrib.height;
slouken@1951
    91
        if (attrib.map_state != IsUnmapped) {
slouken@1951
    92
            window->flags |= SDL_WINDOW_SHOWN;
slouken@1951
    93
        } else {
slouken@1951
    94
            window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1951
    95
        }
slouken@1951
    96
    }
slouken@1951
    97
    /* FIXME: How can I tell?
slouken@1951
    98
       {
slouken@1951
    99
       DWORD style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1951
   100
       if (style & WS_VISIBLE) {
slouken@1951
   101
       if (style & (WS_BORDER | WS_THICKFRAME)) {
slouken@1951
   102
       window->flags &= ~SDL_WINDOW_BORDERLESS;
slouken@1951
   103
       } else {
slouken@1951
   104
       window->flags |= SDL_WINDOW_BORDERLESS;
slouken@1951
   105
       }
slouken@1951
   106
       if (style & WS_THICKFRAME) {
slouken@1951
   107
       window->flags |= SDL_WINDOW_RESIZABLE;
slouken@1951
   108
       } else {
slouken@1951
   109
       window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@1951
   110
       }
slouken@1951
   111
       if (style & WS_MAXIMIZE) {
slouken@1951
   112
       window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1951
   113
       } else {
slouken@1951
   114
       window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@1951
   115
       }
slouken@1951
   116
       if (style & WS_MINIMIZE) {
slouken@1951
   117
       window->flags |= SDL_WINDOW_MINIMIZED;
slouken@1951
   118
       } else {
slouken@1951
   119
       window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@1951
   120
       }
slouken@1951
   121
       }
slouken@1951
   122
       if (GetFocus() == hwnd) {
slouken@1951
   123
       int index = data->videodata->keyboard;
slouken@1951
   124
       window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@1951
   125
       SDL_SetKeyboardFocus(index, data->windowID);
slouken@1951
   126
slouken@1951
   127
       if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1951
   128
       RECT rect;
slouken@1951
   129
       GetClientRect(hwnd, &rect);
slouken@1951
   130
       ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1951
   131
       ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1951
   132
       ClipCursor(&rect);
slouken@1951
   133
       }
slouken@1951
   134
       }
slouken@1951
   135
     */
slouken@1951
   136
slouken@1951
   137
    /* All done! */
slouken@1951
   138
    window->driverdata = data;
slouken@1951
   139
    return 0;
slouken@1951
   140
}
slouken@1951
   141
slouken@1951
   142
int
slouken@1951
   143
X11_CreateWindow(_THIS, SDL_Window * window)
slouken@1951
   144
{
slouken@1951
   145
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   146
    SDL_DisplayData *displaydata =
slouken@1952
   147
        (SDL_DisplayData *) SDL_GetDisplayFromWindow(window)->driverdata;
slouken@1951
   148
    Visual *visual;
slouken@1951
   149
    int depth;
slouken@1951
   150
    XSetWindowAttributes xattr;
slouken@1951
   151
    int x, y;
slouken@1951
   152
    Window w;
slouken@1951
   153
    XSizeHints *sizehints;
slouken@1951
   154
    XWMHints *wmhints;
slouken@1951
   155
    XClassHint *classhints;
slouken@2710
   156
    extern XEventClass SDL_XEvents[];
slouken@2710
   157
    extern int SDL_NumOfXEvents;
slouken@1951
   158
slouken@1951
   159
#if SDL_VIDEO_DRIVER_X11_XINERAMA
slouken@1951
   160
/* FIXME
slouken@1951
   161
    if ( use_xinerama ) {
slouken@1951
   162
        x = xinerama_info.x_org;
slouken@1951
   163
        y = xinerama_info.y_org;
slouken@1951
   164
    }
slouken@1951
   165
*/
slouken@1951
   166
#endif
slouken@1952
   167
#ifdef SDL_VIDEO_OPENGL_GLX
slouken@1951
   168
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@1952
   169
        XVisualInfo *vinfo;
slouken@1952
   170
slouken@1952
   171
        if (X11_GL_Initialize(_this) < 0) {
slouken@1952
   172
            return -1;
slouken@1952
   173
        }
slouken@1952
   174
        vinfo = X11_GL_GetVisual(_this, data->display, displaydata->screen);
slouken@1952
   175
        if (!vinfo) {
slouken@1952
   176
            return -1;
slouken@1952
   177
        }
slouken@1952
   178
        visual = vinfo->visual;
slouken@1952
   179
        depth = vinfo->depth;
slouken@1952
   180
        XFree(vinfo);
slouken@1952
   181
    } else
slouken@1952
   182
#endif
slouken@1952
   183
    {
slouken@1951
   184
        visual = displaydata->visual;
slouken@1951
   185
        depth = displaydata->depth;
slouken@1951
   186
    }
slouken@1951
   187
slouken@1951
   188
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1951
   189
        xattr.override_redirect = True;
slouken@1951
   190
    } else {
slouken@1951
   191
        xattr.override_redirect = False;
slouken@1951
   192
    }
slouken@1951
   193
    xattr.background_pixel = 0;
slouken@1951
   194
    xattr.border_pixel = 0;
bob@2214
   195
bob@2211
   196
    if (visual->class == DirectColor || visual->class == PseudoColor) {
bob@2185
   197
        int nmaps;
bob@2214
   198
        XStandardColormap cmap;
bob@2185
   199
        XStandardColormap *stdmaps;
bob@2214
   200
        XColor *colorcells;
bob@2214
   201
        Colormap colormap;
bob@2185
   202
        Bool found = False;
bob@2214
   203
        int i;
bob@2214
   204
        int ncolors;
bob@2214
   205
        int rmax, gmax, bmax;
bob@2214
   206
        int rmul, gmul, bmul;
bob@2185
   207
bob@2214
   208
        if (colormap =
bob@2214
   209
            X11_LookupColormap(data->display, displaydata->screen,
bob@2214
   210
                               visual->visualid)) {
bob@2214
   211
            xattr.colormap = colormap;
bob@2214
   212
        } else {
bob@2214
   213
            /* check to see if the colormap we need already exists */
bob@2214
   214
            if (0 != XGetRGBColormaps(data->display,
bob@2214
   215
                                      RootWindow(data->display,
bob@2214
   216
                                                 displaydata->screen),
bob@2214
   217
                                      &stdmaps, &nmaps, XA_RGB_BEST_MAP)) {
bob@2214
   218
                for (i = 0; i < nmaps; i++) {
bob@2214
   219
                    if (stdmaps[i].visualid == visual->visualid) {
bob@2214
   220
                        SDL_memcpy(&cmap, &stdmaps[i],
bob@2214
   221
                                   sizeof(XStandardColormap));
bob@2214
   222
                        found = True;
bob@2214
   223
                        break;
bob@2214
   224
                    }
bob@2185
   225
                }
bob@2214
   226
                XFree(stdmaps);
bob@2185
   227
            }
bob@2213
   228
bob@2214
   229
            /* it doesn't exist, so create it */
bob@2214
   230
            if (!found) {
bob@2214
   231
                int max = visual->map_entries - 1;
bob@2214
   232
                stdmaps =
bob@2214
   233
                    XmuStandardColormap(data->display, displaydata->screen,
bob@2214
   234
                                        visual->visualid, depth,
bob@2214
   235
                                        XA_RGB_BEST_MAP, None, max, max, max);
bob@2214
   236
                if (NULL == stdmaps || stdmaps->visualid != visual->visualid) {
bob@2214
   237
                    SDL_SetError
bob@2214
   238
                        ("Couldn't create window:XA_RGB_BEST_MAP not found and could not be created");
bob@2214
   239
                    return -1;
bob@2214
   240
                }
bob@2214
   241
                SDL_memcpy(&cmap, stdmaps, sizeof(XStandardColormap));
bob@2322
   242
                XFree(stdmaps);
bob@2214
   243
            }
bob@2214
   244
bob@2214
   245
            /* OK, we have the best color map, now copy it for use by the
bob@2214
   246
               program */
bob@2214
   247
bob@2214
   248
            colorcells = SDL_malloc(visual->map_entries * sizeof(XColor));
bob@2214
   249
            if (NULL == colorcells) {
bob@2214
   250
                SDL_SetError("out of memory in X11_CreateWindow");
bob@2185
   251
                return -1;
bob@2185
   252
            }
bob@2214
   253
            ncolors = visual->map_entries;
bob@2214
   254
            rmax = cmap.red_max + 1;
bob@2214
   255
            gmax = cmap.blue_max + 1;
bob@2214
   256
            bmax = cmap.green_max + 1;
bob@2214
   257
bob@2214
   258
            rmul = cmap.red_mult;
bob@2214
   259
            gmul = cmap.blue_mult;
bob@2214
   260
            bmul = cmap.green_mult;
bob@2214
   261
bob@2214
   262
            /* build the color table pixel values */
bob@2214
   263
            for (i = 0; i < ncolors; i++) {
bob@2214
   264
                Uint32 red = (rmax * i) / ncolors;
bob@2214
   265
                Uint32 green = (gmax * i) / ncolors;
bob@2214
   266
                Uint32 blue = (bmax * i) / ncolors;
bob@2214
   267
bob@2214
   268
                colorcells[i].pixel =
bob@2214
   269
                    (red * rmul) | (green * gmul) | (blue * bmul);
bob@2214
   270
            }
bob@2214
   271
            XQueryColors(data->display, cmap.colormap, colorcells, ncolors);
bob@2214
   272
            colormap = XCreateColormap(data->display,
bob@2214
   273
                                       RootWindow(data->display,
bob@2214
   274
                                                  displaydata->screen),
bob@2214
   275
                                       visual, AllocAll);
bob@2214
   276
            XStoreColors(data->display, colormap, colorcells, ncolors);
bob@2214
   277
            SDL_free(colorcells);
bob@2214
   278
bob@2214
   279
            xattr.colormap = colormap;
bob@2214
   280
            X11_TrackColormap(data->display, displaydata->screen, colormap,
bob@2214
   281
                              &cmap, visual);
bob@2185
   282
        }
slouken@1951
   283
    } else {
slouken@1951
   284
        xattr.colormap =
slouken@1951
   285
            XCreateColormap(data->display,
slouken@1951
   286
                            RootWindow(data->display, displaydata->screen),
slouken@1951
   287
                            visual, AllocNone);
slouken@1951
   288
    }
slouken@1951
   289
slouken@2875
   290
    if ((window->flags & SDL_WINDOW_FULLSCREEN) || window->x == SDL_WINDOWPOS_CENTERED) {
slouken@1951
   291
        x = (DisplayWidth(data->display, displaydata->screen) -
slouken@1951
   292
             window->w) / 2;
slouken@1951
   293
    } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
slouken@1951
   294
        x = 0;
slouken@1951
   295
    } else {
slouken@1951
   296
        x = window->x;
slouken@1951
   297
    }
slouken@2875
   298
    if ((window->flags & SDL_WINDOW_FULLSCREEN) || window->y == SDL_WINDOWPOS_CENTERED) {
slouken@1951
   299
        y = (DisplayHeight(data->display, displaydata->screen) -
slouken@1951
   300
             window->h) / 2;
slouken@1951
   301
    } else if (window->y == SDL_WINDOWPOS_UNDEFINED) {
slouken@1951
   302
        y = 0;
slouken@1951
   303
    } else {
slouken@1951
   304
        y = window->y;
slouken@1951
   305
    }
slouken@1951
   306
slouken@1951
   307
    w = XCreateWindow(data->display,
slouken@1951
   308
                      RootWindow(data->display, displaydata->screen), x, y,
slouken@1951
   309
                      window->w, window->h, 0, depth, InputOutput, visual,
slouken@1951
   310
                      (CWOverrideRedirect | CWBackPixel | CWBorderPixel |
slouken@1951
   311
                       CWColormap), &xattr);
slouken@1952
   312
    if (!w) {
slouken@1952
   313
#ifdef SDL_VIDEO_OPENGL_GLX
slouken@1952
   314
        if (window->flags & SDL_WINDOW_OPENGL) {
slouken@1952
   315
            X11_GL_Shutdown(_this);
slouken@1952
   316
        }
slouken@1952
   317
#endif
slouken@1952
   318
        SDL_SetError("Couldn't create window");
slouken@1952
   319
        return -1;
slouken@1952
   320
    }
slouken@1951
   321
slouken@1951
   322
    sizehints = XAllocSizeHints();
slouken@1951
   323
    if (sizehints) {
slouken@2875
   324
        if ((window->flags & SDL_WINDOW_RESIZABLE) && !(window->flags & SDL_WINDOW_FULLSCREEN)) {
slouken@1951
   325
            sizehints->min_width = 32;
slouken@1951
   326
            sizehints->min_height = 32;
slouken@1951
   327
            sizehints->max_height = 4096;
slouken@1951
   328
            sizehints->max_width = 4096;
slouken@1951
   329
        } else {
slouken@1951
   330
            sizehints->min_width = sizehints->max_width = window->w;
slouken@1951
   331
            sizehints->min_height = sizehints->max_height = window->h;
slouken@1951
   332
        }
slouken@1951
   333
        sizehints->flags = PMaxSize | PMinSize;
slouken@1951
   334
        if (!(window->flags & SDL_WINDOW_FULLSCREEN)
slouken@1951
   335
            && window->x != SDL_WINDOWPOS_UNDEFINED
slouken@1951
   336
            && window->y != SDL_WINDOWPOS_UNDEFINED) {
slouken@1951
   337
            sizehints->x = x;
slouken@1951
   338
            sizehints->y = y;
slouken@1951
   339
            sizehints->flags |= USPosition;
slouken@1951
   340
        }
slouken@1951
   341
        XSetWMNormalHints(data->display, w, sizehints);
slouken@1951
   342
        XFree(sizehints);
slouken@1951
   343
    }
slouken@1951
   344
slouken@2875
   345
    if (window->flags & (SDL_WINDOW_BORDERLESS|SDL_WINDOW_FULLSCREEN)) {
slouken@1951
   346
        SDL_bool set;
slouken@1951
   347
        Atom WM_HINTS;
slouken@1951
   348
slouken@1951
   349
        /* We haven't modified the window manager hints yet */
slouken@1951
   350
        set = SDL_FALSE;
slouken@1951
   351
slouken@1951
   352
        /* First try to set MWM hints */
slouken@1951
   353
        WM_HINTS = XInternAtom(data->display, "_MOTIF_WM_HINTS", True);
slouken@1951
   354
        if (WM_HINTS != None) {
slouken@1951
   355
            /* Hints used by Motif compliant window managers */
slouken@1951
   356
            struct
slouken@1951
   357
            {
slouken@1951
   358
                unsigned long flags;
slouken@1951
   359
                unsigned long functions;
slouken@1951
   360
                unsigned long decorations;
slouken@1951
   361
                long input_mode;
slouken@1951
   362
                unsigned long status;
slouken@1951
   363
            } MWMHints = {
slouken@1951
   364
            (1L << 1), 0, 0, 0, 0};
slouken@1951
   365
slouken@1951
   366
            XChangeProperty(data->display, w, WM_HINTS, WM_HINTS, 32,
slouken@1951
   367
                            PropModeReplace, (unsigned char *) &MWMHints,
slouken@1951
   368
                            sizeof(MWMHints) / sizeof(long));
slouken@1951
   369
            set = SDL_TRUE;
slouken@1951
   370
        }
slouken@1951
   371
        /* Now try to set KWM hints */
slouken@1951
   372
        WM_HINTS = XInternAtom(data->display, "KWM_WIN_DECORATION", True);
slouken@1951
   373
        if (WM_HINTS != None) {
slouken@1951
   374
            long KWMHints = 0;
slouken@1951
   375
slouken@1951
   376
            XChangeProperty(data->display, w,
slouken@1951
   377
                            WM_HINTS, WM_HINTS, 32,
slouken@1951
   378
                            PropModeReplace,
slouken@1951
   379
                            (unsigned char *) &KWMHints,
slouken@1951
   380
                            sizeof(KWMHints) / sizeof(long));
slouken@1951
   381
            set = SDL_TRUE;
slouken@1951
   382
        }
slouken@1951
   383
        /* Now try to set GNOME hints */
slouken@1951
   384
        WM_HINTS = XInternAtom(data->display, "_WIN_HINTS", True);
slouken@1951
   385
        if (WM_HINTS != None) {
slouken@1951
   386
            long GNOMEHints = 0;
slouken@1951
   387
slouken@1951
   388
            XChangeProperty(data->display, w,
slouken@1951
   389
                            WM_HINTS, WM_HINTS, 32,
slouken@1951
   390
                            PropModeReplace,
slouken@1951
   391
                            (unsigned char *) &GNOMEHints,
slouken@1951
   392
                            sizeof(GNOMEHints) / sizeof(long));
slouken@1951
   393
            set = SDL_TRUE;
slouken@1951
   394
        }
slouken@1951
   395
        /* Finally set the transient hints if necessary */
slouken@1951
   396
        if (!set) {
slouken@1951
   397
            XSetTransientForHint(data->display, w,
slouken@1951
   398
                                 RootWindow(data->display,
slouken@1951
   399
                                            displaydata->screen));
slouken@1951
   400
        }
slouken@1951
   401
    } else {
slouken@1951
   402
        SDL_bool set;
slouken@1951
   403
        Atom WM_HINTS;
slouken@1951
   404
slouken@1951
   405
        /* We haven't modified the window manager hints yet */
slouken@1951
   406
        set = SDL_FALSE;
slouken@1951
   407
slouken@1951
   408
        /* First try to unset MWM hints */
slouken@1951
   409
        WM_HINTS = XInternAtom(data->display, "_MOTIF_WM_HINTS", True);
slouken@1951
   410
        if (WM_HINTS != None) {
slouken@1951
   411
            XDeleteProperty(data->display, w, WM_HINTS);
slouken@1951
   412
            set = SDL_TRUE;
slouken@1951
   413
        }
slouken@1951
   414
        /* Now try to unset KWM hints */
slouken@1951
   415
        WM_HINTS = XInternAtom(data->display, "KWM_WIN_DECORATION", True);
slouken@1951
   416
        if (WM_HINTS != None) {
slouken@1951
   417
            XDeleteProperty(data->display, w, WM_HINTS);
slouken@1951
   418
            set = SDL_TRUE;
slouken@1951
   419
        }
slouken@1951
   420
        /* Now try to unset GNOME hints */
slouken@1951
   421
        WM_HINTS = XInternAtom(data->display, "_WIN_HINTS", True);
slouken@1951
   422
        if (WM_HINTS != None) {
slouken@1951
   423
            XDeleteProperty(data->display, w, WM_HINTS);
slouken@1951
   424
            set = SDL_TRUE;
slouken@1951
   425
        }
slouken@1951
   426
        /* Finally unset the transient hints if necessary */
slouken@1951
   427
        if (!set) {
slouken@1951
   428
            /* NOTE: Does this work? */
slouken@1951
   429
            XSetTransientForHint(data->display, w, None);
slouken@1951
   430
        }
slouken@1951
   431
    }
slouken@1951
   432
slouken@1951
   433
    /* Tell KDE to keep fullscreen windows on top */
slouken@1951
   434
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1951
   435
        XEvent ev;
slouken@1951
   436
        long mask;
slouken@1951
   437
slouken@1951
   438
        SDL_zero(ev);
slouken@1951
   439
        ev.xclient.type = ClientMessage;
slouken@1951
   440
        ev.xclient.window = RootWindow(data->display, displaydata->screen);
slouken@1951
   441
        ev.xclient.message_type =
slouken@1951
   442
            XInternAtom(data->display, "KWM_KEEP_ON_TOP", False);
slouken@1951
   443
        ev.xclient.format = 32;
slouken@1951
   444
        ev.xclient.data.l[0] = w;
slouken@1951
   445
        ev.xclient.data.l[1] = CurrentTime;
slouken@1951
   446
        XSendEvent(data->display,
slouken@1951
   447
                   RootWindow(data->display, displaydata->screen), False,
slouken@1951
   448
                   SubstructureRedirectMask, &ev);
slouken@1951
   449
    }
slouken@1951
   450
slouken@1951
   451
    /* Set the input hints so we get keyboard input */
slouken@1951
   452
    wmhints = XAllocWMHints();
slouken@1951
   453
    if (wmhints) {
slouken@1951
   454
        wmhints->input = True;
slouken@1956
   455
        wmhints->flags = InputHint;
slouken@1951
   456
        XSetWMHints(data->display, w, wmhints);
slouken@1951
   457
        XFree(wmhints);
slouken@1951
   458
    }
slouken@1951
   459
slouken@1951
   460
    /* Set the class hints so we can get an icon (AfterStep) */
slouken@1951
   461
    classhints = XAllocClassHint();
slouken@1951
   462
    if (classhints != NULL) {
slouken@1951
   463
        classhints->res_name = data->classname;
slouken@1951
   464
        classhints->res_class = data->classname;
slouken@1951
   465
        XSetClassHint(data->display, w, classhints);
slouken@1951
   466
        XFree(classhints);
slouken@1951
   467
    }
slouken@1951
   468
slouken@1951
   469
    /* Allow the window to be deleted by the window manager */
slouken@1951
   470
    XSetWMProtocols(data->display, w, &data->WM_DELETE_WINDOW, 1);
slouken@1951
   471
slouken@1951
   472
    if (SetupWindowData(_this, window, w, SDL_TRUE) < 0) {
slouken@1952
   473
#ifdef SDL_VIDEO_OPENGL_GLX
slouken@1952
   474
        if (window->flags & SDL_WINDOW_OPENGL) {
slouken@1952
   475
            X11_GL_Shutdown(_this);
slouken@1952
   476
        }
slouken@1952
   477
#endif
slouken@1951
   478
        XDestroyWindow(data->display, w);
slouken@1951
   479
        return -1;
slouken@1951
   480
    }
bob@2325
   481
#ifdef X_HAVE_UTF8_STRING
bob@2325
   482
    {
bob@2325
   483
        Uint32 fevent = 0;
bob@2325
   484
        pXGetICValues(((SDL_WindowData *) window->driverdata)->ic,
bob@2325
   485
                      XNFilterEvents, &fevent, NULL);
bob@2325
   486
        XSelectInput(data->display, w,
bob@2325
   487
                     (FocusChangeMask | EnterWindowMask | LeaveWindowMask |
bob@2325
   488
                      ExposureMask | ButtonPressMask | ButtonReleaseMask |
bob@2325
   489
                      PointerMotionMask | KeyPressMask | KeyReleaseMask |
bob@2325
   490
                      PropertyChangeMask | StructureNotifyMask |
bob@2325
   491
                      KeymapStateMask | fevent));
bob@2325
   492
    }
bob@2325
   493
#else
kazeuser@2718
   494
    {
kazeuser@2718
   495
        XSelectInput(data->display, w,
slouken@2725
   496
                     (FocusChangeMask | EnterWindowMask | LeaveWindowMask |
slouken@2725
   497
                      ExposureMask | ButtonPressMask | ButtonReleaseMask |
slouken@2725
   498
                      PointerMotionMask | KeyPressMask | KeyReleaseMask |
slouken@2725
   499
                      PropertyChangeMask | StructureNotifyMask |
slouken@2725
   500
                      KeymapStateMask));
kazeuser@2718
   501
    }
bob@2325
   502
#endif
bob@2325
   503
slouken@2710
   504
    /* we're informing the display what extension events we want to receive from it */
slouken@2710
   505
    XSelectExtensionEvent(data->display, w, SDL_XEvents, SDL_NumOfXEvents);
slouken@2710
   506
slouken@1951
   507
    return 0;
slouken@1951
   508
}
slouken@1951
   509
slouken@1951
   510
int
bob@2324
   511
X11_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1951
   512
{
slouken@1951
   513
    Window w = (Window) data;
slouken@1951
   514
slouken@1951
   515
    /* FIXME: Query the title from the existing window */
slouken@1951
   516
slouken@1951
   517
    if (SetupWindowData(_this, window, w, SDL_FALSE) < 0) {
slouken@1951
   518
        return -1;
slouken@1951
   519
    }
slouken@1951
   520
    return 0;
slouken@1951
   521
}
slouken@1951
   522
slouken@1951
   523
void
slouken@1951
   524
X11_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1951
   525
{
slouken@1951
   526
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   527
    Display *display = data->videodata->display;
slouken@1951
   528
    XTextProperty titleprop, iconprop;
slouken@1951
   529
    Status status;
slouken@1951
   530
    const char *title = window->title;
slouken@1951
   531
    const char *icon = NULL;
slouken@1951
   532
slouken@1951
   533
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   534
    Atom _NET_WM_NAME = 0;
slouken@1951
   535
    Atom _NET_WM_ICON_NAME = 0;
slouken@1951
   536
slouken@1951
   537
    /* Look up some useful Atoms */
slouken@1951
   538
    if (SDL_X11_HAVE_UTF8) {
slouken@1951
   539
        _NET_WM_NAME = XInternAtom(display, "_NET_WM_NAME", False);
slouken@1951
   540
        _NET_WM_ICON_NAME = XInternAtom(display, "_NET_WM_ICON_NAME", False);
slouken@1951
   541
    }
slouken@1951
   542
#endif
slouken@1951
   543
slouken@1951
   544
    if (title != NULL) {
slouken@2143
   545
        char *title_locale = SDL_iconv_utf8_locale(title);
slouken@2143
   546
        if (!title_locale) {
slouken@1951
   547
            SDL_OutOfMemory();
slouken@1951
   548
            return;
slouken@1951
   549
        }
slouken@2143
   550
        status = XStringListToTextProperty(&title_locale, 1, &titleprop);
slouken@2143
   551
        SDL_free(title_locale);
slouken@1951
   552
        if (status) {
slouken@1951
   553
            XSetTextProperty(display, data->window, &titleprop, XA_WM_NAME);
slouken@1951
   554
            XFree(titleprop.value);
slouken@1951
   555
        }
slouken@1951
   556
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   557
        if (SDL_X11_HAVE_UTF8) {
slouken@1951
   558
            status =
slouken@1951
   559
                Xutf8TextListToTextProperty(display, (char **) &title, 1,
slouken@1951
   560
                                            XUTF8StringStyle, &titleprop);
slouken@1951
   561
            if (status == Success) {
slouken@1951
   562
                XSetTextProperty(display, data->window, &titleprop,
slouken@1951
   563
                                 _NET_WM_NAME);
slouken@1951
   564
                XFree(titleprop.value);
slouken@1951
   565
            }
slouken@1951
   566
        }
slouken@1951
   567
#endif
slouken@1951
   568
    }
slouken@1951
   569
    if (icon != NULL) {
slouken@2143
   570
        char *icon_locale = SDL_iconv_utf8_locale(icon);
slouken@2143
   571
        if (!icon_locale) {
slouken@1951
   572
            SDL_OutOfMemory();
slouken@1951
   573
            return;
slouken@1951
   574
        }
slouken@2143
   575
        status = XStringListToTextProperty(&icon_locale, 1, &iconprop);
slouken@2143
   576
        SDL_free(icon_locale);
slouken@1951
   577
        if (status) {
slouken@1951
   578
            XSetTextProperty(display, data->window, &iconprop,
slouken@1951
   579
                             XA_WM_ICON_NAME);
slouken@1951
   580
            XFree(iconprop.value);
slouken@1951
   581
        }
slouken@1951
   582
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   583
        if (SDL_X11_HAVE_UTF8) {
slouken@1951
   584
            status =
slouken@1951
   585
                Xutf8TextListToTextProperty(display, (char **) &icon, 1,
slouken@1951
   586
                                            XUTF8StringStyle, &iconprop);
slouken@1951
   587
            if (status == Success) {
slouken@1951
   588
                XSetTextProperty(display, data->window, &iconprop,
slouken@1951
   589
                                 _NET_WM_ICON_NAME);
slouken@1951
   590
                XFree(iconprop.value);
slouken@1951
   591
            }
slouken@1951
   592
        }
slouken@1951
   593
#endif
slouken@1951
   594
    }
slouken@1951
   595
}
slouken@1951
   596
slouken@1951
   597
void
slouken@1951
   598
X11_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1951
   599
{
slouken@1951
   600
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   601
    SDL_DisplayData *displaydata =
slouken@1952
   602
        (SDL_DisplayData *) SDL_GetDisplayFromWindow(window)->driverdata;
slouken@1951
   603
    Display *display = data->videodata->display;
slouken@2875
   604
    int x, y;
slouken@1951
   605
slouken@2875
   606
    if ((window->flags & SDL_WINDOW_FULLSCREEN) || window->x == SDL_WINDOWPOS_CENTERED) {
slouken@2875
   607
        x = (DisplayWidth(display, displaydata->screen) - window->w) / 2;
slouken@2875
   608
    } else {
slouken@2875
   609
        x = window->x;
slouken@2875
   610
    }
slouken@2875
   611
    if ((window->flags & SDL_WINDOW_FULLSCREEN) || window->y == SDL_WINDOWPOS_CENTERED) {
slouken@2875
   612
        y = (DisplayHeight(display, displaydata->screen) - window->h) / 2;
slouken@2875
   613
    } else {
slouken@2875
   614
        y = window->y;
slouken@2875
   615
    }
slouken@2875
   616
    XMoveWindow(display, data->window, x, y);
slouken@1951
   617
}
slouken@1951
   618
slouken@1951
   619
void
slouken@1951
   620
X11_SetWindowSize(_THIS, SDL_Window * window)
slouken@1951
   621
{
slouken@1951
   622
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   623
    Display *display = data->videodata->display;
slouken@1951
   624
slouken@1951
   625
    XMoveWindow(display, data->window, window->w, window->h);
slouken@1951
   626
}
slouken@1951
   627
slouken@1951
   628
void
slouken@1951
   629
X11_ShowWindow(_THIS, SDL_Window * window)
slouken@1951
   630
{
slouken@1951
   631
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   632
    Display *display = data->videodata->display;
slouken@1951
   633
slouken@1951
   634
    XMapRaised(display, data->window);
slouken@1951
   635
}
slouken@1951
   636
slouken@1951
   637
void
slouken@1951
   638
X11_HideWindow(_THIS, SDL_Window * window)
slouken@1951
   639
{
slouken@1951
   640
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   641
    Display *display = data->videodata->display;
slouken@1951
   642
slouken@1951
   643
    XUnmapWindow(display, data->window);
slouken@1951
   644
}
slouken@1951
   645
slouken@1951
   646
void
slouken@1951
   647
X11_RaiseWindow(_THIS, SDL_Window * window)
slouken@1951
   648
{
slouken@1951
   649
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1951
   650
    Display *display = data->videodata->display;
slouken@1951
   651
slouken@1951
   652
    XRaiseWindow(display, data->window);
slouken@1951
   653
}
slouken@1951
   654
slouken@1951
   655
void
slouken@1951
   656
X11_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1951
   657
{
slouken@1951
   658
    /* FIXME: is this even possible? */
slouken@1951
   659
}
slouken@1951
   660
slouken@1951
   661
void
slouken@1951
   662
X11_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1951
   663
{
slouken@1951
   664
    X11_HideWindow(_this, window);
slouken@1951
   665
}
slouken@1951
   666
slouken@1951
   667
void
slouken@1951
   668
X11_RestoreWindow(_THIS, SDL_Window * window)
slouken@1951
   669
{
slouken@1951
   670
    X11_ShowWindow(_this, window);
slouken@1951
   671
}
slouken@1951
   672
slouken@1951
   673
void
slouken@1951
   674
X11_SetWindowGrab(_THIS, SDL_Window * window)
slouken@1951
   675
{
slouken@2875
   676
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@2875
   677
    Display *display = data->videodata->display;
slouken@2875
   678
slouken@2875
   679
    if ((window->flags & (SDL_WINDOW_INPUT_GRABBED|SDL_WINDOW_FULLSCREEN)) &&
slouken@2875
   680
        (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@2875
   681
        /* Try to grab the mouse */
slouken@2875
   682
        for ( ; ; ) {
slouken@2875
   683
            int result = XGrabPointer(display, data->window, True, 0, GrabModeAsync, GrabModeAsync, data->window, None, CurrentTime);
slouken@2875
   684
            if ( result == GrabSuccess ) {
slouken@2875
   685
                break;
slouken@2875
   686
            }
slouken@2875
   687
            SDL_Delay(100);
slouken@2875
   688
        }
slouken@2875
   689
slouken@2875
   690
        /* Raise the window if we grab the mouse */
slouken@2875
   691
        XRaiseWindow(display, data->window);
slouken@2875
   692
slouken@2875
   693
        /* Now grab the keyboard */
slouken@2875
   694
        XGrabKeyboard(display, data->window, True, GrabModeAsync, GrabModeAsync, CurrentTime);
slouken@2875
   695
    } else {
slouken@2875
   696
        XUngrabPointer(display, CurrentTime);
slouken@2875
   697
        XUngrabKeyboard(display, CurrentTime);
slouken@2875
   698
    }
slouken@1951
   699
}
slouken@1951
   700
slouken@1951
   701
void
slouken@1951
   702
X11_DestroyWindow(_THIS, SDL_Window * window)
slouken@1951
   703
{
slouken@1951
   704
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
bob@2323
   705
    window->driverdata = NULL;
slouken@1951
   706
slouken@1951
   707
    if (data) {
bob@2323
   708
        SDL_VideoData *videodata = (SDL_VideoData *) data->videodata;
bob@2323
   709
        Display *display = videodata->display;
bob@2323
   710
        int numwindows = videodata->numwindows;
bob@2323
   711
        SDL_WindowData **windowlist = videodata->windowlist;
bob@2323
   712
        int i;
bob@2323
   713
bob@2323
   714
        if (windowlist) {
bob@2324
   715
            for (i = 0; i < numwindows; ++i) {
bob@2324
   716
                if (windowlist[i] && (windowlist[i]->windowID == window->id)) {
bob@2324
   717
                    windowlist[i] = windowlist[numwindows - 1];
bob@2324
   718
                    windowlist[numwindows - 1] = NULL;
bob@2324
   719
                    videodata->numwindows--;
bob@2324
   720
                    break;
bob@2324
   721
                }
bob@2323
   722
            }
bob@2323
   723
        }
slouken@1952
   724
#ifdef SDL_VIDEO_OPENGL_GLX
slouken@1952
   725
        if (window->flags & SDL_WINDOW_OPENGL) {
slouken@1952
   726
            X11_GL_Shutdown(_this);
slouken@1952
   727
        }
slouken@1951
   728
#endif
slouken@1951
   729
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   730
        if (data->ic) {
slouken@1951
   731
            XDestroyIC(data->ic);
slouken@1951
   732
        }
slouken@1951
   733
#endif
slouken@1951
   734
        if (data->created) {
slouken@1951
   735
            XDestroyWindow(display, data->window);
slouken@1951
   736
        }
slouken@1951
   737
        SDL_free(data);
slouken@1951
   738
    }
slouken@1951
   739
}
slouken@1951
   740
slouken@1951
   741
SDL_bool
slouken@1951
   742
X11_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1951
   743
{
slouken@1951
   744
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@1951
   745
        /* FIXME! */
slouken@1951
   746
        return SDL_TRUE;
slouken@1951
   747
    } else {
slouken@1951
   748
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1951
   749
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1951
   750
        return SDL_FALSE;
slouken@1951
   751
    }
slouken@1951
   752
}
slouken@1951
   753
slouken@1951
   754
/* vi: set ts=4 sw=4 expandtab: */