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 "gdkinternals.h"
25 #include "gdkdeviceprivate.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 pointer 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_device_get_source (device) != GDK_SOURCE_KEYBOARD);
427 g_return_if_fail (GDK_IS_WINDOW (window));
429 if (GDK_DEVICE_GET_CLASS (device)->get_state)
430 GDK_DEVICE_GET_CLASS (device)->get_state (device, window, axes, mask);
434 * gdk_device_get_history:
435 * @device: a #GdkDevice
436 * @window: the window with respect to which which the event coordinates will be reported
437 * @start: starting timestamp for range of events to return
438 * @stop: ending timestamp for the range of events to return
439 * @events: (array length=n_events) (out) (transfer none): location to store a newly-allocated array of #GdkTimeCoord, or %NULL
440 * @n_events: location to store the length of @events, or %NULL
442 * Obtains the motion history for a pointer device; given a starting and
443 * ending timestamp, return all events in the motion history for
444 * the device in the given range of time. Some windowing systems
445 * do not support motion history, in which case, %FALSE will
446 * be returned. (This is not distinguishable from the case where
447 * motion history is supported and no events were found.)
449 * Return value: %TRUE if the windowing system supports motion history and
450 * at least one event was found.
453 gdk_device_get_history (GdkDevice *device,
457 GdkTimeCoord ***events,
460 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
461 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
462 g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
470 if (GDK_WINDOW_DESTROYED (window))
473 if (!GDK_DEVICE_GET_CLASS (device)->get_history)
476 return GDK_DEVICE_GET_CLASS (device)->get_history (device, window,
482 _gdk_device_allocate_history (GdkDevice *device,
485 GdkTimeCoord **result = g_new (GdkTimeCoord *, n_events);
488 for (i = 0; i < n_events; i++)
489 result[i] = g_malloc (sizeof (GdkTimeCoord) -
490 sizeof (double) * (GDK_MAX_TIMECOORD_AXES - device->priv->axes->len));
495 * gdk_device_free_history:
496 * @events: (inout) (transfer none): an array of #GdkTimeCoord.
497 * @n_events: the length of the array.
499 * Frees an array of #GdkTimeCoord that was returned by gdk_device_get_history().
502 gdk_device_free_history (GdkTimeCoord **events,
507 for (i = 0; i < n_events; i++)
514 * gdk_device_get_name:
515 * @device: a #GdkDevice
517 * Determines the name of the device.
519 * Return value: a name
524 gdk_device_get_name (GdkDevice *device)
526 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
528 return device->priv->name;
532 * gdk_device_get_has_cursor:
533 * @device: a #GdkDevice
535 * Determines whether the pointer follows device motion.
537 * Return value: %TRUE if the pointer follows device motion
542 gdk_device_get_has_cursor (GdkDevice *device)
544 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
545 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
547 return device->priv->has_cursor;
551 * gdk_device_get_source:
552 * @device: a #GdkDevice
554 * Determines the type of the device.
556 * Return value: a #GdkInputSource
561 gdk_device_get_source (GdkDevice *device)
563 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
565 return device->priv->source;
569 * gdk_device_set_source:
570 * @device: a #GdkDevice.
571 * @source: the source type.
573 * Sets the source type for an input device.
576 gdk_device_set_source (GdkDevice *device,
577 GdkInputSource source)
579 g_return_if_fail (GDK_IS_DEVICE (device));
581 device->priv->source = source;
582 g_object_notify (G_OBJECT (device), "input-source");
586 * gdk_device_get_mode:
587 * @device: a #GdkDevice
589 * Determines the mode of the device.
591 * Return value: a #GdkInputSource
596 gdk_device_get_mode (GdkDevice *device)
598 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
600 return device->priv->mode;
604 * gdk_device_set_mode:
605 * @device: a #GdkDevice.
606 * @mode: the input mode.
608 * Sets a the mode of an input device. The mode controls if the
609 * device is active and whether the device's range is mapped to the
610 * entire screen or to a single window.
612 * Returns: %TRUE if the mode was successfully changed.
615 gdk_device_set_mode (GdkDevice *device,
618 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
620 if (device->priv->mode == mode)
623 if (mode == GDK_MODE_DISABLED &&
624 gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER)
627 device->priv->mode = mode;
628 g_object_notify (G_OBJECT (device), "input-mode");
634 * gdk_device_get_n_keys:
635 * @device: a #GdkDevice
637 * Returns the number of keys the device currently has.
639 * Returns: the number of keys.
644 gdk_device_get_n_keys (GdkDevice *device)
646 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
648 return device->priv->num_keys;
652 * gdk_device_get_key:
653 * @device: a #GdkDevice.
654 * @index_: the index of the macro button to get.
655 * @keyval: return value for the keyval.
656 * @modifiers: return value for modifiers.
658 * If @index_ has a valid keyval, this function will return %TRUE
659 * and fill in @keyval and @modifiers with the keyval settings.
661 * Returns: %TRUE if keyval is set for @index.
666 gdk_device_get_key (GdkDevice *device,
669 GdkModifierType *modifiers)
671 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
672 g_return_val_if_fail (index_ < device->priv->num_keys, FALSE);
674 if (!device->priv->keys[index_].keyval &&
675 !device->priv->keys[index_].modifiers)
679 *keyval = device->priv->keys[index_].keyval;
682 *modifiers = device->priv->keys[index_].modifiers;
688 * gdk_device_set_key:
689 * @device: a #GdkDevice
690 * @index_: the index of the macro button to set
691 * @keyval: the keyval to generate
692 * @modifiers: the modifiers to set
694 * Specifies the X key event to generate when a macro button of a device
698 gdk_device_set_key (GdkDevice *device,
701 GdkModifierType modifiers)
703 g_return_if_fail (GDK_IS_DEVICE (device));
704 g_return_if_fail (index_ < device->priv->num_keys);
706 device->priv->keys[index_].keyval = keyval;
707 device->priv->keys[index_].modifiers = modifiers;
711 * gdk_device_get_axis_use:
712 * @device: a pointer #GdkDevice.
713 * @index_: the index of the axis.
715 * Returns the axis use for @index_.
717 * Returns: a #GdkAxisUse specifying how the axis is used.
722 gdk_device_get_axis_use (GdkDevice *device,
727 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_AXIS_IGNORE);
728 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, GDK_AXIS_IGNORE);
729 g_return_val_if_fail (index_ < device->priv->axes->len, GDK_AXIS_IGNORE);
731 info = &g_array_index (device->priv->axes, GdkAxisInfo, index_);
737 * gdk_device_set_axis_use:
738 * @device: a pointer #GdkDevice
739 * @index_: the index of the axis
740 * @use: specifies how the axis is used
742 * Specifies how an axis of a device is used.
745 gdk_device_set_axis_use (GdkDevice *device,
749 GdkDevicePrivate *priv;
752 g_return_if_fail (GDK_IS_DEVICE (device));
753 g_return_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD);
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_object_notify (G_OBJECT (device), "type");
849 _gdk_device_set_associated_device (GdkDevice *device,
850 GdkDevice *associated)
852 GdkDevicePrivate *priv;
854 g_return_if_fail (GDK_IS_DEVICE (device));
855 g_return_if_fail (associated == NULL || GDK_IS_DEVICE (associated));
859 if (priv->associated == associated)
862 if (priv->associated)
864 g_object_unref (priv->associated);
865 priv->associated = NULL;
869 priv->associated = g_object_ref (associated);
871 if (priv->type != GDK_DEVICE_TYPE_MASTER)
873 if (priv->associated)
874 _gdk_device_set_device_type (device, GDK_DEVICE_TYPE_SLAVE);
876 _gdk_device_set_device_type (device, GDK_DEVICE_TYPE_FLOATING);
881 * gdk_device_list_slave_devices:
882 * @device: a #GdkDevice
884 * If the device if of type %GDK_DEVICE_TYPE_MASTER, it will return
885 * the list of slave devices attached to it, otherwise it will return
888 * Returns: (transfer container): the list of slave devices, or %NULL. The
889 * list must be freed with g_list_free(), the contents of the list
890 * are owned by GTK+ and should not be freed.
893 gdk_device_list_slave_devices (GdkDevice *device)
895 GdkDevicePrivate *priv;
897 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
898 g_return_val_if_fail (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER, NULL);
902 return g_list_copy (priv->slaves);
906 _gdk_device_add_slave (GdkDevice *device,
909 GdkDevicePrivate *priv;
911 g_return_if_fail (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER);
912 g_return_if_fail (gdk_device_get_device_type (slave) != GDK_DEVICE_TYPE_MASTER);
916 if (!g_list_find (priv->slaves, slave))
917 priv->slaves = g_list_prepend (priv->slaves, slave);
921 _gdk_device_remove_slave (GdkDevice *device,
924 GdkDevicePrivate *priv;
927 g_return_if_fail (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER);
928 g_return_if_fail (gdk_device_get_device_type (slave) != GDK_DEVICE_TYPE_MASTER);
931 elem = g_list_find (priv->slaves, slave);
936 priv->slaves = g_list_delete_link (priv->slaves, elem);
940 * gdk_device_get_device_type:
941 * @device: a #GdkDevice
943 * Returns the device type for @device.
945 * Returns: the #GdkDeviceType for @device.
950 gdk_device_get_device_type (GdkDevice *device)
952 GdkDevicePrivate *priv;
954 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_DEVICE_TYPE_MASTER);
962 * gdk_device_get_n_axes:
963 * @device: a pointer #GdkDevice
965 * Returns the number of axes the device currently has.
967 * Returns: the number of axes.
972 gdk_device_get_n_axes (GdkDevice *device)
974 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
975 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, 0);
977 return device->priv->axes->len;
981 * gdk_device_list_axes:
982 * @device: a pointer #GdkDevice
984 * Returns a #GList of #GdkAtom<!-- -->s, containing the labels for
985 * the axes that @device currently has.
987 * Returns: (transfer container) (element-type GdkAtom):
988 * A #GList of #GdkAtom<!-- -->s, free with g_list_free().
993 gdk_device_list_axes (GdkDevice *device)
995 GdkDevicePrivate *priv;
999 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
1000 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, NULL);
1002 priv = device->priv;
1004 for (i = 0; i < priv->axes->len; i++)
1006 GdkAxisInfo axis_info;
1008 axis_info = g_array_index (priv->axes, GdkAxisInfo, i);
1009 axes = g_list_prepend (axes, GDK_ATOM_TO_POINTER (axis_info.label));
1012 return g_list_reverse (axes);
1016 * gdk_device_get_axis_value:
1017 * @device: a pointer #GdkDevice.
1018 * @axes: pointer to an array of axes
1019 * @axis_label: #GdkAtom with the axis label.
1020 * @value: location to store the found value.
1022 * Interprets an array of double as axis values for a given device,
1023 * and locates the value in the array for a given axis label, as returned
1024 * by gdk_device_list_axes()
1026 * Returns: %TRUE if the given axis use was found, otherwise %FALSE.
1031 gdk_device_get_axis_value (GdkDevice *device,
1036 GdkDevicePrivate *priv;
1039 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
1040 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
1045 priv = device->priv;
1047 for (i = 0; i < priv->axes->len; i++)
1049 GdkAxisInfo axis_info;
1051 axis_info = g_array_index (priv->axes, GdkAxisInfo, i);
1053 if (axis_info.label != axis_label)
1066 * gdk_device_get_axis:
1067 * @device: a #GdkDevice
1068 * @axes: pointer to an array of axes
1069 * @use: the use to look for
1070 * @value: location to store the found value.
1072 * Interprets an array of double as axis values for a given device,
1073 * and locates the value in the array for a given axis use.
1075 * Return value: %TRUE if the given axis use was found, otherwise %FALSE
1078 gdk_device_get_axis (GdkDevice *device,
1083 GdkDevicePrivate *priv;
1086 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
1087 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
1092 priv = device->priv;
1094 g_return_val_if_fail (priv->axes != NULL, FALSE);
1096 for (i = 0; i < priv->axes->len; i++)
1098 GdkAxisInfo axis_info;
1100 axis_info = g_array_index (priv->axes, GdkAxisInfo, i);
1102 if (axis_info.use != use)
1115 get_native_grab_event_mask (GdkEventMask grab_mask)
1117 /* Similar to the above but for pointer events only */
1119 GDK_POINTER_MOTION_MASK |
1120 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1121 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1124 ~(GDK_POINTER_MOTION_HINT_MASK |
1125 GDK_BUTTON_MOTION_MASK |
1126 GDK_BUTTON1_MOTION_MASK |
1127 GDK_BUTTON2_MOTION_MASK |
1128 GDK_BUTTON3_MOTION_MASK));
1133 * @device: a #GdkDevice
1134 * @window: the #GdkWindow which will own the grab (the grab window)
1135 * @grab_ownership: specifies the grab ownership.
1136 * @owner_events: if %FALSE then all device events are reported with respect to
1137 * @window and are only reported if selected by @event_mask. If
1138 * %TRUE then pointer events for this application are reported
1139 * as normal, but pointer events outside this application are
1140 * reported with respect to @window and only if selected by
1141 * @event_mask. In either mode, unreported events are discarded.
1142 * @event_mask: specifies the event mask, which is used in accordance with
1144 * @cursor: the cursor to display while the grab is active if the device is
1145 * a pointer. If this is %NULL then the normal cursors are used for
1146 * @window and its descendants, and the cursor for @window is used
1148 * @time_: the timestamp of the event which led to this pointer grab. This
1149 * usually comes from the #GdkEvent struct, though %GDK_CURRENT_TIME
1150 * can be used if the time isn't known.
1152 * Grabs the device so that all events coming from this device are passed to
1153 * this application until the device is ungrabbed with gdk_device_ungrab(),
1154 * or the window becomes unviewable. This overrides any previous grab on the device
1157 * Device grabs are used for operations which need complete control over the
1158 * given device events (either pointer or keyboard). For example in GTK+ this
1159 * is used for Drag and Drop operations, popup menus and such.
1161 * Note that if the event mask of an X window has selected both button press
1162 * and button release events, then a button press event will cause an automatic
1163 * pointer grab until the button is released. X does this automatically since
1164 * most applications expect to receive button press and release events in pairs.
1165 * It is equivalent to a pointer grab on the window with @owner_events set to
1168 * If you set up anything at the time you take the grab that needs to be
1169 * cleaned up when the grab ends, you should handle the #GdkEventGrabBroken
1170 * events that are emitted when the grab ends unvoluntarily.
1172 * Returns: %GDK_GRAB_SUCCESS if the grab was successful.
1177 gdk_device_grab (GdkDevice *device,
1179 GdkGrabOwnership grab_ownership,
1180 gboolean owner_events,
1181 GdkEventMask event_mask,
1188 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_GRAB_SUCCESS);
1189 g_return_val_if_fail (GDK_IS_WINDOW (window), GDK_GRAB_SUCCESS);
1191 if (_gdk_native_windows)
1194 native = gdk_window_get_toplevel (window);
1196 while (native->window_type == GDK_WINDOW_OFFSCREEN)
1198 native = gdk_offscreen_window_get_embedder (native);
1200 if (native == NULL ||
1201 (!_gdk_window_has_impl (native) &&
1202 !gdk_window_is_viewable (native)))
1203 return GDK_GRAB_NOT_VIEWABLE;
1205 native = gdk_window_get_toplevel (native);
1208 if (native == NULL || GDK_WINDOW_DESTROYED (native))
1209 return GDK_GRAB_NOT_VIEWABLE;
1211 res = GDK_DEVICE_GET_CLASS (device)->grab (device,
1214 get_native_grab_event_mask (event_mask),
1219 if (res == GDK_GRAB_SUCCESS)
1221 GdkDisplay *display;
1224 display = gdk_window_get_display (window);
1225 serial = _gdk_windowing_window_get_next_serial (display);
1227 _gdk_display_add_device_grab (display,
1243 * gdk_device_ungrab:
1244 * @device: a #GdkDevice
1245 * @time_: a timestap (e.g. %GDK_CURRENT_TIME).
1247 * Release any grab on @device.
1252 gdk_device_ungrab (GdkDevice *device,
1255 g_return_if_fail (GDK_IS_DEVICE (device));
1257 GDK_DEVICE_GET_CLASS (device)->ungrab (device, time_);
1262 _gdk_device_reset_axes (GdkDevice *device)
1264 GdkDevicePrivate *priv;
1267 priv = device->priv;
1269 for (i = priv->axes->len - 1; i >= 0; i--)
1270 g_array_remove_index (priv->axes, i);
1272 g_object_notify (G_OBJECT (device), "n-axes");
1276 _gdk_device_add_axis (GdkDevice *device,
1283 GdkDevicePrivate *priv;
1284 GdkAxisInfo axis_info;
1287 priv = device->priv;
1289 axis_info.use = use;
1290 axis_info.label = label_atom;
1291 axis_info.min_value = min_value;
1292 axis_info.max_value = max_value;
1293 axis_info.resolution = resolution;
1299 axis_info.min_axis = 0;
1300 axis_info.max_axis = 0;
1302 case GDK_AXIS_XTILT:
1303 case GDK_AXIS_YTILT:
1304 axis_info.min_axis = -1;
1305 axis_info.max_axis = 1;
1308 axis_info.min_axis = 0;
1309 axis_info.max_axis = 1;
1313 priv->axes = g_array_append_val (priv->axes, axis_info);
1314 pos = device->priv->axes->len - 1;
1316 g_object_notify (G_OBJECT (device), "n-axes");
1322 _gdk_device_set_keys (GdkDevice *device,
1325 if (device->priv->keys)
1326 g_free (device->priv->keys);
1328 device->priv->num_keys = num_keys;
1329 device->priv->keys = g_new0 (GdkDeviceKey, num_keys);
1332 static GdkAxisInfo *
1333 find_axis_info (GArray *array,
1339 for (i = 0; i < GDK_AXIS_LAST; i++)
1341 info = &g_array_index (array, GdkAxisInfo, i);
1343 if (info->use == use)
1351 _gdk_device_get_axis_use (GdkDevice *device,
1354 GdkDevicePrivate *priv;
1357 priv = device->priv;
1359 info = g_array_index (priv->axes, GdkAxisInfo, index_);
1364 _gdk_device_translate_window_coord (GdkDevice *device,
1368 gdouble *axis_value)
1370 GdkDevicePrivate *priv;
1371 GdkAxisInfo axis_info;
1372 GdkAxisInfo *axis_info_x, *axis_info_y;
1373 gdouble device_width, device_height;
1374 gdouble x_offset, y_offset;
1375 gdouble x_scale, y_scale;
1376 gdouble x_min, y_min;
1377 gdouble x_resolution, y_resolution;
1378 gdouble device_aspect;
1379 gint window_width, window_height;
1381 priv = device->priv;
1383 if (index_ >= priv->axes->len)
1386 axis_info = g_array_index (priv->axes, GdkAxisInfo, index_);
1388 if (axis_info.use != GDK_AXIS_X &&
1389 axis_info.use != GDK_AXIS_Y)
1392 if (axis_info.use == GDK_AXIS_X)
1394 axis_info_x = &axis_info;
1395 axis_info_y = find_axis_info (priv->axes, GDK_AXIS_Y);
1399 axis_info_x = find_axis_info (priv->axes, GDK_AXIS_X);
1400 axis_info_y = &axis_info;
1403 device_width = axis_info_x->max_value - axis_info_x->min_value;
1404 device_height = axis_info_y->max_value - axis_info_y->min_value;
1406 if (device_width > 0)
1407 x_min = axis_info_x->min_value;
1410 device_width = gdk_screen_get_width (gdk_window_get_screen (window));
1414 if (device_height > 0)
1415 y_min = axis_info_y->min_value;
1418 device_height = gdk_screen_get_height (gdk_window_get_screen (window));
1422 window_width = gdk_window_get_width (window);
1423 window_height = gdk_window_get_height (window);
1425 x_resolution = axis_info_x->resolution;
1426 y_resolution = axis_info_y->resolution;
1429 * Some drivers incorrectly report the resolution of the device
1430 * as zero (in partiular linuxwacom < 0.5.3 with usb tablets).
1431 * This causes the device_aspect to become NaN and totally
1432 * breaks windowed mode. If this is the case, the best we can
1433 * do is to assume the resolution is non-zero is equal in both
1434 * directions (which is true for many devices). The absolute
1435 * value of the resolution doesn't matter since we only use the
1438 if (x_resolution == 0 || y_resolution == 0)
1444 device_aspect = (device_height * y_resolution) /
1445 (device_width * x_resolution);
1447 if (device_aspect * window_width >= window_height)
1449 /* device taller than window */
1450 x_scale = window_width / device_width;
1451 y_scale = (x_scale * x_resolution) / y_resolution;
1454 y_offset = - (device_height * y_scale - window_height) / 2;
1458 /* window taller than device */
1459 y_scale = window_height / device_height;
1460 x_scale = (y_scale * y_resolution) / x_resolution;
1463 x_offset = - (device_width * x_scale - window_width) / 2;
1468 if (axis_info.use == GDK_AXIS_X)
1469 *axis_value = x_offset + x_scale * (value - x_min);
1471 *axis_value = y_offset + y_scale * (value - y_min);
1478 _gdk_device_translate_screen_coord (GdkDevice *device,
1484 gdouble *axis_value)
1486 GdkDevicePrivate *priv = device->priv;
1487 GdkAxisInfo axis_info;
1488 gdouble axis_width, scale, offset;
1490 if (priv->mode != GDK_MODE_SCREEN)
1493 if (index_ >= priv->axes->len)
1496 axis_info = g_array_index (priv->axes, GdkAxisInfo, index_);
1498 if (axis_info.use != GDK_AXIS_X &&
1499 axis_info.use != GDK_AXIS_Y)
1502 axis_width = axis_info.max_value - axis_info.min_value;
1504 if (axis_info.use == GDK_AXIS_X)
1507 scale = gdk_screen_get_width (gdk_window_get_screen (window)) / axis_width;
1511 offset = - window_root_x - window->abs_x;
1516 scale = gdk_screen_get_height (gdk_window_get_screen (window)) / axis_width;
1520 offset = - window_root_y - window->abs_y;
1524 *axis_value = offset + scale * (value - axis_info.min_value);
1530 _gdk_device_translate_axis (GdkDevice *device,
1533 gdouble *axis_value)
1535 GdkDevicePrivate *priv;
1536 GdkAxisInfo axis_info;
1537 gdouble axis_width, out;
1539 priv = device->priv;
1541 if (index_ >= priv->axes->len)
1544 axis_info = g_array_index (priv->axes, GdkAxisInfo, index_);
1546 if (axis_info.use == GDK_AXIS_X ||
1547 axis_info.use == GDK_AXIS_Y)
1550 axis_width = axis_info.max_value - axis_info.min_value;
1551 out = (axis_info.max_axis * (value - axis_info.min_value) +
1552 axis_info.min_axis * (axis_info.max_value - value)) / axis_width;