winecoreaudio: Make wodMessage signature match WINEMM_msgFunc32 typedef.
[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 "winnls.h"
32 #include "excpt.h"
33
34 #include "wine/library.h"
35 #include "wine/exception.h"
36 #include "wine/debug.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr);
39
40 #ifdef SONAME_LIBHAL
41
42 #include <dbus/dbus.h>
43 #include <hal/libhal.h>
44
45 #define DBUS_FUNCS \
46     DO_FUNC(dbus_bus_get); \
47     DO_FUNC(dbus_connection_close); \
48     DO_FUNC(dbus_connection_read_write_dispatch); \
49     DO_FUNC(dbus_error_init); \
50     DO_FUNC(dbus_error_free); \
51     DO_FUNC(dbus_error_is_set)
52
53 #define HAL_FUNCS \
54     DO_FUNC(libhal_ctx_free); \
55     DO_FUNC(libhal_ctx_init); \
56     DO_FUNC(libhal_ctx_new); \
57     DO_FUNC(libhal_ctx_set_dbus_connection); \
58     DO_FUNC(libhal_ctx_set_device_added); \
59     DO_FUNC(libhal_ctx_set_device_property_modified); \
60     DO_FUNC(libhal_ctx_set_device_removed); \
61     DO_FUNC(libhal_ctx_shutdown); \
62     DO_FUNC(libhal_device_get_property_bool); \
63     DO_FUNC(libhal_device_get_property_string); \
64     DO_FUNC(libhal_device_add_property_watch); \
65     DO_FUNC(libhal_device_remove_property_watch); \
66     DO_FUNC(libhal_free_string); \
67     DO_FUNC(libhal_free_string_array); \
68     DO_FUNC(libhal_get_all_devices)
69
70 #define DO_FUNC(f) static typeof(f) * p_##f
71 DBUS_FUNCS;
72 HAL_FUNCS;
73 #undef DO_FUNC
74
75 static BOOL load_functions(void)
76 {
77     void *hal_handle;
78     char error[128];
79
80     /* Load libhal with RTLD_GLOBAL so that the dbus symbols are available.
81      * We can't load libdbus directly since libhal may have been built against a
82      * different version but with the same soname. Binary compatibility is for wimps. */
83
84     if (!(hal_handle = wine_dlopen(SONAME_LIBHAL, RTLD_NOW|RTLD_GLOBAL, error, sizeof(error))))
85         goto failed;
86
87 #define DO_FUNC(f) if (!(p_##f = wine_dlsym( RTLD_DEFAULT, #f, error, sizeof(error) ))) goto failed
88     DBUS_FUNCS;
89 #undef DO_FUNC
90
91 #define DO_FUNC(f) if (!(p_##f = wine_dlsym( hal_handle, #f, error, sizeof(error) ))) goto failed
92     HAL_FUNCS;
93 #undef DO_FUNC
94
95     return TRUE;
96
97 failed:
98     WARN( "failed to load HAL support: %s\n", error );
99     return FALSE;
100 }
101
102 static LONG WINAPI assert_fault(EXCEPTION_POINTERS *eptr)
103 {
104     if (eptr->ExceptionRecord->ExceptionCode == EXCEPTION_WINE_ASSERTION)
105         return EXCEPTION_EXECUTE_HANDLER;
106     return EXCEPTION_CONTINUE_SEARCH;
107 }
108
109 static GUID *parse_uuid( GUID *guid, const char *str )
110 {
111     /* standard uuid format */
112     if (strlen(str) == 36)
113     {
114         UNICODE_STRING strW;
115         WCHAR buffer[39];
116
117         if (MultiByteToWideChar( CP_UNIXCP, 0, str, 36, buffer + 1, 36 ))
118         {
119             buffer[0] = '{';
120             buffer[37] = '}';
121             buffer[38] = 0;
122             RtlInitUnicodeString( &strW, buffer );
123             if (!RtlGUIDFromString( &strW, guid )) return guid;
124         }
125     }
126
127     /* check for xxxx-xxxx format (FAT serial number) */
128     if (strlen(str) == 9 && str[4] == '-')
129     {
130         memset( guid, 0, sizeof(*guid) );
131         if (sscanf( str, "%hx-%hx", &guid->Data2, &guid->Data3 ) == 2) return guid;
132     }
133     return NULL;
134 }
135
136 /* HAL callback for new device */
137 static void new_device( LibHalContext *ctx, const char *udi )
138 {
139     DBusError error;
140     char *parent = NULL;
141     char *mount_point = NULL;
142     char *device = NULL;
143     char *type = NULL;
144     char *uuid_str = NULL;
145     GUID guid, *guid_ptr = NULL;
146     enum device_type drive_type;
147
148     p_dbus_error_init( &error );
149
150     if (!(device = p_libhal_device_get_property_string( ctx, udi, "block.device", &error )))
151         goto done;
152
153     if (!(mount_point = p_libhal_device_get_property_string( ctx, udi, "volume.mount_point", &error )))
154         goto done;
155
156     if (!(parent = p_libhal_device_get_property_string( ctx, udi, "info.parent", &error )))
157         goto done;
158
159     if (!(uuid_str = p_libhal_device_get_property_string( ctx, udi, "volume.uuid", &error )))
160         p_dbus_error_free( &error );  /* ignore error */
161     else
162         guid_ptr = parse_uuid( &guid, uuid_str );
163
164     if (!(type = p_libhal_device_get_property_string( ctx, parent, "storage.drive_type", &error )))
165         p_dbus_error_free( &error );  /* ignore error */
166
167     if (type && !strcmp( type, "cdrom" )) drive_type = DEVICE_CDROM;
168     else if (type && !strcmp( type, "floppy" )) drive_type = DEVICE_FLOPPY;
169     else drive_type = DEVICE_UNKNOWN;
170
171     if (p_libhal_device_get_property_bool( ctx, parent, "storage.removable", &error ))
172     {
173         add_dos_device( -1, udi, device, mount_point, drive_type, guid_ptr );
174         /* add property watch for mount point */
175         p_libhal_device_add_property_watch( ctx, udi, &error );
176     }
177     else if (guid_ptr) add_volume( udi, device, mount_point, DEVICE_HARDDISK_VOL, guid_ptr );
178
179 done:
180     if (type) p_libhal_free_string( type );
181     if (parent) p_libhal_free_string( parent );
182     if (device) p_libhal_free_string( device );
183     if (uuid_str) p_libhal_free_string( uuid_str );
184     if (mount_point) p_libhal_free_string( mount_point );
185     p_dbus_error_free( &error );
186 }
187
188 /* HAL callback for removed device */
189 static void removed_device( LibHalContext *ctx, const char *udi )
190 {
191     DBusError error;
192
193     TRACE( "removed %s\n", wine_dbgstr_a(udi) );
194
195     if (!remove_dos_device( -1, udi ))
196     {
197         p_dbus_error_init( &error );
198         p_libhal_device_remove_property_watch( ctx, udi, &error );
199         p_dbus_error_free( &error );
200     }
201     else remove_volume( udi );
202 }
203
204 /* HAL callback for property changes */
205 static void property_modified (LibHalContext *ctx, const char *udi,
206                                const char *key, dbus_bool_t is_removed, dbus_bool_t is_added)
207 {
208     TRACE( "udi %s key %s %s\n", wine_dbgstr_a(udi), wine_dbgstr_a(key),
209                 is_added ? "added" : is_removed ? "removed" : "modified" );
210
211     if (!strcmp( key, "volume.mount_point" )) new_device( ctx, udi );
212 }
213
214
215 static DWORD WINAPI hal_thread( void *arg )
216 {
217     DBusError error;
218     DBusConnection *dbc;
219     LibHalContext *ctx;
220     int i, num;
221     char **list;
222
223     if (!(ctx = p_libhal_ctx_new())) return 1;
224
225     p_dbus_error_init( &error );
226     if (!(dbc = p_dbus_bus_get( DBUS_BUS_SYSTEM, &error )))
227     {
228         WARN( "failed to get system dbus connection: %s\n", error.message );
229         p_dbus_error_free( &error );
230         return 1;
231     }
232
233     p_libhal_ctx_set_dbus_connection( ctx, dbc );
234     p_libhal_ctx_set_device_added( ctx, new_device );
235     p_libhal_ctx_set_device_removed( ctx, removed_device );
236     p_libhal_ctx_set_device_property_modified( ctx, property_modified );
237
238     if (!p_libhal_ctx_init( ctx, &error ))
239     {
240         WARN( "HAL context init failed: %s\n", error.message );
241         p_dbus_error_free( &error );
242         return 1;
243     }
244
245     /* retrieve all existing devices */
246     if (!(list = p_libhal_get_all_devices( ctx, &num, &error ))) p_dbus_error_free( &error );
247     else
248     {
249         for (i = 0; i < num; i++) new_device( ctx, list[i] );
250         p_libhal_free_string_array( list );
251     }
252
253     __TRY
254     {
255         while (p_dbus_connection_read_write_dispatch( dbc, -1 )) /* nothing */ ;
256     }
257     __EXCEPT( assert_fault )
258     {
259         WARN( "dbus assertion failure, disabling HAL support\n" );
260         return 1;
261     }
262     __ENDTRY;
263
264     p_libhal_ctx_shutdown( ctx, &error );
265     p_dbus_error_free( &error );  /* just in case */
266     p_dbus_connection_close( dbc );
267     p_libhal_ctx_free( ctx );
268     return 0;
269 }
270
271 void initialize_hal(void)
272 {
273     HANDLE handle;
274
275     if (!load_functions()) return;
276     if (!(handle = CreateThread( NULL, 0, hal_thread, NULL, 0, NULL ))) return;
277     CloseHandle( handle );
278 }
279
280 #else  /* SONAME_LIBHAL */
281
282 void initialize_hal(void)
283 {
284     TRACE( "Skipping, HAL support not compiled in\n" );
285 }
286
287 #endif  /* SONAME_LIBHAL */