src/video/x11/SDL_x11events.c
author Dimitris Zenios <dimitris.zenios@gmail.com>
Thu, 31 May 2012 13:37:02 +0300
changeset 6316 a89c79a3f0cd
parent 6311 4e5e46f43438
child 6318 49b2cb56db6e
permissions -rwxr-xr-x
1.Moved all xinput2 functionality to its own file
2.Implement touch events using Xinput2.Leave evtouch as a fallback when xinput2 is not supported
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);
slouken@4518
   268
                keysym = XKeycodeToKeysym(display, keycode, 0);
bob@2299
   269
                fprintf(stderr,
slouken@4556
   270
                        "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
   271
                        keycode, keycode - min_keycode, keysym,
slouken@2305
   272
                        XKeysymToString(keysym));
bob@2299
   273
            }
bob@2295
   274
#endif
bob@2325
   275
            /* */
slouken@2305
   276
            SDL_zero(text);
bob@2325
   277
#ifdef X_HAVE_UTF8_STRING
bob@2325
   278
            if (data->ic) {
bob@2325
   279
                Xutf8LookupString(data->ic, &xevent.xkey, text, sizeof(text),
slouken@2738
   280
                                  &keysym, &status);
bob@2325
   281
            }
bob@2325
   282
#else
slouken@2306
   283
            XLookupString(&xevent.xkey, text, sizeof(text), &keysym, NULL);
bob@2325
   284
#endif
slouken@2305
   285
            if (*text) {
slouken@4465
   286
                SDL_SendKeyboardText(text);
bob@2300
   287
            }
slouken@1951
   288
        }
slouken@1951
   289
        break;
slouken@1951
   290
slouken@1951
   291
        /* Key release? */
slouken@1951
   292
    case KeyRelease:{
slouken@1951
   293
            KeyCode keycode = xevent.xkey.keycode;
slouken@1951
   294
slouken@1951
   295
#ifdef DEBUG_XEVENTS
slouken@1951
   296
            printf("KeyRelease (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
slouken@1951
   297
#endif
slouken@4565
   298
            if (X11_KeyRepeat(display, &xevent)) {
slouken@4565
   299
                /* We're about to get a repeated key down, ignore the key up */
slouken@4565
   300
                break;
slouken@4565
   301
            }
slouken@4465
   302
            SDL_SendKeyboardKey(SDL_RELEASED, videodata->key_layout[keycode]);
slouken@1951
   303
        }
slouken@1951
   304
        break;
slouken@1951
   305
slouken@1951
   306
        /* Have we been iconified? */
slouken@1951
   307
    case UnmapNotify:{
slouken@1951
   308
#ifdef DEBUG_XEVENTS
slouken@1951
   309
            printf("UnmapNotify!\n");
slouken@1951
   310
#endif
slouken@3685
   311
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@3685
   312
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1951
   313
        }
slouken@1951
   314
        break;
slouken@1951
   315
slouken@1951
   316
        /* Have we been restored? */
slouken@1951
   317
    case MapNotify:{
slouken@1951
   318
#ifdef DEBUG_XEVENTS
slouken@1951
   319
            printf("MapNotify!\n");
slouken@1951
   320
#endif
slouken@3685
   321
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@3685
   322
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@1951
   323
        }
slouken@1951
   324
        break;
slouken@1951
   325
slouken@1951
   326
        /* Have we been resized or moved? */
slouken@1951
   327
    case ConfigureNotify:{
slouken@1951
   328
#ifdef DEBUG_XEVENTS
slouken@1951
   329
            printf("ConfigureNotify! (resize: %dx%d)\n",
slouken@1951
   330
                   xevent.xconfigure.width, xevent.xconfigure.height);
slouken@1951
   331
#endif
slouken@3685
   332
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED,
slouken@1951
   333
                                xevent.xconfigure.x, xevent.xconfigure.y);
slouken@3685
   334
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED,
slouken@1951
   335
                                xevent.xconfigure.width,
slouken@1951
   336
                                xevent.xconfigure.height);
slouken@1951
   337
        }
slouken@1951
   338
        break;
slouken@1951
   339
slouken@1951
   340
        /* Have we been requested to quit (or another client message?) */
