src/video/x11/SDL_x11events.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 30 May 2012 11:25:35 -0400
changeset 6311 4e5e46f43438
parent 6184 976d9ec6c290
child 6316 a89c79a3f0cd
permissions -rwxr-xr-x
Initial support for XInput2 by Dimitris Zenios

1.initial work on XInput2 support
2.Implemented relative mouse motion when XInput2 is enabled
3.Created a test app to test relative mouse motion
4.Fixed Bug #1498
slouken@1951
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@1951
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1951
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1951
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1951
    20
*/
slouken@1951
    21
#include "SDL_config.h"
slouken@1951
    22
slouken@5481
    23
#if SDL_VIDEO_DRIVER_X11
slouken@5481
    24
slouken@2046
    25
#include <sys/types.h>
slouken@2046
    26
#include <sys/time.h>
slouken@4465
    27
#include <signal.h>
slouken@2046
    28
#include <unistd.h>
slouken@4558
    29
#include <limits.h> /* For INT_MAX */
slouken@2046
    30
slouken@1951
    31
#include "SDL_x11video.h"
slouken@4934
    32
#include "SDL_x11touch.h"
slouken@1951
    33
#include "../../events/SDL_events_c.h"
slouken@2940
    34
#include "../../events/SDL_mouse_c.h"
jim@4642
    35
#include "../../events/SDL_touch_c.h"
slouken@1951
    36
slouken@4465
    37
#include "SDL_timer.h"
slouken@3241
    38
#include "SDL_syswm.h"
slouken@3241
    39
jim@4642
    40
#include <stdio.h>
jim@4642
    41
jim@4681
    42
#ifdef SDL_INPUT_LINUXEV
jim@4642
    43
//Touch Input/event* includes
jim@4642
    44
#include <linux/input.h>
jim@4642
    45
#include <fcntl.h>
jimtla@4669
    46
#endif
slouken@4508
    47
slouken@4565
    48
/* Check to see if this is a repeated key.
slouken@4565
    49
   (idea shamelessly lifted from GII -- thanks guys! :)
slouken@4565
    50
 */
slouken@4565
    51
static SDL_bool X11_KeyRepeat(Display *display, XEvent *event)
slouken@4565
    52
{
slouken@4565
    53
    XEvent peekevent;
slouken@4565
    54
slouken@4565
    55
    if (XPending(display)) {
slouken@4565
    56
        XPeekEvent(display, &peekevent);
slouken@4565
    57
        if ((peekevent.type == KeyPress) &&
slouken@4565
    58
            (peekevent.xkey.keycode == event->xkey.keycode) &&
slouken@4565
    59
            ((peekevent.xkey.time-event->xkey.time) < 2)) {
slouken@4565
    60
            return SDL_TRUE;
slouken@4565
    61
        }
slouken@4565
    62
    }
slouken@4565
    63
    return SDL_FALSE;
slouken@4565
    64
}
slouken@4565
    65
slouken@5530
    66
static SDL_bool X11_IsWheelEvent(Display * display,XEvent * event,int * ticks)
slouken@5530
    67
{
slouken@5530
    68
    XEvent peekevent;
slouken@5530
    69
    if (XPending(display)) {
slouken@5530
    70
        /* according to the xlib docs, no specific mouse wheel events exist.
slouken@5530
    71
           however, mouse wheel events trigger a button press and a button release
slouken@5530
    72
           immediately. thus, checking if the same button was released at the same
slouken@5530
    73
           time as it was pressed, should be an adequate hack to derive a mouse 
slouken@5530
    74
           wheel event. */
slouken@5530
    75
        XPeekEvent(display,&peekevent);
slouken@5530
    76
        if ((peekevent.type           == ButtonRelease) &&
slouken@5530
    77
            (peekevent.xbutton.button == event->xbutton.button) &&
slouken@5530
    78
            (peekevent.xbutton.time   == event->xbutton.time)) {
slouken@5530
    79
slouken@5530
    80
            /* by default, X11 only knows 5 buttons. on most 3 button + wheel mouse,
slouken@5530
    81
               Button4 maps to wheel up, Button5 maps to wheel down. */
slouken@5530
    82
            if (event->xbutton.button == Button4) {
slouken@5530
    83
                *ticks = 1;
slouken@5530
    84
            }
slouken@6184
    85
            else if (event->xbutton.button == Button5) {
slouken@5530
    86
                *ticks = -1;
slouken@5530
    87
            }
slouken@5530
    88
slouken@5530
    89
            /* remove the following release event, as this is now a wheel event */
slouken@5530
    90
            XNextEvent(display,&peekevent);
slouken@5530
    91
            return SDL_TRUE;
slouken@5530
    92
        }
slouken@5530
    93
    }
slouken@5530
    94
    return SDL_FALSE;
slouken@5530
    95
}
slouken@5530
    96
slouken@6311
    97
#if SDL_VIDEO_DRIVER_X11_XINPUT2
slouken@6311
    98
