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