1 /*****************************************************************************
2 * hal.c : HAL interface module
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
5 * Copyright © 2006-2007 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 *****************************************************************************/
31 #include <vlc_playlist.h>
33 #include <vlc_network.h>
35 #include <errno.h> /* ENOMEM */
40 #ifdef HAVE_SYS_TIME_H
41 # include <sys/time.h>
44 #include <hal/libhal.h>
46 #define MAX_LINE_LENGTH 256
48 /*****************************************************************************
50 *****************************************************************************/
52 /* store relation between item id and udi for ejection */
60 struct services_discovery_sys_t
64 DBusConnection *p_connection;
66 struct udi_input_id_t **pp_devices;
69 static void Run ( services_discovery_t *p_intf );
71 static int Open ( vlc_object_t * );
72 static void Close( vlc_object_t * );
76 void DeviceAdded( LibHalContext *p_ctx, const char *psz_udi );
77 void DeviceRemoved( LibHalContext *p_ctx, const char *psz_udi );
78 /* to retrieve p_sd in HAL callbacks */
79 services_discovery_t *p_sd_global;
82 /*****************************************************************************
84 *****************************************************************************/
86 set_description( _("HAL devices detection") );
87 set_category( CAT_PLAYLIST );
88 set_subcategory( SUBCAT_PLAYLIST_SD );
90 set_capability( "services_discovery", 0 );
91 set_callbacks( Open, Close );
96 /*****************************************************************************
97 * Open: initialize and create stuff
98 *****************************************************************************/
99 static int Open( vlc_object_t *p_this )
101 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
102 services_discovery_sys_t *p_sys = malloc(
103 sizeof( services_discovery_sys_t ) );
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 services_discovery_SetLocalizedName( p_sd, _("Devices") );
168 /*****************************************************************************
170 *****************************************************************************/
171 static void Close( vlc_object_t *p_this )
173 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
174 services_discovery_sys_t *p_sys = p_sd->p_sys;
176 dbus_connection_unref( p_sys->p_connection );
177 struct udi_input_id_t *p_udi_entry;
179 while( p_sys->i_devices_number > 0 )
181 p_udi_entry = p_sys->pp_devices[0];
182 free( p_udi_entry->psz_udi );
183 TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
184 p_sys->pp_devices[0] );
187 p_sys->pp_devices = NULL;
192 static void AddItem( services_discovery_t *p_sd, input_item_t * p_input
194 , const char* psz_device
198 services_discovery_sys_t *p_sys = p_sd->p_sys;
199 services_discovery_AddItem( p_sd, p_input, NULL /* no category */ );
202 struct udi_input_id_t *p_udi_entry;
203 p_udi_entry = malloc( sizeof( struct udi_input_id_t ) );
208 vlc_gc_incref( p_input );
209 p_udi_entry->p_item = p_input;
210 p_udi_entry->psz_udi = strdup( psz_device );
211 TAB_APPEND( p_sys->i_devices_number, p_sys->pp_devices, p_udi_entry );
215 static void AddDvd( services_discovery_t *p_sd, const char *psz_device )
219 char *psz_blockdevice;
220 input_item_t *p_input;
222 psz_name = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
223 psz_device, "volume.label", NULL );
224 psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
225 psz_device, "block.device", NULL );
227 psz_name = hal_device_get_property_string( p_sd->p_sys->p_ctx,
228 psz_device, "volume.label" );
229 psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
230 psz_device, "block.device" );
232 if( asprintf( &psz_uri, "dvd://%s", psz_blockdevice ) == -1 )
234 /* Create the playlist item here */
235 p_input = input_ItemNew( p_sd, psz_uri, psz_name );
242 AddItem( p_sd, p_input, psz_device );
244 AddItem( p_sd, p_input );
246 vlc_gc_decref( p_input );
250 static void DelItem( services_discovery_t *p_sd, const char* psz_udi )
252 services_discovery_sys_t *p_sys = p_sd->p_sys;
255 for( i = 0; i < p_sys->i_devices_number; i++ )
256 { /* looks for a matching udi */
257 if( strcmp( psz_udi, p_sys->pp_devices[i]->psz_udi ) == 0 )
258 { /* delete the corresponding item */
259 services_discovery_RemoveItem( p_sd, p_sys->pp_devices[i]->p_item );
260 vlc_gc_decref( p_sys->pp_devices[i]->p_item );
261 free( p_sys->pp_devices[i]->psz_udi );
262 TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
263 p_sys->pp_devices[i] );
269 static void AddCdda( services_discovery_t *p_sd, const char *psz_device )
272 char *psz_blockdevice;
273 input_item_t *p_input;
275 psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
276 psz_device, "block.device", NULL );
278 psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
279 psz_device, "block.device" );
281 if( asprintf( &psz_uri, "cdda://%s", psz_blockdevice ) == -1 )
283 /* Create the item here */
284 p_input = input_ItemNew( p_sd, psz_uri, "Audio CD" );
289 AddItem( p_sd, p_input, psz_device );
291 AddItem( p_sd, p_input );
293 vlc_gc_decref( p_input );
296 static void ParseDevice( services_discovery_t *p_sd, const char *psz_device )
299 services_discovery_sys_t *p_sys = p_sd->p_sys;
301 if( libhal_device_property_exists( p_sys->p_ctx, psz_device,
302 "volume.disc.type", NULL ) )
304 psz_disc_type = libhal_device_get_property_string( p_sys->p_ctx,
309 if( hal_device_property_exists( p_sys->p_ctx, psz_device,
310 "volume.disc.type" ) )
312 psz_disc_type = hal_device_get_property_string( p_sys->p_ctx,
314 "volume.disc.type" );
316 if( !strncmp( psz_disc_type, "dvd_r", 5 ) )
319 /* hal 0.2.9.7 (HAVE_HAL) has not is_videodvd
320 * but hal 0.5.0 (HAVE_HAL_1) has */
321 if (libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
322 "volume.disc.is_videodvd", NULL ) )
324 AddDvd( p_sd, psz_device );
326 else if( !strncmp( psz_disc_type, "cd_r", 4 ) )
329 if( libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
330 "volume.disc.has_audio" , NULL ) )
332 if( hal_device_get_property_bool( p_sys->p_ctx, psz_device,
333 "volume.disc.has_audio" ) )
336 AddCdda( p_sd, psz_device );
342 /*****************************************************************************
343 * Run: main HAL thread
344 *****************************************************************************/
345 static void Run( services_discovery_t *p_sd )
349 services_discovery_sys_t *p_sys = p_sd->p_sys;
351 /* parse existing devices first */
353 if( ( devices = libhal_get_all_devices( p_sys->p_ctx, &i_devices, NULL ) ) )
355 if( ( devices = hal_get_all_devices( p_sys->p_ctx, &i_devices ) ) )
358 for( i = 0; i < i_devices; i++ )
360 ParseDevice( p_sd, devices[ i ] );
362 libhal_free_string( devices[ i ] );
364 hal_free_string( devices[ i ] );
370 while( !p_sd->b_die )
372 /* look for events on the bus, blocking 1 second */
373 dbus_connection_read_write_dispatch( p_sys->p_connection, 1000 );
374 /* HAL 0.5.8.1 can use libhal_ctx_get_dbus_connection(p_sys->p_ctx) */
381 void DeviceAdded( LibHalContext *p_ctx, const char *psz_udi )
384 ParseDevice( p_sd_global, psz_udi );
386 void DeviceRemoved( LibHalContext *p_ctx, const char *psz_udi )
389 DelItem( p_sd_global, psz_udi );