gdi32: Make gdi.exe into a stand-alone 16-bit module.
[wine] / dlls / mountmgr.sys / diskarb.c
1 /*
2  * Devices support using the MacOS Disk Arbitration library.
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 "mountmgr.h"
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(mountmgr);
34
35 #ifdef HAVE_DISKARBITRATION_DISKARBITRATION_H
36
37 #include <DiskArbitration/DiskArbitration.h>
38
39 static void appeared_callback( DADiskRef disk, void *context )
40 {
41     CFDictionaryRef dict = DADiskCopyDescription( disk );
42     const void *ref;
43     char device[64];
44     char mount_point[PATH_MAX];
45     GUID guid, *guid_ptr = NULL;
46     enum device_type type = DEVICE_UNKNOWN;
47
48     if (!dict) return;
49
50     if ((ref = CFDictionaryGetValue( dict, CFSTR("DAVolumeUUID") )))
51     {
52         CFUUIDBytes bytes = CFUUIDGetUUIDBytes( ref );
53         memcpy( &guid, &bytes, sizeof(guid) );
54         guid_ptr = &guid;
55     }
56
57     /* get device name */
58     if (!(ref = CFDictionaryGetValue( dict, CFSTR("DAMediaBSDName") ))) goto done;
59     strcpy( device, "/dev/r" );
60     CFStringGetCString( ref, device + 6, sizeof(device) - 6, kCFStringEncodingASCII );
61
62     if ((ref = CFDictionaryGetValue( dict, CFSTR("DAVolumePath") )))
63         CFURLGetFileSystemRepresentation( ref, true, (UInt8 *)mount_point, sizeof(mount_point) );
64     else
65         mount_point[0] = 0;
66
67     if ((ref = CFDictionaryGetValue( dict, CFSTR("DAVolumeKind") )))
68     {
69         if (!CFStringCompare( ref, CFSTR("cd9660"), 0 ) ||
70             !CFStringCompare( ref, CFSTR("udf"), 0 ))
71             type = DEVICE_CDROM;
72     }
73
74     TRACE( "got mount notification for '%s' on '%s' uuid %s\n",
75            device, mount_point, wine_dbgstr_guid(guid_ptr) );
76
77     if ((ref = CFDictionaryGetValue( dict, CFSTR("DAMediaRemovable") )) && CFBooleanGetValue( ref ))
78         add_dos_device( -1, device, device, mount_point, type, guid_ptr );
79     else
80         if (guid_ptr) add_volume( device, device, mount_point, DEVICE_HARDDISK_VOL, guid_ptr );
81
82 done:
83     CFRelease( dict );
84 }
85
86 static void changed_callback( DADiskRef disk, CFArrayRef keys, void *context )
87 {
88     appeared_callback( disk, context );
89 }
90
91 static void disappeared_callback( DADiskRef disk, void *context )
92 {
93     CFDictionaryRef dict = DADiskCopyDescription( disk );
94     const void *ref;
95     char device[100];
96
97     if (!dict) return;
98
99     /* get device name */
100     if (!(ref = CFDictionaryGetValue( dict, CFSTR("DAMediaBSDName") ))) goto done;
101     strcpy( device, "/dev/r" );
102     CFStringGetCString( ref, device + 6, sizeof(device) - 6, kCFStringEncodingASCII );
103
104     TRACE( "got unmount notification for '%s'\n", device );
105
106     if ((ref = CFDictionaryGetValue( dict, CFSTR("DAMediaRemovable") )) && CFBooleanGetValue( ref ))
107         remove_dos_device( -1, device );
108     else
109         remove_volume( device );
110
111 done:
112     CFRelease( dict );
113 }
114
115 static DWORD WINAPI runloop_thread( void *arg )
116 {
117     DASessionRef session = DASessionCreate( NULL );
118
119     if (!session) return 1;
120
121     DASessionScheduleWithRunLoop( session, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode );
122     DARegisterDiskAppearedCallback( session, kDADiskDescriptionMatchVolumeMountable,
123                                     appeared_callback, NULL );
124     DARegisterDiskDisappearedCallback( session, kDADiskDescriptionMatchVolumeMountable,
125                                        disappeared_callback, NULL );
126     DARegisterDiskDescriptionChangedCallback( session, kDADiskDescriptionMatchVolumeMountable,
127                                               kDADiskDescriptionWatchVolumePath, changed_callback, NULL );
128     CFRunLoopRun();
129     DASessionUnscheduleFromRunLoop( session, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode );
130     CFRelease( session );
131     return 0;
132 }
133
134 void initialize_diskarbitration(void)
135 {
136     HANDLE handle;
137
138     if (!(handle = CreateThread( NULL, 0, runloop_thread, NULL, 0, NULL ))) return;
139     CloseHandle( handle );
140 }
141
142 #else  /*  HAVE_DISKARBITRATION_DISKARBITRATION_H */
143
144 void initialize_diskarbitration(void)
145 {
146     TRACE( "Skipping, Disk Arbitration support not compiled in\n" );
147 }
148
149 #endif  /* HAVE_DISKARBITRATION_DISKARBITRATION_H */