]> git.sesse.net Git - vlc/blob - modules/services_discovery/hal.c
Merge back branch 0.8.6-playlist-vlm to trunk.
[vlc] / modules / services_discovery / hal.c
1 /*****************************************************************************
2  * hal.c :  HAL interface module
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #include <vlc/vlc.h>
25 #include <vlc/intf.h>
26
27 #include <vlc/input.h>
28
29 #include "network.h"
30
31 #include <errno.h>                                                 /* ENOMEM */
32
33 #ifdef HAVE_UNISTD_H
34 #    include <unistd.h>
35 #endif
36 #ifdef HAVE_SYS_TIME_H
37 #    include <sys/time.h>
38 #endif
39
40 #include <hal/libhal.h>
41
42 #define MAX_LINE_LENGTH 256
43
44 /*****************************************************************************
45  * Local prototypes
46  *****************************************************************************/
47 struct services_discovery_sys_t
48 {
49     LibHalContext *p_ctx;
50     playlist_item_t *p_node_cat;
51     playlist_item_t *p_node_one;
52 };
53 static void AddItem( services_discovery_t *p_sd, input_item_t * p_input );
54 static void Run    ( services_discovery_t *p_intf );
55
56 static int  Open ( vlc_object_t * );
57 static void Close( vlc_object_t * );
58
59 /*****************************************************************************
60  * Module descriptor
61  *****************************************************************************/
62 vlc_module_begin();
63     set_description( _("HAL devices detection") );
64     set_category( CAT_PLAYLIST );
65     set_subcategory( SUBCAT_PLAYLIST_SD );
66
67     set_capability( "services_discovery", 0 );
68     set_callbacks( Open, Close );
69
70 vlc_module_end();
71
72
73 /*****************************************************************************
74  * Open: initialize and create stuff
75  *****************************************************************************/
76 static int Open( vlc_object_t *p_this )
77 {
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 ) );
81
82     vlc_value_t         val;
83     playlist_t          *p_playlist;
84
85     DBusError           dbus_error;
86     DBusConnection      *p_connection;
87
88     p_sd->pf_run = Run;
89     p_sd->p_sys  = p_sys;
90
91     dbus_error_init( &dbus_error );
92
93 #ifdef HAVE_HAL_1
94     p_sys->p_ctx = libhal_ctx_new();
95     if( !p_sys->p_ctx )
96     {
97         msg_Err( p_sd, "unable to create HAL context") ;
98         free( p_sys );
99         return VLC_EGENERIC;
100     }
101     p_connection = dbus_bus_get( DBUS_BUS_SYSTEM, &dbus_error );
102     if( dbus_error_is_set( &dbus_error ) )
103     {
104         msg_Err( p_sd, "unable to connect to DBUS: %s", dbus_error.message );
105         dbus_error_free( &dbus_error );
106         free( p_sys );
107         return VLC_EGENERIC;
108     }
109     libhal_ctx_set_dbus_connection( p_sys->p_ctx, p_connection );
110     if( !libhal_ctx_init( p_sys->p_ctx, &dbus_error ) )
111 #else
112     if( !(p_sys->p_ctx = hal_initialize( NULL, FALSE ) ) )
113 #endif
114     {
115         msg_Err( p_sd, "hal not available : %s", dbus_error.message );
116         dbus_error_free( &dbus_error );
117         free( p_sys );
118         return VLC_EGENERIC;
119     }
120
121     /* Create our playlist node */
122     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
123                                                 FIND_ANYWHERE );
124     if( !p_playlist )
125     {
126         msg_Warn( p_sd, "unable to find playlist, cancelling HAL listening");
127         return VLC_EGENERIC;
128     }
129
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;
136
137     val.b_bool = VLC_TRUE;
138     var_Set( p_playlist, "intf-change", val );
139
140     vlc_object_release( p_playlist );
141
142     return VLC_SUCCESS;
143 }
144
145 /*****************************************************************************
146  * Close:
147  *****************************************************************************/
148 static void Close( vlc_object_t *p_this )
149 {
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 );
154     if( p_playlist )
155     {
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 );
159     }
160     free( p_sys );
161 }
162
163 static void AddDvd( services_discovery_t *p_sd, char *psz_device )
164 {
165     char *psz_name;
166     char *psz_uri;
167     char *psz_blockdevice;
168     input_item_t        *p_input;
169 #ifdef HAVE_HAL_1
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 );
174 #else
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" );
179 #endif
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 );
183     free( psz_uri );
184 #ifdef HAVE_HAL_1
185     libhal_free_string( psz_device );
186 #else
187     hal_free_string( psz_device );
188 #endif
189     if( !p_input )
190     {
191         return;
192     }
193     AddItem( p_sd, p_input );
194 }
195
196 static void AddItem( services_discovery_t *p_sd, input_item_t * p_input )
197 {
198     playlist_item_t *p_item;
199     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_sd,
200                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
201     if( !p_playlist )
202     {
203         msg_Err( p_sd, "playlist not found" );
204         return;
205     }
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;
212
213     vlc_object_release( p_playlist );
214 }
215
216 static void AddCdda( services_discovery_t *p_sd, char *psz_device )
217 {
218     char *psz_name = "Audio CD";
219     char *psz_uri;
220     char *psz_blockdevice;
221     input_item_t     *p_input;
222 #ifdef HAVE_HAL_1
223     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
224                                             psz_device, "block.device", NULL );
225 #else
226     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
227                                                  psz_device, "block.device" );
228 #endif
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 );
232     free( psz_uri );
233 #ifdef HAVE_HAL_1
234     libhal_free_string( psz_device );
235 #else
236     hal_free_string( psz_device );
237 #endif
238     if( !p_input )
239         return;
240     AddItem( p_sd, p_input );
241 }
242
243 static void ParseDevice( services_discovery_t *p_sd, char *psz_device )
244 {
245     char *psz_disc_type;
246     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
247 #ifdef HAVE_HAL_1
248     if( libhal_device_property_exists( p_sys->p_ctx, psz_device,
249                                        "volume.disc.type", NULL ) )
250     {
251         psz_disc_type = libhal_device_get_property_string( p_sys->p_ctx,
252                                                         psz_device,
253                                                         "volume.disc.type",
254                                                         NULL );
255 #else
256     if( hal_device_property_exists( p_sys->p_ctx, psz_device,
257                                     "volume.disc.type" ) )
258     {
259         psz_disc_type = hal_device_get_property_string( p_sys->p_ctx,
260                                                         psz_device,
261                                                         "volume.disc.type" );
262 #endif
263         if( !strcmp( psz_disc_type, "dvd_rom" ) )
264         {
265             AddDvd( p_sd, psz_device );
266         }
267         else if( !strcmp( psz_disc_type, "cd_rom" ) )
268         {
269 #ifdef HAVE_HAL_1
270             if( libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
271                                          "volume.disc.has_audio" , NULL ) )
272 #else
273             if( hal_device_get_property_bool( p_sys->p_ctx, psz_device,
274                                          "volume.disc.has_audio" ) )
275 #endif
276             {
277                 AddCdda( p_sd, psz_device );
278             }
279         }
280 #ifdef HAVE_HAL_1
281         libhal_free_string( psz_disc_type );
282 #else
283         hal_free_string( psz_disc_type );
284 #endif
285     }
286 }
287
288 /*****************************************************************************
289  * Run: main HAL thread
290  *****************************************************************************/
291 static void Run( services_discovery_t *p_sd )
292 {
293     int i, i_devices;
294     char **devices;
295     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
296
297     /* parse existing devices first */
298 #ifdef HAVE_HAL_1
299     if( ( devices = libhal_get_all_devices( p_sys->p_ctx, &i_devices, NULL ) ) )
300 #else
301     if( ( devices = hal_get_all_devices( p_sys->p_ctx, &i_devices ) ) )
302 #endif
303     {
304         for( i = 0; i < i_devices; i++ )
305         {
306             ParseDevice( p_sd, devices[ i ] );
307         }
308     }
309
310     while( !p_sd->b_die )
311     {
312         msleep( 100000 );
313     }
314 }