src/video/x11/SDL_x11events.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 07 Oct 2016 18:57:40 -0700
changeset 10496 6660aa9120d6
parent 10494 c54de4000f9f
child 10499 363c1c7e7a41
permissions -rw-r--r--
Fixed bug 2824 - Add Fcitx Input Method Support

Weitian Leung

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