1 /*****************************************************************************
2 * sap.c : SAP interface module
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
5 * $Id: sap.c 9217 2004-11-07 11:02:59Z courmisch $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
32 #include <vlc/input.h>
36 #include <errno.h> /* ENOMEM */
41 #ifdef HAVE_SYS_TIME_H
42 # include <sys/time.h>
45 #include <hal/libhal.h>
47 /************************************************************************
48 * Macros and definitions
49 ************************************************************************/
51 #define MAX_LINE_LENGTH 256
54 /*****************************************************************************
56 *****************************************************************************/
59 static int Open ( vlc_object_t * );
60 static void Close( vlc_object_t * );
63 set_description( _("HAL device detection") );
65 set_capability( "interface", 0 );
66 set_callbacks( Open, Close );
71 /*****************************************************************************
73 *****************************************************************************/
80 playlist_item_t *p_node;
84 /*****************************************************************************
86 *****************************************************************************/
89 static void Run ( intf_thread_t *p_intf );
91 /*****************************************************************************
92 * Open: initialize and create stuff
93 *****************************************************************************/
94 static int Open( vlc_object_t *p_this )
96 intf_thread_t *p_intf = ( intf_thread_t* )p_this;
97 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
99 playlist_t *p_playlist;
100 playlist_view_t *p_view;
102 p_intf->pf_run = Run;
103 p_intf->p_sys = p_sys;
105 if( !( p_sys->p_ctx = hal_initialize( NULL, FALSE ) ) )
108 msg_Err( p_intf, "hal not available" );
112 /* Create our playlist node */
113 p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
117 msg_Warn( p_intf, "unable to find playlist, cancelling HAL listening");
121 p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
122 p_sys->p_node = playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
123 _("Devices"), p_view->p_root );
125 vlc_object_release( p_playlist );
130 /*****************************************************************************
132 *****************************************************************************/
133 static void Close( vlc_object_t *p_this )
135 intf_thread_t *p_intf = ( intf_thread_t* )p_this;
136 intf_sys_t *p_sys = p_intf->p_sys;
141 static void AddDvd( intf_thread_t *p_intf, char *psz_device )
145 char *psz_blockdevice;
146 intf_sys_t *p_sys = p_intf->p_sys;
147 playlist_t *p_playlist;
148 playlist_item_t *p_item;
149 psz_name = hal_device_get_property_string( p_intf->p_sys->p_ctx,
150 psz_device, "volume.label" );
151 psz_blockdevice = hal_device_get_property_string( p_intf->p_sys->p_ctx,
152 psz_device, "block.device" );
153 asprintf( &psz_uri, "dvd://%s", psz_blockdevice );
154 /* Create the playlist item here */
155 p_item = playlist_ItemNew( p_intf, psz_uri,
158 hal_free_string( psz_device );
163 p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
164 p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
168 msg_Err( p_intf, "playlist not found" );
172 playlist_NodeAddItem( p_playlist, p_item, VIEW_CATEGORY, p_sys->p_node,
173 PLAYLIST_APPEND, PLAYLIST_END );
175 vlc_object_release( p_playlist );
179 static void AddCdda( intf_thread_t *p_intf, char *psz_device )
181 char *psz_name = "Audio CD";
183 char *psz_blockdevice;
184 intf_sys_t *p_sys = p_intf->p_sys;
185 playlist_t *p_playlist;
186 playlist_item_t *p_item;
187 psz_blockdevice = hal_device_get_property_string( p_intf->p_sys->p_ctx,
188 psz_device, "block.device" );
189 asprintf( &psz_uri, "cdda://%s", psz_blockdevice );
190 /* Create the playlist item here */
191 p_item = playlist_ItemNew( p_intf, psz_uri,
194 hal_free_string( psz_device );
199 p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
200 p_playlist = (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
204 msg_Err( p_intf, "playlist not found" );
208 playlist_NodeAddItem( p_playlist, p_item, VIEW_CATEGORY, p_sys->p_node,
209 PLAYLIST_APPEND, PLAYLIST_END );
211 vlc_object_release( p_playlist );
216 static void ParseDevice( intf_thread_t *p_intf, char *psz_device )
219 intf_sys_t *p_sys = p_intf->p_sys;
220 if( hal_device_property_exists( p_sys->p_ctx, psz_device,
221 "volume.disc.type" ) )
223 psz_disc_type = hal_device_get_property_string( p_sys->p_ctx,
225 "volume.disc.type" );
226 if( !strcmp( psz_disc_type, "dvd_rom" ) )
228 AddDvd( p_intf, psz_device );
230 else if( !strcmp( psz_disc_type, "cd_rom" ) )
232 if( hal_device_get_property_bool( p_sys->p_ctx, psz_device, "volume.disc.has_audio" ) )
234 AddCdda( p_intf, psz_device );
237 hal_free_string( psz_disc_type );
242 /*****************************************************************************
243 * Run: main HAL thread
244 *****************************************************************************/
245 static void Run( intf_thread_t *p_intf )
249 intf_sys_t *p_sys = p_intf->p_sys;
251 /* parse existing devices first */
252 if( ( devices = hal_get_all_devices( p_sys->p_ctx, &i_devices ) ) )
254 for( i = 0; i < i_devices; i++ )
256 ParseDevice( p_intf, devices[ i ] );
260 while( !p_intf->b_die )