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 "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.
35 * See the #GdkDeviceManager documentation for more information
36 * about the various kinds of master and slave devices, and their
40 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 _gdk_device_set_associated_device (device->associated, NULL);
275 g_object_unref (device->associated);
276 device->associated = NULL;
281 g_array_free (device->axes, TRUE);
285 g_free (device->name);
286 g_free (device->keys);
291 G_OBJECT_CLASS (gdk_device_parent_class)->dispose (object);
295 gdk_device_set_property (GObject *object,
300 GdkDevice *device = GDK_DEVICE (object);
305 device->display = g_value_get_object (value);
307 case PROP_DEVICE_MANAGER:
308 device->manager = g_value_get_object (value);
312 g_free (device->name);
314 device->name = g_value_dup_string (value);
317 device->type = g_value_get_enum (value);
319 case PROP_INPUT_SOURCE:
320 device->source = g_value_get_enum (value);
322 case PROP_INPUT_MODE:
323 gdk_device_set_mode (device, g_value_get_enum (value));
325 case PROP_HAS_CURSOR:
326 device->has_cursor = g_value_get_boolean (value);
329 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
335 gdk_device_get_property (GObject *object,
340 GdkDevice *device = GDK_DEVICE (object);
345 g_value_set_object (value, device->display);
347 case PROP_DEVICE_MANAGER:
348 g_value_set_object (value, device->manager);
350 case PROP_ASSOCIATED_DEVICE:
351 g_value_set_object (value, device->associated);
354 g_value_set_string (value, device->name);
357 g_value_set_enum (value, device->type);
359 case PROP_INPUT_SOURCE:
360 g_value_set_enum (value, device->source);
362 case PROP_INPUT_MODE:
363 g_value_set_enum (value, device->mode);
365 case PROP_HAS_CURSOR:
366 g_value_set_boolean (value, device->has_cursor);
369 g_value_set_uint (value, device->axes->len);
372 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
378 * gdk_device_get_state:
379 * @device: a #GdkDevice.
380 * @window: a #GdkWindow.
381 * @axes: an array of doubles to store the values of the axes of @device in,
383 * @mask: location to store the modifiers, or %NULL.
385 * Gets the current state of a pointer device relative to @window.
388 gdk_device_get_state (GdkDevice *device,
391 GdkModifierType *mask)
393 g_return_if_fail (GDK_IS_DEVICE (device));
394 g_return_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD);
395 g_return_if_fail (GDK_IS_WINDOW (window));
397 if (GDK_DEVICE_GET_CLASS (device)->get_state)
398 GDK_DEVICE_GET_CLASS (device)->get_state (device, window, axes, mask);
402 * gdk_device_get_position:
403 * @device: pointer device to query status about.
404 * @screen: (out) (transfer none) (allow-none): location to store the #GdkScreen
405 * the @device is on, or %NULL.
406 * @x: (out) (allow-none): location to store root window X coordinate of @device, or %NULL.
407 * @y: (out) (allow-none): location to store root window Y coordinate of @device, or %NULL.
409 * Gets the current location of @device.
414 gdk_device_get_position (GdkDevice *device,
419 GdkScreen *tmp_screen;
422 GdkModifierType tmp_mask;
424 g_return_if_fail (GDK_IS_DEVICE (device));
425 g_return_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD);
427 display = gdk_device_get_display (device);
428 display->device_hooks->get_device_state (display, device, &tmp_screen, &tmp_x, &tmp_y, &tmp_mask);
431 *screen = tmp_screen;
439 * gdk_device_get_window_at_position:
440 * @device: pointer #GdkDevice to query info to.
441 * @win_x: (out) (allow-none): return location for the X coordinate of the device location,
442 * relative to the window origin, or %NULL.
443 * @win_y: (out) (allow-none): return location for the Y coordinate of the device location,
444 * relative to the window origin, or %NULL.
446 * Obtains the window underneath @device, returning the location of the device in @win_x and @win_y. Returns
447 * %NULL if the window tree under @device is not known to GDK (for example, belongs to another application).
449 * Returns: (transfer none): the #GdkWindow under the device position, or %NULL.
454 gdk_device_get_window_at_position (GdkDevice *device,
462 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
463 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, NULL);
465 display = gdk_device_get_display (device);
467 window = display->device_hooks->window_at_device_position (display, device, &tmp_x, &tmp_y);
478 * gdk_device_get_history:
479 * @device: a #GdkDevice
480 * @window: the window with respect to which which the event coordinates will be reported
481 * @start: starting timestamp for range of events to return
482 * @stop: ending timestamp for the range of events to return
483 * @events: (array length=n_events) (out) (transfer none): location to store a newly-allocated array of #GdkTimeCoord, or %NULL
484 * @n_events: location to store the length of @events, or %NULL
486 * Obtains the motion history for a pointer device; given a starting and
487 * ending timestamp, return all events in the motion history for
488 * the device in the given range of time. Some windowing systems
489 * do not support motion history, in which case, %FALSE will
490 * be returned. (This is not distinguishable from the case where
491 * motion history is supported and no events were found.)
493 * Return value: %TRUE if the windowing system supports motion history and
494 * at least one event was found.
497 gdk_device_get_history (GdkDevice *device,
501 GdkTimeCoord ***events,
504 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
505 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
506 g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
514 if (GDK_WINDOW_DESTROYED (window))
517 if (!GDK_DEVICE_GET_CLASS (device)->get_history)
520 return GDK_DEVICE_GET_CLASS (device)->get_history (device, window,
526 _gdk_device_allocate_history (GdkDevice *device,
529 GdkTimeCoord **result = g_new (GdkTimeCoord *, n_events);
532 for (i = 0; i < n_events; i++)
533 result[i] = g_malloc (sizeof (GdkTimeCoord) -
534 sizeof (double) * (GDK_MAX_TIMECOORD_AXES - device->axes->len));
539 * gdk_device_free_history:
540 * @events: (inout) (transfer none): an array of #GdkTimeCoord.
541 * @n_events: the length of the array.
543 * Frees an array of #GdkTimeCoord that was returned by gdk_device_get_history().
546 gdk_device_free_history (GdkTimeCoord **events,
551 for (i = 0; i < n_events; i++)
558 * gdk_device_get_name:
559 * @device: a #GdkDevice
561 * Determines the name of the device.
563 * Return value: a name
568 gdk_device_get_name (GdkDevice *device)
570 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
576 * gdk_device_get_has_cursor:
577 * @device: a #GdkDevice
579 * Determines whether the pointer follows device motion.
581 * Return value: %TRUE if the pointer follows device motion
586 gdk_device_get_has_cursor (GdkDevice *device)
588 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
589 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
591 return device->has_cursor;
595 * gdk_device_get_source:
596 * @device: a #GdkDevice
598 * Determines the type of the device.
600 * Return value: a #GdkInputSource
605 gdk_device_get_source (GdkDevice *device)
607 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
609 return device->source;
613 * gdk_device_get_mode:
614 * @device: a #GdkDevice
616 * Determines the mode of the device.
618 * Return value: a #GdkInputSource
623 gdk_device_get_mode (GdkDevice *device)
625 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
631 * gdk_device_set_mode:
632 * @device: a #GdkDevice.
633 * @mode: the input mode.
635 * Sets a the mode of an input device. The mode controls if the
636 * device is active and whether the device's range is mapped to the
637 * entire screen or to a single window.
639 * Returns: %TRUE if the mode was successfully changed.
642 gdk_device_set_mode (GdkDevice *device,
645 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
647 if (device->mode == mode)
650 if (mode == GDK_MODE_DISABLED &&
651 gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER)
655 g_object_notify (G_OBJECT (device), "input-mode");
661 * gdk_device_get_n_keys:
662 * @device: a #GdkDevice
664 * Returns the number of keys the device currently has.
666 * Returns: the number of keys.
671 gdk_device_get_n_keys (GdkDevice *device)
673 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
675 return device->num_keys;
679 * gdk_device_get_key:
680 * @device: a #GdkDevice.
681 * @index_: the index of the macro button to get.
682 * @keyval: return value for the keyval.
683 * @modifiers: return value for modifiers.
685 * If @index_ has a valid keyval, this function will return %TRUE
686 * and fill in @keyval and @modifiers with the keyval settings.
688 * Returns: %TRUE if keyval is set for @index.
693 gdk_device_get_key (GdkDevice *device,
696 GdkModifierType *modifiers)
698 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
699 g_return_val_if_fail (index_ < device->num_keys, FALSE);
701 if (!device->keys[index_].keyval &&
702 !device->keys[index_].modifiers)
706 *keyval = device->keys[index_].keyval;
709 *modifiers = device->keys[index_].modifiers;
715 * gdk_device_set_key:
716 * @device: a #GdkDevice
717 * @index_: the index of the macro button to set
718 * @keyval: the keyval to generate
719 * @modifiers: the modifiers to set
721 * Specifies the X key event to generate when a macro button of a device
725 gdk_device_set_key (GdkDevice *device,
728 GdkModifierType modifiers)
730 g_return_if_fail (GDK_IS_DEVICE (device));
731 g_return_if_fail (index_ < device->num_keys);
733 device->keys[index_].keyval = keyval;
734 device->keys[index_].modifiers = modifiers;
738 * gdk_device_get_axis_use:
739 * @device: a pointer #GdkDevice.
740 * @index_: the index of the axis.
742 * Returns the axis use for @index_.
744 * Returns: a #GdkAxisUse specifying how the axis is used.
749 gdk_device_get_axis_use (GdkDevice *device,
754 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_AXIS_IGNORE);
755 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, GDK_AXIS_IGNORE);
756 g_return_val_if_fail (index_ < device->axes->len, GDK_AXIS_IGNORE);
758 info = &g_array_index (device->axes, GdkAxisInfo, index_);
764 * gdk_device_set_axis_use:
765 * @device: a pointer #GdkDevice
766 * @index_: the index of the axis
767 * @use: specifies how the axis is used
769 * Specifies how an axis of a device is used.
772 gdk_device_set_axis_use (GdkDevice *device,
778 g_return_if_fail (GDK_IS_DEVICE (device));
779 g_return_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD);
780 g_return_if_fail (index_ < device->axes->len);
782 info = &g_array_index (device->axes, GdkAxisInfo, index_);
805 * gdk_device_get_display:
806 * @device: a #GdkDevice
808 * Returns the #GdkDisplay to which @device pertains.
810 * Returns: (transfer none): a #GdkDisplay. This memory is owned
811 * by GTK+, and must not be freed or unreffed.
816 gdk_device_get_display (GdkDevice *device)
818 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
820 return device->display;
824 * gdk_device_get_associated_device:
825 * @device: a #GdkDevice
827 * Returns the associated device to @device, if @device is of type
828 * %GDK_DEVICE_TYPE_MASTER, it will return the paired pointer or
831 * If @device is of type %GDK_DEVICE_TYPE_SLAVE, it will return
832 * the master device to which @device is attached to.
834 * If @device is of type %GDK_DEVICE_TYPE_FLOATING, %NULL will be
835 * returned, as there is no associated device.
837 * Returns: (transfer none): The associated device, or %NULL
842 gdk_device_get_associated_device (GdkDevice *device)
844 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
846 return device->associated;
850 _gdk_device_set_device_type (GdkDevice *device,
853 if (device->type != type)
857 g_object_notify (G_OBJECT (device), "type");
862 _gdk_device_set_associated_device (GdkDevice *device,
863 GdkDevice *associated)
865 g_return_if_fail (GDK_IS_DEVICE (device));
866 g_return_if_fail (associated == NULL || GDK_IS_DEVICE (associated));
868 if (device->associated == associated)
871 if (device->associated)
873 g_object_unref (device->associated);
874 device->associated = NULL;
878 device->associated = g_object_ref (associated);
880 if (device->type != GDK_DEVICE_TYPE_MASTER)
882 if (device->associated)
883 _gdk_device_set_device_type (device, GDK_DEVICE_TYPE_SLAVE);
885 _gdk_device_set_device_type (device, GDK_DEVICE_TYPE_FLOATING);
890 * gdk_device_list_slave_devices:
891 * @device: a #GdkDevice
893 * If the device if of type %GDK_DEVICE_TYPE_MASTER, it will return
894 * the list of slave devices attached to it, otherwise it will return
897 * Returns: (transfer container): the list of slave devices, or %NULL. The
898 * list must be freed with g_list_free(), the contents of the list
899 * are owned by GTK+ and should not be freed.
902 gdk_device_list_slave_devices (GdkDevice *device)
904 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
905 g_return_val_if_fail (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER, NULL);
907 return g_list_copy (device->slaves);
911 _gdk_device_add_slave (GdkDevice *device,
914 g_return_if_fail (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER);
915 g_return_if_fail (gdk_device_get_device_type (slave) != GDK_DEVICE_TYPE_MASTER);
917 if (!g_list_find (device->slaves, slave))
918 device->slaves = g_list_prepend (device->slaves, slave);
922 _gdk_device_remove_slave (GdkDevice *device,
927 g_return_if_fail (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER);
928 g_return_if_fail (gdk_device_get_device_type (slave) != GDK_DEVICE_TYPE_MASTER);
930 elem = g_list_find (device->slaves, slave);
935 device->slaves = g_list_delete_link (device->slaves, elem);
939 * gdk_device_get_device_type:
940 * @device: a #GdkDevice
942 * Returns the device type for @device.
944 * Returns: the #GdkDeviceType for @device.
949 gdk_device_get_device_type (GdkDevice *device)
951 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_DEVICE_TYPE_MASTER);
957 * gdk_device_get_n_axes:
958 * @device: a pointer #GdkDevice
960 * Returns the number of axes the device currently has.
962 * Returns: the number of axes.
967 gdk_device_get_n_axes (GdkDevice *device)
969 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
970 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, 0);
972 return device->axes->len;
976 * gdk_device_list_axes:
977 * @device: a pointer #GdkDevice
979 * Returns a #GList of #GdkAtom<!-- -->s, containing the labels for
980 * the axes that @device currently has.
982 * Returns: (transfer container) (element-type GdkAtom):
983 * A #GList of #GdkAtom<!-- -->s, free with g_list_free().
988 gdk_device_list_axes (GdkDevice *device)
993 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
994 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, NULL);
996 for (i = 0; i < device->axes->len; i++)
998 GdkAxisInfo axis_info;
1000 axis_info = g_array_index (device->axes, GdkAxisInfo, i);
1001 axes = g_list_prepend (axes, GDK_ATOM_TO_POINTER (axis_info.label));
1004 return g_list_reverse (axes);
1008 * gdk_device_get_axis_value:
1009 * @device: a pointer #GdkDevice.
1010 * @axes: pointer to an array of axes
1011 * @axis_label: #GdkAtom with the axis label.
1012 * @value: location to store the found value.
1014 * Interprets an array of double as axis values for a given device,
1015 * and locates the value in the array for a given axis label, as returned
1016 * by gdk_device_list_axes()
1018 * Returns: %TRUE if the given axis use was found, otherwise %FALSE.
1023 gdk_device_get_axis_value (GdkDevice *device,
1030 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
1031 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
1036 for (i = 0; i < device->axes->len; i++)
1038 GdkAxisInfo axis_info;
1040 axis_info = g_array_index (device->axes, GdkAxisInfo, i);
1042 if (axis_info.label != axis_label)
1055 * gdk_device_get_axis:
1056 * @device: a #GdkDevice
1057 * @axes: pointer to an array of axes
1058 * @use: the use to look for
1059 * @value: location to store the found value.
1061 * Interprets an array of double as axis values for a given device,
1062 * and locates the value in the array for a given axis use.
1064 * Return value: %TRUE if the given axis use was found, otherwise %FALSE
1067 gdk_device_get_axis (GdkDevice *device,
1074 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
1075 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
1080 g_return_val_if_fail (device->axes != NULL, FALSE);
1082 for (i = 0; i < device->axes->len; i++)
1084 GdkAxisInfo axis_info;
1086 axis_info = g_array_index (device->axes, GdkAxisInfo, i);
1088 if (axis_info.use != use)
1101 get_native_grab_event_mask (GdkEventMask grab_mask)
1103 /* Similar to the above but for pointer events only */
1105 GDK_POINTER_MOTION_MASK |
1106 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1107 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1110 ~(GDK_POINTER_MOTION_HINT_MASK |
1111 GDK_BUTTON_MOTION_MASK |
1112 GDK_BUTTON1_MOTION_MASK |
1113 GDK_BUTTON2_MOTION_MASK |
1114 GDK_BUTTON3_MOTION_MASK));
1119 * @device: a #GdkDevice. To get the device you can use gtk_get_current_event_device()
1120 * or gdk_event_get_device() if the grab is in reaction to an event. Also, you can use
1121 * gdk_device_manager_get_client_pointer() but only in code that isn't triggered by a
1122 * #GdkEvent and there aren't other means to get a meaningful #GdkDevice to operate on.
1123 * @window: the #GdkWindow which will own the grab (the grab window)
1124 * @grab_ownership: specifies the grab ownership.
1125 * @owner_events: if %FALSE then all device events are reported with respect to
1126 * @window and are only reported if selected by @event_mask. If
1127 * %TRUE then pointer events for this application are reported
1128 * as normal, but pointer events outside this application are
1129 * reported with respect to @window and only if selected by
1130 * @event_mask. In either mode, unreported events are discarded.
1131 * @event_mask: specifies the event mask, which is used in accordance with
1133 * @cursor: the cursor to display while the grab is active if the device is
1134 * a pointer. If this is %NULL then the normal cursors are used for
1135 * @window and its descendants, and the cursor for @window is used
1137 * @time_: the timestamp of the event which led to this pointer grab. This
1138 * usually comes from the #GdkEvent struct, though %GDK_CURRENT_TIME
1139 * can be used if the time isn't known.
1141 * Grabs the device so that all events coming from this device are passed to
1142 * this application until the device is ungrabbed with gdk_device_ungrab(),
1143 * or the window becomes unviewable. This overrides any previous grab on the device
1146 * Device grabs are used for operations which need complete control over the
1147 * given device events (either pointer or keyboard). For example in GTK+ this
1148 * is used for Drag and Drop operations, popup menus and such.
1150 * Note that if the event mask of an X window has selected both button press
1151 * and button release events, then a button press event will cause an automatic
1152 * pointer grab until the button is released. X does this automatically since
1153 * most applications expect to receive button press and release events in pairs.
1154 * It is equivalent to a pointer grab on the window with @owner_events set to
1157 * If you set up anything at the time you take the grab that needs to be
1158 * cleaned up when the grab ends, you should handle the #GdkEventGrabBroken
1159 * events that are emitted when the grab ends unvoluntarily.
1161 * Returns: %GDK_GRAB_SUCCESS if the grab was successful.
1166 gdk_device_grab (GdkDevice *device,
1168 GdkGrabOwnership grab_ownership,
1169 gboolean owner_events,
1170 GdkEventMask event_mask,
1177 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_GRAB_SUCCESS);
1178 g_return_val_if_fail (GDK_IS_WINDOW (window), GDK_GRAB_SUCCESS);
1180 if (_gdk_native_windows)
1183 native = gdk_window_get_toplevel (window);
1185 while (native->window_type == GDK_WINDOW_OFFSCREEN)
1187 native = gdk_offscreen_window_get_embedder (native);
1189 if (native == NULL ||
1190 (!_gdk_window_has_impl (native) &&
1191 !gdk_window_is_viewable (native)))
1192 return GDK_GRAB_NOT_VIEWABLE;
1194 native = gdk_window_get_toplevel (native);
1197 if (native == NULL || GDK_WINDOW_DESTROYED (native))
1198 return GDK_GRAB_NOT_VIEWABLE;
1200 res = GDK_DEVICE_GET_CLASS (device)->grab (device,
1203 get_native_grab_event_mask (event_mask),
1208 if (res == GDK_GRAB_SUCCESS)
1210 GdkDisplay *display;
1213 display = gdk_window_get_display (window);
1214 serial = _gdk_display_get_next_serial (display);
1216 _gdk_display_add_device_grab (display,
1232 * gdk_device_ungrab:
1233 * @device: a #GdkDevice
1234 * @time_: a timestap (e.g. %GDK_CURRENT_TIME).
1236 * Release any grab on @device.
1241 gdk_device_ungrab (GdkDevice *device,
1244 g_return_if_fail (GDK_IS_DEVICE (device));
1246 GDK_DEVICE_GET_CLASS (device)->ungrab (device, time_);
1251 * @device: the device to warp.
1252 * @screen: the screen to warp @device to.
1253 * @x: the X coordinate of the destination.
1254 * @y: the Y coordinate of the destination.
1256 * Warps @device in @display to the point @x,@y on
1257 * the screen @screen, unless the device is confined
1258 * to a window by a grab, in which case it will be moved
1259 * as far as allowed by the grab. Warping the pointer
1260 * creates events as if the user had moved the mouse
1261 * instantaneously to the destination.
1263 * Note that the pointer should normally be under the
1264 * control of the user. This function was added to cover
1265 * some rare use cases like keyboard navigation support
1266 * for the color picker in the #GtkColorSelectionDialog.
1271 gdk_device_warp (GdkDevice *device,
1276 g_return_if_fail (GDK_IS_DEVICE (device));
1277 g_return_if_fail (GDK_IS_SCREEN (screen));
1278 g_return_if_fail (gdk_device_get_display (device) == gdk_screen_get_display (screen));
1280 GDK_DEVICE_GET_CLASS (device)->warp (device, screen, x, y);
1285 _gdk_device_reset_axes (GdkDevice *device)
1289 for (i = device->axes->len - 1; i >= 0; i--)
1290 g_array_remove_index (device->axes, i);
1292 g_object_notify (G_OBJECT (device), "n-axes");
1296 _gdk_device_add_axis (GdkDevice *device,
1303 GdkAxisInfo axis_info;
1306 axis_info.use = use;
1307 axis_info.label = label_atom;
1308 axis_info.min_value = min_value;
1309 axis_info.max_value = max_value;
1310 axis_info.resolution = resolution;
1316 axis_info.min_axis = 0;
1317 axis_info.max_axis = 0;
1319 case GDK_AXIS_XTILT:
1320 case GDK_AXIS_YTILT:
1321 axis_info.min_axis = -1;
1322 axis_info.max_axis = 1;
1325 axis_info.min_axis = 0;
1326 axis_info.max_axis = 1;
1330 device->axes = g_array_append_val (device->axes, axis_info);
1331 pos = device->axes->len - 1;
1333 g_object_notify (G_OBJECT (device), "n-axes");
1339 _gdk_device_set_keys (GdkDevice *device,
1343 g_free (device->keys);
1345 device->num_keys = num_keys;
1346 device->keys = g_new0 (GdkDeviceKey, num_keys);
1349 static GdkAxisInfo *
1350 find_axis_info (GArray *array,
1356 for (i = 0; i < GDK_AXIS_LAST; i++)
1358 info = &g_array_index (array, GdkAxisInfo, i);
1360 if (info->use == use)
1368 _gdk_device_get_axis_use (GdkDevice *device,
1373 info = g_array_index (device->axes, GdkAxisInfo, index_);
1378 _gdk_device_translate_window_coord (GdkDevice *device,
1382 gdouble *axis_value)
1384 GdkAxisInfo axis_info;
1385 GdkAxisInfo *axis_info_x, *axis_info_y;
1386 gdouble device_width, device_height;
1387 gdouble x_offset, y_offset;
1388 gdouble x_scale, y_scale;
1389 gdouble x_min, y_min;
1390 gdouble x_resolution, y_resolution;
1391 gdouble device_aspect;
1392 gint window_width, window_height;
1394 if (index_ >= device->axes->len)
1397 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1399 if (axis_info.use != GDK_AXIS_X &&
1400 axis_info.use != GDK_AXIS_Y)
1403 if (axis_info.use == GDK_AXIS_X)
1405 axis_info_x = &axis_info;
1406 axis_info_y = find_axis_info (device->axes, GDK_AXIS_Y);
1410 axis_info_x = find_axis_info (device->axes, GDK_AXIS_X);
1411 axis_info_y = &axis_info;
1414 device_width = axis_info_x->max_value - axis_info_x->min_value;
1415 device_height = axis_info_y->max_value - axis_info_y->min_value;
1417 if (device_width > 0)
1418 x_min = axis_info_x->min_value;
1421 device_width = gdk_screen_get_width (gdk_window_get_screen (window));
1425 if (device_height > 0)
1426 y_min = axis_info_y->min_value;
1429 device_height = gdk_screen_get_height (gdk_window_get_screen (window));
1433 window_width = gdk_window_get_width (window);
1434 window_height = gdk_window_get_height (window);
1436 x_resolution = axis_info_x->resolution;
1437 y_resolution = axis_info_y->resolution;
1440 * Some drivers incorrectly report the resolution of the device
1441 * as zero (in partiular linuxwacom < 0.5.3 with usb tablets).
1442 * This causes the device_aspect to become NaN and totally
1443 * breaks windowed mode. If this is the case, the best we can
1444 * do is to assume the resolution is non-zero is equal in both
1445 * directions (which is true for many devices). The absolute
1446 * value of the resolution doesn't matter since we only use the
1449 if (x_resolution == 0 || y_resolution == 0)
1455 device_aspect = (device_height * y_resolution) /
1456 (device_width * x_resolution);
1458 if (device_aspect * window_width >= window_height)
1460 /* device taller than window */
1461 x_scale = window_width / device_width;
1462 y_scale = (x_scale * x_resolution) / y_resolution;
1465 y_offset = - (device_height * y_scale - window_height) / 2;
1469 /* window taller than device */
1470 y_scale = window_height / device_height;
1471 x_scale = (y_scale * y_resolution) / x_resolution;
1474 x_offset = - (device_width * x_scale - window_width) / 2;
1479 if (axis_info.use == GDK_AXIS_X)
1480 *axis_value = x_offset + x_scale * (value - x_min);
1482 *axis_value = y_offset + y_scale * (value - y_min);
1489 _gdk_device_translate_screen_coord (GdkDevice *device,
1495 gdouble *axis_value)
1497 GdkAxisInfo axis_info;
1498 gdouble axis_width, scale, offset;
1500 if (device->mode != GDK_MODE_SCREEN)
1503 if (index_ >= device->axes->len)
1506 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1508 if (axis_info.use != GDK_AXIS_X &&
1509 axis_info.use != GDK_AXIS_Y)
1512 axis_width = axis_info.max_value - axis_info.min_value;
1514 if (axis_info.use == GDK_AXIS_X)
1517 scale = gdk_screen_get_width (gdk_window_get_screen (window)) / axis_width;
1521 offset = - window_root_x - window->abs_x;
1526 scale = gdk_screen_get_height (gdk_window_get_screen (window)) / axis_width;
1530 offset = - window_root_y - window->abs_y;
1534 *axis_value = offset + scale * (value - axis_info.min_value);
1540 _gdk_device_translate_axis (GdkDevice *device,
1543 gdouble *axis_value)
1545 GdkAxisInfo axis_info;
1546 gdouble axis_width, out;
1548 if (index_ >= device->axes->len)
1551 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1553 if (axis_info.use == GDK_AXIS_X ||
1554 axis_info.use == GDK_AXIS_Y)
1557 axis_width = axis_info.max_value - axis_info.min_value;
1558 out = (axis_info.max_axis * (value - axis_info.min_value) +
1559 axis_info.min_axis * (axis_info.max_value - value)) / axis_width;
1568 _gdk_device_query_state (GdkDevice *device,
1570 GdkWindow **root_window,
1571 GdkWindow **child_window,
1576 GdkModifierType *mask)
1578 return GDK_DEVICE_GET_CLASS (device)->query_state (device,
1590 _gdk_device_window_at_position (GdkDevice *device,
1593 GdkModifierType *mask,
1594 gboolean get_toplevel)
1596 return GDK_DEVICE_GET_CLASS (device)->window_at_position (device,