src/video/x11/SDL_x11events.c
author Sam Lantinga
Sun, 20 Oct 2013 20:42:55 -0700
changeset 7857 6388f5229bb7
parent 7839 d2804f8153fe
child 7885 a8bd63b33636
permissions -rw-r--r--
Added a macro SDL_TICKS_PASSED() to correctly compare two 32-bit tick values.
Went through the code and used the macro and fixed a couple places that were using incorrect timestamp comparisons.
slouken@1951
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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
slouken@7001
    44
typedef struct {
slouken@7191
    45
    unsigned char *data;
slouken@7191
    46
    int format, count;
slouken@7191
    47
    Atom type;
slouken@7001
    48
} SDL_x11Prop;
slouken@7001
    49
slouken@7001
    50
/* Reads property
icculus@7827
    51
   Must call X11_XFree on results
slouken@7001
    52
 */
slouken@7191
    53
static void X11_ReadProperty(SDL_x11Prop *p, Display *disp, Window w, Atom prop)
slouken@7001
    54
{
slouken@7001
    55
    unsigned char *ret=NULL;
slouken@7001
    56
    Atom type;
slouken@7001
    57
    int fmt;
slouken@7001
    58
    unsigned long count;
slouken@7001
    59
    unsigned long bytes_left;
slouken@7191
    60
    int bytes_fetch = 0;
slouken@7191
    61
slouken@7001
    62
    do {
icculus@7827
    63
        if (ret != 0) X11_XFree(ret);
icculus@7827
    64
        X11_XGetWindowProperty(disp, w, prop, 0, bytes_fetch, False, AnyPropertyType, &type, &fmt, &count, &bytes_left, &ret);
slouken@7001
    65
        bytes_fetch += bytes_left;
slouken@7001
    66
    } while (bytes_left != 0);
slouken@7191
    67
slouken@7001
    68
    p->data=ret;
slouken@7001
    69
    p->format=fmt;
slouken@7001
    70
    p->count=count;
slouken@7001
    71
    p->type=type;
slouken@7001
    72
}
slouken@7001
    73
slouken@7001
    74
/* Find text-uri-list in a list of targets and return it's atom
slouken@7001
    75
   if available, else return None */
slouken@7001
    76
static Atom X11_PickTarget(Display *disp, Atom list[], int list_count)
slouken@7001
    77
{
slouken@7001
    78
    Atom request = None;
slouken@7001
    79
    char *name;
slouken@7001
    80
    int i;
slouken@7001
    81
    for (i=0; i < list_count && request == None; i++) {
icculus@7827
    82
        name = X11_XGetAtomName(disp, list[i]);
slouken@7001
    83
        if (strcmp("text/uri-list", name)==0) request = list[i];
icculus@7827
    84
        X11_XFree(name);
slouken@7001
    85
    }
slouken@7001
    86
    return request;
slouken@7001
    87
}
slouken@7001
    88
slouken@7001
    89
/* Wrapper for X11_PickTarget for a maximum of three targets, a special
slouken@7001
    90
   case in the Xdnd protocol */
slouken@7001
    91
static Atom X11_PickTargetFromAtoms(Display *disp, Atom a0, Atom a1, Atom a2)
slouken@7001
    92
{
slouken@7001
    93
    int count=0;
slouken@7001
    94
    Atom atom[3];
slouken@7001
    95
    if (a0 != None) atom[count++] = a0;
slouken@7001
    96
    if (a1 != None) atom[count++] = a1;
slouken@7001
    97
    if (a2 != None) atom[count++] = a2;
slouken@7001
    98
    return X11_PickTarget(disp, atom, count);
slouken@7001
    99
}
gabomdq@7678
   100
/* #define DEBUG_XEVENTS */
slouken@6474
   101
icculus@7475
   102
struct KeyRepeatCheckData
icculus@7475
   103
{
icculus@7475
   104
    XEvent *event;
icculus@7475
   105
    SDL_bool found;
icculus@7475
   106
};
icculus@7475
   107
icculus@7475
   108
static Bool X11_KeyRepeatCheckIfEvent(Display *display, XEvent *chkev,
icculus@7475
   109
    XPointer arg)
icculus@7475
   110
{
icculus@7475
   111
    struct KeyRepeatCheckData *d = (struct KeyRepeatCheckData *) arg;
icculus@7475
   112
    if (chkev->type == KeyPress &&
icculus@7475
   113
        chkev->xkey.keycode == d->event->xkey.keycode &&
icculus@7475
   114
        chkev->xkey.time - d->event->xkey.time < 2)
icculus@7475
   115
        d->found = SDL_TRUE;
icculus@7475
   116
    return False;
icculus@7475
   117
}
icculus@7475
   118
slouken@4565
   119
/* Check to see if this is a repeated key.
slouken@4565
   120
   (idea shamelessly lifted from GII -- thanks guys! :)
slouken@4565
   121
 */
slouken@4565
   122
static SDL_bool X11_KeyRepeat(Display *display, XEvent *event)
slouken@4565
   123
{
icculus@7475
   124
    XEvent dummyev;
icculus@7475
   125
    struct KeyRepeatCheckData d;
icculus@7475
   126
    d.event = event;
icculus@7475
   127
    d.found = SDL_FALSE;
icculus@7827
   128
    if (X11_XPending(display))
icculus@7827
   129
        X11_XCheckIfEvent(display, &dummyev, X11_KeyRepeatCheckIfEvent,
icculus@7475
   130
            (XPointer) &d);
icculus@7475
   131
    return d.found;
icculus@7475
   132
}
slouken@4565
   133
icculus@7475
   134
static Bool X11_IsWheelCheckIfEvent(Display *display, XEvent *chkev,
icculus@7475
   135
    XPointer arg)
icculus@7475
   136
{
icculus@7475
   137
    XEvent *event = (XEvent *) arg;
slouken@7749
   138
    /* we only handle buttons 4 and 5 - false positive avoidance */
icculus@7475
   139
    if (chkev->type == ButtonRelease &&
slouken@7749
   140
        (event->xbutton.button == Button4 || event->xbutton.button == Button5) &&
icculus@7475
   141
        chkev->xbutton.button == event->xbutton.button &&
icculus@7475
   142
        chkev->xbutton.time == event->xbutton.time)
icculus@7475
   143
        return True;
icculus@7475
   144
    return False;
slouken@4565
   145
}
slouken@4565
   146
slouken@5530
   147
