src/video/x11/SDL_x11video.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 21 Jan 2013 02:50:11 -0500
changeset 6817 93103b719488
parent 6774 ad8522052ce6
child 6885 700f1b25f77f
permissions -rw-r--r--
Cleaned out a few C++ single-line comments.

Should we bother doing this anymore? Is there really a C compiler we care
about that doesn't support this?
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
icculus@6774
    42
/* !!! FIXME: move dbus stuff to somewhere under src/core/linux ... */
icculus@6774
    43
#if SDL_USE_LIBDBUS
icculus@6774
    44
/* we never link directly to libdbus. */
icculus@6774
    45
#include "SDL_loadso.h"
icculus@6774
    46
static const char *dbus_library = "libdbus-1.so.3";
icculus@6774
    47
static void *dbus_handle = NULL;
icculus@6774
    48
icculus@6774
    49
/* !!! FIXME: this is kinda ugly. */
icculus@6774
    50
static SDL_bool
icculus@6774
    51
load_dbus_sym(const char *fn, void **addr)
icculus@6774
    52
{
icculus@6774
    53
    *addr = SDL_LoadFunction(dbus_handle, fn);
icculus@6774
    54
    if (*addr == NULL) {
icculus@6774
    55
        /* Don't call SDL_SetError(): SDL_LoadFunction already did. */
icculus@6774
    56
        return SDL_FALSE;
icculus@6774
    57
    }
icculus@6774
    58
icculus@6774
    59
    return SDL_TRUE;
icculus@6774
    60
}
icculus@6774
    61
icculus@6774
    62
/* libdbus entry points... */
icculus@6774
    63
static DBusConnection *(*DBUS_dbus_bus_get_private)(DBusBusType, DBusError *) = NULL;
icculus@6774
    64
static void (*DBUS_dbus_connection_set_exit_on_disconnect)(DBusConnection *, dbus_bool_t) = NULL;
icculus@6774
    65
static dbus_bool_t (*DBUS_dbus_connection_send)(DBusConnection *, DBusMessage *, dbus_uint32_t *) = NULL;
icculus@6774
    66
static void (*DBUS_dbus_connection_close)(DBusConnection *) = NULL;
icculus@6774
    67
static void (*DBUS_dbus_connection_unref)(DBusConnection *) = NULL;
icculus@6774
    68
static void (*DBUS_dbus_connection_flush)(DBusConnection *) = NULL;
icculus@6774
    69
static DBusMessage *(*DBUS_dbus_message_new_method_call)(const char *, const char *, const char *, const char *) = NULL;
icculus@6774
    70
static void (*DBUS_dbus_message_unref)(DBusMessage *) = NULL;
icculus@6774
    71
static void (*DBUS_dbus_error_init)(DBusError *) = NULL;
icculus@6774
    72
static dbus_bool_t (*DBUS_dbus_error_is_set)(const DBusError *) = NULL;
icculus@6774
    73
static void (*DBUS_dbus_error_free)(DBusError *) = NULL;
icculus@6774
    74
icculus@6774
    75
static int
icculus@6774
    76
