1 /*****************************************************************************
2 * hal.c : HAL interface module
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
5 * Copyright (C) 2006 Rafaël Carré
8 * Authors: Clément Stenac <zorglub@videolan.org>
9 * Rafaël Carré <funman at videolanorg>
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.
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.
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 *****************************************************************************/
27 #include <vlc_playlist.h>
29 #include <vlc_network.h>
31 #include <errno.h> /* ENOMEM */
36 #ifdef HAVE_SYS_TIME_H
37 # include <sys/time.h>
40 #include <hal/libhal.h>
42 #define MAX_LINE_LENGTH 256
44 /*****************************************************************************
46 *****************************************************************************/
48 /* store relation between item id and udi for ejection */
56 struct services_discovery_sys_t
59 playlist_item_t *p_node_cat;
60 playlist_item_t *p_node_one;
62 DBusConnection *p_connection;
64 struct udi_input_id_t **pp_devices;
67 static void Run ( services_discovery_t *p_intf );
69 static int Open ( vlc_object_t * );
70 static void Close( vlc_object_t * );
74 void DeviceAdded( LibHalContext *p_ctx, const char *psz_udi );
75 void DeviceRemoved( LibHalContext *p_ctx, const char *psz_udi );
76 /* to retrieve p_sd in HAL callbacks */
77 services_discovery_t *p_sd_global;
80 /*****************************************************************************
82 *****************************************************************************/
84 set_description( _("HAL devices detection") );
85 set_category( CAT_PLAYLIST );
86 set_subcategory( SUBCAT_PLAYLIST_SD );
88 set_capability( "services_discovery", 0 );
89 set_callbacks( Open, Close );
94 /*****************************************************************************
95 * Open: initialize and create stuff
96 *****************************************************************************/
97 static int Open( vlc_object_t *p_this )
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 ) );
103 playlist_t *p_playlist;
106 DBusError dbus_error;
107 DBusConnection *p_connection;
110 p_sys->i_devices_number = 0;
111 p_sys->pp_devices = NULL;
118 dbus_error_init( &dbus_error );
120 p_sys->p_ctx = libhal_ctx_new();
123 msg_Err( p_sd, "unable to create HAL context") ;
127 p_connection = dbus_bus_get( DBUS_BUS_SYSTEM, &dbus_error );
128 if( dbus_error_is_set( &dbus_error ) )
130 msg_Err( p_sd, "unable to connect to DBUS: %s", dbus_error.message );
131 dbus_error_free( &dbus_error );
135 libhal_ctx_set_dbus_connection( p_sys->p_ctx, p_connection );
136 p_sys->p_connection = p_connection;
137 if( !libhal_ctx_init( p_sys->p_ctx, &dbus_error ) )
139 if( !(p_sys->p_ctx = hal_initialize( NULL, FALSE ) ) )
143 msg_Err( p_sd, "hal not available : %s", dbus_error.message );
144 dbus_error_free( &dbus_error );
146 msg_Err( p_sd, "hal not available" );
153 if( !libhal_ctx_set_device_added( p_sys->p_ctx, DeviceAdded ) ||
154 !libhal_ctx_set_device_removed( p_sys->p_ctx, DeviceRemoved ) )
156 msg_Err( p_sd, "unable to add callback" );
157 dbus_error_free( &dbus_error );
163 /* Create our playlist node */
164 p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
168 msg_Warn( p_sd, "unable to find playlist, cancelling HAL listening");
172 playlist_NodesPairCreate( p_playlist, _("Devices"),
173 &p_sys->p_node_cat, &p_sys->p_node_one,
175 vlc_object_release( p_playlist );
180 /*****************************************************************************
182 *****************************************************************************/
183 static void Close( vlc_object_t *p_this )
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 );
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 );
197 dbus_connection_unref( p_sys->p_connection );
199 struct udi_input_id_t *p_udi_entry;
201 while( p_sys->i_devices_number > 0 )
203 p_udi_entry = p_sys->pp_devices[0];
204 if( p_udi_entry->psz_udi ) free( p_udi_entry->psz_udi );
205 TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
206 p_sys->pp_devices[0] );
207 if( p_udi_entry ) free( p_udi_entry );
209 p_sys->pp_devices = NULL;
213 static void AddItem( services_discovery_t *p_sd, input_item_t * p_input
219 playlist_item_t *p_item_cat, *p_item_one;
220 services_discovery_sys_t *p_sys = p_sd->p_sys;
221 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_sd,
222 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
225 msg_Err( p_sd, "playlist not found" );
228 p_item_cat = playlist_NodeAddInput( p_playlist,
229 p_input,p_sd->p_sys->p_node_cat, PLAYLIST_APPEND, PLAYLIST_END,
231 p_item_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
232 p_item_one = playlist_NodeAddInput( p_playlist,
233 p_input,p_sd->p_sys->p_node_one, PLAYLIST_APPEND, PLAYLIST_END,
235 p_item_one->i_flags &= ~PLAYLIST_SKIP_FLAG;
237 vlc_object_release( p_playlist );
240 struct udi_input_id_t *p_udi_entry;
241 p_udi_entry = malloc( sizeof( struct udi_input_id_t ) );
246 p_udi_entry->i_id = p_item_cat->i_id;
247 p_udi_entry->psz_udi = strdup( psz_device );
248 TAB_APPEND( p_sys->i_devices_number, p_sys->pp_devices, p_udi_entry );
252 static void AddDvd( services_discovery_t *p_sd, char *psz_device )
256 char *psz_blockdevice;
257 input_item_t *p_input;
259 psz_name = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
260 psz_device, "volume.label", NULL );
261 psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
262 psz_device, "block.device", NULL );
264 psz_name = hal_device_get_property_string( p_sd->p_sys->p_ctx,
265 psz_device, "volume.label" );
266 psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
267 psz_device, "block.device" );
269 asprintf( &psz_uri, "dvd://%s", psz_blockdevice );
270 /* Create the playlist item here */
271 p_input = input_ItemNew( p_sd, psz_uri, psz_name );
278 AddItem( p_sd, p_input, psz_device );
280 AddItem( p_sd, p_input );
285 static void DelItem( services_discovery_t *p_sd, char* psz_udi )
287 services_discovery_sys_t *p_sys = p_sd->p_sys;
289 playlist_item_t *p_pl_item;
291 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_sd,
292 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
295 msg_Err( p_sd, "playlist not found" );
299 for( i = 0; i < p_sys->i_devices_number; i++ )
300 { /* looks for a matching udi */
301 if( strcmp( psz_udi, p_sys->pp_devices[i]->psz_udi ) == 0 )
302 { /* delete the corresponding item */
303 p_pl_item = playlist_ItemGetById( p_playlist,
304 p_sys->pp_devices[i]->i_id, VLC_FALSE );
308 while( p_pl_item->i_children > 0 )
309 { /* delete all childs */
310 playlist_DeleteFromInput( p_playlist,
311 p_pl_item->pp_children[j]->p_input->i_id, VLC_FALSE );
313 /* delete parent item */
315 /* HACK: if i_children == 0 the item won't be deleted
316 * That means that it _had_ children but they were deleted */
317 if( p_pl_item->i_children == 0 )
318 p_pl_item->i_children = -1;
320 playlist_DeleteFromInput( p_playlist,
321 p_pl_item->p_input->i_id, VLC_FALSE );
324 if( p_sys->pp_devices[i]->psz_udi )
325 free( p_sys->pp_devices[i]->psz_udi );
326 TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
327 p_sys->pp_devices[i] );
331 vlc_object_release( p_playlist );
335 static void AddCdda( services_discovery_t *p_sd, char *psz_device )
338 char *psz_blockdevice;
339 input_item_t *p_input;
341 psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
342 psz_device, "block.device", NULL );
344 psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
345 psz_device, "block.device" );
347 asprintf( &psz_uri, "cdda://%s", psz_blockdevice );
348 /* Create the playlist item here */
349 p_input = input_ItemNew( p_sd, psz_uri, "Audio CD" );
354 AddItem( p_sd, p_input, psz_device );
356 AddItem( p_sd, p_input );
360 static void ParseDevice( services_discovery_t *p_sd, char *psz_device )
363 services_discovery_sys_t *p_sys = p_sd->p_sys;
365 if( libhal_device_property_exists( p_sys->p_ctx, psz_device,
366 "volume.disc.type", NULL ) )
368 psz_disc_type = libhal_device_get_property_string( p_sys->p_ctx,
373 if( hal_device_property_exists( p_sys->p_ctx, psz_device,
374 "volume.disc.type" ) )
376 psz_disc_type = hal_device_get_property_string( p_sys->p_ctx,
378 "volume.disc.type" );
380 if( !strncmp( psz_disc_type, "dvd_r", 5 ) )
383 /* hal 0.2.9.7 (HAVE_HAL) has not is_videodvd
384 * but hal 0.5.0 (HAVE_HAL_1) has */
385 if (libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
386 "volume.disc.is_videodvd", NULL ) )
388 AddDvd( p_sd, psz_device );
390 else if( !strncmp( psz_disc_type, "cd_r", 4 ) )
393 if( libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
394 "volume.disc.has_audio" , NULL ) )
396 if( hal_device_get_property_bool( p_sys->p_ctx, psz_device,
397 "volume.disc.has_audio" ) )
400 AddCdda( p_sd, psz_device );
406 /*****************************************************************************
407 * Run: main HAL thread
408 *****************************************************************************/
409 static void Run( services_discovery_t *p_sd )
413 services_discovery_sys_t *p_sys = p_sd->p_sys;
415 /* parse existing devices first */
417 if( ( devices = libhal_get_all_devices( p_sys->p_ctx, &i_devices, NULL ) ) )
419 if( ( devices = hal_get_all_devices( p_sys->p_ctx, &i_devices ) ) )
422 for( i = 0; i < i_devices; i++ )
424 ParseDevice( p_sd, devices[ i ] );
426 libhal_free_string( devices[ i ] );
428 hal_free_string( devices[ i ] );
434 while( !p_sd->b_die )
436 /* look for events on the bus, blocking 1 second */
437 dbus_connection_read_write_dispatch( p_sys->p_connection, 1000 );
438 /* HAL 0.5.8.1 can use libhal_ctx_get_dbus_connection(p_sys->p_ctx) */
445 void DeviceAdded( LibHalContext *p_ctx, const char *psz_udi )
447 ParseDevice( p_sd_global, (char*) psz_udi );
449 void DeviceRemoved( LibHalContext *p_ctx, const char *psz_udi )
451 DelItem( p_sd_global, (char*) psz_udi );