src/video/x11/SDL_x11events.c
author Bob Pendleton <bob@pendleton.com>
Tue, 08 Jan 2008 00:10:46 +0000
changeset 2295 dbc6d1893869
parent 2046 da8332c8f480
child 2299 a7cbc25071b6
permissions -rw-r--r--
Checking in Christian Walther's patch for x11 keyboard input. Minor code tweaks by Bob.
slouken@1951
     1
/*
slouken@1951
     2
    SDL - Simple DirectMedia Layer
slouken@1951
     3
    Copyright (C) 1997-2006 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@2046
    26
#include <unistd.h>
slouken@2046
    27
slouken@1951
    28
#include "SDL_syswm.h"
slouken@1951
    29
#include "SDL_x11video.h"
slouken@1951
    30
#include "../../events/SDL_events_c.h"
slouken@1951
    31
slouken@1951
    32
bob@2295
    33
/* Check to see if this is a repeated key.
bob@2295
    34
   (idea shamelessly lifted from GII -- thanks guys! :)
bob@2295
    35
 */
bob@2295
    36
static int
bob@2295
    37
X11_KeyRepeat(Display * display, XEvent * event)
bob@2295
    38
{
bob@2295
    39
    XEvent peekevent;
bob@2295
    40
    int repeated;
bob@2295
    41
bob@2295
    42
    repeated = 0;
bob@2295
    43
    if (XPending(display)) {
bob@2295
    44
        XPeekEvent(display, &peekevent);
bob@2295
    45
        if ((peekevent.type == KeyPress) &&
bob@2295
    46
            (peekevent.xkey.keycode == event->xkey.keycode) &&
bob@2295
    47
            ((peekevent.xkey.time - event->xkey.time) < 2)) {
bob@2295
    48
            repeated = 1;
bob@2295
    49
            XNextEvent(display, &peekevent);
bob@2295
    50
        }
bob@2295
    51
    }
bob@2295
    52
    return (repeated);
bob@2295
    53
}
bob@2295
    54
slouken@1951
    55
static void
slouken@1951
    56