static void X11_HandleRawMotion(SDL_VideoData *videodata,const XIRawEvent *rawev)
slouken@6311
    99
{
slouken@6311
   100
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@6311
   101
    const double *values = rawev->raw_values;
slouken@6311
   102
    int relative_cords[2] = {0,0};
slouken@6311
   103
    int i;
slouken@6311
   104
slouken@6311
   105
    if (!mouse->relative_mode) {
slouken@6311
   106
        return;
slouken@6311
   107
    }
slouken@6311
   108
   
slouken@6311
   109
    /*2 axis,X-Y*/
slouken@6311
   110
    for (i = 0; i < 2; i++) {
slouken@6311
   111
        if (XIMaskIsSet(rawev->valuators.mask, i)) {
slouken@6311
   112
            const int value = (int) *values;
slouken@6311
   113
            relative_cords[i] = value;
slouken@6311
   114
            values++;
slouken@6311
   115
        }
slouken@6311
   116
    }
slouken@6311
   117
#ifdef DEBUG_MOTION
slouken@6311
   118
    printf("XInput relative motion: %d,%d\n", relative_cords[0],relative_cords[1]);
slouken@6311
   119
#endif
slouken@6311
   120
    SDL_SendMouseMotion(mouse->focus,1,relative_cords[0],relative_cords[1]);
slouken@6311
   121
}
slouken@6311
   122
#endif /* SDL_VIDEO_DRIVER_X11_XINPUT2 */
slouken@6311
   123
slouken@6311
   124
#if SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS
slouken@6311
   125
static void X11_HandleGenericEvent(SDL_VideoData *videodata,XEvent event)
slouken@6311
   126
{
slouken@6311
   127
    XGenericEventCookie *cookie = &event.xcookie;
slouken@6311
   128
    XGetEventData(videodata->display, cookie);
slouken@6311
   129
#if SDL_VIDEO_DRIVER_X11_XINPUT2
slouken@6311
   130
    if(cookie->extension == videodata->xinput_opcode) {
slouken@6311
   131
        switch(cookie->evtype) {
slouken@6311
   132
            case XI_RawMotion: {
slouken@6311
   133
                const XIRawEvent *rawev = (const XIRawEvent*)cookie->data;
slouken@6311
   134
                X11_HandleRawMotion(videodata,rawev);
slouken@6311
   135
                }
slouken@6311
   136
                break;
slouken@6311
   137
slouken@6311
   138
        }
slouken@6311
   139
    }
slouken@6311
   140
#endif
slouken@6311
   141
    XFreeEventData(videodata->display,cookie);
slouken@6311
   142
}
slouken@6311
   143
#endif /* SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS */
slouken@6311
   144
slouken@6311
   145
slouken@6311
   146
slouken@1951
   147
static void
slouken@1951
   148
