1
/* This file is a copy of cheese-camera-device-monitor.c from Empathy. We
2
* just renamespaced it to avoid conflicts when linking on libcheese. */
2
4
* Copyright © 2007,2008 Jaap Haitsma <jaap@haitsma.org>
3
5
* Copyright © 2007-2009 daniel g. siegel <dgsiegel@gnome.org>
49
51
* @short_description: Simple object to enumerate v4l devices
50
52
* @include: cheese/cheese-camera-device-monitor.h
52
* #CheeseCameraDeviceMonitor provides a basic interface for
54
* #EmpathyCameraDeviceMonitor provides a basic interface for
53
55
* video4linux device enumeration and hotplugging.
55
57
* It uses either GUdev or some platform specific code to list video
56
58
* devices. It is also capable (right now in linux only, with the
57
59
* udev backend) to monitor device plugging and emit a
58
* CheeseCameraDeviceMonitor::added or
59
* CheeseCameraDeviceMonitor::removed signal when an event happens.
60
* EmpathyCameraDeviceMonitor::added or
61
* EmpathyCameraDeviceMonitor::removed signal when an event happens.
62
G_DEFINE_TYPE (CheeseCameraDeviceMonitor, cheese_camera_device_monitor, G_TYPE_OBJECT)
64
#define CHEESE_CAMERA_DEVICE_MONITOR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
65
CHEESE_TYPE_CAMERA_DEVICE_MONITOR, \
66
CheeseCameraDeviceMonitorPrivate))
68
#define CHEESE_CAMERA_DEVICE_MONITOR_ERROR cheese_camera_device_monitor_error_quark ()
70
GST_DEBUG_CATEGORY (cheese_device_monitor_cat);
71
#define GST_CAT_DEFAULT cheese_device_monitor_cat
73
enum CheeseCameraDeviceMonitorError
64
G_DEFINE_TYPE (EmpathyCameraDeviceMonitor, empathy_camera_device_monitor, G_TYPE_OBJECT)
66
#define EMPATHY_CAMERA_DEVICE_MONITOR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
67
EMPATHY_TYPE_CAMERA_DEVICE_MONITOR, \
68
EmpathyCameraDeviceMonitorPrivate))
70
#define EMPATHY_CAMERA_DEVICE_MONITOR_ERROR empathy_camera_device_monitor_error_quark ()
72
GST_DEBUG_CATEGORY (empathy_device_monitor_cat);
73
#define GST_CAT_DEFAULT empathy_device_monitor_cat
75
enum EmpathyCameraDeviceMonitorError
75
CHEESE_CAMERA_DEVICE_MONITOR_ERROR_UNKNOWN,
76
CHEESE_CAMERA_DEVICE_MONITOR_ERROR_ELEMENT_NOT_FOUND
77
EMPATHY_CAMERA_DEVICE_MONITOR_ERROR_UNKNOWN,
78
EMPATHY_CAMERA_DEVICE_MONITOR_ERROR_ELEMENT_NOT_FOUND
99
cheese_camera_device_monitor_error_quark (void)
101
empathy_camera_device_monitor_error_quark (void)
101
return g_quark_from_static_string ("cheese-camera-error-quark");
103
return g_quark_from_static_string ("empathy-camera-error-quark");
107
cheese_camera_device_monitor_added (CheeseCameraDeviceMonitor *monitor,
109
empathy_camera_device_monitor_added (EmpathyCameraDeviceMonitor *monitor,
108
110
GUdevDevice *udevice)
110
112
const char *device_file;
193
cheese_camera_device_monitor_removed (CheeseCameraDeviceMonitor *monitor,
195
empathy_camera_device_monitor_removed (EmpathyCameraDeviceMonitor *monitor,
194
196
GUdevDevice *udevice)
196
198
g_signal_emit (monitor, monitor_signals[REMOVED], 0,
201
cheese_camera_device_monitor_uevent_cb (GUdevClient *client,
203
empathy_camera_device_monitor_uevent_cb (GUdevClient *client,
202
204
const gchar *action,
203
205
GUdevDevice *udevice,
204
CheeseCameraDeviceMonitor *monitor)
206
EmpathyCameraDeviceMonitor *monitor)
206
208
if (g_str_equal (action, "remove"))
207
cheese_camera_device_monitor_removed (monitor, udevice);
209
empathy_camera_device_monitor_removed (monitor, udevice);
208
210
else if (g_str_equal (action, "add"))
209
cheese_camera_device_monitor_added (monitor, udevice);
211
empathy_camera_device_monitor_added (monitor, udevice);
213
* cheese_camera_device_monitor_coldplug:
214
* @monitor: a #CheeseCameraDeviceMonitor object.
215
* empathy_camera_device_monitor_coldplug:
216
* @monitor: a #EmpathyCameraDeviceMonitor object.
216
218
* Will actively look for plugged in cameras and emit
217
219
* ::added for those new cameras.
219
221
* to those signals before they are emitted.
222
cheese_camera_device_monitor_coldplug (CheeseCameraDeviceMonitor *monitor)
224
empathy_camera_device_monitor_coldplug (EmpathyCameraDeviceMonitor *monitor)
224
CheeseCameraDeviceMonitorPrivate *priv = CHEESE_CAMERA_DEVICE_MONITOR_GET_PRIVATE (monitor);
226
EmpathyCameraDeviceMonitorPrivate *priv = EMPATHY_CAMERA_DEVICE_MONITOR_GET_PRIVATE (monitor);
225
227
GList *devices, *l;
235
237
/* Initialize camera structures */
236
238
for (l = devices; l != NULL; l = l->next)
238
cheese_camera_device_monitor_added (monitor, l->data);
240
empathy_camera_device_monitor_added (monitor, l->data);
239
241
g_object_unref (l->data);
247
249
#else /* HAVE_UDEV */
249
cheese_camera_device_monitor_coldplug (CheeseCameraDeviceMonitor *monitor)
251
empathy_camera_device_monitor_coldplug (EmpathyCameraDeviceMonitor *monitor)
252
CheeseCameraDeviceMonitorPrivate *priv = CHEESE_CAMERA_DEVICE_MONITOR_GET_PRIVATE (monitor);
254
EmpathyCameraDeviceMonitorPrivate *priv = EMPATHY_CAMERA_DEVICE_MONITOR_GET_PRIVATE (monitor);
253
255
struct v4l2_capability v2cap;
254
256
struct video_capability v1cap;
307
309
/* Initialize camera structures */
308
310
for (l = devices; l != NULL; l = l->next)
310
cheese_camera_device_monitor_added (monitor, l->data);
312
empathy_camera_device_monitor_added (monitor, l->data);
311
313
g_object_unref (l->data);
313
315
g_list_free (devices);
317
319
#endif /* HAVE_UDEV */
320
cheese_camera_device_monitor_finalize (GObject *object)
322
empathy_camera_device_monitor_finalize (GObject *object)
323
CheeseCameraDeviceMonitor *monitor = CHEESE_CAMERA_DEVICE_MONITOR (object);
324
CheeseCameraDeviceMonitorPrivate *priv = CHEESE_CAMERA_DEVICE_MONITOR_GET_PRIVATE (monitor);
325
EmpathyCameraDeviceMonitor *monitor = EMPATHY_CAMERA_DEVICE_MONITOR (object);
326
EmpathyCameraDeviceMonitorPrivate *priv = EMPATHY_CAMERA_DEVICE_MONITOR_GET_PRIVATE (monitor);
326
328
if (priv->client != NULL)
329
331
priv->client = NULL;
331
333
#endif /* HAVE_UDEV */
332
G_OBJECT_CLASS (cheese_camera_device_monitor_parent_class)->finalize (object);
334
G_OBJECT_CLASS (empathy_camera_device_monitor_parent_class)->finalize (object);
336
cheese_camera_device_monitor_class_init (CheeseCameraDeviceMonitorClass *klass)
338
empathy_camera_device_monitor_class_init (EmpathyCameraDeviceMonitorClass *klass)
338
340
GObjectClass *object_class = G_OBJECT_CLASS (klass);
340
if (cheese_device_monitor_cat == NULL)
341
GST_DEBUG_CATEGORY_INIT (cheese_device_monitor_cat,
342
"cheese-device-monitor",
343
0, "Cheese Camera Device Monitor");
342
if (empathy_device_monitor_cat == NULL)
343
GST_DEBUG_CATEGORY_INIT (empathy_device_monitor_cat,
344
"empathy-device-monitor",
345
0, "Empathy Camera Device Monitor");
345
object_class->finalize = cheese_camera_device_monitor_finalize;
347
object_class->finalize = empathy_camera_device_monitor_finalize;
348
* CheeseCameraDeviceMonitor::added:
350
* EmpathyCameraDeviceMonitor::added:
349
351
* @device: A private object representing the newly added camera.
350
352
* @id: Device unique identifier.
351
353
* @device: Device file name (e.g. /dev/video2).
353
355
* @api_version: Supported video4linux API: 1 for v4l, 2 for v4l2.
355
357
* The ::added signal is emitted when a camera is added, or on start-up
356
* after #cheese_camera_device_monitor_colplug is called.
358
* after #empathy_camera_device_monitor_colplug is called.
358
360
monitor_signals[ADDED] = g_signal_new ("added", G_OBJECT_CLASS_TYPE (klass),
359
361
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
360
G_STRUCT_OFFSET (CheeseCameraDeviceMonitorClass, added),
362
G_STRUCT_OFFSET (EmpathyCameraDeviceMonitorClass, added),
362
364
g_cclosure_marshal_generic,
363
365
G_TYPE_NONE, 4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT);
366
* CheeseCameraDeviceMonitor::removed:
368
* EmpathyCameraDeviceMonitor::removed:
367
369
* @device: A private object representing the newly added camera
368
370
* @id: Device unique identifier.
373
375
monitor_signals[REMOVED] = g_signal_new ("removed", G_OBJECT_CLASS_TYPE (klass),
374
376
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
375
G_STRUCT_OFFSET (CheeseCameraDeviceMonitorClass, removed),
377
G_STRUCT_OFFSET (EmpathyCameraDeviceMonitorClass, removed),
377
379
g_cclosure_marshal_generic,
378
380
G_TYPE_NONE, 1, G_TYPE_STRING);
380
g_type_class_add_private (klass, sizeof (CheeseCameraDeviceMonitorPrivate));
382
g_type_class_add_private (klass, sizeof (EmpathyCameraDeviceMonitorPrivate));
384
cheese_camera_device_monitor_init (CheeseCameraDeviceMonitor *monitor)
386
empathy_camera_device_monitor_init (EmpathyCameraDeviceMonitor *monitor)
387
CheeseCameraDeviceMonitorPrivate *priv = CHEESE_CAMERA_DEVICE_MONITOR_GET_PRIVATE (monitor);
389
EmpathyCameraDeviceMonitorPrivate *priv = EMPATHY_CAMERA_DEVICE_MONITOR_GET_PRIVATE (monitor);
388
390
const gchar *const subsystems[] = {"video4linux", NULL};
390
392
priv->client = g_udev_client_new (subsystems);
391
393
g_signal_connect (G_OBJECT (priv->client), "uevent",
392
G_CALLBACK (cheese_camera_device_monitor_uevent_cb), monitor);
394
G_CALLBACK (empathy_camera_device_monitor_uevent_cb), monitor);
393
395
#endif /* HAVE_UDEV */
397
* cheese_camera_device_monitor_new:
399
* Returns a new #CheeseCameraDeviceMonitor object.
401
* Return value: a new #CheeseCameraDeviceMonitor object.
399
* empathy_camera_device_monitor_new:
401
* Returns a new #EmpathyCameraDeviceMonitor object.
403
* Return value: a new #EmpathyCameraDeviceMonitor object.
403
CheeseCameraDeviceMonitor *
404
cheese_camera_device_monitor_new (void)
405
EmpathyCameraDeviceMonitor *
406
empathy_camera_device_monitor_new (void)
406
return g_object_new (CHEESE_TYPE_CAMERA_DEVICE_MONITOR, NULL);
408
return g_object_new (EMPATHY_TYPE_CAMERA_DEVICE_MONITOR, NULL);