configure: Get rid of the va_copy checks.
[wine/wine64.git] / dlls / mountmgr.sys / hal.c
blob310b14437565fab17171e64ef71094dcb049a0bc
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 "mountmgr.h"
31 #include "excpt.h"
33 #include "wine/library.h"
34 #include "wine/exception.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr);
39 #ifdef SONAME_LIBHAL
41 #include <dbus/dbus.h>
42 #include <hal/libhal.h>
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)
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)
69 #define DO_FUNC(f) static typeof(f) * p_##f
70 DBUS_FUNCS;
71 HAL_FUNCS;
72 #undef DO_FUNC
74 static BOOL load_functions(void)
76 void *hal_handle;
77 char error[128];
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. */
83 if (!(hal_handle = wine_dlopen(SONAME_LIBHAL, RTLD_NOW|RTLD_GLOBAL, error, sizeof(error))))
84 goto failed;
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
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
94 return TRUE;
96 failed:
97 WARN( "failed to load HAL support: %s\n", error );
98 return FALSE;
101 static LONG WINAPI assert_fault(EXCEPTION_POINTERS *eptr)
103 if (eptr->ExceptionRecord->ExceptionCode == EXCEPTION_WINE_ASSERTION)
104 return EXCEPTION_EXECUTE_HANDLER;
105 return EXCEPTION_CONTINUE_SEARCH;
108 /* HAL callback for new device */
109 static void new_device( LibHalContext *ctx, const char *udi )
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;
118 p_dbus_error_init( &error );
120 if (!(device = p_libhal_device_get_property_string( ctx, udi, "block.device", &error )))
121 goto done;
123 if (!(mount_point = p_libhal_device_get_property_string( ctx, udi, "volume.mount_point", &error )))
124 goto done;
126 if (!(parent = p_libhal_device_get_property_string( ctx, udi, "info.parent", &error )))
127 goto done;
129 if (!p_libhal_device_get_property_bool( ctx, parent, "storage.removable", &error ))
130 goto done;
132 if (!(type = p_libhal_device_get_property_string( ctx, parent, "storage.drive_type", &error )))
133 p_dbus_error_free( &error ); /* ignore error */
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;
139 add_dos_device( -1, udi, device, mount_point, drive_type );
141 /* add property watch for mount point */
142 p_libhal_device_add_property_watch( ctx, udi, &error );
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 );
152 /* HAL callback for removed device */
153 static void removed_device( LibHalContext *ctx, const char *udi )
155 DBusError error;
157 TRACE( "removed %s\n", wine_dbgstr_a(udi) );
159 if (!remove_dos_device( -1, udi ))
161 p_dbus_error_init( &error );
162 p_libhal_device_remove_property_watch( ctx, udi, &error );
163 p_dbus_error_free( &error );
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)
171 TRACE( "udi %s key %s %s\n", wine_dbgstr_a(udi), wine_dbgstr_a(key),
172 is_added ? "added" : is_removed ? "removed" : "modified" );
174 if (!strcmp( key, "volume.mount_point" )) new_device( ctx, udi );
178 static DWORD WINAPI hal_thread( void *arg )
180 DBusError error;
181 DBusConnection *dbc;
182 LibHalContext *ctx;
183 int i, num;
184 char **list;
186 if (!(ctx = p_libhal_ctx_new())) return 1;
188 p_dbus_error_init( &error );
189 if (!(dbc = p_dbus_bus_get( DBUS_BUS_SYSTEM, &error )))
191 WARN( "failed to get system dbus connection: %s\n", error.message );
192 p_dbus_error_free( &error );
193 return 1;
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 );
201 if (!p_libhal_ctx_init( ctx, &error ))
203 WARN( "HAL context init failed: %s\n", error.message );
204 p_dbus_error_free( &error );
205 return 1;
208 /* retrieve all existing devices */
209 if (!(list = p_libhal_get_all_devices( ctx, &num, &error ))) p_dbus_error_free( &error );
210 else
212 for (i = 0; i < num; i++) new_device( ctx, list[i] );
213 p_libhal_free_string_array( list );
216 __TRY
218 while (p_dbus_connection_read_write_dispatch( dbc, -1 )) /* nothing */ ;
220 __EXCEPT( assert_fault )
222 WARN( "dbus assertion failure, disabling HAL support\n" );
223 return 1;
225 __ENDTRY;
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;
234 void initialize_hal(void)
236 HANDLE handle;
238 if (!load_functions()) return;
239 if (!(handle = CreateThread( NULL, 0, hal_thread, NULL, 0, NULL ))) return;
240 CloseHandle( handle );
243 #else /* SONAME_LIBHAL */
245 void initialize_hal(void)
247 TRACE( "Skipping, HAL support not compiled in\n" );
250 #endif /* SONAME_LIBHAL */