explorer: Implement a global system tray window.
[wine] / programs / explorer / device.c
1 /*
2  * Dynamic 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 "dbt.h"
35
36 #include "wine/library.h"
37 #include "wine/list.h"
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(explorer);
41
42 struct dos_drive
43 {
44     struct list entry;
45     char *udi;
46     int   drive;
47 };
48
49 static struct list drives_list = LIST_INIT(drives_list);
50
51 static char *get_dosdevices_path(void)
52 {
53     const char *config_dir = wine_get_config_dir();
54     size_t len = strlen(config_dir) + sizeof("/dosdevices/a::");
55     char *path = HeapAlloc( GetProcessHeap(), 0, len );
56     if (path)
57     {
58         strcpy( path, config_dir );
59         strcat( path, "/dosdevices/a::" );
60     }
61     return path;
62 }
63
64 /* send notification about a change to a given drive */
65 static void send_notify( int drive, int code )
66 {
67     DWORD_PTR result;
68     DEV_BROADCAST_VOLUME info;
69
70     info.dbcv_size       = sizeof(info);
71     info.dbcv_devicetype = DBT_DEVTYP_VOLUME;
72     info.dbcv_reserved   = 0;
73     info.dbcv_unitmask   = 1 << drive;
74     info.dbcv_flags      = DBTF_MEDIA;
75     SendMessageTimeoutW( HWND_BROADCAST, WM_DEVICECHANGE, code, (LPARAM)&info,
76                          SMTO_ABORTIFHUNG, 0, &result );
77 }
78
79 static inline int is_valid_device( struct stat *st )
80 {
81 #if defined(linux) || defined(__sun__)
82     return S_ISBLK( st->st_mode );
83 #else
84     /* disks are char devices on *BSD */
85     return S_ISCHR( st->st_mode );
86 #endif
87 }
88
89 /* find or create a DOS drive for the corresponding device */
90 static int add_drive( const char *device, const char *type )
91 {
92     char *path, *p;
93     char in_use[26];
94     struct stat dev_st, drive_st;
95     int drive, first, last, avail = 0;
96
97     if (stat( device, &dev_st ) == -1 || !is_valid_device( &dev_st )) return -1;
98
99     if (!(path = get_dosdevices_path())) return -1;
100     p = path + strlen(path) - 3;
101
102     memset( in_use, 0, sizeof(in_use) );
103
104     first = 2;
105     last = 26;
106     if (type && !strcmp( type, "floppy" ))
107     {
108         first = 0;
109         last = 2;
110     }
111
112     while (avail != -1)
113     {
114         avail = -1;
115         for (drive = first; drive < last; drive++)
116         {
117             if (in_use[drive]) continue;  /* already checked */
118             *p = 'a' + drive;
119             if (stat( path, &drive_st ) == -1)
120             {
121                 if (lstat( path, &drive_st ) == -1 && errno == ENOENT)  /* this is a candidate */
122                 {
123                     if (avail == -1)
124                     {
125                         p[2] = 0;
126                         /* if mount point symlink doesn't exist either, it's available */
127                         if (lstat( path, &drive_st ) == -1 && errno == ENOENT) avail = drive;
128                         p[2] = ':';
129                     }
130                 }
131                 else in_use[drive] = 1;
132             }
133             else
134             {
135                 in_use[drive] = 1;
136                 if (!is_valid_device( &drive_st )) continue;
137                 if (dev_st.st_rdev == drive_st.st_rdev) goto done;
138             }
139         }
140         if (avail != -1)
141         {
142             /* try to use the one we found */
143             drive = avail;
144             *p = 'a' + drive;
145             if (symlink( device, path ) != -1) goto done;
146             /* failed, retry the search */
147         }
148     }
149     drive = -1;
150
151 done:
152     HeapFree( GetProcessHeap(), 0, path );
153     return drive;
154 }
155
156 static BOOL set_mount_point( struct dos_drive *drive, const char *mount_point )
157 {
158     char *path, *p;
159     struct stat path_st, mnt_st;
160     BOOL modified = FALSE;
161
162     if (drive->drive == -1) return FALSE;
163     if (!(path = get_dosdevices_path())) return FALSE;
164     p = path + strlen(path) - 3;
165     *p = 'a' + drive->drive;
166     p[2] = 0;
167
168     if (mount_point[0])
169     {
170         /* try to avoid unlinking if already set correctly */
171         if (stat( path, &path_st ) == -1 || stat( mount_point, &mnt_st ) == -1 ||
172             path_st.st_dev != mnt_st.st_dev || path_st.st_ino != mnt_st.st_ino)
173         {
174             unlink( path );
175             symlink( mount_point, path );
176             modified = TRUE;
177         }
178     }
179     else
180     {
181         if (unlink( path ) != -1) modified = TRUE;
182     }
183
184     HeapFree( GetProcessHeap(), 0, path );
185     return modified;
186 }
187
188 BOOL add_dos_device( const char *udi, const char *device,
189                      const char *mount_point, const char *type )
190 {
191     struct dos_drive *drive;
192
193     /* first check if it already exists */
194     LIST_FOR_EACH_ENTRY( drive, &drives_list, struct dos_drive, entry )
195     {
196         if (!strcmp( udi, drive->udi )) goto found;
197     }
198
199     if (!(drive = HeapAlloc( GetProcessHeap(), 0, sizeof(*drive) ))) return FALSE;
200     if (!(drive->udi = HeapAlloc( GetProcessHeap(), 0, strlen(udi)+1 )))
201     {
202         HeapFree( GetProcessHeap(), 0, drive );
203         return FALSE;
204     }
205     strcpy( drive->udi, udi );
206     list_add_tail( &drives_list, &drive->entry );
207
208 found:
209     drive->drive = add_drive( device, type );
210     if (drive->drive != -1)
211     {
212         HKEY hkey;
213
214         set_mount_point( drive, mount_point );
215
216         WINE_TRACE( "added device %c: udi %s for %s on %s type %s\n",
217                     'a' + drive->drive, wine_dbgstr_a(udi), wine_dbgstr_a(device),
218                     wine_dbgstr_a(mount_point), wine_dbgstr_a(type) );
219
220         /* hack: force the drive type in the registry */
221         if (!RegCreateKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Drives", &hkey ))
222         {
223             char name[3] = "a:";
224             name[0] += drive->drive;
225             if (!type || strcmp( type, "cdrom" )) type = "floppy";  /* FIXME: default to floppy */
226             RegSetValueExA( hkey, name, 0, REG_SZ, (const BYTE *)type, strlen(type) + 1 );
227             RegCloseKey( hkey );
228         }
229
230         send_notify( drive->drive, DBT_DEVICEARRIVAL );
231     }
232     return TRUE;
233 }
234
235 BOOL remove_dos_device( const char *udi )
236 {
237     HKEY hkey;
238     struct dos_drive *drive;
239
240     LIST_FOR_EACH_ENTRY( drive, &drives_list, struct dos_drive, entry )
241     {
242         if (strcmp( udi, drive->udi )) continue;
243
244         if (drive->drive != -1)
245         {
246             BOOL modified = set_mount_point( drive, "" );
247
248             /* clear the registry key too */
249             if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Drives", &hkey ))
250             {
251                 char name[3] = "a:";
252                 name[0] += drive->drive;
253                 RegDeleteValueA( hkey, name );
254                 RegCloseKey( hkey );
255             }
256
257             if (modified) send_notify( drive->drive, DBT_DEVICEREMOVECOMPLETE );
258         }
259
260         list_remove( &drive->entry );
261         HeapFree( GetProcessHeap(), 0, drive->udi );
262         HeapFree( GetProcessHeap(), 0, drive );
263         return TRUE;
264     }
265     return FALSE;
266 }