src/video/x11/SDL_x11events.c
author Bob Pendleton <bob@pendleton.com>
Fri, 07 Mar 2008 20:54:11 +0000
changeset 2325 c7bcf84ba1b9
parent 2324 3202e4826c57
child 2326 133562468ff2
permissions -rw-r--r--
Next version of internationalized input for X11. On my machine (famous last words :-) with a US English keyboard and locale I can compose ` and e and get a text
input event with the character รจ. You still get the keypress keyrelease events for the individual keys that go into composing the character.
bob@2325
     1
#define DEBUG_XEVENTS
slouken@1951
     2
/*
slouken@1951
     3
    SDL - Simple DirectMedia Layer
slouken@1951
     4
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1951
     5
slouken@1951
     6
    This library is free software; you can redistribute it and/or
slouken@1951
     7
    modify it under the terms of the GNU Lesser General Public
slouken@1951
     8
    License as published by the Free Software Foundation; either
slouken@1951
     9
    version 2.1 of the License, or (at your option) any later version.
slouken@1951
    10
slouken@1951
    11
    This library is distributed in the hope that it will be useful,
slouken@1951
    12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1951
    13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1951
    14
    Lesser General Public License for more details.
slouken@1951
    15
slouken@1951
    16
    You should have received a copy of the GNU Lesser General Public
slouken@1951
    17
    License along with this library; if not, write to the Free Software
slouken@1951
    18
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1951
    19
slouken@1951
    20
    Sam Lantinga
slouken@1951
    21
    slouken@libsdl.org
slouken@1951
    22
*/
slouken@1951
    23
#include "SDL_config.h"
slouken@1951
    24
slouken@2046
    25
#include <sys/types.h>
slouken@2046
    26
#include <sys/time.h>
slouken@2046
    27
#include <unistd.h>
slouken@2046
    28
slouken@1951
    29
#include "SDL_syswm.h"
slouken@1951
    30
#include "SDL_x11video.h"
slouken@1951
    31
#include "../../events/SDL_events_c.h"
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
bob@2325
    44
    /* filter events catchs XIM events and sends them to the correct
bob@2325
    45
       handler */
bob@2325
    46
    if (XFilterEvent(&xevent, None) == True) {
bob@2325
    47
#ifdef DEBUG_XEVENTS
bob@2325
    48
        printf("Filtered event of type = 0x%X\n", xevent.type);
bob@2325
    49
#endif
bob@2325
    50
        return;
bob@2325
    51
    }
bob@2325
    52
slouken@1951
    53
    /* Send a SDL_SYSWMEVENT if the application wants them */
slouken@1951
    54
    if (SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE) {
slouken@1951
    55
        SDL_SysWMmsg wmmsg;
slouken@1951
    56
slouken@1951
    57
        SDL_VERSION(&wmmsg.version);
slouken@1951
    58
        wmmsg.subsystem = SDL_SYSWM_X11;
slouken@1951
    59
        wmmsg.event.xevent = xevent;
slouken@1951
    60
        SDL_SendSysWMEvent(&wmmsg);
slouken@1951
    61
    }
slouken@1951
    62
slouken@1951
    63
    data = NULL;
bob@2324
    64
    if (videodata && videodata->windowlist) {
bob@2324
    65
        for (i = 0; i < videodata->numwindows; ++i) {
bob@2324
    66
            if ((videodata->windowlist[i] != NULL) &&
bob@2324
    67
                (videodata->windowlist[i]->window == xevent.xany.window)) {
bob@2324
    68
                data = videodata->windowlist[i];
bob@2324
    69
                break;
bob@2324
    70
            }
slouken@1951
    71
        }
slouken@1951
    72
    }
slouken@1951
    73
    if (!data) {
slouken@1951
    74
        return;
slouken@1951
    75
    }
slouken@1951
    76
slouken@1951
    77
    switch (xevent.type) {
slouken@1951
    78
slouken@1951
    79
        /* Gaining mouse coverage? */
slouken@1951
    80
    case EnterNotify:{
slouken@1951
    81
#ifdef DEBUG_XEVENTS
slouken@1951
    82
            printf("EnterNotify! (%d,%d)\n", xevent.xcrossing.x,
slouken@1951
    83
                   xevent.xcrossing.y);
slouken@1951
    84
            if (xevent.xcrossing.mode == NotifyGrab)
slouken@1951
    85
                printf("Mode: NotifyGrab\n");
slouken@1951
    86
            if (xevent.xcrossing.mode == NotifyUngrab)
slouken@1951
    87
                printf("Mode: NotifyUngrab\n");
slouken@1951
    88
#endif
slouken@1951
    89
            if ((xevent.xcrossing.mode != NotifyGrab) &&
slouken@1951
    90
                (xevent.xcrossing.mode != NotifyUngrab)) {
slouken@1951
    91
                SDL_SetMouseFocus(videodata->mouse, data->windowID);
slouken@1951
    92
                SDL_SendMouseMotion(videodata->mouse, 0, xevent.xcrossing.x,
slouken@1951
    93
                                    xevent.xcrossing.y);
slouken@1951
    94
            }
slouken@1951
    95
        }
slouken@1951
    96
        break;
slouken@1951
    97
slouken@1951
    98
        /* Losing mouse coverage? */
slouken@1951
    99
    case LeaveNotify:{
slouken@1951
   100
#ifdef DEBUG_XEVENTS
slouken@1951
   101
            printf("LeaveNotify! (%d,%d)\n", xevent.xcrossing.x,
slouken@1951
   102
                   xevent.xcrossing.y);
slouken@1951
   103
            if (xevent.xcrossing.mode == NotifyGrab)
slouken@1951
   104
                printf("Mode: NotifyGrab\n");
slouken@1951
   105
            if (xevent.xcrossing.mode == NotifyUngrab)
slouken@1951
   106
                printf("Mode: NotifyUngrab\n");
slouken@1951
   107
#endif
slouken@1951
   108
            if ((xevent.xcrossing.mode != NotifyGrab) &&
slouken@1951
   109
                (xevent.xcrossing.mode != NotifyUngrab) &&
slouken@1951
   110
                (xevent.xcrossing.detail != NotifyInferior)) {
slouken@1951
   111
                SDL_SendMouseMotion(videodata->mouse, 0,
slouken@1951
   112
                                    xevent.xcrossing.x, xevent.xcrossing.y);
slouken@1951
   113
                SDL_SetMouseFocus(videodata->mouse, 0);
slouken@1951
   114
            }
slouken@1951
   115
        }
slouken@1951
   116
        break;
slouken@1951
   117
slouken@1951
   118
        /* Gaining input focus? */
slouken@1951
   119
    case FocusIn:{
slouken@1951
   120
#ifdef DEBUG_XEVENTS
slouken@1951
   121
            printf("FocusIn!\n");
slouken@1951
   122
#endif
slouken@1951
   123
            SDL_SetKeyboardFocus(videodata->keyboard, data->windowID);
slouken@1951
   124
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   125
            if (data->ic) {
slouken@1951
   126
                XSetICFocus(data->ic);
slouken@1951
   127
            }
slouken@1951
   128
#endif
slouken@1951
   129
        }
slouken@1951
   130
        break;
slouken@1951
   131
slouken@1951
   132
        /* Losing input focus? */
slouken@1951
   133
    case FocusOut:{
slouken@1951
   134
#ifdef DEBUG_XEVENTS
slouken@1951
   135
            printf("FocusOut!\n");
slouken@1951
   136
#endif
slouken@1951
   137
            SDL_SetKeyboardFocus(videodata->keyboard, 0);
slouken@1951
   138
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   139
            if (data->ic) {
slouken@1951
   140
                XUnsetICFocus(data->ic);
slouken@1951
   141
            }
slouken@1951
   142
#endif
slouken@1951
   143
        }
slouken@1951
   144
        break;
slouken@1951
   145
slouken@1951
   146
        /* Generated upon EnterWindow and FocusIn */
slouken@1951
   147
    case KeymapNotify:{
slouken@1951
   148
#ifdef DEBUG_XEVENTS
slouken@1951
   149
            printf("KeymapNotify!\n");
slouken@1951
   150
#endif
slouken@1951
   151
            /* FIXME:
slouken@1951
   152
               X11_SetKeyboardState(SDL_Display, xevent.xkeymap.key_vector);
slouken@1951
   153
             */
slouken@1951
   154
        }
slouken@1951
   155
        break;
slouken@1951
   156
slouken@2305
   157
        /* Has the keyboard layout changed? */
slouken@2305
   158
    case MappingNotify:{
slouken@2305
   159
#ifdef DEBUG_XEVENTS
slouken@2305
   160
            printf("MappingNotify!\n");
slouken@2305
   161
#endif
slouken@2306
   162
            X11_UpdateKeymap(_this);
slouken@2305
   163
        }
slouken@2305
   164
        break;
slouken@2305
   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;
bob@2300
   193
            KeySym keysym = NoSymbol;
slouken@2306
   194
            char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];
bob@2325
   195
            Status status = 0;
slouken@1951
   196
slouken@1951
   197
#ifdef DEBUG_XEVENTS
slouken@1951
   198
            printf("KeyPress (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
slouken@1951
   199
#endif
bob@2299
   200
            SDL_SendKeyboardKey(videodata->keyboard, SDL_PRESSED,
slouken@2305
   201
                                videodata->key_layout[keycode]);
bob@2325
   202
#if 0
slouken@2305
   203
            if (videodata->key_layout[keycode] == SDLK_UNKNOWN) {
slouken@2305
   204
                int min_keycode, max_keycode;
slouken@2305
   205
                XDisplayKeycodes(videodata->display, &min_keycode,
slouken@2305
   206
                                 &max_keycode);
slouken@2305
   207
                keysym = XKeycodeToKeysym(videodata->display, keycode, 0);
bob@2299
   208
                fprintf(stderr,
slouken@2305
   209
                        "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
   210
                        keycode, keycode - min_keycode, keysym,
slouken@2305
   211
                        XKeysymToString(keysym));
bob@2299
   212
            }
bob@2295
   213
#endif
bob@2325
   214
            /* */
slouken@2305
   215
            SDL_zero(text);
bob@2325
   216
#ifdef X_HAVE_UTF8_STRING
bob@2325
   217
            if (data->ic) {
bob@2325
   218
                Xutf8LookupString(data->ic, &xevent.xkey, text, sizeof(text),
bob@2325
   219
                                  &keysym, status);
bob@2325
   220
            }
bob@2325
   221
#else
slouken@2306
   222
            XLookupString(&xevent.xkey, text, sizeof(text), &keysym, NULL);
bob@2325
   223
#endif
slouken@2305
   224
            if (*text) {
slouken@2305
   225
                printf("Sending text event %s\n", text);
bob@2300
   226
                SDL_SendKeyboardText(videodata->keyboard, text);
bob@2300
   227
            }
slouken@1951
   228
        }
slouken@1951
   229
        break;
slouken@1951
   230
slouken@1951
   231
        /* Key release? */
slouken@1951
   232
    case KeyRelease:{
slouken@1951
   233
            KeyCode keycode = xevent.xkey.keycode;
slouken@1951
   234
slouken@1951
   235
#ifdef DEBUG_XEVENTS
slouken@1951
   236
            printf("KeyRelease (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
slouken@1951
   237
#endif
bob@2295
   238
            SDL_SendKeyboardKey(videodata->keyboard, SDL_RELEASED,
slouken@2305
   239
                                videodata->key_layout[keycode]);
slouken@1951
   240
        }
slouken@1951
   241
        break;
slouken@1951
   242
slouken@1951
   243
        /* Have we been iconified? */
slouken@1951
   244
    case UnmapNotify:{
slouken@1951
   245
#ifdef DEBUG_XEVENTS
slouken@1951
   246
            printf("UnmapNotify!\n");
slouken@1951
   247
#endif
slouken@1951
   248
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@1951
   249
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_MINIMIZED, 0,
slouken@1951
   250
                                0);
slouken@1951
   251
        }
slouken@1951
   252
        break;
slouken@1951
   253
slouken@1951
   254
        /* Have we been restored? */
slouken@1951
   255
    case MapNotify:{
slouken@1951
   256
#ifdef DEBUG_XEVENTS
slouken@1951
   257
            printf("MapNotify!\n");
slouken@1951
   258
#endif
slouken@1951
   259
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@1951
   260
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_RESTORED, 0,
slouken@1951
   261
                                0);
slouken@1951
   262
        }
slouken@1951
   263
        break;
slouken@1951
   264
slouken@1951
   265
        /* Have we been resized or moved? */
slouken@1951
   266
    case ConfigureNotify:{
slouken@1951
   267
#ifdef DEBUG_XEVENTS
slouken@1951
   268
            printf("ConfigureNotify! (resize: %dx%d)\n",
slouken@1951
   269
                   xevent.xconfigure.width, xevent.xconfigure.height);
slouken@1951
   270
#endif
slouken@1951
   271
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_MOVED,
slouken@1951
   272
                                xevent.xconfigure.x, xevent.xconfigure.y);
slouken@1951
   273
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_RESIZED,
slouken@1951
   274
                                xevent.xconfigure.width,
slouken@1951
   275
                                xevent.xconfigure.height);
slouken@1951
   276
        }
slouken@1951
   277
        break;
slouken@1951
   278
slouken@1951
   279
        /* Have we been requested to quit (or another client message?) */
slouken@1951
   280
    case ClientMessage:{
slouken@1951
   281
            if ((xevent.xclient.format == 32) &&
slouken@1951
   282
                (xevent.xclient.data.l[0] == videodata->WM_DELETE_WINDOW)) {
slouken@1951
   283
slouken@1951
   284
                SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_CLOSE, 0,
slouken@1951
   285
                                    0);
slouken@1951
   286
            }
slouken@1951
   287
        }
slouken@1951
   288
        break;
slouken@1951
   289
slouken@1951
   290
        /* Do we need to refresh ourselves? */
slouken@1951
   291
    case Expose:{
slouken@1951
   292
#ifdef DEBUG_XEVENTS
slouken@1951
   293
            printf("Expose (count = %d)\n", xevent.xexpose.count);
slouken@1951
   294
#endif
slouken@1951
   295
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_EXPOSED, 0,
slouken@1951
   296
                                0);
slouken@1951
   297
        }
slouken@1951
   298
        break;
slouken@1951
   299
slouken@1951
   300
    default:{
slouken@1951
   301
#ifdef DEBUG_XEVENTS
slouken@1951
   302
            printf("Unhandled event %d\n", xevent.type);
slouken@1951
   303
#endif
slouken@1951
   304
        }
slouken@1951
   305
        break;
slouken@1951
   306
    }
slouken@1951
   307
}
slouken@1951
   308
slouken@1951
   309
/* Ack!  XPending() actually performs a blocking read if no events available */
slouken@1951
   310
int
slouken@1951
   311
X11_Pending(Display * display)
slouken@1951
   312
{
slouken@1951
   313
    /* Flush the display connection and look to see if events are queued */
slouken@1951
   314
    XFlush(display);
slouken@1951
   315
    if (XEventsQueued(display, QueuedAlready)) {
slouken@1951
   316
        return (1);
slouken@1951
   317
    }
slouken@1951
   318
slouken@1951
   319
    /* More drastic measures are required -- see if X is ready to talk */
slouken@1951
   320
    {
slouken@1951
   321
        static struct timeval zero_time;        /* static == 0 */
slouken@1951
   322
        int x11_fd;
slouken@1951
   323
        fd_set fdset;
slouken@1951
   324
slouken@1951
   325
        x11_fd = ConnectionNumber(display);
slouken@1951
   326
        FD_ZERO(&fdset);
slouken@1951
   327
        FD_SET(x11_fd, &fdset);
slouken@1951
   328
        if (select(x11_fd + 1, &fdset, NULL, NULL, &zero_time) == 1) {
slouken@1951
   329
            return (XPending(display));
slouken@1951
   330
        }
slouken@1951
   331
    }
slouken@1951
   332
slouken@1951
   333
    /* Oh well, nothing is ready .. */
slouken@1951
   334
    return (0);
slouken@1951
   335
}
slouken@1951
   336
slouken@1951
   337
void
slouken@1951
   338
X11_PumpEvents(_THIS)
slouken@1951
   339
{
slouken@1951
   340
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   341
slouken@1951
   342
    /* Keep processing pending events */
slouken@1951
   343
    while (X11_Pending(data->display)) {
slouken@1951
   344
        X11_DispatchEvent(_this);
slouken@1951
   345
    }
slouken@1951
   346
}
slouken@1951
   347
slouken@1951
   348
void
slouken@1951
   349
X11_SaveScreenSaver(Display * display, int *saved_timeout, BOOL * dpms)
slouken@1951
   350
{
slouken@1951
   351
    int timeout, interval, prefer_blank, allow_exp;
slouken@1951
   352
    XGetScreenSaver(display, &timeout, &interval, &prefer_blank, &allow_exp);
slouken@1951
   353
    *saved_timeout = timeout;
slouken@1951
   354
slouken@1951
   355
#if SDL_VIDEO_DRIVER_X11_DPMS
slouken@1951
   356
    if (SDL_X11_HAVE_DPMS) {
slouken@1951
   357
        int dummy;
slouken@1951
   358
        if (DPMSQueryExtension(display, &dummy, &dummy)) {
slouken@1951
   359
            CARD16 state;
slouken@1951
   360
            DPMSInfo(display, &state, dpms);
slouken@1951
   361
        }
slouken@1951
   362
    }
slouken@1951
   363
#else
slouken@1951
   364
    *dpms = 0;
slouken@1951
   365
#endif /* SDL_VIDEO_DRIVER_X11_DPMS */
slouken@1951
   366
}
slouken@1951
   367
slouken@1951
   368
void
slouken@1951
   369
X11_DisableScreenSaver(Display * display)
slouken@1951
   370
{
slouken@1951
   371
    int timeout, interval, prefer_blank, allow_exp;
slouken@1951
   372
    XGetScreenSaver(display, &timeout, &interval, &prefer_blank, &allow_exp);
slouken@1951
   373
    timeout = 0;
slouken@1951
   374
    XSetScreenSaver(display, timeout, interval, prefer_blank, allow_exp);
slouken@1951
   375
slouken@1951
   376
#if SDL_VIDEO_DRIVER_X11_DPMS
slouken@1951
   377
    if (SDL_X11_HAVE_DPMS) {
slouken@1951
   378
        int dummy;
slouken@1951
   379
        if (DPMSQueryExtension(display, &dummy, &dummy)) {
slouken@1951
   380
            DPMSDisable(display);
slouken@1951
   381
        }
slouken@1951
   382
    }
slouken@1951
   383
#endif /* SDL_VIDEO_DRIVER_X11_DPMS */
slouken@1951
   384
}
slouken@1951
   385
slouken@1951
   386
void
slouken@1951
   387
X11_RestoreScreenSaver(Display * display, int saved_timeout, BOOL dpms)
slouken@1951
   388
{
slouken@1951
   389
    int timeout, interval, prefer_blank, allow_exp;
slouken@1951
   390
    XGetScreenSaver(display, &timeout, &interval, &prefer_blank, &allow_exp);
slouken@1951
   391
    timeout = saved_timeout;
slouken@1951
   392
    XSetScreenSaver(display, timeout, interval, prefer_blank, allow_exp);
slouken@1951
   393
slouken@1951
   394
#if SDL_VIDEO_DRIVER_X11_DPMS
slouken@1951
   395
    if (SDL_X11_HAVE_DPMS) {
slouken@1951
   396
        int dummy;
slouken@1951
   397
        if (DPMSQueryExtension(display, &dummy, &dummy)) {
slouken@1951
   398
            if (dpms) {
slouken@1951
   399
                DPMSEnable(display);
slouken@1951
   400
            }
slouken@1951
   401
        }
slouken@1951
   402
    }
slouken@1951
   403
#endif /* SDL_VIDEO_DRIVER_X11_DPMS */
slouken@1951
   404
}
slouken@1951
   405
slouken@1951
   406
/* vi: set ts=4 sw=4 expandtab: */