src/video/x11/SDL_x11events.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 27 Nov 2008 21:53:18 +0000
changeset 2794 f7872b7a8732
parent 2763 6fc50bdd88c0
child 2859 99210400e8b9
permissions -rw-r--r--
Fixed mouse coordinate range on Mac OS X
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
static void
slouken@1951
    33
X11_DispatchEvent(_THIS)
slouken@1951
    34
{
slouken@1951
    35
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1951
    36
    SDL_WindowData *data;
slouken@1951
    37
    XEvent xevent;
slouken@1951
    38
    int i;
slouken@1951
    39
slouken@1951
    40
    SDL_zero(xevent);           /* valgrind fix. --ryan. */
slouken@1951
    41
    XNextEvent(videodata->display, &xevent);
slouken@1951
    42
bob@2325
    43
    /* filter events catchs XIM events and sends them to the correct
bob@2325
    44
       handler */
bob@2325
    45
    if (XFilterEvent(&xevent, None) == True) {
bob@2327
    46
#if 0
bob@2328
    47
        printf("Filtered event type = %d display = %d window = %d\n",
bob@2328
    48
               xevent.type, xevent.xany.display, xevent.xany.window);
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
    }
bob@2327
    76
#if 0
bob@2328
    77
    printf("type = %d display = %d window = %d\n",
bob@2328
    78
           xevent.type, xevent.xany.display, xevent.xany.window);
bob@2327
    79
#endif
slouken@1951
    80
    switch (xevent.type) {
slouken@1951
    81
slouken@1951
    82
        /* Gaining mouse coverage? */
slouken@1951
    83
    case EnterNotify:{
slouken@1951
    84
#ifdef DEBUG_XEVENTS
slouken@1951
    85
            printf("EnterNotify! (%d,%d)\n", xevent.xcrossing.x,
slouken@1951
    86
                   xevent.xcrossing.y);
slouken@1951
    87
            if (xevent.xcrossing.mode == NotifyGrab)
slouken@1951
    88
                printf("Mode: NotifyGrab\n");
slouken@1951
    89
            if (xevent.xcrossing.mode == NotifyUngrab)
slouken@1951
    90
                printf("Mode: NotifyUngrab\n");
slouken@1951
    91
#endif
slouken@1951
    92
            if ((xevent.xcrossing.mode != NotifyGrab) &&
slouken@1951
    93
                (xevent.xcrossing.mode != NotifyUngrab)) {
slouken@2710
    94
                XDeviceMotionEvent *move = (XDeviceMotionEvent *) & xevent;
slouken@2710
    95
                SDL_SetMouseFocus(move->deviceid, data->windowID);
slouken@2710
    96
                SDL_SendMouseMotion(move->deviceid, 0, move->x,
slouken@2710
    97
                                    move->y, move->axis_data[2]);
slouken@1951
    98
            }
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
slouken@1951
   105
            printf("LeaveNotify! (%d,%d)\n", xevent.xcrossing.x,
slouken@1951
   106
                   xevent.xcrossing.y);
slouken@1951
   107
            if (xevent.xcrossing.mode == NotifyGrab)
slouken@1951
   108
                printf("Mode: NotifyGrab\n");
slouken@1951
   109
            if (xevent.xcrossing.mode == NotifyUngrab)
slouken@1951
   110
                printf("Mode: NotifyUngrab\n");
slouken@1951
   111
#endif
slouken@1951
   112
            if ((xevent.xcrossing.mode != NotifyGrab) &&
slouken@1951
   113
                (xevent.xcrossing.mode != NotifyUngrab) &&
slouken@1951
   114
                (xevent.xcrossing.detail != NotifyInferior)) {
slouken@2710
   115
                XDeviceMotionEvent *move = (XDeviceMotionEvent *) & xevent;
slouken@2710
   116
                SDL_SetMouseFocus(move->deviceid, 0);
slouken@1951
   117
            }
slouken@1951
   118
        }
slouken@1951
   119
        break;
slouken@1951
   120
slouken@1951
   121
        /* Gaining input focus? */
slouken@1951
   122
    case FocusIn:{
slouken@1951
   123
#ifdef DEBUG_XEVENTS
slouken@1951
   124
            printf("FocusIn!\n");
slouken@1951
   125
#endif
slouken@1951
   126
            SDL_SetKeyboardFocus(videodata->keyboard, data->windowID);
slouken@1951
   127
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   128
            if (data->ic) {
slouken@1951
   129
                XSetICFocus(data->ic);
slouken@1951
   130
            }
slouken@1951
   131
#endif
slouken@1951
   132
        }
slouken@1951
   133
        break;
slouken@1951
   134
slouken@1951
   135
        /* Losing input focus? */
slouken@1951
   136
    case FocusOut:{
slouken@1951
   137
#ifdef DEBUG_XEVENTS
slouken@1951
   138
            printf("FocusOut!\n");
slouken@1951
   139
#endif
slouken@1951
   140
            SDL_SetKeyboardFocus(videodata->keyboard, 0);
slouken@1951
   141
#ifdef X_HAVE_UTF8_STRING
slouken@1951
   142
            if (data->ic) {
slouken@1951
   143
                XUnsetICFocus(data->ic);
slouken@1951
   144
            }
slouken@1951
   145
#endif
slouken@1951
   146
        }
slouken@1951
   147
        break;
slouken@1951
   148
slouken@1951
   149
        /* Generated upon EnterWindow and FocusIn */
slouken@1951
   150
    case KeymapNotify:{
slouken@1951
   151
#ifdef DEBUG_XEVENTS
slouken@1951
   152
            printf("KeymapNotify!\n");
slouken@1951
   153
#endif
slouken@1951
   154
            /* FIXME:
slouken@1951
   155
               X11_SetKeyboardState(SDL_Display, xevent.xkeymap.key_vector);
slouken@1951
   156
             */
slouken@1951
   157
        }
slouken@1951
   158
        break;
slouken@1951
   159
slouken@2305
   160
        /* Has the keyboard layout changed? */
slouken@2305
   161
    case MappingNotify:{
slouken@2305
   162
#ifdef DEBUG_XEVENTS
slouken@2305
   163
            printf("MappingNotify!\n");
slouken@2305
   164
#endif
slouken@2306
   165
            X11_UpdateKeymap(_this);
slouken@2305
   166
        }
slouken@2305
   167
        break;
slouken@2305
   168
slouken@1951
   169
        /* Key press? */
slouken@1951
   170
    case KeyPress:{
slouken@1951
   171
            KeyCode keycode = xevent.xkey.keycode;
bob@2300
   172
            KeySym keysym = NoSymbol;
slouken@2306
   173
            char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];
bob@2325
   174
            Status status = 0;
slouken@1951
   175
slouken@1951
   176
#ifdef DEBUG_XEVENTS
slouken@1951
   177
            printf("KeyPress (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
slouken@1951
   178
#endif
bob@2299
   179
            SDL_SendKeyboardKey(videodata->keyboard, SDL_PRESSED,
slouken@2305
   180
                                videodata->key_layout[keycode]);
bob@2325
   181
#if 0
slouken@2305
   182
            if (videodata->key_layout[keycode] == SDLK_UNKNOWN) {
slouken@2305
   183
                int min_keycode, max_keycode;
slouken@2305
   184
                XDisplayKeycodes(videodata->display, &min_keycode,
slouken@2305
   185
                                 &max_keycode);
slouken@2305
   186
                keysym = XKeycodeToKeysym(videodata->display, keycode, 0);
bob@2299
   187
                fprintf(stderr,
slouken@2305
   188
                        "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
   189
                        keycode, keycode - min_keycode, keysym,
slouken@2305
   190
                        XKeysymToString(keysym));
bob@2299
   191
            }
bob@2295
   192
#endif
bob@2325
   193
            /* */
slouken@2305
   194
            SDL_zero(text);
bob@2325
   195
#ifdef X_HAVE_UTF8_STRING
bob@2325
   196
            if (data->ic) {
bob@2325
   197
                Xutf8LookupString(data->ic, &xevent.xkey, text, sizeof(text),
slouken@2738
   198
                                  &keysym, &status);
bob@2325
   199
            }
bob@2325
   200
#else
slouken@2306
   201
            XLookupString(&xevent.xkey, text, sizeof(text), &keysym, NULL);
bob@2325
   202
#endif
slouken@2305
   203
            if (*text) {
slouken@2305
   204
                printf("Sending text event %s\n", text);
bob@2300
   205
                SDL_SendKeyboardText(videodata->keyboard, text);
bob@2300
   206
            }
slouken@1951
   207
        }
slouken@1951
   208
        break;
slouken@1951
   209
slouken@1951
   210
        /* Key release? */
slouken@1951
   211
    case KeyRelease:{
slouken@1951
   212
            KeyCode keycode = xevent.xkey.keycode;
slouken@1951
   213
slouken@1951
   214
#ifdef DEBUG_XEVENTS
slouken@1951
   215
            printf("KeyRelease (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
slouken@1951
   216
#endif
bob@2295
   217
            SDL_SendKeyboardKey(videodata->keyboard, SDL_RELEASED,
slouken@2305
   218
                                videodata->key_layout[keycode]);
slouken@1951
   219
        }
slouken@1951
   220
        break;
slouken@1951
   221
slouken@1951
   222
        /* Have we been iconified? */
slouken@1951
   223
    case UnmapNotify:{
slouken@1951
   224
#ifdef DEBUG_XEVENTS
slouken@1951
   225
            printf("UnmapNotify!\n");
slouken@1951
   226
#endif
slouken@1951
   227
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@1951
   228
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_MINIMIZED, 0,
slouken@1951
   229
                                0);
slouken@1951
   230
        }
slouken@1951
   231
        break;
slouken@1951
   232
slouken@1951
   233
        /* Have we been restored? */
slouken@1951
   234
    case MapNotify:{
slouken@1951
   235
#ifdef DEBUG_XEVENTS
slouken@1951
   236
            printf("MapNotify!\n");
slouken@1951
   237
#endif
slouken@1951
   238
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@1951
   239
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_RESTORED, 0,
slouken@1951
   240
                                0);
slouken@1951
   241
        }
slouken@1951
   242
        break;
slouken@1951
   243
slouken@1951
   244
        /* Have we been resized or moved? */
slouken@1951
   245
    case ConfigureNotify:{
slouken@1951
   246
#ifdef DEBUG_XEVENTS
slouken@1951
   247
            printf("ConfigureNotify! (resize: %dx%d)\n",
slouken@1951
   248
                   xevent.xconfigure.width, xevent.xconfigure.height);
slouken@1951
   249
#endif
slouken@1951
   250
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_MOVED,
slouken@1951
   251
                                xevent.xconfigure.x, xevent.xconfigure.y);
slouken@1951
   252
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_RESIZED,
slouken@1951
   253
                                xevent.xconfigure.width,
slouken@1951
   254
                                xevent.xconfigure.height);
