src/video/x11/SDL_x11events.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 09 May 2010 20:47:22 -0700
changeset 4465 3e69e077cb95
parent 4429 faa9fc8e7f67
child 4472 791b3256fb22
permissions -rw-r--r--
Removed multi-mouse / multi-keyboard support in anticipation of a real multi-mouse and multi-touch API.

Plus, this lets me start implementing cursor support.
slouken@1951
     1
/*
slouken@1951
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 Sam Lantinga
slouken@1951
     4
slouken@1951
     5
    This library is free software; you can redistribute it and/or
slouken@1951
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1951
     7
    License as published by the Free Software Foundation; either
slouken@1951
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1951
     9
slouken@1951
    10
    This library is distributed in the hope that it will be useful,
slouken@1951
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1951
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1951
    13
    Lesser General Public License for more details.
slouken@1951
    14
slouken@1951
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1951
    16
    License along with this library; if not, write to the Free Software
slouken@1951
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1951
    18
slouken@1951
    19
    Sam Lantinga
slouken@1951
    20
    slouken@libsdl.org
slouken@1951
    21
*/
slouken@1951
    22
#include "SDL_config.h"
slouken@1951
    23
slouken@2046
    24
#include <sys/types.h>
slouken@2046
    25
#include <sys/time.h>
slouken@4465
    26
#include <signal.h>
slouken@2046
    27
#include <unistd.h>
slouken@2046
    28
slouken@1951
    29
#include "SDL_x11video.h"
slouken@1951
    30
#include "../../events/SDL_events_c.h"
slouken@2940
    31
#include "../../events/SDL_mouse_c.h"
slouken@1951
    32
slouken@4465
    33
#include "SDL_timer.h"
slouken@3241
    34
#include "SDL_syswm.h"
slouken@3241
    35
slouken@1951
    36
static void
slouken@1951
    37
