src/video/x11/SDL_x11window.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Jan 2009 17:14:27 +0000
changeset 2990 502adab079a4
parent 2967 e4a469d6ddab
child 3009 546c022a9ae5
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 #include "../../events/SDL_mouse_c.h"
    28 
    29 #include "SDL_x11video.h"
    30 #include "SDL_x11mouse.h"
    31 #include "../Xext/extensions/StdCmap.h"
    32 
    33 static void
    34 X11_GetDisplaySize(_THIS, SDL_Window * window, int *w, int *h)
    35 {
    36     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    37     SDL_DisplayData *displaydata =
    38         (SDL_DisplayData *) SDL_GetDisplayFromWindow(window)->driverdata;
    39     XWindowAttributes attr;
    40 
    41     XGetWindowAttributes(data->display, RootWindow(data->display,
    42                                                    displaydata->screen),
    43                          &attr);
    44     if (w) {
    45         *w = attr.width;
    46     }
    47     if (h) {
    48         *h = attr.height;
    49     }
    50 }
    51 
    52 static int
    53 SetupWindowData(_THIS, SDL_Window * window, Window w, BOOL created)
    54 {
    55     SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
    56     SDL_WindowData *data;
    57     int numwindows = videodata->numwindows;
    58     int windowlistlength = videodata->windowlistlength;
    59     SDL_WindowData **windowlist = videodata->windowlist;
    60     int index;
    61 
    62     /* Allocate the window data */
    63     data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
    64     if (!data) {
    65         SDL_OutOfMemory();
    66         return -1;
    67     }
    68     data->windowID = window->id;
    69     data->window = w;
    70 #ifdef X_HAVE_UTF8_STRING
    71     if (SDL_X11_HAVE_UTF8) {
    72         data->ic =
    73             pXCreateIC(videodata->im, XNClientWindow, w, XNFocusWindow, w,
    74                        XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
    75                        XNResourceName, videodata->classname, XNResourceClass,
    76                        videodata->classname, NULL);
    77     }
    78 #endif
    79     data->created = created;
    80     data->videodata = videodata;
    81 
    82     /* Associate the data with the window */
    83 
    84     if (numwindows < windowlistlength) {
    85         windowlist[numwindows] = data;
    86         videodata->numwindows++;
    87     } else {
    88         windowlist =
    89             (SDL_WindowData **) SDL_realloc(windowlist,
    90                                             (numwindows +
    91                                              1) * sizeof(*windowlist));
    92         if (!windowlist) {
    93             SDL_OutOfMemory();
    94             SDL_free(data);
    95             return -1;
    96         }
    97         windowlist[numwindows] = data;
    98         videodata->numwindows++;
    99         videodata->windowlistlength++;
   100         videodata->windowlist = windowlist;
   101     }
   102 
   103     /* Fill in the SDL window with the window data */
   104     {
   105         XWindowAttributes attrib;
   106 
   107         XGetWindowAttributes(data->videodata->display, w, &attrib);
   108         window->x = attrib.x;
   109         window->y = attrib.y;
   110         window->w = attrib.width;
   111         window->h = attrib.height;
   112         if (attrib.map_state != IsUnmapped) {
   113             window->flags |= SDL_WINDOW_SHOWN;
   114         } else {
   115             window->flags &= ~SDL_WINDOW_SHOWN;
   116         }
   117     }
   118     /* FIXME: How can I tell?
   119        {
   120        DWORD style = GetWindowLong(hwnd, GWL_STYLE);
   121        if (style & WS_VISIBLE) {
   122        if (style & (WS_BORDER | WS_THICKFRAME)) {
   123        window->flags &= ~SDL_WINDOW_BORDERLESS;
   124        } else {
   125        window->flags |= SDL_WINDOW_BORDERLESS;
   126        }
   127        if (style & WS_THICKFRAME) {
   128        window->flags |= SDL_WINDOW_RESIZABLE;
   129        } else {
   130        window->flags &= ~SDL_WINDOW_RESIZABLE;
   131        }
   132        if (style & WS_MAXIMIZE) {
   133        window->flags |= SDL_WINDOW_MAXIMIZED;
   134        } else {
   135        window->flags &= ~SDL_WINDOW_MAXIMIZED;
   136        }
   137        if (style & WS_MINIMIZE) {
   138        window->flags |= SDL_WINDOW_MINIMIZED;
   139        } else {
   140        window->flags &= ~SDL_WINDOW_MINIMIZED;
   141        }
   142        }
   143        if (GetFocus() == hwnd) {
   144        int index = data->videodata->keyboard;
   145        window->flags |= SDL_WINDOW_INPUT_FOCUS;
   146        SDL_SetKeyboardFocus(index, data->windowID);
   147 
   148        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
   149        RECT rect;
   150        GetClientRect(hwnd, &rect);
   151        ClientToScreen(hwnd, (LPPOINT) & rect);
   152        ClientToScreen(hwnd, (LPPOINT) & rect + 1);
   153        ClipCursor(&rect);
   154        }
   155        }
   156      */
   157 
   158     /* All done! */
   159     window->driverdata = data;
   160     return 0;
   161 }
   162 
   163 int
   164 X11_CreateWindow(_THIS, SDL_Window * window)
   165 {
   166     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   167     SDL_DisplayData *displaydata =
   168         (SDL_DisplayData *) SDL_GetDisplayFromWindow(window)->driverdata;
   169     Visual *visual;
   170     int depth;
   171     XSetWindowAttributes xattr;
   172     int x, y;
   173     Window w;
   174     XSizeHints *sizehints;
   175     XWMHints *wmhints;
   176     XClassHint *classhints;
   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 == 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 if (visual->class == DirectColor) {
   303         /* FIXME: Allocate a read-write colormap for gamma fading someday */
   304         xattr.colormap =
   305             XCreateColormap(data->display,
   306                             RootWindow(data->display, displaydata->screen),
   307                             visual, AllocNone);
   308     } else {
   309         xattr.colormap =
   310             XCreateColormap(data->display,
   311                             RootWindow(data->display, displaydata->screen),
   312                             visual, AllocNone);
   313     }
   314 
   315     if ((window->flags & SDL_WINDOW_FULLSCREEN)
   316         || window->x == SDL_WINDOWPOS_CENTERED) {
   317         X11_GetDisplaySize(_this, window, &x, NULL);
   318         x = (x - window->w) / 2;
   319     } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
   320         x = 0;
   321     } else {
   322         x = window->x;
   323     }
   324     if ((window->flags & SDL_WINDOW_FULLSCREEN)
   325         || window->y == SDL_WINDOWPOS_CENTERED) {
   326         X11_GetDisplaySize(_this, window, NULL, &y);
   327         y = (y - window->h) / 2;
   328     } else if (window->y == SDL_WINDOWPOS_UNDEFINED) {
   329         y = 0;
   330     } else {
   331         y = window->y;
   332     }
   333 
   334     w = XCreateWindow(data->display,
   335                       RootWindow(data->display, displaydata->screen), x, y,
   336                       window->w, window->h, 0, depth, InputOutput, visual,
   337                       (CWOverrideRedirect | CWBackPixel | CWBorderPixel |
   338                        CWColormap), &xattr);
   339     if (!w) {
   340 #ifdef SDL_VIDEO_OPENGL_GLX
   341         if (window->flags & SDL_WINDOW_OPENGL) {
   342             X11_GL_Shutdown(_this);
   343         }
   344 #endif
   345         SDL_SetError("Couldn't create window");
   346         return -1;
   347     }
   348 
   349     sizehints = XAllocSizeHints();
   350     if (sizehints) {
   351         if ((window->flags & SDL_WINDOW_RESIZABLE)
   352             && !(window->flags & SDL_WINDOW_FULLSCREEN)) {
   353             sizehints->min_width = 32;
   354             sizehints->min_height = 32;
   355             sizehints->max_height = 4096;
   356             sizehints->max_width = 4096;
   357         } else {
   358             sizehints->min_width = sizehints->max_width = window->w;
   359             sizehints->min_height = sizehints->max_height = window->h;
   360         }
   361         sizehints->flags = PMaxSize | PMinSize;
   362         if (!(window->flags & SDL_WINDOW_FULLSCREEN)
   363             && window->x != SDL_WINDOWPOS_UNDEFINED
   364             && window->y != SDL_WINDOWPOS_UNDEFINED) {
   365             sizehints->x = x;
   366             sizehints->y = y;
   367             sizehints->flags |= USPosition;
   368         }
   369         XSetWMNormalHints(data->display, w, sizehints);
   370         XFree(sizehints);
   371     }
   372 
   373     if (window->flags & (SDL_WINDOW_BORDERLESS | SDL_WINDOW_FULLSCREEN)) {
   374         SDL_bool set;
   375         Atom WM_HINTS;
   376 
   377         /* We haven't modified the window manager hints yet */
   378         set = SDL_FALSE;
   379 
   380         /* First try to set MWM hints */
   381         WM_HINTS = XInternAtom(data->display, "_MOTIF_WM_HINTS", True);
   382         if (WM_HINTS != None) {
   383             /* Hints used by Motif compliant window managers */
   384             struct
   385             {
   386                 unsigned long flags;
   387                 unsigned long functions;
   388                 unsigned long decorations;
   389                 long input_mode;
   390                 unsigned long status;
   391             } MWMHints = {
   392             (1L << 1), 0, 0, 0, 0};
   393 
   394             XChangeProperty(data->display, w, WM_HINTS, WM_HINTS, 32,
   395                             PropModeReplace, (unsigned char *) &MWMHints,
   396                             sizeof(MWMHints) / sizeof(long));
   397             set = SDL_TRUE;
   398         }
   399         /* Now try to set KWM hints */
   400         WM_HINTS = XInternAtom(data->display, "KWM_WIN_DECORATION", True);
   401         if (WM_HINTS != None) {
   402             long KWMHints = 0;
   403 
   404             XChangeProperty(data->display, w,
   405                             WM_HINTS, WM_HINTS, 32,
   406                             PropModeReplace,
   407                             (unsigned char *) &KWMHints,
   408                             sizeof(KWMHints) / sizeof(long));
   409             set = SDL_TRUE;
   410         }
   411         /* Now try to set GNOME hints */
   412         WM_HINTS = XInternAtom(data->display, "_WIN_HINTS", True);
   413         if (WM_HINTS != None) {
   414             long GNOMEHints = 0;
   415 
   416             XChangeProperty(data->display, w,
   417                             WM_HINTS, WM_HINTS, 32,
   418                             PropModeReplace,
   419                             (unsigned char *) &GNOMEHints,
   420                             sizeof(GNOMEHints) / sizeof(long));
   421             set = SDL_TRUE;
   422         }
   423         /* Finally set the transient hints if necessary */
   424         if (!set) {
   425             XSetTransientForHint(data->display, w,
   426                                  RootWindow(data->display,
   427                                             displaydata->screen));
   428         }
   429     } else {
   430         SDL_bool set;
   431         Atom WM_HINTS;
   432 
   433         /* We haven't modified the window manager hints yet */
   434         set = SDL_FALSE;
   435 
   436         /* First try to unset MWM hints */
   437         WM_HINTS = XInternAtom(data->display, "_MOTIF_WM_HINTS", True);
   438         if (WM_HINTS != None) {
   439             XDeleteProperty(data->display, w, WM_HINTS);
   440             set = SDL_TRUE;
   441         }
   442         /* Now try to unset KWM hints */
   443         WM_HINTS = XInternAtom(data->display, "KWM_WIN_DECORATION", True);
   444         if (WM_HINTS != None) {
   445             XDeleteProperty(data->display, w, WM_HINTS);
   446             set = SDL_TRUE;
   447         }
   448         /* Now try to unset GNOME hints */
   449         WM_HINTS = XInternAtom(data->display, "_WIN_HINTS", True);
   450         if (WM_HINTS != None) {
   451             XDeleteProperty(data->display, w, WM_HINTS);
   452             set = SDL_TRUE;
   453         }
   454         /* Finally unset the transient hints if necessary */
   455         if (!set) {
   456             /* NOTE: Does this work? */
   457             XSetTransientForHint(data->display, w, None);
   458         }
   459     }
   460 
   461     /* Tell KDE to keep fullscreen windows on top */
   462     if (window->flags & SDL_WINDOW_FULLSCREEN) {
   463         XEvent ev;
   464         long mask;
   465 
   466         SDL_zero(ev);
   467         ev.xclient.type = ClientMessage;
   468         ev.xclient.window = RootWindow(data->display, displaydata->screen);
   469         ev.xclient.message_type =
   470             XInternAtom(data->display, "KWM_KEEP_ON_TOP", False);
   471         ev.xclient.format = 32;
   472         ev.xclient.data.l[0] = w;
   473         ev.xclient.data.l[1] = CurrentTime;
   474         XSendEvent(data->display,
   475                    RootWindow(data->display, displaydata->screen), False,
   476                    SubstructureRedirectMask, &ev);
   477     }
   478 
   479     /* Set the input hints so we get keyboard input */
   480     wmhints = XAllocWMHints();
   481     if (wmhints) {
   482         wmhints->input = True;
   483         wmhints->flags = InputHint;
   484         XSetWMHints(data->display, w, wmhints);
   485         XFree(wmhints);
   486     }
   487 
   488     /* Set the class hints so we can get an icon (AfterStep) */
   489     classhints = XAllocClassHint();
   490     if (classhints != NULL) {
   491         classhints->res_name = data->classname;
   492         classhints->res_class = data->classname;
   493         XSetClassHint(data->display, w, classhints);
   494         XFree(classhints);
   495     }
   496 
   497     /* Allow the window to be deleted by the window manager */
   498     XSetWMProtocols(data->display, w, &data->WM_DELETE_WINDOW, 1);
   499 
   500     if (SetupWindowData(_this, window, w, SDL_TRUE) < 0) {
   501 #ifdef SDL_VIDEO_OPENGL_GLX
   502         if (window->flags & SDL_WINDOW_OPENGL) {
   503             X11_GL_Shutdown(_this);
   504         }
   505 #endif
   506         XDestroyWindow(data->display, w);
   507         return -1;
   508     }
   509 #ifdef X_HAVE_UTF8_STRING
   510     {
   511         Uint32 fevent = 0;
   512         pXGetICValues(((SDL_WindowData *) window->driverdata)->ic,
   513                       XNFilterEvents, &fevent, NULL);
   514         XSelectInput(data->display, w,
   515                      (FocusChangeMask | EnterWindowMask | LeaveWindowMask |
   516                       ExposureMask | ButtonPressMask | ButtonReleaseMask |
   517                       PointerMotionMask | KeyPressMask | KeyReleaseMask |
   518                       PropertyChangeMask | StructureNotifyMask |
   519                       KeymapStateMask | fevent));
   520     }
   521 #else
   522     {
   523         XSelectInput(data->display, w,
   524                      (FocusChangeMask | EnterWindowMask | LeaveWindowMask |
   525                       ExposureMask | ButtonPressMask | ButtonReleaseMask |
   526                       PointerMotionMask | KeyPressMask | KeyReleaseMask |
   527                       PropertyChangeMask | StructureNotifyMask |
   528                       KeymapStateMask));
   529     }
   530 #endif
   531 
   532 #if SDL_VIDEO_DRIVER_X11_XINPUT
   533     /* we're informing the display what extension events we want to receive from it */
   534     {
   535         int i, j, n = 0;
   536         XEventClass xevents[256];
   537 
   538         for (i = 0; i < SDL_GetNumMice(); ++i) {
   539             SDL_Mouse *mouse;
   540             X11_MouseData *data;
   541 
   542             mouse = SDL_GetMouse(i);
   543             data = (X11_MouseData *) mouse->driverdata;
   544             if (!data) {
   545                 continue;
   546             }
   547 
   548             for (j = 0; j < data->num_xevents; ++j) {
   549                 xevents[n++] = data->xevents[j];
   550             }
   551         }
   552         if (n > 0) {
   553             XSelectExtensionEvent(data->display, w, xevents, n);
   554         }
   555     }
   556 #endif
   557 
   558     return 0;
   559 }
   560 
   561 int
   562 X11_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
   563 {
   564     Window w = (Window) data;
   565 
   566     /* FIXME: Query the title from the existing window */
   567 
   568     if (SetupWindowData(_this, window, w, SDL_FALSE) < 0) {
   569         return -1;
   570     }
   571     return 0;
   572 }
   573 
   574 void
   575 X11_SetWindowTitle(_THIS, SDL_Window * window)
   576 {
   577     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   578     Display *display = data->videodata->display;
   579     XTextProperty titleprop, iconprop;
   580     Status status;
   581     const char *title = window->title;
   582     const char *icon = NULL;
   583 
   584 #ifdef X_HAVE_UTF8_STRING
   585     Atom _NET_WM_NAME = 0;
   586     Atom _NET_WM_ICON_NAME = 0;
   587 
   588     /* Look up some useful Atoms */
   589     if (SDL_X11_HAVE_UTF8) {
   590         _NET_WM_NAME = XInternAtom(display, "_NET_WM_NAME", False);
   591         _NET_WM_ICON_NAME = XInternAtom(display, "_NET_WM_ICON_NAME", False);
   592     }
   593 #endif
   594 
   595     if (title != NULL) {
   596         char *title_locale = SDL_iconv_utf8_locale(title);
   597         if (!title_locale) {
   598             SDL_OutOfMemory();
   599             return;
   600         }
   601         status = XStringListToTextProperty(&title_locale, 1, &titleprop);
   602         SDL_free(title_locale);
   603         if (status) {
   604             XSetTextProperty(display, data->window, &titleprop, XA_WM_NAME);
   605             XFree(titleprop.value);
   606         }
   607 #ifdef X_HAVE_UTF8_STRING
   608         if (SDL_X11_HAVE_UTF8) {
   609             status =
   610                 Xutf8TextListToTextProperty(display, (char **) &title, 1,
   611                                             XUTF8StringStyle, &titleprop);
   612             if (status == Success) {
   613                 XSetTextProperty(display, data->window, &titleprop,
   614                                  _NET_WM_NAME);
   615                 XFree(titleprop.value);
   616             }
   617         }
   618 #endif
   619     }
   620     if (icon != NULL) {
   621         char *icon_locale = SDL_iconv_utf8_locale(icon);
   622         if (!icon_locale) {
   623             SDL_OutOfMemory();
   624             return;
   625         }
   626         status = XStringListToTextProperty(&icon_locale, 1, &iconprop);
   627         SDL_free(icon_locale);
   628         if (status) {
   629             XSetTextProperty(display, data->window, &iconprop,
   630                              XA_WM_ICON_NAME);
   631             XFree(iconprop.value);
   632         }
   633 #ifdef X_HAVE_UTF8_STRING
   634         if (SDL_X11_HAVE_UTF8) {
   635             status =
   636                 Xutf8TextListToTextProperty(display, (char **) &icon, 1,
   637                                             XUTF8StringStyle, &iconprop);
   638             if (status == Success) {
   639                 XSetTextProperty(display, data->window, &iconprop,
   640                                  _NET_WM_ICON_NAME);
   641                 XFree(iconprop.value);
   642             }
   643         }
   644 #endif
   645     }
   646 }
   647 
   648 void
   649 X11_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
   650 {
   651     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   652     Display *display = data->videodata->display;
   653     Atom _NET_WM_ICON = XInternAtom(display, "_NET_WM_ICON", False);
   654 
   655     if (icon) {
   656         SDL_PixelFormat format;
   657         SDL_Surface *surface;
   658         int propsize;
   659         Uint32 *propdata;
   660 
   661         /* Convert the icon to ARGB for modern window managers */
   662         SDL_InitFormat(&format, 32, 0x00FF0000, 0x0000FF00, 0x000000FF,
   663                        0xFF000000);
   664         surface = SDL_ConvertSurface(icon, &format, 0);
   665         if (!surface) {
   666             return;
   667         }
   668 
   669         /* Set the _NET_WM_ICON property */
   670         propsize = 2 + (icon->w * icon->h);
   671         propdata = SDL_malloc(propsize * sizeof(Uint32));
   672         if (propdata) {
   673             propdata[0] = icon->w;
   674             propdata[1] = icon->h;
   675             SDL_memcpy(&propdata[2], surface->pixels,
   676                        surface->h * surface->pitch);
   677             XChangeProperty(display, data->window, _NET_WM_ICON, XA_CARDINAL,
   678                             32, PropModeReplace, (unsigned char *) propdata,
   679                             propsize);
   680         }
   681         SDL_FreeSurface(surface);
   682     } else {
   683         XDeleteProperty(display, data->window, _NET_WM_ICON);
   684     }
   685 }
   686 
   687 void
   688 X11_SetWindowPosition(_THIS, SDL_Window * window)
   689 {
   690     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   691     SDL_DisplayData *displaydata =
   692         (SDL_DisplayData *) SDL_GetDisplayFromWindow(window)->driverdata;
   693     Display *display = data->videodata->display;
   694     int x, y;
   695 
   696     if ((window->flags & SDL_WINDOW_FULLSCREEN)
   697         || window->x == SDL_WINDOWPOS_CENTERED) {
   698         X11_GetDisplaySize(_this, window, &x, NULL);
   699         x = (x - window->w) / 2;
   700     } else {
   701         x = window->x;
   702     }
   703     if ((window->flags & SDL_WINDOW_FULLSCREEN)
   704         || window->y == SDL_WINDOWPOS_CENTERED) {
   705         X11_GetDisplaySize(_this, window, NULL, &y);
   706         y = (y - window->h) / 2;
   707     } else {
   708         y = window->y;
   709     }
   710     XMoveWindow(display, data->window, x, y);
   711 }
   712 
   713 void
   714 X11_SetWindowSize(_THIS, SDL_Window * window)
   715 {
   716     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   717     Display *display = data->videodata->display;
   718 
   719     XResizeWindow(display, data->window, window->w, window->h);
   720 }
   721 
   722 void
   723 X11_ShowWindow(_THIS, SDL_Window * window)
   724 {
   725     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   726     Display *display = data->videodata->display;
   727 
   728     XMapRaised(display, data->window);
   729 }
   730 
   731 void
   732 X11_HideWindow(_THIS, SDL_Window * window)
   733 {
   734     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   735     Display *display = data->videodata->display;
   736 
   737     XUnmapWindow(display, data->window);
   738 }
   739 
   740 void
   741 X11_RaiseWindow(_THIS, SDL_Window * window)
   742 {
   743     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   744     Display *display = data->videodata->display;
   745 
   746     XRaiseWindow(display, data->window);
   747 }
   748 
   749 void
   750 X11_MaximizeWindow(_THIS, SDL_Window * window)
   751 {
   752     /* FIXME: is this even possible? */
   753 }
   754 
   755 void
   756 X11_MinimizeWindow(_THIS, SDL_Window * window)
   757 {
   758     X11_HideWindow(_this, window);
   759 }
   760 
   761 void
   762 X11_RestoreWindow(_THIS, SDL_Window * window)
   763 {
   764     X11_ShowWindow(_this, window);
   765 }
   766 
   767 void
   768 X11_SetWindowGrab(_THIS, SDL_Window * window)
   769 {
   770     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   771     Display *display = data->videodata->display;
   772 
   773     if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
   774         && (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
   775         /* Try to grab the mouse */
   776         for (;;) {
   777             int result =
   778                 XGrabPointer(display, data->window, True, 0, GrabModeAsync,
   779                              GrabModeAsync, data->window, None, CurrentTime);
   780             if (result == GrabSuccess) {
   781                 break;
   782             }
   783             SDL_Delay(100);
   784         }
   785 
   786         /* Raise the window if we grab the mouse */
   787         XRaiseWindow(display, data->window);
   788 
   789         /* Now grab the keyboard */
   790         XGrabKeyboard(display, data->window, True, GrabModeAsync,
   791                       GrabModeAsync, CurrentTime);
   792     } else {
   793         XUngrabPointer(display, CurrentTime);
   794         XUngrabKeyboard(display, CurrentTime);
   795     }
   796 }
   797 
   798 void
   799 X11_DestroyWindow(_THIS, SDL_Window * window)
   800 {
   801     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   802     window->driverdata = NULL;
   803 
   804     if (data) {
   805         SDL_VideoData *videodata = (SDL_VideoData *) data->videodata;
   806         Display *display = videodata->display;
   807         int numwindows = videodata->numwindows;
   808         SDL_WindowData **windowlist = videodata->windowlist;
   809         int i;
   810 
   811         if (windowlist) {
   812             for (i = 0; i < numwindows; ++i) {
   813                 if (windowlist[i] && (windowlist[i]->windowID == window->id)) {
   814                     windowlist[i] = windowlist[numwindows - 1];
   815                     windowlist[numwindows - 1] = NULL;
   816                     videodata->numwindows--;
   817                     break;
   818                 }
   819             }
   820         }
   821 #ifdef SDL_VIDEO_OPENGL_GLX
   822         if (window->flags & SDL_WINDOW_OPENGL) {
   823             X11_GL_Shutdown(_this);
   824         }
   825 #endif
   826 #ifdef X_HAVE_UTF8_STRING
   827         if (data->ic) {
   828             XDestroyIC(data->ic);
   829         }
   830 #endif
   831         if (data->created) {
   832             XDestroyWindow(display, data->window);
   833         }
   834         SDL_free(data);
   835     }
   836 }
   837 
   838 SDL_bool
   839 X11_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
   840 {
   841     if (info->version.major <= SDL_MAJOR_VERSION) {
   842         /* FIXME! */
   843         return SDL_TRUE;
   844     } else {
   845         SDL_SetError("Application not compiled with SDL %d.%d\n",
   846                      SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
   847         return SDL_FALSE;
   848     }
   849 }
   850 
   851 /* vi: set ts=4 sw=4 expandtab: */