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
22 #include "wine/port.h"
30 # include <dbus/dbus.h>
31 # include <hal/libhal.h>
38 #include "wine/library.h"
39 #include "wine/exception.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr
);
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)
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
76 static BOOL
load_functions(void)
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
))))
88 #define DO_FUNC(f) if (!(p_##f = wine_dlsym( RTLD_DEFAULT, #f, error, sizeof(error) ))) goto failed
92 #define DO_FUNC(f) if (!(p_##f = wine_dlsym( hal_handle, #f, error, sizeof(error) ))) goto failed
99 WARN( "failed to load HAL support: %s\n", error
);
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)
118 if (MultiByteToWideChar( CP_UNIXCP
, 0, str
, 36, buffer
+ 1, 36 ))
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
;
137 /* HAL callback for new device */
138 static void new_device( LibHalContext
*ctx
, const char *udi
)
142 char *mount_point
= 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
)))
154 if (!(mount_point
= p_libhal_device_get_property_string( ctx
, udi
, "volume.mount_point", &error
)))
157 if (!(parent
= p_libhal_device_get_property_string( ctx
, udi
, "info.parent", &error
)))
160 if (!(uuid_str
= p_libhal_device_get_property_string( ctx
, udi
, "volume.uuid", &error
)))
161 p_dbus_error_free( &error
); /* ignore error */
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
);
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
)
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
)
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
);
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
);
246 /* retrieve all existing devices */
247 if (!(list
= p_libhal_get_all_devices( ctx
, &num
, &error
))) p_dbus_error_free( &error
);
250 for (i
= 0; i
< num
; i
++) new_device( ctx
, list
[i
] );
251 p_libhal_free_string_array( list
);
256 while (p_dbus_connection_read_write_dispatch( dbc
, -1 )) /* nothing */ ;
258 __EXCEPT( assert_fault
)
260 WARN( "dbus assertion failure, disabling HAL support\n" );
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
);
272 void initialize_hal(void)
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 */