src/video/x11/SDL_x11events.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 07 Nov 2012 08:49:59 -0800
changeset 6660 4fa0723e6874
parent 6659 4016ef96095a
child 6661 cd8befc0a969
permissions -rw-r--r--
Don't lose focus if a child window gains focus
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@6660
   252
            if (xevent.xfocus.detail == NotifyInferior) {
slouken@6660
   253
#ifdef DEBUG_XEVENTS
slouken@6660
   254
                printf("window %p: FocusIn (NotifierInferior, ignoring)\n", data);
slouken@6660
   255
#endif
slouken@6660
   256
                break;
slouken@6660
   257
            }
slouken@1951
   258
#ifdef DEBUG_XEVENTS
slouken@6504
   259
            printf("window %p: FocusIn!\n", data);
slouken@1951
   260
#endif
slouken@6504
   261
            data->pending_focus = PENDING_FOCUS_IN;
slouken@6504
   262
            data->pending_focus_time = SDL_GetTicks() + PENDING_FOCUS_IN_TIME;
slouken@1951
   263
        }
slouken@1951
   264
        break;
slouken@1951
   265
slouken@1951
   266
        /* Losing input focus? */
slouken@1951
   267
    case FocusOut:{
slouken@6660
   268
            if (xevent.xfocus.detail == NotifyInferior) {
slouken@6660
   269
                /* We still have focus if a child gets focus */
slouken@6660
   270
#ifdef DEBUG_XEVENTS
slouken@6660
   271
                printf("window %p: FocusOut (NotifierInferior, ignoring)\n", data);
slouken@6660
   272
#endif
slouken@6660
   273
                break;
slouken@6660
   274
            }
slouken@1951
   275
#ifdef DEBUG_XEVENTS
slouken@6504
   276
            printf("window %p: FocusOut!\n", data);
slouken@1951
   277
#endif
slouken@6504
   278
            data->pending_focus = PENDING_FOCUS_OUT;
slouken@6504
   279
            data->pending_focus_time = SDL_GetTicks() + PENDING_FOCUS_OUT_TIME;
slouken@1951
   280
        }
slouken@1951
   281
        break;
slouken@1951
   282
slouken@1951
   283
        /* Generated upon EnterWindow and FocusIn */
slouken@1951
   284
    case KeymapNotify:{
slouken@1951
   285
#ifdef DEBUG_XEVENTS
slouken@6504
   286
            printf("window %p: KeymapNotify!\n", data);
slouken@1951
   287
#endif
slouken@1951
   288
            /* FIXME:
slouken@1951
   289
               X11_SetKeyboardState(SDL_Display, xevent.xkeymap.key_vector);
slouken@1951
   290
             */
slouken@1951
   291
        }
slouken@1951
   292
        break;
slouken@1951
   293
slouken@2305
   294
        /* Has the keyboard layout changed? */
slouken@2305
   295
    case MappingNotify:{
slouken@2305
   296
#ifdef DEBUG_XEVENTS
slouken@6504
   297
            printf("window %p: MappingNotify!\n", data);
slouken@2305
   298
#endif
slouken@2306
   299
            X11_UpdateKeymap(_this);
slouken@2305
   300
        }
slouken@2305
   301
        break;
slouken@2305
   302
slouken@1951
   303
        /* Key press? */
slouken@1951
   304
    case KeyPress:{
slouken@1951
   305
            KeyCode keycode = xevent.xkey.keycode;
bob@2300
   306
            KeySym keysym = NoSymbol;
slouken@2306
   307
            char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];
bob@2325
   308
            Status status = 0;
slouken@1951
   309
slouken@1951
   310
#ifdef DEBUG_XEVENTS
slouken@6504
   311
            printf("window %p: KeyPress (X11 keycode = 0x%X)\n", data, xevent.xkey.keycode);
slouken@1951
   312
#endif
slouken@4465
   313
            SDL_SendKeyboardKey(SDL_PRESSED, videodata->key_layout[keycode]);
slouken@4535
   314
