7 |
|
* |
8 |
|
* Based on Apple's CDROMSample.c and Evan Jones' cd-discid.c patches |
9 |
|
* |
10 |
< |
* Basilisk II (C) 1997-2004 Christian Bauer |
10 |
> |
* Basilisk II (C) 1997-2008 Christian Bauer |
11 |
|
* |
12 |
|
* This program is free software; you can redistribute it and/or modify |
13 |
|
* it under the terms of the GNU General Public License as published by |
31 |
|
#import <IOKit/serial/IOSerialKeys.h> |
32 |
|
#import <IOKit/storage/IOMedia.h> |
33 |
|
#import <IOKit/storage/IOMediaBSDClient.h> |
34 |
< |
#ifdef MAC_OS_X_VERSION_10_2 |
34 |
> |
#ifdef HAVE_IOKIT_STORAGE_IOBLOCKSTORAGEDEVICE_H |
35 |
|
#import <IOKit/storage/IOBlockStorageDevice.h> |
36 |
|
#endif |
37 |
|
#import <IOKit/storage/IOCDMedia.h> |
38 |
|
#import <IOKit/storage/IOCDMediaBSDClient.h> |
39 |
|
#import <CoreFoundation/CoreFoundation.h> |
40 |
|
|
41 |
< |
#import "sysdeps.h" |
41 |
> |
#include "sysdeps.h" |
42 |
|
|
43 |
< |
#import "prefs.h" |
43 |
> |
#include "sys.h" |
44 |
> |
#include "prefs.h" |
45 |
|
|
46 |
|
#define DEBUG 0 |
47 |
|
#import "debug.h" |
48 |
|
|
49 |
|
|
50 |
+ |
// Global variables |
51 |
+ |
static volatile CFRunLoopRef media_poll_loop = NULL; |
52 |
+ |
static bool media_thread_active = false; |
53 |
+ |
static pthread_t media_thread; |
54 |
+ |
|
55 |
+ |
// Prototypes |
56 |
+ |
static void *media_poll_func(void *); |
57 |
+ |
|
58 |
+ |
// From sys_unix.cpp |
59 |
+ |
extern void SysMediaArrived(const char *path, int type); |
60 |
+ |
extern void SysMediaRemoved(const char *path, int type); |
61 |
+ |
|
62 |
|
|
63 |
|
/* |
64 |
< |
* This gets called when no "cdrom" prefs items are found |
52 |
< |
* It scans for available CD-ROM drives and adds appropriate prefs items |
64 |
> |
* Initialization |
65 |
|
*/ |
66 |
|
|
67 |
< |
void DarwinAddCDROMPrefs(void) |
67 |
> |
void DarwinSysInit(void) |
68 |
|
{ |
69 |
< |
mach_port_t masterPort; // The way to talk to the kernel |
70 |
< |
io_iterator_t allCDs; // List of CD drives on the system |
71 |
< |
CFMutableDictionaryRef classesToMatch; |
72 |
< |
io_object_t nextCD; |
69 |
> |
if (!PrefsFindBool("nocdrom")) { |
70 |
> |
media_thread_active = (pthread_create(&media_thread, NULL, media_poll_func, NULL) == 0); |
71 |
> |
D(bug("Media poll thread installed (%ld)\n", media_thread)); |
72 |
> |
} |
73 |
> |
} |
74 |
|
|
75 |
|
|
76 |
< |
// Don't scan for drives if nocdrom option given |
77 |
< |
if ( PrefsFindBool("nocdrom") ) |
78 |
< |
return; |
76 |
> |
/* |
77 |
> |
* Deinitialization |
78 |
> |
*/ |
79 |
|
|
80 |
+ |
void DarwinSysExit(void) |
81 |
+ |
{ |
82 |
+ |
// Stop media poll thread |
83 |
+ |
if (media_thread_active) { |
84 |
+ |
while (media_poll_loop == NULL || !CFRunLoopIsWaiting(media_poll_loop)) |
85 |
+ |
usleep(0); |
86 |
+ |
CFRunLoopStop(media_poll_loop); |
87 |
+ |
pthread_join(media_thread, NULL); |
88 |
+ |
media_poll_loop = NULL; |
89 |
+ |
media_thread_active = false; |
90 |
+ |
} |
91 |
+ |
} |
92 |
|
|
68 |
– |
// Let this task talk to the guts of the kernel: |
69 |
– |
if ( IOMasterPort(MACH_PORT_NULL, &masterPort) != KERN_SUCCESS ) |
70 |
– |
bug("IOMasterPort failed. Won't be able to do anything with CD drives\n"); |
93 |
|
|
94 |
+ |
/* |
95 |
+ |
* Get the BSD-style path of specified object |
96 |
+ |
*/ |
97 |
|
|
98 |
< |
// CD media are instances of class kIOCDMediaClass |
99 |
< |
classesToMatch = IOServiceMatching(kIOCDMediaClass); |
100 |
< |
if ( classesToMatch ) |
101 |
< |
{ |
102 |
< |
// Narrow the search a little further. Each IOMedia object |
103 |
< |
// has a property with key kIOMediaEjectable. We limit |
104 |
< |
// the match only to those CDs that are actually ejectable |
105 |
< |
CFDictionarySetValue(classesToMatch, |
106 |
< |
CFSTR(kIOMediaEjectableKey), kCFBooleanTrue); |
98 |
> |
static kern_return_t get_device_path(io_object_t obj, char *path, size_t maxPathLength) |
99 |
> |
{ |
100 |
> |
kern_return_t kernResult = KERN_FAILURE; |
101 |
> |
CFTypeRef pathAsCFString = IORegistryEntryCreateCFProperty(obj, CFSTR(kIOBSDNameKey), |
102 |
> |
kCFAllocatorDefault, 0); |
103 |
> |
if (pathAsCFString) { |
104 |
> |
strcpy(path, "/dev/"); |
105 |
> |
size_t pathLength = strlen(path); |
106 |
> |
if (CFStringGetCString((const __CFString *)pathAsCFString, |
107 |
> |
path + pathLength, |
108 |
> |
maxPathLength - pathLength, |
109 |
> |
kCFStringEncodingASCII)) |
110 |
> |
kernResult = KERN_SUCCESS; |
111 |
> |
CFRelease(pathAsCFString); |
112 |
|
} |
113 |
+ |
return kernResult; |
114 |
+ |
} |
115 |
|
|
84 |
– |
if ( IOServiceGetMatchingServices(masterPort, |
85 |
– |
classesToMatch, &allCDs) != KERN_SUCCESS ) |
86 |
– |
{ |
87 |
– |
D(bug("IOServiceGetMatchingServices failed. No CD media drives found?\n")); |
88 |
– |
return; |
89 |
– |
} |
116 |
|
|
117 |
+ |
/* |
118 |
+ |
* kIOMatchedNotification handler |
119 |
+ |
*/ |
120 |
|
|
121 |
< |
// Iterate through each CD drive |
122 |
< |
while ( nextCD = IOIteratorNext(allCDs)) |
123 |
< |
{ |
124 |
< |
char bsdPath[MAXPATHLEN]; |
125 |
< |
CFTypeRef bsdPathAsCFString = |
126 |
< |
IORegistryEntryCreateCFProperty(nextCD, |
127 |
< |
CFSTR(kIOBSDNameKey), |
128 |
< |
kCFAllocatorDefault, 0); |
129 |
< |
*bsdPath = '\0'; |
130 |
< |
if ( bsdPathAsCFString ) |
131 |
< |
{ |
132 |
< |
size_t devPathLength; |
121 |
> |
static void media_arrived(int type, io_iterator_t iterator) |
122 |
> |
{ |
123 |
> |
io_object_t obj; |
124 |
> |
while ((obj = IOIteratorNext(iterator))) { |
125 |
> |
char path[MAXPATHLEN]; |
126 |
> |
kern_return_t kernResult = get_device_path(obj, path, sizeof(path)); |
127 |
> |
if (kernResult == KERN_SUCCESS) { |
128 |
> |
D(bug("Media Arrived: %s\n", path)); |
129 |
> |
SysMediaArrived(path, type); |
130 |
> |
} |
131 |
> |
kernResult = IOObjectRelease(obj); |
132 |
> |
if (kernResult != KERN_SUCCESS) { |
133 |
> |
fprintf(stderr, "IOObjectRelease() returned %d\n", kernResult); |
134 |
> |
} |
135 |
> |
} |
136 |
> |
} |
137 |
|
|
105 |
– |
strcpy(bsdPath, "/dev/"); |
106 |
– |
devPathLength = strlen(bsdPath); |
138 |
|
|
139 |
< |
if ( CFStringGetCString((const __CFString *)bsdPathAsCFString, |
140 |
< |
bsdPath + devPathLength, |
141 |
< |
MAXPATHLEN - devPathLength, |
111 |
< |
kCFStringEncodingASCII) ) |
112 |
< |
{ |
113 |
< |
D(bug("CDROM BSD path: %s\n", bsdPath)); |
114 |
< |
PrefsAddString("cdrom", bsdPath); |
115 |
< |
} |
116 |
< |
else |
117 |
< |
D(bug("Could not get BSD device path for CD\n")); |
139 |
> |
/* |
140 |
> |
* kIOTerminatedNotification handler |
141 |
> |
*/ |
142 |
|
|
143 |
< |
CFRelease(bsdPathAsCFString); |
143 |
> |
static void media_removed(int type, io_iterator_t iterator) |
144 |
> |
{ |
145 |
> |
io_object_t obj; |
146 |
> |
while ((obj = IOIteratorNext(iterator))) { |
147 |
> |
char path[MAXPATHLEN]; |
148 |
> |
kern_return_t kernResult = get_device_path(obj, path, sizeof(path)); |
149 |
> |
if (kernResult == KERN_SUCCESS) { |
150 |
> |
D(bug("Media Removed: %s\n", path)); |
151 |
> |
SysMediaRemoved(path, type); |
152 |
> |
} |
153 |
> |
kernResult = IOObjectRelease(obj); |
154 |
> |
if (kernResult != KERN_SUCCESS) { |
155 |
> |
fprintf(stderr, "IOObjectRelease() returned %d\n", kernResult); |
156 |
|
} |
121 |
– |
else |
122 |
– |
D(bug("Cannot determine bsdPath for CD\n")); |
157 |
|
} |
158 |
+ |
} |
159 |
+ |
|
160 |
+ |
|
161 |
+ |
/* |
162 |
+ |
* Media poll function |
163 |
+ |
* |
164 |
+ |
* NOTE: to facilitate orderly thread termination, media_poll_func MUST end up waiting in CFRunLoopRun. |
165 |
+ |
* Early returns must be avoided, even if there is nothing useful to be done here. See DarwinSysExit. |
166 |
+ |
*/ |
167 |
|
|
168 |
< |
IOObjectRelease(nextCD); |
169 |
< |
IOObjectRelease(allCDs); |
168 |
> |
static void dummy(void *) { }; // stub for dummy runloop source |
169 |
> |
|
170 |
> |
static void *media_poll_func(void *) |
171 |
> |
{ |
172 |
> |
media_poll_loop = CFRunLoopGetCurrent(); |
173 |
> |
|
174 |
> |
mach_port_t masterPort; |
175 |
> |
kern_return_t kernResult; |
176 |
> |
CFMutableDictionaryRef matchingDictionary; |
177 |
> |
CFRunLoopSourceRef loopSource = NULL; |
178 |
> |
CFRunLoopSourceRef dummySource = NULL; |
179 |
> |
|
180 |
> |
if ((kernResult = IOMasterPort(bootstrap_port, &masterPort)) != KERN_SUCCESS) |
181 |
> |
fprintf(stderr, "IOMasterPort() returned %d\n", kernResult); |
182 |
> |
else if ((matchingDictionary = IOServiceMatching(kIOCDMediaClass)) == NULL) |
183 |
> |
fprintf(stderr, "IOServiceMatching() returned a NULL dictionary\n"); |
184 |
> |
else { |
185 |
> |
matchingDictionary = (CFMutableDictionaryRef)CFRetain(matchingDictionary); |
186 |
> |
IONotificationPortRef notificationPort = IONotificationPortCreate(kIOMasterPortDefault); |
187 |
> |
loopSource = IONotificationPortGetRunLoopSource(notificationPort); |
188 |
> |
CFRunLoopAddSource(media_poll_loop, loopSource, kCFRunLoopDefaultMode); |
189 |
> |
|
190 |
> |
io_iterator_t mediaArrivedIterator; |
191 |
> |
kernResult = IOServiceAddMatchingNotification(notificationPort, |
192 |
> |
kIOMatchedNotification, |
193 |
> |
matchingDictionary, |
194 |
> |
(IOServiceMatchingCallback)media_arrived, |
195 |
> |
(void *)MEDIA_CD, &mediaArrivedIterator); |
196 |
> |
if (kernResult != KERN_SUCCESS) |
197 |
> |
fprintf(stderr, "IOServiceAddMatchingNotification() returned %d\n", kernResult); |
198 |
> |
media_arrived(MEDIA_CD, mediaArrivedIterator); |
199 |
> |
|
200 |
> |
io_iterator_t mediaRemovedIterator; |
201 |
> |
kernResult = IOServiceAddMatchingNotification(notificationPort, |
202 |
> |
kIOTerminatedNotification, |
203 |
> |
matchingDictionary, |
204 |
> |
(IOServiceMatchingCallback)media_removed, |
205 |
> |
(void *)MEDIA_CD, &mediaRemovedIterator); |
206 |
> |
if (kernResult != KERN_SUCCESS) |
207 |
> |
fprintf(stderr, "IOServiceAddMatchingNotification() returned %d\n", kernResult); |
208 |
> |
media_removed(MEDIA_CD, mediaRemovedIterator); |
209 |
> |
} |
210 |
> |
|
211 |
> |
if (loopSource == NULL) { |
212 |
> |
// add a dummy runloop source to prevent premature return from CFRunLoopRun |
213 |
> |
CFRunLoopSourceContext context = { 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, dummy }; |
214 |
> |
dummySource = CFRunLoopSourceCreate(NULL, 0, &context); |
215 |
> |
CFRunLoopAddSource(media_poll_loop, dummySource, kCFRunLoopDefaultMode); |
216 |
> |
} |
217 |
> |
|
218 |
> |
CFRunLoopRun(); |
219 |
> |
|
220 |
> |
if (dummySource != NULL) |
221 |
> |
CFRelease(dummySource); |
222 |
> |
return NULL; |
223 |
|
} |
224 |
|
|
225 |
|
|
226 |
|
void DarwinAddFloppyPrefs(void) |
227 |
|
{ |
132 |
– |
#ifdef MAC_OS_X_VERSION_10_2 |
228 |
|
mach_port_t masterPort; // The way to talk to the kernel |
229 |
< |
io_iterator_t allFloppies; // List of CD drives on the system |
229 |
> |
io_iterator_t allFloppies; // List of possible floppys |
230 |
|
CFMutableDictionaryRef classesToMatch; |
231 |
|
io_object_t nextFloppy; |
232 |
|
|
235 |
|
bug("IOMasterPort failed. Won't be able to do anything with floppy drives\n"); |
236 |
|
|
237 |
|
|
238 |
+ |
// This selects all partitions of all disks |
239 |
|
classesToMatch = IOServiceMatching(kIOMediaClass); |
240 |
|
if ( classesToMatch ) |
241 |
|
{ |
242 |
< |
// We acually want removables that are _not_ CDs, |
147 |
< |
// but I don't know how to do that yet. |
242 |
> |
// Skip drivers and partitions |
243 |
|
CFDictionarySetValue(classesToMatch, |
244 |
< |
CFSTR(kIOMediaRemovableKey), kCFBooleanTrue); |
244 |
> |
CFSTR(kIOMediaWholeKey), kCFBooleanTrue); |
245 |
> |
|
246 |
> |
// Skip fixed drives (hard disks?) |
247 |
> |
CFDictionarySetValue(classesToMatch, |
248 |
> |
CFSTR(kIOMediaEjectableKey), kCFBooleanTrue); |
249 |
|
} |
250 |
|
|
251 |
|
if ( IOServiceGetMatchingServices(masterPort, |
255 |
|
return; |
256 |
|
} |
257 |
|
|
159 |
– |
|
258 |
|
// Iterate through each floppy |
259 |
|
while ( nextFloppy = IOIteratorNext(allFloppies)) |
260 |
|
{ |
261 |
|
char bsdPath[MAXPATHLEN]; |
262 |
< |
CFTypeRef bsdPathAsCFString = |
262 |
> |
long size; |
263 |
> |
CFTypeRef sizeAsCFNumber = |
264 |
|
IORegistryEntryCreateCFProperty(nextFloppy, |
265 |
< |
CFSTR(kIOBSDNameKey), |
265 |
> |
CFSTR(kIOMediaSizeKey), |
266 |
|
kCFAllocatorDefault, 0); |
168 |
– |
*bsdPath = '\0'; |
169 |
– |
if ( bsdPathAsCFString ) |
170 |
– |
{ |
171 |
– |
size_t devPathLength; |
267 |
|
|
268 |
< |
strcpy(bsdPath, "/dev/"); |
269 |
< |
devPathLength = strlen(bsdPath); |
270 |
< |
|
271 |
< |
if ( CFStringGetCString((const __CFString *)bsdPathAsCFString, |
272 |
< |
bsdPath + devPathLength, |
178 |
< |
MAXPATHLEN - devPathLength, |
179 |
< |
kCFStringEncodingASCII) ) |
268 |
> |
if ( CFNumberGetValue((CFNumberRef)sizeAsCFNumber, |
269 |
> |
kCFNumberSInt32Type, &size) ) |
270 |
> |
{ |
271 |
> |
D(bug("Got size of %ld\n", size)); |
272 |
> |
if ( size < 800 * 1024 || size > 1440 * 1024 ) |
273 |
|
{ |
274 |
< |
D(bug("Floppy BSD path: %s\n", bsdPath)); |
275 |
< |
PrefsAddString("floppy", bsdPath); |
274 |
> |
D(puts("Device does not appear to be 800k or 1440k")); |
275 |
> |
continue; |
276 |
|
} |
277 |
< |
else |
278 |
< |
D(bug("Could not get BSD device path for floppy\n")); |
277 |
> |
} |
278 |
> |
else { |
279 |
> |
D(puts("Couldn't get kIOMediaSizeKey of device")); |
280 |
> |
continue; // if kIOMediaSizeKey is unavailable, we shouldn't use it anyway |
281 |
> |
} |
282 |
|
|
283 |
< |
CFRelease(bsdPathAsCFString); |
283 |
> |
if (get_device_path(nextFloppy, bsdPath, sizeof(bsdPath)) == KERN_SUCCESS) { |
284 |
> |
PrefsAddString("floppy", bsdPath); |
285 |
> |
} else { |
286 |
> |
D(bug("Could not get BSD device path for floppy\n")); |
287 |
|
} |
189 |
– |
else |
190 |
– |
D(bug("Cannot determine bsdPath for floppy\n")); |
288 |
|
} |
289 |
|
|
290 |
|
IOObjectRelease(nextFloppy); |
291 |
|
IOObjectRelease(allFloppies); |
195 |
– |
#endif |
292 |
|
} |
293 |
|
|
294 |
|
|