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