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