src/video/x11/SDL_x11video.c
author Sam Lantinga
Sun, 02 Feb 2014 00:53:27 -0800
changeset 8149 681eb46b8ac4
parent 8093 b43765095a6f
child 8287 fee216d949e3
permissions -rw-r--r--
Fixed bug 2374 - Update copyright for 2014...

Is it that time already??
slouken@1950
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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
*/
icculus@8093
    21
#include "../../SDL_internal.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@7191
    35
#include "SDL_x11touch.h"
dimitris@6316
    36
#include "SDL_x11xinput2.h"
slouken@2710
    37
gabomdq@7659
    38
#if SDL_VIDEO_OPENGL_EGL
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()) {
icculus@7827
   233
        display = X11_XOpenDisplay(NULL);
slouken@1950
   234
        if (display != NULL) {
icculus@7827
   235
            X11_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) {
icculus@7827
   247
        X11_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
{
gabomdq@7679
   262
    SDL_VideoDevice *device = NULL;
icculus@6498
   263
    /* if we trigger an error in our error handler, don't try again. */
icculus@6498
   264
    if (!safety_net_triggered) {
icculus@6498
   265
        safety_net_triggered = SDL_TRUE;
gabomdq@7679
   266
        device = SDL_GetVideoDevice();
icculus@6498
   267
        if (device != NULL) {
icculus@6498
   268
            int i;
icculus@6498
   269
            for (i = 0; i < device->num_displays; i++) {
icculus@6498
   270
                SDL_VideoDisplay *display = &device->displays[i];
icculus@6498
   271
                if (SDL_memcmp(&display->current_mode, &display->desktop_mode,
icculus@6498
   272
                               sizeof (SDL_DisplayMode)) != 0) {
icculus@6498
   273
                    X11_SetDisplayMode(device, display, &display->desktop_mode);
icculus@6498
   274
                }
icculus@6498
   275
            }
icculus@6498
   276
        }
icculus@6498
   277
    }
icculus@6498
   278
icculus@6498
   279
    if (orig_x11_errhandler != NULL) {
icculus@6498
   280
        return orig_x11_errhandler(d, e);  /* probably terminate. */
icculus@6498
   281
    }
icculus@6498
   282
icculus@6498
   283
    return 0;
icculus@6498
   284
}
icculus@6498
   285
slouken@1950
   286
static SDL_VideoDevice *
slouken@1950
   287
X11_CreateDevice(int devindex)
slouken@1950
   288
{
slouken@1950
   289
    SDL_VideoDevice *device;
slouken@1950
   290
    SDL_VideoData *data;
slouken@1950
   291
    const char *display = NULL; /* Use the DISPLAY environment variable */
slouken@1950
   292
slouken@1950
   293
    if (!SDL_X11_LoadSymbols()) {
slouken@1950
   294
        return NULL;
slouken@1950
   295
    }
slouken@1950
   296
icculus@6817
   297
    /* Need for threading gl calls. This is also required for the proprietary
icculus@6817
   298
        nVidia driver to be threaded. */
icculus@7827
   299
    X11_XInitThreads();
mikesart@6569
   300
slouken@1950
   301
    /* Initialize all variables that we clean on shutdown */
slouken@1950
   302
    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
bob@2323
   303
    if (!device) {
bob@2323
   304
        SDL_OutOfMemory();
bob@2323
   305
        return NULL;
slouken@1950
   306
    }
bob@2323
   307
    data = (struct SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
bob@2323
   308
    if (!data) {
icculus@7037
   309
        SDL_free(device);
slouken@1950
   310
        SDL_OutOfMemory();
slouken@1950
   311
        return NULL;
slouken@1950
   312
    }
slouken@1950
   313
    device->driverdata = data;
slouken@1950
   314
slouken@1950
   315
    /* FIXME: Do we need this?
icculus@7827
   316
       if ( (SDL_strncmp(X11_XDisplayName(display), ":", 1) == 0) ||
icculus@7827
   317
       (SDL_strncmp(X11_XDisplayName(display), "unix:", 5) == 0) ) {
slouken@1950
   318
       local_X11 = 1;
slouken@1950
   319
       } else {
slouken@1950
   320
       local_X11 = 0;
slouken@1950
   321
       }
slouken@1950
   322
     */
icculus@7827
   323
    data->display = X11_XOpenDisplay(display);
slouken@1950
   324
#if defined(__osf__) && defined(SDL_VIDEO_DRIVER_X11_DYNAMIC)
slouken@1950
   325
    /* On Tru64 if linking without -lX11, it fails and you get following message.
slouken@1950
   326
     * Xlib: connection to ":0.0" refused by server
slouken@1950
   327
     * Xlib: XDM authorization key matches an existing client!
slouken@1950
   328
     *
slouken@1950
   329
     * It succeeds if retrying 1 second later
slouken@1950
   330
     * or if running xhost +localhost on shell.
slouken@1950
   331
     */
slouken@1950
   332
    if (data->display == NULL) {
slouken@1950
   333
        SDL_Delay(1000);
icculus@7827
   334
        data->display = X11_XOpenDisplay(display);
slouken@1950
   335
    }
slouken@1950
   336
#endif
slouken@1950
   337
    if (data->display == NULL) {
slouken@6367
   338
        SDL_free(device->driverdata);
slouken@1950
   339
        SDL_free(device);
slouken@1950
   340
        SDL_SetError("Couldn't open X11 display");
slouken@1950
   341
        return NULL;
slouken@1950
   342
    }
slouken@1950
   343
#ifdef X11_DEBUG
icculus@7827
   344
    X11_XSynchronize(data->display, True);
slouken@1950
   345
#endif
slouken@1950
   346
icculus@6498
   347
    /* Hook up an X11 error handler to recover the desktop resolution. */
icculus@6498
   348
    safety_net_triggered = SDL_FALSE;
icculus@7827
   349
    orig_x11_errhandler = X11_XSetErrorHandler(X11_SafetyNetErrHandler);
icculus@6498
   350
slouken@1950
   351
    /* Set the function pointers */
slouken@1950
   352
    device->VideoInit = X11_VideoInit;
slouken@1950
   353
    device->VideoQuit = X11_VideoQuit;
slouken@1950
   354
    device->GetDisplayModes = X11_GetDisplayModes;
gabomdq@6331
   355
    device->GetDisplayBounds = X11_GetDisplayBounds;
slouken@1950
   356
    device->SetDisplayMode = X11_SetDisplayMode;
slouken@3025
   357
    device->SuspendScreenSaver = X11_SuspendScreenSaver;
slouken@1951
   358
    device->PumpEvents = X11_PumpEvents;
slouken@1950
   359
slouken@1950
   360
    device->CreateWindow = X11_CreateWindow;
slouken@1950
   361
    device->CreateWindowFrom = X11_CreateWindowFrom;
slouken@1950
   362
    device->SetWindowTitle = X11_SetWindowTitle;
slouken@2967
   363
    device->SetWindowIcon = X11_SetWindowIcon;
slouken@1950
   364
    device->SetWindowPosition = X11_SetWindowPosition;
slouken@1950
   365
    device->SetWindowSize = X11_SetWindowSize;
slouken@7617
   366
    device->SetWindowMinimumSize = X11_SetWindowMinimumSize;
slouken@7617
   367
    device->SetWindowMaximumSize = X11_SetWindowMaximumSize;
slouken@1950
   368
    device->ShowWindow = X11_ShowWindow;
slouken@1950
   369
    device->HideWindow = X11_HideWindow;
slouken@1950
   370
    device->RaiseWindow = X11_RaiseWindow;
slouken@1950
   371
    device->MaximizeWindow = X11_MaximizeWindow;
slouken@1950
   372
    device->MinimizeWindow = X11_MinimizeWindow;
slouken@1950
   373
    device->RestoreWindow = X11_RestoreWindow;
icculus@6422
   374
    device->SetWindowBordered = X11_SetWindowBordered;
slouken@5302
   375
    device->SetWindowFullscreen = X11_SetWindowFullscreen;
slouken@5466
   376
    device->SetWindowGammaRamp = X11_SetWindowGammaRamp;
slouken@1950
   377
    device->SetWindowGrab = X11_SetWindowGrab;
slouken@1950
   378
    device->DestroyWindow = X11_DestroyWindow;
slouken@5182
   379
    device->CreateWindowFramebuffer = X11_CreateWindowFramebuffer;
slouken@5182
   380
    device->UpdateWindowFramebuffer = X11_UpdateWindowFramebuffer;
slouken@5182
   381
    device->DestroyWindowFramebuffer = X11_DestroyWindowFramebuffer;
slouken@1950
   382
    device->GetWindowWMInfo = X11_GetWindowWMInfo;
slouken@5182
   383
eligottlieb@4782
   384
    device->shape_driver.CreateShaper = X11_CreateShaper;
eligottlieb@4782
   385
    device->shape_driver.SetWindowShape = X11_SetWindowShape;
eligottlieb@4782
   386
    device->shape_driver.ResizeWindowShape = X11_ResizeWindowShape;
slouken@5182
   387
slouken@5088
   388
#if SDL_VIDEO_OPENGL_GLX
slouken@6370
   389
    device->GL_LoadLibrary = X11_GL_LoadLibrary;
slouken@6370
   390
    device->GL_GetProcAddress = X11_GL_GetProcAddress;
slouken@6370
   391
    device->GL_UnloadLibrary = X11_GL_UnloadLibrary;
slouken@6370
   392
    device->GL_CreateContext = X11_GL_CreateContext;
slouken@6370
   393
    device->GL_MakeCurrent = X11_GL_MakeCurrent;
slouken@6370
   394
    device->GL_SetSwapInterval = X11_GL_SetSwapInterval;
slouken@6370
   395
    device->GL_GetSwapInterval = X11_GL_GetSwapInterval;
slouken@6370
   396
    device->GL_SwapWindow = X11_GL_SwapWindow;
slouken@6370
   397
    device->GL_DeleteContext = X11_GL_DeleteContext;
gabomdq@7659
   398
#elif SDL_VIDEO_OPENGL_EGL
slouken@6370
   399
    device->GL_LoadLibrary = X11_GLES_LoadLibrary;
slouken@6370
   400
    device->GL_GetProcAddress = X11_GLES_GetProcAddress;
slouken@6370
   401
    device->GL_UnloadLibrary = X11_GLES_UnloadLibrary;
slouken@6370
   402
    device->GL_CreateContext = X11_GLES_CreateContext;
slouken@6370
   403
    device->GL_MakeCurrent = X11_GLES_MakeCurrent;
slouken@6370
   404
    device->GL_SetSwapInterval = X11_GLES_SetSwapInterval;
slouken@6370
   405
    device->GL_GetSwapInterval = X11_GLES_GetSwapInterval;
slouken@6370
   406
    device->GL_SwapWindow = X11_GLES_SwapWindow;
slouken@6370
   407
    device->GL_DeleteContext = X11_GLES_DeleteContext;
slouken@3218
   408
#endif
slouken@1950
   409
slouken@4508
   410
    device->SetClipboardText = X11_SetClipboardText;
slouken@4508
   411
    device->GetClipboardText = X11_GetClipboardText;
slouken@4508
   412
    device->HasClipboardText = X11_HasClipboardText;
slouken@4508
   413
slouken@1950
   414
    device->free = X11_DeleteDevice;
slouken@1950
   415
slouken@1950
   416
    return device;
slouken@1950
   417
}
slouken@1950
   418
slouken@1950
   419
VideoBootStrap X11_bootstrap = {
slouken@1950
   420
    "x11", "SDL X11 video driver",
slouken@1950
   421
    X11_Available, X11_CreateDevice
slouken@1950
   422
};
slouken@1950
   423
slouken@4559
   424
static int (*handler) (Display *, XErrorEvent *) = NULL;
slouken@4559
   425
static int
slouken@4559
   426
X11_CheckWindowManagerErrorHandler(Display * d, XErrorEvent * e)
slouken@4559
   427
{
slouken@4559
   428
    if (e->error_code == BadWindow) {
slouken@4559
   429
        return (0);
slouken@4559
   430
    } else {
slouken@4559
   431
        return (handler(d, e));
slouken@4559
   432
    }
slouken@4559
   433
}
slouken@1950
   434
slouken@4518
   435
static void
slouken@4518
   436
X11_CheckWindowManager(_THIS)
slouken@4518
   437
{
slouken@4518
   438
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@4518
   439
    Display *display = data->display;
slouken@4518
   440
    Atom _NET_SUPPORTING_WM_CHECK;
slouken@4518
   441
    int status, real_format;
slouken@4518
   442
    Atom real_type;
slouken@8034
   443
    unsigned long items_read = 0, items_left = 0;
slouken@8034
   444
    unsigned char *propdata = NULL;
slouken@4518
   445
    Window wm_window = 0;
slouken@4518
   446
#ifdef DEBUG_WINDOW_MANAGER
slouken@4518
   447
    char *wm_name;
slouken@4518
   448
#endif
slouken@4518
   449
slouken@4559
   450
    /* Set up a handler to gracefully catch errors */
icculus@7827
   451
    X11_XSync(display, False);
icculus@7827
   452
    handler = X11_XSetErrorHandler(X11_CheckWindowManagerErrorHandler);
slouken@4559
   453
icculus@7827
   454
    _NET_SUPPORTING_WM_CHECK = X11_XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
icculus@7827
   455
    status = X11_XGetWindowProperty(display, DefaultRootWindow(display), _NET_SUPPORTING_WM_CHECK, 0L, 1L, False, XA_WINDOW, &real_type, &real_format, &items_read, &items_left, &propdata);
slouken@8034
   456
    if (status == Success) {
slouken@8034
   457
        if (items_read) {
slouken@8034
   458
            wm_window = ((Window*)propdata)[0];
slouken@8034
   459
        }
slouken@8034
   460
        if (propdata) {
slouken@8034
   461
            X11_XFree(propdata);
slouken@8034
   462
            propdata = NULL;
slouken@8034
   463
        }
slouken@4559
   464
    }
slouken@4559
   465
slouken@4559
   466
    if (wm_window) {
icculus@7827
   467
        status = X11_XGetWindowProperty(display, wm_window, _NET_SUPPORTING_WM_CHECK, 0L, 1L, False, XA_WINDOW, &real_type, &real_format, &items_read, &items_left, &propdata);
slouken@4559
   468
        if (status != Success || !items_read || wm_window != ((Window*)propdata)[0]) {
slouken@4559
   469
            wm_window = None;
slouken@4559
   470
        }
slouken@8034
   471
        if (status == Success && propdata) {
icculus@7827
   472
            X11_XFree(propdata);
slouken@8034
   473
            propdata = NULL;
slouken@4559
   474
        }
slouken@4559
   475
    }
slouken@4559
   476
slouken@4559
   477
    /* Reset the error handler, we're done checking */
icculus@7827
   478
    X11_XSync(display, False);
icculus@7827
   479
    X11_XSetErrorHandler(handler);
slouken@4518
   480
slouken@4518
   481
    if (!wm_window) {
slouken@4518
   482
#ifdef DEBUG_WINDOW_MANAGER
slouken@4518
   483
        printf("Couldn't get _NET_SUPPORTING_WM_CHECK property\n");
slouken@4518
   484
#endif
slouken@4518
   485
        return;
slouken@4518
   486
    }
slouken@4518
   487
    data->net_wm = SDL_TRUE;
slouken@4518
   488
slouken@4518
   489
#ifdef DEBUG_WINDOW_MANAGER
slouken@4518
   490
    wm_name = X11_GetWindowTitle(_this, wm_window);
slouken@4518
   491
    printf("Window manager: %s\n", wm_name);
slouken@4518
   492
    SDL_free(wm_name);
slouken@4518
   493
#endif
slouken@4518
   494
}
slouken@1950
   495
icculus@6774
   496
slouken@1950
   497
int
slouken@1950
   498
X11_VideoInit(_THIS)
slouken@1950
   499
{
slouken@1951
   500
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   501
slouken@1951
   502
    /* Get the window class name, usually the name of the application */
slouken@1951
   503
    data->classname = get_classname();
slouken@1951
   504
mail@6167
   505
    /* Get the process PID to be associated to the window */
mail@6167
   506
    data->pid = getpid();
mail@6167
   507
slouken@1951
   508
    /* Open a connection to the X input manager */
slouken@1951
   509
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   510
    if (SDL_X11_HAVE_UTF8) {
slouken@1951
   511
        data->im =
icculus@7827
   512
            X11_XOpenIM(data->display, NULL, data->classname, data->classname);
slouken@1951
   513
    }
slouken@1951
   514
#endif
slouken@1951
   515
slouken@1951
   516
    /* Look up some useful Atoms */
icculus@7827
   517
#define GET_ATOM(X) data->X = X11_XInternAtom(data->display, #X, False)
icculus@6637
   518
    GET_ATOM(WM_PROTOCOLS);
slouken@4518
   519
    GET_ATOM(WM_DELETE_WINDOW);
slouken@4518
   520
    GET_ATOM(_NET_WM_STATE);
slouken@4518
   521
    GET_ATOM(_NET_WM_STATE_HIDDEN);
slouken@6481
   522
    GET_ATOM(_NET_WM_STATE_FOCUSED);
slouken@4518
   523
    GET_ATOM(_NET_WM_STATE_MAXIMIZED_VERT);
slouken@4518
   524
    GET_ATOM(_NET_WM_STATE_MAXIMIZED_HORZ);
slouken@4518
   525
    GET_ATOM(_NET_WM_STATE_FULLSCREEN);
icculus@6466
   526
    GET_ATOM(_NET_WM_ALLOWED_ACTIONS);
icculus@6466
   527
    GET_ATOM(_NET_WM_ACTION_FULLSCREEN);
slouken@4518
   528
    GET_ATOM(_NET_WM_NAME);
slouken@4518
   529
    GET_ATOM(_NET_WM_ICON_NAME);
slouken@4518
   530
    GET_ATOM(_NET_WM_ICON);
icculus@6638
   531
    GET_ATOM(_NET_WM_PING);
slouken@6970
   532
    GET_ATOM(_NET_ACTIVE_WINDOW);
slouken@4518
   533
    GET_ATOM(UTF8_STRING);
slouken@7001
   534
    GET_ATOM(PRIMARY);
slouken@7001
   535
    GET_ATOM(XdndEnter);
slouken@7001
   536
    GET_ATOM(XdndPosition);
slouken@7001
   537
    GET_ATOM(XdndStatus);
slouken@7001
   538
    GET_ATOM(XdndTypeList);
slouken@7001
   539
    GET_ATOM(XdndActionCopy);
slouken@7001
   540
    GET_ATOM(XdndDrop);
slouken@7001
   541
    GET_ATOM(XdndFinished);
slouken@7001
   542
    GET_ATOM(XdndSelection);
slouken@4518
   543
slouken@4518
   544
    /* Detect the window manager */
slouken@4518
   545
    X11_CheckWindowManager(_this);
slouken@1951
   546
slouken@3521
   547
    if (X11_InitModes(_this) < 0) {
slouken@3521
   548
        return -1;
slouken@3521
   549
    }
slouken@1950
   550
dimitris@6316
   551
    X11_InitXinput2(_this);
slouken@6311
   552
bob@2295
   553
    if (X11_InitKeyboard(_this) != 0) {
bob@2295
   554
        return -1;
bob@2295
   555
    }
slouken@1950
   556
    X11_InitMouse(_this);
slouken@1950
   557
jim@4645
   558
    X11_InitTouch(_this);
icculus@6774
   559
icculus@6774
   560
#if SDL_USE_LIBDBUS
icculus@6774
   561
    X11_InitDBus(_this);
icculus@6774
   562
#endif
icculus@6774
   563
slouken@1950
   564
    return 0;
slouken@1950
   565
}
slouken@1950
   566
slouken@1950
   567
void
slouken@1950
   568
X11_VideoQuit(_THIS)
slouken@1950
   569
{
slouken@1951
   570
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   571
slouken@7719
   572
    SDL_free(data->classname);
slouken@1951
   573
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   574
    if (data->im) {
icculus@7827
   575
        X11_XCloseIM(data->im);
slouken@1951
   576
    }
slouken@1951
   577
#endif
slouken@1951
   578
slouken@1950
   579
    X11_QuitModes(_this);
slouken@1950
   580
    X11_QuitKeyboard(_this);
slouken@1950
   581
    X11_QuitMouse(_this);
jim@4645
   582
    X11_QuitTouch(_this);
icculus@6774
   583
icculus@6774
   584
#if SDL_USE_LIBDBUS
icculus@6774
   585
    X11_QuitDBus(_this);
icculus@6774
   586
#endif
slouken@1950
   587
}
slouken@1950
   588
slouken@5466
   589
SDL_bool
slouken@5466
   590
X11_UseDirectColorVisuals(void)
slouken@5466
   591
{
slouken@5466
   592
    return SDL_getenv("SDL_VIDEO_X11_NODIRECTCOLOR") ? SDL_FALSE : SDL_TRUE;
slouken@5466
   593
}
slouken@5466
   594
slouken@5481
   595
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
   596
slouken@1950
   597
/* vim: set ts=4 sw=4 expandtab: */