src/video/x11/SDL_x11events.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 01 Oct 2006 02:28:41 +0000
changeset 2046 da8332c8f480
parent 1951 7177581dc9fa
child 2295 dbc6d1893869
permissions -rw-r--r--
Replaced strncmp for SDL_VIDEODRIVER test with strcasecmp
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
slouken@1951
    33
static void
slouken@1951
    34
X11_DispatchEvent(_THIS)
slouken@1951
    35
{
slouken@1951
    36
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1951
    37
    SDL_WindowData *data;
slouken@1951
    38
    XEvent xevent;
slouken@1951
    39
    int i;
slouken@1951
    40
slouken@1951
    41
    SDL_zero(xevent);           /* valgrind fix. --ryan. */
slouken@1951
    42
    XNextEvent(videodata->display, &xevent);
slouken@1951
    43
slouken@1951
    44
    /* Send a SDL_SYSWMEVENT if the application wants them */
slouken@1951
    45
    if (SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE) {
slouken@1951
    46
        SDL_SysWMmsg wmmsg;
slouken@1951
    47
slouken@1951
    48
        SDL_VERSION(&wmmsg.version);
slouken@1951
    49
        wmmsg.subsystem = SDL_SYSWM_X11;
slouken@1951
    50
        wmmsg.event.xevent = xevent;
slouken@1951
    51
        SDL_SendSysWMEvent(&wmmsg);
slouken@1951
    52
    }
slouken@1951
    53
slouken@1951
    54
    data = NULL;
slouken@1951
    55
    for (i = 0; i < videodata->numwindows; ++i) {
slouken@1951
    56
        if (videodata->windowlist[i]->window == xevent.xany.window) {
slouken@1951
    57
            data = videodata->windowlist[i];
slouken@1951
    58
        }
slouken@1951
    59
    }
slouken@1951
    60
    if (!data) {
slouken@1951
    61
        return;
slouken@1951
    62
    }
slouken@1951
    63
slouken@1951
    64
    switch (xevent.type) {
slouken@1951
    65
slouken@1951
    66
        /* Gaining mouse coverage? */
slouken@1951
    67
    case EnterNotify:{
slouken@1951
    68
#ifdef DEBUG_XEVENTS
slouken@1951
    69
            printf("EnterNotify! (%d,%d)\n", xevent.xcrossing.x,
slouken@1951
    70
                   xevent.xcrossing.y);
slouken@1951
    71
            if (xevent.xcrossing.mode == NotifyGrab)
slouken@1951
    72
                printf("Mode: NotifyGrab\n");
slouken@1951
    73
            if (xevent.xcrossing.mode == NotifyUngrab)
slouken@1951
    74
                printf("Mode: NotifyUngrab\n");
slouken@1951
    75
#endif
slouken@1951
    76
            if ((xevent.xcrossing.mode != NotifyGrab) &&
slouken@1951
    77
                (xevent.xcrossing.mode != NotifyUngrab)) {
slouken@1951
    78
                SDL_SetMouseFocus(videodata->mouse, data->windowID);
slouken@1951
    79
                SDL_SendMouseMotion(videodata->mouse, 0, xevent.xcrossing.x,
slouken@1951
    80
                                    xevent.xcrossing.y);
slouken@1951
    81
            }
slouken@1951
    82
        }
slouken@1951
    83
        break;
slouken@1951
    84
slouken@1951
    85
        /* Losing mouse coverage? */
slouken@1951
    86
    case LeaveNotify:{
slouken@1951
    87
#ifdef DEBUG_XEVENTS
slouken@1951
    88
            printf("LeaveNotify! (%d,%d)\n", xevent.xcrossing.x,
slouken@1951
    89
                   xevent.xcrossing.y);
slouken@1951
    90
            if (xevent.xcrossing.mode == NotifyGrab)
slouken@1951
    91
                printf("Mode: NotifyGrab\n");
slouken@1951
    92
            if (xevent.xcrossing.mode == NotifyUngrab)
slouken@1951
    93
                printf("Mode: NotifyUngrab\n");
slouken@1951
    94
#endif
slouken@1951
    95
            if ((xevent.xcrossing.mode != NotifyGrab) &&
slouken@1951
    96
                (xevent.xcrossing.mode != NotifyUngrab) &&
slouken@1951
    97
                (xevent.xcrossing.detail != NotifyInferior)) {
slouken@1951
    98
                SDL_SendMouseMotion(videodata->mouse, 0,
slouken@1951
    99
                                    xevent.xcrossing.x, xevent.xcrossing.y);
slouken@1951
   100
                SDL_SetMouseFocus(videodata->mouse, 0);
slouken@1951
   101
            }
slouken@1951
   102
        }
slouken@1951
   103
        break;
slouken@1951
   104
slouken@1951
   105
        /* Gaining input focus? */
slouken@1951
   106
    case FocusIn:{
slouken@1951
   107
#ifdef DEBUG_XEVENTS
slouken@1951
   108
            printf("FocusIn!\n");
slouken@1951
   109
#endif
slouken@1951
   110
            SDL_SetKeyboardFocus(videodata->keyboard, data->windowID);
slouken@1951
   111
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   112
            if (data->ic) {
slouken@1951
   113
                XSetICFocus(data->ic);
slouken@1951
   114
            }
slouken@1951
   115
#endif
slouken@1951
   116
        }
slouken@1951
   117
        break;
slouken@1951
   118
slouken@1951
   119
        /* Losing input focus? */
slouken@1951
   120
    case FocusOut:{
slouken@1951
   121
#ifdef DEBUG_XEVENTS
slouken@1951
   122
            printf("FocusOut!\n");
slouken@1951
   123
#endif
slouken@1951
   124
            SDL_SetKeyboardFocus(videodata->keyboard, 0);
slouken@1951
   125
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   126
            if (data->ic) {
slouken@1951
   127
                XUnsetICFocus(data->ic);
slouken@1951
   128
            }
slouken@1951
   129
#endif
slouken@1951
   130
        }
slouken@1951
   131
        break;
slouken@1951
   132
slouken@1951
   133
        /* Generated upon EnterWindow and FocusIn */
slouken@1951
   134
    case KeymapNotify:{
slouken@1951
   135
#ifdef DEBUG_XEVENTS
slouken@1951
   136
            printf("KeymapNotify!\n");
slouken@1951
   137
#endif
slouken@1951
   138
            /* FIXME:
slouken@1951
   139
               X11_SetKeyboardState(SDL_Display, xevent.xkeymap.key_vector);
slouken@1951
   140
             */
slouken@1951
   141
        }
slouken@1951
   142
        break;
slouken@1951
   143
slouken@1951
   144
        /* Mouse motion? */
slouken@1951
   145
    case MotionNotify:{
slouken@1951
   146
#ifdef DEBUG_MOTION
slouken@1951
   147
            printf("X11 motion: %d,%d\n", xevent.xmotion.x, xevent.xmotion.y);
slouken@1951
   148
#endif
slouken@1951
   149
            SDL_SendMouseMotion(videodata->mouse, 0, xevent.xmotion.x,
slouken@1951
   150
                                xevent.xmotion.y);
slouken@1951
   151
        }
slouken@1951
   152
        break;
slouken@1951
   153
slouken@1951
   154
        /* Mouse button press? */
slouken@1951
   155
    case ButtonPress:{
slouken@1951
   156
            SDL_SendMouseButton(videodata->mouse, SDL_PRESSED,
slouken@1951
   157
                                xevent.xbutton.button);
slouken@1951
   158
        }
slouken@1951
   159
        break;
slouken@1951
   160
slouken@1951
   161
        /* Mouse button release? */
slouken@1951
   162
    case ButtonRelease:{
slouken@1951
   163
            SDL_SendMouseButton(videodata->mouse, SDL_RELEASED,
slouken@1951
   164
                                xevent.xbutton.button);
slouken@1951
   165
        }
slouken@1951
   166
        break;
slouken@1951
   167
slouken@1951
   168
        /* Key press? */
slouken@1951
   169
    case KeyPress:{
slouken@1951
   170
#if 0                           /* FIXME */
slouken@1951
   171
            static SDL_keysym saved_keysym;
slouken@1951
   172
            SDL_keysym keysym;
slouken@1951
   173
            KeyCode keycode = xevent.xkey.keycode;
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@1951
   178
            /* Get the translated SDL virtual keysym */
slouken@1951
   179
            if (keycode) {
slouken@1951
   180
                keysym.scancode = keycode;
slouken@1951
   181
                keysym.sym = X11_TranslateKeycode(SDL_Display, keycode);
slouken@1951
   182
                keysym.mod = KMOD_NONE;
slouken@1951
   183
                keysym.unicode = 0;
slouken@1951
   184
            } else {
slouken@1951
   185
                keysym = saved_keysym;
slouken@1951
   186
            }
slouken@1951
   187
slouken@1951
   188
            /* If we're not doing translation, we're done! */
slouken@1951
   189
            if (!SDL_TranslateUNICODE) {
slouken@1951
   190
                posted = SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
slouken@1951
   191
                break;
slouken@1951
   192
            }
slouken@1951
   193
slouken@1951
   194
            if (XFilterEvent(&xevent, None)) {
slouken@1951
   195
                if (xevent.xkey.keycode) {
slouken@1951
   196
                    posted = SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
slouken@1951
   197
                } else {
slouken@1951
   198
                    /* Save event to be associated with IM text
slouken@1951
   199
                       In 1.3 we'll have a text event instead.. */
slouken@1951
   200
                    saved_keysym = keysym;
slouken@1951
   201
                }
slouken@1951
   202
                break;
slouken@1951
   203
            }
slouken@1951
   204
slouken@1951
   205
            /* Look up the translated value for the key event */
slouken@1951
   206
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   207
            if (data->ic != NULL) {
slouken@1951
   208
                static Status state;
slouken@1951
   209
                /* A UTF-8 character can be at most 6 bytes */
slouken@1951
   210
                char keybuf[6];
slouken@1951
   211
                if (Xutf8LookupString(data->ic, &xevent.xkey,
slouken@1951
   212
                                      keybuf, sizeof(keybuf), NULL, &state)) {
slouken@1951
   213
                    keysym.unicode = Utf8ToUcs4((Uint8 *) keybuf);
slouken@1951
   214
                }
slouken@1951
   215
            } else
slouken@1951
   216
#endif
slouken@1951
   217
            {
slouken@1951
   218
                static XComposeStatus state;
slouken@1951
   219
                char keybuf[32];
slouken@1951
   220
slouken@1951
   221
                if (XLookupString(&xevent.xkey,
slouken@1951
   222
                                  keybuf, sizeof(keybuf), NULL, &state)) {
slouken@1951
   223
                    /*
slouken@1951
   224
                     * FIXME: XLookupString() may yield more than one
slouken@1951
   225
                     * character, so we need a mechanism to allow for
slouken@1951
   226
                     * this (perhaps null keypress events with a
slouken@1951
   227
                     * unicode value)
slouken@1951
   228
                     */
slouken@1951
   229
                    keysym.unicode = (Uint8) keybuf[0];
slouken@1951
   230
                }
slouken@1951
   231
            }
slouken@1951
   232
            posted = SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
slouken@1951
   233
#endif // 0
slouken@1951
   234
        }
slouken@1951
   235
        break;
slouken@1951
   236
slouken@1951
   237
        /* Key release? */
slouken@1951
   238
    case KeyRelease:{
slouken@1951
   239
#if 0                           /* FIXME */
slouken@1951
   240
            SDL_keysym keysym;
slouken@1951
   241
            KeyCode keycode = xevent.xkey.keycode;
slouken@1951
   242
slouken@1951
   243
#ifdef DEBUG_XEVENTS
slouken@1951
   244
            printf("KeyRelease (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
slouken@1951
   245
#endif
slouken@1951
   246
            /* Check to see if this is a repeated key */
slouken@1951
   247
            if (X11_KeyRepeat(SDL_Display, &xevent)) {
slouken@1951
   248
                break;
slouken@1951
   249
            }
slouken@1951
   250
slouken@1951
   251
            /* Get the translated SDL virtual keysym */
slouken@1951
   252
            keysym.scancode = keycode;
slouken@1951
   253
            keysym.sym = X11_TranslateKeycode(SDL_Display, keycode);
slouken@1951
   254
            keysym.mod = KMOD_NONE;
slouken@1951
   255
            keysym.unicode = 0;
slouken@1951
   256
slouken@1951
   257
            posted = SDL_PrivateKeyboard(SDL_RELEASED, &keysym);
slouken@1951
   258
#endif // 0
slouken@1951
   259
        }
slouken@1951
   260
        break;
slouken@1951
   261
slouken@1951
   262
        /* Have we been iconified? */
slouken@1951
   263
    case UnmapNotify:{
slouken@1951
   264
#ifdef DEBUG_XEVENTS
slouken@1951
   265
            printf("UnmapNotify!\n");
slouken@1951
   266
#endif
slouken@1951
   267
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@1951
   268
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_MINIMIZED, 0,
slouken@1951
   269
                                0);
slouken@1951
   270
        }
slouken@1951
   271
        break;
slouken@1951
   272
slouken@1951
   273
        /* Have we been restored? */
slouken@1951
   274
    case MapNotify:{
slouken@1951
   275
#ifdef DEBUG_XEVENTS
slouken@1951
   276
            printf("MapNotify!\n");
slouken@1951
   277
#endif
slouken@1951
   278
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@1951
   279
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_RESTORED, 0,
slouken@1951
   280
                                0);
slouken@1951
   281
        }
slouken@1951
   282
        break;
slouken@1951
   283
slouken@1951
   284
        /* Have we been resized or moved? */
slouken@1951
   285
    case ConfigureNotify:{
slouken@1951
   286
#ifdef DEBUG_XEVENTS
slouken@1951
   287
            printf("ConfigureNotify! (resize: %dx%d)\n",
slouken@1951
   288
                   xevent.xconfigure.width, xevent.xconfigure.height);
slouken@1951
   289
#endif
slouken@1951
   290
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_MOVED,
slouken@1951
   291
                                xevent.xconfigure.x, xevent.xconfigure.y);
slouken@1951
   292
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_RESIZED,
slouken@1951
   293
                                xevent.xconfigure.width,
slouken@1951
   294
                                xevent.xconfigure.height);
