src/video/x11/SDL_x11events.c
author Sam Lantinga
Sun, 02 Feb 2014 00:53:27 -0800
changeset 8149 681eb46b8ac4
parent 8093 b43765095a6f
child 8229 437c4e5961e0
permissions -rw-r--r--
Fixed bug 2374 - Update copyright for 2014...

Is it that time already??
slouken@1951
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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
*/
icculus@8093
    21
#include "../../SDL_internal.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@7902
   248
    /* If another window has already processed a focus in, then don't try to
slouken@7902
   249
     * remove focus here.  Doing so will incorrectly remove focus from that
slouken@7902
   250
     * window, and the focus lost event for this window will have already
slouken@7902
   251
     * been dispatched anyway. */
slouken@7902
   252
    if (data->window == SDL_GetKeyboardFocus()) {
slouken@7902
   253
        SDL_SetKeyboardFocus(NULL);
slouken@7902
   254
    }
slouken@6504
   255
#ifdef X_HAVE_UTF8_STRING
slouken@6504
   256
    if (data->ic) {
icculus@7827
   257
        X11_XUnsetICFocus(data->ic);
slouken@6504
   258
    }
slouken@6504
   259
#endif
slouken@6504
   260
}
slouken@6504
   261
slouken@6504
   262
static void
slouken@6481
   263
X11_DispatchMapNotify(SDL_WindowData *data)
slouken@6481
   264
{
slouken@6481
   265
    SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@6481
   266
    SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@6481
   267
}
slouken@6481
   268
slouken@6481
   269
static void
slouken@6481
   270
X11_DispatchUnmapNotify(SDL_WindowData *data)
slouken@6481
   271
{
slouken@6481
   272
    SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@6481
   273
    SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@6481
   274
}
slouken@6311
   275
slouken@1951
   276
static void
slouken@1951
   277
