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