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