X11_DispatchEvent(_THIS)
slouken@1951
   278
{
slouken@1951
   279
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@4518
   280
    Display *display = videodata->display;
slouken@1951
   281
    SDL_WindowData *data;
slouken@1951
   282
    XEvent xevent;
slouken@7943
   283
    int orig_event_type;
slouken@7943
   284
    KeyCode orig_keycode;
slouken@7943
   285
    XClientMessageEvent m;
slouken@1951
   286
    int i;
slouken@1951
   287
slouken@1951
   288
    SDL_zero(xevent);           /* valgrind fix. --ryan. */
icculus@7827
   289
    X11_XNextEvent(display, &xevent);
slouken@1951
   290
slouken@7943
   291
    /* Save the original keycode for dead keys, which are filtered out by
slouken@7943
   292
       the XFilterEvent() call below.
slouken@7943
   293
    */
slouken@7943
   294
    orig_event_type = xevent.type;
slouken@7943
   295
    if (orig_event_type == KeyPress || orig_event_type == KeyRelease) {
slouken@7943
   296
        orig_keycode = xevent.xkey.keycode;
slouken@7943
   297
    } else {
slouken@7943
   298
        orig_keycode = 0;
slouken@7943
   299
    }
slouken@7943
   300
slouken@7943
   301
    /* filter events catchs XIM events and sends them to the correct handler */
icculus@7827
   302
    if (X11_XFilterEvent(&xevent, None) == True) {
bob@2327
   303
#if 0
bob@2328
   304
        printf("Filtered event type = %d display = %d window = %d\n",
bob@2328
   305
               xevent.type, xevent.xany.display, xevent.xany.window);
bob@2325
   306
#endif
slouken@7943
   307
        if (orig_keycode) {
slouken@7943
   308
            /* Make sure dead key press/release events are sent */
slouken@7943
   309
            SDL_Scancode scancode = videodata->key_layout[orig_keycode];
slouken@7943
   310
            if (orig_event_type == KeyPress) {
slouken@7943
   311
                SDL_SendKeyboardKey(SDL_PRESSED, scancode);
slouken@7943
   312
            } else {
slouken@7943
   313
                SDL_SendKeyboardKey(SDL_RELEASED, scancode);
slouken@7943
   314
            }
slouken@7943
   315
        }
bob@2325
   316
        return;
bob@2325
   317
    }
bob@2325
   318
slouken@1951
   319
    /* Send a SDL_SYSWMEVENT if the application wants them */
slouken@4429
   320
    if (SDL_GetEventState(SDL_SYSWMEVENT) == SDL_ENABLE) {
slouken@1951
   321
        SDL_SysWMmsg wmmsg;
slouken@1951
   322
slouken@1951
   323
        SDL_VERSION(&wmmsg.version);
slouken@1951
   324
        wmmsg.subsystem = SDL_SYSWM_X11;
slouken@5056
   325
        wmmsg.msg.x11.event = xevent;
slouken@1951
   326
        SDL_SendSysWMEvent(&wmmsg);
slouken@1951
   327
    }
slouken@1951
   328
slouken@6311
   329
#if SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS
slouken@6311
   330
    if(xevent.type == GenericEvent) {
slouken@6311
   331
        X11_HandleGenericEvent(videodata,xevent);
slouken@6311
   332
        return;
slouken@6311
   333
    }
slouken@6311
   334
#endif
slouken@6311
   335
slouken@6476
   336
#if 0
slouken@6476
   337
    printf("type = %d display = %d window = %d\n",
slouken@6476
   338
           xevent.type, xevent.xany.display, xevent.xany.window);
slouken@6476
   339
#endif
slouken@6476
   340
slouken@1951
   341
    data = NULL;
bob@2324
   342
    if (videodata && videodata->windowlist) {
bob@2324
   343
        for (i = 0; i < videodata->numwindows; ++i) {
bob@2324
   344
            if ((videodata->windowlist[i] != NULL) &&
slouken@3685
   345
                (videodata->windowlist[i]->xwindow == xevent.xany.window)) {
bob@2324
   346
                data = videodata->windowlist[i];
bob@2324
   347
                break;
bob@2324
   348
            }
slouken@1951
   349
        }
slouken@1951
   350
    }
slouken@1951
   351
    if (!data) {
slouken@1951
   352
        return;
slouken@1951
   353
    }
slouken@4518
   354
slouken@1951
   355
    switch (xevent.type) {
slouken@1951
   356
slouken@1951
   357
        /* Gaining mouse coverage? */
slouken@1951
   358
    case EnterNotify:{
slouken@1951
   359
#ifdef DEBUG_XEVENTS
slouken@6504
   360
            printf("window %p: EnterNotify! (%d,%d,%d)\n", data,
slouken@4558
   361
                   xevent.xcrossing.x,
slouken@4558
   362
                   xevent.xcrossing.y,
bob@3195
   363
                   xevent.xcrossing.mode);
slouken@1951
   364
            if (xevent.xcrossing.mode == NotifyGrab)
slouken@1951
   365
                printf("Mode: NotifyGrab\n");
slouken@1951
   366
            if (xevent.xcrossing.mode == NotifyUngrab)
slouken@1951
   367
                printf("Mode: NotifyUngrab\n");
slouken@1951
   368
#endif
slouken@4465
   369
            SDL_SetMouseFocus(data->window);
jorgen@7097
   370
jorgen@7097
   371
            if (!SDL_GetMouse()->relative_mode) {
jorgen@7097
   372
                SDL_SendMouseMotion(data->window, 0, 0, xevent.xcrossing.x, xevent.xcrossing.y);
jorgen@7097
   373
            }
slouken@1951
   374
        }
slouken@1951
   375
        break;
slouken@1951
   376
        /* Losing mouse coverage? */
slouken@1951
   377
    case LeaveNotify:{
slouken@1951
   378
#ifdef DEBUG_XEVENTS
slouken@6504
   379
            printf("window %p: LeaveNotify! (%d,%d,%d)\n", data,
slouken@4558
   380
                   xevent.xcrossing.x,
slouken@4558
   381
                   xevent.xcrossing.y,
bob@3195
   382
                   xevent.xcrossing.mode);
slouken@1951
   383
            if (xevent.xcrossing.mode == NotifyGrab)
slouken@1951
   384
                printf("Mode: NotifyGrab\n");
slouken@1951
   385
            if (xevent.xcrossing.mode == NotifyUngrab)
slouken@1951
   386
                printf("Mode: NotifyUngrab\n");
slouken@1951
   387
#endif
jorgen@7089
   388
            if (!SDL_GetMouse()->relative_mode) {
jorgen@7089
   389
                SDL_SendMouseMotion(data->window, 0, 0, xevent.xcrossing.x, xevent.xcrossing.y);
jorgen@7089
   390
            }
jorgen@7089
   391
slouken@4558
   392
            if (xevent.xcrossing.mode != NotifyGrab &&
slouken@4558
   393
                xevent.xcrossing.mode != NotifyUngrab &&
slouken@4558
   394
                xevent.xcrossing.detail != NotifyInferior) {
slouken@4465
   395
                SDL_SetMouseFocus(NULL);
slouken@1951
   396
            }
slouken@1951
   397
        }
slouken@1951
   398
        break;
slouken@1951
   399
slouken@1951
   400
        /* Gaining input focus? */
slouken@1951
   401
    case FocusIn:{
gabomdq@7945
   402
            if (xevent.xfocus.mode == NotifyGrab || xevent.xfocus.mode == NotifyUngrab) {
gabomdq@7945
   403
                /* Someone is handling a global hotkey, ignore it */
gabomdq@7945
   404
#ifdef DEBUG_XEVENTS
gabomdq@7945
   405
                printf("window %p: FocusIn (NotifyGrab/NotifyUngrab, ignoring)\n", data);
gabomdq@7945
   406
#endif
gabomdq@7945
   407
                break;
gabomdq@7945
   408
            }
gabomdq@7945
   409
slouken@6660
   410
            if (xevent.xfocus.detail == NotifyInferior) {
slouken@6660
   411
#ifdef DEBUG_XEVENTS
slouken@6660
   412
                printf("window %p: FocusIn (NotifierInferior, ignoring)\n", data);
slouken@6660
   413
#endif
slouken@6660
   414
                break;
slouken@6660
   415
            }
slouken@1951
   416
#ifdef DEBUG_XEVENTS
slouken@6504
   417
            printf("window %p: FocusIn!\n", data);
slouken@1951
   418
#endif
slouken@6661
   419
            if (data->pending_focus == PENDING_FOCUS_OUT &&
slouken@6661
   420
                data->window == SDL_GetKeyboardFocus()) {
slouken@6661
   421
                /* We want to reset the keyboard here, because we may have
slouken@6661
   422
                   missed keyboard messages after our previous FocusOut.
slouken@6661
   423
                 */
slouken@7199
   424
                /* Actually, if we do this we clear the ALT key on Unity
slouken@7199
   425
                   because it briefly takes focus for their dashboard.
slouken@7199
   426
slouken@7199
   427
                   I think it's better to think the ALT key is held down
slouken@7199
   428
                   when it's not, then always lose the ALT modifier on Unity.
slouken@7199
   429
                 */
gabomdq@7678
   430
                /* SDL_ResetKeyboard(); */
slouken@6661
   431
            }
slouken@6504
   432
            data->pending_focus = PENDING_FOCUS_IN;
slouken@6504
   433
            data->pending_focus_time = SDL_GetTicks() + PENDING_FOCUS_IN_TIME;
slouken@1951
   434
        }
slouken@1951
   435
        break;
slouken@1951
   436
slouken@1951
   437
        /* Losing input focus? */
slouken@1951
   438
    case FocusOut:{
gabomdq@7945
   439
            if (xevent.xfocus.mode == NotifyGrab || xevent.xfocus.mode == NotifyUngrab) {
slouken@7942
   440
                /* Someone is handling a global hotkey, ignore it */
slouken@7942
   441
#ifdef DEBUG_XEVENTS
gabomdq@7945
   442
                printf("window %p: FocusOut (NotifyGrab/NotifyUngrab, ignoring)\n", data);
slouken@7942
   443
#endif
slouken@7942
   444
                break;
slouken@7942
   445
            }
slouken@6660
   446
            if (xevent.xfocus.detail == NotifyInferior) {
slouken@6660
   447
                /* We still have focus if a child gets focus */
slouken@6660
   448
#ifdef DEBUG_XEVENTS
slouken@6660
   449
                printf("window %p: FocusOut (NotifierInferior, ignoring)\n", data);
slouken@6660
   450
#endif
slouken@6660
   451
                break;
slouken@6660
   452
            }
slouken@1951
   453
#ifdef DEBUG_XEVENTS
slouken@6504
   454
            printf("window %p: FocusOut!\n", data);
slouken@1951
   455
#endif
slouken@6504
   456
            data->pending_focus = PENDING_FOCUS_OUT;
slouken@6504
   457
            data->pending_focus_time = SDL_GetTicks() + PENDING_FOCUS_OUT_TIME;
slouken@1951
   458
        }
slouken@1951
   459
        break;
slouken@1951
   460
slouken@1951
   461
        /* Generated upon EnterWindow and FocusIn */
slouken@1951
   462
    case KeymapNotify:{
slouken@1951
   463
#ifdef DEBUG_XEVENTS
slouken@6504
   464
            printf("window %p: KeymapNotify!\n", data);
slouken@1951
   465
#endif
slouken@1951
   466
            /* FIXME:
slouken@1951
   467
               X11_SetKeyboardState(SDL_Display, xevent.xkeymap.key_vector);
slouken@1951
   468
             */
slouken@1951
   469
        }
slouken@1951
   470
        break;
slouken@1951
   471
slouken@2305
   472
        /* Has the keyboard layout changed? */
slouken@2305
   473
    case MappingNotify:{
slouken@2305
   474
#ifdef DEBUG_XEVENTS
slouken@6504
   475
            printf("window %p: MappingNotify!\n", data);
slouken@2305
   476
#endif
slouken@2306
   477
            X11_UpdateKeymap(_this);
slouken@2305
   478
        }
slouken@2305
   479
        break;
slouken@2305
   480
slouken@1951
   481
        /* Key press? */
slouken@1951
   482
    case KeyPress:{
slouken@1951
   483
            KeyCode keycode = xevent.xkey.keycode;
bob@2300
   484
            KeySym keysym = NoSymbol;
slouken@2306
   485
            char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];
bob@2325
   486
            Status status = 0;
slouken@1951
   487
slouken@1951
   488
#ifdef DEBUG_XEVENTS
slouken@6504
   489
            printf("window %p: KeyPress (X11 keycode = 0x%X)\n", data, xevent.xkey.keycode);
slouken@1951
   490
#endif
slouken@4465
   491
            SDL_SendKeyboardKey(SDL_PRESSED, videodata->key_layout[keycode]);
slouken@4535
   492
#if 1
slouken@7638
   493
            if (videodata->key_layout[keycode] == SDL_SCANCODE_UNKNOWN && keycode) {
slouken@2305
   494
                int min_keycode, max_keycode;
icculus@7827
   495
                X11_XDisplayKeycodes(display, &min_keycode, &max_keycode);
dimitris@6318
   496
#if SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM
icculus@7827
   497
                keysym = X11_XkbKeycodeToKeysym(display, keycode, 0, 0);
dimitris@6318
   498
#else
slouken@4518
   499
                keysym = XKeycodeToKeysym(display, keycode, 0);
dimitris@6318
   500
#endif
bob@2299
   501
                fprintf(stderr,
slouken@4556
   502
                        "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
   503
                        keycode, keycode - min_keycode, keysym,
icculus@7827
   504
                        X11_XKeysymToString(keysym));
bob@2299
   505
            }
bob@2295
   506
#endif
bob@2325
   507
            /* */
slouken@2305
   508
            SDL_zero(text);
bob@2325
   509
#ifdef X_HAVE_UTF8_STRING
bob@2325
   510
            if (data->ic) {
icculus@7827
   511
                X11_Xutf8LookupString(data->ic, &xevent.xkey, text, sizeof(text),
slouken@2738
   512
                                  &keysym, &status);
bob@2325
   513
            }
bob@2325
   514
#else
slouken@2306
   515
            XLookupString(&xevent.xkey, text, sizeof(text), &keysym, NULL);
bob@2325
   516
#endif
slouken@2305
   517
            if (*text) {
slouken@4465
   518
                SDL_SendKeyboardText(text);
bob@2300
   519
            }
slouken@1951
   520
        }
slouken@1951
   521
        break;
slouken@1951
   522
slouken@1951
   523
        /* Key release? */
slouken@1951
   524
    case KeyRelease:{
slouken@1951
   525
            KeyCode keycode = xevent.xkey.keycode;
slouken@1951
   526
slouken@1951
   527
#ifdef DEBUG_XEVENTS
slouken@6504
   528
            printf("window %p: KeyRelease (X11 keycode = 0x%X)\n", data, xevent.xkey.keycode);
slouken@1951
   529
#endif
slouken@4565
   530
            if (X11_KeyRepeat(display, &xevent)) {
slouken@4565
   531
                /* We're about to get a repeated key down, ignore the key up */
slouken@4565
   532
                break;
slouken@4565
   533
            }
slouken@4465
   534
            SDL_SendKeyboardKey(SDL_RELEASED, videodata->key_layout[keycode]);
slouken@1951
   535
        }
slouken@1951
   536
        break;
slouken@1951
   537
slouken@1951
   538
        /* Have we been iconified? */
slouken@1951
   539
    case UnmapNotify:{
slouken@1951
   540
#ifdef DEBUG_XEVENTS
slouken@6504
   541
            printf("window %p: UnmapNotify!\n", data);
slouken@1951
   542
#endif
slouken@6481
   543
            X11_DispatchUnmapNotify(data);
slouken@1951
   544
        }
slouken@1951
   545
        break;
slouken@1951
   546
slouken@1951
   547
        /* Have we been restored? */
slouken@1951
   548
    case MapNotify:{
slouken@1951
   549
#ifdef DEBUG_XEVENTS
slouken@6504
   550
            printf("window %p: MapNotify!\n", data);
slouken@1951
   551
#endif
slouken@6481
   552
            X11_DispatchMapNotify(data);
slouken@1951
   553
        }
slouken@1951
   554
        break;
slouken@1951
   555
slouken@1951
   556
        /* Have we been resized or moved? */
slouken@1951
   557
    case ConfigureNotify:{
slouken@1951
   558
#ifdef DEBUG_XEVENTS
slouken@6542
   559
            printf("window %p: ConfigureNotify! (position: %d,%d, size: %dx%d)\n", data,
slouken@6542
   560
                   xevent.xconfigure.x, xevent.xconfigure.y,
slouken@1951
   561
                   xevent.xconfigure.width, xevent.xconfigure.height);
slouken@1951
   562
#endif
stapostol@7838
   563
            long border_left = 0;
stapostol@7838
   564
            long border_right = 0;
stapostol@7838
   565
            long border_top = 0;
stapostol@7838
   566
            long border_bottom = 0;
stapostol@7838
   567
            if (data->xwindow) {
slouken@7839
   568
                Atom _net_frame_extents = X11_XInternAtom(display, "_NET_FRAME_EXTENTS", 0);
pgriffais@7885
   569
                Atom type = None;
stapostol@7838
   570
                int format;
pgriffais@7885
   571
                unsigned long nitems = 0, bytes_after;
stapostol@7838
   572
                unsigned char *property;
slouken@7839
   573
                X11_XGetWindowProperty(display, data->xwindow,
stapostol@7838
   574
                    _net_frame_extents, 0, 16, 0,
stapostol@7838
   575
                    XA_CARDINAL, &type, &format,
stapostol@7838
   576
                    &nitems, &bytes_after, &property);
stapostol@7838
   577
pgriffais@7885
   578
                if (type != None && nitems == 4)
pgriffais@7885
   579
                {
pgriffais@7885
   580
                    border_left = ((long*)property)[0];
pgriffais@7885
   581
                    border_right = ((long*)property)[1];
pgriffais@7885
   582
                    border_top = ((long*)property)[2];
pgriffais@7885
   583
                    border_bottom = ((long*)property)[3];
pgriffais@7885
   584
                }
stapostol@7838
   585
            }
stapostol@7838
   586
slouken@6542
   587
            if (xevent.xconfigure.x != data->last_xconfigure.x ||
slouken@6542
   588
                xevent.xconfigure.y != data->last_xconfigure.y) {
slouken@6542
   589
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED,
stapostol@7838
   590
                                    xevent.xconfigure.x - border_left,
stapostol@7838
   591
                                    xevent.xconfigure.y - border_top);
slouken@6542
   592
            }
slouken@6542
   593
            if (xevent.xconfigure.width != data->last_xconfigure.width ||
slouken@6542
   594
                xevent.xconfigure.height != data->last_xconfigure.height) {
slouken@6542
   595
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED,
slouken@6542
   596
                                    xevent.xconfigure.width,
slouken@6542
   597
                                    xevent.xconfigure.height);
slouken@6542
   598
            }
