src/video/x11/SDL_x11events.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 05 Apr 2011 09:35:56 -0700
changeset 5530 4e46a7b6773d
parent 5481 22dfc3958dc3
child 5535 96594ac5fd1a
permissions -rw-r--r--
Fixed bug 1173 (No mouse wheel event on linux/x11)

Matthias 2011-03-20 23:07:02 PDT

On X11, SDL 1.3 does not generate a mouse wheel event. Instead, button
down/buttton up events are generated by SDL. After looking at the code in
SDL_x11events.c, I assume this is due to the fact that X11 does not have a
dedicated mouse wheel event.

I did a little research on the behavior of mouse wheel events on X11 systems.
Apparently, mouse wheel events generate a button down/button up event with the
same time, i.e. with exact same timestamp.

Attached you can find my changes to SDL_x11events.c, which generates SDL mouse
wheel events for those button down events that have a button release event
immediately following it (for the same button, and with the same timestamp).

I did have to make an assumption: As standard X11 implementations know only 5
buttons, I have mapped Button4 to "wheel up" (i.e. +1), and Button5 to "wheel
down" (i.e. -1).

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