slouken@1951
   255
        }
slouken@1951
   256
        break;
slouken@1951
   257
slouken@1951
   258
        /* Have we been requested to quit (or another client message?) */
slouken@1951
   259
    case ClientMessage:{
slouken@1951
   260
            if ((xevent.xclient.format == 32) &&
slouken@1951
   261
                (xevent.xclient.data.l[0] == videodata->WM_DELETE_WINDOW)) {
slouken@1951
   262
slouken@1951
   263
                SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_CLOSE, 0,
slouken@1951
   264
                                    0);
slouken@1951
   265
            }
slouken@1951
   266
        }
slouken@1951
   267
        break;
slouken@1951
   268
slouken@1951
   269
        /* Do we need to refresh ourselves? */
slouken@1951
   270
    case Expose:{
slouken@1951
   271
#ifdef DEBUG_XEVENTS
slouken@1951
   272
            printf("Expose (count = %d)\n", xevent.xexpose.count);
slouken@1951
   273
#endif
slouken@1951
   274
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_EXPOSED, 0,
slouken@1951
   275
                                0);
slouken@1951
   276
        }
slouken@1951
   277
        break;
slouken@1951
   278
slouken@1951
   279
    default:{
slouken@2710
   280
            if (xevent.type == motion) {        /* MotionNotify */
slouken@2794
   281
                XDeviceMotionEvent *move = (XDeviceMotionEvent *) & xevent;
slouken@2710
   282
#ifdef DEBUG_MOTION
slouken@2794
   283
                printf("X11 motion: %d,%d\n", move->x, move->y);
slouken@2710
   284
#endif
slouken@2710
   285
                SDL_SendMouseMotion(move->deviceid, 0, move->x,
slouken@2710
   286
                                    move->y, move->axis_data[2]);
slouken@2710
   287
            } else if (xevent.type == button_pressed) { /* ButtonPress */
slouken@2710
   288
                XDeviceButtonPressedEvent *pressed =
slouken@2710
   289
                    (XDeviceButtonPressedEvent *) & xevent;
slouken@2710
   290
                SDL_SendMouseButton(pressed->deviceid, SDL_PRESSED,
slouken@2710
   291
                                    pressed->button);
slouken@2710
   292
            } else if (xevent.type == button_released) {        /* ButtonRelease */
slouken@2710
   293
                XDeviceButtonReleasedEvent *released =
slouken@2710
   294
                    (XDeviceButtonReleasedEvent *) & xevent;
slouken@2710
   295
                SDL_SendMouseButton(released->deviceid, SDL_RELEASED,
slouken@2710
   296
                                    released->button);
slouken@2710
   297
            } else if (xevent.type == proximity_in) {
slouken@2710
   298
                XProximityNotifyEvent *proximity =
slouken@2710
   299
                    (XProximityNotifyEvent *) & xevent;
slouken@2710
   300
                SDL_SendProximity(proximity->deviceid, proximity->x,
slouken@2710
   301
                                  proximity->y, SDL_PROXIMITYIN);
slouken@2710
   302
            } else if (xevent.type == proximity_out) {
slouken@2710
   303
                XProximityNotifyEvent *proximity =
slouken@2710
   304
                    (XProximityNotifyEvent *) & xevent;
slouken@2710
   305
                SDL_SendProximity(proximity->deviceid, proximity->x,
slouken@2710
   306
                                  proximity->y, SDL_PROXIMITYOUT);
slouken@2710
   307
            }
slouken@1951
   308
#ifdef DEBUG_XEVENTS
slouken@2710
   309
            else {
slouken@2710
   310
                printf("Unhandled event %d\n", xevent.type);
slouken@2710
   311
            }
slouken@1951
   312
#endif
slouken@1951
   313
        }
