server: Hold a pointer to the queue from the async operations.
[wine] / programs / explorer / hal.c
1 /*
2  * HAL devices support
3  *
4  * Copyright 2006 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <errno.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <sys/time.h>
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winreg.h"
33 #include "winuser.h"
34 #include "excpt.h"
35
36 #include "wine/library.h"
37 #include "wine/exception.h"
38 #include "wine/debug.h"
39 #include "explorer_private.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(explorer);
42
43 #ifdef HAVE_LIBHAL
44
45 #include <dbus/dbus.h>
46 #include <hal/libhal.h>
47
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)
55
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)
72
73 #define DO_FUNC(f) static typeof(f) * p_##f
74 DBUS_FUNCS;
75 HAL_FUNCS;
76 #undef DO_FUNC
77
78 static BOOL load_functions(void)
79 {
80     void *hal_handle;
81     char error[128];
82
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. */
86
87     if (!(hal_handle = wine_dlopen(SONAME_LIBHAL, RTLD_NOW|RTLD_GLOBAL, error, sizeof(error))))
88         goto failed;
89
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
93
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
97
98     return TRUE;
99
100 failed:
101     WINE_WARN( "failed to load HAL support: %s\n", error );
102     return FALSE;
103 }
104
105 static WINE_EXCEPTION_FILTER(assert_fault)
106 {
107     if (GetExceptionCode() == EXCEPTION_WINE_ASSERTION) return EXCEPTION_EXECUTE_HANDLER;
108     return EXCEPTION_CONTINUE_SEARCH;
109 }
110
111 /* HAL callback for new device */
112 static void new_device( LibHalContext *ctx, const char *udi )
113 {
114     DBusError error;
115     char *parent, *mount_point, *device, *type;
116
117     p_dbus_error_init( &error );
118
119     if (!(device = p_libhal_device_get_property_string( ctx, udi, "block.device", &error )))
120         goto done;
121
122     if (!(mount_point = p_libhal_device_get_property_string( ctx, udi, "volume.mount_point", &error )))
123         goto done;
124
125     if (!(parent = p_libhal_device_get_property_string( ctx, udi, "info.parent", &error )))
126         goto done;
127
128     if (!p_libhal_device_get_property_bool( ctx, parent, "storage.removable", &error ))
129         goto done;
130
131     if (!(type = p_libhal_device_get_property_string( ctx, parent, "storage.drive_type", &error )))
132         p_dbus_error_free( &error );  /* ignore error */
133
134     add_dos_device( udi, device, mount_point, type );
135
136     if (type) p_libhal_free_string( type );
137     p_libhal_free_string( parent );
138     p_libhal_free_string( device );
139     p_libhal_free_string( mount_point );
140
141     /* add property watch for mount point */
142     p_libhal_device_add_property_watch( ctx, udi, &error );
143
144 done:
145     p_dbus_error_free( &error );
146 }
147
148 /* HAL callback for removed device */
149 static void removed_device( LibHalContext *ctx, const char *udi )
150 {
151     DBusError error;
152
153     WINE_TRACE( "removed %s\n", wine_dbgstr_a(udi) );
154
155     if (remove_dos_device( udi ))
156     {
157         p_dbus_error_init( &error );
158         p_libhal_device_remove_property_watch( ctx, udi, &error );
159         p_dbus_error_free( &error );
160     }
161 }
162
163 /* HAL callback for property changes */
164 static void property_modified (LibHalContext *ctx, const char *udi,
165                                const char *key, dbus_bool_t is_removed, dbus_bool_t is_added)
166 {
167     WINE_TRACE( "udi %s key %s %s\n", wine_dbgstr_a(udi), wine_dbgstr_a(key),
168                 is_added ? "added" : is_removed ? "removed" : "modified" );
169
170     if (!strcmp( key, "volume.mount_point" )) new_device( ctx, udi );
171 }
172
173
174 static DWORD WINAPI hal_thread( void *arg )
175 {
176     DBusError error;
177     DBusConnection *dbc;
178     LibHalContext *ctx;
179     int i, num;
180     char **list;
181
182     if (!(ctx = p_libhal_ctx_new())) return 1;
183
184     p_dbus_error_init( &error );
185     if (!(dbc = p_dbus_bus_get( DBUS_BUS_SYSTEM, &error )))
186     {
187         WINE_WARN( "failed to get system dbus connection: %s\n", error.message );
188         p_dbus_error_free( &error );
189         return 1;
190     }
191
192     p_libhal_ctx_set_dbus_connection( ctx, dbc );
193     p_libhal_ctx_set_device_added( ctx, new_device );
194     p_libhal_ctx_set_device_removed( ctx, removed_device );
195     p_libhal_ctx_set_device_property_modified( ctx, property_modified );
196
197     if (!p_libhal_ctx_init( ctx, &error ))
198     {
199         WINE_WARN( "HAL context init failed: %s\n", error.message );
200         p_dbus_error_free( &error );
201         return 1;
202     }
203
204     /* retrieve all existing devices */
205     if (!(list = p_libhal_get_all_devices( ctx, &num, &error ))) p_dbus_error_free( &error );
206     else
207     {
208         for (i = 0; i < num; i++) new_device( ctx, list[i] );
209         p_libhal_free_string_array( list );
210     }
211
212     __TRY
213     {
214         while (p_dbus_connection_read_write_dispatch( dbc, -1 )) /* nothing */ ;
215     }
216     __EXCEPT( assert_fault )
217     {
218         WINE_WARN( "dbus assertion failure, disabling HAL support\n" );
219         return 1;
220     }
221     __ENDTRY;
222
223     p_libhal_ctx_shutdown( ctx, &error );
224     p_dbus_error_free( &error );  /* just in case */
225     p_dbus_connection_close( dbc );
226     p_libhal_ctx_free( ctx );
227     return 0;
228 }
229
230 void initialize_hal(void)
231 {
232     HANDLE handle;
233
234     if (!load_functions()) return;
235     if (!(handle = CreateThread( NULL, 0, hal_thread, NULL, 0, NULL ))) return;
236     CloseHandle( handle );
237 }
238
239 #else  /* HAVE_LIBHAL */
240
241 void initialize_hal(void)
242 {
243     WINE_TRACE( "Skipping, HAL support not compiled in\n" );
244 }
245
246 #endif  /* HAVE_LIBHAL */