src/video/x11/SDL_x11events.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 27 Oct 2015 11:17:32 -0700
changeset 9898 0da384bef562
parent 9845 41ce18cb4b3b
child 9973 4d8a561cf978
permissions -rw-r--r--
Add a new SDL_KEYMAPCHANGED SDL event to abstract notification of keyboard layout or input language changes.
slouken@1951
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9619
     3
  Copyright (C) 1997-2015 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@4934
    32
#include "SDL_x11touch.h"
dimitris@6316
    33
#include "SDL_x11xinput2.h"
slouken@1951
    34
#include "../../events/SDL_events_c.h"
slouken@2940
    35
#include "../../events/SDL_mouse_c.h"
jim@4642
    36
#include "../../events/SDL_touch_c.h"
slouken@1951
    37
slouken@4465
    38
#include "SDL_timer.h"
slouken@3241
    39
#include "SDL_syswm.h"
slouken@3241
    40
jim@4642
    41
#include <stdio.h>
jim@4642
    42
slouken@9669
    43
/*#define DEBUG_XEVENTS*/
slouken@9669
    44
ionut@8946
    45
#ifndef _NET_WM_MOVERESIZE_SIZE_TOPLEFT
ionut@8946
    46
#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT      0
ionut@8946
    47
#endif
ionut@8946
    48
ionut@8946
    49
#ifndef _NET_WM_MOVERESIZE_SIZE_TOP
ionut@8946
    50
#define _NET_WM_MOVERESIZE_SIZE_TOP          1
ionut@8946
    51
#endif
ionut@8946
    52
ionut@8946
    53
#ifndef _NET_WM_MOVERESIZE_SIZE_TOPRIGHT
ionut@8946
    54
#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT     2
ionut@8946
    55
#endif
ionut@8946
    56
ionut@8946
    57
#ifndef _NET_WM_MOVERESIZE_SIZE_RIGHT
ionut@8946
    58
#define _NET_WM_MOVERESIZE_SIZE_RIGHT        3
ionut@8946
    59
#endif
ionut@8946
    60
ionut@8946
    61
#ifndef _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT
ionut@8946
    62
#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT  4
ionut@8946
    63
#endif
ionut@8946
    64
ionut@8946
    65
#ifndef _NET_WM_MOVERESIZE_SIZE_BOTTOM
ionut@8946
    66
#define _NET_WM_MOVERESIZE_SIZE_BOTTOM       5
ionut@8946
    67
#endif
ionut@8946
    68
ionut@8946
    69
#ifndef _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT
ionut@8946
    70
#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT   6
ionut@8946
    71
#endif
ionut@8946
    72
ionut@8946
    73
#ifndef _NET_WM_MOVERESIZE_SIZE_LEFT
ionut@8946
    74
#define _NET_WM_MOVERESIZE_SIZE_LEFT         7
ionut@8946
    75
#endif
ionut@8946
    76
icculus@8934
    77
#ifndef _NET_WM_MOVERESIZE_MOVE
ionut@8946
    78
#define _NET_WM_MOVERESIZE_MOVE              8
icculus@8934
    79
#endif
icculus@8934
    80
slouken@7001
    81
typedef struct {
slouken@7191
    82
    unsigned char *data;
slouken@7191
    83
    int format, count;
slouken@7191
    84
    Atom type;
slouken@7001
    85
} SDL_x11Prop;
slouken@7001
    86
slouken@7001
    87
/* Reads property
icculus@7827
    88
   Must call X11_XFree on results
slouken@7001
    89
 */
slouken@7191
    90
static void X11_ReadProperty(SDL_x11Prop *p, Display *disp, Window w, Atom prop)
slouken@7001
    91
{
slouken@7001
    92
    unsigned char *ret=NULL;
slouken@7001
    93
    Atom type;
slouken@7001
    94
    int fmt;
slouken@7001
    95
    unsigned long count;
slouken@7001
    96
    unsigned long bytes_left;
slouken@7191
    97
    int bytes_fetch = 0;
slouken@7191
    98
slouken@7001
    99
    do {
icculus@7827
   100
        if (ret != 0) X11_XFree(ret);
icculus@7827
   101
        X11_XGetWindowProperty(disp, w, prop, 0, bytes_fetch, False, AnyPropertyType, &type, &fmt, &count, &bytes_left, &ret);
slouken@7001
   102
        bytes_fetch += bytes_left;
slouken@7001
   103
    } while (bytes_left != 0);
slouken@7191
   104
slouken@7001
   105
    p->data=ret;
slouken@7001
   106
    p->format=fmt;
slouken@7001
   107
    p->count=count;
slouken@7001
   108
    p->type=type;
slouken@7001
   109
}
slouken@7001
   110
slouken@7001
   111
/* Find text-uri-list in a list of targets and return it's atom
slouken@7001
   112
   if available, else return None */
slouken@7001
   113
static Atom X11_PickTarget(Display *disp, Atom list[], int list_count)
slouken@7001
   114
{
slouken@7001
   115
    Atom request = None;
slouken@7001
   116
    char *name;
slouken@7001
   117
    int i;
slouken@7001
   118
    for (i=0; i < list_count && request == None; i++) {
icculus@7827
   119
        name = X11_XGetAtomName(disp, list[i]);
slouken@7001
   120
        if (strcmp("text/uri-list", name)==0) request = list[i];
icculus@7827
   121
        X11_XFree(name);
slouken@7001
   122
    }
slouken@7001
   123
    return request;
slouken@7001
   124
}
slouken@7001
   125
slouken@7001
   126
/* Wrapper for X11_PickTarget for a maximum of three targets, a special
slouken@7001
   127
   case in the Xdnd protocol */
slouken@7001
   128
static Atom X11_PickTargetFromAtoms(Display *disp, Atom a0, Atom a1, Atom a2)
slouken@7001
   129
{
slouken@7001
   130
    int count=0;
slouken@7001
   131
    Atom atom[3];
slouken@7001
   132
    if (a0 != None) atom[count++] = a0;
slouken@7001
   133
    if (a1 != None) atom[count++] = a1;
slouken@7001
   134
    if (a2 != None) atom[count++] = a2;
slouken@7001
   135
    return X11_PickTarget(disp, atom, count);
slouken@7001
   136
}
slouken@6474
   137
icculus@7475
   138
struct KeyRepeatCheckData
icculus@7475
   139
{
icculus@7475
   140
    XEvent *event;
icculus@7475
   141
    SDL_bool found;
icculus@7475
   142
};
icculus@7475
   143
icculus@7475
   144
static Bool X11_KeyRepeatCheckIfEvent(Display *display, XEvent *chkev,
icculus@7475
   145
    XPointer arg)
icculus@7475
   146
{
icculus@7475
   147
    struct KeyRepeatCheckData *d = (struct KeyRepeatCheckData *) arg;
icculus@7475
   148
    if (chkev->type == KeyPress &&
icculus@7475
   149
        chkev->xkey.keycode == d->event->xkey.keycode &&
icculus@7475
   150
        chkev->xkey.time - d->event->xkey.time < 2)
icculus@7475
   151
        d->found = SDL_TRUE;
icculus@7475
   152
    return False;
icculus@7475
   153
}
icculus@7475
   154
slouken@4565
   155
/* Check to see if this is a repeated key.
slouken@4565
   156
   (idea shamelessly lifted from GII -- thanks guys! :)
slouken@4565
   157
 */
slouken@4565
   158
static SDL_bool X11_KeyRepeat(Display *display, XEvent *event)
slouken@4565
   159
{
icculus@7475
   160
    XEvent dummyev;
icculus@7475
   161
    struct KeyRepeatCheckData d;
icculus@7475
   162
    d.event = event;
icculus@7475
   163
    d.found = SDL_FALSE;
icculus@7827
   164
    if (X11_XPending(display))
icculus@7827
   165
        X11_XCheckIfEvent(display, &dummyev, X11_KeyRepeatCheckIfEvent,
icculus@7475
   166
            (XPointer) &d);
icculus@7475
   167
    return d.found;
icculus@7475
   168
}
slouken@4565
   169
icculus@9839
   170
static SDL_bool
icculus@9839
   171
X11_IsWheelEvent(Display * display,XEvent * event,int * xticks,int * yticks)
icculus@7475
   172
{
icculus@9839
   173
    /* according to the xlib docs, no specific mouse wheel events exist.
icculus@9839
   174
       However, the defacto standard is that the vertical wheel is X buttons
icculus@9839
   175
       4 (up) and 5 (down) and a horizontal wheel is 6 (left) and 7 (right). */
slouken@4565
   176
icculus@9839
   177
    /* Xlib defines "Button1" through 5, so we just use literals here. */
icculus@9839
   178
    switch (event->xbutton.button) {
icculus@9839
   179
        case 4: *yticks = 1; return SDL_TRUE;
icculus@9839
   180
        case 5: *yticks = -1; return SDL_TRUE;
icculus@9839
   181
        case 6: *xticks = 1; return SDL_TRUE;
icculus@9839
   182
        case 7: *xticks = -1; return SDL_TRUE;
icculus@9839
   183
        default: break;
slouken@5530
   184
    }
slouken@5530
   185
    return SDL_FALSE;
slouken@5530
   186
}
slouken@5530
   187
slouken@8870
   188
/* Decodes URI escape sequences in string buf of len bytes
slouken@8870
   189
   (excluding the terminating NULL byte) in-place. Since
slouken@8870
   190
   URI-encoded characters take three times the space of
slouken@8870
   191
   normal characters, this should not be an issue.
slouken@8870
   192
slouken@8870
   193
   Returns the number of decoded bytes that wound up in
slouken@8870
   194
   the buffer, excluding the terminating NULL byte.
slouken@8870
   195
slouken@8870
   196
   The buffer is guaranteed to be NULL-terminated but
slouken@8870
   197
   may contain embedded NULL bytes.
slouken@8870
   198
slouken@8870
   199
   On error, -1 is returned.
slouken@8870
   200
 */
slouken@8870
   201
