src/video/x11/SDL_x11events.c
author Pierre-Loup A. Griffais
Wed, 23 Oct 2013 13:43:17 -0700
changeset 7885 a8bd63b33636
parent 7857 6388f5229bb7
child 7902 0c2e2b91eeea
permissions -rw-r--r--
Fix regression causing crashes when running without a window manager.
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);
pgriffais@7885
   528
                Atom type = None;
stapostol@7838
   529
                int format;
pgriffais@7885
   530
                unsigned long nitems = 0, 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
pgriffais@7885
   537
                if (type != None && nitems == 4)
pgriffais@7885
   538
                {
pgriffais@7885
   539
                    border_left = ((long*)property)[0];
pgriffais@7885
   540
                    border_right = ((long*)property)[1];
pgriffais@7885
   541
                    border_top = ((long*)property)[2];
pgriffais@7885
   542
                    border_bottom = ((long*)property)[3];
pgriffais@7885
   543
                }
stapostol@7838
   544
            }
stapostol@7838
   545
slouken@6542
   546
            if (xevent.xconfigure.x != data->last_xconfigure.x ||
slouken@6542
   547
                xevent.xconfigure.y != data->last_xconfigure.y) {
slouken@6542
   548
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED,
stapostol@7838
   549
                                    xevent.xconfigure.x - border_left,
stapostol@7838
   550
                                    xevent.xconfigure.y - border_top);
slouken@6542
   551
            }
slouken@6542
   552
            if (xevent.xconfigure.width != data->last_xconfigure.width ||
slouken@6542
   553
                xevent.xconfigure.height != data->last_xconfigure.height) {
slouken@6542
   554
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED,
slouken@6542
   555
                                    xevent.xconfigure.width,
slouken@6542
   556
                                    xevent.xconfigure.height);
slouken@6542
   557
            }
slouken@6542
   558
            data->last_xconfigure = xevent.xconfigure;
slouken@1951
   559
        }
slouken@1951
   560
        break;
slouken@1951
   561
slouken@1951
   562
        /* Have we been requested to quit (or another client message?) */
