]> git.sesse.net Git - vlc/blob - modules/services_discovery/udev.c
Initial udev service discovery plugin
[vlc] / modules / services_discovery / udev.c
1 /**
2  * @file udev.c
3  * @brief List of multimedia devices for VLC media player
4  */
5 /*****************************************************************************
6  * Copyright © 2009 Rémi Denis-Courmont
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 License
10  * as published by the Free Software Foundation; either version 2.1
11  * 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
16  * GNU 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, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  ****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <libudev.h>
28 #include <vlc_common.h>
29 #include <vlc_services_discovery.h>
30 #include <vlc_plugin.h>
31 #include <poll.h>
32 #include <errno.h>
33
34 static int  Open (vlc_object_t *);
35 static void Close (vlc_object_t *);
36
37 /*
38  * Module descriptor
39  */
40 vlc_module_begin ()
41     set_shortname (N_("Devices"))
42     set_description (N_("Capture devices"))
43     set_category (CAT_PLAYLIST)
44     set_subcategory (SUBCAT_PLAYLIST_SD)
45     set_capability ("services_discovery", 0)
46     set_callbacks (Open, Close)
47
48     add_shortcut ("udev")
49 vlc_module_end ()
50
51 struct services_discovery_sys_t
52 {
53     struct udev_monitor *monitor;
54     vlc_thread_t         thread;
55 };
56
57 static void *Run (void *);
58 static void HandleDevice (services_discovery_t *, struct udev_device *, bool);
59
60 /**
61  * Probes and initializes.
62  */
63 static int Open (vlc_object_t *obj)
64 {
65     const char subsys[] = "video4linux";
66     services_discovery_t *sd = (services_discovery_t *)obj;
67     services_discovery_sys_t *p_sys = malloc (sizeof (*p_sys));
68
69     if (p_sys == NULL)
70         return VLC_ENOMEM;
71     sd->p_sys = p_sys;
72
73     struct udev_monitor *mon = NULL;
74     struct udev *udev = udev_new ();
75     if (udev == NULL)
76         goto error;
77
78     mon = udev_monitor_new_from_netlink (udev, "udev");
79     if (mon == NULL
80      || udev_monitor_filter_add_match_subsystem_devtype (mon, subsys, NULL))
81         goto error;
82     p_sys->monitor = mon;
83
84     /* Enumerate existing devices */
85     struct udev_enumerate *devenum = udev_enumerate_new (udev);
86     if (devenum == NULL)
87         goto error;
88     if (udev_enumerate_add_match_subsystem (devenum, subsys))
89     {
90         udev_enumerate_unref (devenum);
91         goto error;
92     }
93
94     udev_monitor_enable_receiving (mon);
95     /* Note that we enumerate _after_ monitoring is enabled so that we do not
96      * loose device events occuring while we are enumerating. We could still
97      * loose events if the Netlink socket receive buffer overflows. */
98     udev_enumerate_scan_devices (devenum);
99     struct udev_list_entry *devlist = udev_enumerate_get_list_entry (devenum);
100     struct udev_list_entry *deventry;
101     udev_list_entry_foreach (deventry, devlist)
102     {
103         const char *path = udev_list_entry_get_name (deventry);
104         struct udev_device *dev = udev_device_new_from_syspath (udev, path);
105         HandleDevice (sd, dev, true);
106         udev_device_unref (dev);
107     }
108     udev_enumerate_unref (devenum);
109
110     if (vlc_clone (&p_sys->thread, Run, sd, VLC_THREAD_PRIORITY_LOW))
111         goto error;
112     return VLC_SUCCESS;
113
114 error:
115     if (mon != NULL)
116         udev_monitor_unref (mon);
117     if (udev != NULL)
118         udev_unref (udev);
119     free (p_sys);
120     return VLC_EGENERIC;
121 }
122
123
124 /**
125  * Releases resources
126  */
127 static void Close (vlc_object_t *obj)
128 {
129     services_discovery_t *sd = (services_discovery_t *)obj;
130     services_discovery_sys_t *p_sys = sd->p_sys;
131     struct udev *udev = udev_monitor_get_udev (p_sys->monitor);
132
133     vlc_cancel (p_sys->thread);
134     vlc_join (p_sys->thread, NULL);
135     udev_monitor_unref (p_sys->monitor);
136     udev_unref (udev);
137     free (p_sys);
138 }
139
140
141 static void *Run (void *data)
142 {
143     services_discovery_t *sd = data;
144     services_discovery_sys_t *p_sys = sd->p_sys;
145     struct udev_monitor *mon = p_sys->monitor;
146
147     int fd = udev_monitor_get_fd (mon);
148     struct pollfd ufd = { .fd = fd, .events = POLLIN, };
149
150     for (;;)
151     {
152         while (poll (&ufd, 1, -1) == -1)
153             if (errno != EINTR)
154                 break;
155
156         struct udev_device *dev = udev_monitor_receive_device (mon);
157         if (dev == NULL)
158             continue;
159
160         /* FIXME: handle change, offline, online */
161         if (!strcmp (udev_device_get_action (dev), "add"))
162             HandleDevice (sd, dev, true);
163         else if (!strcmp (udev_device_get_action (dev), "remove"))
164             HandleDevice (sd, dev, false);
165
166         //udev_device_unref (dev);
167     }
168     return NULL;
169 }
170
171 static int hex (char c)
172 {
173     if (c >= '0' && c <= '9')
174         return c - '0';
175     if (c >= 'A' && c <= 'F')
176         return c + 10 - 'A';
177     if (c >= 'a' && c <= 'f')
178         return c + 10 - 'a';
179     return -1;
180 }
181
182 static char *decode (const char *enc)
183 {
184     char *ret = enc ? strdup (enc) : NULL;
185     if (ret == NULL)
186         return NULL;
187
188     char *out = ret;
189     for (const char *in = ret; *in; out++)
190     {
191         int h1, h2;
192
193         if ((in[0] == '\\') && (in[1] == 'x')
194          && ((h1 = hex (in[2])) != -1)
195          && ((h2 = hex (in[3])) != -1))
196         {
197             *out = (h1 << 4) | h2;
198             in += 4;
199         }
200         else
201         {
202             *out = *in;
203             in++;
204         }
205     }
206     *out = 0;
207     return ret;
208 }
209
210 static char *decode_property (struct udev_device *dev, const char *name)
211 {
212     return decode (udev_device_get_property_value (dev, name));
213 }
214
215 static void HandleDevice (services_discovery_t *sd, struct udev_device *dev,
216                           bool add)
217 {
218     //services_discovery_sys_t *p_sys = sd->p_sys;
219     if (!add)
220     {
221         msg_Err (sd, "FIXME: removing device not implemented!");
222         return;
223     }
224
225     const char *scheme = "v4l2"; /* FIXME: V4L v1 */
226     const char *node = udev_device_get_devnode (dev);
227     char *vnd = decode_property (dev, "ID_VENDOR_ENC");
228     char *name = decode_property (dev, "ID_MODEL_ENC");
229
230     char *mrl;
231     if (asprintf (&mrl, "%s://%s", scheme, node) == -1)
232         return;
233
234     /* FIXME: check for duplicates (race between monitor starting to receive
235      * and initial enumeration). */
236     input_item_t *item;
237     item = input_item_NewWithType (VLC_OBJECT (sd), mrl,
238                                    name ? name : "Unnamed",
239                                    0, NULL, 0, -1, ITEM_TYPE_CARD);
240     msg_Dbg (sd, "adding %s", mrl);
241     free (name);
242     free (mrl);
243
244     if (item != NULL)
245     {
246         services_discovery_AddItem (sd, item, vnd ? vnd : "Generic");
247         vlc_gc_decref (item);
248     }
249     free (vnd);
250 }