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 *****************************************************************************/
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
60 DBusConnection *p_connection;
62 struct udi_input_id_t **pp_devices;
65 static void Run ( services_discovery_t *p_intf );
67 static int Open ( vlc_object_t * );
68 static void Close( vlc_object_t * );
72 void DeviceAdded( LibHalContext *p_ctx, const char *psz_udi );
73 void DeviceRemoved( LibHalContext *p_ctx, const char *psz_udi );
74 /* to retrieve p_sd in HAL callbacks */
75 services_discovery_t *p_sd_global;
78 /*****************************************************************************
80 *****************************************************************************/
82 set_description( _("HAL devices detection") );
83 set_category( CAT_PLAYLIST );
84 set_subcategory( SUBCAT_PLAYLIST_SD );
86 set_capability( "services_discovery", 0 );
87 set_callbacks( Open, Close );
92 /*****************************************************************************
93 * Open: initialize and create stuff
94 *****************************************************************************/
95 static int Open( vlc_object_t *p_this )
97 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
98 services_discovery_sys_t *p_sys = malloc(
99 sizeof( services_discovery_sys_t ) );
102 DBusError dbus_error;
103 DBusConnection *p_connection;
106 p_sys->i_devices_number = 0;
107 p_sys->pp_devices = NULL;
114 dbus_error_init( &dbus_error );
116 p_sys->p_ctx = libhal_ctx_new();
119 msg_Err( p_sd, "unable to create HAL context") ;
123 p_connection = dbus_bus_get( DBUS_BUS_SYSTEM, &dbus_error );
124 if( dbus_error_is_set( &dbus_error ) )
126 msg_Err( p_sd, "unable to connect to DBUS: %s", dbus_error.message );
127 dbus_error_free( &dbus_error );
131 libhal_ctx_set_dbus_connection( p_sys->p_ctx, p_connection );
132 p_sys->p_connection = p_connection;
133 if( !libhal_ctx_init( p_sys->p_ctx, &dbus_error ) )
135 if( !(p_sys->p_ctx = hal_initialize( NULL, FALSE ) ) )
139 msg_Err( p_sd, "hal not available : %s", dbus_error.message );
140 dbus_error_free( &dbus_error );
142 msg_Err( p_sd, "hal not available" );
149 if( !libhal_ctx_set_device_added( p_sys->p_ctx, DeviceAdded ) ||
150 !libhal_ctx_set_device_removed( p_sys->p_ctx, DeviceRemoved ) )
152 msg_Err( p_sd, "unable to add callback" );
153 dbus_error_free( &dbus_error );
159 services_discovery_SetLocalizedName( p_sd, _("Devices") );
164 /*****************************************************************************
166 *****************************************************************************/
167 static void Close( vlc_object_t *p_this )
169 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
170 services_discovery_sys_t *p_sys = p_sd->p_sys;
172 dbus_connection_unref( p_sys->p_connection );
173 struct udi_input_id_t *p_udi_entry;
175 while( p_sys->i_devices_number > 0 )
177 p_udi_entry = p_sys->pp_devices[0];
178 if( p_udi_entry->psz_udi ) free( p_udi_entry->psz_udi );
179 TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
180 p_sys->pp_devices[0] );
181 if( p_udi_entry ) free( p_udi_entry );
183 p_sys->pp_devices = NULL;
188 static void AddItem( services_discovery_t *p_sd, input_item_t * p_input
194 services_discovery_sys_t *p_sys = p_sd->p_sys;
195 services_discovery_AddItem( p_sd, p_input, NULL /* no category */ );
198 struct udi_input_id_t *p_udi_entry;
199 p_udi_entry = malloc( sizeof( struct udi_input_id_t ) );
204 p_udi_entry->p_item = p_input;
205 p_udi_entry->psz_udi = strdup( psz_device );
206 TAB_APPEND( p_sys->i_devices_number, p_sys->pp_devices, p_udi_entry );
210 static void AddDvd( services_discovery_t *p_sd, char *psz_device )
214 char *psz_blockdevice;
215 input_item_t *p_input;
217 psz_name = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
218 psz_device, "volume.label", NULL );
219 psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
220 psz_device, "block.device", NULL );
222 psz_name = hal_device_get_property_string( p_sd->p_sys->p_ctx,
223 psz_device, "volume.label" );
224 psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
225 psz_device, "block.device" );
227 if( asprintf( &psz_uri, "dvd://%s", psz_blockdevice ) == -1 )
229 /* Create the playlist item here */
230 p_input = input_ItemNew( p_sd, psz_uri, psz_name );
237 AddItem( p_sd, p_input, psz_device );
239 AddItem( p_sd, p_input );
244 static void DelItem( services_discovery_t *p_sd, char* psz_udi )
246 services_discovery_sys_t *p_sys = p_sd->p_sys;
249 for( i = 0; i < p_sys->i_devices_number; i++ )
250 { /* looks for a matching udi */
251 if( strcmp( psz_udi, p_sys->pp_devices[i]->psz_udi ) == 0 )
252 { /* delete the corresponding item */
253 services_discovery_RemoveItem( p_sd, p_sys->pp_devices[i]->p_item );
254 if( p_sys->pp_devices[i]->psz_udi )
255 free( p_sys->pp_devices[i]->psz_udi );
256 TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
257 p_sys->pp_devices[i] );
263 static void AddCdda( services_discovery_t *p_sd, char *psz_device )
266 char *psz_blockdevice;
267 input_item_t *p_input;
269 psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
270 psz_device, "block.device", NULL );
272 psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
273 psz_device, "block.device" );
275 if( asprintf( &psz_uri, "cdda://%s", psz_blockdevice ) == -1 )
277 /* Create the item here */
278 p_input = input_ItemNew( p_sd, psz_uri, "Audio CD" );
283 AddItem( p_sd, p_input, psz_device );
285 AddItem( p_sd, p_input );
289 static void ParseDevice( services_discovery_t *p_sd, char *psz_device )
292 services_discovery_sys_t *p_sys = p_sd->p_sys;
294 if( libhal_device_property_exists( p_sys->p_ctx, psz_device,
295 "volume.disc.type", NULL ) )
297 psz_disc_type = libhal_device_get_property_string( p_sys->p_ctx,
302 if( hal_device_property_exists( p_sys->p_ctx, psz_device,
303 "volume.disc.type" ) )
305 psz_disc_type = hal_device_get_property_string( p_sys->p_ctx,
307 "volume.disc.type" );
309 if( !strncmp( psz_disc_type, "dvd_r", 5 ) )
312 /* hal 0.2.9.7 (HAVE_HAL) has not is_videodvd
313 * but hal 0.5.0 (HAVE_HAL_1) has */
314 if (libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
315 "volume.disc.is_videodvd", NULL ) )
317 AddDvd( p_sd, psz_device );
319 else if( !strncmp( psz_disc_type, "cd_r", 4 ) )
322 if( libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
323 "volume.disc.has_audio" , NULL ) )
325 if( hal_device_get_property_bool( p_sys->p_ctx, psz_device,
326 "volume.disc.has_audio" ) )
329 AddCdda( p_sd, psz_device );
335 /*****************************************************************************
336 * Run: main HAL thread
337 *****************************************************************************/
338 static void Run( services_discovery_t *p_sd )
342 services_discovery_sys_t *p_sys = p_sd->p_sys;
344 /* parse existing devices first */
346 if( ( devices = libhal_get_all_devices( p_sys->p_ctx, &i_devices, NULL ) ) )
348 if( ( devices = hal_get_all_devices( p_sys->p_ctx, &i_devices ) ) )
351 for( i = 0; i < i_devices; i++ )
353 ParseDevice( p_sd, devices[ i ] );
355 libhal_free_string( devices[ i ] );
357 hal_free_string( devices[ i ] );
363 while( !p_sd->b_die )
365 /* look for events on the bus, blocking 1 second */
366 dbus_connection_read_write_dispatch( p_sys->p_connection, 1000 );
367 /* HAL 0.5.8.1 can use libhal_ctx_get_dbus_connection(p_sys->p_ctx) */
374 void DeviceAdded( LibHalContext *p_ctx, const char *psz_udi )
376 ParseDevice( p_sd_global, (char*) psz_udi );
378 void DeviceRemoved( LibHalContext *p_ctx, const char *psz_udi )
380 DelItem( p_sd_global, (char*) psz_udi );