#if 1
icculus@5981
   315
            if (videodata->key_layout[keycode] == SDL_SCANCODE_UNKNOWN) {
slouken@2305
   316
                int min_keycode, max_keycode;
slouken@4518
   317
                XDisplayKeycodes(display, &min_keycode, &max_keycode);
dimitris@6318
   318
#if SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM
dimitris@6318
   319
                keysym = XkbKeycodeToKeysym(display, keycode, 0, 0);
dimitris@6318
   320
#else
slouken@4518
   321
                keysym = XKeycodeToKeysym(display, keycode, 0);
dimitris@6318
   322
#endif
bob@2299
   323
                fprintf(stderr,
slouken@4556
   324
                        "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
   325
                        keycode, keycode - min_keycode, keysym,
slouken@2305
   326
                        XKeysymToString(keysym));
bob@2299
   327
            }
bob@2295
   328
#endif
bob@2325
   329
            /* */
slouken@2305
   330
            SDL_zero(text);
bob@2325
   331
#ifdef X_HAVE_UTF8_STRING
bob@2325
   332
            if (data->ic) {
bob@2325
   333
                Xutf8LookupString(data->ic, &xevent.xkey, text, sizeof(text),
slouken@2738
   334
                                  &keysym, &status);
bob@2325
   335
            }
bob@2325
   336
#else
slouken@2306
   337
            XLookupString(&xevent.xkey, text, sizeof(text), &keysym, NULL);
bob@2325
   338
#endif
slouken@2305
   339
            if (*text) {
slouken@4465
   340
                SDL_SendKeyboardText(text);
bob@2300
   341
            }
slouken@1951
   342
        }
slouken@1951
   343
        break;
slouken@1951
   344
slouken@1951
   345
        /* Key release? */
slouken@1951
   346
    case KeyRelease:{
slouken@1951
   347
            KeyCode keycode = xevent.xkey.keycode;
slouken@1951
   348
slouken@1951
   349
#ifdef DEBUG_XEVENTS
slouken@6504
   350
            printf("window %p: KeyRelease (X11 keycode = 0x%X)\n", data, xevent.xkey.keycode);
slouken@1951
   351
#endif
slouken@4565
   352
            if (X11_KeyRepeat(display, &xevent)) {
slouken@4565
   353
                /* We're about to get a repeated key down, ignore the key up */
slouken@4565
   354
                break;
slouken@4565
   355
            }
slouken@4465
   356
            SDL_SendKeyboardKey(SDL_RELEASED, videodata->key_layout[keycode]);
slouken@1951
   357
        }
slouken@1951
   358
        break;
slouken@1951
   359
slouken@1951
   360
        /* Have we been iconified? */
slouken@1951
   361
    case UnmapNotify:{
slouken@1951
   362
#ifdef DEBUG_XEVENTS
slouken@6504
   363
            printf("window %p: UnmapNotify!\n", data);
slouken@1951
   364
#endif
slouken@6481
   365
            X11_DispatchUnmapNotify(data);
slouken@1951
   366
        }
slouken@1951
   367
        break;
slouken@1951
   368
slouken@1951
   369
        /* Have we been restored? */
slouken@1951
   370
    case MapNotify:{
slouken@1951
   371
#ifdef DEBUG_XEVENTS
slouken@6504
   372
            printf("window %p: MapNotify!\n", data);
slouken@1951
   373
#endif
slouken@6481
   374
            X11_DispatchMapNotify(data);
slouken@1951
   375
        }
slouken@1951
   376
        break;
slouken@1951
   377
slouken@1951
   378
        /* Have we been resized or moved? */
slouken@1951
   379
    case ConfigureNotify:{
slouken@1951
   380
#ifdef DEBUG_XEVENTS
slouken@6542
   381
            printf("window %p: ConfigureNotify! (position: %d,%d, size: %dx%d)\n", data,
slouken@6542
   382
                   xevent.xconfigure.x, xevent.xconfigure.y,
slouken@1951
   383
                   xevent.xconfigure.width, xevent.xconfigure.height);
slouken@1951
   384
#endif
slouken@6542
   385
            if (xevent.xconfigure.x != data->last_xconfigure.x ||
slouken@6542
   386
                xevent.xconfigure.y != data->last_xconfigure.y) {
slouken@6542
   387
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED,
slouken@6542
   388
                                    xevent.xconfigure.x, xevent.xconfigure.y);
slouken@6542
   389
            }