int X11_URIDecode(char *buf, int len) {
slouken@8870
   202
    int ri, wi, di;
slouken@8870
   203
    char decode = '\0';
slouken@8870
   204
    if (buf == NULL || len < 0) {
slouken@8870
   205
        errno = EINVAL;
slouken@8870
   206
        return -1;
slouken@8870
   207
    }
slouken@8870
   208
    if (len == 0) {
slouken@8870
   209
        len = SDL_strlen(buf);
slouken@8870
   210
    }
slouken@8870
   211
    for (ri = 0, wi = 0, di = 0; ri < len && wi < len; ri += 1) {
slouken@8870
   212
        if (di == 0) {
slouken@8870
   213
            /* start decoding */
slouken@8870
   214
            if (buf[ri] == '%') {
slouken@8870
   215
                decode = '\0';
slouken@8870
   216
                di += 1;
slouken@8870
   217
                continue;
slouken@8870
   218
            }
slouken@8870
   219
            /* normal write */
slouken@8870
   220
            buf[wi] = buf[ri];
slouken@8870
   221
            wi += 1;
slouken@8870
   222
            continue;
slouken@8870
   223
        } else if (di == 1 || di == 2) {
slouken@8870
   224
            char off = '\0';
slouken@8870
   225
            char isa = buf[ri] >= 'a' && buf[ri] <= 'f';
slouken@8870
   226
            char isA = buf[ri] >= 'A' && buf[ri] <= 'F';
slouken@8870
   227
            char isn = buf[ri] >= '0' && buf[ri] <= '9';
slouken@8870
   228
            if (!(isa || isA || isn)) {
slouken@8870
   229
                /* not a hexadecimal */
slouken@8870
   230
                int sri;
slouken@8870
   231
                for (sri = ri - di; sri <= ri; sri += 1) {
slouken@8870
   232
                    buf[wi] = buf[sri];
slouken@8870
   233
                    wi += 1;
slouken@8870
   234
                }
slouken@8870
   235
                di = 0;
slouken@8870
   236
                continue;
slouken@8870
   237
            }
slouken@8870
   238
            /* itsy bitsy magicsy */
slouken@8870
   239
            if (isn) {
slouken@8870
   240
                off = 0 - '0';
slouken@8870
   241
            } else if (isa) {
slouken@8870
   242
                off = 10 - 'a';
slouken@8870
   243
            } else if (isA) {
slouken@8870
   244
                off = 10 - 'A';
slouken@8870
   245
            }
slouken@8870
   246
            decode |= (buf[ri] + off) << (2 - di) * 4;
slouken@8870
   247
            if (di == 2) {
slouken@8870
   248
                buf[wi] = decode;
slouken@8870
   249
                wi += 1;
slouken@8870
   250
                di = 0;
slouken@8870
   251
            } else {
slouken@8870
   252
                di += 1;
slouken@8870
   253
            }
slouken@8870
   254
            continue;
slouken@8870
   255
        }
slouken@8870
   256
    }
slouken@8870
   257
    buf[wi] = '\0';
slouken@8870
   258
    return wi;
slouken@8870
   259
}
slouken@8870
   260
slouken@7001
   261
/* Convert URI to local filename
slouken@7001
   262
   return filename if possible, else NULL
slouken@7001
   263
*/
slouken@7001
   264
static char* X11_URIToLocal(char* uri) {
slouken@7001
   265
    char *file = NULL;
gabomdq@7679
   266
    SDL_bool local;
slouken@7001
   267
slouken@7001
   268
    if (memcmp(uri,"file:/",6) == 0) uri += 6;      /* local file? */
slouken@7001
   269
    else if (strstr(uri,":/") != NULL) return file; /* wrong scheme */
slouken@7001
   270
slouken@9666
   271
    local = uri[0] != '/' || (uri[0] != '\0' && uri[1] == '/');
slouken@7001
   272
slouken@7001
   273
    /* got a hostname? */
slouken@9666
   274
    if (!local && uri[0] == '/' && uri[2] != '/') {
slouken@9666
   275
      char* hostname_end = strchr(uri+1, '/');
slouken@9666
   276
      if (hostname_end != NULL) {
slouken@7001
   277
          char hostname[ 257 ];
slouken@9666
   278
          if (gethostname(hostname, 255) == 0) {
slouken@7001
   279
            hostname[ 256 ] = '\0';
slouken@9666
   280
            if (memcmp(uri+1, hostname, hostname_end - (uri+1)) == 0) {
slouken@7001
   281
                uri = hostname_end + 1;
slouken@7001
   282
                local = SDL_TRUE;
slouken@7001
   283
            }
slouken@7001
   284
          }
slouken@7001
   285
      }
slouken@7001
   286
    }
slouken@9666
   287
    if (local) {
slouken@7001
   288
      file = uri;
slouken@8870
   289
      /* Convert URI escape sequences to real characters */
slouken@8870
   290
      X11_URIDecode(file, 0);
slouken@9666
   291
      if (uri[1] == '/') {
slouken@7001
   292
          file++;
slouken@7001
   293
      } else {
slouken@7001
   294
          file--;
slouken@7001
   295
      }
slouken@7001
   296
    }
slouken@7001
   297
    return file;
slouken@7001
   298
}
slouken@6311
   299
slouken@6311
   300
#if SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS
slouken@6311
   301
static void X11_HandleGenericEvent(SDL_VideoData *videodata,XEvent event)
slouken@6311
   302
{
icculus@7402
   303
    /* event is a union, so cookie == &event, but this is type safe. */
icculus@7402
   304
    XGenericEventCookie *cookie = &event.xcookie;
icculus@7827
   305
    if (X11_XGetEventData(videodata->display, cookie)) {
urkle@7214
   306
        X11_HandleXinput2Event(videodata, cookie);
icculus@7827
   307
        X11_XFreeEventData(videodata->display, cookie);
urkle@7214
   308
    }
slouken@6311
   309
}
slouken@6311
   310
#endif /* SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS */
slouken@6311
   311
slouken@9667
   312
static unsigned
slouken@9667
   313
X11_GetNumLockModifierMask(_THIS)
slouken@9667
   314
{
slouken@9667
   315
    SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
slouken@9667
   316
    Display *display = viddata->display;
slouken@9667
   317
    unsigned num_mask = 0;
slouken@9667
   318
    int i, j;
slouken@9667
   319
    XModifierKeymap *xmods;
slouken@9667
   320
    unsigned n;
slouken@9667
   321
slouken@9667
   322
    xmods = X11_XGetModifierMapping(display);
slouken@9667
   323
    n = xmods->max_keypermod;
slouken@9667
   324
    for(i = 3; i < 8; i++) {
slouken@9667
   325
        for(j = 0; j < n; j++) {
slouken@9667
   326
            KeyCode kc = xmods->modifiermap[i * n + j];
slouken@9667
   327
            if (viddata->key_layout[kc] == SDL_SCANCODE_NUMLOCKCLEAR) {
slouken@9667
   328
                num_mask = 1 << i;
slouken@9667
   329
                break;
slouken@9667
   330
            }
slouken@9667
   331
        }
slouken@9667
   332
    }
slouken@9667
   333
    X11_XFreeModifiermap(xmods);
slouken@9667
   334
slouken@9667
   335
    return num_mask;
slouken@9667
   336
}
slouken@9667
   337
slouken@9667
   338
static void
slouken@9669
   339
X11_ReconcileKeyboardState(_THIS)
slouken@9667
   340
{
slouken@9667
   341
    SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
slouken@9667
   342
    Display *display = viddata->display;
slouken@9667
   343
    char keys[32];
slouken@9667
   344
    int keycode;
slouken@9667
   345
    Window junk_window;
slouken@9667
   346
    int x, y;
slouken@9667
   347
    unsigned int mask;
slouken@9667
   348
slouken@9667
   349
    X11_XQueryKeymap(display, keys);
slouken@9667
   350
slouken@9667
   351
    /* Get the keyboard modifier state */
slouken@9667
   352
    if (X11_XQueryPointer(display, DefaultRootWindow(display), &junk_window, &junk_window, &x, &y, &x, &y, &mask)) {
slouken@9667
   353
        unsigned num_mask = X11_GetNumLockModifierMask(_this);
slouken@9667
   354
        const Uint8 *keystate = SDL_GetKeyboardState(NULL);
slouken@9667
   355
        Uint8 capslockState = keystate[SDL_SCANCODE_CAPSLOCK];
slouken@9667
   356
        Uint8 numlockState = keystate[SDL_SCANCODE_NUMLOCKCLEAR];
slouken@9667
   357
slouken@9667
   358
        /* Toggle key mod state if needed */
slouken@9667
   359
        if (!!(mask & LockMask) != !!(SDL_GetModState() & KMOD_CAPS)) {
slouken@9667
   360
            SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_CAPSLOCK);
slouken@9667
   361
            if (capslockState == SDL_RELEASED) {
slouken@9667
   362
                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_CAPSLOCK);
slouken@9667
   363
            }
slouken@9667
   364
        }
slouken@9667
   365
slouken@9667
   366
        if (!!(mask & num_mask) != !!(SDL_GetModState() & KMOD_NUM)) {
slouken@9667
   367
            SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_NUMLOCKCLEAR);
slouken@9667
   368
            if (numlockState == SDL_RELEASED) {
slouken@9667
   369
                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_NUMLOCKCLEAR);
slouken@9667
   370
            }
slouken@9667
   371
        }
slouken@9667
   372
    }
slouken@9667
   373
slouken@9667
   374
    for (keycode = 0; keycode < 256; ++keycode) {
slouken@9667
   375
        if (keys[keycode / 8] & (1 << (keycode % 8))) {
slouken@9667
   376
            SDL_SendKeyboardKey(SDL_PRESSED, viddata->key_layout[keycode]);
slouken@9667
   377
        } else {
slouken@9667
   378
            SDL_SendKeyboardKey(SDL_RELEASED, viddata->key_layout[keycode]);
slouken@9667
   379
        }
slouken@9667
   380
    }
slouken@9667
   381
}
slouken@9667
   382
slouken@6311
   383
slouken@6481
   384
static void
slouken@9666
   385
X11_DispatchFocusIn(_THIS, SDL_WindowData *data)
slouken@6504
   386
{
slouken@6504
   387
#ifdef DEBUG_XEVENTS
slouken@6504
   388
    printf("window %p: Dispatching FocusIn\n", data);
slouken@6504
   389
#endif
slouken@6504
   390
    SDL_SetKeyboardFocus(data->window);
slouken@9669
   391
    X11_ReconcileKeyboardState(_this);
slouken@6504
   392
#ifdef X_HAVE_UTF8_STRING
slouken@6504
   393
    if (data->ic) {
icculus@7827
   394
        X11_XSetICFocus(data->ic);
slouken@6504
   395
    }
slouken@6504
   396
#endif
alex@9096
   397
#ifdef SDL_USE_IBUS
alex@9096
   398
    SDL_IBus_SetFocus(SDL_TRUE);
alex@9096
   399
#endif
slouken@6504
   400
}
slouken@6504
   401