slouken@1951
   341
    case ClientMessage:{
slouken@1951
   342
            if ((xevent.xclient.format == 32) &&
slouken@1951
   343
                (xevent.xclient.data.l[0] == videodata->WM_DELETE_WINDOW)) {
slouken@1951
   344
slouken@3685
   345
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
slouken@1951
   346
            }
slouken@1951
   347
        }
slouken@1951
   348
        break;
slouken@1951
   349
slouken@1951
   350
        /* Do we need to refresh ourselves? */
slouken@1951
   351
    case Expose:{
slouken@1951
   352
#ifdef DEBUG_XEVENTS
slouken@1951
   353
            printf("Expose (count = %d)\n", xevent.xexpose.count);
slouken@1951
   354
#endif
slouken@3685
   355
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
slouken@1951
   356
        }
slouken@1951
   357
        break;
slouken@1951
   358
slouken@4465
   359
    case MotionNotify:{
slouken@6311
   360
            SDL_Mouse *mouse = SDL_GetMouse();  
slouken@6311
   361
            if(!mouse->relative_mode) {
slouken@4465
   362
#ifdef DEBUG_MOTION
slouken@6311
   363
                printf("X11 motion: %d,%d\n", xevent.xmotion.x, xevent.xmotion.y);
slouken@4465
   364
#endif
slouken@6311
   365
slouken@6311
   366
                SDL_SendMouseMotion(data->window, 0, xevent.xmotion.x, xevent.xmotion.y);
slouken@6311
   367
            }
slouken@4465
   368
        }
slouken@4465
   369
        break;
slouken@4465
   370
slouken@4465
   371
    case ButtonPress:{
slouken@5530
   372
            int ticks = 0;
slouken@5530
   373
            if (X11_IsWheelEvent(display,&xevent,&ticks) == SDL_TRUE) {
slouken@5530
   374
                SDL_SendMouseWheel(data->window, 0, ticks);
slouken@5530
   375
            }
slouken@5530
   376
            else {
slouken@5530
   377
                SDL_SendMouseButton(data->window, SDL_PRESSED, xevent.xbutton.button);
slouken@5530
   378
            }
slouken@4465
   379
        }
slouken@4465
   380
        break;
slouken@4465
   381
slouken@4465
   382
    case ButtonRelease:{
slouken@4484
   383
            SDL_SendMouseButton(data->window, SDL_RELEASED, xevent.xbutton.button);
slouken@4465
   384
        }
slouken@4465
   385
        break;
slouken@4465
   386