X11_DispatchEvent(_THIS)
slouken@1951
   149
{
slouken@1951
   150
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@4518
   151
    Display *display = videodata->display;
slouken@1951
   152
    SDL_WindowData *data;
slouken@1951
   153
    XEvent xevent;
slouken@1951
   154
    int i;
slouken@1951
   155
slouken@1951
   156
    SDL_zero(xevent);           /* valgrind fix. --ryan. */
slouken@4518
   157
    XNextEvent(display, &xevent);
slouken@1951
   158
bob@2325
   159
    /* filter events catchs XIM events and sends them to the correct
bob@2325
   160
       handler */
bob@2325
   161
    if (XFilterEvent(&xevent, None) == True) {
bob@2327
   162
#if 0
bob@2328
   163
        printf("Filtered event type = %d display = %d window = %d\n",
bob@2328
   164
               xevent.type, xevent.xany.display, xevent.xany.window);
bob@2325
   165
#endif
bob@2325
   166
        return;
bob@2325
   167
    }
bob@2325
   168
slouken@1951
   169
    /* Send a SDL_SYSWMEVENT if the application wants them */
slouken@4429
   170
    if (SDL_GetEventState(SDL_SYSWMEVENT) == SDL_ENABLE) {
slouken@1951
   171
        SDL_SysWMmsg wmmsg;
slouken@1951
   172
slouken@1951
   173
        SDL_VERSION(&wmmsg.version);
slouken@1951
   174
        wmmsg.subsystem = SDL_SYSWM_X11;
slouken@5056
   175
        wmmsg.msg.x11.event = xevent;
slouken@1951
   176
        SDL_SendSysWMEvent(&wmmsg);
slouken@1951
   177
    }
slouken@1951
   178
slouken@6311
   179
#if SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS
slouken@6311
   180
    if(xevent.type == GenericEvent) {
slouken@6311
   181
        X11_HandleGenericEvent(videodata,xevent);
slouken@6311
   182
        return;
slouken@6311
   183
    }
slouken@6311
   184
#endif
slouken@6311
   185
slouken@1951
   186
    data = NULL;
bob@2324
   187
    if (videodata && videodata->windowlist) {
bob@2324
   188
        for (i = 0; i < videodata->numwindows; ++i) {
bob@2324
   189
            if ((videodata->windowlist[i] != NULL) &&
slouken@3685
   190
                (videodata->windowlist[i]->xwindow == xevent.xany.window)) {
bob@2324
   191
                data = videodata->windowlist[i];
bob@2324
   192
                break;
bob@2324
   193
            }
slouken@1951
   194
        }
slouken@1951
   195
    }
slouken@1951
   196
    if (!data) {
slouken@1951
   197
        return;
slouken@1951
   198
    }
slouken@4518
   199
bob@2327
   200
#if 0
bob@2328
   201
    printf("type = %d display = %d window = %d\n",
bob@2328
   202
           xevent.type, xevent.xany.display, xevent.xany.window);
bob@2327
   203
#endif
slouken@1951
   204
    switch (xevent.type) {
slouken@1951
   205
slouken@1951
   206
        /* Gaining mouse coverage? */
slouken@1951
   207
    case EnterNotify:{
slouken@1951
   208
#ifdef DEBUG_XEVENTS
bob@3195
   209
            printf("EnterNotify! (%d,%d,%d)\n", 
slouken@4558
   210
                   xevent.xcrossing.x,
slouken@4558
   211
                   xevent.xcrossing.y,
bob@3195
   212
                   xevent.xcrossing.mode);
slouken@1951
   213
            if (xevent.xcrossing.mode == NotifyGrab)
slouken@1951
   214
                printf("Mode: NotifyGrab\n");
slouken@1951
   215
            if (xevent.xcrossing.mode == NotifyUngrab)
slouken@1951
   216
                printf("Mode: NotifyUngrab\n");
slouken@1951
   217
#endif
slouken@4465
   218
            SDL_SetMouseFocus(data->window);
slouken@1951
   219
        }
slouken@1951
   220
        break;
slouken@1951
   221
        /* Losing mouse coverage? */
slouken@1951
   222
    case LeaveNotify:{
slouken@1951
   223
#ifdef DEBUG_XEVENTS
bob@3195
   224
            printf("LeaveNotify! (%d,%d,%d)\n", 
slouken@4558
   225
                   xevent.xcrossing.x,
slouken@4558
   226
                   xevent.xcrossing.y,
bob@3195
   227
                   xevent.xcrossing.mode);
slouken@1951
   228
            if (xevent.xcrossing.mode == NotifyGrab)
slouken@1951
   229
                printf("Mode: NotifyGrab\n");
slouken@1951
   230
            if (xevent.xcrossing.mode == NotifyUngrab)
slouken@1951
   231
                printf("Mode: NotifyUngrab\n");
slouken@1951
   232
#endif
slouken@4558
   233
            if (xevent.xcrossing.mode != NotifyGrab &&
slouken@4558
   234
                xevent.xcrossing.mode != NotifyUngrab &&
slouken@4558
   235
                xevent.xcrossing.detail != NotifyInferior) {
slouken@4465
   236
                SDL_SetMouseFocus(NULL);
slouken@1951
   237
            }
slouken@1951
   238
        }
slouken@1951
   239
        break;
slouken@1951
   240
slouken@1951
   241
        /* Gaining input focus? */
slouken@1951
   242
    case FocusIn:{
slouken@1951
   243
#ifdef DEBUG_XEVENTS
slouken@1951
   244
            printf("FocusIn!\n");
slouken@1951
   245
#endif
slouken@4465
   246
            SDL_SetKeyboardFocus(data->window);
slouken@1951
   247
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   248
            if (data->ic) {
slouken@1951
   249
                XSetICFocus(data->ic);
slouken@1951
   250
            }
slouken@1951
   251
#endif
slouken@1951
   252
        }
slouken@1951
   253
        break;
slouken@1951
   254
slouken@1951
   255
        /* Losing input focus? */
slouken@1951
   256
    case FocusOut:{
slouken@1951
   257
#ifdef DEBUG_XEVENTS
slouken@1951
   258
            printf("FocusOut!\n");
slouken@1951
   259
#endif
slouken@4465
   260
            SDL_SetKeyboardFocus(NULL);
slouken@1951
   261
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   262
            if (data->ic) {
slouken@1951
   263
                XUnsetICFocus(data->ic);
slouken@1951
   264
            }
slouken@1951
   265
#endif
slouken@1951
   266
        }
slouken@1951
   267
        break;
slouken@1951
   268
slouken@1951
   269
        /* Generated upon EnterWindow and FocusIn */
slouken@1951
   270
    case KeymapNotify:{
slouken@1951
   271
#ifdef DEBUG_XEVENTS
slouken@1951
   272
            printf("KeymapNotify!\n");
slouken@1951
   273
#endif
slouken@1951
   274
            /* FIXME:
slouken@1951
   275
               X11_SetKeyboardState(SDL_Display, xevent.xkeymap.key_vector);
slouken@1951
   276
             */
slouken@1951
   277
        }
slouken@1951
   278
        break;
slouken@1951
   279
slouken@2305
   280
        /* Has the keyboard layout changed? */
slouken@2305
   281
    case MappingNotify:{
slouken@2305
   282
#ifdef DEBUG_XEVENTS
slouken@2305
   283
            printf("MappingNotify!\n");
slouken@2305
   284
#endif
slouken@2306
   285
            X11_UpdateKeymap(_this);
slouken@2305
   286
        }
slouken@2305
   287
        break;
slouken@2305
   288
slouken@1951
   289
        /* Key press? */
slouken@1951
   290
    case KeyPress:{
slouken@1951
   291
            KeyCode keycode = xevent.xkey.keycode;
bob@2300
   292
            KeySym keysym = NoSymbol;
slouken@2306
   293
            char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];
bob@2325
   294
            Status status = 0;
slouken@1951
   295
slouken@1951
   296
#ifdef DEBUG_XEVENTS
slouken@1951
   297
            printf("KeyPress (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
slouken@1951
   298
#endif
slouken@4465
   299
            SDL_SendKeyboardKey(SDL_PRESSED, videodata->key_layout[keycode]);
slouken@4535
   300
#if 1
icculus@5981
   301
            if (videodata->key_layout[keycode] == SDL_SCANCODE_UNKNOWN) {
slouken@2305
   302
                int min_keycode, max_keycode;
slouken@4518
   303
                XDisplayKeycodes(display, &min_keycode, &max_keycode);
slouken@4518
   304
                keysym = XKeycodeToKeysym(display, keycode, 0);
bob@2299
   305
                fprintf(stderr,
slouken@4556
   306
                        "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
   307
                        keycode, keycode - min_keycode, keysym,
slouken@2305
   308
                        XKeysymToString(keysym));
bob@2299
   309
            }
bob@2295
   310
#endif
bob@2325
   311
            /* */
slouken@2305
   312
            SDL_zero(text);
bob@2325
   313
#ifdef X_HAVE_UTF8_STRING
bob@2325
   314
            if (data->ic) {
bob@2325
   315
                Xutf8LookupString(data->ic, &xevent.xkey, text, sizeof(text),
slouken@2738
   316
                                  &keysym, &status);
bob@2325
   317
            }
bob@2325
   318
#else
slouken@2306
   319
            XLookupString(&xevent.xkey, text, sizeof(text), &keysym, NULL);
bob@2325
   320
#endif
slouken@2305
   321
            if (*text) {
slouken@4465
   322
                SDL_SendKeyboardText(text);
bob@2300
   323
            }
slouken@1951
   324
        }
slouken@1951
   325
        break;
slouken@1951
   326
slouken@1951
   327
        /* Key release? */
slouken@1951
   328
    case KeyRelease:{
slouken@1951
   329
            KeyCode keycode = xevent.xkey.keycode;
slouken@1951
   330
slouken@1951
   331
#ifdef DEBUG_XEVENTS
slouken@1951
   332
            printf("KeyRelease (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
slouken@1951
   333
#endif
slouken@4565
   334
            if (X11_KeyRepeat(display, &xevent)) {
slouken@4565
   335
                /* We're about to get a repeated key down, ignore the key up */
slouken@4565
   336
                break;
slouken@4565
   337
            }
slouken@4465
   338
            SDL_SendKeyboardKey(SDL_RELEASED, videodata->key_layout[keycode]);
slouken@1951
   339
        }
slouken@1951
   340
        break;
slouken@1951
   341
slouken@1951
   342
        /* Have we been iconified? */
slouken@1951
   343
    case UnmapNotify:{
slouken@1951
   344
#ifdef DEBUG_XEVENTS
slouken@1951
   345
            printf("UnmapNotify!\n");
slouken@1951
   346
#endif
slouken@3685
   347
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@3685
   348
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1951
   349
        }
slouken@1951
   350
        break;
slouken@1951
   351
slouken@1951
   352
        /* Have we been restored? */
slouken@1951
   353
    case MapNotify:{
slouken@1951
   354
#ifdef DEBUG_XEVENTS
slouken@1951
   355
            printf("MapNotify!\n");
slouken@1951
   356
#endif
slouken@3685
   357
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@3685
   358
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@1951
   359
        }
slouken@1951
   360
        break;
slouken@1951
   361
slouken@1951
   362
        /* Have we been resized or moved? */
slouken@1951
   363
    case ConfigureNotify:{
slouken@1951
   364
#ifdef DEBUG_XEVENTS
slouken@1951
   365
            printf("ConfigureNotify! (resize: %dx%d)\n",
slouken@1951
   366
                   xevent.xconfigure.width, xevent.xconfigure.height);
slouken@1951
   367
#endif
slouken@3685
   368
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED,
slouken@1951
   369
                                xevent.xconfigure.x, xevent.xconfigure.y);
slouken@3685
   370
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED,
slouken@1951
   371
                                xevent.xconfigure.width,
slouken@1951
   372
                                xevent.xconfigure.height);
slouken@1951
   373
        }
slouken@1951
   374
        break;
slouken@1951
   375
slouken@1951
   376
        /* Have we been requested to quit (or another client message?) */
slouken@1951
   377
    case ClientMessage:{
slouken@1951
   378
            if ((xevent.xclient.format == 32) &&
slouken@1951
   379
                (xevent.xclient.data.l[0] == videodata->WM_DELETE_WINDOW)) {
slouken@1951
   380
slouken@3685
   381
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
slouken@1951
   382
            }
slouken@1951
   383
        }
slouken@1951
   384
        break;
slouken@1951
   385
slouken@1951
   386
        /* Do we need to refresh ourselves? */
slouken@1951
   387
    case Expose:{
slouken@1951
   388
#ifdef DEBUG_XEVENTS
slouken@1951
   389
            printf("Expose (count = %d)\n", xevent.xexpose.count);
slouken@1951
   390
#endif
slouken@3685
   391
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
slouken@1951
   392
        }
slouken@1951
   393
        break;
slouken@1951
   394
slouken@4465
   395
    case MotionNotify:{
slouken@6311
   396
            SDL_Mouse *mouse = SDL_GetMouse();  
slouken@6311
   397
            if(!mouse->relative_mode) {
slouken@4465
   398
#ifdef DEBUG_MOTION
slouken@6311
   399
                printf("X11 motion: %d,%d\n", xevent.xmotion.x, xevent.xmotion.y);
slouken@4465
   400
#endif
slouken@6311
   401
slouken@6311
   402
                SDL_SendMouseMotion(data->window, 0, xevent.xmotion.x, xevent.xmotion.y);
slouken@6311
   403
            }
slouken@4465
   404
        }
slouken@4465
   405
        break;
slouken@4465
   406
slouken@4465
   407
    case ButtonPress:{
slouken@5530
   408
            int ticks = 0;
slouken@5530
   409
            if (X11_IsWheelEvent(display,&xevent,&ticks) == SDL_TRUE) {
slouken@5530
   410
                SDL_SendMouseWheel(data->window, 0, ticks);
slouken@5530
   411
            }
slouken@5530
   412
            else {
slouken@5530
   413
                SDL_SendMouseButton(data->window, SDL_PRESSED, xevent.xbutton.button);
slouken@5530
   414
            }
slouken@4465
   415
        }
slouken@4465
   416
        break;
slouken@4465
   417
slouken@4465
   418
    case ButtonRelease:{
slouken@4484
   419
            SDL_SendMouseButton(data->window, SDL_RELEASED, xevent.xbutton.button);
slouken@4465
   420
        }
slouken@4465
   421
        break;
slouken@4465
   422
slouken@4518
   423
    case PropertyNotify:{
slouken@4518
   424
#ifdef DEBUG_XEVENTS
slouken@4520
   425
            unsigned char *propdata;
slouken@4520
   426
            int status, real_format;
slouken@4520
   427
            Atom real_type;
slouken@4520
   428
            unsigned long items_read, items_left, i;
slouken@4520
   429
slouken@4518
   430
            char *name = XGetAtomName(display, xevent.xproperty.atom);
slouken@4518
   431
            if (name) {
slouken@4556
   432
                printf("PropertyNotify: %s %s\n", name, (xevent.xproperty.state == PropertyDelete) ? "deleted" : "changed");
slouken@4518
   433
                XFree(name);
slouken@4518
   434
            }
slouken@4518
   435
slouken@4520
   436
            status = XGetWindowProperty(display, data->xwindow, xevent.xproperty.atom, 0L, 8192L, False, AnyPropertyType, &real_type, &real_format, &items_read, &items_left, &propdata);
slouken@4556
   437
            if (status == Success && items_read > 0) {
slouken@4520
   438
                if (real_type == XA_INTEGER) {
slouken@4520
   439
                    int *values = (int *)propdata;
slouken@4520
   440
slouken@4520
   441
                    printf("{");
slouken@4520
   442
                    for (i = 0; i < items_read; i++) {
slouken@4520
   443
                        printf(" %d", values[i]);
slouken@4520
   444
                    }
slouken@4520
   445
                    printf(" }\n");
slouken@4520
   446
                } else if (real_type == XA_CARDINAL) {
slouken@4520
   447
                    if (real_format == 32) {
slouken@4520
   448
                        Uint32 *values = (Uint32 *)propdata;
slouken@4520
   449
slouken@4520
   450
                        printf("{");
slouken@4520
   451
                        for (i = 0; i < items_read; i++) {
slouken@4520
   452
                            printf(" %d", values[i]);
slouken@4520
   453
                        }
slouken@4520
   454
                        printf(" }\n");
slouken@4520
   455
                    } else if (real_format == 16) {
slouken@4520
   456
                        Uint16 *values = (Uint16 *)propdata;
slouken@4520
   457
slouken@4520
   458
                        printf("{");
slouken@4520
   459
                        for (i = 0; i < items_read; i++) {
slouken@4520
   460
                            printf(" %d", values[i]);
slouken@4520
   461
                        }
slouken@4520
   462
                        printf(" }\n");
slouken@4520
   463
                    } else if (real_format == 8) {
slouken@4520
   464
                        Uint8 *values = (Uint8 *)propdata;
slouken@4520
   465
slouken@4520
   466
                        printf("{");
slouken@4520
   467
                        for (i = 0; i < items_read; i++) {
slouken@4520
   468
                            printf(" %d", values[i]);
slouken@4520
   469
                        }
slouken@4520
   470
                        printf(" }\n");
slouken@4520
   471
                    }
slouken@4520
   472
                } else if (real_type == XA_STRING ||
slouken@4520
   473
                           real_type == videodata->UTF8_STRING) {
slouken@4520
   474
                    printf("{ \"%s\" }\n", propdata);
slouken@4520
   475
                } else if (real_type == XA_ATOM) {
slouken@4518
   476
                    Atom *atoms = (Atom *)propdata;
slouken@4520
   477
slouken@4520
   478
                    printf("{");
slouken@4518
   479
                    for (i = 0; i < items_read; i++) {
slouken@4520
   480
                        char *name = XGetAtomName(display, atoms[i]);
slouken@4520
   481
                        if (name) {
slouken@4520
   482
                            printf(" %s", name);
slouken@4520
   483
                            XFree(name);
slouken@4518
   484
                        }
slouken@4518
   485
                    }
slouken@4520
   486
                    printf(" }\n");
slouken@4520
   487
                } else {
slouken@4520
   488
                    char *name = XGetAtomName(display, real_type);
slouken@4520
   489
                    printf("Unknown type: %ld (%s)\n", real_type, name ? name : "UNKNOWN");
slouken@4520
   490
                    if (name) {
slouken@4520
   491
                        XFree(name);
slouken@4520
   492
                    }
slouken@4518
   493
                }
slouken@4520
   494
            }
slouken@4518
   495
#endif
slouken@4518
   496
        }
slouken@4518
   497
        break;
slouken@4518
   498
slouken@4508
   499
    /* Copy the selection from XA_CUT_BUFFER0 to the requested property */
slouken@4508
   500
    case SelectionRequest: {
slouken@4508
   501
            XSelectionRequestEvent *req;
slouken@4508
   502
            XEvent sevent;
slouken@4508
   503
            int seln_format;
slouken@4508
   504
            unsigned long nbytes;
slouken@4508
   505
            unsigned long overflow;
slouken@4508
   506
            unsigned char *seln_data;
slouken@4508
   507
slouken@4508
   508
            req = &xevent.xselectionrequest;
slouken@4508
   509
#ifdef DEBUG_XEVENTS
slouken@4508
   510
            printf("SelectionRequest (requestor = %ld, target = %ld)\n",
slouken@4508
   511
                req->requestor, req->target);
slouken@4508
   512
#endif
slouken@4508
   513
slouken@4524
   514
            SDL_zero(sevent);
slouken@4520
   515
            sevent.xany.type = SelectionNotify;
slouken@4508
   516
            sevent.xselection.selection = req->selection;
slouken@4508
   517
            sevent.xselection.target = None;
slouken@4508
   518
            sevent.xselection.property = None;
slouken@4508
   519
            sevent.xselection.requestor = req->requestor;
slouken@4508
   520
            sevent.xselection.time = req->time;
slouken@4508
   521
            if (XGetWindowProperty(display, DefaultRootWindow(display),
slouken@4508
   522
                    XA_CUT_BUFFER0, 0, INT_MAX/4, False, req->target,
slouken@4508
   523
                    &sevent.xselection.target, &seln_format, &nbytes,
slouken@4508
   524
                    &overflow, &seln_data) == Success) {
slouken@4508
   525
                if (sevent.xselection.target == req->target) {
slouken@4508
   526
                    XChangeProperty(display, req->requestor, req->property,
slouken@4508
   527
                        sevent.xselection.target, seln_format, PropModeReplace,
slouken@4508
   528
                        seln_data, nbytes);
slouken@4508
   529
                    sevent.xselection.property = req->property;
slouken@4508
   530
                }
slouken@4508
   531
                XFree(seln_data);
slouken@4508
   532
            }
slouken@4508
   533
            XSendEvent(display, req->requestor, False, 0, &sevent);
slouken@4508
   534
            XSync(display, False);
slouken@4508
   535
        }
slouken@4508
   536
        break;
slouken@4508
   537
slouken@4508
   538
    case SelectionNotify: {
slouken@4508
   539
#ifdef DEBUG_XEVENTS
slouken@4508
   540
            printf("SelectionNotify (requestor = %ld, target = %ld)\n",
slouken@4508
   541
                xevent.xselection.requestor, xevent.xselection.target);
slouken@4508
   542
#endif
slouken@4508
   543
            videodata->selection_waiting = SDL_FALSE;
slouken@4508
   544
        }
slouken@4508
   545
        break;
slouken@4508
   546
slouken@1951
   547
    default:{
slouken@1951
   548
#ifdef DEBUG_XEVENTS
slouken@2940
   549
            printf("Unhandled event %d\n", xevent.type);
slouken@1951
   550
#endif
slouken@1951
   551
        }
slouken@1951
   552
        break;
slouken@1951
   553
    }