slouken@1951
   314
        break;
slouken@1951
   315
    }
slouken@1951
   316
}
slouken@1951
   317
slouken@1951
   318
/* Ack!  XPending() actually performs a blocking read if no events available */
slouken@1951
   319
int
slouken@1951
   320
X11_Pending(Display * display)
slouken@1951
   321
{
slouken@1951
   322
    /* Flush the display connection and look to see if events are queued */
slouken@1951
   323
    XFlush(display);
slouken@1951
   324
    if (XEventsQueued(display, QueuedAlready)) {
slouken@1951
   325
        return (1);
slouken@1951
   326
    }
slouken@1951
   327
slouken@1951
   328
    /* More drastic measures are required -- see if X is ready to talk */
slouken@1951
   329
    {
slouken@1951
   330
        static struct timeval zero_time;        /* static == 0 */
slouken@1951
   331
        int x11_fd;
slouken@1951
   332
        fd_set fdset;
slouken@1951
   333
slouken@1951
   334
        x11_fd = ConnectionNumber(display);
slouken@1951
   335
        FD_ZERO(&fdset);
slouken@1951
   336
        FD_SET(x11_fd, &fdset);
slouken@1951
   337
        if (select(x11_fd + 1, &fdset, NULL, NULL, &zero_time) == 1) {
slouken@1951
   338
            return (XPending(display));
slouken@1951
   339
        }
slouken@1951
   340
    }
slouken@1951
   341
slouken@1951
   342
    /* Oh well, nothing is ready .. */
slouken@1951
   343
    return (0);
slouken@1951
   344
}
slouken@1951
   345