slouken@4518
   387
    case PropertyNotify:{
slouken@4518
   388
#ifdef DEBUG_XEVENTS
slouken@4520
   389
            unsigned char *propdata;
slouken@4520
   390
            int status, real_format;
slouken@4520
   391
            Atom real_type;
slouken@4520
   392
            unsigned long items_read, items_left, i;
slouken@4520
   393
slouken@4518
   394
            char *name = XGetAtomName(display, xevent.xproperty.atom);
slouken@4518
   395
            if (name) {
slouken@4556
   396
                printf("PropertyNotify: %s %s\n", name, (xevent.xproperty.state == PropertyDelete) ? "deleted" : "changed");
slouken@4518
   397
                XFree(name);
slouken@4518
   398
            }
slouken@4518
   399
slouken@4520
   400
            status = XGetWindowProperty(display, data->xwindow, xevent.xproperty.atom, 0L, 8192L, False, AnyPropertyType, &real_type, &real_format, &items_read, &items_left, &propdata);
slouken@4556
   401
            if (status == Success && items_read > 0) {
slouken@4520
   402
                if (real_type == XA_INTEGER) {
slouken@4520
   403
                    int *values = (int *)propdata;
slouken@4520
   404
slouken@4520
   405
                    printf("{");
slouken@4520
   406
                    for (i = 0; i < items_read; i++) {
slouken@4520
   407
                        printf(" %d", values[i]);
slouken@4520
   408
                    }
slouken@4520
   409
                    printf(" }\n");
slouken@4520
   410
                } else if (real_type == XA_CARDINAL) {
slouken@4520
   411
                    if (real_format == 32) {
slouken@4520
   412
                        Uint32 *values = (Uint32 *)propdata;
slouken@4520
   413
slouken@4520
   414
                        printf("{");
slouken@4520
   415
                        for (i = 0; i < items_read; i++) {
slouken@4520
   416
                            printf(" %d", values[i]);
slouken@4520
   417
                        }
slouken@4520
   418
                        printf(" }\n");
slouken@4520
   419
                    } else if (real_format == 16) {
slouken@4520
   420
                        Uint16 *values = (Uint16 *)propdata;
slouken@4520
   421
slouken@4520
   422
                        printf("{");
slouken@4520
   423
                        for (i = 0; i < items_read; i++) {
slouken@4520
   424
                            printf(" %d", values[i]);
slouken@4520
   425
                        }
slouken@4520
   426
                        printf(" }\n");
slouken@4520
   427
                    } else if (real_format == 8) {
slouken@4520
   428
                        Uint8 *values = (Uint8 *)propdata;
slouken@4520
   429
slouken@4520
   430
                        printf("{");
slouken@4520
   431
                        for (i = 0; i < items_read; i++) {
slouken@4520
   432
                            printf(" %d", values[i]);
slouken@4520
   433
                        }
slouken@4520
   434
                        printf(" }\n");
slouken@4520
   435
                    }
slouken@4520
   436
                } else if (real_type == XA_STRING ||
slouken@4520
   437
                           real_type == videodata->UTF8_STRING) {
slouken@4520
   438
                    printf("{ \"%s\" }\n", propdata);
slouken@4520
   439
                } else if (real_type == XA_ATOM) {
slouken@4518
   440
                    Atom *atoms = (Atom *)propdata;
slouken@4520
   441
slouken@4520
   442
                    printf("{");
slouken@4518
   443
                    for (i = 0; i < items_read; i++) {
slouken@4520
   444
                        char *name = XGetAtomName(display, atoms[i]);
slouken@4520
   445
                        if (name) {
slouken@4520
   446
                            printf(" %s", name);
slouken@4520
   447
                            XFree(name);
slouken@4518
   448
                        }
slouken@4518
   449
                    }
slouken@4520
   450
                    printf(" }\n");
slouken@4520
   451
                } else {
slouken@4520
   452
                    char *name = XGetAtomName(display, real_type);
slouken@4520
   453
                    printf("Unknown type: %ld (%s)\n", real_type, name ? name : "UNKNOWN");
slouken@4520
   454
                    if (name) {
slouken@4520
   455
                        XFree(name);
slouken@4520
   456
                    }
slouken@4518
   457
                }
slouken@4520
   458
            }
slouken@4518
   459
#endif
slouken@4518
   460
        }
slouken@4518
   461
        break;
slouken@4518
   462
slouken@4508
   463
    /* Copy the selection from XA_CUT_BUFFER0 to the requested property */
slouken@4508
   464
    case SelectionRequest: {
slouken@4508
   465
            XSelectionRequestEvent *req;
slouken@4508
   466
            XEvent sevent;
slouken@4508
   467
            int seln_format;
slouken@4508
   468
            unsigned long nbytes;
slouken@4508
   469
            unsigned long overflow;
slouken@4508
   470
            unsigned char *seln_data;
slouken@4508
   471
slouken@4508
   472
            req = &xevent.xselectionrequest;
slouken@4508
   473
#ifdef DEBUG_XEVENTS
slouken@4508
   474
            printf("SelectionRequest (requestor = %ld, target = %ld)\n",
slouken@4508
   475
                req->requestor, req->target);
slouken@4508
   476
#endif
slouken@4508
   477
slouken@4524
   478
            SDL_zero(sevent);
slouken@4520
   479
            sevent.xany.type = SelectionNotify;
slouken@4508
   480
            sevent.xselection.selection = req->selection;
slouken@4508
   481
            sevent.xselection.target = None;
slouken@4508
   482
            sevent.xselection.property = None;
slouken@4508
   483
            sevent.xselection.requestor = req->requestor;
slouken@4508
   484
            sevent.xselection.time = req->time;
slouken@4508
   485
            if (XGetWindowProperty(display, DefaultRootWindow(display),
slouken@4508
   486
                    XA_CUT_BUFFER0, 0, INT_MAX/4, False, req->target,
slouken@4508
   487
                    &sevent.xselection.target, &seln_format, &nbytes,
slouken@4508
   488
                    &overflow, &seln_data) == Success) {
slouken@4508
   489
                if (sevent.xselection.target == req->target) {
slouken@4508
   490
                    XChangeProperty(display, req->requestor, req->property,
slouken@4508
   491
                        sevent.xselection.target, seln_format, PropModeReplace,
slouken@4508
   492
                        seln_data, nbytes);
slouken@4508
   493
                    sevent.xselection.property = req->property;
slouken@4508
   494
                }
slouken@4508
   495
                XFree(seln_data);
slouken@4508
   496
            }
slouken@4508
   497
            XSendEvent(display, req->requestor, False, 0, &sevent);
slouken@4508
   498
            XSync(display, False);
slouken@4508
   499
        }
