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.
24 #include "gdkdeviceprivate.h"
25 #include "gdkdisplayprivate.h"
26 #include "gdkinternals.h"
31 * @Short_description: Object representing an input device
33 * @See_also: #GdkDeviceManager
35 * The #GdkDevice object represents a single input device, such
36 * as a keyboard, a mouse, a touchpad, etc.
38 * See the #GdkDeviceManager documentation for more information
39 * about the various kinds of master and slave devices, and their
43 typedef struct _GdkAxisInfo GdkAxisInfo;
62 static guint signals [LAST_SIGNAL] = { 0 };
65 static void gdk_device_dispose (GObject *object);
66 static void gdk_device_set_property (GObject *object,
70 static void gdk_device_get_property (GObject *object,
76 G_DEFINE_ABSTRACT_TYPE (GdkDevice, gdk_device, G_TYPE_OBJECT)
83 PROP_ASSOCIATED_DEVICE,
93 gdk_device_class_init (GdkDeviceClass *klass)
95 GObjectClass *object_class = G_OBJECT_CLASS (klass);
97 object_class->dispose = gdk_device_dispose;
98 object_class->set_property = gdk_device_set_property;
99 object_class->get_property = gdk_device_get_property;
104 * The #GdkDisplay the #GdkDevice pertains to.
108 g_object_class_install_property (object_class,
110 g_param_spec_object ("display",
111 P_("Device Display"),
112 P_("Display which the device belongs to"),
114 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
115 G_PARAM_STATIC_STRINGS));
117 * GdkDevice:device-manager:
119 * The #GdkDeviceManager the #GdkDevice pertains to.
123 g_object_class_install_property (object_class,
125 g_param_spec_object ("device-manager",
126 P_("Device manager"),
127 P_("Device manager which the device belongs to"),
128 GDK_TYPE_DEVICE_MANAGER,
129 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
130 G_PARAM_STATIC_STRINGS));
138 g_object_class_install_property (object_class,
140 g_param_spec_string ("name",
144 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
145 G_PARAM_STATIC_STRINGS));
149 * Device role in the device manager.
153 g_object_class_install_property (object_class,
155 g_param_spec_enum ("type",
157 P_("Device role in the device manager"),
158 GDK_TYPE_DEVICE_TYPE,
159 GDK_DEVICE_TYPE_MASTER,
160 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
161 G_PARAM_STATIC_STRINGS));
163 * GdkDevice:associated-device:
165 * Associated pointer or keyboard with this device, if any. Devices of type #GDK_DEVICE_TYPE_MASTER
166 * always come in keyboard/pointer pairs. Other device types will have a %NULL associated device.
170 g_object_class_install_property (object_class,
171 PROP_ASSOCIATED_DEVICE,
172 g_param_spec_object ("associated-device",
173 P_("Associated device"),
174 P_("Associated pointer or keyboard with this device"),
176 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
178 * GdkDevice:input-source:
180 * Source type for the device.
184 g_object_class_install_property (object_class,
186 g_param_spec_enum ("input-source",
188 P_("Source type for the device"),
189 GDK_TYPE_INPUT_SOURCE,
191 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
192 G_PARAM_STATIC_STRINGS));
194 * GdkDevice:input-mode:
196 * Input mode for the device.
200 g_object_class_install_property (object_class,
202 g_param_spec_enum ("input-mode",
203 P_("Input mode for the device"),
204 P_("Input mode for the device"),
207 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
209 * GdkDevice:has-cursor:
211 * Whether the device is represented by a cursor on the screen. Devices of type
212 * %GDK_DEVICE_TYPE_MASTER will have %TRUE here.
216 g_object_class_install_property (object_class,
218 g_param_spec_boolean ("has-cursor",
219 P_("Whether the device has a cursor"),
220 P_("Whether there is a visible cursor following device motion"),
222 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
223 G_PARAM_STATIC_STRINGS));
227 * Number of axes in the device.
231 g_object_class_install_property (object_class,
233 g_param_spec_uint ("n-axes",
234 P_("Number of axes in the device"),
235 P_("Number of axes in the device"),
237 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
240 * GdkDevice::changed:
241 * @device: the #GdkDevice that changed.
243 * The ::changed signal is emitted either when the #GdkDevice
244 * has changed the number of either axes or keys. For example
245 * In X this will normally happen when the slave device routing
246 * events through the master device changes (for example, user
247 * switches from the USB mouse to a tablet), in that case the
248 * master device will change to reflect the new slave device
252 g_signal_new (g_intern_static_string ("changed"),
253 G_TYPE_FROM_CLASS (object_class),
256 g_cclosure_marshal_VOID__VOID,
261 gdk_device_init (GdkDevice *device)
263 device->axes = g_array_new (FALSE, TRUE, sizeof (GdkAxisInfo));
267 gdk_device_dispose (GObject *object)
269 GdkDevice *device = GDK_DEVICE (object);
271 if (device->type == GDK_DEVICE_TYPE_SLAVE)
272 _gdk_device_remove_slave (device->associated, device);
274 if (device->associated)
276 if (device->type == GDK_DEVICE_TYPE_MASTER)
277 _gdk_device_set_associated_device (device->associated, NULL);
279 g_object_unref (device->associated);
280 device->associated = NULL;
285 g_array_free (device->axes, TRUE);
289 g_free (device->name);
290 g_free (device->keys);
295 G_OBJECT_CLASS (gdk_device_parent_class)->dispose (object);
299 gdk_device_set_property (GObject *object,
304 GdkDevice *device = GDK_DEVICE (object);
309 device->display = g_value_get_object (value);
311 case PROP_DEVICE_MANAGER:
312 device->manager = g_value_get_object (value);
316 g_free (device->name);
318 device->name = g_value_dup_string (value);
321 device->type = g_value_get_enum (value);
323 case PROP_INPUT_SOURCE:
324 device->source = g_value_get_enum (value);
326 case PROP_INPUT_MODE:
327 gdk_device_set_mode (device, g_value_get_enum (value));
329 case PROP_HAS_CURSOR:
330 device->has_cursor = g_value_get_boolean (value);
333 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
339 gdk_device_get_property (GObject *object,
344 GdkDevice *device = GDK_DEVICE (object);
349 g_value_set_object (value, device->display);
351 case PROP_DEVICE_MANAGER:
352 g_value_set_object (value, device->manager);
354 case PROP_ASSOCIATED_DEVICE:
355 g_value_set_object (value, device->associated);
358 g_value_set_string (value, device->name);
361 g_value_set_enum (value, device->type);
363 case PROP_INPUT_SOURCE:
364 g_value_set_enum (value, device->source);
366 case PROP_INPUT_MODE:
367 g_value_set_enum (value, device->mode);
369 case PROP_HAS_CURSOR:
370 g_value_set_boolean (value, device->has_cursor);
373 g_value_set_uint (value, device->axes->len);
376 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
382 * gdk_device_get_state: (skip)
383 * @device: a #GdkDevice.
384 * @window: a #GdkWindow.
385 * @axes: an array of doubles to store the values of the axes of @device in,
387 * @mask: location to store the modifiers, or %NULL.
389 * Gets the current state of a pointer device relative to @window. As a slave
390 * device coordinates are those of its master pointer, This
391 * function may not be called on devices of type %GDK_DEVICE_TYPE_SLAVE,
392 * unless there is an ongoing grab on them, see gdk_device_grab().
395 gdk_device_get_state (GdkDevice *device,
398 GdkModifierType *mask)
400 g_return_if_fail (GDK_IS_DEVICE (device));
401 g_return_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD);
402 g_return_if_fail (GDK_IS_WINDOW (window));
403 g_return_if_fail (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_SLAVE ||
404 gdk_display_device_is_grabbed (gdk_device_get_display (device), device));
406 if (GDK_DEVICE_GET_CLASS (device)->get_state)
407 GDK_DEVICE_GET_CLASS (device)->get_state (device, window, axes, mask);
411 * gdk_device_get_position:
412 * @device: pointer device to query status about.
413 * @screen: (out) (transfer none) (allow-none): location to store the #GdkScreen
414 * the @device is on, or %NULL.
415 * @x: (out) (allow-none): location to store root window X coordinate of @device, or %NULL.
416 * @y: (out) (allow-none): location to store root window Y coordinate of @device, or %NULL.
418 * Gets the current location of @device. As a slave device
419 * coordinates are those of its master pointer, This function
420 * may not be called on devices of type %GDK_DEVICE_TYPE_SLAVE,
421 * unless there is an ongoing grab on them, see gdk_device_grab().
426 gdk_device_get_position (GdkDevice *device,
433 GdkScreen *default_screen;
436 g_return_if_fail (GDK_IS_DEVICE (device));
437 g_return_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD);
439 display = gdk_device_get_display (device);
441 g_return_if_fail (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_SLAVE ||
442 gdk_display_device_is_grabbed (display, device));
444 default_screen = gdk_display_get_default_screen (display);
446 _gdk_device_query_state (device,
447 gdk_screen_get_root_window (default_screen),
453 *screen = gdk_window_get_screen (root);
461 * gdk_device_get_window_at_position:
462 * @device: pointer #GdkDevice to query info to.
463 * @win_x: (out) (allow-none): return location for the X coordinate of the device location,
464 * relative to the window origin, or %NULL.
465 * @win_y: (out) (allow-none): return location for the Y coordinate of the device location,
466 * relative to the window origin, or %NULL.
468 * Obtains the window underneath @device, returning the location of the device in @win_x and @win_y. Returns
469 * %NULL if the window tree under @device is not known to GDK (for example, belongs to another application).
471 * As a slave device coordinates are those of its master pointer, This
472 * function may not be called on devices of type %GDK_DEVICE_TYPE_SLAVE,
473 * unless there is an ongoing grab on them, see gdk_device_grab().
475 * Returns: (transfer none): the #GdkWindow under the device position, or %NULL.
480 gdk_device_get_window_at_position (GdkDevice *device,
487 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
488 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, NULL);
489 g_return_val_if_fail (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_SLAVE ||
490 gdk_display_device_is_grabbed (gdk_device_get_display (device), device), NULL);
492 window = _gdk_device_window_at_position (device, &tmp_x, &tmp_y, NULL, FALSE);
494 /* This might need corrections, as the native window returned
495 may contain client side children */
500 window = _gdk_window_find_descendant_at (window,
503 tmp_x = floor (xx + 0.5);
504 tmp_y = floor (yy + 0.5);
516 * gdk_device_get_history: (skip)
517 * @device: a #GdkDevice
518 * @window: the window with respect to which which the event coordinates will be reported
519 * @start: starting timestamp for range of events to return
520 * @stop: ending timestamp for the range of events to return
521 * @events: (array length=n_events) (out) (transfer full): location to store a newly-allocated array of #GdkTimeCoord, or %NULL
522 * @n_events: location to store the length of @events, or %NULL
524 * Obtains the motion history for a pointer device; given a starting and
525 * ending timestamp, return all events in the motion history for
526 * the device in the given range of time. Some windowing systems
527 * do not support motion history, in which case, %FALSE will
528 * be returned. (This is not distinguishable from the case where
529 * motion history is supported and no events were found.)
531 * Return value: %TRUE if the windowing system supports motion history and
532 * at least one event was found.
535 gdk_device_get_history (GdkDevice *device,
539 GdkTimeCoord ***events,
542 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
543 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
544 g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
552 if (GDK_WINDOW_DESTROYED (window))
555 if (!GDK_DEVICE_GET_CLASS (device)->get_history)
558 return GDK_DEVICE_GET_CLASS (device)->get_history (device, window,
564 _gdk_device_allocate_history (GdkDevice *device,
567 GdkTimeCoord **result = g_new (GdkTimeCoord *, n_events);
570 for (i = 0; i < n_events; i++)
571 result[i] = g_malloc (sizeof (GdkTimeCoord) -
572 sizeof (double) * (GDK_MAX_TIMECOORD_AXES - device->axes->len));
577 * gdk_device_free_history: (skip)
578 * @events: an array of #GdkTimeCoord.
579 * @n_events: the length of the array.
581 * Frees an array of #GdkTimeCoord that was returned by gdk_device_get_history().
584 gdk_device_free_history (GdkTimeCoord **events,
589 for (i = 0; i < n_events; i++)
596 * gdk_device_get_name:
597 * @device: a #GdkDevice
599 * Determines the name of the device.
601 * Return value: a name
606 gdk_device_get_name (GdkDevice *device)
608 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
614 * gdk_device_get_has_cursor:
615 * @device: a #GdkDevice
617 * Determines whether the pointer follows device motion.
619 * Return value: %TRUE if the pointer follows device motion
624 gdk_device_get_has_cursor (GdkDevice *device)
626 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
627 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
629 return device->has_cursor;
633 * gdk_device_get_source:
634 * @device: a #GdkDevice
636 * Determines the type of the device.
638 * Return value: a #GdkInputSource
643 gdk_device_get_source (GdkDevice *device)
645 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
647 return device->source;
651 * gdk_device_get_mode:
652 * @device: a #GdkDevice
654 * Determines the mode of the device.
656 * Return value: a #GdkInputSource
661 gdk_device_get_mode (GdkDevice *device)
663 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
669 * gdk_device_set_mode:
670 * @device: a #GdkDevice.
671 * @mode: the input mode.
673 * Sets a the mode of an input device. The mode controls if the
674 * device is active and whether the device's range is mapped to the
675 * entire screen or to a single window.
677 * Returns: %TRUE if the mode was successfully changed.
680 gdk_device_set_mode (GdkDevice *device,
683 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
685 if (device->mode == mode)
688 if (mode == GDK_MODE_DISABLED &&
689 gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER)
693 g_object_notify (G_OBJECT (device), "input-mode");
699 * gdk_device_get_n_keys:
700 * @device: a #GdkDevice
702 * Returns the number of keys the device currently has.
704 * Returns: the number of keys.
709 gdk_device_get_n_keys (GdkDevice *device)
711 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
713 return device->num_keys;
717 * gdk_device_get_key:
718 * @device: a #GdkDevice.
719 * @index_: the index of the macro button to get.
720 * @keyval: (out): return value for the keyval.
721 * @modifiers: (out): return value for modifiers.
723 * If @index_ has a valid keyval, this function will return %TRUE
724 * and fill in @keyval and @modifiers with the keyval settings.
726 * Returns: %TRUE if keyval is set for @index.
731 gdk_device_get_key (GdkDevice *device,
734 GdkModifierType *modifiers)
736 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
737 g_return_val_if_fail (index_ < device->num_keys, FALSE);
739 if (!device->keys[index_].keyval &&
740 !device->keys[index_].modifiers)
744 *keyval = device->keys[index_].keyval;
747 *modifiers = device->keys[index_].modifiers;
753 * gdk_device_set_key:
754 * @device: a #GdkDevice
755 * @index_: the index of the macro button to set
756 * @keyval: the keyval to generate
757 * @modifiers: the modifiers to set
759 * Specifies the X key event to generate when a macro button of a device
763 gdk_device_set_key (GdkDevice *device,
766 GdkModifierType modifiers)
768 g_return_if_fail (GDK_IS_DEVICE (device));
769 g_return_if_fail (index_ < device->num_keys);
771 device->keys[index_].keyval = keyval;
772 device->keys[index_].modifiers = modifiers;
776 * gdk_device_get_axis_use:
777 * @device: a pointer #GdkDevice.
778 * @index_: the index of the axis.
780 * Returns the axis use for @index_.
782 * Returns: a #GdkAxisUse specifying how the axis is used.
787 gdk_device_get_axis_use (GdkDevice *device,
792 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_AXIS_IGNORE);
793 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, GDK_AXIS_IGNORE);
794 g_return_val_if_fail (index_ < device->axes->len, GDK_AXIS_IGNORE);
796 info = &g_array_index (device->axes, GdkAxisInfo, index_);
802 * gdk_device_set_axis_use:
803 * @device: a pointer #GdkDevice
804 * @index_: the index of the axis
805 * @use: specifies how the axis is used
807 * Specifies how an axis of a device is used.
810 gdk_device_set_axis_use (GdkDevice *device,
816 g_return_if_fail (GDK_IS_DEVICE (device));
817 g_return_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD);
818 g_return_if_fail (index_ < device->axes->len);
820 info = &g_array_index (device->axes, GdkAxisInfo, index_);
843 * gdk_device_get_display:
844 * @device: a #GdkDevice
846 * Returns the #GdkDisplay to which @device pertains.
848 * Returns: (transfer none): a #GdkDisplay. This memory is owned
849 * by GTK+, and must not be freed or unreffed.
854 gdk_device_get_display (GdkDevice *device)
856 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
858 return device->display;
862 * gdk_device_get_associated_device:
863 * @device: a #GdkDevice
865 * Returns the associated device to @device, if @device is of type
866 * %GDK_DEVICE_TYPE_MASTER, it will return the paired pointer or
869 * If @device is of type %GDK_DEVICE_TYPE_SLAVE, it will return
870 * the master device to which @device is attached to.
872 * If @device is of type %GDK_DEVICE_TYPE_FLOATING, %NULL will be
873 * returned, as there is no associated device.
875 * Returns: (transfer none): The associated device, or %NULL
880 gdk_device_get_associated_device (GdkDevice *device)
882 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
884 return device->associated;
888 _gdk_device_set_device_type (GdkDevice *device,
891 if (device->type != type)
895 g_object_notify (G_OBJECT (device), "type");
900 _gdk_device_set_associated_device (GdkDevice *device,
901 GdkDevice *associated)
903 g_return_if_fail (GDK_IS_DEVICE (device));
904 g_return_if_fail (associated == NULL || GDK_IS_DEVICE (associated));
906 if (device->associated == associated)
909 if (device->associated)
911 g_object_unref (device->associated);
912 device->associated = NULL;
916 device->associated = g_object_ref (associated);
918 if (device->type != GDK_DEVICE_TYPE_MASTER)
920 if (device->associated)
921 _gdk_device_set_device_type (device, GDK_DEVICE_TYPE_SLAVE);
923 _gdk_device_set_device_type (device, GDK_DEVICE_TYPE_FLOATING);
928 * gdk_device_list_slave_devices:
929 * @device: a #GdkDevice
931 * If the device if of type %GDK_DEVICE_TYPE_MASTER, it will return
932 * the list of slave devices attached to it, otherwise it will return
935 * Returns: (transfer container) (element-type GdkDevice): the list of
936 * slave devices, or %NULL. The list must be freed with
937 * g_list_free(), the contents of the list are owned by GTK+
938 * and should not be freed.
941 gdk_device_list_slave_devices (GdkDevice *device)
943 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
944 g_return_val_if_fail (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER, NULL);
946 return g_list_copy (device->slaves);
950 _gdk_device_add_slave (GdkDevice *device,
953 g_return_if_fail (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER);
954 g_return_if_fail (gdk_device_get_device_type (slave) != GDK_DEVICE_TYPE_MASTER);
956 if (!g_list_find (device->slaves, slave))
957 device->slaves = g_list_prepend (device->slaves, slave);
961 _gdk_device_remove_slave (GdkDevice *device,
966 g_return_if_fail (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER);
967 g_return_if_fail (gdk_device_get_device_type (slave) != GDK_DEVICE_TYPE_MASTER);
969 elem = g_list_find (device->slaves, slave);
974 device->slaves = g_list_delete_link (device->slaves, elem);
978 * gdk_device_get_device_type:
979 * @device: a #GdkDevice
981 * Returns the device type for @device.
983 * Returns: the #GdkDeviceType for @device.
988 gdk_device_get_device_type (GdkDevice *device)
990 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_DEVICE_TYPE_MASTER);
996 * gdk_device_get_n_axes:
997 * @device: a pointer #GdkDevice
999 * Returns the number of axes the device currently has.
1001 * Returns: the number of axes.
1006 gdk_device_get_n_axes (GdkDevice *device)
1008 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
1009 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, 0);
1011 return device->axes->len;
1015 * gdk_device_list_axes:
1016 * @device: a pointer #GdkDevice
1018 * Returns a #GList of #GdkAtom<!-- -->s, containing the labels for
1019 * the axes that @device currently has.
1021 * Returns: (transfer container) (element-type GdkAtom):
1022 * A #GList of #GdkAtom<!-- -->s, free with g_list_free().
1027 gdk_device_list_axes (GdkDevice *device)
1032 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
1033 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, NULL);
1035 for (i = 0; i < device->axes->len; i++)
1037 GdkAxisInfo axis_info;
1039 axis_info = g_array_index (device->axes, GdkAxisInfo, i);
1040 axes = g_list_prepend (axes, GDK_ATOM_TO_POINTER (axis_info.label));
1043 return g_list_reverse (axes);
1047 * gdk_device_get_axis_value: (skip)
1048 * @device: a pointer #GdkDevice.
1049 * @axes: (array): pointer to an array of axes
1050 * @axis_label: #GdkAtom with the axis label.
1051 * @value: location to store the found value.
1053 * Interprets an array of double as axis values for a given device,
1054 * and locates the value in the array for a given axis label, as returned
1055 * by gdk_device_list_axes()
1057 * Returns: %TRUE if the given axis use was found, otherwise %FALSE.
1062 gdk_device_get_axis_value (GdkDevice *device,
1069 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
1070 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
1075 for (i = 0; i < device->axes->len; i++)
1077 GdkAxisInfo axis_info;
1079 axis_info = g_array_index (device->axes, GdkAxisInfo, i);
1081 if (axis_info.label != axis_label)
1094 * gdk_device_get_axis: (skip)
1095 * @device: a #GdkDevice
1096 * @axes: (array): pointer to an array of axes
1097 * @use: the use to look for
1098 * @value: (out): location to store the found value.
1100 * Interprets an array of double as axis values for a given device,
1101 * and locates the value in the array for a given axis use.
1103 * Return value: %TRUE if the given axis use was found, otherwise %FALSE
1106 gdk_device_get_axis (GdkDevice *device,
1113 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
1114 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
1119 g_return_val_if_fail (device->axes != NULL, FALSE);
1121 for (i = 0; i < device->axes->len; i++)
1123 GdkAxisInfo axis_info;
1125 axis_info = g_array_index (device->axes, GdkAxisInfo, i);
1127 if (axis_info.use != use)
1140 get_native_grab_event_mask (GdkEventMask grab_mask)
1142 /* Similar to the above but for pointer events only */
1144 GDK_POINTER_MOTION_MASK |
1145 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1146 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1149 ~(GDK_POINTER_MOTION_HINT_MASK |
1150 GDK_BUTTON_MOTION_MASK |
1151 GDK_BUTTON1_MOTION_MASK |
1152 GDK_BUTTON2_MOTION_MASK |
1153 GDK_BUTTON3_MOTION_MASK));
1158 * @device: a #GdkDevice. To get the device you can use gtk_get_current_event_device()
1159 * or gdk_event_get_device() if the grab is in reaction to an event. Also, you can use
1160 * gdk_device_manager_get_client_pointer() but only in code that isn't triggered by a
1161 * #GdkEvent and there aren't other means to get a meaningful #GdkDevice to operate on.
1162 * @window: the #GdkWindow which will own the grab (the grab window)
1163 * @grab_ownership: specifies the grab ownership.
1164 * @owner_events: if %FALSE then all device events are reported with respect to
1165 * @window and are only reported if selected by @event_mask. If
1166 * %TRUE then pointer events for this application are reported
1167 * as normal, but pointer events outside this application are
1168 * reported with respect to @window and only if selected by
1169 * @event_mask. In either mode, unreported events are discarded.
1170 * @event_mask: specifies the event mask, which is used in accordance with
1172 * @cursor: (allow-none): the cursor to display while the grab is active if the device is
1173 * a pointer. If this is %NULL then the normal cursors are used for
1174 * @window and its descendants, and the cursor for @window is used
1176 * @time_: the timestamp of the event which led to this pointer grab. This
1177 * usually comes from the #GdkEvent struct, though %GDK_CURRENT_TIME
1178 * can be used if the time isn't known.
1180 * Grabs the device so that all events coming from this device are passed to
1181 * this application until the device is ungrabbed with gdk_device_ungrab(),
1182 * or the window becomes unviewable. This overrides any previous grab on the device
1185 * Device grabs are used for operations which need complete control over the
1186 * given device events (either pointer or keyboard). For example in GTK+ this
1187 * is used for Drag and Drop operations, popup menus and such.
1189 * Note that if the event mask of an X window has selected both button press
1190 * and button release events, then a button press event will cause an automatic
1191 * pointer grab until the button is released. X does this automatically since
1192 * most applications expect to receive button press and release events in pairs.
1193 * It is equivalent to a pointer grab on the window with @owner_events set to
1196 * If you set up anything at the time you take the grab that needs to be
1197 * cleaned up when the grab ends, you should handle the #GdkEventGrabBroken
1198 * events that are emitted when the grab ends unvoluntarily.
1200 * Returns: %GDK_GRAB_SUCCESS if the grab was successful.
1205 gdk_device_grab (GdkDevice *device,
1207 GdkGrabOwnership grab_ownership,
1208 gboolean owner_events,
1209 GdkEventMask event_mask,
1216 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_GRAB_SUCCESS);
1217 g_return_val_if_fail (GDK_IS_WINDOW (window), GDK_GRAB_SUCCESS);
1219 native = gdk_window_get_toplevel (window);
1221 while (native->window_type == GDK_WINDOW_OFFSCREEN)
1223 native = gdk_offscreen_window_get_embedder (native);
1225 if (native == NULL ||
1226 (!_gdk_window_has_impl (native) &&
1227 !gdk_window_is_viewable (native)))
1228 return GDK_GRAB_NOT_VIEWABLE;
1230 native = gdk_window_get_toplevel (native);
1233 if (native == NULL || GDK_WINDOW_DESTROYED (native))
1234 return GDK_GRAB_NOT_VIEWABLE;
1236 res = GDK_DEVICE_GET_CLASS (device)->grab (device,
1239 get_native_grab_event_mask (event_mask),
1244 if (res == GDK_GRAB_SUCCESS)
1246 GdkDisplay *display;
1249 display = gdk_window_get_display (window);
1250 serial = _gdk_display_get_next_serial (display);
1252 _gdk_display_add_device_grab (display,
1268 * gdk_device_ungrab:
1269 * @device: a #GdkDevice
1270 * @time_: a timestap (e.g. %GDK_CURRENT_TIME).
1272 * Release any grab on @device.
1277 gdk_device_ungrab (GdkDevice *device,
1280 g_return_if_fail (GDK_IS_DEVICE (device));
1282 GDK_DEVICE_GET_CLASS (device)->ungrab (device, time_);
1287 * @device: the device to warp.
1288 * @screen: the screen to warp @device to.
1289 * @x: the X coordinate of the destination.
1290 * @y: the Y coordinate of the destination.
1292 * Warps @device in @display to the point @x,@y on
1293 * the screen @screen, unless the device is confined
1294 * to a window by a grab, in which case it will be moved
1295 * as far as allowed by the grab. Warping the pointer
1296 * creates events as if the user had moved the mouse
1297 * instantaneously to the destination.
1299 * Note that the pointer should normally be under the
1300 * control of the user. This function was added to cover
1301 * some rare use cases like keyboard navigation support
1302 * for the color picker in the #GtkColorSelectionDialog.
1307 gdk_device_warp (GdkDevice *device,
1312 g_return_if_fail (GDK_IS_DEVICE (device));
1313 g_return_if_fail (GDK_IS_SCREEN (screen));
1314 g_return_if_fail (gdk_device_get_display (device) == gdk_screen_get_display (screen));
1316 GDK_DEVICE_GET_CLASS (device)->warp (device, screen, x, y);
1321 _gdk_device_reset_axes (GdkDevice *device)
1325 for (i = device->axes->len - 1; i >= 0; i--)
1326 g_array_remove_index (device->axes, i);
1328 g_object_notify (G_OBJECT (device), "n-axes");
1332 _gdk_device_add_axis (GdkDevice *device,
1339 GdkAxisInfo axis_info;
1342 axis_info.use = use;
1343 axis_info.label = label_atom;
1344 axis_info.min_value = min_value;
1345 axis_info.max_value = max_value;
1346 axis_info.resolution = resolution;
1352 axis_info.min_axis = 0;
1353 axis_info.max_axis = 0;
1355 case GDK_AXIS_XTILT:
1356 case GDK_AXIS_YTILT:
1357 axis_info.min_axis = -1;
1358 axis_info.max_axis = 1;
1361 axis_info.min_axis = 0;
1362 axis_info.max_axis = 1;
1366 device->axes = g_array_append_val (device->axes, axis_info);
1367 pos = device->axes->len - 1;
1369 g_object_notify (G_OBJECT (device), "n-axes");
1375 _gdk_device_set_keys (GdkDevice *device,
1379 g_free (device->keys);
1381 device->num_keys = num_keys;
1382 device->keys = g_new0 (GdkDeviceKey, num_keys);
1385 static GdkAxisInfo *
1386 find_axis_info (GArray *array,
1392 for (i = 0; i < GDK_AXIS_LAST; i++)
1394 info = &g_array_index (array, GdkAxisInfo, i);
1396 if (info->use == use)
1404 _gdk_device_get_axis_use (GdkDevice *device,
1409 info = g_array_index (device->axes, GdkAxisInfo, index_);
1414 _gdk_device_translate_window_coord (GdkDevice *device,
1418 gdouble *axis_value)
1420 GdkAxisInfo axis_info;
1421 GdkAxisInfo *axis_info_x, *axis_info_y;
1422 gdouble device_width, device_height;
1423 gdouble x_offset, y_offset;
1424 gdouble x_scale, y_scale;
1425 gdouble x_min, y_min;
1426 gdouble x_resolution, y_resolution;
1427 gdouble device_aspect;
1428 gint window_width, window_height;
1430 if (index_ >= device->axes->len)
1433 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1435 if (axis_info.use != GDK_AXIS_X &&
1436 axis_info.use != GDK_AXIS_Y)
1439 if (axis_info.use == GDK_AXIS_X)
1441 axis_info_x = &axis_info;
1442 axis_info_y = find_axis_info (device->axes, GDK_AXIS_Y);
1446 axis_info_x = find_axis_info (device->axes, GDK_AXIS_X);
1447 axis_info_y = &axis_info;
1450 device_width = axis_info_x->max_value - axis_info_x->min_value;
1451 device_height = axis_info_y->max_value - axis_info_y->min_value;
1453 if (device_width > 0)
1454 x_min = axis_info_x->min_value;
1457 device_width = gdk_screen_get_width (gdk_window_get_screen (window));
1461 if (device_height > 0)
1462 y_min = axis_info_y->min_value;
1465 device_height = gdk_screen_get_height (gdk_window_get_screen (window));
1469 window_width = gdk_window_get_width (window);
1470 window_height = gdk_window_get_height (window);
1472 x_resolution = axis_info_x->resolution;
1473 y_resolution = axis_info_y->resolution;
1476 * Some drivers incorrectly report the resolution of the device
1477 * as zero (in partiular linuxwacom < 0.5.3 with usb tablets).
1478 * This causes the device_aspect to become NaN and totally
1479 * breaks windowed mode. If this is the case, the best we can
1480 * do is to assume the resolution is non-zero is equal in both
1481 * directions (which is true for many devices). The absolute
1482 * value of the resolution doesn't matter since we only use the
1485 if (x_resolution == 0 || y_resolution == 0)
1491 device_aspect = (device_height * y_resolution) /
1492 (device_width * x_resolution);
1494 if (device_aspect * window_width >= window_height)
1496 /* device taller than window */
1497 x_scale = window_width / device_width;
1498 y_scale = (x_scale * x_resolution) / y_resolution;
1501 y_offset = - (device_height * y_scale - window_height) / 2;
1505 /* window taller than device */
1506 y_scale = window_height / device_height;
1507 x_scale = (y_scale * y_resolution) / x_resolution;
1510 x_offset = - (device_width * x_scale - window_width) / 2;
1515 if (axis_info.use == GDK_AXIS_X)
1516 *axis_value = x_offset + x_scale * (value - x_min);
1518 *axis_value = y_offset + y_scale * (value - y_min);
1525 _gdk_device_translate_screen_coord (GdkDevice *device,
1531 gdouble *axis_value)
1533 GdkAxisInfo axis_info;
1534 gdouble axis_width, scale, offset;
1536 if (device->mode != GDK_MODE_SCREEN)
1539 if (index_ >= device->axes->len)
1542 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1544 if (axis_info.use != GDK_AXIS_X &&
1545 axis_info.use != GDK_AXIS_Y)
1548 axis_width = axis_info.max_value - axis_info.min_value;
1550 if (axis_info.use == GDK_AXIS_X)
1553 scale = gdk_screen_get_width (gdk_window_get_screen (window)) / axis_width;
1557 offset = - window_root_x - window->abs_x;
1562 scale = gdk_screen_get_height (gdk_window_get_screen (window)) / axis_width;
1566 offset = - window_root_y - window->abs_y;
1570 *axis_value = offset + scale * (value - axis_info.min_value);
1576 _gdk_device_translate_axis (GdkDevice *device,
1579 gdouble *axis_value)
1581 GdkAxisInfo axis_info;
1582 gdouble axis_width, out;
1584 if (index_ >= device->axes->len)
1587 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1589 if (axis_info.use == GDK_AXIS_X ||
1590 axis_info.use == GDK_AXIS_Y)
1593 axis_width = axis_info.max_value - axis_info.min_value;
1594 out = (axis_info.max_axis * (value - axis_info.min_value) +
1595 axis_info.min_axis * (axis_info.max_value - value)) / axis_width;
1604 _gdk_device_query_state (GdkDevice *device,
1606 GdkWindow **root_window,
1607 GdkWindow **child_window,
1612 GdkModifierType *mask)
1614 return GDK_DEVICE_GET_CLASS (device)->query_state (device,
1626 _gdk_device_window_at_position (GdkDevice *device,
1629 GdkModifierType *mask,
1630 gboolean get_toplevel)
1632 return GDK_DEVICE_GET_CLASS (device)->window_at_position (device,