slouken@6504
   402
static void
slouken@9666
   403
X11_DispatchFocusOut(_THIS, SDL_WindowData *data)
slouken@6504
   404
{
slouken@6504
   405
#ifdef DEBUG_XEVENTS
slouken@6504
   406
    printf("window %p: Dispatching FocusOut\n", data);
slouken@6504
   407
#endif
slouken@7902
   408
    /* If another window has already processed a focus in, then don't try to
slouken@7902
   409
     * remove focus here.  Doing so will incorrectly remove focus from that
slouken@7902
   410
     * window, and the focus lost event for this window will have already
slouken@7902
   411
     * been dispatched anyway. */
slouken@7902
   412
    if (data->window == SDL_GetKeyboardFocus()) {
slouken@7902
   413
        SDL_SetKeyboardFocus(NULL);
slouken@7902
   414
    }
slouken@6504
   415
#ifdef X_HAVE_UTF8_STRING
slouken@6504
   416
    if (data->ic) {
icculus@7827
   417
        X11_XUnsetICFocus(data->ic);
slouken@6504
   418
    }
slouken@6504
   419
#endif
alex@9096
   420
#ifdef SDL_USE_IBUS
alex@9096
   421
    SDL_IBus_SetFocus(SDL_FALSE);
alex@9096
   422
#endif
slouken@6504
   423
}
slouken@6504
   424
slouken@6504
   425
static void
slouken@6481
   426
X11_DispatchMapNotify(SDL_WindowData *data)
slouken@6481
   427
{
slouken@6481
   428
    SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@6481
   429
    SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@6481
   430
}
slouken@6481
   431
slouken@6481
   432
static void
slouken@6481
   433
X11_DispatchUnmapNotify(SDL_WindowData *data)
slouken@6481
   434
{
slouken@6481
   435
    SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@6481
   436
    SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@6481
   437
}
slouken@6311
   438
slouken@1951
   439
static void
icculus@8933
   440
InitiateWindowMove(_THIS, const SDL_WindowData *data, const SDL_Point *point)
rush@8932
   441
{
icculus@8933
   442
    SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
icculus@8933
   443
    SDL_Window* window = data->window;
icculus@8933
   444
    Display *display = viddata->display;
icculus@8954
   445
    XEvent evt;
rush@8932
   446
icculus@8933
   447
    /* !!! FIXME: we need to regrab this if necessary when the drag is done. */
rush@8932
   448
    X11_XUngrabPointer(display, 0L);
rush@8932
   449
    X11_XFlush(display);
rush@8932
   450
rush@8932
   451
    evt.xclient.type = ClientMessage;
icculus@8933
   452
    evt.xclient.window = data->xwindow;
ionut@8946
   453
    evt.xclient.message_type = X11_XInternAtom(display, "_NET_WM_MOVERESIZE", True);
rush@8932
   454
    evt.xclient.format = 32;
icculus@8933
   455
    evt.xclient.data.l[0] = window->x + point->x;
icculus@8933
   456
    evt.xclient.data.l[1] = window->y + point->y;
icculus@8934
   457
    evt.xclient.data.l[2] = _NET_WM_MOVERESIZE_MOVE;
rush@8932
   458
    evt.xclient.data.l[3] = Button1;
rush@8932
   459
    evt.xclient.data.l[4] = 0;
rush@8932
   460
    X11_XSendEvent(display, DefaultRootWindow(display), False, SubstructureRedirectMask | SubstructureNotifyMask, &evt);
rush@8932
   461
rush@8932
   462
    X11_XSync(display, 0);
rush@8932
   463
}
rush@8932
   464
ionut@8946
   465
static void
ionut@8946
   466
InitiateWindowResize(_THIS, const SDL_WindowData *data, const SDL_Point *point, int direction)
ionut@8946
   467
{
ionut@8946
   468
    SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
ionut@8946
   469
    SDL_Window* window = data->window;
ionut@8946
   470
    Display *display = viddata->display;
icculus@8954
   471
    XEvent evt;
ionut@8946
   472
ionut@8946
   473
    if (direction < _NET_WM_MOVERESIZE_SIZE_TOPLEFT || direction > _NET_WM_MOVERESIZE_SIZE_LEFT)
ionut@8946
   474
        return;
ionut@8946
   475
ionut@8946
   476
    /* !!! FIXME: we need to regrab this if necessary when the drag is done. */
ionut@8946
   477
    X11_XUngrabPointer(display, 0L);
ionut@8946
   478
    X11_XFlush(display);
ionut@8946
   479
ionut@8946
   480
    evt.xclient.type = ClientMessage;
ionut@8946
   481
    evt.xclient.window = data->xwindow;
ionut@8946
   482
    evt.xclient.message_type = X11_XInternAtom(display, "_NET_WM_MOVERESIZE", True);
ionut@8946
   483
    evt.xclient.format = 32;
ionut@8946
   484
    evt.xclient.data.l[0] = window->x + point->x;
ionut@8946
   485
    evt.xclient.data.l[1] = window->y + point->y;
ionut@8946
   486
    evt.xclient.data.l[2] = direction;
ionut@8946
   487
    evt.xclient.data.l[3] = Button1;
ionut@8946
   488
    evt.xclient.data.l[4] = 0;
ionut@8946
   489
    X11_XSendEvent(display, DefaultRootWindow(display), False, SubstructureRedirectMask | SubstructureNotifyMask, &evt);
ionut@8946
   490
ionut@8946
   491
    X11_XSync(display, 0);
ionut@8946
   492
}
ionut@8946
   493
icculus@8933
   494
static SDL_bool
icculus@8935
   495
ProcessHitTest(_THIS, const SDL_WindowData *data, const XEvent *xev)
rush@8932
   496
{
icculus@8935
   497
    SDL_Window *window = data->window;
icculus@8933
   498
icculus@8935
   499
    if (window->hit_test) {
icculus@8933
   500
        const SDL_Point point = { xev->xbutton.x, xev->xbutton.y };
icculus@8935
   501
        const SDL_HitTestResult rc = window->hit_test(window, &point, window->hit_test_data);
icculus@9555
   502
        static const int directions[] = {
icculus@9555
   503
            _NET_WM_MOVERESIZE_SIZE_TOPLEFT, _NET_WM_MOVERESIZE_SIZE_TOP,
icculus@9555
   504
            _NET_WM_MOVERESIZE_SIZE_TOPRIGHT, _NET_WM_MOVERESIZE_SIZE_RIGHT,
icculus@9555
   505
            _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT, _NET_WM_MOVERESIZE_SIZE_BOTTOM,
icculus@9555
   506
            _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT, _NET_WM_MOVERESIZE_SIZE_LEFT
icculus@9555
   507
        };
icculus@9555
   508
ionut@8946
   509
        switch (rc) {
icculus@9555
   510
            case SDL_HITTEST_DRAGGABLE:
icculus@9555
   511
                InitiateWindowMove(_this, data, &point);
icculus@9555
   512
                return SDL_TRUE;
icculus@9555
   513
icculus@9555
   514
            case SDL_HITTEST_RESIZE_TOPLEFT:
icculus@9555
   515
            case SDL_HITTEST_RESIZE_TOP:
icculus@9555
   516
            case SDL_HITTEST_RESIZE_TOPRIGHT:
icculus@9555
   517
            case SDL_HITTEST_RESIZE_RIGHT:
icculus@9555
   518
            case SDL_HITTEST_RESIZE_BOTTOMRIGHT:
icculus@9555
   519
            case SDL_HITTEST_RESIZE_BOTTOM:
icculus@9555
   520
            case SDL_HITTEST_RESIZE_BOTTOMLEFT:
icculus@9555
   521
            case SDL_HITTEST_RESIZE_LEFT:
icculus@9555
   522
                InitiateWindowResize(_this, data, &point, directions[rc - SDL_HITTEST_RESIZE_TOPLEFT]);
icculus@9555
   523
                return SDL_TRUE;
icculus@9555
   524
icculus@9555
   525
            default: return SDL_FALSE;
rush@8932
   526
        }
rush@8932
   527
    }
icculus@8933
   528
icculus@9555
   529
    return SDL_FALSE;
rush@8932
   530
}
rush@8932
   531
pgriffais@9133
   532
static void
slouken@1951
   533
