src/video/x11/SDL_x11events.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 12 Mar 2011 13:21:57 -0800
changeset 5481 22dfc3958dc3
parent 5408 35a63789c3f2
child 5530 4e46a7b6773d
permissions -rw-r--r--
Fixed so code will compile with SDL_config_minimal.h
slouken@1951
     1
/*
slouken@1951
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 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@5481
    24
#if SDL_VIDEO_DRIVER_X11
slouken@5481
    25
slouken@2046
    26
#include <sys/types.h>
slouken@2046
    27
#include <sys/time.h>
slouken@4465
    28
#include <signal.h>
slouken@2046
    29
#include <unistd.h>
slouken@4558
    30
#include <limits.h> /* For INT_MAX */
slouken@2046
    31
slouken@1951
    32
#include "SDL_x11video.h"
slouken@4934
    33
#include "SDL_x11touch.h"
slouken@1951
    34
#include "../../events/SDL_events_c.h"
slouken@2940
    35
#include "../../events/SDL_mouse_c.h"
jim@4642
    36
#include "../../events/SDL_touch_c.h"
slouken@1951
    37
slouken@4465
    38
#include "SDL_timer.h"
slouken@3241
    39
#include "SDL_syswm.h"
slouken@3241
    40
jim@4642
    41
#include <stdio.h>
jim@4642
    42
jim@4681
    43
#ifdef SDL_INPUT_LINUXEV
jim@4642
    44
//Touch Input/event* includes
jim@4642
    45
#include <linux/input.h>
jim@4642
    46
#include <fcntl.h>
jimtla@4669
    47
#endif
slouken@4508
    48
/*#define DEBUG_XEVENTS*/
slouken@4508
    49
slouken@4565
    50
/* Check to see if this is a repeated key.
slouken@4565
    51
   (idea shamelessly lifted from GII -- thanks guys! :)
slouken@4565
    52
 */
slouken@4565
    53
static SDL_bool X11_KeyRepeat(Display *display, XEvent *event)
slouken@4565
    54
{
slouken@4565
    55
    XEvent peekevent;
slouken@4565
    56
slouken@4565
    57
    if (XPending(display)) {
slouken@4565
    58
        XPeekEvent(display, &peekevent);
slouken@4565
    59
        if ((peekevent.type == KeyPress) &&
slouken@4565
    60
            (peekevent.xkey.keycode == event->xkey.keycode) &&
slouken@4565
    61
            ((peekevent.xkey.time-event->xkey.time) < 2)) {
slouken@4565
    62
            return SDL_TRUE;
slouken@4565
    63
        }
slouken@4565
    64
    }
slouken@4565
    65
    return SDL_FALSE;
slouken@4565
    66
}
slouken@4565
    67
slouken@1951
    68
static void
slouken@1951
    69
