]> git.sesse.net Git - vlc/blob - modules/services_discovery/hal.c
9ee9e9aa659dfa5f283b28cc971eab622a7c9052
[vlc] / 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
65     set_capability( "services_discovery", 0 );
66     set_callbacks( Open, Close );
67
68 vlc_module_end();
69
70
71 /*****************************************************************************
72  * Local structures
73  *****************************************************************************/
74
75 struct services_discovery_sys_t
76 {
77     LibHalContext *p_ctx;
78
79     /* playlist node */
80     playlist_item_t *p_node;
81
82 };
83
84 /*****************************************************************************
85  * Local prototypes
86  *****************************************************************************/
87
88 /* Main functions */
89     static void Run    ( services_discovery_t *p_intf );
90
91 /*****************************************************************************
92  * Open: initialize and create stuff
93  *****************************************************************************/
94 static int Open( vlc_object_t *p_this )
95 {
96     services_discovery_t *p_sd = ( services_discovery_t* )p_this;
97     services_discovery_sys_t *p_sys  = malloc(
98                                     sizeof( services_discovery_sys_t ) );
99
100     playlist_t          *p_playlist;
101     playlist_view_t     *p_view;
102
103     p_sd->pf_run = Run;
104     p_sd->p_sys  = p_sys;
105
106     if( !( p_sys->p_ctx = hal_initialize( NULL, FALSE ) ) )
107     {
108         free( p_sys );
109         msg_Err( p_sd, "hal not available" );
110         return VLC_EGENERIC;
111     }
112
113     /* Create our playlist node */
114     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
115                                                 FIND_ANYWHERE );
116     if( !p_playlist )
117     {
118         msg_Warn( p_sd, "unable to find playlist, cancelling HAL listening");
119         return VLC_EGENERIC;
120     }
121
122     p_view = playlist_ViewFind( p_playlist, VIEW_CATEGORY );
123     p_sys->p_node = playlist_NodeCreate( p_playlist, VIEW_CATEGORY,
124                                          _("Devices"), p_view->p_root );
125     vlc_object_release( p_playlist );
126
127     return VLC_SUCCESS;
128 }
129
130 /*****************************************************************************
131  * Close:
132  *****************************************************************************/
133 static void Close( vlc_object_t *p_this )
134 {
135     services_discovery_t *p_sd = ( services_discovery_t* )p_sd;
136     services_discovery_sys_t *p_sys  = malloc(
137                                          sizeof( services_discovery_sys_t ) );
138     free( p_sys );
139 }
140
141 static void AddDvd( services_discovery_t *p_sd, char *psz_device )
142 {
143     char *psz_name;
144     char *psz_uri;
145     char *psz_blockdevice;
146     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
147     playlist_t          *p_playlist;
148     playlist_item_t     *p_item;
149     psz_name = hal_device_get_property_string( p_sd->p_sys->p_ctx,
150                                                psz_device, "volume.label" );
151     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
152                                                  psz_device, "block.device" );
153     asprintf( &psz_uri, "dvd://%s", psz_blockdevice );
154     /* Create the playlist item here */
155     p_item = playlist_ItemNew( p_sd, psz_uri,
156                                psz_name );
157     free( psz_uri );
158     hal_free_string( psz_device );
159     if( !p_item )
160     {
161         return;
162     }
163     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
164     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
165                                                 FIND_ANYWHERE );
166     if( !p_playlist )
167     {
168         msg_Err( p_sd, "playlist not found" );
169         return;
170     }
171
172     playlist_NodeAddItem( p_playlist, p_item, VIEW_CATEGORY, p_sys->p_node,
173                           PLAYLIST_APPEND, PLAYLIST_END );
174
175     vlc_object_release( p_playlist );
176 }
177
178 static void AddCdda( services_discovery_t *p_sd, char *psz_device )
179 {
180     char *psz_name = "Audio CD";
181     char *psz_uri;
182     char *psz_blockdevice;
183     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
184     playlist_t          *p_playlist;
185     playlist_item_t     *p_item;
186     psz_blockdevice = hal_device_get_property_string( p_sd->p_sys->p_ctx,
187                                                  psz_device, "block.device" );
188     asprintf( &psz_uri, "cdda://%s", psz_blockdevice );
189     /* Create the playlist item here */
190     p_item = playlist_ItemNew( p_sd, psz_uri,
191                                psz_name );
192     free( psz_uri );
193     hal_free_string( psz_device );
194     if( !p_item )
195     {
196         return;
197     }
198     p_item->i_flags &= ~PLAYLIST_SKIP_FLAG;
199     p_playlist = (playlist_t *)vlc_object_find( p_sd, VLC_OBJECT_PLAYLIST,
200                                                 FIND_ANYWHERE );
201     if( !p_playlist )
202     {
203         msg_Err( p_sd, "playlist not found" );
204         return;
205     }
206
207     playlist_NodeAddItem( p_playlist, p_item, VIEW_CATEGORY, p_sys->p_node,
208                           PLAYLIST_APPEND, PLAYLIST_END );
209
210     vlc_object_release( p_playlist );
211
212 }
213
214 static void ParseDevice( services_discovery_t *p_sd, char *psz_device )
215 {
216     char *psz_disc_type;
217     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
218     if( hal_device_property_exists( p_sys->p_ctx, psz_device,
219                                     "volume.disc.type" ) )
220     {
221         psz_disc_type = hal_device_get_property_string( p_sys->p_ctx,
222                                                         psz_device,
223                                                         "volume.disc.type" );
224         if( !strcmp( psz_disc_type, "dvd_rom" ) )
225         {
226             AddDvd( p_sd, psz_device );
227         }
228         else if( !strcmp( psz_disc_type, "cd_rom" ) )
229         {
230             if( hal_device_get_property_bool( p_sys->p_ctx, psz_device, "volume.disc.has_audio" ) )
231             {
232                 AddCdda( p_sd, psz_device );
233             }
234         }
235         hal_free_string( psz_disc_type );
236     }
237 }
238
239 /*****************************************************************************
240  * Run: main HAL thread
241  *****************************************************************************/
242 static void Run( services_discovery_t *p_sd )
243 {
244     int i, i_devices;
245     char **devices;
246     services_discovery_sys_t    *p_sys  = p_sd->p_sys;
247
248     /* parse existing devices first */
249     if( ( devices = hal_get_all_devices( p_sys->p_ctx, &i_devices ) ) )
250     {
251         for( i = 0; i < i_devices; i++ )
252         {
253             ParseDevice( p_sd, devices[ i ] );
254         }
255     }
256
257     while( !p_sd->b_die )
258     {
259         msleep( 100000 );
260     }
261 }