slouken@6542
   390
            if (xevent.xconfigure.width != data->last_xconfigure.width ||
slouken@6542
   391
                xevent.xconfigure.height != data->last_xconfigure.height) {
slouken@6542
   392
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED,
slouken@6542
   393
                                    xevent.xconfigure.width,
slouken@6542
   394
                                    xevent.xconfigure.height);
slouken@6542
   395
            }
slouken@6542
   396
            data->last_xconfigure = xevent.xconfigure;
slouken@1951
   397
        }
slouken@1951
   398
        break;
slouken@1951
   399
slouken@1951
   400
        /* Have we been requested to quit (or another client message?) */
slouken@1951
   401
    case ClientMessage:{
icculus@6637
   402
            if ((xevent.xclient.message_type == videodata->WM_PROTOCOLS) &&
icculus@6637
   403
                (xevent.xclient.format == 32) &&
icculus@6638
   404
                (xevent.xclient.data.l[0] == videodata->_NET_WM_PING)) {
slouken@6659
   405
                Window root = DefaultRootWindow(display);
icculus@6638
   406
icculus@6638
   407
#ifdef DEBUG_XEVENTS
icculus@6638
   408
                printf("window %p: _NET_WM_PING\n", data);
icculus@6638
   409
#endif
icculus@6638
   410
                xevent.xclient.window = root;
icculus@6638
   411
                XSendEvent(display, root, False, SubstructureRedirectMask | SubstructureNotifyMask, &xevent);
icculus@6638
   412
                break;
icculus@6638
   413
            }
icculus@6638
   414
icculus@6638
   415
            else if ((xevent.xclient.message_type == videodata->WM_PROTOCOLS) &&
icculus@6638
   416
                (xevent.xclient.format == 32) &&
slouken@1951
   417
                (xevent.xclient.data.l[0] == videodata->WM_DELETE_WINDOW)) {
slouken@1951
   418
icculus@6638
   419
#ifdef DEBUG_XEVENTS
icculus@6638
   420
                printf("window %p: WM_DELETE_WINDOW\n", data);
icculus@6638
   421
#endif
slouken@3685
   422
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
icculus@6638
   423
                break;
slouken@1951
   424
            }
slouken@1951
   425
        }
slouken@1951
   426
        break;
slouken@1951
   427
slouken@1951
   428
        /* Do we need to refresh ourselves? */
slouken@1951
   429
    case Expose:{
slouken@1951
   430
#ifdef DEBUG_XEVENTS
slouken@6504
   431
            printf("window %p: Expose (count = %d)\n", data, xevent.xexpose.count);
slouken@1951
   432
#endif
slouken@3685
   433
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
slouken@1951
   434
        }
slouken@1951
   435
        break;
slouken@1951
   436
slouken@4465
   437
    case MotionNotify:{
slouken@6311
   438
            SDL_Mouse *mouse = SDL_GetMouse();  
slouken@6311
   439
            if(!mouse->relative_mode) {
slouken@4465
   440
#ifdef DEBUG_MOTION
slouken@6504
   441
                printf("window %p: X11 motion: %d,%d\n", xevent.xmotion.x, xevent.xmotion.y);
slouken@4465
   442
#endif
slouken@6311
   443
slouken@6311
   444
                SDL_SendMouseMotion(data->window, 0, xevent.xmotion.x, xevent.xmotion.y);
slouken@6311
   445
            }
slouken@4465
   446
        }
slouken@4465
   447
        break;
slouken@4465
   448
slouken@4465
   449
    case ButtonPress:{
slouken@5530
   450
            int ticks = 0;
slouken@5530
   451
            if (X11_IsWheelEvent(display,&xevent,&ticks) == SDL_TRUE) {
slouken@5530
   452
                SDL_SendMouseWheel(data->window, 0, ticks);
slouken@5530
   453
            }
slouken@5530
   454
            else {
slouken@5530
   455
                SDL_SendMouseButton(data->window, SDL_PRESSED, xevent.xbutton.button);
slouken@5530
   456
            }
slouken@4465
   457
        }
slouken@4465
   458
        break;
slouken@4465
   459
slouken@4465
   460
    case ButtonRelease:{
slouken@4484
   461
            SDL_SendMouseButton(data->window, SDL_RELEASED, xevent.xbutton.button);
slouken@4465
   462
        }
slouken@4465
   463
        break;
