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