src/video/x11/SDL_x11events.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 06 Apr 2015 00:10:54 -0400
changeset 9458 543298b36b28
parent 9331 bb0b744fd1a6
child 9555 fff4b6354b99
permissions -rw-r--r--
This function can be static.
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;
ionut@8946
   451
    SDL_bool ret = SDL_FALSE;
icculus@8933
   452
icculus@8935
   453
    if (window->hit_test) {
icculus@8933
   454
        const SDL_Point point = { xev->xbutton.x, xev->xbutton.y };
icculus@8935
   455
        const SDL_HitTestResult rc = window->hit_test(window, &point, window->hit_test_data);
ionut@8946
   456
        switch (rc) {
ionut@8946
   457
            case SDL_HITTEST_DRAGGABLE: {
ionut@8946
   458
                    InitiateWindowMove(_this, data, &point);
ionut@8946
   459
                    ret = SDL_TRUE;
ionut@8946
   460
                }
ionut@8946
   461
                break;
ionut@8946
   462
            case SDL_HITTEST_RESIZE_TOPLEFT: {
ionut@8946
   463
                    InitiateWindowResize(_this, data, &point, _NET_WM_MOVERESIZE_SIZE_TOPLEFT);
ionut@8946
   464
                    ret = SDL_TRUE;
ionut@8946
   465
                }
ionut@8946
   466
                break;
ionut@8946
   467
            case SDL_HITTEST_RESIZE_TOP: {
ionut@8946
   468
                    InitiateWindowResize(_this, data, &point, _NET_WM_MOVERESIZE_SIZE_TOP);
ionut@8946
   469
                    ret = SDL_TRUE;
ionut@8946
   470
                }
ionut@8946
   471
                break;
ionut@8946
   472
            case SDL_HITTEST_RESIZE_TOPRIGHT: {
ionut@8946
   473
                    InitiateWindowResize(_this, data, &point, _NET_WM_MOVERESIZE_SIZE_TOPRIGHT);
ionut@8946
   474
                    ret = SDL_TRUE;
ionut@8946
   475
                }
ionut@8946
   476
                break;
ionut@8946
   477
            case SDL_HITTEST_RESIZE_RIGHT: {
ionut@8946
   478
                    InitiateWindowResize(_this, data, &point, _NET_WM_MOVERESIZE_SIZE_RIGHT);
ionut@8946
   479
                    ret = SDL_TRUE;
ionut@8946
   480
                }
ionut@8946
   481
                break;
ionut@8946
   482
            case SDL_HITTEST_RESIZE_BOTTOMRIGHT: {
ionut@8946
   483
                    InitiateWindowResize(_this, data, &point, _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT);
ionut@8946
   484
                    ret = SDL_TRUE;
ionut@8946
   485
                }
ionut@8946
   486
                break;
ionut@8946
   487
            case SDL_HITTEST_RESIZE_BOTTOM: {
ionut@8946
   488
                    InitiateWindowResize(_this, data, &point, _NET_WM_MOVERESIZE_SIZE_BOTTOM);
ionut@8946
   489
                    ret = SDL_TRUE;
ionut@8946
   490
                }
ionut@8946
   491
                break;
ionut@8946
   492
            case SDL_HITTEST_RESIZE_BOTTOMLEFT: {
ionut@8946
   493
                    InitiateWindowResize(_this, data, &point, _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT);
ionut@8946
   494
                    ret = SDL_TRUE;
ionut@8946
   495
                }
ionut@8946
   496
                break;
ionut@8946
   497
            case SDL_HITTEST_RESIZE_LEFT: {
ionut@8946
   498
                    InitiateWindowResize(_this, data, &point, _NET_WM_MOVERESIZE_SIZE_LEFT);
ionut@8946
   499
                    ret = SDL_TRUE;
ionut@8946
   500
                }
ionut@8946
   501
                break;
ionut@8946
   502
            default:
ionut@8946
   503
                break;
rush@8932
   504
        }
rush@8932
   505
    }
icculus@8933
   506
ionut@8946
   507
    return ret;
rush@8932
   508
}
rush@8932
   509
rush@8932
   510
static void
pgriffais@9133
   511
ReconcileKeyboardState(_THIS, const SDL_WindowData *data)
pgriffais@9133
   512
{
pgriffais@9133
   513
    SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
pgriffais@9133
   514
    Display *display = viddata->display;
pgriffais@9133
   515
    char keys[32];
pgriffais@9133
   516
    int keycode = 0;
pgriffais@9133
   517
pgriffais@9133
   518
    X11_XQueryKeymap( display, keys );
pgriffais@9133
   519
pgriffais@9133
   520
    while ( keycode < 256 ) {
pgriffais@9133
   521
        if ( keys[keycode / 8] & (1 << (keycode % 8)) ) {
pgriffais@9133
   522
            SDL_SendKeyboardKey(SDL_PRESSED, viddata->key_layout[keycode]);
pgriffais@9133
   523
        } else {
pgriffais@9133
   524
            SDL_SendKeyboardKey(SDL_RELEASED, viddata->key_layout[keycode]);
pgriffais@9133
   525
        }
pgriffais@9133
   526
        keycode++;
pgriffais@9133
   527
    }
pgriffais@9133
   528
}
pgriffais@9133
   529
pgriffais@9133
   530
static void
slouken@1951
   531