slouken@1951
   295
        }
slouken@1951
   296
        break;
slouken@1951
   297
slouken@1951
   298
        /* Have we been requested to quit (or another client message?) */
slouken@1951
   299
    case ClientMessage:{
slouken@1951
   300
            if ((xevent.xclient.format == 32) &&
slouken@1951
   301
                (xevent.xclient.data.l[0] == videodata->WM_DELETE_WINDOW)) {
slouken@1951
   302
slouken@1951
   303
                SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_CLOSE, 0,
slouken@1951
   304
                                    0);
slouken@1951
   305
            }
slouken@1951
   306
        }
slouken@1951
   307
        break;
slouken@1951
   308
slouken@1951
   309
        /* Do we need to refresh ourselves? */
slouken@1951
   310
    case Expose:{
slouken@1951
   311
#ifdef DEBUG_XEVENTS
slouken@1951
   312
            printf("Expose (count = %d)\n", xevent.xexpose.count);
slouken@1951
   313
#endif
slouken@1951
   314
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_EXPOSED, 0,
slouken@1951
   315
                                0);
slouken@1951
   316
        }
slouken@1951
   317
        break;
slouken@1951
   318
slouken@1951
   319
    default:{
slouken@1951
   320
#ifdef DEBUG_XEVENTS
slouken@1951
   321
            printf("Unhandled event %d\n", xevent.type);
slouken@1951
   322
#endif
slouken@1951
   323
        }