slouken@6542
   599
            data->last_xconfigure = xevent.xconfigure;
slouken@1951
   600
        }
slouken@1951
   601
        break;
slouken@1951
   602
slouken@1951
   603
        /* Have we been requested to quit (or another client message?) */
slouken@1951
   604
    case ClientMessage:{
slouken@7001
   605
slouken@7001
   606
            int xdnd_version=0;
slouken@7191
   607
slouken@7001
   608
            if (xevent.xclient.message_type == videodata->XdndEnter) {
slouken@7001
   609
                SDL_bool use_list = xevent.xclient.data.l[1] & 1;
slouken@7001
   610
                data->xdnd_source = xevent.xclient.data.l[0];
slouken@7001
   611
                xdnd_version = ( xevent.xclient.data.l[1] >> 24);
slouken@7001
   612
                if (use_list) {
slouken@7001
   613
                    /* fetch conversion targets */
slouken@7001
   614
                    SDL_x11Prop p;
slouken@7001
   615
                    X11_ReadProperty(&p, display, data->xdnd_source, videodata->XdndTypeList);
slouken@7001
   616
                    /* pick one */
slouken@7001
   617
                    data->xdnd_req = X11_PickTarget(display, (Atom*)p.data, p.count);
icculus@7827
   618
                    X11_XFree(p.data);
slouken@7001
   619
                } else {
slouken@7001
   620
                    /* pick from list of three */
slouken@7001
   621
                    data->xdnd_req = X11_PickTargetFromAtoms(display, xevent.xclient.data.l[2], xevent.xclient.data.l[3], xevent.xclient.data.l[4]);
slouken@7001
   622
                }
slouken@7001
   623
            }
slouken@7001
   624
            else if (xevent.xclient.message_type == videodata->XdndPosition) {
slouken@7191
   625
slouken@7001
   626
                /* reply with status */
slouken@7001
   627
                memset(&m, 0, sizeof(XClientMessageEvent));
slouken@7001
   628
                m.type = ClientMessage;
slouken@7001
   629
                m.display = xevent.xclient.display;
slouken@7001
   630
                m.window = xevent.xclient.data.l[0];
slouken@7001
   631
                m.message_type = videodata->XdndStatus;
slouken@7001
   632
                m.format=32;
slouken@7001
   633
                m.data.l[0] = data->xwindow;
slouken@7001
   634
                m.data.l[1] = (data->xdnd_req != None);
slouken@7001
   635
                m.data.l[2] = 0; /* specify an empty rectangle */
slouken@7001
   636
                m.data.l[3] = 0;
slouken@7001
   637
                m.data.l[4] = videodata->XdndActionCopy; /* we only accept copying anyway */
slouken@7191
   638
icculus@7827
   639
                X11_XSendEvent(display, xevent.xclient.data.l[0], False, NoEventMask, (XEvent*)&m);
icculus@7827
   640
                X11_XFlush(display);
slouken@7001
   641
            }
slouken@7001
   642
            else if(xevent.xclient.message_type == videodata->XdndDrop) {
slouken@7001
   643
                if (data->xdnd_req == None) {
slouken@7001
   644
                    /* say again - not interested! */
slouken@7001
   645
                    memset(&m, 0, sizeof(XClientMessageEvent));
slouken@7001
   646
                    m.type = ClientMessage;
slouken@7001
   647
                    m.display = xevent.xclient.display;
slouken@7001
   648
                    m.window = xevent.xclient.data.l[0];
slouken@7001
   649
                    m.message_type = videodata->XdndFinished;
slouken@7001
   650
                    m.format=32;
slouken@7001
   651
                    m.data.l[0] = data->xwindow;
slouken@7001
   652
                    m.data.l[1] = 0;
slouken@7001
   653
                    m.data.l[2] = None; /* fail! */
icculus@7827
   654
                    X11_XSendEvent(display, xevent.xclient.data.l[0], False, NoEventMask, (XEvent*)&m);
slouken@7001
   655
                } else {
slouken@7001
   656
                    /* convert */
slouken@7001
   657
                    if(xdnd_version >= 1) {
icculus@7827
   658
                        X11_XConvertSelection(display, videodata->XdndSelection, data->xdnd_req, videodata->PRIMARY, data->xwindow, xevent.xclient.data.l[2]);
slouken@7001
   659
                    } else {
icculus@7827
   660
                        X11_XConvertSelection(display, videodata->XdndSelection, data->xdnd_req, videodata->PRIMARY, data->xwindow, CurrentTime);
slouken@7001
   661
                    }
slouken@7001
   662
                }
slouken@7001
   663
            }
slouken@7001
   664
            else if ((xevent.xclient.message_type == videodata->WM_PROTOCOLS) &&
icculus@6637
   665
                (xevent.xclient.format == 32) &&
icculus@6638
   666
                (xevent.xclient.data.l[0] == videodata->_NET_WM_PING)) {
slouken@6659
   667
                Window root = DefaultRootWindow(display);
icculus@6638
   668
icculus@6638
   669
#ifdef DEBUG_XEVENTS
icculus@6638
   670
                printf("window %p: _NET_WM_PING\n", data);
icculus@6638
   671
#endif
icculus@6638
   672
                xevent.xclient.window = root;
icculus@7827
   673
                X11_XSendEvent(display, root, False, SubstructureRedirectMask | SubstructureNotifyMask, &xevent);
icculus@6638
   674
                break;
icculus@6638
   675
            }
icculus@6638
   676
icculus@6638
   677
            else if ((xevent.xclient.message_type == videodata->WM_PROTOCOLS) &&
icculus@6638
   678
                (xevent.xclient.format == 32) &&
slouken@1951
   679
                (xevent.xclient.data.l[0] == videodata->WM_DELETE_WINDOW)) {
slouken@1951
   680
icculus@6638
   681
#ifdef DEBUG_XEVENTS
icculus@6638
   682
                printf("window %p: WM_DELETE_WINDOW\n", data);
icculus@6638
   683
#endif
slouken@3685
   684
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
icculus@6638
   685
                break;
slouken@1951
   686
            }
slouken@1951
   687
        }
