src/video/x11/SDL_x11events.c
author Bob Pendleton <bob@pendleton.com>
Tue, 15 Jan 2008 22:37:17 +0000
changeset 2300 c97ad1abe05b
parent 2299 a7cbc25071b6
child 2305 fbe8ff44c519
permissions -rw-r--r--
Minimal implementation of textinput events for x11. It only works for latin-1.
     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             KeySym keysym = NoSymbol;
   171             char text[sizeof(SDL_TEXTINPUTEVENT_TEXT_SIZE)];
   172             Uint32 ucs4 = 0;
   173 
   174 #ifdef DEBUG_XEVENTS
   175             printf("KeyPress (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
   176 #endif
   177             SDLKey physicalKey = videodata->keyCodeToSDLKTable[keycode];
   178             SDL_SendKeyboardKey(videodata->keyboard, SDL_PRESSED,
   179                                 (Uint8) keycode, physicalKey);
   180 #if 1
   181             if (physicalKey == SDLK_UNKNOWN) {
   182                 fprintf(stderr,
   183                         "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",
   184                         (int) keycode,
   185                         (unsigned int) XKeycodeToKeysym(videodata->display,
   186                                                         keycode, 0));
   187             }
   188 #endif
   189             /* works for Latin-1 */
   190             SDL_memset(&text[0], 0, SDL_TEXTINPUTEVENT_TEXT_SIZE);
   191             /* Xutf8LookupString() */
   192             XLookupString(&xevent, text, sizeof(text), &keysym, NULL);
   193             if (0 != SDL_strlen(text)) {
   194                 SDL_SendKeyboardText(videodata->keyboard, text);
   195             }
   196         }
   197         break;
   198 
   199         /* Key release? */
   200     case KeyRelease:{
   201             KeyCode keycode = xevent.xkey.keycode;
   202 
   203 #ifdef DEBUG_XEVENTS
   204             printf("KeyRelease (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
   205 #endif
   206             SDL_SendKeyboardKey(videodata->keyboard, SDL_RELEASED,
   207                                 (Uint8) keycode,
   208                                 videodata->keyCodeToSDLKTable[keycode]);
   209         }
   210         break;
   211 
   212         /* Have we been iconified? */
   213     case UnmapNotify:{
   214 #ifdef DEBUG_XEVENTS
   215             printf("UnmapNotify!\n");
   216 #endif
   217             SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_HIDDEN, 0, 0);
   218             SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_MINIMIZED, 0,
   219                                 0);
   220         }
   221         break;
   222 
   223         /* Have we been restored? */
   224     case MapNotify:{
   225 #ifdef DEBUG_XEVENTS
   226             printf("MapNotify!\n");
   227 #endif
   228             SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_SHOWN, 0, 0);
   229             SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_RESTORED, 0,
   230                                 0);
   231         }
   232         break;
   233 
   234         /* Have we been resized or moved? */
   235     case ConfigureNotify:{
   236 #ifdef DEBUG_XEVENTS
   237             printf("ConfigureNotify! (resize: %dx%d)\n",
   238                    xevent.xconfigure.width, xevent.xconfigure.height);
   239 #endif
   240             SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_MOVED,
   241                                 xevent.xconfigure.x, xevent.xconfigure.y);
   242             SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_RESIZED,
   243                                 xevent.xconfigure.width,
   244                                 xevent.xconfigure.height);
   245         }
   246         break;
   247 
   248         /* Have we been requested to quit (or another client message?) */
   249     case ClientMessage:{
   250             if ((xevent.xclient.format == 32) &&
   251                 (xevent.xclient.data.l[0] == videodata->WM_DELETE_WINDOW)) {
   252 
   253                 SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_CLOSE, 0,
   254                                     0);
   255             }
   256         }
   257         break;
   258 
   259         /* Do we need to refresh ourselves? */
   260     case Expose:{
   261 #ifdef DEBUG_XEVENTS
   262             printf("Expose (count = %d)\n", xevent.xexpose.count);
   263 #endif
   264             SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_EXPOSED, 0,
   265                                 0);
   266         }
   267         break;
   268 
   269     default:{
   270 #ifdef DEBUG_XEVENTS
   271             printf("Unhandled event %d\n", xevent.type);
   272 #endif
   273         }
   274         break;
   275     }
   276 }
   277 
   278 /* Ack!  XPending() actually performs a blocking read if no events available */
   279 int
   280 X11_Pending(Display * display)
   281 {
   282     /* Flush the display connection and look to see if events are queued */
   283     XFlush(display);
   284     if (XEventsQueued(display, QueuedAlready)) {
   285         return (1);
   286     }
   287 
   288     /* More drastic measures are required -- see if X is ready to talk */
   289     {
   290         static struct timeval zero_time;        /* static == 0 */
   291         int x11_fd;
   292         fd_set fdset;
   293 
   294         x11_fd = ConnectionNumber(display);
   295         FD_ZERO(&fdset);
   296         FD_SET(x11_fd, &fdset);
   297         if (select(x11_fd + 1, &fdset, NULL, NULL, &zero_time) == 1) {
   298             return (XPending(display));
   299         }
   300     }
   301 
   302     /* Oh well, nothing is ready .. */
   303     return (0);
   304 }
   305 
   306 void
   307 X11_PumpEvents(_THIS)
   308 {
   309     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   310 
   311     /* Keep processing pending events */
   312     while (X11_Pending(data->display)) {
   313         X11_DispatchEvent(_this);
   314     }
   315 }
   316 
   317 void
   318 X11_SaveScreenSaver(Display * display, int *saved_timeout, BOOL * dpms)
   319 {
   320     int timeout, interval, prefer_blank, allow_exp;
   321     XGetScreenSaver(display, &timeout, &interval, &prefer_blank, &allow_exp);
   322     *saved_timeout = timeout;
   323 
   324 #if SDL_VIDEO_DRIVER_X11_DPMS
   325     if (SDL_X11_HAVE_DPMS) {
   326         int dummy;
   327         if (DPMSQueryExtension(display, &dummy, &dummy)) {
   328             CARD16 state;
   329             DPMSInfo(display, &state, dpms);
   330         }
   331     }
   332 #else
   333     *dpms = 0;
   334 #endif /* SDL_VIDEO_DRIVER_X11_DPMS */
   335 }
   336 
   337 void
   338 X11_DisableScreenSaver(Display * display)
   339 {
   340     int timeout, interval, prefer_blank, allow_exp;
   341     XGetScreenSaver(display, &timeout, &interval, &prefer_blank, &allow_exp);
   342     timeout = 0;
   343     XSetScreenSaver(display, timeout, interval, prefer_blank, allow_exp);
   344 
   345 #if SDL_VIDEO_DRIVER_X11_DPMS
   346     if (SDL_X11_HAVE_DPMS) {
   347         int dummy;
   348         if (DPMSQueryExtension(display, &dummy, &dummy)) {
   349             DPMSDisable(display);
   350         }
   351     }
   352 #endif /* SDL_VIDEO_DRIVER_X11_DPMS */
   353 }
   354 
   355 void
   356 X11_RestoreScreenSaver(Display * display, int saved_timeout, BOOL dpms)
   357 {
   358     int timeout, interval, prefer_blank, allow_exp;
   359     XGetScreenSaver(display, &timeout, &interval, &prefer_blank, &allow_exp);
   360     timeout = saved_timeout;
   361     XSetScreenSaver(display, timeout, interval, prefer_blank, allow_exp);
   362 
   363 #if SDL_VIDEO_DRIVER_X11_DPMS
   364     if (SDL_X11_HAVE_DPMS) {
   365         int dummy;
   366         if (DPMSQueryExtension(display, &dummy, &dummy)) {
   367             if (dpms) {
   368                 DPMSEnable(display);
   369             }
   370         }
   371     }
   372 #endif /* SDL_VIDEO_DRIVER_X11_DPMS */
   373 }
   374 
   375 /* vi: set ts=4 sw=4 expandtab: */