static SDL_bool X11_IsWheelEvent(Display * display,XEvent * event,int * ticks)
slouken@5530
   148
{
icculus@7475
   149
    XEvent relevent;
icculus@7827
   150
    if (X11_XPending(display)) {
slouken@5530
   151
        /* according to the xlib docs, no specific mouse wheel events exist.
slouken@5530
   152
           however, mouse wheel events trigger a button press and a button release
slouken@5530
   153
           immediately. thus, checking if the same button was released at the same
slouken@7191
   154
           time as it was pressed, should be an adequate hack to derive a mouse
slouken@7749
   155
           wheel event.
slouken@7749
   156
           However, there is broken and unusual hardware out there...
slouken@7749
   157
           - False positive: a button for which a release event is
slouken@7749
   158
             generated (or synthesised) immediately.
slouken@7749
   159
           - False negative: a wheel which, when rolled, doesn't have
slouken@7749
   160
             a release event generated immediately. */
icculus@7827
   161
        if (X11_XCheckIfEvent(display, &relevent, X11_IsWheelCheckIfEvent,
icculus@7475
   162
            (XPointer) event)) {
slouken@5530
   163
slouken@5530
   164
            /* by default, X11 only knows 5 buttons. on most 3 button + wheel mouse,
slouken@5530
   165
               Button4 maps to wheel up, Button5 maps to wheel down. */
slouken@5530
   166
            if (event->xbutton.button == Button4) {
slouken@5530
   167
                *ticks = 1;
slouken@5530
   168
            }
slouken@6184
   169
            else if (event->xbutton.button == Button5) {
slouken@5530
   170
                *ticks = -1;
slouken@5530
   171
            }
slouken@5530
   172
            return SDL_TRUE;
slouken@5530
   173
        }
slouken@5530
   174
    }
slouken@5530
   175
    return SDL_FALSE;
slouken@5530
   176
}
slouken@5530
   177
slouken@7001
   178
/* Convert URI to local filename
slouken@7001
   179
   return filename if possible, else NULL
slouken@7001
   180
*/
slouken@7001
   181
static char* X11_URIToLocal(char* uri) {
slouken@7001
   182
    char *file = NULL;
gabomdq@7679
   183
    SDL_bool local;
slouken@7001
   184
slouken@7001
   185
    if (memcmp(uri,"file:/",6) == 0) uri += 6;      /* local file? */
slouken@7001
   186
    else if (strstr(uri,":/") != NULL) return file; /* wrong scheme */
slouken@7001
   187
gabomdq@7679
   188
    local = uri[0] != '/' || ( uri[0] != '\0' && uri[1] == '/' );
slouken@7001
   189
slouken@7001
   190
    /* got a hostname? */
slouken@7001
   191
    if ( !local && uri[0] == '/' && uri[2] != '/' ) {
slouken@7001
   192
      char* hostname_end = strchr( uri+1, '/' );
slouken@7001
   193
      if ( hostname_end != NULL ) {
slouken@7001
   194
          char hostname[ 257 ];
slouken@7001
   195
          if ( gethostname( hostname, 255 ) == 0 ) {
slouken@7001
   196
            hostname[ 256 ] = '\0';
slouken@7001
   197
            if ( memcmp( uri+1, hostname, hostname_end - ( uri+1 )) == 0 ) {
slouken@7001
   198
                uri = hostname_end + 1;
slouken@7001
   199
                local = SDL_TRUE;
slouken@7001
   200
            }
slouken@7001
   201
          }
slouken@7001
   202
      }
slouken@7001
   203
    }
slouken@7001
   204
    if ( local ) {
slouken@7001
   205
      file = uri;
slouken@7001
   206
      if ( uri[1] == '/' ) {
slouken@7001
   207
          file++;
slouken@7001
   208
      } else {
slouken@7001
   209
          file--;
slouken@7001
   210
      }
slouken@7001
   211
    }
slouken@7001
   212
    return file;
slouken@7001
   213
}
slouken@6311
   214
slouken@6311
   215
#if SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS
slouken@6311
   216
static void X11_HandleGenericEvent(SDL_VideoData *videodata,XEvent event)
slouken@6311
   217
{
icculus@7402
   218
    /* event is a union, so cookie == &event, but this is type safe. */
icculus@7402
   219
    XGenericEventCookie *cookie = &event.xcookie;
icculus@7827
   220
    if (X11_XGetEventData(videodata->display, cookie)) {
urkle@7214
   221
        X11_HandleXinput2Event(videodata, cookie);
icculus@7827
   222
        X11_XFreeEventData(videodata->display, cookie);
urkle@7214
   223
    }
slouken@6311
   224
}
slouken@6311
   225
#endif /* SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS */
slouken@6311
   226
slouken@6311
   227
slouken@6481
   228
static void
slouken@6504
   229
X11_DispatchFocusIn(SDL_WindowData *data)
slouken@6504
   230
{
slouken@6504
   231
#ifdef DEBUG_XEVENTS
slouken@6504
   232
    printf("window %p: Dispatching FocusIn\n", data);
slouken@6504
   233
#endif
slouken@6504
   234
    SDL_SetKeyboardFocus(data->window);
slouken@6504
   235
#ifdef X_HAVE_UTF8_STRING
slouken@6504
   236
    if (data->ic) {
icculus@7827
   237
        X11_XSetICFocus(data->ic);
slouken@6504
   238
    }
slouken@6504
   239
#endif
slouken@6504
   240
}
slouken@6504
   241
slouken@6504
   242
static void
slouken@6504
   243
X11_DispatchFocusOut(SDL_WindowData *data)
slouken@6504
   244
{
slouken@6504
   245
#ifdef DEBUG_XEVENTS
slouken@6504
   246
    printf("window %p: Dispatching FocusOut\n", data);
slouken@6504
   247
#endif
slouken@6504
   248
    SDL_SetKeyboardFocus(NULL);
slouken@6504
   249
#ifdef X_HAVE_UTF8_STRING
slouken@6504
   250
    if (data->ic) {
icculus@7827
   251
        X11_XUnsetICFocus(data->ic);
slouken@6504
   252
    }
slouken@6504
   253
#endif
slouken@6504
   254
}
slouken@6504
   255
slouken@6504
   256
static void
slouken@6481
   257
X11_DispatchMapNotify(SDL_WindowData *data)
slouken@6481
   258
{
slouken@6481
   259
    SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@6481
   260
    SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@6481
   261
}
slouken@6481
   262
slouken@6481
   263
static void
slouken@6481
   264
X11_DispatchUnmapNotify(SDL_WindowData *data)
slouken@6481
   265
{
slouken@6481
   266
    SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@6481
   267
    SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@6481
   268
}
slouken@6311
   269
slouken@1951
   270
static void
slouken@1951
   271
