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