slouken@1951
   688
        break;
slouken@1951
   689
slouken@1951
   690
        /* Do we need to refresh ourselves? */
slouken@1951
   691
    case Expose:{
slouken@1951
   692
#ifdef DEBUG_XEVENTS
slouken@6504
   693
            printf("window %p: Expose (count = %d)\n", data, xevent.xexpose.count);
slouken@1951
   694
#endif
slouken@3685
   695
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
slouken@1951
   696
        }
slouken@1951
   697
        break;
slouken@1951
   698
slouken@4465
   699
    case MotionNotify:{
slouken@7191
   700
            SDL_Mouse *mouse = SDL_GetMouse();
slouken@8071
   701
            if(!mouse->relative_mode || mouse->relative_mode_warp) {
slouken@2710
   702
#ifdef DEBUG_MOTION
slouken@6504
   703
                printf("window %p: X11 motion: %d,%d\n", xevent.xmotion.x, xevent.xmotion.y);
slouken@2710
   704
#endif
slouken@6311
   705
slouken@6950
   706
                SDL_SendMouseMotion(data->window, 0, 0, xevent.xmotion.x, xevent.xmotion.y);
slouken@6311
   707
            }
slouken@4465
   708
        }
slouken@4465
   709
        break;
slouken@4465
   710
