Release Clutter 1.11.4 (snapshot)
[profile/ivi/clutter.git] / clutter / clutter-device-manager.c
1 /*
2  * Clutter.
3  *
4  * An OpenGL based 'interactive canvas' library.
5  *
6  * Copyright (C) 2009  Intel Corp.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20  *
21  * Author: Emmanuele Bassi <ebassi@linux.intel.com>
22  */
23
24 /**
25  * SECTION:clutter-device-manager
26  * @short_description: Maintains the list of input devices
27  *
28  * #ClutterDeviceManager is a singleton object, owned by Clutter, which
29  * maintains the list of #ClutterInputDevice<!-- -->s.
30  *
31  * Depending on the backend used by Clutter it is possible to use the
32  * #ClutterDeviceManager::device-added and
33  * #ClutterDeviceManager::device-removed to monitor addition and removal
34  * of devices.
35  *
36  * #ClutterDeviceManager is available since Clutter 1.2
37  */
38
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #include "clutter-backend-private.h"
44 #include "clutter-debug.h"
45 #include "clutter-device-manager-private.h"
46 #include "clutter-enum-types.h"
47 #include "clutter-marshal.h"
48 #include "clutter-private.h"
49 #include "clutter-stage-private.h"
50
51 #define CLUTTER_DEVICE_MANAGER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CLUTTER_TYPE_DEVICE_MANAGER, ClutterDeviceManagerPrivate))
52
53 struct _ClutterDeviceManagerPrivate
54 {
55   /* back-pointer to the backend */
56   ClutterBackend *backend;
57 };
58
59 enum
60 {
61   PROP_0,
62
63   PROP_BACKEND,
64
65   PROP_LAST
66 };
67
68 static GParamSpec *obj_props[PROP_LAST];
69
70 enum
71 {
72   DEVICE_ADDED,
73   DEVICE_REMOVED,
74
75   LAST_SIGNAL
76 };
77
78 static guint manager_signals[LAST_SIGNAL] = { 0, };
79
80 G_DEFINE_ABSTRACT_TYPE (ClutterDeviceManager,
81                         clutter_device_manager,
82                         G_TYPE_OBJECT);
83
84 static void
85 clutter_device_manager_set_property (GObject      *gobject,
86                                      guint         prop_id,
87                                      const GValue *value,
88                                      GParamSpec   *pspec)
89 {
90   ClutterDeviceManagerPrivate *priv = CLUTTER_DEVICE_MANAGER (gobject)->priv;
91
92   switch (prop_id)
93     {
94     case PROP_BACKEND:
95       priv->backend = g_value_get_object (value);
96       break;
97
98     default:
99       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
100     }
101 }
102
103 static void
104 clutter_device_manager_get_property (GObject    *gobject,
105                                      guint       prop_id,
106                                      GValue     *value,
107                                      GParamSpec *pspec)
108 {
109   ClutterDeviceManagerPrivate *priv = CLUTTER_DEVICE_MANAGER (gobject)->priv;
110
111   switch (prop_id)
112     {
113     case PROP_BACKEND:
114       g_value_set_object (value, priv->backend);
115       break;
116
117     default:
118       G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
119     }
120 }
121
122 static void
123 clutter_device_manager_class_init (ClutterDeviceManagerClass *klass)
124 {
125   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
126
127   g_type_class_add_private (klass, sizeof (ClutterDeviceManagerPrivate));
128
129   obj_props[PROP_BACKEND] =
130     g_param_spec_object ("backend",
131                          P_("Backend"),
132                          P_("The ClutterBackend of the device manager"),
133                          CLUTTER_TYPE_BACKEND,
134                          CLUTTER_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
135
136   gobject_class->set_property = clutter_device_manager_set_property;
137   gobject_class->get_property = clutter_device_manager_get_property;
138   g_object_class_install_properties (gobject_class,
139                                      PROP_LAST,
140                                      obj_props);
141
142   /**
143    * ClutterDeviceManager::device-added:
144    * @manager: the #ClutterDeviceManager that emitted the signal
145    * @device: the newly added #ClutterInputDevice
146    *
147    * The ::device-added signal is emitted each time a device has been
148    * added to the #ClutterDeviceManager
149    *
150    * Since: 1.2
151    */
152   manager_signals[DEVICE_ADDED] =
153     g_signal_new (I_("device-added"),
154                   G_TYPE_FROM_CLASS (klass),
155                   G_SIGNAL_RUN_LAST,
156                   0,
157                   NULL, NULL,
158                   _clutter_marshal_VOID__OBJECT,
159                   G_TYPE_NONE, 1,
160                   CLUTTER_TYPE_INPUT_DEVICE);
161
162   /**
163    * ClutterDeviceManager::device-removed:
164    * @manager: the #ClutterDeviceManager that emitted the signal
165    * @device: the removed #ClutterInputDevice
166    *
167    * The ::device-removed signal is emitted each time a device has been
168    * removed from the #ClutterDeviceManager
169    *
170    * Since: 1.2
171    */
172   manager_signals[DEVICE_REMOVED] =
173     g_signal_new (I_("device-removed"),
174                   G_TYPE_FROM_CLASS (klass),
175                   G_SIGNAL_RUN_LAST,
176                   0,
177                   NULL, NULL,
178                   _clutter_marshal_VOID__OBJECT,
179                   G_TYPE_NONE, 1,
180                   CLUTTER_TYPE_INPUT_DEVICE);
181 }
182
183 static void
184 clutter_device_manager_init (ClutterDeviceManager *self)
185 {
186   self->priv = CLUTTER_DEVICE_MANAGER_GET_PRIVATE (self);
187 }
188
189 /**
190  * clutter_device_manager_get_default:
191  *
192  * Retrieves the device manager singleton
193  *
194  * Return value: (transfer none): the #ClutterDeviceManager singleton.
195  *   The returned instance is owned by Clutter and it should not be
196  *   modified or freed
197  *
198  * Since: 1.2
199  */
200 ClutterDeviceManager *
201 clutter_device_manager_get_default (void)
202 {
203   ClutterBackend *backend = clutter_get_default_backend ();
204
205   return backend->device_manager;
206 }
207
208 /**
209  * clutter_device_manager_list_devices:
210  * @device_manager: a #ClutterDeviceManager
211  *
212  * Lists all currently registered input devices
213  *
214  * Return value: (transfer container) (element-type Clutter.InputDevice):
215  *   a newly allocated list of #ClutterInputDevice objects. Use
216  *   g_slist_free() to deallocate it when done
217  *
218  * Since: 1.2
219  */
220 GSList *
221 clutter_device_manager_list_devices (ClutterDeviceManager *device_manager)
222 {
223   const GSList *devices;
224
225   g_return_val_if_fail (CLUTTER_IS_DEVICE_MANAGER (device_manager), NULL);
226
227   devices = clutter_device_manager_peek_devices (device_manager);
228
229   return g_slist_copy ((GSList *) devices);
230 }
231
232 /**
233  * clutter_device_manager_peek_devices:
234  * @device_manager: a #ClutterDeviceManager
235  *
236  * Lists all currently registered input devices
237  *
238  * Return value: (transfer none) (element-type Clutter.InputDevice):
239  *   a pointer to the internal list of #ClutterInputDevice objects. The
240  *   returned list is owned by the #ClutterDeviceManager and should never
241  *   be modified or freed
242  *
243  * Since: 1.2
244  */
245 const GSList *
246 clutter_device_manager_peek_devices (ClutterDeviceManager *device_manager)
247 {
248   ClutterDeviceManagerClass *manager_class;
249
250   g_return_val_if_fail (CLUTTER_IS_DEVICE_MANAGER (device_manager), NULL);
251
252   manager_class = CLUTTER_DEVICE_MANAGER_GET_CLASS (device_manager);
253   return manager_class->get_devices (device_manager);
254 }
255
256 /**
257  * clutter_device_manager_get_device:
258  * @device_manager: a #ClutterDeviceManager
259  * @device_id: the integer id of a device
260  *
261  * Retrieves the #ClutterInputDevice with the given @device_id
262  *
263  * Return value: (transfer none): a #ClutterInputDevice or %NULL. The
264  *   returned device is owned by the #ClutterDeviceManager and should
265  *   never be modified or freed
266  *
267  * Since: 1.2
268  */
269 ClutterInputDevice *
270 clutter_device_manager_get_device (ClutterDeviceManager *device_manager,
271                                    gint                  device_id)
272 {
273   ClutterDeviceManagerClass *manager_class;
274
275   g_return_val_if_fail (CLUTTER_IS_DEVICE_MANAGER (device_manager), NULL);
276
277   manager_class = CLUTTER_DEVICE_MANAGER_GET_CLASS (device_manager);
278   return manager_class->get_device (device_manager, device_id);
279 }
280
281 /**
282  * clutter_device_manager_get_core_device:
283  * @device_manager: a #ClutterDeviceManager
284  * @device_type: the type of the core device
285  *
286  * Retrieves the core #ClutterInputDevice of type @device_type
287  *
288  * Core devices are devices created automatically by the default
289  * Clutter backend
290  *
291  * Return value: (transfer none): a #ClutterInputDevice or %NULL. The
292  *   returned device is owned by the #ClutterDeviceManager and should
293  *   not be modified or freed
294  *
295  * Since: 1.2
296  */
297 ClutterInputDevice *
298 clutter_device_manager_get_core_device (ClutterDeviceManager   *device_manager,
299                                         ClutterInputDeviceType  device_type)
300 {
301   ClutterDeviceManagerClass *manager_class;
302
303   g_return_val_if_fail (CLUTTER_IS_DEVICE_MANAGER (device_manager), NULL);
304
305   manager_class = CLUTTER_DEVICE_MANAGER_GET_CLASS (device_manager);
306   return manager_class->get_core_device (device_manager, device_type);
307 }
308
309 void
310 _clutter_device_manager_select_stage_events (ClutterDeviceManager *device_manager,
311                                              ClutterStage         *stage,
312                                              gint                  event_flags)
313 {
314   ClutterDeviceManagerClass *manager_class;
315   const GSList *devices, *d;
316
317   g_return_if_fail (CLUTTER_IS_DEVICE_MANAGER (device_manager));
318
319   manager_class = CLUTTER_DEVICE_MANAGER_GET_CLASS (device_manager);
320   devices = manager_class->get_devices (device_manager);
321
322   for (d = devices; d != NULL; d = d->next)
323     {
324       ClutterInputDevice *device = d->data;
325
326       if (device->is_enabled)
327         _clutter_input_device_select_stage_events (device, stage, event_flags);
328     }
329 }
330
331 /*
332  * _clutter_device_manager_add_device:
333  * @device_manager: a #ClutterDeviceManager
334  * @device: a #ClutterInputDevice
335  *
336  * Adds @device to the list of #ClutterInputDevice<!-- -->s maintained
337  * by @device_manager
338  *
339  * The reference count of @device is not increased
340  *
341  * The #ClutterDeviceManager::device-added signal is emitted after
342  * adding @device to the list
343  */
344 void
345 _clutter_device_manager_add_device (ClutterDeviceManager *device_manager,
346                                     ClutterInputDevice   *device)
347 {
348   ClutterDeviceManagerClass *manager_class;
349
350   g_return_if_fail (CLUTTER_IS_DEVICE_MANAGER (device_manager));
351
352   manager_class = CLUTTER_DEVICE_MANAGER_GET_CLASS (device_manager);
353   g_assert (manager_class->add_device != NULL);
354
355   manager_class->add_device (device_manager, device);
356
357   g_signal_emit (device_manager, manager_signals[DEVICE_ADDED], 0, device);
358 }
359
360 /*
361  * _clutter_device_manager_remove_device:
362  * @device_manager: a #ClutterDeviceManager
363  * @device: a #ClutterInputDevice
364  *
365  * Removes @device from the list of #ClutterInputDevice<!-- -->s
366  * maintained by @device_manager
367  *
368  * The reference count of @device is not decreased
369  *
370  * The #ClutterDeviceManager::device-removed signal is emitted after
371  * removing @device from the list
372  */
373 void
374 _clutter_device_manager_remove_device (ClutterDeviceManager *device_manager,
375                                        ClutterInputDevice   *device)
376 {
377   ClutterDeviceManagerClass *manager_class;
378
379   g_return_if_fail (CLUTTER_IS_DEVICE_MANAGER (device_manager));
380
381   manager_class = CLUTTER_DEVICE_MANAGER_GET_CLASS (device_manager);
382   g_assert (manager_class->remove_device != NULL);
383
384   manager_class->remove_device (device_manager, device);
385
386   g_signal_emit (device_manager, manager_signals[DEVICE_REMOVED], 0, device);
387 }
388
389 /*
390  * _clutter_device_manager_update_devices:
391  * @device_manager: a #ClutterDeviceManager
392  *
393  * Updates every #ClutterInputDevice handled by @device_manager
394  * by performing a pick paint at the coordinates of each pointer
395  * device
396  */
397 void
398 _clutter_device_manager_update_devices (ClutterDeviceManager *device_manager)
399 {
400   const GSList *d;
401
402   for (d = clutter_device_manager_peek_devices (device_manager);
403        d != NULL;
404        d = d->next)
405     {
406       ClutterInputDevice *device = d->data;
407       ClutterInputDeviceType device_type;
408
409       /* we only care about pointer devices */
410       device_type = clutter_input_device_get_device_type (device);
411       if (device_type != CLUTTER_POINTER_DEVICE)
412         continue;
413
414       /* out of stage */
415       if (device->stage == NULL)
416         continue;
417
418       /* the user disabled motion events delivery on actors for
419        * the stage the device is on; we don't perform any picking
420        * since the source of the events will always be set to be
421        * the stage
422        */
423       if (!clutter_stage_get_motion_events_enabled (device->stage))
424         continue;
425
426       _clutter_input_device_update (device, TRUE);
427     }
428 }
429
430 ClutterBackend *
431 _clutter_device_manager_get_backend (ClutterDeviceManager *manager)
432 {
433   g_return_val_if_fail (CLUTTER_IS_DEVICE_MANAGER (manager), NULL);
434
435   return manager->priv->backend;
436 }