slouken@1951
   554
}
slouken@1951
   555
slouken@1951
   556
/* Ack!  XPending() actually performs a blocking read if no events available */
slouken@4472
   557
static int
slouken@1951
   558
X11_Pending(Display * display)
slouken@1951
   559
{
slouken@1951
   560
    /* Flush the display connection and look to see if events are queued */
slouken@1951
   561
    XFlush(display);
slouken@1951
   562
    if (XEventsQueued(display, QueuedAlready)) {
slouken@1951
   563
        return (1);
slouken@1951
   564
    }
slouken@1951
   565
slouken@1951
   566
    /* More drastic measures are required -- see if X is ready to talk */
slouken@1951
   567
    {
slouken@1951
   568
        static struct timeval zero_time;        /* static == 0 */
slouken@1951
   569
        int x11_fd;
slouken@1951
   570
        fd_set fdset;
slouken@1951
   571
slouken@1951
   572
        x11_fd = ConnectionNumber(display);
slouken@1951
   573
        FD_ZERO(&fdset);
slouken@1951
   574
        FD_SET(x11_fd, &fdset);
slouken@1951
   575
        if (select(x11_fd + 1, &fdset, NULL, NULL, &zero_time) == 1) {
slouken@1951
   576
            return (XPending(display));
slouken@1951
   577
        }
slouken@1951
   578
    }
slouken@1951
   579
slouken@1951
   580
    /* Oh well, nothing is ready .. */
slouken@1951
   581
    return (0);
slouken@1951
   582
}
slouken@1951
   583
