]> git.sesse.net Git - vlc/blob - modules/services_discovery/hal.c
Adds real-time device adding (not removal yet)
[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     playlist_t          *p_playlist;
83
84 #if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
85     DBusError           dbus_error;
86     DBusConnection      *p_connection;
87 #endif
88
89     p_sd->pf_run = Run;
90     p_sd->p_sys  = p_sys;
91
92 #if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
93     dbus_error_init( &dbus_error );
94
95     p_sys->p_ctx = libhal_ctx_new();
96     if( !p_sys->p_ctx )
97     {
98         msg_Err( p_sd, "unable to create HAL context") ;
99         free( p_sys );
100         return VLC_EGENERIC;
101     }
102     p_connection = dbus_bus_get( DBUS_BUS_SYSTEM, &dbus_error );
103     if( dbus_error_is_set( &dbus_error ) )
104     {
105         msg_Err( p_sd, "unable to connect to DBUS: %s", dbus_error.message );
106         dbus_error_free( &dbus_error );
107         free( p_sys );
108         return VLC_EGENERIC;
109     }
110     libhal_ctx_set_dbus_connection( p_sys->p_ctx, p_connection );
111     if( !libhal_ctx_init( p_sys->p_ctx, &dbus_error ) )
112 #else
113     if( !(p_sys->p_ctx = hal_initialize( NULL, FALSE ) ) )
114 #endif
115     {
116 #if defined( HAVE_HAL_1 ) && defined( HAVE_DBUS_2 )
117         msg_Err( p_sd, "hal not available : %s", dbus_error.message );
118         dbus_error_free( &dbus_error );
119 #else
120         msg_Err( p_sd, "hal not available" );
121 #endif
122         free( p_sys );
123         return VLC_EGENERIC;
124     }
125
126     /* Create our playlist node */
127     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
128                                                 FIND_ANYWHERE );
129     if( !p_playlist )
130     {
131         msg_Warn( p_sd, "unable to find playlist, cancelling HAL listening");
132         return VLC_EGENERIC;
133     }
134
135     playlist_NodesPairCreate( p_playlist, _("Devices"),
136                               &p_sys->p_node_cat, &p_sys->p_node_one,
137                               VLC_TRUE );
138     vlc_object_release( p_playlist );
139
140     return VLC_SUCCESS;
141 }
142
143 /*****************************************************************************
144  * Close:
145  *****************************************************************************/
146 static void Close( vlc_object_t *p_this )
147 {
148     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
149     services_discovery_sys_t *p_sys  = p_sd->p_sys;
150     playlist_t *p_playlist =  (playlist_t *) vlc_object_find( p_sd,
151                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
152     if( p_playlist )
153     {
154         playlist_NodeDelete( p_playlist, p_sys->p_node_cat, VLC_TRUE,VLC_TRUE );
155         playlist_NodeDelete( p_playlist, p_sys->p_node_one, VLC_TRUE,VLC_TRUE );
156         vlc_object_release( p_playlist );
157     }
158     free( p_sys );
159 }
160
161 static void AddDvd( services_discovery_t *p_sd, char *psz_device )
162 {
163     char *psz_name;
164     char *psz_uri;
165     char *psz_blockdevice;
166     input_item_t        *p_input;
167 #ifdef HAVE_HAL_1
168     psz_name = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
169                                         psz_device, "volume.label", NULL );
170     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
171                                         psz_device, "block.device", NULL );
172 #else
173     psz_name = hal_device_get_property_string( p_sd->p_sys->p_ctx,
174                                                psz_device, "volume.label" );
175     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
176                                                  psz_device, "block.device" );
177 #endif
178     asprintf( &psz_uri, "dvd://%s", psz_blockdevice );
179     /* Create the playlist item here */
180     p_input = input_ItemNew( p_sd, psz_uri, psz_name );
181     free( psz_uri );
182 #ifdef HAVE_HAL_1
183     libhal_free_string( psz_device );
184 #else
185     hal_free_string( psz_device );
186 #endif
187     if( !p_input )
188     {
189         return;
190     }
191     AddItem( p_sd, p_input );
192 }
193
194 static void AddItem( services_discovery_t *p_sd, input_item_t * p_input )
195 {
196     playlist_item_t *p_item;
197     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_sd,
198                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
199     if( !p_playlist )
200     {
201         msg_Err( p_sd, "playlist not found" );
202         return;
203     }
204     p_item = playlist_NodeAddInput( p_playlist, p_input,p_sd->p_sys->p_node_cat,
205                                     PLAYLIST_APPEND, PLAYLIST_END );
206     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
207     p_item = playlist_NodeAddInput( p_playlist, p_input,p_sd->p_sys->p_node_one,
208                                     PLAYLIST_APPEND, PLAYLIST_END );
209     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
210
211     vlc_object_release( p_playlist );
212 }
213
214 static void AddCdda( services_discovery_t *p_sd, char *psz_device )
215 {
216     char *psz_name = "Audio CD";
217     char *psz_uri;
218     char *psz_blockdevice;
219     input_item_t     *p_input;
220 #ifdef HAVE_HAL_1
221     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
222                                             psz_device, "block.device", NULL );
223 #else
224     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
225                                                  psz_device, "block.device" );
226 #endif
227     asprintf( &psz_uri, "cdda://%s", psz_blockdevice );
228     /* Create the playlist item here */
229     p_input = input_ItemNew( p_sd, psz_uri, psz_name );
230     free( psz_uri );
231 #ifdef HAVE_HAL_1
232     libhal_free_string( psz_device );
233 #else
234     hal_free_string( psz_device );
235 #endif
236     if( !p_input )
237         return;
238     AddItem( p_sd, p_input );
239 }
240
241 static void ParseDevice( services_discovery_t *p_sd, char *psz_device )
242 {
243     char *psz_disc_type;
244     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
245 #ifdef HAVE_HAL_1
246     if( libhal_device_property_exists( p_sys->p_ctx, psz_device,
247                                        "volume.disc.type", NULL ) )
248     {
249         psz_disc_type = libhal_device_get_property_string( p_sys->p_ctx,
250                                                         psz_device,
251                                                         "volume.disc.type",
252                                                         NULL );
253 #else
254     if( hal_device_property_exists( p_sys->p_ctx, psz_device,
255                                     "volume.disc.type" ) )
256     {
257         psz_disc_type = hal_device_get_property_string( p_sys->p_ctx,
258                                                         psz_device,
259                                                         "volume.disc.type" );
260 #endif
261         if( !strcmp( psz_disc_type, "dvd_rom" ) )
262         {
263 #ifdef HAVE_HAL_1
264             /* hal 0.2.9.7 (HAVE_HAL) has not is_videodvd
265              * but hal 0.5.0 (HAVE_HAL_1) has */
266             if (libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
267                                          "volume.disc.is_videodvd", NULL ) )
268 #endif
269             AddDvd( p_sd, psz_device );
270         }
271         else if( !strcmp( psz_disc_type, "cd_rom" ) )
272         {
273 #ifdef HAVE_HAL_1
274             if( libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
275                                          "volume.disc.has_audio" , NULL ) )
276 #else
277             if( hal_device_get_property_bool( p_sys->p_ctx, psz_device,
278                                          "volume.disc.has_audio" ) )
279 #endif
280             {
281                 AddCdda( p_sd, psz_device );
282             }
283         }
284 #ifdef HAVE_HAL_1
285         libhal_free_string( psz_disc_type );
286 #else
287         hal_free_string( psz_disc_type );
288 #endif
289     }
290 }
291
292 /*****************************************************************************
293  * Run: main HAL thread
294  *****************************************************************************/
295 static void Run( services_discovery_t *p_sd )
296 {
297     int i, i_devices;
298     char **devices;
299     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
300
301     /* parse existing devices first */
302 #ifdef HAVE_HAL_1
303     if( ( devices = libhal_get_all_devices( p_sys->p_ctx, &i_devices, NULL ) ) )
304 #else
305     if( ( devices = hal_get_all_devices( p_sys->p_ctx, &i_devices ) ) )
306 #endif
307     {
308         for( i = 0; i < i_devices; i++ )
309         {
310             ParseDevice( p_sd, devices[ i ] );
311         }
312     }
313 #ifdef HAVE_DBUS_2
314     /* We'll use D-Bus to listen for devices evenements */
315     /* TODO: Manage hot removal of devices */
316     DBusMessage*        dbus_message;
317     DBusMessageIter     dbus_args;
318     const char**        psz_dbus_value;
319     char*               psz_dbus_device;
320     DBusError           dbus_error;
321     DBusConnection*     p_connection;
322
323     dbus_error_init( &dbus_error );
324
325     /* connect to the system bus */
326     p_connection = dbus_bus_get( DBUS_BUS_SYSTEM, &dbus_error );
327     if ( dbus_error_is_set( &dbus_error ) )
328     {
329         msg_Err( p_sd, "D-Bus Connection Error (%s)\n", dbus_error.message);
330         dbus_error_free( &dbus_error );
331         return;
332     }
333
334     /* check for hal signals */
335     dbus_bus_add_match( p_connection,
336         "type='signal',interface='org.freedesktop.Hal.Manager'", &dbus_error );
337     dbus_connection_flush( p_connection );
338
339     /* an error ? oooh too bad :) */
340     if ( dbus_error_is_set( &dbus_error ) )
341     { 
342         msg_Err( p_sd, "D-Bus signal match Error (%s)\n", dbus_error.message);
343         return;
344     }
345
346     while( !p_sd->b_die )
347     {
348         /* read next available message */
349         dbus_connection_read_write( p_connection, 0 );
350         dbus_message = dbus_connection_pop_message( p_connection );
351
352         if( dbus_message == NULL )
353         {
354             /* we've worked really hard, now it's time to sleep */
355             msleep( 100000 );
356             continue;
357         }
358         /* check if the message is a signal from the correct interface */
359         if( dbus_message_is_signal( dbus_message, "org.freedesktop.Hal.Manager",
360                     "DeviceAdded" ) )
361         {
362             /* read the parameter (it must be an udi string) */
363             if( dbus_message_iter_init( dbus_message, &dbus_args ) &&
364                 ( dbus_message_iter_get_arg_type( &dbus_args ) ==
365                         DBUS_TYPE_STRING )
366                 )
367             {
368                 dbus_message_iter_get_basic( &dbus_args, &psz_dbus_value );
369                 /* psz_bus_value musn't be freed, but AddCdda will do it
370                  * so we allocate some memory, and copy it into psz_dbus_device
371                  */
372                 psz_dbus_device = strdup( psz_dbus_value );
373                 ParseDevice( p_sd, psz_dbus_device );
374             }
375         }
376         dbus_message_unref( dbus_message );
377     }
378 #endif
379 }