src/video/x11/SDL_x11window.c
author Bob Pendleton <bob@pendleton.com>
Thu, 06 Mar 2008 17:08:10 +0000
changeset 2322 c25d45b7add3
parent 2321 c5feceb0395e
child 2323 4ac07ae446d3
permissions -rw-r--r--
Fixing valgrind errors.

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