mountmgr: Rename hal.c to dbus.c.
[wine/multimedia.git] / dlls / mountmgr.sys / dbus.c
blob42974b2b7b80ef060c5ed88b31a9a970db33fd38
1 /*
2 * DBus 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_LIBDBUS_1
30 # include <dbus/dbus.h>
31 #endif
32 #ifdef SONAME_LIBHAL
33 # include <hal/libhal.h>
34 #endif
36 #include "mountmgr.h"
37 #include "winnls.h"
38 #include "excpt.h"
40 #include "wine/library.h"
41 #include "wine/exception.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr);
46 #ifdef SONAME_LIBHAL
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 static GUID *parse_uuid( GUID *guid, const char *str )
114 /* standard uuid format */
115 if (strlen(str) == 36)
117 UNICODE_STRING strW;
118 WCHAR buffer[39];
120 if (MultiByteToWideChar( CP_UNIXCP, 0, str, 36, buffer + 1, 36 ))
122 buffer[0] = '{';
123 buffer[37] = '}';
124 buffer[38] = 0;
125 RtlInitUnicodeString( &strW, buffer );
126 if (!RtlGUIDFromString( &strW, guid )) return guid;
130 /* check for xxxx-xxxx format (FAT serial number) */
131 if (strlen(str) == 9 && str[4] == '-')
133 memset( guid, 0, sizeof(*guid) );
134 if (sscanf( str, "%hx-%hx", &guid->Data2, &guid->Data3 ) == 2) return guid;
136 return NULL;
139 /* HAL callback for new device */
140 static void hal_new_device( LibHalContext *ctx, const char *udi )
142 DBusError error;
143 char *parent = NULL;
144 char *mount_point = NULL;
145 char *device = NULL;
146 char *type = NULL;
147 char *uuid_str = NULL;
148 GUID guid, *guid_ptr = NULL;
149 enum device_type drive_type;
151 p_dbus_error_init( &error );
153 if (!(device = p_libhal_device_get_property_string( ctx, udi, "block.device", &error )))
154 goto done;
156 if (!(mount_point = p_libhal_device_get_property_string( ctx, udi, "volume.mount_point", &error )))
157 goto done;
159 if (!(parent = p_libhal_device_get_property_string( ctx, udi, "info.parent", &error )))
160 goto done;
162 if (!(uuid_str = p_libhal_device_get_property_string( ctx, udi, "volume.uuid", &error )))
163 p_dbus_error_free( &error ); /* ignore error */
164 else
165 guid_ptr = parse_uuid( &guid, uuid_str );
167 if (!(type = p_libhal_device_get_property_string( ctx, parent, "storage.drive_type", &error )))
168 p_dbus_error_free( &error ); /* ignore error */
170 if (type && !strcmp( type, "cdrom" )) drive_type = DEVICE_CDROM;
171 else if (type && !strcmp( type, "floppy" )) drive_type = DEVICE_FLOPPY;
172 else drive_type = DEVICE_UNKNOWN;
174 if (p_libhal_device_get_property_bool( ctx, parent, "storage.removable", &error ))
176 add_dos_device( -1, udi, device, mount_point, drive_type, guid_ptr );
177 /* add property watch for mount point */
178 p_libhal_device_add_property_watch( ctx, udi, &error );
180 else if (guid_ptr) add_volume( udi, device, mount_point, DEVICE_HARDDISK_VOL, guid_ptr );
182 done:
183 if (type) p_libhal_free_string( type );
184 if (parent) p_libhal_free_string( parent );
185 if (device) p_libhal_free_string( device );
186 if (uuid_str) p_libhal_free_string( uuid_str );
187 if (mount_point) p_libhal_free_string( mount_point );
188 p_dbus_error_free( &error );
191 /* HAL callback for removed device */
192 static void hal_removed_device( LibHalContext *ctx, const char *udi )
194 DBusError error;
196 TRACE( "removed %s\n", wine_dbgstr_a(udi) );
198 if (!remove_dos_device( -1, udi ))
200 p_dbus_error_init( &error );
201 p_libhal_device_remove_property_watch( ctx, udi, &error );
202 p_dbus_error_free( &error );
204 else remove_volume( udi );
207 /* HAL callback for property changes */
208 static void hal_property_modified (LibHalContext *ctx, const char *udi,
209 const char *key, dbus_bool_t is_removed, dbus_bool_t is_added)
211 TRACE( "udi %s key %s %s\n", wine_dbgstr_a(udi), wine_dbgstr_a(key),
212 is_added ? "added" : is_removed ? "removed" : "modified" );
214 if (!strcmp( key, "volume.mount_point" )) hal_new_device( ctx, udi );
218 static DWORD WINAPI dbus_thread( void *arg )
220 DBusError error;
221 DBusConnection *dbc;
222 LibHalContext *ctx;
223 int i, num;
224 char **list;
226 if (!(ctx = p_libhal_ctx_new())) return 1;
228 p_dbus_error_init( &error );
229 if (!(dbc = p_dbus_bus_get( DBUS_BUS_SYSTEM, &error )))
231 WARN( "failed to get system dbus connection: %s\n", error.message );
232 p_dbus_error_free( &error );
233 return 1;
236 p_libhal_ctx_set_dbus_connection( ctx, dbc );
237 p_libhal_ctx_set_device_added( ctx, hal_new_device );
238 p_libhal_ctx_set_device_removed( ctx, hal_removed_device );
239 p_libhal_ctx_set_device_property_modified( ctx, hal_property_modified );
241 if (!p_libhal_ctx_init( ctx, &error ))
243 WARN( "HAL context init failed: %s\n", error.message );
244 p_dbus_error_free( &error );
245 return 1;
248 /* retrieve all existing devices */
249 if (!(list = p_libhal_get_all_devices( ctx, &num, &error ))) p_dbus_error_free( &error );
250 else
252 for (i = 0; i < num; i++) hal_new_device( ctx, list[i] );
253 p_libhal_free_string_array( list );
256 __TRY
258 while (p_dbus_connection_read_write_dispatch( dbc, -1 )) /* nothing */ ;
260 __EXCEPT( assert_fault )
262 WARN( "dbus assertion failure, disabling HAL support\n" );
263 return 1;
265 __ENDTRY;
267 p_libhal_ctx_shutdown( ctx, &error );
268 p_dbus_error_free( &error ); /* just in case */
269 p_dbus_connection_close( dbc );
270 p_libhal_ctx_free( ctx );
271 return 0;
274 void initialize_dbus(void)
276 HANDLE handle;
278 if (!load_functions()) return;
279 if (!(handle = CreateThread( NULL, 0, dbus_thread, NULL, 0, NULL ))) return;
280 CloseHandle( handle );
283 #else /* SONAME_LIBHAL */
285 void initialize_dbus(void)
287 TRACE( "Skipping, DBUS support not compiled in\n" );
290 #endif /* SONAME_LIBHAL */