src/video/x11/SDL_x11events.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 20 Jul 2010 00:05:32 -0700
changeset 4558 4d95152d9e39
parent 4556 cc06f306c053
child 4560 95352c671a6e
permissions -rw-r--r--
Applied the same logic as the fix for bug 894.

Anytime we enter the window, we gain the mouse focus. If we leave the window because of a normal LeaveNotify, then we lose mouse focus.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2010 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 <signal.h>
    27 #include <unistd.h>
    28 #include <limits.h> /* For INT_MAX */
    29 
    30 #include "SDL_x11video.h"
    31 #include "../../events/SDL_events_c.h"
    32 #include "../../events/SDL_mouse_c.h"
    33 
    34 #include "SDL_timer.h"
    35 #include "SDL_syswm.h"
    36 
    37 /*#define DEBUG_XEVENTS*/
    38 
    39 static void
    40 X11_DispatchEvent(_THIS)
    41 {
    42     SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
    43     Display *display = videodata->display;
    44     SDL_WindowData *data;
    45     XEvent xevent;
    46     int i;
    47 
    48     SDL_zero(xevent);           /* valgrind fix. --ryan. */
    49     XNextEvent(display, &xevent);
    50 
    51     /* filter events catchs XIM events and sends them to the correct
    52        handler */
    53     if (XFilterEvent(&xevent, None) == True) {
    54 #if 0
    55         printf("Filtered event type = %d display = %d window = %d\n",
    56                xevent.type, xevent.xany.display, xevent.xany.window);
    57 #endif
    58         return;
    59     }
    60 
    61     /* Send a SDL_SYSWMEVENT if the application wants them */
    62     if (SDL_GetEventState(SDL_SYSWMEVENT) == SDL_ENABLE) {
    63         SDL_SysWMmsg wmmsg;
    64 
    65         SDL_VERSION(&wmmsg.version);
    66         wmmsg.subsystem = SDL_SYSWM_X11;
    67         wmmsg.event.xevent = xevent;
    68         SDL_SendSysWMEvent(&wmmsg);
    69     }
    70 
    71     data = NULL;
    72     if (videodata && videodata->windowlist) {
    73         for (i = 0; i < videodata->numwindows; ++i) {
    74             if ((videodata->windowlist[i] != NULL) &&
    75                 (videodata->windowlist[i]->xwindow == xevent.xany.window)) {
    76                 data = videodata->windowlist[i];
    77                 break;
    78             }
    79         }
    80     }
    81     if (!data) {
    82         return;
    83     }
    84 
    85 #if 0
    86     printf("type = %d display = %d window = %d\n",
    87            xevent.type, xevent.xany.display, xevent.xany.window);
    88 #endif
    89     switch (xevent.type) {
    90 
    91         /* Gaining mouse coverage? */
    92     case EnterNotify:{
    93 #ifdef DEBUG_XEVENTS
    94             printf("EnterNotify! (%d,%d,%d)\n", 
    95                    xevent.xcrossing.x,
    96                    xevent.xcrossing.y,
    97                    xevent.xcrossing.mode);
    98             if (xevent.xcrossing.mode == NotifyGrab)
    99                 printf("Mode: NotifyGrab\n");
   100             if (xevent.xcrossing.mode == NotifyUngrab)
   101                 printf("Mode: NotifyUngrab\n");
   102 #endif
   103             SDL_SetMouseFocus(data->window);
   104         }
   105         break;
   106 
   107         /* Losing mouse coverage? */
   108     case LeaveNotify:{
   109 #ifdef DEBUG_XEVENTS
   110             printf("LeaveNotify! (%d,%d,%d)\n", 
   111                    xevent.xcrossing.x,
   112                    xevent.xcrossing.y,
   113                    xevent.xcrossing.mode);
   114             if (xevent.xcrossing.mode == NotifyGrab)
   115                 printf("Mode: NotifyGrab\n");
   116             if (xevent.xcrossing.mode == NotifyUngrab)
   117                 printf("Mode: NotifyUngrab\n");
   118 #endif
   119             if (xevent.xcrossing.mode != NotifyGrab &&
   120                 xevent.xcrossing.mode != NotifyUngrab &&
   121                 xevent.xcrossing.detail != NotifyInferior) {
   122                 SDL_SetMouseFocus(NULL);
   123             }
   124         }
   125         break;
   126 
   127         /* Gaining input focus? */
   128     case FocusIn:{
   129 #ifdef DEBUG_XEVENTS
   130             printf("FocusIn!\n");
   131 #endif
   132             SDL_SetKeyboardFocus(data->window);
   133 #ifdef X_HAVE_UTF8_STRING
   134             if (data->ic) {
   135                 XSetICFocus(data->ic);
   136             }
   137 #endif
   138         }
   139         break;
   140 
   141         /* Losing input focus? */
   142     case FocusOut:{
   143 #ifdef DEBUG_XEVENTS
   144             printf("FocusOut!\n");
   145 #endif
   146             SDL_SetKeyboardFocus(NULL);
   147 #ifdef X_HAVE_UTF8_STRING
   148             if (data->ic) {
   149                 XUnsetICFocus(data->ic);
   150             }
   151 #endif
   152         }
   153         break;
   154 
   155         /* Generated upon EnterWindow and FocusIn */
   156     case KeymapNotify:{
   157 #ifdef DEBUG_XEVENTS
   158             printf("KeymapNotify!\n");
   159 #endif
   160             /* FIXME:
   161                X11_SetKeyboardState(SDL_Display, xevent.xkeymap.key_vector);
   162              */
   163         }
   164         break;
   165 
   166         /* Has the keyboard layout changed? */
   167     case MappingNotify:{
   168 #ifdef DEBUG_XEVENTS
   169             printf("MappingNotify!\n");
   170 #endif
   171             X11_UpdateKeymap(_this);
   172         }
   173         break;
   174 
   175         /* Key press? */
   176     case KeyPress:{
   177             KeyCode keycode = xevent.xkey.keycode;
   178             KeySym keysym = NoSymbol;
   179             char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];
   180             Status status = 0;
   181 
   182 #ifdef DEBUG_XEVENTS
   183             printf("KeyPress (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
   184 #endif
   185             SDL_SendKeyboardKey(SDL_PRESSED, videodata->key_layout[keycode]);
   186 #if 1
   187             if (videodata->key_layout[keycode] == SDLK_UNKNOWN) {
   188                 int min_keycode, max_keycode;
   189                 XDisplayKeycodes(display, &min_keycode, &max_keycode);
   190                 keysym = XKeycodeToKeysym(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%lX (%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                 SDL_SendKeyboardText(text);
   209             }
   210         }
   211         break;
   212 
   213         /* Key release? */
   214     case KeyRelease:{
   215             KeyCode keycode = xevent.xkey.keycode;
   216 
   217 #ifdef DEBUG_XEVENTS
   218             printf("KeyRelease (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
   219 #endif
   220             SDL_SendKeyboardKey(SDL_RELEASED, videodata->key_layout[keycode]);
   221         }
   222         break;
   223 
   224         /* Have we been iconified? */
   225     case UnmapNotify:{
   226 #ifdef DEBUG_XEVENTS
   227             printf("UnmapNotify!\n");
   228 #endif
   229             SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
   230             SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
   231         }
   232         break;
   233 
   234         /* Have we been restored? */
   235     case MapNotify:{
   236 #ifdef DEBUG_XEVENTS
   237             printf("MapNotify!\n");
   238 #endif
   239             SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
   240             SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESTORED, 0, 0);
   241         }
   242         break;
   243 
   244         /* Have we been resized or moved? */
   245     case ConfigureNotify:{
   246 #ifdef DEBUG_XEVENTS
   247             printf("ConfigureNotify! (resize: %dx%d)\n",
   248                    xevent.xconfigure.width, xevent.xconfigure.height);
   249 #endif
   250             SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED,
   251                                 xevent.xconfigure.x, xevent.xconfigure.y);
   252             SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED,
   253                                 xevent.xconfigure.width,
   254                                 xevent.xconfigure.height);
   255         }
   256         break;
   257 
   258         /* Have we been requested to quit (or another client message?) */
   259     case ClientMessage:{
   260             if ((xevent.xclient.format == 32) &&
   261                 (xevent.xclient.data.l[0] == videodata->WM_DELETE_WINDOW)) {
   262 
   263                 SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
   264             }
   265         }
   266         break;
   267 
   268         /* Do we need to refresh ourselves? */
   269     case Expose:{
   270 #ifdef DEBUG_XEVENTS
   271             printf("Expose (count = %d)\n", xevent.xexpose.count);
   272 #endif
   273             SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
   274         }
   275         break;
   276 
   277     case MotionNotify:{
   278 #ifdef DEBUG_MOTION
   279             printf("X11 motion: %d,%d\n", xevent.xmotion.x, xevent.xmotion.y);
   280 #endif
   281             SDL_SendMouseMotion(data->window, 0, xevent.xmotion.x, xevent.xmotion.y);
   282         }
   283         break;
   284 
   285     case ButtonPress:{
   286             SDL_SendMouseButton(data->window, SDL_PRESSED, xevent.xbutton.button);
   287         }
   288         break;
   289 
   290     case ButtonRelease:{
   291             SDL_SendMouseButton(data->window, SDL_RELEASED, xevent.xbutton.button);
   292         }
   293         break;
   294 
   295     case PropertyNotify:{
   296 #ifdef DEBUG_XEVENTS
   297             unsigned char *propdata;
   298             int status, real_format;
   299             Atom real_type;
   300             unsigned long items_read, items_left, i;
   301 
   302             char *name = XGetAtomName(display, xevent.xproperty.atom);
   303             if (name) {
   304                 printf("PropertyNotify: %s %s\n", name, (xevent.xproperty.state == PropertyDelete) ? "deleted" : "changed");
   305                 XFree(name);
   306             }
   307 
   308             status = XGetWindowProperty(display, data->xwindow, xevent.xproperty.atom, 0L, 8192L, False, AnyPropertyType, &real_type, &real_format, &items_read, &items_left, &propdata);
   309             if (status == Success && items_read > 0) {
   310                 if (real_type == XA_INTEGER) {
   311                     int *values = (int *)propdata;
   312 
   313                     printf("{");
   314                     for (i = 0; i < items_read; i++) {
   315                         printf(" %d", values[i]);
   316                     }
   317                     printf(" }\n");
   318                 } else if (real_type == XA_CARDINAL) {
   319                     if (real_format == 32) {
   320                         Uint32 *values = (Uint32 *)propdata;
   321 
   322                         printf("{");
   323                         for (i = 0; i < items_read; i++) {
   324                             printf(" %d", values[i]);
   325                         }
   326                         printf(" }\n");
   327                     } else if (real_format == 16) {
   328                         Uint16 *values = (Uint16 *)propdata;
   329 
   330                         printf("{");
   331                         for (i = 0; i < items_read; i++) {
   332                             printf(" %d", values[i]);
   333                         }
   334                         printf(" }\n");
   335                     } else if (real_format == 8) {
   336                         Uint8 *values = (Uint8 *)propdata;
   337 
   338                         printf("{");
   339                         for (i = 0; i < items_read; i++) {
   340                             printf(" %d", values[i]);
   341                         }
   342                         printf(" }\n");
   343                     }
   344                 } else if (real_type == XA_STRING ||
   345                            real_type == videodata->UTF8_STRING) {
   346                     printf("{ \"%s\" }\n", propdata);
   347                 } else if (real_type == XA_ATOM) {
   348                     Atom *atoms = (Atom *)propdata;
   349 
   350                     printf("{");
   351                     for (i = 0; i < items_read; i++) {
   352                         char *name = XGetAtomName(display, atoms[i]);
   353                         if (name) {
   354                             printf(" %s", name);
   355                             XFree(name);
   356                         }
   357                     }
   358                     printf(" }\n");
   359                 } else {
   360                     char *name = XGetAtomName(display, real_type);
   361                     printf("Unknown type: %ld (%s)\n", real_type, name ? name : "UNKNOWN");
   362                     if (name) {
   363                         XFree(name);
   364                     }
   365                 }
   366             }
   367 #endif
   368         }
   369         break;
   370 
   371     /* Copy the selection from XA_CUT_BUFFER0 to the requested property */
   372     case SelectionRequest: {
   373             XSelectionRequestEvent *req;
   374             XEvent sevent;
   375             int seln_format;
   376             unsigned long nbytes;
   377             unsigned long overflow;
   378             unsigned char *seln_data;
   379 
   380             req = &xevent.xselectionrequest;
   381 #ifdef DEBUG_XEVENTS
   382             printf("SelectionRequest (requestor = %ld, target = %ld)\n",
   383                 req->requestor, req->target);
   384 #endif
   385 
   386             SDL_zero(sevent);
   387             sevent.xany.type = SelectionNotify;
   388             sevent.xselection.selection = req->selection;
   389             sevent.xselection.target = None;
   390             sevent.xselection.property = None;
   391             sevent.xselection.requestor = req->requestor;
   392             sevent.xselection.time = req->time;
   393             if (XGetWindowProperty(display, DefaultRootWindow(display),
   394                     XA_CUT_BUFFER0, 0, INT_MAX/4, False, req->target,
   395                     &sevent.xselection.target, &seln_format, &nbytes,
   396                     &overflow, &seln_data) == Success) {
   397                 if (sevent.xselection.target == req->target) {
   398                     XChangeProperty(display, req->requestor, req->property,
   399                         sevent.xselection.target, seln_format, PropModeReplace,
   400                         seln_data, nbytes);
   401                     sevent.xselection.property = req->property;
   402                 }
   403                 XFree(seln_data);
   404             }
   405             XSendEvent(display, req->requestor, False, 0, &sevent);
   406             XSync(display, False);
   407         }
   408         break;
   409 
   410     case SelectionNotify: {
   411 #ifdef DEBUG_XEVENTS
   412             printf("SelectionNotify (requestor = %ld, target = %ld)\n",
   413                 xevent.xselection.requestor, xevent.xselection.target);
   414 #endif
   415             videodata->selection_waiting = SDL_FALSE;
   416         }
   417         break;
   418 
   419     default:{
   420 #ifdef DEBUG_XEVENTS
   421             printf("Unhandled event %d\n", xevent.type);
   422 #endif
   423         }
   424         break;
   425     }
   426 }
   427 
   428 /* Ack!  XPending() actually performs a blocking read if no events available */
   429 static int
   430 X11_Pending(Display * display)
   431 {
   432     /* Flush the display connection and look to see if events are queued */
   433     XFlush(display);
   434     if (XEventsQueued(display, QueuedAlready)) {
   435         return (1);
   436     }
   437 
   438     /* More drastic measures are required -- see if X is ready to talk */
   439     {
   440         static struct timeval zero_time;        /* static == 0 */
   441         int x11_fd;
   442         fd_set fdset;
   443 
   444         x11_fd = ConnectionNumber(display);
   445         FD_ZERO(&fdset);
   446         FD_SET(x11_fd, &fdset);
   447         if (select(x11_fd + 1, &fdset, NULL, NULL, &zero_time) == 1) {
   448             return (XPending(display));
   449         }
   450     }
   451 
   452     /* Oh well, nothing is ready .. */
   453     return (0);
   454 }
   455 
   456 void
   457 X11_PumpEvents(_THIS)
   458 {
   459     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   460 
   461     /* Update activity every 30 seconds to prevent screensaver */
   462     if (_this->suspend_screensaver) {
   463         Uint32 now = SDL_GetTicks();
   464         if (!data->screensaver_activity ||
   465             (int) (now - data->screensaver_activity) >= 30000) {
   466             XResetScreenSaver(data->display);
   467             data->screensaver_activity = now;
   468         }
   469     }
   470 
   471     /* Keep processing pending events */
   472     while (X11_Pending(data->display)) {
   473         X11_DispatchEvent(_this);
   474     }
   475 }
   476 
   477 /* This is so wrong it hurts */
   478 #define GNOME_SCREENSAVER_HACK
   479 #ifdef GNOME_SCREENSAVER_HACK
   480 #include <unistd.h>
   481 static pid_t screensaver_inhibit_pid;
   482 static void
   483 gnome_screensaver_disable()
   484 {
   485     screensaver_inhibit_pid = fork();
   486     if (screensaver_inhibit_pid == 0) {
   487         close(0);
   488         close(1);
   489         close(2);
   490         execl("/usr/bin/gnome-screensaver-command",
   491               "gnome-screensaver-command",
   492               "--inhibit",
   493               "--reason",
   494               "GNOME screensaver doesn't respect MIT-SCREEN-SAVER", NULL);
   495         exit(2);
   496     }
   497 }
   498 static void
   499 gnome_screensaver_enable()
   500 {
   501     kill(screensaver_inhibit_pid, 15);
   502 }
   503 #endif
   504 
   505 void
   506 X11_SuspendScreenSaver(_THIS)
   507 {
   508 #if SDL_VIDEO_DRIVER_X11_SCRNSAVER
   509     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   510     int dummy;
   511     int major_version, minor_version;
   512 
   513     if (SDL_X11_HAVE_XSS) {
   514         /* XScreenSaverSuspend was introduced in MIT-SCREEN-SAVER 1.1 */
   515         if (!XScreenSaverQueryExtension(data->display, &dummy, &dummy) ||
   516             !XScreenSaverQueryVersion(data->display,
   517                                       &major_version, &minor_version) ||
   518             major_version < 1 || (major_version == 1 && minor_version < 1)) {
   519             return;
   520         }
   521 
   522         XScreenSaverSuspend(data->display, _this->suspend_screensaver);
   523         XResetScreenSaver(data->display);
   524     }
   525 #endif
   526 
   527 #ifdef GNOME_SCREENSAVER_HACK
   528     if (_this->suspend_screensaver) {
   529         gnome_screensaver_disable();
   530     } else {
   531         gnome_screensaver_enable();
   532     }
   533 #endif
   534 }
   535 
   536 /* vi: set ts=4 sw=4 expandtab: */