X11_DispatchEvent(_THIS)
slouken@1951
   532
{
slouken@1951
   533
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@4518
   534
    Display *display = videodata->display;
slouken@1951
   535
    SDL_WindowData *data;
slouken@1951
   536
    XEvent xevent;
slouken@7943
   537
    int orig_event_type;
slouken@7943
   538
    KeyCode orig_keycode;
slouken@7943
   539
    XClientMessageEvent m;
slouken@1951
   540
    int i;
slouken@1951
   541
slouken@1951
   542
    SDL_zero(xevent);           /* valgrind fix. --ryan. */
icculus@7827
   543
    X11_XNextEvent(display, &xevent);
slouken@1951
   544
slouken@7943
   545
    /* Save the original keycode for dead keys, which are filtered out by
slouken@7943
   546
       the XFilterEvent() call below.
slouken@7943
   547
    */
slouken@7943
   548
    orig_event_type = xevent.type;
slouken@7943
   549
    if (orig_event_type == KeyPress || orig_event_type == KeyRelease) {
slouken@7943
   550
        orig_keycode = xevent.xkey.keycode;
slouken@7943
   551
    } else {
slouken@7943
   552
        orig_keycode = 0;
slouken@7943
   553
    }
slouken@7943
   554
slouken@7943
   555
    /* filter events catchs XIM events and sends them to the correct handler */
icculus@7827
   556
    if (X11_XFilterEvent(&xevent, None) == True) {
bob@2327
   557
#if 0
bob@2328
   558
        printf("Filtered event type = %d display = %d window = %d\n",
bob@2328
   559
               xevent.type, xevent.xany.display, xevent.xany.window);
bob@2325
   560
#endif
slouken@7943
   561
        if (orig_keycode) {
slouken@7943
   562
            /* Make sure dead key press/release events are sent */
slouken@8819
   563
            /* Actually, don't do this because it causes double-delivery
slouken@8819
   564
               of some keys on Ubuntu 14.04 (bug 2526)
slouken@7943
   565
            SDL_Scancode scancode = videodata->key_layout[orig_keycode];
slouken@7943
   566
            if (orig_event_type == KeyPress) {
slouken@7943
   567
                SDL_SendKeyboardKey(SDL_PRESSED, scancode);
slouken@7943
   568
            } else {
slouken@7943
   569
                SDL_SendKeyboardKey(SDL_RELEASED, scancode);
slouken@7943
   570
            }
slouken@8819
   571
            */
slouken@7943
   572
        }
bob@2325
   573
        return;
bob@2325
   574
    }
bob@2325
   575
slouken@1951
   576
    /* Send a SDL_SYSWMEVENT if the application wants them */
slouken@4429
   577
    if (SDL_GetEventState(SDL_SYSWMEVENT) == SDL_ENABLE) {
slouken@1951
   578
        SDL_SysWMmsg wmmsg;
slouken@1951
   579
slouken@1951
   580
        SDL_VERSION(&wmmsg.version);
slouken@1951
   581
        wmmsg.subsystem = SDL_SYSWM_X11;
slouken@5056
   582
        wmmsg.msg.x11.event = xevent;
slouken@1951
   583
        SDL_SendSysWMEvent(&wmmsg);
slouken@1951
   584
    }
slouken@1951
   585
slouken@6311
   586
#if SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS
slouken@6311
   587
    if(xevent.type == GenericEvent) {
slouken@6311
   588
        X11_HandleGenericEvent(videodata,xevent);
slouken@6311
   589
        return;
slouken@6311
   590
    }
slouken@6311
   591
#endif
slouken@6311
   592
slouken@6476
   593
#if 0
slouken@6476
   594
    printf("type = %d display = %d window = %d\n",
slouken@6476
   595
           xevent.type, xevent.xany.display, xevent.xany.window);
slouken@6476
   596
#endif
slouken@6476
   597
slouken@1951
   598
    data = NULL;
bob@2324
   599
    if (videodata && videodata->windowlist) {
bob@2324
   600
        for (i = 0; i < videodata->numwindows; ++i) {
bob@2324
   601
            if ((videodata->windowlist[i] != NULL) &&
slouken@3685
   602
                (videodata->windowlist[i]->xwindow == xevent.xany.window)) {
bob@2324
   603
                data = videodata->windowlist[i];
bob@2324
   604
                break;
bob@2324
   605
            }
slouken@1951
   606
        }
slouken@1951
   607
    }
slouken@1951
   608
    if (!data) {
slouken@1951
   609
        return;
slouken@1951
   610
    }
slouken@4518
   611
slouken@1951
   612
    switch (xevent.type) {
slouken@1951
   613
slouken@1951
   614
        /* Gaining mouse coverage? */
slouken@1951
   615
    case EnterNotify:{
slouken@1951
   616
#ifdef DEBUG_XEVENTS
slouken@6504
   617
            printf("window %p: EnterNotify! (%d,%d,%d)\n", data,
slouken@4558
   618
                   xevent.xcrossing.x,
slouken@4558
   619
                   xevent.xcrossing.y,
bob@3195
   620
                   xevent.xcrossing.mode);
slouken@1951
   621
            if (xevent.xcrossing.mode == NotifyGrab)
slouken@1951
   622
                printf("Mode: NotifyGrab\n");
slouken@1951
   623
            if (xevent.xcrossing.mode == NotifyUngrab)
slouken@1951
   624
                printf("Mode: NotifyUngrab\n");
slouken@1951
   625
#endif
slouken@4465
   626
            SDL_SetMouseFocus(data->window);
jorgen@7097
   627
jorgen@7097
   628
            if (!SDL_GetMouse()->relative_mode) {
jorgen@7097
   629
                SDL_SendMouseMotion(data->window, 0, 0, xevent.xcrossing.x, xevent.xcrossing.y);
jorgen@7097
   630
            }
slouken@1951
   631
        }
slouken@1951
   632
        break;
slouken@1951
   633
        /* Losing mouse coverage? */
slouken@1951
   634
    case LeaveNotify:{
slouken@1951
   635
#ifdef DEBUG_XEVENTS
slouken@6504
   636
            printf("window %p: LeaveNotify! (%d,%d,%d)\n", data,
slouken@4558
   637
                   xevent.xcrossing.x,
slouken@4558
   638
                   xevent.xcrossing.y,
bob@3195
   639
                   xevent.xcrossing.mode);
slouken@1951
   640
            if (xevent.xcrossing.mode == NotifyGrab)
slouken@1951
   641
                printf("Mode: NotifyGrab\n");
slouken@1951
   642
            if (xevent.xcrossing.mode == NotifyUngrab)
slouken@1951
   643
                printf("Mode: NotifyUngrab\n");
slouken@1951
   644
#endif
jorgen@7089
   645
            if (!SDL_GetMouse()->relative_mode) {
jorgen@7089
   646
                SDL_SendMouseMotion(data->window, 0, 0, xevent.xcrossing.x, xevent.xcrossing.y);
jorgen@7089
   647
            }
jorgen@7089
   648
slouken@4558
   649
            if (xevent.xcrossing.mode != NotifyGrab &&
slouken@4558
   650
                xevent.xcrossing.mode != NotifyUngrab &&
slouken@4558
   651
                xevent.xcrossing.detail != NotifyInferior) {
slouken@4465
   652
                SDL_SetMouseFocus(NULL);
slouken@1951
   653
            }
slouken@1951
   654
        }
slouken@1951
   655
        break;
slouken@1951
   656
slouken@1951
   657
        /* Gaining input focus? */
slouken@1951
   658
    case FocusIn:{
gabomdq@7945
   659
            if (xevent.xfocus.mode == NotifyGrab || xevent.xfocus.mode == NotifyUngrab) {
gabomdq@7945
   660
                /* Someone is handling a global hotkey, ignore it */
gabomdq@7945
   661
#ifdef DEBUG_XEVENTS
gabomdq@7945
   662
                printf("window %p: FocusIn (NotifyGrab/NotifyUngrab, ignoring)\n", data);
gabomdq@7945
   663
#endif
gabomdq@7945
   664
                break;
gabomdq@7945
   665
            }
gabomdq@7945
   666
slouken@6660
   667
            if (xevent.xfocus.detail == NotifyInferior) {
slouken@6660
   668
#ifdef DEBUG_XEVENTS
slouken@6660
   669
                printf("window %p: FocusIn (NotifierInferior, ignoring)\n", data);
slouken@6660
   670
#endif
slouken@6660
   671
                break;
slouken@6660
   672
            }
slouken@1951
   673
#ifdef DEBUG_XEVENTS
slouken@6504
   674
            printf("window %p: FocusIn!\n", data);
slouken@1951
   675
#endif
slouken@6661
   676
            if (data->pending_focus == PENDING_FOCUS_OUT &&
slouken@6661
   677
                data->window == SDL_GetKeyboardFocus()) {
pgriffais@9133
   678
                ReconcileKeyboardState(_this, data);
slouken@6661
   679
            }
icculus@9319
   680
            if (!videodata->last_mode_change_deadline) /* no recent mode changes */
icculus@9319
   681
            {
icculus@9319
   682
                data->pending_focus = PENDING_FOCUS_NONE;
icculus@9319
   683
                data->pending_focus_time = 0;
icculus@9319
   684
                X11_DispatchFocusIn(data);
icculus@9319
   685
            }
icculus@9319
   686
            else
icculus@9319
   687
            {
icculus@9319
   688
                data->pending_focus = PENDING_FOCUS_IN;
icculus@9319
   689
                data->pending_focus_time = SDL_GetTicks() + PENDING_FOCUS_TIME;
icculus@9319
   690
            }
slouken@1951
   691
        }
slouken@1951
   692
        break;
slouken@1951
   693
slouken@1951
   694
        /* Losing input focus? */
slouken@1951
   695
    case FocusOut:{
gabomdq@7945
   696
            if (xevent.xfocus.mode == NotifyGrab || xevent.xfocus.mode == NotifyUngrab) {
slouken@7942
   697
                /* Someone is handling a global hotkey, ignore it */
slouken@7942
   698
#ifdef DEBUG_XEVENTS
gabomdq@7945
   699
                printf("window %p: FocusOut (NotifyGrab/NotifyUngrab, ignoring)\n", data);
slouken@7942
   700
#endif
slouken@7942
   701
                break;
slouken@7942
   702
            }
slouken@6660
   703
            if (xevent.xfocus.detail == NotifyInferior) {
slouken@6660
   704
                /* We still have focus if a child gets focus */
slouken@6660
   705
#ifdef DEBUG_XEVENTS
slouken@6660
   706
                printf("window %p: FocusOut (NotifierInferior, ignoring)\n", data);
slouken@6660
   707
#endif
slouken@6660
   708
                break;
slouken@6660
   709
            }
slouken@1951
   710
#ifdef DEBUG_XEVENTS
slouken@6504
   711
            printf("window %p: FocusOut!\n", data);
slouken@1951
   712
#endif
icculus@9319
   713
            if (!videodata->last_mode_change_deadline) /* no recent mode changes */
icculus@9319
   714
            {
icculus@9319
   715
                data->pending_focus = PENDING_FOCUS_NONE;
icculus@9319
   716
                data->pending_focus_time = 0;
icculus@9319
   717
                X11_DispatchFocusOut(data);
icculus@9319
   718
            }
icculus@9319
   719
            else
icculus@9319
   720
            {
icculus@9319
   721
                data->pending_focus = PENDING_FOCUS_OUT;
icculus@9319
   722
                data->pending_focus_time = SDL_GetTicks() + PENDING_FOCUS_TIME;
icculus@9319
   723
            }
slouken@1951
   724
        }
slouken@1951
   725
        break;
slouken@1951
   726
slouken@1951
   727
        /* Generated upon EnterWindow and FocusIn */
slouken@1951
   728
    case KeymapNotify:{
slouken@1951
   729
#ifdef DEBUG_XEVENTS
slouken@6504
   730
            printf("window %p: KeymapNotify!\n", data);
slouken@1951
   731
#endif
slouken@1951
   732
            /* FIXME:
slouken@1951
   733
               X11_SetKeyboardState(SDL_Display, xevent.xkeymap.key_vector);
slouken@1951
   734
             */
slouken@1951
   735
        }
slouken@1951
   736
        break;
slouken@1951
   737
slouken@2305
   738
        /* Has the keyboard layout changed? */
slouken@2305
   739
    case MappingNotify:{
slouken@2305
   740
#ifdef DEBUG_XEVENTS
slouken@6504
   741
            printf("window %p: MappingNotify!\n", data);
slouken@2305
   742
#endif
slouken@2306
   743
            X11_UpdateKeymap(_this);
slouken@2305
   744
        }
slouken@2305
   745
        break;
slouken@2305
   746
slouken@1951
   747
        /* Key press? */
slouken@1951
   748
    case KeyPress:{
slouken@1951
   749
            KeyCode keycode = xevent.xkey.keycode;
bob@2300
   750
            KeySym keysym = NoSymbol;
slouken@2306
   751
            char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];
bob@2325
   752
            Status status = 0;
alex@9096
   753
            SDL_bool handled_by_ime = SDL_FALSE;
slouken@1951
   754
slouken@1951
   755
#ifdef DEBUG_XEVENTS
slouken@6504
   756
            printf("window %p: KeyPress (X11 keycode = 0x%X)\n", data, xevent.xkey.keycode);
slouken@1951
   757
#endif
slouken@4535
   758
#if 1
slouken@7638
   759
            if (videodata->key_layout[keycode] == SDL_SCANCODE_UNKNOWN && keycode) {
slouken@2305
   760
                int min_keycode, max_keycode;
icculus@7827
   761
                X11_XDisplayKeycodes(display, &min_keycode, &max_keycode);
dimitris@6318
   762
#if SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM
icculus@7827
   763
                keysym = X11_XkbKeycodeToKeysym(display, keycode, 0, 0);
dimitris@6318
   764
#else
slouken@4518
   765
                keysym = XKeycodeToKeysym(display, keycode, 0);
dimitris@6318
   766
#endif
bob@2299
   767
                fprintf(stderr,
slouken@4556
   768
                        "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
   769
                        keycode, keycode - min_keycode, keysym,
icculus@7827
   770
                        X11_XKeysymToString(keysym));
bob@2299
   771
            }
bob@2295
   772
#endif
bob@2325
   773
            /* */
slouken@2305
   774
            SDL_zero(text);
bob@2325
   775
#ifdef X_HAVE_UTF8_STRING
bob@2325
   776
            if (data->ic) {
icculus@7827
   777
                X11_Xutf8LookupString(data->ic, &xevent.xkey, text, sizeof(text),
slouken@2738
   778
                                  &keysym, &status);
bob@2325
   779
            }
bob@2325
   780
#else
slouken@2306
   781
            XLookupString(&xevent.xkey, text, sizeof(text), &keysym, NULL);
bob@2325
   782
#endif
alex@8889
   783
#ifdef SDL_USE_IBUS
alex@8889
   784
            if(SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE){
alex@9096
   785
                if(!(handled_by_ime = SDL_IBus_ProcessKeyEvent(keysym, keycode))){
alex@8889
   786
#endif
alex@8889
   787
                    if(*text){
alex@8889
   788
                        SDL_SendKeyboardText(text);
alex@8889
   789
                    }
alex@8889
   790
#ifdef SDL_USE_IBUS
alex@8889
   791
                }
bob@2300
   792
            }
alex@9096
   793
#endif
alex@9096
   794
            if (!handled_by_ime) {
alex@8889
   795
                SDL_SendKeyboardKey(SDL_PRESSED, videodata->key_layout[keycode]);
alex@8889
   796
            }
alex@9096
   797
slouken@1951
   798
        }
slouken@1951
   799
        break;
slouken@1951
   800
slouken@1951
   801
        /* Key release? */
slouken@1951
   802
    case KeyRelease:{
slouken@1951
   803
            KeyCode keycode = xevent.xkey.keycode;
slouken@1951
   804
slouken@1951
   805
#ifdef DEBUG_XEVENTS
slouken@6504
   806
            printf("window %p: KeyRelease (X11 keycode = 0x%X)\n", data, xevent.xkey.keycode);
slouken@1951
   807
#endif
slouken@4565
   808
            if (X11_KeyRepeat(display, &xevent)) {
slouken@4565
   809
                /* We're about to get a repeated key down, ignore the key up */
slouken@4565
   810
                break;
slouken@4565
   811
            }
slouken@4465
   812
            SDL_SendKeyboardKey(SDL_RELEASED, videodata->key_layout[keycode]);
slouken@1951
   813
        }
slouken@1951
   814
        break;
slouken@1951
   815
slouken@1951
   816
        /* Have we been iconified? */
slouken@1951
   817
    case UnmapNotify:{
slouken@1951
   818
#ifdef DEBUG_XEVENTS
slouken@6504
   819
            printf("window %p: UnmapNotify!\n", data);
slouken@1951
   820
#endif
slouken@6481
   821
            X11_DispatchUnmapNotify(data);
slouken@1951
   822
        }
slouken@1951
   823
        break;
slouken@1951
   824
slouken@1951
   825
        /* Have we been restored? */
slouken@1951
   826
    case MapNotify:{
slouken@1951
   827
#ifdef DEBUG_XEVENTS
slouken@6504
   828
            printf("window %p: MapNotify!\n", data);
slouken@1951
   829
#endif
slouken@6481
   830
            X11_DispatchMapNotify(data);
slouken@1951
   831
        }
slouken@1951
   832
        break;
slouken@1951
   833
slouken@1951
   834
        /* Have we been resized or moved? */
slouken@1951
   835
    case ConfigureNotify:{
slouken@1951
   836
#ifdef DEBUG_XEVENTS
slouken@6542
   837
            printf("window %p: ConfigureNotify! (position: %d,%d, size: %dx%d)\n", data,
slouken@6542
   838
                   xevent.xconfigure.x, xevent.xconfigure.y,
slouken@1951
   839
                   xevent.xconfigure.width, xevent.xconfigure.height);
slouken@1951
   840
#endif
stapostol@7838
   841
            long border_left = 0;
stapostol@7838
   842
            long border_top = 0;
stapostol@7838
   843
            if (data->xwindow) {
slouken@7839
   844
                Atom _net_frame_extents = X11_XInternAtom(display, "_NET_FRAME_EXTENTS", 0);
slouken@8706
   845
                Atom type;
stapostol@7838
   846
                int format;
slouken@8706
   847
                unsigned long nitems, bytes_after;
stapostol@7838
   848
                unsigned char *property;
slouken@8706
   849
                if (X11_XGetWindowProperty(display, data->xwindow,
slouken@8706
   850
                        _net_frame_extents, 0, 16, 0,
slouken@8706
   851
                        XA_CARDINAL, &type, &format,
slouken@8706
   852
                        &nitems, &bytes_after, &property) == Success) {
slouken@8706
   853
                    if (type != None && nitems == 4)
slouken@8706
   854
                    {
slouken@8706
   855
                        border_left = ((long*)property)[0];
slouken@8706
   856
                        border_top = ((long*)property)[2];
slouken@8706
   857
                    }
slouken@8706
   858
                    X11_XFree(property);
pgriffais@7885
   859
                }
stapostol@7838
   860
            }
stapostol@7838
   861
slouken@6542
   862
            if (xevent.xconfigure.x != data->last_xconfigure.x ||
slouken@6542
   863
                xevent.xconfigure.y != data->last_xconfigure.y) {
slouken@6542
   864
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED,
stapostol@7838
   865
                                    xevent.xconfigure.x - border_left,
stapostol@7838
   866
                                    xevent.xconfigure.y - border_top);
alex@8889
   867
#ifdef SDL_USE_IBUS
alex@8889
   868
                if(SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE){
alex@8889
   869
                    /* Update IBus candidate list position */
alex@8889
   870
                    SDL_IBus_UpdateTextRect(NULL);
alex@8889
   871
                }
alex@8889
   872
#endif
slouken@6542
   873
            }
slouken@6542
   874
            if (xevent.xconfigure.width != data->last_xconfigure.width ||
slouken@6542
   875
                xevent.xconfigure.height != data->last_xconfigure.height) {
slouken@6542
   876
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED,
slouken@6542
   877
                                    xevent.xconfigure.width,
slouken@6542
   878
                                    xevent.xconfigure.height);
slouken@6542
   879
            }
