hal.c 13.4 KB
Newer Older
1
/*****************************************************************************
2
 * hal.c :  HAL interface module
3
 *****************************************************************************
4
 * Copyright (C) 2004 the VideoLAN team
5
 * Copyright (C) 2006 Rafaël Carré
Antoine Cellerier's avatar
Antoine Cellerier committed
6
 * $Id$
7
 *
8
 * Authors: Clément Stenac <zorglub@videolan.org>
9
 *          Rafaël Carré <funman at videolanorg>
10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
 *****************************************************************************/

#include <vlc/vlc.h>
#include <vlc/intf.h>

#include <vlc/input.h>

#include "network.h"

#include <errno.h>                                                 /* ENOMEM */

#ifdef HAVE_UNISTD_H
#    include <unistd.h>
#endif
#ifdef HAVE_SYS_TIME_H
#    include <sys/time.h>
#endif

#include <hal/libhal.h>

#define MAX_LINE_LENGTH 256

/*****************************************************************************
47
 * Local prototypes
48
 *****************************************************************************/
49
#ifdef HAVE_HAL_1
50 51 52 53 54 55 56 57
/* store relation between item id and udi for ejection */
struct udi_input_id_t
{
    char    *psz_udi;
    int     i_id;
};
#endif

58 59 60 61 62
struct services_discovery_sys_t
{
    LibHalContext *p_ctx;
    playlist_item_t *p_node_cat;
    playlist_item_t *p_node_one;
63
#ifdef HAVE_HAL_1
64 65 66
    int                     i_devices_number;
    struct udi_input_id_t   **pp_devices;
#endif
67 68
};
static void Run    ( services_discovery_t *p_intf );
69

70 71
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );
72

73
#ifdef HAVE_HAL_1
74 75 76 77 78 79 80
/* 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

81 82 83
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
84
vlc_module_begin();
85
    set_description( _("HAL devices detection") );
Clément Stenac's avatar
Clément Stenac committed
86 87
    set_category( CAT_PLAYLIST );
    set_subcategory( SUBCAT_PLAYLIST_SD );
88

89
    set_capability( "services_discovery", 0 );
90 91 92 93 94 95 96 97 98 99
    set_callbacks( Open, Close );

vlc_module_end();


/*****************************************************************************
 * Open: initialize and create stuff
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
100 101 102
    services_discovery_t *p_sd = ( services_discovery_t* )p_this;
    services_discovery_sys_t *p_sys  = malloc(
                                    sizeof( services_discovery_sys_t ) );
103 104 105

    playlist_t          *p_playlist;

106
#ifdef HAVE_HAL_1
107
    DBusError           dbus_error;
Clément Stenac's avatar
Clément Stenac committed
108
    DBusConnection      *p_connection;
109 110 111 112

    p_sd_global = p_sd;
    p_sys->i_devices_number = 0;
    p_sys->pp_devices = NULL;
113
#endif
114

115 116
    p_sd->pf_run = Run;
    p_sd->p_sys  = p_sys;
117

118
#ifdef HAVE_HAL_1
119 120
    dbus_error_init( &dbus_error );

Clément Stenac's avatar
Clément Stenac committed
121 122 123
    p_sys->p_ctx = libhal_ctx_new();
    if( !p_sys->p_ctx )
    {
124
        msg_Err( p_sd, "unable to create HAL context") ;
Clément Stenac's avatar
Clément Stenac committed
125 126 127 128 129 130
        free( p_sys );
        return VLC_EGENERIC;
    }
    p_connection = dbus_bus_get( DBUS_BUS_SYSTEM, &dbus_error );
    if( dbus_error_is_set( &dbus_error ) )
    {
131
        msg_Err( p_sd, "unable to connect to DBUS: %s", dbus_error.message );
Clément Stenac's avatar
Clément Stenac committed
132 133 134 135 136 137
        dbus_error_free( &dbus_error );
        free( p_sys );
        return VLC_EGENERIC;
    }
    libhal_ctx_set_dbus_connection( p_sys->p_ctx, p_connection );
    if( !libhal_ctx_init( p_sys->p_ctx, &dbus_error ) )
138
#else
Clément Stenac's avatar
Clément Stenac committed
139
    if( !(p_sys->p_ctx = hal_initialize( NULL, FALSE ) ) )
140
#endif
141
    {
142
#ifdef HAVE_HAL_1
143
        msg_Err( p_sd, "hal not available : %s", dbus_error.message );
Clément Stenac's avatar
Clément Stenac committed
144
        dbus_error_free( &dbus_error );
145 146 147
#else
        msg_Err( p_sd, "hal not available" );
#endif
Clément Stenac's avatar
Clément Stenac committed
148
        free( p_sys );
149 150 151
        return VLC_EGENERIC;
    }

152
#ifdef HAVE_HAL_1
153 154 155 156 157 158 159 160 161 162
        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

163
    /* Create our playlist node */