X11_DispatchEvent(_THIS)
slouken@1951
    57
{
slouken@1951
    58
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1951
    59
    SDL_WindowData *data;
slouken@1951
    60
    XEvent xevent;
slouken@1951
    61
    int i;
slouken@1951
    62
slouken@1951
    63
    SDL_zero(xevent);           /* valgrind fix. --ryan. */
slouken@1951
    64
    XNextEvent(videodata->display, &xevent);
slouken@1951
    65
slouken@1951
    66
    /* Send a SDL_SYSWMEVENT if the application wants them */
slouken@1951
    67
    if (SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE) {
slouken@1951
    68
        SDL_SysWMmsg wmmsg;
slouken@1951
    69
slouken@1951
    70
        SDL_VERSION(&wmmsg.version);
slouken@1951
    71
        wmmsg.subsystem = SDL_SYSWM_X11;
slouken@1951
    72
        wmmsg.event.xevent = xevent;
slouken@1951
    73
        SDL_SendSysWMEvent(&wmmsg);
slouken@1951
    74
    }
slouken@1951
    75
slouken@1951
    76
    data = NULL;
slouken@1951
    77
    for (i = 0; i < videodata->numwindows; ++i) {
slouken@1951
    78
        if (videodata->windowlist[i]->window == xevent.xany.window) {
slouken@1951
    79
            data = videodata->windowlist[i];
slouken@1951
    80
        }
slouken@1951
    81
    }
slouken@1951
    82
    if (!data) {
slouken@1951
    83
        return;
slouken@1951
    84
    }
slouken@1951
    85
slouken@1951
    86
    switch (xevent.type) {
slouken@1951
    87
slouken@1951
    88
        /* Gaining mouse coverage? */
slouken@1951
    89
    case EnterNotify:{
slouken@1951
    90
#ifdef DEBUG_XEVENTS
slouken@1951
    91
            printf("EnterNotify! (%d,%d)\n", xevent.xcrossing.x,
slouken@1951
    92
                   xevent.xcrossing.y);
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@1951
    98
            if ((xevent.xcrossing.mode != NotifyGrab) &&
slouken@1951
    99
                (xevent.xcrossing.mode != NotifyUngrab)) {
slouken@1951
   100
                SDL_SetMouseFocus(videodata->mouse, data->windowID);
slouken@1951
   101
                SDL_SendMouseMotion(videodata->mouse, 0, xevent.xcrossing.x,
slouken@1951
   102
                                    xevent.xcrossing.y);
slouken@1951
   103
            }
slouken@1951
   104
        }
slouken@1951
   105
        break;
slouken@1951
   106
slouken@1951
   107
        /* Losing mouse coverage? */
slouken@1951
   108
    case LeaveNotify:{
slouken@1951
   109
#ifdef DEBUG_XEVENTS
slouken@1951
   110
            printf("LeaveNotify! (%d,%d)\n", xevent.xcrossing.x,
slouken@1951
   111
                   xevent.xcrossing.y);
slouken@1951
   112
            if (xevent.xcrossing.mode == NotifyGrab)
slouken@1951
   113
                printf("Mode: NotifyGrab\n");
slouken@1951
   114
            if (xevent.xcrossing.mode == NotifyUngrab)
slouken@1951
   115
                printf("Mode: NotifyUngrab\n");
slouken@1951
   116
#endif
slouken@1951
   117
            if ((xevent.xcrossing.mode != NotifyGrab) &&
slouken@1951
   118
                (xevent.xcrossing.mode != NotifyUngrab) &&
slouken@1951
   119
                (xevent.xcrossing.detail != NotifyInferior)) {
slouken@1951
   120
                SDL_SendMouseMotion(videodata->mouse, 0,
slouken@1951
   121
                                    xevent.xcrossing.x, xevent.xcrossing.y);
slouken@1951
   122
                SDL_SetMouseFocus(videodata->mouse, 0);
slouken@1951
   123
            }
slouken@1951
   124
        }
slouken@1951
   125
        break;
slouken@1951
   126
slouken@1951
   127
        /* Gaining input focus? */
slouken@1951
   128
    case FocusIn:{
slouken@1951
   129
#ifdef DEBUG_XEVENTS
slouken@1951
   130
            printf("FocusIn!\n");
slouken@1951
   131
#endif
slouken@1951
   132
            SDL_SetKeyboardFocus(videodata->keyboard, data->windowID);
slouken@1951
   133
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   134
            if (data->ic) {
slouken@1951
   135
                XSetICFocus(data->ic);
slouken@1951
   136
            }
slouken@1951
   137
#endif
slouken@1951
   138
        }
slouken@1951
   139
        break;
slouken@1951
   140
slouken@1951
   141
        /* Losing input focus? */
slouken@1951
   142
    case FocusOut:{
slouken@1951
   143
#ifdef DEBUG_XEVENTS
slouken@1951
   144
            printf("FocusOut!\n");
slouken@1951
   145
#endif
slouken@1951
   146
            SDL_SetKeyboardFocus(videodata->keyboard, 0);
slouken@1951
   147
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   148
            if (data->ic) {
slouken@1951
   149
                XUnsetICFocus(data->ic);
slouken@1951
   150
            }
slouken@1951
   151
#endif
slouken@1951
   152
        }
slouken@1951
   153
        break;
slouken@1951
   154
slouken@1951
   155
        /* Generated upon EnterWindow and FocusIn */
slouken@1951
   156
    case KeymapNotify:{
slouken@1951
   157
#ifdef DEBUG_XEVENTS
slouken@1951
   158
            printf("KeymapNotify!\n");
slouken@1951
   159
#endif
slouken@1951
   160
            /* FIXME:
slouken@1951
   161
               X11_SetKeyboardState(SDL_Display, xevent.xkeymap.key_vector);
slouken@1951
   162
             */
slouken@1951
   163
        }
slouken@1951
   164
        break;
slouken@1951
   165
slouken@1951
   166
        /* Mouse motion? */
slouken@1951
   167
    case MotionNotify:{
slouken@1951
   168
#ifdef DEBUG_MOTION
slouken@1951
   169
            printf("X11 motion: %d,%d\n", xevent.xmotion.x, xevent.xmotion.y);
slouken@1951
   170
#endif
slouken@1951
   171
            SDL_SendMouseMotion(videodata->mouse, 0, xevent.xmotion.x,
slouken@1951
   172
                                xevent.xmotion.y);
slouken@1951
   173
        }
slouken@1951
   174
        break;
slouken@1951
   175
slouken@1951
   176
        /* Mouse button press? */
slouken@1951
   177
    case ButtonPress:{
slouken@1951
   178
            SDL_SendMouseButton(videodata->mouse, SDL_PRESSED,
slouken@1951
   179
                                xevent.xbutton.button);
slouken@1951
   180
        }
slouken@1951
   181
        break;
slouken@1951
   182
slouken@1951
   183
        /* Mouse button release? */
slouken@1951
   184
    case ButtonRelease:{
slouken@1951
   185
            SDL_SendMouseButton(videodata->mouse, SDL_RELEASED,
slouken@1951
   186
                                xevent.xbutton.button);
slouken@1951
   187
        }
slouken@1951
   188
        break;
slouken@1951
   189
slouken@1951
   190
        /* Key press? */
slouken@1951
   191
    case KeyPress:{
slouken@1951
   192
            KeyCode keycode = xevent.xkey.keycode;
slouken@1951
   193
slouken@1951
   194
#ifdef DEBUG_XEVENTS
slouken@1951
   195
            printf("KeyPress (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
slouken@1951
   196
#endif
bob@2295
   197
            if (!X11_KeyRepeat(videodata->display, &xevent)) {
bob@2295
   198
                SDLKey physicalKey = videodata->keyCodeToSDLKTable[keycode];
bob@2295
   199
                SDL_SendKeyboardKey(videodata->keyboard, SDL_PRESSED,
bob@2295
   200
                                    (Uint8) keycode, physicalKey);
bob@2295
   201
#if 1
bob@2295
   202
                if (physicalKey == SDLK_UNKNOWN) {
bob@2295
   203
                    fprintf(stderr,
bob@2295
   204
                            "The key you just pressed is not recognized by SDL. To help get this fixed, report this to the SDL mailing list <sdl@libsdl.org> or to Christian Walther <cwalther@gmx.ch>. X11 KeyCode is %d, X11 KeySym 0x%X.\n",
bob@2295
   205
                            (int) keycode,
bob@2295
   206
                            (unsigned int) XKeycodeToKeysym(videodata->
bob@2295
   207
                                                            display, keycode,
bob@2295
   208
                                                            0));
bob@2295
   209
                }
bob@2295
   210
#endif
slouken@1951
   211
            }
slouken@1951
   212
        }
slouken@1951
   213
        break;
slouken@1951
   214
slouken@1951
   215
        /* Key release? */
slouken@1951
   216
    case KeyRelease:{
slouken@1951
   217
            KeyCode keycode = xevent.xkey.keycode;
slouken@1951
   218
slouken@1951
   219
#ifdef DEBUG_XEVENTS
slouken@1951
   220
            printf("KeyRelease (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
slouken@1951
   221
#endif
slouken@1951
   222
            /* Check to see if this is a repeated key */
bob@2295
   223
            if (X11_KeyRepeat(videodata->display, &xevent)) {
slouken@1951
   224
                break;
slouken@1951
   225
            }
slouken@1951
   226
bob@2295
   227
            SDL_SendKeyboardKey(videodata->keyboard, SDL_RELEASED,
bob@2295
   228
                                (Uint8) keycode,
bob@2295
   229
                                videodata->keyCodeToSDLKTable[keycode]);
slouken@1951
   230
        }
slouken@1951
   231
        break;
slouken@1951
   232
slouken@1951
   233
        /* Have we been iconified? */
slouken@1951
   234
    case UnmapNotify:{
slouken@1951
   235
#ifdef DEBUG_XEVENTS
slouken@1951
   236
            printf("UnmapNotify!\n");
slouken@1951
   237
#endif
slouken@1951
   238
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@1951
   239
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_MINIMIZED, 0,
slouken@1951
   240
                                0);
slouken@1951
   241
        }
slouken@1951
   242
        break;
slouken@1951
   243
slouken@1951
   244
        /* Have we been restored? */
slouken@1951
   245
    case MapNotify:{
slouken@1951
   246
#ifdef DEBUG_XEVENTS
slouken@1951
   247
            printf("MapNotify!\n");
slouken@1951
   248
#endif
slouken@1951
   249
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@1951
   250
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_RESTORED, 0,
slouken@1951
   251
                                0);
slouken@1951
   252
        }
slouken@1951
   253
        break;
slouken@1951
   254
slouken@1951
   255
        /* Have we been resized or moved? */
slouken@1951
   256
    case ConfigureNotify:{
slouken@1951
   257
#ifdef DEBUG_XEVENTS
slouken@1951
   258
            printf("ConfigureNotify! (resize: %dx%d)\n",
slouken@1951
   259
                   xevent.xconfigure.width, xevent.xconfigure.height);
slouken@1951
   260
#endif
slouken@1951
   261
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_MOVED,
slouken@1951
   262
                                xevent.xconfigure.x, xevent.xconfigure.y);
slouken@1951
   263
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_RESIZED,
slouken@1951
   264
                                xevent.xconfigure.width,
slouken@1951
   265
                                xevent.xconfigure.height);
slouken@1951
   266
        }