slouken@1951
   563
    case ClientMessage:{
slouken@7001
   564
slouken@7001
   565
            int xdnd_version=0;
slouken@7191
   566
slouken@7001
   567
            if (xevent.xclient.message_type == videodata->XdndEnter) {
slouken@7001
   568
                SDL_bool use_list = xevent.xclient.data.l[1] & 1;
slouken@7001
   569
                data->xdnd_source = xevent.xclient.data.l[0];
slouken@7001
   570
                xdnd_version = ( xevent.xclient.data.l[1] >> 24);
slouken@7001
   571
                if (use_list) {
slouken@7001
   572
                    /* fetch conversion targets */
slouken@7001
   573
                    SDL_x11Prop p;
slouken@7001
   574
                    X11_ReadProperty(&p, display, data->xdnd_source, videodata->XdndTypeList);
slouken@7001
   575
                    /* pick one */
slouken@7001
   576
                    data->xdnd_req = X11_PickTarget(display, (Atom*)p.data, p.count);
icculus@7827
   577
                    X11_XFree(p.data);
slouken@7001
   578
                } else {
slouken@7001
   579
                    /* pick from list of three */
slouken@7001
   580
                    data->xdnd_req = X11_PickTargetFromAtoms(display, xevent.xclient.data.l[2], xevent.xclient.data.l[3], xevent.xclient.data.l[4]);
slouken@7001
   581
                }
slouken@7001
   582
            }
slouken@7001
   583
            else if (xevent.xclient.message_type == videodata->XdndPosition) {
slouken@7191
   584
slouken@7001
   585
                /* reply with status */
slouken@7001
   586
                memset(&m, 0, sizeof(XClientMessageEvent));
slouken@7001
   587
                m.type = ClientMessage;
slouken@7001
   588
                m.display = xevent.xclient.display;
slouken@7001
   589
                m.window = xevent.xclient.data.l[0];
slouken@7001
   590
                m.message_type = videodata->XdndStatus;
slouken@7001
   591
                m.format=32;
slouken@7001
   592
                m.data.l[0] = data->xwindow;
slouken@7001
   593
                m.data.l[1] = (data->xdnd_req != None);
slouken@7001
   594
                m.data.l[2] = 0; /* specify an empty rectangle */
slouken@7001
   595
                m.data.l[3] = 0;
slouken@7001
   596
                m.data.l[4] = videodata->XdndActionCopy; /* we only accept copying anyway */
slouken@7191
   597
icculus@7827
   598
                X11_XSendEvent(display, xevent.xclient.data.l[0], False, NoEventMask, (XEvent*)&m);
icculus@7827
   599
                X11_XFlush(display);
slouken@7001
   600
            }
slouken@7001
   601
            else if(xevent.xclient.message_type == videodata->XdndDrop) {
slouken@7001
   602
                if (data->xdnd_req == None) {
slouken@7001
   603
                    /* say again - not interested! */
slouken@7001
   604
                    memset(&m, 0, sizeof(XClientMessageEvent));
slouken@7001
   605
                    m.type = ClientMessage;
slouken@7001
   606
                    m.display = xevent.xclient.display;
slouken@7001
   607
                    m.window = xevent.xclient.data.l[0];
slouken@7001
   608
                    m.message_type = videodata->XdndFinished;
slouken@7001
   609
                    m.format=32;
slouken@7001
   610
                    m.data.l[0] = data->xwindow;
slouken@7001
   611
                    m.data.l[1] = 0;
slouken@7001
   612
                    m.data.l[2] = None; /* fail! */
icculus@7827
   613
                    X11_XSendEvent(display, xevent.xclient.data.l[0], False, NoEventMask, (XEvent*)&m);
slouken@7001
   614
                } else {
slouken@7001
   615
                    /* convert */
slouken@7001
   616
                    if(xdnd_version >= 1) {
icculus@7827
   617
                        X11_XConvertSelection(display, videodata->XdndSelection, data->xdnd_req, videodata->PRIMARY, data->xwindow, xevent.xclient.data.l[2]);
slouken@7001
   618
                    } else {
icculus@7827
   619
                        X11_XConvertSelection(display, videodata->XdndSelection, data->xdnd_req, videodata->PRIMARY, data->xwindow, CurrentTime);
slouken@7001
   620
                    }
slouken@7001
   621
                }
slouken@7001
   622
            }
slouken@7001
   623
            else if ((xevent.xclient.message_type == videodata->WM_PROTOCOLS) &&
icculus@6637
   624
                (xevent.xclient.format == 32) &&
icculus@6638
   625
                (xevent.xclient.data.l[0] == videodata->_NET_WM_PING)) {
slouken@6659
   626
                Window root = DefaultRootWindow(display);
icculus@6638
   627
icculus@6638
   628
#ifdef DEBUG_XEVENTS
icculus@6638
   629
                printf("window %p: _NET_WM_PING\n", data);
icculus@6638
   630
#endif
icculus@6638
   631
                xevent.xclient.window = root;
icculus@7827
   632
                X11_XSendEvent(display, root, False, SubstructureRedirectMask | SubstructureNotifyMask, &xevent);
icculus@6638
   633
                break;
icculus@6638
   634
            }
icculus@6638
   635
icculus@6638
   636
            else if ((xevent.xclient.message_type == videodata->WM_PROTOCOLS) &&
icculus@6638
   637
                (xevent.xclient.format == 32) &&
slouken@1951
   638
                (xevent.xclient.data.l[0] == videodata->WM_DELETE_WINDOW)) {
slouken@1951
   639
icculus@6638
   640
#ifdef DEBUG_XEVENTS
icculus@6638
   641
                printf("window %p: WM_DELETE_WINDOW\n", data);
icculus@6638
   642
#endif
slouken@3685
   643
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
icculus@6638
   644
                break;
slouken@1951
   645
            }
slouken@1951
   646
        }
slouken@1951
   647
        break;
slouken@1951
   648
slouken@1951
   649
        /* Do we need to refresh ourselves? */
slouken@1951
   650
    case Expose:{
slouken@1951
   651
#ifdef DEBUG_XEVENTS
slouken@6504
   652
            printf("window %p: Expose (count = %d)\n", data, xevent.xexpose.count);
slouken@1951
   653
#endif
slouken@3685
   654
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
slouken@1951
   655
        }
slouken@1951
   656
        break;