slouken@6542
   880
            data->last_xconfigure = xevent.xconfigure;
slouken@1951
   881
        }
slouken@1951
   882
        break;
slouken@1951
   883
slouken@1951
   884
        /* Have we been requested to quit (or another client message?) */
slouken@1951
   885
    case ClientMessage:{
slouken@7001
   886
slouken@9245
   887
            static int xdnd_version=0;
slouken@7191
   888
slouken@7001
   889
            if (xevent.xclient.message_type == videodata->XdndEnter) {
slouken@9245
   890
slouken@7001
   891
                SDL_bool use_list = xevent.xclient.data.l[1] & 1;
slouken@7001
   892
                data->xdnd_source = xevent.xclient.data.l[0];
slouken@7001
   893
                xdnd_version = ( xevent.xclient.data.l[1] >> 24);
slouken@9245
   894
#ifdef DEBUG_XEVENTS
slouken@9245
   895
                printf("XID of source window : %ld\n", data->xdnd_source);
slouken@9245
   896
                printf("Protocol version to use : %ld\n", xdnd_version);
slouken@9245
   897
                printf("More then 3 data types : %ld\n", use_list); 
slouken@9245
   898
#endif
slouken@9245
   899
 
slouken@7001
   900
                if (use_list) {
slouken@7001
   901
                    /* fetch conversion targets */
slouken@7001
   902
                    SDL_x11Prop p;
slouken@7001
   903
                    X11_ReadProperty(&p, display, data->xdnd_source, videodata->XdndTypeList);
slouken@7001
   904
                    /* pick one */
slouken@7001
   905
                    data->xdnd_req = X11_PickTarget(display, (Atom*)p.data, p.count);
icculus@7827
   906
                    X11_XFree(p.data);
slouken@7001
   907
                } else {
slouken@7001
   908
                    /* pick from list of three */
slouken@7001
   909
                    data->xdnd_req = X11_PickTargetFromAtoms(display, xevent.xclient.data.l[2], xevent.xclient.data.l[3], xevent.xclient.data.l[4]);
slouken@7001
   910
                }
slouken@7001
   911
            }
slouken@7001
   912
            else if (xevent.xclient.message_type == videodata->XdndPosition) {
slouken@9245
   913
            
slouken@9245
   914
#ifdef DEBUG_XEVENTS
slouken@9245
   915
                Atom act= videodata->XdndActionCopy;
slouken@9245
   916
                if(xdnd_version >= 2) {
slouken@9245
   917
                    act = xevent.xclient.data.l[4];
slouken@9245
   918
                }
slouken@9245
   919
                printf("Action requested by user is : %s\n", X11_XGetAtomName(display , act));
slouken@9245
   920
#endif
slouken@9245
   921
                
slouken@7191
   922
slouken@7001
   923
                /* reply with status */
slouken@7001
   924
                memset(&m, 0, sizeof(XClientMessageEvent));
slouken@7001
   925
                m.type = ClientMessage;
slouken@7001
   926
                m.display = xevent.xclient.display;
slouken@7001
   927
                m.window = xevent.xclient.data.l[0];
slouken@7001
   928
                m.message_type = videodata->XdndStatus;
slouken@7001
   929
                m.format=32;
slouken@7001
   930
                m.data.l[0] = data->xwindow;
slouken@7001
   931
                m.data.l[1] = (data->xdnd_req != None);
slouken@7001
   932
                m.data.l[2] = 0; /* specify an empty rectangle */
slouken@7001
   933
                m.data.l[3] = 0;
slouken@7001
   934
                m.data.l[4] = videodata->XdndActionCopy; /* we only accept copying anyway */
slouken@7191
   935
icculus@7827
   936
                X11_XSendEvent(display, xevent.xclient.data.l[0], False, NoEventMask, (XEvent*)&m);
icculus@7827
   937
                X11_XFlush(display);
slouken@7001
   938
            }
slouken@7001
   939
            else if(xevent.xclient.message_type == videodata->XdndDrop) {
slouken@7001
   940
                if (data->xdnd_req == None) {
slouken@7001
   941
                    /* say again - not interested! */
slouken@7001
   942
                    memset(&m, 0, sizeof(XClientMessageEvent));
slouken@7001
   943
                    m.type = ClientMessage;
slouken@7001
   944
                    m.display = xevent.xclient.display;
slouken@7001
   945
                    m.window = xevent.xclient.data.l[0];
slouken@7001
   946
                    m.message_type = videodata->XdndFinished;
slouken@7001
   947
                    m.format=32;
slouken@7001
   948
                    m.data.l[0] = data->xwindow;
slouken@7001
   949
                    m.data.l[1] = 0;
slouken@7001
   950
                    m.data.l[2] = None; /* fail! */
icculus@7827
   951
                    X11_XSendEvent(display, xevent.xclient.data.l[0], False, NoEventMask, (XEvent*)&m);
slouken@7001
   952
                } else {
slouken@7001
   953
                    /* convert */
slouken@7001
   954
                    if(xdnd_version >= 1) {
icculus@7827
   955
                        X11_XConvertSelection(display, videodata->XdndSelection, data->xdnd_req, videodata->PRIMARY, data->xwindow, xevent.xclient.data.l[2]);
slouken@7001
   956
                    } else {
icculus@7827
   957
                        X11_XConvertSelection(display, videodata->XdndSelection, data->xdnd_req, videodata->PRIMARY, data->xwindow, CurrentTime);
slouken@7001
   958
                    }
slouken@7001
   959
                }
slouken@7001
   960
            }
slouken@7001
   961
            else if ((xevent.xclient.message_type == videodata->WM_PROTOCOLS) &&
icculus@6637
   962
                (xevent.xclient.format == 32) &&
icculus@6638
   963
                (xevent.xclient.data.l[0] == videodata->_NET_WM_PING)) {
slouken@6659
   964
                Window root = DefaultRootWindow(display);
icculus@6638
   965
icculus@6638
   966
#ifdef DEBUG_XEVENTS
icculus@6638
   967
                printf("window %p: _NET_WM_PING\n", data);
icculus@6638
   968
#endif
icculus@6638
   969
                xevent.xclient.window = root;
icculus@7827
   970
                X11_XSendEvent(display, root, False, SubstructureRedirectMask | SubstructureNotifyMask, &xevent);
icculus@6638
   971
                break;
icculus@6638
   972
            }
icculus@6638
   973
icculus@6638
   974
            else if ((xevent.xclient.message_type == videodata->WM_PROTOCOLS) &&
icculus@6638
   975
                (xevent.xclient.format == 32) &&
slouken@1951
   976
                (xevent.xclient.data.l[0] == videodata->WM_DELETE_WINDOW)) {
slouken@1951
   977
icculus@6638
   978
#ifdef DEBUG_XEVENTS
icculus@6638
   979
                printf("window %p: WM_DELETE_WINDOW\n", data);
icculus@6638
   980
#endif
slouken@3685
   981
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
icculus@6638
   982
                break;
slouken@1951
   983
            }
slouken@1951
   984
        }
