1 /*****************************************************************************
2 * hal.c : HAL interface module
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
27 #include <vlc/input.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 *****************************************************************************/
47 struct services_discovery_sys_t
50 playlist_item_t *p_node_cat;
51 playlist_item_t *p_node_one;
53 static void AddItem( services_discovery_t *p_sd, input_item_t * p_input );
54 static void Run ( services_discovery_t *p_intf );
56 static int Open ( vlc_object_t * );
57 static void Close( vlc_object_t * );
59 /*****************************************************************************
61 *****************************************************************************/
63 set_description( _("HAL devices detection") );
64 set_category( CAT_PLAYLIST );
65 set_subcategory( SUBCAT_PLAYLIST_SD );
67 set_capability( "services_discovery", 0 );
68 set_callbacks( Open, Close );
73 /*****************************************************************************
74 * Open: initialize and create stuff
75 *****************************************************************************/
76 static int Open( vlc_object_t *p_this )
78 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
79 services_discovery_sys_t *p_sys = malloc(
80 sizeof( services_discovery_sys_t ) );
83 playlist_t *p_playlist;
86 DBusConnection *p_connection;
91 dbus_error_init( &dbus_error );
94 p_sys->p_ctx = libhal_ctx_new();
97 msg_Err( p_sd, "unable to create HAL context") ;
101 p_connection = dbus_bus_get( DBUS_BUS_SYSTEM, &dbus_error );
102 if( dbus_error_is_set( &dbus_error ) )
104 msg_Err( p_sd, "unable to connect to DBUS: %s", dbus_error.message );
105 dbus_error_free( &dbus_error );
109 libhal_ctx_set_dbus_connection( p_sys->p_ctx, p_connection );
110 if( !libhal_ctx_init( p_sys->p_ctx, &dbus_error ) )
112 if( !(p_sys->p_ctx = hal_initialize( NULL, FALSE ) ) )
115 msg_Err( p_sd, "hal not available : %s", dbus_error.message );
116 dbus_error_free( &dbus_error );
121 /* Create our playlist node */
122 p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
126 msg_Warn( p_sd, "unable to find playlist, cancelling HAL listening");
130 p_sys->p_node_cat = playlist_NodeCreate( p_playlist, _("Devices"),
131 p_playlist->p_root_category );
132 p_sys->p_node_cat->i_flags |= PLAYLIST_RO_FLAG;
133 p_sys->p_node_one = playlist_NodeCreate( p_playlist, _("Devices"),
134 p_playlist->p_root_onelevel );
135 p_sys->p_node_one->i_flags |= PLAYLIST_RO_FLAG;
137 val.b_bool = VLC_TRUE;
138 var_Set( p_playlist, "intf-change", val );
140 vlc_object_release( p_playlist );
145 /*****************************************************************************
147 *****************************************************************************/
148 static void Close( vlc_object_t *p_this )
150 services_discovery_t *p_sd = ( services_discovery_t* )p_this;
151 services_discovery_sys_t *p_sys = p_sd->p_sys;
152 playlist_t *p_playlist = (playlist_t *) vlc_object_find( p_sd,
153 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
156 playlist_NodeDelete( p_playlist, p_sys->p_node_cat, VLC_TRUE,VLC_TRUE );
157 playlist_NodeDelete( p_playlist, p_sys->p_node_one, VLC_TRUE,VLC_TRUE );
158 vlc_object_release( p_playlist );
163 static void AddDvd( services_discovery_t *p_sd, char *psz_device )
167 char *psz_blockdevice;
168 input_item_t *p_input;
170 psz_name = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
171 psz_device, "volume.label", NULL );
172 psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
173 psz_device, "block.device", NULL );
175 psz_name = hal_device_get_property_string( p_sd->p_sys->p_ctx,
176 psz_device, "volume.label" );
177 psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
178 psz_device, "block.device" );
180 asprintf( &psz_uri, "dvd://%s", psz_blockdevice );
181 /* Create the playlist item here */
182 p_input = input_ItemNew( p_sd, psz_uri, psz_name );
185 libhal_free_string( psz_device );
187 hal_free_string( psz_device );
193 AddItem( p_sd, p_input );
196 static void AddItem( services_discovery_t *p_sd, input_item_t * p_input )
198 playlist_item_t *p_item;
199 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_sd,
200 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
203 msg_Err( p_sd, "playlist not found" );
206 p_item = playlist_NodeAddInput( p_playlist, p_input,p_sd->p_sys->p_node_cat,
207 PLAYLIST_APPEND, PLAYLIST_END );
208 p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
209 p_item = playlist_NodeAddInput( p_playlist, p_input,p_sd->p_sys->p_node_one,
210 PLAYLIST_APPEND, PLAYLIST_END );
211 p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
213 vlc_object_release( p_playlist );
216 static void AddCdda( services_discovery_t *p_sd, char *psz_device )
218 char *psz_name = "Audio CD";
220 char *psz_blockdevice;
221 input_item_t *p_input;
223 psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
224 psz_device, "block.device", NULL );
226 psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
227 psz_device, "block.device" );
229 asprintf( &psz_uri, "cdda://%s", psz_blockdevice );
230 /* Create the playlist item here */
231 p_input = input_ItemNew( p_sd, psz_uri, psz_name );
234 libhal_free_string( psz_device );
236 hal_free_string( psz_device );
240 AddItem( p_sd, p_input );
243 static void ParseDevice( services_discovery_t *p_sd, char *psz_device )
246 services_discovery_sys_t *p_sys = p_sd->p_sys;
248 if( libhal_device_property_exists( p_sys->p_ctx, psz_device,
249 "volume.disc.type", NULL ) )
251 psz_disc_type = libhal_device_get_property_string( p_sys->p_ctx,
256 if( hal_device_property_exists( p_sys->p_ctx, psz_device,
257 "volume.disc.type" ) )
259 psz_disc_type = hal_device_get_property_string( p_sys->p_ctx,
261 "volume.disc.type" );
263 if( !strcmp( psz_disc_type, "dvd_rom" ) )
265 AddDvd( p_sd, psz_device );
267 else if( !strcmp( psz_disc_type, "cd_rom" ) )
270 if( libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
271 "volume.disc.has_audio" , NULL ) )
273 if( hal_device_get_property_bool( p_sys->p_ctx, psz_device,
274 "volume.disc.has_audio" ) )
277 AddCdda( p_sd, psz_device );
281 libhal_free_string( psz_disc_type );
283 hal_free_string( psz_disc_type );
288 /*****************************************************************************
289 * Run: main HAL thread
290 *****************************************************************************/
291 static void Run( services_discovery_t *p_sd )
295 services_discovery_sys_t *p_sys = p_sd->p_sys;
297 /* parse existing devices first */
299 if( ( devices = libhal_get_all_devices( p_sys->p_ctx, &i_devices, NULL ) ) )
301 if( ( devices = hal_get_all_devices( p_sys->p_ctx, &i_devices ) ) )
304 for( i = 0; i < i_devices; i++ )
306 ParseDevice( p_sd, devices[ i ] );
310 while( !p_sd->b_die )