X11_DispatchEvent(_THIS)
slouken@1951
   534
{
slouken@1951
   535
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@4518
   536
    Display *display = videodata->display;
slouken@1951
   537
    SDL_WindowData *data;
slouken@1951
   538
    XEvent xevent;
slouken@7943
   539
    int orig_event_type;
slouken@7943
   540
    KeyCode orig_keycode;
slouken@7943
   541
    XClientMessageEvent m;
slouken@1951
   542
    int i;
slouken@1951
   543
slouken@1951
   544
    SDL_zero(xevent);           /* valgrind fix. --ryan. */
icculus@7827
   545
    X11_XNextEvent(display, &xevent);
slouken@1951
   546
slouken@7943
   547
    /* Save the original keycode for dead keys, which are filtered out by
slouken@7943
   548
       the XFilterEvent() call below.
slouken@7943
   549
    */
slouken@7943
   550
    orig_event_type = xevent.type;
slouken@7943
   551
    if (orig_event_type == KeyPress || orig_event_type == KeyRelease) {
slouken@7943
   552
        orig_keycode = xevent.xkey.keycode;
slouken@7943
   553
    } else {
slouken@7943
   554
        orig_keycode = 0;
slouken@7943
   555
    }
slouken@7943
   556
slouken@7943
   557
    /* filter events catchs XIM events and sends them to the correct handler */
icculus@7827
   558
    if (X11_XFilterEvent(&xevent, None) == True) {
bob@2327
   559
#if 0
bob@2328
   560
        printf("Filtered event type = %d display = %d window = %d\n",
bob@2328
   561
               xevent.type, xevent.xany.display, xevent.xany.window);
bob@2325
   562
#endif
slouken@7943
   563
        if (orig_keycode) {
slouken@7943
   564
            /* Make sure dead key press/release events are sent */
slouken@8819
   565
            /* Actually, don't do this because it causes double-delivery
slouken@8819
   566
               of some keys on Ubuntu 14.04 (bug 2526)
slouken@7943
   567
            SDL_Scancode scancode = videodata->key_layout[orig_keycode];
slouken@7943
   568
            if (orig_event_type == KeyPress) {
slouken@7943
   569
                SDL_SendKeyboardKey(SDL_PRESSED, scancode);
slouken@7943
   570
            } else {
slouken@7943
   571
                SDL_SendKeyboardKey(SDL_RELEASED, scancode);
slouken@7943
   572
            }
slouken@8819
   573
            */
slouken@7943
   574
        }
bob@2325
   575
        return;
bob@2325
   576
    }
bob@2325
   577
slouken@1951
   578
    /* Send a SDL_SYSWMEVENT if the application wants them */
slouken@4429
   579
    if (SDL_GetEventState(SDL_SYSWMEVENT) == SDL_ENABLE) {
slouken@1951
   580
        SDL_SysWMmsg wmmsg;
slouken@1951
   581
slouken@1951
   582
        SDL_VERSION(&wmmsg.version);
slouken@1951
   583
        wmmsg.subsystem = SDL_SYSWM_X11;
slouken@5056
   584
        wmmsg.msg.x11.event = xevent;
slouken@1951
   585
        SDL_SendSysWMEvent(&wmmsg);
slouken@1951
   586
    }
slouken@1951
   587
slouken@6311
   588
#if SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS
slouken@6311
   589
    if(xevent.type == GenericEvent) {
slouken@6311
   590
        X11_HandleGenericEvent(videodata,xevent);
slouken@6311
   591
        return;
slouken@6311
   592
    }
slouken@6311
   593
#endif
slouken@6311
   594
slouken@6476
   595
#if 0
slouken@6476
   596
    printf("type = %d display = %d window = %d\n",
slouken@6476
   597
           xevent.type, xevent.xany.display, xevent.xany.window);
slouken@6476
   598
#endif
slouken@6476
   599
slouken@1951
   600
    data = NULL;
bob@2324
   601
    if (videodata && videodata->windowlist) {
bob@2324
   602
        for (i = 0; i < videodata->numwindows; ++i) {
bob@2324
   603
            if ((videodata->windowlist[i] != NULL) &&
slouken@3685
   604
                (videodata->windowlist[i]->xwindow == xevent.xany.window)) {
bob@2324
   605
                data = videodata->windowlist[i];
bob@2324
   606
                break;
bob@2324
   607
            }
slouken@1951
   608
        }
slouken@1951
   609
    }
slouken@1951
   610
    if (!data) {
icculus@9787
   611
        /* The window for KeymapNotify, etc events is 0 */
slouken@9669
   612
        if (xevent.type == KeymapNotify) {
slouken@9669
   613
            if (SDL_GetKeyboardFocus() != NULL) {
slouken@9669
   614
                X11_ReconcileKeyboardState(_this);
slouken@9669
   615
            }
icculus@9787
   616
        } else if (xevent.type == MappingNotify) {
icculus@9787
   617
            /* Has the keyboard layout changed? */
icculus@9788
   618
            const int request = xevent.xmapping.request;
icculus@9788
   619
icculus@9787
   620
#ifdef DEBUG_XEVENTS
icculus@9787
   621
            printf("window %p: MappingNotify!\n", data);
icculus@9787
   622
#endif
icculus@9788
   623
            if ((request == MappingKeyboard) || (request == MappingModifier)) {
icculus@9788
   624
                X11_XRefreshKeyboardMapping(&xevent.xmapping);
icculus@9788
   625
            }
icculus@9788
   626
icculus@9787
   627
            X11_UpdateKeymap(_this);
slouken@9898
   628
            SDL_SendKeymapChangedEvent();
slouken@9669
   629
        }
slouken@1951
   630
        return;
slouken@1951
   631
    }
slouken@4518
   632
slouken@1951
   633
    switch (xevent.type) {
slouken@1951
   634
slouken@1951
   635
        /* Gaining mouse coverage? */
slouken@1951
   636
    case EnterNotify:{
slouken@1951
   637
#ifdef DEBUG_XEVENTS
slouken@6504
   638
            printf("window %p: EnterNotify! (%d,%d,%d)\n", data,
slouken@4558
   639
                   xevent.xcrossing.x,
slouken@4558
   640
                   xevent.xcrossing.y,
bob@3195
   641
                   xevent.xcrossing.mode);
slouken@1951
   642
            if (xevent.xcrossing.mode == NotifyGrab)
slouken@1951
   643
                printf("Mode: NotifyGrab\n");
slouken@1951
   644
            if (xevent.xcrossing.mode == NotifyUngrab)
slouken@1951
   645
                printf("Mode: NotifyUngrab\n");
slouken@1951
   646
#endif
slouken@4465
   647
            SDL_SetMouseFocus(data->window);
jorgen@7097
   648
jorgen@7097
   649
            if (!SDL_GetMouse()->relative_mode) {
jorgen@7097
   650
                SDL_SendMouseMotion(data->window, 0, 0, xevent.xcrossing.x, xevent.xcrossing.y);
jorgen@7097
   651
            }
slouken@1951
   652
        }
slouken@1951
   653
        break;
slouken@1951
   654
        /* Losing mouse coverage? */
slouken@1951
   655
    case LeaveNotify:{
slouken@1951
   656
#ifdef DEBUG_XEVENTS
slouken@6504
   657
            printf("window %p: LeaveNotify! (%d,%d,%d)\n", data,
slouken@4558
   658
                   xevent.xcrossing.x,
slouken@4558
   659
                   xevent.xcrossing.y,
bob@3195
   660
                   xevent.xcrossing.mode);
slouken@1951
   661
            if (xevent.xcrossing.mode == NotifyGrab)
slouken@1951
   662
                printf("Mode: NotifyGrab\n");
slouken@1951
   663
            if (xevent.xcrossing.mode == NotifyUngrab)
slouken@1951
   664
                printf("Mode: NotifyUngrab\n");
slouken@1951
   665
#endif
jorgen@7089
   666
            if (!SDL_GetMouse()->relative_mode) {
jorgen@7089
   667
                SDL_SendMouseMotion(data->window, 0, 0, xevent.xcrossing.x, xevent.xcrossing.y);
jorgen@7089
   668
            }
jorgen@7089
   669
slouken@4558
   670
            if (xevent.xcrossing.mode != NotifyGrab &&
slouken@4558
   671
                xevent.xcrossing.mode != NotifyUngrab &&
slouken@4558
   672
                xevent.xcrossing.detail != NotifyInferior) {
slouken@4465
   673
                SDL_SetMouseFocus(NULL);
slouken@1951
   674
            }
slouken@1951
   675
        }
slouken@1951
   676
        break;
slouken@1951
   677
slouken@1951
   678
        /* Gaining input focus? */
slouken@1951
   679
    case FocusIn:{
gabomdq@7945
   680
            if (xevent.xfocus.mode == NotifyGrab || xevent.xfocus.mode == NotifyUngrab) {
gabomdq@7945
   681
                /* Someone is handling a global hotkey, ignore it */
gabomdq@7945
   682
#ifdef DEBUG_XEVENTS
gabomdq@7945
   683
                printf("window %p: FocusIn (NotifyGrab/NotifyUngrab, ignoring)\n", data);
gabomdq@7945
   684
#endif
gabomdq@7945
   685
                break;
gabomdq@7945
   686
            }
gabomdq@7945
   687
slouken@6660
   688
            if (xevent.xfocus.detail == NotifyInferior) {
slouken@6660
   689
#ifdef DEBUG_XEVENTS
slouken@6660
   690
                printf("window %p: FocusIn (NotifierInferior, ignoring)\n", data);
slouken@6660
   691
#endif
slouken@6660
   692
                break;
slouken@6660
   693
            }
slouken@1951
   694
#ifdef DEBUG_XEVENTS
slouken@6504
   695
            printf("window %p: FocusIn!\n", data);
slouken@1951
   696
#endif
icculus@9319
   697
            if (!videodata->last_mode_change_deadline) /* no recent mode changes */
icculus@9319
   698
            {
icculus@9319
   699
                data->pending_focus = PENDING_FOCUS_NONE;
icculus@9319
   700
                data->pending_focus_time = 0;
slouken@9666
   701
                X11_DispatchFocusIn(_this, data);
icculus@9319
   702
            }
icculus@9319
   703
            else
icculus@9319
   704
            {
icculus@9319
   705
                data->pending_focus = PENDING_FOCUS_IN;
icculus@9319
   706
                data->pending_focus_time = SDL_GetTicks() + PENDING_FOCUS_TIME;
icculus@9319
   707
            }
slouken@1951
   708
        }
slouken@1951
   709
        break;
slouken@1951
   710
slouken@1951
   711
        /* Losing input focus? */
slouken@1951
   712
    case FocusOut:{
gabomdq@7945
   713
            if (xevent.xfocus.mode == NotifyGrab || xevent.xfocus.mode == NotifyUngrab) {
slouken@7942
   714
                /* Someone is handling a global hotkey, ignore it */
slouken@7942
   715
#ifdef DEBUG_XEVENTS
gabomdq@7945
   716
                printf("window %p: FocusOut (NotifyGrab/NotifyUngrab, ignoring)\n", data);
slouken@7942
   717
#endif
slouken@7942
   718
                break;
slouken@7942
   719
            }
slouken@6660
   720
            if (xevent.xfocus.detail == NotifyInferior) {
slouken@6660
   721
                /* We still have focus if a child gets focus */
slouken@6660
   722
#ifdef DEBUG_XEVENTS
slouken@6660
   723
                printf("window %p: FocusOut (NotifierInferior, ignoring)\n", data);
slouken@6660
   724
#endif
slouken@6660
   725
                break;
slouken@6660
   726
            }
slouken@1951
   727
#ifdef DEBUG_XEVENTS
slouken@6504
   728
            printf("window %p: FocusOut!\n", data);
slouken@1951
   729
#endif
icculus@9319
   730
            if (!videodata->last_mode_change_deadline) /* no recent mode changes */
icculus@9319
   731
            {
icculus@9319
   732
                data->pending_focus = PENDING_FOCUS_NONE;
icculus@9319
   733
                data->pending_focus_time = 0;
slouken@9667
   734
                X11_DispatchFocusOut(_this, data);
icculus@9319
   735
            }
icculus@9319
   736
            else
icculus@9319
   737
            {
icculus@9319
   738
                data->pending_focus = PENDING_FOCUS_OUT;
icculus@9319
   739
                data->pending_focus_time = SDL_GetTicks() + PENDING_FOCUS_TIME;
icculus@9319
   740
            }
slouken@1951
   741
        }
slouken@1951
   742
        break;
slouken@1951
   743
slouken@1951
   744
        /* Key press? */
slouken@1951
   745
    case KeyPress:{
slouken@1951
   746
            KeyCode keycode = xevent.xkey.keycode;
bob@2300
   747
            KeySym keysym = NoSymbol;
slouken@2306
   748
            char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];
bob@2325
   749
            Status status = 0;
alex@9096
   750
            SDL_bool handled_by_ime = SDL_FALSE;
slouken@1951
   751
slouken@1951
   752
#ifdef DEBUG_XEVENTS
slouken@6504
   753
            printf("window %p: KeyPress (X11 keycode = 0x%X)\n", data, xevent.xkey.keycode);
slouken@1951
   754
#endif
slouken@4535
   755
#if 1
slouken@7638
   756
            if (videodata->key_layout[keycode] == SDL_SCANCODE_UNKNOWN && keycode) {
slouken@2305
   757
                int min_keycode, max_keycode;
icculus@7827
   758
                X11_XDisplayKeycodes(display, &min_keycode, &max_keycode);
dimitris@6318
   759
#if SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM
icculus@7827
   760
                keysym = X11_XkbKeycodeToKeysym(display, keycode, 0, 0);
dimitris@6318
   761
#else
slouken@9784
   762
                keysym = X11_XKeycodeToKeysym(display, keycode, 0);
dimitris@6318
   763
#endif
bob@2299
   764
                fprintf(stderr,
slouken@4556
   765
                        "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
   766
                        keycode, keycode - min_keycode, keysym,
icculus@7827
   767
                        X11_XKeysymToString(keysym));
bob@2299
   768
            }
bob@2295
   769
#endif
bob@2325
   770
            /* */
slouken@2305
   771
            SDL_zero(text);
bob@2325
   772
#ifdef X_HAVE_UTF8_STRING
bob@2325
   773
            if (data->ic) {
icculus@7827
   774
                X11_Xutf8LookupString(data->ic, &xevent.xkey, text, sizeof(text),
slouken@2738
   775
                                  &keysym, &status);
bob@2325
   776
            }
bob@2325
   777
#else
icculus@9799
   778
            X11_XLookupString(&xevent.xkey, text, sizeof(text), &keysym, NULL);
bob@2325
   779
#endif
icculus@9582
   780
alex@8889
   781
#ifdef SDL_USE_IBUS
alex@8889
   782
            if(SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE){
icculus@9582
   783
                handled_by_ime = SDL_IBus_ProcessKeyEvent(keysym, keycode);
bob@2300
   784
            }
alex@9096
   785
#endif
alex@9096
   786
            if (!handled_by_ime) {
alex@8889
   787
                SDL_SendKeyboardKey(SDL_PRESSED, videodata->key_layout[keycode]);
icculus@9582
   788
                if(*text) {
icculus@9582
   789
                    SDL_SendKeyboardText(text);
icculus@9582
   790
                }
alex@8889
   791
            }
alex@9096
   792
slouken@1951
   793
        }
slouken@1951
   794
        break;
slouken@1951
   795
slouken@1951
   796
        /* Key release? */
slouken@1951
   797
    case KeyRelease:{
slouken@1951
   798
            KeyCode keycode = xevent.xkey.keycode;
slouken@1951
   799
slouken@1951
   800
#ifdef DEBUG_XEVENTS
slouken@6504
   801
            printf("window %p: KeyRelease (X11 keycode = 0x%X)\n", data, xevent.xkey.keycode);
slouken@1951
   802
#endif
slouken@4565
   803
            if (X11_KeyRepeat(display, &xevent)) {
slouken@4565
   804
                /* We're about to get a repeated key down, ignore the key up */
slouken@4565
   805
                break;
slouken@4565
   806
            }
slouken@4465
   807
            SDL_SendKeyboardKey(SDL_RELEASED, videodata->key_layout[keycode]);
slouken@1951
   808
        }
slouken@1951
   809
        break;
slouken@1951
   810
slouken@1951
   811
        /* Have we been iconified? */
slouken@1951
   812
    case UnmapNotify:{
slouken@1951
   813
#ifdef DEBUG_XEVENTS
slouken@6504
   814
            printf("window %p: UnmapNotify!\n", data);
slouken@1951
   815
#endif
slouken@6481
   816
            X11_DispatchUnmapNotify(data);
slouken@1951
   817
        }
slouken@1951
   818
        break;
slouken@1951
   819
slouken@1951
   820
        /* Have we been restored? */
slouken@1951
   821
    case MapNotify:{
slouken@1951
   822
#ifdef DEBUG_XEVENTS
slouken@6504
   823
            printf("window %p: MapNotify!\n", data);
slouken@1951
   824
#endif
slouken@6481
   825
            X11_DispatchMapNotify(data);
slouken@1951
   826
        }
slouken@1951
   827
        break;
slouken@1951
   828
slouken@1951
   829
        /* Have we been resized or moved? */
slouken@1951
   830
    case ConfigureNotify:{
slouken@1951
   831
#ifdef DEBUG_XEVENTS
slouken@6542
   832
            printf("window %p: ConfigureNotify! (position: %d,%d, size: %dx%d)\n", data,
slouken@6542
   833
                   xevent.xconfigure.x, xevent.xconfigure.y,
slouken@1951
   834
                   xevent.xconfigure.width, xevent.xconfigure.height);
slouken@1951
   835
#endif
stapostol@7838
   836
            long border_left = 0;
stapostol@7838
   837
            long border_top = 0;
stapostol@7838
   838
            if (data->xwindow) {
slouken@7839
   839
                Atom _net_frame_extents = X11_XInternAtom(display, "_NET_FRAME_EXTENTS", 0);
slouken@8706
   840
                Atom type;
stapostol@7838
   841
                int format;
slouken@8706
   842
                unsigned long nitems, bytes_after;
stapostol@7838
   843
                unsigned char *property;
slouken@8706
   844
                if (X11_XGetWindowProperty(display, data->xwindow,
slouken@8706
   845
                        _net_frame_extents, 0, 16, 0,
slouken@8706
   846
                        XA_CARDINAL, &type, &format,
slouken@8706
   847
                        &nitems, &bytes_after, &property) == Success) {
slouken@8706
   848
                    if (type != None && nitems == 4)
slouken@8706
   849
                    {
slouken@8706
   850
                        border_left = ((long*)property)[0];
slouken@8706
   851
                        border_top = ((long*)property)[2];
slouken@8706
   852
                    }
slouken@8706
   853
                    X11_XFree(property);
pgriffais@7885
   854
                }
stapostol@7838
   855
            }
stapostol@7838
   856
slouken@6542
   857
            if (xevent.xconfigure.x != data->last_xconfigure.x ||
slouken@6542
   858
                xevent.xconfigure.y != data->last_xconfigure.y) {
slouken@6542
   859
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED,
stapostol@7838
   860
                                    xevent.xconfigure.x - border_left,
stapostol@7838
   861
                                    xevent.xconfigure.y - border_top);
alex@8889
   862
#ifdef SDL_USE_IBUS
alex@8889
   863
                if(SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE){
alex@8889
   864
                    /* Update IBus candidate list position */
alex@8889
   865
                    SDL_IBus_UpdateTextRect(NULL);
alex@8889
   866
                }
alex@8889
   867
#endif
slouken@6542
   868
            }
slouken@6542
   869
            if (xevent.xconfigure.width != data->last_xconfigure.width ||
slouken@6542
   870
                xevent.xconfigure.height != data->last_xconfigure.height) {
slouken@6542
   871
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED,
slouken@6542
   872
                                    xevent.xconfigure.width,
slouken@6542
   873
                                    xevent.xconfigure.height);
slouken@6542
   874
            }
