src/video/x11/SDL_x11events.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 21 Jul 2010 21:47:12 -0700
changeset 4565 e2d46c5c7483
parent 4560 95352c671a6e
child 4694 c24ba2cc9583
permissions -rw-r--r--
Fixed key repeat detection on X11, and simplified the code for everyone else.
slouken@1951
     1
/*
slouken@1951
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 Sam Lantinga
slouken@1951
     4
slouken@1951
     5
    This library is free software; you can redistribute it and/or
slouken@1951
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1951
     7
    License as published by the Free Software Foundation; either
slouken@1951
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1951
     9
slouken@1951
    10
    This library is distributed in the hope that it will be useful,
slouken@1951
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1951
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1951
    13
    Lesser General Public License for more details.
slouken@1951
    14
slouken@1951
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1951
    16
    License along with this library; if not, write to the Free Software
slouken@1951
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1951
    18
slouken@1951
    19
    Sam Lantinga
slouken@1951
    20
    slouken@libsdl.org
slouken@1951
    21
*/
slouken@1951
    22
#include "SDL_config.h"
slouken@1951
    23
slouken@2046
    24
#include <sys/types.h>
slouken@2046
    25
#include <sys/time.h>
slouken@4465
    26
#include <signal.h>
slouken@2046
    27
#include <unistd.h>
slouken@4558
    28
#include <limits.h> /* For INT_MAX */
slouken@2046
    29
slouken@1951
    30
#include "SDL_x11video.h"
slouken@1951
    31
#include "../../events/SDL_events_c.h"
slouken@2940
    32
#include "../../events/SDL_mouse_c.h"
slouken@1951
    33
slouken@4465
    34
#include "SDL_timer.h"
slouken@3241
    35
#include "SDL_syswm.h"
slouken@3241
    36
slouken@4556
    37
/*#define DEBUG_XEVENTS*/
slouken@4508
    38
slouken@4565
    39
/* Check to see if this is a repeated key.
slouken@4565
    40
   (idea shamelessly lifted from GII -- thanks guys! :)
slouken@4565
    41
 */
slouken@4565
    42
static SDL_bool X11_KeyRepeat(Display *display, XEvent *event)
slouken@4565
    43
{
slouken@4565
    44
    XEvent peekevent;
slouken@4565
    45
slouken@4565
    46
    if (XPending(display)) {
slouken@4565
    47
        XPeekEvent(display, &peekevent);
slouken@4565
    48
        if ((peekevent.type == KeyPress) &&
slouken@4565
    49
            (peekevent.xkey.keycode == event->xkey.keycode) &&
slouken@4565
    50
            ((peekevent.xkey.time-event->xkey.time) < 2)) {
slouken@4565
    51
            return SDL_TRUE;
slouken@4565
    52
        }
slouken@4565
    53
    }
slouken@4565
    54
    return SDL_FALSE;
slouken@4565
    55
}
slouken@4565
    56
slouken@1951
    57
static void
slouken@1951
    58
