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-xi2.h"
21 #include "gdkdeviceprivate.h"
25 #include "gdkprivate-x11.h"
29 #include <X11/Xutil.h>
30 #include <X11/extensions/XInput2.h>
33 typedef struct _ScrollValuator ScrollValuator;
35 struct _ScrollValuator
39 guint last_value_valid : 1;
44 struct _GdkX11DeviceXI2
46 GdkDevice parent_instance;
49 GArray *scroll_valuators;
52 struct _GdkX11DeviceXI2Class
54 GdkDeviceClass parent_class;
57 G_DEFINE_TYPE (GdkX11DeviceXI2, gdk_x11_device_xi2, GDK_TYPE_DEVICE)
60 static void gdk_x11_device_xi2_finalize (GObject *object);
61 static void gdk_x11_device_xi2_get_property (GObject *object,
65 static void gdk_x11_device_xi2_set_property (GObject *object,
70 static void gdk_x11_device_xi2_get_state (GdkDevice *device,
73 GdkModifierType *mask);
74 static void gdk_x11_device_xi2_set_window_cursor (GdkDevice *device,
77 static void gdk_x11_device_xi2_warp (GdkDevice *device,
81 static void gdk_x11_device_xi2_query_state (GdkDevice *device,
83 GdkWindow **root_window,
84 GdkWindow **child_window,
89 GdkModifierType *mask);
91 static GdkGrabStatus gdk_x11_device_xi2_grab (GdkDevice *device,
93 gboolean owner_events,
94 GdkEventMask event_mask,
95 GdkWindow *confine_to,
98 static void gdk_x11_device_xi2_ungrab (GdkDevice *device,
101 static GdkWindow * gdk_x11_device_xi2_window_at_position (GdkDevice *device,
104 GdkModifierType *mask,
105 gboolean get_toplevel);
106 static void gdk_x11_device_xi2_select_window_events (GdkDevice *device,
108 GdkEventMask event_mask);
117 gdk_x11_device_xi2_class_init (GdkX11DeviceXI2Class *klass)
119 GObjectClass *object_class = G_OBJECT_CLASS (klass);
120 GdkDeviceClass *device_class = GDK_DEVICE_CLASS (klass);
122 object_class->finalize = gdk_x11_device_xi2_finalize;
123 object_class->get_property = gdk_x11_device_xi2_get_property;
124 object_class->set_property = gdk_x11_device_xi2_set_property;
126 device_class->get_state = gdk_x11_device_xi2_get_state;
127 device_class->set_window_cursor = gdk_x11_device_xi2_set_window_cursor;
128 device_class->warp = gdk_x11_device_xi2_warp;
129 device_class->query_state = gdk_x11_device_xi2_query_state;
130 device_class->grab = gdk_x11_device_xi2_grab;
131 device_class->ungrab = gdk_x11_device_xi2_ungrab;
132 device_class->window_at_position = gdk_x11_device_xi2_window_at_position;
133 device_class->select_window_events = gdk_x11_device_xi2_select_window_events;
135 g_object_class_install_property (object_class,
137 g_param_spec_int ("device-id",
139 P_("Device identifier"),
141 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
145 gdk_x11_device_xi2_init (GdkX11DeviceXI2 *device)
147 device->scroll_valuators = g_array_new (FALSE, FALSE, sizeof (ScrollValuator));
151 gdk_x11_device_xi2_finalize (GObject *object)
153 GdkX11DeviceXI2 *device = GDK_X11_DEVICE_XI2 (object);
155 g_array_free (device->scroll_valuators, TRUE);
157 G_OBJECT_CLASS (gdk_x11_device_xi2_parent_class)->finalize (object);
161 gdk_x11_device_xi2_get_property (GObject *object,
166 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (object);
171 g_value_set_int (value, device_xi2->device_id);
174 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
180 gdk_x11_device_xi2_set_property (GObject *object,
185 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (object);
190 device_xi2->device_id = g_value_get_int (value);
193 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
199 gdk_x11_device_xi2_get_state (GdkDevice *device,
202 GdkModifierType *mask)
204 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
212 display = gdk_device_get_display (device);
214 gdk_x11_display_error_trap_push (display);
215 info = XIQueryDevice (GDK_DISPLAY_XDISPLAY (display),
216 device_xi2->device_id, &ndevices);
217 gdk_x11_display_error_trap_pop_ignored (display);
219 for (i = 0, j = 0; info && i < info->num_classes; i++)
221 XIAnyClassInfo *class_info = info->classes[i];
225 if (class_info->type != XIValuatorClass)
228 value = ((XIValuatorClassInfo *) class_info)->value;
229 use = gdk_device_get_axis_use (device, j);
235 case GDK_AXIS_IGNORE:
236 if (gdk_device_get_mode (device) == GDK_MODE_WINDOW)
237 _gdk_device_translate_window_coord (device, window, j, value, &axes[j]);
242 /* FIXME: Maybe root coords chaching should happen here */
243 gdk_window_get_origin (window, &root_x, &root_y);
244 _gdk_device_translate_screen_coord (device, window,
251 _gdk_device_translate_axis (device, j, value, &axes[j]);
259 XIFreeDeviceInfo (info);
263 gdk_x11_device_xi2_query_state (device, window,
271 gdk_x11_device_xi2_set_window_cursor (GdkDevice *device,
275 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
277 /* Non-master devices don't have a cursor */
278 if (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER)
282 XIDefineCursor (GDK_WINDOW_XDISPLAY (window),
283 device_xi2->device_id,
284 GDK_WINDOW_XID (window),
285 gdk_x11_cursor_get_xcursor (cursor));
287 XIUndefineCursor (GDK_WINDOW_XDISPLAY (window),
288 device_xi2->device_id,
289 GDK_WINDOW_XID (window));
293 gdk_x11_device_xi2_warp (GdkDevice *device,
298 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
301 dest = GDK_WINDOW_XID (gdk_screen_get_root_window (screen));
303 XIWarpPointer (GDK_SCREEN_XDISPLAY (screen),
304 device_xi2->device_id,
310 gdk_x11_device_xi2_query_state (GdkDevice *device,
312 GdkWindow **root_window,
313 GdkWindow **child_window,
318 GdkModifierType *mask)
320 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
322 GdkScreen *default_screen;
323 Window xroot_window, xchild_window;
324 gdouble xroot_x, xroot_y, xwin_x, xwin_y;
325 XIButtonState button_state;
326 XIModifierState mod_state;
327 XIGroupState group_state;
329 display = gdk_window_get_display (window);
330 default_screen = gdk_display_get_default_screen (display);
332 if (!GDK_X11_DISPLAY (display)->trusted_client ||
333 !XIQueryPointer (GDK_WINDOW_XDISPLAY (window),
334 device_xi2->device_id,
335 GDK_WINDOW_XID (window),
344 XSetWindowAttributes attributes;
348 /* FIXME: untrusted clients not multidevice-safe */
349 xdisplay = GDK_SCREEN_XDISPLAY (default_screen);
350 xwindow = GDK_SCREEN_XROOTWIN (default_screen);
352 w = XCreateWindow (xdisplay, xwindow, 0, 0, 1, 1, 0,
353 CopyFromParent, InputOnly, CopyFromParent,
355 XIQueryPointer (xdisplay, device_xi2->device_id,
364 XDestroyWindow (xdisplay, w);
368 *root_window = gdk_x11_window_lookup_for_display (display, xroot_window);
371 *child_window = gdk_x11_window_lookup_for_display (display, xchild_window);
374 *root_x = (gint) xroot_x;
377 *root_y = (gint) xroot_y;
380 *win_x = (gint) xwin_x;
383 *win_y = (gint) xwin_y;
386 *mask = _gdk_x11_device_xi2_translate_state (&mod_state, &button_state, &group_state);
388 free (button_state.mask);
392 gdk_x11_device_xi2_grab (GdkDevice *device,
394 gboolean owner_events,
395 GdkEventMask event_mask,
396 GdkWindow *confine_to,
400 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
401 GdkX11DeviceManagerXI2 *device_manager_xi2;
408 display = gdk_device_get_display (device);
409 device_manager_xi2 = GDK_X11_DEVICE_MANAGER_XI2 (gdk_display_get_device_manager (display));
411 /* FIXME: confine_to is actually unused */
413 xwindow = GDK_WINDOW_XID (window);
419 _gdk_x11_cursor_update_theme (cursor);
420 xcursor = gdk_x11_cursor_get_xcursor (cursor);
423 mask.deviceid = device_xi2->device_id;
424 mask.mask = _gdk_x11_device_xi2_translate_event_mask (device_manager_xi2,
428 #ifdef G_ENABLE_DEBUG
429 if (_gdk_debug_flags & GDK_DEBUG_NOGRABS)
430 status = GrabSuccess;
433 status = XIGrabDevice (GDK_DISPLAY_XDISPLAY (display),
434 device_xi2->device_id,
438 GrabModeAsync, GrabModeAsync,
444 _gdk_x11_display_update_grab_info (display, device, status);
446 return _gdk_x11_convert_grab_status (status);
450 gdk_x11_device_xi2_ungrab (GdkDevice *device,
453 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
457 display = gdk_device_get_display (device);
458 serial = NextRequest (GDK_DISPLAY_XDISPLAY (display));
460 XIUngrabDevice (GDK_DISPLAY_XDISPLAY (display), device_xi2->device_id, time_);
462 _gdk_x11_display_update_grab_info_ungrab (display, device, time_, serial);
466 gdk_x11_device_xi2_window_at_position (GdkDevice *device,
469 GdkModifierType *mask,
470 gboolean get_toplevel)
472 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
477 Window xwindow, root, child, last = None;
478 gdouble xroot_x, xroot_y, xwin_x, xwin_y;
479 XIButtonState button_state = { 0 };
480 XIModifierState mod_state;
481 XIGroupState group_state;
483 display = gdk_device_get_display (device);
484 screen = gdk_display_get_default_screen (display);
486 /* This function really only works if the mouse pointer is held still
487 * during its operation. If it moves from one leaf window to another
488 * than we'll end up with inaccurate values for win_x, win_y
491 gdk_x11_display_grab (display);
493 xdisplay = GDK_SCREEN_XDISPLAY (screen);
494 xwindow = GDK_SCREEN_XROOTWIN (screen);
496 if (G_LIKELY (GDK_X11_DISPLAY (display)->trusted_client))
498 XIQueryPointer (xdisplay,
499 device_xi2->device_id,
515 gint i, screens, width, height;
516 GList *toplevels, *list;
517 Window pointer_window, root, child;
519 /* FIXME: untrusted clients case not multidevice-safe */
520 pointer_window = None;
521 screens = gdk_display_get_n_screens (display);
523 for (i = 0; i < screens; ++i)
525 screen = gdk_display_get_screen (display, i);
526 toplevels = gdk_screen_get_toplevel_windows (screen);
527 for (list = toplevels; list != NULL; list = g_list_next (list))
529 window = GDK_WINDOW (list->data);
530 xwindow = GDK_WINDOW_XID (window);
532 /* Free previous button mask, if any */
533 g_free (button_state.mask);
535 gdk_x11_display_error_trap_push (display);
536 XIQueryPointer (xdisplay,
537 device_xi2->device_id,
545 if (gdk_x11_display_error_trap_pop (display))
549 pointer_window = child;
552 gdk_window_get_geometry (window, NULL, NULL, &width, &height);
553 if (xwin_x >= 0 && xwin_y >= 0 && xwin_x < width && xwin_y < height)
555 /* A childless toplevel, or below another window? */
556 XSetWindowAttributes attributes;
559 free (button_state.mask);
561 w = XCreateWindow (xdisplay, xwindow, (int)xwin_x, (int)xwin_y, 1, 1, 0,
562 CopyFromParent, InputOnly, CopyFromParent,
564 XMapWindow (xdisplay, w);
565 XIQueryPointer (xdisplay,
566 device_xi2->device_id,
574 XDestroyWindow (xdisplay, w);
577 pointer_window = xwindow;
583 g_list_free (toplevels);
584 if (pointer_window != None)
588 xwindow = pointer_window;
594 free (button_state.mask);
596 gdk_x11_display_error_trap_push (display);
597 XIQueryPointer (xdisplay,
598 device_xi2->device_id,
606 if (gdk_x11_display_error_trap_pop (display))
609 if (get_toplevel && last != root &&
610 (window = gdk_x11_window_lookup_for_display (display, last)) != NULL &&
611 GDK_WINDOW_TYPE (window) != GDK_WINDOW_FOREIGN)
618 gdk_x11_display_ungrab (display);
620 window = gdk_x11_window_lookup_for_display (display, last);
623 *win_x = (window) ? (gint) xwin_x : -1;
626 *win_y = (window) ? (gint) xwin_y : -1;
629 *mask = _gdk_x11_device_xi2_translate_state (&mod_state, &button_state, &group_state);
631 free (button_state.mask);
637 gdk_x11_device_xi2_select_window_events (GdkDevice *device,
639 GdkEventMask event_mask)
641 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
642 GdkX11DeviceManagerXI2 *device_manager_xi2;
646 display = gdk_device_get_display (device);
647 device_manager_xi2 = GDK_X11_DEVICE_MANAGER_XI2 (gdk_display_get_device_manager (display));
649 evmask.deviceid = device_xi2->device_id;
650 evmask.mask = _gdk_x11_device_xi2_translate_event_mask (device_manager_xi2,
654 XISelectEvents (GDK_WINDOW_XDISPLAY (window),
655 GDK_WINDOW_XID (window),
658 g_free (evmask.mask);
662 _gdk_x11_device_xi2_translate_event_mask (GdkX11DeviceManagerXI2 *device_manager_xi2,
663 GdkEventMask event_mask,
669 g_object_get (device_manager_xi2, "minor", &minor, NULL);
671 *len = XIMaskLen (XI_LASTEVENT);
672 mask = g_new0 (guchar, *len);
674 if (event_mask & GDK_POINTER_MOTION_MASK ||
675 event_mask & GDK_POINTER_MOTION_HINT_MASK)
676 XISetMask (mask, XI_Motion);
678 if (event_mask & GDK_BUTTON_MOTION_MASK ||
679 event_mask & GDK_BUTTON1_MOTION_MASK ||
680 event_mask & GDK_BUTTON2_MOTION_MASK ||
681 event_mask & GDK_BUTTON3_MOTION_MASK)
683 XISetMask (mask, XI_ButtonPress);
684 XISetMask (mask, XI_ButtonRelease);
685 XISetMask (mask, XI_Motion);
688 if (event_mask & GDK_SCROLL_MASK)
690 XISetMask (mask, XI_ButtonPress);
691 XISetMask (mask, XI_ButtonRelease);
694 if (event_mask & GDK_BUTTON_PRESS_MASK)
695 XISetMask (mask, XI_ButtonPress);
697 if (event_mask & GDK_BUTTON_RELEASE_MASK)
698 XISetMask (mask, XI_ButtonRelease);
700 if (event_mask & GDK_KEY_PRESS_MASK)
701 XISetMask (mask, XI_KeyPress);
703 if (event_mask & GDK_KEY_RELEASE_MASK)
704 XISetMask (mask, XI_KeyRelease);
706 if (event_mask & GDK_ENTER_NOTIFY_MASK)
707 XISetMask (mask, XI_Enter);
709 if (event_mask & GDK_LEAVE_NOTIFY_MASK)
710 XISetMask (mask, XI_Leave);
712 if (event_mask & GDK_FOCUS_CHANGE_MASK)
714 XISetMask (mask, XI_FocusIn);
715 XISetMask (mask, XI_FocusOut);
719 /* XInput 2.2 includes multitouch support */
721 event_mask & GDK_TOUCH_MASK)
723 XISetMask (mask, XI_TouchBegin);
724 XISetMask (mask, XI_TouchUpdate);
725 XISetMask (mask, XI_TouchEnd);
727 #endif /* XINPUT_2_2 */
733 _gdk_x11_device_xi2_translate_state (XIModifierState *mods_state,
734 XIButtonState *buttons_state,
735 XIGroupState *group_state)
740 state = mods_state->effective;
746 /* We're only interested in the first 5 buttons */
747 len = MIN (5, buttons_state->mask_len * 8);
749 for (i = 0; i < len; i++)
751 if (!XIMaskIsSet (buttons_state->mask, i))
757 state |= GDK_BUTTON1_MASK;
760 state |= GDK_BUTTON2_MASK;
763 state |= GDK_BUTTON3_MASK;
766 state |= GDK_BUTTON4_MASK;
769 state |= GDK_BUTTON5_MASK;
778 state |= (group_state->effective) << 13;
784 _gdk_x11_device_xi2_add_scroll_valuator (GdkX11DeviceXI2 *device,
786 GdkScrollDirection direction,
789 ScrollValuator scroll;
791 g_return_if_fail (GDK_IS_X11_DEVICE_XI2 (device));
792 g_return_if_fail (n_valuator < gdk_device_get_n_axes (GDK_DEVICE (device)));
794 scroll.n_valuator = n_valuator;
795 scroll.direction = direction;
796 scroll.last_value_valid = FALSE;
797 scroll.increment = increment;
799 g_array_append_val (device->scroll_valuators, scroll);
803 _gdk_x11_device_xi2_get_scroll_delta (GdkX11DeviceXI2 *device,
805 gdouble valuator_value,
806 GdkScrollDirection *direction_ret,
811 g_return_val_if_fail (GDK_IS_X11_DEVICE_XI2 (device), FALSE);
812 g_return_val_if_fail (n_valuator < gdk_device_get_n_axes (GDK_DEVICE (device)), FALSE);
814 for (i = 0; i < device->scroll_valuators->len; i++)
816 ScrollValuator *scroll;
818 scroll = &g_array_index (device->scroll_valuators, ScrollValuator, i);
820 if (scroll->n_valuator == n_valuator)
823 *direction_ret = scroll->direction;
828 if (scroll->last_value_valid)
831 *delta_ret = (valuator_value - scroll->last_value) / scroll->increment;
833 scroll->last_value = valuator_value;
837 scroll->last_value = valuator_value;
838 scroll->last_value_valid = TRUE;
849 _gdk_device_xi2_reset_scroll_valuators (GdkX11DeviceXI2 *device)
853 for (i = 0; i < device->scroll_valuators->len; i++)
855 ScrollValuator *scroll;
857 scroll = &g_array_index (device->scroll_valuators, ScrollValuator, i);
858 scroll->last_value_valid = FALSE;
863 _gdk_x11_device_xi2_get_id (GdkX11DeviceXI2 *device)
865 g_return_val_if_fail (GDK_IS_X11_DEVICE_XI2 (device), 0);
867 return device->device_id;