X11_DispatchEvent(_THIS)
slouken@1951
    70
{
slouken@1951
    71
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@4518
    72
    Display *display = videodata->display;
slouken@1951
    73
    SDL_WindowData *data;
slouken@1951
    74
    XEvent xevent;
slouken@1951
    75
    int i;
slouken@1951
    76
slouken@1951
    77
    SDL_zero(xevent);           /* valgrind fix. --ryan. */
slouken@4518
    78
    XNextEvent(display, &xevent);
slouken@1951
    79
bob@2325
    80
    /* filter events catchs XIM events and sends them to the correct
bob@2325
    81
       handler */
bob@2325
    82
    if (XFilterEvent(&xevent, None) == True) {
bob@2327
    83
#if 0
bob@2328
    84
        printf("Filtered event type = %d display = %d window = %d\n",
bob@2328
    85
               xevent.type, xevent.xany.display, xevent.xany.window);
bob@2325
    86
#endif
bob@2325
    87
        return;
bob@2325
    88
    }
bob@2325
    89
slouken@1951
    90
    /* Send a SDL_SYSWMEVENT if the application wants them */
slouken@4429
    91
    if (SDL_GetEventState(SDL_SYSWMEVENT) == SDL_ENABLE) {
slouken@1951
    92
        SDL_SysWMmsg wmmsg;
slouken@1951
    93
slouken@1951
    94
        SDL_VERSION(&wmmsg.version);
slouken@1951
    95
        wmmsg.subsystem = SDL_SYSWM_X11;
slouken@5056
    96
        wmmsg.msg.x11.event = xevent;
slouken@1951
    97
        SDL_SendSysWMEvent(&wmmsg);
slouken@1951
    98
    }
slouken@1951
    99
slouken@1951
   100
    data = NULL;
bob@2324
   101
    if (videodata && videodata->windowlist) {
bob@2324
   102
        for (i = 0; i < videodata->numwindows; ++i) {
bob@2324
   103
            if ((videodata->windowlist[i] != NULL) &&
slouken@3685
   104
                (videodata->windowlist[i]->xwindow == xevent.xany.window)) {
bob@2324
   105
                data = videodata->windowlist[i];
bob@2324
   106
                break;
bob@2324
   107
            }
slouken@1951
   108
        }
slouken@1951
   109
    }
slouken@1951
   110
    if (!data) {
slouken@1951
   111
        return;
slouken@1951
   112
    }
slouken@4518
   113
bob@2327
   114
#if 0
bob@2328
   115
    printf("type = %d display = %d window = %d\n",
bob@2328
   116
           xevent.type, xevent.xany.display, xevent.xany.window);
bob@2327
   117
#endif
slouken@1951
   118
    switch (xevent.type) {
slouken@1951
   119
slouken@1951
   120
        /* Gaining mouse coverage? */
slouken@1951
   121
    case EnterNotify:{
slouken@1951
   122
#ifdef DEBUG_XEVENTS
bob@3195
   123
            printf("EnterNotify! (%d,%d,%d)\n", 
slouken@4558
   124
                   xevent.xcrossing.x,
slouken@4558
   125
                   xevent.xcrossing.y,
bob@3195
   126
                   xevent.xcrossing.mode);
slouken@1951
   127
            if (xevent.xcrossing.mode == NotifyGrab)
slouken@1951
   128
                printf("Mode: NotifyGrab\n");
slouken@1951
   129
            if (xevent.xcrossing.mode == NotifyUngrab)
slouken@1951
   130
                printf("Mode: NotifyUngrab\n");
slouken@1951
   131
#endif
slouken@4465
   132
            SDL_SetMouseFocus(data->window);
slouken@1951
   133
        }
slouken@1951
   134
        break;
slouken@1951
   135
        /* Losing mouse coverage? */
slouken@1951
   136
    case LeaveNotify:{
slouken@1951
   137
#ifdef DEBUG_XEVENTS
bob@3195
   138
            printf("LeaveNotify! (%d,%d,%d)\n", 
slouken@4558
   139
                   xevent.xcrossing.x,
slouken@4558
   140
                   xevent.xcrossing.y,
bob@3195
   141
                   xevent.xcrossing.mode);
slouken@1951
   142
            if (xevent.xcrossing.mode == NotifyGrab)
slouken@1951
   143
                printf("Mode: NotifyGrab\n");
slouken@1951
   144
            if (xevent.xcrossing.mode == NotifyUngrab)
slouken@1951
   145
                printf("Mode: NotifyUngrab\n");
slouken@1951
   146
#endif
slouken@4558
   147
            if (xevent.xcrossing.mode != NotifyGrab &&
slouken@4558
   148
                xevent.xcrossing.mode != NotifyUngrab &&
slouken@4558
   149
                xevent.xcrossing.detail != NotifyInferior) {
slouken@4465
   150
                SDL_SetMouseFocus(NULL);
slouken@1951
   151
            }
slouken@1951
   152
        }
slouken@1951
   153
        break;
slouken@1951
   154
slouken@1951
   155
        /* Gaining input focus? */
slouken@1951
   156
    case FocusIn:{
slouken@1951
   157
#ifdef DEBUG_XEVENTS
slouken@1951
   158
            printf("FocusIn!\n");
slouken@1951
   159
#endif
slouken@4465
   160
            SDL_SetKeyboardFocus(data->window);
slouken@1951
   161
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   162
            if (data->ic) {
slouken@1951
   163
                XSetICFocus(data->ic);
slouken@1951
   164
            }
slouken@1951
   165
#endif
slouken@1951
   166
        }
slouken@1951
   167
        break;
slouken@1951
   168
slouken@1951
   169
        /* Losing input focus? */
slouken@1951
   170
    case FocusOut:{
slouken@1951
   171
#ifdef DEBUG_XEVENTS
slouken@1951
   172
            printf("FocusOut!\n");
slouken@1951
   173
#endif
slouken@4465
   174
            SDL_SetKeyboardFocus(NULL);
slouken@1951
   175
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   176
            if (data->ic) {
slouken@1951
   177
                XUnsetICFocus(data->ic);
slouken@1951
   178
            }
slouken@1951
   179
#endif
slouken@1951
   180
        }
slouken@1951
   181
        break;
slouken@1951
   182
slouken@1951
   183
        /* Generated upon EnterWindow and FocusIn */
slouken@1951
   184
    case KeymapNotify:{
slouken@1951
   185
#ifdef DEBUG_XEVENTS
slouken@1951
   186
            printf("KeymapNotify!\n");
slouken@1951
   187
#endif
slouken@1951
   188
            /* FIXME:
slouken@1951
   189
               X11_SetKeyboardState(SDL_Display, xevent.xkeymap.key_vector);
slouken@1951
   190
             */
slouken@1951
   191
        }
slouken@1951
   192
        break;
slouken@1951
   193
slouken@2305
   194
        /* Has the keyboard layout changed? */
slouken@2305
   195
    case MappingNotify:{
slouken@2305
   196
#ifdef DEBUG_XEVENTS
slouken@2305
   197
            printf("MappingNotify!\n");
slouken@2305
   198
#endif
slouken@2306
   199
            X11_UpdateKeymap(_this);
slouken@2305
   200
        }
slouken@2305
   201
        break;
slouken@2305
   202
slouken@1951
   203
        /* Key press? */
slouken@1951
   204
    case KeyPress:{
slouken@1951
   205
            KeyCode keycode = xevent.xkey.keycode;
bob@2300
   206
            KeySym keysym = NoSymbol;
slouken@5218
   207
            SDL_Scancode scancode;
slouken@2306
   208
            char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];
bob@2325
   209
            Status status = 0;
slouken@1951
   210
slouken@1951
   211
#ifdef DEBUG_XEVENTS
slouken@1951
   212
            printf("KeyPress (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
slouken@1951
   213
#endif
slouken@4465
   214
            SDL_SendKeyboardKey(SDL_PRESSED, videodata->key_layout[keycode]);
slouken@4535
   215
#if 1
slouken@2305
   216
            if (videodata->key_layout[keycode] == SDLK_UNKNOWN) {
slouken@2305
   217
                int min_keycode, max_keycode;
slouken@4518
   218
                XDisplayKeycodes(display, &min_keycode, &max_keycode);
slouken@4518
   219
                keysym = XKeycodeToKeysym(display, keycode, 0);
bob@2299
   220
                fprintf(stderr,
slouken@4556
   221
                        "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
   222
                        keycode, keycode - min_keycode, keysym,
slouken@2305
   223
                        XKeysymToString(keysym));
bob@2299
   224
            }
bob@2295
   225
#endif
bob@2325
   226
            /* */
slouken@2305
   227
            SDL_zero(text);
bob@2325
   228
#ifdef X_HAVE_UTF8_STRING
bob@2325
   229
            if (data->ic) {
bob@2325
   230
                Xutf8LookupString(data->ic, &xevent.xkey, text, sizeof(text),
slouken@2738
   231
                                  &keysym, &status);
bob@2325
   232
            }
bob@2325
   233
#else
slouken@2306
   234
            XLookupString(&xevent.xkey, text, sizeof(text), &keysym, NULL);
bob@2325
   235
#endif
slouken@2305
   236
            if (*text) {
slouken@4465
   237
                SDL_SendKeyboardText(text);
bob@2300
   238
            }
slouken@1951
   239
        }
slouken@1951
   240
        break;
slouken@1951
   241
slouken@1951
   242
        /* Key release? */
slouken@1951
   243
    case KeyRelease:{
slouken@1951
   244
            KeyCode keycode = xevent.xkey.keycode;
slouken@1951
   245
slouken@1951
   246
#ifdef DEBUG_XEVENTS
slouken@1951
   247
            printf("KeyRelease (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
slouken@1951
   248
#endif
slouken@4565
   249
            if (X11_KeyRepeat(display, &xevent)) {
slouken@4565
   250
                /* We're about to get a repeated key down, ignore the key up */
slouken@4565
   251
                break;
slouken@4565
   252
            }
slouken@4465
   253
            SDL_SendKeyboardKey(SDL_RELEASED, videodata->key_layout[keycode]);
slouken@1951
   254
        }
slouken@1951
   255
        break;
slouken@1951
   256
slouken@1951
   257
        /* Have we been iconified? */
slouken@1951
   258
    case UnmapNotify:{
slouken@1951
   259
#ifdef DEBUG_XEVENTS
slouken@1951
   260
            printf("UnmapNotify!\n");
slouken@1951
   261
#endif
slouken@3685
   262
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@3685
   263
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1951
   264
        }
slouken@1951
   265
        break;
slouken@1951
   266
slouken@1951
   267
        /* Have we been restored? */
slouken@1951
   268
    case MapNotify:{
slouken@1951
   269
#ifdef DEBUG_XEVENTS
slouken@1951
   270
            printf("MapNotify!\n");
slouken@1951
   271
#endif
slouken@3685
   272
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@3685
   273
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@1951
   274
        }
slouken@1951
   275
        break;
slouken@1951
   276
slouken@1951
   277
        /* Have we been resized or moved? */
slouken@1951
   278
    case ConfigureNotify:{
slouken@1951
   279
#ifdef DEBUG_XEVENTS
slouken@1951
   280
            printf("ConfigureNotify! (resize: %dx%d)\n",
slouken@1951
   281
                   xevent.xconfigure.width, xevent.xconfigure.height);
slouken@1951
   282
#endif
slouken@3685
   283
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED,
slouken@1951
   284
                                xevent.xconfigure.x, xevent.xconfigure.y);
slouken@3685
   285
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED,
slouken@1951
   286
                                xevent.xconfigure.width,
slouken@1951
   287
                                xevent.xconfigure.height);
slouken@1951
   288
        }
slouken@1951
   289
        break;
slouken@1951
   290
slouken@1951
   291
        /* Have we been requested to quit (or another client message?) */
slouken@1951
   292
    case ClientMessage:{
slouken@1951
   293
            if ((xevent.xclient.format == 32) &&
slouken@1951
   294
                (xevent.xclient.data.l[0] == videodata->WM_DELETE_WINDOW)) {
slouken@1951
   295
slouken@3685
   296
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
slouken@1951
   297
            }
slouken@1951
   298
        }
slouken@1951
   299
        break;
slouken@1951
   300
slouken@1951
   301
        /* Do we need to refresh ourselves? */
slouken@1951
   302
    case Expose:{
slouken@1951
   303
#ifdef DEBUG_XEVENTS
slouken@1951
   304
            printf("Expose (count = %d)\n", xevent.xexpose.count);
slouken@1951
   305
#endif
slouken@3685
   306
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
slouken@1951
   307
        }
slouken@1951
   308
        break;
slouken@1951
   309
slouken@4465
   310
    case MotionNotify:{
slouken@4465
   311
#ifdef DEBUG_MOTION
slouken@4465
   312
            printf("X11 motion: %d,%d\n", xevent.xmotion.x, xevent.xmotion.y);
slouken@4465
   313
#endif
slouken@4484
   314
            SDL_SendMouseMotion(data->window, 0, xevent.xmotion.x, xevent.xmotion.y);
slouken@4465
   315
        }
slouken@4465
   316
        break;
slouken@4465
   317
slouken@4465
   318
    case ButtonPress:{
slouken@4484
   319
            SDL_SendMouseButton(data->window, SDL_PRESSED, xevent.xbutton.button);
slouken@4465
   320
        }
slouken@4465
   321
        break;
slouken@4465
   322
slouken@4465
   323
    case ButtonRelease:{
slouken@4484
   324
            SDL_SendMouseButton(data->window, SDL_RELEASED, xevent.xbutton.button);
slouken@4465
   325
        }
slouken@4465
   326
        break;
slouken@4465
   327
slouken@4518
   328
    case PropertyNotify:{
slouken@4518
   329
#ifdef DEBUG_XEVENTS
slouken@4520
   330
            unsigned char *propdata;
slouken@4520
   331
            int status, real_format;
slouken@4520
   332
            Atom real_type;
slouken@4520
   333
            unsigned long items_read, items_left, i;
slouken@4520
   334
slouken@4518
   335
            char *name = XGetAtomName(display, xevent.xproperty.atom);
slouken@4518
   336
            if (name) {
slouken@4556
   337
                printf("PropertyNotify: %s %s\n", name, (xevent.xproperty.state == PropertyDelete) ? "deleted" : "changed");
slouken@4518
   338
                XFree(name);
slouken@4518
   339
            }
slouken@4518
   340
slouken@4520
   341
            status = XGetWindowProperty(display, data->xwindow, xevent.xproperty.atom, 0L, 8192L, False, AnyPropertyType, &real_type, &real_format, &items_read, &items_left, &propdata);
slouken@4556
   342
            if (status == Success && items_read > 0) {
slouken@4520
   343
                if (real_type == XA_INTEGER) {
slouken@4520
   344
                    int *values = (int *)propdata;
slouken@4520
   345
slouken@4520
   346
                    printf("{");
slouken@4520
   347
                    for (i = 0; i < items_read; i++) {
slouken@4520
   348
                        printf(" %d", values[i]);
slouken@4520
   349
                    }
slouken@4520
   350
                    printf(" }\n");
slouken@4520
   351
                } else if (real_type == XA_CARDINAL) {
slouken@4520
   352
                    if (real_format == 32) {
slouken@4520
   353
                        Uint32 *values = (Uint32 *)propdata;
slouken@4520
   354
slouken@4520
   355
                        printf("{");
slouken@4520
   356
                        for (i = 0; i < items_read; i++) {
slouken@4520
   357
                            printf(" %d", values[i]);
slouken@4520
   358
                        }
slouken@4520
   359
                        printf(" }\n");
slouken@4520
   360
                    } else if (real_format == 16) {
slouken@4520
   361
                        Uint16 *values = (Uint16 *)propdata;
slouken@4520
   362
slouken@4520
   363
                        printf("{");
slouken@4520
   364
                        for (i = 0; i < items_read; i++) {
slouken@4520
   365
                            printf(" %d", values[i]);
slouken@4520
   366
                        }
slouken@4520
   367
                        printf(" }\n");
slouken@4520
   368
                    } else if (real_format == 8) {
slouken@4520
   369
                        Uint8 *values = (Uint8 *)propdata;
slouken@4520
   370
slouken@4520
   371
                        printf("{");
slouken@4520
   372
                        for (i = 0; i < items_read; i++) {
slouken@4520
   373
                            printf(" %d", values[i]);
slouken@4520
   374
                        }
slouken@4520
   375
                        printf(" }\n");
slouken@4520
   376
                    }
slouken@4520
   377
                } else if (real_type == XA_STRING ||
slouken@4520
   378
                           real_type == videodata->UTF8_STRING) {
slouken@4520
   379
                    printf("{ \"%s\" }\n", propdata);
slouken@4520
   380
                } else if (real_type == XA_ATOM) {
slouken@4518
   381
                    Atom *atoms = (Atom *)propdata;
slouken@4520
   382
slouken@4520
   383
                    printf("{");
slouken@4518
   384
                    for (i = 0; i < items_read; i++) {
slouken@4520
   385
                        char *name = XGetAtomName(display, atoms[i]);
slouken@4520
   386
                        if (name) {
slouken@4520
   387
                            printf(" %s", name);
slouken@4520
   388
                            XFree(name);
slouken@4518
   389
                        }
slouken@4518
   390
                    }
slouken@4520
   391
                    printf(" }\n");
slouken@4520
   392
                } else {
slouken@4520
   393
                    char *name = XGetAtomName(display, real_type);
slouken@4520
   394
                    printf("Unknown type: %ld (%s)\n", real_type, name ? name : "UNKNOWN");
slouken@4520
   395
                    if (name) {
slouken@4520
   396
                        XFree(name);
slouken@4520
   397
                    }
slouken@4518
   398
                }
slouken@4520
   399
            }
slouken@4518
   400
#endif
slouken@4518
   401
        }
slouken@4518
   402
        break;
slouken@4518
   403
slouken@4508
   404
    /* Copy the selection from XA_CUT_BUFFER0 to the requested property */
slouken@4508
   405
    case SelectionRequest: {
slouken@4508
   406
            XSelectionRequestEvent *req;
slouken@4508
   407
            XEvent sevent;
slouken@4508
   408
            int seln_format;
slouken@4508
   409
            unsigned long nbytes;
slouken@4508
   410
            unsigned long overflow;
slouken@4508
   411
            unsigned char *seln_data;
slouken@4508
   412
slouken@4508
   413
            req = &xevent.xselectionrequest;
slouken@4508
   414
#ifdef DEBUG_XEVENTS
slouken@4508
   415
            printf("SelectionRequest (requestor = %ld, target = %ld)\n",
slouken@4508
   416
                req->requestor, req->target);
slouken@4508
   417
#endif
slouken@4508
   418
slouken@4524
   419
            SDL_zero(sevent);
slouken@4520
   420
            sevent.xany.type = SelectionNotify;
slouken@4508
   421
            sevent.xselection.selection = req->selection;
slouken@4508
   422
            sevent.xselection.target = None;
slouken@4508
   423
            sevent.xselection.property = None;
slouken@4508
   424
            sevent.xselection.requestor = req->requestor;
slouken@4508
   425
            sevent.xselection.time = req->time;
slouken@4508
   426
            if (XGetWindowProperty(display, DefaultRootWindow(display),
slouken@4508
   427
                    XA_CUT_BUFFER0, 0, INT_MAX/4, False, req->target,
slouken@4508
   428
                    &sevent.xselection.target, &seln_format, &nbytes,
slouken@4508
   429
                    &overflow, &seln_data) == Success) {
slouken@4508
   430
                if (sevent.xselection.target == req->target) {
slouken@4508
   431
                    XChangeProperty(display, req->requestor, req->property,
slouken@4508
   432
                        sevent.xselection.target, seln_format, PropModeReplace,
slouken@4508
   433
                        seln_data, nbytes);
slouken@4508
   434
                    sevent.xselection.property = req->property;
slouken@4508
   435
                }
slouken@4508
   436
                XFree(seln_data);
slouken@4508
   437
            }
slouken@4508
   438
            XSendEvent(display, req->requestor, False, 0, &sevent);
slouken@4508
   439
            XSync(display, False);
slouken@4508
   440
        }
slouken@4508
   441
        break;
slouken@4508
   442
slouken@4508
   443
    case SelectionNotify: {
slouken@4508
   444
#ifdef DEBUG_XEVENTS
slouken@4508
   445
            printf("SelectionNotify (requestor = %ld, target = %ld)\n",
slouken@4508
   446
                xevent.xselection.requestor, xevent.xselection.target);
slouken@4508
   447
#endif
slouken@4508
   448
            videodata->selection_waiting = SDL_FALSE;
slouken@4508
   449
        }
slouken@4508
   450
        break;
slouken@4508
   451
slouken@1951
   452
    default:{
slouken@1951
   453
#ifdef DEBUG_XEVENTS
slouken@2940
   454
            printf("Unhandled event %d\n", xevent.type);
slouken@1951
   455
#endif
slouken@1951
   456
        }
slouken@1951
   457
        break;
slouken@1951
   458
    }
