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