slouken@1951
   985
        break;
slouken@1951
   986
slouken@1951
   987
        /* Do we need to refresh ourselves? */
slouken@1951
   988
    case Expose:{
slouken@1951
   989
#ifdef DEBUG_XEVENTS
slouken@6504
   990
            printf("window %p: Expose (count = %d)\n", data, xevent.xexpose.count);
slouken@1951
   991
#endif
slouken@3685
   992
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
slouken@1951
   993
        }
slouken@1951
   994
        break;
slouken@1951
   995
slouken@4465
   996
    case MotionNotify:{
slouken@7191
   997
            SDL_Mouse *mouse = SDL_GetMouse();
slouken@8071
   998
            if(!mouse->relative_mode || mouse->relative_mode_warp) {
slouken@4465
   999
#ifdef DEBUG_MOTION
slouken@6504
  1000
                printf("window %p: X11 motion: %d,%d\n", xevent.xmotion.x, xevent.xmotion.y);
slouken@4465
  1001
#endif
slouken@6311
  1002
slouken@6950
  1003
                SDL_SendMouseMotion(data->window, 0, 0, xevent.xmotion.x, xevent.xmotion.y);
slouken@6311
  1004
            }
slouken@4465
  1005
        }
slouken@4465
  1006
        break;
slouken@4465
  1007
