]> git.0d.be Git - empathy.git/blob - libempathy/cheese-camera-device-monitor.c
Use double quotes for all internal headers
[empathy.git] / libempathy / cheese-camera-device-monitor.c
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. */
3 /*
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>
8  *
9  * Licensed under the GNU General Public License Version 2
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 #ifdef HAVE_CONFIG_H
25   #include "config.h"
26 #endif
27
28 #include <string.h>
29
30 #ifdef HAVE_UDEV
31   #define G_UDEV_API_IS_SUBJECT_TO_CHANGE 1
32   #include <gudev/gudev.h>
33 #else
34   #include <fcntl.h>
35   #include <unistd.h>
36   #include <sys/ioctl.h>
37   #if USE_SYS_VIDEOIO_H > 0
38     #include <sys/types.h>
39     #include <sys/videoio.h>
40   #elif defined (__sun)
41     #include <sys/types.h>
42     #include <sys/videodev2.h>
43   #endif /* USE_SYS_VIDEOIO_H */
44 #endif
45
46 #include "cheese-camera-device-monitor.h"
47
48 /**
49  * SECTION:cheese-camera-device-monitor
50  * @short_description: Simple object to enumerate v4l devices
51  * @include: cheese/cheese-camera-device-monitor.h
52  *
53  * #EmpathyCameraDeviceMonitor provides a basic interface for
54  * video4linux device enumeration and hotplugging.
55  *
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.
61  */
62
63 G_DEFINE_TYPE (EmpathyCameraDeviceMonitor, empathy_camera_device_monitor, G_TYPE_OBJECT)
64
65 #define EMPATHY_CAMERA_DEVICE_MONITOR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o),                               \
66                                                                                   EMPATHY_TYPE_CAMERA_DEVICE_MONITOR, \
67                                                                                   EmpathyCameraDeviceMonitorPrivate))
68
69 #define EMPATHY_CAMERA_DEVICE_MONITOR_ERROR empathy_camera_device_monitor_error_quark ()
70
71 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
72 #include "libempathy/empathy-debug.h"
73
74 enum EmpathyCameraDeviceMonitorError
75 {
76   EMPATHY_CAMERA_DEVICE_MONITOR_ERROR_UNKNOWN,
77   EMPATHY_CAMERA_DEVICE_MONITOR_ERROR_ELEMENT_NOT_FOUND
78 };
79
80 typedef struct
81 {
82 #ifdef HAVE_UDEV
83   GUdevClient *client;
84 #else
85   guint filler;
86 #endif /* HAVE_UDEV */
87 } EmpathyCameraDeviceMonitorPrivate;
88
89 enum
90 {
91   ADDED,
92   REMOVED,
93   LAST_SIGNAL
94 };
95
96 static guint monitor_signals[LAST_SIGNAL];
97
98 #if 0
99 GQuark
100 empathy_camera_device_monitor_error_quark (void)
101 {
102   return g_quark_from_static_string ("empathy-camera-error-quark");
103 }
104 #endif
105
106 #ifdef HAVE_UDEV
107 static void
108 empathy_camera_device_monitor_added (EmpathyCameraDeviceMonitor *monitor,
109                                     GUdevDevice               *udevice)
110 {
111   const char *device_file;
112   const char *product_name;
113   const char *vendor;
114   const char *product;
115   const char *bus;
116   gint        vendor_id   = 0;
117   gint        product_id  = 0;
118   gint        v4l_version = 0;
119
120   const gchar *devpath = g_udev_device_get_property (udevice, "DEVPATH");
121
122   DEBUG ("Checking udev device '%s'", devpath);
123
124   bus = g_udev_device_get_property (udevice, "ID_BUS");
125   if (g_strcmp0 (bus, "usb") == 0)
126   {
127     vendor = g_udev_device_get_property (udevice, "ID_VENDOR_ID");
128     if (vendor != NULL)
129       vendor_id = g_ascii_strtoll (vendor, NULL, 16);
130     product = g_udev_device_get_property (udevice, "ID_MODEL_ID");
131     if (product != NULL)
132       product_id = g_ascii_strtoll (product, NULL, 16);
133     if (vendor_id == 0 || product_id == 0)
134     {
135       DEBUG ("Error getting vendor and product id");
136     }
137     else
138     {
139       DEBUG ("Found device %04x:%04x, getting capabilities...", vendor_id, product_id);
140     }
141   }
142   else
143   {
144     DEBUG ("Not an usb device, skipping vendor and model id retrieval");
145   }
146
147   device_file = g_udev_device_get_device_file (udevice);
148   if (device_file == NULL)
149   {
150     DEBUG ("Error getting V4L device");
151     return;
152   }
153
154   /* vbi devices support capture capability too, but cannot be used,
155    * so detect them by device name */
156   if (strstr (device_file, "vbi"))
157   {
158     DEBUG ("Skipping vbi device: %s", device_file);
159     return;
160   }
161
162   v4l_version = g_udev_device_get_property_as_int (udevice, "ID_V4L_VERSION");
163   if (v4l_version == 2 || v4l_version == 1)
164   {
165     const char *caps;
166
167     caps = g_udev_device_get_property (udevice, "ID_V4L_CAPABILITIES");
168     if (caps == NULL || strstr (caps, ":capture:") == NULL)
169     {
170       DEBUG ("Device %s seems to not have the capture capability, (radio tuner?)"
171                    "Removing it from device list.", device_file);
172       return;
173     }
174     product_name = g_udev_device_get_property (udevice, "ID_V4L_PRODUCT");
175   }
176   else if (v4l_version == 0)
177   {
178     DEBUG ("Fix your udev installation to include v4l_id, ignoring %s", device_file);
179     return;
180   }
181   else
182   {
183     g_assert_not_reached ();
184   }
185
186   g_signal_emit (monitor, monitor_signals[ADDED], 0,
187                  devpath,
188                  device_file,
189                  product_name,
190                  v4l_version);
191 }
192
193 static void
194 empathy_camera_device_monitor_removed (EmpathyCameraDeviceMonitor *monitor,
195                                       GUdevDevice               *udevice)
196 {
197   g_signal_emit (monitor, monitor_signals[REMOVED], 0,
198                  g_udev_device_get_property (udevice, "DEVPATH"));
199 }
200
201 static void
202 empathy_camera_device_monitor_uevent_cb (GUdevClient               *client,
203                                         const gchar               *action,
204                                         GUdevDevice               *udevice,
205                                         EmpathyCameraDeviceMonitor *monitor)
206 {
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);
211 }
212
213 /**
214  * empathy_camera_device_monitor_coldplug:
215  * @monitor: a #EmpathyCameraDeviceMonitor object.
216  *
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.
221  */
222 void
223 empathy_camera_device_monitor_coldplug (EmpathyCameraDeviceMonitor *monitor)
224 {
225   EmpathyCameraDeviceMonitorPrivate *priv = EMPATHY_CAMERA_DEVICE_MONITOR_GET_PRIVATE (monitor);
226   GList                            *devices, *l;
227   gint                              i = 0;
228
229   if (priv->client == NULL)
230     return;
231
232   DEBUG ("Probing devices with udev...");
233
234   devices = g_udev_client_query_by_subsystem (priv->client, "video4linux");
235
236   /* Initialize camera structures */
237   for (l = devices; l != NULL; l = l->next)
238   {
239     empathy_camera_device_monitor_added (monitor, l->data);
240     g_object_unref (l->data);
241     i++;
242   }
243   g_list_free (devices);
244
245   if (i == 0) DEBUG ("No device found");
246 }
247
248 #else /* HAVE_UDEV */
249 void
250 empathy_camera_device_monitor_coldplug (EmpathyCameraDeviceMonitor *monitor)
251 {
252   #if 0
253   EmpathyCameraDeviceMonitorPrivate *priv = EMPATHY_CAMERA_DEVICE_MONITOR_GET_PRIVATE (monitor);
254   struct v4l2_capability            v2cap;
255   struct video_capability           v1cap;
256   int                               fd, ok;
257
258   if ((fd = open (device_path, O_RDONLY | O_NONBLOCK)) < 0)
259   {
260     g_warning ("Failed to open %s: %s", device_path, strerror (errno));
261     return;
262   }
263   ok = ioctl (fd, VIDIOC_QUERYCAP, &v2cap);
264   if (ok < 0)
265   {
266     ok = ioctl (fd, VIDIOCGCAP, &v1cap);
267     if (ok < 0)
268     {
269       g_warning ("Error while probing v4l capabilities for %s: %s",
270                  device_path, strerror (errno));
271       close (fd);
272       return;
273     }
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;
278   }
279   else
280   {
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);
284
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))
288     {
289       g_print ("Device %s seems to not have the capture capability, (radio tuner?)\n"
290                "Removing it from device list.\n", device_path);
291       close (fd);
292       return;
293     }
294     gstreamer_src = "v4l2src";
295     product_name  = (char *) v2cap.card;
296   }
297   close (fd);
298
299   GList *devices, *l;
300
301   g_print ("Probing devices with udev...\n");
302
303   if (priv->client == NULL)
304     return;
305
306   devices = g_udev_client_query_by_subsystem (priv->client, "video4linux");
307
308   /* Initialize camera structures */
309   for (l = devices; l != NULL; l = l->next)
310   {
311     empathy_camera_device_monitor_added (monitor, l->data);
312     g_object_unref (l->data);
313   }
314   g_list_free (devices);
315   #endif
316 }
317
318 #endif /* HAVE_UDEV */
319
320 static void
321 empathy_camera_device_monitor_finalize (GObject *object)
322 {
323 #ifdef HAVE_UDEV
324   EmpathyCameraDeviceMonitor *monitor = EMPATHY_CAMERA_DEVICE_MONITOR (object);
325   EmpathyCameraDeviceMonitorPrivate *priv = EMPATHY_CAMERA_DEVICE_MONITOR_GET_PRIVATE (monitor);
326
327   if (priv->client != NULL)
328   {
329     g_object_unref (priv->client);
330     priv->client = NULL;
331   }
332 #endif /* HAVE_UDEV */
333   G_OBJECT_CLASS (empathy_camera_device_monitor_parent_class)->finalize (object);
334 }
335
336 static void
337 empathy_camera_device_monitor_class_init (EmpathyCameraDeviceMonitorClass *klass)
338 {
339   GObjectClass *object_class = G_OBJECT_CLASS (klass);
340
341   object_class->finalize = empathy_camera_device_monitor_finalize;
342
343   /**
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.
350    *
351    * The ::added signal is emitted when a camera is added, or on start-up
352    * after #empathy_camera_device_monitor_colplug is called.
353    **/
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),
357                                          NULL, NULL,
358                                          g_cclosure_marshal_generic,
359                                          G_TYPE_NONE, 4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT);
360
361   /**
362    * EmpathyCameraDeviceMonitor::removed:
363    * @device: A private object representing the newly added camera
364    * @id: Device unique identifier.
365    *
366    * The ::removed signal is emitted when a camera is un-plugged, or
367    * disabled on the system.
368    **/
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),
372                                            NULL, NULL,
373                                            g_cclosure_marshal_generic,
374                                            G_TYPE_NONE, 1, G_TYPE_STRING);
375
376   g_type_class_add_private (klass, sizeof (EmpathyCameraDeviceMonitorPrivate));
377 }
378
379 static void
380 empathy_camera_device_monitor_init (EmpathyCameraDeviceMonitor *monitor)
381 {
382 #ifdef HAVE_UDEV
383   EmpathyCameraDeviceMonitorPrivate *priv         = EMPATHY_CAMERA_DEVICE_MONITOR_GET_PRIVATE (monitor);
384   const gchar *const                subsystems[] = {"video4linux", NULL};
385
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 */
390 }
391
392 /**
393  * empathy_camera_device_monitor_new:
394  *
395  * Returns a new #EmpathyCameraDeviceMonitor object.
396  *
397  * Return value: a new #EmpathyCameraDeviceMonitor object.
398  **/
399 EmpathyCameraDeviceMonitor *
400 empathy_camera_device_monitor_new (void)
401 {
402   return g_object_new (EMPATHY_TYPE_CAMERA_DEVICE_MONITOR, NULL);
403 }
404
405 /*
406  * vim: sw=2 ts=8 cindent noai bs=2
407  */