icculus@5981
   584
icculus@5981
   585
/* !!! FIXME: this should be exposed in a header, or something. */
icculus@5981
   586
int SDL_GetNumTouch(void);
icculus@5981
   587
icculus@5981
   588
slouken@1951
   589
void
slouken@1951
   590
X11_PumpEvents(_THIS)
slouken@1951
   591
{
slouken@1951
   592
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   593
slouken@3025
   594
    /* Update activity every 30 seconds to prevent screensaver */
slouken@3025
   595
    if (_this->suspend_screensaver) {
slouken@3025
   596
        Uint32 now = SDL_GetTicks();
slouken@3025
   597
        if (!data->screensaver_activity ||
slouken@3040
   598
            (int) (now - data->screensaver_activity) >= 30000) {
slouken@3025
   599
            XResetScreenSaver(data->display);
slouken@3025
   600
            data->screensaver_activity = now;
slouken@3025
   601
        }
slouken@3025
   602
    }
slouken@3025
   603
slouken@1951
   604
    /* Keep processing pending events */
slouken@1951
   605
    while (X11_Pending(data->display)) {
slouken@1951
   606
        X11_DispatchEvent(_this);
slouken@1951
   607
    }
jim@4640
   608
jim@4681
   609
#ifdef SDL_INPUT_LINUXEV
jim@4642
   610
    /* Process Touch events - TODO When X gets touch support, use that instead*/
jim@4642
   611
    int i = 0,rd;
jim@4642
   612
    struct input_event ev[64];
jim@4642
   613
    int size = sizeof (struct input_event);
jim@4645
   614
icculus@5603
   615
/* !!! FIXME: clean the tabstops out of here. */
jim@4642
   616
    for(i = 0;i < SDL_GetNumTouch();++i) {
jim@4642
   617
	SDL_Touch* touch = SDL_GetTouchIndex(i);
jim@4642
   618
	if(!touch) printf("Touch %i/%i DNE\n",i,SDL_GetNumTouch());
jim@4642
   619
	EventTouchData* data;
jim@4645
   620
	data = (EventTouchData*)(touch->driverdata);
jim@4645
   621
	if(data == NULL) {
jim@4645
   622
	  printf("No driver data\n");
jim@4645
   623
	  continue;
jim@4642
   624
	}
jim@4642
   625
	if(data->eventStream <= 0) 
jim@4642
   626
	    printf("Error: Couldn't open stream\n");
jim@4642
   627
	rd = read(data->eventStream, ev, size * 64);
jim@4642
   628
	if(rd >= size) {
jim@4642
   629
	    for (i = 0; i < rd / sizeof(struct input_event); i++) {
jim@4642
   630
		switch (ev[i].type) {
jim@4642
   631
		case EV_ABS:
jim@4644
   632
		    switch (ev[i].code) {
jim@4644
   633
			case ABS_X:
jim@4644
   634
			    data->x = ev[i].value;
jim@4644
   635
			    break;
jim@4644
   636
			case ABS_Y:
jim@4644
   637
			    data->y = ev[i].value;
jim@4644
   638
			    break;
jim@4644
   639
			case ABS_PRESSURE:
jim@4644
   640
			    data->pressure = ev[i].value;
jim@4646
   641
			    if(data->pressure < 0) data->pressure = 0;
jim@4644
   642
			    break;
jim@4644
   643
			case ABS_MISC:
jim@4683
   644
			    if(ev[i].value == 0)
jim@4683
   645
			        data->up = SDL_TRUE;			    
jim@4644
   646
			    break;
jim@4644
   647
			}
jim@4642
   648
		    break;
jim@4642
   649
		case EV_MSC:
icculus@5603
   650
			if(ev[i].code == MSC_SERIAL)
icculus@5603
   651
				data->finger = ev[i].value;
icculus@5603
   652
			break;
icculus@5603
   653
		case EV_KEY:
icculus@5603
   654
			if(ev[i].code == BTN_TOUCH)
icculus@5603
   655
			    if(ev[i].value == 0)
icculus@5603
   656
			        data->up = SDL_TRUE;
icculus@5603
   657
			break;
jim@4642
   658
		case EV_SYN:
icculus@5603
   659
		  if(!data->down) {
icculus@5603
   660
		      data->down = SDL_TRUE;
jim@4655
   661
		      SDL_SendFingerDown(touch->id,data->finger,
icculus@5603
   662
		    		  data->down, data->x, data->y,
icculus@5603
   663
		    		  data->pressure);
jim@4655
   664
		  }
icculus@5603
   665
		  else if(!data->up)
jim@4683
   666
		    SDL_SendTouchMotion(touch->id,data->finger, 
icculus@5603
   667
					SDL_FALSE, data->x,data->y,
jim@4683
   668
					data->pressure);
icculus@5603
   669
		  else
icculus@5603
   670
		  {
icculus@5603
   671
		      data->down = SDL_FALSE;
icculus@5603
   672
			  SDL_SendFingerDown(touch->id,data->finger,
icculus@5603
   673
					  data->down, data->x,data->y,
icculus@5603
   674
					  data->pressure);
icculus@5603
   675
			  data->x = -1;
icculus@5603
   676
			  data->y = -1;
icculus@5603
   677
			  data->pressure = -1;
icculus@5603
   678
			  data->finger = 0;
icculus@5603
   679
			  data->up = SDL_FALSE;
icculus@5603
   680
		  }
jim@4683
   681
		  break;		
jim@4642
   682
		}
jim@4642
   683
	    }
jim@4642
   684
	}
jim@4642
   685
    }
jimtla@4669
   686
#endif
slouken@1951
   687
}
slouken@1951
   688
