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