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 "gdkdeviceprivate.h"
26 #include "gdkinternals.h"
29 typedef struct _GdkAxisInfo GdkAxisInfo;
44 struct _GdkDevicePrivate
46 GdkDeviceManager *device_manager;
48 GdkDevice *associated;
53 static void gdk_device_dispose (GObject *object);
54 static void gdk_device_set_property (GObject *object,
58 static void gdk_device_get_property (GObject *object,
64 G_DEFINE_ABSTRACT_TYPE (GdkDevice, gdk_device, G_TYPE_OBJECT)
71 PROP_ASSOCIATED_DEVICE,
81 gdk_device_class_init (GdkDeviceClass *klass)
83 GObjectClass *object_class = G_OBJECT_CLASS (klass);
85 object_class->dispose = gdk_device_dispose;
86 object_class->set_property = gdk_device_set_property;
87 object_class->get_property = gdk_device_get_property;
92 * The #GdkDisplay the #GdkDevice pertains to.
96 g_object_class_install_property (object_class,
98 g_param_spec_object ("display",
100 P_("Display which the device belongs to"),
102 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
103 G_PARAM_STATIC_STRINGS));
105 * GdkDevice:device-manager:
107 * The #GdkDeviceManager the #GdkDevice pertains to.
111 g_object_class_install_property (object_class,
113 g_param_spec_object ("device-manager",
114 P_("Device manager"),
115 P_("Device manager which the device belongs to"),
116 GDK_TYPE_DEVICE_MANAGER,
117 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
118 G_PARAM_STATIC_STRINGS));
126 g_object_class_install_property (object_class,
128 g_param_spec_string ("name",
132 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
133 G_PARAM_STATIC_STRINGS));
137 * Device role in the device manager.
141 g_object_class_install_property (object_class,
143 g_param_spec_enum ("type",
145 P_("Device role in the device manager"),
146 GDK_TYPE_DEVICE_TYPE,
147 GDK_DEVICE_TYPE_MASTER,
148 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
149 G_PARAM_STATIC_STRINGS));
151 * GdkDevice:associated-device:
153 * Associated pointer or keyboard with this device, if any. Devices of type #GDK_DEVICE_TYPE_MASTER
154 * always come in keyboard/pointer pairs. Other device types will have a %NULL associated device.
158 g_object_class_install_property (object_class,
159 PROP_ASSOCIATED_DEVICE,
160 g_param_spec_object ("associated-device",
161 P_("Associated device"),
162 P_("Associated pointer or keyboard with this device"),
164 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
166 * GdkDevice:input-source:
168 * Source type for the device.
172 g_object_class_install_property (object_class,
174 g_param_spec_enum ("input-source",
176 P_("Source type for the device"),
177 GDK_TYPE_INPUT_SOURCE,
179 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
180 G_PARAM_STATIC_STRINGS));
182 * GdkDevice:input-mode:
184 * Input mode for the device.
188 g_object_class_install_property (object_class,
190 g_param_spec_enum ("input-mode",
191 P_("Input mode for the device"),
192 P_("Input mode for the device"),
195 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
197 * GdkDevice:has-cursor:
199 * Whether the device is represented by a cursor on the screen. Devices of type
200 * %GDK_DEVICE_TYPE_MASTER will have %TRUE here.
204 g_object_class_install_property (object_class,
206 g_param_spec_boolean ("has-cursor",
207 P_("Whether the device has a cursor"),
208 P_("Whether there is a visible cursor following device motion"),
210 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
211 G_PARAM_STATIC_STRINGS));
215 * Number of axes in the device.
219 g_object_class_install_property (object_class,
221 g_param_spec_uint ("n-axes",
222 P_("Number of axes in the device"),
223 P_("Number of axes in the device"),
225 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
227 g_type_class_add_private (object_class, sizeof (GdkDevicePrivate));
231 gdk_device_init (GdkDevice *device)
233 GdkDevicePrivate *priv;
235 device->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (device,
239 priv->axes = g_array_new (FALSE, TRUE, sizeof (GdkAxisInfo));
243 gdk_device_dispose (GObject *object)
245 GdkDevicePrivate *priv;
248 device = GDK_DEVICE (object);
251 if (priv->associated)
253 _gdk_device_set_associated_device (priv->associated, NULL);
254 g_object_unref (priv->associated);
255 priv->associated = NULL;
260 g_array_free (priv->axes, TRUE);
264 g_free (device->name);
265 g_free (device->keys);
266 g_free (device->axes);
272 G_OBJECT_CLASS (gdk_device_parent_class)->dispose (object);
276 gdk_device_set_property (GObject *object,
281 GdkDevice *device = GDK_DEVICE (object);
282 GdkDevicePrivate *priv = device->priv;
287 priv->display = g_value_get_object (value);
289 case PROP_DEVICE_MANAGER:
290 priv->device_manager = g_value_get_object (value);
294 g_free (device->name);
296 device->name = g_value_dup_string (value);
299 priv->type = g_value_get_enum (value);
301 case PROP_INPUT_SOURCE:
302 device->source = g_value_get_enum (value);
304 case PROP_INPUT_MODE:
305 gdk_device_set_mode (device, g_value_get_enum (value));
307 case PROP_HAS_CURSOR:
308 device->has_cursor = g_value_get_boolean (value);
311 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
317 gdk_device_get_property (GObject *object,
322 GdkDevice *device = GDK_DEVICE (object);
323 GdkDevicePrivate *priv = device->priv;
328 g_value_set_object (value, priv->display);
330 case PROP_DEVICE_MANAGER:
331 g_value_set_object (value, priv->device_manager);
333 case PROP_ASSOCIATED_DEVICE:
334 g_value_set_object (value, priv->associated);
337 g_value_set_string (value,
341 g_value_set_enum (value, priv->type);
343 case PROP_INPUT_SOURCE:
344 g_value_set_enum (value, device->source);
346 case PROP_INPUT_MODE:
347 g_value_set_enum (value, device->mode);
349 case PROP_HAS_CURSOR:
350 g_value_set_boolean (value,
354 g_value_set_uint (value, priv->axes->len);
357 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
363 * gdk_device_get_state:
364 * @device: a #GdkDevice.
365 * @window: a #GdkWindow.
366 * @axes: an array of doubles to store the values of the axes of @device in,
368 * @mask: location to store the modifiers, or %NULL.
370 * Gets the current state of a device relative to @window.
373 gdk_device_get_state (GdkDevice *device,
376 GdkModifierType *mask)
378 g_return_if_fail (GDK_IS_DEVICE (device));
379 g_return_if_fail (GDK_IS_WINDOW (window));
381 if (GDK_DEVICE_GET_CLASS (device)->get_state)
382 GDK_DEVICE_GET_CLASS (device)->get_state (device, window, axes, mask);
386 * gdk_device_get_history:
387 * @device: a #GdkDevice
388 * @window: the window with respect to which which the event coordinates will be reported
389 * @start: starting timestamp for range of events to return
390 * @stop: ending timestamp for the range of events to return
391 * @events: (array length=n_events) (out) (transfer none): location to store a newly-allocated array of #GdkTimeCoord, or %NULL
392 * @n_events: location to store the length of @events, or %NULL
394 * Obtains the motion history for a device; given a starting and
395 * ending timestamp, return all events in the motion history for
396 * the device in the given range of time. Some windowing systems
397 * do not support motion history, in which case, %FALSE will
398 * be returned. (This is not distinguishable from the case where
399 * motion history is supported and no events were found.)
401 * Return value: %TRUE if the windowing system supports motion history and
402 * at least one event was found.
405 gdk_device_get_history (GdkDevice *device,
409 GdkTimeCoord ***events,
412 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
413 g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
421 if (GDK_WINDOW_DESTROYED (window))
424 if (!GDK_DEVICE_GET_CLASS (device)->get_history)
427 return GDK_DEVICE_GET_CLASS (device)->get_history (device, window,
433 _gdk_device_allocate_history (GdkDevice *device,
436 GdkTimeCoord **result = g_new (GdkTimeCoord *, n_events);
439 for (i = 0; i < n_events; i++)
440 result[i] = g_malloc (sizeof (GdkTimeCoord) -
441 sizeof (double) * (GDK_MAX_TIMECOORD_AXES - device->num_axes));
446 * gdk_device_free_history:
447 * @events: (inout) (transfer none): an array of #GdkTimeCoord.
448 * @n_events: the length of the array.
450 * Frees an array of #GdkTimeCoord that was returned by gdk_device_get_history().
453 gdk_device_free_history (GdkTimeCoord **events,
458 for (i = 0; i < n_events; i++)
465 * gdk_device_get_name:
466 * @device: a #GdkDevice
468 * Determines the name of the device.
470 * Return value: a name
475 gdk_device_get_name (GdkDevice *device)
477 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
483 * gdk_device_get_has_cursor:
484 * @device: a #GdkDevice
486 * Determines whether the pointer follows device motion.
488 * Return value: %TRUE if the pointer follows device motion
493 gdk_device_get_has_cursor (GdkDevice *device)
495 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
497 return device->has_cursor;
501 * gdk_device_get_source:
502 * @device: a #GdkDevice
504 * Determines the type of the device.
506 * Return value: a #GdkInputSource
511 gdk_device_get_source (GdkDevice *device)
513 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
515 return device->source;
519 * gdk_device_set_source:
520 * @device: a #GdkDevice.
521 * @source: the source type.
523 * Sets the source type for an input device.
526 gdk_device_set_source (GdkDevice *device,
527 GdkInputSource source)
529 g_return_if_fail (GDK_IS_DEVICE (device));
531 device->source = source;
535 * gdk_device_get_mode:
536 * @device: a #GdkDevice
538 * Determines the mode of the device.
540 * Return value: a #GdkInputSource
545 gdk_device_get_mode (GdkDevice *device)
547 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
553 * gdk_device_set_mode:
554 * @device: a #GdkDevice.
555 * @mode: the input mode.
557 * Sets a the mode of an input device. The mode controls if the
558 * device is active and whether the device's range is mapped to the
559 * entire screen or to a single window.
561 * Returns: %TRUE if the mode was successfully changed.
564 gdk_device_set_mode (GdkDevice *device,
567 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
569 if (device->mode == mode)
572 if (mode == GDK_MODE_DISABLED &&
573 gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER)
576 /* FIXME: setting has_cursor when mode is window? */
579 g_object_notify (G_OBJECT (device), "input-mode");
581 if (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER)
582 _gdk_input_check_extension_events (device);
588 * gdk_device_get_key:
589 * @device: a #GdkDevice.
590 * @index_: the index of the macro button to get.
591 * @keyval: return value for the keyval.
592 * @modifiers: return value for modifiers.
594 * If @index_ has a valid keyval, this function will return %TRUE
595 * and fill in @keyval and @modifiers with the keyval settings.
597 * Returns: %TRUE if keyval is set for @index.
602 gdk_device_get_key (GdkDevice *device,
605 GdkModifierType *modifiers)
607 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
608 g_return_val_if_fail (index_ < device->num_keys, FALSE);
610 if (!device->keys[index_].keyval &&
611 !device->keys[index_].modifiers)
615 *keyval = device->keys[index_].keyval;
618 *modifiers = device->keys[index_].modifiers;
624 * gdk_device_set_key:
625 * @device: a #GdkDevice
626 * @index_: the index of the macro button to set
627 * @keyval: the keyval to generate
628 * @modifiers: the modifiers to set
630 * Specifies the X key event to generate when a macro button of a device
634 gdk_device_set_key (GdkDevice *device,
637 GdkModifierType modifiers)
639 g_return_if_fail (GDK_IS_DEVICE (device));
640 g_return_if_fail (index_ < device->num_keys);
642 device->keys[index_].keyval = keyval;
643 device->keys[index_].modifiers = modifiers;
647 * gdk_device_get_axis_use:
648 * @device: a #GdkDevice.
649 * @index_: the index of the axis.
651 * Returns the axis use for @index_.
653 * Returns: a #GdkAxisUse specifying how the axis is used.
658 gdk_device_get_axis_use (GdkDevice *device,
661 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_AXIS_IGNORE);
662 g_return_val_if_fail (index_ < device->num_axes, GDK_AXIS_IGNORE);
664 return device->axes[index_].use;
668 * gdk_device_set_axis_use:
669 * @device: a #GdkDevice
670 * @index_: the index of the axis
671 * @use: specifies how the axis is used
673 * Specifies how an axis of a device is used.
676 gdk_device_set_axis_use (GdkDevice *device,
680 GdkDevicePrivate *priv;
683 g_return_if_fail (GDK_IS_DEVICE (device));
684 g_return_if_fail (index_ < device->num_axes);
687 info = &g_array_index (priv->axes, GdkAxisInfo, index_);
690 device->axes[index_].use = use;
696 device->axes[index_].min = info->min_axis = 0;
697 device->axes[index_].max = info->max_axis = 0;
701 device->axes[index_].min = info->min_axis = -1;
702 device->axes[index_].max = info->max_axis = 1;
705 device->axes[index_].min = info->min_axis = 0;
706 device->axes[index_].max = info->max_axis = 1;
712 * gdk_device_get_display:
713 * @device: a #GdkDevice
715 * Returns the #GdkDisplay to which @device pertains.
717 * Returns: a #GdkDisplay. This memory is owned by GTK+,
718 * and must not be freed or unreffed.
723 gdk_device_get_display (GdkDevice *device)
725 GdkDevicePrivate *priv;
727 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
731 return priv->display;
735 * gdk_device_get_associated_device:
736 * @device: a #GdkDevice
738 * Returns the associated device to @device, if @device is of type
739 * %GDK_DEVICE_TYPE_MASTER, it will return the paired pointer or
742 * If @device is of type %GDK_DEVICE_TYPE_SLAVE, it will return
743 * the master device to which @device is attached to.
745 * If @device is of type %GDK_DEVICE_TYPE_FLOATING, %NULL will be
746 * returned, as there is no associated device.
748 * Returns: The associated device, or %NULL
753 gdk_device_get_associated_device (GdkDevice *device)
755 GdkDevicePrivate *priv;
757 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
761 return priv->associated;
765 _gdk_device_set_associated_device (GdkDevice *device,
766 GdkDevice *associated)
768 GdkDevicePrivate *priv;
770 g_return_if_fail (GDK_IS_DEVICE (device));
771 g_return_if_fail (associated == NULL || GDK_IS_DEVICE (associated));
775 if (priv->associated == associated)
778 if (priv->associated)
780 g_object_unref (priv->associated);
781 priv->associated = NULL;
785 priv->associated = g_object_ref (associated);
789 * gdk_device_get_device_type:
790 * @device: a #GdkDevice
792 * Returns the device type for @device.
794 * Returns: the #GdkDeviceType for @device.
799 gdk_device_get_device_type (GdkDevice *device)
801 GdkDevicePrivate *priv;
803 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_DEVICE_TYPE_MASTER);
811 * gdk_device_get_n_axes:
812 * @device: a #GdkDevice
814 * Returns the number of axes the device currently has.
816 * Returns: the number of axes.
821 gdk_device_get_n_axes (GdkDevice *device)
823 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
825 return device->num_axes;
829 * gdk_device_list_axes:
830 * @device: a #GdkDevice
832 * Returns a #GList of #GdkAtom<!-- -->s, containing the labels for
833 * the axes that @device currently has.
835 * Returns: (transfer container) (element-type GdkAtom):
836 * A #GList of #GdkAtom<!-- -->s, free with g_list_free().
841 gdk_device_list_axes (GdkDevice *device)
843 GdkDevicePrivate *priv;
849 for (i = 0; i < priv->axes->len; i++)
851 GdkAxisInfo axis_info;
853 axis_info = g_array_index (priv->axes, GdkAxisInfo, i);
854 axes = g_list_prepend (axes, GDK_ATOM_TO_POINTER (axis_info.label));
857 return g_list_reverse (axes);
861 * gdk_device_get_axis_value:
862 * @device: a #GdkDevice.
863 * @axes: pointer to an array of axes
864 * @axis_label: #GdkAtom with the axis label.
865 * @value: location to store the found value.
867 * Interprets an array of double as axis values for a given device,
868 * and locates the value in the array for a given axis label, as returned
869 * by gdk_device_list_axes()
871 * Returns: %TRUE if the given axis use was found, otherwise %FALSE.
876 gdk_device_get_axis_value (GdkDevice *device,
881 GdkDevicePrivate *priv;
884 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
891 for (i = 0; i < priv->axes->len; i++)
893 GdkAxisInfo axis_info;
895 axis_info = g_array_index (priv->axes, GdkAxisInfo, i);
897 if (axis_info.label != axis_label)
910 * gdk_device_get_axis:
911 * @device: a #GdkDevice
912 * @axes: pointer to an array of axes
913 * @use: the use to look for
914 * @value: location to store the found value.
916 * Interprets an array of double as axis values for a given device,
917 * and locates the value in the array for a given axis use.
919 * Return value: %TRUE if the given axis use was found, otherwise %FALSE
922 gdk_device_get_axis (GdkDevice *device,
927 GdkDevicePrivate *priv;
930 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
937 g_return_val_if_fail (priv->axes != NULL, FALSE);
939 for (i = 0; i < priv->axes->len; i++)
941 GdkAxisInfo axis_info;
943 axis_info = g_array_index (priv->axes, GdkAxisInfo, i);
945 if (axis_info.use != use)
958 get_native_grab_event_mask (GdkEventMask grab_mask)
960 /* Similar to the above but for pointer events only */
962 GDK_POINTER_MOTION_MASK |
963 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
964 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
967 ~(GDK_POINTER_MOTION_HINT_MASK |
968 GDK_BUTTON_MOTION_MASK |
969 GDK_BUTTON1_MOTION_MASK |
970 GDK_BUTTON2_MOTION_MASK |
971 GDK_BUTTON3_MOTION_MASK));
976 * @device: a #GdkDevice
977 * @window: the #GdkWindow which will own the grab (the grab window)
978 * @grab_ownership: specifies the grab ownership.
979 * @owner_events: if %FALSE then all device events are reported with respect to
980 * @window and are only reported if selected by @event_mask. If
981 * %TRUE then pointer events for this application are reported
982 * as normal, but pointer events outside this application are
983 * reported with respect to @window and only if selected by
984 * @event_mask. In either mode, unreported events are discarded.
985 * @event_mask: specifies the event mask, which is used in accordance with
987 * @cursor: the cursor to display while the grab is active if the device is
988 * a pointer. If this is %NULL then the normal cursors are used for
989 * @window and its descendants, and the cursor for @window is used
991 * @time_: the timestamp of the event which led to this pointer grab. This
992 * usually comes from the #GdkEvent struct, though %GDK_CURRENT_TIME
993 * can be used if the time isn't known.
995 * Grabs the device so that all events coming from this device are passed to
996 * this application until the device is ungrabbed with gdk_device_ungrab(),
997 * or the window becomes unviewable. This overrides any previous grab on the device
1000 * Device grabs are used for operations which need complete control over the
1001 * given device events (either pointer or keyboard). For example in GTK+ this
1002 * is used for Drag and Drop operations, popup menus and such.
1004 * Note that if the event mask of an X window has selected both button press
1005 * and button release events, then a button press event will cause an automatic
1006 * pointer grab until the button is released. X does this automatically since
1007 * most applications expect to receive button press and release events in pairs.
1008 * It is equivalent to a pointer grab on the window with @owner_events set to
1011 * If you set up anything at the time you take the grab that needs to be
1012 * cleaned up when the grab ends, you should handle the #GdkEventGrabBroken
1013 * events that are emitted when the grab ends unvoluntarily.
1015 * Returns: %GDK_GRAB_SUCCESS if the grab was successful.
1020 gdk_device_grab (GdkDevice *device,
1022 GdkGrabOwnership grab_ownership,
1023 gboolean owner_events,
1024 GdkEventMask event_mask,
1031 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
1032 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
1034 if (_gdk_native_windows)
1037 native = gdk_window_get_toplevel (window);
1039 while (((GdkWindowObject *) native)->window_type == GDK_WINDOW_OFFSCREEN)
1041 native = gdk_offscreen_window_get_embedder (native);
1043 if (native == NULL ||
1044 (!_gdk_window_has_impl (native) &&
1045 !gdk_window_is_viewable (native)))
1046 return GDK_GRAB_NOT_VIEWABLE;
1048 native = gdk_window_get_toplevel (native);
1051 res = _gdk_windowing_device_grab (device,
1055 get_native_grab_event_mask (event_mask),
1060 if (res == GDK_GRAB_SUCCESS)
1062 GdkDisplay *display;
1065 display = gdk_window_get_display (window);
1066 serial = _gdk_windowing_window_get_next_serial (display);
1068 _gdk_display_add_device_grab (display,
1085 _gdk_device_reset_axes (GdkDevice *device)
1087 GdkDevicePrivate *priv;
1090 priv = device->priv;
1092 for (i = priv->axes->len - 1; i >= 0; i--)
1093 g_array_remove_index (priv->axes, i);
1095 g_object_notify (G_OBJECT (device), "n-axes");
1097 /* This is done for backwards compatibility */
1098 g_free (device->axes);
1099 device->axes = NULL;
1103 _gdk_device_add_axis (GdkDevice *device,
1110 GdkDevicePrivate *priv;
1111 GdkAxisInfo axis_info;
1114 priv = device->priv;
1116 axis_info.use = use;
1117 axis_info.label = label_atom;
1118 axis_info.min_value = min_value;
1119 axis_info.max_value = max_value;
1120 axis_info.resolution = resolution;
1126 axis_info.min_axis = 0;
1127 axis_info.max_axis = 0;
1129 case GDK_AXIS_XTILT:
1130 case GDK_AXIS_YTILT:
1131 axis_info.min_axis = -1;
1132 axis_info.max_axis = 1;
1135 axis_info.min_axis = 0;
1136 axis_info.max_axis = 1;
1140 priv->axes = g_array_append_val (priv->axes, axis_info);
1141 device->num_axes = priv->axes->len;
1142 pos = device->num_axes - 1;
1144 /* This is done for backwards compatibility, since the public
1145 * struct doesn't actually store the device data.
1147 device->axes = g_realloc (device->axes, sizeof (GdkDeviceAxis) * priv->axes->len);
1148 device->axes[pos].use = axis_info.use;
1149 device->axes[pos].min = axis_info.min_axis;
1150 device->axes[pos].max = axis_info.max_axis;
1152 g_object_notify (G_OBJECT (device), "n-axes");
1158 _gdk_device_set_keys (GdkDevice *device,
1162 g_free (device->keys);
1164 device->num_keys = num_keys;
1165 device->keys = g_new0 (GdkDeviceKey, num_keys);
1168 static GdkAxisInfo *
1169 find_axis_info (GArray *array,
1175 for (i = 0; i < GDK_AXIS_LAST; i++)
1177 info = &g_array_index (array, GdkAxisInfo, i);
1179 if (info->use == use)
1187 _gdk_device_get_axis_use (GdkDevice *device,
1190 GdkDevicePrivate *priv;
1193 priv = device->priv;
1195 info = g_array_index (priv->axes, GdkAxisInfo, index_);
1200 _gdk_device_translate_window_coord (GdkDevice *device,
1204 gdouble *axis_value)
1206 GdkDevicePrivate *priv;
1207 GdkAxisInfo axis_info;
1208 GdkAxisInfo *axis_info_x, *axis_info_y;
1209 gdouble device_width, device_height;
1210 gdouble x_offset, y_offset;
1211 gdouble x_scale, y_scale;
1212 gdouble x_min, y_min;
1213 gdouble x_resolution, y_resolution;
1214 gdouble device_aspect;
1215 gint window_width, window_height;
1216 GdkWindowObject *window_private;
1218 priv = device->priv;
1220 if (index_ >= priv->axes->len)
1223 axis_info = g_array_index (priv->axes, GdkAxisInfo, index_);
1225 if (axis_info.use != GDK_AXIS_X &&
1226 axis_info.use != GDK_AXIS_Y)
1229 if (axis_info.use == GDK_AXIS_X)
1231 axis_info_x = &axis_info;
1232 axis_info_y = find_axis_info (priv->axes, GDK_AXIS_Y);
1236 axis_info_x = find_axis_info (priv->axes, GDK_AXIS_X);
1237 axis_info_y = &axis_info;
1240 device_width = axis_info_x->max_value - axis_info_x->min_value;
1241 device_height = axis_info_y->max_value - axis_info_y->min_value;
1243 if (device_width > 0)
1244 x_min = axis_info_x->min_value;
1247 device_width = gdk_screen_get_width (gdk_window_get_screen (window));
1251 if (device_height > 0)
1252 y_min = axis_info_y->min_value;
1255 device_height = gdk_screen_get_height (gdk_window_get_screen (window));
1259 window_private = (GdkWindowObject *) window;
1260 window_width = gdk_window_get_width (window);
1261 window_height = gdk_window_get_height (window);
1263 x_resolution = axis_info_x->resolution;
1264 y_resolution = axis_info_y->resolution;
1267 * Some drivers incorrectly report the resolution of the device
1268 * as zero (in partiular linuxwacom < 0.5.3 with usb tablets).
1269 * This causes the device_aspect to become NaN and totally
1270 * breaks windowed mode. If this is the case, the best we can
1271 * do is to assume the resolution is non-zero is equal in both
1272 * directions (which is true for many devices). The absolute
1273 * value of the resolution doesn't matter since we only use the
1276 if (x_resolution == 0 || y_resolution == 0)
1282 device_aspect = (device_height * y_resolution) /
1283 (device_width * x_resolution);
1285 if (device_aspect * window_width >= window_height)
1287 /* device taller than window */
1288 x_scale = window_width / device_width;
1289 y_scale = (x_scale * x_resolution) / y_resolution;
1292 y_offset = - (device_height * y_scale - window_height) / 2;
1296 /* window taller than device */
1297 y_scale = window_height / device_height;
1298 x_scale = (y_scale * y_resolution) / x_resolution;
1301 x_offset = - (device_width * x_scale - window_width) / 2;
1306 if (axis_info.use == GDK_AXIS_X)
1307 *axis_value = x_offset + x_scale * (value - x_min);
1309 *axis_value = y_offset + y_scale * (value - y_min);
1316 _gdk_device_translate_screen_coord (GdkDevice *device,
1322 gdouble *axis_value)
1324 GdkDevicePrivate *priv;
1325 GdkAxisInfo axis_info;
1326 gdouble axis_width, scale, offset;
1327 GdkWindowObject *window_private;
1329 if (device->mode != GDK_MODE_SCREEN)
1332 priv = device->priv;
1334 if (index_ >= priv->axes->len)
1337 axis_info = g_array_index (priv->axes, GdkAxisInfo, index_);
1339 if (axis_info.use != GDK_AXIS_X &&
1340 axis_info.use != GDK_AXIS_Y)
1343 axis_width = axis_info.max_value - axis_info.min_value;
1344 window_private = (GdkWindowObject *) window;
1346 if (axis_info.use == GDK_AXIS_X)
1349 scale = gdk_screen_get_width (gdk_window_get_screen (window)) / axis_width;
1353 offset = - window_root_x - window_private->abs_x;
1358 scale = gdk_screen_get_height (gdk_window_get_screen (window)) / axis_width;
1362 offset = - window_root_y - window_private->abs_y;
1366 *axis_value = offset + scale * (value - axis_info.min_value);
1372 _gdk_device_translate_axis (GdkDevice *device,
1375 gdouble *axis_value)
1377 GdkDevicePrivate *priv;
1378 GdkAxisInfo axis_info;
1379 gdouble axis_width, out;
1381 priv = device->priv;
1383 if (index_ >= priv->axes->len)
1386 axis_info = g_array_index (priv->axes, GdkAxisInfo, index_);
1388 if (axis_info.use == GDK_AXIS_X ||
1389 axis_info.use == GDK_AXIS_Y)
1392 axis_width = axis_info.max_value - axis_info.min_value;
1393 out = (axis_info.max_axis * (value - axis_info.min_value) +
1394 axis_info.min_axis * (axis_info.max_value - value)) / axis_width;