]> git.sesse.net Git - vlc/blob - modules/services_discovery/hal.c
Rename playlist_NodesCreateForSD to playlist_NodesPairCreate and document it
[vlc] / modules / services_discovery / hal.c
1 /*****************************************************************************
2  * hal.c :  HAL 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 #include <vlc/vlc.h>
25 #include <vlc/intf.h>
26
27 #include <vlc/input.h>
28
29 #include "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 struct services_discovery_sys_t
48 {
49     LibHalContext *p_ctx;
50     playlist_item_t *p_node_cat;
51     playlist_item_t *p_node_one;
52 };
53 static void AddItem( services_discovery_t *p_sd, input_item_t * p_input );
54 static void Run    ( services_discovery_t *p_intf );
55
56 static int  Open ( vlc_object_t * );
57 static void Close( vlc_object_t * );
58
59 /*****************************************************************************
60  * Module descriptor
61  *****************************************************************************/
62 vlc_module_begin();
63     set_description( _("HAL devices 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  * Open: initialize and create stuff
75  *****************************************************************************/
76 static int Open( vlc_object_t *p_this )
77 {
78     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
79     services_discovery_sys_t *p_sys  = malloc(
80                                     sizeof( services_discovery_sys_t ) );
81
82     vlc_value_t         val;
83     playlist_t          *p_playlist;
84
85     DBusError           dbus_error;
86     DBusConnection      *p_connection;
87
88     p_sd->pf_run = Run;
89     p_sd->p_sys  = p_sys;
90
91     dbus_error_init( &dbus_error );
92
93 #ifdef HAVE_HAL_1
94     p_sys->p_ctx = libhal_ctx_new();
95     if( !p_sys->p_ctx )
96     {
97         msg_Err( p_sd, "unable to create HAL context") ;
98         free( p_sys );
99         return VLC_EGENERIC;
100     }
101     p_connection = dbus_bus_get( DBUS_BUS_SYSTEM, &dbus_error );
102     if( dbus_error_is_set( &dbus_error ) )
103     {
104         msg_Err( p_sd, "unable to connect to DBUS: %s", dbus_error.message );
105         dbus_error_free( &dbus_error );
106         free( p_sys );
107         return VLC_EGENERIC;
108     }
109     libhal_ctx_set_dbus_connection( p_sys->p_ctx, p_connection );
110     if( !libhal_ctx_init( p_sys->p_ctx, &dbus_error ) )
111 #else
112     if( !(p_sys->p_ctx = hal_initialize( NULL, FALSE ) ) )
113 #endif
114     {
115         msg_Err( p_sd, "hal not available : %s", dbus_error.message );
116         dbus_error_free( &dbus_error );
117         free( p_sys );
118         return VLC_EGENERIC;
119     }
120
121     /* Create our playlist node */
122     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
123                                                 FIND_ANYWHERE );
124     if( !p_playlist )
125     {
126         msg_Warn( p_sd, "unable to find playlist, cancelling HAL listening");
127         return VLC_EGENERIC;
128     }
129
130     playlist_NodesPairCreate( p_playlist, _("Devices"),
131                               &p_sys->p_node_cat, &p_sys->p_node_one,
132                               VLC_TRUE );
133     vlc_object_release( p_playlist );
134
135     return VLC_SUCCESS;
136 }
137
138 /*****************************************************************************
139  * Close:
140  *****************************************************************************/
141 static void Close( vlc_object_t *p_this )
142 {
143     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
144     services_discovery_sys_t *p_sys  = p_sd->p_sys;
145     playlist_t *p_playlist =  (playlist_t *) vlc_object_find( p_sd,
146                                  VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
147     if( p_playlist )
148     {
149         playlist_NodeDelete( p_playlist, p_sys->p_node_cat, VLC_TRUE,VLC_TRUE );
150         playlist_NodeDelete( p_playlist, p_sys->p_node_one, VLC_TRUE,VLC_TRUE );
151         vlc_object_release( p_playlist );
152     }
153     free( p_sys );
154 }
155
156 static void AddDvd( services_discovery_t *p_sd, char *psz_device )
157 {
158     char *psz_name;
159     char *psz_uri;
160     char *psz_blockdevice;
161     input_item_t        *p_input;
162 #ifdef HAVE_HAL_1
163     psz_name = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
164                                         psz_device, "volume.label", NULL );
165     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
166                                         psz_device, "block.device", NULL );
167 #else
168     psz_name = hal_device_get_property_string( p_sd->p_sys->p_ctx,
169                                                psz_device, "volume.label" );
170     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
171                                                  psz_device, "block.device" );
172 #endif
173     asprintf( &psz_uri, "dvd://%s", psz_blockdevice );
174     /* Create the playlist item here */
175     p_input = input_ItemNew( p_sd, psz_uri, psz_name );
176     free( psz_uri );
177 #ifdef HAVE_HAL_1
178     libhal_free_string( psz_device );
179 #else
180     hal_free_string( psz_device );
181 #endif
182     if( !p_input )
183     {
184         return;
185     }
186     AddItem( p_sd, p_input );
187 }
188
189 static void AddItem( services_discovery_t *p_sd, input_item_t * p_input )
190 {
191     playlist_item_t *p_item;
192     playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_sd,
193                                         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
194     if( !p_playlist )
195     {
196         msg_Err( p_sd, "playlist not found" );
197         return;
198     }
199     p_item = playlist_NodeAddInput( p_playlist, p_input,p_sd->p_sys->p_node_cat,
200                                     PLAYLIST_APPEND, PLAYLIST_END );
201     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
202     p_item = playlist_NodeAddInput( p_playlist, p_input,p_sd->p_sys->p_node_one,
203                                     PLAYLIST_APPEND, PLAYLIST_END );
204     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
205
206     vlc_object_release( p_playlist );
207 }
208
209 static void AddCdda( services_discovery_t *p_sd, char *psz_device )
210 {
211     char *psz_name = "Audio CD";
212     char *psz_uri;
213     char *psz_blockdevice;
214     input_item_t     *p_input;
215 #ifdef HAVE_HAL_1
216     psz_blockdevice = libhal_device_get_property_string( p_sd->p_sys->p_ctx,
217                                             psz_device, "block.device", NULL );
218 #else
219     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
220                                                  psz_device, "block.device" );
221 #endif
222     asprintf( &psz_uri, "cdda://%s", psz_blockdevice );
223     /* Create the playlist item here */
224     p_input = input_ItemNew( p_sd, psz_uri, psz_name );
225     free( psz_uri );
226 #ifdef HAVE_HAL_1
227     libhal_free_string( psz_device );
228 #else
229     hal_free_string( psz_device );
230 #endif
231     if( !p_input )
232         return;
233     AddItem( p_sd, p_input );
234 }
235
236 static void ParseDevice( services_discovery_t *p_sd, char *psz_device )
237 {
238     char *psz_disc_type;
239     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
240 #ifdef HAVE_HAL_1
241     if( libhal_device_property_exists( p_sys->p_ctx, psz_device,
242                                        "volume.disc.type", NULL ) )
243     {
244         psz_disc_type = libhal_device_get_property_string( p_sys->p_ctx,
245                                                         psz_device,
246                                                         "volume.disc.type",
247                                                         NULL );
248 #else
249     if( hal_device_property_exists( p_sys->p_ctx, psz_device,
250                                     "volume.disc.type" ) )
251     {
252         psz_disc_type = hal_device_get_property_string( p_sys->p_ctx,
253                                                         psz_device,
254                                                         "volume.disc.type" );
255 #endif
256         if( !strcmp( psz_disc_type, "dvd_rom" ) )
257         {
258             AddDvd( p_sd, psz_device );
259         }
260         else if( !strcmp( psz_disc_type, "cd_rom" ) )
261         {
262 #ifdef HAVE_HAL_1
263             if( libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
264                                          "volume.disc.has_audio" , NULL ) )
265 #else
266             if( hal_device_get_property_bool( p_sys->p_ctx, psz_device,
267                                          "volume.disc.has_audio" ) )
268 #endif
269             {
270                 AddCdda( p_sd, psz_device );
271             }
272         }
273 #ifdef HAVE_HAL_1
274         libhal_free_string( psz_disc_type );
275 #else
276         hal_free_string( psz_disc_type );
277 #endif
278     }
279 }
280
281 /*****************************************************************************
282  * Run: main HAL thread
283  *****************************************************************************/
284 static void Run( services_discovery_t *p_sd )
285 {
286     int i, i_devices;
287     char **devices;
288     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
289
290     /* parse existing devices first */
291 #ifdef HAVE_HAL_1
292     if( ( devices = libhal_get_all_devices( p_sys->p_ctx, &i_devices, NULL ) ) )
293 #else
294     if( ( devices = hal_get_all_devices( p_sys->p_ctx, &i_devices ) ) )
295 #endif
296     {
297         for( i = 0; i < i_devices; i++ )
298         {
299             ParseDevice( p_sd, devices[ i ] );
300         }
301     }
302
303     while( !p_sd->b_die )
304     {
305         msleep( 100000 );
306     }
307 }