slouken@1951
   324
        break;
slouken@1951
   325
    }
slouken@1951
   326
}
slouken@1951
   327
slouken@1951
   328
/* Ack!  XPending() actually performs a blocking read if no events available */
slouken@1951
   329
int
slouken@1951
   330
X11_Pending(Display * display)
slouken@1951
   331
{
slouken@1951
   332
    /* Flush the display connection and look to see if events are queued */
slouken@1951
   333
    XFlush(display);
slouken@1951
   334
    if (XEventsQueued(display, QueuedAlready)) {
slouken@1951
   335
        return (1);
slouken@1951
   336
    }
slouken@1951
   337
slouken@1951
   338
    /* More drastic measures are required -- see if X is ready to talk */
slouken@1951
   339
    {
slouken@1951
   340
        static struct timeval zero_time;        /* static == 0 */
slouken@1951
   341
        int x11_fd;
slouken@1951
   342
        fd_set fdset;
slouken@1951
   343
slouken@1951
   344
        x11_fd = ConnectionNumber(display);
slouken@1951
   345
        FD_ZERO(&fdset);
slouken@1951
   346
        FD_SET(x11_fd, &fdset);
slouken@1951
   347
        if (select(x11_fd + 1, &fdset, NULL, NULL, &zero_time) == 1) {
slouken@1951
   348
            return (XPending(display));
slouken@1951
   349
        }
slouken@1951
   350
    }
slouken@1951
   351
slouken@1951
   352
    /* Oh well, nothing is ready .. */
slouken@1951
   353
    return (0);
slouken@1951
   354
}
slouken@1951
   355