slouken@4465
   464
slouken@4518
   465
    case PropertyNotify:{
slouken@4518
   466
#ifdef DEBUG_XEVENTS
slouken@4520
   467
            unsigned char *propdata;
slouken@4520
   468
            int status, real_format;
slouken@4520
   469
            Atom real_type;
slouken@4520
   470
            unsigned long items_read, items_left, i;
slouken@4520
   471
slouken@4518
   472
            char *name = XGetAtomName(display, xevent.xproperty.atom);
slouken@4518
   473
            if (name) {
slouken@6504
   474
                printf("window %p: PropertyNotify: %s %s\n", data, name, (xevent.xproperty.state == PropertyDelete) ? "deleted" : "changed");
slouken@4518
   475
                XFree(name);
slouken@4518
   476
            }
slouken@4518
   477
slouken@4520
   478
            status = XGetWindowProperty(display, data->xwindow, xevent.xproperty.atom, 0L, 8192L, False, AnyPropertyType, &real_type, &real_format, &items_read, &items_left, &propdata);
slouken@4556
   479
            if (status == Success && items_read > 0) {
slouken@4520
   480
                if (real_type == XA_INTEGER) {
slouken@4520
   481
                    int *values = (int *)propdata;
slouken@4520
   482
slouken@4520
   483
                    printf("{");
slouken@4520
   484
                    for (i = 0; i < items_read; i++) {
slouken@4520
   485
                        printf(" %d", values[i]);
slouken@4520
   486
                    }
slouken@4520
   487
                    printf(" }\n");
slouken@4520
   488
                } else if (real_type == XA_CARDINAL) {
slouken@4520
   489
                    if (real_format == 32) {
slouken@4520
   490
                        Uint32 *values = (Uint32 *)propdata;
slouken@4520
   491
slouken@4520
   492
                        printf("{");
slouken@4520
   493
                        for (i = 0; i < items_read; i++) {
slouken@4520
   494
                            printf(" %d", values[i]);
slouken@4520
   495
                        }
slouken@4520
   496
                        printf(" }\n");
slouken@4520
   497
                    } else if (real_format == 16) {
slouken@4520
   498
                        Uint16 *values = (Uint16 *)propdata;
slouken@4520
   499
slouken@4520
   500
                        printf("{");
slouken@4520
   501
                        for (i = 0; i < items_read; i++) {
slouken@4520
   502
                            printf(" %d", values[i]);
slouken@4520
   503
                        }
slouken@4520
   504
                        printf(" }\n");
slouken@4520
   505
                    } else if (real_format == 8) {
slouken@4520
   506
                        Uint8 *values = (Uint8 *)propdata;
slouken@4520
   507
slouken@4520
   508
                        printf("{");
slouken@4520
   509
                        for (i = 0; i < items_read; i++) {
slouken@4520
   510
                            printf(" %d", values[i]);
slouken@4520
   511
                        }
slouken@4520
   512
                        printf(" }\n");
slouken@4520
   513
                    }
slouken@4520
   514
                } else if (real_type == XA_STRING ||
slouken@4520
   515
                           real_type == videodata->UTF8_STRING) {
slouken@4520
   516
                    printf("{ \"%s\" }\n", propdata);
slouken@4520
   517
                } else if (real_type == XA_ATOM) {
slouken@4518
   518
                    Atom *atoms = (Atom *)propdata;
slouken@4520
   519
slouken@4520
   520
                    printf("{");
slouken@4518
   521
                    for (i = 0; i < items_read; i++) {
slouken@4520
   522
                        char *name = XGetAtomName(display, atoms[i]);
slouken@4520
   523
                        if (name) {
slouken@4520
   524
                            printf(" %s", name);
slouken@4520
   525
                            XFree(name);
slouken@4518
   526
                        }
slouken@4518
   527
                    }
slouken@4520
   528
                    printf(" }\n");
slouken@4520
   529
                } else {
slouken@4520
   530
                    char *name = XGetAtomName(display, real_type);
slouken@4520
   531
                    printf("Unknown type: %ld (%s)\n", real_type, name ? name : "UNKNOWN");
slouken@4520
   532
                    if (name) {
slouken@4520
   533
                        XFree(name);
slouken@4520
   534
                    }
slouken@4518
   535
                }
slouken@4520
   536
            }
slouken@6481
   537
            if (status == Success) {
slouken@6481
   538
                XFree(propdata);
slouken@6481
   539
            }
slouken@6481
   540
#endif /* DEBUG_XEVENTS */
slouken@6481
   541
slouken@6481
   542
            if (xevent.xproperty.atom == data->videodata->_NET_WM_STATE) {
slouken@6481
   543
                /* Get the new state from the window manager.
slouken@6481
   544
                   Compositing window managers can alter visibility of windows
slouken@6481
   545
                   without ever mapping / unmapping them, so we handle that here,
slouken@6481
   546
                   because they use the NETWM protocol to notify us of changes.
slouken@6481
   547
                 */
slouken@6487
   548
                Uint32 flags = X11_GetNetWMState(_this, xevent.xproperty.window);
slouken@6481
   549
                if ((flags^data->window->flags) & SDL_WINDOW_HIDDEN) {
slouken@6481
   550
                    if (flags & SDL_WINDOW_HIDDEN) {
slouken@6481
   551
                        X11_DispatchUnmapNotify(data);
slouken@6481
   552
                    } else {
slouken@6481
   553
                        X11_DispatchMapNotify(data);
slouken@6481
   554
                    }
slouken@6481
   555
                }
slouken@6481
   556
            }
slouken@4518
   557
        }
