push 8742e928a3d078c1d2cecb5ceee0ffde3118cbb7
[wine/hacks.git] / dlls / mountmgr.sys / hal.c
blobc1d6e7087944607f426a3b64cf27300da71c20ef
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>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winreg.h"
33 #include "winuser.h"
34 #include "excpt.h"
36 #include "wine/library.h"
37 #include "wine/exception.h"
38 #include "wine/debug.h"
39 #include "mountmgr.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr);
43 #ifdef SONAME_LIBHAL
45 #include <dbus/dbus.h>
46 #include <hal/libhal.h>
48 #define DBUS_FUNCS \
49 DO_FUNC(dbus_bus_get); \
50 DO_FUNC(dbus_connection_close); \
51 DO_FUNC(dbus_connection_read_write_dispatch); \
52 DO_FUNC(dbus_error_init); \
53 DO_FUNC(dbus_error_free); \
54 DO_FUNC(dbus_error_is_set)
56 #define HAL_FUNCS \
57 DO_FUNC(libhal_ctx_free); \
58 DO_FUNC(libhal_ctx_init); \
59 DO_FUNC(libhal_ctx_new); \
60 DO_FUNC(libhal_ctx_set_dbus_connection); \
61 DO_FUNC(libhal_ctx_set_device_added); \
62 DO_FUNC(libhal_ctx_set_device_property_modified); \
63 DO_FUNC(libhal_ctx_set_device_removed); \
64 DO_FUNC(libhal_ctx_shutdown); \
65 DO_FUNC(libhal_device_get_property_bool); \
66 DO_FUNC(libhal_device_get_property_string); \
67 DO_FUNC(libhal_device_add_property_watch); \
68 DO_FUNC(libhal_device_remove_property_watch); \
69 DO_FUNC(libhal_free_string); \
70 DO_FUNC(libhal_free_string_array); \
71 DO_FUNC(libhal_get_all_devices)
73 #define DO_FUNC(f) static typeof(f) * p_##f
74 DBUS_FUNCS;
75 HAL_FUNCS;
76 #undef DO_FUNC
78 static BOOL load_functions(void)
80 void *hal_handle;
81 char error[128];
83 /* Load libhal with RTLD_GLOBAL so that the dbus symbols are available.
84 * We can't load libdbus directly since libhal may have been built against a
85 * different version but with the same soname. Binary compatibility is for wimps. */
87 if (!(hal_handle = wine_dlopen(SONAME_LIBHAL, RTLD_NOW|RTLD_GLOBAL, error, sizeof(error))))
88 goto failed;
90 #define DO_FUNC(f) if (!(p_##f = wine_dlsym( RTLD_DEFAULT, #f, error, sizeof(error) ))) goto failed
91 DBUS_FUNCS;
92 #undef DO_FUNC
94 #define DO_FUNC(f) if (!(p_##f = wine_dlsym( hal_handle, #f, error, sizeof(error) ))) goto failed
95 HAL_FUNCS;
96 #undef DO_FUNC
98 return TRUE;
100 failed:
101 WARN( "failed to load HAL support: %s\n", error );
102 return FALSE;
105 static LONG WINAPI assert_fault(EXCEPTION_POINTERS *eptr)
107 if (eptr->ExceptionRecord->ExceptionCode == EXCEPTION_WINE_ASSERTION)
108 return EXCEPTION_EXECUTE_HANDLER;
109 return EXCEPTION_CONTINUE_SEARCH;
112 /* HAL callback for new device */
113 static void new_device( LibHalContext *ctx, const char *udi )
115 DBusError error;
116 char *parent = NULL;
117 char *mount_point = NULL;
118 char *device = NULL;
119 char *type = NULL;
120 DWORD drive_type;
122 p_dbus_error_init( &error );
124 if (!(device = p_libhal_device_get_property_string( ctx, udi, "block.device", &error )))
125 goto done;
127 if (!(mount_point = p_libhal_device_get_property_string( ctx, udi, "volume.mount_point", &error )))
128 goto done;
130 if (!(parent = p_libhal_device_get_property_string( ctx, udi, "info.parent", &error )))
131 goto done;
133 if (!p_libhal_device_get_property_bool( ctx, parent, "storage.removable", &error ))
134 goto done;
136 if (!(type = p_libhal_device_get_property_string( ctx, parent, "storage.drive_type", &error )))
137 p_dbus_error_free( &error ); /* ignore error */
139 if (type && !strcmp( type, "cdrom" )) drive_type = DRIVE_CDROM;
140 else drive_type = DRIVE_REMOVABLE; /* FIXME: default to removable */
142 add_dos_device( udi, device, mount_point, drive_type );
144 /* add property watch for mount point */
145 p_libhal_device_add_property_watch( ctx, udi, &error );
147 done:
148 if (type) p_libhal_free_string( type );
149 if (parent) p_libhal_free_string( parent );
150 if (device) p_libhal_free_string( device );
151 if (mount_point) p_libhal_free_string( mount_point );
152 p_dbus_error_free( &error );
155 /* HAL callback for removed device */
156 static void removed_device( LibHalContext *ctx, const char *udi )
158 DBusError error;
160 TRACE( "removed %s\n", wine_dbgstr_a(udi) );
162 if (remove_dos_device( udi ))
164 p_dbus_error_init( &error );
165 p_libhal_device_remove_property_watch( ctx, udi, &error );
166 p_dbus_error_free( &error );
170 /* HAL callback for property changes */
171 static void property_modified (LibHalContext *ctx, const char *udi,
172 const char *key, dbus_bool_t is_removed, dbus_bool_t is_added)
174 TRACE( "udi %s key %s %s\n", wine_dbgstr_a(udi), wine_dbgstr_a(key),
175 is_added ? "added" : is_removed ? "removed" : "modified" );
177 if (!strcmp( key, "volume.mount_point" )) new_device( ctx, udi );
181 static DWORD WINAPI hal_thread( void *arg )
183 DBusError error;
184 DBusConnection *dbc;
185 LibHalContext *ctx;
186 int i, num;
187 char **list;
189 if (!(ctx = p_libhal_ctx_new())) return 1;
191 p_dbus_error_init( &error );
192 if (!(dbc = p_dbus_bus_get( DBUS_BUS_SYSTEM, &error )))
194 WARN( "failed to get system dbus connection: %s\n", error.message );
195 p_dbus_error_free( &error );
196 return 1;
199 p_libhal_ctx_set_dbus_connection( ctx, dbc );
200 p_libhal_ctx_set_device_added( ctx, new_device );
201 p_libhal_ctx_set_device_removed( ctx, removed_device );
202 p_libhal_ctx_set_device_property_modified( ctx, property_modified );
204 if (!p_libhal_ctx_init( ctx, &error ))
206 WARN( "HAL context init failed: %s\n", error.message );
207 p_dbus_error_free( &error );
208 return 1;
211 /* retrieve all existing devices */
212 if (!(list = p_libhal_get_all_devices( ctx, &num, &error ))) p_dbus_error_free( &error );
213 else
215 for (i = 0; i < num; i++) new_device( ctx, list[i] );
216 p_libhal_free_string_array( list );
219 __TRY
221 while (p_dbus_connection_read_write_dispatch( dbc, -1 )) /* nothing */ ;
223 __EXCEPT( assert_fault )
225 WARN( "dbus assertion failure, disabling HAL support\n" );
226 return 1;
228 __ENDTRY;
230 p_libhal_ctx_shutdown( ctx, &error );
231 p_dbus_error_free( &error ); /* just in case */
232 p_dbus_connection_close( dbc );
233 p_libhal_ctx_free( ctx );
234 return 0;
237 void initialize_hal(void)
239 HANDLE handle;
241 if (!load_functions()) return;
242 if (!(handle = CreateThread( NULL, 0, hal_thread, NULL, 0, NULL ))) return;
243 CloseHandle( handle );
246 #else /* SONAME_LIBHAL */
248 void initialize_hal(void)
250 TRACE( "Skipping, HAL support not compiled in\n" );
253 #endif /* SONAME_LIBHAL */