]> git.sesse.net Git - vlc/blob - modules/services_discovery/hal.c
Don't include config.h from the headers - refs #297.
[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         if( p_udi_entry->psz_udi ) 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         if( p_udi_entry ) 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                 ,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, 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, 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             if( p_sys->pp_devices[i]->psz_udi )
262                 free( p_sys->pp_devices[i]->psz_udi );
263             TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
264                     p_sys->pp_devices[i] );
265         }
266     }
267 }
268 #endif
269
270 static void AddCdda( services_discovery_t *p_sd, char *psz_device )
271 {
272     char *psz_uri;
273     char *psz_blockdevice;
274     input_item_t     *p_input;
275 #ifdef HAVE_HAL_1
276     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
277                                             psz_device, "block.device", NULL );
278 #else
279     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
280                                                  psz_device, "block.device" );
281 #endif
282     if( asprintf( &psz_uri, "cdda://%s", psz_blockdevice ) == -1 )
283         return;
284     /* Create the item here */
285     p_input = input_ItemNew( p_sd, psz_uri, "Audio CD" );
286     free( psz_uri );
287     if( !p_input )
288         return;
289 #ifdef HAVE_HAL_1
290     AddItem( p_sd, p_input, psz_device );
291 #else
292     AddItem( p_sd, p_input );
293 #endif
294     vlc_gc_decref( p_input );
295 }
296
297 static void ParseDevice( services_discovery_t *p_sd, char *psz_device )
298 {
299     char *psz_disc_type;
300     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
301 #ifdef HAVE_HAL_1
302     if( libhal_device_property_exists( p_sys->p_ctx, psz_device,
303                                        "volume.disc.type", NULL ) )
304     {
305         psz_disc_type = libhal_device_get_property_string( p_sys->p_ctx,
306                                                         psz_device,
307                                                         "volume.disc.type",
308                                                         NULL );
309 #else
310     if( hal_device_property_exists( p_sys->p_ctx, psz_device,
311                                     "volume.disc.type" ) )
312     {
313         psz_disc_type = hal_device_get_property_string( p_sys->p_ctx,
314                                                         psz_device,
315                                                         "volume.disc.type" );
316 #endif
317         if( !strncmp( psz_disc_type, "dvd_r", 5 ) )
318         {
319 #ifdef HAVE_HAL_1
320             /* hal 0.2.9.7 (HAVE_HAL) has not is_videodvd
321              * but hal 0.5.0 (HAVE_HAL_1) has */
322             if (libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
323                                          "volume.disc.is_videodvd", NULL ) )
324 #endif
325             AddDvd( p_sd, psz_device );
326         }
327         else if( !strncmp( psz_disc_type, "cd_r", 4 ) )
328         {
329 #ifdef HAVE_HAL_1
330             if( libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
331                                          "volume.disc.has_audio" , NULL ) )
332 #else
333             if( hal_device_get_property_bool( p_sys->p_ctx, psz_device,
334                                          "volume.disc.has_audio" ) )
335 #endif
336             {
337                 AddCdda( p_sd, psz_device );
338             }
339         }
340     }
341 }
342
343 /*****************************************************************************
344  * Run: main HAL thread
345  *****************************************************************************/
346 static void Run( services_discovery_t *p_sd )
347 {
348     int i, i_devices;
349     char **devices;
350     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
351
352     /* parse existing devices first */
353 #ifdef HAVE_HAL_1
354     if( ( devices = libhal_get_all_devices( p_sys->p_ctx, &i_devices, NULL ) ) )
355 #else
356     if( ( devices = hal_get_all_devices( p_sys->p_ctx, &i_devices ) ) )
357 #endif
358     {
359         for( i = 0; i < i_devices; i++ )
360         {
361             ParseDevice( p_sd, devices[ i ] );
362 #ifdef HAVE_HAL_1
363             libhal_free_string( devices[ i ] );
364 #else
365             hal_free_string( devices[ i ] );
366 #endif
367
368         }
369     }
370 #ifdef HAVE_HAL_1
371     while( !p_sd->b_die )
372     {
373     /* look for events on the bus, blocking 1 second */
374     dbus_connection_read_write_dispatch( p_sys->p_connection, 1000 );
375     /* HAL 0.5.8.1 can use libhal_ctx_get_dbus_connection(p_sys->p_ctx) */
376     }
377 #endif
378
379 }
380
381 #ifdef HAVE_HAL_1
382 void DeviceAdded( LibHalContext *p_ctx, const char *psz_udi )
383 {
384         ParseDevice( p_sd_global, (char*) psz_udi );
385 }
386 void DeviceRemoved( LibHalContext *p_ctx, const char *psz_udi )
387 {
388         DelItem( p_sd_global, (char*) psz_udi );
389 }
390 #endif
391