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 *****************************************************************************/
29 #include <vlc/input.h>
33 #include <errno.h> /* ENOMEM */
38 #ifdef HAVE_SYS_TIME_H
39 # include <sys/time.h>
42 #include <hal/libhal.h>
44 #define MAX_LINE_LENGTH 256
46 /*****************************************************************************
48 *****************************************************************************/
50 /* store relation between item id and udi for ejection */
58 struct services_discovery_sys_t
61 playlist_item_t *p_node_cat;
62 playlist_item_t *p_node_one;
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 ) );
105 playlist_t *p_playlist;
108 DBusError dbus_error;
109 DBusConnection *p_connection;
112 p_sys->i_devices_number = 0;
113 p_sys->pp_devices = NULL;
120 dbus_error_init( &dbus_error );
122 p_sys->p_ctx = libhal_ctx_new();
125 msg_Err( p_sd, "unable to create HAL context") ;
129 p_connection = dbus_bus_get( DBUS_BUS_SYSTEM, &dbus_error );
130 if( dbus_error_is_set( &dbus_error ) )
132 msg_Err( p_sd, "unable to connect to DBUS: %s", dbus_error.message );
133 dbus_error_free( &dbus_error );
137 libhal_ctx_set_dbus_connection( p_sys->p_ctx, p_connection );
138 p_sys->p_connection = p_connection;
139 if( !libhal_ctx_init( p_sys->p_ctx, &dbus_error ) )
141 if( !(p_sys->p_ctx = hal_initialize( NULL, FALSE ) ) )
145 msg_Err( p_sd, "hal not available : %s", dbus_error.message );
146 dbus_error_free( &dbus_error );
148 msg_Err( p_sd, "hal not available" );
155 if( !libhal_ctx_set_device_added( p_sys->p_ctx, DeviceAdded ) ||
156 !libhal_ctx_set_device_removed( p_sys->p_ctx, DeviceRemoved ) )
158 msg_Err( p_sd, "unable to add callback" );
159 dbus_error_free( &dbus_error );
165 /* Create our playlist node */
166 p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
170 msg_Warn( p_sd, "unable to find playlist, cancelling HAL listening");
174 playlist_NodesPairCreate( p_playlist, _("Devices"),
175 &p_sys->p_node_cat, &p_sys->p_node_one,
177 vlc_object_release( p_playlist );
182 /*****************************************************************************
184 *****************************************************************************/
185 static void Close( vlc_object_t *p_this )
187 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
188 services_discovery_sys_t *p_sys = p_sd->p_sys;
189 playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_sd,
190 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
193 playlist_NodeDelete( p_playlist, p_sys->p_node_cat, VLC_TRUE,VLC_TRUE );
194 playlist_NodeDelete( p_playlist, p_sys->p_node_one, VLC_TRUE,VLC_TRUE );
195 vlc_object_release( p_playlist );
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;
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 = playlist_NodeAddInput( p_playlist, p_input,p_sd->p_sys->p_node_cat,
229 PLAYLIST_APPEND, PLAYLIST_END );
230 p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
231 p_item = playlist_NodeAddInput( p_playlist, p_input,p_sd->p_sys->p_node_one,
232 PLAYLIST_APPEND, PLAYLIST_END );
233 p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
235 vlc_object_release( p_playlist );
238 struct udi_input_id_t *p_udi_entry;
239 p_udi_entry = malloc( sizeof( struct udi_input_id_t ) );
244 p_udi_entry->i_id = p_item->i_id;
245 p_udi_entry->psz_udi = strdup( psz_device );
246 TAB_APPEND( p_sys->i_devices_number, p_sys->pp_devices, p_udi_entry );
250 static void AddDvd( services_discovery_t *p_sd, char *psz_device )
254 char *psz_blockdevice;
255 input_item_t *p_input;
257 psz_name = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
258 psz_device, "volume.label", NULL );
259 psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
260 psz_device, "block.device", NULL );
262 psz_name = hal_device_get_property_string( p_sd->p_sys->p_ctx,
263 psz_device, "volume.label" );
264 psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
265 psz_device, "block.device" );
267 asprintf( &psz_uri, "dvd://%s", psz_blockdevice );
268 /* Create the playlist item here */
269 p_input = input_ItemNew( p_sd, psz_uri, psz_name );
276 AddItem( p_sd, p_input, psz_device );
278 AddItem( p_sd, p_input );
283 static void DelItem( services_discovery_t *p_sd, char* psz_udi )
285 services_discovery_sys_t *p_sys = p_sd->p_sys;
288 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_sd,
289 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
292 msg_Err( p_sd, "playlist not found" );
296 for( i = 0; i < p_sys->i_devices_number; i++ )
298 if( strcmp( psz_udi, p_sys->pp_devices[i]->psz_udi ) == 0 )
300 playlist_DeleteFromItemId( p_playlist, p_sys->pp_devices[i]->i_id );
301 TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
302 p_sys->pp_devices[i] );
306 vlc_object_release( p_playlist );
310 static void AddCdda( services_discovery_t *p_sd, char *psz_device )
313 char *psz_blockdevice;
314 input_item_t *p_input;
316 psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
317 psz_device, "block.device", NULL );
319 psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
320 psz_device, "block.device" );
322 asprintf( &psz_uri, "cdda://%s", psz_blockdevice );
323 /* Create the playlist item here */
324 p_input = input_ItemNew( p_sd, psz_uri, "Audio CD" );
329 AddItem( p_sd, p_input, psz_device );
331 AddItem( p_sd, p_input );
335 static void ParseDevice( services_discovery_t *p_sd, char *psz_device )
338 services_discovery_sys_t *p_sys = p_sd->p_sys;
340 if( libhal_device_property_exists( p_sys->p_ctx, psz_device,
341 "volume.disc.type", NULL ) )
343 psz_disc_type = libhal_device_get_property_string( p_sys->p_ctx,
348 if( hal_device_property_exists( p_sys->p_ctx, psz_device,
349 "volume.disc.type" ) )
351 psz_disc_type = hal_device_get_property_string( p_sys->p_ctx,
353 "volume.disc.type" );
355 if( !strcmp( psz_disc_type, "dvd_rom" ) )
358 /* hal 0.2.9.7 (HAVE_HAL) has not is_videodvd
359 * but hal 0.5.0 (HAVE_HAL_1) has */
360 if (libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
361 "volume.disc.is_videodvd", NULL ) )
363 AddDvd( p_sd, psz_device );
365 else if( !strcmp( psz_disc_type, "cd_rom" ) )
368 if( libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
369 "volume.disc.has_audio" , NULL ) )
371 if( hal_device_get_property_bool( p_sys->p_ctx, psz_device,
372 "volume.disc.has_audio" ) )
375 AddCdda( p_sd, psz_device );
381 /*****************************************************************************
382 * Run: main HAL thread
383 *****************************************************************************/
384 static void Run( services_discovery_t *p_sd )
388 services_discovery_sys_t *p_sys = p_sd->p_sys;
390 /* parse existing devices first */
392 if( ( devices = libhal_get_all_devices( p_sys->p_ctx, &i_devices, NULL ) ) )
394 if( ( devices = hal_get_all_devices( p_sys->p_ctx, &i_devices ) ) )
397 for( i = 0; i < i_devices; i++ )
399 ParseDevice( p_sd, devices[ i ] );
401 libhal_free_string( devices[ i ] );
403 hal_free_string( devices[ i ] );
409 while( !p_sd->b_die )
411 /* look for events on the bus, blocking 1 second */
412 dbus_connection_read_write_dispatch( p_sys->p_connection, 1000 );
413 /* HAL 0.5.8.1 can use libhal_ctx_get_dbus_connection(p_sys->p_ctx) */
420 void DeviceAdded( LibHalContext *p_ctx, const char *psz_udi )
422 ParseDevice( p_sd_global, (char*) psz_udi );
424 void DeviceRemoved( LibHalContext *p_ctx, const char *psz_udi )
426 DelItem( p_sd_global, (char*) psz_udi );