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 "gdkx11device-xi2.h"
23 #include "gdkdeviceprivate.h"
27 #include "gdkprivate-x11.h"
30 #include <X11/Xutil.h>
31 #include <X11/extensions/XInput2.h>
33 struct _GdkX11DeviceXI2
35 GdkDevice parent_instance;
40 struct _GdkX11DeviceXI2Class
42 GdkDeviceClass parent_class;
45 static void gdk_x11_device_xi2_get_property (GObject *object,
49 static void gdk_x11_device_xi2_set_property (GObject *object,
54 static void gdk_x11_device_xi2_get_state (GdkDevice *device,
57 GdkModifierType *mask);
58 static void gdk_x11_device_xi2_set_window_cursor (GdkDevice *device,
61 static void gdk_x11_device_xi2_warp (GdkDevice *device,
65 static gboolean gdk_x11_device_xi2_query_state (GdkDevice *device,
67 GdkWindow **root_window,
68 GdkWindow **child_window,
73 GdkModifierType *mask);
75 static GdkGrabStatus gdk_x11_device_xi2_grab (GdkDevice *device,
77 gboolean owner_events,
78 GdkEventMask event_mask,
79 GdkWindow *confine_to,
82 static void gdk_x11_device_xi2_ungrab (GdkDevice *device,
85 static GdkWindow * gdk_x11_device_xi2_window_at_position (GdkDevice *device,
88 GdkModifierType *mask,
89 gboolean get_toplevel);
90 static void gdk_x11_device_xi2_select_window_events (GdkDevice *device,
92 GdkEventMask event_mask);
95 G_DEFINE_TYPE (GdkX11DeviceXI2, gdk_x11_device_xi2, GDK_TYPE_DEVICE)
103 gdk_x11_device_xi2_class_init (GdkX11DeviceXI2Class *klass)
105 GObjectClass *object_class = G_OBJECT_CLASS (klass);
106 GdkDeviceClass *device_class = GDK_DEVICE_CLASS (klass);
108 object_class->get_property = gdk_x11_device_xi2_get_property;
109 object_class->set_property = gdk_x11_device_xi2_set_property;
111 device_class->get_state = gdk_x11_device_xi2_get_state;
112 device_class->set_window_cursor = gdk_x11_device_xi2_set_window_cursor;
113 device_class->warp = gdk_x11_device_xi2_warp;
114 device_class->query_state = gdk_x11_device_xi2_query_state;
115 device_class->grab = gdk_x11_device_xi2_grab;
116 device_class->ungrab = gdk_x11_device_xi2_ungrab;
117 device_class->window_at_position = gdk_x11_device_xi2_window_at_position;
118 device_class->select_window_events = gdk_x11_device_xi2_select_window_events;
120 g_object_class_install_property (object_class,
122 g_param_spec_int ("device-id",
124 P_("Device identifier"),
126 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
130 gdk_x11_device_xi2_init (GdkX11DeviceXI2 *device)
135 gdk_x11_device_xi2_get_property (GObject *object,
140 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (object);
145 g_value_set_int (value, device_xi2->device_id);
148 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
154 gdk_x11_device_xi2_set_property (GObject *object,
159 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (object);
164 device_xi2->device_id = g_value_get_int (value);
167 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
173 gdk_x11_device_xi2_get_state (GdkDevice *device,
176 GdkModifierType *mask)
178 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
183 display = gdk_device_get_display (device);
187 info = XIQueryDevice(GDK_DISPLAY_XDISPLAY (display),
188 device_xi2->device_id, &ndevices);
190 for (i = 0, j = 0; i < info->num_classes; i++)
192 XIAnyClassInfo *class_info = info->classes[i];
196 if (class_info->type != XIValuatorClass)
199 value = ((XIValuatorClassInfo *) class_info)->value;
200 use = _gdk_device_get_axis_use (device, j);
206 case GDK_AXIS_IGNORE:
207 if (gdk_device_get_mode (device) == GDK_MODE_WINDOW)
208 _gdk_device_translate_window_coord (device, window, j, value, &axes[j]);
213 /* FIXME: Maybe root coords chaching should happen here */
214 gdk_window_get_origin (window, &root_x, &root_y);
215 _gdk_device_translate_screen_coord (device, window,
222 _gdk_device_translate_axis (device, j, value, &axes[j]);
229 XIFreeDeviceInfo (info);
233 gdk_x11_device_xi2_query_state (device, window,
241 gdk_x11_device_xi2_set_window_cursor (GdkDevice *device,
245 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
247 /* Non-master devices don't have a cursor */
248 if (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER)
252 XIDefineCursor (GDK_WINDOW_XDISPLAY (window),
253 device_xi2->device_id,
254 GDK_WINDOW_XID (window),
255 gdk_x11_cursor_get_xcursor (cursor));
257 XIUndefineCursor (GDK_WINDOW_XDISPLAY (window),
258 device_xi2->device_id,
259 GDK_WINDOW_XID (window));
263 gdk_x11_device_xi2_warp (GdkDevice *device,
268 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
271 dest = GDK_WINDOW_XID (gdk_screen_get_root_window (screen));
273 XIWarpPointer (GDK_SCREEN_XDISPLAY (screen),
274 device_xi2->device_id,
280 gdk_x11_device_xi2_query_state (GdkDevice *device,
282 GdkWindow **root_window,
283 GdkWindow **child_window,
288 GdkModifierType *mask)
290 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
292 GdkScreen *default_screen;
293 Window xroot_window, xchild_window;
294 gdouble xroot_x, xroot_y, xwin_x, xwin_y;
295 XIButtonState button_state;
296 XIModifierState mod_state;
297 XIGroupState group_state;
299 if (!window || GDK_WINDOW_DESTROYED (window))
302 display = gdk_window_get_display (window);
303 default_screen = gdk_display_get_default_screen (display);
305 if (G_LIKELY (GDK_X11_DISPLAY (display)->trusted_client))
307 if (!XIQueryPointer (GDK_WINDOW_XDISPLAY (window),
308 device_xi2->device_id,
309 GDK_WINDOW_XID (window),
321 XSetWindowAttributes attributes;
325 /* FIXME: untrusted clients not multidevice-safe */
326 xdisplay = GDK_SCREEN_XDISPLAY (default_screen);
327 xwindow = GDK_SCREEN_XROOTWIN (default_screen);
329 w = XCreateWindow (xdisplay, xwindow, 0, 0, 1, 1, 0,
330 CopyFromParent, InputOnly, CopyFromParent,
332 XIQueryPointer (xdisplay, device_xi2->device_id,
341 XDestroyWindow (xdisplay, w);
345 *root_window = gdk_x11_window_lookup_for_display (display, xroot_window);
348 *child_window = gdk_x11_window_lookup_for_display (display, xchild_window);
351 *root_x = (gint) xroot_x;
354 *root_y = (gint) xroot_y;
357 *win_x = (gint) xwin_x;
360 *win_y = (gint) xwin_y;
363 *mask = _gdk_x11_device_xi2_translate_state (&mod_state, &button_state);
369 gdk_x11_device_xi2_grab (GdkDevice *device,
371 gboolean owner_events,
372 GdkEventMask event_mask,
373 GdkWindow *confine_to,
377 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
384 display = gdk_device_get_display (device);
386 /* FIXME: confine_to is actually unused */
388 xwindow = GDK_WINDOW_XID (window);
394 _gdk_x11_cursor_update_theme (cursor);
395 xcursor = gdk_x11_cursor_get_xcursor (cursor);
398 mask.deviceid = device_xi2->device_id;
399 mask.mask = _gdk_x11_device_xi2_translate_event_mask (event_mask, &mask.mask_len);
401 #ifdef G_ENABLE_DEBUG
402 if (_gdk_debug_flags & GDK_DEBUG_NOGRABS)
403 status = GrabSuccess;
406 status = XIGrabDevice (GDK_DISPLAY_XDISPLAY (display),
407 device_xi2->device_id,
411 GrabModeAsync, GrabModeAsync,
417 _gdk_x11_display_update_grab_info (display, device, status);
419 return _gdk_x11_convert_grab_status (status);
423 gdk_x11_device_xi2_ungrab (GdkDevice *device,
426 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
430 display = gdk_device_get_display (device);
431 serial = NextRequest (GDK_DISPLAY_XDISPLAY (display));
433 XIUngrabDevice (GDK_DISPLAY_XDISPLAY (display), device_xi2->device_id, time_);
435 _gdk_x11_display_update_grab_info_ungrab (display, device, time_, serial);
439 gdk_x11_device_xi2_window_at_position (GdkDevice *device,
442 GdkModifierType *mask,
443 gboolean get_toplevel)
445 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
450 Window xwindow, root, child, last = None;
451 gdouble xroot_x, xroot_y, xwin_x, xwin_y;
452 XIButtonState button_state;
453 XIModifierState mod_state;
454 XIGroupState group_state;
456 display = gdk_device_get_display (device);
457 screen = gdk_display_get_default_screen (display);
459 /* This function really only works if the mouse pointer is held still
460 * during its operation. If it moves from one leaf window to another
461 * than we'll end up with inaccurate values for win_x, win_y
464 gdk_x11_display_grab (display);
466 xdisplay = GDK_SCREEN_XDISPLAY (screen);
467 xwindow = GDK_SCREEN_XROOTWIN (screen);
469 if (G_LIKELY (GDK_X11_DISPLAY (display)->trusted_client))
471 XIQueryPointer (xdisplay,
472 device_xi2->device_id,
488 gint i, screens, width, height;
489 GList *toplevels, *list;
490 Window pointer_window, root, child;
492 /* FIXME: untrusted clients case not multidevice-safe */
493 pointer_window = None;
494 screens = gdk_display_get_n_screens (display);
496 for (i = 0; i < screens; ++i)
498 screen = gdk_display_get_screen (display, i);
499 toplevels = gdk_screen_get_toplevel_windows (screen);
500 for (list = toplevels; list != NULL; list = g_list_next (list))
502 window = GDK_WINDOW (list->data);
503 xwindow = GDK_WINDOW_XID (window);
504 gdk_x11_display_error_trap_push (display);
505 XIQueryPointer (xdisplay,
506 device_xi2->device_id,
514 if (gdk_x11_display_error_trap_pop (display))
518 pointer_window = child;
521 gdk_window_get_geometry (window, NULL, NULL, &width, &height);
522 if (xwin_x >= 0 && xwin_y >= 0 && xwin_x < width && xwin_y < height)
524 /* A childless toplevel, or below another window? */
525 XSetWindowAttributes attributes;
528 w = XCreateWindow (xdisplay, xwindow, (int)xwin_x, (int)xwin_y, 1, 1, 0,
529 CopyFromParent, InputOnly, CopyFromParent,
531 XMapWindow (xdisplay, w);
532 XIQueryPointer (xdisplay,
533 device_xi2->device_id,
541 XDestroyWindow (xdisplay, w);
544 pointer_window = xwindow;
550 g_list_free (toplevels);
551 if (pointer_window != None)
555 xwindow = pointer_window;
561 gdk_x11_display_error_trap_push (display);
562 XIQueryPointer (xdisplay,
563 device_xi2->device_id,
571 if (gdk_x11_display_error_trap_pop (display))
574 if (get_toplevel && last != root &&
575 (window = gdk_x11_window_lookup_for_display (display, last)) != NULL &&
576 GDK_WINDOW_TYPE (window) != GDK_WINDOW_FOREIGN)
583 gdk_x11_display_ungrab (display);
585 window = gdk_x11_window_lookup_for_display (display, last);
588 *win_x = (window) ? (gint) xwin_x : -1;
591 *win_y = (window) ? (gint) xwin_y : -1;
594 *mask = _gdk_x11_device_xi2_translate_state (&mod_state, &button_state);
600 gdk_x11_device_xi2_select_window_events (GdkDevice *device,
602 GdkEventMask event_mask)
604 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
607 evmask.deviceid = device_xi2->device_id;
608 evmask.mask = _gdk_x11_device_xi2_translate_event_mask (event_mask, &evmask.mask_len);
610 XISelectEvents (GDK_WINDOW_XDISPLAY (window),
611 GDK_WINDOW_XID (window),
614 g_free (evmask.mask);
618 _gdk_x11_device_xi2_translate_event_mask (GdkEventMask event_mask,
623 *len = XIMaskLen (XI_LASTEVENT);
624 mask = g_new0 (guchar, *len);
626 if (event_mask & GDK_POINTER_MOTION_MASK ||
627 event_mask & GDK_POINTER_MOTION_HINT_MASK)
628 XISetMask (mask, XI_Motion);
630 if (event_mask & GDK_BUTTON_MOTION_MASK ||
631 event_mask & GDK_BUTTON1_MOTION_MASK ||
632 event_mask & GDK_BUTTON2_MOTION_MASK ||
633 event_mask & GDK_BUTTON3_MOTION_MASK)
635 XISetMask (mask, XI_ButtonPress);
636 XISetMask (mask, XI_ButtonRelease);
637 XISetMask (mask, XI_Motion);
640 if (event_mask & GDK_SCROLL_MASK)
642 XISetMask (mask, XI_ButtonPress);
643 XISetMask (mask, XI_ButtonRelease);
646 if (event_mask & GDK_BUTTON_PRESS_MASK)
647 XISetMask (mask, XI_ButtonPress);
649 if (event_mask & GDK_BUTTON_RELEASE_MASK)
650 XISetMask (mask, XI_ButtonRelease);
652 if (event_mask & GDK_KEY_PRESS_MASK)
653 XISetMask (mask, XI_KeyPress);
655 if (event_mask & GDK_KEY_RELEASE_MASK)
656 XISetMask (mask, XI_KeyRelease);
658 if (event_mask & GDK_ENTER_NOTIFY_MASK)
659 XISetMask (mask, XI_Enter);
661 if (event_mask & GDK_LEAVE_NOTIFY_MASK)
662 XISetMask (mask, XI_Leave);
664 if (event_mask & GDK_FOCUS_CHANGE_MASK)
666 XISetMask (mask, XI_FocusIn);
667 XISetMask (mask, XI_FocusOut);
674 _gdk_x11_device_xi2_translate_state (XIModifierState *mods_state,
675 XIButtonState *buttons_state)
680 state = (guint) mods_state->effective;
686 /* We're only interested in the first 5 buttons */
687 len = MIN (5, buttons_state->mask_len * 8);
689 for (i = 0; i < len; i++)
691 if (!XIMaskIsSet (buttons_state->mask, i))
697 state |= GDK_BUTTON1_MASK;
700 state |= GDK_BUTTON2_MASK;
703 state |= GDK_BUTTON3_MASK;
706 state |= GDK_BUTTON4_MASK;
709 state |= GDK_BUTTON5_MASK;