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;
72 static guint signals [LAST_SIGNAL] = { 0 };
75 static void gdk_device_dispose (GObject *object);
76 static void gdk_device_set_property (GObject *object,
80 static void gdk_device_get_property (GObject *object,
86 G_DEFINE_ABSTRACT_TYPE (GdkDevice, gdk_device, G_TYPE_OBJECT)
93 PROP_ASSOCIATED_DEVICE,
103 gdk_device_class_init (GdkDeviceClass *klass)
105 GObjectClass *object_class = G_OBJECT_CLASS (klass);
107 object_class->dispose = gdk_device_dispose;
108 object_class->set_property = gdk_device_set_property;
109 object_class->get_property = gdk_device_get_property;
114 * The #GdkDisplay the #GdkDevice pertains to.
118 g_object_class_install_property (object_class,
120 g_param_spec_object ("display",
121 P_("Device Display"),
122 P_("Display which the device belongs to"),
124 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
125 G_PARAM_STATIC_STRINGS));
127 * GdkDevice:device-manager:
129 * The #GdkDeviceManager the #GdkDevice pertains to.
133 g_object_class_install_property (object_class,
135 g_param_spec_object ("device-manager",
136 P_("Device manager"),
137 P_("Device manager which the device belongs to"),
138 GDK_TYPE_DEVICE_MANAGER,
139 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
140 G_PARAM_STATIC_STRINGS));
148 g_object_class_install_property (object_class,
150 g_param_spec_string ("name",
154 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
155 G_PARAM_STATIC_STRINGS));
159 * Device role in the device manager.
163 g_object_class_install_property (object_class,
165 g_param_spec_enum ("type",
167 P_("Device role in the device manager"),
168 GDK_TYPE_DEVICE_TYPE,
169 GDK_DEVICE_TYPE_MASTER,
170 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
171 G_PARAM_STATIC_STRINGS));
173 * GdkDevice:associated-device:
175 * Associated pointer or keyboard with this device, if any. Devices of type #GDK_DEVICE_TYPE_MASTER
176 * always come in keyboard/pointer pairs. Other device types will have a %NULL associated device.
180 g_object_class_install_property (object_class,
181 PROP_ASSOCIATED_DEVICE,
182 g_param_spec_object ("associated-device",
183 P_("Associated device"),
184 P_("Associated pointer or keyboard with this device"),
186 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
188 * GdkDevice:input-source:
190 * Source type for the device.
194 g_object_class_install_property (object_class,
196 g_param_spec_enum ("input-source",
198 P_("Source type for the device"),
199 GDK_TYPE_INPUT_SOURCE,
201 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
202 G_PARAM_STATIC_STRINGS));
204 * GdkDevice:input-mode:
206 * Input mode for the device.
210 g_object_class_install_property (object_class,
212 g_param_spec_enum ("input-mode",
213 P_("Input mode for the device"),
214 P_("Input mode for the device"),
217 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
219 * GdkDevice:has-cursor:
221 * Whether the device is represented by a cursor on the screen. Devices of type
222 * %GDK_DEVICE_TYPE_MASTER will have %TRUE here.
226 g_object_class_install_property (object_class,
228 g_param_spec_boolean ("has-cursor",
229 P_("Whether the device has a cursor"),
230 P_("Whether there is a visible cursor following device motion"),
232 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
233 G_PARAM_STATIC_STRINGS));
237 * Number of axes in the device.
241 g_object_class_install_property (object_class,
243 g_param_spec_uint ("n-axes",
244 P_("Number of axes in the device"),
245 P_("Number of axes in the device"),
247 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
250 * GdkDevice::changed:
251 * @device: the #GdkDevice that changed.
253 * The ::changed signal is emitted either when the #GdkDevice
254 * has changed the number of either axes or keys. For example
255 * In X this will normally happen when the slave device routing
256 * events through the master device changes (for example, user
257 * switches from the USB mouse to a tablet), in that case the
258 * master device will change to reflect the new slave device
262 g_signal_new (g_intern_static_string ("changed"),
263 G_TYPE_FROM_CLASS (object_class),
266 g_cclosure_marshal_VOID__VOID,
269 g_type_class_add_private (object_class, sizeof (GdkDevicePrivate));
273 gdk_device_init (GdkDevice *device)
275 GdkDevicePrivate *priv;
277 device->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (device,
281 priv->axes = g_array_new (FALSE, TRUE, sizeof (GdkAxisInfo));
285 gdk_device_dispose (GObject *object)
287 GdkDevicePrivate *priv;
290 device = GDK_DEVICE (object);
293 if (priv->associated)
295 _gdk_device_set_associated_device (priv->associated, NULL);
296 g_object_unref (priv->associated);
297 priv->associated = NULL;
302 g_array_free (priv->axes, TRUE);
312 G_OBJECT_CLASS (gdk_device_parent_class)->dispose (object);
316 gdk_device_set_property (GObject *object,
321 GdkDevice *device = GDK_DEVICE (object);
322 GdkDevicePrivate *priv = device->priv;
327 priv->display = g_value_get_object (value);
329 case PROP_DEVICE_MANAGER:
330 priv->device_manager = g_value_get_object (value);
336 priv->name = g_value_dup_string (value);
339 priv->type = g_value_get_enum (value);
341 case PROP_INPUT_SOURCE:
342 priv->source = g_value_get_enum (value);
344 case PROP_INPUT_MODE:
345 gdk_device_set_mode (device, g_value_get_enum (value));
347 case PROP_HAS_CURSOR:
348 priv->has_cursor = g_value_get_boolean (value);
351 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
357 gdk_device_get_property (GObject *object,
362 GdkDevice *device = GDK_DEVICE (object);
363 GdkDevicePrivate *priv = device->priv;
368 g_value_set_object (value, priv->display);
370 case PROP_DEVICE_MANAGER:
371 g_value_set_object (value, priv->device_manager);
373 case PROP_ASSOCIATED_DEVICE:
374 g_value_set_object (value, priv->associated);
377 g_value_set_string (value, priv->name);
380 g_value_set_enum (value, priv->type);
382 case PROP_INPUT_SOURCE:
383 g_value_set_enum (value, priv->source);
385 case PROP_INPUT_MODE:
386 g_value_set_enum (value, priv->mode);
388 case PROP_HAS_CURSOR:
389 g_value_set_boolean (value, priv->has_cursor);
392 g_value_set_uint (value, priv->axes->len);
395 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
401 * gdk_device_get_state:
402 * @device: a #GdkDevice.
403 * @window: a #GdkWindow.
404 * @axes: an array of doubles to store the values of the axes of @device in,
406 * @mask: location to store the modifiers, or %NULL.
408 * Gets the current state of a device relative to @window.
411 gdk_device_get_state (GdkDevice *device,
414 GdkModifierType *mask)
416 g_return_if_fail (GDK_IS_DEVICE (device));
417 g_return_if_fail (GDK_IS_WINDOW (window));
419 if (GDK_DEVICE_GET_CLASS (device)->get_state)
420 GDK_DEVICE_GET_CLASS (device)->get_state (device, window, axes, mask);
424 * gdk_device_get_history:
425 * @device: a #GdkDevice
426 * @window: the window with respect to which which the event coordinates will be reported
427 * @start: starting timestamp for range of events to return
428 * @stop: ending timestamp for the range of events to return
429 * @events: (array length=n_events) (out) (transfer none): location to store a newly-allocated array of #GdkTimeCoord, or %NULL
430 * @n_events: location to store the length of @events, or %NULL
432 * Obtains the motion history for a device; given a starting and
433 * ending timestamp, return all events in the motion history for
434 * the device in the given range of time. Some windowing systems
435 * do not support motion history, in which case, %FALSE will
436 * be returned. (This is not distinguishable from the case where
437 * motion history is supported and no events were found.)
439 * Return value: %TRUE if the windowing system supports motion history and
440 * at least one event was found.
443 gdk_device_get_history (GdkDevice *device,
447 GdkTimeCoord ***events,
450 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
451 g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
459 if (GDK_WINDOW_DESTROYED (window))
462 if (!GDK_DEVICE_GET_CLASS (device)->get_history)
465 return GDK_DEVICE_GET_CLASS (device)->get_history (device, window,
471 _gdk_device_allocate_history (GdkDevice *device,
474 GdkTimeCoord **result = g_new (GdkTimeCoord *, n_events);
477 for (i = 0; i < n_events; i++)
478 result[i] = g_malloc (sizeof (GdkTimeCoord) -
479 sizeof (double) * (GDK_MAX_TIMECOORD_AXES - device->priv->axes->len));
484 * gdk_device_free_history:
485 * @events: (inout) (transfer none): an array of #GdkTimeCoord.
486 * @n_events: the length of the array.
488 * Frees an array of #GdkTimeCoord that was returned by gdk_device_get_history().
491 gdk_device_free_history (GdkTimeCoord **events,
496 for (i = 0; i < n_events; i++)
503 * gdk_device_get_name:
504 * @device: a #GdkDevice
506 * Determines the name of the device.
508 * Return value: a name
513 gdk_device_get_name (GdkDevice *device)
515 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
517 return device->priv->name;
521 * gdk_device_get_has_cursor:
522 * @device: a #GdkDevice
524 * Determines whether the pointer follows device motion.
526 * Return value: %TRUE if the pointer follows device motion
531 gdk_device_get_has_cursor (GdkDevice *device)
533 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
535 return device->priv->has_cursor;
539 * gdk_device_get_source:
540 * @device: a #GdkDevice
542 * Determines the type of the device.
544 * Return value: a #GdkInputSource
549 gdk_device_get_source (GdkDevice *device)
551 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
553 return device->priv->source;
557 * gdk_device_set_source:
558 * @device: a #GdkDevice.
559 * @source: the source type.
561 * Sets the source type for an input device.
564 gdk_device_set_source (GdkDevice *device,
565 GdkInputSource source)
567 g_return_if_fail (GDK_IS_DEVICE (device));
569 device->priv->source = source;
570 g_object_notify (G_OBJECT (device), "input-source");
574 * gdk_device_get_mode:
575 * @device: a #GdkDevice
577 * Determines the mode of the device.
579 * Return value: a #GdkInputSource
584 gdk_device_get_mode (GdkDevice *device)
586 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
588 return device->priv->mode;
592 * gdk_device_set_mode:
593 * @device: a #GdkDevice.
594 * @mode: the input mode.
596 * Sets a the mode of an input device. The mode controls if the
597 * device is active and whether the device's range is mapped to the
598 * entire screen or to a single window.
600 * Returns: %TRUE if the mode was successfully changed.
603 gdk_device_set_mode (GdkDevice *device,
606 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
608 if (device->priv->mode == mode)
611 if (mode == GDK_MODE_DISABLED &&
612 gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER)
615 /* FIXME: setting has_cursor when mode is window? */
617 device->priv->mode = mode;
618 g_object_notify (G_OBJECT (device), "input-mode");
620 if (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER)
621 _gdk_input_check_extension_events (device);
627 * gdk_device_get_n_keys:
628 * @device: a #GdkDevice
630 * Returns the number of keys the device currently has.
632 * Returns: the number of keys.
637 gdk_device_get_n_keys (GdkDevice *device)
639 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
641 return device->priv->num_keys;
645 * gdk_device_get_key:
646 * @device: a #GdkDevice.
647 * @index_: the index of the macro button to get.
648 * @keyval: return value for the keyval.
649 * @modifiers: return value for modifiers.
651 * If @index_ has a valid keyval, this function will return %TRUE
652 * and fill in @keyval and @modifiers with the keyval settings.
654 * Returns: %TRUE if keyval is set for @index.
659 gdk_device_get_key (GdkDevice *device,
662 GdkModifierType *modifiers)
664 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
665 g_return_val_if_fail (index_ < device->priv->num_keys, FALSE);
667 if (!device->priv->keys[index_].keyval &&
668 !device->priv->keys[index_].modifiers)
672 *keyval = device->priv->keys[index_].keyval;
675 *modifiers = device->priv->keys[index_].modifiers;
681 * gdk_device_set_key:
682 * @device: a #GdkDevice
683 * @index_: the index of the macro button to set
684 * @keyval: the keyval to generate
685 * @modifiers: the modifiers to set
687 * Specifies the X key event to generate when a macro button of a device
691 gdk_device_set_key (GdkDevice *device,
694 GdkModifierType modifiers)
696 g_return_if_fail (GDK_IS_DEVICE (device));
697 g_return_if_fail (index_ < device->priv->num_keys);
699 device->priv->keys[index_].keyval = keyval;
700 device->priv->keys[index_].modifiers = modifiers;
704 * gdk_device_get_axis_use:
705 * @device: a #GdkDevice.
706 * @index_: the index of the axis.
708 * Returns the axis use for @index_.
710 * Returns: a #GdkAxisUse specifying how the axis is used.
715 gdk_device_get_axis_use (GdkDevice *device,
720 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_AXIS_IGNORE);
721 g_return_val_if_fail (index_ < device->priv->axes->len, GDK_AXIS_IGNORE);
723 info = &g_array_index (device->priv->axes, GdkAxisInfo, index_);
729 * gdk_device_set_axis_use:
730 * @device: a #GdkDevice
731 * @index_: the index of the axis
732 * @use: specifies how the axis is used
734 * Specifies how an axis of a device is used.
737 gdk_device_set_axis_use (GdkDevice *device,
741 GdkDevicePrivate *priv;
744 g_return_if_fail (GDK_IS_DEVICE (device));
745 g_return_if_fail (index_ < device->priv->axes->len);
748 info = &g_array_index (priv->axes, GdkAxisInfo, index_);
771 * gdk_device_get_display:
772 * @device: a #GdkDevice
774 * Returns the #GdkDisplay to which @device pertains.
776 * Returns: (transfer none): a #GdkDisplay. This memory is owned
777 * by GTK+, and must not be freed or unreffed.
782 gdk_device_get_display (GdkDevice *device)
784 GdkDevicePrivate *priv;
786 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
790 return priv->display;
794 * gdk_device_get_associated_device:
795 * @device: a #GdkDevice
797 * Returns the associated device to @device, if @device is of type
798 * %GDK_DEVICE_TYPE_MASTER, it will return the paired pointer or
801 * If @device is of type %GDK_DEVICE_TYPE_SLAVE, it will return
802 * the master device to which @device is attached to.
804 * If @device is of type %GDK_DEVICE_TYPE_FLOATING, %NULL will be
805 * returned, as there is no associated device.
807 * Returns: (transfer none): The associated device, or %NULL
812 gdk_device_get_associated_device (GdkDevice *device)
814 GdkDevicePrivate *priv;
816 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
820 return priv->associated;
824 _gdk_device_set_associated_device (GdkDevice *device,
825 GdkDevice *associated)
827 GdkDevicePrivate *priv;
829 g_return_if_fail (GDK_IS_DEVICE (device));
830 g_return_if_fail (associated == NULL || GDK_IS_DEVICE (associated));
834 if (priv->associated == associated)
837 if (priv->associated)
839 g_object_unref (priv->associated);
840 priv->associated = NULL;
844 priv->associated = g_object_ref (associated);
848 * gdk_device_get_device_type:
849 * @device: a #GdkDevice
851 * Returns the device type for @device.
853 * Returns: the #GdkDeviceType for @device.
858 gdk_device_get_device_type (GdkDevice *device)
860 GdkDevicePrivate *priv;
862 g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_DEVICE_TYPE_MASTER);
870 * gdk_device_get_n_axes:
871 * @device: a #GdkDevice
873 * Returns the number of axes the device currently has.
875 * Returns: the number of axes.
880 gdk_device_get_n_axes (GdkDevice *device)
882 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
884 return device->priv->axes->len;
888 * gdk_device_list_axes:
889 * @device: a #GdkDevice
891 * Returns a #GList of #GdkAtom<!-- -->s, containing the labels for
892 * the axes that @device currently has.
894 * Returns: (transfer container) (element-type GdkAtom):
895 * A #GList of #GdkAtom<!-- -->s, free with g_list_free().
900 gdk_device_list_axes (GdkDevice *device)
902 GdkDevicePrivate *priv;
908 for (i = 0; i < priv->axes->len; i++)
910 GdkAxisInfo axis_info;
912 axis_info = g_array_index (priv->axes, GdkAxisInfo, i);
913 axes = g_list_prepend (axes, GDK_ATOM_TO_POINTER (axis_info.label));
916 return g_list_reverse (axes);
920 * gdk_device_get_axis_value:
921 * @device: a #GdkDevice.
922 * @axes: pointer to an array of axes
923 * @axis_label: #GdkAtom with the axis label.
924 * @value: location to store the found value.
926 * Interprets an array of double as axis values for a given device,
927 * and locates the value in the array for a given axis label, as returned
928 * by gdk_device_list_axes()
930 * Returns: %TRUE if the given axis use was found, otherwise %FALSE.
935 gdk_device_get_axis_value (GdkDevice *device,
940 GdkDevicePrivate *priv;
943 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
950 for (i = 0; i < priv->axes->len; i++)
952 GdkAxisInfo axis_info;
954 axis_info = g_array_index (priv->axes, GdkAxisInfo, i);
956 if (axis_info.label != axis_label)
969 * gdk_device_get_axis:
970 * @device: a #GdkDevice
971 * @axes: pointer to an array of axes
972 * @use: the use to look for
973 * @value: location to store the found value.
975 * Interprets an array of double as axis values for a given device,
976 * and locates the value in the array for a given axis use.
978 * Return value: %TRUE if the given axis use was found, otherwise %FALSE
981 gdk_device_get_axis (GdkDevice *device,
986 GdkDevicePrivate *priv;
989 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
996 g_return_val_if_fail (priv->axes != NULL, FALSE);
998 for (i = 0; i < priv->axes->len; i++)
1000 GdkAxisInfo axis_info;
1002 axis_info = g_array_index (priv->axes, GdkAxisInfo, i);
1004 if (axis_info.use != use)
1017 get_native_grab_event_mask (GdkEventMask grab_mask)
1019 /* Similar to the above but for pointer events only */
1021 GDK_POINTER_MOTION_MASK |
1022 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1023 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1026 ~(GDK_POINTER_MOTION_HINT_MASK |
1027 GDK_BUTTON_MOTION_MASK |
1028 GDK_BUTTON1_MOTION_MASK |
1029 GDK_BUTTON2_MOTION_MASK |
1030 GDK_BUTTON3_MOTION_MASK));
1035 * @device: a #GdkDevice
1036 * @window: the #GdkWindow which will own the grab (the grab window)
1037 * @grab_ownership: specifies the grab ownership.
1038 * @owner_events: if %FALSE then all device events are reported with respect to
1039 * @window and are only reported if selected by @event_mask. If
1040 * %TRUE then pointer events for this application are reported
1041 * as normal, but pointer events outside this application are
1042 * reported with respect to @window and only if selected by
1043 * @event_mask. In either mode, unreported events are discarded.
1044 * @event_mask: specifies the event mask, which is used in accordance with
1046 * @cursor: the cursor to display while the grab is active if the device is
1047 * a pointer. If this is %NULL then the normal cursors are used for
1048 * @window and its descendants, and the cursor for @window is used
1050 * @time_: the timestamp of the event which led to this pointer grab. This
1051 * usually comes from the #GdkEvent struct, though %GDK_CURRENT_TIME
1052 * can be used if the time isn't known.
1054 * Grabs the device so that all events coming from this device are passed to
1055 * this application until the device is ungrabbed with gdk_device_ungrab(),
1056 * or the window becomes unviewable. This overrides any previous grab on the device
1059 * Device grabs are used for operations which need complete control over the
1060 * given device events (either pointer or keyboard). For example in GTK+ this
1061 * is used for Drag and Drop operations, popup menus and such.
1063 * Note that if the event mask of an X window has selected both button press
1064 * and button release events, then a button press event will cause an automatic
1065 * pointer grab until the button is released. X does this automatically since
1066 * most applications expect to receive button press and release events in pairs.
1067 * It is equivalent to a pointer grab on the window with @owner_events set to
1070 * If you set up anything at the time you take the grab that needs to be
1071 * cleaned up when the grab ends, you should handle the #GdkEventGrabBroken
1072 * events that are emitted when the grab ends unvoluntarily.
1074 * Returns: %GDK_GRAB_SUCCESS if the grab was successful.
1079 gdk_device_grab (GdkDevice *device,
1081 GdkGrabOwnership grab_ownership,
1082 gboolean owner_events,
1083 GdkEventMask event_mask,
1090 g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
1091 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
1093 if (_gdk_native_windows)
1096 native = gdk_window_get_toplevel (window);
1098 while (native->window_type == GDK_WINDOW_OFFSCREEN)
1100 native = gdk_offscreen_window_get_embedder (native);
1102 if (native == NULL ||
1103 (!_gdk_window_has_impl (native) &&
1104 !gdk_window_is_viewable (native)))
1105 return GDK_GRAB_NOT_VIEWABLE;
1107 native = gdk_window_get_toplevel (native);
1110 res = _gdk_windowing_device_grab (device,
1114 get_native_grab_event_mask (event_mask),
1119 if (res == GDK_GRAB_SUCCESS)
1121 GdkDisplay *display;
1124 display = gdk_window_get_display (window);
1125 serial = _gdk_windowing_window_get_next_serial (display);
1127 _gdk_display_add_device_grab (display,
1144 _gdk_device_reset_axes (GdkDevice *device)
1146 GdkDevicePrivate *priv;
1149 priv = device->priv;
1151 for (i = priv->axes->len - 1; i >= 0; i--)
1152 g_array_remove_index (priv->axes, i);
1154 g_object_notify (G_OBJECT (device), "n-axes");
1158 _gdk_device_add_axis (GdkDevice *device,
1165 GdkDevicePrivate *priv;
1166 GdkAxisInfo axis_info;
1169 priv = device->priv;
1171 axis_info.use = use;
1172 axis_info.label = label_atom;
1173 axis_info.min_value = min_value;
1174 axis_info.max_value = max_value;
1175 axis_info.resolution = resolution;
1181 axis_info.min_axis = 0;
1182 axis_info.max_axis = 0;
1184 case GDK_AXIS_XTILT:
1185 case GDK_AXIS_YTILT:
1186 axis_info.min_axis = -1;
1187 axis_info.max_axis = 1;
1190 axis_info.min_axis = 0;
1191 axis_info.max_axis = 1;
1195 priv->axes = g_array_append_val (priv->axes, axis_info);
1196 pos = device->priv->axes->len - 1;
1198 g_object_notify (G_OBJECT (device), "n-axes");
1204 _gdk_device_set_keys (GdkDevice *device,
1207 if (device->priv->keys)
1208 g_free (device->priv->keys);
1210 device->priv->num_keys = num_keys;
1211 device->priv->keys = g_new0 (GdkDeviceKey, num_keys);
1214 static GdkAxisInfo *
1215 find_axis_info (GArray *array,
1221 for (i = 0; i < GDK_AXIS_LAST; i++)
1223 info = &g_array_index (array, GdkAxisInfo, i);
1225 if (info->use == use)
1233 _gdk_device_get_axis_use (GdkDevice *device,
1236 GdkDevicePrivate *priv;
1239 priv = device->priv;
1241 info = g_array_index (priv->axes, GdkAxisInfo, index_);
1246 _gdk_device_translate_window_coord (GdkDevice *device,
1250 gdouble *axis_value)
1252 GdkDevicePrivate *priv;
1253 GdkAxisInfo axis_info;
1254 GdkAxisInfo *axis_info_x, *axis_info_y;
1255 gdouble device_width, device_height;
1256 gdouble x_offset, y_offset;
1257 gdouble x_scale, y_scale;
1258 gdouble x_min, y_min;
1259 gdouble x_resolution, y_resolution;
1260 gdouble device_aspect;
1261 gint window_width, window_height;
1263 priv = device->priv;
1265 if (index_ >= priv->axes->len)
1268 axis_info = g_array_index (priv->axes, GdkAxisInfo, index_);
1270 if (axis_info.use != GDK_AXIS_X &&
1271 axis_info.use != GDK_AXIS_Y)
1274 if (axis_info.use == GDK_AXIS_X)
1276 axis_info_x = &axis_info;
1277 axis_info_y = find_axis_info (priv->axes, GDK_AXIS_Y);
1281 axis_info_x = find_axis_info (priv->axes, GDK_AXIS_X);
1282 axis_info_y = &axis_info;
1285 device_width = axis_info_x->max_value - axis_info_x->min_value;
1286 device_height = axis_info_y->max_value - axis_info_y->min_value;
1288 if (device_width > 0)
1289 x_min = axis_info_x->min_value;
1292 device_width = gdk_screen_get_width (gdk_window_get_screen (window));
1296 if (device_height > 0)
1297 y_min = axis_info_y->min_value;
1300 device_height = gdk_screen_get_height (gdk_window_get_screen (window));
1304 window_width = gdk_window_get_width (window);
1305 window_height = gdk_window_get_height (window);
1307 x_resolution = axis_info_x->resolution;
1308 y_resolution = axis_info_y->resolution;
1311 * Some drivers incorrectly report the resolution of the device
1312 * as zero (in partiular linuxwacom < 0.5.3 with usb tablets).
1313 * This causes the device_aspect to become NaN and totally
1314 * breaks windowed mode. If this is the case, the best we can
1315 * do is to assume the resolution is non-zero is equal in both
1316 * directions (which is true for many devices). The absolute
1317 * value of the resolution doesn't matter since we only use the
1320 if (x_resolution == 0 || y_resolution == 0)
1326 device_aspect = (device_height * y_resolution) /
1327 (device_width * x_resolution);
1329 if (device_aspect * window_width >= window_height)
1331 /* device taller than window */
1332 x_scale = window_width / device_width;
1333 y_scale = (x_scale * x_resolution) / y_resolution;
1336 y_offset = - (device_height * y_scale - window_height) / 2;
1340 /* window taller than device */
1341 y_scale = window_height / device_height;
1342 x_scale = (y_scale * y_resolution) / x_resolution;
1345 x_offset = - (device_width * x_scale - window_width) / 2;
1350 if (axis_info.use == GDK_AXIS_X)
1351 *axis_value = x_offset + x_scale * (value - x_min);
1353 *axis_value = y_offset + y_scale * (value - y_min);
1360 _gdk_device_translate_screen_coord (GdkDevice *device,
1366 gdouble *axis_value)
1368 GdkDevicePrivate *priv = device->priv;
1369 GdkAxisInfo axis_info;
1370 gdouble axis_width, scale, offset;
1372 if (priv->mode != GDK_MODE_SCREEN)
1375 if (index_ >= priv->axes->len)
1378 axis_info = g_array_index (priv->axes, GdkAxisInfo, index_);
1380 if (axis_info.use != GDK_AXIS_X &&
1381 axis_info.use != GDK_AXIS_Y)
1384 axis_width = axis_info.max_value - axis_info.min_value;
1386 if (axis_info.use == GDK_AXIS_X)
1389 scale = gdk_screen_get_width (gdk_window_get_screen (window)) / axis_width;
1393 offset = - window_root_x - window->abs_x;
1398 scale = gdk_screen_get_height (gdk_window_get_screen (window)) / axis_width;
1402 offset = - window_root_y - window->abs_y;
1406 *axis_value = offset + scale * (value - axis_info.min_value);
1412 _gdk_device_translate_axis (GdkDevice *device,
1415 gdouble *axis_value)
1417 GdkDevicePrivate *priv;
1418 GdkAxisInfo axis_info;
1419 gdouble axis_width, out;
1421 priv = device->priv;
1423 if (index_ >= priv->axes->len)
1426 axis_info = g_array_index (priv->axes, GdkAxisInfo, index_);
1428 if (axis_info.use == GDK_AXIS_X ||
1429 axis_info.use == GDK_AXIS_Y)
1432 axis_width = axis_info.max_value - axis_info.min_value;
1433 out = (axis_info.max_axis * (value - axis_info.min_value) +
1434 axis_info.min_axis * (axis_info.max_value - value)) / axis_width;