configure: Add a check for libdbus independent from the libhal check.
[wine/multimedia.git] / dlls / mountmgr.sys / hal.c
blobd3698797ccaf8fcf21ea9007ee614ef3834be799
1 /*
2 * HAL devices support
4 * Copyright 2006 Alexandre Julliard
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <errno.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <sys/time.h>
29 #ifdef SONAME_LIBHAL
30 # include <dbus/dbus.h>
31 # include <hal/libhal.h>
32 #endif
34 #include "mountmgr.h"
35 #include "winnls.h"
36 #include "excpt.h"
38 #include "wine/library.h"
39 #include "wine/exception.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr);
44 #ifdef SONAME_LIBHAL
46 #define DBUS_FUNCS \
47 DO_FUNC(dbus_bus_get); \
48 DO_FUNC(dbus_connection_close); \
49 DO_FUNC(dbus_connection_read_write_dispatch); \
50 DO_FUNC(dbus_error_init); \
51 DO_FUNC(dbus_error_free); \
52 DO_FUNC(dbus_error_is_set)
54 #define HAL_FUNCS \
55 DO_FUNC(libhal_ctx_free); \
56 DO_FUNC(libhal_ctx_init); \
57 DO_FUNC(libhal_ctx_new); \
58 DO_FUNC(libhal_ctx_set_dbus_connection); \
59 DO_FUNC(libhal_ctx_set_device_added); \
60 DO_FUNC(libhal_ctx_set_device_property_modified); \
61 DO_FUNC(libhal_ctx_set_device_removed); \
62 DO_FUNC(libhal_ctx_shutdown); \
63 DO_FUNC(libhal_device_get_property_bool); \
64 DO_FUNC(libhal_device_get_property_string); \
65 DO_FUNC(libhal_device_add_property_watch); \
66 DO_FUNC(libhal_device_remove_property_watch); \
67 DO_FUNC(libhal_free_string); \
68 DO_FUNC(libhal_free_string_array); \
69 DO_FUNC(libhal_get_all_devices)
71 #define DO_FUNC(f) static typeof(f) * p_##f
72 DBUS_FUNCS;
73 HAL_FUNCS;
74 #undef DO_FUNC
76 static BOOL load_functions(void)
78 void *hal_handle;
79 char error[128];
81 /* Load libhal with RTLD_GLOBAL so that the dbus symbols are available.
82 * We can't load libdbus directly since libhal may have been built against a
83 * different version but with the same soname. Binary compatibility is for wimps. */
85 if (!(hal_handle = wine_dlopen(SONAME_LIBHAL, RTLD_NOW|RTLD_GLOBAL, error, sizeof(error))))
86 goto failed;
88 #define DO_FUNC(f) if (!(p_##f = wine_dlsym( RTLD_DEFAULT, #f, error, sizeof(error) ))) goto failed
89 DBUS_FUNCS;
90 #undef DO_FUNC
92 #define DO_FUNC(f) if (!(p_##f = wine_dlsym( hal_handle, #f, error, sizeof(error) ))) goto failed
93 HAL_FUNCS;
94 #undef DO_FUNC
96 return TRUE;
98 failed:
99 WARN( "failed to load HAL support: %s\n", error );
100 return FALSE;
103 static LONG WINAPI assert_fault(EXCEPTION_POINTERS *eptr)
105 if (eptr->ExceptionRecord->ExceptionCode == EXCEPTION_WINE_ASSERTION)
106 return EXCEPTION_EXECUTE_HANDLER;
107 return EXCEPTION_CONTINUE_SEARCH;
110 static GUID *parse_uuid( GUID *guid, const char *str )
112 /* standard uuid format */
113 if (strlen(str) == 36)
115 UNICODE_STRING strW;
116 WCHAR buffer[39];
118 if (MultiByteToWideChar( CP_UNIXCP, 0, str, 36, buffer + 1, 36 ))
120 buffer[0] = '{';
121 buffer[37] = '}';
122 buffer[38] = 0;
123 RtlInitUnicodeString( &strW, buffer );
124 if (!RtlGUIDFromString( &strW, guid )) return guid;
128 /* check for xxxx-xxxx format (FAT serial number) */
129 if (strlen(str) == 9 && str[4] == '-')
131 memset( guid, 0, sizeof(*guid) );
132 if (sscanf( str, "%hx-%hx", &guid->Data2, &guid->Data3 ) == 2) return guid;
134 return NULL;
137 /* HAL callback for new device */
138 static void new_device( LibHalContext *ctx, const char *udi )
140 DBusError error;
141 char *parent = NULL;
142 char *mount_point = NULL;
143 char *device = NULL;
144 char *type = NULL;
145 char *uuid_str = NULL;
146 GUID guid, *guid_ptr = NULL;
147 enum device_type drive_type;
149 p_dbus_error_init( &error );
151 if (!(device = p_libhal_device_get_property_string( ctx, udi, "block.device", &error )))
152 goto done;
154 if (!(mount_point = p_libhal_device_get_property_string( ctx, udi, "volume.mount_point", &error )))
155 goto done;
157 if (!(parent = p_libhal_device_get_property_string( ctx, udi, "info.parent", &error )))
158 goto done;
160 if (!(uuid_str = p_libhal_device_get_property_string( ctx, udi, "volume.uuid", &error )))
161 p_dbus_error_free( &error ); /* ignore error */
162 else
163 guid_ptr = parse_uuid( &guid, uuid_str );
165 if (!(type = p_libhal_device_get_property_string( ctx, parent, "storage.drive_type", &error )))
166 p_dbus_error_free( &error ); /* ignore error */
168 if (type && !strcmp( type, "cdrom" )) drive_type = DEVICE_CDROM;
169 else if (type && !strcmp( type, "floppy" )) drive_type = DEVICE_FLOPPY;
170 else drive_type = DEVICE_UNKNOWN;
172 if (p_libhal_device_get_property_bool( ctx, parent, "storage.removable", &error ))
174 add_dos_device( -1, udi, device, mount_point, drive_type, guid_ptr );
175 /* add property watch for mount point */
176 p_libhal_device_add_property_watch( ctx, udi, &error );
178 else if (guid_ptr) add_volume( udi, device, mount_point, DEVICE_HARDDISK_VOL, guid_ptr );
180 done:
181 if (type) p_libhal_free_string( type );
182 if (parent) p_libhal_free_string( parent );
183 if (device) p_libhal_free_string( device );
184 if (uuid_str) p_libhal_free_string( uuid_str );
185 if (mount_point) p_libhal_free_string( mount_point );
186 p_dbus_error_free( &error );
189 /* HAL callback for removed device */
190 static void removed_device( LibHalContext *ctx, const char *udi )
192 DBusError error;
194 TRACE( "removed %s\n", wine_dbgstr_a(udi) );
196 if (!remove_dos_device( -1, udi ))
198 p_dbus_error_init( &error );
199 p_libhal_device_remove_property_watch( ctx, udi, &error );
200 p_dbus_error_free( &error );
202 else remove_volume( udi );
205 /* HAL callback for property changes */
206 static void property_modified (LibHalContext *ctx, const char *udi,
207 const char *key, dbus_bool_t is_removed, dbus_bool_t is_added)
209 TRACE( "udi %s key %s %s\n", wine_dbgstr_a(udi), wine_dbgstr_a(key),
210 is_added ? "added" : is_removed ? "removed" : "modified" );
212 if (!strcmp( key, "volume.mount_point" )) new_device( ctx, udi );
216 static DWORD WINAPI hal_thread( void *arg )
218 DBusError error;
219 DBusConnection *dbc;
220 LibHalContext *ctx;
221 int i, num;
222 char **list;
224 if (!(ctx = p_libhal_ctx_new())) return 1;
226 p_dbus_error_init( &error );
227 if (!(dbc = p_dbus_bus_get( DBUS_BUS_SYSTEM, &error )))
229 WARN( "failed to get system dbus connection: %s\n", error.message );
230 p_dbus_error_free( &error );
231 return 1;
234 p_libhal_ctx_set_dbus_connection( ctx, dbc );
235 p_libhal_ctx_set_device_added( ctx, new_device );
236 p_libhal_ctx_set_device_removed( ctx, removed_device );
237 p_libhal_ctx_set_device_property_modified( ctx, property_modified );
239 if (!p_libhal_ctx_init( ctx, &error ))
241 WARN( "HAL context init failed: %s\n", error.message );
242 p_dbus_error_free( &error );
243 return 1;
246 /* retrieve all existing devices */
247 if (!(list = p_libhal_get_all_devices( ctx, &num, &error ))) p_dbus_error_free( &error );
248 else
250 for (i = 0; i < num; i++) new_device( ctx, list[i] );
251 p_libhal_free_string_array( list );
254 __TRY
256 while (p_dbus_connection_read_write_dispatch( dbc, -1 )) /* nothing */ ;
258 __EXCEPT( assert_fault )
260 WARN( "dbus assertion failure, disabling HAL support\n" );
261 return 1;
263 __ENDTRY;
265 p_libhal_ctx_shutdown( ctx, &error );
266 p_dbus_error_free( &error ); /* just in case */
267 p_dbus_connection_close( dbc );
268 p_libhal_ctx_free( ctx );
269 return 0;
272 void initialize_hal(void)
274 HANDLE handle;
276 if (!load_functions()) return;
277 if (!(handle = CreateThread( NULL, 0, hal_thread, NULL, 0, NULL ))) return;
278 CloseHandle( handle );
281 #else /* SONAME_LIBHAL */
283 void initialize_hal(void)
285 TRACE( "Skipping, HAL support not compiled in\n" );
288 #endif /* SONAME_LIBHAL */