slouken@1951
   267
        break;
slouken@1951
   268
slouken@1951
   269
        /* Have we been requested to quit (or another client message?) */
slouken@1951
   270
    case ClientMessage:{
slouken@1951
   271
            if ((xevent.xclient.format == 32) &&
slouken@1951
   272
                (xevent.xclient.data.l[0] == videodata->WM_DELETE_WINDOW)) {
slouken@1951
   273
slouken@1951
   274
                SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_CLOSE, 0,
slouken@1951
   275
                                    0);
slouken@1951
   276
            }
slouken@1951
   277
        }
slouken@1951
   278
        break;
slouken@1951
   279
slouken@1951
   280
        /* Do we need to refresh ourselves? */
slouken@1951
   281
    case Expose:{
slouken@1951
   282
#ifdef DEBUG_XEVENTS
slouken@1951
   283
            printf("Expose (count = %d)\n", xevent.xexpose.count);
slouken@1951
   284
#endif
slouken@1951
   285
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_EXPOSED, 0,
slouken@1951
   286
                                0);
slouken@1951
   287
        }
slouken@1951
   288
        break;
slouken@1951
   289
slouken@1951
   290
    default:{
slouken@1951
   291
#ifdef DEBUG_XEVENTS
slouken@1951
   292
            printf("Unhandled event %d\n", xevent.type);
slouken@1951
   293
#endif
slouken@1951
   294
        }
