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