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