slouken@1951
   459
}
slouken@1951
   460
slouken@1951
   461
/* Ack!  XPending() actually performs a blocking read if no events available */
slouken@4472
   462
static int
slouken@1951
   463
X11_Pending(Display * display)
slouken@1951
   464
{
slouken@1951
   465
    /* Flush the display connection and look to see if events are queued */
slouken@1951
   466
    XFlush(display);
slouken@1951
   467
    if (XEventsQueued(display, QueuedAlready)) {
slouken@1951
   468
        return (1);
slouken@1951
   469
    }
slouken@1951
   470
slouken@1951
   471
    /* More drastic measures are required -- see if X is ready to talk */
slouken@1951
   472
    {
slouken@1951
   473
        static struct timeval zero_time;        /* static == 0 */
slouken@1951
   474
        int x11_fd;
slouken@1951
   475
        fd_set fdset;
slouken@1951
   476
slouken@1951
   477
        x11_fd = ConnectionNumber(display);
slouken@1951
   478
        FD_ZERO(&fdset);
slouken@1951
   479
        FD_SET(x11_fd, &fdset);
slouken@1951
   480
        if (select(x11_fd + 1, &fdset, NULL, NULL, &zero_time) == 1) {
slouken@1951
   481
            return (XPending(display));
slouken@1951
   482
        }
slouken@1951
   483
    }
slouken@1951
   484
slouken@1951
   485
    /* Oh well, nothing is ready .. */
slouken@1951
   486
    return (0);
slouken@1951
   487
}
slouken@1951
   488