slouken@1951
   346
void
slouken@1951
   347
X11_PumpEvents(_THIS)
slouken@1951
   348
{
slouken@1951
   349
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1951
   350
slouken@1951
   351
    /* Keep processing pending events */
slouken@1951
   352
    while (X11_Pending(data->display)) {
slouken@1951
   353
        X11_DispatchEvent(_this);
slouken@1951
   354
    }
slouken@1951
   355
}
slouken@1951
   356
slouken@1951
   357
void
slouken@1951
   358
X11_SaveScreenSaver(Display * display, int *saved_timeout, BOOL * dpms)
slouken@1951
   359
{
slouken@1951
   360
    int timeout, interval, prefer_blank, allow_exp;
slouken@1951
   361
    XGetScreenSaver(display, &timeout, &interval, &prefer_blank, &allow_exp);
slouken@1951
   362
    *saved_timeout = timeout;
slouken@1951
   363
slouken@1951
   364
#if SDL_VIDEO_DRIVER_X11_DPMS
slouken@1951
   365
    if (SDL_X11_HAVE_DPMS) {
slouken@1951
   366
        int dummy;
slouken@1951
   367
        if (DPMSQueryExtension(display, &dummy, &dummy)) {
slouken@1951
   368
            CARD16 state;
slouken@1951
   369
            DPMSInfo(display, &state, dpms);
slouken@1951
   370
        }
slouken@1951
   371
    }
slouken@1951
   372
#else
slouken@1951
   373
    *dpms = 0;
slouken@1951
   374
#endif /* SDL_VIDEO_DRIVER_X11_DPMS */
slouken@1951
   375
}
slouken@1951
   376