slouken@6542
   875
            data->last_xconfigure = xevent.xconfigure;
slouken@1951
   876
        }
slouken@1951
   877
        break;
slouken@1951
   878
slouken@1951
   879
        /* Have we been requested to quit (or another client message?) */
slouken@1951
   880
    case ClientMessage:{
slouken@7001
   881
slouken@9245
   882
            static int xdnd_version=0;
slouken@7191
   883
slouken@7001
   884
            if (xevent.xclient.message_type == videodata->XdndEnter) {
slouken@9245
   885
slouken@7001
   886
                SDL_bool use_list = xevent.xclient.data.l[1] & 1;
slouken@7001
   887
                data->xdnd_source = xevent.xclient.data.l[0];
slouken@9666
   888
                xdnd_version = (xevent.xclient.data.l[1] >> 24);
slouken@9245
   889
#ifdef DEBUG_XEVENTS
slouken@9245
   890
                printf("XID of source window : %ld\n", data->xdnd_source);
icculus@9705
   891
                printf("Protocol version to use : %d\n", xdnd_version);
icculus@9705
   892
                printf("More then 3 data types : %d\n", (int) use_list);
slouken@9245
   893
#endif
slouken@9245
   894
 
slouken@7001
   895
                if (use_list) {
slouken@7001
   896
                    /* fetch conversion targets */
slouken@7001
   897
                    SDL_x11Prop p;
slouken@7001
   898
                    X11_ReadProperty(&p, display, data->xdnd_source, videodata->XdndTypeList);
slouken@7001
   899
                    /* pick one */
slouken@7001
   900
                    data->xdnd_req = X11_PickTarget(display, (Atom*)p.data, p.count);
icculus@7827
   901
                    X11_XFree(p.data);
slouken@7001
   902
                } else {
slouken@7001
   903
                    /* pick from list of three */
slouken@7001
   904
                    data->xdnd_req = X11_PickTargetFromAtoms(display, xevent.xclient.data.l[2], xevent.xclient.data.l[3], xevent.xclient.data.l[4]);
slouken@7001
   905
                }
slouken@7001
   906
            }
slouken@7001
   907
            else if (xevent.xclient.message_type == videodata->XdndPosition) {
slouken@9245
   908
            
slouken@9245
   909
#ifdef DEBUG_XEVENTS
slouken@9245
   910
                Atom act= videodata->XdndActionCopy;
slouken@9245
   911
                if(xdnd_version >= 2) {
slouken@9245
   912
                    act = xevent.xclient.data.l[4];
slouken@9245
   913
                }
slouken@9245
   914
                printf("Action requested by user is : %s\n", X11_XGetAtomName(display , act));
slouken@9245
   915
#endif
slouken@9245
   916
                
slouken@7191
   917
slouken@7001
   918
                /* reply with status */
slouken@7001
   919
                memset(&m, 0, sizeof(XClientMessageEvent));
slouken@7001
   920
                m.type = ClientMessage;
slouken@7001
   921
                m.display = xevent.xclient.display;
slouken@7001
   922
                m.window = xevent.xclient.data.l[0];
slouken@7001
   923
                m.message_type = videodata->XdndStatus;
slouken@7001
   924
                m.format=32;
slouken@7001
   925
                m.data.l[0] = data->xwindow;
slouken@7001
   926
                m.data.l[1] = (data->xdnd_req != None);
slouken@7001
   927
                m.data.l[2] = 0; /* specify an empty rectangle */
slouken@7001
   928
                m.data.l[3] = 0;
slouken@7001
   929
                m.data.l[4] = videodata->XdndActionCopy; /* we only accept copying anyway */
slouken@7191
   930
icculus@7827
   931
                X11_XSendEvent(display, xevent.xclient.data.l[0], False, NoEventMask, (XEvent*)&m);
icculus@7827
   932
                X11_XFlush(display);
slouken@7001
   933
            }
slouken@7001
   934
            else if(xevent.xclient.message_type == videodata->XdndDrop) {
slouken@7001
   935
                if (data->xdnd_req == None) {
slouken@7001
   936
                    /* say again - not interested! */
slouken@7001
   937
                    memset(&m, 0, sizeof(XClientMessageEvent));
slouken@7001
   938
                    m.type = ClientMessage;
slouken@7001
   939
                    m.display = xevent.xclient.display;
slouken@7001
   940
                    m.window = xevent.xclient.data.l[0];
slouken@7001
   941
                    m.message_type = videodata->XdndFinished;
slouken@7001
   942
                    m.format=32;
slouken@7001
   943
                    m.data.l[0] = data->xwindow;
slouken@7001
   944
                    m.data.l[1] = 0;
slouken@7001
   945
                    m.data.l[2] = None; /* fail! */
icculus@7827
   946
                    X11_XSendEvent(display, xevent.xclient.data.l[0], False, NoEventMask, (XEvent*)&m);
slouken@7001
   947
                } else {
slouken@7001
   948
                    /* convert */
slouken@7001
   949
                    if(xdnd_version >= 1) {
icculus@7827
   950
                        X11_XConvertSelection(display, videodata->XdndSelection, data->xdnd_req, videodata->PRIMARY, data->xwindow, xevent.xclient.data.l[2]);
slouken@7001
   951
                    } else {
icculus@7827
   952
                        X11_XConvertSelection(display, videodata->XdndSelection, data->xdnd_req, videodata->PRIMARY, data->xwindow, CurrentTime);
slouken@7001
   953
                    }
slouken@7001
   954
                }
slouken@7001
   955
            }
slouken@7001
   956
            else if ((xevent.xclient.message_type == videodata->WM_PROTOCOLS) &&
icculus@6637
   957
                (xevent.xclient.format == 32) &&
icculus@6638
   958
                (xevent.xclient.data.l[0] == videodata->_NET_WM_PING)) {
slouken@6659
   959
                Window root = DefaultRootWindow(display);
icculus@6638
   960
icculus@6638
   961
#ifdef DEBUG_XEVENTS
icculus@6638
   962
                printf("window %p: _NET_WM_PING\n", data);
icculus@6638
   963
#endif
icculus@6638
   964
                xevent.xclient.window = root;
icculus@7827
   965
                X11_XSendEvent(display, root, False, SubstructureRedirectMask | SubstructureNotifyMask, &xevent);
icculus@6638
   966
                break;
icculus@6638
   967
            }
icculus@6638
   968
icculus@6638
   969
            else if ((xevent.xclient.message_type == videodata->WM_PROTOCOLS) &&
icculus@6638
   970
                (xevent.xclient.format == 32) &&
slouken@1951
   971
                (xevent.xclient.data.l[0] == videodata->WM_DELETE_WINDOW)) {
slouken@1951
   972
icculus@6638
   973
#ifdef DEBUG_XEVENTS
icculus@6638
   974
                printf("window %p: WM_DELETE_WINDOW\n", data);
icculus@6638
   975
#endif
slouken@3685
   976
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
icculus@6638
   977
                break;
slouken@1951
   978
            }
slouken@1951
   979
        }