slouken@4518
   558
        break;
slouken@4518
   559
slouken@4508
   560
    /* Copy the selection from XA_CUT_BUFFER0 to the requested property */
slouken@4508
   561
    case SelectionRequest: {
slouken@4508
   562
            XSelectionRequestEvent *req;
slouken@4508
   563
            XEvent sevent;
slouken@4508
   564
            int seln_format;
slouken@4508
   565
            unsigned long nbytes;
slouken@4508
   566
            unsigned long overflow;
slouken@4508
   567
            unsigned char *seln_data;
slouken@4508
   568
slouken@4508
   569
            req = &xevent.xselectionrequest;
slouken@4508
   570
#ifdef DEBUG_XEVENTS
slouken@6504
   571
            printf("window %p: SelectionRequest (requestor = %ld, target = %ld)\n", data,
slouken@4508
   572
                req->requestor, req->target);
slouken@4508
   573
#endif
slouken@4508
   574
slouken@4524
   575
            SDL_zero(sevent);
slouken@4520
   576
            sevent.xany.type = SelectionNotify;
slouken@4508
   577
            sevent.xselection.selection = req->selection;
slouken@4508
   578
            sevent.xselection.target = None;
slouken@4508
   579
            sevent.xselection.property = None;
slouken@4508
   580
            sevent.xselection.requestor = req->requestor;
slouken@4508
   581
            sevent.xselection.time = req->time;
slouken@4508
   582
            if (XGetWindowProperty(display, DefaultRootWindow(display),
slouken@4508
   583
                    XA_CUT_BUFFER0, 0, INT_MAX/4, False, req->target,
slouken@4508
   584
                    &sevent.xselection.target, &seln_format, &nbytes,
slouken@4508
   585
                    &overflow, &seln_data) == Success) {
slouken@4508
   586
                if (sevent.xselection.target == req->target) {
slouken@4508
   587
                    XChangeProperty(display, req->requestor, req->property,
slouken@4508
   588
                        sevent.xselection.target, seln_format, PropModeReplace,
slouken@4508
   589
                        seln_data, nbytes);
slouken@4508
   590
                    sevent.xselection.property = req->property;
slouken@4508
   591
                }
slouken@4508
   592
                XFree(seln_data);
slouken@4508
   593
            }
slouken@4508
   594
            XSendEvent(display, req->requestor, False, 0, &sevent);
slouken@4508
   595
            XSync(display, False);
slouken@4508
   596
        }
slouken@4508
   597
        break;
slouken@4508
   598
slouken@4508
   599
    case SelectionNotify: {
slouken@4508
   600
#ifdef DEBUG_XEVENTS
slouken@6504
   601
            printf("window %p: SelectionNotify (requestor = %ld, target = %ld)\n", data,
slouken@4508
   602
                xevent.xselection.requestor, xevent.xselection.target);
slouken@4508
   603
#endif
slouken@4508
   604
            videodata->selection_waiting = SDL_FALSE;
slouken@4508
   605
        }
