]> git.sesse.net Git - vlc/blob - modules/services_discovery/hal.c
Uniformize source files encoding
[vlc] / modules / services_discovery / hal.c
1 /*****************************************************************************
2  * sap.c :  SAP interface module
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Includes
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/intf.h>
31
32 #include <vlc/input.h>
33
34 #include "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 /************************************************************************
48  * Macros and definitions
49  ************************************************************************/
50
51 #define MAX_LINE_LENGTH 256
52
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57
58 /* Callbacks */
59     static int  Open ( vlc_object_t * );
60     static void Close( vlc_object_t * );
61
62 vlc_module_begin();
63     set_description( _("HAL device detection") );
64     set_category( CAT_PLAYLIST );
65     set_subcategory( SUBCAT_PLAYLIST_SD );
66
67     set_capability( "services_discovery", 0 );
68     set_callbacks( Open, Close );
69
70 vlc_module_end();
71
72
73 /*****************************************************************************
74  * Local structures
75  *****************************************************************************/
76
77 struct services_discovery_sys_t
78 {
79     LibHalContext *p_ctx;
80
81     /* playlist node */
82     playlist_item_t *p_node;
83
84 };
85
86 /*****************************************************************************
87  * Local prototypes
88  *****************************************************************************/
89
90 /* Main functions */
91     static void Run    ( services_discovery_t *p_intf );
92
93 /*****************************************************************************
94  * Open: initialize and create stuff
95  *****************************************************************************/
96 static int Open( vlc_object_t *p_this )
97 {
98     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
99     services_discovery_sys_t *p_sys  = malloc(
100                                     sizeof( services_discovery_sys_t ) );
101
102     vlc_value_t         val;
103     playlist_t          *p_playlist;
104     playlist_view_t     *p_view;
105
106     DBusError           dbus_error;
107     DBusConnection      *p_connection;
108
109     p_sd->pf_run = Run;
110     p_sd->p_sys  = p_sys;
111
112     dbus_error_init( &dbus_error );
113
114 #ifdef HAVE_HAL_1
115     p_sys->p_ctx = libhal_ctx_new();
116     if( !p_sys->p_ctx )
117     {
118         msg_Err( p_sd, "Unable to create HAL context") ;
119         free( p_sys );
120         return VLC_EGENERIC;
121     }
122     p_connection = dbus_bus_get( DBUS_BUS_SYSTEM, &dbus_error );
123     if( dbus_error_is_set( &dbus_error ) )
124     {
125         msg_Err( p_sd, "Unable to connect to DBUS: %s", dbus_error.message );
126         dbus_error_free( &dbus_error );
127         free( p_sys );
128         return VLC_EGENERIC;
129     }
130     libhal_ctx_set_dbus_connection( p_sys->p_ctx, p_connection );
131     if( !libhal_ctx_init( p_sys->p_ctx, &dbus_error ) )
132 #else
133     if( !(p_sys->p_ctx = hal_initialize( NULL, FALSE ) ) )
134 #endif
135     {
136         msg_Err( p_sd, "hal not available : %s", dbus_error.message );
137         dbus_error_free( &dbus_error );
138         free( p_sys );
139         return VLC_EGENERIC;
140     }
141
142     /* Create our playlist node */
143     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
144                                                 FIND_ANYWHERE );
145     if( !p_playlist )
146     {
147         msg_Warn( p_sd, "unable to find playlist, cancelling HAL listening");
148         return VLC_EGENERIC;
149     }
150
151     p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
152     p_sys->p_node = playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
153                                          _("Devices"), p_view->p_root );
154
155     p_sys->p_node->i_flags |= PLAYLIST_RO_FLAG;
156     val.b_bool = VLC_TRUE;
157     var_Set( p_playlist, "intf-change", val );
158
159     vlc_object_release( p_playlist );
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     playlist_t *p_playlist =  (playlist_t *) vlc_object_find( p_sd,
172                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
173     if( p_playlist )
174     {
175         playlist_NodeDelete( p_playlist, p_sys->p_node, VLC_TRUE, VLC_TRUE );
176         vlc_object_release( p_playlist );
177     }
178     free( p_sys );
179 }
180
181 static void AddDvd( services_discovery_t *p_sd, char *psz_device )
182 {
183     char *psz_name;
184     char *psz_uri;
185     char *psz_blockdevice;
186     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
187     playlist_t          *p_playlist;
188     playlist_item_t     *p_item;
189 #ifdef HAVE_HAL_1
190     psz_name = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
191                                         psz_device, "volume.label", NULL );
192     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
193                                         psz_device, "block.device", NULL );
194 #else
195     psz_name = hal_device_get_property_string( p_sd->p_sys->p_ctx,
196                                                psz_device, "volume.label" );
197     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
198                                                  psz_device, "block.device" );
199 #endif
200     asprintf( &psz_uri, "dvd://%s", psz_blockdevice );
201     /* Create the playlist item here */
202     p_item = playlist_ItemNew( p_sd, psz_uri,
203                                psz_name );
204     free( psz_uri );
205 #ifdef HAVE_HAL_1
206     libhal_free_string( psz_device );
207 #else
208     hal_free_string( psz_device );
209 #endif
210     if( !p_item )
211     {
212         return;
213     }
214     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
215     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
216                                                 FIND_ANYWHERE );
217     if( !p_playlist )
218     {
219         msg_Err( p_sd, "playlist not found" );
220         return;
221     }
222
223     playlist_NodeAddItem( p_playlist, p_item, VIEW_CATEGORY, p_sys->p_node,
224                           PLAYLIST_APPEND, PLAYLIST_END );
225
226     vlc_object_release( p_playlist );
227 }
228
229 static void AddCdda( services_discovery_t *p_sd, char *psz_device )
230 {
231     char *psz_name = "Audio CD";
232     char *psz_uri;
233     char *psz_blockdevice;
234     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
235     playlist_t          *p_playlist;
236     playlist_item_t     *p_item;
237 #ifdef HAVE_HAL_1
238     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
239                                             psz_device, "block.device", NULL );
240 #else
241     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
242                                                  psz_device, "block.device" );
243 #endif
244     asprintf( &psz_uri, "cdda://%s", psz_blockdevice );
245     /* Create the playlist item here */
246     p_item = playlist_ItemNew( p_sd, psz_uri,
247                                psz_name );
248     free( psz_uri );
249 #ifdef HAVE_HAL_1
250     libhal_free_string( psz_device );
251 #else
252     hal_free_string( psz_device );
253 #endif
254     if( !p_item )
255     {
256         return;
257     }
258     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
259     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
260                                                 FIND_ANYWHERE );
261     if( !p_playlist )
262     {
263         msg_Err( p_sd, "playlist not found" );
264         return;
265     }
266
267     playlist_NodeAddItem( p_playlist, p_item, VIEW_CATEGORY, p_sys->p_node,
268                           PLAYLIST_APPEND, PLAYLIST_END );
269
270     vlc_object_release( p_playlist );
271
272 }
273
274 static void ParseDevice( services_discovery_t *p_sd, char *psz_device )
275 {
276     char *psz_disc_type;
277     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
278 #ifdef HAVE_HAL_1
279     if( libhal_device_property_exists( p_sys->p_ctx, psz_device,
280                                        "volume.disc.type", NULL ) )
281     {
282         psz_disc_type = libhal_device_get_property_string( p_sys->p_ctx,
283                                                         psz_device,
284                                                         "volume.disc.type",
285                                                         NULL );
286 #else
287     if( hal_device_property_exists( p_sys->p_ctx, psz_device,
288                                     "volume.disc.type" ) )
289     {
290         psz_disc_type = hal_device_get_property_string( p_sys->p_ctx,
291                                                         psz_device,
292                                                         "volume.disc.type" );
293 #endif
294         if( !strcmp( psz_disc_type, "dvd_rom" ) )
295         {
296             AddDvd( p_sd, psz_device );
297         }
298         else if( !strcmp( psz_disc_type, "cd_rom" ) )
299         {
300 #ifdef HAVE_HAL_1
301             if( libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
302                                          "volume.disc.has_audio" , NULL ) )
303 #else
304             if( hal_device_get_property_bool( p_sys->p_ctx, psz_device,
305                                          "volume.disc.has_audio" ) )
306 #endif
307             {
308                 AddCdda( p_sd, psz_device );
309             }
310         }
311 #ifdef HAVE_HAL_1
312         libhal_free_string( psz_disc_type );
313 #else
314         hal_free_string( psz_disc_type );
315 #endif
316     }
317 }
318
319 /*****************************************************************************
320  * Run: main HAL thread
321  *****************************************************************************/
322 static void Run( services_discovery_t *p_sd )
323 {
324     int i, i_devices;
325     char **devices;
326     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
327
328     /* parse existing devices first */
329 #ifdef HAVE_HAL_1
330     if( ( devices = libhal_get_all_devices( p_sys->p_ctx, &i_devices, NULL ) ) )
331 #else
332     if( ( devices = hal_get_all_devices( p_sys->p_ctx, &i_devices ) ) )
333 #endif
334     {
335         for( i = 0; i < i_devices; i++ )
336         {
337             ParseDevice( p_sd, devices[ i ] );
338         }
339     }
340
341     while( !p_sd->b_die )
342     {
343         msleep( 100000 );
344     }
345 }