slouken@1951
   356
void
slouken@1951
   357
X11_PumpEvents(_THIS)
slouken@1951
   358
{
slouken@1951
   359
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   360
slouken@1951
   361
    /* Keep processing pending events */
slouken@1951
   362
    while (X11_Pending(data->display)) {
slouken@1951
   363
        X11_DispatchEvent(_this);
slouken@1951
   364
    }
slouken@1951
   365
}
slouken@1951
   366
slouken@1951
   367
void
slouken@1951
   368
X11_SaveScreenSaver(Display * display, int *saved_timeout, BOOL * dpms)
slouken@1951
   369
{
slouken@1951
   370
    int timeout, interval, prefer_blank, allow_exp;
slouken@1951
   371
    XGetScreenSaver(display, &timeout, &interval, &prefer_blank, &allow_exp);
slouken@1951
   372
    *saved_timeout = timeout;
slouken@1951
   373
slouken@1951
   374
#if SDL_VIDEO_DRIVER_X11_DPMS
slouken@1951
   375
    if (SDL_X11_HAVE_DPMS) {
slouken@1951
   376
        int dummy;
slouken@1951
   377
        if (DPMSQueryExtension(display, &dummy, &dummy)) {
slouken@1951
   378
            CARD16 state;
slouken@1951
   379
            DPMSInfo(display, &state, dpms);
slouken@1951
   380
        }
slouken@1951
   381
    }
slouken@1951
   382
#else
slouken@1951
   383
    *dpms = 0;
slouken@1951
   384
#endif /* SDL_VIDEO_DRIVER_X11_DPMS */
slouken@1951
   385
}
slouken@1951
   386
