src/video/x11/SDL_x11video.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 07 Feb 2008 15:31:09 +0000
changeset 2305 fbe8ff44c519
parent 2295 dbc6d1893869
child 2323 4ac07ae446d3
permissions -rw-r--r--
First pass of new SDL scancode concept for X11.
     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(device->driverdata);
   105     SDL_free(device);
   106 
   107     SDL_X11_UnloadSymbols();
   108 }
   109 
   110 static SDL_VideoDevice *
   111 X11_CreateDevice(int devindex)
   112 {
   113     SDL_VideoDevice *device;
   114     SDL_VideoData *data;
   115     const char *display = NULL; /* Use the DISPLAY environment variable */
   116 
   117     if (!SDL_X11_LoadSymbols()) {
   118         return NULL;
   119     }
   120 
   121     /* Initialize all variables that we clean on shutdown */
   122     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
   123     if (device) {
   124         data = (struct SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
   125     }
   126     if (!device || !data) {
   127         SDL_OutOfMemory();
   128         if (device) {
   129             SDL_free(device);
   130         }
   131         return NULL;
   132     }
   133     device->driverdata = data;
   134 
   135     /* FIXME: Do we need this?
   136        if ( (SDL_strncmp(XDisplayName(display), ":", 1) == 0) ||
   137        (SDL_strncmp(XDisplayName(display), "unix:", 5) == 0) ) {
   138        local_X11 = 1;
   139        } else {
   140        local_X11 = 0;
   141        }
   142      */
   143     data->display = XOpenDisplay(display);
   144 #if defined(__osf__) && defined(SDL_VIDEO_DRIVER_X11_DYNAMIC)
   145     /* On Tru64 if linking without -lX11, it fails and you get following message.
   146      * Xlib: connection to ":0.0" refused by server
   147      * Xlib: XDM authorization key matches an existing client!
   148      *
   149      * It succeeds if retrying 1 second later
   150      * or if running xhost +localhost on shell.
   151      */
   152     if (data->display == NULL) {
   153         SDL_Delay(1000);
   154         data->display = XOpenDisplay(display);
   155     }
   156 #endif
   157     if (data->display == NULL) {
   158         SDL_free(device);
   159         SDL_SetError("Couldn't open X11 display");
   160         return NULL;
   161     }
   162 #ifdef X11_DEBUG
   163     XSynchronize(data->display, True);
   164 #endif
   165 
   166     /* Set the function pointers */
   167     device->VideoInit = X11_VideoInit;
   168     device->VideoQuit = X11_VideoQuit;
   169     device->GetDisplayModes = X11_GetDisplayModes;
   170     device->SetDisplayMode = X11_SetDisplayMode;
   171     device->SetDisplayGammaRamp = X11_SetDisplayGammaRamp;
   172     device->GetDisplayGammaRamp = X11_GetDisplayGammaRamp;
   173     device->PumpEvents = X11_PumpEvents;
   174 
   175     device->CreateWindow = X11_CreateWindow;
   176     device->CreateWindowFrom = X11_CreateWindowFrom;
   177     device->SetWindowTitle = X11_SetWindowTitle;
   178     device->SetWindowPosition = X11_SetWindowPosition;
   179     device->SetWindowSize = X11_SetWindowSize;
   180     device->ShowWindow = X11_ShowWindow;
   181     device->HideWindow = X11_HideWindow;
   182     device->RaiseWindow = X11_RaiseWindow;
   183     device->MaximizeWindow = X11_MaximizeWindow;
   184     device->MinimizeWindow = X11_MinimizeWindow;
   185     device->RestoreWindow = X11_RestoreWindow;
   186     device->SetWindowGrab = X11_SetWindowGrab;
   187     device->DestroyWindow = X11_DestroyWindow;
   188     device->GetWindowWMInfo = X11_GetWindowWMInfo;
   189 #ifdef SDL_VIDEO_OPENGL_GLX
   190     device->GL_LoadLibrary = X11_GL_LoadLibrary;
   191     device->GL_GetProcAddress = X11_GL_GetProcAddress;
   192     device->GL_CreateContext = X11_GL_CreateContext;
   193     device->GL_MakeCurrent = X11_GL_MakeCurrent;
   194     device->GL_SetSwapInterval = X11_GL_SetSwapInterval;
   195     device->GL_GetSwapInterval = X11_GL_GetSwapInterval;
   196     device->GL_SwapWindow = X11_GL_SwapWindow;
   197     device->GL_DeleteContext = X11_GL_DeleteContext;
   198 #endif
   199 
   200     device->free = X11_DeleteDevice;
   201 
   202     return device;
   203 }
   204 
   205 VideoBootStrap X11_bootstrap = {
   206     "x11", "SDL X11 video driver",
   207     X11_Available, X11_CreateDevice
   208 };
   209 
   210 
   211 int
   212 X11_VideoInit(_THIS)
   213 {
   214     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   215 
   216     /* Get the window class name, usually the name of the application */
   217     data->classname = get_classname();
   218 
   219     /* Open a connection to the X input manager */
   220 #ifdef X_HAVE_UTF8_STRING
   221     if (SDL_X11_HAVE_UTF8) {
   222         data->im =
   223             XOpenIM(data->display, NULL, data->classname, data->classname);
   224     }
   225 #endif
   226 
   227     /* Save DPMS and screensaver settings */
   228     X11_SaveScreenSaver(data->display, &data->screensaver_timeout,
   229                         &data->dpms_enabled);
   230     X11_DisableScreenSaver(data->display);
   231 
   232     /* Look up some useful Atoms */
   233     data->WM_DELETE_WINDOW =
   234         XInternAtom(data->display, "WM_DELETE_WINDOW", False);
   235 
   236     X11_InitModes(_this);
   237 
   238     if (X11_InitKeyboard(_this) != 0) {
   239         return -1;
   240     }
   241     X11_InitMouse(_this);
   242 
   243     return 0;
   244 }
   245 
   246 void
   247 X11_VideoQuit(_THIS)
   248 {
   249     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   250 
   251     if (data->classname) {
   252         SDL_free(data->classname);
   253     }
   254 #ifdef X_HAVE_UTF8_STRING
   255     if (data->im) {
   256         XCloseIM(data->im);
   257     }
   258 #endif
   259     X11_RestoreScreenSaver(data->display, data->screensaver_timeout,
   260                            data->dpms_enabled);
   261 
   262     X11_QuitModes(_this);
   263     X11_QuitKeyboard(_this);
   264     X11_QuitMouse(_this);
   265 }
   266 
   267 /* vim: set ts=4 sw=4 expandtab: */