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. */
4 * Copyright © 2007,2008 Jaap Haitsma <jaap@haitsma.org>
5 * Copyright © 2007-2009 daniel g. siegel <dgsiegel@gnome.org>
6 * Copyright © 2008 Ryan Zeigler <zeiglerr@gmail.com>
7 * Copyright © 2010 Filippo Argiolas <filippo.argiolas@gmail.com>
9 * Licensed under the GNU General Public License Version 2
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #define G_UDEV_API_IS_SUBJECT_TO_CHANGE 1
32 #include <gudev/gudev.h>
36 #include <sys/ioctl.h>
37 #if USE_SYS_VIDEOIO_H > 0
38 #include <sys/types.h>
39 #include <sys/videoio.h>
41 #include <sys/types.h>
42 #include <sys/videodev2.h>
43 #endif /* USE_SYS_VIDEOIO_H */
46 #include "cheese-camera-device-monitor.h"
49 * SECTION:cheese-camera-device-monitor
50 * @short_description: Simple object to enumerate v4l devices
51 * @include: cheese/cheese-camera-device-monitor.h
53 * #EmpathyCameraDeviceMonitor provides a basic interface for
54 * video4linux device enumeration and hotplugging.
56 * It uses either GUdev or some platform specific code to list video
57 * devices. It is also capable (right now in linux only, with the
58 * udev backend) to monitor device plugging and emit a
59 * EmpathyCameraDeviceMonitor::added or
60 * EmpathyCameraDeviceMonitor::removed signal when an event happens.
63 G_DEFINE_TYPE (EmpathyCameraDeviceMonitor, empathy_camera_device_monitor, G_TYPE_OBJECT)
65 #define EMPATHY_CAMERA_DEVICE_MONITOR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
66 EMPATHY_TYPE_CAMERA_DEVICE_MONITOR, \
67 EmpathyCameraDeviceMonitorPrivate))
69 #define EMPATHY_CAMERA_DEVICE_MONITOR_ERROR empathy_camera_device_monitor_error_quark ()
71 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
72 #include <libempathy/empathy-debug.h>
74 enum EmpathyCameraDeviceMonitorError
76 EMPATHY_CAMERA_DEVICE_MONITOR_ERROR_UNKNOWN,
77 EMPATHY_CAMERA_DEVICE_MONITOR_ERROR_ELEMENT_NOT_FOUND
86 #endif /* HAVE_UDEV */
87 } EmpathyCameraDeviceMonitorPrivate;
96 static guint monitor_signals[LAST_SIGNAL];
100 empathy_camera_device_monitor_error_quark (void)
102 return g_quark_from_static_string ("empathy-camera-error-quark");
108 empathy_camera_device_monitor_added (EmpathyCameraDeviceMonitor *monitor,
109 GUdevDevice *udevice)
111 const char *device_file;
112 const char *product_name;
118 gint v4l_version = 0;
120 const gchar *devpath = g_udev_device_get_property (udevice, "DEVPATH");
122 DEBUG ("Checking udev device '%s'", devpath);
124 bus = g_udev_device_get_property (udevice, "ID_BUS");
125 if (g_strcmp0 (bus, "usb") == 0)
127 vendor = g_udev_device_get_property (udevice, "ID_VENDOR_ID");
129 vendor_id = g_ascii_strtoll (vendor, NULL, 16);
130 product = g_udev_device_get_property (udevice, "ID_MODEL_ID");
132 product_id = g_ascii_strtoll (product, NULL, 16);
133 if (vendor_id == 0 || product_id == 0)
135 DEBUG ("Error getting vendor and product id");
139 DEBUG ("Found device %04x:%04x, getting capabilities...", vendor_id, product_id);
144 DEBUG ("Not an usb device, skipping vendor and model id retrieval");
147 device_file = g_udev_device_get_device_file (udevice);
148 if (device_file == NULL)
150 DEBUG ("Error getting V4L device");
154 /* vbi devices support capture capability too, but cannot be used,
155 * so detect them by device name */
156 if (strstr (device_file, "vbi"))
158 DEBUG ("Skipping vbi device: %s", device_file);
162 v4l_version = g_udev_device_get_property_as_int (udevice, "ID_V4L_VERSION");
163 if (v4l_version == 2 || v4l_version == 1)
167 caps = g_udev_device_get_property (udevice, "ID_V4L_CAPABILITIES");
168 if (caps == NULL || strstr (caps, ":capture:") == NULL)
170 DEBUG ("Device %s seems to not have the capture capability, (radio tuner?)"
171 "Removing it from device list.", device_file);
174 product_name = g_udev_device_get_property (udevice, "ID_V4L_PRODUCT");
176 else if (v4l_version == 0)
178 DEBUG ("Fix your udev installation to include v4l_id, ignoring %s", device_file);
183 g_assert_not_reached ();
186 g_signal_emit (monitor, monitor_signals[ADDED], 0,
194 empathy_camera_device_monitor_removed (EmpathyCameraDeviceMonitor *monitor,
195 GUdevDevice *udevice)
197 g_signal_emit (monitor, monitor_signals[REMOVED], 0,
198 g_udev_device_get_property (udevice, "DEVPATH"));
202 empathy_camera_device_monitor_uevent_cb (GUdevClient *client,
204 GUdevDevice *udevice,
205 EmpathyCameraDeviceMonitor *monitor)
207 if (g_str_equal (action, "remove"))
208 empathy_camera_device_monitor_removed (monitor, udevice);
209 else if (g_str_equal (action, "add"))
210 empathy_camera_device_monitor_added (monitor, udevice);
214 * empathy_camera_device_monitor_coldplug:
215 * @monitor: a #EmpathyCameraDeviceMonitor object.
217 * Will actively look for plugged in cameras and emit
218 * ::added for those new cameras.
219 * This is only required when your program starts, so as to connect
220 * to those signals before they are emitted.
223 empathy_camera_device_monitor_coldplug (EmpathyCameraDeviceMonitor *monitor)
225 EmpathyCameraDeviceMonitorPrivate *priv = EMPATHY_CAMERA_DEVICE_MONITOR_GET_PRIVATE (monitor);
229 if (priv->client == NULL)
232 DEBUG ("Probing devices with udev...");
234 devices = g_udev_client_query_by_subsystem (priv->client, "video4linux");
236 /* Initialize camera structures */
237 for (l = devices; l != NULL; l = l->next)
239 empathy_camera_device_monitor_added (monitor, l->data);
240 g_object_unref (l->data);
243 g_list_free (devices);
245 if (i == 0) DEBUG ("No device found");
248 #else /* HAVE_UDEV */
250 empathy_camera_device_monitor_coldplug (EmpathyCameraDeviceMonitor *monitor)
253 EmpathyCameraDeviceMonitorPrivate *priv = EMPATHY_CAMERA_DEVICE_MONITOR_GET_PRIVATE (monitor);
254 struct v4l2_capability v2cap;
255 struct video_capability v1cap;
258 if ((fd = open (device_path, O_RDONLY | O_NONBLOCK)) < 0)
260 g_warning ("Failed to open %s: %s", device_path, strerror (errno));
263 ok = ioctl (fd, VIDIOC_QUERYCAP, &v2cap);
266 ok = ioctl (fd, VIDIOCGCAP, &v1cap);
269 g_warning ("Error while probing v4l capabilities for %s: %s",
270 device_path, strerror (errno));
274 g_print ("Detected v4l device: %s\n", v1cap.name);
275 g_print ("Device type: %d\n", v1cap.type);
276 gstreamer_src = "v4lsrc";
277 product_name = v1cap.name;
281 guint cap = v2cap.capabilities;
282 g_print ("Detected v4l2 device: %s\n", v2cap.card);
283 g_print ("Driver: %s, version: %d\n", v2cap.driver, v2cap.version);
285 /* g_print ("Bus info: %s\n", v2cap.bus_info); */ /* Doesn't seem anything useful */
286 g_print ("Capabilities: 0x%08X\n", v2cap.capabilities);
287 if (!(cap & V4L2_CAP_VIDEO_CAPTURE))
289 g_print ("Device %s seems to not have the capture capability, (radio tuner?)\n"
290 "Removing it from device list.\n", device_path);
294 gstreamer_src = "v4l2src";
295 product_name = (char *) v2cap.card;
301 g_print ("Probing devices with udev...\n");
303 if (priv->client == NULL)
306 devices = g_udev_client_query_by_subsystem (priv->client, "video4linux");
308 /* Initialize camera structures */
309 for (l = devices; l != NULL; l = l->next)
311 empathy_camera_device_monitor_added (monitor, l->data);
312 g_object_unref (l->data);
314 g_list_free (devices);
318 #endif /* HAVE_UDEV */
321 empathy_camera_device_monitor_finalize (GObject *object)
324 EmpathyCameraDeviceMonitor *monitor = EMPATHY_CAMERA_DEVICE_MONITOR (object);
325 EmpathyCameraDeviceMonitorPrivate *priv = EMPATHY_CAMERA_DEVICE_MONITOR_GET_PRIVATE (monitor);
327 if (priv->client != NULL)
329 g_object_unref (priv->client);
332 #endif /* HAVE_UDEV */
333 G_OBJECT_CLASS (empathy_camera_device_monitor_parent_class)->finalize (object);
337 empathy_camera_device_monitor_class_init (EmpathyCameraDeviceMonitorClass *klass)
339 GObjectClass *object_class = G_OBJECT_CLASS (klass);
341 object_class->finalize = empathy_camera_device_monitor_finalize;
344 * EmpathyCameraDeviceMonitor::added:
345 * @device: A private object representing the newly added camera.
346 * @id: Device unique identifier.
347 * @device: Device file name (e.g. /dev/video2).
348 * @product_name: Device product name (human readable, intended to be displayed in a UI).
349 * @api_version: Supported video4linux API: 1 for v4l, 2 for v4l2.
351 * The ::added signal is emitted when a camera is added, or on start-up
352 * after #empathy_camera_device_monitor_colplug is called.
354 monitor_signals[ADDED] = g_signal_new ("added", G_OBJECT_CLASS_TYPE (klass),
355 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
356 G_STRUCT_OFFSET (EmpathyCameraDeviceMonitorClass, added),
358 g_cclosure_marshal_generic,
359 G_TYPE_NONE, 4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT);
362 * EmpathyCameraDeviceMonitor::removed:
363 * @device: A private object representing the newly added camera
364 * @id: Device unique identifier.
366 * The ::removed signal is emitted when a camera is un-plugged, or
367 * disabled on the system.
369 monitor_signals[REMOVED] = g_signal_new ("removed", G_OBJECT_CLASS_TYPE (klass),
370 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
371 G_STRUCT_OFFSET (EmpathyCameraDeviceMonitorClass, removed),
373 g_cclosure_marshal_generic,
374 G_TYPE_NONE, 1, G_TYPE_STRING);
376 g_type_class_add_private (klass, sizeof (EmpathyCameraDeviceMonitorPrivate));
380 empathy_camera_device_monitor_init (EmpathyCameraDeviceMonitor *monitor)
383 EmpathyCameraDeviceMonitorPrivate *priv = EMPATHY_CAMERA_DEVICE_MONITOR_GET_PRIVATE (monitor);
384 const gchar *const subsystems[] = {"video4linux", NULL};
386 priv->client = g_udev_client_new (subsystems);
387 g_signal_connect (G_OBJECT (priv->client), "uevent",
388 G_CALLBACK (empathy_camera_device_monitor_uevent_cb), monitor);
389 #endif /* HAVE_UDEV */
393 * empathy_camera_device_monitor_new:
395 * Returns a new #EmpathyCameraDeviceMonitor object.
397 * Return value: a new #EmpathyCameraDeviceMonitor object.
399 EmpathyCameraDeviceMonitor *
400 empathy_camera_device_monitor_new (void)
402 return g_object_new (EMPATHY_TYPE_CAMERA_DEVICE_MONITOR, NULL);
406 * vim: sw=2 ts=8 cindent noai bs=2