src/video/x11/SDL_x11events.c
author Bob Pendleton <bob@pendleton.com>
Fri, 09 Jan 2009 20:43:30 +0000
changeset 3011 8f4ed5ec2b06
parent 3003 699d68ef2aca
child 3013 8cc00819c8d6
permissions -rw-r--r--
I ran a global "make indent" it modified the following files.
     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             for (i = 0; i < SDL_GetNumMice(); ++i) {
   285                 SDL_Mouse *mouse;
   286 #if SDL_VIDEO_DRIVER_X11_XINPUT
   287                 X11_MouseData *data;
   288 #endif
   289 
   290                 mouse = SDL_GetMouse(i);
   291                 if (!mouse->driverdata) {
   292                     switch (xevent.type) {
   293                     case MotionNotify:
   294 #ifdef DEBUG_MOTION
   295                         printf("X11 motion: %d,%d\n", xevent.xmotion.x,
   296                                xevent.xmotion.y);
   297 #endif
   298                         SDL_SendMouseMotion(mouse->id, 0, xevent.xmotion.x,
   299                                             xevent.xmotion.y, 0);
   300                         break;
   301 
   302                     case ButtonPress:
   303                         SDL_SendMouseButton(mouse->id, SDL_PRESSED,
   304                                             xevent.xbutton.button);
   305                         break;
   306 
   307                     case ButtonRelease:
   308                         SDL_SendMouseButton(mouse->id, SDL_RELEASED,
   309                                             xevent.xbutton.button);
   310                         break;
   311                     }
   312                     continue;
   313                 }
   314 #if SDL_VIDEO_DRIVER_X11_XINPUT
   315                 data = (X11_MouseData *) mouse->driverdata;
   316                 if (xevent.type == data->motion) {
   317                     XDeviceMotionEvent *move =
   318                         (XDeviceMotionEvent *) & xevent;
   319 #ifdef DEBUG_MOTION
   320                     printf("X11 motion: %d,%d\n", move->x, move->y);
   321 #endif
   322                     SDL_SendMouseMotion(move->deviceid, 0, move->x, move->y,
   323                                         move->axis_data[2]);
   324                     return;
   325                 }
   326                 if (xevent.type == data->button_pressed) {
   327                     XDeviceButtonPressedEvent *pressed =
   328                         (XDeviceButtonPressedEvent *) & xevent;
   329                     SDL_SendMouseButton(pressed->deviceid, SDL_PRESSED,
   330                                         pressed->button);
   331                     return;
   332                 }
   333                 if (xevent.type == data->button_released) {
   334                     XDeviceButtonReleasedEvent *released =
   335                         (XDeviceButtonReleasedEvent *) & xevent;
   336                     SDL_SendMouseButton(released->deviceid, SDL_RELEASED,
   337                                         released->button);
   338                     return;
   339                 }
   340                 if (xevent.type == data->proximity_in) {
   341                     XProximityNotifyEvent *proximity =
   342                         (XProximityNotifyEvent *) & xevent;
   343                     SDL_SendProximity(proximity->deviceid, proximity->x,
   344                                       proximity->y, SDL_PROXIMITYIN);
   345                     return;
   346                 }
   347                 if (xevent.type == data->proximity_out) {
   348                     XProximityNotifyEvent *proximity =
   349                         (XProximityNotifyEvent *) & xevent;
   350                     SDL_SendProximity(proximity->deviceid, proximity->x,
   351                                       proximity->y, SDL_PROXIMITYOUT);
   352                     return;
   353                 }
   354 #endif
   355             }
   356 #ifdef DEBUG_XEVENTS
   357             printf("Unhandled event %d\n", xevent.type);
   358 #endif
   359         }
   360         break;
   361     }
   362 }
   363 
   364 /* Ack!  XPending() actually performs a blocking read if no events available */
   365 int
   366 X11_Pending(Display * display)
   367 {
   368     /* Flush the display connection and look to see if events are queued */
   369     XFlush(display);
   370     if (XEventsQueued(display, QueuedAlready)) {
   371         return (1);
   372     }
   373 
   374     /* More drastic measures are required -- see if X is ready to talk */
   375     {
   376         static struct timeval zero_time;        /* static == 0 */
   377         int x11_fd;
   378         fd_set fdset;
   379 
   380         x11_fd = ConnectionNumber(display);
   381         FD_ZERO(&fdset);
   382         FD_SET(x11_fd, &fdset);
   383         if (select(x11_fd + 1, &fdset, NULL, NULL, &zero_time) == 1) {
   384             return (XPending(display));
   385         }
   386     }
   387 
   388     /* Oh well, nothing is ready .. */
   389     return (0);
   390 }
   391 
   392 void
   393 X11_PumpEvents(_THIS)
   394 {
   395     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   396 
   397     /* Keep processing pending events */
   398     while (X11_Pending(data->display)) {
   399         X11_DispatchEvent(_this);
   400     }
   401 }
   402 
   403 void
   404 X11_SaveScreenSaver(Display * display, int *saved_timeout, BOOL * dpms)
   405 {
   406     int timeout, interval, prefer_blank, allow_exp;
   407     XGetScreenSaver(display, &timeout, &interval, &prefer_blank, &allow_exp);
   408     *saved_timeout = timeout;
   409 
   410 #if SDL_VIDEO_DRIVER_X11_DPMS
   411     if (SDL_X11_HAVE_DPMS) {
   412         int dummy;
   413         if (DPMSQueryExtension(display, &dummy, &dummy)) {
   414             CARD16 state;
   415             DPMSInfo(display, &state, dpms);
   416         }
   417     }
   418 #else
   419     *dpms = 0;
   420 #endif /* SDL_VIDEO_DRIVER_X11_DPMS */
   421 }
   422 
   423 void
   424 X11_DisableScreenSaver(Display * display)
   425 {
   426     int timeout, interval, prefer_blank, allow_exp;
   427     XGetScreenSaver(display, &timeout, &interval, &prefer_blank, &allow_exp);
   428     timeout = 0;
   429     XSetScreenSaver(display, timeout, interval, prefer_blank, allow_exp);
   430 
   431 #if SDL_VIDEO_DRIVER_X11_DPMS
   432     if (SDL_X11_HAVE_DPMS) {
   433         int dummy;
   434         if (DPMSQueryExtension(display, &dummy, &dummy)) {
   435             DPMSDisable(display);
   436         }
   437     }
   438 #endif /* SDL_VIDEO_DRIVER_X11_DPMS */
   439 }
   440 
   441 void
   442 X11_RestoreScreenSaver(Display * display, int saved_timeout, BOOL dpms)
   443 {
   444     int timeout, interval, prefer_blank, allow_exp;
   445     XGetScreenSaver(display, &timeout, &interval, &prefer_blank, &allow_exp);
   446     timeout = saved_timeout;
   447     XSetScreenSaver(display, timeout, interval, prefer_blank, allow_exp);
   448 
   449 #if SDL_VIDEO_DRIVER_X11_DPMS
   450     if (SDL_X11_HAVE_DPMS) {
   451         int dummy;
   452         if (DPMSQueryExtension(display, &dummy, &dummy)) {
   453             if (dpms) {
   454                 DPMSEnable(display);
   455             }
   456         }
   457     }
   458 #endif /* SDL_VIDEO_DRIVER_X11_DPMS */
   459 }
   460 
   461 /* vi: set ts=4 sw=4 expandtab: */