slouken@1951
   980
        break;
slouken@1951
   981
slouken@1951
   982
        /* Do we need to refresh ourselves? */
slouken@1951
   983
    case Expose:{
slouken@1951
   984
#ifdef DEBUG_XEVENTS
slouken@6504
   985
            printf("window %p: Expose (count = %d)\n", data, xevent.xexpose.count);
slouken@1951
   986
#endif
slouken@3685
   987
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
slouken@1951
   988
        }
slouken@1951
   989
        break;
slouken@1951
   990
slouken@4465
   991
    case MotionNotify:{
slouken@7191
   992
            SDL_Mouse *mouse = SDL_GetMouse();
slouken@8071
   993
            if(!mouse->relative_mode || mouse->relative_mode_warp) {
slouken@4465
   994
#ifdef DEBUG_MOTION
slouken@6504
   995
                printf("window %p: X11 motion: %d,%d\n", xevent.xmotion.x, xevent.xmotion.y);
slouken@4465
   996
#endif
slouken@6311
   997
slouken@6950
   998
                SDL_SendMouseMotion(data->window, 0, 0, xevent.xmotion.x, xevent.xmotion.y);
slouken@6311
   999
            }
slouken@4465
  1000
        }
slouken@4465
  1001
        break;
slouken@4465
  1002
slouken@4465
  1003
    case ButtonPress:{
icculus@9686
  1004
            int xticks = 0, yticks = 0;
icculus@9686
  1005
            if (X11_IsWheelEvent(display,&xevent,&xticks, &yticks)) {
icculus@9686
  1006
                SDL_SendMouseWheel(data->window, 0, xticks, yticks, SDL_MOUSEWHEEL_NORMAL);
slouken@6950
  1007
            } else {
icculus@9686
  1008
                int button = xevent.xbutton.button;
icculus@9686
  1009
                if(button == Button1) {
icculus@8935
  1010
                    if (ProcessHitTest(_this, data, &xevent)) {
icculus@8933
  1011
                        break;  /* don't pass this event on to app. */
icculus@8933
  1012
                    }
rush@8932
  1013
                }
icculus@9686
  1014
                else if(button > 7) {
icculus@9686
  1015
                    /* X button values 4-7 are used for scrolling, so X1 is 8, X2 is 9, ...
icculus@9686
  1016
                       => subtract (8-SDL_BUTTON_X1) to get value SDL expects */
icculus@9686
  1017
                    button -= (8-SDL_BUTTON_X1);
icculus@9686
  1018
                }
icculus@9686
  1019
                SDL_SendMouseButton(data->window, 0, SDL_PRESSED, button);
slouken@5530
  1020
            }
slouken@4465
  1021
        }
slouken@4465
  1022
        break;
slouken@4465
  1023
slouken@4465
  1024
    case ButtonRelease:{
icculus@9686
  1025
            int button = xevent.xbutton.button;
icculus@9839
  1026
            /* The X server sends a Release event for each Press for wheels. Ignore them. */
icculus@9839
  1027
            int xticks = 0, yticks = 0;
icculus@9839
  1028
            if (!X11_IsWheelEvent(display,&xevent,&xticks, &yticks)) {
icculus@9839
  1029
                if (button > 7) {
icculus@9839
  1030
                    /* see explanation at case ButtonPress */
icculus@9839
  1031
                    button -= (8-SDL_BUTTON_X1);
icculus@9839
  1032
                }
icculus@9840
  1033
                SDL_SendMouseButton(data->window, 0, SDL_RELEASED, button);
icculus@9686
  1034
            }
slouken@4465
  1035
        }
slouken@4465
  1036
        break;
slouken@4465
  1037