load_dbus_syms(void)
icculus@6774
    77
{
icculus@6774
    78
    /* cast funcs to char* first, to please GCC's strict aliasing rules. */
icculus@6774
    79
    #define SDL_DBUS_SYM(x) \
icculus@6774
    80
        if (!load_dbus_sym(#x, (void **) (char *) &DBUS_##x)) return -1
icculus@6774
    81
icculus@6774
    82
    SDL_DBUS_SYM(dbus_bus_get_private);
icculus@6774
    83
    SDL_DBUS_SYM(dbus_connection_set_exit_on_disconnect);
icculus@6774
    84
    SDL_DBUS_SYM(dbus_connection_send);
icculus@6774
    85
    SDL_DBUS_SYM(dbus_connection_close);
icculus@6774
    86
    SDL_DBUS_SYM(dbus_connection_unref);
icculus@6774
    87
    SDL_DBUS_SYM(dbus_connection_flush);
icculus@6774
    88
    SDL_DBUS_SYM(dbus_message_new_method_call);
icculus@6774
    89
    SDL_DBUS_SYM(dbus_message_unref);
icculus@6774
    90
    SDL_DBUS_SYM(dbus_error_init);
icculus@6774
    91
    SDL_DBUS_SYM(dbus_error_is_set);
icculus@6774
    92
    SDL_DBUS_SYM(dbus_error_free);
icculus@6774
    93
icculus@6774
    94
    #undef SDL_DBUS_SYM
icculus@6774
    95
icculus@6774
    96
    return 0;
icculus@6774
    97
}
icculus@6774
    98
icculus@6774
    99
static void
icculus@6774
   100
UnloadDBUSLibrary(void)
icculus@6774
   101
{
icculus@6774
   102
    if (dbus_handle != NULL) {
icculus@6774
   103
        SDL_UnloadObject(dbus_handle);
icculus@6774
   104
        dbus_handle = NULL;
icculus@6774
   105
    }
icculus@6774
   106
}
icculus@6774
   107
icculus@6774
   108
static int
icculus@6774
   109
LoadDBUSLibrary(void)
icculus@6774
   110
{
icculus@6774
   111
    int retval = 0;
icculus@6774
   112
    if (dbus_handle == NULL) {
icculus@6774
   113
        dbus_handle = SDL_LoadObject(dbus_library);
icculus@6774
   114
        if (dbus_handle == NULL) {
icculus@6774
   115
            retval = -1;
icculus@6774
   116
            /* Don't call SDL_SetError(): SDL_LoadObject already did. */
icculus@6774
   117
        } else {
icculus@6774
   118
            retval = load_dbus_syms();
icculus@6774
   119
            if (retval < 0) {
icculus@6774
   120
                UnloadDBUSLibrary();
icculus@6774
   121
            }
icculus@6774
   122
        }
icculus@6774
   123
    }
icculus@6774
   124
icculus@6774
   125
    return retval;
icculus@6774
   126
}
icculus@6774
   127
icculus@6774
   128
static void
icculus@6774
   129
X11_InitDBus(_THIS)
icculus@6774
   130
{
icculus@6774
   131
    if (LoadDBUSLibrary() != -1) {
icculus@6774
   132
        SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
icculus@6774
   133
        DBusError err;
icculus@6774
   134
        DBUS_dbus_error_init(&err);
icculus@6774
   135
        data->dbus = DBUS_dbus_bus_get_private(DBUS_BUS_SESSION, &err);
icculus@6774
   136
        if (DBUS_dbus_error_is_set(&err)) {
icculus@6774
   137
            DBUS_dbus_error_free(&err);
icculus@6774
   138
            if (data->dbus) {
icculus@6774
   139
                DBUS_dbus_connection_unref(data->dbus);
icculus@6774
   140
                data->dbus = NULL;
icculus@6774
   141
            }
icculus@6774
   142
            return;  /* oh well */
icculus@6774
   143
        }
icculus@6774
   144
        DBUS_dbus_connection_set_exit_on_disconnect(data->dbus, 0);
icculus@6774
   145
    }
icculus@6774
   146
}
icculus@6774
   147
icculus@6774
   148
static void
icculus@6774
   149
X11_QuitDBus(_THIS)
icculus@6774
   150
{
icculus@6774
   151
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
icculus@6774
   152
    if (data->dbus) {
icculus@6774
   153
        DBUS_dbus_connection_close(data->dbus);
icculus@6774
   154
        DBUS_dbus_connection_unref(data->dbus);
icculus@6774
   155
        data->dbus = NULL;
icculus@6774
   156
    }
icculus@6774
   157
}
icculus@6774
   158
icculus@6774
   159
void
icculus@6774
   160
SDL_dbus_screensaver_tickle(_THIS)
icculus@6774
   161
{
icculus@6774
   162
    const SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
icculus@6774
   163
    DBusConnection *conn = data->dbus;
icculus@6774
   164
    if (conn != NULL) {
icculus@6774
   165
        DBusMessage *msg = DBUS_dbus_message_new_method_call("org.gnome.ScreenSaver",
icculus@6774
   166
                                                             "/org/gnome/ScreenSaver",
icculus@6774
   167
                                                             "org.gnome.ScreenSaver",
icculus@6774
   168
                                                             "SimulateUserActivity");
icculus@6774
   169
        if (msg != NULL) {
icculus@6774
   170
            if (DBUS_dbus_connection_send(conn, msg, NULL)) {
icculus@6774
   171
                DBUS_dbus_connection_flush(conn);
icculus@6774
   172
            }
icculus@6774
   173
            DBUS_dbus_message_unref(msg);
icculus@6774
   174
        }
icculus@6774
   175
    }
icculus@6774
   176
}
icculus@6774
   177
#endif
icculus@6774
   178
slouken@1950
   179
/* Initialization/Query functions */
slouken@1950
   180
static int X11_VideoInit(_THIS);
slouken@1950
   181
static void X11_VideoQuit(_THIS);
slouken@1950
   182
slouken@1951
   183
/* Find out what class name we should use */
slouken@1951
   184
static char *
slouken@1951
   185
get_classname()
slouken@1951
   186
{
slouken@1951
   187
    char *spot;
slouken@1951
   188
#if defined(__LINUX__) || defined(__FREEBSD__)
slouken@1951
   189
    char procfile[1024];
slouken@1951
   190
    char linkfile[1024];
slouken@1951
   191
    int linksize;
slouken@1951
   192
#endif
slouken@1951
   193
slouken@1951
   194
    /* First allow environment variable override */
slouken@1951
   195
    spot = SDL_getenv("SDL_VIDEO_X11_WMCLASS");
slouken@1951
   196
    if (spot) {
slouken@1951
   197
        return SDL_strdup(spot);
slouken@1951
   198
    }
slouken@1951
   199
slouken@1951
   200
    /* Next look at the application's executable name */
slouken@1951
   201
#if defined(__LINUX__) || defined(__FREEBSD__)
slouken@1951
   202
#if defined(__LINUX__)
slouken@1951
   203
    SDL_snprintf(procfile, SDL_arraysize(procfile), "/proc/%d/exe", getpid());
slouken@1951
   204
#elif defined(__FREEBSD__)
slouken@1951
   205
    SDL_snprintf(procfile, SDL_arraysize(procfile), "/proc/%d/file",
slouken@1951
   206
                 getpid());
slouken@1951
   207
#else
slouken@1951
   208
#error Where can we find the executable name?
slouken@1951
   209
#endif
slouken@1951
   210
    linksize = readlink(procfile, linkfile, sizeof(linkfile) - 1);
slouken@1951
   211
    if (linksize > 0) {
slouken@1951
   212
        linkfile[linksize] = '\0';
slouken@1951
   213
        spot = SDL_strrchr(linkfile, '/');
slouken@1951
   214
        if (spot) {
slouken@1951
   215
            return SDL_strdup(spot + 1);
slouken@1951
   216
        } else {
slouken@1951
   217
            return SDL_strdup(linkfile);
slouken@1951
   218
        }
slouken@1951
   219
    }
slouken@1951
   220
#endif /* __LINUX__ || __FREEBSD__ */
slouken@1951
   221
slouken@1951
   222
    /* Finally use the default we've used forever */
slouken@1951
   223
    return SDL_strdup("SDL_App");
slouken@1951
   224
}
slouken@1951
   225
slouken@1950
   226
/* X11 driver bootstrap functions */
slouken@1950
   227
slouken@1950
   228
static int
slouken@1950
   229
X11_Available(void)
slouken@1950
   230
{
slouken@1950
   231
    Display *display = NULL;
slouken@1950
   232
    if (SDL_X11_LoadSymbols()) {
slouken@1950
   233
        display = XOpenDisplay(NULL);
slouken@1950
   234
        if (display != NULL) {
slouken@1950
   235
            XCloseDisplay(display);
slouken@1950
   236
        }
slouken@1950
   237
        SDL_X11_UnloadSymbols();
slouken@1950
   238
    }
slouken@1950
   239
    return (display != NULL);
slouken@1950
   240
}
slouken@1950
   241
slouken@1950
   242
static void
slouken@1950
   243
X11_DeleteDevice(SDL_VideoDevice * device)
slouken@1950
   244
{
slouken@1950
   245
    SDL_VideoData *data = (SDL_VideoData *) device->driverdata;
slouken@1950
   246
    if (data->display) {
slouken@1950
   247
        XCloseDisplay(data->display);
slouken@1950
   248
    }
bob@2324
   249
    SDL_free(data->windowlist);
slouken@1950
   250
    SDL_free(device->driverdata);
slouken@1950
   251
    SDL_free(device);
slouken@1950
   252
slouken@1950
   253
    SDL_X11_UnloadSymbols();
slouken@1950
   254
}
slouken@1950
   255
icculus@6498
   256
/* An error handler to reset the vidmode and then call the default handler. */
icculus@6498
   257
static SDL_bool safety_net_triggered = SDL_FALSE;
icculus@6498
   258
static int (*orig_x11_errhandler) (Display *, XErrorEvent *) = NULL;
icculus@6498
   259
static int
icculus@6498
   260
X11_SafetyNetErrHandler(Display * d, XErrorEvent * e)
icculus@6498
   261
{
icculus@6498
   262
    /* if we trigger an error in our error handler, don't try again. */
icculus@6498
   263
    if (!safety_net_triggered) {
icculus@6498
   264
        safety_net_triggered = SDL_TRUE;
icculus@6498
   265
        SDL_VideoDevice *device = SDL_GetVideoDevice();
icculus@6498
   266
        if (device != NULL) {
icculus@6498
   267
            int i;
icculus@6498
   268
            for (i = 0; i < device->num_displays; i++) {
icculus@6498
   269
                SDL_VideoDisplay *display = &device->displays[i];
icculus@6498
   270
                if (SDL_memcmp(&display->current_mode, &display->desktop_mode,
icculus@6498
   271
                               sizeof (SDL_DisplayMode)) != 0) {
icculus@6498
   272
                    X11_SetDisplayMode(device, display, &display->desktop_mode);
icculus@6498
   273
                }
icculus@6498
   274
            }
icculus@6498
   275
        }
icculus@6498
   276
    }
icculus@6498
   277
icculus@6498
   278
    if (orig_x11_errhandler != NULL) {
icculus@6498
   279
        return orig_x11_errhandler(d, e);  /* probably terminate. */
icculus@6498
   280
    }
icculus@6498
   281
icculus@6498
   282
    return 0;
icculus@6498
   283
}
icculus@6498
   284
slouken@1950
   285
static SDL_VideoDevice *
slouken@1950
   286
X11_CreateDevice(int devindex)
slouken@1950
   287
{
slouken@1950
   288
    SDL_VideoDevice *device;
slouken@1950
   289
    SDL_VideoData *data;
slouken@1950
   290
    const char *display = NULL; /* Use the DISPLAY environment variable */
slouken@1950
   291
slouken@1950
   292
    if (!SDL_X11_LoadSymbols()) {
slouken@1950
   293
        return NULL;
slouken@1950
   294
    }
slouken@1950
   295
icculus@6817
   296
    /* Need for threading gl calls. This is also required for the proprietary
icculus@6817
   297
        nVidia driver to be threaded. */
mikesart@6569
   298
    XInitThreads();
mikesart@6569
   299
slouken@1950
   300
    /* Initialize all variables that we clean on shutdown */
slouken@1950
   301
    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
bob@2323
   302
    if (!device) {
bob@2323
   303
        SDL_OutOfMemory();
bob@2323
   304
        return NULL;
slouken@1950
   305
    }
bob@2323
   306
    data = (struct SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
bob@2323
   307
    if (!data) {
slouken@1950
   308
        SDL_OutOfMemory();
bob@2323
   309
        SDL_free(device);
slouken@1950
   310
        return NULL;
slouken@1950
   311
    }
slouken@1950
   312
    device->driverdata = data;
slouken@1950
   313
slouken@1950
   314
    /* FIXME: Do we need this?
slouken@1950
   315
       if ( (SDL_strncmp(XDisplayName(display), ":", 1) == 0) ||
slouken@1950
   316
       (SDL_strncmp(XDisplayName(display), "unix:", 5) == 0) ) {
slouken@1950
   317
       local_X11 = 1;
slouken@1950
   318
       } else {
slouken@1950
   319
       local_X11 = 0;
slouken@1950
   320
       }
slouken@1950
   321
     */
slouken@1950
   322
    data->display = XOpenDisplay(display);
slouken@1950
   323
#if defined(__osf__) && defined(SDL_VIDEO_DRIVER_X11_DYNAMIC)
slouken@1950
   324
    /* On Tru64 if linking without -lX11, it fails and you get following message.
slouken@1950
   325
     * Xlib: connection to ":0.0" refused by server
slouken@1950
   326
     * Xlib: XDM authorization key matches an existing client!
slouken@1950
   327
     *
slouken@1950
   328
     * It succeeds if retrying 1 second later
slouken@1950
   329
     * or if running xhost +localhost on shell.
slouken@1950
   330
     */
slouken@1950
   331
    if (data->display == NULL) {
slouken@1950
   332
        SDL_Delay(1000);
slouken@1950
   333
        data->display = XOpenDisplay(display);
slouken@1950
   334
    }
slouken@1950
   335
#endif
slouken@1950
   336
    if (data->display == NULL) {
slouken@6367
   337
        SDL_free(device->driverdata);
slouken@1950
   338
        SDL_free(device);
slouken@1950
   339
        SDL_SetError("Couldn't open X11 display");
slouken@1950
   340
        return NULL;
slouken@1950
   341
    }
slouken@1950
   342
#ifdef X11_DEBUG
slouken@1950
   343
    XSynchronize(data->display, True);
slouken@1950
   344
#endif
slouken@1950
   345
icculus@6498
   346
    /* Hook up an X11 error handler to recover the desktop resolution. */
icculus@6498
   347
    safety_net_triggered = SDL_FALSE;
icculus@6498
   348
    orig_x11_errhandler = XSetErrorHandler(X11_SafetyNetErrHandler);
icculus@6498
   349
slouken@1950
   350
    /* Set the function pointers */
slouken@1950
   351
    device->VideoInit = X11_VideoInit;
slouken@1950
   352
    device->VideoQuit = X11_VideoQuit;
slouken@1950
   353
    device->GetDisplayModes = X11_GetDisplayModes;
gabomdq@6331
   354
    device->GetDisplayBounds = X11_GetDisplayBounds;
slouken@1950
   355
    device->SetDisplayMode = X11_SetDisplayMode;
slouken@3025
   356
    device->SuspendScreenSaver = X11_SuspendScreenSaver;
slouken@1951
   357
    device->PumpEvents = X11_PumpEvents;
slouken@1950
   358
slouken@1950
   359
    device->CreateWindow = X11_CreateWindow;
slouken@1950
   360
    device->CreateWindowFrom = X11_CreateWindowFrom;
slouken@1950
   361
    device->SetWindowTitle = X11_SetWindowTitle;
slouken@2967
   362
    device->SetWindowIcon = X11_SetWindowIcon;
slouken@1950
   363
    device->SetWindowPosition = X11_SetWindowPosition;
slouken@1950
   364
    device->SetWindowSize = X11_SetWindowSize;
slouken@1950
   365
    device->ShowWindow = X11_ShowWindow;
slouken@1950
   366
    device->HideWindow = X11_HideWindow;
slouken@1950
   367
    device->RaiseWindow = X11_RaiseWindow;
slouken@1950
   368
    device->MaximizeWindow = X11_MaximizeWindow;
slouken@1950
   369
    device->MinimizeWindow = X11_MinimizeWindow;
slouken@1950
   370
    device->RestoreWindow = X11_RestoreWindow;
icculus@6422
   371
    device->SetWindowBordered = X11_SetWindowBordered;
slouken@5302
   372
    device->SetWindowFullscreen = X11_SetWindowFullscreen;
slouken@5466
   373
    device->SetWindowGammaRamp = X11_SetWindowGammaRamp;
slouken@1950
   374
    device->SetWindowGrab = X11_SetWindowGrab;
slouken@1950
   375
    device->DestroyWindow = X11_DestroyWindow;
slouken@5182
   376
    device->CreateWindowFramebuffer = X11_CreateWindowFramebuffer;
slouken@5182
   377
    device->UpdateWindowFramebuffer = X11_UpdateWindowFramebuffer;
slouken@5182
   378
    device->DestroyWindowFramebuffer = X11_DestroyWindowFramebuffer;
slouken@1950
   379
    device->GetWindowWMInfo = X11_GetWindowWMInfo;
slouken@5182
   380
eligottlieb@4782
   381
    device->shape_driver.CreateShaper = X11_CreateShaper;
eligottlieb@4782
   382
    device->shape_driver.SetWindowShape = X11_SetWindowShape;
eligottlieb@4782
   383
    device->shape_driver.ResizeWindowShape = X11_ResizeWindowShape;
slouken@5182
   384
slouken@5088
   385
#if SDL_VIDEO_OPENGL_GLX
slouken@6370
   386
    device->GL_LoadLibrary = X11_GL_LoadLibrary;
slouken@6370
   387
    device->GL_GetProcAddress = X11_GL_GetProcAddress;
slouken@6370
   388
    device->GL_UnloadLibrary = X11_GL_UnloadLibrary;
slouken@6370
   389
    device->GL_CreateContext = X11_GL_CreateContext;
slouken@6370
   390
    device->GL_MakeCurrent = X11_GL_MakeCurrent;
slouken@6370
   391
    device->GL_SetSwapInterval = X11_GL_SetSwapInterval;
slouken@6370
   392
    device->GL_GetSwapInterval = X11_GL_GetSwapInterval;
slouken@6370
   393
    device->GL_SwapWindow = X11_GL_SwapWindow;
slouken@6370
   394
    device->GL_DeleteContext = X11_GL_DeleteContext;
slouken@6370
   395
#elif SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
slouken@6370
   396
    device->GL_LoadLibrary = X11_GLES_LoadLibrary;
slouken@6370
   397
    device->GL_GetProcAddress = X11_GLES_GetProcAddress;
slouken@6370
   398
    device->GL_UnloadLibrary = X11_GLES_UnloadLibrary;
slouken@6370
   399
    device->GL_CreateContext = X11_GLES_CreateContext;
slouken@6370
   400
    device->GL_MakeCurrent = X11_GLES_MakeCurrent;
slouken@6370
   401
    device->GL_SetSwapInterval = X11_GLES_SetSwapInterval;
slouken@6370
   402
    device->GL_GetSwapInterval = X11_GLES_GetSwapInterval;
slouken@6370
   403
    device->GL_SwapWindow = X11_GLES_SwapWindow;
slouken@6370
   404
    device->GL_DeleteContext = X11_GLES_DeleteContext;
slouken@3218
   405
#endif
slouken@1950
   406
slouken@4508
   407
    device->SetClipboardText = X11_SetClipboardText;
slouken@4508
   408
    device->GetClipboardText = X11_GetClipboardText;
slouken@4508
   409
    device->HasClipboardText = X11_HasClipboardText;
slouken@4508
   410
slouken@1950
   411
    device->free = X11_DeleteDevice;
slouken@1950
   412
slouken@1950
   413
    return device;
slouken@1950
   414
}
slouken@1950
   415
slouken@1950
   416
VideoBootStrap X11_bootstrap = {
slouken@1950
   417
    "x11", "SDL X11 video driver",
slouken@1950
   418
    X11_Available, X11_CreateDevice
slouken@1950
   419
};
slouken@1950
   420
slouken@4559
   421
static int (*handler) (Display *, XErrorEvent *) = NULL;
slouken@4559
   422
static int
slouken@4559
   423
X11_CheckWindowManagerErrorHandler(Display * d, XErrorEvent * e)
slouken@4559
   424
{
slouken@4559
   425
    if (e->error_code == BadWindow) {
slouken@4559
   426
        return (0);
slouken@4559
   427
    } else {
slouken@4559
   428
        return (handler(d, e));
slouken@4559
   429
    }
slouken@4559
   430
}
slouken@4508
   431
slouken@4518
   432
static void
slouken@4518
   433
X11_CheckWindowManager(_THIS)
slouken@4518
   434
{
slouken@4518
   435
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@4518
   436
    Display *display = data->display;
slouken@4518
   437
    Atom _NET_SUPPORTING_WM_CHECK;
slouken@4518
   438
    int status, real_format;
slouken@4518
   439
    Atom real_type;
slouken@4518
   440
    unsigned long items_read, items_left;
slouken@4518
   441
    unsigned char *propdata;
slouken@4518
   442
    Window wm_window = 0;
slouken@4518
   443
#ifdef DEBUG_WINDOW_MANAGER
slouken@4518
   444
    char *wm_name;
slouken@4518
   445
#endif
slouken@4518
   446
slouken@4559
   447
    /* Set up a handler to gracefully catch errors */
slouken@4559
   448
    XSync(display, False);
slouken@4559
   449
    handler = XSetErrorHandler(X11_CheckWindowManagerErrorHandler);
slouken@4559
   450
slouken@4518
   451
    _NET_SUPPORTING_WM_CHECK = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
slouken@4518
   452
    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
   453
    if (status == Success && items_read) {
slouken@4518
   454
        wm_window = ((Window*)propdata)[0];
slouken@4518
   455
    }
slouken@4559
   456
    if (propdata) {
slouken@4559
   457
        XFree(propdata);
slouken@4559
   458
    }
slouken@4559
   459
slouken@4559
   460
    if (wm_window) {
slouken@4559
   461
        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
   462
        if (status != Success || !items_read || wm_window != ((Window*)propdata)[0]) {
slouken@4559
   463
            wm_window = None;
slouken@4559
   464
        }
slouken@4559
   465
        if (propdata) {
slouken@4559
   466
            XFree(propdata);
slouken@4559
   467
        }
slouken@4559
   468
    }
slouken@4559
   469
slouken@4559
   470
    /* Reset the error handler, we're done checking */
slouken@4559
   471
    XSync(display, False);
slouken@4559
   472
    XSetErrorHandler(handler);
slouken@4518
   473
slouken@4518
   474
    if (!wm_window) {
slouken@4518
   475
#ifdef DEBUG_WINDOW_MANAGER
slouken@4518
   476
        printf("Couldn't get _NET_SUPPORTING_WM_CHECK property\n");
slouken@4518
   477
#endif
slouken@4518
   478
        return;
slouken@4518
   479
    }
slouken@4518
   480
    data->net_wm = SDL_TRUE;
slouken@4518
   481
slouken@4518
   482
#ifdef DEBUG_WINDOW_MANAGER
slouken@4518
   483
    wm_name = X11_GetWindowTitle(_this, wm_window);
slouken@4518
   484
    printf("Window manager: %s\n", wm_name);
slouken@4518
   485
    SDL_free(wm_name);
slouken@4518
   486
#endif
slouken@4518
   487
}
slouken@1950
   488
icculus@6774
   489
slouken@1950
   490
int
slouken@1950
   491
X11_VideoInit(_THIS)
slouken@1950
   492
{
slouken@1951
   493
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   494
slouken@1951
   495
    /* Get the window class name, usually the name of the application */
slouken@1951
   496
    data->classname = get_classname();
slouken@1951
   497
mail@6167
   498
    /* Get the process PID to be associated to the window */
mail@6167
   499
    data->pid = getpid();
mail@6167
   500
slouken@1951
   501
    /* Open a connection to the X input manager */
slouken@1951
   502
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   503
    if (SDL_X11_HAVE_UTF8) {
slouken@1951
   504
        data->im =
slouken@1951
   505
            XOpenIM(data->display, NULL, data->classname, data->classname);
slouken@1951
   506
    }
slouken@1951
   507
#endif
slouken@1951
   508
slouken@1951
   509
    /* Look up some useful Atoms */
slouken@4518
   510
#define GET_ATOM(X) data->X = XInternAtom(data->display, #X, False)
icculus@6637
   511
    GET_ATOM(WM_PROTOCOLS);
slouken@4518
   512
    GET_ATOM(WM_DELETE_WINDOW);
slouken@4518
   513
    GET_ATOM(_NET_WM_STATE);
slouken@4518
   514
    GET_ATOM(_NET_WM_STATE_HIDDEN);
slouken@6481
   515
    GET_ATOM(_NET_WM_STATE_FOCUSED);
slouken@4518
   516
    GET_ATOM(_NET_WM_STATE_MAXIMIZED_VERT);
slouken@4518
   517
    GET_ATOM(_NET_WM_STATE_MAXIMIZED_HORZ);
slouken@4518
   518
    GET_ATOM(_NET_WM_STATE_FULLSCREEN);
icculus@6466
   519
    GET_ATOM(_NET_WM_ALLOWED_ACTIONS);
icculus@6466
   520
    GET_ATOM(_NET_WM_ACTION_FULLSCREEN);
slouken@4518
   521
    GET_ATOM(_NET_WM_NAME);
slouken@4518
   522
    GET_ATOM(_NET_WM_ICON_NAME);
slouken@4518
   523
    GET_ATOM(_NET_WM_ICON);
icculus@6638
   524
    GET_ATOM(_NET_WM_PING);
slouken@4518
   525
    GET_ATOM(UTF8_STRING);
slouken@4518
   526
slouken@4518
   527
    /* Detect the window manager */
slouken@4518
   528
    X11_CheckWindowManager(_this);
slouken@1951
   529
slouken@3521
   530
    if (X11_InitModes(_this) < 0) {
slouken@3521
   531
        return -1;
slouken@3521
   532
    }
slouken@1950
   533
dimitris@6316
   534
    X11_InitXinput2(_this);
slouken@6311
   535
bob@2295
   536
    if (X11_InitKeyboard(_this) != 0) {
bob@2295
   537
        return -1;
bob@2295
   538
    }
slouken@1950
   539
    X11_InitMouse(_this);
slouken@1950
   540
jim@4645
   541
    X11_InitTouch(_this);
icculus@6774
   542
icculus@6774
   543
#if SDL_USE_LIBDBUS
icculus@6774
   544
    X11_InitDBus(_this);
icculus@6774
   545
#endif
icculus@6774
   546
slouken@1950
   547
    return 0;
slouken@1950
   548
}
slouken@1950
   549
slouken@1950
   550
void
slouken@1950
   551
X11_VideoQuit(_THIS)
slouken@1950
   552
{
slouken@1951
   553
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   554
slouken@1951
   555
    if (data->classname) {
slouken@1951
   556
        SDL_free(data->classname);
slouken@1951
   557
    }
slouken@1951
   558
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   559
    if (data->im) {
slouken@1951
   560
        XCloseIM(data->im);
slouken@1951
   561
    }
slouken@1951
   562
#endif
slouken@1951
   563
slouken@1950
   564
    X11_QuitModes(_this);
slouken@1950
   565
    X11_QuitKeyboard(_this);
slouken@1950
   566
    X11_QuitMouse(_this);
jim@4645
   567
    X11_QuitTouch(_this);
icculus@6774
   568
icculus@6774
   569
#if SDL_USE_LIBDBUS
icculus@6774
   570
    X11_QuitDBus(_this);
icculus@6774
   571
#endif
slouken@1950
   572
}
slouken@1950
   573
slouken@5466
   574
SDL_bool
slouken@5466
   575
X11_UseDirectColorVisuals(void)
slouken@5466
   576
{
slouken@5466
   577
    return SDL_getenv("SDL_VIDEO_X11_NODIRECTCOLOR") ? SDL_FALSE : SDL_TRUE;
slouken@5466
   578
}
slouken@5466
   579
slouken@5481
   580
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
   581
slouken@1950
   582
/* vim: set ts=4 sw=4 expandtab: */