slouken@4508
   500
        break;
slouken@4508
   501
slouken@4508
   502
    case SelectionNotify: {
slouken@4508
   503
#ifdef DEBUG_XEVENTS
slouken@4508
   504
            printf("SelectionNotify (requestor = %ld, target = %ld)\n",
slouken@4508
   505
                xevent.xselection.requestor, xevent.xselection.target);
slouken@4508
   506
#endif
slouken@4508
   507
            videodata->selection_waiting = SDL_FALSE;
slouken@4508
   508
        }
slouken@4508
   509
        break;
slouken@4508
   510
slouken@1951
   511
    default:{
slouken@1951
   512
#ifdef DEBUG_XEVENTS
slouken@2940
   513
            printf("Unhandled event %d\n", xevent.type);
slouken@1951
   514
#endif
slouken@1951
   515
        }
slouken@1951
   516
        break;
slouken@1951
   517
    }
slouken@1951
   518
}
slouken@1951
   519
slouken@1951
   520
/* Ack!  XPending() actually performs a blocking read if no events available */
slouken@4472
   521
static int
slouken@1951
   522
X11_Pending(Display * display)
slouken@1951
   523
{
slouken@1951
   524
    /* Flush the display connection and look to see if events are queued */
slouken@1951
   525
    XFlush(display);
slouken@1951
   526
    if (XEventsQueued(display, QueuedAlready)) {
slouken@1951
   527
        return (1);
slouken@1951
   528
    }
slouken@1951
   529
slouken@1951
   530
    /* More drastic measures are required -- see if X is ready to talk */
slouken@1951
   531
    {
slouken@1951
   532
        static struct timeval zero_time;        /* static == 0 */
slouken@1951
   533
        int x11_fd;
slouken@1951
   534
        fd_set fdset;
slouken@1951
   535
slouken@1951
   536
        x11_fd = ConnectionNumber(display);
slouken@1951
   537
        FD_ZERO(&fdset);
slouken@1951
   538
        FD_SET(x11_fd, &fdset);
slouken@1951
   539
        if (select(x11_fd + 1, &fdset, NULL, NULL, &zero_time) == 1) {
slouken@1951
   540
            return (XPending(display));
slouken@1951
   541
        }
slouken@1951
   542
    }
slouken@1951
   543
slouken@1951
   544
    /* Oh well, nothing is ready .. */
slouken@1951
   545
    return (0);
slouken@1951
   546
}
slouken@1951
   547
icculus@5981
   548
icculus@5981
   549
/* !!! FIXME: this should be exposed in a header, or something. */
icculus@5981
   550
int SDL_GetNumTouch(void);
icculus@5981
   551
icculus@5981
   552
slouken@1951
   553
void
slouken@1951
   554
