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. To get the device you can use gtk_get_current_event_device()
1048 * or gdk_event_get_device() if the grab is in reaction to an event. Also, you can use
1049 * gdk_device_manager_get_client_pointer() but only in code that isn't triggered by a
1050 * #GdkEvent and there aren't other means to get a meaningful #GdkDevice to operate on.
1051 * @window: the #GdkWindow which will own the grab (the grab window)
1052 * @grab_ownership: specifies the grab ownership.
1053 * @owner_events: if %FALSE then all device events are reported with respect to
1054 * @window and are only reported if selected by @event_mask. If
1055 * %TRUE then pointer events for this application are reported
1056 * as normal, but pointer events outside this application are
1057 * reported with respect to @window and only if selected by
1058 * @event_mask. In either mode, unreported events are discarded.
1059 * @event_mask: specifies the event mask, which is used in accordance with
1061 * @cursor: the cursor to display while the grab is active if the device is
1062 * a pointer. If this is %NULL then the normal cursors are used for
1063 * @window and its descendants, and the cursor for @window is used
1065 * @time_: the timestamp of the event which led to this pointer grab. This
1066 * usually comes from the #GdkEvent struct, though %GDK_CURRENT_TIME
1067 * can be used if the time isn't known.
1069 * Grabs the device so that all events coming from this device are passed to
1070 * this application until the device is ungrabbed with gdk_device_ungrab(),
1071 * or the window becomes unviewable. This overrides any previous grab on the device
1074 * Device grabs are used for operations which need complete control over the
1075 * given device events (either pointer or keyboard). For example in GTK+ this
1076 * is used for Drag and Drop operations, popup menus and such.
1078 * Note that if the event mask of an X window has selected both button press
1079 * and button release events, then a button press event will cause an automatic
1080 * pointer grab until the button is released. X does this automatically since
1081 * most applications expect to receive button press and release events in pairs.
1082 * It is equivalent to a pointer grab on the window with @owner_events set to
1085 * If you set up anything at the time you take the grab that needs to be
1086 * cleaned up when the grab ends, you should handle the #GdkEventGrabBroken
1087 * events that are emitted when the grab ends unvoluntarily.
1089 * Returns: %GDK_GRAB_SUCCESS if the grab was successful.
1094 gdk_device_grab (GdkDevice *device,
1096 GdkGrabOwnership grab_ownership,
1097 gboolean owner_events,
1098 GdkEventMask event_mask,
1105 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_GRAB_SUCCESS);
1106 g_return_val_if_fail (GDK_IS_WINDOW (window), GDK_GRAB_SUCCESS);
1108 if (_gdk_native_windows)
1111 native = gdk_window_get_toplevel (window);
1113 while (native->window_type == GDK_WINDOW_OFFSCREEN)
1115 native = gdk_offscreen_window_get_embedder (native);
1117 if (native == NULL ||
1118 (!_gdk_window_has_impl (native) &&
1119 !gdk_window_is_viewable (native)))
1120 return GDK_GRAB_NOT_VIEWABLE;
1122 native = gdk_window_get_toplevel (native);
1125 if (native == NULL || GDK_WINDOW_DESTROYED (native))
1126 return GDK_GRAB_NOT_VIEWABLE;
1128 res = GDK_DEVICE_GET_CLASS (device)->grab (device,
1131 get_native_grab_event_mask (event_mask),
1136 if (res == GDK_GRAB_SUCCESS)
1138 GdkDisplay *display;
1141 display = gdk_window_get_display (window);
1142 serial = _gdk_display_get_next_serial (display);
1144 _gdk_display_add_device_grab (display,
1160 * gdk_device_ungrab:
1161 * @device: a #GdkDevice
1162 * @time_: a timestap (e.g. %GDK_CURRENT_TIME).
1164 * Release any grab on @device.
1169 gdk_device_ungrab (GdkDevice *device,
1172 g_return_if_fail (GDK_IS_DEVICE (device));
1174 GDK_DEVICE_GET_CLASS (device)->ungrab (device, time_);
1179 * @device: the device to warp.
1180 * @screen: the screen to warp @device to.
1181 * @x: the X coordinate of the destination.
1182 * @y: the Y coordinate of the destination.
1184 * Warps @device in @display to the point @x,@y on
1185 * the screen @screen, unless the device is confined
1186 * to a window by a grab, in which case it will be moved
1187 * as far as allowed by the grab. Warping the pointer
1188 * creates events as if the user had moved the mouse
1189 * instantaneously to the destination.
1191 * Note that the pointer should normally be under the
1192 * control of the user. This function was added to cover
1193 * some rare use cases like keyboard navigation support
1194 * for the color picker in the #GtkColorSelectionDialog.
1199 gdk_device_warp (GdkDevice *device,
1204 g_return_if_fail (GDK_IS_DEVICE (device));
1205 g_return_if_fail (GDK_IS_SCREEN (screen));
1206 g_return_if_fail (gdk_device_get_display (device) == gdk_screen_get_display (screen));
1208 GDK_DEVICE_GET_CLASS (device)->warp (device, screen, x, y);
1213 _gdk_device_reset_axes (GdkDevice *device)
1217 for (i = device->axes->len - 1; i >= 0; i--)
1218 g_array_remove_index (device->axes, i);
1220 g_object_notify (G_OBJECT (device), "n-axes");
1224 _gdk_device_add_axis (GdkDevice *device,
1231 GdkAxisInfo axis_info;
1234 axis_info.use = use;
1235 axis_info.label = label_atom;
1236 axis_info.min_value = min_value;
1237 axis_info.max_value = max_value;
1238 axis_info.resolution = resolution;
1244 axis_info.min_axis = 0;
1245 axis_info.max_axis = 0;
1247 case GDK_AXIS_XTILT:
1248 case GDK_AXIS_YTILT:
1249 axis_info.min_axis = -1;
1250 axis_info.max_axis = 1;
1253 axis_info.min_axis = 0;
1254 axis_info.max_axis = 1;
1258 device->axes = g_array_append_val (device->axes, axis_info);
1259 pos = device->axes->len - 1;
1261 g_object_notify (G_OBJECT (device), "n-axes");
1267 _gdk_device_set_keys (GdkDevice *device,
1271 g_free (device->keys);
1273 device->num_keys = num_keys;
1274 device->keys = g_new0 (GdkDeviceKey, num_keys);
1277 static GdkAxisInfo *
1278 find_axis_info (GArray *array,
1284 for (i = 0; i < GDK_AXIS_LAST; i++)
1286 info = &g_array_index (array, GdkAxisInfo, i);
1288 if (info->use == use)
1296 _gdk_device_get_axis_use (GdkDevice *device,
1301 info = g_array_index (device->axes, GdkAxisInfo, index_);
1306 _gdk_device_translate_window_coord (GdkDevice *device,
1310 gdouble *axis_value)
1312 GdkAxisInfo axis_info;
1313 GdkAxisInfo *axis_info_x, *axis_info_y;
1314 gdouble device_width, device_height;
1315 gdouble x_offset, y_offset;
1316 gdouble x_scale, y_scale;
1317 gdouble x_min, y_min;
1318 gdouble x_resolution, y_resolution;
1319 gdouble device_aspect;
1320 gint window_width, window_height;
1322 if (index_ >= device->axes->len)
1325 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1327 if (axis_info.use != GDK_AXIS_X &&
1328 axis_info.use != GDK_AXIS_Y)
1331 if (axis_info.use == GDK_AXIS_X)
1333 axis_info_x = &axis_info;
1334 axis_info_y = find_axis_info (device->axes, GDK_AXIS_Y);
1338 axis_info_x = find_axis_info (device->axes, GDK_AXIS_X);
1339 axis_info_y = &axis_info;
1342 device_width = axis_info_x->max_value - axis_info_x->min_value;
1343 device_height = axis_info_y->max_value - axis_info_y->min_value;
1345 if (device_width > 0)
1346 x_min = axis_info_x->min_value;
1349 device_width = gdk_screen_get_width (gdk_window_get_screen (window));
1353 if (device_height > 0)
1354 y_min = axis_info_y->min_value;
1357 device_height = gdk_screen_get_height (gdk_window_get_screen (window));
1361 window_width = gdk_window_get_width (window);
1362 window_height = gdk_window_get_height (window);
1364 x_resolution = axis_info_x->resolution;
1365 y_resolution = axis_info_y->resolution;
1368 * Some drivers incorrectly report the resolution of the device
1369 * as zero (in partiular linuxwacom < 0.5.3 with usb tablets).
1370 * This causes the device_aspect to become NaN and totally
1371 * breaks windowed mode. If this is the case, the best we can
1372 * do is to assume the resolution is non-zero is equal in both
1373 * directions (which is true for many devices). The absolute
1374 * value of the resolution doesn't matter since we only use the
1377 if (x_resolution == 0 || y_resolution == 0)
1383 device_aspect = (device_height * y_resolution) /
1384 (device_width * x_resolution);
1386 if (device_aspect * window_width >= window_height)
1388 /* device taller than window */
1389 x_scale = window_width / device_width;
1390 y_scale = (x_scale * x_resolution) / y_resolution;
1393 y_offset = - (device_height * y_scale - window_height) / 2;
1397 /* window taller than device */
1398 y_scale = window_height / device_height;
1399 x_scale = (y_scale * y_resolution) / x_resolution;
1402 x_offset = - (device_width * x_scale - window_width) / 2;
1407 if (axis_info.use == GDK_AXIS_X)
1408 *axis_value = x_offset + x_scale * (value - x_min);
1410 *axis_value = y_offset + y_scale * (value - y_min);
1417 _gdk_device_translate_screen_coord (GdkDevice *device,
1423 gdouble *axis_value)
1425 GdkAxisInfo axis_info;
1426 gdouble axis_width, scale, offset;
1428 if (device->mode != GDK_MODE_SCREEN)
1431 if (index_ >= device->axes->len)
1434 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1436 if (axis_info.use != GDK_AXIS_X &&
1437 axis_info.use != GDK_AXIS_Y)
1440 axis_width = axis_info.max_value - axis_info.min_value;
1442 if (axis_info.use == GDK_AXIS_X)
1445 scale = gdk_screen_get_width (gdk_window_get_screen (window)) / axis_width;
1449 offset = - window_root_x - window->abs_x;
1454 scale = gdk_screen_get_height (gdk_window_get_screen (window)) / axis_width;
1458 offset = - window_root_y - window->abs_y;
1462 *axis_value = offset + scale * (value - axis_info.min_value);
1468 _gdk_device_translate_axis (GdkDevice *device,
1471 gdouble *axis_value)
1473 GdkAxisInfo axis_info;
1474 gdouble axis_width, out;
1476 if (index_ >= device->axes->len)
1479 axis_info = g_array_index (device->axes, GdkAxisInfo, index_);
1481 if (axis_info.use == GDK_AXIS_X ||
1482 axis_info.use == GDK_AXIS_Y)
1485 axis_width = axis_info.max_value - axis_info.min_value;
1486 out = (axis_info.max_axis * (value - axis_info.min_value) +
1487 axis_info.min_axis * (axis_info.max_value - value)) / axis_width;
1496 _gdk_device_query_state (GdkDevice *device,
1498 GdkWindow **root_window,
1499 GdkWindow **child_window,
1504 GdkModifierType *mask)
1506 return GDK_DEVICE_GET_CLASS (device)->query_state (device,
1518 _gdk_device_window_at_position (GdkDevice *device,
1521 GdkModifierType *mask,
1522 gboolean get_toplevel)
1524 return GDK_DEVICE_GET_CLASS (device)->window_at_position (device,