]> git.sesse.net Git - vlc/blob - modules/services_discovery/hal.c
Remove useless test before a free()
[vlc] / modules / services_discovery / hal.c
1 /*****************************************************************************
2  * hal.c :  HAL interface module
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * Copyright © 2006-2007 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 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <vlc/vlc.h>
31 #include <vlc_playlist.h>
32
33 #include <vlc_network.h>
34
35 #include <errno.h>                                                 /* ENOMEM */
36
37 #ifdef HAVE_UNISTD_H
38 #    include <unistd.h>
39 #endif
40 #ifdef HAVE_SYS_TIME_H
41 #    include <sys/time.h>
42 #endif
43
44 #include <hal/libhal.h>
45
46 #define MAX_LINE_LENGTH 256
47
48 /*****************************************************************************
49  * Local prototypes
50  *****************************************************************************/
51 #ifdef HAVE_HAL_1
52 /* store relation between item id and udi for ejection */
53 struct udi_input_id_t
54 {
55     char            *psz_udi;
56     input_item_t    *p_item;
57 };
58 #endif
59
60 struct services_discovery_sys_t
61 {
62     LibHalContext           *p_ctx;
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 #ifdef HAVE_HAL_1
106     DBusError           dbus_error;
107     DBusConnection      *p_connection;
108
109     p_sd_global = p_sd;
110     p_sys->i_devices_number = 0;
111     p_sys->pp_devices = NULL;
112 #endif
113
114     p_sd->pf_run = Run;
115     p_sd->p_sys  = p_sys;
116
117 #ifdef HAVE_HAL_1
118     dbus_error_init( &dbus_error );
119
120     p_sys->p_ctx = libhal_ctx_new();
121     if( !p_sys->p_ctx )
122     {
123         msg_Err( p_sd, "unable to create HAL context") ;
124         free( p_sys );
125         return VLC_EGENERIC;
126     }
127     p_connection = dbus_bus_get( DBUS_BUS_SYSTEM, &dbus_error );
128     if( dbus_error_is_set( &dbus_error ) )
129     {
130         msg_Err( p_sd, "unable to connect to DBUS: %s", dbus_error.message );
131         dbus_error_free( &dbus_error );
132         free( p_sys );
133         return VLC_EGENERIC;
134     }
135     libhal_ctx_set_dbus_connection( p_sys->p_ctx, p_connection );
136     p_sys->p_connection = p_connection;
137     if( !libhal_ctx_init( p_sys->p_ctx, &dbus_error ) )
138 #else
139     if( !(p_sys->p_ctx = hal_initialize( NULL, FALSE ) ) )
140 #endif
141     {
142 #ifdef HAVE_HAL_1
143         msg_Err( p_sd, "hal not available : %s", dbus_error.message );
144         dbus_error_free( &dbus_error );
145 #else
146         msg_Err( p_sd, "hal not available" );
147 #endif
148         free( p_sys );
149         return VLC_EGENERIC;
150     }
151
152 #ifdef HAVE_HAL_1
153         if( !libhal_ctx_set_device_added( p_sys->p_ctx, DeviceAdded ) ||
154                 !libhal_ctx_set_device_removed( p_sys->p_ctx, DeviceRemoved ) )
155         {
156             msg_Err( p_sd, "unable to add callback" );
157             dbus_error_free( &dbus_error );
158             free( p_sys );
159             return VLC_EGENERIC;
160         }
161 #endif
162
163     services_discovery_SetLocalizedName( p_sd, _("Devices") );
164
165     return VLC_SUCCESS;
166 }
167
168 /*****************************************************************************
169  * Close:
170  *****************************************************************************/
171 static void Close( vlc_object_t *p_this )
172 {
173     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
174     services_discovery_sys_t *p_sys  = p_sd->p_sys;
175 #ifdef HAVE_HAL_1
176     dbus_connection_unref( p_sys->p_connection );
177     struct udi_input_id_t *p_udi_entry;
178
179     while( p_sys->i_devices_number > 0 )
180     {
181         p_udi_entry = p_sys->pp_devices[0];
182         free( p_udi_entry->psz_udi );
183         TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
184                 p_sys->pp_devices[0] );
185         free( p_udi_entry );
186     }
187     p_sys->pp_devices = NULL;
188 #endif
189     free( p_sys );
190 }
191
192 static void AddItem( services_discovery_t *p_sd, input_item_t * p_input
193 #ifdef HAVE_HAL_1
194                 , const char* psz_device
195 #endif
196                     )
197 {
198     services_discovery_sys_t *p_sys  = p_sd->p_sys;
199     services_discovery_AddItem( p_sd, p_input, NULL /* no category */ );
200
201 #ifdef HAVE_HAL_1
202     struct udi_input_id_t *p_udi_entry;
203     p_udi_entry = malloc( sizeof( struct udi_input_id_t ) );
204     if( !p_udi_entry )
205     {
206         return;
207     }
208     vlc_gc_incref( p_input );
209     p_udi_entry->p_item = p_input;
210     p_udi_entry->psz_udi = strdup( psz_device );
211     TAB_APPEND( p_sys->i_devices_number, p_sys->pp_devices, p_udi_entry );
212 #endif
213 }
214
215 static void AddDvd( services_discovery_t *p_sd, const char *psz_device )
216 {
217     char *psz_name;
218     char *psz_uri;
219     char *psz_blockdevice;
220     input_item_t        *p_input;
221 #ifdef HAVE_HAL_1
222     psz_name = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
223                                         psz_device, "volume.label", NULL );
224     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
225                                         psz_device, "block.device", NULL );
226 #else
227     psz_name = hal_device_get_property_string( p_sd->p_sys->p_ctx,
228                                                psz_device, "volume.label" );
229     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
230                                                  psz_device, "block.device" );
231 #endif
232     if( asprintf( &psz_uri, "dvd://%s", psz_blockdevice ) == -1 )
233         return;
234     /* Create the playlist item here */
235     p_input = input_ItemNew( p_sd, psz_uri, psz_name );
236     free( psz_uri );
237     if( !p_input )
238     {
239         return;
240     }
241 #ifdef HAVE_HAL_1
242     AddItem( p_sd, p_input, psz_device );
243 #else
244     AddItem( p_sd, p_input );
245 #endif
246     vlc_gc_decref( p_input );
247 }
248
249 #ifdef HAVE_HAL_1
250 static void DelItem( services_discovery_t *p_sd, const char* psz_udi )
251 {
252     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
253
254     int i;
255     for( i = 0; i < p_sys->i_devices_number; i++ )
256     { /*  looks for a matching udi */
257         if( strcmp( psz_udi, p_sys->pp_devices[i]->psz_udi ) == 0 )
258         { /* delete the corresponding item */    
259             services_discovery_RemoveItem( p_sd, p_sys->pp_devices[i]->p_item );
260             vlc_gc_decref( p_sys->pp_devices[i]->p_item );
261             free( p_sys->pp_devices[i]->psz_udi );
262             TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
263                     p_sys->pp_devices[i] );
264         }
265     }
266 }
267 #endif
268
269 static void AddCdda( services_discovery_t *p_sd, const char *psz_device )
270 {
271     char *psz_uri;
272     char *psz_blockdevice;
273     input_item_t     *p_input;
274 #ifdef HAVE_HAL_1
275     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
276                                             psz_device, "block.device", NULL );
277 #else
278     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
279                                                  psz_device, "block.device" );
280 #endif
281     if( asprintf( &psz_uri, "cdda://%s", psz_blockdevice ) == -1 )
282         return;
283     /* Create the item here */
284     p_input = input_ItemNew( p_sd, psz_uri, "Audio CD" );
285     free( psz_uri );
286     if( !p_input )
287         return;
288 #ifdef HAVE_HAL_1
289     AddItem( p_sd, p_input, psz_device );
290 #else
291     AddItem( p_sd, p_input );
292 #endif
293     vlc_gc_decref( p_input );
294 }
295
296 static void ParseDevice( services_discovery_t *p_sd, const char *psz_device )
297 {
298     char *psz_disc_type;
299     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
300 #ifdef HAVE_HAL_1
301     if( libhal_device_property_exists( p_sys->p_ctx, psz_device,
302                                        "volume.disc.type", NULL ) )
303     {
304         psz_disc_type = libhal_device_get_property_string( p_sys->p_ctx,
305                                                         psz_device,
306                                                         "volume.disc.type",
307                                                         NULL );
308 #else
309     if( hal_device_property_exists( p_sys->p_ctx, psz_device,
310                                     "volume.disc.type" ) )
311     {
312         psz_disc_type = hal_device_get_property_string( p_sys->p_ctx,
313                                                         psz_device,
314                                                         "volume.disc.type" );
315 #endif
316         if( !strncmp( psz_disc_type, "dvd_r", 5 ) )
317         {
318 #ifdef HAVE_HAL_1
319             /* hal 0.2.9.7 (HAVE_HAL) has not is_videodvd
320              * but hal 0.5.0 (HAVE_HAL_1) has */
321             if (libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
322                                          "volume.disc.is_videodvd", NULL ) )
323 #endif
324             AddDvd( p_sd, psz_device );
325         }
326         else if( !strncmp( psz_disc_type, "cd_r", 4 ) )
327         {
328 #ifdef HAVE_HAL_1
329             if( libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
330                                          "volume.disc.has_audio" , NULL ) )
331 #else
332             if( hal_device_get_property_bool( p_sys->p_ctx, psz_device,
333                                          "volume.disc.has_audio" ) )
334 #endif
335             {
336                 AddCdda( p_sd, psz_device );
337             }
338         }
339     }
340 }
341
342 /*****************************************************************************
343  * Run: main HAL thread
344  *****************************************************************************/
345 static void Run( services_discovery_t *p_sd )
346 {
347     int i, i_devices;
348     char **devices;
349     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
350
351     /* parse existing devices first */
352 #ifdef HAVE_HAL_1
353     if( ( devices = libhal_get_all_devices( p_sys->p_ctx, &i_devices, NULL ) ) )
354 #else
355     if( ( devices = hal_get_all_devices( p_sys->p_ctx, &i_devices ) ) )
356 #endif
357     {
358         for( i = 0; i < i_devices; i++ )
359         {
360             ParseDevice( p_sd, devices[ i ] );
361 #ifdef HAVE_HAL_1
362             libhal_free_string( devices[ i ] );
363 #else
364             hal_free_string( devices[ i ] );
365 #endif
366
367         }
368     }
369 #ifdef HAVE_HAL_1
370     while( !p_sd->b_die )
371     {
372     /* look for events on the bus, blocking 1 second */
373     dbus_connection_read_write_dispatch( p_sys->p_connection, 1000 );
374     /* HAL 0.5.8.1 can use libhal_ctx_get_dbus_connection(p_sys->p_ctx) */
375     }
376 #endif
377
378 }
379
380 #ifdef HAVE_HAL_1
381 void DeviceAdded( LibHalContext *p_ctx, const char *psz_udi )
382 {
383     VLC_UNUSED(p_ctx);
384     ParseDevice( p_sd_global, psz_udi );
385 }
386 void DeviceRemoved( LibHalContext *p_ctx, const char *psz_udi )
387 {
388     VLC_UNUSED(p_ctx);
389     DelItem( p_sd_global, psz_udi );
390 }
391 #endif
392