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 "gdkdevice.h"
24 #include "gdkdeviceprivate.h"
26 #include "gdkinternals.h"
29 typedef struct _GdkDeviceKey GdkDeviceKey;
34 GdkModifierType modifiers;
37 typedef struct _GdkAxisInfo GdkAxisInfo;
52 struct _GdkDevicePrivate
55 GdkInputSource source;
60 GdkDeviceManager *device_manager;
63 /* Paired master for master,
64 * associated master for slaves
66 GdkDevice *associated;
78 static guint signals [LAST_SIGNAL] = { 0 };
81 static void gdk_device_dispose (GObject *object);
82 static void gdk_device_set_property (GObject *object,
86 static void gdk_device_get_property (GObject *object,
92 G_DEFINE_ABSTRACT_TYPE (GdkDevice, gdk_device, G_TYPE_OBJECT)
99 PROP_ASSOCIATED_DEVICE,
109 gdk_device_class_init (GdkDeviceClass *klass)
111 GObjectClass *object_class = G_OBJECT_CLASS (klass);
113 object_class->dispose = gdk_device_dispose;
114 object_class->set_property = gdk_device_set_property;
115 object_class->get_property = gdk_device_get_property;
120 * The #GdkDisplay the #GdkDevice pertains to.
124 g_object_class_install_property (object_class,
126 g_param_spec_object ("display",
127 P_("Device Display"),
128 P_("Display which the device belongs to"),
130 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
131 G_PARAM_STATIC_STRINGS));
133 * GdkDevice:device-manager:
135 * The #GdkDeviceManager the #GdkDevice pertains to.
139 g_object_class_install_property (object_class,
141 g_param_spec_object ("device-manager",
142 P_("Device manager"),
143 P_("Device manager which the device belongs to"),
144 GDK_TYPE_DEVICE_MANAGER,
145 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
146 G_PARAM_STATIC_STRINGS));
154 g_object_class_install_property (object_class,
156 g_param_spec_string ("name",
160 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
161 G_PARAM_STATIC_STRINGS));
165 * Device role in the device manager.
169 g_object_class_install_property (object_class,
171 g_param_spec_enum ("type",
173 P_("Device role in the device manager"),
174 GDK_TYPE_DEVICE_TYPE,
175 GDK_DEVICE_TYPE_MASTER,
176 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
177 G_PARAM_STATIC_STRINGS));
179 * GdkDevice:associated-device:
181 * Associated pointer or keyboard with this device, if any. Devices of type #GDK_DEVICE_TYPE_MASTER
182 * always come in keyboard/pointer pairs. Other device types will have a %NULL associated device.
186 g_object_class_install_property (object_class,
187 PROP_ASSOCIATED_DEVICE,
188 g_param_spec_object ("associated-device",
189 P_("Associated device"),
190 P_("Associated pointer or keyboard with this device"),
192 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
194 * GdkDevice:input-source:
196 * Source type for the device.
200 g_object_class_install_property (object_class,
202 g_param_spec_enum ("input-source",
204 P_("Source type for the device"),
205 GDK_TYPE_INPUT_SOURCE,
207 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
208 G_PARAM_STATIC_STRINGS));
210 * GdkDevice:input-mode:
212 * Input mode for the device.
216 g_object_class_install_property (object_class,
218 g_param_spec_enum ("input-mode",
219 P_("Input mode for the device"),
220 P_("Input mode for the device"),
223 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
225 * GdkDevice:has-cursor:
227 * Whether the device is represented by a cursor on the screen. Devices of type
228 * %GDK_DEVICE_TYPE_MASTER will have %TRUE here.
232 g_object_class_install_property (object_class,
234 g_param_spec_boolean ("has-cursor",
235 P_("Whether the device has a cursor"),
236 P_("Whether there is a visible cursor following device motion"),
238 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
239 G_PARAM_STATIC_STRINGS));
243 * Number of axes in the device.
247 g_object_class_install_property (object_class,
249 g_param_spec_uint ("n-axes",
250 P_("Number of axes in the device"),
251 P_("Number of axes in the device"),
253 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
256 * GdkDevice::changed:
257 * @device: the #GdkDevice that changed.
259 * The ::changed signal is emitted either when the #GdkDevice
260 * has changed the number of either axes or keys. For example
261 * In X this will normally happen when the slave device routing
262 * events through the master device changes (for example, user
263 * switches from the USB mouse to a tablet), in that case the
264 * master device will change to reflect the new slave device
268 g_signal_new (g_intern_static_string ("changed"),
269 G_TYPE_FROM_CLASS (object_class),
272 g_cclosure_marshal_VOID__VOID,
275 g_type_class_add_private (object_class, sizeof (GdkDevicePrivate));
279 gdk_device_init (GdkDevice *device)
281 GdkDevicePrivate *priv;
283 device->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (device,
287 priv->axes = g_array_new (FALSE, TRUE, sizeof (GdkAxisInfo));
291 gdk_device_dispose (GObject *object)
293 GdkDevicePrivate *priv;
296 device = GDK_DEVICE (object);
299 if (priv->type == GDK_DEVICE_TYPE_SLAVE)
300 _gdk_device_remove_slave (priv->associated, device);
302 if (priv->associated)
304 _gdk_device_set_associated_device (priv->associated, NULL);
305 g_object_unref (priv->associated);
306 priv->associated = NULL;
311 g_array_free (priv->axes, TRUE);
321 G_OBJECT_CLASS (gdk_device_parent_class)->dispose (object);
325 gdk_device_set_property (GObject *object,
330 GdkDevice *device = GDK_DEVICE (object);
331 GdkDevicePrivate *priv = device->priv;
336 priv->display = g_value_get_object (value);
338 case PROP_DEVICE_MANAGER:
339 priv->device_manager = g_value_get_object (value);
345 priv->name = g_value_dup_string (value);
348 priv->type = g_value_get_enum (value);
350 case PROP_INPUT_SOURCE:
351 priv->source = g_value_get_enum (value);
353 case PROP_INPUT_MODE:
354 gdk_device_set_mode (device, g_value_get_enum (value));
356 case PROP_HAS_CURSOR:
357 priv->has_cursor = g_value_get_boolean (value);
360 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
366 gdk_device_get_property (GObject *object,
371 GdkDevice *device = GDK_DEVICE (object);
372 GdkDevicePrivate *priv = device->priv;
377 g_value_set_object (value, priv->display);
379 case PROP_DEVICE_MANAGER:
380 g_value_set_object (value, priv->device_manager);
382 case PROP_ASSOCIATED_DEVICE:
383 g_value_set_object (value, priv->associated);
386 g_value_set_string (value, priv->name);
389 g_value_set_enum (value, priv->type);
391 case PROP_INPUT_SOURCE:
392 g_value_set_enum (value, priv->source);
394 case PROP_INPUT_MODE:
395 g_value_set_enum (value, priv->mode);
397 case PROP_HAS_CURSOR:
398 g_value_set_boolean (value, priv->has_cursor);
401 g_value_set_uint (value, priv->axes->len);
404 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
410 * gdk_device_get_state:
411 * @device: a #GdkDevice.
412 * @window: a #GdkWindow.
413 * @axes: an array of doubles to store the values of the axes of @device in,
415 * @mask: location to store the modifiers, or %NULL.
417 * Gets the current state of a device relative to @window.
420 gdk_device_get_state (GdkDevice *device,
423 GdkModifierType *mask)
425 g_return_if_fail (GDK_IS_DEVICE (device));
426 g_return_if_fail (GDK_IS_WINDOW (window));
428 if (GDK_DEVICE_GET_CLASS (device)->get_state)
429 GDK_DEVICE_GET_CLASS (device)->get_state (device, window, axes, mask);
433 * gdk_device_get_history:
434 * @device: a #GdkDevice
435 * @window: the window with respect to which which the event coordinates will be reported
436 * @start: starting timestamp for range of events to return
437 * @stop: ending timestamp for the range of events to return
438 * @events: (array length=n_events) (out) (transfer none): location to store a newly-allocated array of #GdkTimeCoord, or %NULL
439 * @n_events: location to store the length of @events, or %NULL
441 * Obtains the motion history for a device; given a starting and
442 * ending timestamp, return all events in the motion history for
443 * the device in the given range of time. Some windowing systems
444 * do not support motion history, in which case, %FALSE will
445 * be returned. (This is not distinguishable from the case where
446 * motion history is supported and no events were found.)
448 * Return value: %TRUE if the windowing system supports motion history and
449 * at least one event was found.
452 gdk_device_get_history (GdkDevice *device,
456 GdkTimeCoord ***events,
459 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
460 g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
468 if (GDK_WINDOW_DESTROYED (window))
471 if (!GDK_DEVICE_GET_CLASS (device)->get_history)
474 return GDK_DEVICE_GET_CLASS (device)->get_history (device, window,
480 _gdk_device_allocate_history (GdkDevice *device,
483 GdkTimeCoord **result = g_new (GdkTimeCoord *, n_events);
486 for (i = 0; i < n_events; i++)
487 result[i] = g_malloc (sizeof (GdkTimeCoord) -
488 sizeof (double) * (GDK_MAX_TIMECOORD_AXES - device->priv->axes->len));
493 * gdk_device_free_history:
494 * @events: (inout) (transfer none): an array of #GdkTimeCoord.
495 * @n_events: the length of the array.
497 * Frees an array of #GdkTimeCoord that was returned by gdk_device_get_history().
500 gdk_device_free_history (GdkTimeCoord **events,
505 for (i = 0; i < n_events; i++)
512 * gdk_device_get_name:
513 * @device: a #GdkDevice
515 * Determines the name of the device.
517 * Return value: a name
522 gdk_device_get_name (GdkDevice *device)
524 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
526 return device->priv->name;
530 * gdk_device_get_has_cursor:
531 * @device: a #GdkDevice
533 * Determines whether the pointer follows device motion.
535 * Return value: %TRUE if the pointer follows device motion
540 gdk_device_get_has_cursor (GdkDevice *device)
542 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
544 return device->priv->has_cursor;
548 * gdk_device_get_source:
549 * @device: a #GdkDevice
551 * Determines the type of the device.
553 * Return value: a #GdkInputSource
558 gdk_device_get_source (GdkDevice *device)
560 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
562 return device->priv->source;
566 * gdk_device_set_source:
567 * @device: a #GdkDevice.
568 * @source: the source type.
570 * Sets the source type for an input device.
573 gdk_device_set_source (GdkDevice *device,
574 GdkInputSource source)
576 g_return_if_fail (GDK_IS_DEVICE (device));
578 device->priv->source = source;
579 g_object_notify (G_OBJECT (device), "input-source");
583 * gdk_device_get_mode:
584 * @device: a #GdkDevice
586 * Determines the mode of the device.
588 * Return value: a #GdkInputSource
593 gdk_device_get_mode (GdkDevice *device)
595 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
597 return device->priv->mode;
601 * gdk_device_set_mode:
602 * @device: a #GdkDevice.
603 * @mode: the input mode.
605 * Sets a the mode of an input device. The mode controls if the
606 * device is active and whether the device's range is mapped to the
607 * entire screen or to a single window.
609 * Returns: %TRUE if the mode was successfully changed.
612 gdk_device_set_mode (GdkDevice *device,
615 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
617 if (device->priv->mode == mode)
620 if (mode == GDK_MODE_DISABLED &&
621 gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER)
624 /* FIXME: setting has_cursor when mode is window? */
626 device->priv->mode = mode;
627 g_object_notify (G_OBJECT (device), "input-mode");
629 if (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER)
630 _gdk_input_check_extension_events (device);
636 * gdk_device_get_n_keys:
637 * @device: a #GdkDevice
639 * Returns the number of keys the device currently has.
641 * Returns: the number of keys.
646 gdk_device_get_n_keys (GdkDevice *device)
648 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
650 return device->priv->num_keys;
654 * gdk_device_get_key:
655 * @device: a #GdkDevice.
656 * @index_: the index of the macro button to get.
657 * @keyval: return value for the keyval.
658 * @modifiers: return value for modifiers.
660 * If @index_ has a valid keyval, this function will return %TRUE
661 * and fill in @keyval and @modifiers with the keyval settings.
663 * Returns: %TRUE if keyval is set for @index.
668 gdk_device_get_key (GdkDevice *device,
671 GdkModifierType *modifiers)
673 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
674 g_return_val_if_fail (index_ < device->priv->num_keys, FALSE);
676 if (!device->priv->keys[index_].keyval &&
677 !device->priv->keys[index_].modifiers)
681 *keyval = device->priv->keys[index_].keyval;
684 *modifiers = device->priv->keys[index_].modifiers;
690 * gdk_device_set_key:
691 * @device: a #GdkDevice
692 * @index_: the index of the macro button to set
693 * @keyval: the keyval to generate
694 * @modifiers: the modifiers to set
696 * Specifies the X key event to generate when a macro button of a device
700 gdk_device_set_key (GdkDevice *device,
703 GdkModifierType modifiers)
705 g_return_if_fail (GDK_IS_DEVICE (device));
706 g_return_if_fail (index_ < device->priv->num_keys);
708 device->priv->keys[index_].keyval = keyval;
709 device->priv->keys[index_].modifiers = modifiers;
713 * gdk_device_get_axis_use:
714 * @device: a #GdkDevice.
715 * @index_: the index of the axis.
717 * Returns the axis use for @index_.
719 * Returns: a #GdkAxisUse specifying how the axis is used.
724 gdk_device_get_axis_use (GdkDevice *device,
729 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_AXIS_IGNORE);
730 g_return_val_if_fail (index_ < device->priv->axes->len, GDK_AXIS_IGNORE);
732 info = &g_array_index (device->priv->axes, GdkAxisInfo, index_);
738 * gdk_device_set_axis_use:
739 * @device: a #GdkDevice
740 * @index_: the index of the axis
741 * @use: specifies how the axis is used
743 * Specifies how an axis of a device is used.
746 gdk_device_set_axis_use (GdkDevice *device,
750 GdkDevicePrivate *priv;
753 g_return_if_fail (GDK_IS_DEVICE (device));
754 g_return_if_fail (index_ < device->priv->axes->len);
757 info = &g_array_index (priv->axes, GdkAxisInfo, index_);
780 * gdk_device_get_display:
781 * @device: a #GdkDevice
783 * Returns the #GdkDisplay to which @device pertains.
785 * Returns: (transfer none): a #GdkDisplay. This memory is owned
786 * by GTK+, and must not be freed or unreffed.
791 gdk_device_get_display (GdkDevice *device)
793 GdkDevicePrivate *priv;
795 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
799 return priv->display;
803 * gdk_device_get_associated_device:
804 * @device: a #GdkDevice
806 * Returns the associated device to @device, if @device is of type
807 * %GDK_DEVICE_TYPE_MASTER, it will return the paired pointer or
810 * If @device is of type %GDK_DEVICE_TYPE_SLAVE, it will return
811 * the master device to which @device is attached to.
813 * If @device is of type %GDK_DEVICE_TYPE_FLOATING, %NULL will be
814 * returned, as there is no associated device.
816 * Returns: (transfer none): The associated device, or %NULL
821 gdk_device_get_associated_device (GdkDevice *device)
823 GdkDevicePrivate *priv;
825 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
829 return priv->associated;
833 _gdk_device_set_device_type (GdkDevice *device,
836 GdkDevicePrivate *priv;
840 if (priv->type != type)
844 g_print ("Setting device type to %d\n", type);
846 g_object_notify (G_OBJECT (device), "type");
851 _gdk_device_set_associated_device (GdkDevice *device,
852 GdkDevice *associated)
854 GdkDevicePrivate *priv;
856 g_return_if_fail (GDK_IS_DEVICE (device));
857 g_return_if_fail (associated == NULL || GDK_IS_DEVICE (associated));
861 if (priv->associated == associated)
864 if (priv->associated)
866 g_object_unref (priv->associated);
867 priv->associated = NULL;
871 priv->associated = g_object_ref (associated);
873 if (priv->type != GDK_DEVICE_TYPE_MASTER)
875 if (priv->associated)
876 _gdk_device_set_device_type (device, GDK_DEVICE_TYPE_SLAVE);
878 _gdk_device_set_device_type (device, GDK_DEVICE_TYPE_FLOATING);
883 * gdk_device_list_slave_devices:
884 * @device: a #GdkDevice
886 * If the device if of type %GDK_DEVICE_TYPE_MASTER, it will return
887 * the list of slave devices attached to it, otherwise it will return
890 * Returns: (transfer container): the list of slave devices, or %NULL. The
891 * list must be freed with g_list_free(), the contents of the list
892 * are owned by GTK+ and should not be freed.
895 gdk_device_list_slave_devices (GdkDevice *device)
897 GdkDevicePrivate *priv;
899 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
900 g_return_val_if_fail (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER, NULL);
904 return g_list_copy (priv->slaves);
908 _gdk_device_add_slave (GdkDevice *device,
911 GdkDevicePrivate *priv;
913 g_return_if_fail (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER);
914 g_return_if_fail (gdk_device_get_device_type (slave) != GDK_DEVICE_TYPE_MASTER);
918 g_print ("Adding %s ---> %s\n",
919 gdk_device_get_name (slave),
920 gdk_device_get_name (device));
922 if (!g_list_find (priv->slaves, slave))
923 priv->slaves = g_list_prepend (priv->slaves, slave);
927 _gdk_device_remove_slave (GdkDevice *device,
930 GdkDevicePrivate *priv;
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);
937 elem = g_list_find (priv->slaves, slave);
942 g_print ("Removing %s ---> %s\n",
943 gdk_device_get_name (slave),
944 gdk_device_get_name (device));
946 priv->slaves = g_list_delete_link (priv->slaves, elem);
950 * gdk_device_get_device_type:
951 * @device: a #GdkDevice
953 * Returns the device type for @device.
955 * Returns: the #GdkDeviceType for @device.
960 gdk_device_get_device_type (GdkDevice *device)
962 GdkDevicePrivate *priv;
964 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_DEVICE_TYPE_MASTER);
972 * gdk_device_get_n_axes:
973 * @device: a #GdkDevice
975 * Returns the number of axes the device currently has.
977 * Returns: the number of axes.
982 gdk_device_get_n_axes (GdkDevice *device)
984 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
986 return device->priv->axes->len;
990 * gdk_device_list_axes:
991 * @device: a #GdkDevice
993 * Returns a #GList of #GdkAtom<!-- -->s, containing the labels for
994 * the axes that @device currently has.
996 * Returns: (transfer container) (element-type GdkAtom):
997 * A #GList of #GdkAtom<!-- -->s, free with g_list_free().
1002 gdk_device_list_axes (GdkDevice *device)
1004 GdkDevicePrivate *priv;
1008 priv = device->priv;
1010 for (i = 0; i < priv->axes->len; i++)
1012 GdkAxisInfo axis_info;
1014 axis_info = g_array_index (priv->axes, GdkAxisInfo, i);
1015 axes = g_list_prepend (axes, GDK_ATOM_TO_POINTER (axis_info.label));
1018 return g_list_reverse (axes);
1022 * gdk_device_get_axis_value:
1023 * @device: a #GdkDevice.
1024 * @axes: pointer to an array of axes
1025 * @axis_label: #GdkAtom with the axis label.
1026 * @value: location to store the found value.
1028 * Interprets an array of double as axis values for a given device,
1029 * and locates the value in the array for a given axis label, as returned
1030 * by gdk_device_list_axes()
1032 * Returns: %TRUE if the given axis use was found, otherwise %FALSE.
1037 gdk_device_get_axis_value (GdkDevice *device,
1042 GdkDevicePrivate *priv;
1045 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
1050 priv = device->priv;
1052 for (i = 0; i < priv->axes->len; i++)
1054 GdkAxisInfo axis_info;
1056 axis_info = g_array_index (priv->axes, GdkAxisInfo, i);
1058 if (axis_info.label != axis_label)
1071 * gdk_device_get_axis:
1072 * @device: a #GdkDevice
1073 * @axes: pointer to an array of axes
1074 * @use: the use to look for
1075 * @value: location to store the found value.
1077 * Interprets an array of double as axis values for a given device,
1078 * and locates the value in the array for a given axis use.
1080 * Return value: %TRUE if the given axis use was found, otherwise %FALSE
1083 gdk_device_get_axis (GdkDevice *device,
1088 GdkDevicePrivate *priv;
1091 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
1096 priv = device->priv;
1098 g_return_val_if_fail (priv->axes != NULL, FALSE);
1100 for (i = 0; i < priv->axes->len; i++)
1102 GdkAxisInfo axis_info;
1104 axis_info = g_array_index (priv->axes, GdkAxisInfo, i);
1106 if (axis_info.use != use)
1119 get_native_grab_event_mask (GdkEventMask grab_mask)
1121 /* Similar to the above but for pointer events only */
1123 GDK_POINTER_MOTION_MASK |
1124 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1125 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1128 ~(GDK_POINTER_MOTION_HINT_MASK |
1129 GDK_BUTTON_MOTION_MASK |
1130 GDK_BUTTON1_MOTION_MASK |
1131 GDK_BUTTON2_MOTION_MASK |
1132 GDK_BUTTON3_MOTION_MASK));
1137 * @device: a #GdkDevice
1138 * @window: the #GdkWindow which will own the grab (the grab window)
1139 * @grab_ownership: specifies the grab ownership.
1140 * @owner_events: if %FALSE then all device events are reported with respect to
1141 * @window and are only reported if selected by @event_mask. If
1142 * %TRUE then pointer events for this application are reported
1143 * as normal, but pointer events outside this application are
1144 * reported with respect to @window and only if selected by
1145 * @event_mask. In either mode, unreported events are discarded.
1146 * @event_mask: specifies the event mask, which is used in accordance with
1148 * @cursor: the cursor to display while the grab is active if the device is
1149 * a pointer. If this is %NULL then the normal cursors are used for
1150 * @window and its descendants, and the cursor for @window is used
1152 * @time_: the timestamp of the event which led to this pointer grab. This
1153 * usually comes from the #GdkEvent struct, though %GDK_CURRENT_TIME
1154 * can be used if the time isn't known.
1156 * Grabs the device so that all events coming from this device are passed to
1157 * this application until the device is ungrabbed with gdk_device_ungrab(),
1158 * or the window becomes unviewable. This overrides any previous grab on the device
1161 * Device grabs are used for operations which need complete control over the
1162 * given device events (either pointer or keyboard). For example in GTK+ this
1163 * is used for Drag and Drop operations, popup menus and such.
1165 * Note that if the event mask of an X window has selected both button press
1166 * and button release events, then a button press event will cause an automatic
1167 * pointer grab until the button is released. X does this automatically since
1168 * most applications expect to receive button press and release events in pairs.
1169 * It is equivalent to a pointer grab on the window with @owner_events set to
1172 * If you set up anything at the time you take the grab that needs to be
1173 * cleaned up when the grab ends, you should handle the #GdkEventGrabBroken
1174 * events that are emitted when the grab ends unvoluntarily.
1176 * Returns: %GDK_GRAB_SUCCESS if the grab was successful.
1181 gdk_device_grab (GdkDevice *device,
1183 GdkGrabOwnership grab_ownership,
1184 gboolean owner_events,
1185 GdkEventMask event_mask,
1192 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
1193 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
1195 if (_gdk_native_windows)
1198 native = gdk_window_get_toplevel (window);
1200 while (native->window_type == GDK_WINDOW_OFFSCREEN)
1202 native = gdk_offscreen_window_get_embedder (native);
1204 if (native == NULL ||
1205 (!_gdk_window_has_impl (native) &&
1206 !gdk_window_is_viewable (native)))
1207 return GDK_GRAB_NOT_VIEWABLE;
1209 native = gdk_window_get_toplevel (native);
1212 res = _gdk_windowing_device_grab (device,
1216 get_native_grab_event_mask (event_mask),
1221 if (res == GDK_GRAB_SUCCESS)
1223 GdkDisplay *display;
1226 display = gdk_window_get_display (window);
1227 serial = _gdk_windowing_window_get_next_serial (display);
1229 _gdk_display_add_device_grab (display,
1246 _gdk_device_reset_axes (GdkDevice *device)
1248 GdkDevicePrivate *priv;
1251 priv = device->priv;
1253 for (i = priv->axes->len - 1; i >= 0; i--)
1254 g_array_remove_index (priv->axes, i);
1256 g_object_notify (G_OBJECT (device), "n-axes");
1260 _gdk_device_add_axis (GdkDevice *device,
1267 GdkDevicePrivate *priv;
1268 GdkAxisInfo axis_info;
1271 priv = device->priv;
1273 axis_info.use = use;
1274 axis_info.label = label_atom;
1275 axis_info.min_value = min_value;
1276 axis_info.max_value = max_value;
1277 axis_info.resolution = resolution;
1283 axis_info.min_axis = 0;
1284 axis_info.max_axis = 0;
1286 case GDK_AXIS_XTILT:
1287 case GDK_AXIS_YTILT:
1288 axis_info.min_axis = -1;
1289 axis_info.max_axis = 1;
1292 axis_info.min_axis = 0;
1293 axis_info.max_axis = 1;
1297 priv->axes = g_array_append_val (priv->axes, axis_info);
1298 pos = device->priv->axes->len - 1;
1300 g_object_notify (G_OBJECT (device), "n-axes");
1306 _gdk_device_set_keys (GdkDevice *device,
1309 if (device->priv->keys)
1310 g_free (device->priv->keys);
1312 device->priv->num_keys = num_keys;
1313 device->priv->keys = g_new0 (GdkDeviceKey, num_keys);
1316 static GdkAxisInfo *
1317 find_axis_info (GArray *array,
1323 for (i = 0; i < GDK_AXIS_LAST; i++)
1325 info = &g_array_index (array, GdkAxisInfo, i);
1327 if (info->use == use)
1335 _gdk_device_get_axis_use (GdkDevice *device,
1338 GdkDevicePrivate *priv;
1341 priv = device->priv;
1343 info = g_array_index (priv->axes, GdkAxisInfo, index_);
1348 _gdk_device_translate_window_coord (GdkDevice *device,
1352 gdouble *axis_value)
1354 GdkDevicePrivate *priv;
1355 GdkAxisInfo axis_info;
1356 GdkAxisInfo *axis_info_x, *axis_info_y;
1357 gdouble device_width, device_height;
1358 gdouble x_offset, y_offset;
1359 gdouble x_scale, y_scale;
1360 gdouble x_min, y_min;
1361 gdouble x_resolution, y_resolution;
1362 gdouble device_aspect;
1363 gint window_width, window_height;
1365 priv = device->priv;
1367 if (index_ >= priv->axes->len)
1370 axis_info = g_array_index (priv->axes, GdkAxisInfo, index_);
1372 if (axis_info.use != GDK_AXIS_X &&
1373 axis_info.use != GDK_AXIS_Y)
1376 if (axis_info.use == GDK_AXIS_X)
1378 axis_info_x = &axis_info;
1379 axis_info_y = find_axis_info (priv->axes, GDK_AXIS_Y);
1383 axis_info_x = find_axis_info (priv->axes, GDK_AXIS_X);
1384 axis_info_y = &axis_info;
1387 device_width = axis_info_x->max_value - axis_info_x->min_value;
1388 device_height = axis_info_y->max_value - axis_info_y->min_value;
1390 if (device_width > 0)
1391 x_min = axis_info_x->min_value;
1394 device_width = gdk_screen_get_width (gdk_window_get_screen (window));
1398 if (device_height > 0)
1399 y_min = axis_info_y->min_value;
1402 device_height = gdk_screen_get_height (gdk_window_get_screen (window));
1406 window_width = gdk_window_get_width (window);
1407 window_height = gdk_window_get_height (window);
1409 x_resolution = axis_info_x->resolution;
1410 y_resolution = axis_info_y->resolution;
1413 * Some drivers incorrectly report the resolution of the device
1414 * as zero (in partiular linuxwacom < 0.5.3 with usb tablets).
1415 * This causes the device_aspect to become NaN and totally
1416 * breaks windowed mode. If this is the case, the best we can
1417 * do is to assume the resolution is non-zero is equal in both
1418 * directions (which is true for many devices). The absolute
1419 * value of the resolution doesn't matter since we only use the
1422 if (x_resolution == 0 || y_resolution == 0)
1428 device_aspect = (device_height * y_resolution) /
1429 (device_width * x_resolution);
1431 if (device_aspect * window_width >= window_height)
1433 /* device taller than window */
1434 x_scale = window_width / device_width;
1435 y_scale = (x_scale * x_resolution) / y_resolution;
1438 y_offset = - (device_height * y_scale - window_height) / 2;
1442 /* window taller than device */
1443 y_scale = window_height / device_height;
1444 x_scale = (y_scale * y_resolution) / x_resolution;
1447 x_offset = - (device_width * x_scale - window_width) / 2;
1452 if (axis_info.use == GDK_AXIS_X)
1453 *axis_value = x_offset + x_scale * (value - x_min);
1455 *axis_value = y_offset + y_scale * (value - y_min);
1462 _gdk_device_translate_screen_coord (GdkDevice *device,
1468 gdouble *axis_value)
1470 GdkDevicePrivate *priv = device->priv;
1471 GdkAxisInfo axis_info;
1472 gdouble axis_width, scale, offset;
1474 if (priv->mode != GDK_MODE_SCREEN)
1477 if (index_ >= priv->axes->len)
1480 axis_info = g_array_index (priv->axes, GdkAxisInfo, index_);
1482 if (axis_info.use != GDK_AXIS_X &&
1483 axis_info.use != GDK_AXIS_Y)
1486 axis_width = axis_info.max_value - axis_info.min_value;
1488 if (axis_info.use == GDK_AXIS_X)
1491 scale = gdk_screen_get_width (gdk_window_get_screen (window)) / axis_width;
1495 offset = - window_root_x - window->abs_x;
1500 scale = gdk_screen_get_height (gdk_window_get_screen (window)) / axis_width;
1504 offset = - window_root_y - window->abs_y;
1508 *axis_value = offset + scale * (value - axis_info.min_value);
1514 _gdk_device_translate_axis (GdkDevice *device,
1517 gdouble *axis_value)
1519 GdkDevicePrivate *priv;
1520 GdkAxisInfo axis_info;
1521 gdouble axis_width, out;
1523 priv = device->priv;
1525 if (index_ >= priv->axes->len)
1528 axis_info = g_array_index (priv->axes, GdkAxisInfo, index_);
1530 if (axis_info.use == GDK_AXIS_X ||
1531 axis_info.use == GDK_AXIS_Y)
1534 axis_width = axis_info.max_value - axis_info.min_value;
1535 out = (axis_info.max_axis * (value - axis_info.min_value) +
1536 axis_info.min_axis * (axis_info.max_value - value)) / axis_width;