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