X11_DispatchEvent(_THIS)
slouken@1951
   272
{
slouken@1951
   273
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@4518
   274
    Display *display = videodata->display;
slouken@1951
   275
    SDL_WindowData *data;
slouken@1951
   276
    XEvent xevent;
slouken@1951
   277
    int i;
gabomdq@7679
   278
    XClientMessageEvent m;
slouken@1951
   279
slouken@1951
   280
    SDL_zero(xevent);           /* valgrind fix. --ryan. */
icculus@7827
   281
    X11_XNextEvent(display, &xevent);
slouken@1951
   282
bob@2325
   283
    /* filter events catchs XIM events and sends them to the correct
bob@2325
   284
       handler */
icculus@7827
   285
    if (X11_XFilterEvent(&xevent, None) == True) {
bob@2327
   286
#if 0
bob@2328
   287
        printf("Filtered event type = %d display = %d window = %d\n",
bob@2328
   288
               xevent.type, xevent.xany.display, xevent.xany.window);
bob@2325
   289
#endif
bob@2325
   290
        return;
bob@2325
   291
    }
bob@2325
   292
slouken@1951
   293
    /* Send a SDL_SYSWMEVENT if the application wants them */
slouken@4429
   294
    if (SDL_GetEventState(SDL_SYSWMEVENT) == SDL_ENABLE) {
slouken@1951
   295
        SDL_SysWMmsg wmmsg;
slouken@1951
   296
slouken@1951
   297
        SDL_VERSION(&wmmsg.version);
slouken@1951
   298
        wmmsg.subsystem = SDL_SYSWM_X11;
slouken@5056
   299
        wmmsg.msg.x11.event = xevent;
slouken@1951
   300
        SDL_SendSysWMEvent(&wmmsg);
slouken@1951
   301
    }
slouken@1951
   302
slouken@6311
   303
#if SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS
slouken@6311
   304
    if(xevent.type == GenericEvent) {
slouken@6311
   305
        X11_HandleGenericEvent(videodata,xevent);
slouken@6311
   306
        return;
slouken@6311
   307
    }
slouken@6311
   308
#endif
slouken@6311
   309
slouken@6476
   310
#if 0
slouken@6476
   311
    printf("type = %d display = %d window = %d\n",
slouken@6476
   312
           xevent.type, xevent.xany.display, xevent.xany.window);
slouken@6476
   313
#endif
slouken@6476
   314
slouken@1951
   315
    data = NULL;
bob@2324
   316
    if (videodata && videodata->windowlist) {
bob@2324
   317
        for (i = 0; i < videodata->numwindows; ++i) {
bob@2324
   318
            if ((videodata->windowlist[i] != NULL) &&
slouken@3685
   319
                (videodata->windowlist[i]->xwindow == xevent.xany.window)) {
bob@2324
   320
                data = videodata->windowlist[i];
bob@2324
   321
                break;
bob@2324
   322
            }
slouken@1951
   323
        }
slouken@1951
   324
    }
slouken@1951
   325
    if (!data) {
slouken@1951
   326
        return;
slouken@1951
   327
    }
slouken@4518
   328
slouken@1951
   329
    switch (xevent.type) {
slouken@1951
   330
slouken@1951
   331
        /* Gaining mouse coverage? */
slouken@1951
   332
    case EnterNotify:{
slouken@1951
   333
#ifdef DEBUG_XEVENTS
slouken@6504
   334
            printf("window %p: EnterNotify! (%d,%d,%d)\n", data,
slouken@4558
   335
                   xevent.xcrossing.x,
slouken@4558
   336
                   xevent.xcrossing.y,
bob@3195
   337
                   xevent.xcrossing.mode);
slouken@1951
   338
            if (xevent.xcrossing.mode == NotifyGrab)
slouken@1951
   339
                printf("Mode: NotifyGrab\n");
slouken@1951
   340
            if (xevent.xcrossing.mode == NotifyUngrab)
slouken@1951
   341
                printf("Mode: NotifyUngrab\n");
slouken@1951
   342
#endif
slouken@4465
   343
            SDL_SetMouseFocus(data->window);
jorgen@7097
   344
jorgen@7097
   345
            if (!SDL_GetMouse()->relative_mode) {
jorgen@7097
   346
                SDL_SendMouseMotion(data->window, 0, 0, xevent.xcrossing.x, xevent.xcrossing.y);
jorgen@7097
   347
            }
slouken@1951
   348
        }
slouken@1951
   349
        break;
slouken@1951
   350
        /* Losing mouse coverage? */
slouken@1951
   351
    case LeaveNotify:{
slouken@1951
   352
#ifdef DEBUG_XEVENTS
slouken@6504
   353
            printf("window %p: LeaveNotify! (%d,%d,%d)\n", data,
slouken@4558
   354
                   xevent.xcrossing.x,
slouken@4558
   355
                   xevent.xcrossing.y,
bob@3195
   356
                   xevent.xcrossing.mode);
slouken@1951
   357
            if (xevent.xcrossing.mode == NotifyGrab)
slouken@1951
   358
                printf("Mode: NotifyGrab\n");
slouken@1951
   359
            if (xevent.xcrossing.mode == NotifyUngrab)
slouken@1951
   360
                printf("Mode: NotifyUngrab\n");
slouken@1951
   361
#endif
jorgen@7089
   362
            if (!SDL_GetMouse()->relative_mode) {
jorgen@7089
   363
                SDL_SendMouseMotion(data->window, 0, 0, xevent.xcrossing.x, xevent.xcrossing.y);
jorgen@7089
   364
            }
jorgen@7089
   365
slouken@4558
   366
            if (xevent.xcrossing.mode != NotifyGrab &&
slouken@4558
   367
                xevent.xcrossing.mode != NotifyUngrab &&
slouken@4558
   368
                xevent.xcrossing.detail != NotifyInferior) {
slouken@4465
   369
                SDL_SetMouseFocus(NULL);
slouken@1951
   370
            }
slouken@1951
   371
        }
slouken@1951
   372
        break;
slouken@1951
   373
slouken@1951
   374
        /* Gaining input focus? */
slouken@1951
   375
    case FocusIn:{
slouken@6660
   376
            if (xevent.xfocus.detail == NotifyInferior) {
slouken@6660
   377
#ifdef DEBUG_XEVENTS
slouken@6660
   378
                printf("window %p: FocusIn (NotifierInferior, ignoring)\n", data);
slouken@6660
   379
#endif
slouken@6660
   380
                break;
slouken@6660
   381
            }
slouken@1951
   382
#ifdef DEBUG_XEVENTS
slouken@6504
   383
            printf("window %p: FocusIn!\n", data);
slouken@1951
   384
#endif
slouken@6661
   385
            if (data->pending_focus == PENDING_FOCUS_OUT &&
slouken@6661
   386
                data->window == SDL_GetKeyboardFocus()) {
slouken@6661
   387
                /* We want to reset the keyboard here, because we may have
slouken@6661
   388
                   missed keyboard messages after our previous FocusOut.
slouken@6661
   389
                 */
slouken@7199
   390
                /* Actually, if we do this we clear the ALT key on Unity
slouken@7199
   391
                   because it briefly takes focus for their dashboard.
slouken@7199
   392
slouken@7199
   393
                   I think it's better to think the ALT key is held down
slouken@7199
   394
                   when it's not, then always lose the ALT modifier on Unity.
slouken@7199
   395
                 */
gabomdq@7678
   396
                /* SDL_ResetKeyboard(); */
slouken@6661
   397
            }
slouken@6504
   398
            data->pending_focus = PENDING_FOCUS_IN;
slouken@6504
   399
            data->pending_focus_time = SDL_GetTicks() + PENDING_FOCUS_IN_TIME;
slouken@1951
   400
        }
slouken@1951
   401
        break;
slouken@1951
   402
slouken@1951
   403
        /* Losing input focus? */
slouken@1951
   404
    case FocusOut:{
slouken@6660
   405
            if (xevent.xfocus.detail == NotifyInferior) {
slouken@6660
   406
                /* We still have focus if a child gets focus */
slouken@6660
   407
#ifdef DEBUG_XEVENTS
slouken@6660
   408
                printf("window %p: FocusOut (NotifierInferior, ignoring)\n", data);
slouken@6660
   409
#endif
slouken@6660
   410
                break;
slouken@6660
   411
            }
slouken@1951
   412
#ifdef DEBUG_XEVENTS
slouken@6504
   413
            printf("window %p: FocusOut!\n", data);
slouken@1951
   414
#endif
slouken@6504
   415
            data->pending_focus = PENDING_FOCUS_OUT;
slouken@6504
   416
            data->pending_focus_time = SDL_GetTicks() + PENDING_FOCUS_OUT_TIME;
slouken@1951
   417
        }
slouken@1951
   418
        break;
slouken@1951
   419
slouken@1951
   420
        /* Generated upon EnterWindow and FocusIn */
slouken@1951
   421
    case KeymapNotify:{
slouken@1951
   422
#ifdef DEBUG_XEVENTS
slouken@6504
   423
            printf("window %p: KeymapNotify!\n", data);
slouken@1951
   424
#endif
slouken@1951
   425
            /* FIXME:
slouken@1951
   426
               X11_SetKeyboardState(SDL_Display, xevent.xkeymap.key_vector);
slouken@1951
   427
             */
slouken@1951
   428
        }
slouken@1951
   429
        break;
slouken@1951
   430
slouken@2305
   431
        /* Has the keyboard layout changed? */
slouken@2305
   432
    case MappingNotify:{
slouken@2305
   433
#ifdef DEBUG_XEVENTS
slouken@6504
   434
            printf("window %p: MappingNotify!\n", data);
slouken@2305
   435
#endif
slouken@2306
   436
            X11_UpdateKeymap(_this);
slouken@2305
   437
        }
slouken@2305
   438
        break;
slouken@2305
   439
slouken@1951
   440
        /* Key press? */
slouken@1951
   441
    case KeyPress:{
slouken@1951
   442
            KeyCode keycode = xevent.xkey.keycode;
bob@2300
   443
            KeySym keysym = NoSymbol;
slouken@2306
   444
            char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];
bob@2325
   445
            Status status = 0;
slouken@1951
   446
slouken@1951
   447
#ifdef DEBUG_XEVENTS
slouken@6504
   448
            printf("window %p: KeyPress (X11 keycode = 0x%X)\n", data, xevent.xkey.keycode);
slouken@1951
   449
#endif
slouken@4465
   450
            SDL_SendKeyboardKey(SDL_PRESSED, videodata->key_layout[keycode]);
slouken@4535
   451
#if 1
slouken@7638
   452
            if (videodata->key_layout[keycode] == SDL_SCANCODE_UNKNOWN && keycode) {
slouken@2305
   453
                int min_keycode, max_keycode;
icculus@7827
   454
                X11_XDisplayKeycodes(display, &min_keycode, &max_keycode);
dimitris@6318
   455
#if SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM
icculus@7827
   456
                keysym = X11_XkbKeycodeToKeysym(display, keycode, 0, 0);
dimitris@6318
   457
#else
slouken@4518
   458
                keysym = XKeycodeToKeysym(display, keycode, 0);
dimitris@6318
   459
#endif
bob@2299
   460
                fprintf(stderr,
slouken@4556
   461
                        "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
   462
                        keycode, keycode - min_keycode, keysym,
icculus@7827
   463
                        X11_XKeysymToString(keysym));
bob@2299
   464
            }
bob@2295
   465
#endif
bob@2325
   466
            /* */
slouken@2305
   467
            SDL_zero(text);
bob@2325
   468
#ifdef X_HAVE_UTF8_STRING
bob@2325
   469
            if (data->ic) {
icculus@7827
   470
                X11_Xutf8LookupString(data->ic, &xevent.xkey, text, sizeof(text),
slouken@2738
   471
                                  &keysym, &status);
bob@2325
   472
            }
bob@2325
   473
#else
slouken@2306
   474
            XLookupString(&xevent.xkey, text, sizeof(text), &keysym, NULL);
bob@2325
   475
#endif
slouken@2305
   476
            if (*text) {
slouken@4465
   477
                SDL_SendKeyboardText(text);
bob@2300
   478
            }
slouken@1951
   479
        }
slouken@1951
   480
        break;
slouken@1951
   481
slouken@1951
   482
        /* Key release? */
slouken@1951
   483
    case KeyRelease:{
slouken@1951
   484
            KeyCode keycode = xevent.xkey.keycode;
slouken@1951
   485
slouken@1951
   486
#ifdef DEBUG_XEVENTS
slouken@6504
   487
            printf("window %p: KeyRelease (X11 keycode = 0x%X)\n", data, xevent.xkey.keycode);
slouken@1951
   488
#endif
slouken@4565
   489
            if (X11_KeyRepeat(display, &xevent)) {
slouken@4565
   490
                /* We're about to get a repeated key down, ignore the key up */
slouken@4565
   491
                break;
slouken@4565
   492
            }
slouken@4465
   493
            SDL_SendKeyboardKey(SDL_RELEASED, videodata->key_layout[keycode]);
slouken@1951
   494
        }
slouken@1951
   495
        break;
slouken@1951
   496
slouken@1951
   497
        /* Have we been iconified? */
slouken@1951
   498
    case UnmapNotify:{
slouken@1951
   499
#ifdef DEBUG_XEVENTS
slouken@6504
   500
            printf("window %p: UnmapNotify!\n", data);
slouken@1951
   501
#endif
slouken@6481
   502
            X11_DispatchUnmapNotify(data);
slouken@1951
   503
        }
slouken@1951
   504
        break;
slouken@1951
   505
slouken@1951
   506
        /* Have we been restored? */
slouken@1951
   507
    case MapNotify:{
slouken@1951
   508
#ifdef DEBUG_XEVENTS
slouken@6504
   509
            printf("window %p: MapNotify!\n", data);
slouken@1951
   510
#endif
slouken@6481
   511
            X11_DispatchMapNotify(data);
slouken@1951
   512
        }
slouken@1951
   513
        break;
slouken@1951
   514
slouken@1951
   515
        /* Have we been resized or moved? */
slouken@1951
   516
    case ConfigureNotify:{
slouken@1951
   517
#ifdef DEBUG_XEVENTS
slouken@6542
   518
            printf("window %p: ConfigureNotify! (position: %d,%d, size: %dx%d)\n", data,
slouken@6542
   519
                   xevent.xconfigure.x, xevent.xconfigure.y,
slouken@1951
   520
                   xevent.xconfigure.width, xevent.xconfigure.height);
slouken@1951
   521
#endif
stapostol@7838
   522
            long border_left = 0;
stapostol@7838
   523
            long border_right = 0;
stapostol@7838
   524
            long border_top = 0;
stapostol@7838
   525
            long border_bottom = 0;
stapostol@7838
   526
            if (data->xwindow) {
slouken@7839
   527
                Atom _net_frame_extents = X11_XInternAtom(display, "_NET_FRAME_EXTENTS", 0);
stapostol@7838
   528
                Atom type;
stapostol@7838
   529
                int format;
stapostol@7838
   530
                unsigned long nitems, bytes_after;
stapostol@7838
   531
                unsigned char *property;
slouken@7839
   532
                X11_XGetWindowProperty(display, data->xwindow,
stapostol@7838
   533
                    _net_frame_extents, 0, 16, 0,
stapostol@7838
   534
                    XA_CARDINAL, &type, &format,
stapostol@7838
   535
                    &nitems, &bytes_after, &property);
stapostol@7838
   536
stapostol@7838
   537
                border_left = ((long*)property)[0];
stapostol@7838
   538
                border_right = ((long*)property)[1];
stapostol@7838
   539
                border_top = ((long*)property)[2];
stapostol@7838
   540
                border_bottom = ((long*)property)[3];
stapostol@7838
   541
            }
stapostol@7838
   542
slouken@6542
   543
            if (xevent.xconfigure.x != data->last_xconfigure.x ||
slouken@6542
   544
                xevent.xconfigure.y != data->last_xconfigure.y) {
slouken@6542
   545
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED,
stapostol@7838
   546
                                    xevent.xconfigure.x - border_left,
stapostol@7838
   547
                                    xevent.xconfigure.y - border_top);
slouken@6542
   548
            }
slouken@6542
   549
            if (xevent.xconfigure.width != data->last_xconfigure.width ||
slouken@6542
   550
                xevent.xconfigure.height != data->last_xconfigure.height) {
slouken@6542
   551
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED,
slouken@6542
   552
                                    xevent.xconfigure.width,
slouken@6542
   553
                                    xevent.xconfigure.height);
slouken@6542
   554
            }
