src/video/x11/SDL_x11video.c
author Bob Pendleton <bob@pendleton.com>
Fri, 07 Mar 2008 20:54:11 +0000
changeset 2325 c7bcf84ba1b9
parent 2324 3202e4826c57
child 2710 44e49d3fa6cf
child 3760 64f346a83ed3
permissions -rw-r--r--
Next version of internationalized input for X11. On my machine (famous last words :-) with a US English keyboard and locale I can compose ` and e and get a text
input event with the character รจ. You still get the keypress keyrelease events for the individual keys that go into composing the character.
     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_video.h"
    25 #include "SDL_mouse.h"
    26 #include "../SDL_sysvideo.h"
    27 #include "../SDL_pixels_c.h"
    28 
    29 #include "SDL_x11video.h"
    30 //#include "SDL_d3drender.h"
    31 //#include "SDL_gdirender.h"
    32 
    33 /* Initialization/Query functions */
    34 static int X11_VideoInit(_THIS);
    35 static void X11_VideoQuit(_THIS);
    36 
    37 /* Find out what class name we should use */
    38 static char *
    39 get_classname()
    40 {
    41     char *spot;
    42 #if defined(__LINUX__) || defined(__FREEBSD__)
    43     char procfile[1024];
    44     char linkfile[1024];
    45     int linksize;
    46 #endif
    47 
    48     /* First allow environment variable override */
    49     spot = SDL_getenv("SDL_VIDEO_X11_WMCLASS");
    50     if (spot) {
    51         return SDL_strdup(spot);
    52     }
    53 
    54     /* Next look at the application's executable name */
    55 #if defined(__LINUX__) || defined(__FREEBSD__)
    56 #if defined(__LINUX__)
    57     SDL_snprintf(procfile, SDL_arraysize(procfile), "/proc/%d/exe", getpid());
    58 #elif defined(__FREEBSD__)
    59     SDL_snprintf(procfile, SDL_arraysize(procfile), "/proc/%d/file",
    60                  getpid());
    61 #else
    62 #error Where can we find the executable name?
    63 #endif
    64     linksize = readlink(procfile, linkfile, sizeof(linkfile) - 1);
    65     if (linksize > 0) {
    66         linkfile[linksize] = '\0';
    67         spot = SDL_strrchr(linkfile, '/');
    68         if (spot) {
    69             return SDL_strdup(spot + 1);
    70         } else {
    71             return SDL_strdup(linkfile);
    72         }
    73     }
    74 #endif /* __LINUX__ || __FREEBSD__ */
    75 
    76     /* Finally use the default we've used forever */
    77     return SDL_strdup("SDL_App");
    78 }
    79 
    80 /* X11 driver bootstrap functions */
    81 
    82 static int
    83 X11_Available(void)
    84 {
    85     Display *display = NULL;
    86     if (SDL_X11_LoadSymbols()) {
    87         display = XOpenDisplay(NULL);
    88         if (display != NULL) {
    89             XCloseDisplay(display);
    90         }
    91         SDL_X11_UnloadSymbols();
    92     }
    93     return (display != NULL);
    94 }
    95 
    96 static void
    97 X11_DeleteDevice(SDL_VideoDevice * device)
    98 {
    99     SDL_VideoData *data = (SDL_VideoData *) device->driverdata;
   100 
   101     if (data->display) {
   102         XCloseDisplay(data->display);
   103     }
   104     SDL_free(data->windowlist);
   105     SDL_free(device->driverdata);
   106     SDL_free(device);
   107 
   108     SDL_X11_UnloadSymbols();
   109 }
   110 
   111 static SDL_VideoDevice *
   112 X11_CreateDevice(int devindex)
   113 {
   114     SDL_VideoDevice *device;
   115     SDL_VideoData *data;
   116     const char *display = NULL; /* Use the DISPLAY environment variable */
   117 
   118     if (!SDL_X11_LoadSymbols()) {
   119         return NULL;
   120     }
   121 
   122     /* Initialize all variables that we clean on shutdown */
   123     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
   124     if (!device) {
   125         SDL_OutOfMemory();
   126         return NULL;
   127     }
   128     data = (struct SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
   129     if (!data) {
   130         SDL_OutOfMemory();
   131         SDL_free(device);
   132         return NULL;
   133     }
   134     device->driverdata = data;
   135 
   136     /* FIXME: Do we need this?
   137        if ( (SDL_strncmp(XDisplayName(display), ":", 1) == 0) ||
   138        (SDL_strncmp(XDisplayName(display), "unix:", 5) == 0) ) {
   139        local_X11 = 1;
   140        } else {
   141        local_X11 = 0;
   142        }
   143      */
   144     data->display = XOpenDisplay(display);
   145 #if defined(__osf__) && defined(SDL_VIDEO_DRIVER_X11_DYNAMIC)
   146     /* On Tru64 if linking without -lX11, it fails and you get following message.
   147      * Xlib: connection to ":0.0" refused by server
   148      * Xlib: XDM authorization key matches an existing client!
   149      *
   150      * It succeeds if retrying 1 second later
   151      * or if running xhost +localhost on shell.
   152      */
   153     if (data->display == NULL) {
   154         SDL_Delay(1000);
   155         data->display = XOpenDisplay(display);
   156     }
   157 #endif
   158     if (data->display == NULL) {
   159         SDL_free(device);
   160         SDL_SetError("Couldn't open X11 display");
   161         return NULL;
   162     }
   163 #ifdef X11_DEBUG
   164     XSynchronize(data->display, True);
   165 #endif
   166 
   167     /* Set the function pointers */
   168     device->VideoInit = X11_VideoInit;
   169     device->VideoQuit = X11_VideoQuit;
   170     device->GetDisplayModes = X11_GetDisplayModes;
   171     device->SetDisplayMode = X11_SetDisplayMode;
   172     device->SetDisplayGammaRamp = X11_SetDisplayGammaRamp;
   173     device->GetDisplayGammaRamp = X11_GetDisplayGammaRamp;
   174     device->PumpEvents = X11_PumpEvents;
   175 
   176     device->CreateWindow = X11_CreateWindow;
   177     device->CreateWindowFrom = X11_CreateWindowFrom;
   178     device->SetWindowTitle = X11_SetWindowTitle;
   179     device->SetWindowPosition = X11_SetWindowPosition;
   180     device->SetWindowSize = X11_SetWindowSize;
   181     device->ShowWindow = X11_ShowWindow;
   182     device->HideWindow = X11_HideWindow;
   183     device->RaiseWindow = X11_RaiseWindow;
   184     device->MaximizeWindow = X11_MaximizeWindow;
   185     device->MinimizeWindow = X11_MinimizeWindow;
   186     device->RestoreWindow = X11_RestoreWindow;
   187     device->SetWindowGrab = X11_SetWindowGrab;
   188     device->DestroyWindow = X11_DestroyWindow;
   189     device->GetWindowWMInfo = X11_GetWindowWMInfo;
   190 #ifdef SDL_VIDEO_OPENGL_GLX
   191     device->GL_LoadLibrary = X11_GL_LoadLibrary;
   192     device->GL_GetProcAddress = X11_GL_GetProcAddress;
   193     device->GL_CreateContext = X11_GL_CreateContext;
   194     device->GL_MakeCurrent = X11_GL_MakeCurrent;
   195     device->GL_SetSwapInterval = X11_GL_SetSwapInterval;
   196     device->GL_GetSwapInterval = X11_GL_GetSwapInterval;
   197     device->GL_SwapWindow = X11_GL_SwapWindow;
   198     device->GL_DeleteContext = X11_GL_DeleteContext;
   199 #endif
   200 
   201     device->free = X11_DeleteDevice;
   202 
   203     return device;
   204 }
   205 
   206 VideoBootStrap X11_bootstrap = {
   207     "x11", "SDL X11 video driver",
   208     X11_Available, X11_CreateDevice
   209 };
   210 
   211 
   212 int
   213 X11_VideoInit(_THIS)
   214 {
   215     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   216 
   217     /* Get the window class name, usually the name of the application */
   218     data->classname = get_classname();
   219 
   220     /* Open a connection to the X input manager */
   221 #ifdef X_HAVE_UTF8_STRING
   222     if (SDL_X11_HAVE_UTF8) {
   223         data->im =
   224             XOpenIM(data->display, NULL, data->classname, data->classname);
   225     }
   226 #endif
   227 
   228     /* Save DPMS and screensaver settings */
   229     X11_SaveScreenSaver(data->display, &data->screensaver_timeout,
   230                         &data->dpms_enabled);
   231     X11_DisableScreenSaver(data->display);
   232 
   233     /* Look up some useful Atoms */
   234     data->WM_DELETE_WINDOW =
   235         XInternAtom(data->display, "WM_DELETE_WINDOW", False);
   236 
   237     X11_InitModes(_this);
   238 
   239     if (X11_InitKeyboard(_this) != 0) {
   240         return -1;
   241     }
   242     X11_InitMouse(_this);
   243 
   244     return 0;
   245 }
   246 
   247 void
   248 X11_VideoQuit(_THIS)
   249 {
   250     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   251 
   252     if (data->classname) {
   253         SDL_free(data->classname);
   254     }
   255 #ifdef X_HAVE_UTF8_STRING
   256     if (data->im) {
   257         XCloseIM(data->im);
   258     }
   259 #endif
   260     X11_RestoreScreenSaver(data->display, data->screensaver_timeout,
   261                            data->dpms_enabled);
   262 
   263     X11_QuitModes(_this);
   264     X11_QuitKeyboard(_this);
   265     X11_QuitMouse(_this);
   266 }
   267 
   268 /* vim: set ts=4 sw=4 expandtab: */