slouken@1951
   377
void
slouken@1951
   378
X11_DisableScreenSaver(Display * display)
slouken@1951
   379
{
slouken@1951
   380
    int timeout, interval, prefer_blank, allow_exp;
slouken@1951
   381
    XGetScreenSaver(display, &timeout, &interval, &prefer_blank, &allow_exp);
slouken@1951
   382
    timeout = 0;
slouken@1951
   383
    XSetScreenSaver(display, timeout, interval, prefer_blank, allow_exp);
slouken@1951
   384
slouken@1951
   385
#if SDL_VIDEO_DRIVER_X11_DPMS
slouken@1951
   386
    if (SDL_X11_HAVE_DPMS) {
slouken@1951
   387
        int dummy;
slouken@1951
   388
        if (DPMSQueryExtension(display, &dummy, &dummy)) {
slouken@1951
   389
            DPMSDisable(display);
slouken@1951
   390
        }
slouken@1951
   391
    }
slouken@1951
   392
#endif /* SDL_VIDEO_DRIVER_X11_DPMS */
slouken@1951
   393
}
slouken@1951
   394
slouken@1951
   395
void
slouken@1951
   396
X11_RestoreScreenSaver(Display * display, int saved_timeout, BOOL dpms)
slouken@1951
   397
{
slouken@1951
   398
    int timeout, interval, prefer_blank, allow_exp;
slouken@1951
   399
    XGetScreenSaver(display, &timeout, &interval, &prefer_blank, &allow_exp);
slouken@1951
   400
    timeout = saved_timeout;
slouken@1951
   401
    XSetScreenSaver(display, timeout, interval, prefer_blank, allow_exp);
slouken@1951
   402
slouken@1951
   403
#if SDL_VIDEO_DRIVER_X11_DPMS
slouken@1951
   404
    if (SDL_X11_HAVE_DPMS) {
slouken@1951
   405
        int dummy;
slouken@1951
   406
        if (DPMSQueryExtension(display, &dummy, &dummy)) {
slouken@1951
   407
            if (dpms) {
slouken@1951
   408
                DPMSEnable(display);
slouken@1951
   409
            }
slouken@1951
   410
        }
slouken@1951
   411
    }
slouken@1951
   412
#endif /* SDL_VIDEO_DRIVER_X11_DPMS */
slouken@1951
   413
}
slouken@1951
   414
slouken@1951
   415
/* vi: set ts=4 sw=4 expandtab: */