slouken@6542
   555
            data->last_xconfigure = xevent.xconfigure;
slouken@1951
   556
        }
slouken@1951
   557
        break;
slouken@1951
   558
slouken@1951
   559
        /* Have we been requested to quit (or another client message?) */
slouken@1951
   560
    case ClientMessage:{
slouken@7001
   561
slouken@7001
   562
            int xdnd_version=0;
slouken@7191
   563
slouken@7001
   564
            if (xevent.xclient.message_type == videodata->XdndEnter) {
slouken@7001
   565
                SDL_bool use_list = xevent.xclient.data.l[1] & 1;
slouken@7001
   566
                data->xdnd_source = xevent.xclient.data.l[0];
slouken@7001
   567
                xdnd_version = ( xevent.xclient.data.l[1] >> 24);
slouken@7001
   568
                if (use_list) {
slouken@7001
   569
                    /* fetch conversion targets */
slouken@7001
   570
                    SDL_x11Prop p;
slouken@7001
   571
                    X11_ReadProperty(&p, display, data->xdnd_source, videodata->XdndTypeList);
slouken@7001
   572
                    /* pick one */
slouken@7001
   573
                    data->xdnd_req = X11_PickTarget(display, (Atom*)p.data, p.count);
icculus@7827
   574
                    X11_XFree(p.data);
slouken@7001
   575
                } else {
slouken@7001
   576
                    /* pick from list of three */
slouken@7001
   577
                    data->xdnd_req = X11_PickTargetFromAtoms(display, xevent.xclient.data.l[2], xevent.xclient.data.l[3], xevent.xclient.data.l[4]);
slouken@7001
   578
                }
slouken@7001
   579
            }
slouken@7001
   580
            else if (xevent.xclient.message_type == videodata->XdndPosition) {
slouken@7191
   581
slouken@7001
   582
                /* reply with status */
slouken@7001
   583
                memset(&m, 0, sizeof(XClientMessageEvent));
slouken@7001
   584
                m.type = ClientMessage;
slouken@7001
   585
                m.display = xevent.xclient.display;
slouken@7001
   586
                m.window = xevent.xclient.data.l[0];
slouken@7001
   587
                m.message_type = videodata->XdndStatus;
slouken@7001
   588
                m.format=32;
slouken@7001
   589
                m.data.l[0] = data->xwindow;
slouken@7001
   590
                m.data.l[1] = (data->xdnd_req != None);
slouken@7001
   591
                m.data.l[2] = 0; /* specify an empty rectangle */
slouken@7001
   592
                m.data.l[3] = 0;
slouken@7001
   593
                m.data.l[4] = videodata->XdndActionCopy; /* we only accept copying anyway */
slouken@7191
   594
icculus@7827
   595
                X11_XSendEvent(display, xevent.xclient.data.l[0], False, NoEventMask, (XEvent*)&m);
icculus@7827
   596
                X11_XFlush(display);
slouken@7001
   597
            }
slouken@7001
   598
            else if(xevent.xclient.message_type == videodata->XdndDrop) {
slouken@7001
   599
                if (data->xdnd_req == None) {
slouken@7001
   600
                    /* say again - not interested! */
slouken@7001
   601
                    memset(&m, 0, sizeof(XClientMessageEvent));
slouken@7001
   602
                    m.type = ClientMessage;
slouken@7001
   603
                    m.display = xevent.xclient.display;
slouken@7001
   604
                    m.window = xevent.xclient.data.l[0];
slouken@7001
   605
                    m.message_type = videodata->XdndFinished;
slouken@7001
   606
                    m.format=32;
slouken@7001
   607
                    m.data.l[0] = data->xwindow;
slouken@7001
   608
                    m.data.l[1] = 0;
slouken@7001
   609
                    m.data.l[2] = None; /* fail! */
icculus@7827
   610
                    X11_XSendEvent(display, xevent.xclient.data.l[0], False, NoEventMask, (XEvent*)&m);
slouken@7001
   611
                } else {
slouken@7001
   612
                    /* convert */
slouken@7001
   613
                    if(xdnd_version >= 1) {
icculus@7827
   614
                        X11_XConvertSelection(display, videodata->XdndSelection, data->xdnd_req, videodata->PRIMARY, data->xwindow, xevent.xclient.data.l[2]);
slouken@7001
   615
                    } else {
icculus@7827
   616
                        X11_XConvertSelection(display, videodata->XdndSelection, data->xdnd_req, videodata->PRIMARY, data->xwindow, CurrentTime);
slouken@7001
   617
                    }
slouken@7001
   618
                }
slouken@7001
   619
            }
slouken@7001
   620
            else if ((xevent.xclient.message_type == videodata->WM_PROTOCOLS) &&
icculus@6637
   621
                (xevent.xclient.format == 32) &&
icculus@6638
   622
                (xevent.xclient.data.l[0] == videodata->_NET_WM_PING)) {
slouken@6659
   623
                Window root = DefaultRootWindow(display);
icculus@6638
   624
icculus@6638
   625
#ifdef DEBUG_XEVENTS
icculus@6638
   626
                printf("window %p: _NET_WM_PING\n", data);
icculus@6638
   627
#endif
icculus@6638
   628
                xevent.xclient.window = root;
icculus@7827
   629
                X11_XSendEvent(display, root, False, SubstructureRedirectMask | SubstructureNotifyMask, &xevent);
icculus@6638
   630
                break;
icculus@6638
   631
            }
icculus@6638
   632
icculus@6638
   633
            else if ((xevent.xclient.message_type == videodata->WM_PROTOCOLS) &&
icculus@6638
   634
                (xevent.xclient.format == 32) &&
slouken@1951
   635
                (xevent.xclient.data.l[0] == videodata->WM_DELETE_WINDOW)) {
slouken@1951
   636
icculus@6638
   637
#ifdef DEBUG_XEVENTS
icculus@6638
   638
                printf("window %p: WM_DELETE_WINDOW\n", data);
icculus@6638
   639
#endif
slouken@3685
   640
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
icculus@6638
   641
                break;
slouken@1951
   642
            }
slouken@1951
   643
        }