slouken@4465
   711
    case ButtonPress:{
slouken@5530
   712
            int ticks = 0;
slouken@6950
   713
            if (X11_IsWheelEvent(display,&xevent,&ticks)) {
slouken@6950
   714
                SDL_SendMouseWheel(data->window, 0, 0, ticks);
slouken@6950
   715
            } else {
slouken@6950
   716
                SDL_SendMouseButton(data->window, 0, SDL_PRESSED, xevent.xbutton.button);
slouken@5530
   717
            }
slouken@4465
   718
        }
slouken@4465
   719
        break;
slouken@4465
   720
slouken@4465
   721
    case ButtonRelease:{
slouken@6950
   722
            SDL_SendMouseButton(data->window, 0, SDL_RELEASED, xevent.xbutton.button);
slouken@4465
   723
        }
slouken@4465
   724
        break;
slouken@4465
   725
slouken@4518
   726
    case PropertyNotify:{
slouken@4518
   727
#ifdef DEBUG_XEVENTS
slouken@4520
   728
            unsigned char *propdata;
slouken@4520
   729
            int status, real_format;
slouken@4520
   730
            Atom real_type;
slouken@4520
   731
            unsigned long items_read, items_left, i;
slouken@4520
   732
icculus@7827
   733
            char *name = X11_XGetAtomName(display, xevent.xproperty.atom);
slouken@4518
   734
            if (name) {
slouken@6504
   735
                printf("window %p: PropertyNotify: %s %s\n", data, name, (xevent.xproperty.state == PropertyDelete) ? "deleted" : "changed");
icculus@7827
   736
                X11_XFree(name);
slouken@4518
   737
            }
slouken@4520
   738
icculus@7827
   739
            status = X11_XGetWindowProperty(display, data->xwindow, xevent.xproperty.atom, 0L, 8192L, False, AnyPropertyType, &real_type, &real_format, &items_read, &items_left, &propdata);
slouken@4556
   740
            if (status == Success && items_read > 0) {
slouken@4520
   741
                if (real_type == XA_INTEGER) {
slouken@4520
   742
                    int *values = (int *)propdata;
slouken@4520
   743
slouken@4520
   744
                    printf("{");
slouken@4520
   745
                    for (i = 0; i < items_read; i++) {
slouken@4520
   746
                        printf(" %d", values[i]);
slouken@4520
   747
                    }
slouken@4520
   748
                    printf(" }\n");
slouken@4520
   749
                } else if (real_type == XA_CARDINAL) {
slouken@4520
   750
                    if (real_format == 32) {
slouken@4520
   751
                        Uint32 *values = (Uint32 *)propdata;
slouken@4520
   752
slouken@4520
   753
                        printf("{");
slouken@4520
   754
                        for (i = 0; i < items_read; i++) {
slouken@4520
   755
                            printf(" %d", values[i]);
slouken@4520
   756
                        }
slouken@4520
   757
                        printf(" }\n");
slouken@4520
   758
                    } else if (real_format == 16) {
slouken@4520
   759
                        Uint16 *values = (Uint16 *)propdata;
slouken@4520
   760
slouken@4520
   761
                        printf("{");
slouken@4520
   762
                        for (i = 0; i < items_read; i++) {
slouken@4520
   763
                            printf(" %d", values[i]);
slouken@4520
   764
                        }
slouken@4520
   765
                        printf(" }\n");
slouken@4520
   766
                    } else if (real_format == 8) {
slouken@4520
   767
                        Uint8 *values = (Uint8 *)propdata;
slouken@4520
   768
slouken@4520
   769
                        printf("{");
slouken@4520
   770
                        for (i = 0; i < items_read; i++) {
slouken@4520
   771
                            printf(" %d", values[i]);
slouken@4520
   772
                        }
slouken@4520
   773
                        printf(" }\n");
slouken@4520
   774
                    }
slouken@4520
   775
                } else if (real_type == XA_STRING ||
slouken@4520
   776
                           real_type == videodata->UTF8_STRING) {
slouken@4520
   777
                    printf("{ \"%s\" }\n", propdata);
slouken@4520
   778
                } else if (real_type == XA_ATOM) {
slouken@4520
   779
                    Atom *atoms = (Atom *)propdata;
slouken@4520
   780
slouken@4520
   781
                    printf("{");
slouken@4520
   782
                    for (i = 0; i < items_read; i++) {
icculus@7827
   783
                        char *name = X11_XGetAtomName(display, atoms[i]);
slouken@4520
   784
                        if (name) {
slouken@4520
   785
                            printf(" %s", name);
icculus@7827
   786
                            X11_XFree(name);
slouken@4520
   787
                        }
slouken@4520
   788
                    }
slouken@4520
   789
                    printf(" }\n");
slouken@4520
   790
                } else {
icculus@7827
   791
                    char *name = X11_XGetAtomName(display, real_type);
slouken@4520
   792
                    printf("Unknown type: %ld (%s)\n", real_type, name ? name : "UNKNOWN");
slouken@4520
   793
                    if (name) {
icculus@7827
   794
                        X11_XFree(name);
slouken@4520
   795
                    }
slouken@4520
   796
                }
slouken@4520
   797
            }
slouken@6481
   798
            if (status == Success) {
icculus@7827
   799
                X11_XFree(propdata);
slouken@6481
   800
            }
slouken@6481
   801
#endif /* DEBUG_XEVENTS */
slouken@6481
   802
slouken@6481
   803
            if (xevent.xproperty.atom == data->videodata->_NET_WM_STATE) {
slouken@6481
   804
                /* Get the new state from the window manager.
slouken@6481
   805
                   Compositing window managers can alter visibility of windows
slouken@6481
   806
                   without ever mapping / unmapping them, so we handle that here,
slouken@6481
   807
                   because they use the NETWM protocol to notify us of changes.
slouken@6481
   808
                 */
slouken@6487
   809
                Uint32 flags = X11_GetNetWMState(_this, xevent.xproperty.window);
slouken@6481
   810
                if ((flags^data->window->flags) & SDL_WINDOW_HIDDEN) {
slouken@6481
   811
                    if (flags & SDL_WINDOW_HIDDEN) {
slouken@6481
   812
                        X11_DispatchUnmapNotify(data);
slouken@6481
   813
                    } else {
slouken@6481
   814
                        X11_DispatchMapNotify(data);
slouken@6481
   815
                    }
slouken@6481
   816
                }
slouken@6481
   817
            }
slouken@4518
   818
        }