slouken@4508
   606
        break;
slouken@4508
   607
slouken@1951
   608
    default:{
slouken@1951
   609
#ifdef DEBUG_XEVENTS
slouken@6504
   610
            printf("window %p: Unhandled event %d\n", data, xevent.type);
slouken@1951
   611
#endif
slouken@1951
   612
        }
slouken@1951
   613
        break;
slouken@1951
   614
    }
slouken@1951
   615
}
slouken@1951
   616
slouken@6504
   617
static void
slouken@6504
   618
X11_HandleFocusChanges(_THIS)
slouken@6504
   619
{
slouken@6504
   620
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@6504
   621
    int i;
slouken@6504
   622
slouken@6504
   623
    if (videodata && videodata->windowlist) {
slouken@6504
   624
        for (i = 0; i < videodata->numwindows; ++i) {
slouken@6504
   625
            SDL_WindowData *data = videodata->windowlist[i];
slouken@6504
   626
            if (data && data->pending_focus != PENDING_FOCUS_NONE) {
slouken@6504
   627
                Uint32 now = SDL_GetTicks();
slouken@6504
   628
                if ( (int)(data->pending_focus_time-now) <= 0 ) {
slouken@6504
   629
                    if ( data->pending_focus == PENDING_FOCUS_IN ) {
slouken@6504
   630
                        X11_DispatchFocusIn(data);
slouken@6504
   631
                    } else {
slouken@6504
   632
                        X11_DispatchFocusOut(data);
slouken@6504
   633
                    }
slouken@6504
   634
                    data->pending_focus = PENDING_FOCUS_NONE;
slouken@6504
   635
                }
slouken@6504
   636
            }
slouken@6504
   637
        }
slouken@6504
   638
    }
slouken@6504
   639
}
slouken@1951
   640
/* Ack!  XPending() actually performs a blocking read if no events available */
slouken@4472
   641
static int
slouken@1951
   642
X11_Pending(Display * display)
slouken@1951
   643
{
slouken@1951
   644
    /* Flush the display connection and look to see if events are queued */
slouken@1951
   645
    XFlush(display);
slouken@1951
   646
    if (XEventsQueued(display, QueuedAlready)) {
slouken@1951
   647
        return (1);
slouken@1951
   648
    }
slouken@1951
   649
slouken@1951
   650
    /* More drastic measures are required -- see if X is ready to talk */
slouken@1951
   651
    {
slouken@1951
   652
        static struct timeval zero_time;        /* static == 0 */
slouken@1951
   653
        int x11_fd;
slouken@1951
   654
        fd_set fdset;
slouken@1951
   655
slouken@1951
   656
        x11_fd = ConnectionNumber(display);
slouken@1951
   657
        FD_ZERO(&fdset);
slouken@1951
   658
        FD_SET(x11_fd, &fdset);
slouken@1951
   659
        if (select(x11_fd + 1, &fdset, NULL, NULL, &zero_time) == 1) {
slouken@1951
   660
            return (XPending(display));
slouken@1951
   661
        }
slouken@1951
   662
    }
slouken@1951
   663
slouken@1951
   664
    /* Oh well, nothing is ready .. */
slouken@1951
   665
    return (0);
slouken@1951
   666
}
slouken@1951
   667
icculus@5981
   668
icculus@5981
   669
/* !!! FIXME: this should be exposed in a header, or something. */
icculus@5981
   670
int SDL_GetNumTouch(void);
icculus@5981
   671
icculus@5981
   672
slouken@1951
   673
void
slouken@1951
   674