slouken@1951
   644
        break;
slouken@1951
   645
slouken@1951
   646
        /* Do we need to refresh ourselves? */
slouken@1951
   647
    case Expose:{
slouken@1951
   648
#ifdef DEBUG_XEVENTS
slouken@6504
   649
            printf("window %p: Expose (count = %d)\n", data, xevent.xexpose.count);
slouken@1951
   650
#endif
slouken@3685
   651
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
slouken@1951
   652
        }
slouken@1951
   653
        break;
slouken@1951
   654
slouken@4465
   655
    case MotionNotify:{
slouken@7191
   656
            SDL_Mouse *mouse = SDL_GetMouse();
slouken@6311
   657
            if(!mouse->relative_mode) {
slouken@2710
   658
#ifdef DEBUG_MOTION
slouken@6504
   659
                printf("window %p: X11 motion: %d,%d\n", xevent.xmotion.x, xevent.xmotion.y);
slouken@2710
   660
#endif
slouken@6311
   661
slouken@6950
   662
                SDL_SendMouseMotion(data->window, 0, 0, xevent.xmotion.x, xevent.xmotion.y);
slouken@6311
   663
            }
slouken@4465
   664
        }
slouken@4465
   665
        break;
slouken@4465
   666
slouken@4465
   667
    case ButtonPress:{
slouken@5530
   668
            int ticks = 0;
slouken@6950
   669
            if (X11_IsWheelEvent(display,&xevent,&ticks)) {
slouken@6950
   670
                SDL_SendMouseWheel(data->window, 0, 0, ticks);
slouken@6950
   671
            } else {
slouken@6950
   672
                SDL_SendMouseButton(data->window, 0, SDL_PRESSED, xevent.xbutton.button);
slouken@5530
   673
            }
slouken@4465
   674
        }
