]> Pileus Git - ~andy/gtk/blob - gdk/gdkdevice.c
Make GdkDevice parallel-implementable
[~andy/gtk] / gdk / gdkdevice.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 2009 Carlos Garnacho <carlosg@gnome.org>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "config.h"
21
22 #include "gdkdevice.h"
23
24 #include "gdkinternals.h"
25 #include "gdkdeviceprivate.h"
26 #include "gdkintl.h"
27
28
29 typedef struct _GdkDeviceKey GdkDeviceKey;
30
31 struct _GdkDeviceKey
32 {
33   guint keyval;
34   GdkModifierType modifiers;
35 };
36
37 typedef struct _GdkAxisInfo GdkAxisInfo;
38
39 struct _GdkAxisInfo
40 {
41   GdkAtom label;
42   GdkAxisUse use;
43
44   gdouble min_axis;
45   gdouble max_axis;
46
47   gdouble min_value;
48   gdouble max_value;
49   gdouble resolution;
50 };
51
52 struct _GdkDevicePrivate
53 {
54   gchar *name;
55   GdkInputSource source;
56   GdkInputMode mode;
57   gboolean has_cursor;
58   gint num_keys;
59   GdkDeviceKey *keys;
60   GdkDeviceManager *device_manager;
61   GdkDisplay *display;
62
63   /* Paired master for master,
64    * associated master for slaves
65    */
66   GdkDevice *associated;
67
68   GList *slaves;
69   GdkDeviceType type;
70   GArray *axes;
71 };
72
73 enum {
74   CHANGED,
75   LAST_SIGNAL
76 };
77
78 static guint signals [LAST_SIGNAL] = { 0 };
79
80
81 static void gdk_device_dispose      (GObject      *object);
82 static void gdk_device_set_property (GObject      *object,
83                                      guint         prop_id,
84                                      const GValue *value,
85                                      GParamSpec   *pspec);
86 static void gdk_device_get_property (GObject      *object,
87                                      guint         prop_id,
88                                      GValue       *value,
89                                      GParamSpec   *pspec);
90
91
92 G_DEFINE_ABSTRACT_TYPE (GdkDevice, gdk_device, G_TYPE_OBJECT)
93
94 enum {
95   PROP_0,
96   PROP_DISPLAY,
97   PROP_DEVICE_MANAGER,
98   PROP_NAME,
99   PROP_ASSOCIATED_DEVICE,
100   PROP_TYPE,
101   PROP_INPUT_SOURCE,
102   PROP_INPUT_MODE,
103   PROP_HAS_CURSOR,
104   PROP_N_AXES
105 };
106
107
108 static void
109 gdk_device_class_init (GdkDeviceClass *klass)
110 {
111   GObjectClass *object_class = G_OBJECT_CLASS (klass);
112
113   object_class->dispose = gdk_device_dispose;
114   object_class->set_property = gdk_device_set_property;
115   object_class->get_property = gdk_device_get_property;
116
117   /**
118    * GdkDevice:display:
119    *
120    * The #GdkDisplay the #GdkDevice pertains to.
121    *
122    * Since: 3.0
123    */
124   g_object_class_install_property (object_class,
125                                    PROP_DISPLAY,
126                                    g_param_spec_object ("display",
127                                                         P_("Device Display"),
128                                                         P_("Display which the device belongs to"),
129                                                         GDK_TYPE_DISPLAY,
130                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
131                                                         G_PARAM_STATIC_STRINGS));
132   /**
133    * GdkDevice:device-manager:
134    *
135    * The #GdkDeviceManager the #GdkDevice pertains to.
136    *
137    * Since: 3.0
138    */
139   g_object_class_install_property (object_class,
140                                    PROP_DEVICE_MANAGER,
141                                    g_param_spec_object ("device-manager",
142                                                         P_("Device manager"),
143                                                         P_("Device manager which the device belongs to"),
144                                                         GDK_TYPE_DEVICE_MANAGER,
145                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
146                                                         G_PARAM_STATIC_STRINGS));
147   /**
148    * GdkDevice:name:
149    *
150    * The device name.
151    *
152    * Since: 3.0
153    */
154   g_object_class_install_property (object_class,
155                                    PROP_NAME,
156                                    g_param_spec_string ("name",
157                                                         P_("Device name"),
158                                                         P_("Device name"),
159                                                         NULL,
160                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
161                                                         G_PARAM_STATIC_STRINGS));
162   /**
163    * GdkDevice:type:
164    *
165    * Device role in the device manager.
166    *
167    * Since: 3.0
168    */
169   g_object_class_install_property (object_class,
170                                    PROP_TYPE,
171                                    g_param_spec_enum ("type",
172                                                       P_("Device type"),
173                                                       P_("Device role in the device manager"),
174                                                       GDK_TYPE_DEVICE_TYPE,
175                                                       GDK_DEVICE_TYPE_MASTER,
176                                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
177                                                       G_PARAM_STATIC_STRINGS));
178   /**
179    * GdkDevice:associated-device:
180    *
181    * Associated pointer or keyboard with this device, if any. Devices of type #GDK_DEVICE_TYPE_MASTER
182    * always come in keyboard/pointer pairs. Other device types will have a %NULL associated device.
183    *
184    * Since: 3.0
185    */
186   g_object_class_install_property (object_class,
187                                    PROP_ASSOCIATED_DEVICE,
188                                    g_param_spec_object ("associated-device",
189                                                         P_("Associated device"),
190                                                         P_("Associated pointer or keyboard with this device"),
191                                                         GDK_TYPE_DEVICE,
192                                                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
193   /**
194    * GdkDevice:input-source:
195    *
196    * Source type for the device.
197    *
198    * Since: 3.0
199    */
200   g_object_class_install_property (object_class,
201                                    PROP_INPUT_SOURCE,
202                                    g_param_spec_enum ("input-source",
203                                                       P_("Input source"),
204                                                       P_("Source type for the device"),
205                                                       GDK_TYPE_INPUT_SOURCE,
206                                                       GDK_SOURCE_MOUSE,
207                                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
208                                                       G_PARAM_STATIC_STRINGS));
209   /**
210    * GdkDevice:input-mode:
211    *
212    * Input mode for the device.
213    *
214    * Since: 3.0
215    */
216   g_object_class_install_property (object_class,
217                                    PROP_INPUT_MODE,
218                                    g_param_spec_enum ("input-mode",
219                                                       P_("Input mode for the device"),
220                                                       P_("Input mode for the device"),
221                                                       GDK_TYPE_INPUT_MODE,
222                                                       GDK_MODE_DISABLED,
223                                                       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
224   /**
225    * GdkDevice:has-cursor:
226    *
227    * Whether the device is represented by a cursor on the screen. Devices of type
228    * %GDK_DEVICE_TYPE_MASTER will have %TRUE here.
229    *
230    * Since: 3.0
231    */
232   g_object_class_install_property (object_class,
233                                    PROP_HAS_CURSOR,
234                                    g_param_spec_boolean ("has-cursor",
235                                                          P_("Whether the device has a cursor"),
236                                                          P_("Whether there is a visible cursor following device motion"),
237                                                          FALSE,
238                                                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
239                                                          G_PARAM_STATIC_STRINGS));
240   /**
241    * GdkDevice:n-axes:
242    *
243    * Number of axes in the device.
244    *
245    * Since: 3.0
246    */
247   g_object_class_install_property (object_class,
248                                    PROP_N_AXES,
249                                    g_param_spec_uint ("n-axes",
250                                                       P_("Number of axes in the device"),
251                                                       P_("Number of axes in the device"),
252                                                       0, G_MAXUINT, 0,
253                                                       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
254
255   /**
256    * GdkDevice::changed:
257    * @device: the #GdkDevice that changed.
258    *
259    * The ::changed signal is emitted either when the #GdkDevice
260    * has changed the number of either axes or keys. For example
261    * In X this will normally happen when the slave device routing
262    * events through the master device changes (for example, user
263    * switches from the USB mouse to a tablet), in that case the
264    * master device will change to reflect the new slave device
265    * axes and keys.
266    */
267   signals[CHANGED] =
268     g_signal_new (g_intern_static_string ("changed"),
269                   G_TYPE_FROM_CLASS (object_class),
270                   G_SIGNAL_RUN_LAST,
271                   0, NULL, NULL,
272                   g_cclosure_marshal_VOID__VOID,
273                   G_TYPE_NONE, 0);
274
275   g_type_class_add_private (object_class, sizeof (GdkDevicePrivate));
276 }
277
278 static void
279 gdk_device_init (GdkDevice *device)
280 {
281   GdkDevicePrivate *priv;
282
283   device->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (device,
284                                                      GDK_TYPE_DEVICE,
285                                                      GdkDevicePrivate);
286
287   priv->axes = g_array_new (FALSE, TRUE, sizeof (GdkAxisInfo));
288 }
289
290 static void
291 gdk_device_dispose (GObject *object)
292 {
293   GdkDevicePrivate *priv;
294   GdkDevice *device;
295
296   device = GDK_DEVICE (object);
297   priv = device->priv;
298
299   if (priv->type == GDK_DEVICE_TYPE_SLAVE)
300     _gdk_device_remove_slave (priv->associated, device);
301
302   if (priv->associated)
303     {
304       _gdk_device_set_associated_device (priv->associated, NULL);
305       g_object_unref (priv->associated);
306       priv->associated = NULL;
307     }
308
309   if (priv->axes)
310     {
311       g_array_free (priv->axes, TRUE);
312       priv->axes = NULL;
313     }
314
315   g_free (priv->name);
316   g_free (priv->keys);
317
318   priv->name = NULL;
319   priv->keys = NULL;
320
321   G_OBJECT_CLASS (gdk_device_parent_class)->dispose (object);
322 }
323
324 static void
325 gdk_device_set_property (GObject      *object,
326                          guint         prop_id,
327                          const GValue *value,
328                          GParamSpec   *pspec)
329 {
330   GdkDevice *device = GDK_DEVICE (object);
331   GdkDevicePrivate *priv = device->priv;
332
333   switch (prop_id)
334     {
335     case PROP_DISPLAY:
336       priv->display = g_value_get_object (value);
337       break;
338     case PROP_DEVICE_MANAGER:
339       priv->device_manager = g_value_get_object (value);
340       break;
341     case PROP_NAME:
342       if (priv->name)
343         g_free (priv->name);
344
345       priv->name = g_value_dup_string (value);
346       break;
347     case PROP_TYPE:
348       priv->type = g_value_get_enum (value);
349       break;
350     case PROP_INPUT_SOURCE:
351       priv->source = g_value_get_enum (value);
352       break;
353     case PROP_INPUT_MODE:
354       gdk_device_set_mode (device, g_value_get_enum (value));
355       break;
356     case PROP_HAS_CURSOR:
357       priv->has_cursor = g_value_get_boolean (value);
358       break;
359     default:
360       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
361       break;
362     }
363 }
364
365 static void
366 gdk_device_get_property (GObject    *object,
367                          guint       prop_id,
368                          GValue     *value,
369                          GParamSpec *pspec)
370 {
371   GdkDevice *device = GDK_DEVICE (object);
372   GdkDevicePrivate *priv = device->priv;
373
374   switch (prop_id)
375     {
376     case PROP_DISPLAY:
377       g_value_set_object (value, priv->display);
378       break;
379     case PROP_DEVICE_MANAGER:
380       g_value_set_object (value, priv->device_manager);
381       break;
382     case PROP_ASSOCIATED_DEVICE:
383       g_value_set_object (value, priv->associated);
384       break;
385     case PROP_NAME:
386       g_value_set_string (value, priv->name);
387       break;
388     case PROP_TYPE:
389       g_value_set_enum (value, priv->type);
390       break;
391     case PROP_INPUT_SOURCE:
392       g_value_set_enum (value, priv->source);
393       break;
394     case PROP_INPUT_MODE:
395       g_value_set_enum (value, priv->mode);
396       break;
397     case PROP_HAS_CURSOR:
398       g_value_set_boolean (value, priv->has_cursor);
399       break;
400     case PROP_N_AXES:
401       g_value_set_uint (value, priv->axes->len);
402       break;
403     default:
404       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
405       break;
406     }
407 }
408
409 /**
410  * gdk_device_get_state:
411  * @device: a #GdkDevice.
412  * @window: a #GdkWindow.
413  * @axes: an array of doubles to store the values of the axes of @device in,
414  * or %NULL.
415  * @mask: location to store the modifiers, or %NULL.
416  *
417  * Gets the current state of a pointer device relative to @window.
418  */
419 void
420 gdk_device_get_state (GdkDevice       *device,
421                       GdkWindow       *window,
422                       gdouble         *axes,
423                       GdkModifierType *mask)
424 {
425   g_return_if_fail (GDK_IS_DEVICE (device));
426   g_return_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD);
427   g_return_if_fail (GDK_IS_WINDOW (window));
428
429   if (GDK_DEVICE_GET_CLASS (device)->get_state)
430     GDK_DEVICE_GET_CLASS (device)->get_state (device, window, axes, mask);
431 }
432
433 /**
434  * gdk_device_get_history:
435  * @device: a #GdkDevice
436  * @window: the window with respect to which which the event coordinates will be reported
437  * @start: starting timestamp for range of events to return
438  * @stop: ending timestamp for the range of events to return
439  * @events: (array length=n_events) (out) (transfer none): location to store a newly-allocated array of #GdkTimeCoord, or %NULL
440  * @n_events: location to store the length of @events, or %NULL
441  *
442  * Obtains the motion history for a pointer device; given a starting and
443  * ending timestamp, return all events in the motion history for
444  * the device in the given range of time. Some windowing systems
445  * do not support motion history, in which case, %FALSE will
446  * be returned. (This is not distinguishable from the case where
447  * motion history is supported and no events were found.)
448  *
449  * Return value: %TRUE if the windowing system supports motion history and
450  *  at least one event was found.
451  **/
452 gboolean
453 gdk_device_get_history (GdkDevice      *device,
454                         GdkWindow      *window,
455                         guint32         start,
456                         guint32         stop,
457                         GdkTimeCoord ***events,
458                         gint           *n_events)
459 {
460   g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
461   g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
462   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
463
464   if (n_events)
465     *n_events = 0;
466
467   if (events)
468     *events = NULL;
469
470   if (GDK_WINDOW_DESTROYED (window))
471     return FALSE;
472
473   if (!GDK_DEVICE_GET_CLASS (device)->get_history)
474     return FALSE;
475
476   return GDK_DEVICE_GET_CLASS (device)->get_history (device, window,
477                                                      start, stop,
478                                                      events, n_events);
479 }
480
481 GdkTimeCoord **
482 _gdk_device_allocate_history (GdkDevice *device,
483                               gint       n_events)
484 {
485   GdkTimeCoord **result = g_new (GdkTimeCoord *, n_events);
486   gint i;
487
488   for (i = 0; i < n_events; i++)
489     result[i] = g_malloc (sizeof (GdkTimeCoord) -
490                           sizeof (double) * (GDK_MAX_TIMECOORD_AXES - device->priv->axes->len));
491   return result;
492 }
493
494 /**
495  * gdk_device_free_history:
496  * @events: (inout) (transfer none): an array of #GdkTimeCoord.
497  * @n_events: the length of the array.
498  *
499  * Frees an array of #GdkTimeCoord that was returned by gdk_device_get_history().
500  */
501 void
502 gdk_device_free_history (GdkTimeCoord **events,
503                          gint           n_events)
504 {
505   gint i;
506
507   for (i = 0; i < n_events; i++)
508     g_free (events[i]);
509
510   g_free (events);
511 }
512
513 /**
514  * gdk_device_get_name:
515  * @device: a #GdkDevice
516  *
517  * Determines the name of the device.
518  *
519  * Return value: a name
520  *
521  * Since: 2.20
522  **/
523 const gchar *
524 gdk_device_get_name (GdkDevice *device)
525 {
526   g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
527
528   return device->priv->name;
529 }
530
531 /**
532  * gdk_device_get_has_cursor:
533  * @device: a #GdkDevice
534  *
535  * Determines whether the pointer follows device motion.
536  *
537  * Return value: %TRUE if the pointer follows device motion
538  *
539  * Since: 2.20
540  **/
541 gboolean
542 gdk_device_get_has_cursor (GdkDevice *device)
543 {
544   g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
545   g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
546
547   return device->priv->has_cursor;
548 }
549
550 /**
551  * gdk_device_get_source:
552  * @device: a #GdkDevice
553  *
554  * Determines the type of the device.
555  *
556  * Return value: a #GdkInputSource
557  *
558  * Since: 2.20
559  **/
560 GdkInputSource
561 gdk_device_get_source (GdkDevice *device)
562 {
563   g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
564
565   return device->priv->source;
566 }
567
568 /**
569  * gdk_device_set_source:
570  * @device: a #GdkDevice.
571  * @source: the source type.
572  *
573  * Sets the source type for an input device.
574  **/
575 void
576 gdk_device_set_source (GdkDevice      *device,
577                        GdkInputSource  source)
578 {
579   g_return_if_fail (GDK_IS_DEVICE (device));
580
581   device->priv->source = source;
582   g_object_notify (G_OBJECT (device), "input-source");
583 }
584
585 /**
586  * gdk_device_get_mode:
587  * @device: a #GdkDevice
588  *
589  * Determines the mode of the device.
590  *
591  * Return value: a #GdkInputSource
592  *
593  * Since: 2.20
594  **/
595 GdkInputMode
596 gdk_device_get_mode (GdkDevice *device)
597 {
598   g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
599
600   return device->priv->mode;
601 }
602
603 /**
604  * gdk_device_set_mode:
605  * @device: a #GdkDevice.
606  * @mode: the input mode.
607  *
608  * Sets a the mode of an input device. The mode controls if the
609  * device is active and whether the device's range is mapped to the
610  * entire screen or to a single window.
611  *
612  * Returns: %TRUE if the mode was successfully changed.
613  **/
614 gboolean
615 gdk_device_set_mode (GdkDevice    *device,
616                      GdkInputMode  mode)
617 {
618   g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
619
620   if (device->priv->mode == mode)
621     return TRUE;
622
623   if (mode == GDK_MODE_DISABLED &&
624       gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER)
625     return FALSE;
626
627   device->priv->mode = mode;
628   g_object_notify (G_OBJECT (device), "input-mode");
629
630   return TRUE;
631 }
632
633 /**
634  * gdk_device_get_n_keys:
635  * @device: a #GdkDevice
636  *
637  * Returns the number of keys the device currently has.
638  *
639  * Returns: the number of keys.
640  *
641  * Since: 2.24
642  **/
643 gint
644 gdk_device_get_n_keys (GdkDevice *device)
645 {
646   g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
647
648   return device->priv->num_keys;
649 }
650
651 /**
652  * gdk_device_get_key:
653  * @device: a #GdkDevice.
654  * @index_: the index of the macro button to get.
655  * @keyval: return value for the keyval.
656  * @modifiers: return value for modifiers.
657  *
658  * If @index_ has a valid keyval, this function will return %TRUE
659  * and fill in @keyval and @modifiers with the keyval settings.
660  *
661  * Returns: %TRUE if keyval is set for @index.
662  *
663  * Since: 2.20
664  **/
665 gboolean
666 gdk_device_get_key (GdkDevice       *device,
667                     guint            index_,
668                     guint           *keyval,
669                     GdkModifierType *modifiers)
670 {
671   g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
672   g_return_val_if_fail (index_ < device->priv->num_keys, FALSE);
673
674   if (!device->priv->keys[index_].keyval &&
675       !device->priv->keys[index_].modifiers)
676     return FALSE;
677
678   if (keyval)
679     *keyval = device->priv->keys[index_].keyval;
680
681   if (modifiers)
682     *modifiers = device->priv->keys[index_].modifiers;
683
684   return TRUE;
685 }
686
687 /**
688  * gdk_device_set_key:
689  * @device: a #GdkDevice
690  * @index_: the index of the macro button to set
691  * @keyval: the keyval to generate
692  * @modifiers: the modifiers to set
693  *
694  * Specifies the X key event to generate when a macro button of a device
695  * is pressed.
696  **/
697 void
698 gdk_device_set_key (GdkDevice      *device,
699                     guint           index_,
700                     guint           keyval,
701                     GdkModifierType modifiers)
702 {
703   g_return_if_fail (GDK_IS_DEVICE (device));
704   g_return_if_fail (index_ < device->priv->num_keys);
705
706   device->priv->keys[index_].keyval = keyval;
707   device->priv->keys[index_].modifiers = modifiers;
708 }
709
710 /**
711  * gdk_device_get_axis_use:
712  * @device: a pointer #GdkDevice.
713  * @index_: the index of the axis.
714  *
715  * Returns the axis use for @index_.
716  *
717  * Returns: a #GdkAxisUse specifying how the axis is used.
718  *
719  * Since: 2.20
720  **/
721 GdkAxisUse
722 gdk_device_get_axis_use (GdkDevice *device,
723                          guint      index_)
724 {
725   GdkAxisInfo *info;
726
727   g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_AXIS_IGNORE);
728   g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, GDK_AXIS_IGNORE);
729   g_return_val_if_fail (index_ < device->priv->axes->len, GDK_AXIS_IGNORE);
730
731   info = &g_array_index (device->priv->axes, GdkAxisInfo, index_);
732
733   return info->use;
734 }
735
736 /**
737  * gdk_device_set_axis_use:
738  * @device: a pointer #GdkDevice
739  * @index_: the index of the axis
740  * @use: specifies how the axis is used
741  *
742  * Specifies how an axis of a device is used.
743  **/
744 void
745 gdk_device_set_axis_use (GdkDevice   *device,
746                          guint        index_,
747                          GdkAxisUse   use)
748 {
749   GdkDevicePrivate *priv;
750   GdkAxisInfo *info;
751
752   g_return_if_fail (GDK_IS_DEVICE (device));
753   g_return_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD);
754   g_return_if_fail (index_ < device->priv->axes->len);
755
756   priv = device->priv;
757   info = &g_array_index (priv->axes, GdkAxisInfo, index_);
758   info->use = use;
759
760   switch (use)
761     {
762     case GDK_AXIS_X:
763     case GDK_AXIS_Y:
764       info->min_axis = 0;
765       info->max_axis = 0;
766       break;
767     case GDK_AXIS_XTILT:
768     case GDK_AXIS_YTILT:
769       info->min_axis = -1;
770       info->max_axis = 1;
771       break;
772     default:
773       info->min_axis = 0;
774       info->max_axis = 1;
775       break;
776     }
777 }
778
779 /**
780  * gdk_device_get_display:
781  * @device: a #GdkDevice
782  *
783  * Returns the #GdkDisplay to which @device pertains.
784  *
785  * Returns: (transfer none): a #GdkDisplay. This memory is owned
786  *          by GTK+, and must not be freed or unreffed.
787  *
788  * Since: 3.0
789  **/
790 GdkDisplay *
791 gdk_device_get_display (GdkDevice *device)
792 {
793   GdkDevicePrivate *priv;
794
795   g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
796
797   priv = device->priv;
798
799   return priv->display;
800 }
801
802 /**
803  * gdk_device_get_associated_device:
804  * @device: a #GdkDevice
805  *
806  * Returns the associated device to @device, if @device is of type
807  * %GDK_DEVICE_TYPE_MASTER, it will return the paired pointer or
808  * keyboard.
809  *
810  * If @device is of type %GDK_DEVICE_TYPE_SLAVE, it will return
811  * the master device to which @device is attached to.
812  *
813  * If @device is of type %GDK_DEVICE_TYPE_FLOATING, %NULL will be
814  * returned, as there is no associated device.
815  *
816  * Returns: (transfer none): The associated device, or %NULL
817  *
818  * Since: 3.0
819  **/
820 GdkDevice *
821 gdk_device_get_associated_device (GdkDevice *device)
822 {
823   GdkDevicePrivate *priv;
824
825   g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
826
827   priv = device->priv;
828
829   return priv->associated;
830 }
831
832 static void
833 _gdk_device_set_device_type (GdkDevice     *device,
834                              GdkDeviceType  type)
835 {
836   GdkDevicePrivate *priv;
837
838   priv = device->priv;
839
840   if (priv->type != type)
841     {
842       priv->type = type;
843
844       g_object_notify (G_OBJECT (device), "type");
845     }
846 }
847
848 void
849 _gdk_device_set_associated_device (GdkDevice *device,
850                                    GdkDevice *associated)
851 {
852   GdkDevicePrivate *priv;
853
854   g_return_if_fail (GDK_IS_DEVICE (device));
855   g_return_if_fail (associated == NULL || GDK_IS_DEVICE (associated));
856
857   priv = device->priv;
858
859   if (priv->associated == associated)
860     return;
861
862   if (priv->associated)
863     {
864       g_object_unref (priv->associated);
865       priv->associated = NULL;
866     }
867
868   if (associated)
869     priv->associated = g_object_ref (associated);
870
871   if (priv->type != GDK_DEVICE_TYPE_MASTER)
872     {
873       if (priv->associated)
874         _gdk_device_set_device_type (device, GDK_DEVICE_TYPE_SLAVE);
875       else
876         _gdk_device_set_device_type (device, GDK_DEVICE_TYPE_FLOATING);
877     }
878 }
879
880 /**
881  * gdk_device_list_slave_devices:
882  * @device: a #GdkDevice
883  *
884  * If the device if of type %GDK_DEVICE_TYPE_MASTER, it will return
885  * the list of slave devices attached to it, otherwise it will return
886  * %NULL
887  *
888  * Returns: (transfer container): the list of slave devices, or %NULL. The
889  *          list must be freed with g_list_free(), the contents of the list
890  *          are owned by GTK+ and should not be freed.
891  **/
892 GList *
893 gdk_device_list_slave_devices (GdkDevice *device)
894 {
895   GdkDevicePrivate *priv;
896
897   g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
898   g_return_val_if_fail (gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_MASTER, NULL);
899
900   priv = device->priv;
901
902   return g_list_copy (priv->slaves);
903 }
904
905 void
906 _gdk_device_add_slave (GdkDevice *device,
907                        GdkDevice *slave)
908 {
909   GdkDevicePrivate *priv;
910
911   g_return_if_fail (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER);
912   g_return_if_fail (gdk_device_get_device_type (slave) != GDK_DEVICE_TYPE_MASTER);
913
914   priv = device->priv;
915
916   if (!g_list_find (priv->slaves, slave))
917     priv->slaves = g_list_prepend (priv->slaves, slave);
918 }
919
920 void
921 _gdk_device_remove_slave (GdkDevice *device,
922                           GdkDevice *slave)
923 {
924   GdkDevicePrivate *priv;
925   GList *elem;
926
927   g_return_if_fail (gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_MASTER);
928   g_return_if_fail (gdk_device_get_device_type (slave) != GDK_DEVICE_TYPE_MASTER);
929
930   priv = device->priv;
931   elem = g_list_find (priv->slaves, slave);
932
933   if (!elem)
934     return;
935
936   priv->slaves = g_list_delete_link (priv->slaves, elem);
937 }
938
939 /**
940  * gdk_device_get_device_type:
941  * @device: a #GdkDevice
942  *
943  * Returns the device type for @device.
944  *
945  * Returns: the #GdkDeviceType for @device.
946  *
947  * Since: 3.0
948  **/
949 GdkDeviceType
950 gdk_device_get_device_type (GdkDevice *device)
951 {
952   GdkDevicePrivate *priv;
953
954   g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_DEVICE_TYPE_MASTER);
955
956   priv = device->priv;
957
958   return priv->type;
959 }
960
961 /**
962  * gdk_device_get_n_axes:
963  * @device: a pointer #GdkDevice
964  *
965  * Returns the number of axes the device currently has.
966  *
967  * Returns: the number of axes.
968  *
969  * Since: 3.0
970  **/
971 gint
972 gdk_device_get_n_axes (GdkDevice *device)
973 {
974   g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
975   g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, 0);
976
977   return device->priv->axes->len;
978 }
979
980 /**
981  * gdk_device_list_axes:
982  * @device: a pointer #GdkDevice
983  *
984  * Returns a #GList of #GdkAtom<!-- -->s, containing the labels for
985  * the axes that @device currently has.
986  *
987  * Returns: (transfer container) (element-type GdkAtom):
988  *     A #GList of #GdkAtom<!-- -->s, free with g_list_free().
989  *
990  * Since: 3.0
991  **/
992 GList *
993 gdk_device_list_axes (GdkDevice *device)
994 {
995   GdkDevicePrivate *priv;
996   GList *axes = NULL;
997   gint i;
998
999   g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
1000   g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, NULL);
1001
1002   priv = device->priv;
1003
1004   for (i = 0; i < priv->axes->len; i++)
1005     {
1006       GdkAxisInfo axis_info;
1007
1008       axis_info = g_array_index (priv->axes, GdkAxisInfo, i);
1009       axes = g_list_prepend (axes, GDK_ATOM_TO_POINTER (axis_info.label));
1010     }
1011
1012   return g_list_reverse (axes);
1013 }
1014
1015 /**
1016  * gdk_device_get_axis_value:
1017  * @device: a pointer #GdkDevice.
1018  * @axes: pointer to an array of axes
1019  * @axis_label: #GdkAtom with the axis label.
1020  * @value: location to store the found value.
1021  *
1022  * Interprets an array of double as axis values for a given device,
1023  * and locates the value in the array for a given axis label, as returned
1024  * by gdk_device_list_axes()
1025  *
1026  * Returns: %TRUE if the given axis use was found, otherwise %FALSE.
1027  *
1028  * Since: 3.0
1029  **/
1030 gboolean
1031 gdk_device_get_axis_value (GdkDevice *device,
1032                            gdouble   *axes,
1033                            GdkAtom    axis_label,
1034                            gdouble   *value)
1035 {
1036   GdkDevicePrivate *priv;
1037   gint i;
1038
1039   g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
1040   g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
1041
1042   if (axes == NULL)
1043     return FALSE;
1044
1045   priv = device->priv;
1046
1047   for (i = 0; i < priv->axes->len; i++)
1048     {
1049       GdkAxisInfo axis_info;
1050
1051       axis_info = g_array_index (priv->axes, GdkAxisInfo, i);
1052
1053       if (axis_info.label != axis_label)
1054         continue;
1055
1056       if (value)
1057         *value = axes[i];
1058
1059       return TRUE;
1060     }
1061
1062   return FALSE;
1063 }
1064
1065 /**
1066  * gdk_device_get_axis:
1067  * @device: a #GdkDevice
1068  * @axes: pointer to an array of axes
1069  * @use: the use to look for
1070  * @value: location to store the found value.
1071  *
1072  * Interprets an array of double as axis values for a given device,
1073  * and locates the value in the array for a given axis use.
1074  *
1075  * Return value: %TRUE if the given axis use was found, otherwise %FALSE
1076  **/
1077 gboolean
1078 gdk_device_get_axis (GdkDevice  *device,
1079                      gdouble    *axes,
1080                      GdkAxisUse  use,
1081                      gdouble    *value)
1082 {
1083   GdkDevicePrivate *priv;
1084   gint i;
1085
1086   g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
1087   g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
1088
1089   if (axes == NULL)
1090     return FALSE;
1091
1092   priv = device->priv;
1093
1094   g_return_val_if_fail (priv->axes != NULL, FALSE);
1095
1096   for (i = 0; i < priv->axes->len; i++)
1097     {
1098       GdkAxisInfo axis_info;
1099
1100       axis_info = g_array_index (priv->axes, GdkAxisInfo, i);
1101
1102       if (axis_info.use != use)
1103         continue;
1104
1105       if (value)
1106         *value = axes[i];
1107
1108       return TRUE;
1109     }
1110
1111   return FALSE;
1112 }
1113
1114 static GdkEventMask
1115 get_native_grab_event_mask (GdkEventMask grab_mask)
1116 {
1117   /* Similar to the above but for pointer events only */
1118   return
1119     GDK_POINTER_MOTION_MASK |
1120     GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1121     GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1122     GDK_SCROLL_MASK |
1123     (grab_mask &
1124      ~(GDK_POINTER_MOTION_HINT_MASK |
1125        GDK_BUTTON_MOTION_MASK |
1126        GDK_BUTTON1_MOTION_MASK |
1127        GDK_BUTTON2_MOTION_MASK |
1128        GDK_BUTTON3_MOTION_MASK));
1129 }
1130
1131 /**
1132  * gdk_device_grab:
1133  * @device: a #GdkDevice
1134  * @window: the #GdkWindow which will own the grab (the grab window)
1135  * @grab_ownership: specifies the grab ownership.
1136  * @owner_events: if %FALSE then all device events are reported with respect to
1137  *                @window and are only reported if selected by @event_mask. If
1138  *                %TRUE then pointer events for this application are reported
1139  *                as normal, but pointer events outside this application are
1140  *                reported with respect to @window and only if selected by
1141  *                @event_mask. In either mode, unreported events are discarded.
1142  * @event_mask: specifies the event mask, which is used in accordance with
1143  *              @owner_events.
1144  * @cursor: the cursor to display while the grab is active if the device is
1145  *          a pointer. If this is %NULL then the normal cursors are used for
1146  *          @window and its descendants, and the cursor for @window is used
1147  *          elsewhere.
1148  * @time_: the timestamp of the event which led to this pointer grab. This
1149  *         usually comes from the #GdkEvent struct, though %GDK_CURRENT_TIME
1150  *         can be used if the time isn't known.
1151  *
1152  * Grabs the device so that all events coming from this device are passed to
1153  * this application until the device is ungrabbed with gdk_device_ungrab(),
1154  * or the window becomes unviewable. This overrides any previous grab on the device
1155  * by this client.
1156  *
1157  * Device grabs are used for operations which need complete control over the
1158  * given device events (either pointer or keyboard). For example in GTK+ this
1159  * is used for Drag and Drop operations, popup menus and such.
1160  *
1161  * Note that if the event mask of an X window has selected both button press
1162  * and button release events, then a button press event will cause an automatic
1163  * pointer grab until the button is released. X does this automatically since
1164  * most applications expect to receive button press and release events in pairs.
1165  * It is equivalent to a pointer grab on the window with @owner_events set to
1166  * %TRUE.
1167  *
1168  * If you set up anything at the time you take the grab that needs to be
1169  * cleaned up when the grab ends, you should handle the #GdkEventGrabBroken
1170  * events that are emitted when the grab ends unvoluntarily.
1171  *
1172  * Returns: %GDK_GRAB_SUCCESS if the grab was successful.
1173  *
1174  * Since: 3.0
1175  **/
1176 GdkGrabStatus
1177 gdk_device_grab (GdkDevice        *device,
1178                  GdkWindow        *window,
1179                  GdkGrabOwnership  grab_ownership,
1180                  gboolean          owner_events,
1181                  GdkEventMask      event_mask,
1182                  GdkCursor        *cursor,
1183                  guint32           time_)
1184 {
1185   GdkGrabStatus res;
1186   GdkWindow *native;
1187
1188   g_return_val_if_fail (GDK_IS_DEVICE (device), GDK_GRAB_SUCCESS);
1189   g_return_val_if_fail (GDK_IS_WINDOW (window), GDK_GRAB_SUCCESS);
1190
1191   if (_gdk_native_windows)
1192     native = window;
1193   else
1194     native = gdk_window_get_toplevel (window);
1195
1196   while (native->window_type == GDK_WINDOW_OFFSCREEN)
1197     {
1198       native = gdk_offscreen_window_get_embedder (native);
1199
1200       if (native == NULL ||
1201           (!_gdk_window_has_impl (native) &&
1202            !gdk_window_is_viewable (native)))
1203         return GDK_GRAB_NOT_VIEWABLE;
1204
1205       native = gdk_window_get_toplevel (native);
1206     }
1207
1208   if (native == NULL || GDK_WINDOW_DESTROYED (native))
1209     return GDK_GRAB_NOT_VIEWABLE;
1210
1211   res = GDK_DEVICE_GET_CLASS (device)->grab (device,
1212                                              native,
1213                                              owner_events,
1214                                              get_native_grab_event_mask (event_mask),
1215                                              NULL,
1216                                              cursor,
1217                                              time_);
1218
1219   if (res == GDK_GRAB_SUCCESS)
1220     {
1221       GdkDisplay *display;
1222       gulong serial;
1223
1224       display = gdk_window_get_display (window);
1225       serial = _gdk_windowing_window_get_next_serial (display);
1226
1227       _gdk_display_add_device_grab (display,
1228                                     device,
1229                                     window,
1230                                     native,
1231                                     grab_ownership,
1232                                     owner_events,
1233                                     event_mask,
1234                                     serial,
1235                                     time_,
1236                                     FALSE);
1237     }
1238
1239   return res;
1240 }
1241
1242 /**
1243  * gdk_device_ungrab:
1244  * @device: a #GdkDevice
1245  * @time_: a timestap (e.g. %GDK_CURRENT_TIME).
1246  *
1247  * Release any grab on @device.
1248  *
1249  * Since: 3.0
1250  */
1251 void
1252 gdk_device_ungrab (GdkDevice  *device,
1253                    guint32     time_)
1254 {
1255   g_return_if_fail (GDK_IS_DEVICE (device));
1256
1257   GDK_DEVICE_GET_CLASS (device)->ungrab (device, time_);
1258 }
1259
1260 /* Private API */
1261 void
1262 _gdk_device_reset_axes (GdkDevice *device)
1263 {
1264   GdkDevicePrivate *priv;
1265   gint i;
1266
1267   priv = device->priv;
1268
1269   for (i = priv->axes->len - 1; i >= 0; i--)
1270     g_array_remove_index (priv->axes, i);
1271
1272   g_object_notify (G_OBJECT (device), "n-axes");
1273 }
1274
1275 guint
1276 _gdk_device_add_axis (GdkDevice   *device,
1277                       GdkAtom      label_atom,
1278                       GdkAxisUse   use,
1279                       gdouble      min_value,
1280                       gdouble      max_value,
1281                       gdouble      resolution)
1282 {
1283   GdkDevicePrivate *priv;
1284   GdkAxisInfo axis_info;
1285   guint pos;
1286
1287   priv = device->priv;
1288
1289   axis_info.use = use;
1290   axis_info.label = label_atom;
1291   axis_info.min_value = min_value;
1292   axis_info.max_value = max_value;
1293   axis_info.resolution = resolution;
1294
1295   switch (use)
1296     {
1297     case GDK_AXIS_X:
1298     case GDK_AXIS_Y:
1299       axis_info.min_axis = 0;
1300       axis_info.max_axis = 0;
1301       break;
1302     case GDK_AXIS_XTILT:
1303     case GDK_AXIS_YTILT:
1304       axis_info.min_axis = -1;
1305       axis_info.max_axis = 1;
1306       break;
1307     default:
1308       axis_info.min_axis = 0;
1309       axis_info.max_axis = 1;
1310       break;
1311     }
1312
1313   priv->axes = g_array_append_val (priv->axes, axis_info);
1314   pos = device->priv->axes->len - 1;
1315
1316   g_object_notify (G_OBJECT (device), "n-axes");
1317
1318   return pos;
1319 }
1320
1321 void
1322 _gdk_device_set_keys (GdkDevice *device,
1323                       guint      num_keys)
1324 {
1325   if (device->priv->keys)
1326     g_free (device->priv->keys);
1327
1328   device->priv->num_keys = num_keys;
1329   device->priv->keys = g_new0 (GdkDeviceKey, num_keys);
1330 }
1331
1332 static GdkAxisInfo *
1333 find_axis_info (GArray     *array,
1334                 GdkAxisUse  use)
1335 {
1336   GdkAxisInfo *info;
1337   gint i;
1338
1339   for (i = 0; i < GDK_AXIS_LAST; i++)
1340     {
1341       info = &g_array_index (array, GdkAxisInfo, i);
1342
1343       if (info->use == use)
1344         return info;
1345     }
1346
1347   return NULL;
1348 }
1349
1350 GdkAxisUse
1351 _gdk_device_get_axis_use (GdkDevice *device,
1352                           guint      index_)
1353 {
1354   GdkDevicePrivate *priv;
1355   GdkAxisInfo info;
1356
1357   priv = device->priv;
1358
1359   info = g_array_index (priv->axes, GdkAxisInfo, index_);
1360   return info.use;
1361 }
1362
1363 gboolean
1364 _gdk_device_translate_window_coord (GdkDevice *device,
1365                                     GdkWindow *window,
1366                                     guint      index_,
1367                                     gdouble    value,
1368                                     gdouble   *axis_value)
1369 {
1370   GdkDevicePrivate *priv;
1371   GdkAxisInfo axis_info;
1372   GdkAxisInfo *axis_info_x, *axis_info_y;
1373   gdouble device_width, device_height;
1374   gdouble x_offset, y_offset;
1375   gdouble x_scale, y_scale;
1376   gdouble x_min, y_min;
1377   gdouble x_resolution, y_resolution;
1378   gdouble device_aspect;
1379   gint window_width, window_height;
1380
1381   priv = device->priv;
1382
1383   if (index_ >= priv->axes->len)
1384     return FALSE;
1385
1386   axis_info = g_array_index (priv->axes, GdkAxisInfo, index_);
1387
1388   if (axis_info.use != GDK_AXIS_X &&
1389       axis_info.use != GDK_AXIS_Y)
1390     return FALSE;
1391
1392   if (axis_info.use == GDK_AXIS_X)
1393     {
1394       axis_info_x = &axis_info;
1395       axis_info_y = find_axis_info (priv->axes, GDK_AXIS_Y);
1396     }
1397   else
1398     {
1399       axis_info_x = find_axis_info (priv->axes, GDK_AXIS_X);
1400       axis_info_y = &axis_info;
1401     }
1402
1403   device_width = axis_info_x->max_value - axis_info_x->min_value;
1404   device_height = axis_info_y->max_value - axis_info_y->min_value;
1405
1406   if (device_width > 0)
1407     x_min = axis_info_x->min_value;
1408   else
1409     {
1410       device_width = gdk_screen_get_width (gdk_window_get_screen (window));
1411       x_min = 0;
1412     }
1413
1414   if (device_height > 0)
1415     y_min = axis_info_y->min_value;
1416   else
1417     {
1418       device_height = gdk_screen_get_height (gdk_window_get_screen (window));
1419       y_min = 0;
1420     }
1421
1422   window_width = gdk_window_get_width (window);
1423   window_height = gdk_window_get_height (window);
1424
1425   x_resolution = axis_info_x->resolution;
1426   y_resolution = axis_info_y->resolution;
1427
1428   /*
1429    * Some drivers incorrectly report the resolution of the device
1430    * as zero (in partiular linuxwacom < 0.5.3 with usb tablets).
1431    * This causes the device_aspect to become NaN and totally
1432    * breaks windowed mode.  If this is the case, the best we can
1433    * do is to assume the resolution is non-zero is equal in both
1434    * directions (which is true for many devices).  The absolute
1435    * value of the resolution doesn't matter since we only use the
1436    * ratio.
1437    */
1438   if (x_resolution == 0 || y_resolution == 0)
1439     {
1440       x_resolution = 1;
1441       y_resolution = 1;
1442     }
1443
1444   device_aspect = (device_height * y_resolution) /
1445     (device_width * x_resolution);
1446
1447   if (device_aspect * window_width >= window_height)
1448     {
1449       /* device taller than window */
1450       x_scale = window_width / device_width;
1451       y_scale = (x_scale * x_resolution) / y_resolution;
1452
1453       x_offset = 0;
1454       y_offset = - (device_height * y_scale - window_height) / 2;
1455     }
1456   else
1457     {
1458       /* window taller than device */
1459       y_scale = window_height / device_height;
1460       x_scale = (y_scale * y_resolution) / x_resolution;
1461
1462       y_offset = 0;
1463       x_offset = - (device_width * x_scale - window_width) / 2;
1464     }
1465
1466   if (axis_value)
1467     {
1468       if (axis_info.use == GDK_AXIS_X)
1469         *axis_value = x_offset + x_scale * (value - x_min);
1470       else
1471         *axis_value = y_offset + y_scale * (value - y_min);
1472     }
1473
1474   return TRUE;
1475 }
1476
1477 gboolean
1478 _gdk_device_translate_screen_coord (GdkDevice *device,
1479                                     GdkWindow *window,
1480                                     gint       window_root_x,
1481                                     gint       window_root_y,
1482                                     guint      index_,
1483                                     gdouble    value,
1484                                     gdouble   *axis_value)
1485 {
1486   GdkDevicePrivate *priv = device->priv;
1487   GdkAxisInfo axis_info;
1488   gdouble axis_width, scale, offset;
1489
1490   if (priv->mode != GDK_MODE_SCREEN)
1491     return FALSE;
1492
1493   if (index_ >= priv->axes->len)
1494     return FALSE;
1495
1496   axis_info = g_array_index (priv->axes, GdkAxisInfo, index_);
1497
1498   if (axis_info.use != GDK_AXIS_X &&
1499       axis_info.use != GDK_AXIS_Y)
1500     return FALSE;
1501
1502   axis_width = axis_info.max_value - axis_info.min_value;
1503
1504   if (axis_info.use == GDK_AXIS_X)
1505     {
1506       if (axis_width > 0)
1507         scale = gdk_screen_get_width (gdk_window_get_screen (window)) / axis_width;
1508       else
1509         scale = 1;
1510
1511       offset = - window_root_x - window->abs_x;
1512     }
1513   else
1514     {
1515       if (axis_width > 0)
1516         scale = gdk_screen_get_height (gdk_window_get_screen (window)) / axis_width;
1517       else
1518         scale = 1;
1519
1520       offset = - window_root_y - window->abs_y;
1521     }
1522
1523   if (axis_value)
1524     *axis_value = offset + scale * (value - axis_info.min_value);
1525
1526   return TRUE;
1527 }
1528
1529 gboolean
1530 _gdk_device_translate_axis (GdkDevice *device,
1531                             guint      index_,
1532                             gdouble    value,
1533                             gdouble   *axis_value)
1534 {
1535   GdkDevicePrivate *priv;
1536   GdkAxisInfo axis_info;
1537   gdouble axis_width, out;
1538
1539   priv = device->priv;
1540
1541   if (index_ >= priv->axes->len)
1542     return FALSE;
1543
1544   axis_info = g_array_index (priv->axes, GdkAxisInfo, index_);
1545
1546   if (axis_info.use == GDK_AXIS_X ||
1547       axis_info.use == GDK_AXIS_Y)
1548     return FALSE;
1549
1550   axis_width = axis_info.max_value - axis_info.min_value;
1551   out = (axis_info.max_axis * (value - axis_info.min_value) +
1552          axis_info.min_axis * (axis_info.max_value - value)) / axis_width;
1553
1554   if (axis_value)
1555     *axis_value = out;
1556
1557   return TRUE;
1558 }
1559