slouken@4518
   819
        break;
slouken@4518
   820
slouken@4508
   821
    /* Copy the selection from XA_CUT_BUFFER0 to the requested property */
slouken@4508
   822
    case SelectionRequest: {
slouken@4508
   823
            XSelectionRequestEvent *req;
slouken@4508
   824
            XEvent sevent;
slouken@4508
   825
            int seln_format;
slouken@4508
   826
            unsigned long nbytes;
slouken@4508
   827
            unsigned long overflow;
slouken@4508
   828
            unsigned char *seln_data;
slouken@4508
   829
slouken@4508
   830
            req = &xevent.xselectionrequest;
slouken@4508
   831
#ifdef DEBUG_XEVENTS
slouken@6504
   832
            printf("window %p: SelectionRequest (requestor = %ld, target = %ld)\n", data,
slouken@4508
   833
                req->requestor, req->target);
slouken@4508
   834
#endif
slouken@4508
   835
slouken@4524
   836
            SDL_zero(sevent);
slouken@4520
   837
            sevent.xany.type = SelectionNotify;
slouken@4508
   838
            sevent.xselection.selection = req->selection;
slouken@4508
   839
            sevent.xselection.target = None;
slouken@4508
   840
            sevent.xselection.property = None;
slouken@4508
   841
            sevent.xselection.requestor = req->requestor;
slouken@4508
   842
            sevent.xselection.time = req->time;
icculus@7827
   843
            if (X11_XGetWindowProperty(display, DefaultRootWindow(display),
slouken@4508
   844
                    XA_CUT_BUFFER0, 0, INT_MAX/4, False, req->target,
slouken@4508
   845
                    &sevent.xselection.target, &seln_format, &nbytes,
slouken@4508
   846
                    &overflow, &seln_data) == Success) {
icculus@7827
   847
                Atom XA_TARGETS = X11_XInternAtom(display, "TARGETS", 0);
slouken@4508
   848
                if (sevent.xselection.target == req->target) {
icculus@7827
   849
                    X11_XChangeProperty(display, req->requestor, req->property,
slouken@4508
   850
                        sevent.xselection.target, seln_format, PropModeReplace,
slouken@4508
   851
                        seln_data, nbytes);
slouken@4508
   852
                    sevent.xselection.property = req->property;
slouken@7021
   853
                } else if (XA_TARGETS == req->target) {
slouken@7021
   854
                    Atom SupportedFormats[] = { sevent.xselection.target, XA_TARGETS };
icculus@7827
   855
                    X11_XChangeProperty(display, req->requestor, req->property,
slouken@7021
   856
                        XA_ATOM, 32, PropModeReplace,
slouken@7021
   857
                        (unsigned char*)SupportedFormats,
slouken@7021
   858
                        sizeof(SupportedFormats)/sizeof(*SupportedFormats));
slouken@7021
   859
                    sevent.xselection.property = req->property;
slouken@4508
   860
                }
icculus@7827
   861
                X11_XFree(seln_data);
slouken@4508
   862
            }
icculus@7827
   863
            X11_XSendEvent(display, req->requestor, False, 0, &sevent);
icculus@7827
   864
            X11_XSync(display, False);
slouken@4508
   865
        }