slouken@1951
   489
void
slouken@1951
   490
X11_PumpEvents(_THIS)
slouken@1951
   491
{
slouken@1951
   492
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   493
slouken@3025
   494
    /* Update activity every 30 seconds to prevent screensaver */
slouken@3025
   495
    if (_this->suspend_screensaver) {
slouken@3025
   496
        Uint32 now = SDL_GetTicks();
slouken@3025
   497
        if (!data->screensaver_activity ||
slouken@3040
   498
            (int) (now - data->screensaver_activity) >= 30000) {
slouken@3025
   499
            XResetScreenSaver(data->display);
slouken@3025
   500
            data->screensaver_activity = now;
slouken@3025
   501
        }
slouken@3025
   502
    }
slouken@3025
   503
slouken@1951
   504
    /* Keep processing pending events */
slouken@1951
   505
    while (X11_Pending(data->display)) {
slouken@1951
   506
        X11_DispatchEvent(_this);
slouken@1951
   507
    }
jim@4640
   508
jim@4681
   509
#ifdef SDL_INPUT_LINUXEV
jim@4642
   510
    /* Process Touch events - TODO When X gets touch support, use that instead*/
jim@4642
   511
    int i = 0,rd;
jim@4643
   512
    char name[256];
jim@4642
   513
    struct input_event ev[64];
jim@4642
   514
    int size = sizeof (struct input_event);
jim@4645
   515
jim@4642
   516
    for(i = 0;i < SDL_GetNumTouch();++i) {
jim@4642
   517
	SDL_Touch* touch = SDL_GetTouchIndex(i);
jim@4642
   518
	if(!touch) printf("Touch %i/%i DNE\n",i,SDL_GetNumTouch());
jim@4642
   519
	EventTouchData* data;
jim@4645
   520
	data = (EventTouchData*)(touch->driverdata);
jim@4645
   521
	if(data == NULL) {
jim@4645
   522
	  printf("No driver data\n");
jim@4645
   523
	  continue;
jim@4642
   524
	}
jim@4642
   525
	if(data->eventStream <= 0) 
jim@4642
   526
	    printf("Error: Couldn't open stream\n");
jim@4642
   527
	rd = read(data->eventStream, ev, size * 64);
jim@4642
   528
	//printf("Got %i/%i bytes\n",rd,size);
jim@4642
   529
	if(rd >= size) {
jim@4642
   530
	    for (i = 0; i < rd / sizeof(struct input_event); i++) {
jim@4642
   531
		switch (ev[i].type) {
jim@4642
   532
		case EV_ABS:
jim@4642
   533
		    //printf("Got position x: %i!\n",data->x);
jim@4644
   534
		    switch (ev[i].code) {
jim@4644
   535
			case ABS_X:
jim@4644
   536
			    data->x = ev[i].value;
jim@4644
   537
			    break;
jim@4644
   538
			case ABS_Y:
jim@4644
   539
			    data->y = ev[i].value;
jim@4644
   540
			    break;
jim@4644
   541
			case ABS_PRESSURE:
jim@4644
   542
			    data->pressure = ev[i].value;
jim@4646
   543
			    if(data->pressure < 0) data->pressure = 0;
jim@4644
   544
			    break;
jim@4644
   545
			case ABS_MISC:
jim@4683
   546
			    if(ev[i].value == 0)
jim@4683
   547
			        data->up = SDL_TRUE;			    
jim@4644
   548
			    break;
jim@4644
   549
			}
jim@4642
   550
		    break;
jim@4642
   551
		case EV_MSC:
jim@4642
   552
		    if(ev[i].code == MSC_SERIAL)
jim@4642
   553
			data->finger = ev[i].value;
jim@4642
   554
		    break;
jim@4642
   555
		case EV_SYN:
jim@4646
   556
		  //printf("Id: %i\n",touch->id); 
jim@4655
   557
		  if(data->up) {
jim@4655
   558
		      SDL_SendFingerDown(touch->id,data->finger,
jim@4655
   559
			  	       SDL_FALSE,data->x,data->y,
jim@4683
   560
				       data->pressure);		    
jim@4655
   561
		  }
jim@4655
   562
		  else if(data->x >= 0 || data->y >= 0)
jim@4683
   563
		    SDL_SendTouchMotion(touch->id,data->finger, 
jim@4655
   564
					SDL_FALSE,data->x,data->y,
jim@4683
   565
					data->pressure);
jim@4655
   566
		  
jim@4642
   567
		    //printf("Synched: %i tx: %i, ty: %i\n",
jim@4642
   568
		    //	   data->finger,data->x,data->y);
jim@4683
   569
		  data->x = -1;
jim@4683
   570
		  data->y = -1;
jim@4683
   571
		  data->pressure = -1;
jim@4683
   572
		  data->finger = 0;
jim@4683
   573
		  data->up = SDL_FALSE;
jim@4642
   574
		    
jim@4683
   575
		  break;		
jim@4642
   576
		}
jim@4642
   577
	    }
jim@4642
   578
	}
jim@4642
   579
    }
jimtla@4669
   580
#endif
slouken@1951
   581
}
slouken@1951
   582
