]> git.sesse.net Git - vlc/blob - modules/services_discovery/hal.c
New helper to create nodes for services discovery
[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     playlist_NodesCreateForSD( p_playlist, _("Devices"),
131                                &p_sys->p_node_cat, &p_sys->p_node_one );
132     vlc_object_release( p_playlist );
133
134     return VLC_SUCCESS;
135 }
136
137 /*****************************************************************************
138  * Close:
139  *****************************************************************************/
140 static void Close( vlc_object_t *p_this )
141 {
142     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
143     services_discovery_sys_t *p_sys  = p_sd->p_sys;
144     playlist_t *p_playlist =  (playlist_t *) vlc_object_find( p_sd,
145                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
146     if( p_playlist )
147     {
148         playlist_NodeDelete( p_playlist, p_sys->p_node_cat, VLC_TRUE,VLC_TRUE );
149         playlist_NodeDelete( p_playlist, p_sys->p_node_one, VLC_TRUE,VLC_TRUE );
150         vlc_object_release( p_playlist );
151     }
152     free( p_sys );
153 }
154
155 static void AddDvd( services_discovery_t *p_sd, char *psz_device )
156 {
157     char *psz_name;
158     char *psz_uri;
159     char *psz_blockdevice;
160     input_item_t        *p_input;
161 #ifdef HAVE_HAL_1
162     psz_name = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
163                                         psz_device, "volume.label", NULL );
164     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
165                                         psz_device, "block.device", NULL );
166 #else
167     psz_name = hal_device_get_property_string( p_sd->p_sys->p_ctx,
168                                                psz_device, "volume.label" );
169     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
170                                                  psz_device, "block.device" );
171 #endif
172     asprintf( &psz_uri, "dvd://%s", psz_blockdevice );
173     /* Create the playlist item here */
174     p_input = input_ItemNew( p_sd, psz_uri, psz_name );
175     free( psz_uri );
176 #ifdef HAVE_HAL_1
177     libhal_free_string( psz_device );
178 #else
179     hal_free_string( psz_device );
180 #endif
181     if( !p_input )
182     {
183         return;
184     }
185     AddItem( p_sd, p_input );
186 }
187
188 static void AddItem( services_discovery_t *p_sd, input_item_t * p_input )
189 {
190     playlist_item_t *p_item;
191     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_sd,
192                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
193     if( !p_playlist )
194     {
195         msg_Err( p_sd, "playlist not found" );
196         return;
197     }
198     p_item = playlist_NodeAddInput( p_playlist, p_input,p_sd->p_sys->p_node_cat,
199                                     PLAYLIST_APPEND, PLAYLIST_END );
200     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
201     p_item = playlist_NodeAddInput( p_playlist, p_input,p_sd->p_sys->p_node_one,
202                                     PLAYLIST_APPEND, PLAYLIST_END );
203     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
204
205     vlc_object_release( p_playlist );
206 }
207
208 static void AddCdda( services_discovery_t *p_sd, char *psz_device )
209 {
210     char *psz_name = "Audio CD";
211     char *psz_uri;
212     char *psz_blockdevice;
213     input_item_t     *p_input;
214 #ifdef HAVE_HAL_1
215     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
216                                             psz_device, "block.device", NULL );
217 #else
218     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
219                                                  psz_device, "block.device" );
220 #endif
221     asprintf( &psz_uri, "cdda://%s", psz_blockdevice );
222     /* Create the playlist item here */
223     p_input = input_ItemNew( p_sd, psz_uri, psz_name );
224     free( psz_uri );
225 #ifdef HAVE_HAL_1
226     libhal_free_string( psz_device );
227 #else
228     hal_free_string( psz_device );
229 #endif
230     if( !p_input )
231         return;
232     AddItem( p_sd, p_input );
233 }
234
235 static void ParseDevice( services_discovery_t *p_sd, char *psz_device )
236 {
237     char *psz_disc_type;
238     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
239 #ifdef HAVE_HAL_1
240     if( libhal_device_property_exists( p_sys->p_ctx, psz_device,
241                                        "volume.disc.type", NULL ) )
242     {
243         psz_disc_type = libhal_device_get_property_string( p_sys->p_ctx,
244                                                         psz_device,
245                                                         "volume.disc.type",
246                                                         NULL );
247 #else
248     if( hal_device_property_exists( p_sys->p_ctx, psz_device,
249                                     "volume.disc.type" ) )
250     {
251         psz_disc_type = hal_device_get_property_string( p_sys->p_ctx,
252                                                         psz_device,
253                                                         "volume.disc.type" );
254 #endif
255         if( !strcmp( psz_disc_type, "dvd_rom" ) )
256         {
257             AddDvd( p_sd, psz_device );
258         }
259         else if( !strcmp( psz_disc_type, "cd_rom" ) )
260         {
261 #ifdef HAVE_HAL_1
262             if( libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
263                                          "volume.disc.has_audio" , NULL ) )
264 #else
265             if( hal_device_get_property_bool( p_sys->p_ctx, psz_device,
266                                          "volume.disc.has_audio" ) )
267 #endif
268             {
269                 AddCdda( p_sd, psz_device );
270             }
271         }
272 #ifdef HAVE_HAL_1
273         libhal_free_string( psz_disc_type );
274 #else
275         hal_free_string( psz_disc_type );
276 #endif
277     }
278 }
279
280 /*****************************************************************************
281  * Run: main HAL thread
282  *****************************************************************************/
283 static void Run( services_discovery_t *p_sd )
284 {
285     int i, i_devices;
286     char **devices;
287     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
288
289     /* parse existing devices first */
290 #ifdef HAVE_HAL_1
291     if( ( devices = libhal_get_all_devices( p_sys->p_ctx, &i_devices, NULL ) ) )
292 #else
293     if( ( devices = hal_get_all_devices( p_sys->p_ctx, &i_devices ) ) )
294 #endif
295     {
296         for( i = 0; i < i_devices; i++ )
297         {
298             ParseDevice( p_sd, devices[ i ] );
299         }
300     }
301
302     while( !p_sd->b_die )
303     {
304         msleep( 100000 );
305     }
306 }