slouken@3030
   689
/* This is so wrong it hurts */
slouken@3030
   690
#define GNOME_SCREENSAVER_HACK
slouken@3030
   691
#ifdef GNOME_SCREENSAVER_HACK
slouken@3030
   692
#include <unistd.h>
slouken@3030
   693
static pid_t screensaver_inhibit_pid;
slouken@3040
   694
static void
slouken@3040
   695
gnome_screensaver_disable()
slouken@3030
   696
{
slouken@3030
   697
    screensaver_inhibit_pid = fork();
slouken@3030
   698
    if (screensaver_inhibit_pid == 0) {
slouken@3031
   699
        close(0);
slouken@3031
   700
        close(1);
slouken@3031
   701
        close(2);
slouken@3030
   702
        execl("/usr/bin/gnome-screensaver-command",
slouken@3030
   703
              "gnome-screensaver-command",
slouken@3030
   704
              "--inhibit",
slouken@3030
   705
              "--reason",
slouken@3040
   706
              "GNOME screensaver doesn't respect MIT-SCREEN-SAVER", NULL);
slouken@3030
   707
        exit(2);
slouken@3030
   708
    }
slouken@3030
   709
}
slouken@3040
   710
static void
slouken@3040
   711
gnome_screensaver_enable()
slouken@3030
   712
{
slouken@3030
   713
    kill(screensaver_inhibit_pid, 15);
slouken@3030
   714
}
slouken@3030
   715