slouken@4465
   675
        break;
slouken@4465
   676
slouken@4465
   677
    case ButtonRelease:{
slouken@6950
   678
            SDL_SendMouseButton(data->window, 0, SDL_RELEASED, xevent.xbutton.button);
slouken@4465
   679
        }
slouken@4465
   680
        break;
slouken@4465
   681
slouken@4518
   682
    case PropertyNotify:{
slouken@4518
   683
#ifdef DEBUG_XEVENTS
slouken@4520
   684
            unsigned char *propdata;
slouken@4520
   685
            int status, real_format;
slouken@4520
   686
            Atom real_type;
slouken@4520
   687
            unsigned long items_read, items_left, i;
slouken@4520
   688
icculus@7827
   689
            char *name = X11_XGetAtomName(display, xevent.xproperty.atom);
slouken@4518
   690
            if (name) {
slouken@6504
   691
                printf("window %p: PropertyNotify: %s %s\n", data, name, (xevent.xproperty.state == PropertyDelete) ? "deleted" : "changed");
icculus@7827
   692
                X11_XFree(name);
slouken@4518
   693
            }
slouken@4520
   694
icculus@7827
   695
            status = X11_XGetWindowProperty(display, data->xwindow, xevent.xproperty.atom, 0L, 8192L, False, AnyPropertyType, &real_type, &real_format, &items_read, &items_left, &propdata);
slouken@4556
   696
            if (status == Success && items_read > 0) {
slouken@4520
   697
                if (real_type == XA_INTEGER) {
slouken@4520
   698
                    int *values = (int *)propdata;
slouken@4520
   699
slouken@4520
   700
                    printf("{");
slouken@4520
   701
                    for (i = 0; i < items_read; i++) {
slouken@4520
   702
                        printf(" %d", values[i]);
slouken@4520
   703
                    }
slouken@4520
   704
                    printf(" }\n");
slouken@4520
   705
                } else if (real_type == XA_CARDINAL) {
slouken@4520
   706
                    if (real_format == 32) {
slouken@4520
   707
                        Uint32 *values = (Uint32 *)propdata;
slouken@4520
   708
slouken@4520
   709
                        printf("{");
slouken@4520
   710
                        for (i = 0; i < items_read; i++) {
slouken@4520
   711
                            printf(" %d", values[i]);
slouken@4520
   712
                        }
slouken@4520
   713
                        printf(" }\n");
slouken@4520
   714
                    } else if (real_format == 16) {
slouken@4520
   715
                        Uint16 *values = (Uint16 *)propdata;
slouken@4520
   716
slouken@4520
   717
                        printf("{");
slouken@4520
   718
                        for (i = 0; i < items_read; i++) {
slouken@4520
   719
                            printf(" %d", values[i]);
slouken@4520
   720
                        }
slouken@4520
   721
                        printf(" }\n");
slouken@4520
   722
                    } else if (real_format == 8) {
slouken@4520
   723
                        Uint8 *values = (Uint8 *)propdata;
slouken@4520
   724
slouken@4520
   725
                        printf("{");
slouken@4520
   726
                        for (i = 0; i < items_read; i++) {
slouken@4520
   727
                            printf(" %d", values[i]);
slouken@4520
   728
                        }
slouken@4520
   729
                        printf(" }\n");
slouken@4520
   730
                    }
slouken@4520
   731
                } else if (real_type == XA_STRING ||
slouken@4520
   732
                           real_type == videodata->UTF8_STRING) {
slouken@4520
   733
                    printf("{ \"%s\" }\n", propdata);
slouken@4520
   734
                } else if (real_type == XA_ATOM) {
slouken@4520
   735
                    Atom *atoms = (Atom *)propdata;
slouken@4520
   736
slouken@4520
   737
                    printf("{");
slouken@4520
   738
                    for (i = 0; i < items_read; i++) {
icculus@7827
   739
                        char *name = X11_XGetAtomName(display, atoms[i]);
slouken@4520
   740
                        if (name) {
slouken@4520
   741
                            printf(" %s", name);
icculus@7827
   742
                            X11_XFree(name);
slouken@4520
   743
                        }
slouken@4520
   744
                    }
slouken@4520
   745
                    printf(" }\n");
slouken@4520
   746
                } else {
icculus@7827
   747
                    char *name = X11_XGetAtomName(display, real_type);
slouken@4520
   748
                    printf("Unknown type: %ld (%s)\n", real_type, name ? name : "UNKNOWN");
slouken@4520
   749
                    if (name) {
icculus@7827
   750
                        X11_XFree(name);
slouken@4520
   751
                    }
slouken@4520
   752
                }
slouken@4520
   753
            }
slouken@6481
   754
            if (status == Success) {
icculus@7827
   755
                X11_XFree(propdata);
slouken@6481
   756
            }
slouken@6481
   757
#endif /* DEBUG_XEVENTS */
slouken@6481
   758
slouken@6481
   759
            if (xevent.xproperty.atom == data->videodata->_NET_WM_STATE) {
slouken@6481
   760
                /* Get the new state from the window manager.
slouken@6481
   761
                   Compositing window managers can alter visibility of windows
slouken@6481
   762
                   without ever mapping / unmapping them, so we handle that here,
slouken@6481
   763
                   because they use the NETWM protocol to notify us of changes.
slouken@6481
   764
                 */
slouken@6487
   765
                Uint32 flags = X11_GetNetWMState(_this, xevent.xproperty.window);
slouken@6481
   766
                if ((flags^data->window->flags) & SDL_WINDOW_HIDDEN) {
slouken@6481
   767
                    if (flags & SDL_WINDOW_HIDDEN) {
slouken@6481
   768
                        X11_DispatchUnmapNotify(data);
slouken@6481
   769
                    } else {
slouken@6481
   770
                        X11_DispatchMapNotify(data);
slouken@6481
   771
                    }
slouken@6481
   772
                }
slouken@6481
   773
            }
slouken@4518
   774
        }
