]> git.sesse.net Git - vlc/blob - modules/services_discovery/hal.c
Support new HAL API. Based on patch by Josselin Mouette
[vlc] / modules / services_discovery / hal.c
1 /*****************************************************************************
2  * sap.c :  SAP interface module
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id: sap.c 9217 2004-11-07 11:02:59Z courmisch $
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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
108     p_sd->pf_run = Run;
109     p_sd->p_sys  = p_sys;
110
111     dbus_error_init( &dbus_error );
112
113 #ifdef HAVE_HAL_1
114     if( !( p_sys->p_ctx = libhal_ctx_init_direct( &dbus_error ) ) )
115 #else
116     if( !( p_sys->p_ctx = hal_initialize( NULL, FALSE ) ) )
117 #endif
118     {
119         free( p_sys );
120         msg_Err( p_sd, "hal not available : %s", dbus_error.message );
121         return VLC_EGENERIC;
122     }
123
124     /* Create our playlist node */
125     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
126                                                 FIND_ANYWHERE );
127     if( !p_playlist )
128     {
129         msg_Warn( p_sd, "unable to find playlist, cancelling HAL listening");
130         return VLC_EGENERIC;
131     }
132
133     p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
134     p_sys->p_node = playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
135                                          _("Devices"), p_view->p_root );
136
137     p_sys->p_node->i_flags |= PLAYLIST_RO_FLAG;
138     val.b_bool = VLC_TRUE;
139     var_Set( p_playlist, "intf-change", val );
140
141     vlc_object_release( p_playlist );
142
143     return VLC_SUCCESS;
144 }
145
146 /*****************************************************************************
147  * Close:
148  *****************************************************************************/
149 static void Close( vlc_object_t *p_this )
150 {
151     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
152     services_discovery_sys_t *p_sys  = p_sd->p_sys;
153     playlist_t *p_playlist =  (playlist_t *) vlc_object_find( p_sd,
154                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
155     if( p_playlist )
156     {
157         playlist_NodeDelete( p_playlist, p_sys->p_node, VLC_TRUE, VLC_TRUE );
158         vlc_object_release( p_playlist );
159     }
160     free( p_sys );
161 }
162
163 static void AddDvd( services_discovery_t *p_sd, char *psz_device )
164 {
165     char *psz_name;
166     char *psz_uri;
167     char *psz_blockdevice;
168     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
169     playlist_t          *p_playlist;
170     playlist_item_t     *p_item;
171 #ifdef HAVE_HAL_1
172     psz_name = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
173                                         psz_device, "volume.label", NULL );
174     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
175                                         psz_device, "block.device", NULL );
176 #else
177     psz_name = hal_device_get_property_string( p_sd->p_sys->p_ctx,
178                                                psz_device, "volume.label" );
179     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
180                                                  psz_device, "block.device" );
181 #endif
182     asprintf( &psz_uri, "dvd://%s", psz_blockdevice );
183     /* Create the playlist item here */
184     p_item = playlist_ItemNew( p_sd, psz_uri,
185                                psz_name );
186     free( psz_uri );
187 #ifdef HAVE_HAL_1
188     libhal_free_string( psz_device );
189 #else
190     hal_free_string( psz_device );
191 #endif
192     if( !p_item )
193     {
194         return;
195     }
196     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
197     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
198                                                 FIND_ANYWHERE );
199     if( !p_playlist )
200     {
201         msg_Err( p_sd, "playlist not found" );
202         return;
203     }
204
205     playlist_NodeAddItem( p_playlist, p_item, VIEW_CATEGORY, p_sys->p_node,
206                           PLAYLIST_APPEND, PLAYLIST_END );
207
208     vlc_object_release( p_playlist );
209 }
210
211 static void AddCdda( services_discovery_t *p_sd, char *psz_device )
212 {
213     char *psz_name = "Audio CD";
214     char *psz_uri;
215     char *psz_blockdevice;
216     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
217     playlist_t          *p_playlist;
218     playlist_item_t     *p_item;
219 #ifdef HAVE_HAL_1
220     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
221                                             psz_device, "block.device", NULL );
222 #else
223     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
224                                                  psz_device, "block.device" );
225 #endif
226     asprintf( &psz_uri, "cdda://%s", psz_blockdevice );
227     /* Create the playlist item here */
228     p_item = playlist_ItemNew( p_sd, psz_uri,
229                                psz_name );
230     free( psz_uri );
231 #ifdef HAVE_HAL_1
232     libhal_free_string( psz_device );
233 #else
234     hal_free_string( psz_device );
235 #endif
236     if( !p_item )
237     {
238         return;
239     }
240     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
241     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
242                                                 FIND_ANYWHERE );
243     if( !p_playlist )
244     {
245         msg_Err( p_sd, "playlist not found" );
246         return;
247     }
248
249     playlist_NodeAddItem( p_playlist, p_item, VIEW_CATEGORY, p_sys->p_node,
250                           PLAYLIST_APPEND, PLAYLIST_END );
251
252     vlc_object_release( p_playlist );
253
254 }
255
256 static void ParseDevice( services_discovery_t *p_sd, char *psz_device )
257 {
258     char *psz_disc_type;
259     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
260 #ifdef HAVE_HAL_1
261     if( libhal_device_property_exists( p_sys->p_ctx, psz_device,
262                                        "volume.disc.type", NULL ) )
263     {
264         psz_disc_type = libhal_device_get_property_string( p_sys->p_ctx,
265                                                         psz_device,
266                                                         "volume.disc.type",
267                                                         NULL );
268 #else
269     if( hal_device_property_exists( p_sys->p_ctx, psz_device,
270                                     "volume.disc.type" ) )
271     {
272         psz_disc_type = hal_device_get_property_string( p_sys->p_ctx,
273                                                         psz_device,
274                                                         "volume.disc.type" );
275 #endif
276         if( !strcmp( psz_disc_type, "dvd_rom" ) )
277         {
278             AddDvd( p_sd, psz_device );
279         }
280         else if( !strcmp( psz_disc_type, "cd_rom" ) )
281         {
282 #ifdef HAVE_HAL_1
283             if( libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
284                                          "volume.disc.has_audio" , NULL ) )
285 #else
286             if( hal_device_get_property_bool( p_sys->p_ctx, psz_device,
287                                          "volume.disc.has_audio" ) )
288 #endif
289             {
290                 AddCdda( p_sd, psz_device );
291             }
292         }
293 #ifdef HAVE_HAL_1
294         libhal_free_string( psz_disc_type );
295 #else
296         hal_free_string( psz_disc_type );
297 #endif
298     }
299 }
300
301 /*****************************************************************************
302  * Run: main HAL thread
303  *****************************************************************************/
304 static void Run( services_discovery_t *p_sd )
305 {
306     int i, i_devices;
307     char **devices;
308     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
309
310     /* parse existing devices first */
311 #ifdef HAVE_HAL_1
312     if( ( devices = libhal_get_all_devices( p_sys->p_ctx, &i_devices, NULL ) ) )
313 #else
314     if( ( devices = hal_get_all_devices( p_sys->p_ctx, &i_devices ) ) )
315 #endif
316     {
317         for( i = 0; i < i_devices; i++ )
318         {
319             ParseDevice( p_sd, devices[ i ] );
320         }
321     }
322
323     while( !p_sd->b_die )
324     {
325         msleep( 100000 );
326     }
327 }