slouken@1951
   295
        break;
slouken@1951
   296
    }
slouken@1951
   297
}
slouken@1951
   298
slouken@1951
   299
/* Ack!  XPending() actually performs a blocking read if no events available */
slouken@1951
   300
int
slouken@1951
   301
X11_Pending(Display * display)
slouken@1951
   302
{
slouken@1951
   303
    /* Flush the display connection and look to see if events are queued */
slouken@1951
   304
    XFlush(display);
slouken@1951
   305
    if (XEventsQueued(display, QueuedAlready)) {
slouken@1951
   306
        return (1);
slouken@1951
   307
    }
slouken@1951
   308
slouken@1951
   309
    /* More drastic measures are required -- see if X is ready to talk */
slouken@1951
   310
    {
slouken@1951
   311
        static struct timeval zero_time;        /* static == 0 */
slouken@1951
   312
        int x11_fd;
slouken@1951
   313
        fd_set fdset;
slouken@1951
   314
slouken@1951
   315
        x11_fd = ConnectionNumber(display);
slouken@1951
   316
        FD_ZERO(&fdset);
slouken@1951
   317
        FD_SET(x11_fd, &fdset);
slouken@1951
   318
        if (select(x11_fd + 1, &fdset, NULL, NULL, &zero_time) == 1) {
slouken@1951
   319
            return (XPending(display));
slouken@1951
   320
        }
slouken@1951
   321
    }
slouken@1951
   322
slouken@1951
   323
    /* Oh well, nothing is ready .. */
slouken@1951
   324
    return (0);
slouken@1951
   325
}
slouken@1951
   326