164
    p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
165 166 167
                                                FIND_ANYWHERE );
    if( !p_playlist )
    {
168
        msg_Warn( p_sd, "unable to find playlist, cancelling HAL listening");
169 170 171
        return VLC_EGENERIC;
    }

172 173 174
    playlist_NodesPairCreate( p_playlist, _("Devices"),
                              &p_sys->p_node_cat, &p_sys->p_node_one,
                              VLC_TRUE );
175 176 177 178 179 180 181 182 183 184
    vlc_object_release( p_playlist );

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close:
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
185 186 187 188 189 190
    services_discovery_t *p_sd = ( services_discovery_t* )p_this;
    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 )
    {
191 192
        playlist_NodeDelete( p_playlist, p_sys->p_node_cat, VLC_TRUE,VLC_TRUE );
        playlist_NodeDelete( p_playlist, p_sys->p_node_one, VLC_TRUE,VLC_TRUE );
193 194
        vlc_object_release( p_playlist );
    }
195
    free( p_sys );
196
#ifdef HAVE_HAL_1
197 198 199
    struct udi_input_id_t *p_udi_entry;

    while( p_sys->i_devices_number > 0 )
200
    {
201
        p_udi_entry = p_sys->pp_devices[0];
202
        if( p_udi_entry->psz_udi ) free( p_udi_entry->psz_udi );
203 204
        TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
                p_sys->pp_devices[0] );
205 206 207 208 209 210 211
        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
212
#ifdef HAVE_HAL_1
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
                ,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 );

235
#ifdef HAVE_HAL_1
236 237 238 239 240 241 242 243 244 245
    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
246 247
}

248
static void AddDvd( services_discovery_t *p_sd, char *psz_device )
249 250 251 252
{
    char *psz_name;
    char *psz_uri;
    char *psz_blockdevice;
253
    input_item_t        *p_input;
254
#ifdef HAVE_HAL_1
255 256 257 258 259
    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,
                                        psz_device, "block.device", NULL );
#else
260
    psz_name = hal_device_get_property_string( p_sd->p_sys->p_ctx,
261
                                               psz_device, "volume.label" );
262
    psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
263
                                                 psz_device, "block.device" );
264
#endif
265 266
    asprintf( &psz_uri, "dvd://%s", psz_blockdevice );
    /* Create the playlist item here */
267
    p_input = input_ItemNew( p_sd, psz_uri, psz_name );
268
    free( psz_uri );
269
    if( !p_input )
270 271 272
    {
        return;
    }
273
#ifdef HAVE_HAL_1
274 275
    AddItem( p_sd, p_input, psz_device );
#else
276
    AddItem( p_sd, p_input );
277
#endif
278 279
}

280
#ifdef HAVE_HAL_1
281
static void DelItem( services_discovery_t *p_sd, char* psz_udi )
282
{
283 284 285
    services_discovery_sys_t    *p_sys  = p_sd->p_sys;
    int                         i;

286 287
    playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_sd,
                                        VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
288 289
    if( !p_playlist )
    {
290
        msg_Err( p_sd, "playlist not found" );
291 292
        return;
    }
293 294 295 296 297 298

    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 );
299 300
            TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
                    p_sys->pp_devices[i] );
301 302
        }
    }
303 304 305

    vlc_object_release( p_playlist );
}
306
#endif
307 308

