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 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_playlist.h>
34 #include <vlc_network.h>
36 #include <errno.h> /* ENOMEM */
41 #ifdef HAVE_SYS_TIME_H
42 # include <sys/time.h>
45 #include <hal/libhal.h>
47 #define MAX_LINE_LENGTH 256
49 /*****************************************************************************
51 *****************************************************************************/
53 /* 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;
68 static void *Run ( void * );
69 static int Open ( vlc_object_t * );
70 static void Close( vlc_object_t * );
73 void DeviceAdded( LibHalContext *p_ctx, const char *psz_udi );
74 void DeviceRemoved( LibHalContext *p_ctx, const char *psz_udi );
76 /* to retrieve p_sd in HAL callbacks */
77 services_discovery_t *p_sd_global;
79 /*****************************************************************************
81 *****************************************************************************/
83 set_description( N_("HAL devices detection") )
84 set_category( CAT_PLAYLIST )
85 set_subcategory( SUBCAT_PLAYLIST_SD )
87 set_capability( "services_discovery", 0 )
88 set_callbacks( Open, Close )
93 /*****************************************************************************
94 * Open: initialize and create stuff
95 *****************************************************************************/
96 static int Open( vlc_object_t *p_this )
98 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
99 services_discovery_sys_t *p_sys = malloc(
100 sizeof( services_discovery_sys_t ) );
104 DBusError dbus_error;
105 DBusConnection *p_connection;
108 p_sys->i_devices_number = 0;
109 p_sys->pp_devices = NULL;
113 dbus_error_init( &dbus_error );
115 p_sys->p_ctx = libhal_ctx_new();
118 msg_Err( p_sd, "unable to create HAL context") ;
122 p_connection = dbus_bus_get( DBUS_BUS_SYSTEM, &dbus_error );
123 if( dbus_error_is_set( &dbus_error ) )
125 msg_Err( p_sd, "unable to connect to DBUS: %s", dbus_error.message );
126 dbus_error_free( &dbus_error );
130 libhal_ctx_set_dbus_connection( p_sys->p_ctx, p_connection );
131 p_sys->p_connection = p_connection;
132 if( !libhal_ctx_init( p_sys->p_ctx, &dbus_error ) )
134 msg_Err( p_sd, "hal not available : %s", dbus_error.message );
138 if( !libhal_ctx_set_device_added( p_sys->p_ctx, DeviceAdded ) ||
139 !libhal_ctx_set_device_removed( p_sys->p_ctx, DeviceRemoved ) )
141 msg_Err( p_sd, "unable to add callback" );
145 if( vlc_clone( &p_sys->thread, Run, p_this, VLC_THREAD_PRIORITY_LOW ) )
148 services_discovery_SetLocalizedName( p_sd, _("Devices") );
152 dbus_error_free( &dbus_error );
157 /*****************************************************************************
159 *****************************************************************************/
160 static void Close( vlc_object_t *p_this )
162 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
163 services_discovery_sys_t *p_sys = p_sd->p_sys;
165 /*vlc_cancel( p_sys->thread );*/
166 vlc_object_kill( p_sd );
167 vlc_join( p_sys->thread, NULL );
168 dbus_connection_unref( p_sys->p_connection );
169 struct udi_input_id_t *p_udi_entry;
171 while( p_sys->i_devices_number > 0 )
173 p_udi_entry = p_sys->pp_devices[0];
174 free( p_udi_entry->psz_udi );
175 TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
176 p_sys->pp_devices[0] );
179 p_sys->pp_devices = NULL;
184 static void AddItem( services_discovery_t *p_sd, input_item_t * p_input,
185 const char* psz_device )
187 services_discovery_sys_t *p_sys = p_sd->p_sys;
188 services_discovery_AddItem( p_sd, p_input, NULL /* no category */ );
190 struct udi_input_id_t *p_udi_entry;
191 p_udi_entry = malloc( sizeof( struct udi_input_id_t ) );
194 p_udi_entry->psz_udi = strdup( psz_device );
195 if( !p_udi_entry->psz_udi )
201 vlc_gc_incref( p_input );
202 p_udi_entry->p_item = p_input;
203 TAB_APPEND( p_sys->i_devices_number, p_sys->pp_devices, p_udi_entry );
206 static void AddDvd( services_discovery_t *p_sd, const char *psz_device )
210 char *psz_blockdevice;
211 input_item_t *p_input;
213 psz_name = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
214 psz_device, "volume.label", NULL );
215 psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
216 psz_device, "block.device", NULL );
218 if( asprintf( &psz_uri, "dvd://%s", psz_blockdevice ) == -1 )
220 /* Create the playlist item here */
221 p_input = input_item_New( p_sd, psz_uri, psz_name );
228 AddItem( p_sd, p_input, psz_device );
230 vlc_gc_decref( p_input );
233 static void DelItem( services_discovery_t *p_sd, const char* psz_udi )
235 services_discovery_sys_t *p_sys = p_sd->p_sys;
238 for( i = 0; i < p_sys->i_devices_number; i++ )
239 { /* looks for a matching udi */
240 if( strcmp( psz_udi, p_sys->pp_devices[i]->psz_udi ) == 0 )
241 { /* delete the corresponding item */
242 services_discovery_RemoveItem( p_sd, p_sys->pp_devices[i]->p_item );
243 vlc_gc_decref( p_sys->pp_devices[i]->p_item );
244 free( p_sys->pp_devices[i]->psz_udi );
245 TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
246 p_sys->pp_devices[i] );
251 static void AddCdda( services_discovery_t *p_sd, const char *psz_device )
254 char *psz_blockdevice;
255 input_item_t *p_input;
257 psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
258 psz_device, "block.device", NULL );
260 if( asprintf( &psz_uri, "cdda://%s", psz_blockdevice ) == -1 )
262 /* Create the item here */
263 p_input = input_item_New( p_sd, psz_uri, "Audio CD" );
268 AddItem( p_sd, p_input, psz_device );
270 vlc_gc_decref( p_input );
273 static void ParseDevice( services_discovery_t *p_sd, const char *psz_device )
276 services_discovery_sys_t *p_sys = p_sd->p_sys;
278 if( !libhal_device_property_exists( p_sys->p_ctx, psz_device,
279 "volume.disc.type", NULL ) )
282 psz_disc_type = libhal_device_get_property_string( p_sys->p_ctx,
286 if( !strncmp( psz_disc_type, "dvd_r", 5 ) )
288 if (libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
289 "volume.disc.is_videodvd", NULL ) )
290 AddDvd( p_sd, psz_device );
292 else if( !strncmp( psz_disc_type, "cd_r", 4 ) )
294 if( libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
295 "volume.disc.has_audio" , NULL ) )
296 AddCdda( p_sd, psz_device );
300 /*****************************************************************************
301 * Run: main HAL thread
302 *****************************************************************************/
303 static void *Run( void *data )
305 services_discovery_t *p_sd = data;
306 services_discovery_sys_t *p_sys = p_sd->p_sys;
309 int canc = vlc_savecancel();
311 /* parse existing devices first */
312 if( ( devices = libhal_get_all_devices( p_sys->p_ctx, &i_devices, NULL ) ) )
314 for( i = 0; i < i_devices; i++ )
316 ParseDevice( p_sd, devices[ i ] );
317 libhal_free_string( devices[ i ] );
321 /* FIXME: Totally lame. There are DBus watch functions to do this properly.
322 * -- Courmisch, 28/08/2008 */
323 while( vlc_object_alive (p_sd) )
325 /* look for events on the bus, blocking 1 second */
326 dbus_connection_read_write_dispatch( p_sys->p_connection, 1000 );
327 /* HAL 0.5.8.1 can use libhal_ctx_get_dbus_connection(p_sys->p_ctx) */
329 vlc_restorecancel (canc);
333 void DeviceAdded( LibHalContext *p_ctx, const char *psz_udi )
336 ParseDevice( p_sd_global, psz_udi );
339 void DeviceRemoved( LibHalContext *p_ctx, const char *psz_udi )
342 DelItem( p_sd_global, psz_udi );