src/video/x11/SDL_x11video.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 25 Aug 2008 06:33:00 +0000
changeset 2710 44e49d3fa6cf
parent 2324 3202e4826c57
child 2763 6fc50bdd88c0
permissions -rw-r--r--
Final merge of Google Summer of Code 2008 work...

Many-mouse and tablet support
by Szymon Wilczek, mentored by Ryan C. Gordon

Everything concerning the project is noted on the wiki:
http://wilku.ravenlord.ws/doku.php?id=start
slouken@1950
     1
/*
slouken@1950
     2
    SDL - Simple DirectMedia Layer
slouken@1950
     3
    Copyright (C) 1997-2006 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@1950
    24
#include "SDL_video.h"
slouken@1950
    25
#include "SDL_mouse.h"
slouken@1950
    26
#include "../SDL_sysvideo.h"
slouken@1950
    27
#include "../SDL_pixels_c.h"
slouken@1950
    28
slouken@1950
    29
#include "SDL_x11video.h"
slouken@2710
    30
slouken@2710
    31
XDevice **SDL_XDevices;
slouken@2710
    32
int SDL_NumOfXDevices;
slouken@2710
    33
XEventClass SDL_XEvents[256];
slouken@2710
    34
int SDL_NumOfXEvents;
slouken@2710
    35
slouken@2710
    36
int motion, button_pressed, button_released;    /* the definitions of the mice events */
slouken@2710
    37
int proximity_in, proximity_out;
slouken@1950
    38
slouken@1950
    39
/* Initialization/Query functions */
slouken@1950
    40
static int X11_VideoInit(_THIS);
slouken@1950
    41
static void X11_VideoQuit(_THIS);
slouken@1950
    42
slouken@1951
    43
/* Find out what class name we should use */
slouken@1951
    44
static char *
slouken@1951
    45
get_classname()
slouken@1951
    46
{
slouken@1951
    47
    char *spot;
slouken@1951
    48
#if defined(__LINUX__) || defined(__FREEBSD__)
slouken@1951
    49
    char procfile[1024];
slouken@1951
    50
    char linkfile[1024];
slouken@1951
    51
    int linksize;
slouken@1951
    52
#endif
slouken@1951
    53
slouken@1951
    54
    /* First allow environment variable override */
slouken@1951
    55
    spot = SDL_getenv("SDL_VIDEO_X11_WMCLASS");
slouken@1951
    56
    if (spot) {
slouken@1951
    57
        return SDL_strdup(spot);
slouken@1951
    58
    }
slouken@1951
    59
slouken@1951
    60
    /* Next look at the application's executable name */
slouken@1951
    61
#if defined(__LINUX__) || defined(__FREEBSD__)
slouken@1951
    62
#if defined(__LINUX__)
slouken@1951
    63
    SDL_snprintf(procfile, SDL_arraysize(procfile), "/proc/%d/exe", getpid());
slouken@1951
    64
#elif defined(__FREEBSD__)
slouken@1951
    65
    SDL_snprintf(procfile, SDL_arraysize(procfile), "/proc/%d/file",
slouken@1951
    66
                 getpid());
slouken@1951
    67
#else
slouken@1951
    68
#error Where can we find the executable name?
slouken@1951
    69
#endif
slouken@1951
    70
    linksize = readlink(procfile, linkfile, sizeof(linkfile) - 1);
slouken@1951
    71
    if (linksize > 0) {
slouken@1951
    72
        linkfile[linksize] = '\0';
slouken@1951
    73
        spot = SDL_strrchr(linkfile, '/');
slouken@1951
    74
        if (spot) {
slouken@1951
    75
            return SDL_strdup(spot + 1);
slouken@1951
    76
        } else {
slouken@1951
    77
            return SDL_strdup(linkfile);
slouken@1951
    78
        }
slouken@1951
    79
    }
slouken@1951
    80
#endif /* __LINUX__ || __FREEBSD__ */
slouken@1951
    81
slouken@1951
    82
    /* Finally use the default we've used forever */
slouken@1951
    83
    return SDL_strdup("SDL_App");
slouken@1951
    84
}
slouken@1951
    85
slouken@1950
    86
/* X11 driver bootstrap functions */
slouken@1950
    87