static void AddCdda( services_discovery_t *p_sd, char *psz_device )
309 310 311
{
    char *psz_uri;
    char *psz_blockdevice;
312
    input_item_t     *p_input;
313
#ifdef HAVE_HAL_1
314 315 316
    psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
                                            psz_device, "block.device", NULL );
#else
317
    psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
318
                                                 psz_device, "block.device" );
319
#endif
320 321
    asprintf( &psz_uri, "cdda://%s", psz_blockdevice );
    /* Create the playlist item here */
322
    p_input = input_ItemNew( p_sd, psz_uri, "Audio CD" );
323
    free( psz_uri );
324
    if( !p_input )
325
        return;
326
#ifdef HAVE_HAL_1
327 328
    AddItem( p_sd, p_input, psz_device );
#else
329
    AddItem( p_sd, p_input );
330
#endif
331 332
}

333
static void ParseDevice( services_discovery_t *p_sd, char *psz_device )
334 335
{
    char *psz_disc_type;
336
    services_discovery_sys_t    *p_sys  = p_sd->p_sys;
337
#ifdef HAVE_HAL_1
338 339 340 341 342 343 344 345
    if( libhal_device_property_exists( p_sys->p_ctx, psz_device,
                                       "volume.disc.type", NULL ) )
    {
        psz_disc_type = libhal_device_get_property_string( p_sys->p_ctx,
                                                        psz_device,
                                                        "volume.disc.type",
                                                        NULL );
#else
346 347 348 349 350 351
    if( hal_device_property_exists( p_sys->p_ctx, psz_device,
                                    "volume.disc.type" ) )
    {
        psz_disc_type = hal_device_get_property_string( p_sys->p_ctx,
                                                        psz_device,
                                                        "volume.disc.type" );
352
#endif
353 354
        if( !strcmp( psz_disc_type, "dvd_rom" ) )
        {
355
#ifdef HAVE_HAL_1
356 357 358 359 360
            /* 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,
                                         "volume.disc.is_videodvd", NULL ) )
#endif
361
            AddDvd( p_sd, psz_device );
362 363 364
        }
        else if( !strcmp( psz_disc_type, "cd_rom" ) )
        {
365
#ifdef HAVE_HAL_1
366 367 368 369 370 371
            if( libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
                                         "volume.disc.has_audio" , NULL ) )
#else
            if( hal_device_get_property_bool( p_sys->p_ctx, psz_device,
                                         "volume.disc.has_audio" ) )
#endif
372
            {
373
                AddCdda( p_sd, psz_device );
374 375 376 377 378 379 380 381
            }
        }
    }
}

/*****************************************************************************
 * Run: main HAL thread
 *****************************************************************************/
382
static void Run( services_discovery_t *p_sd )
383 384 385
{
    int i, i_devices;
    char **devices;
386
    services_discovery_sys_t    *p_sys  = p_sd->p_sys;
387 388

    /* parse existing devices first */
389
#ifdef HAVE_HAL_1
390 391
    if( ( devices = libhal_get_all_devices( p_sys->p_ctx, &i_devices, NULL ) ) )
#else
392
    if( ( devices = hal_get_all_devices( p_sys->p_ctx, &i_devices ) ) )
393
#endif
394 395 396
    {
        for( i = 0; i < i_devices; i++ )
        {
397
            ParseDevice( p_sd, devices[ i ] );
398
#ifdef HAVE_HAL_1
399 400 401 402
            libhal_free_string( devices[ i ] );
#else
            hal_free_string( devices[ i ] );
#endif
403

404
        }
405
    }
406
#ifdef HAVE_HAL_1
407 408
    while( !p_sd->b_die )
    {
409 410 411
    /* look for events on the bus, blocking 1 second */
    dbus_connection_read_write_dispatch(
            libhal_ctx_get_dbus_connection(p_sys->p_ctx), 1000 );
412 413
    }
#endif
414 415 416

}

417
#ifdef HAVE_HAL_1
418 419 420
void DeviceAdded( LibHalContext *p_ctx, const char *psz_udi )
{
        ParseDevice( p_sd_global, (char*) psz_udi );
421
}
422 423 424 425 426 427
void DeviceRemoved( LibHalContext *p_ctx, const char *psz_udi )
{
        DelItem( p_sd_global, (char*) psz_udi );
}
#endif