4 * An OpenGL based 'interactive canvas' library.
6 * Copyright (C) 2009 Intel Corp.
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.
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.
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/>.
21 * Author: Emmanuele Bassi <ebassi@linux.intel.com>
25 * SECTION:clutter-device-manager
26 * @short_description: Maintains the list of input devices
28 * #ClutterDeviceManager is a singleton object, owned by Clutter, which
29 * maintains the list of #ClutterInputDevice<!-- -->s.
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
36 * #ClutterDeviceManager is available since Clutter 1.2
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"
51 #define CLUTTER_DEVICE_MANAGER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CLUTTER_TYPE_DEVICE_MANAGER, ClutterDeviceManagerPrivate))
53 struct _ClutterDeviceManagerPrivate
55 /* back-pointer to the backend */
56 ClutterBackend *backend;
68 static GParamSpec *obj_props[PROP_LAST];
78 static guint manager_signals[LAST_SIGNAL] = { 0, };
80 G_DEFINE_ABSTRACT_TYPE (ClutterDeviceManager,
81 clutter_device_manager,
85 clutter_device_manager_set_property (GObject *gobject,
90 ClutterDeviceManagerPrivate *priv = CLUTTER_DEVICE_MANAGER (gobject)->priv;
95 priv->backend = g_value_get_object (value);
99 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
104 clutter_device_manager_get_property (GObject *gobject,
109 ClutterDeviceManagerPrivate *priv = CLUTTER_DEVICE_MANAGER (gobject)->priv;
114 g_value_set_object (value, priv->backend);
118 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
123 clutter_device_manager_class_init (ClutterDeviceManagerClass *klass)
125 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
127 g_type_class_add_private (klass, sizeof (ClutterDeviceManagerPrivate));
129 obj_props[PROP_BACKEND] =
130 g_param_spec_object ("backend",
132 P_("The ClutterBackend of the device manager"),
133 CLUTTER_TYPE_BACKEND,
134 CLUTTER_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
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,
143 * ClutterDeviceManager::device-added:
144 * @manager: the #ClutterDeviceManager that emitted the signal
145 * @device: the newly added #ClutterInputDevice
147 * The ::device-added signal is emitted each time a device has been
148 * added to the #ClutterDeviceManager
152 manager_signals[DEVICE_ADDED] =
153 g_signal_new (I_("device-added"),
154 G_TYPE_FROM_CLASS (klass),
158 _clutter_marshal_VOID__OBJECT,
160 CLUTTER_TYPE_INPUT_DEVICE);
163 * ClutterDeviceManager::device-removed:
164 * @manager: the #ClutterDeviceManager that emitted the signal
165 * @device: the removed #ClutterInputDevice
167 * The ::device-removed signal is emitted each time a device has been
168 * removed from the #ClutterDeviceManager
172 manager_signals[DEVICE_REMOVED] =
173 g_signal_new (I_("device-removed"),
174 G_TYPE_FROM_CLASS (klass),
178 _clutter_marshal_VOID__OBJECT,
180 CLUTTER_TYPE_INPUT_DEVICE);
184 clutter_device_manager_init (ClutterDeviceManager *self)
186 self->priv = CLUTTER_DEVICE_MANAGER_GET_PRIVATE (self);
190 * clutter_device_manager_get_default:
192 * Retrieves the device manager singleton
194 * Return value: (transfer none): the #ClutterDeviceManager singleton.
195 * The returned instance is owned by Clutter and it should not be
200 ClutterDeviceManager *
201 clutter_device_manager_get_default (void)
203 ClutterBackend *backend = clutter_get_default_backend ();
205 return backend->device_manager;
209 * clutter_device_manager_list_devices:
210 * @device_manager: a #ClutterDeviceManager
212 * Lists all currently registered input devices
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
221 clutter_device_manager_list_devices (ClutterDeviceManager *device_manager)
223 const GSList *devices;
225 g_return_val_if_fail (CLUTTER_IS_DEVICE_MANAGER (device_manager), NULL);
227 devices = clutter_device_manager_peek_devices (device_manager);
229 return g_slist_copy ((GSList *) devices);
233 * clutter_device_manager_peek_devices:
234 * @device_manager: a #ClutterDeviceManager
236 * Lists all currently registered input devices
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
246 clutter_device_manager_peek_devices (ClutterDeviceManager *device_manager)
248 ClutterDeviceManagerClass *manager_class;
250 g_return_val_if_fail (CLUTTER_IS_DEVICE_MANAGER (device_manager), NULL);
252 manager_class = CLUTTER_DEVICE_MANAGER_GET_CLASS (device_manager);
253 return manager_class->get_devices (device_manager);
257 * clutter_device_manager_get_device:
258 * @device_manager: a #ClutterDeviceManager
259 * @device_id: the integer id of a device
261 * Retrieves the #ClutterInputDevice with the given @device_id
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
270 clutter_device_manager_get_device (ClutterDeviceManager *device_manager,
273 ClutterDeviceManagerClass *manager_class;
275 g_return_val_if_fail (CLUTTER_IS_DEVICE_MANAGER (device_manager), NULL);
277 manager_class = CLUTTER_DEVICE_MANAGER_GET_CLASS (device_manager);
278 return manager_class->get_device (device_manager, device_id);
282 * clutter_device_manager_get_core_device:
283 * @device_manager: a #ClutterDeviceManager
284 * @device_type: the type of the core device
286 * Retrieves the core #ClutterInputDevice of type @device_type
288 * Core devices are devices created automatically by the default
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
298 clutter_device_manager_get_core_device (ClutterDeviceManager *device_manager,
299 ClutterInputDeviceType device_type)
301 ClutterDeviceManagerClass *manager_class;
303 g_return_val_if_fail (CLUTTER_IS_DEVICE_MANAGER (device_manager), NULL);
305 manager_class = CLUTTER_DEVICE_MANAGER_GET_CLASS (device_manager);
306 return manager_class->get_core_device (device_manager, device_type);
310 _clutter_device_manager_select_stage_events (ClutterDeviceManager *device_manager,
314 ClutterDeviceManagerClass *manager_class;
315 const GSList *devices, *d;
317 g_return_if_fail (CLUTTER_IS_DEVICE_MANAGER (device_manager));
319 manager_class = CLUTTER_DEVICE_MANAGER_GET_CLASS (device_manager);
320 devices = manager_class->get_devices (device_manager);
322 for (d = devices; d != NULL; d = d->next)
324 ClutterInputDevice *device = d->data;
326 if (device->is_enabled)
327 _clutter_input_device_select_stage_events (device, stage, event_flags);
332 * _clutter_device_manager_add_device:
333 * @device_manager: a #ClutterDeviceManager
334 * @device: a #ClutterInputDevice
336 * Adds @device to the list of #ClutterInputDevice<!-- -->s maintained
339 * The reference count of @device is not increased
341 * The #ClutterDeviceManager::device-added signal is emitted after
342 * adding @device to the list
345 _clutter_device_manager_add_device (ClutterDeviceManager *device_manager,
346 ClutterInputDevice *device)
348 ClutterDeviceManagerClass *manager_class;
350 g_return_if_fail (CLUTTER_IS_DEVICE_MANAGER (device_manager));
352 manager_class = CLUTTER_DEVICE_MANAGER_GET_CLASS (device_manager);
353 g_assert (manager_class->add_device != NULL);
355 manager_class->add_device (device_manager, device);
357 g_signal_emit (device_manager, manager_signals[DEVICE_ADDED], 0, device);
361 * _clutter_device_manager_remove_device:
362 * @device_manager: a #ClutterDeviceManager
363 * @device: a #ClutterInputDevice
365 * Removes @device from the list of #ClutterInputDevice<!-- -->s
366 * maintained by @device_manager
368 * The reference count of @device is not decreased
370 * The #ClutterDeviceManager::device-removed signal is emitted after
371 * removing @device from the list
374 _clutter_device_manager_remove_device (ClutterDeviceManager *device_manager,
375 ClutterInputDevice *device)
377 ClutterDeviceManagerClass *manager_class;
379 g_return_if_fail (CLUTTER_IS_DEVICE_MANAGER (device_manager));
381 manager_class = CLUTTER_DEVICE_MANAGER_GET_CLASS (device_manager);
382 g_assert (manager_class->remove_device != NULL);
384 manager_class->remove_device (device_manager, device);
386 g_signal_emit (device_manager, manager_signals[DEVICE_REMOVED], 0, device);
390 * _clutter_device_manager_update_devices:
391 * @device_manager: a #ClutterDeviceManager
393 * Updates every #ClutterInputDevice handled by @device_manager
394 * by performing a pick paint at the coordinates of each pointer
398 _clutter_device_manager_update_devices (ClutterDeviceManager *device_manager)
402 for (d = clutter_device_manager_peek_devices (device_manager);
406 ClutterInputDevice *device = d->data;
407 ClutterInputDeviceType device_type;
409 /* we only care about pointer devices */
410 device_type = clutter_input_device_get_device_type (device);
411 if (device_type != CLUTTER_POINTER_DEVICE)
415 if (device->stage == NULL)
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
423 if (!clutter_stage_get_motion_events_enabled (device->stage))
426 _clutter_input_device_update (device, TRUE);
431 _clutter_device_manager_get_backend (ClutterDeviceManager *manager)
433 g_return_val_if_fail (CLUTTER_IS_DEVICE_MANAGER (manager), NULL);
435 return manager->priv->backend;