Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

hal.c

00001 /*****************************************************************************
00002  * sap.c :  SAP interface module
00003  *****************************************************************************
00004  * Copyright (C) 2004 the VideoLAN team
00005  * $Id: sap.c 9217 2004-11-07 11:02:59Z courmisch $
00006  *
00007  * Authors: Clément Stenac <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 /*****************************************************************************
00025  * Includes
00026  *****************************************************************************/
00027 #include <stdlib.h>                                      /* malloc(), free() */
00028 
00029 #include <vlc/vlc.h>
00030 #include <vlc/intf.h>
00031 
00032 #include <vlc/input.h>
00033 
00034 #include "network.h"
00035 
00036 #include <errno.h>                                                 /* ENOMEM */
00037 
00038 #ifdef HAVE_UNISTD_H
00039 #    include <unistd.h>
00040 #endif
00041 #ifdef HAVE_SYS_TIME_H
00042 #    include <sys/time.h>
00043 #endif
00044 
00045 #include <hal/libhal.h>
00046 
00047 /************************************************************************
00048  * Macros and definitions
00049  ************************************************************************/
00050 
00051 #define MAX_LINE_LENGTH 256
00052 
00053 
00054 /*****************************************************************************
00055  * Module descriptor
00056  *****************************************************************************/
00057 
00058 /* Callbacks */
00059     static int  Open ( vlc_object_t * );
00060     static void Close( vlc_object_t * );
00061 
00062 vlc_module_begin();
00063     set_description( _("HAL device detection") );
00064     set_category( CAT_PLAYLIST );
00065     set_subcategory( SUBCAT_PLAYLIST_SD );
00066 
00067     set_capability( "services_discovery", 0 );
00068     set_callbacks( Open, Close );
00069 
00070 vlc_module_end();
00071 
00072 
00073 /*****************************************************************************
00074  * Local structures
00075  *****************************************************************************/
00076 
00077 struct services_discovery_sys_t
00078 {
00079     LibHalContext *p_ctx;
00080 
00081     /* playlist node */
00082     playlist_item_t *p_node;
00083 
00084 };
00085 
00086 /*****************************************************************************
00087  * Local prototypes
00088  *****************************************************************************/
00089 
00090 /* Main functions */
00091     static void Run    ( services_discovery_t *p_intf );
00092 
00093 /*****************************************************************************
00094  * Open: initialize and create stuff
00095  *****************************************************************************/
00096 static int Open( vlc_object_t *p_this )
00097 {
00098     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
00099     services_discovery_sys_t *p_sys  = malloc(
00100                                     sizeof( services_discovery_sys_t ) );
00101 
00102     vlc_value_t         val;
00103     playlist_t          *p_playlist;
00104     playlist_view_t     *p_view;
00105 
00106     DBusError           dbus_error;
00107     DBusConnection      *p_connection;
00108 
00109     p_sd->pf_run = Run;
00110     p_sd->p_sys  = p_sys;
00111 
00112     dbus_error_init( &dbus_error );
00113 
00114 #ifdef HAVE_HAL_1
00115     p_sys->p_ctx = libhal_ctx_new();
00116     if( !p_sys->p_ctx )
00117     {
00118         msg_Err( p_sd, "Unable to create HAL context") ;
00119         free( p_sys );
00120         return VLC_EGENERIC;
00121     }
00122     p_connection = dbus_bus_get( DBUS_BUS_SYSTEM, &dbus_error );
00123     if( dbus_error_is_set( &dbus_error ) )
00124     {
00125         msg_Err( p_sd, "Unable to connect to DBUS: %s", dbus_error.message );
00126         dbus_error_free( &dbus_error );
00127         free( p_sys );
00128         return VLC_EGENERIC;
00129     }
00130     libhal_ctx_set_dbus_connection( p_sys->p_ctx, p_connection );
00131     if( !libhal_ctx_init( p_sys->p_ctx, &dbus_error ) )
00132 #else
00133     if( !(p_sys->p_ctx = hal_initialize( NULL, FALSE ) ) )
00134 #endif
00135     {
00136         msg_Err( p_sd, "hal not available : %s", dbus_error.message );
00137         dbus_error_free( &dbus_error );
00138         free( p_sys );
00139         return VLC_EGENERIC;
00140     }
00141 
00142     /* Create our playlist node */
00143     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
00144                                                 FIND_ANYWHERE );
00145     if( !p_playlist )
00146     {
00147         msg_Warn( p_sd, "unable to find playlist, cancelling HAL listening");
00148         return VLC_EGENERIC;
00149     }
00150 
00151     p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
00152     p_sys->p_node = playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
00153                                          _("Devices"), p_view->p_root );
00154 
00155     p_sys->p_node->i_flags |= PLAYLIST_RO_FLAG;
00156     val.b_bool = VLC_TRUE;
00157     var_Set( p_playlist, "intf-change", val );
00158 
00159     vlc_object_release( p_playlist );
00160 
00161     return VLC_SUCCESS;
00162 }
00163 
00164 /*****************************************************************************
00165  * Close:
00166  *****************************************************************************/
00167 static void Close( vlc_object_t *p_this )
00168 {
00169     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
00170     services_discovery_sys_t *p_sys  = p_sd->p_sys;
00171     playlist_t *p_playlist =  (playlist_t *) vlc_object_find( p_sd,
00172                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
00173     if( p_playlist )
00174     {
00175         playlist_NodeDelete( p_playlist, p_sys->p_node, VLC_TRUE, VLC_TRUE );
00176         vlc_object_release( p_playlist );
00177     }
00178     free( p_sys );
00179 }
00180 
00181 static void AddDvd( services_discovery_t *p_sd, char *psz_device )
00182 {
00183     char *psz_name;
00184     char *psz_uri;
00185     char *psz_blockdevice;
00186     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
00187     playlist_t          *p_playlist;
00188     playlist_item_t     *p_item;
00189 #ifdef HAVE_HAL_1
00190     psz_name = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
00191                                         psz_device, "volume.label", NULL );
00192     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
00193                                         psz_device, "block.device", NULL );
00194 #else
00195     psz_name = hal_device_get_property_string( p_sd->p_sys->p_ctx,
00196                                                psz_device, "volume.label" );
00197     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
00198                                                  psz_device, "block.device" );
00199 #endif
00200     asprintf( &psz_uri, "dvd://%s", psz_blockdevice );
00201     /* Create the playlist item here */
00202     p_item = playlist_ItemNew( p_sd, psz_uri,
00203                                psz_name );
00204     free( psz_uri );
00205 #ifdef HAVE_HAL_1
00206     libhal_free_string( psz_device );
00207 #else
00208     hal_free_string( psz_device );
00209 #endif
00210     if( !p_item )
00211     {
00212         return;
00213     }
00214     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
00215     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
00216                                                 FIND_ANYWHERE );
00217     if( !p_playlist )
00218     {
00219         msg_Err( p_sd, "playlist not found" );
00220         return;
00221     }
00222 
00223     playlist_NodeAddItem( p_playlist, p_item, VIEW_CATEGORY, p_sys->p_node,
00224                           PLAYLIST_APPEND, PLAYLIST_END );
00225 
00226     vlc_object_release( p_playlist );
00227 }
00228 
00229 static void AddCdda( services_discovery_t *p_sd, char *psz_device )
00230 {
00231     char *psz_name = "Audio CD";
00232     char *psz_uri;
00233     char *psz_blockdevice;
00234     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
00235     playlist_t          *p_playlist;
00236     playlist_item_t     *p_item;
00237 #ifdef HAVE_HAL_1
00238     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
00239                                             psz_device, "block.device", NULL );
00240 #else
00241     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
00242                                                  psz_device, "block.device" );
00243 #endif
00244     asprintf( &psz_uri, "cdda://%s", psz_blockdevice );
00245     /* Create the playlist item here */
00246     p_item = playlist_ItemNew( p_sd, psz_uri,
00247                                psz_name );
00248     free( psz_uri );
00249 #ifdef HAVE_HAL_1
00250     libhal_free_string( psz_device );
00251 #else
00252     hal_free_string( psz_device );
00253 #endif
00254     if( !p_item )
00255     {
00256         return;
00257     }
00258     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
00259     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
00260                                                 FIND_ANYWHERE );
00261     if( !p_playlist )
00262     {
00263         msg_Err( p_sd, "playlist not found" );
00264         return;
00265     }
00266 
00267     playlist_NodeAddItem( p_playlist, p_item, VIEW_CATEGORY, p_sys->p_node,
00268                           PLAYLIST_APPEND, PLAYLIST_END );
00269 
00270     vlc_object_release( p_playlist );
00271 
00272 }
00273 
00274 static void ParseDevice( services_discovery_t *p_sd, char *psz_device )
00275 {
00276     char *psz_disc_type;
00277     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
00278 #ifdef HAVE_HAL_1
00279     if( libhal_device_property_exists( p_sys->p_ctx, psz_device,
00280                                        "volume.disc.type", NULL ) )
00281     {
00282         psz_disc_type = libhal_device_get_property_string( p_sys->p_ctx,
00283                                                         psz_device,
00284                                                         "volume.disc.type",
00285                                                         NULL );
00286 #else
00287     if( hal_device_property_exists( p_sys->p_ctx, psz_device,
00288                                     "volume.disc.type" ) )
00289     {
00290         psz_disc_type = hal_device_get_property_string( p_sys->p_ctx,
00291                                                         psz_device,
00292                                                         "volume.disc.type" );
00293 #endif
00294         if( !strcmp( psz_disc_type, "dvd_rom" ) )
00295         {
00296             AddDvd( p_sd, psz_device );
00297         }
00298         else if( !strcmp( psz_disc_type, "cd_rom" ) )
00299         {
00300 #ifdef HAVE_HAL_1
00301             if( libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
00302                                          "volume.disc.has_audio" , NULL ) )
00303 #else
00304             if( hal_device_get_property_bool( p_sys->p_ctx, psz_device,
00305                                          "volume.disc.has_audio" ) )
00306 #endif
00307             {
00308                 AddCdda( p_sd, psz_device );
00309             }
00310         }
00311 #ifdef HAVE_HAL_1
00312         libhal_free_string( psz_disc_type );
00313 #else
00314         hal_free_string( psz_disc_type );
00315 #endif
00316     }
00317 }
00318 
00319 /*****************************************************************************
00320  * Run: main HAL thread
00321  *****************************************************************************/
00322 static void Run( services_discovery_t *p_sd )
00323 {
00324     int i, i_devices;
00325     char **devices;
00326     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
00327 
00328     /* parse existing devices first */
00329 #ifdef HAVE_HAL_1
00330     if( ( devices = libhal_get_all_devices( p_sys->p_ctx, &i_devices, NULL ) ) )
00331 #else
00332     if( ( devices = hal_get_all_devices( p_sys->p_ctx, &i_devices ) ) )
00333 #endif
00334     {
00335         for( i = 0; i < i_devices; i++ )
00336         {
00337             ParseDevice( p_sd, devices[ i ] );
00338         }
00339     }
00340 
00341     while( !p_sd->b_die )
00342     {
00343         msleep( 100000 );
00344     }
00345 }

Generated on Tue Dec 20 10:14:49 2005 for vlc-0.8.4a by  doxygen 1.4.2