slouken@3030
   583
/* This is so wrong it hurts */
slouken@3030
   584
#define GNOME_SCREENSAVER_HACK
slouken@3030
   585
#ifdef GNOME_SCREENSAVER_HACK
slouken@3030
   586
#include <unistd.h>
slouken@3030
   587
static pid_t screensaver_inhibit_pid;
slouken@3040
   588
static void
slouken@3040
   589
gnome_screensaver_disable()
slouken@3030
   590
{
slouken@3030
   591
    screensaver_inhibit_pid = fork();
slouken@3030
   592
    if (screensaver_inhibit_pid == 0) {
slouken@3031
   593
        close(0);
slouken@3031
   594
        close(1);
slouken@3031
   595
        close(2);
slouken@3030
   596
        execl("/usr/bin/gnome-screensaver-command",
slouken@3030
   597
              "gnome-screensaver-command",
slouken@3030
   598
              "--inhibit",
slouken@3030
   599
              "--reason",
slouken@3040
   600
              "GNOME screensaver doesn't respect MIT-SCREEN-SAVER", NULL);
slouken@3030
   601
        exit(2);
slouken@3030
   602
    }
slouken@3030
   603
}
slouken@3040
   604
static void
slouken@3040
   605
gnome_screensaver_enable()
slouken@3030
   606
{
slouken@3030
   607
    kill(screensaver_inhibit_pid, 15);
slouken@3030
   608
}
slouken@3030
   609
