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