slouken@1951
   387
void
slouken@1951
   388
X11_DisableScreenSaver(Display * display)
slouken@1951
   389
{
slouken@1951
   390
    int timeout, interval, prefer_blank, allow_exp;
slouken@1951
   391
    XGetScreenSaver(display, &timeout, &interval, &prefer_blank, &allow_exp);
slouken@1951
   392
    timeout = 0;
slouken@1951
   393
    XSetScreenSaver(display, timeout, interval, prefer_blank, allow_exp);
slouken@1951
   394
slouken@1951
   395
#if SDL_VIDEO_DRIVER_X11_DPMS
slouken@1951
   396
    if (SDL_X11_HAVE_DPMS) {
slouken@1951
   397
        int dummy;
slouken@1951
   398
        if (DPMSQueryExtension(display, &dummy, &dummy)) {
slouken@1951
   399
            DPMSDisable(display);
slouken@1951
   400
        }
slouken@1951
   401
    }
slouken@1951
   402
#endif /* SDL_VIDEO_DRIVER_X11_DPMS */
slouken@1951
   403
}
slouken@1951
   404
slouken@1951
   405
void
slouken@1951
   406
X11_RestoreScreenSaver(Display * display, int saved_timeout, BOOL dpms)
slouken@1951
   407
{
slouken@1951
   408
    int timeout, interval, prefer_blank, allow_exp;
slouken@1951
   409
    XGetScreenSaver(display, &timeout, &interval, &prefer_blank, &allow_exp);
slouken@1951
   410
    timeout = saved_timeout;
slouken@1951
   411
    XSetScreenSaver(display, timeout, interval, prefer_blank, allow_exp);
slouken@1951
   412
slouken@1951
   413
#if SDL_VIDEO_DRIVER_X11_DPMS
slouken@1951
   414
    if (SDL_X11_HAVE_DPMS) {
slouken@1951
   415
        int dummy;
slouken@1951
   416
        if (DPMSQueryExtension(display, &dummy, &dummy)) {
slouken@1951
   417
            if (dpms) {
slouken@1951
   418
                DPMSEnable(display);
slouken@1951
   419
            }
slouken@1951
   420
        }
slouken@1951
   421
    }
slouken@1951
   422
#endif /* SDL_VIDEO_DRIVER_X11_DPMS */
slouken@1951
   423
}
slouken@1951
   424
slouken@1951
   425
/* vi: set ts=4 sw=4 expandtab: */