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