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 _GdkDeviceKey GdkDeviceKey;
34 GdkModifierType modifiers;
37 typedef struct _GdkAxisInfo GdkAxisInfo;
52 struct _GdkDevicePrivate
55 GdkInputSource source;
60 GdkDeviceManager *device_manager;
62 GdkDevice *associated;
67 static void gdk_device_dispose (GObject *object);
68 static void gdk_device_set_property (GObject *object,
72 static void gdk_device_get_property (GObject *object,
78 G_DEFINE_ABSTRACT_TYPE (GdkDevice, gdk_device, G_TYPE_OBJECT)
85 PROP_ASSOCIATED_DEVICE,
95 gdk_device_class_init (GdkDeviceClass *klass)
97 GObjectClass *object_class = G_OBJECT_CLASS (klass);
99 object_class->dispose = gdk_device_dispose;
100 object_class->set_property = gdk_device_set_property;
101 object_class->get_property = gdk_device_get_property;
106 * The #GdkDisplay the #GdkDevice pertains to.
110 g_object_class_install_property (object_class,
112 g_param_spec_object ("display",
113 P_("Device Display"),
114 P_("Display which the device belongs to"),
116 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
117 G_PARAM_STATIC_STRINGS));
119 * GdkDevice:device-manager:
121 * The #GdkDeviceManager the #GdkDevice pertains to.
125 g_object_class_install_property (object_class,
127 g_param_spec_object ("device-manager",
128 P_("Device manager"),
129 P_("Device manager which the device belongs to"),
130 GDK_TYPE_DEVICE_MANAGER,
131 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
132 G_PARAM_STATIC_STRINGS));
140 g_object_class_install_property (object_class,
142 g_param_spec_string ("name",
146 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
147 G_PARAM_STATIC_STRINGS));
151 * Device role in the device manager.
155 g_object_class_install_property (object_class,
157 g_param_spec_enum ("type",
159 P_("Device role in the device manager"),
160 GDK_TYPE_DEVICE_TYPE,
161 GDK_DEVICE_TYPE_MASTER,
162 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
163 G_PARAM_STATIC_STRINGS));
165 * GdkDevice:associated-device:
167 * Associated pointer or keyboard with this device, if any. Devices of type #GDK_DEVICE_TYPE_MASTER
168 * always come in keyboard/pointer pairs. Other device types will have a %NULL associated device.
172 g_object_class_install_property (object_class,
173 PROP_ASSOCIATED_DEVICE,
174 g_param_spec_object ("associated-device",
175 P_("Associated device"),
176 P_("Associated pointer or keyboard with this device"),
178 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
180 * GdkDevice:input-source:
182 * Source type for the device.
186 g_object_class_install_property (object_class,
188 g_param_spec_enum ("input-source",
190 P_("Source type for the device"),
191 GDK_TYPE_INPUT_SOURCE,
193 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
194 G_PARAM_STATIC_STRINGS));
196 * GdkDevice:input-mode:
198 * Input mode for the device.
202 g_object_class_install_property (object_class,
204 g_param_spec_enum ("input-mode",
205 P_("Input mode for the device"),
206 P_("Input mode for the device"),
209 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
211 * GdkDevice:has-cursor:
213 * Whether the device is represented by a cursor on the screen. Devices of type
214 * %GDK_DEVICE_TYPE_MASTER will have %TRUE here.
218 g_object_class_install_property (object_class,
220 g_param_spec_boolean ("has-cursor",
221 P_("Whether the device has a cursor"),
222 P_("Whether there is a visible cursor following device motion"),
224 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
225 G_PARAM_STATIC_STRINGS));
229 * Number of axes in the device.
233 g_object_class_install_property (object_class,
235 g_param_spec_uint ("n-axes",
236 P_("Number of axes in the device"),
237 P_("Number of axes in the device"),
239 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
241 g_type_class_add_private (object_class, sizeof (GdkDevicePrivate));
245 gdk_device_init (GdkDevice *device)
247 GdkDevicePrivate *priv;
249 device->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (device,
253 priv->axes = g_array_new (FALSE, TRUE, sizeof (GdkAxisInfo));
257 gdk_device_dispose (GObject *object)
259 GdkDevicePrivate *priv;
262 device = GDK_DEVICE (object);
265 if (priv->associated)
267 _gdk_device_set_associated_device (priv->associated, NULL);
268 g_object_unref (priv->associated);
269 priv->associated = NULL;
274 g_array_free (priv->axes, TRUE);
284 G_OBJECT_CLASS (gdk_device_parent_class)->dispose (object);
288 gdk_device_set_property (GObject *object,
293 GdkDevice *device = GDK_DEVICE (object);
294 GdkDevicePrivate *priv = device->priv;
299 priv->display = g_value_get_object (value);
301 case PROP_DEVICE_MANAGER:
302 priv->device_manager = g_value_get_object (value);
308 priv->name = g_value_dup_string (value);
311 priv->type = g_value_get_enum (value);
313 case PROP_INPUT_SOURCE:
314 priv->source = g_value_get_enum (value);
316 case PROP_INPUT_MODE:
317 gdk_device_set_mode (device, g_value_get_enum (value));
319 case PROP_HAS_CURSOR:
320 priv->has_cursor = g_value_get_boolean (value);
323 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
329 gdk_device_get_property (GObject *object,
334 GdkDevice *device = GDK_DEVICE (object);
335 GdkDevicePrivate *priv = device->priv;
340 g_value_set_object (value, priv->display);
342 case PROP_DEVICE_MANAGER:
343 g_value_set_object (value, priv->device_manager);
345 case PROP_ASSOCIATED_DEVICE:
346 g_value_set_object (value, priv->associated);
349 g_value_set_string (value, priv->name);
352 g_value_set_enum (value, priv->type);
354 case PROP_INPUT_SOURCE:
355 g_value_set_enum (value, priv->source);
357 case PROP_INPUT_MODE:
358 g_value_set_enum (value, priv->mode);
360 case PROP_HAS_CURSOR:
361 g_value_set_boolean (value, priv->has_cursor);
364 g_value_set_uint (value, priv->axes->len);
367 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
373 * gdk_device_get_state:
374 * @device: a #GdkDevice.
375 * @window: a #GdkWindow.
376 * @axes: an array of doubles to store the values of the axes of @device in,
378 * @mask: location to store the modifiers, or %NULL.
380 * Gets the current state of a device relative to @window.
383 gdk_device_get_state (GdkDevice *device,
386 GdkModifierType *mask)
388 g_return_if_fail (GDK_IS_DEVICE (device));
389 g_return_if_fail (GDK_IS_WINDOW (window));
391 if (GDK_DEVICE_GET_CLASS (device)->get_state)
392 GDK_DEVICE_GET_CLASS (device)->get_state (device, window, axes, mask);
396 * gdk_device_get_history:
397 * @device: a #GdkDevice
398 * @window: the window with respect to which which the event coordinates will be reported
399 * @start: starting timestamp for range of events to return
400 * @stop: ending timestamp for the range of events to return
401 * @events: (array length=n_events) (out) (transfer none): location to store a newly-allocated array of #GdkTimeCoord, or %NULL
402 * @n_events: location to store the length of @events, or %NULL
404 * Obtains the motion history for a device; given a starting and
405 * ending timestamp, return all events in the motion history for
406 * the device in the given range of time. Some windowing systems
407 * do not support motion history, in which case, %FALSE will
408 * be returned. (This is not distinguishable from the case where
409 * motion history is supported and no events were found.)
411 * Return value: %TRUE if the windowing system supports motion history and
412 * at least one event was found.
415 gdk_device_get_history (GdkDevice *device,
419 GdkTimeCoord ***events,
422 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
423 g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
431 if (GDK_WINDOW_DESTROYED (window))
434 if (!GDK_DEVICE_GET_CLASS (device)->get_history)
437 return GDK_DEVICE_GET_CLASS (device)->get_history (device, window,
443 _gdk_device_allocate_history (GdkDevice *device,
446 GdkTimeCoord **result = g_new (GdkTimeCoord *, n_events);
449 for (i = 0; i < n_events; i++)
450 result[i] = g_malloc (sizeof (GdkTimeCoord) -
451 sizeof (double) * (GDK_MAX_TIMECOORD_AXES - device->priv->axes->len));
456 * gdk_device_free_history:
457 * @events: (inout) (transfer none): an array of #GdkTimeCoord.
458 * @n_events: the length of the array.
460 * Frees an array of #GdkTimeCoord that was returned by gdk_device_get_history().
463 gdk_device_free_history (GdkTimeCoord **events,
468 for (i = 0; i < n_events; i++)
475 * gdk_device_get_name:
476 * @device: a #GdkDevice
478 * Determines the name of the device.
480 * Return value: a name
485 gdk_device_get_name (GdkDevice *device)
487 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
489 return device->priv->name;
493 * gdk_device_get_has_cursor:
494 * @device: a #GdkDevice
496 * Determines whether the pointer follows device motion.
498 * Return value: %TRUE if the pointer follows device motion
503 gdk_device_get_has_cursor (GdkDevice *device)
505 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
507 return device->priv->has_cursor;
511 * gdk_device_get_source:
512 * @device: a #GdkDevice
514 * Determines the type of the device.
516 * Return value: a #GdkInputSource
521 gdk_device_get_source (GdkDevice *device)
523 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
525 return device->priv->source;
529 * gdk_device_set_source:
530 * @device: a #GdkDevice.
531 * @source: the source type.
533 * Sets the source type for an input device.
536 gdk_device_set_source (GdkDevice *device,
537 GdkInputSource source)
539 g_return_if_fail (GDK_IS_DEVICE (device));
541 device->priv->source = source;
542 g_object_notify (G_OBJECT (device), "input-source");
546 * gdk_device_get_mode:
547 * @device: a #GdkDevice
549 * Determines the mode of the device.
551 * Return value: a #GdkInputSource
556 gdk_device_get_mode (GdkDevice *device)
558 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
560 return device->priv->mode;
564 * gdk_device_set_mode:
565 * @device: a #GdkDevice.
566 * @mode: the input mode.
568 * Sets a the mode of an input device. The mode controls if the
569 * device is active and whether the device's range is mapped to the
570 * entire screen or to a single window.
572 * Returns: %TRUE if the mode was successfully changed.
575 gdk_device_set_mode (GdkDevice *device,
578 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
580 if (device->priv->mode == mode)
583 if (mode == GDK_MODE_DISABLED &&
584 gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER)
587 /* FIXME: setting has_cursor when mode is window? */
589 device->priv->mode = mode;
590 g_object_notify (G_OBJECT (device), "input-mode");
592 if (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER)
593 _gdk_input_check_extension_events (device);
599 * gdk_device_get_n_keys:
600 * @device: a #GdkDevice
602 * Returns the number of keys the device currently has.
604 * Returns: the number of keys.
609 gdk_device_get_n_keys (GdkDevice *device)
611 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
613 return device->priv->num_keys;
617 * gdk_device_get_key:
618 * @device: a #GdkDevice.
619 * @index_: the index of the macro button to get.
620 * @keyval: return value for the keyval.
621 * @modifiers: return value for modifiers.
623 * If @index_ has a valid keyval, this function will return %TRUE
624 * and fill in @keyval and @modifiers with the keyval settings.
626 * Returns: %TRUE if keyval is set for @index.
631 gdk_device_get_key (GdkDevice *device,
634 GdkModifierType *modifiers)
636 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
637 g_return_val_if_fail (index_ < device->priv->num_keys, FALSE);
639 if (!device->priv->keys[index_].keyval &&
640 !device->priv->keys[index_].modifiers)
644 *keyval = device->priv->keys[index_].keyval;
647 *modifiers = device->priv->keys[index_].modifiers;
653 * gdk_device_set_key:
654 * @device: a #GdkDevice
655 * @index_: the index of the macro button to set
656 * @keyval: the keyval to generate
657 * @modifiers: the modifiers to set
659 * Specifies the X key event to generate when a macro button of a device
663 gdk_device_set_key (GdkDevice *device,
666 GdkModifierType modifiers)
668 g_return_if_fail (GDK_IS_DEVICE (device));
669 g_return_if_fail (index_ < device->priv->num_keys);
671 device->priv->keys[index_].keyval = keyval;
672 device->priv->keys[index_].modifiers = modifiers;
676 * gdk_device_get_axis_use:
677 * @device: a #GdkDevice.
678 * @index_: the index of the axis.
680 * Returns the axis use for @index_.
682 * Returns: a #GdkAxisUse specifying how the axis is used.
687 gdk_device_get_axis_use (GdkDevice *device,
692 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_AXIS_IGNORE);
693 g_return_val_if_fail (index_ < device->priv->axes->len, GDK_AXIS_IGNORE);
695 info = &g_array_index (device->priv->axes, GdkAxisInfo, index_);
701 * gdk_device_set_axis_use:
702 * @device: a #GdkDevice
703 * @index_: the index of the axis
704 * @use: specifies how the axis is used
706 * Specifies how an axis of a device is used.
709 gdk_device_set_axis_use (GdkDevice *device,
713 GdkDevicePrivate *priv;
716 g_return_if_fail (GDK_IS_DEVICE (device));
717 g_return_if_fail (index_ < device->priv->axes->len);
720 info = &g_array_index (priv->axes, GdkAxisInfo, index_);
743 * gdk_device_get_display:
744 * @device: a #GdkDevice
746 * Returns the #GdkDisplay to which @device pertains.
748 * Returns: (transfer none): a #GdkDisplay. This memory is owned
749 * by GTK+, and must not be freed or unreffed.
754 gdk_device_get_display (GdkDevice *device)
756 GdkDevicePrivate *priv;
758 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
762 return priv->display;
766 * gdk_device_get_associated_device:
767 * @device: a #GdkDevice
769 * Returns the associated device to @device, if @device is of type
770 * %GDK_DEVICE_TYPE_MASTER, it will return the paired pointer or
773 * If @device is of type %GDK_DEVICE_TYPE_SLAVE, it will return
774 * the master device to which @device is attached to.
776 * If @device is of type %GDK_DEVICE_TYPE_FLOATING, %NULL will be
777 * returned, as there is no associated device.
779 * Returns: (transfer none): The associated device, or %NULL
784 gdk_device_get_associated_device (GdkDevice *device)
786 GdkDevicePrivate *priv;
788 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
792 return priv->associated;
796 _gdk_device_set_associated_device (GdkDevice *device,
797 GdkDevice *associated)
799 GdkDevicePrivate *priv;
801 g_return_if_fail (GDK_IS_DEVICE (device));
802 g_return_if_fail (associated == NULL || GDK_IS_DEVICE (associated));
806 if (priv->associated == associated)
809 if (priv->associated)
811 g_object_unref (priv->associated);
812 priv->associated = NULL;
816 priv->associated = g_object_ref (associated);
820 * gdk_device_get_device_type:
821 * @device: a #GdkDevice
823 * Returns the device type for @device.
825 * Returns: the #GdkDeviceType for @device.
830 gdk_device_get_device_type (GdkDevice *device)
832 GdkDevicePrivate *priv;
834 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_DEVICE_TYPE_MASTER);
842 * gdk_device_get_n_axes:
843 * @device: a #GdkDevice
845 * Returns the number of axes the device currently has.
847 * Returns: the number of axes.
852 gdk_device_get_n_axes (GdkDevice *device)
854 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
856 return device->priv->axes->len;
860 * gdk_device_list_axes:
861 * @device: a #GdkDevice
863 * Returns a #GList of #GdkAtom<!-- -->s, containing the labels for
864 * the axes that @device currently has.
866 * Returns: (transfer container) (element-type GdkAtom):
867 * A #GList of #GdkAtom<!-- -->s, free with g_list_free().
872 gdk_device_list_axes (GdkDevice *device)
874 GdkDevicePrivate *priv;
880 for (i = 0; i < priv->axes->len; i++)
882 GdkAxisInfo axis_info;
884 axis_info = g_array_index (priv->axes, GdkAxisInfo, i);
885 axes = g_list_prepend (axes, GDK_ATOM_TO_POINTER (axis_info.label));
888 return g_list_reverse (axes);
892 * gdk_device_get_axis_value:
893 * @device: a #GdkDevice.
894 * @axes: pointer to an array of axes
895 * @axis_label: #GdkAtom with the axis label.
896 * @value: location to store the found value.
898 * Interprets an array of double as axis values for a given device,
899 * and locates the value in the array for a given axis label, as returned
900 * by gdk_device_list_axes()
902 * Returns: %TRUE if the given axis use was found, otherwise %FALSE.
907 gdk_device_get_axis_value (GdkDevice *device,
912 GdkDevicePrivate *priv;
915 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
922 for (i = 0; i < priv->axes->len; i++)
924 GdkAxisInfo axis_info;
926 axis_info = g_array_index (priv->axes, GdkAxisInfo, i);
928 if (axis_info.label != axis_label)
941 * gdk_device_get_axis:
942 * @device: a #GdkDevice
943 * @axes: pointer to an array of axes
944 * @use: the use to look for
945 * @value: location to store the found value.
947 * Interprets an array of double as axis values for a given device,
948 * and locates the value in the array for a given axis use.
950 * Return value: %TRUE if the given axis use was found, otherwise %FALSE
953 gdk_device_get_axis (GdkDevice *device,
958 GdkDevicePrivate *priv;
961 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
968 g_return_val_if_fail (priv->axes != NULL, FALSE);
970 for (i = 0; i < priv->axes->len; i++)
972 GdkAxisInfo axis_info;
974 axis_info = g_array_index (priv->axes, GdkAxisInfo, i);
976 if (axis_info.use != use)
989 get_native_grab_event_mask (GdkEventMask grab_mask)
991 /* Similar to the above but for pointer events only */
993 GDK_POINTER_MOTION_MASK |
994 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
995 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
998 ~(GDK_POINTER_MOTION_HINT_MASK |
999 GDK_BUTTON_MOTION_MASK |
1000 GDK_BUTTON1_MOTION_MASK |
1001 GDK_BUTTON2_MOTION_MASK |
1002 GDK_BUTTON3_MOTION_MASK));
1007 * @device: a #GdkDevice
1008 * @window: the #GdkWindow which will own the grab (the grab window)
1009 * @grab_ownership: specifies the grab ownership.
1010 * @owner_events: if %FALSE then all device events are reported with respect to
1011 * @window and are only reported if selected by @event_mask. If
1012 * %TRUE then pointer events for this application are reported
1013 * as normal, but pointer events outside this application are
1014 * reported with respect to @window and only if selected by
1015 * @event_mask. In either mode, unreported events are discarded.
1016 * @event_mask: specifies the event mask, which is used in accordance with
1018 * @cursor: the cursor to display while the grab is active if the device is
1019 * a pointer. If this is %NULL then the normal cursors are used for
1020 * @window and its descendants, and the cursor for @window is used
1022 * @time_: the timestamp of the event which led to this pointer grab. This
1023 * usually comes from the #GdkEvent struct, though %GDK_CURRENT_TIME
1024 * can be used if the time isn't known.
1026 * Grabs the device so that all events coming from this device are passed to
1027 * this application until the device is ungrabbed with gdk_device_ungrab(),
1028 * or the window becomes unviewable. This overrides any previous grab on the device
1031 * Device grabs are used for operations which need complete control over the
1032 * given device events (either pointer or keyboard). For example in GTK+ this
1033 * is used for Drag and Drop operations, popup menus and such.
1035 * Note that if the event mask of an X window has selected both button press
1036 * and button release events, then a button press event will cause an automatic
1037 * pointer grab until the button is released. X does this automatically since
1038 * most applications expect to receive button press and release events in pairs.
1039 * It is equivalent to a pointer grab on the window with @owner_events set to
1042 * If you set up anything at the time you take the grab that needs to be
1043 * cleaned up when the grab ends, you should handle the #GdkEventGrabBroken
1044 * events that are emitted when the grab ends unvoluntarily.
1046 * Returns: %GDK_GRAB_SUCCESS if the grab was successful.
1051 gdk_device_grab (GdkDevice *device,
1053 GdkGrabOwnership grab_ownership,
1054 gboolean owner_events,
1055 GdkEventMask event_mask,
1062 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
1063 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
1065 if (_gdk_native_windows)
1068 native = gdk_window_get_toplevel (window);
1070 while (((GdkWindowObject *) native)->window_type == GDK_WINDOW_OFFSCREEN)
1072 native = gdk_offscreen_window_get_embedder (native);
1074 if (native == NULL ||
1075 (!_gdk_window_has_impl (native) &&
1076 !gdk_window_is_viewable (native)))
1077 return GDK_GRAB_NOT_VIEWABLE;
1079 native = gdk_window_get_toplevel (native);
1082 res = _gdk_windowing_device_grab (device,
1086 get_native_grab_event_mask (event_mask),
1091 if (res == GDK_GRAB_SUCCESS)
1093 GdkDisplay *display;
1096 display = gdk_window_get_display (window);
1097 serial = _gdk_windowing_window_get_next_serial (display);
1099 _gdk_display_add_device_grab (display,
1116 _gdk_device_reset_axes (GdkDevice *device)
1118 GdkDevicePrivate *priv;
1121 priv = device->priv;
1123 for (i = priv->axes->len - 1; i >= 0; i--)
1124 g_array_remove_index (priv->axes, i);
1126 g_object_notify (G_OBJECT (device), "n-axes");
1130 _gdk_device_add_axis (GdkDevice *device,
1137 GdkDevicePrivate *priv;
1138 GdkAxisInfo axis_info;
1141 priv = device->priv;
1143 axis_info.use = use;
1144 axis_info.label = label_atom;
1145 axis_info.min_value = min_value;
1146 axis_info.max_value = max_value;
1147 axis_info.resolution = resolution;
1153 axis_info.min_axis = 0;
1154 axis_info.max_axis = 0;
1156 case GDK_AXIS_XTILT:
1157 case GDK_AXIS_YTILT:
1158 axis_info.min_axis = -1;
1159 axis_info.max_axis = 1;
1162 axis_info.min_axis = 0;
1163 axis_info.max_axis = 1;
1167 priv->axes = g_array_append_val (priv->axes, axis_info);
1168 pos = device->priv->axes->len - 1;
1170 g_object_notify (G_OBJECT (device), "n-axes");
1176 _gdk_device_set_keys (GdkDevice *device,
1179 if (device->priv->keys)
1180 g_free (device->priv->keys);
1182 device->priv->num_keys = num_keys;
1183 device->priv->keys = g_new0 (GdkDeviceKey, num_keys);
1186 static GdkAxisInfo *
1187 find_axis_info (GArray *array,
1193 for (i = 0; i < GDK_AXIS_LAST; i++)
1195 info = &g_array_index (array, GdkAxisInfo, i);
1197 if (info->use == use)
1205 _gdk_device_get_axis_use (GdkDevice *device,
1208 GdkDevicePrivate *priv;
1211 priv = device->priv;
1213 info = g_array_index (priv->axes, GdkAxisInfo, index_);
1218 _gdk_device_translate_window_coord (GdkDevice *device,
1222 gdouble *axis_value)
1224 GdkDevicePrivate *priv;
1225 GdkAxisInfo axis_info;
1226 GdkAxisInfo *axis_info_x, *axis_info_y;
1227 gdouble device_width, device_height;
1228 gdouble x_offset, y_offset;
1229 gdouble x_scale, y_scale;
1230 gdouble x_min, y_min;
1231 gdouble x_resolution, y_resolution;
1232 gdouble device_aspect;
1233 gint window_width, window_height;
1234 GdkWindowObject *window_private;
1236 priv = device->priv;
1238 if (index_ >= priv->axes->len)
1241 axis_info = g_array_index (priv->axes, GdkAxisInfo, index_);
1243 if (axis_info.use != GDK_AXIS_X &&
1244 axis_info.use != GDK_AXIS_Y)
1247 if (axis_info.use == GDK_AXIS_X)
1249 axis_info_x = &axis_info;
1250 axis_info_y = find_axis_info (priv->axes, GDK_AXIS_Y);
1254 axis_info_x = find_axis_info (priv->axes, GDK_AXIS_X);
1255 axis_info_y = &axis_info;
1258 device_width = axis_info_x->max_value - axis_info_x->min_value;
1259 device_height = axis_info_y->max_value - axis_info_y->min_value;
1261 if (device_width > 0)
1262 x_min = axis_info_x->min_value;
1265 device_width = gdk_screen_get_width (gdk_window_get_screen (window));
1269 if (device_height > 0)
1270 y_min = axis_info_y->min_value;
1273 device_height = gdk_screen_get_height (gdk_window_get_screen (window));
1277 window_private = (GdkWindowObject *) window;
1278 window_width = gdk_window_get_width (window);
1279 window_height = gdk_window_get_height (window);
1281 x_resolution = axis_info_x->resolution;
1282 y_resolution = axis_info_y->resolution;
1285 * Some drivers incorrectly report the resolution of the device
1286 * as zero (in partiular linuxwacom < 0.5.3 with usb tablets).
1287 * This causes the device_aspect to become NaN and totally
1288 * breaks windowed mode. If this is the case, the best we can
1289 * do is to assume the resolution is non-zero is equal in both
1290 * directions (which is true for many devices). The absolute
1291 * value of the resolution doesn't matter since we only use the
1294 if (x_resolution == 0 || y_resolution == 0)
1300 device_aspect = (device_height * y_resolution) /
1301 (device_width * x_resolution);
1303 if (device_aspect * window_width >= window_height)
1305 /* device taller than window */
1306 x_scale = window_width / device_width;
1307 y_scale = (x_scale * x_resolution) / y_resolution;
1310 y_offset = - (device_height * y_scale - window_height) / 2;
1314 /* window taller than device */
1315 y_scale = window_height / device_height;
1316 x_scale = (y_scale * y_resolution) / x_resolution;
1319 x_offset = - (device_width * x_scale - window_width) / 2;
1324 if (axis_info.use == GDK_AXIS_X)
1325 *axis_value = x_offset + x_scale * (value - x_min);
1327 *axis_value = y_offset + y_scale * (value - y_min);
1334 _gdk_device_translate_screen_coord (GdkDevice *device,
1340 gdouble *axis_value)
1342 GdkDevicePrivate *priv = device->priv;
1343 GdkAxisInfo axis_info;
1344 gdouble axis_width, scale, offset;
1345 GdkWindowObject *window_private;
1347 if (priv->mode != GDK_MODE_SCREEN)
1350 if (index_ >= priv->axes->len)
1353 axis_info = g_array_index (priv->axes, GdkAxisInfo, index_);
1355 if (axis_info.use != GDK_AXIS_X &&
1356 axis_info.use != GDK_AXIS_Y)
1359 axis_width = axis_info.max_value - axis_info.min_value;
1360 window_private = (GdkWindowObject *) window;
1362 if (axis_info.use == GDK_AXIS_X)
1365 scale = gdk_screen_get_width (gdk_window_get_screen (window)) / axis_width;
1369 offset = - window_root_x - window_private->abs_x;
1374 scale = gdk_screen_get_height (gdk_window_get_screen (window)) / axis_width;
1378 offset = - window_root_y - window_private->abs_y;
1382 *axis_value = offset + scale * (value - axis_info.min_value);
1388 _gdk_device_translate_axis (GdkDevice *device,
1391 gdouble *axis_value)
1393 GdkDevicePrivate *priv;
1394 GdkAxisInfo axis_info;
1395 gdouble axis_width, out;
1397 priv = device->priv;
1399 if (index_ >= priv->axes->len)
1402 axis_info = g_array_index (priv->axes, GdkAxisInfo, index_);
1404 if (axis_info.use == GDK_AXIS_X ||
1405 axis_info.use == GDK_AXIS_Y)
1408 axis_width = axis_info.max_value - axis_info.min_value;
1409 out = (axis_info.max_axis * (value - axis_info.min_value) +
1410 axis_info.min_axis * (axis_info.max_value - value)) / axis_width;