X11_DispatchEvent(_THIS)
slouken@1951
    38
{
slouken@1951
    39
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1951
    40
    SDL_WindowData *data;
slouken@1951
    41
    XEvent xevent;
slouken@1951
    42
    int i;
slouken@1951
    43
slouken@1951
    44
    SDL_zero(xevent);           /* valgrind fix. --ryan. */
slouken@1951
    45
    XNextEvent(videodata->display, &xevent);
slouken@1951
    46
bob@2325
    47
    /* filter events catchs XIM events and sends them to the correct
bob@2325
    48
       handler */
bob@2325
    49
    if (XFilterEvent(&xevent, None) == True) {
bob@2327
    50
#if 0
bob@2328
    51
        printf("Filtered event type = %d display = %d window = %d\n",
bob@2328
    52
               xevent.type, xevent.xany.display, xevent.xany.window);
bob@2325
    53
#endif
bob@2325
    54
        return;
bob@2325
    55
    }
bob@2325
    56
slouken@1951
    57
    /* Send a SDL_SYSWMEVENT if the application wants them */
slouken@4429
    58
    if (SDL_GetEventState(SDL_SYSWMEVENT) == SDL_ENABLE) {
slouken@1951
    59
        SDL_SysWMmsg wmmsg;
slouken@1951
    60
slouken@1951
    61
        SDL_VERSION(&wmmsg.version);
slouken@1951
    62
        wmmsg.subsystem = SDL_SYSWM_X11;
slouken@1951
    63
        wmmsg.event.xevent = xevent;
slouken@1951
    64
        SDL_SendSysWMEvent(&wmmsg);
slouken@1951
    65
    }
slouken@1951
    66
slouken@1951
    67
    data = NULL;
bob@2324
    68
    if (videodata && videodata->windowlist) {
bob@2324
    69
        for (i = 0; i < videodata->numwindows; ++i) {
bob@2324
    70
            if ((videodata->windowlist[i] != NULL) &&
slouken@3685
    71
                (videodata->windowlist[i]->xwindow == xevent.xany.window)) {
bob@2324
    72
                data = videodata->windowlist[i];
bob@2324
    73
                break;
bob@2324
    74
            }
slouken@1951
    75
        }
slouken@1951
    76
    }
slouken@1951
    77
    if (!data) {
slouken@1951
    78
        return;
slouken@1951
    79
    }
bob@2327
    80
#if 0
bob@2328
    81
    printf("type = %d display = %d window = %d\n",
bob@2328
    82
           xevent.type, xevent.xany.display, xevent.xany.window);
bob@2327
    83
#endif
slouken@1951
    84
    switch (xevent.type) {
slouken@1951
    85
slouken@1951
    86
        /* Gaining mouse coverage? */
slouken@1951
    87
    case EnterNotify:{
slouken@1951
    88
#ifdef DEBUG_XEVENTS
bob@3195
    89
            printf("EnterNotify! (%d,%d,%d)\n", 
bob@3195
    90
	           xevent.xcrossing.x,
bob@3195
    91
 	           xevent.xcrossing.y,
bob@3195
    92
                   xevent.xcrossing.mode);
slouken@1951
    93
            if (xevent.xcrossing.mode == NotifyGrab)
slouken@1951
    94
                printf("Mode: NotifyGrab\n");
slouken@1951
    95
            if (xevent.xcrossing.mode == NotifyUngrab)
slouken@1951
    96
                printf("Mode: NotifyUngrab\n");
slouken@1951
    97
#endif
slouken@4465
    98
            SDL_SetMouseFocus(data->window);
slouken@1951
    99
        }
slouken@1951
   100
        break;
slouken@1951
   101
slouken@1951
   102
        /* Losing mouse coverage? */
slouken@1951
   103
    case LeaveNotify:{
slouken@1951
   104
#ifdef DEBUG_XEVENTS
bob@3195
   105
            printf("LeaveNotify! (%d,%d,%d)\n", 
bob@3195
   106
	           xevent.xcrossing.x,
bob@3195
   107
 	           xevent.xcrossing.y,
bob@3195
   108
                   xevent.xcrossing.mode);
slouken@1951
   109
            if (xevent.xcrossing.mode == NotifyGrab)
slouken@1951
   110
                printf("Mode: NotifyGrab\n");
slouken@1951
   111
            if (xevent.xcrossing.mode == NotifyUngrab)
slouken@1951
   112
                printf("Mode: NotifyUngrab\n");
slouken@1951
   113
#endif
slouken@3322
   114
            if (xevent.xcrossing.detail != NotifyInferior) {
slouken@4465
   115
                SDL_SetMouseFocus(NULL);
slouken@1951
   116
            }
slouken@1951
   117
        }
slouken@1951
   118
        break;
slouken@1951
   119
slouken@1951
   120
        /* Gaining input focus? */
slouken@1951
   121
    case FocusIn:{
slouken@1951
   122
#ifdef DEBUG_XEVENTS
slouken@1951
   123
            printf("FocusIn!\n");
slouken@1951
   124
#endif
slouken@4465
   125
            SDL_SetKeyboardFocus(data->window);
slouken@1951
   126
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   127
            if (data->ic) {
slouken@1951
   128
                XSetICFocus(data->ic);
slouken@1951
   129
            }
slouken@1951
   130
#endif
slouken@1951
   131
        }
slouken@1951
   132
        break;
slouken@1951
   133
slouken@1951
   134
        /* Losing input focus? */
slouken@1951
   135
    case FocusOut:{
slouken@1951
   136
#ifdef DEBUG_XEVENTS
slouken@1951
   137
            printf("FocusOut!\n");
slouken@1951
   138
#endif
slouken@4465
   139
            SDL_SetKeyboardFocus(NULL);
slouken@1951
   140
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   141
            if (data->ic) {
slouken@1951
   142
                XUnsetICFocus(data->ic);
slouken@1951
   143
            }
slouken@1951
   144
#endif
slouken@1951
   145
        }
slouken@1951
   146
        break;
slouken@1951
   147
slouken@1951
   148
        /* Generated upon EnterWindow and FocusIn */
slouken@1951
   149
    case KeymapNotify:{
slouken@1951
   150
#ifdef DEBUG_XEVENTS
slouken@1951
   151
            printf("KeymapNotify!\n");
slouken@1951
   152
#endif
slouken@1951
   153
            /* FIXME:
slouken@1951
   154
               X11_SetKeyboardState(SDL_Display, xevent.xkeymap.key_vector);
slouken@1951
   155
             */
slouken@1951
   156
        }
slouken@1951
   157
        break;
slouken@1951
   158
slouken@2305
   159
        /* Has the keyboard layout changed? */
slouken@2305
   160
    case MappingNotify:{
slouken@2305
   161
#ifdef DEBUG_XEVENTS
slouken@2305
   162
            printf("MappingNotify!\n");
slouken@2305
   163
#endif
slouken@2306
   164
            X11_UpdateKeymap(_this);
slouken@2305
   165
        }
slouken@2305
   166
        break;
slouken@2305
   167
slouken@1951
   168
        /* Key press? */
slouken@1951
   169
    case KeyPress:{
slouken@1951
   170
            KeyCode keycode = xevent.xkey.keycode;
bob@2300
   171
            KeySym keysym = NoSymbol;
slouken@2306
   172
            char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];
bob@2325
   173
            Status status = 0;
slouken@1951
   174
slouken@1951
   175
#ifdef DEBUG_XEVENTS
slouken@1951
   176
            printf("KeyPress (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
slouken@1951
   177
#endif
slouken@4465
   178
            SDL_SendKeyboardKey(SDL_PRESSED, videodata->key_layout[keycode]);
bob@2325
   179
#if 0
slouken@2305
   180
            if (videodata->key_layout[keycode] == SDLK_UNKNOWN) {
slouken@2305
   181
                int min_keycode, max_keycode;
slouken@2305
   182
                XDisplayKeycodes(videodata->display, &min_keycode,
slouken@2305
   183
                                 &max_keycode);
slouken@2305
   184
                keysym = XKeycodeToKeysym(videodata->display, keycode, 0);
bob@2299
   185
                fprintf(stderr,
slouken@2305
   186
                        "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%X (%s).\n",
slouken@2305
   187
                        keycode, keycode - min_keycode, keysym,
slouken@2305
   188
                        XKeysymToString(keysym));
bob@2299
   189
            }
bob@2295
   190
#endif
bob@2325
   191
            /* */
slouken@2305
   192
            SDL_zero(text);
bob@2325
   193
#ifdef X_HAVE_UTF8_STRING
bob@2325
   194
            if (data->ic) {
bob@2325
   195
                Xutf8LookupString(data->ic, &xevent.xkey, text, sizeof(text),
slouken@2738
   196
                                  &keysym, &status);
bob@2325
   197
            }
bob@2325
   198
#else
slouken@2306
   199
            XLookupString(&xevent.xkey, text, sizeof(text), &keysym, NULL);
bob@2325
   200
#endif
slouken@2305
   201
            if (*text) {
slouken@4465
   202
                SDL_SendKeyboardText(text);
bob@2300
   203
            }
slouken@1951
   204
        }
slouken@1951
   205
        break;
slouken@1951
   206
slouken@1951
   207
        /* Key release? */
slouken@1951
   208
    case KeyRelease:{
slouken@1951
   209
            KeyCode keycode = xevent.xkey.keycode;
slouken@1951
   210
slouken@1951
   211
#ifdef DEBUG_XEVENTS
slouken@1951
   212
            printf("KeyRelease (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
slouken@1951
   213
#endif
slouken@4465
   214
            SDL_SendKeyboardKey(SDL_RELEASED, videodata->key_layout[keycode]);
slouken@1951
   215
        }
slouken@1951
   216
        break;
slouken@1951
   217
slouken@1951
   218
        /* Have we been iconified? */
slouken@1951
   219
    case UnmapNotify:{
slouken@1951
   220
#ifdef DEBUG_XEVENTS
slouken@1951
   221
            printf("UnmapNotify!\n");
slouken@1951
   222
#endif
slouken@3685
   223
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@3685
   224
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1951
   225
        }
slouken@1951
   226
        break;
slouken@1951
   227
slouken@1951
   228
        /* Have we been restored? */
slouken@1951
   229
    case MapNotify:{
slouken@1951
   230
#ifdef DEBUG_XEVENTS
slouken@1951
   231
            printf("MapNotify!\n");
slouken@1951
   232
#endif
slouken@3685
   233
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@3685
   234
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@1951
   235
        }
slouken@1951
   236
        break;
slouken@1951
   237
slouken@1951
   238
        /* Have we been resized or moved? */
slouken@1951
   239
    case ConfigureNotify:{
slouken@1951
   240
#ifdef DEBUG_XEVENTS
slouken@1951
   241
            printf("ConfigureNotify! (resize: %dx%d)\n",
slouken@1951
   242
                   xevent.xconfigure.width, xevent.xconfigure.height);
slouken@1951
   243
#endif
slouken@3685
   244
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED,
slouken@1951
   245
                                xevent.xconfigure.x, xevent.xconfigure.y);
slouken@3685
   246
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED,
slouken@1951
   247
                                xevent.xconfigure.width,
slouken@1951
   248
                                xevent.xconfigure.height);
slouken@1951
   249
        }
slouken@1951
   250
        break;
slouken@1951
   251
slouken@1951
   252
        /* Have we been requested to quit (or another client message?) */
slouken@1951
   253
    case ClientMessage:{
slouken@1951
   254
            if ((xevent.xclient.format == 32) &&
slouken@1951
   255
                (xevent.xclient.data.l[0] == videodata->WM_DELETE_WINDOW)) {
slouken@1951
   256
slouken@3685
   257
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
slouken@1951
   258
            }
slouken@1951
   259
        }
slouken@1951
   260
        break;
slouken@1951
   261
slouken@1951
   262
        /* Do we need to refresh ourselves? */
slouken@1951
   263
    case Expose:{
slouken@1951
   264
#ifdef DEBUG_XEVENTS
slouken@1951
   265
            printf("Expose (count = %d)\n", xevent.xexpose.count);
slouken@1951
   266
#endif
slouken@3685
   267
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
slouken@1951
   268
        }
slouken@1951
   269
        break;
slouken@1951
   270
slouken@4465
   271
    case MotionNotify:{
slouken@4465
   272
#ifdef DEBUG_MOTION
slouken@4465
   273
            printf("X11 motion: %d,%d\n", xevent.xmotion.x, xevent.xmotion.y);
slouken@4465
   274
#endif
slouken@4465
   275
            SDL_SendMouseMotion(0, xevent.xmotion.x, xevent.xmotion.y);
slouken@4465
   276
        }
slouken@4465
   277
        break;
slouken@4465
   278
slouken@4465
   279
    case ButtonPress:{
slouken@4465
   280
            SDL_SendMouseButton(SDL_PRESSED, xevent.xbutton.button);
slouken@4465
   281
        }
slouken@4465
   282
        break;
slouken@4465
   283
slouken@4465
   284
    case ButtonRelease:{
slouken@4465
   285
            SDL_SendMouseButton(SDL_RELEASED, xevent.xbutton.button);
slouken@4465
   286
        }
slouken@4465
   287
        break;
slouken@4465
   288
slouken@1951
   289
    default:{
slouken@1951
   290
#ifdef DEBUG_XEVENTS
slouken@2940
   291
            printf("Unhandled event %d\n", xevent.type);
slouken@1951
   292
#endif
slouken@1951
   293
        }
slouken@1951
   294
        break;
slouken@1951
   295
    }
