src/video/x11/SDL_x11events.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Jan 2009 19:26:50 +0000
changeset 2993 2fad80c77c17
parent 2992 dbff5769d742
child 2997 e4f025078c1c
permissions -rw-r--r--
Technically more correct
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #include <sys/types.h>
    25 #include <sys/time.h>
    26 #include <unistd.h>
    27 
    28 #include "SDL_syswm.h"
    29 #include "SDL_x11video.h"
    30 #include "../../events/SDL_events_c.h"
    31 #include "../../events/SDL_mouse_c.h"
    32 
    33 static void
    34 X11_DispatchEvent(_THIS)
    35 {
    36     SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
    37     SDL_WindowData *data;
    38     XEvent xevent;
    39     int i;
    40 
    41     SDL_zero(xevent);           /* valgrind fix. --ryan. */
    42     XNextEvent(videodata->display, &xevent);
    43 
    44     /* filter events catchs XIM events and sends them to the correct
    45        handler */
    46     if (XFilterEvent(&xevent, None) == True) {
    47 #if 0
    48         printf("Filtered event type = %d display = %d window = %d\n",
    49                xevent.type, xevent.xany.display, xevent.xany.window);
    50 #endif
    51         return;
    52     }
    53 
    54     /* Send a SDL_SYSWMEVENT if the application wants them */
    55     if (SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE) {
    56         SDL_SysWMmsg wmmsg;
    57 
    58         SDL_VERSION(&wmmsg.version);
    59         wmmsg.subsystem = SDL_SYSWM_X11;
    60         wmmsg.event.xevent = xevent;
    61         SDL_SendSysWMEvent(&wmmsg);
    62     }
    63 
    64     data = NULL;
    65     if (videodata && videodata->windowlist) {
    66         for (i = 0; i < videodata->numwindows; ++i) {
    67             if ((videodata->windowlist[i] != NULL) &&
    68                 (videodata->windowlist[i]->window == xevent.xany.window)) {
    69                 data = videodata->windowlist[i];
    70                 break;
    71             }
    72         }
    73     }
    74     if (!data) {
    75         return;
    76     }
    77 #if 0
    78     printf("type = %d display = %d window = %d\n",
    79            xevent.type, xevent.xany.display, xevent.xany.window);
    80 #endif
    81     switch (xevent.type) {
    82 
    83         /* Gaining mouse coverage? */
    84     case EnterNotify:{
    85 #ifdef DEBUG_XEVENTS
    86             printf("EnterNotify! (%d,%d)\n", xevent.xcrossing.x,
    87                    xevent.xcrossing.y);
    88             if (xevent.xcrossing.mode == NotifyGrab)
    89                 printf("Mode: NotifyGrab\n");
    90             if (xevent.xcrossing.mode == NotifyUngrab)
    91                 printf("Mode: NotifyUngrab\n");
    92 #endif
    93             if ((xevent.xcrossing.mode != NotifyGrab) &&
    94                 (xevent.xcrossing.mode != NotifyUngrab)) {
    95                 /* FIXME: Should we reset data for all mice? */
    96 #if 0
    97                 SDL_SetMouseFocus(0, data->windowID);
    98                 SDL_SendMouseMotion(0, 0, move->x, move->y, 0);
    99 #endif
   100             }
   101         }
   102         break;
   103 
   104         /* Losing mouse coverage? */
   105     case LeaveNotify:{
   106 #ifdef DEBUG_XEVENTS
   107             printf("LeaveNotify! (%d,%d)\n", xevent.xcrossing.x,
   108                    xevent.xcrossing.y);
   109             if (xevent.xcrossing.mode == NotifyGrab)
   110                 printf("Mode: NotifyGrab\n");
   111             if (xevent.xcrossing.mode == NotifyUngrab)
   112                 printf("Mode: NotifyUngrab\n");
   113 #endif
   114             if ((xevent.xcrossing.mode != NotifyGrab) &&
   115                 (xevent.xcrossing.mode != NotifyUngrab) &&
   116                 (xevent.xcrossing.detail != NotifyInferior)) {
   117                 /* FIXME: Should we reset data for all mice? */
   118 #if 0
   119                 SDL_SetMouseFocus(0, 0);
   120 #endif
   121             }
   122         }
   123         break;
   124 
   125         /* Gaining input focus? */
   126     case FocusIn:{
   127 #ifdef DEBUG_XEVENTS
   128             printf("FocusIn!\n");
   129 #endif
   130             SDL_SetKeyboardFocus(videodata->keyboard, data->windowID);
   131 #ifdef X_HAVE_UTF8_STRING
   132             if (data->ic) {
   133                 XSetICFocus(data->ic);
   134             }
   135 #endif
   136         }
   137         break;
   138 
   139         /* Losing input focus? */
   140     case FocusOut:{
   141 #ifdef DEBUG_XEVENTS
   142             printf("FocusOut!\n");
   143 #endif
   144             SDL_SetKeyboardFocus(videodata->keyboard, 0);
   145 #ifdef X_HAVE_UTF8_STRING
   146             if (data->ic) {
   147                 XUnsetICFocus(data->ic);
   148             }
   149 #endif
   150         }
   151         break;
   152 
   153         /* Generated upon EnterWindow and FocusIn */
   154     case KeymapNotify:{
   155 #ifdef DEBUG_XEVENTS
   156             printf("KeymapNotify!\n");
   157 #endif
   158             /* FIXME:
   159                X11_SetKeyboardState(SDL_Display, xevent.xkeymap.key_vector);
   160              */
   161         }
   162         break;
   163 
   164         /* Has the keyboard layout changed? */
   165     case MappingNotify:{
   166 #ifdef DEBUG_XEVENTS
   167             printf("MappingNotify!\n");
   168 #endif
   169             X11_UpdateKeymap(_this);
   170         }
   171         break;
   172 
   173         /* Key press? */
   174     case KeyPress:{
   175             KeyCode keycode = xevent.xkey.keycode;
   176             KeySym keysym = NoSymbol;
   177             char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];
   178             Status status = 0;
   179 
   180 #ifdef DEBUG_XEVENTS
   181             printf("KeyPress (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
   182 #endif
   183             SDL_SendKeyboardKey(videodata->keyboard, SDL_PRESSED,
   184                                 videodata->key_layout[keycode]);
   185 #if 0
   186             if (videodata->key_layout[keycode] == SDLK_UNKNOWN) {
   187                 int min_keycode, max_keycode;
   188                 XDisplayKeycodes(videodata->display, &min_keycode,
   189                                  &max_keycode);
   190                 keysym = XKeycodeToKeysym(videodata->display, keycode, 0);
   191                 fprintf(stderr,
   192                         "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",
   193                         keycode, keycode - min_keycode, keysym,
   194                         XKeysymToString(keysym));
   195             }
   196 #endif
   197             /* */
   198             SDL_zero(text);
   199 #ifdef X_HAVE_UTF8_STRING
   200             if (data->ic) {
   201                 Xutf8LookupString(data->ic, &xevent.xkey, text, sizeof(text),
   202                                   &keysym, &status);
   203             }
   204 #else
   205             XLookupString(&xevent.xkey, text, sizeof(text), &keysym, NULL);
   206 #endif
   207             if (*text) {
   208                 printf("Sending text event %s\n", text);
   209                 SDL_SendKeyboardText(videodata->keyboard, text);
   210             }
   211         }
   212         break;
   213 
   214         /* Key release? */
   215     case KeyRelease:{
   216             KeyCode keycode = xevent.xkey.keycode;
   217 
   218 #ifdef DEBUG_XEVENTS
   219             printf("KeyRelease (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
   220 #endif
   221             SDL_SendKeyboardKey(videodata->keyboard, SDL_RELEASED,
   222                                 videodata->key_layout[keycode]);
   223         }
   224         break;
   225 
   226         /* Have we been iconified? */
   227     case UnmapNotify:{
   228 #ifdef DEBUG_XEVENTS
   229             printf("UnmapNotify!\n");
   230 #endif
   231             SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_HIDDEN, 0, 0);
   232             SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_MINIMIZED, 0,
   233                                 0);
   234         }
   235         break;
   236 
   237         /* Have we been restored? */
   238     case MapNotify:{
   239 #ifdef DEBUG_XEVENTS
   240             printf("MapNotify!\n");
   241 #endif
   242             SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_SHOWN, 0, 0);
   243             SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_RESTORED, 0,
   244                                 0);
   245         }
   246         break;
   247 
   248         /* Have we been resized or moved? */
   249     case ConfigureNotify:{
   250 #ifdef DEBUG_XEVENTS
   251             printf("ConfigureNotify! (resize: %dx%d)\n",
   252                    xevent.xconfigure.width, xevent.xconfigure.height);
   253 #endif
   254             SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_MOVED,
   255                                 xevent.xconfigure.x, xevent.xconfigure.y);
   256             SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_RESIZED,
   257                                 xevent.xconfigure.width,
   258                                 xevent.xconfigure.height);
   259         }
   260         break;
   261 
   262         /* Have we been requested to quit (or another client message?) */
   263     case ClientMessage:{
   264             if ((xevent.xclient.format == 32) &&
   265                 (xevent.xclient.data.l[0] == videodata->WM_DELETE_WINDOW)) {
   266 
   267                 SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_CLOSE, 0,
   268                                     0);
   269             }
   270         }
   271         break;
   272 
   273         /* Do we need to refresh ourselves? */
   274     case Expose:{
   275 #ifdef DEBUG_XEVENTS
   276             printf("Expose (count = %d)\n", xevent.xexpose.count);
   277 #endif
   278             SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_EXPOSED, 0,
   279                                 0);
   280         }
   281         break;
   282 
   283     default:{
   284 #if SDL_VIDEO_DRIVER_X11_XINPUT
   285             for (i = 0; i < SDL_GetNumMice(); ++i) {
   286                 SDL_Mouse *mouse;
   287                 X11_MouseData *data;
   288 
   289                 mouse = SDL_GetMouse(i);
   290                 data = (X11_MouseData *) mouse->driverdata;
   291                 if (!data) {
   292                     switch (xevent.type) {
   293                     case MotionNotify:
   294 #ifdef DEBUG_MOTION
   295                         printf("X11 motion: %d,%d\n", xevent.xmotion.x, xevent.xmotion.y);
   296 #endif
   297                         SDL_SendMouseMotion(mouse->id, 0, xevent.xmotion.x, xevent.xmotion.y, 0);
   298                         break;
   299 
   300                     case ButtonPress:
   301                         SDL_SendMouseButton(mouse->id, SDL_PRESSED, xevent.xbutton.button);
   302                         break;
   303 
   304                     case ButtonRelease:
   305                         SDL_SendMouseButton(mouse->id, SDL_RELEASED, xevent.xbutton.button);
   306                         break;
   307                     }
   308                     continue;
   309                 }
   310 
   311                 if (xevent.type == data->motion) {      /* MotionNotify */
   312                     XDeviceMotionEvent *move =
   313                         (XDeviceMotionEvent *) & xevent;
   314 #ifdef DEBUG_MOTION
   315                     printf("X11 motion: %d,%d\n", move->x, move->y);
   316 #endif
   317                     SDL_SendMouseMotion(move->deviceid, 0, move->x, move->y,
   318                                         move->axis_data[2]);
   319                     return;
   320                 }
   321                 if (xevent.type == data->button_pressed) {      /* ButtonPress */
   322                     XDeviceButtonPressedEvent *pressed =
   323                         (XDeviceButtonPressedEvent *) & xevent;
   324                     SDL_SendMouseButton(pressed->deviceid, SDL_PRESSED,
   325                                         pressed->button);
   326                     return;
   327                 }
   328                 if (xevent.type == data->button_released) {     /* ButtonRelease */
   329                     XDeviceButtonReleasedEvent *released =
   330                         (XDeviceButtonReleasedEvent *) & xevent;
   331                     SDL_SendMouseButton(released->deviceid, SDL_RELEASED,
   332                                         released->button);
   333                     return;
   334                 }
   335                 if (xevent.type == data->proximity_in) {
   336                     XProximityNotifyEvent *proximity =
   337                         (XProximityNotifyEvent *) & xevent;
   338                     SDL_SendProximity(proximity->deviceid, proximity->x,
   339                                       proximity->y, SDL_PROXIMITYIN);
   340                     return;
   341                 }
   342                 if (xevent.type == data->proximity_out) {
   343                     XProximityNotifyEvent *proximity =
   344                         (XProximityNotifyEvent *) & xevent;
   345                     SDL_SendProximity(proximity->deviceid, proximity->x,
   346                                       proximity->y, SDL_PROXIMITYOUT);
   347                     return;
   348                 }
   349             }
   350 #endif
   351 #ifdef DEBUG_XEVENTS
   352             printf("Unhandled event %d\n", xevent.type);
   353 #endif
   354         }
   355         break;
   356     }
   357 }
   358 
   359 /* Ack!  XPending() actually performs a blocking read if no events available */
   360 int
   361 X11_Pending(Display * display)
   362 {
   363     /* Flush the display connection and look to see if events are queued */
   364     XFlush(display);
   365     if (XEventsQueued(display, QueuedAlready)) {
   366         return (1);
   367     }
   368 
   369     /* More drastic measures are required -- see if X is ready to talk */
   370     {
   371         static struct timeval zero_time;        /* static == 0 */
   372         int x11_fd;
   373         fd_set fdset;
   374 
   375         x11_fd = ConnectionNumber(display);
   376         FD_ZERO(&fdset);
   377         FD_SET(x11_fd, &fdset);
   378         if (select(x11_fd + 1, &fdset, NULL, NULL, &zero_time) == 1) {
   379             return (XPending(display));
   380         }
   381     }
   382 
   383     /* Oh well, nothing is ready .. */
   384     return (0);
   385 }
   386 
   387 void
   388 X11_PumpEvents(_THIS)
   389 {
   390     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   391 
   392     /* Keep processing pending events */
   393     while (X11_Pending(data->display)) {
   394         X11_DispatchEvent(_this);
   395     }
   396 }
   397 
   398 void
   399 X11_SaveScreenSaver(Display * display, int *saved_timeout, BOOL * dpms)
   400 {
   401     int timeout, interval, prefer_blank, allow_exp;
   402     XGetScreenSaver(display, &timeout, &interval, &prefer_blank, &allow_exp);
   403     *saved_timeout = timeout;
   404 
   405 #if SDL_VIDEO_DRIVER_X11_DPMS
   406     if (SDL_X11_HAVE_DPMS) {
   407         int dummy;
   408         if (DPMSQueryExtension(display, &dummy, &dummy)) {
   409             CARD16 state;
   410             DPMSInfo(display, &state, dpms);
   411         }
   412     }
   413 #else
   414     *dpms = 0;
   415 #endif /* SDL_VIDEO_DRIVER_X11_DPMS */
   416 }
   417 
   418 void
   419 X11_DisableScreenSaver(Display * display)
   420 {
   421     int timeout, interval, prefer_blank, allow_exp;
   422     XGetScreenSaver(display, &timeout, &interval, &prefer_blank, &allow_exp);
   423     timeout = 0;
   424     XSetScreenSaver(display, timeout, interval, prefer_blank, allow_exp);
   425 
   426 #if SDL_VIDEO_DRIVER_X11_DPMS
   427     if (SDL_X11_HAVE_DPMS) {
   428         int dummy;
   429         if (DPMSQueryExtension(display, &dummy, &dummy)) {
   430             DPMSDisable(display);
   431         }
   432     }
   433 #endif /* SDL_VIDEO_DRIVER_X11_DPMS */
   434 }
   435 
   436 void
   437 X11_RestoreScreenSaver(Display * display, int saved_timeout, BOOL dpms)
   438 {
   439     int timeout, interval, prefer_blank, allow_exp;
   440     XGetScreenSaver(display, &timeout, &interval, &prefer_blank, &allow_exp);
   441     timeout = saved_timeout;
   442     XSetScreenSaver(display, timeout, interval, prefer_blank, allow_exp);
   443 
   444 #if SDL_VIDEO_DRIVER_X11_DPMS
   445     if (SDL_X11_HAVE_DPMS) {
   446         int dummy;
   447         if (DPMSQueryExtension(display, &dummy, &dummy)) {
   448             if (dpms) {
   449                 DPMSEnable(display);
   450             }
   451         }
   452     }
   453 #endif /* SDL_VIDEO_DRIVER_X11_DPMS */
   454 }
   455 
   456 /* vi: set ts=4 sw=4 expandtab: */