slouken@1950
    88
static int
slouken@1950
    89
X11_Available(void)
slouken@1950
    90
{
slouken@1950
    91
    Display *display = NULL;
slouken@1950
    92
    if (SDL_X11_LoadSymbols()) {
slouken@1950
    93
        display = XOpenDisplay(NULL);
slouken@1950
    94
        if (display != NULL) {
slouken@1950
    95
            XCloseDisplay(display);
slouken@1950
    96
        }
slouken@1950
    97
        SDL_X11_UnloadSymbols();
slouken@1950
    98
    }
slouken@1950
    99
    return (display != NULL);
slouken@1950
   100
}
slouken@1950
   101
slouken@1950
   102
static void
slouken@1950
   103
X11_DeleteDevice(SDL_VideoDevice * device)
slouken@1950
   104
{
slouken@1950
   105
    SDL_VideoData *data = (SDL_VideoData *) device->driverdata;
slouken@1950
   106
    if (data->display) {
slouken@1950
   107
        XCloseDisplay(data->display);
slouken@1950
   108
    }
bob@2324
   109
    SDL_free(data->windowlist);
slouken@1950
   110
    SDL_free(device->driverdata);
slouken@1950
   111
    SDL_free(device);
slouken@1950
   112
slouken@1950
   113
    SDL_X11_UnloadSymbols();
slouken@1950
   114
}
slouken@1950
   115
slouken@1950
   116
static SDL_VideoDevice *
slouken@1950
   117
