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"
27 typedef struct _GdkAxisInfo GdkAxisInfo;
47 static guint signals [LAST_SIGNAL] = { 0 };
50 static void gdk_device_dispose (GObject *object);
51 static void gdk_device_set_property (GObject *object,
55 static void gdk_device_get_property (GObject *object,
61 G_DEFINE_ABSTRACT_TYPE (GdkDevice, gdk_device, G_TYPE_OBJECT)
68 PROP_ASSOCIATED_DEVICE,
78 gdk_device_class_init (GdkDeviceClass *klass)
80 GObjectClass *object_class = G_OBJECT_CLASS (klass);
82 object_class->dispose = gdk_device_dispose;
83 object_class->set_property = gdk_device_set_property;
84 object_class->get_property = gdk_device_get_property;
89 * The #GdkDisplay the #GdkDevice pertains to.
93 g_object_class_install_property (object_class,
95 g_param_spec_object ("display",
97 P_("Display which the device belongs to"),
99 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
100 G_PARAM_STATIC_STRINGS));
102 * GdkDevice:device-manager:
104 * The #GdkDeviceManager the #GdkDevice pertains to.
108 g_object_class_install_property (object_class,
110 g_param_spec_object ("device-manager",
111 P_("Device manager"),
112 P_("Device manager which the device belongs to"),
113 GDK_TYPE_DEVICE_MANAGER,
114 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
115 G_PARAM_STATIC_STRINGS));
123 g_object_class_install_property (object_class,
125 g_param_spec_string ("name",
129 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
130 G_PARAM_STATIC_STRINGS));
134 * Device role in the device manager.
138 g_object_class_install_property (object_class,
140 g_param_spec_enum ("type",
142 P_("Device role in the device manager"),
143 GDK_TYPE_DEVICE_TYPE,
144 GDK_DEVICE_TYPE_MASTER,
145 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
146 G_PARAM_STATIC_STRINGS));
148 * GdkDevice:associated-device:
150 * Associated pointer or keyboard with this device, if any. Devices of type #GDK_DEVICE_TYPE_MASTER
151 * always come in keyboard/pointer pairs. Other device types will have a %NULL associated device.
155 g_object_class_install_property (object_class,
156 PROP_ASSOCIATED_DEVICE,
157 g_param_spec_object ("associated-device",
158 P_("Associated device"),
159 P_("Associated pointer or keyboard with this device"),
161 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
163 * GdkDevice:input-source:
165 * Source type for the device.
169 g_object_class_install_property (object_class,
171 g_param_spec_enum ("input-source",
173 P_("Source type for the device"),
174 GDK_TYPE_INPUT_SOURCE,
176 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
177 G_PARAM_STATIC_STRINGS));
179 * GdkDevice:input-mode:
181 * Input mode for the device.
185 g_object_class_install_property (object_class,
187 g_param_spec_enum ("input-mode",
188 P_("Input mode for the device"),
189 P_("Input mode for the device"),
192 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
194 * GdkDevice:has-cursor:
196 * Whether the device is represented by a cursor on the screen. Devices of type
197 * %GDK_DEVICE_TYPE_MASTER will have %TRUE here.
201 g_object_class_install_property (object_class,
203 g_param_spec_boolean ("has-cursor",
204 P_("Whether the device has a cursor"),
205 P_("Whether there is a visible cursor following device motion"),
207 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
208 G_PARAM_STATIC_STRINGS));
212 * Number of axes in the device.
216 g_object_class_install_property (object_class,
218 g_param_spec_uint ("n-axes",
219 P_("Number of axes in the device"),
220 P_("Number of axes in the device"),
222 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
225 * GdkDevice::changed:
226 * @device: the #GdkDevice that changed.
228 * The ::changed signal is emitted either when the #GdkDevice
229 * has changed the number of either axes or keys. For example
230 * In X this will normally happen when the slave device routing
231 * events through the master device changes (for example, user
232 * switches from the USB mouse to a tablet), in that case the
233 * master device will change to reflect the new slave device
237 g_signal_new (g_intern_static_string ("changed"),
238 G_TYPE_FROM_CLASS (object_class),
241 g_cclosure_marshal_VOID__VOID,
246 gdk_device_init (GdkDevice *device)
248 device->axes = g_array_new (FALSE, TRUE, sizeof (GdkAxisInfo));
252 gdk_device_dispose (GObject *object)
254 GdkDevice *device = GDK_DEVICE (object);
256 if (device->type == GDK_DEVICE_TYPE_SLAVE)
257 _gdk_device_remove_slave (device->associated, device);
259 if (device->associated)
261 _gdk_device_set_associated_device (device->associated, NULL);
262 g_object_unref (device->associated);
263 device->associated = NULL;
268 g_array_free (device->axes, TRUE);
272 g_free (device->name);
273 g_free (device->keys);
278 G_OBJECT_CLASS (gdk_device_parent_class)->dispose (object);
282 gdk_device_set_property (GObject *object,
287 GdkDevice *device = GDK_DEVICE (object);
292 device->display = g_value_get_object (value);
294 case PROP_DEVICE_MANAGER:
295 device->manager = g_value_get_object (value);
299 g_free (device->name);
301 device->name = g_value_dup_string (value);
304 device->type = g_value_get_enum (value);
306 case PROP_INPUT_SOURCE:
307 device->source = g_value_get_enum (value);
309 case PROP_INPUT_MODE:
310 gdk_device_set_mode (device, g_value_get_enum (value));
312 case PROP_HAS_CURSOR:
313 device->has_cursor = g_value_get_boolean (value);
316 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
322 gdk_device_get_property (GObject *object,
327 GdkDevice *device = GDK_DEVICE (object);
332 g_value_set_object (value, device->display);
334 case PROP_DEVICE_MANAGER:
335 g_value_set_object (value, device->manager);
337 case PROP_ASSOCIATED_DEVICE:
338 g_value_set_object (value, device->associated);
341 g_value_set_string (value, device->name);
344 g_value_set_enum (value, device->type);
346 case PROP_INPUT_SOURCE:
347 g_value_set_enum (value, device->source);
349 case PROP_INPUT_MODE:
350 g_value_set_enum (value, device->mode);
352 case PROP_HAS_CURSOR:
353 g_value_set_boolean (value, device->has_cursor);
356 g_value_set_uint (value, device->axes->len);
359 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
365 * gdk_device_get_state:
366 * @device: a #GdkDevice.
367 * @window: a #GdkWindow.
368 * @axes: an array of doubles to store the values of the axes of @device in,
370 * @mask: location to store the modifiers, or %NULL.
372 * Gets the current state of a pointer device relative to @window.
375 gdk_device_get_state (GdkDevice *device,
378 GdkModifierType *mask)
380 g_return_if_fail (GDK_IS_DEVICE (device));
381 g_return_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD);
382 g_return_if_fail (GDK_IS_WINDOW (window));
384 if (GDK_DEVICE_GET_CLASS (device)->get_state)
385 GDK_DEVICE_GET_CLASS (device)->get_state (device, window, axes, mask);
389 * gdk_device_get_history:
390 * @device: a #GdkDevice
391 * @window: the window with respect to which which the event coordinates will be reported
392 * @start: starting timestamp for range of events to return
393 * @stop: ending timestamp for the range of events to return
394 * @events: (array length=n_events) (out) (transfer none): location to store a newly-allocated array of #GdkTimeCoord, or %NULL
395 * @n_events: location to store the length of @events, or %NULL
397 * Obtains the motion history for a pointer device; given a starting and
398 * ending timestamp, return all events in the motion history for
399 * the device in the given range of time. Some windowing systems
400 * do not support motion history, in which case, %FALSE will
401 * be returned. (This is not distinguishable from the case where
402 * motion history is supported and no events were found.)
404 * Return value: %TRUE if the windowing system supports motion history and
405 * at least one event was found.
408 gdk_device_get_history (GdkDevice *device,
412 GdkTimeCoord ***events,
415 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
416 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
417 g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
425 if (GDK_WINDOW_DESTROYED (window))
428 if (!GDK_DEVICE_GET_CLASS (device)->get_history)
431 return GDK_DEVICE_GET_CLASS (device)->get_history (device, window,
437 _gdk_device_allocate_history (GdkDevice *device,
440 GdkTimeCoord **result = g_new (GdkTimeCoord *, n_events);
443 for (i = 0; i < n_events; i++)
444 result[i] = g_malloc (sizeof (GdkTimeCoord) -
445 sizeof (double) * (GDK_MAX_TIMECOORD_AXES - device->axes->len));
450 * gdk_device_free_history:
451 * @events: (inout) (transfer none): an array of #GdkTimeCoord.
452 * @n_events: the length of the array.
454 * Frees an array of #GdkTimeCoord that was returned by gdk_device_get_history().
457 gdk_device_free_history (GdkTimeCoord **events,
462 for (i = 0; i < n_events; i++)
469 * gdk_device_get_name:
470 * @device: a #GdkDevice
472 * Determines the name of the device.
474 * Return value: a name
479 gdk_device_get_name (GdkDevice *device)
481 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
487 * gdk_device_get_has_cursor:
488 * @device: a #GdkDevice
490 * Determines whether the pointer follows device motion.
492 * Return value: %TRUE if the pointer follows device motion
497 gdk_device_get_has_cursor (GdkDevice *device)
499 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
500 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
502 return device->has_cursor;
506 * gdk_device_get_source:
507 * @device: a #GdkDevice
509 * Determines the type of the device.
511 * Return value: a #GdkInputSource
516 gdk_device_get_source (GdkDevice *device)
518 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
520 return device->source;
524 * gdk_device_set_source:
525 * @device: a #GdkDevice.
526 * @source: the source type.
528 * Sets the source type for an input device.
531 gdk_device_set_source (GdkDevice *device,
532 GdkInputSource source)
534 g_return_if_fail (GDK_IS_DEVICE (device));
536 device->source = source;
537 g_object_notify (G_OBJECT (device), "input-source");
541 * gdk_device_get_mode:
542 * @device: a #GdkDevice
544 * Determines the mode of the device.
546 * Return value: a #GdkInputSource
551 gdk_device_get_mode (GdkDevice *device)
553 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
559 * gdk_device_set_mode:
560 * @device: a #GdkDevice.
561 * @mode: the input mode.
563 * Sets a the mode of an input device. The mode controls if the
564 * device is active and whether the device's range is mapped to the
565 * entire screen or to a single window.
567 * Returns: %TRUE if the mode was successfully changed.
570 gdk_device_set_mode (GdkDevice *device,
573 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
575 if (device->mode == mode)
578 if (mode == GDK_MODE_DISABLED &&
579 gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER)
583 g_object_notify (G_OBJECT (device), "input-mode");
589 * gdk_device_get_n_keys:
590 * @device: a #GdkDevice
592 * Returns the number of keys the device currently has.
594 * Returns: the number of keys.
599 gdk_device_get_n_keys (GdkDevice *device)
601 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
603 return device->num_keys;
607 * gdk_device_get_key:
608 * @device: a #GdkDevice.
609 * @index_: the index of the macro button to get.
610 * @keyval: return value for the keyval.
611 * @modifiers: return value for modifiers.
613 * If @index_ has a valid keyval, this function will return %TRUE
614 * and fill in @keyval and @modifiers with the keyval settings.
616 * Returns: %TRUE if keyval is set for @index.
621 gdk_device_get_key (GdkDevice *device,
624 GdkModifierType *modifiers)
626 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
627 g_return_val_if_fail (index_ < device->num_keys, FALSE);
629 if (!device->keys[index_].keyval &&
630 !device->keys[index_].modifiers)
634 *keyval = device->keys[index_].keyval;
637 *modifiers = device->keys[index_].modifiers;
643 * gdk_device_set_key:
644 * @device: a #GdkDevice
645 * @index_: the index of the macro button to set
646 * @keyval: the keyval to generate
647 * @modifiers: the modifiers to set
649 * Specifies the X key event to generate when a macro button of a device
653 gdk_device_set_key (GdkDevice *device,
656 GdkModifierType modifiers)
658 g_return_if_fail (GDK_IS_DEVICE (device));
659 g_return_if_fail (index_ < device->num_keys);
661 device->keys[index_].keyval = keyval;
662 device->keys[index_].modifiers = modifiers;
666 * gdk_device_get_axis_use:
667 * @device: a pointer #GdkDevice.
668 * @index_: the index of the axis.
670 * Returns the axis use for @index_.
672 * Returns: a #GdkAxisUse specifying how the axis is used.
677 gdk_device_get_axis_use (GdkDevice *device,
682 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_AXIS_IGNORE);
683 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, GDK_AXIS_IGNORE);
684 g_return_val_if_fail (index_ < device->axes->len, GDK_AXIS_IGNORE);
686 info = &g_array_index (device->axes, GdkAxisInfo, index_);
692 * gdk_device_set_axis_use:
693 * @device: a pointer #GdkDevice
694 * @index_: the index of the axis
695 * @use: specifies how the axis is used
697 * Specifies how an axis of a device is used.
700 gdk_device_set_axis_use (GdkDevice *device,
706 g_return_if_fail (GDK_IS_DEVICE (device));
707 g_return_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD);
708 g_return_if_fail (index_ < device->axes->len);
710 info = &g_array_index (device->axes, GdkAxisInfo, index_);
733 * gdk_device_get_display:
734 * @device: a #GdkDevice
736 * Returns the #GdkDisplay to which @device pertains.
738 * Returns: (transfer none): a #GdkDisplay. This memory is owned
739 * by GTK+, and must not be freed or unreffed.
744 gdk_device_get_display (GdkDevice *device)
746 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
748 return device->display;
752 * gdk_device_get_associated_device:
753 * @device: a #GdkDevice
755 * Returns the associated device to @device, if @device is of type
756 * %GDK_DEVICE_TYPE_MASTER, it will return the paired pointer or
759 * If @device is of type %GDK_DEVICE_TYPE_SLAVE, it will return
760 * the master device to which @device is attached to.
762 * If @device is of type %GDK_DEVICE_TYPE_FLOATING, %NULL will be
763 * returned, as there is no associated device.
765 * Returns: (transfer none): The associated device, or %NULL
770 gdk_device_get_associated_device (GdkDevice *device)
772 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
774 return device->associated;
778 _gdk_device_set_device_type (GdkDevice *device,
781 if (device->type != type)
785 g_object_notify (G_OBJECT (device), "type");
790 _gdk_device_set_associated_device (GdkDevice *device,
791 GdkDevice *associated)
793 g_return_if_fail (GDK_IS_DEVICE (device));
794 g_return_if_fail (associated == NULL || GDK_IS_DEVICE (associated));
796 if (device->associated == associated)
799 if (device->associated)
801 g_object_unref (device->associated);
802 device->associated = NULL;
806 device->associated = g_object_ref (associated);
808 if (device->type != GDK_DEVICE_TYPE_MASTER)
810 if (device->associated)
811 _gdk_device_set_device_type (device, GDK_DEVICE_TYPE_SLAVE);
813 _gdk_device_set_device_type (device, GDK_DEVICE_TYPE_FLOATING);
818 * gdk_device_list_slave_devices:
819 * @device: a #GdkDevice
821 * If the device if of type %GDK_DEVICE_TYPE_MASTER, it will return
822 * the list of slave devices attached to it, otherwise it will return
825 * Returns: (transfer container): the list of slave devices, or %NULL. The
826 * list must be freed with g_list_free(), the contents of the list
827 * are owned by GTK+ and should not be freed.
830 gdk_device_list_slave_devices (GdkDevice *device)
832 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
833 g_return_val_if_fail (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER, NULL);
835 return g_list_copy (device->slaves);
839 _gdk_device_add_slave (GdkDevice *device,
842 g_return_if_fail (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER);
843 g_return_if_fail (gdk_device_get_device_type (slave) != GDK_DEVICE_TYPE_MASTER);
845 if (!g_list_find (device->slaves, slave))
846 device->slaves = g_list_prepend (device->slaves, slave);
850 _gdk_device_remove_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 elem = g_list_find (device->slaves, slave);
863 device->slaves = g_list_delete_link (device->slaves, elem);
867 * gdk_device_get_device_type:
868 * @device: a #GdkDevice
870 * Returns the device type for @device.
872 * Returns: the #GdkDeviceType for @device.
877 gdk_device_get_device_type (GdkDevice *device)
879 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_DEVICE_TYPE_MASTER);
885 * gdk_device_get_n_axes:
886 * @device: a pointer #GdkDevice
888 * Returns the number of axes the device currently has.
890 * Returns: the number of axes.
895 gdk_device_get_n_axes (GdkDevice *device)
897 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
898 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, 0);
900 return device->axes->len;
904 * gdk_device_list_axes:
905 * @device: a pointer #GdkDevice
907 * Returns a #GList of #GdkAtom<!-- -->s, containing the labels for
908 * the axes that @device currently has.
910 * Returns: (transfer container) (element-type GdkAtom):
911 * A #GList of #GdkAtom<!-- -->s, free with g_list_free().
916 gdk_device_list_axes (GdkDevice *device)
921 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
922 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, NULL);
924 for (i = 0; i < device->axes->len; i++)
926 GdkAxisInfo axis_info;
928 axis_info = g_array_index (device->axes, GdkAxisInfo, i);
929 axes = g_list_prepend (axes, GDK_ATOM_TO_POINTER (axis_info.label));
932 return g_list_reverse (axes);
936 * gdk_device_get_axis_value:
937 * @device: a pointer #GdkDevice.
938 * @axes: pointer to an array of axes
939 * @axis_label: #GdkAtom with the axis label.
940 * @value: location to store the found value.
942 * Interprets an array of double as axis values for a given device,
943 * and locates the value in the array for a given axis label, as returned
944 * by gdk_device_list_axes()
946 * Returns: %TRUE if the given axis use was found, otherwise %FALSE.
951 gdk_device_get_axis_value (GdkDevice *device,
958 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
959 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
964 for (i = 0; i < device->axes->len; i++)
966 GdkAxisInfo axis_info;
968 axis_info = g_array_index (device->axes, GdkAxisInfo, i);
970 if (axis_info.label != axis_label)
983 * gdk_device_get_axis:
984 * @device: a #GdkDevice
985 * @axes: pointer to an array of axes
986 * @use: the use to look for
987 * @value: location to store the found value.
989 * Interprets an array of double as axis values for a given device,
990 * and locates the value in the array for a given axis use.
992 * Return value: %TRUE if the given axis use was found, otherwise %FALSE
995 gdk_device_get_axis (GdkDevice *device,
1002 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
1003 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
1008 g_return_val_if_fail (device->axes != NULL, FALSE);
1010 for (i = 0; i < device->axes->len; i++)
1012 GdkAxisInfo axis_info;
1014 axis_info = g_array_index (device->axes, GdkAxisInfo, i);
1016 if (axis_info.use != use)
1029 get_native_grab_event_mask (GdkEventMask grab_mask)
1031 /* Similar to the above but for pointer events only */
1033 GDK_POINTER_MOTION_MASK |
1034 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1035 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1038 ~(GDK_POINTER_MOTION_HINT_MASK |
1039 GDK_BUTTON_MOTION_MASK |
1040 GDK_BUTTON1_MOTION_MASK |
1041 GDK_BUTTON2_MOTION_MASK |
1042 GDK_BUTTON3_MOTION_MASK));
1047 * @device: a #GdkDevice
1048 * @window: the #GdkWindow which will own the grab (the grab window)
1049 * @grab_ownership: specifies the grab ownership.
1050 * @owner_events: if %FALSE then all device events are reported with respect to
1051 * @window and are only reported if selected by @event_mask. If
1052 * %TRUE then pointer events for this application are reported
1053 * as normal, but pointer events outside this application are
1054 * reported with respect to @window and only if selected by
1055 * @event_mask. In either mode, unreported events are discarded.
1056 * @event_mask: specifies the event mask, which is used in accordance with
1058 * @cursor: the cursor to display while the grab is active if the device is
1059 * a pointer. If this is %NULL then the normal cursors are used for
1060 * @window and its descendants, and the cursor for @window is used
1062 * @time_: the timestamp of the event which led to this pointer grab. This
1063 * usually comes from the #GdkEvent struct, though %GDK_CURRENT_TIME
1064 * can be used if the time isn't known.
1066 * Grabs the device so that all events coming from this device are passed to
1067 * this application until the device is ungrabbed with gdk_device_ungrab(),
1068 * or the window becomes unviewable. This overrides any previous grab on the device
1071 * Device grabs are used for operations which need complete control over the
1072 * given device events (either pointer or keyboard). For example in GTK+ this
1073 * is used for Drag and Drop operations, popup menus and such.
1075 * Note that if the event mask of an X window has selected both button press
1076 * and button release events, then a button press event will cause an automatic
1077 * pointer grab until the button is released. X does this automatically since
1078 * most applications expect to receive button press and release events in pairs.
1079 * It is equivalent to a pointer grab on the window with @owner_events set to
1082 * If you set up anything at the time you take the grab that needs to be
1083 * cleaned up when the grab ends, you should handle the #GdkEventGrabBroken
1084 * events that are emitted when the grab ends unvoluntarily.
1086 * Returns: %GDK_GRAB_SUCCESS if the grab was successful.
1091 gdk_device_grab (GdkDevice *device,
1093 GdkGrabOwnership grab_ownership,
1094 gboolean owner_events,
1095 GdkEventMask event_mask,
1102 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_GRAB_SUCCESS);
1103 g_return_val_if_fail (GDK_IS_WINDOW (window), GDK_GRAB_SUCCESS);
1105 if (_gdk_native_windows)
1108 native = gdk_window_get_toplevel (window);
1110 while (native->window_type == GDK_WINDOW_OFFSCREEN)
1112 native = gdk_offscreen_window_get_embedder (native);
1114 if (native == NULL ||
1115 (!_gdk_window_has_impl (native) &&
1116 !gdk_window_is_viewable (native)))
1117 return GDK_GRAB_NOT_VIEWABLE;
1119 native = gdk_window_get_toplevel (native);
1122 if (native == NULL || GDK_WINDOW_DESTROYED (native))
1123 return GDK_GRAB_NOT_VIEWABLE;
1125 res = GDK_DEVICE_GET_CLASS (device)->grab (device,
1128 get_native_grab_event_mask (event_mask),
1133 if (res == GDK_GRAB_SUCCESS)
1135 GdkDisplay *display;
1138 display = gdk_window_get_display (window);
1139 serial = _gdk_windowing_window_get_next_serial (display);
1141 _gdk_display_add_device_grab (display,
1157 * gdk_device_ungrab:
1158 * @device: a #GdkDevice
1159 * @time_: a timestap (e.g. %GDK_CURRENT_TIME).
1161 * Release any grab on @device.
1166 gdk_device_ungrab (GdkDevice *device,
1169 g_return_if_fail (GDK_IS_DEVICE (device));
1171 GDK_DEVICE_GET_CLASS (device)->ungrab (device, time_);
1176 _gdk_device_reset_axes (GdkDevice *device)
1180 for (i = device->axes->len - 1; i >= 0; i--)
1181 g_array_remove_index (device->axes, i);
1183 g_object_notify (G_OBJECT (device), "n-axes");
1187 _gdk_device_add_axis (GdkDevice *device,
1194 GdkAxisInfo axis_info;
1197 axis_info.use = use;
1198 axis_info.label = label_atom;
1199 axis_info.min_value = min_value;
1200 axis_info.max_value = max_value;
1201 axis_info.resolution = resolution;
1207 axis_info.min_axis = 0;
1208 axis_info.max_axis = 0;
1210 case GDK_AXIS_XTILT:
1211 case GDK_AXIS_YTILT:
1212 axis_info.min_axis = -1;
1213 axis_info.max_axis = 1;
1216 axis_info.min_axis = 0;
1217 axis_info.max_axis = 1;
1221 device->axes = g_array_append_val (device->axes, axis_info);
1222 pos = device->axes->len - 1;
1224 g_object_notify (G_OBJECT (device), "n-axes");
1230 _gdk_device_set_keys (GdkDevice *device,
1234 g_free (device->keys);
1236 device->num_keys = num_keys;
1237 device->keys = g_new0 (GdkDeviceKey, num_keys);
1240 static GdkAxisInfo *
1241 find_axis_info (GArray *array,
1247 for (i = 0; i < GDK_AXIS_LAST; i++)
1249 info = &g_array_index (array, GdkAxisInfo, i);
1251 if (info->use == use)
1259 _gdk_device_get_axis_use (GdkDevice *device,
1264 info = g_array_index (device->axes, GdkAxisInfo, index_);
1269 _gdk_device_translate_window_coord (GdkDevice *device,
1273 gdouble *axis_value)
1275 GdkAxisInfo axis_info;
1276 GdkAxisInfo *axis_info_x, *axis_info_y;
1277 gdouble device_width, device_height;
1278 gdouble x_offset, y_offset;
1279 gdouble x_scale, y_scale;
1280 gdouble x_min, y_min;
1281 gdouble x_resolution, y_resolution;
1282 gdouble device_aspect;
1283 gint window_width, window_height;
1285 if (index_ >= device->axes->len)
1288 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1290 if (axis_info.use != GDK_AXIS_X &&
1291 axis_info.use != GDK_AXIS_Y)
1294 if (axis_info.use == GDK_AXIS_X)
1296 axis_info_x = &axis_info;
1297 axis_info_y = find_axis_info (device->axes, GDK_AXIS_Y);
1301 axis_info_x = find_axis_info (device->axes, GDK_AXIS_X);
1302 axis_info_y = &axis_info;
1305 device_width = axis_info_x->max_value - axis_info_x->min_value;
1306 device_height = axis_info_y->max_value - axis_info_y->min_value;
1308 if (device_width > 0)
1309 x_min = axis_info_x->min_value;
1312 device_width = gdk_screen_get_width (gdk_window_get_screen (window));
1316 if (device_height > 0)
1317 y_min = axis_info_y->min_value;
1320 device_height = gdk_screen_get_height (gdk_window_get_screen (window));
1324 window_width = gdk_window_get_width (window);
1325 window_height = gdk_window_get_height (window);
1327 x_resolution = axis_info_x->resolution;
1328 y_resolution = axis_info_y->resolution;
1331 * Some drivers incorrectly report the resolution of the device
1332 * as zero (in partiular linuxwacom < 0.5.3 with usb tablets).
1333 * This causes the device_aspect to become NaN and totally
1334 * breaks windowed mode. If this is the case, the best we can
1335 * do is to assume the resolution is non-zero is equal in both
1336 * directions (which is true for many devices). The absolute
1337 * value of the resolution doesn't matter since we only use the
1340 if (x_resolution == 0 || y_resolution == 0)
1346 device_aspect = (device_height * y_resolution) /
1347 (device_width * x_resolution);
1349 if (device_aspect * window_width >= window_height)
1351 /* device taller than window */
1352 x_scale = window_width / device_width;
1353 y_scale = (x_scale * x_resolution) / y_resolution;
1356 y_offset = - (device_height * y_scale - window_height) / 2;
1360 /* window taller than device */
1361 y_scale = window_height / device_height;
1362 x_scale = (y_scale * y_resolution) / x_resolution;
1365 x_offset = - (device_width * x_scale - window_width) / 2;
1370 if (axis_info.use == GDK_AXIS_X)
1371 *axis_value = x_offset + x_scale * (value - x_min);
1373 *axis_value = y_offset + y_scale * (value - y_min);
1380 _gdk_device_translate_screen_coord (GdkDevice *device,
1386 gdouble *axis_value)
1388 GdkAxisInfo axis_info;
1389 gdouble axis_width, scale, offset;
1391 if (device->mode != GDK_MODE_SCREEN)
1394 if (index_ >= device->axes->len)
1397 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1399 if (axis_info.use != GDK_AXIS_X &&
1400 axis_info.use != GDK_AXIS_Y)
1403 axis_width = axis_info.max_value - axis_info.min_value;
1405 if (axis_info.use == GDK_AXIS_X)
1408 scale = gdk_screen_get_width (gdk_window_get_screen (window)) / axis_width;
1412 offset = - window_root_x - window->abs_x;
1417 scale = gdk_screen_get_height (gdk_window_get_screen (window)) / axis_width;
1421 offset = - window_root_y - window->abs_y;
1425 *axis_value = offset + scale * (value - axis_info.min_value);
1431 _gdk_device_translate_axis (GdkDevice *device,
1434 gdouble *axis_value)
1436 GdkAxisInfo axis_info;
1437 gdouble axis_width, out;
1439 if (index_ >= device->axes->len)
1442 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1444 if (axis_info.use == GDK_AXIS_X ||
1445 axis_info.use == GDK_AXIS_Y)
1448 axis_width = axis_info.max_value - axis_info.min_value;
1449 out = (axis_info.max_axis * (value - axis_info.min_value) +
1450 axis_info.min_axis * (axis_info.max_value - value)) / axis_width;