slouken@1951
   657
slouken@4465
   658
    case MotionNotify:{
slouken@7191
   659
            SDL_Mouse *mouse = SDL_GetMouse();
slouken@6311
   660
            if(!mouse->relative_mode) {
slouken@2710
   661
#ifdef DEBUG_MOTION
slouken@6504
   662
                printf("window %p: X11 motion: %d,%d\n", xevent.xmotion.x, xevent.xmotion.y);
slouken@2710
   663
#endif
slouken@6311
   664
slouken@6950
   665
                SDL_SendMouseMotion(data->window, 0, 0, xevent.xmotion.x, xevent.xmotion.y);
slouken@6311
   666
            }
slouken@4465
   667
        }
slouken@4465
   668
        break;
slouken@4465
   669
slouken@4465
   670
    case ButtonPress:{
slouken@5530
   671
            int ticks = 0;
slouken@6950
   672
            if (X11_IsWheelEvent(display,&xevent,&ticks)) {
slouken@6950
   673
                SDL_SendMouseWheel(data->window, 0, 0, ticks);
slouken@6950
   674
            } else {
slouken@6950
   675
                SDL_SendMouseButton(data->window, 0, SDL_PRESSED, xevent.xbutton.button);
slouken@5530
   676
            }
slouken@4465
   677
        }
slouken@4465
   678
        break;
slouken@4465
   679
slouken@4465
   680
    case ButtonRelease:{
slouken@6950
   681
            SDL_SendMouseButton(data->window, 0, SDL_RELEASED, xevent.xbutton.button);
slouken@4465
   682
        }
slouken@4465
   683
        break;
slouken@4465
   684