slouken@1951
   296
}
slouken@1951
   297
slouken@1951
   298
/* Ack!  XPending() actually performs a blocking read if no events available */
slouken@1951
   299
int
slouken@1951
   300
X11_Pending(Display * display)
slouken@1951
   301
{
slouken@1951
   302
    /* Flush the display connection and look to see if events are queued */
slouken@1951
   303
    XFlush(display);
slouken@1951
   304
    if (XEventsQueued(display, QueuedAlready)) {
slouken@1951
   305
        return (1);
slouken@1951
   306
    }
slouken@1951
   307
slouken@1951
   308
    /* More drastic measures are required -- see if X is ready to talk */
slouken@1951
   309
    {
slouken@1951
   310
        static struct timeval zero_time;        /* static == 0 */
slouken@1951
   311
        int x11_fd;
slouken@1951
   312
        fd_set fdset;
slouken@1951
   313
slouken@1951
   314
        x11_fd = ConnectionNumber(display);
slouken@1951
   315
        FD_ZERO(&fdset);
slouken@1951
   316
        FD_SET(x11_fd, &fdset);
slouken@1951
   317
        if (select(x11_fd + 1, &fdset, NULL, NULL, &zero_time) == 1) {
slouken@1951
   318
            return (XPending(display));
slouken@1951
   319
        }
slouken@1951
   320
    }
slouken@1951
   321
slouken@1951
   322
    /* Oh well, nothing is ready .. */
slouken@1951
   323
    return (0);
slouken@1951
   324
}
slouken@1951
   325
