wineconsole: Updated Korean resource.
[wine] / programs / explorer / 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 "windef.h"
31 #include "winbase.h"
32 #include "winreg.h"
33 #include "winuser.h"
34
35 #include "wine/debug.h"
36 #include "explorer_private.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(explorer);
39
40 #ifdef __APPLE__
41
42 #include <DiskArbitration/DiskArbitration.h>
43
44 static void appeared_callback( DADiskRef disk, void *context )
45 {
46     CFDictionaryRef dict = DADiskCopyDescription( disk );
47     const void *ref;
48     char device[64];
49     char mount_point[PATH_MAX];
50     const char *type = NULL;
51
52     if (!dict) return;
53
54     /* ignore non-removable devices */
55     if (!(ref = CFDictionaryGetValue( dict, CFSTR("DAMediaRemovable") )) ||
56         !CFBooleanGetValue( ref )) return;
57
58     /* get device name */
59     if (!(ref = CFDictionaryGetValue( dict, CFSTR("DAMediaBSDName") ))) return;
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("DAVolumeKind") )))
69     {
70         if (!CFStringCompare( ref, CFSTR("cd9660"), 0 ) ||
71             !CFStringCompare( ref, CFSTR("udf"), 0 ))
72             type = "cdrom";
73     }
74
75     WINE_TRACE( "got mount notification for '%s' on '%s'\n", device, mount_point );
76
77     add_dos_device( device, device, mount_point, type );
78     CFRelease( dict );
79 }
80
81 static void changed_callback( DADiskRef disk, CFArrayRef keys, void *context )
82 {
83     appeared_callback( disk, context );
84 }
85
86 static void disappeared_callback( DADiskRef disk, void *context )
87 {
88     CFDictionaryRef dict = DADiskCopyDescription( disk );
89     const void *ref;
90     char device[100];
91
92     if (!dict) return;
93
94     /* ignore non-removable devices */
95     if (!(ref = CFDictionaryGetValue( dict, CFSTR("DAMediaRemovable") )) ||
96         !CFBooleanGetValue( ref )) return;
97
98     /* get device name */
99     if (!(ref = CFDictionaryGetValue( dict, CFSTR("DAMediaBSDName") ))) return;
100     strcpy( device, "/dev/r" );
101     CFStringGetCString( ref, device + 6, sizeof(device) - 6, kCFStringEncodingASCII );
102
103     WINE_TRACE( "got unmount notification for '%s'\n", device );
104
105     remove_dos_device( device );
106     CFRelease( dict );
107 }
108
109 static DWORD WINAPI runloop_thread( void *arg )
110 {
111     DASessionRef session = DASessionCreate( NULL );
112
113     if (!session) return 1;
114
115     DASessionScheduleWithRunLoop( session, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode );
116     DARegisterDiskAppearedCallback( session, kDADiskDescriptionMatchVolumeMountable,
117                                     appeared_callback, NULL );
118     DARegisterDiskDisappearedCallback( session, kDADiskDescriptionMatchVolumeMountable,
119                                        disappeared_callback, NULL );
120     DARegisterDiskDescriptionChangedCallback( session, kDADiskDescriptionMatchVolumeMountable,
121                                               kDADiskDescriptionWatchVolumePath, changed_callback, NULL );
122     CFRunLoopRun();
123     DASessionUnscheduleFromRunLoop( session, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode );
124     CFRelease( session );
125     return 0;
126 }
127
128 void initialize_diskarbitration(void)
129 {
130     HANDLE handle;
131
132     if (!(handle = CreateThread( NULL, 0, runloop_thread, NULL, 0, NULL ))) return;
133     CloseHandle( handle );
134 }
135
136 #else  /* __APPLE__ */
137
138 void initialize_diskarbitration(void)
139 {
140     WINE_TRACE( "Skipping on non-Apple platform\n" );
141 }
142
143 #endif  /* __APPLE__ */