slouken@4518
   685
    case PropertyNotify:{
slouken@4518
   686
#ifdef DEBUG_XEVENTS
slouken@4520
   687
            unsigned char *propdata;
slouken@4520
   688
            int status, real_format;
slouken@4520
   689
            Atom real_type;
slouken@4520
   690
            unsigned long items_read, items_left, i;
slouken@4520
   691
icculus@7827
   692
            char *name = X11_XGetAtomName(display, xevent.xproperty.atom);
slouken@4518
   693
            if (name) {
slouken@6504
   694
                printf("window %p: PropertyNotify: %s %s\n", data, name, (xevent.xproperty.state == PropertyDelete) ? "deleted" : "changed");
icculus@7827
   695
                X11_XFree(name);
slouken@4518
   696
            }
slouken@4520
   697
icculus@7827
   698
            status = X11_XGetWindowProperty(display, data->xwindow, xevent.xproperty.atom, 0L, 8192L, False, AnyPropertyType, &real_type, &real_format, &items_read, &items_left, &propdata);
slouken@4556
   699
            if (status == Success && items_read > 0) {
slouken@4520
   700
                if (real_type == XA_INTEGER) {
slouken@4520
   701
                    int *values = (int *)propdata;
slouken@4520
   702
slouken@4520
   703
                    printf("{");
slouken@4520
   704
                    for (i = 0; i < items_read; i++) {
slouken@4520
   705
                        printf(" %d", values[i]);
slouken@4520
   706
                    }
slouken@4520
   707
                    printf(" }\n");
slouken@4520
   708
                } else if (real_type == XA_CARDINAL) {
slouken@4520
   709
                    if (real_format == 32) {
slouken@4520
   710
                        Uint32 *values = (Uint32 *)propdata;
slouken@4520
   711
slouken@4520
   712
                        printf("{");
slouken@4520
   713
                        for (i = 0; i < items_read; i++) {
slouken@4520
   714
                            printf(" %d", values[i]);
slouken@4520
   715
                        }
slouken@4520
   716
                        printf(" }\n");
slouken@4520
   717
                    } else if (real_format == 16) {
slouken@4520
   718
                        Uint16 *values = (Uint16 *)propdata;
slouken@4520
   719
slouken@4520
   720
                        printf("{");
slouken@4520
   721
                        for (i = 0; i < items_read; i++) {
slouken@4520
   722
                            printf(" %d", values[i]);
slouken@4520
   723
                        }
slouken@4520
   724
                        printf(" }\n");
slouken@4520
   725
                    } else if (real_format == 8) {
slouken@4520
   726
                        Uint8 *values = (Uint8 *)propdata;
slouken@4520
   727
slouken@4520
   728
                        printf("{");
slouken@4520
   729
                        for (i = 0; i < items_read; i++) {
slouken@4520
   730
                            printf(" %d", values[i]);
slouken@4520
   731
                        }
slouken@4520
   732
                        printf(" }\n");
slouken@4520
   733
                    }
slouken@4520
   734
                } else if (real_type == XA_STRING ||
slouken@4520
   735
                           real_type == videodata->UTF8_STRING) {
slouken@4520
   736
                    printf("{ \"%s\" }\n", propdata);
slouken@4520
   737
                } else if (real_type == XA_ATOM) {
slouken@4520
   738
                    Atom *atoms = (Atom *)propdata;
slouken@4520
   739
slouken@4520
   740
                    printf("{");
slouken@4520
   741
                    for (i = 0; i < items_read; i++) {
icculus@7827
   742
                        char *name = X11_XGetAtomName(display, atoms[i]);
slouken@4520
   743
                        if (name) {
slouken@4520
   744
                            printf(" %s", name);
icculus@7827
   745
                            X11_XFree(name);
slouken@4520
   746
                        }
slouken@4520
   747
                    }
slouken@4520
   748
                    printf(" }\n");
slouken@4520
   749
                } else {
icculus@7827
   750
                    char *name = X11_XGetAtomName(display, real_type);
slouken@4520
   751
                    printf("Unknown type: %ld (%s)\n", real_type, name ? name : "UNKNOWN");
slouken@4520
   752
                    if (name) {
icculus@7827
   753
                        X11_XFree(name);
slouken@4520
   754
                    }
slouken@4520
   755
                }
slouken@4520
   756
            }
slouken@6481
   757
            if (status == Success) {
icculus@7827
   758
                X11_XFree(propdata);
slouken@6481
   759
            }
slouken@6481
   760
#endif /* DEBUG_XEVENTS */
slouken@6481
   761
slouken@6481
   762
            if (xevent.xproperty.atom == data->videodata->_NET_WM_STATE) {
slouken@6481
   763
                /* Get the new state from the window manager.
slouken@6481
   764
                   Compositing window managers can alter visibility of windows
slouken@6481
   765
                   without ever mapping / unmapping them, so we handle that here,
slouken@6481
   766
                   because they use the NETWM protocol to notify us of changes.
slouken@6481
   767
                 */
slouken@6487
   768
                Uint32 flags = X11_GetNetWMState(_this, xevent.xproperty.window);
slouken@6481
   769
                if ((flags^data->window->flags) & SDL_WINDOW_HIDDEN) {
slouken@6481
   770
                    if (flags & SDL_WINDOW_HIDDEN) {
slouken@6481
   771
                        X11_DispatchUnmapNotify(data);
slouken@6481
   772
                    } else {
slouken@6481
   773
                        X11_DispatchMapNotify(data);
slouken@6481
   774
                    }
slouken@6481
   775
                }
slouken@6481
   776
            }
slouken@4518
   777
        }
slouken@4518
   778
        break;
slouken@4518
   779
slouken@4508
   780
    /* Copy the selection from XA_CUT_BUFFER0 to the requested property */
