src/video/x11/SDL_x11video.c
author Sam Lantinga
Wed, 10 Jun 2009 13:54:13 +0000
changeset 3190 c68d2ca5970f
parent 3057 089a77aebb7d
child 3218 81773a1eac83
permissions -rw-r--r--
Added missing files for OpenGL ES support
     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 
    32 /* Initialization/Query functions */
    33 static int X11_VideoInit(_THIS);
    34 static void X11_VideoQuit(_THIS);
    35 
    36 /* Find out what class name we should use */
    37 static char *
    38 get_classname()
    39 {
    40     char *spot;
    41 #if defined(__LINUX__) || defined(__FREEBSD__)
    42     char procfile[1024];
    43     char linkfile[1024];
    44     int linksize;
    45 #endif
    46 
    47     /* First allow environment variable override */
    48     spot = SDL_getenv("SDL_VIDEO_X11_WMCLASS");
    49     if (spot) {
    50         return SDL_strdup(spot);
    51     }
    52 
    53     /* Next look at the application's executable name */
    54 #if defined(__LINUX__) || defined(__FREEBSD__)
    55 #if defined(__LINUX__)
    56     SDL_snprintf(procfile, SDL_arraysize(procfile), "/proc/%d/exe", getpid());
    57 #elif defined(__FREEBSD__)
    58     SDL_snprintf(procfile, SDL_arraysize(procfile), "/proc/%d/file",
    59                  getpid());
    60 #else
    61 #error Where can we find the executable name?
    62 #endif
    63     linksize = readlink(procfile, linkfile, sizeof(linkfile) - 1);
    64     if (linksize > 0) {
    65         linkfile[linksize] = '\0';
    66         spot = SDL_strrchr(linkfile, '/');
    67         if (spot) {
    68             return SDL_strdup(spot + 1);
    69         } else {
    70             return SDL_strdup(linkfile);
    71         }
    72     }
    73 #endif /* __LINUX__ || __FREEBSD__ */
    74 
    75     /* Finally use the default we've used forever */
    76     return SDL_strdup("SDL_App");
    77 }
    78 
    79 /* X11 driver bootstrap functions */
    80 
    81 static int
    82 X11_Available(void)
    83 {
    84     Display *display = NULL;
    85     if (SDL_X11_LoadSymbols()) {
    86         display = XOpenDisplay(NULL);
    87         if (display != NULL) {
    88             XCloseDisplay(display);
    89         }
    90         SDL_X11_UnloadSymbols();
    91     }
    92     return (display != NULL);
    93 }
    94 
    95 static void
    96 X11_DeleteDevice(SDL_VideoDevice * device)
    97 {
    98     SDL_VideoData *data = (SDL_VideoData *) device->driverdata;
    99     if (data->display) {
   100         XCloseDisplay(data->display);
   101     }
   102     SDL_free(data->windowlist);
   103     SDL_free(device->driverdata);
   104     SDL_free(device);
   105 
   106     SDL_X11_UnloadSymbols();
   107 }
   108 
   109 static SDL_VideoDevice *
   110 X11_CreateDevice(int devindex)
   111 {
   112     SDL_VideoDevice *device;
   113     SDL_VideoData *data;
   114     const char *display = NULL; /* Use the DISPLAY environment variable */
   115 
   116     if (!SDL_X11_LoadSymbols()) {
   117         return NULL;
   118     }
   119 
   120     /* Initialize all variables that we clean on shutdown */
   121     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
   122     if (!device) {
   123         SDL_OutOfMemory();
   124         return NULL;
   125     }
   126     data = (struct SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
   127     if (!data) {
   128         SDL_OutOfMemory();
   129         SDL_free(device);
   130         return NULL;
   131     }
   132     device->driverdata = data;
   133 
   134     /* FIXME: Do we need this?
   135        if ( (SDL_strncmp(XDisplayName(display), ":", 1) == 0) ||
   136        (SDL_strncmp(XDisplayName(display), "unix:", 5) == 0) ) {
   137        local_X11 = 1;
   138        } else {
   139        local_X11 = 0;
   140        }
   141      */
   142     data->display = XOpenDisplay(display);
   143 #if defined(__osf__) && defined(SDL_VIDEO_DRIVER_X11_DYNAMIC)
   144     /* On Tru64 if linking without -lX11, it fails and you get following message.
   145      * Xlib: connection to ":0.0" refused by server
   146      * Xlib: XDM authorization key matches an existing client!
   147      *
   148      * It succeeds if retrying 1 second later
   149      * or if running xhost +localhost on shell.
   150      */
   151     if (data->display == NULL) {
   152         SDL_Delay(1000);
   153         data->display = XOpenDisplay(display);
   154     }
   155 #endif
   156     if (data->display == NULL) {
   157         SDL_free(device);
   158         SDL_SetError("Couldn't open X11 display");
   159         return NULL;
   160     }
   161 #ifdef X11_DEBUG
   162     XSynchronize(data->display, True);
   163 #endif
   164 
   165     /* Set the function pointers */
   166     device->VideoInit = X11_VideoInit;
   167     device->VideoQuit = X11_VideoQuit;
   168     device->GetDisplayModes = X11_GetDisplayModes;
   169     device->SetDisplayMode = X11_SetDisplayMode;
   170     device->SetDisplayGammaRamp = X11_SetDisplayGammaRamp;
   171     device->GetDisplayGammaRamp = X11_GetDisplayGammaRamp;
   172     device->SuspendScreenSaver = X11_SuspendScreenSaver;
   173     device->PumpEvents = X11_PumpEvents;
   174 
   175     device->CreateWindow = X11_CreateWindow;
   176     device->CreateWindowFrom = X11_CreateWindowFrom;
   177     device->SetWindowTitle = X11_SetWindowTitle;
   178     device->SetWindowIcon = X11_SetWindowIcon;
   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_UnloadLibrary = X11_GL_UnloadLibrary;
   194     device->GL_CreateContext = X11_GL_CreateContext;
   195     device->GL_MakeCurrent = X11_GL_MakeCurrent;
   196     device->GL_SetSwapInterval = X11_GL_SetSwapInterval;
   197     device->GL_GetSwapInterval = X11_GL_GetSwapInterval;
   198     device->GL_SwapWindow = X11_GL_SwapWindow;
   199     device->GL_DeleteContext = X11_GL_DeleteContext;
   200 #endif
   201 
   202     device->free = X11_DeleteDevice;
   203 
   204     return device;
   205 }
   206 
   207 VideoBootStrap X11_bootstrap = {
   208     "x11", "SDL X11 video driver",
   209     X11_Available, X11_CreateDevice
   210 };
   211 
   212 
   213 int
   214 X11_VideoInit(_THIS)
   215 {
   216     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   217 
   218     /* Get the window class name, usually the name of the application */
   219     data->classname = get_classname();
   220 
   221     /* Open a connection to the X input manager */
   222 #ifdef X_HAVE_UTF8_STRING
   223     if (SDL_X11_HAVE_UTF8) {
   224         data->im =
   225             XOpenIM(data->display, NULL, data->classname, data->classname);
   226     }
   227 #endif
   228 
   229     /* Look up some useful Atoms */
   230     data->WM_DELETE_WINDOW =
   231         XInternAtom(data->display, "WM_DELETE_WINDOW", False);
   232 
   233     X11_InitModes(_this);
   234 
   235 #if SDL_VIDEO_RENDER_X11
   236     X11_AddRenderDriver(_this);
   237 #endif
   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 
   261     X11_QuitModes(_this);
   262     X11_QuitKeyboard(_this);
   263     X11_QuitMouse(_this);
   264 }
   265 
   266 SDL_bool
   267 X11_UseDirectColorVisuals()
   268 {
   269     /* Once we implement DirectColor colormaps and gamma ramp support...
   270        return SDL_getenv("SDL_VIDEO_X11_NODIRECTCOLOR") ? SDL_FALSE : SDL_TRUE;
   271      */
   272     return SDL_FALSE;
   273 }
   274 
   275 /* vim: set ts=4 sw=4 expandtab: */