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