slouken@1951
   326
void
slouken@1951
   327
X11_PumpEvents(_THIS)
slouken@1951
   328
{
slouken@1951
   329
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   330
slouken@3025
   331
    /* Update activity every 30 seconds to prevent screensaver */
slouken@3025
   332
    if (_this->suspend_screensaver) {
slouken@3025
   333
        Uint32 now = SDL_GetTicks();
slouken@3025
   334
        if (!data->screensaver_activity ||
slouken@3040
   335
            (int) (now - data->screensaver_activity) >= 30000) {
slouken@3025
   336
            XResetScreenSaver(data->display);
slouken@3025
   337
            data->screensaver_activity = now;
slouken@3025
   338
        }
slouken@3025
   339
    }
slouken@3025
   340
slouken@1951
   341
    /* Keep processing pending events */
slouken@1951
   342
    while (X11_Pending(data->display)) {
slouken@1951
   343
        X11_DispatchEvent(_this);
slouken@1951
   344
    }
slouken@1951
   345
}
slouken@1951
   346
slouken@3030
   347
/* This is so wrong it hurts */
slouken@3030
   348
#define GNOME_SCREENSAVER_HACK
slouken@3030
   349
#ifdef GNOME_SCREENSAVER_HACK
slouken@3030
   350
#include <unistd.h>
slouken@3030
   351