#endif
slouken@3030
   716
slouken@1951
   717
void
slouken@3025
   718
X11_SuspendScreenSaver(_THIS)
slouken@1951
   719
{
slouken@5408
   720
#if SDL_VIDEO_DRIVER_X11_XSCRNSAVER
slouken@3025
   721
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@3025
   722
    int dummy;
slouken@3025
   723
    int major_version, minor_version;
slouken@1951
   724
slouken@3030
   725
    if (SDL_X11_HAVE_XSS) {
slouken@3030
   726
        /* XScreenSaverSuspend was introduced in MIT-SCREEN-SAVER 1.1 */
slouken@3030
   727
        if (!XScreenSaverQueryExtension(data->display, &dummy, &dummy) ||
slouken@3030
   728
            !XScreenSaverQueryVersion(data->display,
slouken@3030
   729
                                      &major_version, &minor_version) ||
slouken@3030
   730
            major_version < 1 || (major_version == 1 && minor_version < 1)) {
slouken@3030
   731
            return;
slouken@3030
   732
        }
slouken@3030
   733
slouken@3030
   734
        XScreenSaverSuspend(data->display, _this->suspend_screensaver);
slouken@3030
   735
        XResetScreenSaver(data->display);
slouken@1951
   736
    }
slouken@3030
   737
#endif
slouken@1951
   738
slouken@3030
   739
#ifdef GNOME_SCREENSAVER_HACK
slouken@3030
   740
    if (_this->suspend_screensaver) {
slouken@3030
   741
        gnome_screensaver_disable();
slouken@3030
   742
    } else {
slouken@3030
   743
        gnome_screensaver_enable();
slouken@3025
   744
    }
slouken@3025
   745
#endif
slouken@1951
   746
}
slouken@1951
   747
slouken@5481
   748
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
   749
slouken@1951
   750
/* vi: set ts=4 sw=4 expandtab: */