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/>.
22 #include "gdkdeviceprivate.h"
23 #include "gdkdisplayprivate.h"
24 #include "gdkinternals.h"
29 * @Short_description: Object representing an input device
31 * @See_also: #GdkDeviceManager
33 * The #GdkDevice object represents a single input device, such
34 * as a keyboard, a mouse, a touchpad, etc.
36 * See the #GdkDeviceManager documentation for more information
37 * about the various kinds of master and slave devices, and their
41 typedef struct _GdkAxisInfo GdkAxisInfo;
60 static guint signals [LAST_SIGNAL] = { 0 };
63 static void gdk_device_dispose (GObject *object);
64 static void gdk_device_set_property (GObject *object,
68 static void gdk_device_get_property (GObject *object,
74 G_DEFINE_ABSTRACT_TYPE (GdkDevice, gdk_device, G_TYPE_OBJECT)
81 PROP_ASSOCIATED_DEVICE,
91 gdk_device_class_init (GdkDeviceClass *klass)
93 GObjectClass *object_class = G_OBJECT_CLASS (klass);
95 object_class->dispose = gdk_device_dispose;
96 object_class->set_property = gdk_device_set_property;
97 object_class->get_property = gdk_device_get_property;
102 * The #GdkDisplay the #GdkDevice pertains to.
106 g_object_class_install_property (object_class,
108 g_param_spec_object ("display",
109 P_("Device Display"),
110 P_("Display which the device belongs to"),
112 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
113 G_PARAM_STATIC_STRINGS));
115 * GdkDevice:device-manager:
117 * The #GdkDeviceManager the #GdkDevice pertains to.
121 g_object_class_install_property (object_class,
123 g_param_spec_object ("device-manager",
124 P_("Device manager"),
125 P_("Device manager which the device belongs to"),
126 GDK_TYPE_DEVICE_MANAGER,
127 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
128 G_PARAM_STATIC_STRINGS));
136 g_object_class_install_property (object_class,
138 g_param_spec_string ("name",
142 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
143 G_PARAM_STATIC_STRINGS));
147 * Device role in the device manager.
151 g_object_class_install_property (object_class,
153 g_param_spec_enum ("type",
155 P_("Device role in the device manager"),
156 GDK_TYPE_DEVICE_TYPE,
157 GDK_DEVICE_TYPE_MASTER,
158 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
159 G_PARAM_STATIC_STRINGS));
161 * GdkDevice:associated-device:
163 * Associated pointer or keyboard with this device, if any. Devices of type #GDK_DEVICE_TYPE_MASTER
164 * always come in keyboard/pointer pairs. Other device types will have a %NULL associated device.
168 g_object_class_install_property (object_class,
169 PROP_ASSOCIATED_DEVICE,
170 g_param_spec_object ("associated-device",
171 P_("Associated device"),
172 P_("Associated pointer or keyboard with this device"),
174 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
176 * GdkDevice:input-source:
178 * Source type for the device.
182 g_object_class_install_property (object_class,
184 g_param_spec_enum ("input-source",
186 P_("Source type for the device"),
187 GDK_TYPE_INPUT_SOURCE,
189 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
190 G_PARAM_STATIC_STRINGS));
192 * GdkDevice:input-mode:
194 * Input mode for the device.
198 g_object_class_install_property (object_class,
200 g_param_spec_enum ("input-mode",
201 P_("Input mode for the device"),
202 P_("Input mode for the device"),
205 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
207 * GdkDevice:has-cursor:
209 * Whether the device is represented by a cursor on the screen. Devices of type
210 * %GDK_DEVICE_TYPE_MASTER will have %TRUE here.
214 g_object_class_install_property (object_class,
216 g_param_spec_boolean ("has-cursor",
217 P_("Whether the device has a cursor"),
218 P_("Whether there is a visible cursor following device motion"),
220 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
221 G_PARAM_STATIC_STRINGS));
225 * Number of axes in the device.
229 g_object_class_install_property (object_class,
231 g_param_spec_uint ("n-axes",
232 P_("Number of axes in the device"),
233 P_("Number of axes in the device"),
235 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
238 * GdkDevice::changed:
239 * @device: the #GdkDevice that changed.
241 * The ::changed signal is emitted either when the #GdkDevice
242 * has changed the number of either axes or keys. For example
243 * In X this will normally happen when the slave device routing
244 * events through the master device changes (for example, user
245 * switches from the USB mouse to a tablet), in that case the
246 * master device will change to reflect the new slave device
250 g_signal_new (g_intern_static_string ("changed"),
251 G_TYPE_FROM_CLASS (object_class),
254 g_cclosure_marshal_VOID__VOID,
259 gdk_device_init (GdkDevice *device)
261 device->axes = g_array_new (FALSE, TRUE, sizeof (GdkAxisInfo));
265 gdk_device_dispose (GObject *object)
267 GdkDevice *device = GDK_DEVICE (object);
269 if (device->type == GDK_DEVICE_TYPE_SLAVE)
270 _gdk_device_remove_slave (device->associated, device);
272 if (device->associated)
274 if (device->type == GDK_DEVICE_TYPE_MASTER)
275 _gdk_device_set_associated_device (device->associated, NULL);
277 g_object_unref (device->associated);
278 device->associated = NULL;
283 g_array_free (device->axes, TRUE);
287 g_free (device->name);
288 g_free (device->keys);
293 G_OBJECT_CLASS (gdk_device_parent_class)->dispose (object);
297 gdk_device_set_property (GObject *object,
302 GdkDevice *device = GDK_DEVICE (object);
307 device->display = g_value_get_object (value);
309 case PROP_DEVICE_MANAGER:
310 device->manager = g_value_get_object (value);
314 g_free (device->name);
316 device->name = g_value_dup_string (value);
319 device->type = g_value_get_enum (value);
321 case PROP_INPUT_SOURCE:
322 device->source = g_value_get_enum (value);
324 case PROP_INPUT_MODE:
325 gdk_device_set_mode (device, g_value_get_enum (value));
327 case PROP_HAS_CURSOR:
328 device->has_cursor = g_value_get_boolean (value);
331 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
337 gdk_device_get_property (GObject *object,
342 GdkDevice *device = GDK_DEVICE (object);
347 g_value_set_object (value, device->display);
349 case PROP_DEVICE_MANAGER:
350 g_value_set_object (value, device->manager);
352 case PROP_ASSOCIATED_DEVICE:
353 g_value_set_object (value, device->associated);
356 g_value_set_string (value, device->name);
359 g_value_set_enum (value, device->type);
361 case PROP_INPUT_SOURCE:
362 g_value_set_enum (value, device->source);
364 case PROP_INPUT_MODE:
365 g_value_set_enum (value, device->mode);
367 case PROP_HAS_CURSOR:
368 g_value_set_boolean (value, device->has_cursor);
371 g_value_set_uint (value, device->axes->len);
374 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
380 * gdk_device_get_state: (skip)
381 * @device: a #GdkDevice.
382 * @window: a #GdkWindow.
383 * @axes: an array of doubles to store the values of the axes of @device in,
385 * @mask: location to store the modifiers, or %NULL.
387 * Gets the current state of a pointer device relative to @window. As a slave
388 * device coordinates are those of its master pointer, This
389 * function may not be called on devices of type %GDK_DEVICE_TYPE_SLAVE,
390 * unless there is an ongoing grab on them, see gdk_device_grab().
393 gdk_device_get_state (GdkDevice *device,
396 GdkModifierType *mask)
398 g_return_if_fail (GDK_IS_DEVICE (device));
399 g_return_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD);
400 g_return_if_fail (GDK_IS_WINDOW (window));
401 g_return_if_fail (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_SLAVE ||
402 gdk_display_device_is_grabbed (gdk_device_get_display (device), device));
404 if (GDK_DEVICE_GET_CLASS (device)->get_state)
405 GDK_DEVICE_GET_CLASS (device)->get_state (device, window, axes, mask);
409 * gdk_device_get_position:
410 * @device: pointer device to query status about.
411 * @screen: (out) (transfer none) (allow-none): location to store the #GdkScreen
412 * the @device is on, or %NULL.
413 * @x: (out) (allow-none): location to store root window X coordinate of @device, or %NULL.
414 * @y: (out) (allow-none): location to store root window Y coordinate of @device, or %NULL.
416 * Gets the current location of @device. As a slave device
417 * coordinates are those of its master pointer, This function
418 * may not be called on devices of type %GDK_DEVICE_TYPE_SLAVE,
419 * unless there is an ongoing grab on them, see gdk_device_grab().
424 gdk_device_get_position (GdkDevice *device,
431 GdkScreen *default_screen;
434 g_return_if_fail (GDK_IS_DEVICE (device));
435 g_return_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD);
437 display = gdk_device_get_display (device);
439 g_return_if_fail (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_SLAVE ||
440 gdk_display_device_is_grabbed (display, device));
442 default_screen = gdk_display_get_default_screen (display);
444 _gdk_device_query_state (device,
445 gdk_screen_get_root_window (default_screen),
451 *screen = gdk_window_get_screen (root);
459 * gdk_device_get_window_at_position:
460 * @device: pointer #GdkDevice to query info to.
461 * @win_x: (out) (allow-none): return location for the X coordinate of the device location,
462 * relative to the window origin, or %NULL.
463 * @win_y: (out) (allow-none): return location for the Y coordinate of the device location,
464 * relative to the window origin, or %NULL.
466 * Obtains the window underneath @device, returning the location of the device in @win_x and @win_y. Returns
467 * %NULL if the window tree under @device is not known to GDK (for example, belongs to another application).
469 * As a slave device coordinates are those of its master pointer, This
470 * function may not be called on devices of type %GDK_DEVICE_TYPE_SLAVE,
471 * unless there is an ongoing grab on them, see gdk_device_grab().
473 * Returns: (transfer none): the #GdkWindow under the device position, or %NULL.
478 gdk_device_get_window_at_position (GdkDevice *device,
485 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
486 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, NULL);
487 g_return_val_if_fail (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_SLAVE ||
488 gdk_display_device_is_grabbed (gdk_device_get_display (device), device), NULL);
490 window = _gdk_device_window_at_position (device, &tmp_x, &tmp_y, NULL, FALSE);
492 /* This might need corrections, as the native window returned
493 may contain client side children */
498 window = _gdk_window_find_descendant_at (window,
501 tmp_x = floor (xx + 0.5);
502 tmp_y = floor (yy + 0.5);
514 * gdk_device_get_history: (skip)
515 * @device: a #GdkDevice
516 * @window: the window with respect to which which the event coordinates will be reported
517 * @start: starting timestamp for range of events to return
518 * @stop: ending timestamp for the range of events to return
519 * @events: (array length=n_events) (out) (transfer full): location to store a newly-allocated array of #GdkTimeCoord, or %NULL
520 * @n_events: location to store the length of @events, or %NULL
522 * Obtains the motion history for a pointer device; given a starting and
523 * ending timestamp, return all events in the motion history for
524 * the device in the given range of time. Some windowing systems
525 * do not support motion history, in which case, %FALSE will
526 * be returned. (This is not distinguishable from the case where
527 * motion history is supported and no events were found.)
529 * Return value: %TRUE if the windowing system supports motion history and
530 * at least one event was found.
533 gdk_device_get_history (GdkDevice *device,
537 GdkTimeCoord ***events,
540 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
541 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
542 g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
550 if (GDK_WINDOW_DESTROYED (window))
553 if (!GDK_DEVICE_GET_CLASS (device)->get_history)
556 return GDK_DEVICE_GET_CLASS (device)->get_history (device, window,
562 _gdk_device_allocate_history (GdkDevice *device,
565 GdkTimeCoord **result = g_new (GdkTimeCoord *, n_events);
568 for (i = 0; i < n_events; i++)
569 result[i] = g_malloc (sizeof (GdkTimeCoord) -
570 sizeof (double) * (GDK_MAX_TIMECOORD_AXES - device->axes->len));
575 * gdk_device_free_history: (skip)
576 * @events: an array of #GdkTimeCoord.
577 * @n_events: the length of the array.
579 * Frees an array of #GdkTimeCoord that was returned by gdk_device_get_history().
582 gdk_device_free_history (GdkTimeCoord **events,
587 for (i = 0; i < n_events; i++)
594 * gdk_device_get_name:
595 * @device: a #GdkDevice
597 * Determines the name of the device.
599 * Return value: a name
604 gdk_device_get_name (GdkDevice *device)
606 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
612 * gdk_device_get_has_cursor:
613 * @device: a #GdkDevice
615 * Determines whether the pointer follows device motion.
617 * Return value: %TRUE if the pointer follows device motion
622 gdk_device_get_has_cursor (GdkDevice *device)
624 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
625 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
627 return device->has_cursor;
631 * gdk_device_get_source:
632 * @device: a #GdkDevice
634 * Determines the type of the device.
636 * Return value: a #GdkInputSource
641 gdk_device_get_source (GdkDevice *device)
643 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
645 return device->source;
649 * gdk_device_get_mode:
650 * @device: a #GdkDevice
652 * Determines the mode of the device.
654 * Return value: a #GdkInputSource
659 gdk_device_get_mode (GdkDevice *device)
661 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
667 * gdk_device_set_mode:
668 * @device: a #GdkDevice.
669 * @mode: the input mode.
671 * Sets a the mode of an input device. The mode controls if the
672 * device is active and whether the device's range is mapped to the
673 * entire screen or to a single window.
675 * Returns: %TRUE if the mode was successfully changed.
678 gdk_device_set_mode (GdkDevice *device,
681 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
683 if (device->mode == mode)
686 if (mode == GDK_MODE_DISABLED &&
687 gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER)
691 g_object_notify (G_OBJECT (device), "input-mode");
697 * gdk_device_get_n_keys:
698 * @device: a #GdkDevice
700 * Returns the number of keys the device currently has.
702 * Returns: the number of keys.
707 gdk_device_get_n_keys (GdkDevice *device)
709 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
711 return device->num_keys;
715 * gdk_device_get_key:
716 * @device: a #GdkDevice.
717 * @index_: the index of the macro button to get.
718 * @keyval: (out): return value for the keyval.
719 * @modifiers: (out): return value for modifiers.
721 * If @index_ has a valid keyval, this function will return %TRUE
722 * and fill in @keyval and @modifiers with the keyval settings.
724 * Returns: %TRUE if keyval is set for @index.
729 gdk_device_get_key (GdkDevice *device,
732 GdkModifierType *modifiers)
734 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
735 g_return_val_if_fail (index_ < device->num_keys, FALSE);
737 if (!device->keys[index_].keyval &&
738 !device->keys[index_].modifiers)
742 *keyval = device->keys[index_].keyval;
745 *modifiers = device->keys[index_].modifiers;
751 * gdk_device_set_key:
752 * @device: a #GdkDevice
753 * @index_: the index of the macro button to set
754 * @keyval: the keyval to generate
755 * @modifiers: the modifiers to set
757 * Specifies the X key event to generate when a macro button of a device
761 gdk_device_set_key (GdkDevice *device,
764 GdkModifierType modifiers)
766 g_return_if_fail (GDK_IS_DEVICE (device));
767 g_return_if_fail (index_ < device->num_keys);
769 device->keys[index_].keyval = keyval;
770 device->keys[index_].modifiers = modifiers;
774 * gdk_device_get_axis_use:
775 * @device: a pointer #GdkDevice.
776 * @index_: the index of the axis.
778 * Returns the axis use for @index_.
780 * Returns: a #GdkAxisUse specifying how the axis is used.
785 gdk_device_get_axis_use (GdkDevice *device,
790 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_AXIS_IGNORE);
791 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, GDK_AXIS_IGNORE);
792 g_return_val_if_fail (index_ < device->axes->len, GDK_AXIS_IGNORE);
794 info = &g_array_index (device->axes, GdkAxisInfo, index_);
800 * gdk_device_set_axis_use:
801 * @device: a pointer #GdkDevice
802 * @index_: the index of the axis
803 * @use: specifies how the axis is used
805 * Specifies how an axis of a device is used.
808 gdk_device_set_axis_use (GdkDevice *device,
814 g_return_if_fail (GDK_IS_DEVICE (device));
815 g_return_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD);
816 g_return_if_fail (index_ < device->axes->len);
818 info = &g_array_index (device->axes, GdkAxisInfo, index_);
841 * gdk_device_get_display:
842 * @device: a #GdkDevice
844 * Returns the #GdkDisplay to which @device pertains.
846 * Returns: (transfer none): a #GdkDisplay. This memory is owned
847 * by GTK+, and must not be freed or unreffed.
852 gdk_device_get_display (GdkDevice *device)
854 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
856 return device->display;
860 * gdk_device_get_associated_device:
861 * @device: a #GdkDevice
863 * Returns the associated device to @device, if @device is of type
864 * %GDK_DEVICE_TYPE_MASTER, it will return the paired pointer or
867 * If @device is of type %GDK_DEVICE_TYPE_SLAVE, it will return
868 * the master device to which @device is attached to.
870 * If @device is of type %GDK_DEVICE_TYPE_FLOATING, %NULL will be
871 * returned, as there is no associated device.
873 * Returns: (transfer none): The associated device, or %NULL
878 gdk_device_get_associated_device (GdkDevice *device)
880 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
882 return device->associated;
886 _gdk_device_set_device_type (GdkDevice *device,
889 if (device->type != type)
893 g_object_notify (G_OBJECT (device), "type");
898 _gdk_device_set_associated_device (GdkDevice *device,
899 GdkDevice *associated)
901 g_return_if_fail (GDK_IS_DEVICE (device));
902 g_return_if_fail (associated == NULL || GDK_IS_DEVICE (associated));
904 if (device->associated == associated)
907 if (device->associated)
909 g_object_unref (device->associated);
910 device->associated = NULL;
914 device->associated = g_object_ref (associated);
916 if (device->type != GDK_DEVICE_TYPE_MASTER)
918 if (device->associated)
919 _gdk_device_set_device_type (device, GDK_DEVICE_TYPE_SLAVE);
921 _gdk_device_set_device_type (device, GDK_DEVICE_TYPE_FLOATING);
926 * gdk_device_list_slave_devices:
927 * @device: a #GdkDevice
929 * If the device if of type %GDK_DEVICE_TYPE_MASTER, it will return
930 * the list of slave devices attached to it, otherwise it will return
933 * Returns: (transfer container) (element-type GdkDevice): the list of
934 * slave devices, or %NULL. The list must be freed with
935 * g_list_free(), the contents of the list are owned by GTK+
936 * and should not be freed.
939 gdk_device_list_slave_devices (GdkDevice *device)
941 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
942 g_return_val_if_fail (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER, NULL);
944 return g_list_copy (device->slaves);
948 _gdk_device_add_slave (GdkDevice *device,
951 g_return_if_fail (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER);
952 g_return_if_fail (gdk_device_get_device_type (slave) != GDK_DEVICE_TYPE_MASTER);
954 if (!g_list_find (device->slaves, slave))
955 device->slaves = g_list_prepend (device->slaves, slave);
959 _gdk_device_remove_slave (GdkDevice *device,
964 g_return_if_fail (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER);
965 g_return_if_fail (gdk_device_get_device_type (slave) != GDK_DEVICE_TYPE_MASTER);
967 elem = g_list_find (device->slaves, slave);
972 device->slaves = g_list_delete_link (device->slaves, elem);
976 * gdk_device_get_device_type:
977 * @device: a #GdkDevice
979 * Returns the device type for @device.
981 * Returns: the #GdkDeviceType for @device.
986 gdk_device_get_device_type (GdkDevice *device)
988 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_DEVICE_TYPE_MASTER);
994 * gdk_device_get_n_axes:
995 * @device: a pointer #GdkDevice
997 * Returns the number of axes the device currently has.
999 * Returns: the number of axes.
1004 gdk_device_get_n_axes (GdkDevice *device)
1006 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
1007 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, 0);
1009 return device->axes->len;
1013 * gdk_device_list_axes:
1014 * @device: a pointer #GdkDevice
1016 * Returns a #GList of #GdkAtom<!-- -->s, containing the labels for
1017 * the axes that @device currently has.
1019 * Returns: (transfer container) (element-type GdkAtom):
1020 * A #GList of #GdkAtom<!-- -->s, free with g_list_free().
1025 gdk_device_list_axes (GdkDevice *device)
1030 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
1031 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, NULL);
1033 for (i = 0; i < device->axes->len; i++)
1035 GdkAxisInfo axis_info;
1037 axis_info = g_array_index (device->axes, GdkAxisInfo, i);
1038 axes = g_list_prepend (axes, GDK_ATOM_TO_POINTER (axis_info.label));
1041 return g_list_reverse (axes);
1045 * gdk_device_get_axis_value: (skip)
1046 * @device: a pointer #GdkDevice.
1047 * @axes: (array): pointer to an array of axes
1048 * @axis_label: #GdkAtom with the axis label.
1049 * @value: location to store the found value.
1051 * Interprets an array of double as axis values for a given device,
1052 * and locates the value in the array for a given axis label, as returned
1053 * by gdk_device_list_axes()
1055 * Returns: %TRUE if the given axis use was found, otherwise %FALSE.
1060 gdk_device_get_axis_value (GdkDevice *device,
1067 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
1068 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
1073 for (i = 0; i < device->axes->len; i++)
1075 GdkAxisInfo axis_info;
1077 axis_info = g_array_index (device->axes, GdkAxisInfo, i);
1079 if (axis_info.label != axis_label)
1092 * gdk_device_get_axis: (skip)
1093 * @device: a #GdkDevice
1094 * @axes: (array): pointer to an array of axes
1095 * @use: the use to look for
1096 * @value: (out): location to store the found value.
1098 * Interprets an array of double as axis values for a given device,
1099 * and locates the value in the array for a given axis use.
1101 * Return value: %TRUE if the given axis use was found, otherwise %FALSE
1104 gdk_device_get_axis (GdkDevice *device,
1111 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
1112 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
1117 g_return_val_if_fail (device->axes != NULL, FALSE);
1119 for (i = 0; i < device->axes->len; i++)
1121 GdkAxisInfo axis_info;
1123 axis_info = g_array_index (device->axes, GdkAxisInfo, i);
1125 if (axis_info.use != use)
1138 get_native_grab_event_mask (GdkEventMask grab_mask)
1140 /* Similar to the above but for pointer events only */
1142 GDK_POINTER_MOTION_MASK |
1143 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1144 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1147 ~(GDK_POINTER_MOTION_HINT_MASK |
1148 GDK_BUTTON_MOTION_MASK |
1149 GDK_BUTTON1_MOTION_MASK |
1150 GDK_BUTTON2_MOTION_MASK |
1151 GDK_BUTTON3_MOTION_MASK));
1156 * @device: a #GdkDevice. To get the device you can use gtk_get_current_event_device()
1157 * or gdk_event_get_device() if the grab is in reaction to an event. Also, you can use
1158 * gdk_device_manager_get_client_pointer() but only in code that isn't triggered by a
1159 * #GdkEvent and there aren't other means to get a meaningful #GdkDevice to operate on.
1160 * @window: the #GdkWindow which will own the grab (the grab window)
1161 * @grab_ownership: specifies the grab ownership.
1162 * @owner_events: if %FALSE then all device events are reported with respect to
1163 * @window and are only reported if selected by @event_mask. If
1164 * %TRUE then pointer events for this application are reported
1165 * as normal, but pointer events outside this application are
1166 * reported with respect to @window and only if selected by
1167 * @event_mask. In either mode, unreported events are discarded.
1168 * @event_mask: specifies the event mask, which is used in accordance with
1170 * @cursor: (allow-none): the cursor to display while the grab is active if the device is
1171 * a pointer. If this is %NULL then the normal cursors are used for
1172 * @window and its descendants, and the cursor for @window is used
1174 * @time_: the timestamp of the event which led to this pointer grab. This
1175 * usually comes from the #GdkEvent struct, though %GDK_CURRENT_TIME
1176 * can be used if the time isn't known.
1178 * Grabs the device so that all events coming from this device are passed to
1179 * this application until the device is ungrabbed with gdk_device_ungrab(),
1180 * or the window becomes unviewable. This overrides any previous grab on the device
1183 * Device grabs are used for operations which need complete control over the
1184 * given device events (either pointer or keyboard). For example in GTK+ this
1185 * is used for Drag and Drop operations, popup menus and such.
1187 * Note that if the event mask of an X window has selected both button press
1188 * and button release events, then a button press event will cause an automatic
1189 * pointer grab until the button is released. X does this automatically since
1190 * most applications expect to receive button press and release events in pairs.
1191 * It is equivalent to a pointer grab on the window with @owner_events set to
1194 * If you set up anything at the time you take the grab that needs to be
1195 * cleaned up when the grab ends, you should handle the #GdkEventGrabBroken
1196 * events that are emitted when the grab ends unvoluntarily.
1198 * Returns: %GDK_GRAB_SUCCESS if the grab was successful.
1203 gdk_device_grab (GdkDevice *device,
1205 GdkGrabOwnership grab_ownership,
1206 gboolean owner_events,
1207 GdkEventMask event_mask,
1214 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_GRAB_SUCCESS);
1215 g_return_val_if_fail (GDK_IS_WINDOW (window), GDK_GRAB_SUCCESS);
1217 native = gdk_window_get_toplevel (window);
1219 while (native->window_type == GDK_WINDOW_OFFSCREEN)
1221 native = gdk_offscreen_window_get_embedder (native);
1223 if (native == NULL ||
1224 (!_gdk_window_has_impl (native) &&
1225 !gdk_window_is_viewable (native)))
1226 return GDK_GRAB_NOT_VIEWABLE;
1228 native = gdk_window_get_toplevel (native);
1231 if (native == NULL || GDK_WINDOW_DESTROYED (native))
1232 return GDK_GRAB_NOT_VIEWABLE;
1234 res = GDK_DEVICE_GET_CLASS (device)->grab (device,
1237 get_native_grab_event_mask (event_mask),
1242 if (res == GDK_GRAB_SUCCESS)
1244 GdkDisplay *display;
1247 display = gdk_window_get_display (window);
1248 serial = _gdk_display_get_next_serial (display);
1250 _gdk_display_add_device_grab (display,
1266 * gdk_device_ungrab:
1267 * @device: a #GdkDevice
1268 * @time_: a timestap (e.g. %GDK_CURRENT_TIME).
1270 * Release any grab on @device.
1275 gdk_device_ungrab (GdkDevice *device,
1278 g_return_if_fail (GDK_IS_DEVICE (device));
1280 GDK_DEVICE_GET_CLASS (device)->ungrab (device, time_);
1285 * @device: the device to warp.
1286 * @screen: the screen to warp @device to.
1287 * @x: the X coordinate of the destination.
1288 * @y: the Y coordinate of the destination.
1290 * Warps @device in @display to the point @x,@y on
1291 * the screen @screen, unless the device is confined
1292 * to a window by a grab, in which case it will be moved
1293 * as far as allowed by the grab. Warping the pointer
1294 * creates events as if the user had moved the mouse
1295 * instantaneously to the destination.
1297 * Note that the pointer should normally be under the
1298 * control of the user. This function was added to cover
1299 * some rare use cases like keyboard navigation support
1300 * for the color picker in the #GtkColorSelectionDialog.
1305 gdk_device_warp (GdkDevice *device,
1310 g_return_if_fail (GDK_IS_DEVICE (device));
1311 g_return_if_fail (GDK_IS_SCREEN (screen));
1312 g_return_if_fail (gdk_device_get_display (device) == gdk_screen_get_display (screen));
1314 GDK_DEVICE_GET_CLASS (device)->warp (device, screen, x, y);
1319 _gdk_device_reset_axes (GdkDevice *device)
1323 for (i = device->axes->len - 1; i >= 0; i--)
1324 g_array_remove_index (device->axes, i);
1326 g_object_notify (G_OBJECT (device), "n-axes");
1330 _gdk_device_add_axis (GdkDevice *device,
1337 GdkAxisInfo axis_info;
1340 axis_info.use = use;
1341 axis_info.label = label_atom;
1342 axis_info.min_value = min_value;
1343 axis_info.max_value = max_value;
1344 axis_info.resolution = resolution;
1350 axis_info.min_axis = 0;
1351 axis_info.max_axis = 0;
1353 case GDK_AXIS_XTILT:
1354 case GDK_AXIS_YTILT:
1355 axis_info.min_axis = -1;
1356 axis_info.max_axis = 1;
1359 axis_info.min_axis = 0;
1360 axis_info.max_axis = 1;
1364 device->axes = g_array_append_val (device->axes, axis_info);
1365 pos = device->axes->len - 1;
1367 g_object_notify (G_OBJECT (device), "n-axes");
1373 _gdk_device_get_axis_info (GdkDevice *device,
1375 GdkAtom *label_atom,
1379 gdouble *resolution)
1383 g_return_if_fail (GDK_IS_DEVICE (device));
1384 g_return_if_fail (index_ < device->axes->len);
1386 info = &g_array_index (device->axes, GdkAxisInfo, index_);
1388 *label_atom = info->label;
1390 *min_value = info->min_value;
1391 *max_value = info->max_value;
1392 *resolution = info->resolution;
1396 _gdk_device_set_keys (GdkDevice *device,
1400 g_free (device->keys);
1402 device->num_keys = num_keys;
1403 device->keys = g_new0 (GdkDeviceKey, num_keys);
1406 static GdkAxisInfo *
1407 find_axis_info (GArray *array,
1413 for (i = 0; i < GDK_AXIS_LAST; i++)
1415 info = &g_array_index (array, GdkAxisInfo, i);
1417 if (info->use == use)
1425 _gdk_device_translate_window_coord (GdkDevice *device,
1429 gdouble *axis_value)
1431 GdkAxisInfo axis_info;
1432 GdkAxisInfo *axis_info_x, *axis_info_y;
1433 gdouble device_width, device_height;
1434 gdouble x_offset, y_offset;
1435 gdouble x_scale, y_scale;
1436 gdouble x_min, y_min;
1437 gdouble x_resolution, y_resolution;
1438 gdouble device_aspect;
1439 gint window_width, window_height;
1441 if (index_ >= device->axes->len)
1444 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1446 if (axis_info.use != GDK_AXIS_X &&
1447 axis_info.use != GDK_AXIS_Y)
1450 if (axis_info.use == GDK_AXIS_X)
1452 axis_info_x = &axis_info;
1453 axis_info_y = find_axis_info (device->axes, GDK_AXIS_Y);
1457 axis_info_x = find_axis_info (device->axes, GDK_AXIS_X);
1458 axis_info_y = &axis_info;
1461 device_width = axis_info_x->max_value - axis_info_x->min_value;
1462 device_height = axis_info_y->max_value - axis_info_y->min_value;
1464 if (device_width > 0)
1465 x_min = axis_info_x->min_value;
1468 device_width = gdk_screen_get_width (gdk_window_get_screen (window));
1472 if (device_height > 0)
1473 y_min = axis_info_y->min_value;
1476 device_height = gdk_screen_get_height (gdk_window_get_screen (window));
1480 window_width = gdk_window_get_width (window);
1481 window_height = gdk_window_get_height (window);
1483 x_resolution = axis_info_x->resolution;
1484 y_resolution = axis_info_y->resolution;
1487 * Some drivers incorrectly report the resolution of the device
1488 * as zero (in partiular linuxwacom < 0.5.3 with usb tablets).
1489 * This causes the device_aspect to become NaN and totally
1490 * breaks windowed mode. If this is the case, the best we can
1491 * do is to assume the resolution is non-zero is equal in both
1492 * directions (which is true for many devices). The absolute
1493 * value of the resolution doesn't matter since we only use the
1496 if (x_resolution == 0 || y_resolution == 0)
1502 device_aspect = (device_height * y_resolution) /
1503 (device_width * x_resolution);
1505 if (device_aspect * window_width >= window_height)
1507 /* device taller than window */
1508 x_scale = window_width / device_width;
1509 y_scale = (x_scale * x_resolution) / y_resolution;
1512 y_offset = - (device_height * y_scale - window_height) / 2;
1516 /* window taller than device */
1517 y_scale = window_height / device_height;
1518 x_scale = (y_scale * y_resolution) / x_resolution;
1521 x_offset = - (device_width * x_scale - window_width) / 2;
1526 if (axis_info.use == GDK_AXIS_X)
1527 *axis_value = x_offset + x_scale * (value - x_min);
1529 *axis_value = y_offset + y_scale * (value - y_min);
1536 _gdk_device_translate_screen_coord (GdkDevice *device,
1542 gdouble *axis_value)
1544 GdkAxisInfo axis_info;
1545 gdouble axis_width, scale, offset;
1547 if (device->mode != GDK_MODE_SCREEN)
1550 if (index_ >= device->axes->len)
1553 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1555 if (axis_info.use != GDK_AXIS_X &&
1556 axis_info.use != GDK_AXIS_Y)
1559 axis_width = axis_info.max_value - axis_info.min_value;
1561 if (axis_info.use == GDK_AXIS_X)
1564 scale = gdk_screen_get_width (gdk_window_get_screen (window)) / axis_width;
1568 offset = - window_root_x - window->abs_x;
1573 scale = gdk_screen_get_height (gdk_window_get_screen (window)) / axis_width;
1577 offset = - window_root_y - window->abs_y;
1581 *axis_value = offset + scale * (value - axis_info.min_value);
1587 _gdk_device_translate_axis (GdkDevice *device,
1590 gdouble *axis_value)
1592 GdkAxisInfo axis_info;
1593 gdouble axis_width, out;
1595 if (index_ >= device->axes->len)
1598 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1600 if (axis_info.use == GDK_AXIS_X ||
1601 axis_info.use == GDK_AXIS_Y)
1604 axis_width = axis_info.max_value - axis_info.min_value;
1605 out = (axis_info.max_axis * (value - axis_info.min_value) +
1606 axis_info.min_axis * (axis_info.max_value - value)) / axis_width;
1615 _gdk_device_query_state (GdkDevice *device,
1617 GdkWindow **root_window,
1618 GdkWindow **child_window,
1623 GdkModifierType *mask)
1625 GDK_DEVICE_GET_CLASS (device)->query_state (device,
1637 _gdk_device_window_at_position (GdkDevice *device,
1640 GdkModifierType *mask,
1641 gboolean get_toplevel)
1643 return GDK_DEVICE_GET_CLASS (device)->window_at_position (device,