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