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