static pid_t screensaver_inhibit_pid;
slouken@3040
   352
static void
slouken@3040
   353
gnome_screensaver_disable()
slouken@3030
   354
{
slouken@3030
   355
    screensaver_inhibit_pid = fork();
slouken@3030
   356
    if (screensaver_inhibit_pid == 0) {
slouken@3031
   357
        close(0);
slouken@3031
   358
        close(1);
slouken@3031
   359
        close(2);
slouken@3030
   360
        execl("/usr/bin/gnome-screensaver-command",
slouken@3030
   361
              "gnome-screensaver-command",
slouken@3030
   362
              "--inhibit",
slouken@3030
   363
              "--reason",
slouken@3040
   364
              "GNOME screensaver doesn't respect MIT-SCREEN-SAVER", NULL);
slouken@3030
   365
        exit(2);
slouken@3030
   366
    }
slouken@3030
   367
}
slouken@3040
   368
static void
slouken@3040
   369
gnome_screensaver_enable()
slouken@3030
   370
{
slouken@3030
   371
    kill(screensaver_inhibit_pid, 15);
slouken@3030
   372
}
slouken@3030
   373
#endif
slouken@3030
   374
slouken@1951
   375
void
slouken@3025
   376
X11_SuspendScreenSaver(_THIS)
slouken@1951
   377
{
slouken@3025
   378
#if SDL_VIDEO_DRIVER_X11_SCRNSAVER
slouken@3025
   379
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@3025
   380
    int dummy;
slouken@3025
   381
    int major_version, minor_version;
slouken@1951
   382
slouken@3030
   383
    if (SDL_X11_HAVE_XSS) {
slouken@3030
   384
        /* XScreenSaverSuspend was introduced in MIT-SCREEN-SAVER 1.1 */
slouken@3030
   385
        if (!XScreenSaverQueryExtension(data->display, &dummy, &dummy) ||
slouken@3030
   386
            !XScreenSaverQueryVersion(data->display,
slouken@3030
   387
                                      &major_version, &minor_version) ||
slouken@3030
   388
            major_version < 1 || (major_version == 1 && minor_version < 1)) {
slouken@3030
   389
            return;
slouken@3030
   390
        }
slouken@3030
   391
slouken@3030
   392
        XScreenSaverSuspend(data->display, _this->suspend_screensaver);
slouken@3030
   393
        XResetScreenSaver(data->display);
slouken@1951
   394
    }
slouken@3030
   395
#endif
slouken@1951
   396
slouken@3030
   397
#ifdef GNOME_SCREENSAVER_HACK
slouken@3030
   398
    if (_this->suspend_screensaver) {
slouken@3030
   399
        gnome_screensaver_disable();
slouken@3030
   400
    } else {
slouken@3030
   401
        gnome_screensaver_enable();
slouken@3025
   402
    }
slouken@3025
   403
#endif
slouken@1951
   404
}
slouken@1951
   405
slouken@1951
   406
/* vi: set ts=4 sw=4 expandtab: */