slouken@4518
   775
        break;
slouken@4518
   776
slouken@4508
   777
    /* Copy the selection from XA_CUT_BUFFER0 to the requested property */
slouken@4508
   778
    case SelectionRequest: {
slouken@4508
   779
            XSelectionRequestEvent *req;
slouken@4508
   780
            XEvent sevent;
slouken@4508
   781
            int seln_format;
slouken@4508
   782
            unsigned long nbytes;
slouken@4508
   783
            unsigned long overflow;
slouken@4508
   784
            unsigned char *seln_data;
slouken@4508
   785
slouken@4508
   786
            req = &xevent.xselectionrequest;
slouken@4508
   787
#ifdef DEBUG_XEVENTS
slouken@6504
   788
            printf("window %p: SelectionRequest (requestor = %ld, target = %ld)\n", data,
slouken@4508
   789
                req->requestor, req->target);
slouken@4508
   790
#endif
slouken@4508
   791
slouken@4524
   792
            SDL_zero(sevent);
slouken@4520
   793
            sevent.xany.type = SelectionNotify;
slouken@4508
   794
            sevent.xselection.selection = req->selection;
slouken@4508
   795
            sevent.xselection.target = None;
slouken@4508
   796
            sevent.xselection.property = None;
slouken@4508
   797
            sevent.xselection.requestor = req->requestor;
slouken@4508
   798
            sevent.xselection.time = req->time;
icculus@7827
   799
            if (X11_XGetWindowProperty(display, DefaultRootWindow(display),
slouken@4508
   800
                    XA_CUT_BUFFER0, 0, INT_MAX/4, False, req->target,
slouken@4508
   801
                    &sevent.xselection.target, &seln_format, &nbytes,
slouken@4508
   802
                    &overflow, &seln_data) == Success) {
icculus@7827
   803
                Atom XA_TARGETS = X11_XInternAtom(display, "TARGETS", 0);
slouken@4508
   804
                if (sevent.xselection.target == req->target) {
icculus@7827
   805
                    X11_XChangeProperty(display, req->requestor, req->property,
slouken@4508
   806
                        sevent.xselection.target, seln_format, PropModeReplace,
slouken@4508
   807
                        seln_data, nbytes);
slouken@4508
   808
                    sevent.xselection.property = req->property;
slouken@7021
   809
                } else if (XA_TARGETS == req->target) {
slouken@7021
   810
                    Atom SupportedFormats[] = { sevent.xselection.target, XA_TARGETS };
icculus@7827
   811
                    X11_XChangeProperty(display, req->requestor, req->property,
slouken@7021
   812
                        XA_ATOM, 32, PropModeReplace,
slouken@7021
   813
                        (unsigned char*)SupportedFormats,
slouken@7021
   814
                        sizeof(SupportedFormats)/sizeof(*SupportedFormats));
slouken@7021
   815
                    sevent.xselection.property = req->property;
slouken@4508
   816
                }
icculus@7827
   817
                X11_XFree(seln_data);
slouken@4508
   818
            }
icculus@7827
   819
            X11_XSendEvent(display, req->requestor, False, 0, &sevent);
icculus@7827
   820
            X11_XSync(display, False);
slouken@4508
   821
        }
slouken@4508
   822
        break;
slouken@4508
   823
slouken@4508
   824
    case SelectionNotify: {
slouken@4508
   825
#ifdef DEBUG_XEVENTS
slouken@6504
   826
            printf("window %p: SelectionNotify (requestor = %ld, target = %ld)\n", data,
slouken@4508
   827
                xevent.xselection.requestor, xevent.xselection.target);
slouken@4508
   828
#endif
slouken@7001
   829
            Atom target = xevent.xselection.target;
slouken@7001
   830
            if (target == data->xdnd_req) {
slouken@7001
   831
                /* read data */
slouken@7001
   832
                SDL_x11Prop p;
slouken@7001
   833
                X11_ReadProperty(&p, display, data->xwindow, videodata->PRIMARY);
slouken@7191
   834
slouken@7396
   835
                if (p.format == 8) {
slouken@7001
   836
                    SDL_bool expect_lf = SDL_FALSE;
slouken@7001
   837
                    char *start = NULL;
slouken@7001
   838
                    char *scan = (char*)p.data;
slouken@7001
   839
                    char *fn;
slouken@7001
   840
                    char *uri;
slouken@7001
   841
                    int length = 0;
slouken@7001
   842
                    while (p.count--) {
slouken@7001
   843
                        if (!expect_lf) {
slouken@7396
   844
                            if (*scan == 0x0D) {
slouken@7001
   845
                                expect_lf = SDL_TRUE;
slouken@7396
   846
                            }
slouken@7396
   847
                            if (start == NULL) {
slouken@7001
   848
                                start = scan;
slouken@7001
   849
                                length = 0;
slouken@7001
   850
                            }
slouken@7001
   851
                            length++;
slouken@7001
   852
                        } else {
slouken@7396
   853
                            if (*scan == 0x0A && length > 0) {
slouken@7396
   854
                                uri = SDL_malloc(length--);
slouken@7396
   855
                                SDL_memcpy(uri, start, length);
slouken@7396
   856
                                uri[length] = '\0';
slouken@7001
   857
                                fn = X11_URIToLocal(uri);
slouken@7396
   858
                                if (fn) {
slouken@7396
   859
                                    SDL_SendDropFile(fn);
slouken@7396
   860
                                }
slouken@7396
   861
                                SDL_free(uri);
slouken@7001
   862
                            }
slouken@7001
   863
                            expect_lf = SDL_FALSE;
slouken@7001
   864
                            start = NULL;
slouken@7001
   865
                        }
slouken@7001
   866
                        scan++;
slouken@7001
   867
                    }
slouken@7001
   868
                }
slouken@7191
   869
icculus@7827
   870
                X11_XFree(p.data);
slouken@7191
   871
slouken@7001
   872
                /* send reply */
slouken@7396
   873
                SDL_memset(&m, 0, sizeof(XClientMessageEvent));
slouken@7001
   874
                m.type = ClientMessage;
slouken@7001
   875
                m.display = display;
slouken@7001
   876
                m.window = data->xdnd_source;
slouken@7001
   877
                m.message_type = videodata->XdndFinished;
slouken@7396
   878
                m.format = 32;
slouken@7001
   879
                m.data.l[0] = data->xwindow;
slouken@7001
   880
                m.data.l[1] = 1;
slouken@7001
   881
                m.data.l[2] = videodata->XdndActionCopy;
icculus@7827
   882
                X11_XSendEvent(display, data->xdnd_source, False, NoEventMask, (XEvent*)&m);
slouken@7191
   883
icculus@7827
   884
                X11_XSync(display, False);
slouken@7191
   885
slouken@7001
   886
            } else {
slouken@7001
   887
                videodata->selection_waiting = SDL_FALSE;
slouken@7001
   888
            }
slouken@4508
   889
        }
