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_history:
403 * @device: a #GdkDevice
404 * @window: the window with respect to which which the event coordinates will be reported
405 * @start: starting timestamp for range of events to return
406 * @stop: ending timestamp for the range of events to return
407 * @events: (array length=n_events) (out) (transfer none): location to store a newly-allocated array of #GdkTimeCoord, or %NULL
408 * @n_events: location to store the length of @events, or %NULL
410 * Obtains the motion history for a pointer device; given a starting and
411 * ending timestamp, return all events in the motion history for
412 * the device in the given range of time. Some windowing systems
413 * do not support motion history, in which case, %FALSE will
414 * be returned. (This is not distinguishable from the case where
415 * motion history is supported and no events were found.)
417 * Return value: %TRUE if the windowing system supports motion history and
418 * at least one event was found.
421 gdk_device_get_history (GdkDevice *device,
425 GdkTimeCoord ***events,
428 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
429 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
430 g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
438 if (GDK_WINDOW_DESTROYED (window))
441 if (!GDK_DEVICE_GET_CLASS (device)->get_history)
444 return GDK_DEVICE_GET_CLASS (device)->get_history (device, window,
450 _gdk_device_allocate_history (GdkDevice *device,
453 GdkTimeCoord **result = g_new (GdkTimeCoord *, n_events);
456 for (i = 0; i < n_events; i++)
457 result[i] = g_malloc (sizeof (GdkTimeCoord) -
458 sizeof (double) * (GDK_MAX_TIMECOORD_AXES - device->axes->len));
463 * gdk_device_free_history:
464 * @events: (inout) (transfer none): an array of #GdkTimeCoord.
465 * @n_events: the length of the array.
467 * Frees an array of #GdkTimeCoord that was returned by gdk_device_get_history().
470 gdk_device_free_history (GdkTimeCoord **events,
475 for (i = 0; i < n_events; i++)
482 * gdk_device_get_name:
483 * @device: a #GdkDevice
485 * Determines the name of the device.
487 * Return value: a name
492 gdk_device_get_name (GdkDevice *device)
494 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
500 * gdk_device_get_has_cursor:
501 * @device: a #GdkDevice
503 * Determines whether the pointer follows device motion.
505 * Return value: %TRUE if the pointer follows device motion
510 gdk_device_get_has_cursor (GdkDevice *device)
512 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
513 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
515 return device->has_cursor;
519 * gdk_device_get_source:
520 * @device: a #GdkDevice
522 * Determines the type of the device.
524 * Return value: a #GdkInputSource
529 gdk_device_get_source (GdkDevice *device)
531 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
533 return device->source;
537 * gdk_device_set_source:
538 * @device: a #GdkDevice.
539 * @source: the source type.
541 * Sets the source type for an input device.
544 gdk_device_set_source (GdkDevice *device,
545 GdkInputSource source)
547 g_return_if_fail (GDK_IS_DEVICE (device));
549 device->source = source;
550 g_object_notify (G_OBJECT (device), "input-source");
554 * gdk_device_get_mode:
555 * @device: a #GdkDevice
557 * Determines the mode of the device.
559 * Return value: a #GdkInputSource
564 gdk_device_get_mode (GdkDevice *device)
566 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
572 * gdk_device_set_mode:
573 * @device: a #GdkDevice.
574 * @mode: the input mode.
576 * Sets a the mode of an input device. The mode controls if the
577 * device is active and whether the device's range is mapped to the
578 * entire screen or to a single window.
580 * Returns: %TRUE if the mode was successfully changed.
583 gdk_device_set_mode (GdkDevice *device,
586 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
588 if (device->mode == mode)
591 if (mode == GDK_MODE_DISABLED &&
592 gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER)
596 g_object_notify (G_OBJECT (device), "input-mode");
602 * gdk_device_get_n_keys:
603 * @device: a #GdkDevice
605 * Returns the number of keys the device currently has.
607 * Returns: the number of keys.
612 gdk_device_get_n_keys (GdkDevice *device)
614 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
616 return device->num_keys;
620 * gdk_device_get_key:
621 * @device: a #GdkDevice.
622 * @index_: the index of the macro button to get.
623 * @keyval: return value for the keyval.
624 * @modifiers: return value for modifiers.
626 * If @index_ has a valid keyval, this function will return %TRUE
627 * and fill in @keyval and @modifiers with the keyval settings.
629 * Returns: %TRUE if keyval is set for @index.
634 gdk_device_get_key (GdkDevice *device,
637 GdkModifierType *modifiers)
639 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
640 g_return_val_if_fail (index_ < device->num_keys, FALSE);
642 if (!device->keys[index_].keyval &&
643 !device->keys[index_].modifiers)
647 *keyval = device->keys[index_].keyval;
650 *modifiers = device->keys[index_].modifiers;
656 * gdk_device_set_key:
657 * @device: a #GdkDevice
658 * @index_: the index of the macro button to set
659 * @keyval: the keyval to generate
660 * @modifiers: the modifiers to set
662 * Specifies the X key event to generate when a macro button of a device
666 gdk_device_set_key (GdkDevice *device,
669 GdkModifierType modifiers)
671 g_return_if_fail (GDK_IS_DEVICE (device));
672 g_return_if_fail (index_ < device->num_keys);
674 device->keys[index_].keyval = keyval;
675 device->keys[index_].modifiers = modifiers;
679 * gdk_device_get_axis_use:
680 * @device: a pointer #GdkDevice.
681 * @index_: the index of the axis.
683 * Returns the axis use for @index_.
685 * Returns: a #GdkAxisUse specifying how the axis is used.
690 gdk_device_get_axis_use (GdkDevice *device,
695 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_AXIS_IGNORE);
696 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, GDK_AXIS_IGNORE);
697 g_return_val_if_fail (index_ < device->axes->len, GDK_AXIS_IGNORE);
699 info = &g_array_index (device->axes, GdkAxisInfo, index_);
705 * gdk_device_set_axis_use:
706 * @device: a pointer #GdkDevice
707 * @index_: the index of the axis
708 * @use: specifies how the axis is used
710 * Specifies how an axis of a device is used.
713 gdk_device_set_axis_use (GdkDevice *device,
719 g_return_if_fail (GDK_IS_DEVICE (device));
720 g_return_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD);
721 g_return_if_fail (index_ < device->axes->len);
723 info = &g_array_index (device->axes, GdkAxisInfo, index_);
746 * gdk_device_get_display:
747 * @device: a #GdkDevice
749 * Returns the #GdkDisplay to which @device pertains.
751 * Returns: (transfer none): a #GdkDisplay. This memory is owned
752 * by GTK+, and must not be freed or unreffed.
757 gdk_device_get_display (GdkDevice *device)
759 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
761 return device->display;
765 * gdk_device_get_associated_device:
766 * @device: a #GdkDevice
768 * Returns the associated device to @device, if @device is of type
769 * %GDK_DEVICE_TYPE_MASTER, it will return the paired pointer or
772 * If @device is of type %GDK_DEVICE_TYPE_SLAVE, it will return
773 * the master device to which @device is attached to.
775 * If @device is of type %GDK_DEVICE_TYPE_FLOATING, %NULL will be
776 * returned, as there is no associated device.
778 * Returns: (transfer none): The associated device, or %NULL
783 gdk_device_get_associated_device (GdkDevice *device)
785 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
787 return device->associated;
791 _gdk_device_set_device_type (GdkDevice *device,
794 if (device->type != type)
798 g_object_notify (G_OBJECT (device), "type");
803 _gdk_device_set_associated_device (GdkDevice *device,
804 GdkDevice *associated)
806 g_return_if_fail (GDK_IS_DEVICE (device));
807 g_return_if_fail (associated == NULL || GDK_IS_DEVICE (associated));
809 if (device->associated == associated)
812 if (device->associated)
814 g_object_unref (device->associated);
815 device->associated = NULL;
819 device->associated = g_object_ref (associated);
821 if (device->type != GDK_DEVICE_TYPE_MASTER)
823 if (device->associated)
824 _gdk_device_set_device_type (device, GDK_DEVICE_TYPE_SLAVE);
826 _gdk_device_set_device_type (device, GDK_DEVICE_TYPE_FLOATING);
831 * gdk_device_list_slave_devices:
832 * @device: a #GdkDevice
834 * If the device if of type %GDK_DEVICE_TYPE_MASTER, it will return
835 * the list of slave devices attached to it, otherwise it will return
838 * Returns: (transfer container): the list of slave devices, or %NULL. The
839 * list must be freed with g_list_free(), the contents of the list
840 * are owned by GTK+ and should not be freed.
843 gdk_device_list_slave_devices (GdkDevice *device)
845 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
846 g_return_val_if_fail (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER, NULL);
848 return g_list_copy (device->slaves);
852 _gdk_device_add_slave (GdkDevice *device,
855 g_return_if_fail (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER);
856 g_return_if_fail (gdk_device_get_device_type (slave) != GDK_DEVICE_TYPE_MASTER);
858 if (!g_list_find (device->slaves, slave))
859 device->slaves = g_list_prepend (device->slaves, slave);
863 _gdk_device_remove_slave (GdkDevice *device,
868 g_return_if_fail (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER);
869 g_return_if_fail (gdk_device_get_device_type (slave) != GDK_DEVICE_TYPE_MASTER);
871 elem = g_list_find (device->slaves, slave);
876 device->slaves = g_list_delete_link (device->slaves, elem);
880 * gdk_device_get_device_type:
881 * @device: a #GdkDevice
883 * Returns the device type for @device.
885 * Returns: the #GdkDeviceType for @device.
890 gdk_device_get_device_type (GdkDevice *device)
892 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_DEVICE_TYPE_MASTER);
898 * gdk_device_get_n_axes:
899 * @device: a pointer #GdkDevice
901 * Returns the number of axes the device currently has.
903 * Returns: the number of axes.
908 gdk_device_get_n_axes (GdkDevice *device)
910 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
911 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, 0);
913 return device->axes->len;
917 * gdk_device_list_axes:
918 * @device: a pointer #GdkDevice
920 * Returns a #GList of #GdkAtom<!-- -->s, containing the labels for
921 * the axes that @device currently has.
923 * Returns: (transfer container) (element-type GdkAtom):
924 * A #GList of #GdkAtom<!-- -->s, free with g_list_free().
929 gdk_device_list_axes (GdkDevice *device)
934 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
935 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, NULL);
937 for (i = 0; i < device->axes->len; i++)
939 GdkAxisInfo axis_info;
941 axis_info = g_array_index (device->axes, GdkAxisInfo, i);
942 axes = g_list_prepend (axes, GDK_ATOM_TO_POINTER (axis_info.label));
945 return g_list_reverse (axes);
949 * gdk_device_get_axis_value:
950 * @device: a pointer #GdkDevice.
951 * @axes: pointer to an array of axes
952 * @axis_label: #GdkAtom with the axis label.
953 * @value: location to store the found value.
955 * Interprets an array of double as axis values for a given device,
956 * and locates the value in the array for a given axis label, as returned
957 * by gdk_device_list_axes()
959 * Returns: %TRUE if the given axis use was found, otherwise %FALSE.
964 gdk_device_get_axis_value (GdkDevice *device,
971 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
972 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
977 for (i = 0; i < device->axes->len; i++)
979 GdkAxisInfo axis_info;
981 axis_info = g_array_index (device->axes, GdkAxisInfo, i);
983 if (axis_info.label != axis_label)
996 * gdk_device_get_axis:
997 * @device: a #GdkDevice
998 * @axes: pointer to an array of axes
999 * @use: the use to look for
1000 * @value: location to store the found value.
1002 * Interprets an array of double as axis values for a given device,
1003 * and locates the value in the array for a given axis use.
1005 * Return value: %TRUE if the given axis use was found, otherwise %FALSE
1008 gdk_device_get_axis (GdkDevice *device,
1015 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
1016 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
1021 g_return_val_if_fail (device->axes != NULL, FALSE);
1023 for (i = 0; i < device->axes->len; i++)
1025 GdkAxisInfo axis_info;
1027 axis_info = g_array_index (device->axes, GdkAxisInfo, i);
1029 if (axis_info.use != use)
1042 get_native_grab_event_mask (GdkEventMask grab_mask)
1044 /* Similar to the above but for pointer events only */
1046 GDK_POINTER_MOTION_MASK |
1047 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1048 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1051 ~(GDK_POINTER_MOTION_HINT_MASK |
1052 GDK_BUTTON_MOTION_MASK |
1053 GDK_BUTTON1_MOTION_MASK |
1054 GDK_BUTTON2_MOTION_MASK |
1055 GDK_BUTTON3_MOTION_MASK));
1060 * @device: a #GdkDevice. To get the device you can use gtk_get_current_event_device()
1061 * or gdk_event_get_device() if the grab is in reaction to an event. Also, you can use
1062 * gdk_device_manager_get_client_pointer() but only in code that isn't triggered by a
1063 * #GdkEvent and there aren't other means to get a meaningful #GdkDevice to operate on.
1064 * @window: the #GdkWindow which will own the grab (the grab window)
1065 * @grab_ownership: specifies the grab ownership.
1066 * @owner_events: if %FALSE then all device events are reported with respect to
1067 * @window and are only reported if selected by @event_mask. If
1068 * %TRUE then pointer events for this application are reported
1069 * as normal, but pointer events outside this application are
1070 * reported with respect to @window and only if selected by
1071 * @event_mask. In either mode, unreported events are discarded.
1072 * @event_mask: specifies the event mask, which is used in accordance with
1074 * @cursor: the cursor to display while the grab is active if the device is
1075 * a pointer. If this is %NULL then the normal cursors are used for
1076 * @window and its descendants, and the cursor for @window is used
1078 * @time_: the timestamp of the event which led to this pointer grab. This
1079 * usually comes from the #GdkEvent struct, though %GDK_CURRENT_TIME
1080 * can be used if the time isn't known.
1082 * Grabs the device so that all events coming from this device are passed to
1083 * this application until the device is ungrabbed with gdk_device_ungrab(),
1084 * or the window becomes unviewable. This overrides any previous grab on the device
1087 * Device grabs are used for operations which need complete control over the
1088 * given device events (either pointer or keyboard). For example in GTK+ this
1089 * is used for Drag and Drop operations, popup menus and such.
1091 * Note that if the event mask of an X window has selected both button press
1092 * and button release events, then a button press event will cause an automatic
1093 * pointer grab until the button is released. X does this automatically since
1094 * most applications expect to receive button press and release events in pairs.
1095 * It is equivalent to a pointer grab on the window with @owner_events set to
1098 * If you set up anything at the time you take the grab that needs to be
1099 * cleaned up when the grab ends, you should handle the #GdkEventGrabBroken
1100 * events that are emitted when the grab ends unvoluntarily.
1102 * Returns: %GDK_GRAB_SUCCESS if the grab was successful.
1107 gdk_device_grab (GdkDevice *device,
1109 GdkGrabOwnership grab_ownership,
1110 gboolean owner_events,
1111 GdkEventMask event_mask,
1118 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_GRAB_SUCCESS);
1119 g_return_val_if_fail (GDK_IS_WINDOW (window), GDK_GRAB_SUCCESS);
1121 if (_gdk_native_windows)
1124 native = gdk_window_get_toplevel (window);
1126 while (native->window_type == GDK_WINDOW_OFFSCREEN)
1128 native = gdk_offscreen_window_get_embedder (native);
1130 if (native == NULL ||
1131 (!_gdk_window_has_impl (native) &&
1132 !gdk_window_is_viewable (native)))
1133 return GDK_GRAB_NOT_VIEWABLE;
1135 native = gdk_window_get_toplevel (native);
1138 if (native == NULL || GDK_WINDOW_DESTROYED (native))
1139 return GDK_GRAB_NOT_VIEWABLE;
1141 res = GDK_DEVICE_GET_CLASS (device)->grab (device,
1144 get_native_grab_event_mask (event_mask),
1149 if (res == GDK_GRAB_SUCCESS)
1151 GdkDisplay *display;
1154 display = gdk_window_get_display (window);
1155 serial = _gdk_display_get_next_serial (display);
1157 _gdk_display_add_device_grab (display,
1173 * gdk_device_ungrab:
1174 * @device: a #GdkDevice
1175 * @time_: a timestap (e.g. %GDK_CURRENT_TIME).
1177 * Release any grab on @device.
1182 gdk_device_ungrab (GdkDevice *device,
1185 g_return_if_fail (GDK_IS_DEVICE (device));
1187 GDK_DEVICE_GET_CLASS (device)->ungrab (device, time_);
1192 * @device: the device to warp.
1193 * @screen: the screen to warp @device to.
1194 * @x: the X coordinate of the destination.
1195 * @y: the Y coordinate of the destination.
1197 * Warps @device in @display to the point @x,@y on
1198 * the screen @screen, unless the device is confined
1199 * to a window by a grab, in which case it will be moved
1200 * as far as allowed by the grab. Warping the pointer
1201 * creates events as if the user had moved the mouse
1202 * instantaneously to the destination.
1204 * Note that the pointer should normally be under the
1205 * control of the user. This function was added to cover
1206 * some rare use cases like keyboard navigation support
1207 * for the color picker in the #GtkColorSelectionDialog.
1212 gdk_device_warp (GdkDevice *device,
1217 g_return_if_fail (GDK_IS_DEVICE (device));
1218 g_return_if_fail (GDK_IS_SCREEN (screen));
1219 g_return_if_fail (gdk_device_get_display (device) == gdk_screen_get_display (screen));
1221 GDK_DEVICE_GET_CLASS (device)->warp (device, screen, x, y);
1226 _gdk_device_reset_axes (GdkDevice *device)
1230 for (i = device->axes->len - 1; i >= 0; i--)
1231 g_array_remove_index (device->axes, i);
1233 g_object_notify (G_OBJECT (device), "n-axes");
1237 _gdk_device_add_axis (GdkDevice *device,
1244 GdkAxisInfo axis_info;
1247 axis_info.use = use;
1248 axis_info.label = label_atom;
1249 axis_info.min_value = min_value;
1250 axis_info.max_value = max_value;
1251 axis_info.resolution = resolution;
1257 axis_info.min_axis = 0;
1258 axis_info.max_axis = 0;
1260 case GDK_AXIS_XTILT:
1261 case GDK_AXIS_YTILT:
1262 axis_info.min_axis = -1;
1263 axis_info.max_axis = 1;
1266 axis_info.min_axis = 0;
1267 axis_info.max_axis = 1;
1271 device->axes = g_array_append_val (device->axes, axis_info);
1272 pos = device->axes->len - 1;
1274 g_object_notify (G_OBJECT (device), "n-axes");
1280 _gdk_device_set_keys (GdkDevice *device,
1284 g_free (device->keys);
1286 device->num_keys = num_keys;
1287 device->keys = g_new0 (GdkDeviceKey, num_keys);
1290 static GdkAxisInfo *
1291 find_axis_info (GArray *array,
1297 for (i = 0; i < GDK_AXIS_LAST; i++)
1299 info = &g_array_index (array, GdkAxisInfo, i);
1301 if (info->use == use)
1309 _gdk_device_get_axis_use (GdkDevice *device,
1314 info = g_array_index (device->axes, GdkAxisInfo, index_);
1319 _gdk_device_translate_window_coord (GdkDevice *device,
1323 gdouble *axis_value)
1325 GdkAxisInfo axis_info;
1326 GdkAxisInfo *axis_info_x, *axis_info_y;
1327 gdouble device_width, device_height;
1328 gdouble x_offset, y_offset;
1329 gdouble x_scale, y_scale;
1330 gdouble x_min, y_min;
1331 gdouble x_resolution, y_resolution;
1332 gdouble device_aspect;
1333 gint window_width, window_height;
1335 if (index_ >= device->axes->len)
1338 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1340 if (axis_info.use != GDK_AXIS_X &&
1341 axis_info.use != GDK_AXIS_Y)
1344 if (axis_info.use == GDK_AXIS_X)
1346 axis_info_x = &axis_info;
1347 axis_info_y = find_axis_info (device->axes, GDK_AXIS_Y);
1351 axis_info_x = find_axis_info (device->axes, GDK_AXIS_X);
1352 axis_info_y = &axis_info;
1355 device_width = axis_info_x->max_value - axis_info_x->min_value;
1356 device_height = axis_info_y->max_value - axis_info_y->min_value;
1358 if (device_width > 0)
1359 x_min = axis_info_x->min_value;
1362 device_width = gdk_screen_get_width (gdk_window_get_screen (window));
1366 if (device_height > 0)
1367 y_min = axis_info_y->min_value;
1370 device_height = gdk_screen_get_height (gdk_window_get_screen (window));
1374 window_width = gdk_window_get_width (window);
1375 window_height = gdk_window_get_height (window);
1377 x_resolution = axis_info_x->resolution;
1378 y_resolution = axis_info_y->resolution;
1381 * Some drivers incorrectly report the resolution of the device
1382 * as zero (in partiular linuxwacom < 0.5.3 with usb tablets).
1383 * This causes the device_aspect to become NaN and totally
1384 * breaks windowed mode. If this is the case, the best we can
1385 * do is to assume the resolution is non-zero is equal in both
1386 * directions (which is true for many devices). The absolute
1387 * value of the resolution doesn't matter since we only use the
1390 if (x_resolution == 0 || y_resolution == 0)
1396 device_aspect = (device_height * y_resolution) /
1397 (device_width * x_resolution);
1399 if (device_aspect * window_width >= window_height)
1401 /* device taller than window */
1402 x_scale = window_width / device_width;
1403 y_scale = (x_scale * x_resolution) / y_resolution;
1406 y_offset = - (device_height * y_scale - window_height) / 2;
1410 /* window taller than device */
1411 y_scale = window_height / device_height;
1412 x_scale = (y_scale * y_resolution) / x_resolution;
1415 x_offset = - (device_width * x_scale - window_width) / 2;
1420 if (axis_info.use == GDK_AXIS_X)
1421 *axis_value = x_offset + x_scale * (value - x_min);
1423 *axis_value = y_offset + y_scale * (value - y_min);
1430 _gdk_device_translate_screen_coord (GdkDevice *device,
1436 gdouble *axis_value)
1438 GdkAxisInfo axis_info;
1439 gdouble axis_width, scale, offset;
1441 if (device->mode != GDK_MODE_SCREEN)
1444 if (index_ >= device->axes->len)
1447 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1449 if (axis_info.use != GDK_AXIS_X &&
1450 axis_info.use != GDK_AXIS_Y)
1453 axis_width = axis_info.max_value - axis_info.min_value;
1455 if (axis_info.use == GDK_AXIS_X)
1458 scale = gdk_screen_get_width (gdk_window_get_screen (window)) / axis_width;
1462 offset = - window_root_x - window->abs_x;
1467 scale = gdk_screen_get_height (gdk_window_get_screen (window)) / axis_width;
1471 offset = - window_root_y - window->abs_y;
1475 *axis_value = offset + scale * (value - axis_info.min_value);
1481 _gdk_device_translate_axis (GdkDevice *device,
1484 gdouble *axis_value)
1486 GdkAxisInfo axis_info;
1487 gdouble axis_width, out;
1489 if (index_ >= device->axes->len)
1492 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1494 if (axis_info.use == GDK_AXIS_X ||
1495 axis_info.use == GDK_AXIS_Y)
1498 axis_width = axis_info.max_value - axis_info.min_value;
1499 out = (axis_info.max_axis * (value - axis_info.min_value) +
1500 axis_info.min_axis * (axis_info.max_value - value)) / axis_width;
1509 _gdk_device_query_state (GdkDevice *device,
1511 GdkWindow **root_window,
1512 GdkWindow **child_window,
1517 GdkModifierType *mask)
1519 return GDK_DEVICE_GET_CLASS (device)->query_state (device,
1531 _gdk_device_window_at_position (GdkDevice *device,
1534 GdkModifierType *mask,
1535 gboolean get_toplevel)
1537 return GDK_DEVICE_GET_CLASS (device)->window_at_position (device,