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