slouken@4508
   781
    case SelectionRequest: {
slouken@4508
   782
            XSelectionRequestEvent *req;
slouken@4508
   783
            XEvent sevent;
slouken@4508
   784
            int seln_format;
slouken@4508
   785
            unsigned long nbytes;
slouken@4508
   786
            unsigned long overflow;
slouken@4508
   787
            unsigned char *seln_data;
slouken@4508
   788
slouken@4508
   789
            req = &xevent.xselectionrequest;
slouken@4508
   790
#ifdef DEBUG_XEVENTS
slouken@6504
   791
            printf("window %p: SelectionRequest (requestor = %ld, target = %ld)\n", data,
slouken@4508
   792
                req->requestor, req->target);
slouken@4508
   793
#endif
slouken@4508
   794
slouken@4524
   795
            SDL_zero(sevent);
slouken@4520
   796
            sevent.xany.type = SelectionNotify;
slouken@4508
   797
            sevent.xselection.selection = req->selection;
slouken@4508
   798
            sevent.xselection.target = None;
slouken@4508
   799
            sevent.xselection.property = None;
slouken@4508
   800
            sevent.xselection.requestor = req->requestor;
slouken@4508
   801
            sevent.xselection.time = req->time;
icculus@7827
   802
            if (X11_XGetWindowProperty(display, DefaultRootWindow(display),
slouken@4508
   803
                    XA_CUT_BUFFER0, 0, INT_MAX/4, False, req->target,
slouken@4508
   804
                    &sevent.xselection.target, &seln_format, &nbytes,
slouken@4508
   805
                    &overflow, &seln_data) == Success) {
icculus@7827
   806
                Atom XA_TARGETS = X11_XInternAtom(display, "TARGETS", 0);
slouken@4508
   807
                if (sevent.xselection.target == req->target) {
icculus@7827
   808
                    X11_XChangeProperty(display, req->requestor, req->property,
slouken@4508
   809
                        sevent.xselection.target, seln_format, PropModeReplace,
slouken@4508
   810
                        seln_data, nbytes);
slouken@4508
   811
                    sevent.xselection.property = req->property;
slouken@7021
   812
                } else if (XA_TARGETS == req->target) {
slouken@7021
   813
                    Atom SupportedFormats[] = { sevent.xselection.target, XA_TARGETS };
icculus@7827
   814
                    X11_XChangeProperty(display, req->requestor, req->property,
slouken@7021
   815
                        XA_ATOM, 32, PropModeReplace,
slouken@7021
   816
                        (unsigned char*)SupportedFormats,
slouken@7021
   817
                        sizeof(SupportedFormats)/sizeof(*SupportedFormats));
slouken@7021
   818
                    sevent.xselection.property = req->property;
slouken@4508
   819
                }
icculus@7827
   820
                X11_XFree(seln_data);
slouken@4508
   821
            }
icculus@7827
   822
            X11_XSendEvent(display, req->requestor, False, 0, &sevent);
icculus@7827
   823
            X11_XSync(display, False);
slouken@4508
   824
        }
slouken@4508
   825
        break;
slouken@4508
   826
slouken@4508
   827
    case SelectionNotify: {
slouken@4508
   828
#ifdef DEBUG_XEVENTS
slouken@6504
   829
            printf("window %p: SelectionNotify (requestor = %ld, target = %ld)\n", data,
slouken@4508
   830
                xevent.xselection.requestor, xevent.xselection.target);
slouken@4508
   831
#endif
slouken@7001
   832
            Atom target = xevent.xselection.target;
slouken@7001
   833
            if (target == data->xdnd_req) {
slouken@7001
   834
                /* read data */
slouken@7001
   835
                SDL_x11Prop p;
slouken@7001
   836
                X11_ReadProperty(&p, display, data->xwindow, videodata->PRIMARY);
slouken@7191
   837
slouken@7396
   838
                if (p.format == 8) {
slouken@7001
   839
                    SDL_bool expect_lf = SDL_FALSE;
slouken@7001
   840
                    char *start = NULL;
slouken@7001
   841
                    char *scan = (char*)p.data;
slouken@7001
   842
                    char *fn;
slouken@7001
   843
                    char *uri;
slouken@7001
   844
                    int length = 0;
slouken@7001
   845
                    while (p.count--) {
slouken@7001
   846
                        if (!expect_lf) {
slouken@7396
   847
                            if (*scan == 0x0D) {
slouken@7001
   848
                                expect_lf = SDL_TRUE;
slouken@7396
   849
                            }
slouken@7396
   850
                            if (start == NULL) {
slouken@7001
   851
                                start = scan;
slouken@7001
   852
                                length = 0;
slouken@7001
   853
                            }
slouken@7001
   854
                            length++;
slouken@7001
   855
                        } else {
slouken@7396
   856
                            if (*scan == 0x0A && length > 0) {
slouken@7396
   857
                                uri = SDL_malloc(length--);
slouken@7396
   858
                                SDL_memcpy(uri, start, length);
slouken@7396
   859
                                uri[length] = '\0';
slouken@7001
   860
                                fn = X11_URIToLocal(uri);
slouken@7396
   861
                                if (fn) {
slouken@7396
   862
                                    SDL_SendDropFile(fn);
slouken@7396
   863
                                }
slouken@7396
   864
                                SDL_free(uri);
slouken@7001
   865
                            }
slouken@7001
   866
                            expect_lf = SDL_FALSE;
slouken@7001
   867
                            start = NULL;
slouken@7001
   868
                        }
slouken@7001
   869
                        scan++;
slouken@7001
   870
                    }
slouken@7001
   871
                }
slouken@7191
   872
icculus@7827
   873
                X11_XFree(p.data);
slouken@7191
   874
slouken@7001
   875
                /* send reply */
slouken@7396
   876
                SDL_memset(&m, 0, sizeof(XClientMessageEvent));
slouken@7001
   877
                m.type = ClientMessage;
slouken@7001
   878
                m.display = display;
slouken@7001
   879
                m.window = data->xdnd_source;
slouken@7001
   880
                m.message_type = videodata->XdndFinished;
slouken@7396
   881
                m.format = 32;
slouken@7001
   882
                m.data.l[0] = data->xwindow;
slouken@7001
   883
                m.data.l[1] = 1;
slouken@7001
   884
                m.data.l[2] = videodata->XdndActionCopy;
icculus@7827
   885
                X11_XSendEvent(display, data->xdnd_source, False, NoEventMask, (XEvent*)&m);
slouken@7191
   886
icculus@7827
   887
                X11_XSync(display, False);
slouken@7191
   888
slouken@7001
   889
            } else {
slouken@7001
   890
                videodata->selection_waiting = SDL_FALSE;
slouken@7001
   891
            }
slouken@4508
   892
        }
