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