slouken@4465
  1008
    case ButtonPress:{
slouken@5530
  1009
            int ticks = 0;
slouken@6950
  1010
            if (X11_IsWheelEvent(display,&xevent,&ticks)) {
urkle@9257
  1011
                SDL_SendMouseWheel(data->window, 0, 0, ticks, SDL_MOUSEWHEEL_NORMAL);
slouken@6950
  1012
            } else {
icculus@8933
  1013
                if(xevent.xbutton.button == Button1) {
icculus@8935
  1014
                    if (ProcessHitTest(_this, data, &xevent)) {
icculus@8933
  1015
                        break;  /* don't pass this event on to app. */
icculus@8933
  1016
                    }
rush@8932
  1017
                }
slouken@6950
  1018
                SDL_SendMouseButton(data->window, 0, SDL_PRESSED, xevent.xbutton.button);
slouken@5530
  1019
            }
slouken@4465
  1020
        }
slouken@4465
  1021
        break;
slouken@4465
  1022
slouken@4465
  1023
    case ButtonRelease:{
slouken@6950
  1024
            SDL_SendMouseButton(data->window, 0, SDL_RELEASED, xevent.xbutton.button);
slouken@4465
  1025
        }
slouken@4465
  1026
        break;
slouken@4465
  1027
slouken@4518
  1028
    case PropertyNotify:{
slouken@4518
  1029
#ifdef DEBUG_XEVENTS
slouken@4520
  1030
            unsigned char *propdata;
slouken@4520
  1031
            int status, real_format;
slouken@4520
  1032
            Atom real_type;
slouken@4520
  1033
            unsigned long items_read, items_left, i;
slouken@4520
  1034
icculus@7827
  1035
            char *name = X11_XGetAtomName(display, xevent.xproperty.atom);
slouken@4518
  1036
            if (name) {
slouken@6504
  1037
                printf("window %p: PropertyNotify: %s %s\n", data, name, (xevent.xproperty.state == PropertyDelete) ? "deleted" : "changed");
icculus@7827
  1038
                X11_XFree(name);
slouken@4518
  1039
            }
slouken@4518
  1040
icculus@7827
  1041
            status = X11_XGetWindowProperty(display, data->xwindow, xevent.xproperty.atom, 0L, 8192L, False, AnyPropertyType, &real_type, &real_format, &items_read, &items_left, &propdata);
slouken@4556
  1042
            if (status == Success && items_read > 0) {
slouken@4520
  1043
                if (real_type == XA_INTEGER) {
slouken@4520
  1044
                    int *values = (int *)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
                } else if (real_type == XA_CARDINAL) {
slouken@4520
  1052
                    if (real_format == 32) {
slouken@4520
  1053
                        Uint32 *values = (Uint32 *)propdata;
slouken@4520
  1054
slouken@4520
  1055
                        printf("{");
slouken@4520
  1056
                        for (i = 0; i < items_read; i++) {
slouken@4520
  1057
                            printf(" %d", values[i]);
slouken@4520
  1058
                        }
slouken@4520
  1059
                        printf(" }\n");
slouken@4520
  1060
                    } else if (real_format == 16) {
slouken@4520
  1061
                        Uint16 *values = (Uint16 *)propdata;
slouken@4520
  1062
slouken@4520
  1063
                        printf("{");
slouken@4520
  1064
                        for (i = 0; i < items_read; i++) {
slouken@4520
  1065
                            printf(" %d", values[i]);
slouken@4520
  1066
                        }
slouken@4520
  1067
                        printf(" }\n");
slouken@4520
  1068
                    } else if (real_format == 8) {
slouken@4520
  1069
                        Uint8 *values = (Uint8 *)propdata;
slouken@4520
  1070
slouken@4520
  1071
                        printf("{");
slouken@4520
  1072
                        for (i = 0; i < items_read; i++) {
slouken@4520
  1073
                            printf(" %d", values[i]);
slouken@4520
  1074
                        }
slouken@4520
  1075
                        printf(" }\n");
slouken@4520
  1076
                    }
slouken@4520
  1077
                } else if (real_type == XA_STRING ||
slouken@4520
  1078
                           real_type == videodata->UTF8_STRING) {
slouken@4520
  1079
                    printf("{ \"%s\" }\n", propdata);
slouken@4520
  1080
                } else if (real_type == XA_ATOM) {
slouken@4518
  1081
                    Atom *atoms = (Atom *)propdata;
slouken@4520
  1082
slouken@4520
  1083
                    printf("{");
slouken@4518
  1084
                    for (i = 0; i < items_read; i++) {
icculus@7827
  1085
                        char *name = X11_XGetAtomName(display, atoms[i]);
slouken@4520
  1086
                        if (name) {
slouken@4520
  1087
                            printf(" %s", name);
icculus@7827
  1088
                            X11_XFree(name);
slouken@4518
  1089
                        }
slouken@4518
  1090
                    }
slouken@4520
  1091
                    printf(" }\n");
slouken@4520
  1092
                } else {
icculus@7827
  1093
                    char *name = X11_XGetAtomName(display, real_type);
slouken@4520
  1094
                    printf("Unknown type: %ld (%s)\n", real_type, name ? name : "UNKNOWN");
slouken@4520
  1095
                    if (name) {
icculus@7827
  1096
                        X11_XFree(name);
slouken@4520
  1097
                    }
slouken@4518
  1098
                }
slouken@4520
  1099
            }
slouken@6481
  1100
            if (status == Success) {
icculus@7827
  1101
                X11_XFree(propdata);
slouken@6481
  1102
            }
slouken@6481
  1103
#endif /* DEBUG_XEVENTS */
slouken@6481
  1104
slouken@6481
  1105
            if (xevent.xproperty.atom == data->videodata->_NET_WM_STATE) {
slouken@6481
  1106
                /* Get the new state from the window manager.
slouken@6481
  1107
                   Compositing window managers can alter visibility of windows
slouken@6481
  1108
                   without ever mapping / unmapping them, so we handle that here,
slouken@6481
  1109
                   because they use the NETWM protocol to notify us of changes.
slouken@6481
  1110
                 */
icculus@9331
  1111
                const Uint32 flags = X11_GetNetWMState(_this, xevent.xproperty.window);
icculus@9331
  1112
                const Uint32 changed = flags ^ data->window->flags;
icculus@9331
  1113
icculus@9331
  1114
                if ((changed & SDL_WINDOW_HIDDEN) || (changed & SDL_WINDOW_FULLSCREEN)) {
icculus@9331
  1115
                     if (flags & SDL_WINDOW_HIDDEN) {
icculus@9331
  1116
                         X11_DispatchUnmapNotify(data);
icculus@9331
  1117
                     } else {
icculus@9331
  1118
                         X11_DispatchMapNotify(data);
slouken@6481
  1119
                    }
slouken@6481
  1120
                }
icculus@9331
  1121
icculus@9331
  1122
                if (changed & SDL_WINDOW_MAXIMIZED) {
icculus@9331
  1123
                    if (flags & SDL_WINDOW_MAXIMIZED) {
icculus@9331
  1124
                        SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
icculus@9331
  1125
                    } else {
icculus@9331
  1126
                        SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESTORED, 0, 0);
icculus@9331
  1127
                    }
icculus@9331
  1128
                 }
icculus@9331
  1129
slouken@6481
  1130
            }