slouken@4518
  1038
    case PropertyNotify:{
slouken@4518
  1039
#ifdef DEBUG_XEVENTS
slouken@4520
  1040
            unsigned char *propdata;
slouken@4520
  1041
            int status, real_format;
slouken@4520
  1042
            Atom real_type;
icculus@9705
  1043
            unsigned long items_read, items_left;
slouken@4520
  1044
icculus@7827
  1045
            char *name = X11_XGetAtomName(display, xevent.xproperty.atom);
slouken@4518
  1046
            if (name) {
slouken@6504
  1047
                printf("window %p: PropertyNotify: %s %s\n", data, name, (xevent.xproperty.state == PropertyDelete) ? "deleted" : "changed");
icculus@7827
  1048
                X11_XFree(name);
slouken@4518
  1049
            }
slouken@4518
  1050
icculus@7827
  1051
            status = X11_XGetWindowProperty(display, data->xwindow, xevent.xproperty.atom, 0L, 8192L, False, AnyPropertyType, &real_type, &real_format, &items_read, &items_left, &propdata);
slouken@4556
  1052
            if (status == Success && items_read > 0) {
slouken@4520
  1053
                if (real_type == XA_INTEGER) {
slouken@4520
  1054
                    int *values = (int *)propdata;
slouken@4520
  1055
slouken@4520
  1056
                    printf("{");
slouken@4520
  1057
                    for (i = 0; i < items_read; i++) {
slouken@4520
  1058
                        printf(" %d", values[i]);
slouken@4520
  1059
                    }
slouken@4520
  1060
                    printf(" }\n");
slouken@4520
  1061
                } else if (real_type == XA_CARDINAL) {
slouken@4520
  1062
                    if (real_format == 32) {
slouken@4520
  1063
                        Uint32 *values = (Uint32 *)propdata;
slouken@4520
  1064
slouken@4520
  1065
                        printf("{");
slouken@4520
  1066
                        for (i = 0; i < items_read; i++) {
slouken@4520
  1067
                            printf(" %d", values[i]);
slouken@4520
  1068
                        }
slouken@4520
  1069
                        printf(" }\n");
slouken@4520
  1070
                    } else if (real_format == 16) {
slouken@4520
  1071
                        Uint16 *values = (Uint16 *)propdata;
slouken@4520
  1072
slouken@4520
  1073
                        printf("{");
slouken@4520
  1074
                        for (i = 0; i < items_read; i++) {
slouken@4520
  1075
                            printf(" %d", values[i]);
slouken@4520
  1076
                        }
slouken@4520
  1077
                        printf(" }\n");
slouken@4520
  1078
                    } else if (real_format == 8) {
slouken@4520
  1079
                        Uint8 *values = (Uint8 *)propdata;
slouken@4520
  1080
slouken@4520
  1081
                        printf("{");
slouken@4520
  1082
                        for (i = 0; i < items_read; i++) {
slouken@4520
  1083
                            printf(" %d", values[i]);
slouken@4520
  1084
                        }
slouken@4520
  1085
                        printf(" }\n");
slouken@4520
  1086
                    }
slouken@4520
  1087
                } else if (real_type == XA_STRING ||
slouken@4520
  1088
                           real_type == videodata->UTF8_STRING) {
slouken@4520
  1089
                    printf("{ \"%s\" }\n", propdata);
slouken@4520
  1090
                } else if (real_type == XA_ATOM) {
slouken@4518
  1091
                    Atom *atoms = (Atom *)propdata;
slouken@4520
  1092
slouken@4520
  1093
                    printf("{");
slouken@4518
  1094
                    for (i = 0; i < items_read; i++) {
icculus@9705
  1095
                        char *atomname = X11_XGetAtomName(display, atoms[i]);
icculus@9705
  1096
                        if (atomname) {
icculus@9705
  1097
                            printf(" %s", atomname);
icculus@9705
  1098
                            X11_XFree(atomname);
slouken@4518
  1099
                        }
slouken@4518
  1100
                    }
slouken@4520
  1101
                    printf(" }\n");
slouken@4520
  1102
                } else {
icculus@9705
  1103
                    char *atomname = X11_XGetAtomName(display, real_type);
icculus@9705
  1104
                    printf("Unknown type: %ld (%s)\n", real_type, atomname ? atomname : "UNKNOWN");
icculus@9705
  1105
                    if (atomname) {
icculus@9705
  1106
                        X11_XFree(atomname);
slouken@4520
  1107
                    }
slouken@4518
  1108
                }
slouken@4520
  1109
            }
slouken@6481
  1110
            if (status == Success) {
icculus@7827
  1111
                X11_XFree(propdata);
slouken@6481
  1112
            }
slouken@6481
  1113
#endif /* DEBUG_XEVENTS */
slouken@6481
  1114
slouken@6481
  1115
            if (xevent.xproperty.atom == data->videodata->_NET_WM_STATE) {
slouken@6481
  1116
                /* Get the new state from the window manager.
slouken@6481
  1117
                   Compositing window managers can alter visibility of windows
slouken@6481
  1118
                   without ever mapping / unmapping them, so we handle that here,
slouken@6481
  1119
                   because they use the NETWM protocol to notify us of changes.
slouken@6481
  1120
                 */
icculus@9331
  1121
                const Uint32 flags = X11_GetNetWMState(_this, xevent.xproperty.window);
icculus@9331
  1122
                const Uint32 changed = flags ^ data->window->flags;
icculus@9331
  1123
icculus@9331
  1124
                if ((changed & SDL_WINDOW_HIDDEN) || (changed & SDL_WINDOW_FULLSCREEN)) {
icculus@9331
  1125
                     if (flags & SDL_WINDOW_HIDDEN) {
icculus@9331
  1126
                         X11_DispatchUnmapNotify(data);
icculus@9331
  1127
                     } else {
icculus@9331
  1128
                         X11_DispatchMapNotify(data);
slouken@6481
  1129
                    }
slouken@6481
  1130
                }
icculus@9331
  1131
icculus@9331
  1132
                if (changed & SDL_WINDOW_MAXIMIZED) {
icculus@9331
  1133
                    if (flags & SDL_WINDOW_MAXIMIZED) {
icculus@9331
  1134
                        SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
icculus@9331
  1135
                    } else {
icculus@9331
  1136
                        SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESTORED, 0, 0);
icculus@9331
  1137
                    }
icculus@9845
  1138
                }
icculus@9845
  1139
            } else if (xevent.xproperty.atom == videodata->XKLAVIER_STATE) {
icculus@9845
  1140
                /* Hack for Ubuntu 12.04 (etc) that doesn't send MappingNotify
icculus@9845
  1141
                   events when the keyboard layout changes (for example,
icculus@9845
  1142
                   changing from English to French on the menubar's keyboard
icculus@9845
  1143
                   icon). Since it changes the XKLAVIER_STATE property, we
icculus@9845
  1144
                   notice and reinit our keymap here. This might not be the
icculus@9845
  1145
                   right approach, but it seems to work. */
icculus@9845
  1146
                X11_UpdateKeymap(_this);
slouken@9898
  1147
                SDL_SendKeymapChangedEvent();
slouken@6481
  1148
            }
slouken@4518
  1149
        }
slouken@4518
  1150
        break;
slouken@4518
  1151
dimitris@9578
  1152
    /* Copy the selection from our own CUTBUFFER to the requested property */
slouken@4508
  1153
    case SelectionRequest: {
slouken@4508
  1154
            XSelectionRequestEvent *req;
slouken@4508
  1155
            XEvent sevent;
slouken@4508
  1156
            int seln_format;
slouken@4508
  1157
            unsigned long nbytes;
slouken@4508
  1158
            unsigned long overflow;
slouken@4508
  1159
            unsigned char *seln_data;
slouken@4508
  1160
slouken@4508
  1161
            req = &xevent.xselectionrequest;
slouken@4508
  1162
#ifdef DEBUG_XEVENTS
slouken@6504
  1163
            printf("window %p: SelectionRequest (requestor = %ld, target = %ld)\n", data,
slouken@4508
  1164
                req->requestor, req->target);
slouken@4508
  1165
#endif
slouken@4508
  1166
slouken@4524
  1167
            SDL_zero(sevent);
slouken@4520
  1168
            sevent.xany.type = SelectionNotify;
slouken@4508
  1169
            sevent.xselection.selection = req->selection;
slouken@4508
  1170
            sevent.xselection.target = None;
slouken@4508
  1171
            sevent.xselection.property = None;
slouken@4508
  1172
            sevent.xselection.requestor = req->requestor;
slouken@4508
  1173
            sevent.xselection.time = req->time;
icculus@9706
  1174
icculus@7827
  1175
            if (X11_XGetWindowProperty(display, DefaultRootWindow(display),
dimitris@9578
  1176
                    X11_GetSDLCutBufferClipboardType(display), 0, INT_MAX/4, False, req->target,
slouken@4508
  1177
                    &sevent.xselection.target, &seln_format, &nbytes,
slouken@4508
  1178
                    &overflow, &seln_data) == Success) {
icculus@7827
  1179
                Atom XA_TARGETS = X11_XInternAtom(display, "TARGETS", 0);
slouken@4508
  1180
                if (sevent.xselection.target == req->target) {
icculus@7827
  1181
                    X11_XChangeProperty(display, req->requestor, req->property,
slouken@4508
  1182
                        sevent.xselection.target, seln_format, PropModeReplace,
slouken@4508
  1183
                        seln_data, nbytes);
slouken@4508
  1184
                    sevent.xselection.property = req->property;
slouken@7021
  1185
                } else if (XA_TARGETS == req->target) {
icculus@9706
  1186
                    Atom SupportedFormats[] = { XA_TARGETS, sevent.xselection.target };
icculus@7827
  1187
                    X11_XChangeProperty(display, req->requestor, req->property,
slouken@7021
  1188
                        XA_ATOM, 32, PropModeReplace,
slouken@7021
  1189
                        (unsigned char*)SupportedFormats,
icculus@9706
  1190
                        SDL_arraysize(SupportedFormats));
slouken@7021
  1191
                    sevent.xselection.property = req->property;
icculus@9706
  1192
                    sevent.xselection.target = XA_TARGETS;
slouken@4508
  1193
                }
icculus@7827
  1194
                X11_XFree(seln_data);
slouken@4508
  1195
            }
icculus@7827
  1196
            X11_XSendEvent(display, req->requestor, False, 0, &sevent);
icculus@7827
  1197
            X11_XSync(display, False);
slouken@4508
  1198
        }
slouken@4508
  1199
        break;
slouken@4508
  1200
slouken@4508
  1201
    case SelectionNotify: {
slouken@4508
  1202
#ifdef DEBUG_XEVENTS
slouken@6504
  1203
            printf("window %p: SelectionNotify (requestor = %ld, target = %ld)\n", data,
slouken@4508
  1204
                xevent.xselection.requestor, xevent.xselection.target);
slouken@4508
  1205
#endif
slouken@7001
  1206
            Atom target = xevent.xselection.target;
slouken@7001
  1207
            if (target == data->xdnd_req) {
slouken@7001
  1208
                /* read data */
slouken@7001
  1209
                SDL_x11Prop p;
slouken@7001
  1210
                X11_ReadProperty(&p, display, data->xwindow, videodata->PRIMARY);
slouken@7191
  1211
slouken@7396
  1212
                if (p.format == 8) {
slouken@7001
  1213
                    SDL_bool expect_lf = SDL_FALSE;
slouken@7001
  1214
                    char *start = NULL;
slouken@7001
  1215
                    char *scan = (char*)p.data;
slouken@7001
  1216
                    char *fn;
slouken@7001
  1217
                    char *uri;
slouken@7001
  1218
                    int length = 0;
slouken@7001
  1219
                    while (p.count--) {
slouken@7001
  1220
                        if (!expect_lf) {
slouken@7396
  1221
                            if (*scan == 0x0D) {
slouken@7001
  1222
                                expect_lf = SDL_TRUE;
slouken@7396
  1223
                            }
slouken@7396
  1224
                            if (start == NULL) {
slouken@7001
  1225
                                start = scan;
slouken@7001
  1226
                                length = 0;
slouken@7001
  1227
                            }
slouken@7001
  1228
                            length++;
slouken@7001
  1229
                        } else {
slouken@7396
  1230
                            if (*scan == 0x0A && length > 0) {
slouken@7396
  1231
                                uri = SDL_malloc(length--);
slouken@7396
  1232
                                SDL_memcpy(uri, start, length);
slouken@7396
  1233
                                uri[length] = '\0';
slouken@7001
  1234
                                fn = X11_URIToLocal(uri);
slouken@7396
  1235
                                if (fn) {
slouken@7396
  1236
                                    SDL_SendDropFile(fn);
slouken@7396
  1237
                                }
slouken@7396
  1238
                                SDL_free(uri);
slouken@7001
  1239
                            }
slouken@7001
  1240
                            expect_lf = SDL_FALSE;
slouken@7001
  1241
                            start = NULL;
slouken@7001
  1242
                        }
slouken@7001
  1243
                        scan++;
slouken@7001
  1244
                    }
slouken@7001
  1245
                }
slouken@7191
  1246
icculus@7827
  1247
                X11_XFree(p.data);
slouken@7191
  1248
slouken@7001
  1249
                /* send reply */
slouken@7396
  1250
                SDL_memset(&m, 0, sizeof(XClientMessageEvent));
slouken@7001
  1251
                m.type = ClientMessage;
slouken@7001
  1252
                m.display = display;
slouken@7001
  1253
                m.window = data->xdnd_source;
slouken@7001
  1254
                m.message_type = videodata->XdndFinished;
slouken@7396
  1255
                m.format = 32;
slouken@7001
  1256
                m.data.l[0] = data->xwindow;
slouken@7001
  1257
                m.data.l[1] = 1;
slouken@7001
  1258
                m.data.l[2] = videodata->XdndActionCopy;
icculus@7827
  1259
                X11_XSendEvent(display, data->xdnd_source, False, NoEventMask, (XEvent*)&m);
slouken@7191
  1260
icculus@7827
  1261
                X11_XSync(display, False);
slouken@7191
  1262
slouken@7001
  1263
            } else {
slouken@7001
  1264
                videodata->selection_waiting = SDL_FALSE;
slouken@7001
  1265
            }
slouken@4508
  1266
        }
