1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 2009 Carlos Garnacho <carlosg@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 #include "gdkx11device-core.h"
21 #include "gdkdeviceprivate.h"
23 #include "gdkinternals.h"
24 #include "gdkwindow.h"
25 #include "gdkprivate-x11.h"
28 struct _GdkX11DeviceCore
30 GdkDevice parent_instance;
33 struct _GdkX11DeviceCoreClass
35 GdkDeviceClass parent_class;
38 static gboolean gdk_x11_device_core_get_history (GdkDevice *device,
42 GdkTimeCoord ***events,
44 static void gdk_x11_device_core_get_state (GdkDevice *device,
47 GdkModifierType *mask);
48 static void gdk_x11_device_core_set_window_cursor (GdkDevice *device,
51 static void gdk_x11_device_core_warp (GdkDevice *device,
55 static void gdk_x11_device_core_query_state (GdkDevice *device,
57 GdkWindow **root_window,
58 GdkWindow **child_window,
63 GdkModifierType *mask);
64 static GdkGrabStatus gdk_x11_device_core_grab (GdkDevice *device,
66 gboolean owner_events,
67 GdkEventMask event_mask,
68 GdkWindow *confine_to,
71 static void gdk_x11_device_core_ungrab (GdkDevice *device,
73 static GdkWindow * gdk_x11_device_core_window_at_position (GdkDevice *device,
76 GdkModifierType *mask,
77 gboolean get_toplevel);
78 static void gdk_x11_device_core_select_window_events (GdkDevice *device,
80 GdkEventMask event_mask);
82 G_DEFINE_TYPE (GdkX11DeviceCore, gdk_x11_device_core, GDK_TYPE_DEVICE)
85 gdk_x11_device_core_class_init (GdkX11DeviceCoreClass *klass)
87 GdkDeviceClass *device_class = GDK_DEVICE_CLASS (klass);
89 device_class->get_history = gdk_x11_device_core_get_history;
90 device_class->get_state = gdk_x11_device_core_get_state;
91 device_class->set_window_cursor = gdk_x11_device_core_set_window_cursor;
92 device_class->warp = gdk_x11_device_core_warp;
93 device_class->query_state = gdk_x11_device_core_query_state;
94 device_class->grab = gdk_x11_device_core_grab;
95 device_class->ungrab = gdk_x11_device_core_ungrab;
96 device_class->window_at_position = gdk_x11_device_core_window_at_position;
97 device_class->select_window_events = gdk_x11_device_core_select_window_events;
101 gdk_x11_device_core_init (GdkX11DeviceCore *device_core)
105 device = GDK_DEVICE (device_core);
107 _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_X, 0, 0, 1);
108 _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_Y, 0, 0, 1);
112 impl_coord_in_window (GdkWindow *window,
116 if (impl_x < window->abs_x ||
117 impl_x >= window->abs_x + window->width)
120 if (impl_y < window->abs_y ||
121 impl_y >= window->abs_y + window->height)
128 gdk_x11_device_core_get_history (GdkDevice *device,
132 GdkTimeCoord ***events,
136 GdkTimeCoord **coords;
137 GdkWindow *impl_window;
141 impl_window = _gdk_window_get_impl_window (window);
142 xcoords = XGetMotionEvents (GDK_WINDOW_XDISPLAY (window),
143 GDK_WINDOW_XID (impl_window),
144 start, stop, &tmp_n_events);
148 coords = _gdk_device_allocate_history (device, tmp_n_events);
150 for (i = 0, j = 0; i < tmp_n_events; i++)
152 if (impl_coord_in_window (window, xcoords[i].x, xcoords[i].y))
154 coords[j]->time = xcoords[i].time;
155 coords[j]->axes[0] = xcoords[i].x - window->abs_x;
156 coords[j]->axes[1] = xcoords[i].y - window->abs_y;
163 /* free the events we allocated too much */
164 for (i = j; i < tmp_n_events; i++)
172 if (tmp_n_events == 0)
174 gdk_device_free_history (coords, tmp_n_events);
179 *n_events = tmp_n_events;
184 gdk_device_free_history (coords, tmp_n_events);
190 gdk_x11_device_core_get_state (GdkDevice *device,
193 GdkModifierType *mask)
197 gdk_window_get_device_position (window, device, &x_int, &y_int, mask);
207 gdk_x11_device_core_set_window_cursor (GdkDevice *device,
216 xcursor = gdk_x11_cursor_get_xcursor (cursor);
218 XDefineCursor (GDK_WINDOW_XDISPLAY (window),
219 GDK_WINDOW_XID (window),
224 gdk_x11_device_core_warp (GdkDevice *device,
232 xdisplay = GDK_DISPLAY_XDISPLAY (gdk_device_get_display (device));
233 dest = GDK_WINDOW_XID (gdk_screen_get_root_window (screen));
235 XWarpPointer (xdisplay, None, dest, 0, 0, 0, 0, x, y);
239 gdk_x11_device_core_query_state (GdkDevice *device,
241 GdkWindow **root_window,
242 GdkWindow **child_window,
247 GdkModifierType *mask)
250 GdkScreen *default_screen;
251 Window xroot_window, xchild_window;
252 int xroot_x, xroot_y, xwin_x, xwin_y;
255 display = gdk_window_get_display (window);
256 default_screen = gdk_display_get_default_screen (display);
258 if (!GDK_X11_DISPLAY (display)->trusted_client ||
259 !XQueryPointer (GDK_WINDOW_XDISPLAY (window),
260 GDK_WINDOW_XID (window),
267 XSetWindowAttributes attributes;
271 /* FIXME: untrusted clients not multidevice-safe */
272 xdisplay = GDK_SCREEN_XDISPLAY (default_screen);
273 xwindow = GDK_SCREEN_XROOTWIN (default_screen);
275 w = XCreateWindow (xdisplay, xwindow, 0, 0, 1, 1, 0,
276 CopyFromParent, InputOnly, CopyFromParent,
278 XQueryPointer (xdisplay, w,
284 XDestroyWindow (xdisplay, w);
288 *root_window = gdk_x11_window_lookup_for_display (display, xroot_window);
291 *child_window = gdk_x11_window_lookup_for_display (display, xchild_window);
310 gdk_x11_device_core_grab (GdkDevice *device,
312 gboolean owner_events,
313 GdkEventMask event_mask,
314 GdkWindow *confine_to,
319 Window xwindow, xconfine_to;
322 display = gdk_device_get_display (device);
324 xwindow = GDK_WINDOW_XID (window);
327 confine_to = _gdk_window_get_impl_window (confine_to);
329 if (!confine_to || GDK_WINDOW_DESTROYED (confine_to))
332 xconfine_to = GDK_WINDOW_XID (confine_to);
334 #ifdef G_ENABLE_DEBUG
335 if (_gdk_debug_flags & GDK_DEBUG_NOGRABS)
336 status = GrabSuccess;
339 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
341 /* Device is a keyboard */
342 status = XGrabKeyboard (GDK_DISPLAY_XDISPLAY (display),
345 GrabModeAsync, GrabModeAsync,
354 /* Device is a pointer */
359 _gdk_x11_cursor_update_theme (cursor);
360 xcursor = gdk_x11_cursor_get_xcursor (cursor);
365 for (i = 0; i < _gdk_x11_event_mask_table_size; i++)
367 if (event_mask & (1 << (i + 1)))
368 xevent_mask |= _gdk_x11_event_mask_table[i];
371 /* We don't want to set a native motion hint mask, as we're emulating motion
372 * hints. If we set a native one we just wouldn't get any events.
374 xevent_mask &= ~PointerMotionHintMask;
376 status = XGrabPointer (GDK_DISPLAY_XDISPLAY (display),
380 GrabModeAsync, GrabModeAsync,
386 _gdk_x11_display_update_grab_info (display, device, status);
388 return _gdk_x11_convert_grab_status (status);
392 gdk_x11_device_core_ungrab (GdkDevice *device,
398 display = gdk_device_get_display (device);
399 serial = NextRequest (GDK_DISPLAY_XDISPLAY (display));
401 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
402 XUngrabKeyboard (GDK_DISPLAY_XDISPLAY (display), time_);
404 XUngrabPointer (GDK_DISPLAY_XDISPLAY (display), time_);
406 _gdk_x11_display_update_grab_info_ungrab (display, device, time_, serial);
410 gdk_x11_device_core_window_at_position (GdkDevice *device,
413 GdkModifierType *mask,
414 gboolean get_toplevel)
420 Window xwindow, root, child, last;
421 int xroot_x, xroot_y, xwin_x, xwin_y;
425 display = gdk_device_get_display (device);
426 screen = gdk_display_get_default_screen (display);
428 /* This function really only works if the mouse pointer is held still
429 * during its operation. If it moves from one leaf window to another
430 * than we'll end up with inaccurate values for win_x, win_y
433 gdk_x11_display_grab (display);
435 xdisplay = GDK_SCREEN_XDISPLAY (screen);
436 xwindow = GDK_SCREEN_XROOTWIN (screen);
438 if (G_LIKELY (GDK_X11_DISPLAY (display)->trusted_client))
440 XQueryPointer (xdisplay, xwindow,
453 gint i, screens, width, height;
454 GList *toplevels, *list;
455 Window pointer_window, root, child;
456 int rootx = -1, rooty = -1;
460 /* FIXME: untrusted clients case not multidevice-safe */
461 pointer_window = None;
462 screens = gdk_display_get_n_screens (display);
464 for (i = 0; i < screens; ++i)
466 screen = gdk_display_get_screen (display, i);
467 toplevels = gdk_screen_get_toplevel_windows (screen);
468 for (list = toplevels; list != NULL; list = g_list_next (list))
470 window = GDK_WINDOW (list->data);
471 xwindow = GDK_WINDOW_XID (window);
472 gdk_x11_display_error_trap_push (display);
473 XQueryPointer (xdisplay, xwindow,
478 if (gdk_x11_display_error_trap_pop (display))
482 pointer_window = child;
485 gdk_window_get_geometry (window, NULL, NULL, &width, &height);
486 if (winx >= 0 && winy >= 0 && winx < width && winy < height)
488 /* A childless toplevel, or below another window? */
489 XSetWindowAttributes attributes;
492 w = XCreateWindow (xdisplay, xwindow, winx, winy, 1, 1, 0,
493 CopyFromParent, InputOnly, CopyFromParent,
495 XMapWindow (xdisplay, w);
496 XQueryPointer (xdisplay, xwindow,
501 XDestroyWindow (xdisplay, w);
504 pointer_window = xwindow;
510 g_list_free (toplevels);
511 if (pointer_window != None)
515 xwindow = pointer_window;
521 gdk_x11_display_error_trap_push (display);
522 XQueryPointer (xdisplay, xwindow,
527 if (gdk_x11_display_error_trap_pop (display))
530 if (get_toplevel && last != root &&
531 (window = gdk_x11_window_lookup_for_display (display, last)) != NULL &&
532 window->window_type != GDK_WINDOW_FOREIGN)
539 gdk_x11_display_ungrab (display);
541 window = gdk_x11_window_lookup_for_display (display, last);
544 *win_x = (window) ? xwin_x : -1;
547 *win_y = (window) ? xwin_y : -1;
556 gdk_x11_device_core_select_window_events (GdkDevice *device,
558 GdkEventMask event_mask)
560 GdkEventMask filter_mask, window_mask;
564 window_mask = gdk_window_get_events (window);
565 filter_mask = (GDK_POINTER_MOTION_MASK &
566 GDK_POINTER_MOTION_HINT_MASK &
567 GDK_BUTTON_MOTION_MASK &
568 GDK_BUTTON1_MOTION_MASK &
569 GDK_BUTTON2_MOTION_MASK &
570 GDK_BUTTON3_MOTION_MASK &
571 GDK_BUTTON_PRESS_MASK &
572 GDK_BUTTON_RELEASE_MASK &
574 GDK_KEY_RELEASE_MASK &
575 GDK_ENTER_NOTIFY_MASK &
576 GDK_LEAVE_NOTIFY_MASK &
577 GDK_FOCUS_CHANGE_MASK &
578 GDK_PROXIMITY_IN_MASK &
579 GDK_PROXIMITY_OUT_MASK &
582 /* Filter out non-device events */
583 event_mask &= filter_mask;
585 /* Unset device events on window mask */
586 window_mask &= ~(filter_mask);
589 event_mask |= window_mask;
591 for (i = 0; i < _gdk_x11_event_mask_table_size; i++)
593 if (event_mask & (1 << (i + 1)))
594 xmask |= _gdk_x11_event_mask_table[i];
597 if (GDK_WINDOW_XID (window) != GDK_WINDOW_XROOTWIN (window))
598 xmask |= StructureNotifyMask | PropertyChangeMask;
600 XSelectInput (GDK_WINDOW_XDISPLAY (window),
601 GDK_WINDOW_XID (window),