slouken@4518
  1131
        }
slouken@4518
  1132
        break;
slouken@4518
  1133
slouken@4508
  1134
    /* Copy the selection from XA_CUT_BUFFER0 to the requested property */
slouken@4508
  1135
    case SelectionRequest: {
slouken@4508
  1136
            XSelectionRequestEvent *req;
slouken@4508
  1137
            XEvent sevent;
slouken@4508
  1138
            int seln_format;
slouken@4508
  1139
            unsigned long nbytes;
slouken@4508
  1140
            unsigned long overflow;
slouken@4508
  1141
            unsigned char *seln_data;
slouken@4508
  1142
slouken@4508
  1143
            req = &xevent.xselectionrequest;
slouken@4508
  1144
#ifdef DEBUG_XEVENTS
slouken@6504
  1145
            printf("window %p: SelectionRequest (requestor = %ld, target = %ld)\n", data,
slouken@4508
  1146
                req->requestor, req->target);
slouken@4508
  1147
#endif
slouken@4508
  1148
slouken@4524
  1149
            SDL_zero(sevent);
slouken@4520
  1150
            sevent.xany.type = SelectionNotify;
slouken@4508
  1151
            sevent.xselection.selection = req->selection;
slouken@4508
  1152
            sevent.xselection.target = None;
slouken@4508
  1153
            sevent.xselection.property = None;
slouken@4508
  1154
            sevent.xselection.requestor = req->requestor;
slouken@4508
  1155
            sevent.xselection.time = req->time;
icculus@7827
  1156
            if (X11_XGetWindowProperty(display, DefaultRootWindow(display),
slouken@4508
  1157
                    XA_CUT_BUFFER0, 0, INT_MAX/4, False, req->target,
slouken@4508
  1158
                    &sevent.xselection.target, &seln_format, &nbytes,
slouken@4508
  1159
                    &overflow, &seln_data) == Success) {
icculus@7827
  1160
                Atom XA_TARGETS = X11_XInternAtom(display, "TARGETS", 0);
slouken@4508
  1161
                if (sevent.xselection.target == req->target) {
icculus@7827
  1162
                    X11_XChangeProperty(display, req->requestor, req->property,
slouken@4508
  1163
                        sevent.xselection.target, seln_format, PropModeReplace,
slouken@4508
  1164
                        seln_data, nbytes);
slouken@4508
  1165
                    sevent.xselection.property = req->property;
slouken@7021
  1166
                } else if (XA_TARGETS == req->target) {
slouken@7021
  1167
                    Atom SupportedFormats[] = { sevent.xselection.target, XA_TARGETS };
icculus@7827
  1168
                    X11_XChangeProperty(display, req->requestor, req->property,
slouken@7021
  1169
                        XA_ATOM, 32, PropModeReplace,
slouken@7021
  1170
                        (unsigned char*)SupportedFormats,
slouken@7021
  1171
                        sizeof(SupportedFormats)/sizeof(*SupportedFormats));
slouken@7021
  1172
                    sevent.xselection.property = req->property;
slouken@4508
  1173
                }
icculus@7827
  1174
                X11_XFree(seln_data);
slouken@4508
  1175
            }
icculus@7827
  1176
            X11_XSendEvent(display, req->requestor, False, 0, &sevent);
icculus@7827
  1177
            X11_XSync(display, False);
slouken@4508
  1178
        }