X11_CreateDevice(int devindex)
slouken@1950
   118
{
slouken@1950
   119
    SDL_VideoDevice *device;
slouken@1950
   120
    SDL_VideoData *data;
slouken@1950
   121
    const char *display = NULL; /* Use the DISPLAY environment variable */
slouken@1950
   122
slouken@1950
   123
    if (!SDL_X11_LoadSymbols()) {
slouken@1950
   124
        return NULL;
slouken@1950
   125
    }
slouken@1950
   126
slouken@1950
   127
    /* Initialize all variables that we clean on shutdown */
slouken@1950
   128
    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
bob@2323
   129
    if (!device) {
bob@2323
   130
        SDL_OutOfMemory();
bob@2323
   131
        return NULL;
slouken@1950
   132
    }
bob@2323
   133
    data = (struct SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
bob@2323
   134
    if (!data) {
slouken@1950
   135
        SDL_OutOfMemory();
bob@2323
   136
        SDL_free(device);
slouken@1950
   137
        return NULL;
slouken@1950
   138
    }
slouken@1950
   139
    device->driverdata = data;
slouken@1950
   140
slouken@1950
   141
    /* FIXME: Do we need this?
slouken@1950
   142
       if ( (SDL_strncmp(XDisplayName(display), ":", 1) == 0) ||
slouken@1950
   143
       (SDL_strncmp(XDisplayName(display), "unix:", 5) == 0) ) {
slouken@1950
   144
       local_X11 = 1;
slouken@1950
   145
       } else {
slouken@1950
   146
       local_X11 = 0;
slouken@1950
   147
       }
slouken@1950
   148
     */
slouken@1950
   149
    data->display = XOpenDisplay(display);
slouken@1950
   150
#if defined(__osf__) && defined(SDL_VIDEO_DRIVER_X11_DYNAMIC)
slouken@1950
   151
    /* On Tru64 if linking without -lX11, it fails and you get following message.
slouken@1950
   152
     * Xlib: connection to ":0.0" refused by server
slouken@1950
   153
     * Xlib: XDM authorization key matches an existing client!
slouken@1950
   154
     *
slouken@1950
   155
     * It succeeds if retrying 1 second later
slouken@1950
   156
     * or if running xhost +localhost on shell.
slouken@1950
   157
     */
slouken@1950
   158
    if (data->display == NULL) {
slouken@1950
   159
        SDL_Delay(1000);
slouken@1950
   160
        data->display = XOpenDisplay(display);
slouken@1950
   161
    }
slouken@1950
   162
#endif
slouken@1950
   163
    if (data->display == NULL) {
slouken@1950
   164
        SDL_free(device);
slouken@1950
   165
        SDL_SetError("Couldn't open X11 display");
slouken@1950
   166
        return NULL;
slouken@1950
   167
    }
slouken@1950
   168
#ifdef X11_DEBUG
slouken@1950
   169
    XSynchronize(data->display, True);
slouken@1950
   170
#endif
slouken@1950
   171
slouken@1950
   172
    /* Set the function pointers */
slouken@1950
   173
    device->VideoInit = X11_VideoInit;
slouken@1950
   174
    device->VideoQuit = X11_VideoQuit;
slouken@1950
   175
    device->GetDisplayModes = X11_GetDisplayModes;
slouken@1950
   176
    device->SetDisplayMode = X11_SetDisplayMode;
bob@2162
   177
    device->SetDisplayGammaRamp = X11_SetDisplayGammaRamp;
bob@2162
   178
    device->GetDisplayGammaRamp = X11_GetDisplayGammaRamp;
slouken@1951
   179
    device->PumpEvents = X11_PumpEvents;
slouken@1950
   180
slouken@1950
   181
    device->CreateWindow = X11_CreateWindow;
slouken@1950
   182
    device->CreateWindowFrom = X11_CreateWindowFrom;
slouken@1950
   183
    device->SetWindowTitle = X11_SetWindowTitle;
slouken@1950
   184
    device->SetWindowPosition = X11_SetWindowPosition;
slouken@1950
   185
    device->SetWindowSize = X11_SetWindowSize;
slouken@1950
   186
    device->ShowWindow = X11_ShowWindow;
slouken@1950
   187
    device->HideWindow = X11_HideWindow;
slouken@1950
   188
    device->RaiseWindow = X11_RaiseWindow;
slouken@1950
   189
    device->MaximizeWindow = X11_MaximizeWindow;
slouken@1950
   190
    device->MinimizeWindow = X11_MinimizeWindow;
slouken@1950
   191
    device->RestoreWindow = X11_RestoreWindow;
slouken@1950
   192
    device->SetWindowGrab = X11_SetWindowGrab;
slouken@1950
   193
    device->DestroyWindow = X11_DestroyWindow;
slouken@1950
   194
    device->GetWindowWMInfo = X11_GetWindowWMInfo;
slouken@1952
   195
#ifdef SDL_VIDEO_OPENGL_GLX
slouken@1950
   196
    device->GL_LoadLibrary = X11_GL_LoadLibrary;
slouken@1950
   197
    device->GL_GetProcAddress = X11_GL_GetProcAddress;
slouken@1950
   198
    device->GL_CreateContext = X11_GL_CreateContext;
slouken@1950
   199
    device->GL_MakeCurrent = X11_GL_MakeCurrent;
slouken@1950
   200
    device->GL_SetSwapInterval = X11_GL_SetSwapInterval;
slouken@1950
   201
    device->GL_GetSwapInterval = X11_GL_GetSwapInterval;
slouken@1950
   202
    device->GL_SwapWindow = X11_GL_SwapWindow;
slouken@1950
   203
    device->GL_DeleteContext = X11_GL_DeleteContext;
slouken@1950
   204
#endif
slouken@1950
   205
slouken@1950
   206
    device->free = X11_DeleteDevice;
slouken@1950
   207
slouken@1950
   208
    return device;
slouken@1950
   209
}
slouken@1950
   210
slouken@1950
   211
VideoBootStrap X11_bootstrap = {
slouken@1950
   212
    "x11", "SDL X11 video driver",
slouken@1950
   213
    X11_Available, X11_CreateDevice
slouken@1950
   214
};
slouken@1950
   215
slouken@1950
   216
slouken@1950
   217
int
slouken@1950
   218
X11_VideoInit(_THIS)
slouken@1950
   219
{
slouken@2710
   220
    int i, index = 0, event_code;
slouken@2710
   221
    XEventClass xEvent;
slouken@1951
   222
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   223
slouken@1951
   224
    /* Get the window class name, usually the name of the application */
slouken@1951
   225
    data->classname = get_classname();
slouken@1951
   226
slouken@1951
   227
    /* Open a connection to the X input manager */
slouken@1951
   228
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   229
    if (SDL_X11_HAVE_UTF8) {
slouken@1951
   230
        data->im =
slouken@1951
   231
            XOpenIM(data->display, NULL, data->classname, data->classname);
slouken@1951
   232
    }
slouken@1951
   233
#endif
slouken@1951
   234
slouken@1951
   235
    /* Save DPMS and screensaver settings */
slouken@1951
   236
    X11_SaveScreenSaver(data->display, &data->screensaver_timeout,
slouken@1951
   237
                        &data->dpms_enabled);
slouken@1951
   238
    X11_DisableScreenSaver(data->display);
slouken@1951
   239
slouken@1951
   240
    /* Look up some useful Atoms */
slouken@1951
   241
    data->WM_DELETE_WINDOW =
slouken@1951
   242
        XInternAtom(data->display, "WM_DELETE_WINDOW", False);
slouken@1951
   243
slouken@1950
   244
    X11_InitModes(_this);
slouken@1950
   245
bob@2295
   246
    if (X11_InitKeyboard(_this) != 0) {
bob@2295
   247
        return -1;
bob@2295
   248
    }
slouken@1950
   249
    X11_InitMouse(_this);
slouken@1950
   250
slouken@2710
   251
    /* we're generating the table of events that should be recognized */
slouken@2710
   252
    for (i = 0; i < SDL_NumOfXDevices; ++i) {
slouken@2710
   253
        /* button events */
slouken@2710
   254
        DeviceButtonPress(SDL_XDevices[i], event_code, xEvent);
slouken@2710
   255
        if (xEvent) {
slouken@2710
   256
            SDL_XEvents[index++] = xEvent;
slouken@2710
   257
            button_pressed = event_code;
slouken@2710
   258
        }
slouken@2710
   259
        DeviceButtonRelease(SDL_XDevices[i], event_code, xEvent);
slouken@2710
   260
        if (xEvent) {
slouken@2710
   261
            SDL_XEvents[index++] = xEvent;
slouken@2710
   262
            button_released = event_code;
slouken@2710
   263
        }
slouken@2710
   264
slouken@2710
   265
        /* proximity events */
slouken@2710
   266
        ProximityIn(SDL_XDevices[i], event_code, xEvent);
slouken@2710
   267
        if (xEvent) {
slouken@2710
   268
            SDL_XEvents[index++] = xEvent;
slouken@2710
   269
            proximity_in = event_code;
slouken@2710
   270
        }
slouken@2710
   271
        ProximityOut(SDL_XDevices[i], event_code, xEvent);
slouken@2710
   272
        if (xEvent) {
slouken@2710
   273
            SDL_XEvents[index++] = xEvent;
slouken@2710
   274
            proximity_out = event_code;
slouken@2710
   275
        }
slouken@2710
   276
slouken@2710
   277
        /* motion events */
slouken@2710
   278
        DeviceMotionNotify(SDL_XDevices[i], event_code, xEvent);
slouken@2710
   279
        if (xEvent) {
slouken@2710
   280
            SDL_XEvents[index++] = xEvent;
slouken@2710
   281
            motion = event_code;
slouken@2710
   282
        }
slouken@2710
   283
slouken@2710
   284
    }
slouken@2710
   285
    SDL_NumOfXEvents = index;
slouken@2710
   286
slouken@1950
   287
    return 0;
slouken@1950
   288
}
slouken@1950
   289
slouken@1950
   290
void
slouken@1950
   291
X11_VideoQuit(_THIS)
slouken@1950
   292
{
slouken@1951
   293
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   294
slouken@1951
   295
    if (data->classname) {
slouken@1951
   296
        SDL_free(data->classname);
slouken@1951
   297
    }
slouken@1951
   298
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   299
    if (data->im) {
slouken@1951
   300
        XCloseIM(data->im);
slouken@1951
   301
    }
slouken@1951
   302
#endif
slouken@1951
   303
    X11_RestoreScreenSaver(data->display, data->screensaver_timeout,
slouken@1951
   304
                           data->dpms_enabled);
slouken@1951
   305
slouken@1950
   306
    X11_QuitModes(_this);
slouken@1950
   307
    X11_QuitKeyboard(_this);
slouken@1950
   308
    X11_QuitMouse(_this);
slouken@2710
   309
    free(SDL_XDevices);
slouken@1950
   310
}
slouken@1950
   311
slouken@1950
   312
/* vim: set ts=4 sw=4 expandtab: */