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.
37 * See the #GdkDeviceManager documentation for more information
38 * about the various kinds of master and slave devices, and their
42 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 _gdk_device_set_associated_device (device->associated, NULL);
277 g_object_unref (device->associated);
278 device->associated = NULL;
283 g_array_free (device->axes, TRUE);
287 g_free (device->name);
288 g_free (device->keys);
293 G_OBJECT_CLASS (gdk_device_parent_class)->dispose (object);
297 gdk_device_set_property (GObject *object,
302 GdkDevice *device = GDK_DEVICE (object);
307 device->display = g_value_get_object (value);
309 case PROP_DEVICE_MANAGER:
310 device->manager = g_value_get_object (value);
314 g_free (device->name);
316 device->name = g_value_dup_string (value);
319 device->type = g_value_get_enum (value);
321 case PROP_INPUT_SOURCE:
322 device->source = g_value_get_enum (value);
324 case PROP_INPUT_MODE:
325 gdk_device_set_mode (device, g_value_get_enum (value));
327 case PROP_HAS_CURSOR:
328 device->has_cursor = g_value_get_boolean (value);
331 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
337 gdk_device_get_property (GObject *object,
342 GdkDevice *device = GDK_DEVICE (object);
347 g_value_set_object (value, device->display);
349 case PROP_DEVICE_MANAGER:
350 g_value_set_object (value, device->manager);
352 case PROP_ASSOCIATED_DEVICE:
353 g_value_set_object (value, device->associated);
356 g_value_set_string (value, device->name);
359 g_value_set_enum (value, device->type);
361 case PROP_INPUT_SOURCE:
362 g_value_set_enum (value, device->source);
364 case PROP_INPUT_MODE:
365 g_value_set_enum (value, device->mode);
367 case PROP_HAS_CURSOR:
368 g_value_set_boolean (value, device->has_cursor);
371 g_value_set_uint (value, device->axes->len);
374 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
380 * gdk_device_get_state: (skip)
381 * @device: a #GdkDevice.
382 * @window: a #GdkWindow.
383 * @axes: an array of doubles to store the values of the axes of @device in,
385 * @mask: location to store the modifiers, or %NULL.
387 * Gets the current state of a pointer device relative to @window.
390 gdk_device_get_state (GdkDevice *device,
393 GdkModifierType *mask)
395 g_return_if_fail (GDK_IS_DEVICE (device));
396 g_return_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD);
397 g_return_if_fail (GDK_IS_WINDOW (window));
399 if (GDK_DEVICE_GET_CLASS (device)->get_state)
400 GDK_DEVICE_GET_CLASS (device)->get_state (device, window, axes, mask);
404 * gdk_device_get_position:
405 * @device: pointer device to query status about.
406 * @screen: (out) (transfer none) (allow-none): location to store the #GdkScreen
407 * the @device is on, or %NULL.
408 * @x: (out) (allow-none): location to store root window X coordinate of @device, or %NULL.
409 * @y: (out) (allow-none): location to store root window Y coordinate of @device, or %NULL.
411 * Gets the current location of @device.
416 gdk_device_get_position (GdkDevice *device,
423 GdkScreen *default_screen;
426 g_return_if_fail (GDK_IS_DEVICE (device));
427 g_return_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD);
429 display = gdk_device_get_display (device);
430 default_screen = gdk_display_get_default_screen (display);
432 _gdk_device_query_state (device,
433 gdk_screen_get_root_window (default_screen),
439 *screen = gdk_window_get_screen (root);
447 * gdk_device_get_window_at_position:
448 * @device: pointer #GdkDevice to query info to.
449 * @win_x: (out) (allow-none): return location for the X coordinate of the device location,
450 * relative to the window origin, or %NULL.
451 * @win_y: (out) (allow-none): return location for the Y coordinate of the device location,
452 * relative to the window origin, or %NULL.
454 * Obtains the window underneath @device, returning the location of the device in @win_x and @win_y. Returns
455 * %NULL if the window tree under @device is not known to GDK (for example, belongs to another application).
457 * Returns: (transfer none): the #GdkWindow under the device position, or %NULL.
462 gdk_device_get_window_at_position (GdkDevice *device,
469 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
470 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, NULL);
472 window = _gdk_device_window_at_position (device, &tmp_x, &tmp_y, NULL, FALSE);
474 /* This might need corrections, as the native window returned
475 may contain client side children */
480 window = _gdk_window_find_descendant_at (window,
483 tmp_x = floor (xx + 0.5);
484 tmp_y = floor (yy + 0.5);
496 * gdk_device_get_history: (skip)
497 * @device: a #GdkDevice
498 * @window: the window with respect to which which the event coordinates will be reported
499 * @start: starting timestamp for range of events to return
500 * @stop: ending timestamp for the range of events to return
501 * @events: (array length=n_events) (out) (transfer full): location to store a newly-allocated array of #GdkTimeCoord, or %NULL
502 * @n_events: location to store the length of @events, or %NULL
504 * Obtains the motion history for a pointer device; given a starting and
505 * ending timestamp, return all events in the motion history for
506 * the device in the given range of time. Some windowing systems
507 * do not support motion history, in which case, %FALSE will
508 * be returned. (This is not distinguishable from the case where
509 * motion history is supported and no events were found.)
511 * Return value: %TRUE if the windowing system supports motion history and
512 * at least one event was found.
515 gdk_device_get_history (GdkDevice *device,
519 GdkTimeCoord ***events,
522 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
523 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
524 g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
532 if (GDK_WINDOW_DESTROYED (window))
535 if (!GDK_DEVICE_GET_CLASS (device)->get_history)
538 return GDK_DEVICE_GET_CLASS (device)->get_history (device, window,
544 _gdk_device_allocate_history (GdkDevice *device,
547 GdkTimeCoord **result = g_new (GdkTimeCoord *, n_events);
550 for (i = 0; i < n_events; i++)
551 result[i] = g_malloc (sizeof (GdkTimeCoord) -
552 sizeof (double) * (GDK_MAX_TIMECOORD_AXES - device->axes->len));
557 * gdk_device_free_history: (skip)
558 * @events: an array of #GdkTimeCoord.
559 * @n_events: the length of the array.
561 * Frees an array of #GdkTimeCoord that was returned by gdk_device_get_history().
564 gdk_device_free_history (GdkTimeCoord **events,
569 for (i = 0; i < n_events; i++)
576 * gdk_device_get_name:
577 * @device: a #GdkDevice
579 * Determines the name of the device.
581 * Return value: a name
586 gdk_device_get_name (GdkDevice *device)
588 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
594 * gdk_device_get_has_cursor:
595 * @device: a #GdkDevice
597 * Determines whether the pointer follows device motion.
599 * Return value: %TRUE if the pointer follows device motion
604 gdk_device_get_has_cursor (GdkDevice *device)
606 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
607 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
609 return device->has_cursor;
613 * gdk_device_get_source:
614 * @device: a #GdkDevice
616 * Determines the type of the device.
618 * Return value: a #GdkInputSource
623 gdk_device_get_source (GdkDevice *device)
625 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
627 return device->source;
631 * gdk_device_get_mode:
632 * @device: a #GdkDevice
634 * Determines the mode of the device.
636 * Return value: a #GdkInputSource
641 gdk_device_get_mode (GdkDevice *device)
643 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
649 * gdk_device_set_mode:
650 * @device: a #GdkDevice.
651 * @mode: the input mode.
653 * Sets a the mode of an input device. The mode controls if the
654 * device is active and whether the device's range is mapped to the
655 * entire screen or to a single window.
657 * Returns: %TRUE if the mode was successfully changed.
660 gdk_device_set_mode (GdkDevice *device,
663 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
665 if (device->mode == mode)
668 if (mode == GDK_MODE_DISABLED &&
669 gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER)
673 g_object_notify (G_OBJECT (device), "input-mode");
679 * gdk_device_get_n_keys:
680 * @device: a #GdkDevice
682 * Returns the number of keys the device currently has.
684 * Returns: the number of keys.
689 gdk_device_get_n_keys (GdkDevice *device)
691 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
693 return device->num_keys;
697 * gdk_device_get_key:
698 * @device: a #GdkDevice.
699 * @index_: the index of the macro button to get.
700 * @keyval: (out): return value for the keyval.
701 * @modifiers: (out): return value for modifiers.
703 * If @index_ has a valid keyval, this function will return %TRUE
704 * and fill in @keyval and @modifiers with the keyval settings.
706 * Returns: %TRUE if keyval is set for @index.
711 gdk_device_get_key (GdkDevice *device,
714 GdkModifierType *modifiers)
716 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
717 g_return_val_if_fail (index_ < device->num_keys, FALSE);
719 if (!device->keys[index_].keyval &&
720 !device->keys[index_].modifiers)
724 *keyval = device->keys[index_].keyval;
727 *modifiers = device->keys[index_].modifiers;
733 * gdk_device_set_key:
734 * @device: a #GdkDevice
735 * @index_: the index of the macro button to set
736 * @keyval: the keyval to generate
737 * @modifiers: the modifiers to set
739 * Specifies the X key event to generate when a macro button of a device
743 gdk_device_set_key (GdkDevice *device,
746 GdkModifierType modifiers)
748 g_return_if_fail (GDK_IS_DEVICE (device));
749 g_return_if_fail (index_ < device->num_keys);
751 device->keys[index_].keyval = keyval;
752 device->keys[index_].modifiers = modifiers;
756 * gdk_device_get_axis_use:
757 * @device: a pointer #GdkDevice.
758 * @index_: the index of the axis.
760 * Returns the axis use for @index_.
762 * Returns: a #GdkAxisUse specifying how the axis is used.
767 gdk_device_get_axis_use (GdkDevice *device,
772 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_AXIS_IGNORE);
773 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, GDK_AXIS_IGNORE);
774 g_return_val_if_fail (index_ < device->axes->len, GDK_AXIS_IGNORE);
776 info = &g_array_index (device->axes, GdkAxisInfo, index_);
782 * gdk_device_set_axis_use:
783 * @device: a pointer #GdkDevice
784 * @index_: the index of the axis
785 * @use: specifies how the axis is used
787 * Specifies how an axis of a device is used.
790 gdk_device_set_axis_use (GdkDevice *device,
796 g_return_if_fail (GDK_IS_DEVICE (device));
797 g_return_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD);
798 g_return_if_fail (index_ < device->axes->len);
800 info = &g_array_index (device->axes, GdkAxisInfo, index_);
823 * gdk_device_get_display:
824 * @device: a #GdkDevice
826 * Returns the #GdkDisplay to which @device pertains.
828 * Returns: (transfer none): a #GdkDisplay. This memory is owned
829 * by GTK+, and must not be freed or unreffed.
834 gdk_device_get_display (GdkDevice *device)
836 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
838 return device->display;
842 * gdk_device_get_associated_device:
843 * @device: a #GdkDevice
845 * Returns the associated device to @device, if @device is of type
846 * %GDK_DEVICE_TYPE_MASTER, it will return the paired pointer or
849 * If @device is of type %GDK_DEVICE_TYPE_SLAVE, it will return
850 * the master device to which @device is attached to.
852 * If @device is of type %GDK_DEVICE_TYPE_FLOATING, %NULL will be
853 * returned, as there is no associated device.
855 * Returns: (transfer none): The associated device, or %NULL
860 gdk_device_get_associated_device (GdkDevice *device)
862 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
864 return device->associated;
868 _gdk_device_set_device_type (GdkDevice *device,
871 if (device->type != type)
875 g_object_notify (G_OBJECT (device), "type");
880 _gdk_device_set_associated_device (GdkDevice *device,
881 GdkDevice *associated)
883 g_return_if_fail (GDK_IS_DEVICE (device));
884 g_return_if_fail (associated == NULL || GDK_IS_DEVICE (associated));
886 if (device->associated == associated)
889 if (device->associated)
891 g_object_unref (device->associated);
892 device->associated = NULL;
896 device->associated = g_object_ref (associated);
898 if (device->type != GDK_DEVICE_TYPE_MASTER)
900 if (device->associated)
901 _gdk_device_set_device_type (device, GDK_DEVICE_TYPE_SLAVE);
903 _gdk_device_set_device_type (device, GDK_DEVICE_TYPE_FLOATING);
908 * gdk_device_list_slave_devices:
909 * @device: a #GdkDevice
911 * If the device if of type %GDK_DEVICE_TYPE_MASTER, it will return
912 * the list of slave devices attached to it, otherwise it will return
915 * Returns: (transfer container) (element-type GdkDevice): the list of
916 * slave devices, or %NULL. The list must be freed with
917 * g_list_free(), the contents of the list are owned by GTK+
918 * and should not be freed.
921 gdk_device_list_slave_devices (GdkDevice *device)
923 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
924 g_return_val_if_fail (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER, NULL);
926 return g_list_copy (device->slaves);
930 _gdk_device_add_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 if (!g_list_find (device->slaves, slave))
937 device->slaves = g_list_prepend (device->slaves, slave);
941 _gdk_device_remove_slave (GdkDevice *device,
946 g_return_if_fail (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER);
947 g_return_if_fail (gdk_device_get_device_type (slave) != GDK_DEVICE_TYPE_MASTER);
949 elem = g_list_find (device->slaves, slave);
954 device->slaves = g_list_delete_link (device->slaves, elem);
958 * gdk_device_get_device_type:
959 * @device: a #GdkDevice
961 * Returns the device type for @device.
963 * Returns: the #GdkDeviceType for @device.
968 gdk_device_get_device_type (GdkDevice *device)
970 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_DEVICE_TYPE_MASTER);
976 * gdk_device_get_n_axes:
977 * @device: a pointer #GdkDevice
979 * Returns the number of axes the device currently has.
981 * Returns: the number of axes.
986 gdk_device_get_n_axes (GdkDevice *device)
988 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
989 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, 0);
991 return device->axes->len;
995 * gdk_device_list_axes:
996 * @device: a pointer #GdkDevice
998 * Returns a #GList of #GdkAtom<!-- -->s, containing the labels for
999 * the axes that @device currently has.
1001 * Returns: (transfer container) (element-type GdkAtom):
1002 * A #GList of #GdkAtom<!-- -->s, free with g_list_free().
1007 gdk_device_list_axes (GdkDevice *device)
1012 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
1013 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, NULL);
1015 for (i = 0; i < device->axes->len; i++)
1017 GdkAxisInfo axis_info;
1019 axis_info = g_array_index (device->axes, GdkAxisInfo, i);
1020 axes = g_list_prepend (axes, GDK_ATOM_TO_POINTER (axis_info.label));
1023 return g_list_reverse (axes);
1027 * gdk_device_get_axis_value: (skip)
1028 * @device: a pointer #GdkDevice.
1029 * @axes: (array): pointer to an array of axes
1030 * @axis_label: #GdkAtom with the axis label.
1031 * @value: location to store the found value.
1033 * Interprets an array of double as axis values for a given device,
1034 * and locates the value in the array for a given axis label, as returned
1035 * by gdk_device_list_axes()
1037 * Returns: %TRUE if the given axis use was found, otherwise %FALSE.
1042 gdk_device_get_axis_value (GdkDevice *device,
1049 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
1050 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
1055 for (i = 0; i < device->axes->len; i++)
1057 GdkAxisInfo axis_info;
1059 axis_info = g_array_index (device->axes, GdkAxisInfo, i);
1061 if (axis_info.label != axis_label)
1074 * gdk_device_get_axis: (skip)
1075 * @device: a #GdkDevice
1076 * @axes: (array): pointer to an array of axes
1077 * @use: the use to look for
1078 * @value: (out): location to store the found value.
1080 * Interprets an array of double as axis values for a given device,
1081 * and locates the value in the array for a given axis use.
1083 * Return value: %TRUE if the given axis use was found, otherwise %FALSE
1086 gdk_device_get_axis (GdkDevice *device,
1093 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
1094 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
1099 g_return_val_if_fail (device->axes != NULL, FALSE);
1101 for (i = 0; i < device->axes->len; i++)
1103 GdkAxisInfo axis_info;
1105 axis_info = g_array_index (device->axes, GdkAxisInfo, i);
1107 if (axis_info.use != use)
1120 get_native_grab_event_mask (GdkEventMask grab_mask)
1122 /* Similar to the above but for pointer events only */
1124 GDK_POINTER_MOTION_MASK |
1125 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1126 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1129 ~(GDK_POINTER_MOTION_HINT_MASK |
1130 GDK_BUTTON_MOTION_MASK |
1131 GDK_BUTTON1_MOTION_MASK |
1132 GDK_BUTTON2_MOTION_MASK |
1133 GDK_BUTTON3_MOTION_MASK));
1138 * @device: a #GdkDevice. To get the device you can use gtk_get_current_event_device()
1139 * or gdk_event_get_device() if the grab is in reaction to an event. Also, you can use
1140 * gdk_device_manager_get_client_pointer() but only in code that isn't triggered by a
1141 * #GdkEvent and there aren't other means to get a meaningful #GdkDevice to operate on.
1142 * @window: the #GdkWindow which will own the grab (the grab window)
1143 * @grab_ownership: specifies the grab ownership.
1144 * @owner_events: if %FALSE then all device events are reported with respect to
1145 * @window and are only reported if selected by @event_mask. If
1146 * %TRUE then pointer events for this application are reported
1147 * as normal, but pointer events outside this application are
1148 * reported with respect to @window and only if selected by
1149 * @event_mask. In either mode, unreported events are discarded.
1150 * @event_mask: specifies the event mask, which is used in accordance with
1152 * @cursor: (allow-none): the cursor to display while the grab is active if the device is
1153 * a pointer. If this is %NULL then the normal cursors are used for
1154 * @window and its descendants, and the cursor for @window is used
1156 * @time_: the timestamp of the event which led to this pointer grab. This
1157 * usually comes from the #GdkEvent struct, though %GDK_CURRENT_TIME
1158 * can be used if the time isn't known.
1160 * Grabs the device so that all events coming from this device are passed to
1161 * this application until the device is ungrabbed with gdk_device_ungrab(),
1162 * or the window becomes unviewable. This overrides any previous grab on the device
1165 * Device grabs are used for operations which need complete control over the
1166 * given device events (either pointer or keyboard). For example in GTK+ this
1167 * is used for Drag and Drop operations, popup menus and such.
1169 * Note that if the event mask of an X window has selected both button press
1170 * and button release events, then a button press event will cause an automatic
1171 * pointer grab until the button is released. X does this automatically since
1172 * most applications expect to receive button press and release events in pairs.
1173 * It is equivalent to a pointer grab on the window with @owner_events set to
1176 * If you set up anything at the time you take the grab that needs to be
1177 * cleaned up when the grab ends, you should handle the #GdkEventGrabBroken
1178 * events that are emitted when the grab ends unvoluntarily.
1180 * Returns: %GDK_GRAB_SUCCESS if the grab was successful.
1185 gdk_device_grab (GdkDevice *device,
1187 GdkGrabOwnership grab_ownership,
1188 gboolean owner_events,
1189 GdkEventMask event_mask,
1196 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_GRAB_SUCCESS);
1197 g_return_val_if_fail (GDK_IS_WINDOW (window), GDK_GRAB_SUCCESS);
1199 if (_gdk_native_windows)
1202 native = gdk_window_get_toplevel (window);
1204 while (native->window_type == GDK_WINDOW_OFFSCREEN)
1206 native = gdk_offscreen_window_get_embedder (native);
1208 if (native == NULL ||
1209 (!_gdk_window_has_impl (native) &&
1210 !gdk_window_is_viewable (native)))
1211 return GDK_GRAB_NOT_VIEWABLE;
1213 native = gdk_window_get_toplevel (native);
1216 if (native == NULL || GDK_WINDOW_DESTROYED (native))
1217 return GDK_GRAB_NOT_VIEWABLE;
1219 res = GDK_DEVICE_GET_CLASS (device)->grab (device,
1222 get_native_grab_event_mask (event_mask),
1227 if (res == GDK_GRAB_SUCCESS)
1229 GdkDisplay *display;
1232 display = gdk_window_get_display (window);
1233 serial = _gdk_display_get_next_serial (display);
1235 _gdk_display_add_device_grab (display,
1251 * gdk_device_ungrab:
1252 * @device: a #GdkDevice
1253 * @time_: a timestap (e.g. %GDK_CURRENT_TIME).
1255 * Release any grab on @device.
1260 gdk_device_ungrab (GdkDevice *device,
1263 g_return_if_fail (GDK_IS_DEVICE (device));
1265 GDK_DEVICE_GET_CLASS (device)->ungrab (device, time_);
1270 * @device: the device to warp.
1271 * @screen: the screen to warp @device to.
1272 * @x: the X coordinate of the destination.
1273 * @y: the Y coordinate of the destination.
1275 * Warps @device in @display to the point @x,@y on
1276 * the screen @screen, unless the device is confined
1277 * to a window by a grab, in which case it will be moved
1278 * as far as allowed by the grab. Warping the pointer
1279 * creates events as if the user had moved the mouse
1280 * instantaneously to the destination.
1282 * Note that the pointer should normally be under the
1283 * control of the user. This function was added to cover
1284 * some rare use cases like keyboard navigation support
1285 * for the color picker in the #GtkColorSelectionDialog.
1290 gdk_device_warp (GdkDevice *device,
1295 g_return_if_fail (GDK_IS_DEVICE (device));
1296 g_return_if_fail (GDK_IS_SCREEN (screen));
1297 g_return_if_fail (gdk_device_get_display (device) == gdk_screen_get_display (screen));
1299 GDK_DEVICE_GET_CLASS (device)->warp (device, screen, x, y);
1304 _gdk_device_reset_axes (GdkDevice *device)
1308 for (i = device->axes->len - 1; i >= 0; i--)
1309 g_array_remove_index (device->axes, i);
1311 g_object_notify (G_OBJECT (device), "n-axes");
1315 _gdk_device_add_axis (GdkDevice *device,
1322 GdkAxisInfo axis_info;
1325 axis_info.use = use;
1326 axis_info.label = label_atom;
1327 axis_info.min_value = min_value;
1328 axis_info.max_value = max_value;
1329 axis_info.resolution = resolution;
1335 axis_info.min_axis = 0;
1336 axis_info.max_axis = 0;
1338 case GDK_AXIS_XTILT:
1339 case GDK_AXIS_YTILT:
1340 axis_info.min_axis = -1;
1341 axis_info.max_axis = 1;
1344 axis_info.min_axis = 0;
1345 axis_info.max_axis = 1;
1349 device->axes = g_array_append_val (device->axes, axis_info);
1350 pos = device->axes->len - 1;
1352 g_object_notify (G_OBJECT (device), "n-axes");
1358 _gdk_device_set_keys (GdkDevice *device,
1362 g_free (device->keys);
1364 device->num_keys = num_keys;
1365 device->keys = g_new0 (GdkDeviceKey, num_keys);
1368 static GdkAxisInfo *
1369 find_axis_info (GArray *array,
1375 for (i = 0; i < GDK_AXIS_LAST; i++)
1377 info = &g_array_index (array, GdkAxisInfo, i);
1379 if (info->use == use)
1387 _gdk_device_get_axis_use (GdkDevice *device,
1392 info = g_array_index (device->axes, GdkAxisInfo, index_);
1397 _gdk_device_translate_window_coord (GdkDevice *device,
1401 gdouble *axis_value)
1403 GdkAxisInfo axis_info;
1404 GdkAxisInfo *axis_info_x, *axis_info_y;
1405 gdouble device_width, device_height;
1406 gdouble x_offset, y_offset;
1407 gdouble x_scale, y_scale;
1408 gdouble x_min, y_min;
1409 gdouble x_resolution, y_resolution;
1410 gdouble device_aspect;
1411 gint window_width, window_height;
1413 if (index_ >= device->axes->len)
1416 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1418 if (axis_info.use != GDK_AXIS_X &&
1419 axis_info.use != GDK_AXIS_Y)
1422 if (axis_info.use == GDK_AXIS_X)
1424 axis_info_x = &axis_info;
1425 axis_info_y = find_axis_info (device->axes, GDK_AXIS_Y);
1429 axis_info_x = find_axis_info (device->axes, GDK_AXIS_X);
1430 axis_info_y = &axis_info;
1433 device_width = axis_info_x->max_value - axis_info_x->min_value;
1434 device_height = axis_info_y->max_value - axis_info_y->min_value;
1436 if (device_width > 0)
1437 x_min = axis_info_x->min_value;
1440 device_width = gdk_screen_get_width (gdk_window_get_screen (window));
1444 if (device_height > 0)
1445 y_min = axis_info_y->min_value;
1448 device_height = gdk_screen_get_height (gdk_window_get_screen (window));
1452 window_width = gdk_window_get_width (window);
1453 window_height = gdk_window_get_height (window);
1455 x_resolution = axis_info_x->resolution;
1456 y_resolution = axis_info_y->resolution;
1459 * Some drivers incorrectly report the resolution of the device
1460 * as zero (in partiular linuxwacom < 0.5.3 with usb tablets).
1461 * This causes the device_aspect to become NaN and totally
1462 * breaks windowed mode. If this is the case, the best we can
1463 * do is to assume the resolution is non-zero is equal in both
1464 * directions (which is true for many devices). The absolute
1465 * value of the resolution doesn't matter since we only use the
1468 if (x_resolution == 0 || y_resolution == 0)
1474 device_aspect = (device_height * y_resolution) /
1475 (device_width * x_resolution);
1477 if (device_aspect * window_width >= window_height)
1479 /* device taller than window */
1480 x_scale = window_width / device_width;
1481 y_scale = (x_scale * x_resolution) / y_resolution;
1484 y_offset = - (device_height * y_scale - window_height) / 2;
1488 /* window taller than device */
1489 y_scale = window_height / device_height;
1490 x_scale = (y_scale * y_resolution) / x_resolution;
1493 x_offset = - (device_width * x_scale - window_width) / 2;
1498 if (axis_info.use == GDK_AXIS_X)
1499 *axis_value = x_offset + x_scale * (value - x_min);
1501 *axis_value = y_offset + y_scale * (value - y_min);
1508 _gdk_device_translate_screen_coord (GdkDevice *device,
1514 gdouble *axis_value)
1516 GdkAxisInfo axis_info;
1517 gdouble axis_width, scale, offset;
1519 if (device->mode != GDK_MODE_SCREEN)
1522 if (index_ >= device->axes->len)
1525 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1527 if (axis_info.use != GDK_AXIS_X &&
1528 axis_info.use != GDK_AXIS_Y)
1531 axis_width = axis_info.max_value - axis_info.min_value;
1533 if (axis_info.use == GDK_AXIS_X)
1536 scale = gdk_screen_get_width (gdk_window_get_screen (window)) / axis_width;
1540 offset = - window_root_x - window->abs_x;
1545 scale = gdk_screen_get_height (gdk_window_get_screen (window)) / axis_width;
1549 offset = - window_root_y - window->abs_y;
1553 *axis_value = offset + scale * (value - axis_info.min_value);
1559 _gdk_device_translate_axis (GdkDevice *device,
1562 gdouble *axis_value)
1564 GdkAxisInfo axis_info;
1565 gdouble axis_width, out;
1567 if (index_ >= device->axes->len)
1570 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1572 if (axis_info.use == GDK_AXIS_X ||
1573 axis_info.use == GDK_AXIS_Y)
1576 axis_width = axis_info.max_value - axis_info.min_value;
1577 out = (axis_info.max_axis * (value - axis_info.min_value) +
1578 axis_info.min_axis * (axis_info.max_value - value)) / axis_width;
1587 _gdk_device_query_state (GdkDevice *device,
1589 GdkWindow **root_window,
1590 GdkWindow **child_window,
1595 GdkModifierType *mask)
1597 return GDK_DEVICE_GET_CLASS (device)->query_state (device,
1609 _gdk_device_window_at_position (GdkDevice *device,
1612 GdkModifierType *mask,
1613 gboolean get_toplevel)
1615 return GDK_DEVICE_GET_CLASS (device)->window_at_position (device,