slouken@1951
   327
void
slouken@1951
   328
X11_PumpEvents(_THIS)
slouken@1951
   329
{
slouken@1951
   330
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   331
slouken@1951
   332
    /* Keep processing pending events */
slouken@1951
   333
    while (X11_Pending(data->display)) {
slouken@1951
   334
        X11_DispatchEvent(_this);
slouken@1951
   335
    }
slouken@1951
   336
}
slouken@1951
   337
slouken@1951
   338
void
slouken@1951
   339
X11_SaveScreenSaver(Display * display, int *saved_timeout, BOOL * dpms)
slouken@1951
   340
{
slouken@1951
   341
    int timeout, interval, prefer_blank, allow_exp;
slouken@1951
   342
    XGetScreenSaver(display, &timeout, &interval, &prefer_blank, &allow_exp);
slouken@1951
   343
    *saved_timeout = timeout;
slouken@1951
   344
slouken@1951
   345
#if SDL_VIDEO_DRIVER_X11_DPMS
slouken@1951
   346
    if (SDL_X11_HAVE_DPMS) {
slouken@1951
   347
        int dummy;
slouken@1951
   348
        if (DPMSQueryExtension(display, &dummy, &dummy)) {
slouken@1951
   349
            CARD16 state;
slouken@1951
   350
            DPMSInfo(display, &state, dpms);
slouken@1951
   351
        }
slouken@1951
   352
    }
slouken@1951
   353
#else
slouken@1951
   354
    *dpms = 0;
slouken@1951
   355
#endif /* SDL_VIDEO_DRIVER_X11_DPMS */
slouken@1951
   356
}
slouken@1951
   357
slouken@1951
   358
void
slouken@1951
   359
X11_DisableScreenSaver(Display * display)
slouken@1951
   360
{
slouken@1951
   361
    int timeout, interval, prefer_blank, allow_exp;
slouken@1951
   362
    XGetScreenSaver(display, &timeout, &interval, &prefer_blank, &allow_exp);
slouken@1951
   363
    timeout = 0;
slouken@1951
   364
    XSetScreenSaver(display, timeout, interval, prefer_blank, allow_exp);
slouken@1951
   365
slouken@1951
   366
#if SDL_VIDEO_DRIVER_X11_DPMS
slouken@1951
   367
    if (SDL_X11_HAVE_DPMS) {
slouken@1951
   368
        int dummy;
slouken@1951
   369
        if (DPMSQueryExtension(display, &dummy, &dummy)) {
slouken@1951
   370
            DPMSDisable(display);
slouken@1951
   371
        }
slouken@1951
   372
    }
slouken@1951
   373
#endif /* SDL_VIDEO_DRIVER_X11_DPMS */
slouken@1951
   374
}
slouken@1951
   375
slouken@1951
   376
void
slouken@1951
   377
X11_RestoreScreenSaver(Display * display, int saved_timeout, BOOL dpms)
slouken@1951
   378
{
slouken@1951
   379
    int timeout, interval, prefer_blank, allow_exp;
slouken@1951
   380
    XGetScreenSaver(display, &timeout, &interval, &prefer_blank, &allow_exp);
slouken@1951
   381
    timeout = saved_timeout;
slouken@1951
   382
    XSetScreenSaver(display, timeout, interval, prefer_blank, allow_exp);
slouken@1951
   383
slouken@1951
   384
#if SDL_VIDEO_DRIVER_X11_DPMS
slouken@1951
   385
    if (SDL_X11_HAVE_DPMS) {
slouken@1951
   386
        int dummy;
slouken@1951
   387
        if (DPMSQueryExtension(display, &dummy, &dummy)) {
slouken@1951
   388
            if (dpms) {
slouken@1951
   389
                DPMSEnable(display);
slouken@1951
   390
            }
slouken@1951
   391
        }
slouken@1951
   392
    }
slouken@1951
   393
#endif /* SDL_VIDEO_DRIVER_X11_DPMS */
slouken@1951
   394
}
slouken@1951
   395
slouken@1951
   396
/* vi: set ts=4 sw=4 expandtab: */