]> git.sesse.net Git - vlc/blob - modules/services_discovery/hal.c
Finish the playlist API transition (hopefully)
[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     struct udi_input_id_t *p_udi_entry;
200
201     while( p_sys->i_devices_number > 0 )
202     {
203         p_udi_entry = p_sys->pp_devices[0];
204         if( p_udi_entry->psz_udi ) free( p_udi_entry->psz_udi );
205         TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
206                 p_sys->pp_devices[0] );
207         if( p_udi_entry ) free( p_udi_entry );
208     }
209     p_sys->pp_devices = NULL;
210 #endif
211 }
212
213 static void AddItem( services_discovery_t *p_sd, input_item_t * p_input
214 #ifdef HAVE_HAL_1
215                 ,char* psz_device
216 #endif
217                     )
218 {
219     playlist_item_t *p_item;
220     services_discovery_sys_t *p_sys  = p_sd->p_sys;
221     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_sd,
222                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
223     if( !p_playlist )
224     {
225         msg_Err( p_sd, "playlist not found" );
226         return;
227     }
228     p_item = playlist_NodeAddInput( p_playlist, p_input,p_sd->p_sys->p_node_cat,
229                                     PLAYLIST_APPEND, PLAYLIST_END );
230     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
231     p_item = playlist_NodeAddInput( p_playlist, p_input,p_sd->p_sys->p_node_one,
232                                     PLAYLIST_APPEND, PLAYLIST_END );
233     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
234
235     vlc_object_release( p_playlist );
236
237 #ifdef HAVE_HAL_1
238     struct udi_input_id_t *p_udi_entry;
239     p_udi_entry = malloc( sizeof( struct udi_input_id_t ) );
240     if( !p_udi_entry )
241     {
242         return;
243     }
244     p_udi_entry->i_id = p_item->i_id;
245     p_udi_entry->psz_udi = strdup( psz_device );
246     TAB_APPEND( p_sys->i_devices_number, p_sys->pp_devices, p_udi_entry );
247 #endif
248 }
249
250 static void AddDvd( services_discovery_t *p_sd, char *psz_device )
251 {
252     char *psz_name;
253     char *psz_uri;
254     char *psz_blockdevice;
255     input_item_t        *p_input;
256 #ifdef HAVE_HAL_1
257     psz_name = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
258                                         psz_device, "volume.label", NULL );
259     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
260                                         psz_device, "block.device", NULL );
261 #else
262     psz_name = hal_device_get_property_string( p_sd->p_sys->p_ctx,
263                                                psz_device, "volume.label" );
264     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
265                                                  psz_device, "block.device" );
266 #endif
267     asprintf( &psz_uri, "dvd://%s", psz_blockdevice );
268     /* Create the playlist item here */
269     p_input = input_ItemNew( p_sd, psz_uri, psz_name );
270     free( psz_uri );
271     if( !p_input )
272     {
273         return;
274     }
275 #ifdef HAVE_HAL_1
276     AddItem( p_sd, p_input, psz_device );
277 #else
278     AddItem( p_sd, p_input );
279 #endif
280 }
281
282 #ifdef HAVE_HAL_1
283 static void DelItem( services_discovery_t *p_sd, char* psz_udi )
284 {
285     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
286     int                         i;
287
288     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_sd,
289                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
290     if( !p_playlist )
291     {
292         msg_Err( p_sd, "playlist not found" );
293         return;
294     }
295
296     for( i = 0; i < p_sys->i_devices_number; i++ )
297     {
298         if( strcmp( psz_udi, p_sys->pp_devices[i]->psz_udi ) == 0 )
299         {
300             msg_Err(p_sd, "HAL delete must be fixed");
301 /*      playlist_DeleteFromItemId( p_playlist, p_sys->pp_devices[i]->i_id );*/
302             TAB_REMOVE( p_sys->i_devices_number, p_sys->pp_devices,
303                     p_sys->pp_devices[i] );
304         }
305     }
306
307     vlc_object_release( p_playlist );
308 }
309 #endif
310
311 static void AddCdda( services_discovery_t *p_sd, char *psz_device )
312 {
313     char *psz_uri;
314     char *psz_blockdevice;
315     input_item_t     *p_input;
316 #ifdef HAVE_HAL_1
317     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
318                                             psz_device, "block.device", NULL );
319 #else
320     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
321                                                  psz_device, "block.device" );
322 #endif
323     asprintf( &psz_uri, "cdda://%s", psz_blockdevice );
324     /* Create the playlist item here */
325     p_input = input_ItemNew( p_sd, psz_uri, "Audio CD" );
326     free( psz_uri );
327     if( !p_input )
328         return;
329 #ifdef HAVE_HAL_1
330     AddItem( p_sd, p_input, psz_device );
331 #else
332     AddItem( p_sd, p_input );
333 #endif
334 }
335
336 static void ParseDevice( services_discovery_t *p_sd, char *psz_device )
337 {
338     char *psz_disc_type;
339     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
340 #ifdef HAVE_HAL_1
341     if( libhal_device_property_exists( p_sys->p_ctx, psz_device,
342                                        "volume.disc.type", NULL ) )
343     {
344         psz_disc_type = libhal_device_get_property_string( p_sys->p_ctx,
345                                                         psz_device,
346                                                         "volume.disc.type",
347                                                         NULL );
348 #else
349     if( hal_device_property_exists( p_sys->p_ctx, psz_device,
350                                     "volume.disc.type" ) )
351     {
352         psz_disc_type = hal_device_get_property_string( p_sys->p_ctx,
353                                                         psz_device,
354                                                         "volume.disc.type" );
355 #endif
356         if( !strcmp( psz_disc_type, "dvd_rom" ) )
357         {
358 #ifdef HAVE_HAL_1
359             /* hal 0.2.9.7 (HAVE_HAL) has not is_videodvd
360              * but hal 0.5.0 (HAVE_HAL_1) has */
361             if (libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
362                                          "volume.disc.is_videodvd", NULL ) )
363 #endif
364             AddDvd( p_sd, psz_device );
365         }
366         else if( !strcmp( psz_disc_type, "cd_rom" ) )
367         {
368 #ifdef HAVE_HAL_1
369             if( libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
370                                          "volume.disc.has_audio" , NULL ) )
371 #else
372             if( hal_device_get_property_bool( p_sys->p_ctx, psz_device,
373                                          "volume.disc.has_audio" ) )
374 #endif
375             {
376                 AddCdda( p_sd, psz_device );
377             }
378         }
379     }
380 }
381
382 /*****************************************************************************
383  * Run: main HAL thread
384  *****************************************************************************/
385 static void Run( services_discovery_t *p_sd )
386 {
387     int i, i_devices;
388     char **devices;
389     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
390
391     /* parse existing devices first */
392 #ifdef HAVE_HAL_1
393     if( ( devices = libhal_get_all_devices( p_sys->p_ctx, &i_devices, NULL ) ) )
394 #else
395     if( ( devices = hal_get_all_devices( p_sys->p_ctx, &i_devices ) ) )
396 #endif
397     {
398         for( i = 0; i < i_devices; i++ )
399         {
400             ParseDevice( p_sd, devices[ i ] );
401 #ifdef HAVE_HAL_1
402             libhal_free_string( devices[ i ] );
403 #else
404             hal_free_string( devices[ i ] );
405 #endif
406
407         }
408     }
409 #ifdef HAVE_HAL_1
410     while( !p_sd->b_die )
411     {
412     /* look for events on the bus, blocking 1 second */
413     dbus_connection_read_write_dispatch( p_sys->p_connection, 1000 );
414     /* HAL 0.5.8.1 can use libhal_ctx_get_dbus_connection(p_sys->p_ctx) */
415     }
416 #endif
417
418 }
419
420 #ifdef HAVE_HAL_1
421 void DeviceAdded( LibHalContext *p_ctx, const char *psz_udi )
422 {
423         ParseDevice( p_sd_global, (char*) psz_udi );
424 }
425 void DeviceRemoved( LibHalContext *p_ctx, const char *psz_udi )
426 {
427         DelItem( p_sd_global, (char*) psz_udi );
428 }
429 #endif
430