X11_PumpEvents(_THIS)
slouken@1951
   555
{
slouken@1951
   556
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   557
slouken@3025
   558
    /* Update activity every 30 seconds to prevent screensaver */
slouken@3025
   559
    if (_this->suspend_screensaver) {
slouken@3025
   560
        Uint32 now = SDL_GetTicks();
slouken@3025
   561
        if (!data->screensaver_activity ||
slouken@3040
   562
            (int) (now - data->screensaver_activity) >= 30000) {
slouken@3025
   563
            XResetScreenSaver(data->display);
slouken@3025
   564
            data->screensaver_activity = now;
slouken@3025
   565
        }
slouken@3025
   566
    }
slouken@3025
   567
slouken@1951
   568
    /* Keep processing pending events */
slouken@1951
   569
    while (X11_Pending(data->display)) {
slouken@1951
   570
        X11_DispatchEvent(_this);
slouken@1951
   571
    }
dimitris@6316
   572
    /*Dont process evtouch events if XInput2 multitouch is supported*/
dimitris@6316
   573
    if(X11_Xinput2IsMutitouchSupported()) {
dimitris@6316
   574
        return;
dimitris@6316
   575
    }
jim@4640
   576
jim@4681
   577
#ifdef SDL_INPUT_LINUXEV
dimitris@6316
   578
    /* Process Touch events*/
jim@4642
   579
    int i = 0,rd;
jim@4642
   580
    struct input_event ev[64];
jim@4642
   581
    int size = sizeof (struct input_event);
jim@4645
   582
icculus@5603
   583
/* !!! FIXME: clean the tabstops out of here. */
jim@4642
   584
    for(i = 0;i < SDL_GetNumTouch();++i) {
jim@4642
   585
	SDL_Touch* touch = SDL_GetTouchIndex(i);
jim@4642
   586
	if(!touch) printf("Touch %i/%i DNE\n",i,SDL_GetNumTouch());
jim@4642
   587
	EventTouchData* data;
jim@4645
   588
	data = (EventTouchData*)(touch->driverdata);
jim@4645
   589
	if(data == NULL) {
jim@4645
   590
	  printf("No driver data\n");
jim@4645
   591
	  continue;
jim@4642
   592
	}
jim@4642
   593
	if(data->eventStream <= 0) 
jim@4642
   594
	    printf("Error: Couldn't open stream\n");
jim@4642
   595
	rd = read(data->eventStream, ev, size * 64);
jim@4642
   596
	if(rd >= size) {
jim@4642
   597
	    for (i = 0; i < rd / sizeof(struct input_event); i++) {
jim@4642
   598
		switch (ev[i].type) {
jim@4642
   599
		case EV_ABS:
jim@4644
   600
		    switch (ev[i].code) {
jim@4644
   601
			case ABS_X:
jim@4644
   602
			    data->x = ev[i].value;
jim@4644
   603
			    break;
jim@4644
   604
			case ABS_Y:
jim@4644
   605
			    data->y = ev[i].value;
jim@4644
   606
			    break;
jim@4644
   607
			case ABS_PRESSURE:
jim@4644
   608
			    data->pressure = ev[i].value;
jim@4646
   609
			    if(data->pressure < 0) data->pressure = 0;
jim@4644
   610
			    break;
jim@4644
   611
			case ABS_MISC:
jim@4683
   612
			    if(ev[i].value == 0)
jim@4683
   613
			        data->up = SDL_TRUE;			    
jim@4644
   614
			    break;
jim@4644
   615
			}
jim@4642
   616
		    break;
jim@4642
   617
		case EV_MSC:
icculus@5603
   618
			if(ev[i].code == MSC_SERIAL)
icculus@5603
   619
				data->finger = ev[i].value;
icculus@5603
   620
			break;
icculus@5603
   621
		case EV_KEY:
icculus@5603
   622
			if(ev[i].code == BTN_TOUCH)
icculus@5603
   623
			    if(ev[i].value == 0)
icculus@5603
   624
			        data->up = SDL_TRUE;
icculus@5603
   625
			break;
jim@4642
   626
		case EV_SYN:
icculus@5603
   627
		  if(!data->down) {
icculus@5603
   628
		      data->down = SDL_TRUE;
jim@4655
   629
		      SDL_SendFingerDown(touch->id,data->finger,
icculus@5603
   630
		    		  data->down, data->x, data->y,
icculus@5603
   631
		    		  data->pressure);
jim@4655
   632
		  }
icculus@5603
   633
		  else if(!data->up)
jim@4683
   634
		    SDL_SendTouchMotion(touch->id,data->finger, 
icculus@5603
   635
					SDL_FALSE, data->x,data->y,
jim@4683
   636
					data->pressure);
icculus@5603
   637
		  else
icculus@5603
   638
		  {
icculus@5603
   639
		      data->down = SDL_FALSE;
icculus@5603
   640
			  SDL_SendFingerDown(touch->id,data->finger,
icculus@5603
   641
					  data->down, data->x,data->y,
icculus@5603
   642
					  data->pressure);
icculus@5603
   643
			  data->x = -1;
icculus@5603
   644
			  data->y = -1;
icculus@5603
   645
			  data->pressure = -1;
icculus@5603
   646
			  data->finger = 0;
icculus@5603
   647
			  data->up = SDL_FALSE;
icculus@5603
   648
		  }
jim@4683
   649
		  break;		
jim@4642
   650
		}
jim@4642
   651
	    }
jim@4642
   652
	}
jim@4642
   653
    }
jimtla@4669
   654
#endif
slouken@1951
   655
}
slouken@1951
   656
