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