X11_DispatchEvent(_THIS)
slouken@1951
    59
{
slouken@1951
    60
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@4518
    61
    Display *display = videodata->display;
slouken@1951
    62
    SDL_WindowData *data;
slouken@1951
    63
    XEvent xevent;
slouken@1951
    64
    int i;
slouken@1951
    65
slouken@1951
    66
    SDL_zero(xevent);           /* valgrind fix. --ryan. */
slouken@4518
    67
    XNextEvent(display, &xevent);
slouken@1951
    68
bob@2325
    69
    /* filter events catchs XIM events and sends them to the correct
bob@2325
    70
       handler */
bob@2325
    71
    if (XFilterEvent(&xevent, None) == True) {
bob@2327
    72
#if 0
bob@2328
    73
        printf("Filtered event type = %d display = %d window = %d\n",
bob@2328
    74
               xevent.type, xevent.xany.display, xevent.xany.window);
bob@2325
    75
#endif
bob@2325
    76
        return;
bob@2325
    77
    }
bob@2325
    78
slouken@1951
    79
    /* Send a SDL_SYSWMEVENT if the application wants them */
slouken@4429
    80
    if (SDL_GetEventState(SDL_SYSWMEVENT) == SDL_ENABLE) {
slouken@1951
    81
        SDL_SysWMmsg wmmsg;
slouken@1951
    82
slouken@1951
    83
        SDL_VERSION(&wmmsg.version);
slouken@1951
    84
        wmmsg.subsystem = SDL_SYSWM_X11;
slouken@1951
    85
        wmmsg.event.xevent = xevent;
slouken@1951
    86
        SDL_SendSysWMEvent(&wmmsg);
slouken@1951
    87
    }
slouken@1951
    88
slouken@1951
    89
    data = NULL;
bob@2324
    90
    if (videodata && videodata->windowlist) {
bob@2324
    91
        for (i = 0; i < videodata->numwindows; ++i) {
bob@2324
    92
            if ((videodata->windowlist[i] != NULL) &&
slouken@3685
    93
                (videodata->windowlist[i]->xwindow == xevent.xany.window)) {
bob@2324
    94
                data = videodata->windowlist[i];
bob@2324
    95
                break;
bob@2324
    96
            }
slouken@1951
    97
        }
slouken@1951
    98
    }
slouken@1951
    99
    if (!data) {
slouken@1951
   100
        return;
slouken@1951
   101
    }
slouken@4518
   102
bob@2327
   103
#if 0
bob@2328
   104
    printf("type = %d display = %d window = %d\n",
bob@2328
   105
           xevent.type, xevent.xany.display, xevent.xany.window);
bob@2327
   106
#endif
slouken@1951
   107
    switch (xevent.type) {
slouken@1951
   108
slouken@1951
   109
        /* Gaining mouse coverage? */
slouken@1951
   110
    case EnterNotify:{
slouken@1951
   111
#ifdef DEBUG_XEVENTS
bob@3195
   112
            printf("EnterNotify! (%d,%d,%d)\n", 
slouken@4558
   113
                   xevent.xcrossing.x,
slouken@4558
   114
                   xevent.xcrossing.y,
bob@3195
   115
                   xevent.xcrossing.mode);
slouken@1951
   116
            if (xevent.xcrossing.mode == NotifyGrab)
slouken@1951
   117
                printf("Mode: NotifyGrab\n");
slouken@1951
   118
            if (xevent.xcrossing.mode == NotifyUngrab)
slouken@1951
   119
                printf("Mode: NotifyUngrab\n");
slouken@1951
   120
#endif
slouken@4465
   121
            SDL_SetMouseFocus(data->window);
slouken@1951
   122
        }
slouken@1951
   123
        break;
slouken@1951
   124
slouken@1951
   125
        /* Losing mouse coverage? */
slouken@1951
   126
    case LeaveNotify:{
slouken@1951
   127
#ifdef DEBUG_XEVENTS
bob@3195
   128
            printf("LeaveNotify! (%d,%d,%d)\n", 
slouken@4558
   129
                   xevent.xcrossing.x,
slouken@4558
   130
                   xevent.xcrossing.y,
bob@3195
   131
                   xevent.xcrossing.mode);
slouken@1951
   132
            if (xevent.xcrossing.mode == NotifyGrab)
slouken@1951
   133
                printf("Mode: NotifyGrab\n");
slouken@1951
   134
            if (xevent.xcrossing.mode == NotifyUngrab)
slouken@1951
   135
                printf("Mode: NotifyUngrab\n");
slouken@1951
   136
#endif
slouken@4558
   137
            if (xevent.xcrossing.mode != NotifyGrab &&
slouken@4558
   138
                xevent.xcrossing.mode != NotifyUngrab &&
slouken@4558
   139
                xevent.xcrossing.detail != NotifyInferior) {
slouken@4465
   140
                SDL_SetMouseFocus(NULL);
slouken@1951
   141
            }
slouken@1951
   142
        }
slouken@1951
   143
        break;
slouken@1951
   144
slouken@1951
   145
        /* Gaining input focus? */
slouken@1951
   146
    case FocusIn:{
slouken@1951
   147
#ifdef DEBUG_XEVENTS
slouken@1951
   148
            printf("FocusIn!\n");
slouken@1951
   149
#endif
slouken@4465
   150
            SDL_SetKeyboardFocus(data->window);
slouken@1951
   151
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   152
            if (data->ic) {
slouken@1951
   153
                XSetICFocus(data->ic);
slouken@1951
   154
            }
slouken@1951
   155
#endif
slouken@1951
   156
        }
slouken@1951
   157
        break;
slouken@1951
   158
slouken@1951
   159
        /* Losing input focus? */
slouken@1951
   160
    case FocusOut:{
slouken@1951
   161
#ifdef DEBUG_XEVENTS
slouken@1951
   162
            printf("FocusOut!\n");
slouken@1951
   163
#endif
slouken@4465
   164
            SDL_SetKeyboardFocus(NULL);
slouken@1951
   165
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   166
            if (data->ic) {
slouken@1951
   167
                XUnsetICFocus(data->ic);
slouken@1951
   168
            }
slouken@1951
   169
#endif
slouken@1951
   170
        }
slouken@1951
   171
        break;
slouken@1951
   172
slouken@1951
   173
        /* Generated upon EnterWindow and FocusIn */
slouken@1951
   174
    case KeymapNotify:{
slouken@1951
   175
#ifdef DEBUG_XEVENTS
slouken@1951
   176
            printf("KeymapNotify!\n");
slouken@1951
   177
#endif
slouken@1951
   178
            /* FIXME:
slouken@1951
   179
               X11_SetKeyboardState(SDL_Display, xevent.xkeymap.key_vector);
slouken@1951
   180
             */
slouken@1951
   181
        }
slouken@1951
   182
        break;
slouken@1951
   183
slouken@2305
   184
        /* Has the keyboard layout changed? */
slouken@2305
   185
    case MappingNotify:{
slouken@2305
   186
#ifdef DEBUG_XEVENTS
slouken@2305
   187
            printf("MappingNotify!\n");
slouken@2305
   188
#endif
slouken@2306
   189
            X11_UpdateKeymap(_this);
slouken@2305
   190
        }
slouken@2305
   191
        break;
slouken@2305
   192
slouken@1951
   193
        /* Key press? */
slouken@1951
   194
    case KeyPress:{
slouken@1951
   195
            KeyCode keycode = xevent.xkey.keycode;
bob@2300
   196
            KeySym keysym = NoSymbol;
slouken@4565
   197
            SDL_scancode scancode;
slouken@2306
   198
            char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];
bob@2325
   199
            Status status = 0;
slouken@1951
   200
slouken@1951
   201
#ifdef DEBUG_XEVENTS
slouken@1951
   202
            printf("KeyPress (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
slouken@1951
   203
#endif
slouken@4565
   204
            SDL_SendKeyboardKey(SDL_PRESSED, videodata->key_layout[keycode]);
slouken@4535
   205
#if 1
slouken@2305
   206
            if (videodata->key_layout[keycode] == SDLK_UNKNOWN) {
slouken@2305
   207
                int min_keycode, max_keycode;
slouken@4518
   208
                XDisplayKeycodes(display, &min_keycode, &max_keycode);
slouken@4518
   209
                keysym = XKeycodeToKeysym(display, keycode, 0);
bob@2299
   210
                fprintf(stderr,
slouken@4556
   211
                        "The key you just pressed is not recognized by SDL. To help get this fixed, please report this to the SDL mailing list <sdl@libsdl.org> X11 KeyCode %d (%d), X11 KeySym 0x%lX (%s).\n",
slouken@2305
   212
                        keycode, keycode - min_keycode, keysym,
slouken@2305
   213
                        XKeysymToString(keysym));
bob@2299
   214
            }
bob@2295
   215
#endif
bob@2325
   216
            /* */
slouken@2305
   217
            SDL_zero(text);
bob@2325
   218
#ifdef X_HAVE_UTF8_STRING
bob@2325
   219
            if (data->ic) {
bob@2325
   220
                Xutf8LookupString(data->ic, &xevent.xkey, text, sizeof(text),
slouken@2738
   221
                                  &keysym, &status);
bob@2325
   222
            }
bob@2325
   223
#else
slouken@2306
   224
            XLookupString(&xevent.xkey, text, sizeof(text), &keysym, NULL);
bob@2325
   225
#endif
slouken@2305
   226
            if (*text) {
slouken@4465
   227
                SDL_SendKeyboardText(text);
bob@2300
   228
            }
slouken@1951
   229
        }
slouken@1951
   230
        break;
slouken@1951
   231
slouken@1951
   232
        /* Key release? */
slouken@1951
   233
    case KeyRelease:{
slouken@1951
   234
            KeyCode keycode = xevent.xkey.keycode;
slouken@1951
   235
slouken@1951
   236
#ifdef DEBUG_XEVENTS
slouken@1951
   237
            printf("KeyRelease (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
slouken@1951
   238
#endif
slouken@4565
   239
            if (X11_KeyRepeat(display, &xevent)) {
slouken@4565
   240
                /* We're about to get a repeated key down, ignore the key up */
slouken@4565
   241
                break;
slouken@4565
   242
            }
slouken@4565
   243
            SDL_SendKeyboardKey(SDL_RELEASED, videodata->key_layout[keycode]);
slouken@1951
   244
        }
slouken@1951
   245
        break;
slouken@1951
   246
slouken@1951
   247
        /* Have we been iconified? */
slouken@1951
   248
    case UnmapNotify:{
slouken@1951
   249
#ifdef DEBUG_XEVENTS
slouken@1951
   250
            printf("UnmapNotify!\n");
slouken@1951
   251
#endif
slouken@3685
   252
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@3685
   253
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1951
   254
        }
slouken@1951
   255
        break;
slouken@1951
   256
slouken@1951
   257
        /* Have we been restored? */
slouken@1951
   258
    case MapNotify:{
slouken@1951
   259
#ifdef DEBUG_XEVENTS
slouken@1951
   260
            printf("MapNotify!\n");
slouken@1951
   261
#endif
slouken@3685
   262
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@3685
   263
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@1951
   264
        }
slouken@1951
   265
        break;
slouken@1951
   266
slouken@1951
   267
        /* Have we been resized or moved? */
slouken@1951
   268
    case ConfigureNotify:{
slouken@1951
   269
#ifdef DEBUG_XEVENTS
slouken@1951
   270
            printf("ConfigureNotify! (resize: %dx%d)\n",
slouken@1951
   271
                   xevent.xconfigure.width, xevent.xconfigure.height);
slouken@1951
   272
#endif
slouken@3685
   273
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED,
slouken@1951
   274
                                xevent.xconfigure.x, xevent.xconfigure.y);
slouken@3685
   275
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED,
slouken@1951
   276
                                xevent.xconfigure.width,
slouken@1951
   277
                                xevent.xconfigure.height);
slouken@1951
   278
        }
slouken@1951
   279
        break;
slouken@1951
   280
slouken@1951
   281
        /* Have we been requested to quit (or another client message?) */
slouken@1951
   282
    case ClientMessage:{
slouken@1951
   283
            if ((xevent.xclient.format == 32) &&
slouken@1951
   284
                (xevent.xclient.data.l[0] == videodata->WM_DELETE_WINDOW)) {
slouken@1951
   285
slouken@3685
   286
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
slouken@1951
   287
            }
slouken@1951
   288
        }
slouken@1951
   289
        break;
slouken@1951
   290
slouken@1951
   291
        /* Do we need to refresh ourselves? */
slouken@1951
   292
    case Expose:{
slouken@1951
   293
#ifdef DEBUG_XEVENTS
slouken@1951
   294
            printf("Expose (count = %d)\n", xevent.xexpose.count);
slouken@1951
   295
#endif
slouken@3685
   296
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
slouken@1951
   297
        }
slouken@1951
   298
        break;
slouken@1951
   299
slouken@4465
   300
    case MotionNotify:{
slouken@4465
   301
#ifdef DEBUG_MOTION
slouken@4465
   302
            printf("X11 motion: %d,%d\n", xevent.xmotion.x, xevent.xmotion.y);
slouken@4465
   303
#endif
slouken@4484
   304
            SDL_SendMouseMotion(data->window, 0, xevent.xmotion.x, xevent.xmotion.y);
slouken@4465
   305
        }
slouken@4465
   306
        break;
slouken@4465
   307
slouken@4465
   308
    case ButtonPress:{
slouken@4484
   309
            SDL_SendMouseButton(data->window, SDL_PRESSED, xevent.xbutton.button);
slouken@4465
   310
        }
slouken@4465
   311
        break;
slouken@4465
   312
slouken@4465
   313
    case ButtonRelease:{
slouken@4484
   314
            SDL_SendMouseButton(data->window, SDL_RELEASED, xevent.xbutton.button);
slouken@4465
   315
        }
slouken@4465
   316
        break;
slouken@4465
   317
slouken@4518
   318
    case PropertyNotify:{
slouken@4518
   319
#ifdef DEBUG_XEVENTS
slouken@4520
   320
            unsigned char *propdata;
slouken@4520
   321
            int status, real_format;
slouken@4520
   322
            Atom real_type;
slouken@4520
   323
            unsigned long items_read, items_left, i;
slouken@4520
   324
slouken@4518
   325
            char *name = XGetAtomName(display, xevent.xproperty.atom);
slouken@4518
   326
            if (name) {
slouken@4556
   327
                printf("PropertyNotify: %s %s\n", name, (xevent.xproperty.state == PropertyDelete) ? "deleted" : "changed");
slouken@4518
   328
                XFree(name);
slouken@4518
   329
            }
slouken@4518
   330
slouken@4520
   331
            status = XGetWindowProperty(display, data->xwindow, xevent.xproperty.atom, 0L, 8192L, False, AnyPropertyType, &real_type, &real_format, &items_read, &items_left, &propdata);
slouken@4556
   332
            if (status == Success && items_read > 0) {
slouken@4520
   333
                if (real_type == XA_INTEGER) {
slouken@4520
   334
                    int *values = (int *)propdata;
slouken@4520
   335
slouken@4520
   336
                    printf("{");
slouken@4520
   337
                    for (i = 0; i < items_read; i++) {
slouken@4520
   338
                        printf(" %d", values[i]);
slouken@4520
   339
                    }
slouken@4520
   340
                    printf(" }\n");
slouken@4520
   341
                } else if (real_type == XA_CARDINAL) {
slouken@4520
   342
                    if (real_format == 32) {
slouken@4520
   343
                        Uint32 *values = (Uint32 *)propdata;
slouken@4520
   344
slouken@4520
   345
                        printf("{");
slouken@4520
   346
                        for (i = 0; i < items_read; i++) {
slouken@4520
   347
                            printf(" %d", values[i]);
slouken@4520
   348
                        }
slouken@4520
   349
                        printf(" }\n");
slouken@4520
   350
                    } else if (real_format == 16) {
slouken@4520
   351
                        Uint16 *values = (Uint16 *)propdata;
slouken@4520
   352
slouken@4520
   353
                        printf("{");
slouken@4520
   354
                        for (i = 0; i < items_read; i++) {
slouken@4520
   355
                            printf(" %d", values[i]);
slouken@4520
   356
                        }
slouken@4520
   357
                        printf(" }\n");
slouken@4520
   358
                    } else if (real_format == 8) {
slouken@4520
   359
                        Uint8 *values = (Uint8 *)propdata;
slouken@4520
   360
slouken@4520
   361
                        printf("{");
slouken@4520
   362
                        for (i = 0; i < items_read; i++) {
slouken@4520
   363
                            printf(" %d", values[i]);
slouken@4520
   364
                        }
slouken@4520
   365
                        printf(" }\n");
slouken@4520
   366
                    }
slouken@4520
   367
                } else if (real_type == XA_STRING ||
slouken@4520
   368
                           real_type == videodata->UTF8_STRING) {
slouken@4520
   369
                    printf("{ \"%s\" }\n", propdata);
slouken@4520
   370
                } else if (real_type == XA_ATOM) {
slouken@4518
   371
                    Atom *atoms = (Atom *)propdata;
slouken@4520
   372
slouken@4520
   373
                    printf("{");
slouken@4518
   374
                    for (i = 0; i < items_read; i++) {
slouken@4520
   375
                        char *name = XGetAtomName(display, atoms[i]);
slouken@4520
   376
                        if (name) {
slouken@4520
   377
                            printf(" %s", name);
slouken@4520
   378
                            XFree(name);
slouken@4518
   379
                        }
slouken@4518
   380
                    }
slouken@4520
   381
                    printf(" }\n");
slouken@4520
   382
                } else {
slouken@4520
   383
                    char *name = XGetAtomName(display, real_type);
slouken@4520
   384
                    printf("Unknown type: %ld (%s)\n", real_type, name ? name : "UNKNOWN");
slouken@4520
   385
                    if (name) {
slouken@4520
   386
                        XFree(name);
slouken@4520
   387
                    }
slouken@4518
   388
                }
slouken@4520
   389
            }
slouken@4518
   390
#endif
slouken@4518
   391
        }
slouken@4518
   392
        break;
slouken@4518
   393
slouken@4508
   394
    /* Copy the selection from XA_CUT_BUFFER0 to the requested property */
slouken@4508
   395
    case SelectionRequest: {
slouken@4508
   396
            XSelectionRequestEvent *req;
slouken@4508
   397
            XEvent sevent;
slouken@4508
   398
            int seln_format;
slouken@4508
   399
            unsigned long nbytes;
slouken@4508
   400
            unsigned long overflow;
slouken@4508
   401
            unsigned char *seln_data;
slouken@4508
   402
slouken@4508
   403
            req = &xevent.xselectionrequest;
slouken@4508
   404
#ifdef DEBUG_XEVENTS
slouken@4508
   405
            printf("SelectionRequest (requestor = %ld, target = %ld)\n",
slouken@4508
   406
                req->requestor, req->target);
slouken@4508
   407
#endif
slouken@4508
   408
slouken@4524
   409
            SDL_zero(sevent);
slouken@4520
   410
            sevent.xany.type = SelectionNotify;
slouken@4508
   411
            sevent.xselection.selection = req->selection;
slouken@4508
   412
            sevent.xselection.target = None;
slouken@4508
   413
            sevent.xselection.property = None;
slouken@4508
   414
            sevent.xselection.requestor = req->requestor;
slouken@4508
   415
            sevent.xselection.time = req->time;
slouken@4508
   416
            if (XGetWindowProperty(display, DefaultRootWindow(display),
slouken@4508
   417
                    XA_CUT_BUFFER0, 0, INT_MAX/4, False, req->target,
slouken@4508
   418
                    &sevent.xselection.target, &seln_format, &nbytes,
slouken@4508
   419
                    &overflow, &seln_data) == Success) {
slouken@4508
   420
                if (sevent.xselection.target == req->target) {
slouken@4508
   421
                    XChangeProperty(display, req->requestor, req->property,
slouken@4508
   422
                        sevent.xselection.target, seln_format, PropModeReplace,
slouken@4508
   423
                        seln_data, nbytes);
slouken@4508
   424
                    sevent.xselection.property = req->property;
slouken@4508
   425
                }
slouken@4508
   426
                XFree(seln_data);
slouken@4508
   427
            }
slouken@4508
   428
            XSendEvent(display, req->requestor, False, 0, &sevent);
slouken@4508
   429
            XSync(display, False);
slouken@4508
   430
        }
slouken@4508
   431
        break;
slouken@4508
   432
slouken@4508
   433
    case SelectionNotify: {
slouken@4508
   434
#ifdef DEBUG_XEVENTS
slouken@4508
   435
            printf("SelectionNotify (requestor = %ld, target = %ld)\n",
slouken@4508
   436
                xevent.xselection.requestor, xevent.xselection.target);
slouken@4508
   437
#endif
slouken@4508
   438
            videodata->selection_waiting = SDL_FALSE;
slouken@4508
   439
        }
slouken@4508
   440
        break;
slouken@4508
   441
slouken@1951
   442
    default:{
slouken@1951
   443
#ifdef DEBUG_XEVENTS
slouken@2940
   444
            printf("Unhandled event %d\n", xevent.type);
slouken@1951
   445
#endif
slouken@1951
   446
        }
slouken@1951
   447
        break;
slouken@1951
   448
    }