slouken@4508
   893
        break;
slouken@4508
   894
slouken@4465
   895
    default:{
slouken@1951
   896
#ifdef DEBUG_XEVENTS
slouken@6504
   897
            printf("window %p: Unhandled event %d\n", data, xevent.type);
slouken@1951
   898
#endif
slouken@1951
   899
        }
slouken@1951
   900
        break;
slouken@1951
   901
    }
slouken@1951
   902
}
slouken@1951
   903
slouken@6504
   904
static void
slouken@6504
   905
X11_HandleFocusChanges(_THIS)
slouken@6504
   906
{
slouken@6504
   907
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@6504
   908
    int i;
slouken@6504
   909
slouken@6504
   910
    if (videodata && videodata->windowlist) {
slouken@6504
   911
        for (i = 0; i < videodata->numwindows; ++i) {
slouken@6504
   912
            SDL_WindowData *data = videodata->windowlist[i];
slouken@6504
   913
            if (data && data->pending_focus != PENDING_FOCUS_NONE) {
slouken@6504
   914
                Uint32 now = SDL_GetTicks();
slouken@7857
   915
                if (SDL_TICKS_PASSED(now, data->pending_focus_time)) {
slouken@6504
   916
                    if ( data->pending_focus == PENDING_FOCUS_IN ) {
slouken@6504
   917
                        X11_DispatchFocusIn(data);
slouken@6504
   918
                    } else {
slouken@6504
   919
                        X11_DispatchFocusOut(data);
slouken@6504
   920
                    }
slouken@6504
   921
                    data->pending_focus = PENDING_FOCUS_NONE;
slouken@6504
   922
                }
slouken@6504
   923
            }
slouken@6504
   924
        }
slouken@6504
   925
    }
slouken@6504
   926
}
icculus@7827
   927
/* Ack!  X11_XPending() actually performs a blocking read if no events available */
slouken@4472
   928
static int
slouken@1951
   929
X11_Pending(Display * display)
slouken@1951
   930
{
slouken@1951
   931
    /* Flush the display connection and look to see if events are queued */
icculus@7827
   932
    X11_XFlush(display);
icculus@7827
   933
    if (X11_XEventsQueued(display, QueuedAlready)) {
slouken@1951
   934
        return (1);
slouken@1951
   935
    }
slouken@1951
   936
slouken@1951
   937
    /* More drastic measures are required -- see if X is ready to talk */
slouken@1951
   938
    {
slouken@1951
   939
        static struct timeval zero_time;        /* static == 0 */
slouken@1951
   940
        int x11_fd;
slouken@1951
   941
        fd_set fdset;
slouken@1951
   942
slouken@1951
   943
        x11_fd = ConnectionNumber(display);
slouken@1951
   944
        FD_ZERO(&fdset);
slouken@1951
   945
        FD_SET(x11_fd, &fdset);
slouken@1951
   946
        if (select(x11_fd + 1, &fdset, NULL, NULL, &zero_time) == 1) {
icculus@7827
   947
            return (X11_XPending(display));
slouken@1951
   948
        }
slouken@1951
   949
    }
slouken@1951
   950
slouken@1951
   951
    /* Oh well, nothing is ready .. */
slouken@1951
   952
    return (0);
slouken@1951
   953
}
slouken@1951
   954
