src/video/x11/SDL_x11video.c
author Sam Lantinga
Tue, 08 Feb 2011 16:50:51 -0800
changeset 5229 c015d3e63631
parent 5182 073b86030262
child 5243 3a8a452b49f0
permissions -rw-r--r--
Fixed setting the texture unit, still doesn't work.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2010 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 <unistd.h> /* For getpid() and readlink() */
    25 
    26 #include "SDL_video.h"
    27 #include "SDL_mouse.h"
    28 #include "../SDL_sysvideo.h"
    29 #include "../SDL_pixels_c.h"
    30 
    31 #include "SDL_x11video.h"
    32 #include "SDL_x11framebuffer.h"
    33 #include "SDL_x11shape.h"
    34 #include "SDL_x11touch.h" 
    35 
    36 #if SDL_VIDEO_DRIVER_PANDORA
    37 #include "SDL_x11opengles.h"
    38 #endif
    39 
    40 /* Initialization/Query functions */
    41 static int X11_VideoInit(_THIS);
    42 static void X11_VideoQuit(_THIS);
    43 
    44 /* Find out what class name we should use */
    45 static char *
    46 get_classname()
    47 {
    48     char *spot;
    49 #if defined(__LINUX__) || defined(__FREEBSD__)
    50     char procfile[1024];
    51     char linkfile[1024];
    52     int linksize;
    53 #endif
    54 
    55     /* First allow environment variable override */
    56     spot = SDL_getenv("SDL_VIDEO_X11_WMCLASS");
    57     if (spot) {
    58         return SDL_strdup(spot);
    59     }
    60 
    61     /* Next look at the application's executable name */
    62 #if defined(__LINUX__) || defined(__FREEBSD__)
    63 #if defined(__LINUX__)
    64     SDL_snprintf(procfile, SDL_arraysize(procfile), "/proc/%d/exe", getpid());
    65 #elif defined(__FREEBSD__)
    66     SDL_snprintf(procfile, SDL_arraysize(procfile), "/proc/%d/file",
    67                  getpid());
    68 #else
    69 #error Where can we find the executable name?
    70 #endif
    71     linksize = readlink(procfile, linkfile, sizeof(linkfile) - 1);
    72     if (linksize > 0) {
    73         linkfile[linksize] = '\0';
    74         spot = SDL_strrchr(linkfile, '/');
    75         if (spot) {
    76             return SDL_strdup(spot + 1);
    77         } else {
    78             return SDL_strdup(linkfile);
    79         }
    80     }
    81 #endif /* __LINUX__ || __FREEBSD__ */
    82 
    83     /* Finally use the default we've used forever */
    84     return SDL_strdup("SDL_App");
    85 }
    86 
    87 /* X11 driver bootstrap functions */
    88 
    89 static int
    90 X11_Available(void)
    91 {
    92     Display *display = NULL;
    93     if (SDL_X11_LoadSymbols()) {
    94         display = XOpenDisplay(NULL);
    95         if (display != NULL) {
    96             XCloseDisplay(display);
    97         }
    98         SDL_X11_UnloadSymbols();
    99     }
   100     return (display != NULL);
   101 }
   102 
   103 static void
   104 X11_DeleteDevice(SDL_VideoDevice * device)
   105 {
   106     SDL_VideoData *data = (SDL_VideoData *) device->driverdata;
   107     if (data->display) {
   108         XCloseDisplay(data->display);
   109     }
   110     SDL_free(data->windowlist);
   111     SDL_free(device->driverdata);
   112 #if SDL_VIDEO_DRIVER_PANDORA
   113     SDL_free(device->gles_data);
   114 #endif
   115     SDL_free(device);
   116 
   117     SDL_X11_UnloadSymbols();
   118 }
   119 
   120 static SDL_VideoDevice *
   121 X11_CreateDevice(int devindex)
   122 {
   123     SDL_VideoDevice *device;
   124     SDL_VideoData *data;
   125     const char *display = NULL; /* Use the DISPLAY environment variable */
   126 
   127     if (!SDL_X11_LoadSymbols()) {
   128         return NULL;
   129     }
   130 
   131     /* Initialize all variables that we clean on shutdown */
   132     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
   133     if (!device) {
   134         SDL_OutOfMemory();
   135         return NULL;
   136     }
   137     data = (struct SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
   138     if (!data) {
   139         SDL_OutOfMemory();
   140         SDL_free(device);
   141         return NULL;
   142     }
   143     device->driverdata = data;
   144 
   145 #if SDL_VIDEO_DRIVER_PANDORA
   146     device->gles_data = (struct SDL_PrivateGLESData *) SDL_calloc(1, sizeof(SDL_PrivateGLESData));
   147     if (!device->gles_data) {
   148         SDL_OutOfMemory();
   149         return NULL;
   150     }
   151 #endif
   152 
   153     /* FIXME: Do we need this?
   154        if ( (SDL_strncmp(XDisplayName(display), ":", 1) == 0) ||
   155        (SDL_strncmp(XDisplayName(display), "unix:", 5) == 0) ) {
   156        local_X11 = 1;
   157        } else {
   158        local_X11 = 0;
   159        }
   160      */
   161     data->display = XOpenDisplay(display);
   162 #if defined(__osf__) && defined(SDL_VIDEO_DRIVER_X11_DYNAMIC)
   163     /* On Tru64 if linking without -lX11, it fails and you get following message.
   164      * Xlib: connection to ":0.0" refused by server
   165      * Xlib: XDM authorization key matches an existing client!
   166      *
   167      * It succeeds if retrying 1 second later
   168      * or if running xhost +localhost on shell.
   169      */
   170     if (data->display == NULL) {
   171         SDL_Delay(1000);
   172         data->display = XOpenDisplay(display);
   173     }
   174 #endif
   175     if (data->display == NULL) {
   176         SDL_free(device);
   177         SDL_SetError("Couldn't open X11 display");
   178         return NULL;
   179     }
   180 #ifdef X11_DEBUG
   181     XSynchronize(data->display, True);
   182 #endif
   183 
   184     /* Set the function pointers */
   185     device->VideoInit = X11_VideoInit;
   186     device->VideoQuit = X11_VideoQuit;
   187     device->GetDisplayModes = X11_GetDisplayModes;
   188     device->SetDisplayMode = X11_SetDisplayMode;
   189     device->SetDisplayGammaRamp = X11_SetDisplayGammaRamp;
   190     device->GetDisplayGammaRamp = X11_GetDisplayGammaRamp;
   191     device->SuspendScreenSaver = X11_SuspendScreenSaver;
   192     device->PumpEvents = X11_PumpEvents;
   193 
   194     device->CreateWindow = X11_CreateWindow;
   195     device->CreateWindowFrom = X11_CreateWindowFrom;
   196     device->SetWindowTitle = X11_SetWindowTitle;
   197     device->SetWindowIcon = X11_SetWindowIcon;
   198     device->SetWindowPosition = X11_SetWindowPosition;
   199     device->SetWindowSize = X11_SetWindowSize;
   200     device->ShowWindow = X11_ShowWindow;
   201     device->HideWindow = X11_HideWindow;
   202     device->RaiseWindow = X11_RaiseWindow;
   203     device->MaximizeWindow = X11_MaximizeWindow;
   204     device->MinimizeWindow = X11_MinimizeWindow;
   205     device->RestoreWindow = X11_RestoreWindow;
   206     device->SetWindowGrab = X11_SetWindowGrab;
   207     device->DestroyWindow = X11_DestroyWindow;
   208     device->CreateWindowFramebuffer = X11_CreateWindowFramebuffer;
   209     device->UpdateWindowFramebuffer = X11_UpdateWindowFramebuffer;
   210     device->DestroyWindowFramebuffer = X11_DestroyWindowFramebuffer;
   211     device->GetWindowWMInfo = X11_GetWindowWMInfo;
   212 
   213     device->shape_driver.CreateShaper = X11_CreateShaper;
   214     device->shape_driver.SetWindowShape = X11_SetWindowShape;
   215     device->shape_driver.ResizeWindowShape = X11_ResizeWindowShape;
   216 
   217 #if SDL_VIDEO_OPENGL_GLX
   218     device->GL_LoadLibrary = X11_GL_LoadLibrary;
   219     device->GL_GetProcAddress = X11_GL_GetProcAddress;
   220     device->GL_UnloadLibrary = X11_GL_UnloadLibrary;
   221     device->GL_CreateContext = X11_GL_CreateContext;
   222     device->GL_MakeCurrent = X11_GL_MakeCurrent;
   223     device->GL_SetSwapInterval = X11_GL_SetSwapInterval;
   224     device->GL_GetSwapInterval = X11_GL_GetSwapInterval;
   225     device->GL_SwapWindow = X11_GL_SwapWindow;
   226     device->GL_DeleteContext = X11_GL_DeleteContext;
   227 #endif
   228 #if SDL_VIDEO_DRIVER_PANDORA
   229     device->GL_LoadLibrary = X11_GLES_LoadLibrary;
   230     device->GL_GetProcAddress = X11_GLES_GetProcAddress;
   231     device->GL_UnloadLibrary = X11_GLES_UnloadLibrary;
   232     device->GL_CreateContext = X11_GLES_CreateContext;
   233     device->GL_MakeCurrent = X11_GLES_MakeCurrent;
   234     device->GL_SetSwapInterval = X11_GLES_SetSwapInterval;
   235     device->GL_GetSwapInterval = X11_GLES_GetSwapInterval;
   236     device->GL_SwapWindow = X11_GLES_SwapWindow;
   237     device->GL_DeleteContext = X11_GLES_DeleteContext;
   238 #endif
   239 
   240     device->SetClipboardText = X11_SetClipboardText;
   241     device->GetClipboardText = X11_GetClipboardText;
   242     device->HasClipboardText = X11_HasClipboardText;
   243 
   244     device->free = X11_DeleteDevice;
   245 
   246     return device;
   247 }
   248 
   249 VideoBootStrap X11_bootstrap = {
   250     "x11", "SDL X11 video driver",
   251     X11_Available, X11_CreateDevice
   252 };
   253 
   254 static int (*handler) (Display *, XErrorEvent *) = NULL;
   255 static int
   256 X11_CheckWindowManagerErrorHandler(Display * d, XErrorEvent * e)
   257 {
   258     if (e->error_code == BadWindow) {
   259         return (0);
   260     } else {
   261         return (handler(d, e));
   262     }
   263 }
   264 
   265 static void
   266 X11_CheckWindowManager(_THIS)
   267 {
   268     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   269     Display *display = data->display;
   270     Atom _NET_SUPPORTING_WM_CHECK;
   271     int status, real_format;
   272     Atom real_type;
   273     unsigned long items_read, items_left;
   274     unsigned char *propdata;
   275     Window wm_window = 0;
   276 #ifdef DEBUG_WINDOW_MANAGER
   277     char *wm_name;
   278 #endif
   279 
   280     /* Set up a handler to gracefully catch errors */
   281     XSync(display, False);
   282     handler = XSetErrorHandler(X11_CheckWindowManagerErrorHandler);
   283 
   284     _NET_SUPPORTING_WM_CHECK = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
   285     status = XGetWindowProperty(display, DefaultRootWindow(display), _NET_SUPPORTING_WM_CHECK, 0L, 1L, False, XA_WINDOW, &real_type, &real_format, &items_read, &items_left, &propdata);
   286     if (status == Success && items_read) {
   287         wm_window = ((Window*)propdata)[0];
   288     }
   289     if (propdata) {
   290         XFree(propdata);
   291     }
   292 
   293     if (wm_window) {
   294         status = XGetWindowProperty(display, wm_window, _NET_SUPPORTING_WM_CHECK, 0L, 1L, False, XA_WINDOW, &real_type, &real_format, &items_read, &items_left, &propdata);
   295         if (status != Success || !items_read || wm_window != ((Window*)propdata)[0]) {
   296             wm_window = None;
   297         }
   298         if (propdata) {
   299             XFree(propdata);
   300         }
   301     }
   302 
   303     /* Reset the error handler, we're done checking */
   304     XSync(display, False);
   305     XSetErrorHandler(handler);
   306 
   307     if (!wm_window) {
   308 #ifdef DEBUG_WINDOW_MANAGER
   309         printf("Couldn't get _NET_SUPPORTING_WM_CHECK property\n");
   310 #endif
   311         return;
   312     }
   313     data->net_wm = SDL_TRUE;
   314 
   315 #ifdef DEBUG_WINDOW_MANAGER
   316     wm_name = X11_GetWindowTitle(_this, wm_window);
   317     printf("Window manager: %s\n", wm_name);
   318     SDL_free(wm_name);
   319 #endif
   320 }
   321 
   322 int
   323 X11_VideoInit(_THIS)
   324 {
   325     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   326 
   327     /* Get the window class name, usually the name of the application */
   328     data->classname = get_classname();
   329 
   330     /* Open a connection to the X input manager */
   331 #ifdef X_HAVE_UTF8_STRING
   332     if (SDL_X11_HAVE_UTF8) {
   333         data->im =
   334             XOpenIM(data->display, NULL, data->classname, data->classname);
   335     }
   336 #endif
   337 
   338     /* Look up some useful Atoms */
   339 #define GET_ATOM(X) data->X = XInternAtom(data->display, #X, False)
   340     GET_ATOM(WM_DELETE_WINDOW);
   341     GET_ATOM(_NET_WM_STATE);
   342     GET_ATOM(_NET_WM_STATE_HIDDEN);
   343     GET_ATOM(_NET_WM_STATE_MAXIMIZED_VERT);
   344     GET_ATOM(_NET_WM_STATE_MAXIMIZED_HORZ);
   345     GET_ATOM(_NET_WM_STATE_FULLSCREEN);
   346     GET_ATOM(_NET_WM_NAME);
   347     GET_ATOM(_NET_WM_ICON_NAME);
   348     GET_ATOM(_NET_WM_ICON);
   349     GET_ATOM(UTF8_STRING);
   350 
   351     /* Detect the window manager */
   352     X11_CheckWindowManager(_this);
   353 
   354     if (X11_InitModes(_this) < 0) {
   355         return -1;
   356     }
   357 
   358     if (X11_InitKeyboard(_this) != 0) {
   359         return -1;
   360     }
   361     X11_InitMouse(_this);
   362 
   363     X11_InitTouch(_this);
   364     return 0;
   365 }
   366 
   367 void
   368 X11_VideoQuit(_THIS)
   369 {
   370     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   371 
   372     if (data->classname) {
   373         SDL_free(data->classname);
   374     }
   375 #ifdef X_HAVE_UTF8_STRING
   376     if (data->im) {
   377         XCloseIM(data->im);
   378     }
   379 #endif
   380 
   381     X11_QuitModes(_this);
   382     X11_QuitKeyboard(_this);
   383     X11_QuitMouse(_this);
   384     X11_QuitTouch(_this);
   385 }
   386 
   387 SDL_bool
   388 X11_UseDirectColorVisuals(void)
   389 {
   390     /* Once we implement DirectColor colormaps and gamma ramp support...
   391        return SDL_getenv("SDL_VIDEO_X11_NODIRECTCOLOR") ? SDL_FALSE : SDL_TRUE;
   392      */
   393     return SDL_FALSE;
   394 }
   395 
   396 /* vim: set ts=4 sw=4 expandtab: */