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