slouken@4508
   890
        break;
slouken@4508
   891
slouken@4465
   892
    default:{
slouken@1951
   893
#ifdef DEBUG_XEVENTS
slouken@6504
   894
            printf("window %p: Unhandled event %d\n", data, xevent.type);
slouken@1951
   895
#endif
slouken@1951
   896
        }
slouken@1951
   897
        break;
slouken@1951
   898
    }
slouken@1951
   899
}
slouken@1951
   900
slouken@6504
   901
static void
slouken@6504
   902
X11_HandleFocusChanges(_THIS)
slouken@6504
   903
{
slouken@6504
   904
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@6504
   905
    int i;
slouken@6504
   906
slouken@6504
   907
    if (videodata && videodata->windowlist) {
slouken@6504
   908
        for (i = 0; i < videodata->numwindows; ++i) {
slouken@6504
   909
            SDL_WindowData *data = videodata->windowlist[i];
slouken@6504
   910
            if (data && data->pending_focus != PENDING_FOCUS_NONE) {
slouken@6504
   911
                Uint32 now = SDL_GetTicks();
slouken@7857
   912
                if (SDL_TICKS_PASSED(now, data->pending_focus_time)) {
slouken@6504
   913
                    if ( data->pending_focus == PENDING_FOCUS_IN ) {
slouken@6504
   914
                        X11_DispatchFocusIn(data);
slouken@6504
   915
                    } else {
slouken@6504
   916
                        X11_DispatchFocusOut(data);
slouken@6504
   917
                    }
slouken@6504
   918
                    data->pending_focus = PENDING_FOCUS_NONE;
slouken@6504
   919
                }
slouken@6504
   920
            }
slouken@6504
   921
        }
slouken@6504
   922
    }
slouken@6504
   923
}
icculus@7827
   924
/* Ack!  X11_XPending() actually performs a blocking read if no events available */
slouken@4472
   925
static int
slouken@1951
   926
X11_Pending(Display * display)
slouken@1951
   927
{
slouken@1951
   928
    /* Flush the display connection and look to see if events are queued */
icculus@7827
   929
    X11_XFlush(display);
icculus@7827
   930
    if (X11_XEventsQueued(display, QueuedAlready)) {
slouken@1951
   931
        return (1);
slouken@1951
   932
    }
slouken@1951
   933
slouken@1951
   934
    /* More drastic measures are required -- see if X is ready to talk */
slouken@1951
   935
    {
slouken@1951
   936
        static struct timeval zero_time;        /* static == 0 */
slouken@1951
   937
        int x11_fd;
slouken@1951
   938
        fd_set fdset;
slouken@1951
   939
slouken@1951
   940
        x11_fd = ConnectionNumber(display);
slouken@1951
   941
        FD_ZERO(&fdset);
slouken@1951
   942
        FD_SET(x11_fd, &fdset);
slouken@1951
   943
        if (select(x11_fd + 1, &fdset, NULL, NULL, &zero_time) == 1) {
icculus@7827
   944
            return (X11_XPending(display));
slouken@1951
   945
        }
slouken@1951
   946
    }
slouken@1951
   947
slouken@1951
   948
    /* Oh well, nothing is ready .. */
slouken@1951
   949
    return (0);
slouken@1951
   950
}
slouken@1951
   951
icculus@5981
   952
icculus@5981
   953
/* !!! FIXME: this should be exposed in a header, or something. */
icculus@5981
   954
int SDL_GetNumTouch(void);
icculus@6774
   955
void SDL_dbus_screensaver_tickle(_THIS);
icculus@5981
   956
slouken@1951
   957
void
slouken@1951
   958
X11_PumpEvents(_THIS)
slouken@1951
   959
{
slouken@1951
   960
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   961
slouken@3025
   962
    /* Update activity every 30 seconds to prevent screensaver */
slouken@3025
   963
    if (_this->suspend_screensaver) {
slouken@3025
   964
        Uint32 now = SDL_GetTicks();
slouken@3025
   965
        if (!data->screensaver_activity ||
slouken@7857
   966
            SDL_TICKS_PASSED(now, data->screensaver_activity + 30000)) {
icculus@7827
   967
            X11_XResetScreenSaver(data->display);
icculus@6774
   968
icculus@6774
   969
            #if SDL_USE_LIBDBUS
icculus@6774
   970
            SDL_dbus_screensaver_tickle(_this);
icculus@6774
   971
            #endif
icculus@6774
   972
slouken@3025
   973
            data->screensaver_activity = now;
slouken@3025
   974
        }
slouken@7191
   975
    }
slouken@3025
   976
slouken@1951
   977
    /* Keep processing pending events */
slouken@1951
   978
    while (X11_Pending(data->display)) {
slouken@1951
   979
        X11_DispatchEvent(_this);
slouken@1951
   980
    }
slouken@6504
   981
slouken@6504
   982
    /* FIXME: Only need to do this when there are pending focus changes */
slouken@6504
   983
    X11_HandleFocusChanges(_this);
slouken@1951
   984
}
slouken@1951
   985
slouken@3030
   986
slouken@1951
   987
void
slouken@3025
   988
X11_SuspendScreenSaver(_THIS)
slouken@1951
   989
{
slouken@5408
   990
#if SDL_VIDEO_DRIVER_X11_XSCRNSAVER
slouken@3025
   991
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@3025
   992
    int dummy;
slouken@3025
   993
    int major_version, minor_version;
slouken@1951
   994
slouken@3030
   995
    if (SDL_X11_HAVE_XSS) {
icculus@7827
   996
        /* X11_XScreenSaverSuspend was introduced in MIT-SCREEN-SAVER 1.1 */
icculus@7827
   997
        if (!X11_XScreenSaverQueryExtension(data->display, &dummy, &dummy) ||
icculus@7827
   998
            !X11_XScreenSaverQueryVersion(data->display,
slouken@3030
   999
                                      &major_version, &minor_version) ||
slouken@3030
  1000
            major_version < 1 || (major_version == 1 && minor_version < 1)) {
slouken@3030
  1001
            return;
slouken@3030
  1002
        }
slouken@1951
  1003
icculus@7827
  1004
        X11_XScreenSaverSuspend(data->display, _this->suspend_screensaver);
icculus@7827
  1005
        X11_XResetScreenSaver(data->display);
slouken@1951
  1006
    }
slouken@3030
  1007
#endif
slouken@1951
  1008
icculus@6774
  1009
#if SDL_USE_LIBDBUS
slouken@3030
  1010
    if (_this->suspend_screensaver) {
icculus@6774
  1011
        SDL_dbus_screensaver_tickle(_this);
slouken@3030
  1012
    }
slouken@3025
  1013
#endif
slouken@1951
  1014
}
slouken@1951
  1015
slouken@5481
  1016
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
  1017
slouken@1951
  1018
/* vi: set ts=4 sw=4 expandtab: */