mkv.cpp: I was wrong, the dirent export works
[vlc.git] / modules / services_discovery / hal.c
1 /*****************************************************************************
2  * sap.c :  SAP interface module
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
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     p_sd->pf_run = Run;
107     p_sd->p_sys  = p_sys;
108
109     if( !( p_sys->p_ctx = hal_initialize( NULL, FALSE ) ) )
110     {
111         free( p_sys );
112         msg_Err( p_sd, "hal not available" );
113         return VLC_EGENERIC;
114     }
115
116     /* Create our playlist node */
117     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
118                                                 FIND_ANYWHERE );
119     if( !p_playlist )
120     {
121         msg_Warn( p_sd, "unable to find playlist, cancelling HAL listening");
122         return VLC_EGENERIC;
123     }
124
125     p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
126     p_sys->p_node = playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
127                                          _("Devices"), p_view->p_root );
128
129     p_sys->p_node->i_flags |= PLAYLIST_RO_FLAG;
130     val.b_bool = VLC_TRUE;
131     var_Set( p_playlist, "intf-change", val );
132
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, VLC_TRUE, VLC_TRUE );
150         vlc_object_release( p_playlist );
151     }
152     free( p_sys );
153 }
154
155 static void AddDvd( services_discovery_t *p_sd, char *psz_device )
156 {
157     char *psz_name;
158     char *psz_uri;
159     char *psz_blockdevice;
160     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
161     playlist_t          *p_playlist;
162     playlist_item_t     *p_item;
163     psz_name = hal_device_get_property_string( p_sd->p_sys->p_ctx,
164                                                psz_device, "volume.label" );
165     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
166                                                  psz_device, "block.device" );
167     asprintf( &psz_uri, "dvd://%s", psz_blockdevice );
168     /* Create the playlist item here */
169     p_item = playlist_ItemNew( p_sd, psz_uri,
170                                psz_name );
171     free( psz_uri );
172     hal_free_string( psz_device );
173     if( !p_item )
174     {
175         return;
176     }
177     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
178     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
179                                                 FIND_ANYWHERE );
180     if( !p_playlist )
181     {
182         msg_Err( p_sd, "playlist not found" );
183         return;
184     }
185
186     playlist_NodeAddItem( p_playlist, p_item, VIEW_CATEGORY, p_sys->p_node,
187                           PLAYLIST_APPEND, PLAYLIST_END );
188
189     vlc_object_release( p_playlist );
190 }
191
192 static void AddCdda( services_discovery_t *p_sd, char *psz_device )
193 {
194     char *psz_name = "Audio CD";
195     char *psz_uri;
196     char *psz_blockdevice;
197     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
198     playlist_t          *p_playlist;
199     playlist_item_t     *p_item;
200     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
201                                                  psz_device, "block.device" );
202     asprintf( &psz_uri, "cdda://%s", psz_blockdevice );
203     /* Create the playlist item here */
204     p_item = playlist_ItemNew( p_sd, psz_uri,
205                                psz_name );
206     free( psz_uri );
207     hal_free_string( psz_device );
208     if( !p_item )
209     {
210         return;
211     }
212     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
213     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
214                                                 FIND_ANYWHERE );
215     if( !p_playlist )
216     {
217         msg_Err( p_sd, "playlist not found" );
218         return;
219     }
220
221     playlist_NodeAddItem( p_playlist, p_item, VIEW_CATEGORY, p_sys->p_node,
222                           PLAYLIST_APPEND, PLAYLIST_END );
223
224     vlc_object_release( p_playlist );
225
226 }
227
228 static void ParseDevice( services_discovery_t *p_sd, char *psz_device )
229 {
230     char *psz_disc_type;
231     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
232     if( hal_device_property_exists( p_sys->p_ctx, psz_device,
233                                     "volume.disc.type" ) )
234     {
235         psz_disc_type = hal_device_get_property_string( p_sys->p_ctx,
236                                                         psz_device,
237                                                         "volume.disc.type" );
238         if( !strcmp( psz_disc_type, "dvd_rom" ) )
239         {
240             AddDvd( p_sd, psz_device );
241         }
242         else if( !strcmp( psz_disc_type, "cd_rom" ) )
243         {
244             if( hal_device_get_property_bool( p_sys->p_ctx, psz_device, "volume.disc.has_audio" ) )
245             {
246                 AddCdda( p_sd, psz_device );
247             }
248         }
249         hal_free_string( psz_disc_type );
250     }
251 }
252
253 /*****************************************************************************
254  * Run: main HAL thread
255  *****************************************************************************/
256 static void Run( services_discovery_t *p_sd )
257 {
258     int i, i_devices;
259     char **devices;
260     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
261
262     /* parse existing devices first */
263     if( ( devices = hal_get_all_devices( p_sys->p_ctx, &i_devices ) ) )
264     {
265         for( i = 0; i < i_devices; i++ )
266         {
267             ParseDevice( p_sd, devices[ i ] );
268         }
269     }
270
271     while( !p_sd->b_die )
272     {
273         msleep( 100000 );
274     }
275 }