]> git.sesse.net Git - vlc/blob - modules/services_discovery/hal.c
Unref D-Bus connections when not using them anymore
[vlc] / modules / services_discovery / hal.c
1 /*****************************************************************************
2  * hal.c :  HAL interface module
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * Copyright (C) 2006 Rafaël Carré
6  * $Id$
7  *
8  * Authors: Clément Stenac <zorglub@videolan.org>
9  *          Rafaël Carré <funman at videolanorg>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #include <vlc/vlc.h>
27 #include <vlc/intf.h>
28
29 #include <vlc/input.h>
30
31 #include "network.h"
32
33 #include <errno.h>                                                 /* ENOMEM */
34
35 #ifdef HAVE_UNISTD_H
36 #    include <unistd.h>
37 #endif
38 #ifdef HAVE_SYS_TIME_H
39 #    include <sys/time.h>
40 #endif
41
42 #include <hal/libhal.h>
43
44 #define MAX_LINE_LENGTH 256
45
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49 #ifdef HAVE_HAL_1
50 /* store relation between item id and udi for ejection */
51 struct udi_input_id_t
52 {
53     char    *psz_udi;
54     int     i_id;
55 };
56 #endif
57
58 struct services_discovery_sys_t
59 {
60     LibHalContext           *p_ctx;
61     playlist_item_t         *p_node_cat;
62     playlist_item_t         *p_node_one;
63 #ifdef HAVE_HAL_1
64     DBusConnection          *p_connection;
65     int                     i_devices_number;
66     struct udi_input_id_t   **pp_devices;
67 #endif
68 };
69 static void Run    ( services_discovery_t *p_intf );
70
71 static int  Open ( vlc_object_t * );
72 static void Close( vlc_object_t * );
73
74 #ifdef HAVE_HAL_1
75 /* HAL callbacks */
76 void DeviceAdded( LibHalContext *p_ctx, const char *psz_udi );
77 void DeviceRemoved( LibHalContext *p_ctx, const char *psz_udi );
78 /* to retrieve p_sd in HAL callbacks */
79 services_discovery_t        *p_sd_global;
80 #endif
81
82 /*****************************************************************************
83  * Module descriptor
84  *****************************************************************************/
85 vlc_module_begin();
86     set_description( _("HAL devices detection") );
87     set_category( CAT_PLAYLIST );
88     set_subcategory( SUBCAT_PLAYLIST_SD );
89
90     set_capability( "services_discovery", 0 );
91     set_callbacks( Open, Close );
92
93 vlc_module_end();
94
95
96 /*****************************************************************************
97  * Open: initialize and create stuff
98  *****************************************************************************/
99 static int Open( vlc_object_t *p_this )
100 {
101     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
102     services_discovery_sys_t *p_sys  = malloc(
103                                     sizeof( services_discovery_sys_t ) );
104
105     playlist_t          *p_playlist;
106
107 #ifdef HAVE_HAL_1
108     DBusError           dbus_error;
109     DBusConnection      *p_connection;
110
111     p_sd_global = p_sd;
112     p_sys->i_devices_number = 0;
113     p_sys->pp_devices = NULL;
114 #endif
115
116     p_sd->pf_run = Run;
117     p_sd->p_sys  = p_sys;
118
119 #ifdef HAVE_HAL_1
120     dbus_error_init( &dbus_error );
121
122     p_sys->p_ctx = libhal_ctx_new();
123     if( !p_sys->p_ctx )
124     {
125         msg_Err( p_sd, "unable to create HAL context") ;
126         free( p_sys );
127         return VLC_EGENERIC;
128     }
129     p_connection = dbus_bus_get( DBUS_BUS_SYSTEM, &dbus_error );
130     if( dbus_error_is_set( &dbus_error ) )
131     {
132         msg_Err( p_sd, "unable to connect to DBUS: %s", dbus_error.message );
133         dbus_error_free( &dbus_error );
134         free( p_sys );
135         return VLC_EGENERIC;
136     }
137     libhal_ctx_set_dbus_connection( p_sys->p_ctx, p_connection );
138     p_sys->p_connection = p_connection;
139     if( !libhal_ctx_init( p_sys->p_ctx, &dbus_error ) )
140 #else
141     if( !(p_sys->p_ctx = hal_initialize( NULL, FALSE ) ) )
142 #endif
143     {
144 #ifdef HAVE_HAL_1
145         msg_Err( p_sd, "hal not available : %s", dbus_error.message );
146         dbus_error_free( &dbus_error );
147 #else
148         msg_Err( p_sd, "hal not available" );
149 #endif
150         free( p_sys );
151         return VLC_EGENERIC;
152     }
153
154 #ifdef HAVE_HAL_1
155         if( !libhal_ctx_set_device_added( p_sys->p_ctx, DeviceAdded ) ||
156                 !libhal_ctx_set_device_removed( p_sys->p_ctx, DeviceRemoved ) )
157         {
158             msg_Err( p_sd, "unable to add callback" );
159             dbus_error_free( &dbus_error );
160             free( p_sys );
161             return VLC_EGENERIC;
162         }
163 #endif
164
165     /* Create our playlist node */
166     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
167                                                 FIND_ANYWHERE );
168     if( !p_playlist )
169     {
170         msg_Warn( p_sd, "unable to find playlist, cancelling HAL listening");
171         return VLC_EGENERIC;
172     }
173
174     playlist_NodesPairCreate( p_playlist, _("Devices"),
175                               &p_sys->p_node_cat, &p_sys->p_node_one,
176                               VLC_TRUE );
177     vlc_object_release( p_playlist );
178
179     return VLC_SUCCESS;
180 }
181
182 /*****************************************************************************
183  * Close:
184  *****************************************************************************/
185 static void Close( vlc_object_t *p_this )
186 {
187     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
188     services_discovery_sys_t *p_sys  = p_sd->p_sys;
189     playlist_t *p_playlist =  (playlist_t *) vlc_object_find( p_sd,
190                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
191     if( p_playlist )
192     {
193         playlist_NodeDelete( p_playlist, p_sys->p_node_cat, VLC_TRUE,VLC_TRUE );
194         playlist_NodeDelete( p_playlist, p_sys->p_node_one, VLC_TRUE,VLC_TRUE );
195         vlc_object_release( p_playlist );
196     }
197     free( p_sys );
198 #ifdef HAVE_HAL_1
199     dbus_connection_unref( p_sys->p_connection );
200
201     struct udi_input_id_t *p_udi_entry;
202
203     while( p_sys->i_devices_number > 0 )
204     {
205         p_udi_entry = p_sys->pp_devices[0];
206         if( p_udi_entry->psz_udi ) free( p_udi_entry->psz_udi );
207         TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
208                 p_sys->pp_devices[0] );
209         if( p_udi_entry ) free( p_udi_entry );
210     }
211     p_sys->pp_devices = NULL;
212 #endif
213 }
214
215 static void AddItem( services_discovery_t *p_sd, input_item_t * p_input
216 #ifdef HAVE_HAL_1
217                 ,char* psz_device
218 #endif
219                     )
220 {
221     playlist_item_t *p_item_cat, *p_item_one;
222     services_discovery_sys_t *p_sys  = p_sd->p_sys;
223     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_sd,
224                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
225     if( !p_playlist )
226     {
227         msg_Err( p_sd, "playlist not found" );
228         return;
229     }
230     p_item_cat = playlist_NodeAddInput( p_playlist,
231             p_input,p_sd->p_sys->p_node_cat, PLAYLIST_APPEND, PLAYLIST_END );
232     p_item_cat->i_flags &= ~PLAYLIST_SKIP_FLAG;
233     p_item_one = playlist_NodeAddInput( p_playlist,
234             p_input,p_sd->p_sys->p_node_one, PLAYLIST_APPEND, PLAYLIST_END );
235     p_item_one->i_flags &= ~PLAYLIST_SKIP_FLAG;
236
237     vlc_object_release( p_playlist );
238
239 #ifdef HAVE_HAL_1
240     struct udi_input_id_t *p_udi_entry;
241     p_udi_entry = malloc( sizeof( struct udi_input_id_t ) );
242     if( !p_udi_entry )
243     {
244         return;
245     }
246     p_udi_entry->i_id = p_item_cat->i_id;
247     p_udi_entry->psz_udi = strdup( psz_device );
248     TAB_APPEND( p_sys->i_devices_number, p_sys->pp_devices, p_udi_entry );
249 #endif
250 }
251
252 static void AddDvd( services_discovery_t *p_sd, char *psz_device )
253 {
254     char *psz_name;
255     char *psz_uri;
256     char *psz_blockdevice;
257     input_item_t        *p_input;
258 #ifdef HAVE_HAL_1
259     psz_name = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
260                                         psz_device, "volume.label", NULL );
261     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
262                                         psz_device, "block.device", NULL );
263 #else
264     psz_name = hal_device_get_property_string( p_sd->p_sys->p_ctx,
265                                                psz_device, "volume.label" );
266     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
267                                                  psz_device, "block.device" );
268 #endif
269     asprintf( &psz_uri, "dvd://%s", psz_blockdevice );
270     /* Create the playlist item here */
271     p_input = input_ItemNew( p_sd, psz_uri, psz_name );
272     free( psz_uri );
273     if( !p_input )
274     {
275         return;
276     }
277 #ifdef HAVE_HAL_1
278     AddItem( p_sd, p_input, psz_device );
279 #else
280     AddItem( p_sd, p_input );
281 #endif
282 }
283
284 #ifdef HAVE_HAL_1
285 static void DelItem( services_discovery_t *p_sd, char* psz_udi )
286 {
287     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
288     int                         i,j;
289     playlist_item_t             *p_pl_item;
290
291     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_sd,
292                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
293     if( !p_playlist )
294     {
295         msg_Err( p_sd, "playlist not found" );
296         return;
297     }
298
299     for( i = 0; i < p_sys->i_devices_number; i++ )
300     { /*  looks for a matching udi */
301         if( strcmp( psz_udi, p_sys->pp_devices[i]->psz_udi ) == 0 )
302         { /* delete the corresponding item */
303             p_pl_item = playlist_ItemGetById( p_playlist,
304                 p_sys->pp_devices[i]->i_id, VLC_FALSE );
305             if( p_pl_item )
306             {
307                 j = 0;
308                 while( p_pl_item->i_children > 0 )
309                 { /* delete all childs */
310                     playlist_DeleteFromInput( p_playlist,
311                         p_pl_item->pp_children[j]->p_input->i_id, VLC_FALSE );
312                 }
313                 /* delete parent item */
314
315                 /* HACK: if i_children == 0 the item won't be deleted 
316                  * That means that it _had_ children but they were deleted */
317                 if( p_pl_item->i_children == 0 )
318                     p_pl_item->i_children = -1;
319
320                 playlist_DeleteFromInput( p_playlist,
321                     p_pl_item->p_input->i_id, VLC_FALSE );
322             }
323
324             if( p_sys->pp_devices[i]->psz_udi )
325                 free( p_sys->pp_devices[i]->psz_udi );
326             TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
327                     p_sys->pp_devices[i] );
328         }
329     }
330
331     vlc_object_release( p_playlist );
332 }
333 #endif
334
335 static void AddCdda( services_discovery_t *p_sd, char *psz_device )
336 {
337     char *psz_uri;
338     char *psz_blockdevice;
339     input_item_t     *p_input;
340 #ifdef HAVE_HAL_1
341     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
342                                             psz_device, "block.device", NULL );
343 #else
344     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
345                                                  psz_device, "block.device" );
346 #endif
347     asprintf( &psz_uri, "cdda://%s", psz_blockdevice );
348     /* Create the playlist item here */
349     p_input = input_ItemNew( p_sd, psz_uri, "Audio CD" );
350     free( psz_uri );
351     if( !p_input )
352         return;
353 #ifdef HAVE_HAL_1
354     AddItem( p_sd, p_input, psz_device );
355 #else
356     AddItem( p_sd, p_input );
357 #endif
358 }
359
360 static void ParseDevice( services_discovery_t *p_sd, char *psz_device )
361 {
362     char *psz_disc_type;
363     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
364 #ifdef HAVE_HAL_1
365     if( libhal_device_property_exists( p_sys->p_ctx, psz_device,
366                                        "volume.disc.type", NULL ) )
367     {
368         psz_disc_type = libhal_device_get_property_string( p_sys->p_ctx,
369                                                         psz_device,
370                                                         "volume.disc.type",
371                                                         NULL );
372 #else
373     if( hal_device_property_exists( p_sys->p_ctx, psz_device,
374                                     "volume.disc.type" ) )
375     {
376         psz_disc_type = hal_device_get_property_string( p_sys->p_ctx,
377                                                         psz_device,
378                                                         "volume.disc.type" );
379 #endif
380         if( !strncmp( psz_disc_type, "dvd_r", 5 ) )
381         {
382 #ifdef HAVE_HAL_1
383             /* hal 0.2.9.7 (HAVE_HAL) has not is_videodvd
384              * but hal 0.5.0 (HAVE_HAL_1) has */
385             if (libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
386                                          "volume.disc.is_videodvd", NULL ) )
387 #endif
388             AddDvd( p_sd, psz_device );
389         }
390         else if( !strncmp( psz_disc_type, "cd_r", 4 ) )
391         {
392 #ifdef HAVE_HAL_1
393             if( libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
394                                          "volume.disc.has_audio" , NULL ) )
395 #else
396             if( hal_device_get_property_bool( p_sys->p_ctx, psz_device,
397                                          "volume.disc.has_audio" ) )
398 #endif
399             {
400                 AddCdda( p_sd, psz_device );
401             }
402         }
403     }
404 }
405
406 /*****************************************************************************
407  * Run: main HAL thread
408  *****************************************************************************/
409 static void Run( services_discovery_t *p_sd )
410 {
411     int i, i_devices;
412     char **devices;
413     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
414
415     /* parse existing devices first */
416 #ifdef HAVE_HAL_1
417     if( ( devices = libhal_get_all_devices( p_sys->p_ctx, &i_devices, NULL ) ) )
418 #else
419     if( ( devices = hal_get_all_devices( p_sys->p_ctx, &i_devices ) ) )
420 #endif
421     {
422         for( i = 0; i < i_devices; i++ )
423         {
424             ParseDevice( p_sd, devices[ i ] );
425 #ifdef HAVE_HAL_1
426             libhal_free_string( devices[ i ] );
427 #else
428             hal_free_string( devices[ i ] );
429 #endif
430
431         }
432     }
433 #ifdef HAVE_HAL_1
434     while( !p_sd->b_die )
435     {
436     /* look for events on the bus, blocking 1 second */
437     dbus_connection_read_write_dispatch( p_sys->p_connection, 1000 );
438     /* HAL 0.5.8.1 can use libhal_ctx_get_dbus_connection(p_sys->p_ctx) */
439     }
440 #endif
441
442 }
443
444 #ifdef HAVE_HAL_1
445 void DeviceAdded( LibHalContext *p_ctx, const char *psz_udi )
446 {
447         ParseDevice( p_sd_global, (char*) psz_udi );
448 }
449 void DeviceRemoved( LibHalContext *p_ctx, const char *psz_udi )
450 {
451         DelItem( p_sd_global, (char*) psz_udi );
452 }
453 #endif
454