]> git.sesse.net Git - vlc/blob - modules/services_discovery/hal.c
udev: remove item when the device is removed
[vlc] / modules / services_discovery / hal.c
1 /*****************************************************************************
2  * hal.c :  HAL interface module
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * Copyright © 2006-2007 Rafaël Carré
6  * $Id$
7  *
8  * Authors: Clément Stenac <zorglub@videolan.org>
9  *          Rafaël Carré <funman at videolanorg>
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, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_playlist.h>
33 #include <vlc_services_discovery.h>
34
35 #include <vlc_network.h>
36
37 #include <errno.h>                                                 /* ENOMEM */
38
39 #ifdef HAVE_UNISTD_H
40 #    include <unistd.h>
41 #endif
42 #ifdef HAVE_SYS_TIME_H
43 #    include <sys/time.h>
44 #endif
45
46 #include <hal/libhal.h>
47
48 #define MAX_LINE_LENGTH 256
49
50 /*****************************************************************************
51  * Local prototypes
52  *****************************************************************************/
53
54 /* store relation between item id and udi for ejection */
55 struct udi_input_id_t
56 {
57     char            *psz_udi;
58     input_item_t    *p_item;
59 };
60
61 struct services_discovery_sys_t
62 {
63     vlc_thread_t            thread;
64     LibHalContext           *p_ctx;
65     DBusConnection          *p_connection;
66     int                     i_devices_number;
67     struct udi_input_id_t   **pp_devices;
68 };
69 static void *Run ( void * );
70 static int  Open ( vlc_object_t * );
71 static void Close( vlc_object_t * );
72
73 /* HAL callbacks */
74 void DeviceAdded( LibHalContext *p_ctx, const char *psz_udi );
75 void DeviceRemoved( LibHalContext *p_ctx, const char *psz_udi );
76
77 /* to retrieve p_sd in HAL callbacks */
78 services_discovery_t        *p_sd_global;
79
80 /*****************************************************************************
81  * Module descriptor
82  *****************************************************************************/
83 vlc_module_begin ()
84     set_description( N_("HAL devices detection") )
85     set_category( CAT_PLAYLIST )
86     set_subcategory( SUBCAT_PLAYLIST_SD )
87
88     set_capability( "services_discovery", 0 )
89     set_callbacks( Open, Close )
90
91 vlc_module_end ()
92
93
94 /*****************************************************************************
95  * Open: initialize and create stuff
96  *****************************************************************************/
97 static int Open( vlc_object_t *p_this )
98 {
99     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
100     services_discovery_sys_t *p_sys  = malloc(
101                                     sizeof( services_discovery_sys_t ) );
102     if( !p_sys )
103         return VLC_ENOMEM;
104
105     DBusError           dbus_error;
106     DBusConnection      *p_connection = NULL;
107
108     p_sd_global = p_sd;
109     p_sys->i_devices_number = 0;
110     p_sys->pp_devices = NULL;
111
112     p_sd->p_sys  = p_sys;
113
114     dbus_error_init( &dbus_error );
115
116     p_sys->p_ctx = libhal_ctx_new();
117     if( !p_sys->p_ctx )
118     {
119         msg_Err( p_sd, "unable to create HAL context") ;
120         free( p_sys );
121         return VLC_EGENERIC;
122     }
123     p_connection = dbus_bus_get( DBUS_BUS_SYSTEM, &dbus_error );
124     if( dbus_error_is_set( &dbus_error ) )
125     {
126         msg_Err( p_sd, "unable to connect to DBUS: %s", dbus_error.message );
127         goto error;
128     }
129     libhal_ctx_set_dbus_connection( p_sys->p_ctx, p_connection );
130     p_sys->p_connection = p_connection;
131     if( !libhal_ctx_init( p_sys->p_ctx, &dbus_error ) )
132     {
133         msg_Err( p_sd, "hal not available : %s", dbus_error.message );
134         goto error;
135     }
136
137     if( !libhal_ctx_set_device_added( p_sys->p_ctx, DeviceAdded ) ||
138             !libhal_ctx_set_device_removed( p_sys->p_ctx, DeviceRemoved ) )
139     {
140         msg_Err( p_sd, "unable to add callback" );
141         goto error;
142     }
143
144     if( vlc_clone( &p_sys->thread, Run, p_this, VLC_THREAD_PRIORITY_LOW ) )
145         goto error;
146
147     return VLC_SUCCESS;
148 error:
149     if( p_connection )
150         dbus_connection_unref( p_connection );
151     dbus_error_free( &dbus_error );
152     libhal_ctx_free( p_sys->p_ctx );
153     free( p_sys );
154     return VLC_EGENERIC;
155 }
156
157 /*****************************************************************************
158  * Close:
159  *****************************************************************************/
160 static void Close( vlc_object_t *p_this )
161 {
162     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
163     services_discovery_sys_t *p_sys  = p_sd->p_sys;
164
165     /*vlc_cancel( p_sys->thread );*/
166     vlc_object_kill( p_sd );
167     vlc_join( p_sys->thread, NULL );
168     dbus_connection_unref( p_sys->p_connection );
169     struct udi_input_id_t *p_udi_entry;
170
171     while( p_sys->i_devices_number > 0 )
172     {
173         p_udi_entry = p_sys->pp_devices[0];
174         free( p_udi_entry->psz_udi );
175         TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
176                 p_sys->pp_devices[0] );
177         free( p_udi_entry );
178     }
179     p_sys->pp_devices = NULL;
180
181     libhal_ctx_free( p_sys->p_ctx );
182
183     free( p_sys );
184 }
185
186 static void AddItem( services_discovery_t *p_sd, input_item_t * p_input,
187                     const char* psz_device )
188 {
189     services_discovery_sys_t *p_sys  = p_sd->p_sys;
190     services_discovery_AddItem( p_sd, p_input, NULL /* no category */ );
191
192     struct udi_input_id_t *p_udi_entry;
193     p_udi_entry = malloc( sizeof( struct udi_input_id_t ) );
194     if( !p_udi_entry )
195         return;
196     p_udi_entry->psz_udi = strdup( psz_device );
197     if( !p_udi_entry->psz_udi )
198     {
199         free( p_udi_entry );
200         return;
201     }
202
203     vlc_gc_incref( p_input );
204     p_udi_entry->p_item = p_input;
205     TAB_APPEND( p_sys->i_devices_number, p_sys->pp_devices, p_udi_entry );
206 }
207
208 static void AddDvd( services_discovery_t *p_sd, const char *psz_device )
209 {
210     char *psz_name;
211     char *psz_uri;
212     char *psz_blockdevice;
213     input_item_t        *p_input;
214
215     psz_name = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
216                                         psz_device, "volume.label", NULL );
217     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
218                                         psz_device, "block.device", NULL );
219
220     if( asprintf( &psz_uri, "dvd://%s", psz_blockdevice ) == -1 )
221         return;
222     /* Create the playlist item here */
223     p_input = input_item_New( p_sd, psz_uri, psz_name );
224     free( psz_uri );
225     if( !p_input )
226     {
227         return;
228     }
229
230     AddItem( p_sd, p_input, psz_device );
231
232     vlc_gc_decref( p_input );
233 }
234
235 static void DelItem( services_discovery_t *p_sd, const char* psz_udi )
236 {
237     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
238
239     int i;
240     for( i = 0; i < p_sys->i_devices_number; i++ )
241     { /*  looks for a matching udi */
242         if( strcmp( psz_udi, p_sys->pp_devices[i]->psz_udi ) == 0 )
243         { /* delete the corresponding item */    
244             services_discovery_RemoveItem( p_sd, p_sys->pp_devices[i]->p_item );
245             vlc_gc_decref( p_sys->pp_devices[i]->p_item );
246             free( p_sys->pp_devices[i]->psz_udi );
247             TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
248                     p_sys->pp_devices[i] );
249         }
250     }
251 }
252
253 static void AddCdda( services_discovery_t *p_sd, const char *psz_device )
254 {
255     char *psz_uri;
256     char *psz_blockdevice;
257     input_item_t     *p_input;
258
259     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
260                                             psz_device, "block.device", NULL );
261
262     if( asprintf( &psz_uri, "cdda://%s", psz_blockdevice ) == -1 )
263         return;
264     /* Create the item here */
265     p_input = input_item_New( p_sd, psz_uri, "Audio CD" );
266     free( psz_uri );
267     if( !p_input )
268         return;
269
270     AddItem( p_sd, p_input, psz_device );
271
272     vlc_gc_decref( p_input );
273 }
274
275 static void ParseDevice( services_discovery_t *p_sd, const char *psz_device )
276 {
277     char *psz_disc_type;
278     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
279
280     if( !libhal_device_property_exists( p_sys->p_ctx, psz_device,
281                                        "volume.disc.type", NULL ) )
282         return;
283
284     psz_disc_type = libhal_device_get_property_string( p_sys->p_ctx,
285                                                     psz_device,
286                                                     "volume.disc.type",
287                                                     NULL );
288     if( !strncmp( psz_disc_type, "dvd_r", 5 ) )
289     {
290         if (libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
291                                      "volume.disc.is_videodvd", NULL ) )
292         AddDvd( p_sd, psz_device );
293     }
294     else if( !strncmp( psz_disc_type, "cd_r", 4 ) )
295     {
296         if( libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
297                                      "volume.disc.has_audio" , NULL ) )
298             AddCdda( p_sd, psz_device );
299     }
300 }
301
302 /*****************************************************************************
303  * Run: main HAL thread
304  *****************************************************************************/
305 static void *Run( void *data )
306 {
307     services_discovery_t     *p_sd  = data;
308     services_discovery_sys_t *p_sys = p_sd->p_sys;
309     char **devices;
310     int i, i_devices;
311     int canc = vlc_savecancel();
312
313     /* parse existing devices first */
314     if( ( devices = libhal_get_all_devices( p_sys->p_ctx, &i_devices, NULL ) ) )
315     {
316         for( i = 0; i < i_devices; i++ )
317         {
318             ParseDevice( p_sd, devices[ i ] );
319             libhal_free_string( devices[ i ] );
320         }
321         free( devices );
322     }
323
324     /* FIXME: Totally lame. There are DBus watch functions to do this properly.
325      * -- Courmisch, 28/08/2008 */
326     while( vlc_object_alive (p_sd) )
327     {
328         /* look for events on the bus, blocking 1 second */
329         dbus_connection_read_write_dispatch( p_sys->p_connection, 1000 );
330         /* HAL 0.5.8.1 can use libhal_ctx_get_dbus_connection(p_sys->p_ctx) */
331     }
332     vlc_restorecancel (canc);
333     return NULL;
334 }
335
336 void DeviceAdded( LibHalContext *p_ctx, const char *psz_udi )
337 {
338     VLC_UNUSED(p_ctx);
339     ParseDevice( p_sd_global, psz_udi );
340 }
341
342 void DeviceRemoved( LibHalContext *p_ctx, const char *psz_udi )
343 {
344     VLC_UNUSED(p_ctx);
345     DelItem( p_sd_global, psz_udi );
346 }