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,
421 GdkScreen *default_screen;
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 default_screen = gdk_display_get_default_screen (display);
430 _gdk_device_query_state (device,
431 gdk_screen_get_root_window (default_screen),
437 *screen = gdk_window_get_screen (root);
445 * gdk_device_get_window_at_position:
446 * @device: pointer #GdkDevice to query info to.
447 * @win_x: (out) (allow-none): return location for the X coordinate of the device location,
448 * relative to the window origin, or %NULL.
449 * @win_y: (out) (allow-none): return location for the Y coordinate of the device location,
450 * relative to the window origin, or %NULL.
452 * Obtains the window underneath @device, returning the location of the device in @win_x and @win_y. Returns
453 * %NULL if the window tree under @device is not known to GDK (for example, belongs to another application).
455 * Returns: (transfer none): the #GdkWindow under the device position, or %NULL.
460 gdk_device_get_window_at_position (GdkDevice *device,
468 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
469 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, NULL);
471 display = gdk_device_get_display (device);
473 window = display->device_hooks->window_at_device_position (display, device, &tmp_x, &tmp_y);
484 * gdk_device_get_history:
485 * @device: a #GdkDevice
486 * @window: the window with respect to which which the event coordinates will be reported
487 * @start: starting timestamp for range of events to return
488 * @stop: ending timestamp for the range of events to return
489 * @events: (array length=n_events) (out) (transfer none): location to store a newly-allocated array of #GdkTimeCoord, or %NULL
490 * @n_events: location to store the length of @events, or %NULL
492 * Obtains the motion history for a pointer device; given a starting and
493 * ending timestamp, return all events in the motion history for
494 * the device in the given range of time. Some windowing systems
495 * do not support motion history, in which case, %FALSE will
496 * be returned. (This is not distinguishable from the case where
497 * motion history is supported and no events were found.)
499 * Return value: %TRUE if the windowing system supports motion history and
500 * at least one event was found.
503 gdk_device_get_history (GdkDevice *device,
507 GdkTimeCoord ***events,
510 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
511 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
512 g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
520 if (GDK_WINDOW_DESTROYED (window))
523 if (!GDK_DEVICE_GET_CLASS (device)->get_history)
526 return GDK_DEVICE_GET_CLASS (device)->get_history (device, window,
532 _gdk_device_allocate_history (GdkDevice *device,
535 GdkTimeCoord **result = g_new (GdkTimeCoord *, n_events);
538 for (i = 0; i < n_events; i++)
539 result[i] = g_malloc (sizeof (GdkTimeCoord) -
540 sizeof (double) * (GDK_MAX_TIMECOORD_AXES - device->axes->len));
545 * gdk_device_free_history:
546 * @events: (inout) (transfer none): an array of #GdkTimeCoord.
547 * @n_events: the length of the array.
549 * Frees an array of #GdkTimeCoord that was returned by gdk_device_get_history().
552 gdk_device_free_history (GdkTimeCoord **events,
557 for (i = 0; i < n_events; i++)
564 * gdk_device_get_name:
565 * @device: a #GdkDevice
567 * Determines the name of the device.
569 * Return value: a name
574 gdk_device_get_name (GdkDevice *device)
576 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
582 * gdk_device_get_has_cursor:
583 * @device: a #GdkDevice
585 * Determines whether the pointer follows device motion.
587 * Return value: %TRUE if the pointer follows device motion
592 gdk_device_get_has_cursor (GdkDevice *device)
594 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
595 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
597 return device->has_cursor;
601 * gdk_device_get_source:
602 * @device: a #GdkDevice
604 * Determines the type of the device.
606 * Return value: a #GdkInputSource
611 gdk_device_get_source (GdkDevice *device)
613 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
615 return device->source;
619 * gdk_device_get_mode:
620 * @device: a #GdkDevice
622 * Determines the mode of the device.
624 * Return value: a #GdkInputSource
629 gdk_device_get_mode (GdkDevice *device)
631 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
637 * gdk_device_set_mode:
638 * @device: a #GdkDevice.
639 * @mode: the input mode.
641 * Sets a the mode of an input device. The mode controls if the
642 * device is active and whether the device's range is mapped to the
643 * entire screen or to a single window.
645 * Returns: %TRUE if the mode was successfully changed.
648 gdk_device_set_mode (GdkDevice *device,
651 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
653 if (device->mode == mode)
656 if (mode == GDK_MODE_DISABLED &&
657 gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER)
661 g_object_notify (G_OBJECT (device), "input-mode");
667 * gdk_device_get_n_keys:
668 * @device: a #GdkDevice
670 * Returns the number of keys the device currently has.
672 * Returns: the number of keys.
677 gdk_device_get_n_keys (GdkDevice *device)
679 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
681 return device->num_keys;
685 * gdk_device_get_key:
686 * @device: a #GdkDevice.
687 * @index_: the index of the macro button to get.
688 * @keyval: return value for the keyval.
689 * @modifiers: return value for modifiers.
691 * If @index_ has a valid keyval, this function will return %TRUE
692 * and fill in @keyval and @modifiers with the keyval settings.
694 * Returns: %TRUE if keyval is set for @index.
699 gdk_device_get_key (GdkDevice *device,
702 GdkModifierType *modifiers)
704 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
705 g_return_val_if_fail (index_ < device->num_keys, FALSE);
707 if (!device->keys[index_].keyval &&
708 !device->keys[index_].modifiers)
712 *keyval = device->keys[index_].keyval;
715 *modifiers = device->keys[index_].modifiers;
721 * gdk_device_set_key:
722 * @device: a #GdkDevice
723 * @index_: the index of the macro button to set
724 * @keyval: the keyval to generate
725 * @modifiers: the modifiers to set
727 * Specifies the X key event to generate when a macro button of a device
731 gdk_device_set_key (GdkDevice *device,
734 GdkModifierType modifiers)
736 g_return_if_fail (GDK_IS_DEVICE (device));
737 g_return_if_fail (index_ < device->num_keys);
739 device->keys[index_].keyval = keyval;
740 device->keys[index_].modifiers = modifiers;
744 * gdk_device_get_axis_use:
745 * @device: a pointer #GdkDevice.
746 * @index_: the index of the axis.
748 * Returns the axis use for @index_.
750 * Returns: a #GdkAxisUse specifying how the axis is used.
755 gdk_device_get_axis_use (GdkDevice *device,
760 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_AXIS_IGNORE);
761 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, GDK_AXIS_IGNORE);
762 g_return_val_if_fail (index_ < device->axes->len, GDK_AXIS_IGNORE);
764 info = &g_array_index (device->axes, GdkAxisInfo, index_);
770 * gdk_device_set_axis_use:
771 * @device: a pointer #GdkDevice
772 * @index_: the index of the axis
773 * @use: specifies how the axis is used
775 * Specifies how an axis of a device is used.
778 gdk_device_set_axis_use (GdkDevice *device,
784 g_return_if_fail (GDK_IS_DEVICE (device));
785 g_return_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD);
786 g_return_if_fail (index_ < device->axes->len);
788 info = &g_array_index (device->axes, GdkAxisInfo, index_);
811 * gdk_device_get_display:
812 * @device: a #GdkDevice
814 * Returns the #GdkDisplay to which @device pertains.
816 * Returns: (transfer none): a #GdkDisplay. This memory is owned
817 * by GTK+, and must not be freed or unreffed.
822 gdk_device_get_display (GdkDevice *device)
824 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
826 return device->display;
830 * gdk_device_get_associated_device:
831 * @device: a #GdkDevice
833 * Returns the associated device to @device, if @device is of type
834 * %GDK_DEVICE_TYPE_MASTER, it will return the paired pointer or
837 * If @device is of type %GDK_DEVICE_TYPE_SLAVE, it will return
838 * the master device to which @device is attached to.
840 * If @device is of type %GDK_DEVICE_TYPE_FLOATING, %NULL will be
841 * returned, as there is no associated device.
843 * Returns: (transfer none): The associated device, or %NULL
848 gdk_device_get_associated_device (GdkDevice *device)
850 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
852 return device->associated;
856 _gdk_device_set_device_type (GdkDevice *device,
859 if (device->type != type)
863 g_object_notify (G_OBJECT (device), "type");
868 _gdk_device_set_associated_device (GdkDevice *device,
869 GdkDevice *associated)
871 g_return_if_fail (GDK_IS_DEVICE (device));
872 g_return_if_fail (associated == NULL || GDK_IS_DEVICE (associated));
874 if (device->associated == associated)
877 if (device->associated)
879 g_object_unref (device->associated);
880 device->associated = NULL;
884 device->associated = g_object_ref (associated);
886 if (device->type != GDK_DEVICE_TYPE_MASTER)
888 if (device->associated)
889 _gdk_device_set_device_type (device, GDK_DEVICE_TYPE_SLAVE);
891 _gdk_device_set_device_type (device, GDK_DEVICE_TYPE_FLOATING);
896 * gdk_device_list_slave_devices:
897 * @device: a #GdkDevice
899 * If the device if of type %GDK_DEVICE_TYPE_MASTER, it will return
900 * the list of slave devices attached to it, otherwise it will return
903 * Returns: (transfer container): the list of slave devices, or %NULL. The
904 * list must be freed with g_list_free(), the contents of the list
905 * are owned by GTK+ and should not be freed.
908 gdk_device_list_slave_devices (GdkDevice *device)
910 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
911 g_return_val_if_fail (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER, NULL);
913 return g_list_copy (device->slaves);
917 _gdk_device_add_slave (GdkDevice *device,
920 g_return_if_fail (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER);
921 g_return_if_fail (gdk_device_get_device_type (slave) != GDK_DEVICE_TYPE_MASTER);
923 if (!g_list_find (device->slaves, slave))
924 device->slaves = g_list_prepend (device->slaves, slave);
928 _gdk_device_remove_slave (GdkDevice *device,
933 g_return_if_fail (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER);
934 g_return_if_fail (gdk_device_get_device_type (slave) != GDK_DEVICE_TYPE_MASTER);
936 elem = g_list_find (device->slaves, slave);
941 device->slaves = g_list_delete_link (device->slaves, elem);
945 * gdk_device_get_device_type:
946 * @device: a #GdkDevice
948 * Returns the device type for @device.
950 * Returns: the #GdkDeviceType for @device.
955 gdk_device_get_device_type (GdkDevice *device)
957 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_DEVICE_TYPE_MASTER);
963 * gdk_device_get_n_axes:
964 * @device: a pointer #GdkDevice
966 * Returns the number of axes the device currently has.
968 * Returns: the number of axes.
973 gdk_device_get_n_axes (GdkDevice *device)
975 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
976 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, 0);
978 return device->axes->len;
982 * gdk_device_list_axes:
983 * @device: a pointer #GdkDevice
985 * Returns a #GList of #GdkAtom<!-- -->s, containing the labels for
986 * the axes that @device currently has.
988 * Returns: (transfer container) (element-type GdkAtom):
989 * A #GList of #GdkAtom<!-- -->s, free with g_list_free().
994 gdk_device_list_axes (GdkDevice *device)
999 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
1000 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, NULL);
1002 for (i = 0; i < device->axes->len; i++)
1004 GdkAxisInfo axis_info;
1006 axis_info = g_array_index (device->axes, GdkAxisInfo, i);
1007 axes = g_list_prepend (axes, GDK_ATOM_TO_POINTER (axis_info.label));
1010 return g_list_reverse (axes);
1014 * gdk_device_get_axis_value:
1015 * @device: a pointer #GdkDevice.
1016 * @axes: pointer to an array of axes
1017 * @axis_label: #GdkAtom with the axis label.
1018 * @value: location to store the found value.
1020 * Interprets an array of double as axis values for a given device,
1021 * and locates the value in the array for a given axis label, as returned
1022 * by gdk_device_list_axes()
1024 * Returns: %TRUE if the given axis use was found, otherwise %FALSE.
1029 gdk_device_get_axis_value (GdkDevice *device,
1036 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
1037 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
1042 for (i = 0; i < device->axes->len; i++)
1044 GdkAxisInfo axis_info;
1046 axis_info = g_array_index (device->axes, GdkAxisInfo, i);
1048 if (axis_info.label != axis_label)
1061 * gdk_device_get_axis:
1062 * @device: a #GdkDevice
1063 * @axes: pointer to an array of axes
1064 * @use: the use to look for
1065 * @value: location to store the found value.
1067 * Interprets an array of double as axis values for a given device,
1068 * and locates the value in the array for a given axis use.
1070 * Return value: %TRUE if the given axis use was found, otherwise %FALSE
1073 gdk_device_get_axis (GdkDevice *device,
1080 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
1081 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
1086 g_return_val_if_fail (device->axes != NULL, FALSE);
1088 for (i = 0; i < device->axes->len; i++)
1090 GdkAxisInfo axis_info;
1092 axis_info = g_array_index (device->axes, GdkAxisInfo, i);
1094 if (axis_info.use != use)
1107 get_native_grab_event_mask (GdkEventMask grab_mask)
1109 /* Similar to the above but for pointer events only */
1111 GDK_POINTER_MOTION_MASK |
1112 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1113 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1116 ~(GDK_POINTER_MOTION_HINT_MASK |
1117 GDK_BUTTON_MOTION_MASK |
1118 GDK_BUTTON1_MOTION_MASK |
1119 GDK_BUTTON2_MOTION_MASK |
1120 GDK_BUTTON3_MOTION_MASK));
1125 * @device: a #GdkDevice. To get the device you can use gtk_get_current_event_device()
1126 * or gdk_event_get_device() if the grab is in reaction to an event. Also, you can use
1127 * gdk_device_manager_get_client_pointer() but only in code that isn't triggered by a
1128 * #GdkEvent and there aren't other means to get a meaningful #GdkDevice to operate on.
1129 * @window: the #GdkWindow which will own the grab (the grab window)
1130 * @grab_ownership: specifies the grab ownership.
1131 * @owner_events: if %FALSE then all device events are reported with respect to
1132 * @window and are only reported if selected by @event_mask. If
1133 * %TRUE then pointer events for this application are reported
1134 * as normal, but pointer events outside this application are
1135 * reported with respect to @window and only if selected by
1136 * @event_mask. In either mode, unreported events are discarded.
1137 * @event_mask: specifies the event mask, which is used in accordance with
1139 * @cursor: the cursor to display while the grab is active if the device is
1140 * a pointer. If this is %NULL then the normal cursors are used for
1141 * @window and its descendants, and the cursor for @window is used
1143 * @time_: the timestamp of the event which led to this pointer grab. This
1144 * usually comes from the #GdkEvent struct, though %GDK_CURRENT_TIME
1145 * can be used if the time isn't known.
1147 * Grabs the device so that all events coming from this device are passed to
1148 * this application until the device is ungrabbed with gdk_device_ungrab(),
1149 * or the window becomes unviewable. This overrides any previous grab on the device
1152 * Device grabs are used for operations which need complete control over the
1153 * given device events (either pointer or keyboard). For example in GTK+ this
1154 * is used for Drag and Drop operations, popup menus and such.
1156 * Note that if the event mask of an X window has selected both button press
1157 * and button release events, then a button press event will cause an automatic
1158 * pointer grab until the button is released. X does this automatically since
1159 * most applications expect to receive button press and release events in pairs.
1160 * It is equivalent to a pointer grab on the window with @owner_events set to
1163 * If you set up anything at the time you take the grab that needs to be
1164 * cleaned up when the grab ends, you should handle the #GdkEventGrabBroken
1165 * events that are emitted when the grab ends unvoluntarily.
1167 * Returns: %GDK_GRAB_SUCCESS if the grab was successful.
1172 gdk_device_grab (GdkDevice *device,
1174 GdkGrabOwnership grab_ownership,
1175 gboolean owner_events,
1176 GdkEventMask event_mask,
1183 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_GRAB_SUCCESS);
1184 g_return_val_if_fail (GDK_IS_WINDOW (window), GDK_GRAB_SUCCESS);
1186 if (_gdk_native_windows)
1189 native = gdk_window_get_toplevel (window);
1191 while (native->window_type == GDK_WINDOW_OFFSCREEN)
1193 native = gdk_offscreen_window_get_embedder (native);
1195 if (native == NULL ||
1196 (!_gdk_window_has_impl (native) &&
1197 !gdk_window_is_viewable (native)))
1198 return GDK_GRAB_NOT_VIEWABLE;
1200 native = gdk_window_get_toplevel (native);
1203 if (native == NULL || GDK_WINDOW_DESTROYED (native))
1204 return GDK_GRAB_NOT_VIEWABLE;
1206 res = GDK_DEVICE_GET_CLASS (device)->grab (device,
1209 get_native_grab_event_mask (event_mask),
1214 if (res == GDK_GRAB_SUCCESS)
1216 GdkDisplay *display;
1219 display = gdk_window_get_display (window);
1220 serial = _gdk_display_get_next_serial (display);
1222 _gdk_display_add_device_grab (display,
1238 * gdk_device_ungrab:
1239 * @device: a #GdkDevice
1240 * @time_: a timestap (e.g. %GDK_CURRENT_TIME).
1242 * Release any grab on @device.
1247 gdk_device_ungrab (GdkDevice *device,
1250 g_return_if_fail (GDK_IS_DEVICE (device));
1252 GDK_DEVICE_GET_CLASS (device)->ungrab (device, time_);
1257 * @device: the device to warp.
1258 * @screen: the screen to warp @device to.
1259 * @x: the X coordinate of the destination.
1260 * @y: the Y coordinate of the destination.
1262 * Warps @device in @display to the point @x,@y on
1263 * the screen @screen, unless the device is confined
1264 * to a window by a grab, in which case it will be moved
1265 * as far as allowed by the grab. Warping the pointer
1266 * creates events as if the user had moved the mouse
1267 * instantaneously to the destination.
1269 * Note that the pointer should normally be under the
1270 * control of the user. This function was added to cover
1271 * some rare use cases like keyboard navigation support
1272 * for the color picker in the #GtkColorSelectionDialog.
1277 gdk_device_warp (GdkDevice *device,
1282 g_return_if_fail (GDK_IS_DEVICE (device));
1283 g_return_if_fail (GDK_IS_SCREEN (screen));
1284 g_return_if_fail (gdk_device_get_display (device) == gdk_screen_get_display (screen));
1286 GDK_DEVICE_GET_CLASS (device)->warp (device, screen, x, y);
1291 _gdk_device_reset_axes (GdkDevice *device)
1295 for (i = device->axes->len - 1; i >= 0; i--)
1296 g_array_remove_index (device->axes, i);
1298 g_object_notify (G_OBJECT (device), "n-axes");
1302 _gdk_device_add_axis (GdkDevice *device,
1309 GdkAxisInfo axis_info;
1312 axis_info.use = use;
1313 axis_info.label = label_atom;
1314 axis_info.min_value = min_value;
1315 axis_info.max_value = max_value;
1316 axis_info.resolution = resolution;
1322 axis_info.min_axis = 0;
1323 axis_info.max_axis = 0;
1325 case GDK_AXIS_XTILT:
1326 case GDK_AXIS_YTILT:
1327 axis_info.min_axis = -1;
1328 axis_info.max_axis = 1;
1331 axis_info.min_axis = 0;
1332 axis_info.max_axis = 1;
1336 device->axes = g_array_append_val (device->axes, axis_info);
1337 pos = device->axes->len - 1;
1339 g_object_notify (G_OBJECT (device), "n-axes");
1345 _gdk_device_set_keys (GdkDevice *device,
1349 g_free (device->keys);
1351 device->num_keys = num_keys;
1352 device->keys = g_new0 (GdkDeviceKey, num_keys);
1355 static GdkAxisInfo *
1356 find_axis_info (GArray *array,
1362 for (i = 0; i < GDK_AXIS_LAST; i++)
1364 info = &g_array_index (array, GdkAxisInfo, i);
1366 if (info->use == use)
1374 _gdk_device_get_axis_use (GdkDevice *device,
1379 info = g_array_index (device->axes, GdkAxisInfo, index_);
1384 _gdk_device_translate_window_coord (GdkDevice *device,
1388 gdouble *axis_value)
1390 GdkAxisInfo axis_info;
1391 GdkAxisInfo *axis_info_x, *axis_info_y;
1392 gdouble device_width, device_height;
1393 gdouble x_offset, y_offset;
1394 gdouble x_scale, y_scale;
1395 gdouble x_min, y_min;
1396 gdouble x_resolution, y_resolution;
1397 gdouble device_aspect;
1398 gint window_width, window_height;
1400 if (index_ >= device->axes->len)
1403 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1405 if (axis_info.use != GDK_AXIS_X &&
1406 axis_info.use != GDK_AXIS_Y)
1409 if (axis_info.use == GDK_AXIS_X)
1411 axis_info_x = &axis_info;
1412 axis_info_y = find_axis_info (device->axes, GDK_AXIS_Y);
1416 axis_info_x = find_axis_info (device->axes, GDK_AXIS_X);
1417 axis_info_y = &axis_info;
1420 device_width = axis_info_x->max_value - axis_info_x->min_value;
1421 device_height = axis_info_y->max_value - axis_info_y->min_value;
1423 if (device_width > 0)
1424 x_min = axis_info_x->min_value;
1427 device_width = gdk_screen_get_width (gdk_window_get_screen (window));
1431 if (device_height > 0)
1432 y_min = axis_info_y->min_value;
1435 device_height = gdk_screen_get_height (gdk_window_get_screen (window));
1439 window_width = gdk_window_get_width (window);
1440 window_height = gdk_window_get_height (window);
1442 x_resolution = axis_info_x->resolution;
1443 y_resolution = axis_info_y->resolution;
1446 * Some drivers incorrectly report the resolution of the device
1447 * as zero (in partiular linuxwacom < 0.5.3 with usb tablets).
1448 * This causes the device_aspect to become NaN and totally
1449 * breaks windowed mode. If this is the case, the best we can
1450 * do is to assume the resolution is non-zero is equal in both
1451 * directions (which is true for many devices). The absolute
1452 * value of the resolution doesn't matter since we only use the
1455 if (x_resolution == 0 || y_resolution == 0)
1461 device_aspect = (device_height * y_resolution) /
1462 (device_width * x_resolution);
1464 if (device_aspect * window_width >= window_height)
1466 /* device taller than window */
1467 x_scale = window_width / device_width;
1468 y_scale = (x_scale * x_resolution) / y_resolution;
1471 y_offset = - (device_height * y_scale - window_height) / 2;
1475 /* window taller than device */
1476 y_scale = window_height / device_height;
1477 x_scale = (y_scale * y_resolution) / x_resolution;
1480 x_offset = - (device_width * x_scale - window_width) / 2;
1485 if (axis_info.use == GDK_AXIS_X)
1486 *axis_value = x_offset + x_scale * (value - x_min);
1488 *axis_value = y_offset + y_scale * (value - y_min);
1495 _gdk_device_translate_screen_coord (GdkDevice *device,
1501 gdouble *axis_value)
1503 GdkAxisInfo axis_info;
1504 gdouble axis_width, scale, offset;
1506 if (device->mode != GDK_MODE_SCREEN)
1509 if (index_ >= device->axes->len)
1512 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1514 if (axis_info.use != GDK_AXIS_X &&
1515 axis_info.use != GDK_AXIS_Y)
1518 axis_width = axis_info.max_value - axis_info.min_value;
1520 if (axis_info.use == GDK_AXIS_X)
1523 scale = gdk_screen_get_width (gdk_window_get_screen (window)) / axis_width;
1527 offset = - window_root_x - window->abs_x;
1532 scale = gdk_screen_get_height (gdk_window_get_screen (window)) / axis_width;
1536 offset = - window_root_y - window->abs_y;
1540 *axis_value = offset + scale * (value - axis_info.min_value);
1546 _gdk_device_translate_axis (GdkDevice *device,
1549 gdouble *axis_value)
1551 GdkAxisInfo axis_info;
1552 gdouble axis_width, out;
1554 if (index_ >= device->axes->len)
1557 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1559 if (axis_info.use == GDK_AXIS_X ||
1560 axis_info.use == GDK_AXIS_Y)
1563 axis_width = axis_info.max_value - axis_info.min_value;
1564 out = (axis_info.max_axis * (value - axis_info.min_value) +
1565 axis_info.min_axis * (axis_info.max_value - value)) / axis_width;
1574 _gdk_device_query_state (GdkDevice *device,
1576 GdkWindow **root_window,
1577 GdkWindow **child_window,
1582 GdkModifierType *mask)
1584 return GDK_DEVICE_GET_CLASS (device)->query_state (device,
1596 _gdk_device_window_at_position (GdkDevice *device,
1599 GdkModifierType *mask,
1600 gboolean get_toplevel)
1602 return GDK_DEVICE_GET_CLASS (device)->window_at_position (device,