src/video/x11/SDL_x11window.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 30 Dec 2008 17:09:42 +0000
changeset 2931 e705adf6f3dc
parent 2876 3fcb0d447bcd
child 2940 b93965a16fe0
permissions -rw-r--r--
Date: Mon, 29 Dec 2008 23:29:52 +0100
From: Couriersud
Subject: SDL1.3: Some X11 diffs

The attached diff fixes the following issues in the X11 backend:

a) When calling resize, actually a move was performed. This has been
corrected.

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