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