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