2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
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.
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.
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
22 #include "SDL_config.h"
24 #include <sys/types.h>
28 #include "SDL_syswm.h"
29 #include "SDL_x11video.h"
30 #include "../../events/SDL_events_c.h"
31 #include "../../events/SDL_mouse_c.h"
34 X11_DispatchEvent(_THIS)
36 SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
41 SDL_zero(xevent); /* valgrind fix. --ryan. */
42 XNextEvent(videodata->display, &xevent);
44 /* filter events catchs XIM events and sends them to the correct
46 if (XFilterEvent(&xevent, None) == True) {
48 printf("Filtered event type = %d display = %d window = %d\n",
49 xevent.type, xevent.xany.display, xevent.xany.window);
54 /* Send a SDL_SYSWMEVENT if the application wants them */
55 if (SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE) {
58 SDL_VERSION(&wmmsg.version);
59 wmmsg.subsystem = SDL_SYSWM_X11;
60 wmmsg.event.xevent = xevent;
61 SDL_SendSysWMEvent(&wmmsg);
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];
78 printf("type = %d display = %d window = %d\n",
79 xevent.type, xevent.xany.display, xevent.xany.window);
81 switch (xevent.type) {
83 /* Gaining mouse coverage? */
86 printf("EnterNotify! (%d,%d)\n", xevent.xcrossing.x,
88 if (xevent.xcrossing.mode == NotifyGrab)
89 printf("Mode: NotifyGrab\n");
90 if (xevent.xcrossing.mode == NotifyUngrab)
91 printf("Mode: NotifyUngrab\n");
93 if ((xevent.xcrossing.mode != NotifyGrab) &&
94 (xevent.xcrossing.mode != NotifyUngrab)) {
95 /* FIXME: Should we reset data for all mice? */
97 SDL_SetMouseFocus(0, data->windowID);
98 SDL_SendMouseMotion(0, 0, move->x, move->y, 0);
104 /* Losing mouse coverage? */
107 printf("LeaveNotify! (%d,%d)\n", xevent.xcrossing.x,
109 if (xevent.xcrossing.mode == NotifyGrab)
110 printf("Mode: NotifyGrab\n");
111 if (xevent.xcrossing.mode == NotifyUngrab)
112 printf("Mode: NotifyUngrab\n");
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? */
119 SDL_SetMouseFocus(0, 0);
125 /* Gaining input focus? */
128 printf("FocusIn!\n");
130 SDL_SetKeyboardFocus(videodata->keyboard, data->windowID);
131 #ifdef X_HAVE_UTF8_STRING
133 XSetICFocus(data->ic);
139 /* Losing input focus? */
142 printf("FocusOut!\n");
144 SDL_SetKeyboardFocus(videodata->keyboard, 0);
145 #ifdef X_HAVE_UTF8_STRING
147 XUnsetICFocus(data->ic);
153 /* Generated upon EnterWindow and FocusIn */
156 printf("KeymapNotify!\n");
159 X11_SetKeyboardState(SDL_Display, xevent.xkeymap.key_vector);
164 /* Has the keyboard layout changed? */
167 printf("MappingNotify!\n");
169 X11_UpdateKeymap(_this);
175 KeyCode keycode = xevent.xkey.keycode;
176 KeySym keysym = NoSymbol;
177 char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];
181 printf("KeyPress (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
183 SDL_SendKeyboardKey(videodata->keyboard, SDL_PRESSED,
184 videodata->key_layout[keycode]);
186 if (videodata->key_layout[keycode] == SDLK_UNKNOWN) {
187 int min_keycode, max_keycode;
188 XDisplayKeycodes(videodata->display, &min_keycode,
190 keysym = XKeycodeToKeysym(videodata->display, keycode, 0);
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));
199 #ifdef X_HAVE_UTF8_STRING
201 Xutf8LookupString(data->ic, &xevent.xkey, text, sizeof(text),
205 XLookupString(&xevent.xkey, text, sizeof(text), &keysym, NULL);
208 SDL_SendKeyboardText(videodata->keyboard, text);
215 KeyCode keycode = xevent.xkey.keycode;
218 printf("KeyRelease (X11 keycode = 0x%X)\n", xevent.xkey.keycode);
220 SDL_SendKeyboardKey(videodata->keyboard, SDL_RELEASED,
221 videodata->key_layout[keycode]);
225 /* Have we been iconified? */
228 printf("UnmapNotify!\n");
230 SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_HIDDEN, 0, 0);
231 SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_MINIMIZED, 0,
236 /* Have we been restored? */
239 printf("MapNotify!\n");
241 SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_SHOWN, 0, 0);
242 SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_RESTORED, 0,
247 /* Have we been resized or moved? */
248 case ConfigureNotify:{
250 printf("ConfigureNotify! (resize: %dx%d)\n",
251 xevent.xconfigure.width, xevent.xconfigure.height);
253 SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_MOVED,
254 xevent.xconfigure.x, xevent.xconfigure.y);
255 SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_RESIZED,
256 xevent.xconfigure.width,
257 xevent.xconfigure.height);
261 /* Have we been requested to quit (or another client message?) */
263 if ((xevent.xclient.format == 32) &&
264 (xevent.xclient.data.l[0] == videodata->WM_DELETE_WINDOW)) {
266 SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_CLOSE, 0,
272 /* Do we need to refresh ourselves? */
275 printf("Expose (count = %d)\n", xevent.xexpose.count);
277 SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_EXPOSED, 0,
283 for (i = 0; i < SDL_GetNumMice(); ++i) {
285 #if SDL_VIDEO_DRIVER_X11_XINPUT
289 mouse = SDL_GetMouse(i);
290 if (!mouse->driverdata) {
291 switch (xevent.type) {
294 printf("X11 motion: %d,%d\n", xevent.xmotion.x,
297 SDL_SendMouseMotion(mouse->id, 0, xevent.xmotion.x,
298 xevent.xmotion.y, 0);
302 SDL_SendMouseButton(mouse->id, SDL_PRESSED,
303 xevent.xbutton.button);
307 SDL_SendMouseButton(mouse->id, SDL_RELEASED,
308 xevent.xbutton.button);
313 #if SDL_VIDEO_DRIVER_X11_XINPUT
314 data = (X11_MouseData *) mouse->driverdata;
315 if (xevent.type == data->motion) {
316 XDeviceMotionEvent *move =
317 (XDeviceMotionEvent *) & xevent;
319 printf("X11 motion: %d,%d\n", move->x, move->y);
321 SDL_SendMouseMotion(move->deviceid, 0, move->x, move->y,
325 if (xevent.type == data->button_pressed) {
326 XDeviceButtonPressedEvent *pressed =
327 (XDeviceButtonPressedEvent *) & xevent;
328 SDL_SendMouseButton(pressed->deviceid, SDL_PRESSED,
332 if (xevent.type == data->button_released) {
333 XDeviceButtonReleasedEvent *released =
334 (XDeviceButtonReleasedEvent *) & xevent;
335 SDL_SendMouseButton(released->deviceid, SDL_RELEASED,
339 if (xevent.type == data->proximity_in) {
340 XProximityNotifyEvent *proximity =
341 (XProximityNotifyEvent *) & xevent;
342 SDL_SendProximity(proximity->deviceid, proximity->x,
343 proximity->y, SDL_PROXIMITYIN);
346 if (xevent.type == data->proximity_out) {
347 XProximityNotifyEvent *proximity =
348 (XProximityNotifyEvent *) & xevent;
349 SDL_SendProximity(proximity->deviceid, proximity->x,
350 proximity->y, SDL_PROXIMITYOUT);
356 printf("Unhandled event %d\n", xevent.type);
363 /* Ack! XPending() actually performs a blocking read if no events available */
365 X11_Pending(Display * display)
367 /* Flush the display connection and look to see if events are queued */
369 if (XEventsQueued(display, QueuedAlready)) {
373 /* More drastic measures are required -- see if X is ready to talk */
375 static struct timeval zero_time; /* static == 0 */
379 x11_fd = ConnectionNumber(display);
381 FD_SET(x11_fd, &fdset);
382 if (select(x11_fd + 1, &fdset, NULL, NULL, &zero_time) == 1) {
383 return (XPending(display));
387 /* Oh well, nothing is ready .. */
392 X11_PumpEvents(_THIS)
394 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
396 /* Update activity every 30 seconds to prevent screensaver */
397 if (_this->suspend_screensaver) {
398 Uint32 now = SDL_GetTicks();
399 if (!data->screensaver_activity ||
400 (int) (now - data->screensaver_activity) >= 30000) {
401 XResetScreenSaver(data->display);
402 data->screensaver_activity = now;
406 /* Keep processing pending events */
407 while (X11_Pending(data->display)) {
408 X11_DispatchEvent(_this);
412 /* This is so wrong it hurts */
413 #define GNOME_SCREENSAVER_HACK
414 #ifdef GNOME_SCREENSAVER_HACK
416 static pid_t screensaver_inhibit_pid;
418 gnome_screensaver_disable()
420 screensaver_inhibit_pid = fork();
421 if (screensaver_inhibit_pid == 0) {
425 execl("/usr/bin/gnome-screensaver-command",
426 "gnome-screensaver-command",
429 "GNOME screensaver doesn't respect MIT-SCREEN-SAVER", NULL);
434 gnome_screensaver_enable()
436 kill(screensaver_inhibit_pid, 15);
441 X11_SuspendScreenSaver(_THIS)
443 #if SDL_VIDEO_DRIVER_X11_SCRNSAVER
444 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
446 int major_version, minor_version;
448 if (SDL_X11_HAVE_XSS) {
449 /* XScreenSaverSuspend was introduced in MIT-SCREEN-SAVER 1.1 */
450 if (!XScreenSaverQueryExtension(data->display, &dummy, &dummy) ||
451 !XScreenSaverQueryVersion(data->display,
452 &major_version, &minor_version) ||
453 major_version < 1 || (major_version == 1 && minor_version < 1)) {
457 XScreenSaverSuspend(data->display, _this->suspend_screensaver);
458 XResetScreenSaver(data->display);
462 #ifdef GNOME_SCREENSAVER_HACK
463 if (_this->suspend_screensaver) {
464 gnome_screensaver_disable();
466 gnome_screensaver_enable();
471 /* vi: set ts=4 sw=4 expandtab: */