slouken@4508
  1179
        break;
slouken@4508
  1180
slouken@4508
  1181
    case SelectionNotify: {
slouken@4508
  1182
#ifdef DEBUG_XEVENTS
slouken@6504
  1183
            printf("window %p: SelectionNotify (requestor = %ld, target = %ld)\n", data,
slouken@4508
  1184
                xevent.xselection.requestor, xevent.xselection.target);
slouken@4508
  1185
#endif
slouken@7001
  1186
            Atom target = xevent.xselection.target;
slouken@7001
  1187
            if (target == data->xdnd_req) {
slouken@7001
  1188
                /* read data */
slouken@7001
  1189
                SDL_x11Prop p;
slouken@7001
  1190
                X11_ReadProperty(&p, display, data->xwindow, videodata->PRIMARY);
slouken@7191
  1191
slouken@7396
  1192
                if (p.format == 8) {
slouken@7001
  1193
                    SDL_bool expect_lf = SDL_FALSE;
slouken@7001
  1194
                    char *start = NULL;
slouken@7001
  1195
                    char *scan = (char*)p.data;
slouken@7001
  1196
                    char *fn;
slouken@7001
  1197
                    char *uri;
slouken@7001
  1198
                    int length = 0;
slouken@7001
  1199
                    while (p.count--) {
slouken@7001
  1200
                        if (!expect_lf) {
slouken@7396
  1201
                            if (*scan == 0x0D) {
slouken@7001
  1202
                                expect_lf = SDL_TRUE;
slouken@7396
  1203
                            }
slouken@7396
  1204
                            if (start == NULL) {
slouken@7001
  1205
                                start = scan;
slouken@7001
  1206
                                length = 0;
slouken@7001
  1207
                            }
slouken@7001
  1208
                            length++;
slouken@7001
  1209
                        } else {
slouken@7396
  1210
                            if (*scan == 0x0A && length > 0) {
slouken@7396
  1211
                                uri = SDL_malloc(length--);
slouken@7396
  1212
                                SDL_memcpy(uri, start, length);
slouken@7396
  1213
                                uri[length] = '\0';
slouken@7001
  1214
                                fn = X11_URIToLocal(uri);
slouken@7396
  1215
                                if (fn) {
slouken@7396
  1216
                                    SDL_SendDropFile(fn);
slouken@7396
  1217
                                }
slouken@7396
  1218
                                SDL_free(uri);
slouken@7001
  1219
                            }
slouken@7001
  1220
                            expect_lf = SDL_FALSE;
slouken@7001
  1221
                            start = NULL;
slouken@7001
  1222
                        }
slouken@7001
  1223
                        scan++;
slouken@7001
  1224
                    }
slouken@7001
  1225
                }
slouken@7191
  1226
icculus@7827
  1227
                X11_XFree(p.data);
slouken@7191
  1228
slouken@7001
  1229
                /* send reply */
slouken@7396
  1230
                SDL_memset(&m, 0, sizeof(XClientMessageEvent));
slouken@7001
  1231
                m.type = ClientMessage;
slouken@7001
  1232
                m.display = display;
slouken@7001
  1233
                m.window = data->xdnd_source;
slouken@7001
  1234
                m.message_type = videodata->XdndFinished;
slouken@7396
  1235
                m.format = 32;
slouken@7001
  1236
                m.data.l[0] = data->xwindow;
slouken@7001
  1237
                m.data.l[1] = 1;
slouken@7001
  1238
                m.data.l[2] = videodata->XdndActionCopy;
icculus@7827
  1239
                X11_XSendEvent(display, data->xdnd_source, False, NoEventMask, (XEvent*)&m);
slouken@7191
  1240
icculus@7827
  1241
                X11_XSync(display, False);
slouken@7191
  1242
slouken@7001
  1243
            } else {
slouken@7001
  1244
                videodata->selection_waiting = SDL_FALSE;
slouken@7001
  1245
            }
slouken@4508
  1246
        }
slouken@4508
  1247
        break;
slouken@4508
  1248
slouken@1951
  1249
    default:{
slouken@1951
  1250
#ifdef DEBUG_XEVENTS
slouken@6504
  1251
            printf("window %p: Unhandled event %d\n", data, xevent.type);
slouken@1951
  1252
#endif
slouken@1951
  1253
        }
slouken@1951
  1254
        break;
slouken@1951
  1255
    }
slouken@1951
  1256
}
slouken@1951
  1257
slouken@6504
  1258
static void
slouken@6504
  1259