X11_PumpEvents(_THIS)
slouken@1951
   675
{
slouken@1951
   676
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   677
slouken@3025
   678
    /* Update activity every 30 seconds to prevent screensaver */
slouken@3025
   679
    if (_this->suspend_screensaver) {
slouken@3025
   680
        Uint32 now = SDL_GetTicks();
slouken@3025
   681
        if (!data->screensaver_activity ||
slouken@3040
   682
            (int) (now - data->screensaver_activity) >= 30000) {
slouken@3025
   683
            XResetScreenSaver(data->display);
slouken@3025
   684
            data->screensaver_activity = now;
slouken@3025
   685
        }
dimitris@6318
   686
    }   
slouken@3025
   687
slouken@1951
   688
    /* Keep processing pending events */
slouken@1951
   689
    while (X11_Pending(data->display)) {
slouken@1951
   690
        X11_DispatchEvent(_this);
slouken@1951
   691
    }
slouken@6504
   692
slouken@6504
   693
    /* FIXME: Only need to do this when there are pending focus changes */
slouken@6504
   694
    X11_HandleFocusChanges(_this);
slouken@6504
   695
dimitris@6316
   696
    /*Dont process evtouch events if XInput2 multitouch is supported*/
icculus@6432
   697
    if(X11_Xinput2IsMultitouchSupported()) {
dimitris@6316
   698
        return;
dimitris@6316
   699
    }
jim@4640
   700
jim@4681
   701
#ifdef SDL_INPUT_LINUXEV
dimitris@6316
   702
    /* Process Touch events*/
jim@4642
   703
    int i = 0,rd;
jim@4642
   704
    struct input_event ev[64];
jim@4642
   705
    int size = sizeof (struct input_event);
jim@4645
   706
icculus@5603
   707
/* !!! FIXME: clean the tabstops out of here. */
jim@4642
   708
    for(i = 0;i < SDL_GetNumTouch();++i) {
jim@4642
   709
	SDL_Touch* touch = SDL_GetTouchIndex(i);
jim@4642
   710
	if(!touch) printf("Touch %i/%i DNE\n",i,SDL_GetNumTouch());
jim@4642
   711
	EventTouchData* data;
jim@4645
   712
	data = (EventTouchData*)(touch->driverdata);
jim@4645
   713
	if(data == NULL) {
jim@4645
   714
	  printf("No driver data\n");
jim@4645
   715
	  continue;
jim@4642
   716
	}
jim@4642
   717
	if(data->eventStream <= 0) 
jim@4642
   718
	    printf("Error: Couldn't open stream\n");
jim@4642
   719
	rd = read(data->eventStream, ev, size * 64);
jim@4642
   720
	if(rd >= size) {
jim@4642
   721
	    for (i = 0; i < rd / sizeof(struct input_event); i++) {
jim@4642
   722
		switch (ev[i].type) {
jim@4642
   723
		case EV_ABS:
jim@4644
   724
		    switch (ev[i].code) {
jim@4644
   725
			case ABS_X:
jim@4644
   726
			    data->x = ev[i].value;
jim@4644
   727
			    break;
jim@4644
   728
			case ABS_Y:
jim@4644
   729
			    data->y = ev[i].value;
jim@4644
   730
			    break;
jim@4644
   731
			case ABS_PRESSURE:
jim@4644
   732
			    data->pressure = ev[i].value;
jim@4646
   733
			    if(data->pressure < 0) data->pressure = 0;
jim@4644
   734
			    break;
jim@4644
   735
			case ABS_MISC:
jim@4683
   736
			    if(ev[i].value == 0)
jim@4683
   737
			        data->up = SDL_TRUE;			    
jim@4644
   738
			    break;
jim@4644
   739
			}
jim@4642
   740
		    break;
jim@4642
   741
		case EV_MSC:
icculus@5603
   742
			if(ev[i].code == MSC_SERIAL)
icculus@5603
   743
				data->finger = ev[i].value;
icculus@5603
   744
			break;
icculus@5603
   745
		case EV_KEY:
icculus@5603
   746
			if(ev[i].code == BTN_TOUCH)
icculus@5603
   747
			    if(ev[i].value == 0)
icculus@5603
   748
			        data->up = SDL_TRUE;
icculus@5603
   749
			break;
jim@4642
   750
		case EV_SYN:
icculus@5603
   751
		  if(!data->down) {
icculus@5603
   752
		      data->down = SDL_TRUE;
jim@4655
   753
		      SDL_SendFingerDown(touch->id,data->finger,
icculus@5603
   754
		    		  data->down, data->x, data->y,
icculus@5603
   755
		    		  data->pressure);
jim@4655
   756
		  }
icculus@5603
   757
		  else if(!data->up)
jim@4683
   758
		    SDL_SendTouchMotion(touch->id,data->finger, 
icculus@5603
   759
					SDL_FALSE, data->x,data->y,
jim@4683
   760
					data->pressure);
icculus@5603
   761
		  else
icculus@5603
   762
		  {
icculus@5603
   763
		      data->down = SDL_FALSE;
icculus@5603
   764
			  SDL_SendFingerDown(touch->id,data->finger,
icculus@5603
   765
					  data->down, data->x,data->y,
icculus@5603
   766
					  data->pressure);
icculus@5603
   767
			  data->x = -1;
icculus@5603
   768
			  data->y = -1;
icculus@5603
   769
			  data->pressure = -1;
icculus@5603
   770
			  data->finger = 0;
icculus@5603
   771
			  data->up = SDL_FALSE;
icculus@5603
   772
		  }
jim@4683
   773
		  break;		
jim@4642
   774
		}
jim@4642
   775
	    }
jim@4642
   776
	}
jim@4642
   777
    }
jimtla@4669
   778
#endif
slouken@1951
   779
}
slouken@1951
   780
