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"
28 typedef struct _GdkAxisInfo GdkAxisInfo;
48 static guint signals [LAST_SIGNAL] = { 0 };
51 static void gdk_device_dispose (GObject *object);
52 static void gdk_device_set_property (GObject *object,
56 static void gdk_device_get_property (GObject *object,
62 G_DEFINE_ABSTRACT_TYPE (GdkDevice, gdk_device, G_TYPE_OBJECT)
69 PROP_ASSOCIATED_DEVICE,
79 gdk_device_class_init (GdkDeviceClass *klass)
81 GObjectClass *object_class = G_OBJECT_CLASS (klass);
83 object_class->dispose = gdk_device_dispose;
84 object_class->set_property = gdk_device_set_property;
85 object_class->get_property = gdk_device_get_property;
90 * The #GdkDisplay the #GdkDevice pertains to.
94 g_object_class_install_property (object_class,
96 g_param_spec_object ("display",
98 P_("Display which the device belongs to"),
100 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
101 G_PARAM_STATIC_STRINGS));
103 * GdkDevice:device-manager:
105 * The #GdkDeviceManager the #GdkDevice pertains to.
109 g_object_class_install_property (object_class,
111 g_param_spec_object ("device-manager",
112 P_("Device manager"),
113 P_("Device manager which the device belongs to"),
114 GDK_TYPE_DEVICE_MANAGER,
115 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
116 G_PARAM_STATIC_STRINGS));
124 g_object_class_install_property (object_class,
126 g_param_spec_string ("name",
130 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
131 G_PARAM_STATIC_STRINGS));
135 * Device role in the device manager.
139 g_object_class_install_property (object_class,
141 g_param_spec_enum ("type",
143 P_("Device role in the device manager"),
144 GDK_TYPE_DEVICE_TYPE,
145 GDK_DEVICE_TYPE_MASTER,
146 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
147 G_PARAM_STATIC_STRINGS));
149 * GdkDevice:associated-device:
151 * Associated pointer or keyboard with this device, if any. Devices of type #GDK_DEVICE_TYPE_MASTER
152 * always come in keyboard/pointer pairs. Other device types will have a %NULL associated device.
156 g_object_class_install_property (object_class,
157 PROP_ASSOCIATED_DEVICE,
158 g_param_spec_object ("associated-device",
159 P_("Associated device"),
160 P_("Associated pointer or keyboard with this device"),
162 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
164 * GdkDevice:input-source:
166 * Source type for the device.
170 g_object_class_install_property (object_class,
172 g_param_spec_enum ("input-source",
174 P_("Source type for the device"),
175 GDK_TYPE_INPUT_SOURCE,
177 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
178 G_PARAM_STATIC_STRINGS));
180 * GdkDevice:input-mode:
182 * Input mode for the device.
186 g_object_class_install_property (object_class,
188 g_param_spec_enum ("input-mode",
189 P_("Input mode for the device"),
190 P_("Input mode for the device"),
193 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
195 * GdkDevice:has-cursor:
197 * Whether the device is represented by a cursor on the screen. Devices of type
198 * %GDK_DEVICE_TYPE_MASTER will have %TRUE here.
202 g_object_class_install_property (object_class,
204 g_param_spec_boolean ("has-cursor",
205 P_("Whether the device has a cursor"),
206 P_("Whether there is a visible cursor following device motion"),
208 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
209 G_PARAM_STATIC_STRINGS));
213 * Number of axes in the device.
217 g_object_class_install_property (object_class,
219 g_param_spec_uint ("n-axes",
220 P_("Number of axes in the device"),
221 P_("Number of axes in the device"),
223 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
226 * GdkDevice::changed:
227 * @device: the #GdkDevice that changed.
229 * The ::changed signal is emitted either when the #GdkDevice
230 * has changed the number of either axes or keys. For example
231 * In X this will normally happen when the slave device routing
232 * events through the master device changes (for example, user
233 * switches from the USB mouse to a tablet), in that case the
234 * master device will change to reflect the new slave device
238 g_signal_new (g_intern_static_string ("changed"),
239 G_TYPE_FROM_CLASS (object_class),
242 g_cclosure_marshal_VOID__VOID,
247 gdk_device_init (GdkDevice *device)
249 device->axes = g_array_new (FALSE, TRUE, sizeof (GdkAxisInfo));
253 gdk_device_dispose (GObject *object)
255 GdkDevice *device = GDK_DEVICE (object);
257 if (device->type == GDK_DEVICE_TYPE_SLAVE)
258 _gdk_device_remove_slave (device->associated, device);
260 if (device->associated)
262 _gdk_device_set_associated_device (device->associated, NULL);
263 g_object_unref (device->associated);
264 device->associated = NULL;
269 g_array_free (device->axes, TRUE);
273 g_free (device->name);
274 g_free (device->keys);
279 G_OBJECT_CLASS (gdk_device_parent_class)->dispose (object);
283 gdk_device_set_property (GObject *object,
288 GdkDevice *device = GDK_DEVICE (object);
293 device->display = g_value_get_object (value);
295 case PROP_DEVICE_MANAGER:
296 device->manager = g_value_get_object (value);
300 g_free (device->name);
302 device->name = g_value_dup_string (value);
305 device->type = g_value_get_enum (value);
307 case PROP_INPUT_SOURCE:
308 device->source = g_value_get_enum (value);
310 case PROP_INPUT_MODE:
311 gdk_device_set_mode (device, g_value_get_enum (value));
313 case PROP_HAS_CURSOR:
314 device->has_cursor = g_value_get_boolean (value);
317 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
323 gdk_device_get_property (GObject *object,
328 GdkDevice *device = GDK_DEVICE (object);
333 g_value_set_object (value, device->display);
335 case PROP_DEVICE_MANAGER:
336 g_value_set_object (value, device->manager);
338 case PROP_ASSOCIATED_DEVICE:
339 g_value_set_object (value, device->associated);
342 g_value_set_string (value, device->name);
345 g_value_set_enum (value, device->type);
347 case PROP_INPUT_SOURCE:
348 g_value_set_enum (value, device->source);
350 case PROP_INPUT_MODE:
351 g_value_set_enum (value, device->mode);
353 case PROP_HAS_CURSOR:
354 g_value_set_boolean (value, device->has_cursor);
357 g_value_set_uint (value, device->axes->len);
360 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
366 * gdk_device_get_state:
367 * @device: a #GdkDevice.
368 * @window: a #GdkWindow.
369 * @axes: an array of doubles to store the values of the axes of @device in,
371 * @mask: location to store the modifiers, or %NULL.
373 * Gets the current state of a pointer device relative to @window.
376 gdk_device_get_state (GdkDevice *device,
379 GdkModifierType *mask)
381 g_return_if_fail (GDK_IS_DEVICE (device));
382 g_return_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD);
383 g_return_if_fail (GDK_IS_WINDOW (window));
385 if (GDK_DEVICE_GET_CLASS (device)->get_state)
386 GDK_DEVICE_GET_CLASS (device)->get_state (device, window, axes, mask);
390 * gdk_device_get_history:
391 * @device: a #GdkDevice
392 * @window: the window with respect to which which the event coordinates will be reported
393 * @start: starting timestamp for range of events to return
394 * @stop: ending timestamp for the range of events to return
395 * @events: (array length=n_events) (out) (transfer none): location to store a newly-allocated array of #GdkTimeCoord, or %NULL
396 * @n_events: location to store the length of @events, or %NULL
398 * Obtains the motion history for a pointer device; given a starting and
399 * ending timestamp, return all events in the motion history for
400 * the device in the given range of time. Some windowing systems
401 * do not support motion history, in which case, %FALSE will
402 * be returned. (This is not distinguishable from the case where
403 * motion history is supported and no events were found.)
405 * Return value: %TRUE if the windowing system supports motion history and
406 * at least one event was found.
409 gdk_device_get_history (GdkDevice *device,
413 GdkTimeCoord ***events,
416 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
417 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
418 g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
426 if (GDK_WINDOW_DESTROYED (window))
429 if (!GDK_DEVICE_GET_CLASS (device)->get_history)
432 return GDK_DEVICE_GET_CLASS (device)->get_history (device, window,
438 _gdk_device_allocate_history (GdkDevice *device,
441 GdkTimeCoord **result = g_new (GdkTimeCoord *, n_events);
444 for (i = 0; i < n_events; i++)
445 result[i] = g_malloc (sizeof (GdkTimeCoord) -
446 sizeof (double) * (GDK_MAX_TIMECOORD_AXES - device->axes->len));
451 * gdk_device_free_history:
452 * @events: (inout) (transfer none): an array of #GdkTimeCoord.
453 * @n_events: the length of the array.
455 * Frees an array of #GdkTimeCoord that was returned by gdk_device_get_history().
458 gdk_device_free_history (GdkTimeCoord **events,
463 for (i = 0; i < n_events; i++)
470 * gdk_device_get_name:
471 * @device: a #GdkDevice
473 * Determines the name of the device.
475 * Return value: a name
480 gdk_device_get_name (GdkDevice *device)
482 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
488 * gdk_device_get_has_cursor:
489 * @device: a #GdkDevice
491 * Determines whether the pointer follows device motion.
493 * Return value: %TRUE if the pointer follows device motion
498 gdk_device_get_has_cursor (GdkDevice *device)
500 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
501 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
503 return device->has_cursor;
507 * gdk_device_get_source:
508 * @device: a #GdkDevice
510 * Determines the type of the device.
512 * Return value: a #GdkInputSource
517 gdk_device_get_source (GdkDevice *device)
519 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
521 return device->source;
525 * gdk_device_set_source:
526 * @device: a #GdkDevice.
527 * @source: the source type.
529 * Sets the source type for an input device.
532 gdk_device_set_source (GdkDevice *device,
533 GdkInputSource source)
535 g_return_if_fail (GDK_IS_DEVICE (device));
537 device->source = source;
538 g_object_notify (G_OBJECT (device), "input-source");
542 * gdk_device_get_mode:
543 * @device: a #GdkDevice
545 * Determines the mode of the device.
547 * Return value: a #GdkInputSource
552 gdk_device_get_mode (GdkDevice *device)
554 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
560 * gdk_device_set_mode:
561 * @device: a #GdkDevice.
562 * @mode: the input mode.
564 * Sets a the mode of an input device. The mode controls if the
565 * device is active and whether the device's range is mapped to the
566 * entire screen or to a single window.
568 * Returns: %TRUE if the mode was successfully changed.
571 gdk_device_set_mode (GdkDevice *device,
574 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
576 if (device->mode == mode)
579 if (mode == GDK_MODE_DISABLED &&
580 gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER)
584 g_object_notify (G_OBJECT (device), "input-mode");
590 * gdk_device_get_n_keys:
591 * @device: a #GdkDevice
593 * Returns the number of keys the device currently has.
595 * Returns: the number of keys.
600 gdk_device_get_n_keys (GdkDevice *device)
602 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
604 return device->num_keys;
608 * gdk_device_get_key:
609 * @device: a #GdkDevice.
610 * @index_: the index of the macro button to get.
611 * @keyval: return value for the keyval.
612 * @modifiers: return value for modifiers.
614 * If @index_ has a valid keyval, this function will return %TRUE
615 * and fill in @keyval and @modifiers with the keyval settings.
617 * Returns: %TRUE if keyval is set for @index.
622 gdk_device_get_key (GdkDevice *device,
625 GdkModifierType *modifiers)
627 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
628 g_return_val_if_fail (index_ < device->num_keys, FALSE);
630 if (!device->keys[index_].keyval &&
631 !device->keys[index_].modifiers)
635 *keyval = device->keys[index_].keyval;
638 *modifiers = device->keys[index_].modifiers;
644 * gdk_device_set_key:
645 * @device: a #GdkDevice
646 * @index_: the index of the macro button to set
647 * @keyval: the keyval to generate
648 * @modifiers: the modifiers to set
650 * Specifies the X key event to generate when a macro button of a device
654 gdk_device_set_key (GdkDevice *device,
657 GdkModifierType modifiers)
659 g_return_if_fail (GDK_IS_DEVICE (device));
660 g_return_if_fail (index_ < device->num_keys);
662 device->keys[index_].keyval = keyval;
663 device->keys[index_].modifiers = modifiers;
667 * gdk_device_get_axis_use:
668 * @device: a pointer #GdkDevice.
669 * @index_: the index of the axis.
671 * Returns the axis use for @index_.
673 * Returns: a #GdkAxisUse specifying how the axis is used.
678 gdk_device_get_axis_use (GdkDevice *device,
683 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_AXIS_IGNORE);
684 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, GDK_AXIS_IGNORE);
685 g_return_val_if_fail (index_ < device->axes->len, GDK_AXIS_IGNORE);
687 info = &g_array_index (device->axes, GdkAxisInfo, index_);
693 * gdk_device_set_axis_use:
694 * @device: a pointer #GdkDevice
695 * @index_: the index of the axis
696 * @use: specifies how the axis is used
698 * Specifies how an axis of a device is used.
701 gdk_device_set_axis_use (GdkDevice *device,
707 g_return_if_fail (GDK_IS_DEVICE (device));
708 g_return_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD);
709 g_return_if_fail (index_ < device->axes->len);
711 info = &g_array_index (device->axes, GdkAxisInfo, index_);
734 * gdk_device_get_display:
735 * @device: a #GdkDevice
737 * Returns the #GdkDisplay to which @device pertains.
739 * Returns: (transfer none): a #GdkDisplay. This memory is owned
740 * by GTK+, and must not be freed or unreffed.
745 gdk_device_get_display (GdkDevice *device)
747 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
749 return device->display;
753 * gdk_device_get_associated_device:
754 * @device: a #GdkDevice
756 * Returns the associated device to @device, if @device is of type
757 * %GDK_DEVICE_TYPE_MASTER, it will return the paired pointer or
760 * If @device is of type %GDK_DEVICE_TYPE_SLAVE, it will return
761 * the master device to which @device is attached to.
763 * If @device is of type %GDK_DEVICE_TYPE_FLOATING, %NULL will be
764 * returned, as there is no associated device.
766 * Returns: (transfer none): The associated device, or %NULL
771 gdk_device_get_associated_device (GdkDevice *device)
773 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
775 return device->associated;
779 _gdk_device_set_device_type (GdkDevice *device,
782 if (device->type != type)
786 g_object_notify (G_OBJECT (device), "type");
791 _gdk_device_set_associated_device (GdkDevice *device,
792 GdkDevice *associated)
794 g_return_if_fail (GDK_IS_DEVICE (device));
795 g_return_if_fail (associated == NULL || GDK_IS_DEVICE (associated));
797 if (device->associated == associated)
800 if (device->associated)
802 g_object_unref (device->associated);
803 device->associated = NULL;
807 device->associated = g_object_ref (associated);
809 if (device->type != GDK_DEVICE_TYPE_MASTER)
811 if (device->associated)
812 _gdk_device_set_device_type (device, GDK_DEVICE_TYPE_SLAVE);
814 _gdk_device_set_device_type (device, GDK_DEVICE_TYPE_FLOATING);
819 * gdk_device_list_slave_devices:
820 * @device: a #GdkDevice
822 * If the device if of type %GDK_DEVICE_TYPE_MASTER, it will return
823 * the list of slave devices attached to it, otherwise it will return
826 * Returns: (transfer container): the list of slave devices, or %NULL. The
827 * list must be freed with g_list_free(), the contents of the list
828 * are owned by GTK+ and should not be freed.
831 gdk_device_list_slave_devices (GdkDevice *device)
833 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
834 g_return_val_if_fail (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER, NULL);
836 return g_list_copy (device->slaves);
840 _gdk_device_add_slave (GdkDevice *device,
843 g_return_if_fail (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER);
844 g_return_if_fail (gdk_device_get_device_type (slave) != GDK_DEVICE_TYPE_MASTER);
846 if (!g_list_find (device->slaves, slave))
847 device->slaves = g_list_prepend (device->slaves, slave);
851 _gdk_device_remove_slave (GdkDevice *device,
856 g_return_if_fail (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER);
857 g_return_if_fail (gdk_device_get_device_type (slave) != GDK_DEVICE_TYPE_MASTER);
859 elem = g_list_find (device->slaves, slave);
864 device->slaves = g_list_delete_link (device->slaves, elem);
868 * gdk_device_get_device_type:
869 * @device: a #GdkDevice
871 * Returns the device type for @device.
873 * Returns: the #GdkDeviceType for @device.
878 gdk_device_get_device_type (GdkDevice *device)
880 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_DEVICE_TYPE_MASTER);
886 * gdk_device_get_n_axes:
887 * @device: a pointer #GdkDevice
889 * Returns the number of axes the device currently has.
891 * Returns: the number of axes.
896 gdk_device_get_n_axes (GdkDevice *device)
898 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
899 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, 0);
901 return device->axes->len;
905 * gdk_device_list_axes:
906 * @device: a pointer #GdkDevice
908 * Returns a #GList of #GdkAtom<!-- -->s, containing the labels for
909 * the axes that @device currently has.
911 * Returns: (transfer container) (element-type GdkAtom):
912 * A #GList of #GdkAtom<!-- -->s, free with g_list_free().
917 gdk_device_list_axes (GdkDevice *device)
922 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
923 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, NULL);
925 for (i = 0; i < device->axes->len; i++)
927 GdkAxisInfo axis_info;
929 axis_info = g_array_index (device->axes, GdkAxisInfo, i);
930 axes = g_list_prepend (axes, GDK_ATOM_TO_POINTER (axis_info.label));
933 return g_list_reverse (axes);
937 * gdk_device_get_axis_value:
938 * @device: a pointer #GdkDevice.
939 * @axes: pointer to an array of axes
940 * @axis_label: #GdkAtom with the axis label.
941 * @value: location to store the found value.
943 * Interprets an array of double as axis values for a given device,
944 * and locates the value in the array for a given axis label, as returned
945 * by gdk_device_list_axes()
947 * Returns: %TRUE if the given axis use was found, otherwise %FALSE.
952 gdk_device_get_axis_value (GdkDevice *device,
959 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
960 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
965 for (i = 0; i < device->axes->len; i++)
967 GdkAxisInfo axis_info;
969 axis_info = g_array_index (device->axes, GdkAxisInfo, i);
971 if (axis_info.label != axis_label)
984 * gdk_device_get_axis:
985 * @device: a #GdkDevice
986 * @axes: pointer to an array of axes
987 * @use: the use to look for
988 * @value: location to store the found value.
990 * Interprets an array of double as axis values for a given device,
991 * and locates the value in the array for a given axis use.
993 * Return value: %TRUE if the given axis use was found, otherwise %FALSE
996 gdk_device_get_axis (GdkDevice *device,
1003 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
1004 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
1009 g_return_val_if_fail (device->axes != NULL, FALSE);
1011 for (i = 0; i < device->axes->len; i++)
1013 GdkAxisInfo axis_info;
1015 axis_info = g_array_index (device->axes, GdkAxisInfo, i);
1017 if (axis_info.use != use)
1030 get_native_grab_event_mask (GdkEventMask grab_mask)
1032 /* Similar to the above but for pointer events only */
1034 GDK_POINTER_MOTION_MASK |
1035 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1036 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1039 ~(GDK_POINTER_MOTION_HINT_MASK |
1040 GDK_BUTTON_MOTION_MASK |
1041 GDK_BUTTON1_MOTION_MASK |
1042 GDK_BUTTON2_MOTION_MASK |
1043 GDK_BUTTON3_MOTION_MASK));
1048 * @device: a #GdkDevice
1049 * @window: the #GdkWindow which will own the grab (the grab window)
1050 * @grab_ownership: specifies the grab ownership.
1051 * @owner_events: if %FALSE then all device events are reported with respect to
1052 * @window and are only reported if selected by @event_mask. If
1053 * %TRUE then pointer events for this application are reported
1054 * as normal, but pointer events outside this application are
1055 * reported with respect to @window and only if selected by
1056 * @event_mask. In either mode, unreported events are discarded.
1057 * @event_mask: specifies the event mask, which is used in accordance with
1059 * @cursor: the cursor to display while the grab is active if the device is
1060 * a pointer. If this is %NULL then the normal cursors are used for
1061 * @window and its descendants, and the cursor for @window is used
1063 * @time_: the timestamp of the event which led to this pointer grab. This
1064 * usually comes from the #GdkEvent struct, though %GDK_CURRENT_TIME
1065 * can be used if the time isn't known.
1067 * Grabs the device so that all events coming from this device are passed to
1068 * this application until the device is ungrabbed with gdk_device_ungrab(),
1069 * or the window becomes unviewable. This overrides any previous grab on the device
1072 * Device grabs are used for operations which need complete control over the
1073 * given device events (either pointer or keyboard). For example in GTK+ this
1074 * is used for Drag and Drop operations, popup menus and such.
1076 * Note that if the event mask of an X window has selected both button press
1077 * and button release events, then a button press event will cause an automatic
1078 * pointer grab until the button is released. X does this automatically since
1079 * most applications expect to receive button press and release events in pairs.
1080 * It is equivalent to a pointer grab on the window with @owner_events set to
1083 * If you set up anything at the time you take the grab that needs to be
1084 * cleaned up when the grab ends, you should handle the #GdkEventGrabBroken
1085 * events that are emitted when the grab ends unvoluntarily.
1087 * Returns: %GDK_GRAB_SUCCESS if the grab was successful.
1092 gdk_device_grab (GdkDevice *device,
1094 GdkGrabOwnership grab_ownership,
1095 gboolean owner_events,
1096 GdkEventMask event_mask,
1103 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_GRAB_SUCCESS);
1104 g_return_val_if_fail (GDK_IS_WINDOW (window), GDK_GRAB_SUCCESS);
1106 if (_gdk_native_windows)
1109 native = gdk_window_get_toplevel (window);
1111 while (native->window_type == GDK_WINDOW_OFFSCREEN)
1113 native = gdk_offscreen_window_get_embedder (native);
1115 if (native == NULL ||
1116 (!_gdk_window_has_impl (native) &&
1117 !gdk_window_is_viewable (native)))
1118 return GDK_GRAB_NOT_VIEWABLE;
1120 native = gdk_window_get_toplevel (native);
1123 if (native == NULL || GDK_WINDOW_DESTROYED (native))
1124 return GDK_GRAB_NOT_VIEWABLE;
1126 res = GDK_DEVICE_GET_CLASS (device)->grab (device,
1129 get_native_grab_event_mask (event_mask),
1134 if (res == GDK_GRAB_SUCCESS)
1136 GdkDisplay *display;
1139 display = gdk_window_get_display (window);
1140 serial = _gdk_windowing_window_get_next_serial (display);
1142 _gdk_display_add_device_grab (display,
1158 * gdk_device_ungrab:
1159 * @device: a #GdkDevice
1160 * @time_: a timestap (e.g. %GDK_CURRENT_TIME).
1162 * Release any grab on @device.
1167 gdk_device_ungrab (GdkDevice *device,
1170 g_return_if_fail (GDK_IS_DEVICE (device));
1172 GDK_DEVICE_GET_CLASS (device)->ungrab (device, time_);
1177 _gdk_device_reset_axes (GdkDevice *device)
1181 for (i = device->axes->len - 1; i >= 0; i--)
1182 g_array_remove_index (device->axes, i);
1184 g_object_notify (G_OBJECT (device), "n-axes");
1188 _gdk_device_add_axis (GdkDevice *device,
1195 GdkAxisInfo axis_info;
1198 axis_info.use = use;
1199 axis_info.label = label_atom;
1200 axis_info.min_value = min_value;
1201 axis_info.max_value = max_value;
1202 axis_info.resolution = resolution;
1208 axis_info.min_axis = 0;
1209 axis_info.max_axis = 0;
1211 case GDK_AXIS_XTILT:
1212 case GDK_AXIS_YTILT:
1213 axis_info.min_axis = -1;
1214 axis_info.max_axis = 1;
1217 axis_info.min_axis = 0;
1218 axis_info.max_axis = 1;
1222 device->axes = g_array_append_val (device->axes, axis_info);
1223 pos = device->axes->len - 1;
1225 g_object_notify (G_OBJECT (device), "n-axes");
1231 _gdk_device_set_keys (GdkDevice *device,
1235 g_free (device->keys);
1237 device->num_keys = num_keys;
1238 device->keys = g_new0 (GdkDeviceKey, num_keys);
1241 static GdkAxisInfo *
1242 find_axis_info (GArray *array,
1248 for (i = 0; i < GDK_AXIS_LAST; i++)
1250 info = &g_array_index (array, GdkAxisInfo, i);
1252 if (info->use == use)
1260 _gdk_device_get_axis_use (GdkDevice *device,
1265 info = g_array_index (device->axes, GdkAxisInfo, index_);
1270 _gdk_device_translate_window_coord (GdkDevice *device,
1274 gdouble *axis_value)
1276 GdkAxisInfo axis_info;
1277 GdkAxisInfo *axis_info_x, *axis_info_y;
1278 gdouble device_width, device_height;
1279 gdouble x_offset, y_offset;
1280 gdouble x_scale, y_scale;
1281 gdouble x_min, y_min;
1282 gdouble x_resolution, y_resolution;
1283 gdouble device_aspect;
1284 gint window_width, window_height;
1286 if (index_ >= device->axes->len)
1289 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1291 if (axis_info.use != GDK_AXIS_X &&
1292 axis_info.use != GDK_AXIS_Y)
1295 if (axis_info.use == GDK_AXIS_X)
1297 axis_info_x = &axis_info;
1298 axis_info_y = find_axis_info (device->axes, GDK_AXIS_Y);
1302 axis_info_x = find_axis_info (device->axes, GDK_AXIS_X);
1303 axis_info_y = &axis_info;
1306 device_width = axis_info_x->max_value - axis_info_x->min_value;
1307 device_height = axis_info_y->max_value - axis_info_y->min_value;
1309 if (device_width > 0)
1310 x_min = axis_info_x->min_value;
1313 device_width = gdk_screen_get_width (gdk_window_get_screen (window));
1317 if (device_height > 0)
1318 y_min = axis_info_y->min_value;
1321 device_height = gdk_screen_get_height (gdk_window_get_screen (window));
1325 window_width = gdk_window_get_width (window);
1326 window_height = gdk_window_get_height (window);
1328 x_resolution = axis_info_x->resolution;
1329 y_resolution = axis_info_y->resolution;
1332 * Some drivers incorrectly report the resolution of the device
1333 * as zero (in partiular linuxwacom < 0.5.3 with usb tablets).
1334 * This causes the device_aspect to become NaN and totally
1335 * breaks windowed mode. If this is the case, the best we can
1336 * do is to assume the resolution is non-zero is equal in both
1337 * directions (which is true for many devices). The absolute
1338 * value of the resolution doesn't matter since we only use the
1341 if (x_resolution == 0 || y_resolution == 0)
1347 device_aspect = (device_height * y_resolution) /
1348 (device_width * x_resolution);
1350 if (device_aspect * window_width >= window_height)
1352 /* device taller than window */
1353 x_scale = window_width / device_width;
1354 y_scale = (x_scale * x_resolution) / y_resolution;
1357 y_offset = - (device_height * y_scale - window_height) / 2;
1361 /* window taller than device */
1362 y_scale = window_height / device_height;
1363 x_scale = (y_scale * y_resolution) / x_resolution;
1366 x_offset = - (device_width * x_scale - window_width) / 2;
1371 if (axis_info.use == GDK_AXIS_X)
1372 *axis_value = x_offset + x_scale * (value - x_min);
1374 *axis_value = y_offset + y_scale * (value - y_min);
1381 _gdk_device_translate_screen_coord (GdkDevice *device,
1387 gdouble *axis_value)
1389 GdkAxisInfo axis_info;
1390 gdouble axis_width, scale, offset;
1392 if (device->mode != GDK_MODE_SCREEN)
1395 if (index_ >= device->axes->len)
1398 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1400 if (axis_info.use != GDK_AXIS_X &&
1401 axis_info.use != GDK_AXIS_Y)
1404 axis_width = axis_info.max_value - axis_info.min_value;
1406 if (axis_info.use == GDK_AXIS_X)
1409 scale = gdk_screen_get_width (gdk_window_get_screen (window)) / axis_width;
1413 offset = - window_root_x - window->abs_x;
1418 scale = gdk_screen_get_height (gdk_window_get_screen (window)) / axis_width;
1422 offset = - window_root_y - window->abs_y;
1426 *axis_value = offset + scale * (value - axis_info.min_value);
1432 _gdk_device_translate_axis (GdkDevice *device,
1435 gdouble *axis_value)
1437 GdkAxisInfo axis_info;
1438 gdouble axis_width, out;
1440 if (index_ >= device->axes->len)
1443 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1445 if (axis_info.use == GDK_AXIS_X ||
1446 axis_info.use == GDK_AXIS_Y)
1449 axis_width = axis_info.max_value - axis_info.min_value;
1450 out = (axis_info.max_axis * (value - axis_info.min_value) +
1451 axis_info.min_axis * (axis_info.max_value - value)) / axis_width;