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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "gdkdevice-xi2.h"
26 #include "gdkprivate-x11.h"
28 #include <X11/extensions/XInput2.h>
30 struct _GdkDeviceXI2Private
35 static void gdk_device_xi2_get_property (GObject *object,
39 static void gdk_device_xi2_set_property (GObject *object,
44 static void gdk_device_xi2_get_state (GdkDevice *device,
47 GdkModifierType *mask);
48 static void gdk_device_xi2_set_window_cursor (GdkDevice *device,
51 static void gdk_device_xi2_warp (GdkDevice *device,
55 static gboolean gdk_device_xi2_query_state (GdkDevice *device,
57 GdkWindow **root_window,
58 GdkWindow **child_window,
63 GdkModifierType *mask);
65 static GdkGrabStatus gdk_device_xi2_grab (GdkDevice *device,
67 gboolean owner_events,
68 GdkEventMask event_mask,
69 GdkWindow *confine_to,
72 static void gdk_device_xi2_ungrab (GdkDevice *device,
75 static GdkWindow * gdk_device_xi2_window_at_position (GdkDevice *device,
78 GdkModifierType *mask,
79 gboolean get_toplevel);
80 static void gdk_device_xi2_select_window_events (GdkDevice *device,
82 GdkEventMask event_mask);
85 G_DEFINE_TYPE (GdkDeviceXI2, gdk_device_xi2, GDK_TYPE_DEVICE)
93 gdk_device_xi2_class_init (GdkDeviceXI2Class *klass)
95 GObjectClass *object_class = G_OBJECT_CLASS (klass);
96 GdkDeviceClass *device_class = GDK_DEVICE_CLASS (klass);
98 object_class->get_property = gdk_device_xi2_get_property;
99 object_class->set_property = gdk_device_xi2_set_property;
101 device_class->get_state = gdk_device_xi2_get_state;
102 device_class->set_window_cursor = gdk_device_xi2_set_window_cursor;
103 device_class->warp = gdk_device_xi2_warp;
104 device_class->query_state = gdk_device_xi2_query_state;
105 device_class->grab = gdk_device_xi2_grab;
106 device_class->ungrab = gdk_device_xi2_ungrab;
107 device_class->window_at_position = gdk_device_xi2_window_at_position;
108 device_class->select_window_events = gdk_device_xi2_select_window_events;
110 g_object_class_install_property (object_class,
112 g_param_spec_int ("device-id",
114 P_("Device identifier"),
116 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
118 g_type_class_add_private (object_class, sizeof (GdkDeviceXI2Private));
122 gdk_device_xi2_init (GdkDeviceXI2 *device)
124 GdkDeviceXI2Private *priv;
126 device->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (device,
128 GdkDeviceXI2Private);
132 gdk_device_xi2_get_property (GObject *object,
137 GdkDeviceXI2Private *priv;
139 priv = GDK_DEVICE_XI2 (object)->priv;
144 g_value_set_int (value, priv->device_id);
147 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
153 gdk_device_xi2_set_property (GObject *object,
158 GdkDeviceXI2Private *priv;
160 priv = GDK_DEVICE_XI2 (object)->priv;
165 priv->device_id = g_value_get_int (value);
168 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174 gdk_device_xi2_get_state (GdkDevice *device,
177 GdkModifierType *mask)
179 GdkDeviceXI2Private *priv;
184 priv = GDK_DEVICE_XI2 (device)->priv;
185 display = gdk_device_get_display (device);
189 info = XIQueryDevice(GDK_DISPLAY_XDISPLAY (display),
190 priv->device_id, &ndevices);
192 for (i = 0, j = 0; i < info->num_classes; i++)
194 XIAnyClassInfo *class_info = info->classes[i];
198 if (class_info->type != XIValuatorClass)
201 value = ((XIValuatorClassInfo *) class_info)->value;
202 use = _gdk_device_get_axis_use (device, j);
208 case GDK_AXIS_IGNORE:
209 if (gdk_device_get_mode (device) == GDK_MODE_WINDOW)
210 _gdk_device_translate_window_coord (device, window, j, value, &axes[j]);
215 /* FIXME: Maybe root coords chaching should happen here */
216 gdk_window_get_origin (window, &root_x, &root_y);
217 _gdk_device_translate_screen_coord (device, window,
224 _gdk_device_translate_axis (device, j, value, &axes[j]);
231 XIFreeDeviceInfo (info);
235 gdk_device_xi2_query_state (device, window,
243 gdk_device_xi2_set_window_cursor (GdkDevice *device,
247 GdkDeviceXI2Private *priv;
248 GdkCursorPrivate *cursor_private;
250 priv = GDK_DEVICE_XI2 (device)->priv;
252 /* Non-master devices don't have a cursor */
253 if (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER)
258 cursor_private = (GdkCursorPrivate*) cursor;
260 XIDefineCursor (GDK_WINDOW_XDISPLAY (window),
262 GDK_WINDOW_XID (window),
263 cursor_private->xcursor);
266 XIUndefineCursor (GDK_WINDOW_XDISPLAY (window),
268 GDK_WINDOW_XID (window));
272 gdk_device_xi2_warp (GdkDevice *device,
277 GdkDeviceXI2Private *priv;
280 priv = GDK_DEVICE_XI2 (device)->priv;
281 dest = GDK_WINDOW_XID (gdk_screen_get_root_window (screen));
283 XIWarpPointer (GDK_SCREEN_XDISPLAY (screen),
290 gdk_device_xi2_query_state (GdkDevice *device,
292 GdkWindow **root_window,
293 GdkWindow **child_window,
298 GdkModifierType *mask)
301 GdkScreen *default_screen;
302 GdkDeviceXI2Private *priv;
303 Window xroot_window, xchild_window;
304 gdouble xroot_x, xroot_y, xwin_x, xwin_y;
305 XIButtonState button_state;
306 XIModifierState mod_state;
307 XIGroupState group_state;
309 if (!window || GDK_WINDOW_DESTROYED (window))
312 priv = GDK_DEVICE_XI2 (device)->priv;
313 display = gdk_window_get_display (window);
314 default_screen = gdk_display_get_default_screen (display);
316 if (G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
318 if (!XIQueryPointer (GDK_WINDOW_XDISPLAY (window),
320 GDK_WINDOW_XID (window),
334 XSetWindowAttributes attributes;
338 /* FIXME: untrusted clients not multidevice-safe */
339 xdisplay = GDK_SCREEN_XDISPLAY (default_screen);
340 xwindow = GDK_SCREEN_XROOTWIN (default_screen);
342 w = XCreateWindow (xdisplay, xwindow, 0, 0, 1, 1, 0,
343 CopyFromParent, InputOnly, CopyFromParent,
345 XIQueryPointer (xdisplay, priv->device_id,
356 XDestroyWindow (xdisplay, w);
360 *root_window = gdk_x11_window_lookup_for_display (display, xroot_window);
363 *child_window = gdk_x11_window_lookup_for_display (display, xchild_window);
366 *root_x = (gint) xroot_x;
369 *root_y = (gint) xroot_y;
372 *win_x = (gint) xwin_x;
375 *win_y = (gint) xwin_y;
378 *mask = gdk_device_xi2_translate_state (&mod_state, &button_state);
384 gdk_device_xi2_grab (GdkDevice *device,
386 gboolean owner_events,
387 GdkEventMask event_mask,
388 GdkWindow *confine_to,
392 GdkDeviceXI2Private *priv;
399 priv = GDK_DEVICE_XI2 (device)->priv;
400 display = gdk_device_get_display (device);
402 /* FIXME: confine_to is actually unused */
404 xwindow = GDK_WINDOW_XID (window);
410 _gdk_x11_cursor_update_theme (cursor);
411 xcursor = ((GdkCursorPrivate *) cursor)->xcursor;
414 mask.deviceid = priv->device_id;
415 mask.mask = gdk_device_xi2_translate_event_mask (event_mask, &mask.mask_len);
417 #ifdef G_ENABLE_DEBUG
418 if (_gdk_debug_flags & GDK_DEBUG_NOGRABS)
419 status = GrabSuccess;
422 status = XIGrabDevice (GDK_DISPLAY_XDISPLAY (display),
427 GrabModeAsync, GrabModeAsync,
433 _gdk_x11_display_update_grab_info (display, device, status);
435 return _gdk_x11_convert_grab_status (status);
439 gdk_device_xi2_ungrab (GdkDevice *device,
442 GdkDeviceXI2Private *priv;
446 priv = GDK_DEVICE_XI2 (device)->priv;
447 display = gdk_device_get_display (device);
448 serial = NextRequest (GDK_DISPLAY_XDISPLAY (display));
450 XIUngrabDevice (GDK_DISPLAY_XDISPLAY (display), priv->device_id, time_);
452 _gdk_x11_display_update_grab_info_ungrab (display, device, time_, serial);
456 gdk_device_xi2_window_at_position (GdkDevice *device,
459 GdkModifierType *mask,
460 gboolean get_toplevel)
462 GdkDeviceXI2Private *priv;
467 Window xwindow, root, child, last = None;
468 gdouble xroot_x, xroot_y, xwin_x, xwin_y;
469 XIButtonState button_state;
470 XIModifierState mod_state;
471 XIGroupState group_state;
473 priv = GDK_DEVICE_XI2 (device)->priv;
474 display = gdk_device_get_display (device);
475 screen = gdk_display_get_default_screen (display);
477 /* This function really only works if the mouse pointer is held still
478 * during its operation. If it moves from one leaf window to another
479 * than we'll end up with inaccurate values for win_x, win_y
482 gdk_x11_display_grab (display);
484 xdisplay = GDK_SCREEN_XDISPLAY (screen);
485 xwindow = GDK_SCREEN_XROOTWIN (screen);
487 if (G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
489 XIQueryPointer (xdisplay,
506 gint i, screens, width, height;
507 GList *toplevels, *list;
508 Window pointer_window, root, child;
510 /* FIXME: untrusted clients case not multidevice-safe */
511 pointer_window = None;
512 screens = gdk_display_get_n_screens (display);
514 for (i = 0; i < screens; ++i)
516 screen = gdk_display_get_screen (display, i);
517 toplevels = gdk_screen_get_toplevel_windows (screen);
518 for (list = toplevels; list != NULL; list = g_list_next (list))
520 window = GDK_WINDOW (list->data);
521 xwindow = GDK_WINDOW_XID (window);
522 gdk_x11_display_error_trap_push (display);
523 XIQueryPointer (xdisplay,
532 if (gdk_x11_display_error_trap_pop (display))
536 pointer_window = child;
539 gdk_window_get_geometry (window, NULL, NULL, &width, &height);
540 if (xwin_x >= 0 && xwin_y >= 0 && xwin_x < width && xwin_y < height)
542 /* A childless toplevel, or below another window? */
543 XSetWindowAttributes attributes;
546 w = XCreateWindow (xdisplay, xwindow, (int)xwin_x, (int)xwin_y, 1, 1, 0,
547 CopyFromParent, InputOnly, CopyFromParent,
549 XMapWindow (xdisplay, w);
550 XIQueryPointer (xdisplay,
559 XDestroyWindow (xdisplay, w);
562 pointer_window = xwindow;
568 g_list_free (toplevels);
569 if (pointer_window != None)
573 xwindow = pointer_window;
579 gdk_x11_display_error_trap_push (display);
580 XIQueryPointer (xdisplay,
589 if (gdk_x11_display_error_trap_pop (display))
592 if (get_toplevel && last != root &&
593 (window = gdk_x11_window_lookup_for_display (display, last)) != NULL &&
594 GDK_WINDOW_TYPE (window) != GDK_WINDOW_FOREIGN)
601 gdk_x11_display_ungrab (display);
603 window = gdk_x11_window_lookup_for_display (display, last);
606 *win_x = (window) ? (gint) xwin_x : -1;
609 *win_y = (window) ? (gint) xwin_y : -1;
612 *mask = gdk_device_xi2_translate_state (&mod_state, &button_state);
618 gdk_device_xi2_select_window_events (GdkDevice *device,
620 GdkEventMask event_mask)
622 GdkDeviceXI2Private *priv;
625 priv = GDK_DEVICE_XI2 (device)->priv;
627 evmask.deviceid = priv->device_id;
628 evmask.mask = gdk_device_xi2_translate_event_mask (event_mask, &evmask.mask_len);
630 XISelectEvents (GDK_WINDOW_XDISPLAY (window),
631 GDK_WINDOW_XID (window),
634 g_free (evmask.mask);
638 gdk_device_xi2_translate_event_mask (GdkEventMask event_mask,
643 *len = XIMaskLen (XI_LASTEVENT);
644 mask = g_new0 (guchar, *len);
646 if (event_mask & GDK_POINTER_MOTION_MASK ||
647 event_mask & GDK_POINTER_MOTION_HINT_MASK)
648 XISetMask (mask, XI_Motion);
650 if (event_mask & GDK_BUTTON_MOTION_MASK ||
651 event_mask & GDK_BUTTON1_MOTION_MASK ||
652 event_mask & GDK_BUTTON2_MOTION_MASK ||
653 event_mask & GDK_BUTTON3_MOTION_MASK)
655 XISetMask (mask, XI_ButtonPress);
656 XISetMask (mask, XI_ButtonRelease);
657 XISetMask (mask, XI_Motion);
660 if (event_mask & GDK_SCROLL_MASK)
662 XISetMask (mask, XI_ButtonPress);
663 XISetMask (mask, XI_ButtonRelease);
666 if (event_mask & GDK_BUTTON_PRESS_MASK)
667 XISetMask (mask, XI_ButtonPress);
669 if (event_mask & GDK_BUTTON_RELEASE_MASK)
670 XISetMask (mask, XI_ButtonRelease);
672 if (event_mask & GDK_KEY_PRESS_MASK)
673 XISetMask (mask, XI_KeyPress);
675 if (event_mask & GDK_KEY_RELEASE_MASK)
676 XISetMask (mask, XI_KeyRelease);
678 if (event_mask & GDK_ENTER_NOTIFY_MASK)
679 XISetMask (mask, XI_Enter);
681 if (event_mask & GDK_LEAVE_NOTIFY_MASK)
682 XISetMask (mask, XI_Leave);
684 if (event_mask & GDK_FOCUS_CHANGE_MASK)
686 XISetMask (mask, XI_FocusIn);
687 XISetMask (mask, XI_FocusOut);
694 gdk_device_xi2_translate_state (XIModifierState *mods_state,
695 XIButtonState *buttons_state)
700 state = (guint) mods_state->effective;
706 /* We're only interested in the first 5 buttons */
707 len = MIN (5, buttons_state->mask_len * 8);
709 for (i = 0; i < len; i++)
711 if (!XIMaskIsSet (buttons_state->mask, i))
717 state |= GDK_BUTTON1_MASK;
720 state |= GDK_BUTTON2_MASK;
723 state |= GDK_BUTTON3_MASK;
726 state |= GDK_BUTTON4_MASK;
729 state |= GDK_BUTTON5_MASK;