src/video/x11/SDL_x11events.c
author Dimitris Zenios <dimitris.zenios@gmail.com>
Sun, 26 Apr 2015 13:53:46 +0300
changeset 9578 e78393ffcd50
parent 9555 fff4b6354b99
child 9582 e0e2e94ce5ea
permissions -rw-r--r--
X11: Use our own cut-buffer for intermediate clipboard storage.

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