slouken@1951
   449
}
slouken@1951
   450
slouken@1951
   451
/* Ack!  XPending() actually performs a blocking read if no events available */
slouken@4472
   452
static int
slouken@1951
   453
X11_Pending(Display * display)
slouken@1951
   454
{
slouken@1951
   455
    /* Flush the display connection and look to see if events are queued */
slouken@1951
   456
    XFlush(display);
slouken@1951
   457
    if (XEventsQueued(display, QueuedAlready)) {
slouken@1951
   458
        return (1);
slouken@1951
   459
    }
slouken@1951
   460
slouken@1951
   461
    /* More drastic measures are required -- see if X is ready to talk */
slouken@1951
   462
    {
slouken@1951
   463
        static struct timeval zero_time;        /* static == 0 */
slouken@1951
   464
        int x11_fd;
slouken@1951
   465
        fd_set fdset;
slouken@1951
   466
slouken@1951
   467
        x11_fd = ConnectionNumber(display);
slouken@1951
   468
        FD_ZERO(&fdset);
slouken@1951
   469
        FD_SET(x11_fd, &fdset);
slouken@1951
   470
        if (select(x11_fd + 1, &fdset, NULL, NULL, &zero_time) == 1) {
slouken@1951
   471
            return (XPending(display));
slouken@1951
   472
        }
slouken@1951
   473
    }
slouken@1951
   474
slouken@1951
   475
    /* Oh well, nothing is ready .. */
slouken@1951
   476
    return (0);
slouken@1951
   477
}
slouken@1951
   478
