src/video/x11/SDL_x11video.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Dec 2009 08:11:06 +0000
changeset 3565 f43c8f688f77
parent 3521 76f9b76ddf0f
child 3697 f7b03b6838cb
permissions -rw-r--r--
Fixed bug #906

Added better error reporting for OpenGL context creation failing.
     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_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 
    31 #if SDL_VIDEO_DRIVER_PANDORA
    32 #include "SDL_x11opengles.h"
    33 #endif
    34 
    35 /* Initialization/Query functions */
    36 static int X11_VideoInit(_THIS);
    37 static void X11_VideoQuit(_THIS);
    38 
    39 /* Find out what class name we should use */
    40 static char *
    41 get_classname()
    42 {
    43     char *spot;
    44 #if defined(__LINUX__) || defined(__FREEBSD__)
    45     char procfile[1024];
    46     char linkfile[1024];
    47     int linksize;
    48 #endif
    49 
    50     /* First allow environment variable override */
    51     spot = SDL_getenv("SDL_VIDEO_X11_WMCLASS");
    52     if (spot) {
    53         return SDL_strdup(spot);
    54     }
    55 
    56     /* Next look at the application's executable name */
    57 #if defined(__LINUX__) || defined(__FREEBSD__)
    58 #if defined(__LINUX__)
    59     SDL_snprintf(procfile, SDL_arraysize(procfile), "/proc/%d/exe", getpid());
    60 #elif defined(__FREEBSD__)
    61     SDL_snprintf(procfile, SDL_arraysize(procfile), "/proc/%d/file",
    62                  getpid());
    63 #else
    64 #error Where can we find the executable name?
    65 #endif
    66     linksize = readlink(procfile, linkfile, sizeof(linkfile) - 1);
    67     if (linksize > 0) {
    68         linkfile[linksize] = '\0';
    69         spot = SDL_strrchr(linkfile, '/');
    70         if (spot) {
    71             return SDL_strdup(spot + 1);
    72         } else {
    73             return SDL_strdup(linkfile);
    74         }
    75     }
    76 #endif /* __LINUX__ || __FREEBSD__ */
    77 
    78     /* Finally use the default we've used forever */
    79     return SDL_strdup("SDL_App");
    80 }
    81 
    82 /* X11 driver bootstrap functions */
    83 
    84 static int
    85 X11_Available(void)
    86 {
    87     Display *display = NULL;
    88     if (SDL_X11_LoadSymbols()) {
    89         display = XOpenDisplay(NULL);
    90         if (display != NULL) {
    91             XCloseDisplay(display);
    92         }
    93         SDL_X11_UnloadSymbols();
    94     }
    95     return (display != NULL);
    96 }
    97 
    98 static void
    99 X11_DeleteDevice(SDL_VideoDevice * device)
   100 {
   101     SDL_VideoData *data = (SDL_VideoData *) device->driverdata;
   102     if (data->display) {
   103         XCloseDisplay(data->display);
   104     }
   105     SDL_free(data->windowlist);
   106     SDL_free(device->driverdata);
   107 #if SDL_VIDEO_DRIVER_PANDORA
   108     SDL_free(device->gles_data);
   109 #endif
   110     SDL_free(device);
   111 
   112     SDL_X11_UnloadSymbols();
   113 }
   114 
   115 static SDL_VideoDevice *
   116 X11_CreateDevice(int devindex)
   117 {
   118     SDL_VideoDevice *device;
   119     SDL_VideoData *data;
   120     const char *display = NULL; /* Use the DISPLAY environment variable */
   121 
   122     if (!SDL_X11_LoadSymbols()) {
   123         return NULL;
   124     }
   125 
   126     /* Initialize all variables that we clean on shutdown */
   127     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
   128     if (!device) {
   129         SDL_OutOfMemory();
   130         return NULL;
   131     }
   132     data = (struct SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
   133     if (!data) {
   134         SDL_OutOfMemory();
   135         SDL_free(device);
   136         return NULL;
   137     }
   138     device->driverdata = data;
   139 
   140 #if SDL_VIDEO_DRIVER_PANDORA
   141     device->gles_data = (struct SDL_PrivateGLESData *) SDL_calloc(1, sizeof(SDL_PrivateGLESData));
   142     if (!device->gles_data) {
   143         SDL_OutOfMemory();
   144         return NULL;
   145     }
   146 #endif
   147 
   148     /* FIXME: Do we need this?
   149        if ( (SDL_strncmp(XDisplayName(display), ":", 1) == 0) ||
   150        (SDL_strncmp(XDisplayName(display), "unix:", 5) == 0) ) {
   151        local_X11 = 1;
   152        } else {
   153        local_X11 = 0;
   154        }
   155      */
   156     data->display = XOpenDisplay(display);
   157 #if defined(__osf__) && defined(SDL_VIDEO_DRIVER_X11_DYNAMIC)
   158     /* On Tru64 if linking without -lX11, it fails and you get following message.
   159      * Xlib: connection to ":0.0" refused by server
   160      * Xlib: XDM authorization key matches an existing client!
   161      *
   162      * It succeeds if retrying 1 second later
   163      * or if running xhost +localhost on shell.
   164      */
   165     if (data->display == NULL) {
   166         SDL_Delay(1000);
   167         data->display = XOpenDisplay(display);
   168     }
   169 #endif
   170     if (data->display == NULL) {
   171         SDL_free(device);
   172         SDL_SetError("Couldn't open X11 display");
   173         return NULL;
   174     }
   175 #ifdef X11_DEBUG
   176     XSynchronize(data->display, True);
   177 #endif
   178 
   179     /* Set the function pointers */
   180     device->VideoInit = X11_VideoInit;
   181     device->VideoQuit = X11_VideoQuit;
   182     device->GetDisplayModes = X11_GetDisplayModes;
   183     device->SetDisplayMode = X11_SetDisplayMode;
   184     device->SetDisplayGammaRamp = X11_SetDisplayGammaRamp;
   185     device->GetDisplayGammaRamp = X11_GetDisplayGammaRamp;
   186     device->SuspendScreenSaver = X11_SuspendScreenSaver;
   187     device->PumpEvents = X11_PumpEvents;
   188 
   189     device->CreateWindow = X11_CreateWindow;
   190     device->CreateWindowFrom = X11_CreateWindowFrom;
   191     device->SetWindowTitle = X11_SetWindowTitle;
   192     device->SetWindowIcon = X11_SetWindowIcon;
   193     device->SetWindowPosition = X11_SetWindowPosition;
   194     device->SetWindowSize = X11_SetWindowSize;
   195     device->ShowWindow = X11_ShowWindow;
   196     device->HideWindow = X11_HideWindow;
   197     device->RaiseWindow = X11_RaiseWindow;
   198     device->MaximizeWindow = X11_MaximizeWindow;
   199     device->MinimizeWindow = X11_MinimizeWindow;
   200     device->RestoreWindow = X11_RestoreWindow;
   201     device->SetWindowGrab = X11_SetWindowGrab;
   202     device->DestroyWindow = X11_DestroyWindow;
   203     device->GetWindowWMInfo = X11_GetWindowWMInfo;
   204 #ifdef SDL_VIDEO_OPENGL_GLX
   205     device->GL_LoadLibrary = X11_GL_LoadLibrary;
   206     device->GL_GetProcAddress = X11_GL_GetProcAddress;
   207     device->GL_UnloadLibrary = X11_GL_UnloadLibrary;
   208     device->GL_CreateContext = X11_GL_CreateContext;
   209     device->GL_MakeCurrent = X11_GL_MakeCurrent;
   210     device->GL_SetSwapInterval = X11_GL_SetSwapInterval;
   211     device->GL_GetSwapInterval = X11_GL_GetSwapInterval;
   212     device->GL_SwapWindow = X11_GL_SwapWindow;
   213     device->GL_DeleteContext = X11_GL_DeleteContext;
   214 #endif
   215 #if SDL_VIDEO_DRIVER_PANDORA
   216     device->GL_LoadLibrary = X11_GLES_LoadLibrary;
   217     device->GL_GetProcAddress = X11_GLES_GetProcAddress;
   218     device->GL_UnloadLibrary = X11_GLES_UnloadLibrary;
   219     device->GL_CreateContext = X11_GLES_CreateContext;
   220     device->GL_MakeCurrent = X11_GLES_MakeCurrent;
   221     device->GL_SetSwapInterval = X11_GLES_SetSwapInterval;
   222     device->GL_GetSwapInterval = X11_GLES_GetSwapInterval;
   223     device->GL_SwapWindow = X11_GLES_SwapWindow;
   224     device->GL_DeleteContext = X11_GLES_DeleteContext;
   225 #endif
   226 
   227     device->free = X11_DeleteDevice;
   228 
   229     return device;
   230 }
   231 
   232 VideoBootStrap X11_bootstrap = {
   233     "x11", "SDL X11 video driver",
   234     X11_Available, X11_CreateDevice
   235 };
   236 
   237 
   238 int
   239 X11_VideoInit(_THIS)
   240 {
   241     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   242 
   243     /* Get the window class name, usually the name of the application */
   244     data->classname = get_classname();
   245 
   246     /* Open a connection to the X input manager */
   247 #ifdef X_HAVE_UTF8_STRING
   248     if (SDL_X11_HAVE_UTF8) {
   249         data->im =
   250             XOpenIM(data->display, NULL, data->classname, data->classname);
   251     }
   252 #endif
   253 
   254     /* Look up some useful Atoms */
   255     data->WM_DELETE_WINDOW =
   256         XInternAtom(data->display, "WM_DELETE_WINDOW", False);
   257 
   258     if (X11_InitModes(_this) < 0) {
   259         return -1;
   260     }
   261 
   262 #if SDL_VIDEO_RENDER_X11
   263     X11_AddRenderDriver(_this);
   264 #endif
   265 
   266     if (X11_InitKeyboard(_this) != 0) {
   267         return -1;
   268     }
   269     X11_InitMouse(_this);
   270 
   271     return 0;
   272 }
   273 
   274 void
   275 X11_VideoQuit(_THIS)
   276 {
   277     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   278 
   279     if (data->classname) {
   280         SDL_free(data->classname);
   281     }
   282 #ifdef X_HAVE_UTF8_STRING
   283     if (data->im) {
   284         XCloseIM(data->im);
   285     }
   286 #endif
   287 
   288     X11_QuitModes(_this);
   289     X11_QuitKeyboard(_this);
   290     X11_QuitMouse(_this);
   291 }
   292 
   293 SDL_bool
   294 X11_UseDirectColorVisuals()
   295 {
   296     /* Once we implement DirectColor colormaps and gamma ramp support...
   297        return SDL_getenv("SDL_VIDEO_X11_NODIRECTCOLOR") ? SDL_FALSE : SDL_TRUE;
   298      */
   299     return SDL_FALSE;
   300 }
   301 
   302 /* vim: set ts=4 sw=4 expandtab: */