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.
21 #include "gdkdevice.h"
22 #include "gdkdeviceprivate.h"
24 #include "gdkinternals.h"
27 typedef struct _GdkAxisInfo GdkAxisInfo;
42 struct _GdkDevicePrivate
44 GdkDeviceManager *device_manager;
46 GdkDevice *associated;
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 to 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 to 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 to 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 to 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 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));
225 g_type_class_add_private (object_class, sizeof (GdkDevicePrivate));
229 gdk_device_init (GdkDevice *device)
231 GdkDevicePrivate *priv;
233 device->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (device,
237 priv->axes = g_array_new (FALSE, TRUE, sizeof (GdkAxisInfo));
241 gdk_device_dispose (GObject *object)
243 GdkDevicePrivate *priv;
246 device = GDK_DEVICE (object);
249 if (priv->associated)
251 _gdk_device_set_associated_device (priv->associated, NULL);
252 g_object_unref (priv->associated);
253 priv->associated = NULL;
258 g_array_free (priv->axes, TRUE);
262 g_free (device->name);
263 g_free (device->keys);
264 g_free (device->axes);
270 G_OBJECT_CLASS (gdk_device_parent_class)->dispose (object);
274 gdk_device_set_property (GObject *object,
279 GdkDevice *device = GDK_DEVICE (object);
280 GdkDevicePrivate *priv = device->priv;
285 priv->display = g_value_get_object (value);
287 case PROP_DEVICE_MANAGER:
288 priv->device_manager = g_value_get_object (value);
292 g_free (device->name);
294 device->name = g_value_dup_string (value);
297 priv->type = g_value_get_enum (value);
299 case PROP_INPUT_SOURCE:
300 device->source = g_value_get_enum (value);
302 case PROP_INPUT_MODE:
303 gdk_device_set_mode (device, g_value_get_enum (value));
305 case PROP_HAS_CURSOR:
306 device->has_cursor = g_value_get_boolean (value);
309 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
315 gdk_device_get_property (GObject *object,
320 GdkDevice *device = GDK_DEVICE (object);
321 GdkDevicePrivate *priv = device->priv;
326 g_value_set_object (value, priv->display);
328 case PROP_DEVICE_MANAGER:
329 g_value_set_object (value, priv->device_manager);
331 case PROP_ASSOCIATED_DEVICE:
332 g_value_set_object (value, priv->associated);
335 g_value_set_string (value,
339 g_value_set_enum (value, priv->type);
341 case PROP_INPUT_SOURCE:
342 g_value_set_enum (value, device->source);
344 case PROP_INPUT_MODE:
345 g_value_set_enum (value, device->mode);
347 case PROP_HAS_CURSOR:
348 g_value_set_boolean (value,
352 g_value_set_uint (value, priv->axes->len);
355 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
361 * gdk_device_get_state:
362 * @device: a #GdkDevice.
363 * @window: a #GdkWindow.
364 * @axes: an array of doubles to store the values of the axes of @device in,
366 * @mask: location to store the modifiers, or %NULL.
368 * Gets the current state of a device relative to @window.
371 gdk_device_get_state (GdkDevice *device,
374 GdkModifierType *mask)
376 g_return_if_fail (GDK_IS_DEVICE (device));
377 g_return_if_fail (GDK_IS_WINDOW (window));
379 if (GDK_DEVICE_GET_CLASS (device)->get_state)
380 GDK_DEVICE_GET_CLASS (device)->get_state (device, window, axes, mask);
384 * gdk_device_get_history:
385 * @device: a #GdkDevice
386 * @window: the window with respect to which which the event coordinates will be reported
387 * @start: starting timestamp for range of events to return
388 * @stop: ending timestamp for the range of events to return
389 * @events: (array length=n_events) (out) (transfer none): location to store a newly-allocated array of #GdkTimeCoord, or %NULL
390 * @n_events: location to store the length of @events, or %NULL
392 * Obtains the motion history for a device; given a starting and
393 * ending timestamp, return all events in the motion history for
394 * the device in the given range of time. Some windowing systems
395 * do not support motion history, in which case, %FALSE will
396 * be returned. (This is not distinguishable from the case where
397 * motion history is supported and no events were found.)
399 * Return value: %TRUE if the windowing system supports motion history and
400 * at least one event was found.
403 gdk_device_get_history (GdkDevice *device,
407 GdkTimeCoord ***events,
410 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
411 g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
419 if (GDK_WINDOW_DESTROYED (window))
422 if (!GDK_DEVICE_GET_CLASS (device)->get_history)
425 return GDK_DEVICE_GET_CLASS (device)->get_history (device, window,
431 _gdk_device_allocate_history (GdkDevice *device,
434 GdkTimeCoord **result = g_new (GdkTimeCoord *, n_events);
437 for (i = 0; i < n_events; i++)
438 result[i] = g_malloc (sizeof (GdkTimeCoord) -
439 sizeof (double) * (GDK_MAX_TIMECOORD_AXES - device->num_axes));
444 * gdk_device_free_history:
445 * @events: (inout) (transfer none): an array of #GdkTimeCoord.
446 * @n_events: the length of the array.
448 * Frees an array of #GdkTimeCoord that was returned by gdk_device_get_history().
451 gdk_device_free_history (GdkTimeCoord **events,
456 for (i = 0; i < n_events; i++)
463 * gdk_device_get_name:
464 * @device: a #GdkDevice
466 * Determines the name of the device.
468 * Return value: a name
473 gdk_device_get_name (GdkDevice *device)
475 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
481 * gdk_device_get_has_cursor:
482 * @device: a #GdkDevice
484 * Determines whether the pointer follows device motion.
486 * Return value: %TRUE if the pointer follows device motion
491 gdk_device_get_has_cursor (GdkDevice *device)
493 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
495 return device->has_cursor;
499 * gdk_device_get_source:
500 * @device: a #GdkDevice
502 * Determines the type of the device.
504 * Return value: a #GdkInputSource
509 gdk_device_get_source (GdkDevice *device)
511 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
513 return device->source;
517 * gdk_device_set_source:
518 * @device: a #GdkDevice.
519 * @source: the source type.
521 * Sets the source type for an input device.
524 gdk_device_set_source (GdkDevice *device,
525 GdkInputSource source)
527 g_return_if_fail (GDK_IS_DEVICE (device));
529 device->source = source;
533 * gdk_device_get_mode:
534 * @device: a #GdkDevice
536 * Determines the mode of the device.
538 * Return value: a #GdkInputSource
543 gdk_device_get_mode (GdkDevice *device)
545 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
551 * gdk_device_set_mode:
552 * @device: a #GdkDevice.
553 * @mode: the input mode.
555 * Sets a the mode of an input device. The mode controls if the
556 * device is active and whether the device's range is mapped to the
557 * entire screen or to a single window.
559 * Returns: %TRUE if the mode was successfully changed.
562 gdk_device_set_mode (GdkDevice *device,
565 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
567 if (device->mode == mode)
570 if (mode == GDK_MODE_DISABLED &&
571 gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER)
574 /* FIXME: setting has_cursor when mode is window? */
577 g_object_notify (G_OBJECT (device), "input-mode");
579 if (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER)
580 _gdk_input_check_extension_events (device);
586 * gdk_device_get_key:
587 * @device: a #GdkDevice.
588 * @index_: the index of the macro button to get.
589 * @keyval: return value for the keyval.
590 * @modifiers: return value for modifiers.
592 * If @index_ has a valid keyval, this function will return %TRUE
593 * and fill in @keyval and @modifiers with the keyval settings.
595 * Returns: %TRUE if keyval is set for @index.
600 gdk_device_get_key (GdkDevice *device,
603 GdkModifierType *modifiers)
605 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
606 g_return_val_if_fail (index_ < device->num_keys, FALSE);
608 if (!device->keys[index_].keyval &&
609 !device->keys[index_].modifiers)
613 *keyval = device->keys[index_].keyval;
616 *modifiers = device->keys[index_].modifiers;
622 * gdk_device_set_key:
623 * @device: a #GdkDevice
624 * @index_: the index of the macro button to set
625 * @keyval: the keyval to generate
626 * @modifiers: the modifiers to set
628 * Specifies the X key event to generate when a macro button of a device
632 gdk_device_set_key (GdkDevice *device,
635 GdkModifierType modifiers)
637 g_return_if_fail (GDK_IS_DEVICE (device));
638 g_return_if_fail (index_ < device->num_keys);
640 device->keys[index_].keyval = keyval;
641 device->keys[index_].modifiers = modifiers;
645 * gdk_device_get_axis_use:
646 * @device: a #GdkDevice.
647 * @index_: the index of the axis.
649 * Returns the axis use for @index_.
651 * Returns: a #GdkAxisUse specifying how the axis is used.
656 gdk_device_get_axis_use (GdkDevice *device,
659 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_AXIS_IGNORE);
660 g_return_val_if_fail (index_ < device->num_axes, GDK_AXIS_IGNORE);
662 return device->axes[index_].use;
666 * gdk_device_set_axis_use:
667 * @device: a #GdkDevice
668 * @index_: the index of the axis
669 * @use: specifies how the axis is used
671 * Specifies how an axis of a device is used.
674 gdk_device_set_axis_use (GdkDevice *device,
678 GdkDevicePrivate *priv;
681 g_return_if_fail (GDK_IS_DEVICE (device));
682 g_return_if_fail (index_ < device->num_axes);
685 info = &g_array_index (priv->axes, GdkAxisInfo, index_);
688 device->axes[index_].use = use;
694 device->axes[index_].min = info->min_axis = 0;
695 device->axes[index_].max = info->max_axis = 0;
699 device->axes[index_].min = info->min_axis = -1;
700 device->axes[index_].max = info->max_axis = 1;
703 device->axes[index_].min = info->min_axis = 0;
704 device->axes[index_].max = info->max_axis = 1;
710 * gdk_device_get_display:
711 * @device: a #GdkDevice
713 * Returns the #GdkDisplay to which @device pertains.
715 * Returns: a #GdkDisplay. This memory is owned by GTK+,
716 * and must not be freed or unreffed.
721 gdk_device_get_display (GdkDevice *device)
723 GdkDevicePrivate *priv;
725 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
729 return priv->display;
733 * gdk_device_get_associated_device:
734 * @device: a #GdkDevice
736 * Returns the associated device to @device, if @device is of type
737 * %GDK_DEVICE_TYPE_MASTER, it will return the paired pointer or
740 * If @device is of type %GDK_DEVICE_TYPE_SLAVE, it will return
741 * the master device to which @device is attached to.
743 * If @device is of type %GDK_DEVICE_TYPE_FLOATING, %NULL will be
744 * returned, as there is no associated device.
746 * Returns: The associated device, or %NULL
751 gdk_device_get_associated_device (GdkDevice *device)
753 GdkDevicePrivate *priv;
755 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
759 return priv->associated;
763 _gdk_device_set_associated_device (GdkDevice *device,
764 GdkDevice *associated)
766 GdkDevicePrivate *priv;
768 g_return_if_fail (GDK_IS_DEVICE (device));
769 g_return_if_fail (GDK_IS_DEVICE (associated));
773 if (priv->associated == associated)
776 if (priv->associated)
778 g_object_unref (priv->associated);
779 priv->associated = NULL;
783 priv->associated = g_object_ref (associated);
787 * gdk_device_get_device_type:
788 * @device: a #GdkDevice
790 * Returns the device type for @device.
792 * Returns: the #GdkDeviceType for @device.
797 gdk_device_get_device_type (GdkDevice *device)
799 GdkDevicePrivate *priv;
801 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_DEVICE_TYPE_MASTER);
809 * gdk_device_get_n_axes:
810 * @device: a #GdkDevice
812 * Returns the number of axes the device currently has.
814 * Returns: the number of axes.
819 gdk_device_get_n_axes (GdkDevice *device)
821 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
823 return device->num_axes;
827 * gdk_device_list_axes:
828 * @device: a #GdkDevice
830 * Returns a #GList of #GdkAtom<!-- -->s, containing the labels for
831 * the axes that @device currently has.
833 * Returns: A #GList of #GdkAtom<!-- -->s, free with g_list_free().
838 gdk_device_list_axes (GdkDevice *device)
840 GdkDevicePrivate *priv;
846 for (i = 0; i < priv->axes->len; i++)
848 GdkAxisInfo axis_info;
850 axis_info = g_array_index (priv->axes, GdkAxisInfo, i);
851 axes = g_list_prepend (axes, GDK_ATOM_TO_POINTER (axis_info.label));
854 return g_list_reverse (axes);
858 * gdk_device_get_axis_value:
859 * @device: a #GdkDevice.
860 * @axes: pointer to an array of axes
861 * @axis_label: #GdkAtom with the axis label.
862 * @value: location to store the found value.
864 * Interprets an array of double as axis values for a given device,
865 * and locates the value in the array for a given axis label, as returned
866 * by gdk_device_list_axes()
868 * Returns: %TRUE if the given axis use was found, otherwise %FALSE.
873 gdk_device_get_axis_value (GdkDevice *device,
878 GdkDevicePrivate *priv;
881 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
888 for (i = 0; i < priv->axes->len; i++)
890 GdkAxisInfo axis_info;
892 axis_info = g_array_index (priv->axes, GdkAxisInfo, i);
894 if (axis_info.label != axis_label)
907 * gdk_device_get_axis:
908 * @device: a #GdkDevice
909 * @axes: pointer to an array of axes
910 * @use: the use to look for
911 * @value: location to store the found value.
913 * Interprets an array of double as axis values for a given device,
914 * and locates the value in the array for a given axis use.
916 * Return value: %TRUE if the given axis use was found, otherwise %FALSE
919 gdk_device_get_axis (GdkDevice *device,
924 GdkDevicePrivate *priv;
927 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
934 g_return_val_if_fail (priv->axes != NULL, FALSE);
936 for (i = 0; i < priv->axes->len; i++)
938 GdkAxisInfo axis_info;
940 axis_info = g_array_index (priv->axes, GdkAxisInfo, i);
942 if (axis_info.use != use)
955 get_native_grab_event_mask (GdkEventMask grab_mask)
957 /* Similar to the above but for pointer events only */
959 GDK_POINTER_MOTION_MASK |
960 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
961 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
964 ~(GDK_POINTER_MOTION_HINT_MASK |
965 GDK_BUTTON_MOTION_MASK |
966 GDK_BUTTON1_MOTION_MASK |
967 GDK_BUTTON2_MOTION_MASK |
968 GDK_BUTTON3_MOTION_MASK));
973 * @device: a #GdkDevice
974 * @window: the #GdkWindow which will own the grab (the grab window)
975 * @grab_ownership: specifies the grab ownership.
976 * @owner_events: if %FALSE then all device events are reported with respect to
977 * @window and are only reported if selected by @event_mask. If
978 * %TRUE then pointer events for this application are reported
979 * as normal, but pointer events outside this application are
980 * reported with respect to @window and only if selected by
981 * @event_mask. In either mode, unreported events are discarded.
982 * @event_mask: specifies the event mask, which is used in accordance with
984 * @cursor: the cursor to display while the grab is active if the device is
985 * a pointer. If this is %NULL then the normal cursors are used for
986 * @window and its descendants, and the cursor for @window is used
988 * @time_: the timestamp of the event which led to this pointer grab. This
989 * usually comes from the #GdkEvent struct, though %GDK_CURRENT_TIME
990 * can be used if the time isn't known.
992 * Grabs the device so that all events coming from this device are passed to
993 * this application until the device is ungrabbed with gdk_device_ungrab(),
994 * or the window becomes unviewable. This overrides any previous grab on the device
997 * Device grabs are used for operations which need complete control over the
998 * given device events (either pointer or keyboard). For example in GTK+ this
999 * is used for Drag and Drop operations, popup menus and such.
1001 * Note that if the event mask of an X window has selected both button press
1002 * and button release events, then a button press event will cause an automatic
1003 * pointer grab until the button is released. X does this automatically since
1004 * most applications expect to receive button press and release events in pairs.
1005 * It is equivalent to a pointer grab on the window with @owner_events set to
1008 * If you set up anything at the time you take the grab that needs to be
1009 * cleaned up when the grab ends, you should handle the #GdkEventGrabBroken
1010 * events that are emitted when the grab ends unvoluntarily.
1012 * Returns: %GDK_GRAB_SUCCESS if the grab was successful.
1017 gdk_device_grab (GdkDevice *device,
1019 GdkGrabOwnership grab_ownership,
1020 gboolean owner_events,
1021 GdkEventMask event_mask,
1028 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
1029 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
1031 if (_gdk_native_windows)
1034 native = gdk_window_get_toplevel (window);
1036 while (((GdkWindowObject *) native)->window_type == GDK_WINDOW_OFFSCREEN)
1038 native = gdk_offscreen_window_get_embedder (native);
1040 if (native == NULL ||
1041 (!_gdk_window_has_impl (native) &&
1042 !gdk_window_is_viewable (native)))
1043 return GDK_GRAB_NOT_VIEWABLE;
1045 native = gdk_window_get_toplevel (native);
1048 res = _gdk_windowing_device_grab (device,
1052 get_native_grab_event_mask (event_mask),
1057 if (res == GDK_GRAB_SUCCESS)
1059 GdkDisplay *display;
1062 display = gdk_drawable_get_display (window);
1063 serial = _gdk_windowing_window_get_next_serial (display);
1065 _gdk_display_add_device_grab (display,
1082 _gdk_device_reset_axes (GdkDevice *device)
1084 GdkDevicePrivate *priv;
1087 priv = device->priv;
1089 for (i = priv->axes->len - 1; i >= 0; i--)
1090 g_array_remove_index (priv->axes, i);
1092 g_object_notify (G_OBJECT (device), "n-axes");
1094 /* This is done for backwards compatibility */
1095 g_free (device->axes);
1096 device->axes = NULL;
1100 _gdk_device_add_axis (GdkDevice *device,
1107 GdkDevicePrivate *priv;
1108 GdkAxisInfo axis_info;
1111 priv = device->priv;
1113 axis_info.use = use;
1114 axis_info.label = label_atom;
1115 axis_info.min_value = min_value;
1116 axis_info.max_value = max_value;
1117 axis_info.resolution = resolution;
1123 axis_info.min_axis = 0;
1124 axis_info.max_axis = 0;
1126 case GDK_AXIS_XTILT:
1127 case GDK_AXIS_YTILT:
1128 axis_info.min_axis = -1;
1129 axis_info.max_axis = 1;
1132 axis_info.min_axis = 0;
1133 axis_info.max_axis = 1;
1137 priv->axes = g_array_append_val (priv->axes, axis_info);
1138 device->num_axes = priv->axes->len;
1139 pos = device->num_axes - 1;
1141 /* This is done for backwards compatibility, since the public
1142 * struct doesn't actually store the device data.
1144 device->axes = g_realloc (device->axes, sizeof (GdkDeviceAxis) * priv->axes->len);
1145 device->axes[pos].use = axis_info.use;
1146 device->axes[pos].min = axis_info.min_axis;
1147 device->axes[pos].max = axis_info.max_axis;
1149 g_object_notify (G_OBJECT (device), "n-axes");
1155 _gdk_device_set_keys (GdkDevice *device,
1159 g_free (device->keys);
1161 device->num_keys = num_keys;
1162 device->keys = g_new0 (GdkDeviceKey, num_keys);
1165 static GdkAxisInfo *
1166 find_axis_info (GArray *array,
1172 for (i = 0; i < GDK_AXIS_LAST; i++)
1174 info = &g_array_index (array, GdkAxisInfo, i);
1176 if (info->use == use)
1184 _gdk_device_get_axis_use (GdkDevice *device,
1187 GdkDevicePrivate *priv;
1190 priv = device->priv;
1192 info = g_array_index (priv->axes, GdkAxisInfo, index_);
1197 _gdk_device_translate_window_coord (GdkDevice *device,
1201 gdouble *axis_value)
1203 GdkDevicePrivate *priv;
1204 GdkAxisInfo axis_info;
1205 GdkAxisInfo *axis_info_x, *axis_info_y;
1206 gdouble device_width, device_height;
1207 gdouble x_offset, y_offset;
1208 gdouble x_scale, y_scale;
1209 gdouble x_min, y_min;
1210 gdouble x_resolution, y_resolution;
1211 gdouble device_aspect;
1212 gint window_width, window_height;
1213 GdkWindowObject *window_private;
1215 priv = device->priv;
1217 if (index_ >= priv->axes->len)
1220 axis_info = g_array_index (priv->axes, GdkAxisInfo, index_);
1222 if (axis_info.use != GDK_AXIS_X &&
1223 axis_info.use != GDK_AXIS_Y)
1226 if (axis_info.use == GDK_AXIS_X)
1228 axis_info_x = &axis_info;
1229 axis_info_y = find_axis_info (priv->axes, GDK_AXIS_Y);
1233 axis_info_x = find_axis_info (priv->axes, GDK_AXIS_X);
1234 axis_info_y = &axis_info;
1237 device_width = axis_info_x->max_value - axis_info_x->min_value;
1238 device_height = axis_info_y->max_value - axis_info_y->min_value;
1240 if (device_width > 0)
1241 x_min = axis_info_x->min_value;
1244 device_width = gdk_screen_get_width (gdk_drawable_get_screen (window));
1248 if (device_height > 0)
1249 y_min = axis_info_y->min_value;
1252 device_height = gdk_screen_get_height (gdk_drawable_get_screen (window));
1256 window_private = (GdkWindowObject *) window;
1257 gdk_drawable_get_size (window, &window_width, &window_height);
1259 x_resolution = axis_info_x->resolution;
1260 y_resolution = axis_info_y->resolution;
1263 * Some drivers incorrectly report the resolution of the device
1264 * as zero (in partiular linuxwacom < 0.5.3 with usb tablets).
1265 * This causes the device_aspect to become NaN and totally
1266 * breaks windowed mode. If this is the case, the best we can
1267 * do is to assume the resolution is non-zero is equal in both
1268 * directions (which is true for many devices). The absolute
1269 * value of the resolution doesn't matter since we only use the
1272 if (x_resolution == 0 || y_resolution == 0)
1278 device_aspect = (device_height * y_resolution) /
1279 (device_width * x_resolution);
1281 if (device_aspect * window_width >= window_height)
1283 /* device taller than window */
1284 x_scale = window_width / device_width;
1285 y_scale = (x_scale * x_resolution) / y_resolution;
1288 y_offset = - (device_height * y_scale - window_height) / 2;
1292 /* window taller than device */
1293 y_scale = window_height / device_height;
1294 x_scale = (y_scale * y_resolution) / x_resolution;
1297 x_offset = - (device_width * x_scale - window_width) / 2;
1302 if (axis_info.use == GDK_AXIS_X)
1303 *axis_value = x_offset + x_scale * (value - x_min);
1305 *axis_value = y_offset + y_scale * (value - y_min);
1312 _gdk_device_translate_screen_coord (GdkDevice *device,
1318 gdouble *axis_value)
1320 GdkDevicePrivate *priv;
1321 GdkAxisInfo axis_info;
1322 gdouble axis_width, scale, offset;
1323 GdkWindowObject *window_private;
1325 if (device->mode != GDK_MODE_SCREEN)
1328 priv = device->priv;
1330 if (index_ >= priv->axes->len)
1333 axis_info = g_array_index (priv->axes, GdkAxisInfo, index_);
1335 if (axis_info.use != GDK_AXIS_X &&
1336 axis_info.use != GDK_AXIS_Y)
1339 axis_width = axis_info.max_value - axis_info.min_value;
1340 window_private = (GdkWindowObject *) window;
1342 if (axis_info.use == GDK_AXIS_X)
1345 scale = gdk_screen_get_width (gdk_drawable_get_screen (window)) / axis_width;
1349 offset = - window_root_x - window_private->abs_x;
1354 scale = gdk_screen_get_height (gdk_drawable_get_screen (window)) / axis_width;
1358 offset = - window_root_y - window_private->abs_y;
1362 *axis_value = offset + scale * (value - axis_info.min_value);
1368 _gdk_device_translate_axis (GdkDevice *device,
1371 gdouble *axis_value)
1373 GdkDevicePrivate *priv;
1374 GdkAxisInfo axis_info;
1375 gdouble axis_width, out;
1377 priv = device->priv;
1379 if (index_ >= priv->axes->len)
1382 axis_info = g_array_index (priv->axes, GdkAxisInfo, index_);
1384 if (axis_info.use == GDK_AXIS_X ||
1385 axis_info.use == GDK_AXIS_Y)
1388 axis_width = axis_info.max_value - axis_info.min_value;
1389 out = (axis_info.max_axis * (value - axis_info.min_value) +
1390 axis_info.min_axis * (axis_info.max_value - value)) / axis_width;