slouken@4508
   866
        break;
slouken@4508
   867
slouken@4508
   868
    case SelectionNotify: {
slouken@4508
   869
#ifdef DEBUG_XEVENTS
slouken@6504
   870
            printf("window %p: SelectionNotify (requestor = %ld, target = %ld)\n", data,
slouken@4508
   871
                xevent.xselection.requestor, xevent.xselection.target);
slouken@4508
   872
#endif
slouken@7001
   873
            Atom target = xevent.xselection.target;
slouken@7001
   874
            if (target == data->xdnd_req) {
slouken@7001
   875
                /* read data */
slouken@7001
   876
                SDL_x11Prop p;
slouken@7001
   877
                X11_ReadProperty(&p, display, data->xwindow, videodata->PRIMARY);
slouken@7191
   878
slouken@7396
   879
                if (p.format == 8) {
slouken@7001
   880
                    SDL_bool expect_lf = SDL_FALSE;
slouken@7001
   881
                    char *start = NULL;
slouken@7001
   882
                    char *scan = (char*)p.data;
slouken@7001
   883
                    char *fn;
slouken@7001
   884
                    char *uri;
slouken@7001
   885
                    int length = 0;
slouken@7001
   886
                    while (p.count--) {
slouken@7001
   887
                        if (!expect_lf) {
slouken@7396
   888
                            if (*scan == 0x0D) {
slouken@7001
   889
                                expect_lf = SDL_TRUE;
slouken@7396
   890
                            }
slouken@7396
   891
                            if (start == NULL) {
slouken@7001
   892
                                start = scan;
slouken@7001
   893
                                length = 0;
slouken@7001
   894
                            }
slouken@7001
   895
                            length++;
slouken@7001
   896
                        } else {
slouken@7396
   897
                            if (*scan == 0x0A && length > 0) {
slouken@7396
   898
                                uri = SDL_malloc(length--);
slouken@7396
   899
                                SDL_memcpy(uri, start, length);
slouken@7396
   900
                                uri[length] = '\0';
slouken@7001
   901
                                fn = X11_URIToLocal(uri);
slouken@7396
   902
                                if (fn) {
slouken@7396
   903
                                    SDL_SendDropFile(fn);
slouken@7396
   904
                                }
slouken@7396
   905
                                SDL_free(uri);
slouken@7001
   906
                            }
slouken@7001
   907
                            expect_lf = SDL_FALSE;
slouken@7001
   908
                            start = NULL;
slouken@7001
   909
                        }
slouken@7001
   910
                        scan++;
slouken@7001
   911
                    }
slouken@7001
   912
                }
slouken@7191
   913
icculus@7827
   914
                X11_XFree(p.data);
slouken@7191
   915
slouken@7001
   916
                /* send reply */
slouken@7396
   917
                SDL_memset(&m, 0, sizeof(XClientMessageEvent));
slouken@7001
   918
                m.type = ClientMessage;
slouken@7001
   919
                m.display = display;
slouken@7001
   920
                m.window = data->xdnd_source;
slouken@7001
   921
                m.message_type = videodata->XdndFinished;
slouken@7396
   922
                m.format = 32;
slouken@7001
   923
                m.data.l[0] = data->xwindow;
slouken@7001
   924
                m.data.l[1] = 1;
slouken@7001
   925
                m.data.l[2] = videodata->XdndActionCopy;
icculus@7827
   926
                X11_XSendEvent(display, data->xdnd_source, False, NoEventMask, (XEvent*)&m);
slouken@7191
   927
icculus@7827
   928
                X11_XSync(display, False);
slouken@7191
   929
slouken@7001
   930
            } else {
slouken@7001
   931
                videodata->selection_waiting = SDL_FALSE;
slouken@7001
   932
            }
slouken@4508
   933
        }
slouken@4508
   934
        break;
slouken@4508
   935
slouken@4465
   936
    default:{
slouken@1951
   937
#ifdef DEBUG_XEVENTS
slouken@6504
   938
            printf("window %p: Unhandled event %d\n", data, xevent.type);
slouken@1951
   939
#endif
slouken@1951
   940
        }
slouken@1951
   941
        break;
slouken@1951
   942
    }
slouken@1951
   943
}
slouken@1951
   944
slouken@6504
   945
static void
slouken@6504
   946