slouken@3030
   781
/* This is so wrong it hurts */
slouken@3030
   782
#define GNOME_SCREENSAVER_HACK
slouken@3030
   783
#ifdef GNOME_SCREENSAVER_HACK
slouken@3030
   784
#include <unistd.h>
slouken@3030
   785
static pid_t screensaver_inhibit_pid;
slouken@3040
   786
static void
slouken@3040
   787
gnome_screensaver_disable()
slouken@3030
   788
{
slouken@3030
   789
    screensaver_inhibit_pid = fork();
slouken@3030
   790
    if (screensaver_inhibit_pid == 0) {
slouken@3031
   791
        close(0);
slouken@3031
   792
        close(1);
slouken@3031
   793
        close(2);
slouken@3030
   794
        execl("/usr/bin/gnome-screensaver-command",
slouken@3030
   795
              "gnome-screensaver-command",
slouken@3030
   796
              "--inhibit",
slouken@3030
   797
              "--reason",
slouken@3040
   798
              "GNOME screensaver doesn't respect MIT-SCREEN-SAVER", NULL);
slouken@3030
   799
        exit(2);
slouken@3030
   800
    }
slouken@3030
   801
}
slouken@3040
   802
static void
slouken@3040
   803
gnome_screensaver_enable()
slouken@3030
   804
{
slouken@3030
   805
    kill(screensaver_inhibit_pid, 15);
slouken@3030
   806
}
slouken@3030
   807
#endif
slouken@3030
   808
slouken@1951
   809
void
slouken@3025
   810
X11_SuspendScreenSaver(_THIS)
slouken@1951
   811
{
slouken@5408
   812
#if SDL_VIDEO_DRIVER_X11_XSCRNSAVER
slouken@3025
   813
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@3025
   814
    int dummy;
slouken@3025
   815
    int major_version, minor_version;
slouken@1951
   816
slouken@3030
   817
    if (SDL_X11_HAVE_XSS) {
slouken@3030
   818
        /* XScreenSaverSuspend was introduced in MIT-SCREEN-SAVER 1.1 */
slouken@3030
   819
        if (!XScreenSaverQueryExtension(data->display, &dummy, &dummy) ||
slouken@3030
   820
            !XScreenSaverQueryVersion(data->display,
slouken@3030
   821
                                      &major_version, &minor_version) ||
slouken@3030
   822
            major_version < 1 || (major_version == 1 && minor_version < 1)) {
slouken@3030
   823
            return;
slouken@3030
   824
        }
slouken@3030
   825
slouken@3030
   826
        XScreenSaverSuspend(data->display, _this->suspend_screensaver);
slouken@3030
   827
        XResetScreenSaver(data->display);
slouken@1951
   828
    }
slouken@3030
   829
#endif
slouken@1951
   830
slouken@3030
   831
#ifdef GNOME_SCREENSAVER_HACK
slouken@3030
   832
    if (_this->suspend_screensaver) {
slouken@3030
   833
        gnome_screensaver_disable();
slouken@3030
   834
    } else {
slouken@3030
   835
        gnome_screensaver_enable();
slouken@3025
   836
    }
slouken@3025
   837
#endif
slouken@1951
   838
}
slouken@1951
   839
slouken@5481
   840
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
   841
slouken@1951
   842
/* vi: set ts=4 sw=4 expandtab: */