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