X11_HandleFocusChanges(_THIS)
slouken@6504
   947
{
slouken@6504
   948
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@6504
   949
    int i;
slouken@6504
   950
slouken@6504
   951
    if (videodata && videodata->windowlist) {
slouken@6504
   952
        for (i = 0; i < videodata->numwindows; ++i) {
slouken@6504
   953
            SDL_WindowData *data = videodata->windowlist[i];
slouken@6504
   954
            if (data && data->pending_focus != PENDING_FOCUS_NONE) {
slouken@6504
   955
                Uint32 now = SDL_GetTicks();
slouken@7857
   956
                if (SDL_TICKS_PASSED(now, data->pending_focus_time)) {
slouken@6504
   957
                    if ( data->pending_focus == PENDING_FOCUS_IN ) {
slouken@6504
   958
                        X11_DispatchFocusIn(data);
slouken@6504
   959
                    } else {
slouken@6504
   960
                        X11_DispatchFocusOut(data);
slouken@6504
   961
                    }
slouken@6504
   962
                    data->pending_focus = PENDING_FOCUS_NONE;
slouken@6504
   963
                }
slouken@6504
   964
            }
slouken@6504
   965
        }
slouken@6504
   966
    }
slouken@6504
   967
}
icculus@7827
   968
/* Ack!  X11_XPending() actually performs a blocking read if no events available */
slouken@4472
   969
static int
slouken@1951
   970
X11_Pending(Display * display)
slouken@1951
   971
{
slouken@1951
   972
    /* Flush the display connection and look to see if events are queued */
icculus@7827
   973
    X11_XFlush(display);
icculus@7827
   974
    if (X11_XEventsQueued(display, QueuedAlready)) {
slouken@1951
   975
        return (1);
slouken@1951
   976
    }
slouken@1951
   977
slouken@1951
   978
    /* More drastic measures are required -- see if X is ready to talk */
slouken@1951
   979
    {
slouken@1951
   980
        static struct timeval zero_time;        /* static == 0 */
slouken@1951
   981
        int x11_fd;
slouken@1951
   982
        fd_set fdset;
slouken@1951
   983
slouken@1951
   984
        x11_fd = ConnectionNumber(display);
slouken@1951
   985
        FD_ZERO(&fdset);
slouken@1951
   986
        FD_SET(x11_fd, &fdset);
slouken@1951
   987
        if (select(x11_fd + 1, &fdset, NULL, NULL, &zero_time) == 1) {
icculus@7827
   988
            return (X11_XPending(display));
slouken@1951
   989
        }
slouken@1951
   990
    }
slouken@1951
   991
slouken@1951
   992
    /* Oh well, nothing is ready .. */
slouken@1951
   993
    return (0);
slouken@1951
   994
}
slouken@1951
   995
icculus@5981
   996
icculus@5981
   997
/* !!! FIXME: this should be exposed in a header, or something. */
icculus@5981
   998
int SDL_GetNumTouch(void);
icculus@6774
   999
void SDL_dbus_screensaver_tickle(_THIS);
icculus@5981
  1000
slouken@1951
  1001
void
slouken@1951
  1002
X11_PumpEvents(_THIS)
slouken@1951
  1003
{
slouken@1951
  1004
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
  1005
slouken@3025
  1006
    /* Update activity every 30 seconds to prevent screensaver */
slouken@3025
  1007
    if (_this->suspend_screensaver) {
slouken@3025
  1008
        Uint32 now = SDL_GetTicks();
slouken@3025
  1009
        if (!data->screensaver_activity ||
slouken@7857
  1010
            SDL_TICKS_PASSED(now, data->screensaver_activity + 30000)) {
icculus@7827
  1011
            X11_XResetScreenSaver(data->display);
icculus@6774
  1012
icculus@6774
  1013
            #if SDL_USE_LIBDBUS
icculus@6774
  1014
            SDL_dbus_screensaver_tickle(_this);
icculus@6774
  1015
            #endif
icculus@6774
  1016
slouken@3025
  1017
            data->screensaver_activity = now;
slouken@3025
  1018
        }
slouken@7191
  1019
    }
slouken@3025
  1020
slouken@1951
  1021
    /* Keep processing pending events */
slouken@1951
  1022
    while (X11_Pending(data->display)) {
slouken@1951
  1023
        X11_DispatchEvent(_this);
slouken@1951
  1024
    }
slouken@6504
  1025
slouken@6504
  1026
    /* FIXME: Only need to do this when there are pending focus changes */
slouken@6504
  1027
    X11_HandleFocusChanges(_this);
slouken@1951
  1028
}
slouken@1951
  1029
slouken@3030
  1030
slouken@1951
  1031
void
slouken@3025
  1032
X11_SuspendScreenSaver(_THIS)
slouken@1951
  1033
{
slouken@5408
  1034
#if SDL_VIDEO_DRIVER_X11_XSCRNSAVER
slouken@3025
  1035
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@3025
  1036
    int dummy;
slouken@3025
  1037
    int major_version, minor_version;
slouken@1951
  1038
slouken@3030
  1039
    if (SDL_X11_HAVE_XSS) {
icculus@7827
  1040
        /* X11_XScreenSaverSuspend was introduced in MIT-SCREEN-SAVER 1.1 */
icculus@7827
  1041
        if (!X11_XScreenSaverQueryExtension(data->display, &dummy, &dummy) ||
icculus@7827
  1042
            !X11_XScreenSaverQueryVersion(data->display,
slouken@3030
  1043
                                      &major_version, &minor_version) ||
slouken@3030
  1044
            major_version < 1 || (major_version == 1 && minor_version < 1)) {
slouken@3030
  1045
            return;
slouken@3030
  1046
        }
slouken@1951
  1047
icculus@7827
  1048
        X11_XScreenSaverSuspend(data->display, _this->suspend_screensaver);
icculus@7827
  1049
        X11_XResetScreenSaver(data->display);
slouken@1951
  1050
    }
slouken@3030
  1051
#endif
slouken@1951
  1052
icculus@6774
  1053
#if SDL_USE_LIBDBUS
slouken@3030
  1054
    if (_this->suspend_screensaver) {
icculus@6774
  1055
        SDL_dbus_screensaver_tickle(_this);
slouken@3030
  1056
    }
slouken@3025
  1057
#endif
slouken@1951
  1058
}
slouken@1951
  1059
slouken@5481
  1060
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
  1061
slouken@1951
  1062
/* vi: set ts=4 sw=4 expandtab: */