Commit 16093d94 authored by Rafaël Carré's avatar Rafaël Carré

Manages hot ejection of devices

Uses hal callbacks instead of pure D-Bus monitoring
Uses HAL >= 0.5.0 only if D-Bus >= 0.9.2 is present
(refs #99)
parent 772fa346
......@@ -2,9 +2,11 @@
* hal.c : HAL interface module
*****************************************************************************
* Copyright (C) 2004 the VideoLAN team
* Copyright (C) 2006 Rafaël Carré
* $Id$
*
* Authors: Clément Stenac <zorglub@videolan.org>
* Rafaël Carré <funman at videolanorg>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -44,18 +46,38 @@
/*****************************************************************************
* Local prototypes
*****************************************************************************/
#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
/* store relation between item id and udi for ejection */
struct udi_input_id_t
{
char *psz_udi;
int i_id;
};
#endif
struct services_discovery_sys_t
{
LibHalContext *p_ctx;
playlist_item_t *p_node_cat;
playlist_item_t *p_node_one;
#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
int i_devices_number;
struct udi_input_id_t **pp_devices;
#endif
};
static void AddItem( services_discovery_t *p_sd, input_item_t * p_input );
static void Run ( services_discovery_t *p_intf );
static int Open ( vlc_object_t * );
static void Close( vlc_object_t * );
#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
/* HAL callbacks */
void DeviceAdded( LibHalContext *p_ctx, const char *psz_udi );
void DeviceRemoved( LibHalContext *p_ctx, const char *psz_udi );
/* to retrieve p_sd in HAL callbacks */
services_discovery_t *p_sd_global;
#endif
/*****************************************************************************
* Module descriptor
*****************************************************************************/
......@@ -84,6 +106,10 @@ static int Open( vlc_object_t *p_this )
#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
DBusError dbus_error;
DBusConnection *p_connection;
p_sd_global = p_sd;
p_sys->i_devices_number = 0;
p_sys->pp_devices = NULL;
#endif
p_sd->pf_run = Run;
......@@ -123,6 +149,17 @@ static int Open( vlc_object_t *p_this )
return VLC_EGENERIC;
}
#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
if( !libhal_ctx_set_device_added( p_sys->p_ctx, DeviceAdded ) ||
!libhal_ctx_set_device_removed( p_sys->p_ctx, DeviceRemoved ) )
{
msg_Err( p_sd, "unable to add callback" );
dbus_error_free( &dbus_error );
free( p_sys );
return VLC_EGENERIC;
}
#endif
/* Create our playlist node */
p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
FIND_ANYWHERE );
......@@ -156,6 +193,53 @@ static void Close( vlc_object_t *p_this )
vlc_object_release( p_playlist );
}
free( p_sys );
#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
while( p_sys->i_devices_number > 0 );
{
struct udi_input_id_t *p_udi_entry = p_sys->pp_devices[0];
if( p_udi_entry->psz_udi ) free( p_udi_entry->psz_udi );
TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices, 0 );
if( p_udi_entry ) free( p_udi_entry );
}
p_sys->pp_devices = NULL;
#endif
}
static void AddItem( services_discovery_t *p_sd, input_item_t * p_input
#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
,char* psz_device
#endif
)
{
playlist_item_t *p_item;
services_discovery_sys_t *p_sys = p_sd->p_sys;
playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_sd,
VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
if( !p_playlist )
{
msg_Err( p_sd, "playlist not found" );
return;
}
p_item = playlist_NodeAddInput( p_playlist, p_input,p_sd->p_sys->p_node_cat,
PLAYLIST_APPEND, PLAYLIST_END );
p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
p_item = playlist_NodeAddInput( p_playlist, p_input,p_sd->p_sys->p_node_one,
PLAYLIST_APPEND, PLAYLIST_END );
p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
vlc_object_release( p_playlist );
#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
struct udi_input_id_t *p_udi_entry;
p_udi_entry = malloc( sizeof( struct udi_input_id_t ) );
if( !p_udi_entry )
{
return;
}
p_udi_entry->i_id = p_item->i_id;
p_udi_entry->psz_udi = strdup( psz_device );
TAB_APPEND( p_sys->i_devices_number, p_sys->pp_devices, p_udi_entry );
#endif
}
static void AddDvd( services_discovery_t *p_sd, char *psz_device )
......@@ -164,7 +248,7 @@ static void AddDvd( services_discovery_t *p_sd, char *psz_device )
char *psz_uri;
char *psz_blockdevice;
input_item_t *p_input;
#ifdef HAVE_HAL_1
#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
psz_name = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
psz_device, "volume.label", NULL );
psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
......@@ -179,21 +263,23 @@ static void AddDvd( services_discovery_t *p_sd, char *psz_device )
/* Create the playlist item here */
p_input = input_ItemNew( p_sd, psz_uri, psz_name );
free( psz_uri );
#ifdef HAVE_HAL_1
libhal_free_string( psz_device );
#else
hal_free_string( psz_device );
#endif
if( !p_input )
{
return;
}
#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
AddItem( p_sd, p_input, psz_device );
#else
AddItem( p_sd, p_input );
#endif
}
static void AddItem( services_discovery_t *p_sd, input_item_t * p_input )
#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
static void DelItem( services_discovery_t *p_sd, char* psz_udi )
{
playlist_item_t *p_item;
services_discovery_sys_t *p_sys = p_sd->p_sys;
int i;
playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_sd,
VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
if( !p_playlist )
......@@ -201,23 +287,25 @@ static void AddItem( services_discovery_t *p_sd, input_item_t * p_input )
msg_Err( p_sd, "playlist not found" );
return;
}
p_item = playlist_NodeAddInput( p_playlist, p_input,p_sd->p_sys->p_node_cat,
PLAYLIST_APPEND, PLAYLIST_END );
p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
p_item = playlist_NodeAddInput( p_playlist, p_input,p_sd->p_sys->p_node_one,
PLAYLIST_APPEND, PLAYLIST_END );
p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
for( i = 0; i < p_sys->i_devices_number; i++ )
{
if( strcmp( psz_udi, p_sys->pp_devices[i]->psz_udi ) == 0 )
{
playlist_DeleteFromItemId( p_playlist, p_sys->pp_devices[i]->i_id );
}
}
vlc_object_release( p_playlist );
}
#endif
static void AddCdda( services_discovery_t *p_sd, char *psz_device )
{
char *psz_name = "Audio CD";
char *psz_uri;
char *psz_blockdevice;
input_item_t *p_input;
#ifdef HAVE_HAL_1
#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
psz_device, "block.device", NULL );
#else
......@@ -226,23 +314,22 @@ static void AddCdda( services_discovery_t *p_sd, char *psz_device )
#endif
asprintf( &psz_uri, "cdda://%s", psz_blockdevice );
/* Create the playlist item here */
p_input = input_ItemNew( p_sd, psz_uri, psz_name );
p_input = input_ItemNew( p_sd, psz_uri, "Audio CD" );
free( psz_uri );
#ifdef HAVE_HAL_1
libhal_free_string( psz_device );
#else
hal_free_string( psz_device );
#endif
if( !p_input )
return;
#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
AddItem( p_sd, p_input, psz_device );
#else
AddItem( p_sd, p_input );
#endif
}
static void ParseDevice( services_discovery_t *p_sd, char *psz_device )
{
char *psz_disc_type;
services_discovery_sys_t *p_sys = p_sd->p_sys;
#ifdef HAVE_HAL_1
#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
if( libhal_device_property_exists( p_sys->p_ctx, psz_device,
"volume.disc.type", NULL ) )
{
......@@ -260,7 +347,7 @@ static void ParseDevice( services_discovery_t *p_sd, char *psz_device )
#endif
if( !strcmp( psz_disc_type, "dvd_rom" ) )
{
#ifdef HAVE_HAL_1
#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
/* hal 0.2.9.7 (HAVE_HAL) has not is_videodvd
* but hal 0.5.0 (HAVE_HAL_1) has */
if (libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
......@@ -270,7 +357,7 @@ static void ParseDevice( services_discovery_t *p_sd, char *psz_device )
}
else if( !strcmp( psz_disc_type, "cd_rom" ) )
{
#ifdef HAVE_HAL_1
#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
if( libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
"volume.disc.has_audio" , NULL ) )
#else
......@@ -281,11 +368,6 @@ static void ParseDevice( services_discovery_t *p_sd, char *psz_device )
AddCdda( p_sd, psz_device );
}
}
#ifdef HAVE_HAL_1
libhal_free_string( psz_disc_type );
#else
hal_free_string( psz_disc_type );
#endif
}
}
......@@ -299,7 +381,7 @@ static void Run( services_discovery_t *p_sd )
services_discovery_sys_t *p_sys = p_sd->p_sys;
/* parse existing devices first */
#ifdef HAVE_HAL_1
#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
if( ( devices = libhal_get_all_devices( p_sys->p_ctx, &i_devices, NULL ) ) )
#else
if( ( devices = hal_get_all_devices( p_sys->p_ctx, &i_devices ) ) )
......@@ -308,72 +390,33 @@ static void Run( services_discovery_t *p_sd )
for( i = 0; i < i_devices; i++ )
{
ParseDevice( p_sd, devices[ i ] );
}
}
#ifdef HAVE_DBUS_2
/* We'll use D-Bus to listen for devices evenements */
/* TODO: Manage hot removal of devices */
DBusMessage* dbus_message;
DBusMessageIter dbus_args;
const char** psz_dbus_value;
char* psz_dbus_device;
DBusError dbus_error;
DBusConnection* p_connection;
dbus_error_init( &dbus_error );
/* connect to the system bus */
p_connection = dbus_bus_get( DBUS_BUS_SYSTEM, &dbus_error );
if ( dbus_error_is_set( &dbus_error ) )
{
msg_Err( p_sd, "D-Bus Connection Error (%s)\n", dbus_error.message);
dbus_error_free( &dbus_error );
return;
}
/* check for hal signals */
dbus_bus_add_match( p_connection,
"type='signal',interface='org.freedesktop.Hal.Manager'", &dbus_error );
dbus_connection_flush( p_connection );
#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
libhal_free_string( devices[ i ] );
#else
hal_free_string( devices[ i ] );
#endif
/* an error ? oooh too bad :) */
if ( dbus_error_is_set( &dbus_error ) )
{
msg_Err( p_sd, "D-Bus signal match Error (%s)\n", dbus_error.message);
return;
}
}
#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
while( !p_sd->b_die )
{
/* read next available message */
dbus_connection_read_write( p_connection, 0 );
dbus_message = dbus_connection_pop_message( p_connection );
if( dbus_message == NULL )
{
/* we've worked really hard, now it's time to sleep */
msleep( 100000 );
continue;
}
/* check if the message is a signal from the correct interface */
if( dbus_message_is_signal( dbus_message, "org.freedesktop.Hal.Manager",
"DeviceAdded" ) )
{
/* read the parameter (it must be an udi string) */
if( dbus_message_iter_init( dbus_message, &dbus_args ) &&
( dbus_message_iter_get_arg_type( &dbus_args ) ==
DBUS_TYPE_STRING )
)
{
dbus_message_iter_get_basic( &dbus_args, &psz_dbus_value );
/* psz_bus_value musn't be freed, but AddCdda will do it
* so we allocate some memory, and copy it into psz_dbus_device
*/
psz_dbus_device = strdup( psz_dbus_value );
ParseDevice( p_sd, psz_dbus_device );
}
}
dbus_message_unref( dbus_message );
/* look for events on the bus, blocking 1 second */
dbus_connection_read_write_dispatch(
libhal_ctx_get_dbus_connection(p_sys->p_ctx), 1000 );
}
#endif
}
#if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
void DeviceAdded( LibHalContext *p_ctx, const char *psz_udi )
{
ParseDevice( p_sd_global, (char*) psz_udi );
}
void DeviceRemoved( LibHalContext *p_ctx, const char *psz_udi )
{
DelItem( p_sd_global, (char*) psz_udi );
}
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment