crypt32: Make sure we show Unicode characters (Dutch translation).
[wine] / dlls / mountmgr.sys / hal.c
1 /*
2  * HAL devices support
3  *
4  * Copyright 2006 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <errno.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <sys/time.h>
29
30 #include "mountmgr.h"
31 #include "excpt.h"
32
33 #include "wine/library.h"
34 #include "wine/exception.h"
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr);
38
39 #ifdef SONAME_LIBHAL
40
41 #include <dbus/dbus.h>
42 #include <hal/libhal.h>
43
44 #define DBUS_FUNCS \
45     DO_FUNC(dbus_bus_get); \
46     DO_FUNC(dbus_connection_close); \
47     DO_FUNC(dbus_connection_read_write_dispatch); \
48     DO_FUNC(dbus_error_init); \
49     DO_FUNC(dbus_error_free); \
50     DO_FUNC(dbus_error_is_set)
51
52 #define HAL_FUNCS \
53     DO_FUNC(libhal_ctx_free); \
54     DO_FUNC(libhal_ctx_init); \
55     DO_FUNC(libhal_ctx_new); \
56     DO_FUNC(libhal_ctx_set_dbus_connection); \
57     DO_FUNC(libhal_ctx_set_device_added); \
58     DO_FUNC(libhal_ctx_set_device_property_modified); \
59     DO_FUNC(libhal_ctx_set_device_removed); \
60     DO_FUNC(libhal_ctx_shutdown); \
61     DO_FUNC(libhal_device_get_property_bool); \
62     DO_FUNC(libhal_device_get_property_string); \
63     DO_FUNC(libhal_device_add_property_watch); \
64     DO_FUNC(libhal_device_remove_property_watch); \
65     DO_FUNC(libhal_free_string); \
66     DO_FUNC(libhal_free_string_array); \
67     DO_FUNC(libhal_get_all_devices)
68
69 #define DO_FUNC(f) static typeof(f) * p_##f
70 DBUS_FUNCS;
71 HAL_FUNCS;
72 #undef DO_FUNC
73
74 static BOOL load_functions(void)
75 {
76     void *hal_handle;
77     char error[128];
78
79     /* Load libhal with RTLD_GLOBAL so that the dbus symbols are available.
80      * We can't load libdbus directly since libhal may have been built against a
81      * different version but with the same soname. Binary compatibility is for wimps. */
82
83     if (!(hal_handle = wine_dlopen(SONAME_LIBHAL, RTLD_NOW|RTLD_GLOBAL, error, sizeof(error))))
84         goto failed;
85
86 #define DO_FUNC(f) if (!(p_##f = wine_dlsym( RTLD_DEFAULT, #f, error, sizeof(error) ))) goto failed
87     DBUS_FUNCS;
88 #undef DO_FUNC
89
90 #define DO_FUNC(f) if (!(p_##f = wine_dlsym( hal_handle, #f, error, sizeof(error) ))) goto failed
91     HAL_FUNCS;
92 #undef DO_FUNC
93
94     return TRUE;
95
96 failed:
97     WARN( "failed to load HAL support: %s\n", error );
98     return FALSE;
99 }
100
101 static LONG WINAPI assert_fault(EXCEPTION_POINTERS *eptr)
102 {
103     if (eptr->ExceptionRecord->ExceptionCode == EXCEPTION_WINE_ASSERTION)
104         return EXCEPTION_EXECUTE_HANDLER;
105     return EXCEPTION_CONTINUE_SEARCH;
106 }
107
108 /* HAL callback for new device */
109 static void new_device( LibHalContext *ctx, const char *udi )
110 {
111     DBusError error;
112     char *parent = NULL;
113     char *mount_point = NULL;
114     char *device = NULL;
115     char *type = NULL;
116     enum device_type drive_type;
117
118     p_dbus_error_init( &error );
119
120     if (!(device = p_libhal_device_get_property_string( ctx, udi, "block.device", &error )))
121         goto done;
122
123     if (!(mount_point = p_libhal_device_get_property_string( ctx, udi, "volume.mount_point", &error )))
124         goto done;
125
126     if (!(parent = p_libhal_device_get_property_string( ctx, udi, "info.parent", &error )))
127         goto done;
128
129     if (!p_libhal_device_get_property_bool( ctx, parent, "storage.removable", &error ))
130         goto done;
131
132     if (!(type = p_libhal_device_get_property_string( ctx, parent, "storage.drive_type", &error )))
133         p_dbus_error_free( &error );  /* ignore error */
134
135     if (type && !strcmp( type, "cdrom" )) drive_type = DEVICE_CDROM;
136     else if (type && !strcmp( type, "floppy" )) drive_type = DEVICE_FLOPPY;
137     else drive_type = DEVICE_UNKNOWN;
138
139     add_dos_device( -1, udi, device, mount_point, drive_type );
140
141     /* add property watch for mount point */
142     p_libhal_device_add_property_watch( ctx, udi, &error );
143
144 done:
145     if (type) p_libhal_free_string( type );
146     if (parent) p_libhal_free_string( parent );
147     if (device) p_libhal_free_string( device );
148     if (mount_point) p_libhal_free_string( mount_point );
149     p_dbus_error_free( &error );
150 }
151
152 /* HAL callback for removed device */
153 static void removed_device( LibHalContext *ctx, const char *udi )
154 {
155     DBusError error;
156
157     TRACE( "removed %s\n", wine_dbgstr_a(udi) );
158
159     if (!remove_dos_device( -1, udi ))
160     {
161         p_dbus_error_init( &error );
162         p_libhal_device_remove_property_watch( ctx, udi, &error );
163         p_dbus_error_free( &error );
164     }
165 }
166
167 /* HAL callback for property changes */
168 static void property_modified (LibHalContext *ctx, const char *udi,
169                                const char *key, dbus_bool_t is_removed, dbus_bool_t is_added)
170 {
171     TRACE( "udi %s key %s %s\n", wine_dbgstr_a(udi), wine_dbgstr_a(key),
172                 is_added ? "added" : is_removed ? "removed" : "modified" );
173
174     if (!strcmp( key, "volume.mount_point" )) new_device( ctx, udi );
175 }
176
177
178 static DWORD WINAPI hal_thread( void *arg )
179 {
180     DBusError error;
181     DBusConnection *dbc;
182     LibHalContext *ctx;
183     int i, num;
184     char **list;
185
186     if (!(ctx = p_libhal_ctx_new())) return 1;
187
188     p_dbus_error_init( &error );
189     if (!(dbc = p_dbus_bus_get( DBUS_BUS_SYSTEM, &error )))
190     {
191         WARN( "failed to get system dbus connection: %s\n", error.message );
192         p_dbus_error_free( &error );
193         return 1;
194     }
195
196     p_libhal_ctx_set_dbus_connection( ctx, dbc );
197     p_libhal_ctx_set_device_added( ctx, new_device );
198     p_libhal_ctx_set_device_removed( ctx, removed_device );
199     p_libhal_ctx_set_device_property_modified( ctx, property_modified );
200
201     if (!p_libhal_ctx_init( ctx, &error ))
202     {
203         WARN( "HAL context init failed: %s\n", error.message );
204         p_dbus_error_free( &error );
205         return 1;
206     }
207
208     /* retrieve all existing devices */
209     if (!(list = p_libhal_get_all_devices( ctx, &num, &error ))) p_dbus_error_free( &error );
210     else
211     {
212         for (i = 0; i < num; i++) new_device( ctx, list[i] );
213         p_libhal_free_string_array( list );
214     }
215
216     __TRY
217     {
218         while (p_dbus_connection_read_write_dispatch( dbc, -1 )) /* nothing */ ;
219     }
220     __EXCEPT( assert_fault )
221     {
222         WARN( "dbus assertion failure, disabling HAL support\n" );
223         return 1;
224     }
225     __ENDTRY;
226
227     p_libhal_ctx_shutdown( ctx, &error );
228     p_dbus_error_free( &error );  /* just in case */
229     p_dbus_connection_close( dbc );
230     p_libhal_ctx_free( ctx );
231     return 0;
232 }
233
234 void initialize_hal(void)
235 {
236     HANDLE handle;
237
238     if (!load_functions()) return;
239     if (!(handle = CreateThread( NULL, 0, hal_thread, NULL, 0, NULL ))) return;
240     CloseHandle( handle );
241 }
242
243 #else  /* SONAME_LIBHAL */
244
245 void initialize_hal(void)
246 {
247     TRACE( "Skipping, HAL support not compiled in\n" );
248 }
249
250 #endif  /* SONAME_LIBHAL */