]> git.sesse.net Git - vlc/blob - modules/services_discovery/hal.c
Fixes devices removal, and crash on exit
[vlc] / modules / services_discovery / hal.c
1 /*****************************************************************************
2  * hal.c :  HAL interface module
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * Copyright (C) 2006 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 #include <vlc/vlc.h>
27 #include <vlc/intf.h>
28
29 #include <vlc/input.h>
30
31 #include "network.h"
32
33 #include <errno.h>                                                 /* ENOMEM */
34
35 #ifdef HAVE_UNISTD_H
36 #    include <unistd.h>
37 #endif
38 #ifdef HAVE_SYS_TIME_H
39 #    include <sys/time.h>
40 #endif
41
42 #include <hal/libhal.h>
43
44 #define MAX_LINE_LENGTH 256
45
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49 #if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
50 /* store relation between item id and udi for ejection */
51 struct udi_input_id_t
52 {
53     char    *psz_udi;
54     int     i_id;
55 };
56 #endif
57
58 struct services_discovery_sys_t
59 {
60     LibHalContext *p_ctx;
61     playlist_item_t *p_node_cat;
62     playlist_item_t *p_node_one;
63 #if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
64     int                     i_devices_number;
65     struct udi_input_id_t   **pp_devices;
66 #endif
67 };
68 static void Run    ( services_discovery_t *p_intf );
69
70 static int  Open ( vlc_object_t * );
71 static void Close( vlc_object_t * );
72
73 #if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
74 /* HAL callbacks */
75 void DeviceAdded( LibHalContext *p_ctx, const char *psz_udi );
76 void DeviceRemoved( LibHalContext *p_ctx, const char *psz_udi );
77 /* to retrieve p_sd in HAL callbacks */
78 services_discovery_t        *p_sd_global;
79 #endif
80
81 /*****************************************************************************
82  * Module descriptor
83  *****************************************************************************/
84 vlc_module_begin();
85     set_description( _("HAL devices detection") );
86     set_category( CAT_PLAYLIST );
87     set_subcategory( SUBCAT_PLAYLIST_SD );
88
89     set_capability( "services_discovery", 0 );
90     set_callbacks( Open, Close );
91
92 vlc_module_end();
93
94
95 /*****************************************************************************
96  * Open: initialize and create stuff
97  *****************************************************************************/
98 static int Open( vlc_object_t *p_this )
99 {
100     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
101     services_discovery_sys_t *p_sys  = malloc(
102                                     sizeof( services_discovery_sys_t ) );
103
104     playlist_t          *p_playlist;
105
106 #if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
107     DBusError           dbus_error;
108     DBusConnection      *p_connection;
109
110     p_sd_global = p_sd;
111     p_sys->i_devices_number = 0;
112     p_sys->pp_devices = NULL;
113 #endif
114
115     p_sd->pf_run = Run;
116     p_sd->p_sys  = p_sys;
117
118 #if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
119     dbus_error_init( &dbus_error );
120
121     p_sys->p_ctx = libhal_ctx_new();
122     if( !p_sys->p_ctx )
123     {
124         msg_Err( p_sd, "unable to create HAL context") ;
125         free( p_sys );
126         return VLC_EGENERIC;
127     }
128     p_connection = dbus_bus_get( DBUS_BUS_SYSTEM, &dbus_error );
129     if( dbus_error_is_set( &dbus_error ) )
130     {
131         msg_Err( p_sd, "unable to connect to DBUS: %s", dbus_error.message );
132         dbus_error_free( &dbus_error );
133         free( p_sys );
134         return VLC_EGENERIC;
135     }
136     libhal_ctx_set_dbus_connection( p_sys->p_ctx, p_connection );
137     if( !libhal_ctx_init( p_sys->p_ctx, &dbus_error ) )
138 #else
139     if( !(p_sys->p_ctx = hal_initialize( NULL, FALSE ) ) )
140 #endif
141     {
142 #if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
143         msg_Err( p_sd, "hal not available : %s", dbus_error.message );
144         dbus_error_free( &dbus_error );
145 #else
146         msg_Err( p_sd, "hal not available" );
147 #endif
148         free( p_sys );
149         return VLC_EGENERIC;
150     }
151
152 #if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
153         if( !libhal_ctx_set_device_added( p_sys->p_ctx, DeviceAdded ) ||
154                 !libhal_ctx_set_device_removed( p_sys->p_ctx, DeviceRemoved ) )
155         {
156             msg_Err( p_sd, "unable to add callback" );
157             dbus_error_free( &dbus_error );
158             free( p_sys );
159             return VLC_EGENERIC;
160         }
161 #endif
162
163     /* Create our playlist node */
164     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
165                                                 FIND_ANYWHERE );
166     if( !p_playlist )
167     {
168         msg_Warn( p_sd, "unable to find playlist, cancelling HAL listening");
169         return VLC_EGENERIC;
170     }
171
172     playlist_NodesPairCreate( p_playlist, _("Devices"),
173                               &p_sys->p_node_cat, &p_sys->p_node_one,
174                               VLC_TRUE );
175     vlc_object_release( p_playlist );
176
177     return VLC_SUCCESS;
178 }
179
180 /*****************************************************************************
181  * Close:
182  *****************************************************************************/
183 static void Close( vlc_object_t *p_this )
184 {
185     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
186     services_discovery_sys_t *p_sys  = p_sd->p_sys;
187     playlist_t *p_playlist =  (playlist_t *) vlc_object_find( p_sd,
188                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
189     if( p_playlist )
190     {
191         playlist_NodeDelete( p_playlist, p_sys->p_node_cat, VLC_TRUE,VLC_TRUE );
192         playlist_NodeDelete( p_playlist, p_sys->p_node_one, VLC_TRUE,VLC_TRUE );
193         vlc_object_release( p_playlist );
194     }
195     free( p_sys );
196 #if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
197     struct udi_input_id_t *p_udi_entry;
198
199     while( p_sys->i_devices_number > 0 )
200     {
201         p_udi_entry = p_sys->pp_devices[0];
202         if( p_udi_entry->psz_udi ) free( p_udi_entry->psz_udi );
203         TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
204                 p_sys->pp_devices[0] );
205         if( p_udi_entry ) free( p_udi_entry );
206     }
207     p_sys->pp_devices = NULL;
208 #endif
209 }
210
211 static void AddItem( services_discovery_t *p_sd, input_item_t * p_input
212 #if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
213                 ,char* psz_device
214 #endif
215                     )
216 {
217     playlist_item_t *p_item;
218     services_discovery_sys_t *p_sys  = p_sd->p_sys;
219     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_sd,
220                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
221     if( !p_playlist )
222     {
223         msg_Err( p_sd, "playlist not found" );
224         return;
225     }
226     p_item = playlist_NodeAddInput( p_playlist, p_input,p_sd->p_sys->p_node_cat,
227                                     PLAYLIST_APPEND, PLAYLIST_END );
228     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
229     p_item = playlist_NodeAddInput( p_playlist, p_input,p_sd->p_sys->p_node_one,
230                                     PLAYLIST_APPEND, PLAYLIST_END );
231     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
232
233     vlc_object_release( p_playlist );
234
235 #if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
236     struct udi_input_id_t *p_udi_entry;
237     p_udi_entry = malloc( sizeof( struct udi_input_id_t ) );
238     if( !p_udi_entry )
239     {
240         return;
241     }
242     p_udi_entry->i_id = p_item->i_id;
243     p_udi_entry->psz_udi = strdup( psz_device );
244     TAB_APPEND( p_sys->i_devices_number, p_sys->pp_devices, p_udi_entry );
245 #endif
246 }
247
248 static void AddDvd( services_discovery_t *p_sd, char *psz_device )
249 {
250     char *psz_name;
251     char *psz_uri;
252     char *psz_blockdevice;
253     input_item_t        *p_input;
254 #if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
255     psz_name = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
256                                         psz_device, "volume.label", NULL );
257     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
258                                         psz_device, "block.device", NULL );
259 #else
260     psz_name = hal_device_get_property_string( p_sd->p_sys->p_ctx,
261                                                psz_device, "volume.label" );
262     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
263                                                  psz_device, "block.device" );
264 #endif
265     asprintf( &psz_uri, "dvd://%s", psz_blockdevice );
266     /* Create the playlist item here */
267     p_input = input_ItemNew( p_sd, psz_uri, psz_name );
268     free( psz_uri );
269     if( !p_input )
270     {
271         return;
272     }
273 #if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
274     AddItem( p_sd, p_input, psz_device );
275 #else
276     AddItem( p_sd, p_input );
277 #endif
278 }
279
280 #if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
281 static void DelItem( services_discovery_t *p_sd, char* psz_udi )
282 {
283     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
284     int                         i;
285
286     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_sd,
287                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
288     if( !p_playlist )
289     {
290         msg_Err( p_sd, "playlist not found" );
291         return;
292     }
293
294     for( i = 0; i < p_sys->i_devices_number; i++ )
295     {
296         if( strcmp( psz_udi, p_sys->pp_devices[i]->psz_udi ) == 0 )
297         {
298             playlist_DeleteFromItemId( p_playlist, p_sys->pp_devices[i]->i_id );
299             TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
300                     p_sys->pp_devices[i] );
301         }
302     }
303
304     vlc_object_release( p_playlist );
305 }
306 #endif
307
308 static void AddCdda( services_discovery_t *p_sd, char *psz_device )
309 {
310     char *psz_uri;
311     char *psz_blockdevice;
312     input_item_t     *p_input;
313 #if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
314     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
315                                             psz_device, "block.device", NULL );
316 #else
317     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
318                                                  psz_device, "block.device" );
319 #endif
320     asprintf( &psz_uri, "cdda://%s", psz_blockdevice );
321     /* Create the playlist item here */
322     p_input = input_ItemNew( p_sd, psz_uri, "Audio CD" );
323     free( psz_uri );
324     if( !p_input )
325         return;
326 #if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
327     AddItem( p_sd, p_input, psz_device );
328 #else
329     AddItem( p_sd, p_input );
330 #endif
331 }
332
333 static void ParseDevice( services_discovery_t *p_sd, char *psz_device )
334 {
335     char *psz_disc_type;
336     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
337 #if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
338     if( libhal_device_property_exists( p_sys->p_ctx, psz_device,
339                                        "volume.disc.type", NULL ) )
340     {
341         psz_disc_type = libhal_device_get_property_string( p_sys->p_ctx,
342                                                         psz_device,
343                                                         "volume.disc.type",
344                                                         NULL );
345 #else
346     if( hal_device_property_exists( p_sys->p_ctx, psz_device,
347                                     "volume.disc.type" ) )
348     {
349         psz_disc_type = hal_device_get_property_string( p_sys->p_ctx,
350                                                         psz_device,
351                                                         "volume.disc.type" );
352 #endif
353         if( !strcmp( psz_disc_type, "dvd_rom" ) )
354         {
355 #if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
356             /* hal 0.2.9.7 (HAVE_HAL) has not is_videodvd
357              * but hal 0.5.0 (HAVE_HAL_1) has */
358             if (libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
359                                          "volume.disc.is_videodvd", NULL ) )
360 #endif
361             AddDvd( p_sd, psz_device );
362         }
363         else if( !strcmp( psz_disc_type, "cd_rom" ) )
364         {
365 #if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
366             if( libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
367                                          "volume.disc.has_audio" , NULL ) )
368 #else
369             if( hal_device_get_property_bool( p_sys->p_ctx, psz_device,
370                                          "volume.disc.has_audio" ) )
371 #endif
372             {
373                 AddCdda( p_sd, psz_device );
374             }
375         }
376     }
377 }
378
379 /*****************************************************************************
380  * Run: main HAL thread
381  *****************************************************************************/
382 static void Run( services_discovery_t *p_sd )
383 {
384     int i, i_devices;
385     char **devices;
386     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
387
388     /* parse existing devices first */
389 #if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
390     if( ( devices = libhal_get_all_devices( p_sys->p_ctx, &i_devices, NULL ) ) )
391 #else
392     if( ( devices = hal_get_all_devices( p_sys->p_ctx, &i_devices ) ) )
393 #endif
394     {
395         for( i = 0; i < i_devices; i++ )
396         {
397             ParseDevice( p_sd, devices[ i ] );
398 #if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
399             libhal_free_string( devices[ i ] );
400 #else
401             hal_free_string( devices[ i ] );
402 #endif
403
404         }
405     }
406 #if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
407     while( !p_sd->b_die )
408     {
409     /* look for events on the bus, blocking 1 second */
410     dbus_connection_read_write_dispatch(
411             libhal_ctx_get_dbus_connection(p_sys->p_ctx), 1000 );
412     }
413 #endif
414
415 }
416
417 #if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
418 void DeviceAdded( LibHalContext *p_ctx, const char *psz_udi )
419 {
420         ParseDevice( p_sd_global, (char*) psz_udi );
421 }
422 void DeviceRemoved( LibHalContext *p_ctx, const char *psz_udi )
423 {
424         DelItem( p_sd_global, (char*) psz_udi );
425 }
426 #endif
427