#endif
slouken@3030
   610
slouken@1951
   611
void
slouken@3025
   612
X11_SuspendScreenSaver(_THIS)
slouken@1951
   613
{
slouken@5408
   614
#if SDL_VIDEO_DRIVER_X11_XSCRNSAVER
slouken@3025
   615
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@3025
   616
    int dummy;
slouken@3025
   617
    int major_version, minor_version;
slouken@1951
   618
slouken@3030
   619
    if (SDL_X11_HAVE_XSS) {
slouken@3030
   620
        /* XScreenSaverSuspend was introduced in MIT-SCREEN-SAVER 1.1 */
slouken@3030
   621
        if (!XScreenSaverQueryExtension(data->display, &dummy, &dummy) ||
slouken@3030
   622
            !XScreenSaverQueryVersion(data->display,
slouken@3030
   623
                                      &major_version, &minor_version) ||
slouken@3030
   624
            major_version < 1 || (major_version == 1 && minor_version < 1)) {
slouken@3030
   625
            return;
slouken@3030
   626
        }
slouken@3030
   627
slouken@3030
   628
        XScreenSaverSuspend(data->display, _this->suspend_screensaver);
slouken@3030
   629
        XResetScreenSaver(data->display);
slouken@1951
   630
    }
slouken@3030
   631
#endif
slouken@1951
   632
slouken@3030
   633
#ifdef GNOME_SCREENSAVER_HACK
slouken@3030
   634
    if (_this->suspend_screensaver) {
slouken@3030
   635
        gnome_screensaver_disable();
slouken@3030
   636
    } else {
slouken@3030
   637
        gnome_screensaver_enable();
slouken@3025
   638
    }
slouken@3025
   639
#endif
slouken@1951
   640
}
slouken@1951
   641
slouken@5481
   642
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
   643
slouken@1951
   644
/* vi: set ts=4 sw=4 expandtab: */