src/video/x11/SDL_x11window.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 01 Jan 2009 08:21:19 +0000
changeset 2942 1e431c2631ee
parent 2940 b93965a16fe0
child 2945 e38423786728
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 == 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 #if SDL_VIDEO_DRIVER_X11_XINPUT
   527     /* we're informing the display what extension events we want to receive from it */
   528     {
   529         int i, j, n = 0;
   530         XEventClass xevents[256];
   531 
   532         for (i = 0; i < SDL_GetNumMice(); ++i) {
   533             SDL_Mouse *mouse;
   534             X11_MouseData *data;
   535 
   536             mouse = SDL_GetMouse(i);
   537             data = (X11_MouseData *) mouse->driverdata;
   538             if (!data) {
   539                 continue;
   540             }
   541 
   542             for (j = 0; j < data->num_xevents; ++j) {
   543                 xevents[n++] = data->xevents[j];
   544             }
   545         }
   546         if (n > 0) {
   547             XSelectExtensionEvent(data->display, w, xevents, n);
   548         }
   549     }
   550 #endif
   551 
   552     return 0;
   553 }
   554 
   555 int
   556 X11_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
   557 {
   558     Window w = (Window) data;
   559 
   560     /* FIXME: Query the title from the existing window */
   561 
   562     if (SetupWindowData(_this, window, w, SDL_FALSE) < 0) {
   563         return -1;
   564     }
   565     return 0;
   566 }
   567 
   568 void
   569 X11_SetWindowTitle(_THIS, SDL_Window * window)
   570 {
   571     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   572     Display *display = data->videodata->display;
   573     XTextProperty titleprop, iconprop;
   574     Status status;
   575     const char *title = window->title;
   576     const char *icon = NULL;
   577 
   578 #ifdef X_HAVE_UTF8_STRING
   579     Atom _NET_WM_NAME = 0;
   580     Atom _NET_WM_ICON_NAME = 0;
   581 
   582     /* Look up some useful Atoms */
   583     if (SDL_X11_HAVE_UTF8) {
   584         _NET_WM_NAME = XInternAtom(display, "_NET_WM_NAME", False);
   585         _NET_WM_ICON_NAME = XInternAtom(display, "_NET_WM_ICON_NAME", False);
   586     }
   587 #endif
   588 
   589     if (title != NULL) {
   590         char *title_locale = SDL_iconv_utf8_locale(title);
   591         if (!title_locale) {
   592             SDL_OutOfMemory();
   593             return;
   594         }
   595         status = XStringListToTextProperty(&title_locale, 1, &titleprop);
   596         SDL_free(title_locale);
   597         if (status) {
   598             XSetTextProperty(display, data->window, &titleprop, XA_WM_NAME);
   599             XFree(titleprop.value);
   600         }
   601 #ifdef X_HAVE_UTF8_STRING
   602         if (SDL_X11_HAVE_UTF8) {
   603             status =
   604                 Xutf8TextListToTextProperty(display, (char **) &title, 1,
   605                                             XUTF8StringStyle, &titleprop);
   606             if (status == Success) {
   607                 XSetTextProperty(display, data->window, &titleprop,
   608                                  _NET_WM_NAME);
   609                 XFree(titleprop.value);
   610             }
   611         }
   612 #endif
   613     }
   614     if (icon != NULL) {
   615         char *icon_locale = SDL_iconv_utf8_locale(icon);
   616         if (!icon_locale) {
   617             SDL_OutOfMemory();
   618             return;
   619         }
   620         status = XStringListToTextProperty(&icon_locale, 1, &iconprop);
   621         SDL_free(icon_locale);
   622         if (status) {
   623             XSetTextProperty(display, data->window, &iconprop,
   624                              XA_WM_ICON_NAME);
   625             XFree(iconprop.value);
   626         }
   627 #ifdef X_HAVE_UTF8_STRING
   628         if (SDL_X11_HAVE_UTF8) {
   629             status =
   630                 Xutf8TextListToTextProperty(display, (char **) &icon, 1,
   631                                             XUTF8StringStyle, &iconprop);
   632             if (status == Success) {
   633                 XSetTextProperty(display, data->window, &iconprop,
   634                                  _NET_WM_ICON_NAME);
   635                 XFree(iconprop.value);
   636             }
   637         }
   638 #endif
   639     }
   640 }
   641 
   642 void
   643 X11_SetWindowPosition(_THIS, SDL_Window * window)
   644 {
   645     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   646     SDL_DisplayData *displaydata =
   647         (SDL_DisplayData *) SDL_GetDisplayFromWindow(window)->driverdata;
   648     Display *display = data->videodata->display;
   649     int x, y;
   650 
   651     if ((window->flags & SDL_WINDOW_FULLSCREEN)
   652         || window->x == SDL_WINDOWPOS_CENTERED) {
   653         X11_GetDisplaySize(_this, window, &x, NULL);
   654         x = (x - window->w) / 2;
   655     } else {
   656         x = window->x;
   657     }
   658     if ((window->flags & SDL_WINDOW_FULLSCREEN)
   659         || window->y == SDL_WINDOWPOS_CENTERED) {
   660         X11_GetDisplaySize(_this, window, NULL, &y);
   661         y = (y - window->h) / 2;
   662     } else {
   663         y = window->y;
   664     }
   665     XMoveWindow(display, data->window, x, y);
   666 }
   667 
   668 void
   669 X11_SetWindowSize(_THIS, SDL_Window * window)
   670 {
   671     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   672     Display *display = data->videodata->display;
   673 
   674     XResizeWindow(display, data->window, window->w, window->h);
   675 }
   676 
   677 void
   678 X11_ShowWindow(_THIS, SDL_Window * window)
   679 {
   680     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   681     Display *display = data->videodata->display;
   682 
   683     XMapRaised(display, data->window);
   684 }
   685 
   686 void
   687 X11_HideWindow(_THIS, SDL_Window * window)
   688 {
   689     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   690     Display *display = data->videodata->display;
   691 
   692     XUnmapWindow(display, data->window);
   693 }
   694 
   695 void
   696 X11_RaiseWindow(_THIS, SDL_Window * window)
   697 {
   698     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   699     Display *display = data->videodata->display;
   700 
   701     XRaiseWindow(display, data->window);
   702 }
   703 
   704 void
   705 X11_MaximizeWindow(_THIS, SDL_Window * window)
   706 {
   707     /* FIXME: is this even possible? */
   708 }
   709 
   710 void
   711 X11_MinimizeWindow(_THIS, SDL_Window * window)
   712 {
   713     X11_HideWindow(_this, window);
   714 }
   715 
   716 void
   717 X11_RestoreWindow(_THIS, SDL_Window * window)
   718 {
   719     X11_ShowWindow(_this, window);
   720 }
   721 
   722 void
   723 X11_SetWindowGrab(_THIS, SDL_Window * window)
   724 {
   725     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   726     Display *display = data->videodata->display;
   727 
   728     if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
   729         && (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
   730         /* Try to grab the mouse */
   731         for (;;) {
   732             int result =
   733                 XGrabPointer(display, data->window, True, 0, GrabModeAsync,
   734                              GrabModeAsync, data->window, None, CurrentTime);
   735             if (result == GrabSuccess) {
   736                 break;
   737             }
   738             SDL_Delay(100);
   739         }
   740 
   741         /* Raise the window if we grab the mouse */
   742         XRaiseWindow(display, data->window);
   743 
   744         /* Now grab the keyboard */
   745         XGrabKeyboard(display, data->window, True, GrabModeAsync,
   746                       GrabModeAsync, CurrentTime);
   747     } else {
   748         XUngrabPointer(display, CurrentTime);
   749         XUngrabKeyboard(display, CurrentTime);
   750     }
   751 }
   752 
   753 void
   754 X11_DestroyWindow(_THIS, SDL_Window * window)
   755 {
   756     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   757     window->driverdata = NULL;
   758 
   759     if (data) {
   760         SDL_VideoData *videodata = (SDL_VideoData *) data->videodata;
   761         Display *display = videodata->display;
   762         int numwindows = videodata->numwindows;
   763         SDL_WindowData **windowlist = videodata->windowlist;
   764         int i;
   765 
   766         if (windowlist) {
   767             for (i = 0; i < numwindows; ++i) {
   768                 if (windowlist[i] && (windowlist[i]->windowID == window->id)) {
   769                     windowlist[i] = windowlist[numwindows - 1];
   770                     windowlist[numwindows - 1] = NULL;
   771                     videodata->numwindows--;
   772                     break;
   773                 }
   774             }
   775         }
   776 #ifdef SDL_VIDEO_OPENGL_GLX
   777         if (window->flags & SDL_WINDOW_OPENGL) {
   778             X11_GL_Shutdown(_this);
   779         }
   780 #endif
   781 #ifdef X_HAVE_UTF8_STRING
   782         if (data->ic) {
   783             XDestroyIC(data->ic);
   784         }
   785 #endif
   786         if (data->created) {
   787             XDestroyWindow(display, data->window);
   788         }
   789         SDL_free(data);
   790     }
   791 }
   792 
   793 SDL_bool
   794 X11_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
   795 {
   796     if (info->version.major <= SDL_MAJOR_VERSION) {
   797         /* FIXME! */
   798         return SDL_TRUE;
   799     } else {
   800         SDL_SetError("Application not compiled with SDL %d.%d\n",
   801                      SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
   802         return SDL_FALSE;
   803     }
   804 }
   805 
   806 /* vi: set ts=4 sw=4 expandtab: */