]> git.sesse.net Git - vlc/blob - modules/misc/probe/hal.c
Cosmetic
[vlc] / modules / misc / probe / hal.c
1 /*****************************************************************************
2  * hal.c :  HAL probing 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 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <vlc_plugin.h>
30 #include <vlc_interface.h>
31 #include <vlc_devices.h>
32
33 #include <hal/libhal.h>
34
35 /*****************************************************************************
36  * Local prototypes
37  *****************************************************************************/
38 struct probe_sys_t
39 {
40     DBusConnection *p_connection;
41     LibHalContext *p_ctx;
42     int            i_devices;
43     device_t     **pp_devices;
44 };
45
46 static int  Open ( vlc_object_t * );
47 static void Close( vlc_object_t * );
48
49 static void Update ( device_probe_t *p_probe );
50 static void UpdateMedia( device_probe_t *p_probe, device_t *p_dev );
51 static void AddDevice( device_probe_t * p_probe, device_t *p_dev );
52 static device_t * ParseDisc( device_probe_t *p_probe,  char *psz_device );
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 vlc_module_begin ()
58     set_description( N_("HAL devices detection") )
59     set_capability( "devices probe", 0 )
60     set_callbacks( Open, Close )
61 vlc_module_end ()
62
63
64 /*****************************************************************************
65  * Open: initialize and create stuff
66  *****************************************************************************/
67 static int Open( vlc_object_t *p_this )
68 {
69     device_probe_t *p_probe = (device_probe_t *)p_this;
70     DBusError       dbus_error;
71     DBusConnection *p_connection;
72     probe_sys_t    *p_sys;
73
74     p_probe->p_sys = p_sys = malloc( sizeof( probe_sys_t ) );
75     p_probe->p_sys->i_devices = 0;
76     p_probe->p_sys->pp_devices = NULL;
77
78     p_probe->pf_run = Update;
79
80     dbus_error_init( &dbus_error );
81
82     p_sys->p_ctx = libhal_ctx_new();
83     if( !p_sys->p_ctx )
84     {
85         msg_Err( p_probe, "unable to create HAL context") ;
86         free( p_sys );
87         return VLC_EGENERIC;
88     }
89     p_connection = dbus_bus_get( DBUS_BUS_SYSTEM, &dbus_error );
90     if( dbus_error_is_set( &dbus_error ) )
91     {
92         msg_Err( p_probe, "unable to connect to DBUS: %s", dbus_error.message );
93         goto error;
94     }
95     p_sys->p_connection = p_connection;
96     libhal_ctx_set_dbus_connection( p_sys->p_ctx, p_connection );
97     if( !libhal_ctx_init( p_sys->p_ctx, &dbus_error ) )
98     {
99         msg_Err( p_probe, "hal not available : %s", dbus_error.message );
100         dbus_connection_unref( p_connection );
101         goto error;
102     }
103     return VLC_SUCCESS;
104 error:
105     dbus_error_free( &dbus_error );
106     libhal_ctx_free( p_sys->p_ctx );
107     free( p_sys );
108     return VLC_EGENERIC;
109 }
110
111 /*****************************************************************************
112  * Close:
113  *****************************************************************************/
114 static void Close( vlc_object_t *p_this )
115 {
116     device_probe_t *p_probe = (device_probe_t *) p_this;
117     probe_sys_t *p_sys = p_probe->p_sys;
118     dbus_connection_unref( p_sys->p_connection );
119     libhal_ctx_free( p_sys->p_ctx );
120     free( p_sys );
121 }
122 #if 0
123 static int GetAllDevices( device_probe_t *p_probe, device_t ***ppp_devices )
124 {
125     /// \todo : fill the dst array
126     return p_probe->p_sys->i_devices;
127 }
128 #endif
129 static void Update( device_probe_t * p_probe )
130 {
131     probe_sys_t *p_sys = p_probe->p_sys;
132     int i, i_devices, j;
133     char **devices;
134     bool b_exists;
135     int canc = vlc_savecancel();
136
137     for ( j = 0 ; j < p_sys->i_devices; j++ )
138         p_sys->pp_devices[j]->b_seen = false;
139
140     /* CD/DVD */
141     if( ( devices = libhal_find_device_by_capability( p_sys->p_ctx,
142                               "storage.cdrom",
143                               &i_devices, NULL ) ) )
144     {
145         for( i = 0; i < i_devices; i++ )
146         {
147             device_t *p_dev = ParseDisc( p_probe, devices[ i ] );
148             b_exists = false;
149
150             for ( j = 0 ; j < p_sys->i_devices; j++ )
151             {
152                 if( !strcmp( p_sys->pp_devices[j]->psz_uri,
153                              p_dev->psz_uri ) )
154                 {
155                     b_exists = true;
156                     p_dev->b_seen = true;
157                     UpdateMedia( p_probe, p_dev );
158                     break;
159                 }
160                 if( !b_exists )
161                     AddDevice( p_probe, p_dev );
162             }
163         }
164     }
165     /// \todo Remove unseen devices
166     vlc_restorecancel( canc );
167 }
168
169
170 static void AddDevice( device_probe_t * p_probe, device_t *p_dev )
171 {
172     INSERT_ELEM( p_probe->p_sys->pp_devices,
173                  p_probe->p_sys->i_devices,
174                  p_probe->p_sys->i_devices,
175                  p_dev );
176     /// \todo : emit variable
177 }
178
179 static device_t * ParseDisc( device_probe_t *p_probe,  char *psz_device )
180 {
181     probe_sys_t *p_sys = p_probe->p_sys;
182     device_t *p_dev;
183     char *block_dev;
184     dbus_bool_t b_dvd;
185
186     if( !libhal_device_property_exists( p_sys->p_ctx, psz_device,
187                                         "storage.cdrom.dvd", NULL ) )
188         return NULL;
189
190     p_dev = (device_t *)malloc( sizeof( device_t ) );
191     p_dev->i_media_type = p_dev->i_capabilities = 0;
192     p_dev->psz_name = p_dev->psz_uri = NULL;
193
194     block_dev =  libhal_device_get_property_string( p_sys->p_ctx, psz_device,
195                                                    "block.device" , NULL );
196     if( block_dev )
197     {
198         p_dev->psz_uri = strdup( block_dev );
199         libhal_free_string( block_dev );
200     }
201
202     b_dvd = libhal_device_get_property_bool( p_sys->p_ctx, psz_device,
203                                             "storage.cdrom.dvd", NULL  );
204     if( b_dvd )
205         p_dev->i_capabilities = DEVICE_CAN_DVD | DEVICE_CAN_CD;
206     else
207         p_dev->i_capabilities = DEVICE_CAN_CD;
208
209     UpdateMedia( p_probe, p_dev );
210     return p_dev;
211 }
212
213 static void UpdateMedia( device_probe_t *p_probe, device_t *p_dev )
214 {
215     probe_sys_t *p_sys = p_probe->p_sys;
216     char **matching_media;
217     int i_matching, i;
218     bool b_changed = false;;
219     int i_old_type = p_dev->i_media_type;
220     p_dev->i_media_type = 0;
221
222     /* Find the media in the drive */
223     matching_media = libhal_manager_find_device_string_match( p_sys->p_ctx,
224                                             "block.device", p_dev->psz_uri,
225                                             &i_matching, NULL );
226     for( i = 0; i < i_matching; i++ )
227     {
228         if( libhal_device_property_exists( p_sys->p_ctx, matching_media[i],
229                                            "volume.disc.type", NULL ) )
230         {
231             char *psz_media_name = libhal_device_get_property_string(
232                                             p_sys->p_ctx,
233                                             matching_media[i],
234                                             "volume.label", NULL );
235             if( psz_media_name )
236             {
237                 if( p_dev->psz_name && strcmp( p_dev->psz_name, psz_media_name))
238                 {
239                     free( p_dev->psz_name );
240                     p_dev->psz_name = NULL;
241                     b_changed = true;
242                 }
243                 if( !p_dev->psz_name )
244                     p_dev->psz_name = strdup( psz_media_name );
245                 libhal_free_string( psz_media_name );
246             }
247
248             if( libhal_device_get_property_bool( p_sys->p_ctx,
249                                              matching_media[i],
250                                              "volume.disc.is_videodvd", NULL) )
251                 p_dev->i_media_type = MEDIA_TYPE_DVD;
252             else if( libhal_device_get_property_bool( p_sys->p_ctx,
253                                              matching_media[i],
254                                              "volume.disc.is_vcd", NULL) ||
255                      libhal_device_get_property_bool( p_sys->p_ctx,
256                                              matching_media[i],
257                                              "volume.disc.is_svcd", NULL) )
258                p_dev->i_media_type = MEDIA_TYPE_VCD;
259             else if( libhal_device_get_property_bool( p_sys->p_ctx,
260                                              matching_media[i],
261                                              "volume.disc.has_audio", NULL) )
262                p_dev->i_media_type = MEDIA_TYPE_CDDA;
263
264             break;
265         }
266     }
267     if( b_changed || p_dev->i_media_type != i_old_type )
268     {
269         /// \todo emit changed signal
270     }
271 }