src/video/x11/SDL_x11video.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Mar 2011 08:49:20 -0800
changeset 5466 006883d5fa51
parent 5302 b1b9ec9874ba
child 5481 22dfc3958dc3
permissions -rw-r--r--
Gamma support is back!

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