X11_HandleFocusChanges(_THIS)
slouken@6504
  1260
{
slouken@6504
  1261
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@6504
  1262
    int i;
slouken@6504
  1263
slouken@6504
  1264
    if (videodata && videodata->windowlist) {
slouken@6504
  1265
        for (i = 0; i < videodata->numwindows; ++i) {
slouken@6504
  1266
            SDL_WindowData *data = videodata->windowlist[i];
slouken@6504
  1267
            if (data && data->pending_focus != PENDING_FOCUS_NONE) {
slouken@6504
  1268
                Uint32 now = SDL_GetTicks();
slouken@7857
  1269
                if (SDL_TICKS_PASSED(now, data->pending_focus_time)) {
slouken@6504
  1270
                    if ( data->pending_focus == PENDING_FOCUS_IN ) {
slouken@6504
  1271
                        X11_DispatchFocusIn(data);
slouken@6504
  1272
                    } else {
slouken@6504
  1273
                        X11_DispatchFocusOut(data);
slouken@6504
  1274
                    }
slouken@6504
  1275
                    data->pending_focus = PENDING_FOCUS_NONE;
slouken@6504
  1276
                }
slouken@6504
  1277
            }
slouken@6504
  1278
        }
slouken@6504
  1279
    }
slouken@6504
  1280
}
icculus@7827
  1281
/* Ack!  X11_XPending() actually performs a blocking read if no events available */
slouken@4472
  1282
static int
slouken@1951
  1283
X11_Pending(Display * display)
slouken@1951
  1284
{
slouken@1951
  1285
    /* Flush the display connection and look to see if events are queued */
icculus@7827
  1286
    X11_XFlush(display);
icculus@7827
  1287
    if (X11_XEventsQueued(display, QueuedAlready)) {
slouken@1951
  1288
        return (1);
slouken@1951
  1289
    }
slouken@1951
  1290
slouken@1951
  1291
    /* More drastic measures are required -- see if X is ready to talk */
slouken@1951
  1292
    {
slouken@1951
  1293
        static struct timeval zero_time;        /* static == 0 */
slouken@1951
  1294
        int x11_fd;
slouken@1951
  1295
        fd_set fdset;
slouken@1951
  1296
slouken@1951
  1297
        x11_fd = ConnectionNumber(display);
slouken@1951
  1298
        FD_ZERO(&fdset);
slouken@1951
  1299
        FD_SET(x11_fd, &fdset);
slouken@1951
  1300
        if (select(x11_fd + 1, &fdset, NULL, NULL, &zero_time) == 1) {
icculus@7827
  1301
            return (X11_XPending(display));
slouken@1951
  1302
        }
slouken@1951
  1303
    }
slouken@1951
  1304
slouken@1951
  1305
    /* Oh well, nothing is ready .. */
slouken@1951
  1306
    return (0);
slouken@1951
  1307
}
slouken@1951
  1308
slouken@1951
  1309
void
slouken@1951
  1310
X11_PumpEvents(_THIS)
slouken@1951
  1311
{
slouken@1951
  1312
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
  1313
icculus@9319
  1314
    if (data->last_mode_change_deadline) {
icculus@9319
  1315
        if (SDL_TICKS_PASSED(SDL_GetTicks(), data->last_mode_change_deadline)) {
icculus@9319
  1316
            data->last_mode_change_deadline = 0;  /* assume we're done. */
icculus@9319
  1317
        }
icculus@9319
  1318
    }
icculus@9319
  1319
slouken@3025
  1320
    /* Update activity every 30 seconds to prevent screensaver */
slouken@3025
  1321
    if (_this->suspend_screensaver) {
icculus@9319
  1322
        const Uint32 now = SDL_GetTicks();
slouken@3025
  1323
        if (!data->screensaver_activity ||
slouken@7857
  1324
            SDL_TICKS_PASSED(now, data->screensaver_activity + 30000)) {
icculus@7827
  1325
            X11_XResetScreenSaver(data->display);
icculus@6774
  1326
alex@8889
  1327
#if SDL_USE_LIBDBUS
alex@8889
  1328
            SDL_DBus_ScreensaverTickle();
alex@8889
  1329
#endif
icculus@6774
  1330
slouken@3025
  1331
            data->screensaver_activity = now;
slouken@3025
  1332
        }
slouken@7191
  1333
    }
slouken@3025
  1334
alex@8889
  1335
#ifdef SDL_USE_IBUS
alex@8889
  1336
    if(SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE){
alex@8889
  1337
        SDL_IBus_PumpEvents();
alex@8889
  1338
    }
alex@8889
  1339
#endif
alex@8889
  1340
slouken@1951
  1341
    /* Keep processing pending events */
slouken@1951
  1342
    while (X11_Pending(data->display)) {
slouken@1951
  1343
        X11_DispatchEvent(_this);
slouken@1951
  1344
    }
slouken@6504
  1345
slouken@6504
  1346
    /* FIXME: Only need to do this when there are pending focus changes */
slouken@6504
  1347
    X11_HandleFocusChanges(_this);
slouken@1951
  1348
}
slouken@1951
  1349
slouken@3030
  1350
slouken@1951
  1351
void
slouken@3025
  1352
X11_SuspendScreenSaver(_THIS)
slouken@1951
  1353
{
slouken@5408
  1354
#if SDL_VIDEO_DRIVER_X11_XSCRNSAVER
slouken@3025
  1355
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@3025
  1356
    int dummy;
slouken@3025
  1357
    int major_version, minor_version;
hadess@8287
  1358
#endif /* SDL_VIDEO_DRIVER_X11_XSCRNSAVER */
slouken@1951
  1359
hadess@8287
  1360
#if SDL_USE_LIBDBUS
alex@8889
  1361
    if (SDL_DBus_ScreensaverInhibit(_this->suspend_screensaver)) {
hadess@8287
  1362
        return;
hadess@8287
  1363
    }
hadess@8287
  1364
hadess@8287
  1365
    if (_this->suspend_screensaver) {
alex@8889
  1366
        SDL_DBus_ScreensaverTickle();
hadess@8287
  1367
    }
hadess@8287
  1368
#endif
hadess@8287
  1369
hadess@8287
  1370
#if SDL_VIDEO_DRIVER_X11_XSCRNSAVER
slouken@3030
  1371
    if (SDL_X11_HAVE_XSS) {
icculus@7827
  1372
        /* X11_XScreenSaverSuspend was introduced in MIT-SCREEN-SAVER 1.1 */
icculus@7827
  1373
        if (!X11_XScreenSaverQueryExtension(data->display, &dummy, &dummy) ||
icculus@7827
  1374
            !X11_XScreenSaverQueryVersion(data->display,
slouken@3030
  1375
                                      &major_version, &minor_version) ||
slouken@3030
  1376
            major_version < 1 || (major_version == 1 && minor_version < 1)) {
slouken@3030
  1377
            return;
slouken@3030
  1378
        }
slouken@3030
  1379
icculus@7827
  1380
        X11_XScreenSaverSuspend(data->display, _this->suspend_screensaver);
icculus@7827
  1381
        X11_XResetScreenSaver(data->display);
slouken@1951
  1382
    }
slouken@3030
  1383
#endif
slouken@1951
  1384
}
slouken@1951
  1385
slouken@5481
  1386
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
  1387
slouken@1951
  1388
/* vi: set ts=4 sw=4 expandtab: */