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