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