slouken@3030
   657
/* This is so wrong it hurts */
slouken@3030
   658
#define GNOME_SCREENSAVER_HACK
slouken@3030
   659
#ifdef GNOME_SCREENSAVER_HACK
slouken@3030
   660
#include <unistd.h>
slouken@3030
   661
static pid_t screensaver_inhibit_pid;
slouken@3040
   662
static void
slouken@3040
   663
gnome_screensaver_disable()
slouken@3030
   664
{
slouken@3030
   665
    screensaver_inhibit_pid = fork();
slouken@3030
   666
    if (screensaver_inhibit_pid == 0) {
slouken@3031
   667
        close(0);
slouken@3031
   668
        close(1);
slouken@3031
   669
        close(2);
slouken@3030
   670
        execl("/usr/bin/gnome-screensaver-command",
slouken@3030
   671
              "gnome-screensaver-command",
slouken@3030
   672
              "--inhibit",
slouken@3030
   673
              "--reason",
slouken@3040
   674
              "GNOME screensaver doesn't respect MIT-SCREEN-SAVER", NULL);
slouken@3030
   675
        exit(2);
slouken@3030
   676
    }
slouken@3030
   677
}
slouken@3040
   678
static void
slouken@3040
   679
gnome_screensaver_enable()
slouken@3030
   680
{
slouken@3030
   681
    kill(screensaver_inhibit_pid, 15);
slouken@3030
   682
}
slouken@3030
   683
#endif
slouken@3030
   684
slouken@1951
   685
void
slouken@3025
   686
X11_SuspendScreenSaver(_THIS)
slouken@1951
   687
{
slouken@5408
   688
#if SDL_VIDEO_DRIVER_X11_XSCRNSAVER
slouken@3025
   689
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@3025
   690
    int dummy;
slouken@3025
   691
    int major_version, minor_version;
slouken@1951
   692
slouken@3030
   693
    if (SDL_X11_HAVE_XSS) {
slouken@3030
   694
        /* XScreenSaverSuspend was introduced in MIT-SCREEN-SAVER 1.1 */
slouken@3030
   695
        if (!XScreenSaverQueryExtension(data->display, &dummy, &dummy) ||
slouken@3030
   696
            !XScreenSaverQueryVersion(data->display,
slouken@3030
   697
                                      &major_version, &minor_version) ||
slouken@3030
   698
            major_version < 1 || (major_version == 1 && minor_version < 1)) {
slouken@3030
   699
            return;
slouken@3030
   700
        }
slouken@3030
   701
slouken@3030
   702
        XScreenSaverSuspend(data->display, _this->suspend_screensaver);
slouken@3030
   703
        XResetScreenSaver(data->display);
slouken@1951
   704
    }
slouken@3030
   705
#endif
slouken@1951
   706
slouken@3030
   707
#ifdef GNOME_SCREENSAVER_HACK
slouken@3030
   708
    if (_this->suspend_screensaver) {
slouken@3030
   709
        gnome_screensaver_disable();
slouken@3030
   710
    } else {
slouken@3030
   711
        gnome_screensaver_enable();
slouken@3025
   712
    }
slouken@3025
   713
#endif
slouken@1951
   714
}
slouken@1951
   715
slouken@5481
   716
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
   717
slouken@1951
   718
/* vi: set ts=4 sw=4 expandtab: */