]> Pileus Git - ~andy/gtk/blob - gdk/gdkdevicemanager.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gdk / gdkdevicemanager.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, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "config.h"
19
20 #include "gdkdevicemanagerprivate.h"
21 #include "gdkdisplay.h"
22 #include "gdkintl.h"
23
24
25 /**
26  * SECTION:gdkdevicemanager
27  * @Short_description: Functions for handling input devices
28  * @Title: GdkDeviceManager
29  * @See_also: #GdkDevice, #GdkEvent
30  *
31  * In addition to a single pointer and keyboard for user interface input,
32  * GDK contains support for a variety of input devices, including graphics
33  * tablets, touchscreens and multiple pointers/keyboards interacting
34  * simultaneously with the user interface. Such input devices often have
35  * additional features, such as sub-pixel positioning information and
36  * additional device-dependent information.
37  *
38  * In order to query the device hierarchy and be aware of changes in the
39  * device hierarchy (such as virtual devices being created or removed, or
40  * physical devices being plugged or unplugged), GDK provides
41  * #GdkDeviceManager.
42  *
43  * By default, and if the platform supports it, GDK is aware of multiple
44  * keyboard/pointer pairs and multitouch devices. This behavior can be
45  * changed by calling gdk_disable_multidevice() before gdk_display_open().
46  * There should rarely be a need to do that though, since GDK defaults
47  * to a compatibility mode in which it will emit just one enter/leave
48  * event pair for all devices on a window. To enable per-device
49  * enter/leave events and other multi-pointer interaction features,
50  * gdk_window_set_support_multidevice() must be called on
51  * #GdkWindows (or gtk_widget_set_support_multidevice() on widgets).
52  * window. See the gdk_window_set_support_multidevice() documentation
53  * for more information.
54  *
55  * On X11, multi-device support is implemented through XInput 2.
56  * Unless gdk_disable_multidevice() is called, the XInput 2
57  * #GdkDeviceManager implementation will be used as the input source.
58  * Otherwise either the core or XInput 1 implementations will be used.
59  *
60  * For simple applications that don't have any special interest in
61  * input devices, the so-called <firstterm>client pointer</firstterm>
62  * provides a reasonable approximation to a simple setup with a single
63  * pointer and keyboard. The device that has been set as the client
64  * pointer can be accessed via gdk_device_manager_get_client_pointer().
65  *
66  * Conceptually, in multidevice mode there are 2 device types. Virtual
67  * devices (or master devices) are represented by the pointer cursors
68  * and keyboard foci that are seen on the screen. Physical devices (or
69  * slave devices) represent the hardware that is controlling the virtual
70  * devices, and thus have no visible cursor on the screen.
71  *
72  * Virtual devices are always paired, so there is a keyboard device for every
73  * pointer device. Associations between devices may be inspected through
74  * gdk_device_get_associated_device().
75  *
76  * There may be several virtual devices, and several physical devices could
77  * be controlling each of these virtual devices. Physical devices may also
78  * be "floating", which means they are not attached to any virtual device.
79  *
80  * <example><title>Master and slave devices</title>
81  * <screen>
82  * carlos@sacarino:~$ xinput list
83  * ⎡ Virtual core pointer                          id=2    [master pointer  (3)]
84  * ⎜   ↳ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
85  * ⎜   ↳ Wacom ISDv4 E6 Pen stylus                 id=10   [slave  pointer  (2)]
86  * ⎜   ↳ Wacom ISDv4 E6 Finger touch               id=11   [slave  pointer  (2)]
87  * ⎜   ↳ SynPS/2 Synaptics TouchPad                id=13   [slave  pointer  (2)]
88  * ⎜   ↳ TPPS/2 IBM TrackPoint                     id=14   [slave  pointer  (2)]
89  * ⎜   ↳ Wacom ISDv4 E6 Pen eraser                 id=16   [slave  pointer  (2)]
90  * ⎣ Virtual core keyboard                         id=3    [master keyboard (2)]
91  *     ↳ Virtual core XTEST keyboard               id=5    [slave  keyboard (3)]
92  *     ↳ Power Button                              id=6    [slave  keyboard (3)]
93  *     ↳ Video Bus                                 id=7    [slave  keyboard (3)]
94  *     ↳ Sleep Button                              id=8    [slave  keyboard (3)]
95  *     ↳ Integrated Camera                         id=9    [slave  keyboard (3)]
96  *     ↳ AT Translated Set 2 keyboard              id=12   [slave  keyboard (3)]
97  *     ↳ ThinkPad Extra Buttons                    id=15   [slave  keyboard (3)]
98  * </screen>
99  * </example>
100  *
101  * By default, GDK will automatically listen for events coming from all
102  * master devices, setting the #GdkDevice for all events coming from input
103  * devices. Events containing device information are #GDK_MOTION_NOTIFY,
104  * #GDK_BUTTON_PRESS, #GDK_2BUTTON_PRESS, #GDK_3BUTTON_PRESS,
105  * #GDK_BUTTON_RELEASE, #GDK_SCROLL, #GDK_KEY_PRESS, #GDK_KEY_RELEASE,
106  * #GDK_ENTER_NOTIFY, #GDK_LEAVE_NOTIFY, #GDK_FOCUS_CHANGE,
107  * #GDK_PROXIMITY_IN, #GDK_PROXIMITY_OUT, #GDK_DRAG_ENTER, #GDK_DRAG_LEAVE,
108  * #GDK_DRAG_MOTION, #GDK_DRAG_STATUS, #GDK_DROP_START, #GDK_DROP_FINISHED
109  * and #GDK_GRAB_BROKEN. When dealing with an event on a master device,
110  * it is possible to get the source (slave) device that the event originated
111  * from via gdk_event_get_source_device().
112  *
113  * In order to listen for events coming from devices
114  * other than a virtual device, gdk_window_set_device_events() must be
115  * called. Generally, this function can be used to modify the event mask
116  * for any given device.
117  *
118  * Input devices may also provide additional information besides X/Y.
119  * For example, graphics tablets may also provide pressure and X/Y tilt
120  * information. This information is device-dependent, and may be
121  * queried through gdk_device_get_axis(). In multidevice mode, virtual
122  * devices will change axes in order to always represent the physical
123  * device that is routing events through it. Whenever the physical device
124  * changes, the #GdkDevice:n-axes property will be notified, and
125  * gdk_device_list_axes() will return the new device axes.
126  *
127  * Devices may also have associated <firstterm>keys</firstterm> or
128  * macro buttons. Such keys can be globally set to map into normal X
129  * keyboard events. The mapping is set using gdk_device_set_key().
130  */
131
132 static void gdk_device_manager_set_property (GObject      *object,
133                                              guint         prop_id,
134                                              const GValue *value,
135                                              GParamSpec   *pspec);
136 static void gdk_device_manager_get_property (GObject      *object,
137                                              guint         prop_id,
138                                              GValue       *value,
139                                              GParamSpec   *pspec);
140
141
142 G_DEFINE_ABSTRACT_TYPE (GdkDeviceManager, gdk_device_manager, G_TYPE_OBJECT)
143
144 enum {
145   PROP_0,
146   PROP_DISPLAY
147 };
148
149 enum {
150   DEVICE_ADDED,
151   DEVICE_REMOVED,
152   DEVICE_CHANGED,
153   LAST_SIGNAL
154 };
155
156 static guint signals [LAST_SIGNAL] = { 0 };
157
158
159 static void
160 gdk_device_manager_class_init (GdkDeviceManagerClass *klass)
161 {
162   GObjectClass *object_class = G_OBJECT_CLASS (klass);
163
164   object_class->set_property = gdk_device_manager_set_property;
165   object_class->get_property = gdk_device_manager_get_property;
166
167   g_object_class_install_property (object_class,
168                                    PROP_DISPLAY,
169                                    g_param_spec_object ("display",
170                                                         P_("Display"),
171                                                         P_("Display for the device manager"),
172                                                         GDK_TYPE_DISPLAY,
173                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
174                                                         G_PARAM_STATIC_STRINGS));
175
176   /**
177    * GdkDeviceManager::device-added:
178    * @device_manager: the object on which the signal is emitted
179    * @device: the newly added #GdkDevice.
180    *
181    * The ::device-added signal is emitted either when a new master
182    * pointer is created, or when a slave (Hardware) input device
183    * is plugged in.
184    */
185   signals [DEVICE_ADDED] =
186     g_signal_new (g_intern_static_string ("device-added"),
187                   G_TYPE_FROM_CLASS (klass),
188                   G_SIGNAL_RUN_LAST,
189                   G_STRUCT_OFFSET (GdkDeviceManagerClass, device_added),
190                   NULL, NULL,
191                   g_cclosure_marshal_VOID__OBJECT,
192                   G_TYPE_NONE, 1,
193                   GDK_TYPE_DEVICE);
194
195   /**
196    * GdkDeviceManager::device-removed:
197    * @device_manager: the object on which the signal is emitted
198    * @device: the just removed #GdkDevice.
199    *
200    * The ::device-removed signal is emitted either when a master
201    * pointer is removed, or when a slave (Hardware) input device
202    * is unplugged.
203    */
204   signals [DEVICE_REMOVED] =
205     g_signal_new (g_intern_static_string ("device-removed"),
206                   G_TYPE_FROM_CLASS (klass),
207                   G_SIGNAL_RUN_LAST,
208                   G_STRUCT_OFFSET (GdkDeviceManagerClass, device_removed),
209                   NULL, NULL,
210                   g_cclosure_marshal_VOID__OBJECT,
211                   G_TYPE_NONE, 1,
212                   GDK_TYPE_DEVICE);
213
214   /**
215    * GdkDeviceManager::device-changed:
216    * @device_manager: the object on which the signal is emitted
217    * @device: the #GdkDevice that changed.
218    *
219    * The ::device-changed signal is emitted whenever a device
220    * has changed in the hierarchy, either slave devices being
221    * disconnected from their master device or connected to
222    * another one, or master devices being added or removed
223    * a slave device.
224    *
225    * If a slave device is detached from all master devices
226    * (gdk_device_get_associated_device() returns %NULL), its
227    * #GdkDeviceType will change to %GDK_DEVICE_TYPE_FLOATING,
228    * if it's attached, it will change to %GDK_DEVICE_TYPE_SLAVE.
229    */
230   signals [DEVICE_CHANGED] =
231     g_signal_new (g_intern_static_string ("device-changed"),
232                   G_TYPE_FROM_CLASS (klass),
233                   G_SIGNAL_RUN_LAST,
234                   G_STRUCT_OFFSET (GdkDeviceManagerClass, device_changed),
235                   NULL, NULL,
236                   g_cclosure_marshal_VOID__OBJECT,
237                   G_TYPE_NONE, 1,
238                   GDK_TYPE_DEVICE);
239 }
240
241 static void
242 gdk_device_manager_init (GdkDeviceManager *device_manager)
243 {
244 }
245
246 static void
247 gdk_device_manager_set_property (GObject      *object,
248                                  guint         prop_id,
249                                  const GValue *value,
250                                  GParamSpec   *pspec)
251 {
252   switch (prop_id)
253     {
254     case PROP_DISPLAY:
255       GDK_DEVICE_MANAGER (object)->display = g_value_get_object (value);
256       break;
257     default:
258       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
259       break;
260     }
261 }
262
263 static void
264 gdk_device_manager_get_property (GObject      *object,
265                                  guint         prop_id,
266                                  GValue       *value,
267                                  GParamSpec   *pspec)
268 {
269
270   switch (prop_id)
271     {
272     case PROP_DISPLAY:
273       g_value_set_object (value, GDK_DEVICE_MANAGER (object)->display);
274       break;
275     default:
276       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
277       break;
278     }
279 }
280
281 /**
282  * gdk_device_manager_get_display:
283  * @device_manager: a #GdkDeviceManager
284  *
285  * Gets the #GdkDisplay associated to @device_manager.
286  *
287  * Returns: (transfer none): the #GdkDisplay to which @device_manager is
288  *          associated to, or #NULL. This memory is owned by GDK and
289  *          must not be freed or unreferenced.
290  *
291  * Since: 3.0
292  **/
293 GdkDisplay *
294 gdk_device_manager_get_display (GdkDeviceManager *device_manager)
295 {
296   g_return_val_if_fail (GDK_IS_DEVICE_MANAGER (device_manager), NULL);
297
298   return device_manager->display;
299 }
300
301 /**
302  * gdk_device_manager_list_devices:
303  * @device_manager: a #GdkDeviceManager
304  * @type: device type to get.
305  *
306  * Returns the list of devices of type @type currently attached to
307  * @device_manager.
308  *
309  * Returns: (transfer container) (element-type Gdk.Device): a list of 
310  *          #GdkDevice<!-- -->s. The returned list must be
311  *          freed with g_list_free (). The list elements are owned by
312  *          GTK+ and must not be freed or unreffed.
313  *
314  * Since: 3.0
315  **/
316 GList *
317 gdk_device_manager_list_devices (GdkDeviceManager *device_manager,
318                                  GdkDeviceType     type)
319 {
320   g_return_val_if_fail (GDK_IS_DEVICE_MANAGER (device_manager), NULL);
321
322   return GDK_DEVICE_MANAGER_GET_CLASS (device_manager)->list_devices (device_manager, type);
323 }
324
325 /**
326  * gdk_device_manager_get_client_pointer:
327  * @device_manager: a #GdkDeviceManager
328  *
329  * Returns the client pointer, that is, the master pointer that acts as the core pointer
330  * for this application. In X11, window managers may change this depending on the interaction
331  * pattern under the presence of several pointers.
332  *
333  * You should use this function sheldomly, only in code that isn't triggered by a #GdkEvent
334  * and there aren't other means to get a meaningful #GdkDevice to operate on.
335  *
336  * Returns: (transfer none): The client pointer. This memory is
337  *          owned by GDK and must not be freed or unreferenced.
338  *
339  * Since: 3.0
340  **/
341 GdkDevice *
342 gdk_device_manager_get_client_pointer (GdkDeviceManager *device_manager)
343 {
344   g_return_val_if_fail (GDK_IS_DEVICE_MANAGER (device_manager), NULL);
345
346   return GDK_DEVICE_MANAGER_GET_CLASS (device_manager)->get_client_pointer (device_manager);
347 }