slouken@1951
   479
void
slouken@1951
   480
X11_PumpEvents(_THIS)
slouken@1951
   481
{
slouken@1951
   482
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   483
slouken@3025
   484
    /* Update activity every 30 seconds to prevent screensaver */
slouken@3025
   485
    if (_this->suspend_screensaver) {
slouken@3025
   486
        Uint32 now = SDL_GetTicks();
slouken@3025
   487
        if (!data->screensaver_activity ||
slouken@3040
   488
            (int) (now - data->screensaver_activity) >= 30000) {
slouken@3025
   489
            XResetScreenSaver(data->display);
slouken@3025
   490
            data->screensaver_activity = now;
slouken@3025
   491
        }
slouken@3025
   492
    }
slouken@3025
   493
slouken@1951
   494
    /* Keep processing pending events */
slouken@1951
   495
    while (X11_Pending(data->display)) {
slouken@1951
   496
        X11_DispatchEvent(_this);
slouken@1951
   497
    }
slouken@1951
   498
}
slouken@1951
   499
slouken@3030
   500
/* This is so wrong it hurts */
slouken@3030
   501
#define GNOME_SCREENSAVER_HACK
slouken@3030
   502
#ifdef GNOME_SCREENSAVER_HACK
slouken@3030
   503
#include <unistd.h>
slouken@3030
   504