slouken@4508
  1267
        break;
slouken@4508
  1268
icculus@9652
  1269
    case SelectionClear: {
icculus@9652
  1270
            Atom XA_CLIPBOARD = X11_XInternAtom(display, "CLIPBOARD", 0);
icculus@9652
  1271
icculus@9652
  1272
            if (xevent.xselectionclear.selection == XA_PRIMARY ||
icculus@9652
  1273
                (XA_CLIPBOARD != None && xevent.xselectionclear.selection == XA_CLIPBOARD)) {
icculus@9652
  1274
                SDL_SendClipboardUpdate();
icculus@9652
  1275
            }
icculus@9652
  1276
        }
icculus@9652
  1277
        break;
icculus@9652
  1278
slouken@1951
  1279
    default:{
slouken@1951
  1280
#ifdef DEBUG_XEVENTS
slouken@6504
  1281
            printf("window %p: Unhandled event %d\n", data, xevent.type);
slouken@1951
  1282
#endif
slouken@1951
  1283
        }
slouken@1951
  1284
        break;
slouken@1951
  1285
    }
slouken@1951
  1286
}
slouken@1951
  1287
slouken@6504
  1288
static void
slouken@6504
  1289
X11_HandleFocusChanges(_THIS)
slouken@6504
  1290
{
slouken@6504
  1291
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@6504
  1292
    int i;
slouken@6504
  1293
slouken@6504
  1294
    if (videodata && videodata->windowlist) {
slouken@6504
  1295
        for (i = 0; i < videodata->numwindows; ++i) {
slouken@6504
  1296
            SDL_WindowData *data = videodata->windowlist[i];
slouken@6504
  1297
            if (data && data->pending_focus != PENDING_FOCUS_NONE) {
slouken@6504
  1298
                Uint32 now = SDL_GetTicks();
slouken@7857
  1299
                if (SDL_TICKS_PASSED(now, data->pending_focus_time)) {
slouken@9666
  1300
                    if (data->pending_focus == PENDING_FOCUS_IN) {
slouken@9666
  1301
                        X11_DispatchFocusIn(_this, data);
slouken@6504
  1302
                    } else {
slouken@9666
  1303
                        X11_DispatchFocusOut(_this, data);
slouken@6504
  1304
                    }
slouken@6504
  1305
                    data->pending_focus = PENDING_FOCUS_NONE;
slouken@6504
  1306
                }
slouken@6504
  1307
            }
slouken@6504
  1308
        }
slouken@6504
  1309
    }
slouken@6504
  1310
}
icculus@7827
  1311
/* Ack!  X11_XPending() actually performs a blocking read if no events available */
slouken@4472
  1312
static int
slouken@1951
  1313
X11_Pending(Display * display)
slouken@1951
  1314
{
slouken@1951
  1315
    /* Flush the display connection and look to see if events are queued */
icculus@7827
  1316
    X11_XFlush(display);
icculus@7827
  1317
    if (X11_XEventsQueued(display, QueuedAlready)) {
slouken@1951
  1318
        return (1);
slouken@1951
  1319
    }
slouken@1951
  1320
slouken@1951
  1321
    /* More drastic measures are required -- see if X is ready to talk */
slouken@1951
  1322
    {
slouken@1951
  1323
        static struct timeval zero_time;        /* static == 0 */
slouken@1951
  1324
        int x11_fd;
slouken@1951
  1325
        fd_set fdset;
slouken@1951
  1326
slouken@1951
  1327
        x11_fd = ConnectionNumber(display);
slouken@1951
  1328
        FD_ZERO(&fdset);
slouken@1951
  1329
        FD_SET(x11_fd, &fdset);
slouken@1951
  1330
        if (select(x11_fd + 1, &fdset, NULL, NULL, &zero_time) == 1) {
icculus@7827
  1331
            return (X11_XPending(display));
slouken@1951
  1332
        }
slouken@1951
  1333
    }
slouken@1951
  1334
slouken@1951
  1335
    /* Oh well, nothing is ready .. */
slouken@1951
  1336
    return (0);
slouken@1951
  1337
}
slouken@1951
  1338
slouken@1951
  1339
void
slouken@1951
  1340
X11_PumpEvents(_THIS)
slouken@1951
  1341
{
slouken@1951
  1342
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
  1343
icculus@9319
  1344
    if (data->last_mode_change_deadline) {
icculus@9319
  1345
        if (SDL_TICKS_PASSED(SDL_GetTicks(), data->last_mode_change_deadline)) {
icculus@9319
  1346
            data->last_mode_change_deadline = 0;  /* assume we're done. */
icculus@9319
  1347
        }
icculus@9319
  1348
    }
icculus@9319
  1349
slouken@3025
  1350
    /* Update activity every 30 seconds to prevent screensaver */
slouken@3025
  1351
    if (_this->suspend_screensaver) {
icculus@9319
  1352
        const Uint32 now = SDL_GetTicks();
slouken@3025
  1353
        if (!data->screensaver_activity ||
slouken@7857
  1354
            SDL_TICKS_PASSED(now, data->screensaver_activity + 30000)) {
icculus@7827
  1355
            X11_XResetScreenSaver(data->display);
icculus@6774
  1356
alex@8889
  1357
#if SDL_USE_LIBDBUS
alex@8889
  1358
            SDL_DBus_ScreensaverTickle();
alex@8889
  1359
#endif
icculus@6774
  1360
slouken@3025
  1361
            data->screensaver_activity = now;
slouken@3025
  1362
        }
slouken@7191
  1363
    }
slouken@3025
  1364
alex@9639
  1365
    /* Keep processing pending events */
alex@9639
  1366
    while (X11_Pending(data->display)) {
alex@9639
  1367
        X11_DispatchEvent(_this);
alex@9639
  1368
    }
alex@9639
  1369
alex@8889
  1370
#ifdef SDL_USE_IBUS
alex@8889
  1371
    if(SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE){
alex@8889
  1372
        SDL_IBus_PumpEvents();
alex@8889
  1373
    }
alex@8889
  1374
#endif
alex@8889
  1375
slouken@6504
  1376
    /* FIXME: Only need to do this when there are pending focus changes */
slouken@6504
  1377
    X11_HandleFocusChanges(_this);
slouken@1951
  1378
}
slouken@1951
  1379
slouken@3030
  1380
slouken@1951
  1381
void
slouken@3025
  1382
X11_SuspendScreenSaver(_THIS)
slouken@1951
  1383
{
slouken@5408
  1384
#if SDL_VIDEO_DRIVER_X11_XSCRNSAVER
slouken@3025
  1385
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@3025
  1386
    int dummy;
slouken@3025
  1387
    int major_version, minor_version;
hadess@8287
  1388
#endif /* SDL_VIDEO_DRIVER_X11_XSCRNSAVER */
slouken@1951
  1389
hadess@8287
  1390
#if SDL_USE_LIBDBUS
alex@8889
  1391
    if (SDL_DBus_ScreensaverInhibit(_this->suspend_screensaver)) {
hadess@8287
  1392
        return;
hadess@8287
  1393
    }
hadess@8287
  1394
hadess@8287
  1395
    if (_this->suspend_screensaver) {
alex@8889
  1396
        SDL_DBus_ScreensaverTickle();
hadess@8287
  1397
    }
hadess@8287
  1398
#endif
hadess@8287
  1399
hadess@8287
  1400
#if SDL_VIDEO_DRIVER_X11_XSCRNSAVER
slouken@3030
  1401
    if (SDL_X11_HAVE_XSS) {
icculus@7827
  1402
        /* X11_XScreenSaverSuspend was introduced in MIT-SCREEN-SAVER 1.1 */
icculus@7827
  1403
        if (!X11_XScreenSaverQueryExtension(data->display, &dummy, &dummy) ||
icculus@7827
  1404
            !X11_XScreenSaverQueryVersion(data->display,
slouken@3030
  1405
                                      &major_version, &minor_version) ||
slouken@3030
  1406
            major_version < 1 || (major_version == 1 && minor_version < 1)) {
slouken@3030
  1407
            return;
slouken@3030
  1408
        }
slouken@3030
  1409
icculus@7827
  1410
        X11_XScreenSaverSuspend(data->display, _this->suspend_screensaver);
icculus@7827
  1411
        X11_XResetScreenSaver(data->display);
slouken@1951
  1412
    }
slouken@3030
  1413
#endif
slouken@1951
  1414
}
slouken@1951
  1415
slouken@5481
  1416
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
  1417
slouken@1951
  1418
/* vi: set ts=4 sw=4 expandtab: */