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