static pid_t screensaver_inhibit_pid;
slouken@3040
   505
static void
slouken@3040
   506
gnome_screensaver_disable()
slouken@3030
   507
{
slouken@3030
   508
    screensaver_inhibit_pid = fork();
slouken@3030
   509
    if (screensaver_inhibit_pid == 0) {
slouken@3031
   510
        close(0);
slouken@3031
   511
        close(1);
slouken@3031
   512
        close(2);
slouken@3030
   513
        execl("/usr/bin/gnome-screensaver-command",
slouken@3030
   514
              "gnome-screensaver-command",
slouken@3030
   515
              "--inhibit",
slouken@3030
   516
              "--reason",
slouken@3040
   517
              "GNOME screensaver doesn't respect MIT-SCREEN-SAVER", NULL);
slouken@3030
   518
        exit(2);
slouken@3030
   519
    }
slouken@3030
   520
}
slouken@3040
   521
static void
slouken@3040
   522
gnome_screensaver_enable()
slouken@3030
   523
{
slouken@3030
   524
    kill(screensaver_inhibit_pid, 15);
slouken@3030
   525
}
slouken@3030
   526
#endif
slouken@3030
   527
slouken@1951
   528
void
slouken@3025
   529
X11_SuspendScreenSaver(_THIS)
slouken@1951
   530
{
slouken@3025
   531
#if SDL_VIDEO_DRIVER_X11_SCRNSAVER
slouken@3025
   532
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@3025
   533
    int dummy;
slouken@3025
   534
    int major_version, minor_version;
slouken@1951
   535
slouken@3030
   536
    if (SDL_X11_HAVE_XSS) {
slouken@3030
   537
        /* XScreenSaverSuspend was introduced in MIT-SCREEN-SAVER 1.1 */
slouken@3030
   538
        if (!XScreenSaverQueryExtension(data->display, &dummy, &dummy) ||
slouken@3030
   539
            !XScreenSaverQueryVersion(data->display,
slouken@3030
   540
                                      &major_version, &minor_version) ||
slouken@3030
   541
            major_version < 1 || (major_version == 1 && minor_version < 1)) {
slouken@3030
   542
            return;
slouken@3030
   543
        }
slouken@3030
   544
slouken@3030
   545
        XScreenSaverSuspend(data->display, _this->suspend_screensaver);
slouken@3030
   546
        XResetScreenSaver(data->display);
slouken@1951
   547
    }
slouken@3030
   548
#endif
slouken@1951
   549
slouken@3030
   550
#ifdef GNOME_SCREENSAVER_HACK
slouken@3030
   551
    if (_this->suspend_screensaver) {
slouken@3030
   552
        gnome_screensaver_disable();
slouken@3030
   553
    } else {
slouken@3030
   554
        gnome_screensaver_enable();
slouken@3025
   555
    }
slouken@3025
   556
#endif
slouken@1951
   557
}
slouken@1951
   558
slouken@1951
   559
/* vi: set ts=4 sw=4 expandtab: */