icculus@5981
   955
icculus@5981
   956
/* !!! FIXME: this should be exposed in a header, or something. */
icculus@5981
   957
int SDL_GetNumTouch(void);
icculus@6774
   958
void SDL_dbus_screensaver_tickle(_THIS);
icculus@5981
   959
slouken@1951
   960
void
slouken@1951
   961
X11_PumpEvents(_THIS)
slouken@1951
   962
{
slouken@1951
   963
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   964
slouken@3025
   965
    /* Update activity every 30 seconds to prevent screensaver */
slouken@3025
   966
    if (_this->suspend_screensaver) {
slouken@3025
   967
        Uint32 now = SDL_GetTicks();
slouken@3025
   968
        if (!data->screensaver_activity ||
slouken@7857
   969
            SDL_TICKS_PASSED(now, data->screensaver_activity + 30000)) {
icculus@7827
   970
            X11_XResetScreenSaver(data->display);
icculus@6774
   971
icculus@6774
   972
            #if SDL_USE_LIBDBUS
icculus@6774
   973
            SDL_dbus_screensaver_tickle(_this);
icculus@6774
   974
            #endif
icculus@6774
   975
slouken@3025
   976
            data->screensaver_activity = now;
slouken@3025
   977
        }
slouken@7191
   978
    }
slouken@3025
   979
slouken@1951
   980
    /* Keep processing pending events */
slouken@1951
   981
    while (X11_Pending(data->display)) {
slouken@1951
   982
        X11_DispatchEvent(_this);
slouken@1951
   983
    }
slouken@6504
   984
slouken@6504
   985
    /* FIXME: Only need to do this when there are pending focus changes */
slouken@6504
   986
    X11_HandleFocusChanges(_this);
slouken@1951
   987
}
slouken@1951
   988
slouken@3030
   989
slouken@1951
   990
void
slouken@3025
   991
X11_SuspendScreenSaver(_THIS)
slouken@1951
   992
{
slouken@5408
   993
#if SDL_VIDEO_DRIVER_X11_XSCRNSAVER
slouken@3025
   994
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@3025
   995
    int dummy;
slouken@3025
   996
    int major_version, minor_version;
slouken@1951
   997
slouken@3030
   998
    if (SDL_X11_HAVE_XSS) {
icculus@7827
   999
        /* X11_XScreenSaverSuspend was introduced in MIT-SCREEN-SAVER 1.1 */
icculus@7827
  1000
        if (!X11_XScreenSaverQueryExtension(data->display, &dummy, &dummy) ||
icculus@7827
  1001
            !X11_XScreenSaverQueryVersion(data->display,
slouken@3030
  1002
                                      &major_version, &minor_version) ||
slouken@3030
  1003
            major_version < 1 || (major_version == 1 && minor_version < 1)) {
slouken@3030
  1004
            return;
slouken@3030
  1005
        }
slouken@1951
  1006
icculus@7827
  1007
        X11_XScreenSaverSuspend(data->display, _this->suspend_screensaver);
icculus@7827
  1008
        X11_XResetScreenSaver(data->display);
slouken@1951
  1009
    }
slouken@3030
  1010
#endif
slouken@1951
  1011
icculus@6774
  1012
#if SDL_USE_LIBDBUS
slouken@3030
  1013
    if (_this->suspend_screensaver) {
icculus@6774
  1014
        SDL_dbus_screensaver_tickle(_this);
slouken@3030
  1015
    }
slouken@3025
  1016
#endif
slouken@1951
  1017
}
slouken@1951
  1018
slouken@5481
  1019
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
  1020
slouken@1951
  1021
/* vi: set ts=4 sw=4 expandtab: */