]> git.sesse.net Git - vlc/blob - modules/services_discovery/hal.c
fa23ac39abb9db4a7a658089758f0d0e87180e3f
[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     if( !p_sys )
105         return VLC_ENOMEM;
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     services_discovery_SetLocalizedName( p_sd, _("Devices") );
166
167     return VLC_SUCCESS;
168 }
169
170 /*****************************************************************************
171  * Close:
172  *****************************************************************************/
173 static void Close( vlc_object_t *p_this )
174 {
175     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
176     services_discovery_sys_t *p_sys  = p_sd->p_sys;
177 #ifdef HAVE_HAL_1
178     dbus_connection_unref( p_sys->p_connection );
179     struct udi_input_id_t *p_udi_entry;
180
181     while( p_sys->i_devices_number > 0 )
182     {
183         p_udi_entry = p_sys->pp_devices[0];
184         free( p_udi_entry->psz_udi );
185         TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
186                 p_sys->pp_devices[0] );
187         free( p_udi_entry );
188     }
189     p_sys->pp_devices = NULL;
190 #endif
191     free( p_sys );
192 }
193
194 static void AddItem( services_discovery_t *p_sd, input_item_t * p_input
195 #ifdef HAVE_HAL_1
196                 , const char* psz_device
197 #endif
198                     )
199 {
200     services_discovery_sys_t *p_sys  = p_sd->p_sys;
201     services_discovery_AddItem( p_sd, p_input, NULL /* no category */ );
202
203 #ifdef HAVE_HAL_1
204     struct udi_input_id_t *p_udi_entry;
205     p_udi_entry = malloc( sizeof( struct udi_input_id_t ) );
206     if( !p_udi_entry )
207         return;
208     p_udi_entry->psz_udi = strdup( psz_device );
209     if( !p_udi_entry->psz_udi )
210         return;
211
212     vlc_gc_incref( p_input );
213     p_udi_entry->p_item = p_input;
214     TAB_APPEND( p_sys->i_devices_number, p_sys->pp_devices, p_udi_entry );
215 #endif
216 }
217
218 static void AddDvd( services_discovery_t *p_sd, const char *psz_device )
219 {
220     char *psz_name;
221     char *psz_uri;
222     char *psz_blockdevice;
223     input_item_t        *p_input;
224 #ifdef HAVE_HAL_1
225     psz_name = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
226                                         psz_device, "volume.label", NULL );
227     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
228                                         psz_device, "block.device", NULL );
229 #else
230     psz_name = hal_device_get_property_string( p_sd->p_sys->p_ctx,
231                                                psz_device, "volume.label" );
232     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
233                                                  psz_device, "block.device" );
234 #endif
235     if( asprintf( &psz_uri, "dvd://%s", psz_blockdevice ) == -1 )
236         return;
237     /* Create the playlist item here */
238     p_input = input_ItemNew( p_sd, psz_uri, psz_name );
239     free( psz_uri );
240     if( !p_input )
241     {
242         return;
243     }
244 #ifdef HAVE_HAL_1
245     AddItem( p_sd, p_input, psz_device );
246 #else
247     AddItem( p_sd, p_input );
248 #endif
249     vlc_gc_decref( p_input );
250 }
251
252 #ifdef HAVE_HAL_1
253 static void DelItem( services_discovery_t *p_sd, const char* psz_udi )
254 {
255     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
256
257     int i;
258     for( i = 0; i < p_sys->i_devices_number; i++ )
259     { /*  looks for a matching udi */
260         if( strcmp( psz_udi, p_sys->pp_devices[i]->psz_udi ) == 0 )
261         { /* delete the corresponding item */    
262             services_discovery_RemoveItem( p_sd, p_sys->pp_devices[i]->p_item );
263             vlc_gc_decref( p_sys->pp_devices[i]->p_item );
264             free( p_sys->pp_devices[i]->psz_udi );
265             TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
266                     p_sys->pp_devices[i] );
267         }
268     }
269 }
270 #endif
271
272 static void AddCdda( services_discovery_t *p_sd, const char *psz_device )
273 {
274     char *psz_uri;
275     char *psz_blockdevice;
276     input_item_t     *p_input;
277 #ifdef HAVE_HAL_1
278     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
279                                             psz_device, "block.device", NULL );
280 #else
281     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
282                                                  psz_device, "block.device" );
283 #endif
284     if( asprintf( &psz_uri, "cdda://%s", psz_blockdevice ) == -1 )
285         return;
286     /* Create the item here */
287     p_input = input_ItemNew( p_sd, psz_uri, "Audio CD" );
288     free( psz_uri );
289     if( !p_input )
290         return;
291 #ifdef HAVE_HAL_1
292     AddItem( p_sd, p_input, psz_device );
293 #else
294     AddItem( p_sd, p_input );
295 #endif
296     vlc_gc_decref( p_input );
297 }
298
299 static void ParseDevice( services_discovery_t *p_sd, const char *psz_device )
300 {
301     char *psz_disc_type;
302     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
303 #ifdef HAVE_HAL_1
304     if( libhal_device_property_exists( p_sys->p_ctx, psz_device,
305                                        "volume.disc.type", NULL ) )
306     {
307         psz_disc_type = libhal_device_get_property_string( p_sys->p_ctx,
308                                                         psz_device,
309                                                         "volume.disc.type",
310                                                         NULL );
311 #else
312     if( hal_device_property_exists( p_sys->p_ctx, psz_device,
313                                     "volume.disc.type" ) )
314     {
315         psz_disc_type = hal_device_get_property_string( p_sys->p_ctx,
316                                                         psz_device,
317                                                         "volume.disc.type" );
318 #endif
319         if( !strncmp( psz_disc_type, "dvd_r", 5 ) )
320         {
321 #ifdef HAVE_HAL_1
322             /* hal 0.2.9.7 (HAVE_HAL) has not is_videodvd
323              * but hal 0.5.0 (HAVE_HAL_1) has */
324             if (libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
325                                          "volume.disc.is_videodvd", NULL ) )
326 #endif
327             AddDvd( p_sd, psz_device );
328         }
329         else if( !strncmp( psz_disc_type, "cd_r", 4 ) )
330         {
331 #ifdef HAVE_HAL_1
332             if( libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
333                                          "volume.disc.has_audio" , NULL ) )
334 #else
335             if( hal_device_get_property_bool( p_sys->p_ctx, psz_device,
336                                          "volume.disc.has_audio" ) )
337 #endif
338             {
339                 AddCdda( p_sd, psz_device );
340             }
341         }
342     }
343 }
344
345 /*****************************************************************************
346  * Run: main HAL thread
347  *****************************************************************************/
348 static void Run( services_discovery_t *p_sd )
349 {
350     int i, i_devices;
351     char **devices;
352     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
353
354     /* parse existing devices first */
355 #ifdef HAVE_HAL_1
356     if( ( devices = libhal_get_all_devices( p_sys->p_ctx, &i_devices, NULL ) ) )
357 #else
358     if( ( devices = hal_get_all_devices( p_sys->p_ctx, &i_devices ) ) )
359 #endif
360     {
361         for( i = 0; i < i_devices; i++ )
362         {
363             ParseDevice( p_sd, devices[ i ] );
364 #ifdef HAVE_HAL_1
365             libhal_free_string( devices[ i ] );
366 #else
367             hal_free_string( devices[ i ] );
368 #endif
369
370         }
371     }
372 #ifdef HAVE_HAL_1
373     while( !p_sd->b_die )
374     {
375     /* look for events on the bus, blocking 1 second */
376     dbus_connection_read_write_dispatch( p_sys->p_connection, 1000 );
377     /* HAL 0.5.8.1 can use libhal_ctx_get_dbus_connection(p_sys->p_ctx) */
378     }
379 #endif
380
381 }
382
383 #ifdef HAVE_HAL_1
384 void DeviceAdded( LibHalContext *p_ctx, const char *psz_udi )
385 {
386     VLC_UNUSED(p_ctx);
387     ParseDevice( p_sd_global, psz_udi );
388 }
389 void DeviceRemoved( LibHalContext *p_ctx, const char *psz_udi )
390 {
391     VLC_UNUSED(p_ctx);
392     DelItem( p_sd_global, psz_udi );
393 }
394 #endif
395