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"
29 struct _GdkX11DeviceXI2
31 GdkDevice parent_instance;
36 struct _GdkX11DeviceXI2Class
38 GdkDeviceClass parent_class;
41 static void gdk_x11_device_xi2_get_property (GObject *object,
45 static void gdk_x11_device_xi2_set_property (GObject *object,
50 static void gdk_x11_device_xi2_get_state (GdkDevice *device,
53 GdkModifierType *mask);
54 static void gdk_x11_device_xi2_set_window_cursor (GdkDevice *device,
57 static void gdk_x11_device_xi2_warp (GdkDevice *device,
61 static gboolean gdk_x11_device_xi2_query_state (GdkDevice *device,
63 GdkWindow **root_window,
64 GdkWindow **child_window,
69 GdkModifierType *mask);
71 static GdkGrabStatus gdk_x11_device_xi2_grab (GdkDevice *device,
73 gboolean owner_events,
74 GdkEventMask event_mask,
75 GdkWindow *confine_to,
78 static void gdk_x11_device_xi2_ungrab (GdkDevice *device,
81 static GdkWindow * gdk_x11_device_xi2_window_at_position (GdkDevice *device,
84 GdkModifierType *mask,
85 gboolean get_toplevel);
86 static void gdk_x11_device_xi2_select_window_events (GdkDevice *device,
88 GdkEventMask event_mask);
91 G_DEFINE_TYPE (GdkX11DeviceXI2, gdk_x11_device_xi2, GDK_TYPE_DEVICE)
99 gdk_x11_device_xi2_class_init (GdkX11DeviceXI2Class *klass)
101 GObjectClass *object_class = G_OBJECT_CLASS (klass);
102 GdkDeviceClass *device_class = GDK_DEVICE_CLASS (klass);
104 object_class->get_property = gdk_x11_device_xi2_get_property;
105 object_class->set_property = gdk_x11_device_xi2_set_property;
107 device_class->get_state = gdk_x11_device_xi2_get_state;
108 device_class->set_window_cursor = gdk_x11_device_xi2_set_window_cursor;
109 device_class->warp = gdk_x11_device_xi2_warp;
110 device_class->query_state = gdk_x11_device_xi2_query_state;
111 device_class->grab = gdk_x11_device_xi2_grab;
112 device_class->ungrab = gdk_x11_device_xi2_ungrab;
113 device_class->window_at_position = gdk_x11_device_xi2_window_at_position;
114 device_class->select_window_events = gdk_x11_device_xi2_select_window_events;
116 g_object_class_install_property (object_class,
118 g_param_spec_int ("device-id",
120 P_("Device identifier"),
122 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
126 gdk_x11_device_xi2_init (GdkX11DeviceXI2 *device)
131 gdk_x11_device_xi2_get_property (GObject *object,
136 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (object);
141 g_value_set_int (value, device_xi2->device_id);
144 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
150 gdk_x11_device_xi2_set_property (GObject *object,
155 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (object);
160 device_xi2->device_id = g_value_get_int (value);
163 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
169 gdk_x11_device_xi2_get_state (GdkDevice *device,
172 GdkModifierType *mask)
174 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
179 display = gdk_device_get_display (device);
183 info = XIQueryDevice(GDK_DISPLAY_XDISPLAY (display),
184 device_xi2->device_id, &ndevices);
186 for (i = 0, j = 0; i < info->num_classes; i++)
188 XIAnyClassInfo *class_info = info->classes[i];
192 if (class_info->type != XIValuatorClass)
195 value = ((XIValuatorClassInfo *) class_info)->value;
196 use = _gdk_device_get_axis_use (device, j);
202 case GDK_AXIS_IGNORE:
203 if (gdk_device_get_mode (device) == GDK_MODE_WINDOW)
204 _gdk_device_translate_window_coord (device, window, j, value, &axes[j]);
209 /* FIXME: Maybe root coords chaching should happen here */
210 gdk_window_get_origin (window, &root_x, &root_y);
211 _gdk_device_translate_screen_coord (device, window,
218 _gdk_device_translate_axis (device, j, value, &axes[j]);
225 XIFreeDeviceInfo (info);
229 gdk_x11_device_xi2_query_state (device, window,
237 gdk_x11_device_xi2_set_window_cursor (GdkDevice *device,
241 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
243 /* Non-master devices don't have a cursor */
244 if (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER)
248 XIDefineCursor (GDK_WINDOW_XDISPLAY (window),
249 device_xi2->device_id,
250 GDK_WINDOW_XID (window),
251 gdk_x11_cursor_get_xcursor (cursor));
253 XIUndefineCursor (GDK_WINDOW_XDISPLAY (window),
254 device_xi2->device_id,
255 GDK_WINDOW_XID (window));
259 gdk_x11_device_xi2_warp (GdkDevice *device,
264 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
267 dest = GDK_WINDOW_XID (gdk_screen_get_root_window (screen));
269 XIWarpPointer (GDK_SCREEN_XDISPLAY (screen),
270 device_xi2->device_id,
276 gdk_x11_device_xi2_query_state (GdkDevice *device,
278 GdkWindow **root_window,
279 GdkWindow **child_window,
284 GdkModifierType *mask)
286 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
288 GdkScreen *default_screen;
289 Window xroot_window, xchild_window;
290 gdouble xroot_x, xroot_y, xwin_x, xwin_y;
291 XIButtonState button_state;
292 XIModifierState mod_state;
293 XIGroupState group_state;
295 if (!window || GDK_WINDOW_DESTROYED (window))
298 display = gdk_window_get_display (window);
299 default_screen = gdk_display_get_default_screen (display);
301 if (G_LIKELY (GDK_X11_DISPLAY (display)->trusted_client))
303 if (!XIQueryPointer (GDK_WINDOW_XDISPLAY (window),
304 device_xi2->device_id,
305 GDK_WINDOW_XID (window),
317 XSetWindowAttributes attributes;
321 /* FIXME: untrusted clients not multidevice-safe */
322 xdisplay = GDK_SCREEN_XDISPLAY (default_screen);
323 xwindow = GDK_SCREEN_XROOTWIN (default_screen);
325 w = XCreateWindow (xdisplay, xwindow, 0, 0, 1, 1, 0,
326 CopyFromParent, InputOnly, CopyFromParent,
328 XIQueryPointer (xdisplay, device_xi2->device_id,
337 XDestroyWindow (xdisplay, w);
341 *root_window = gdk_x11_window_lookup_for_display (display, xroot_window);
344 *child_window = gdk_x11_window_lookup_for_display (display, xchild_window);
347 *root_x = (gint) xroot_x;
350 *root_y = (gint) xroot_y;
353 *win_x = (gint) xwin_x;
356 *win_y = (gint) xwin_y;
359 *mask = _gdk_x11_device_xi2_translate_state (&mod_state, &button_state);
365 gdk_x11_device_xi2_grab (GdkDevice *device,
367 gboolean owner_events,
368 GdkEventMask event_mask,
369 GdkWindow *confine_to,
373 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
380 display = gdk_device_get_display (device);
382 /* FIXME: confine_to is actually unused */
384 xwindow = GDK_WINDOW_XID (window);
390 _gdk_x11_cursor_update_theme (cursor);
391 xcursor = gdk_x11_cursor_get_xcursor (cursor);
394 mask.deviceid = device_xi2->device_id;
395 mask.mask = _gdk_x11_device_xi2_translate_event_mask (event_mask, &mask.mask_len);
397 #ifdef G_ENABLE_DEBUG
398 if (_gdk_debug_flags & GDK_DEBUG_NOGRABS)
399 status = GrabSuccess;
402 status = XIGrabDevice (GDK_DISPLAY_XDISPLAY (display),
403 device_xi2->device_id,
407 GrabModeAsync, GrabModeAsync,
413 _gdk_x11_display_update_grab_info (display, device, status);
415 return _gdk_x11_convert_grab_status (status);
419 gdk_x11_device_xi2_ungrab (GdkDevice *device,
422 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
426 display = gdk_device_get_display (device);
427 serial = NextRequest (GDK_DISPLAY_XDISPLAY (display));
429 XIUngrabDevice (GDK_DISPLAY_XDISPLAY (display), device_xi2->device_id, time_);
431 _gdk_x11_display_update_grab_info_ungrab (display, device, time_, serial);
435 gdk_x11_device_xi2_window_at_position (GdkDevice *device,
438 GdkModifierType *mask,
439 gboolean get_toplevel)
441 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
446 Window xwindow, root, child, last = None;
447 gdouble xroot_x, xroot_y, xwin_x, xwin_y;
448 XIButtonState button_state;
449 XIModifierState mod_state;
450 XIGroupState group_state;
452 display = gdk_device_get_display (device);
453 screen = gdk_display_get_default_screen (display);
455 /* This function really only works if the mouse pointer is held still
456 * during its operation. If it moves from one leaf window to another
457 * than we'll end up with inaccurate values for win_x, win_y
460 gdk_x11_display_grab (display);
462 xdisplay = GDK_SCREEN_XDISPLAY (screen);
463 xwindow = GDK_SCREEN_XROOTWIN (screen);
465 if (G_LIKELY (GDK_X11_DISPLAY (display)->trusted_client))
467 XIQueryPointer (xdisplay,
468 device_xi2->device_id,
484 gint i, screens, width, height;
485 GList *toplevels, *list;
486 Window pointer_window, root, child;
488 /* FIXME: untrusted clients case not multidevice-safe */
489 pointer_window = None;
490 screens = gdk_display_get_n_screens (display);
492 for (i = 0; i < screens; ++i)
494 screen = gdk_display_get_screen (display, i);
495 toplevels = gdk_screen_get_toplevel_windows (screen);
496 for (list = toplevels; list != NULL; list = g_list_next (list))
498 window = GDK_WINDOW (list->data);
499 xwindow = GDK_WINDOW_XID (window);
500 gdk_x11_display_error_trap_push (display);
501 XIQueryPointer (xdisplay,
502 device_xi2->device_id,
510 if (gdk_x11_display_error_trap_pop (display))
514 pointer_window = child;
517 gdk_window_get_geometry (window, NULL, NULL, &width, &height);
518 if (xwin_x >= 0 && xwin_y >= 0 && xwin_x < width && xwin_y < height)
520 /* A childless toplevel, or below another window? */
521 XSetWindowAttributes attributes;
524 w = XCreateWindow (xdisplay, xwindow, (int)xwin_x, (int)xwin_y, 1, 1, 0,
525 CopyFromParent, InputOnly, CopyFromParent,
527 XMapWindow (xdisplay, w);
528 XIQueryPointer (xdisplay,
529 device_xi2->device_id,
537 XDestroyWindow (xdisplay, w);
540 pointer_window = xwindow;
546 g_list_free (toplevels);
547 if (pointer_window != None)
551 xwindow = pointer_window;
557 gdk_x11_display_error_trap_push (display);
558 XIQueryPointer (xdisplay,
559 device_xi2->device_id,
567 if (gdk_x11_display_error_trap_pop (display))
570 if (get_toplevel && last != root &&
571 (window = gdk_x11_window_lookup_for_display (display, last)) != NULL &&
572 GDK_WINDOW_TYPE (window) != GDK_WINDOW_FOREIGN)
579 gdk_x11_display_ungrab (display);
581 window = gdk_x11_window_lookup_for_display (display, last);
584 *win_x = (window) ? (gint) xwin_x : -1;
587 *win_y = (window) ? (gint) xwin_y : -1;
590 *mask = _gdk_x11_device_xi2_translate_state (&mod_state, &button_state);
596 gdk_x11_device_xi2_select_window_events (GdkDevice *device,
598 GdkEventMask event_mask)
600 GdkX11DeviceXI2 *device_xi2 = GDK_X11_DEVICE_XI2 (device);
603 evmask.deviceid = device_xi2->device_id;
604 evmask.mask = _gdk_x11_device_xi2_translate_event_mask (event_mask, &evmask.mask_len);
606 XISelectEvents (GDK_WINDOW_XDISPLAY (window),
607 GDK_WINDOW_XID (window),
610 g_free (evmask.mask);
614 _gdk_x11_device_xi2_translate_event_mask (GdkEventMask event_mask,
619 *len = XIMaskLen (XI_LASTEVENT);
620 mask = g_new0 (guchar, *len);
622 if (event_mask & GDK_POINTER_MOTION_MASK ||
623 event_mask & GDK_POINTER_MOTION_HINT_MASK)
624 XISetMask (mask, XI_Motion);
626 if (event_mask & GDK_BUTTON_MOTION_MASK ||
627 event_mask & GDK_BUTTON1_MOTION_MASK ||
628 event_mask & GDK_BUTTON2_MOTION_MASK ||
629 event_mask & GDK_BUTTON3_MOTION_MASK)
631 XISetMask (mask, XI_ButtonPress);
632 XISetMask (mask, XI_ButtonRelease);
633 XISetMask (mask, XI_Motion);
636 if (event_mask & GDK_SCROLL_MASK)
638 XISetMask (mask, XI_ButtonPress);
639 XISetMask (mask, XI_ButtonRelease);
642 if (event_mask & GDK_BUTTON_PRESS_MASK)
643 XISetMask (mask, XI_ButtonPress);
645 if (event_mask & GDK_BUTTON_RELEASE_MASK)
646 XISetMask (mask, XI_ButtonRelease);
648 if (event_mask & GDK_KEY_PRESS_MASK)
649 XISetMask (mask, XI_KeyPress);
651 if (event_mask & GDK_KEY_RELEASE_MASK)
652 XISetMask (mask, XI_KeyRelease);
654 if (event_mask & GDK_ENTER_NOTIFY_MASK)
655 XISetMask (mask, XI_Enter);
657 if (event_mask & GDK_LEAVE_NOTIFY_MASK)
658 XISetMask (mask, XI_Leave);
660 if (event_mask & GDK_FOCUS_CHANGE_MASK)
662 XISetMask (mask, XI_FocusIn);
663 XISetMask (mask, XI_FocusOut);
670 _gdk_x11_device_xi2_translate_state (XIModifierState *mods_state,
671 XIButtonState *buttons_state)
676 state = (guint) mods_state->effective;
682 /* We're only interested in the first 5 buttons */
683 len = MIN (5, buttons_state->mask_len * 8);
685 for (i = 0; i < len; i++)
687 if (!XIMaskIsSet (buttons_state->mask, i))
693 state |= GDK_BUTTON1_MASK;
696 state |= GDK_BUTTON2_MASK;
699 state |= GDK_BUTTON3_MASK;
702 state |= GDK_BUTTON4_MASK;
705 state |= GDK_BUTTON5_MASK;