ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/MacOSX/sys_darwin.cpp
(Generate patch)

Comparing BasiliskII/src/MacOSX/sys_darwin.cpp (file contents):
Revision 1.7 by gbeauche, 2005-01-30T21:42:13Z vs.
Revision 1.15 by asvitkine, 2008-07-20T07:38:27Z

# Line 7 | Line 7
7   *
8   *      Based on Apple's CDROMSample.c and Evan Jones' cd-discid.c patches
9   *
10 < *  Basilisk II (C) 1997-2005 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
# Line 31 | Line 31
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;
60 <        io_object_t                             nextCD;
69 >        media_thread_active = (pthread_create(&media_thread, NULL, media_poll_func, NULL) == 0);
70 >        D(bug("Media poll thread installed (%ld)\n", media_thread));
71 > }
72  
73  
74 <        // Don't scan for drives if nocdrom option given
75 <        if ( PrefsFindBool("nocdrom") )
76 <                return;
74 > /*
75 > *  Deinitialization
76 > */
77  
78 + void DarwinSysExit(void)
79 + {
80 +        // Stop media poll thread
81 +        if (media_thread_active) {
82 +                while (media_poll_loop == NULL || !CFRunLoopIsWaiting(media_poll_loop))
83 +                        usleep(0);
84 +                CFRunLoopStop(media_poll_loop);
85 +                pthread_join(media_thread, NULL);
86 +                media_poll_loop = NULL;
87 +                media_thread_active = false;
88 +        }
89 + }
90  
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");
91  
92 + /*
93 + *  Get the BSD-style path of specified object
94 + */
95  
96 <        // CD media are instances of class kIOCDMediaClass
97 <        classesToMatch = IOServiceMatching(kIOCDMediaClass);
98 <        if ( classesToMatch )
99 <        {
100 <                // Narrow the search a little further. Each IOMedia object
101 <                // has a property with key kIOMediaEjectable.  We limit
102 <                // the match only to those CDs that are actually ejectable
103 <                CFDictionarySetValue(classesToMatch,
104 <                                                         CFSTR(kIOMediaEjectableKey), kCFBooleanTrue);
96 > static kern_return_t get_device_path(io_object_t obj, char *path, size_t maxPathLength)
97 > {
98 >        kern_return_t kernResult = KERN_FAILURE;
99 >        CFTypeRef pathAsCFString = IORegistryEntryCreateCFProperty(obj, CFSTR(kIOBSDNameKey),
100 >                                                                                                                           kCFAllocatorDefault, 0);
101 >        if (pathAsCFString) {
102 >                strcpy(path, "/dev/");
103 >                size_t pathLength = strlen(path);
104 >                if (CFStringGetCString((const __CFString *)pathAsCFString,
105 >                                                           path + pathLength,
106 >                                                           maxPathLength - pathLength,
107 >                                                           kCFStringEncodingASCII))
108 >                        kernResult = KERN_SUCCESS;
109 >                CFRelease(pathAsCFString);
110          }
111 +        return kernResult;
112 + }
113  
84        if ( IOServiceGetMatchingServices(masterPort,
85                                                                          classesToMatch, &allCDs) != KERN_SUCCESS )
86        {
87                D(bug("IOServiceGetMatchingServices failed. No CD media drives found?\n"));
88                return;
89        }
114  
115 + /*
116 + *  kIOMatchedNotification handler
117 + */
118  
119 <        // Iterate through each CD drive
120 <        while ( nextCD = IOIteratorNext(allCDs))
121 <        {
122 <                char            bsdPath[MAXPATHLEN];
123 <                CFTypeRef       bsdPathAsCFString =
124 <                                                IORegistryEntryCreateCFProperty(nextCD,
125 <                                                                                                                CFSTR(kIOBSDNameKey),
126 <                                                                                                                kCFAllocatorDefault, 0);
127 <                *bsdPath = '\0';
128 <                if ( bsdPathAsCFString )
129 <                {
130 <                        size_t devPathLength;
119 > static void media_arrived(int type, io_iterator_t iterator)
120 > {
121 >        io_object_t obj;
122 >        while ((obj = IOIteratorNext(iterator))) {
123 >                char path[MAXPATHLEN];
124 >                kern_return_t kernResult = get_device_path(obj, path, sizeof(path));
125 >                if (kernResult == KERN_SUCCESS) {
126 >                        D(bug("Media Arrived: %s\n", path));
127 >                        SysMediaArrived(path, type);
128 >                }
129 >                kernResult = IOObjectRelease(obj);
130 >                if (kernResult != KERN_SUCCESS) {
131 >                        fprintf(stderr, "IOObjectRelease() returned %d\n", kernResult);
132 >                }
133 >        }
134 > }
135  
105                        strcpy(bsdPath, "/dev/");
106                        devPathLength = strlen(bsdPath);
136  
137 <                        if ( CFStringGetCString((const __CFString *)bsdPathAsCFString,
138 <                                                                         bsdPath + devPathLength,
139 <                                                                         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"));
137 > /*
138 > *  kIOTerminatedNotification handler
139 > */
140  
141 <                        CFRelease(bsdPathAsCFString);
141 > static void media_removed(int type, io_iterator_t iterator)
142 > {
143 >        io_object_t obj;
144 >        while ((obj = IOIteratorNext(iterator))) {
145 >                char path[MAXPATHLEN];
146 >                kern_return_t kernResult = get_device_path(obj, path, sizeof(path));
147 >                if (kernResult == KERN_SUCCESS) {
148 >                        D(bug("Media Removed: %s\n", path));
149 >                        SysMediaRemoved(path, type);
150 >                }
151 >                kernResult = IOObjectRelease(obj);
152 >                if (kernResult != KERN_SUCCESS) {
153 >                        fprintf(stderr, "IOObjectRelease() returned %d\n", kernResult);
154                  }
121                else
122                        D(bug("Cannot determine bsdPath for CD\n"));
155          }
156 + }
157 +
158 +
159 + /*
160 + *  Media poll function
161 + *
162 + *  NOTE: to facilitate orderly thread termination, media_poll_func MUST end up waiting in CFRunLoopRun.
163 + *  Early returns must be avoided, even if there is nothing useful to be done here. See DarwinSysExit.
164 + */
165  
166 <        IOObjectRelease(nextCD);
167 <        IOObjectRelease(allCDs);
166 > static void dummy(void *) { };  // stub for dummy runloop source
167 >
168 > static void *media_poll_func(void *)
169 > {
170 >        media_poll_loop = CFRunLoopGetCurrent();
171 >
172 >        mach_port_t masterPort;
173 >        kern_return_t kernResult;
174 >        CFMutableDictionaryRef matchingDictionary;
175 >        CFRunLoopSourceRef loopSource = NULL;
176 >        CFRunLoopSourceRef dummySource = NULL;
177 >
178 >        if ((kernResult = IOMasterPort(bootstrap_port, &masterPort)) != KERN_SUCCESS)
179 >                fprintf(stderr, "IOMasterPort() returned %d\n", kernResult);
180 >        else if ((matchingDictionary = IOServiceMatching(kIOCDMediaClass)) == NULL)
181 >                fprintf(stderr, "IOServiceMatching() returned a NULL dictionary\n");
182 >        else {
183 >                matchingDictionary = (CFMutableDictionaryRef)CFRetain(matchingDictionary);
184 >                IONotificationPortRef notificationPort = IONotificationPortCreate(kIOMasterPortDefault);
185 >                loopSource = IONotificationPortGetRunLoopSource(notificationPort);
186 >                CFRunLoopAddSource(media_poll_loop, loopSource, kCFRunLoopDefaultMode);
187 >
188 >                io_iterator_t mediaArrivedIterator;
189 >                kernResult = IOServiceAddMatchingNotification(notificationPort,
190 >                        kIOMatchedNotification,
191 >                        matchingDictionary,
192 >                        (IOServiceMatchingCallback)media_arrived,
193 >                        (void *)MEDIA_CD, &mediaArrivedIterator);
194 >                if (kernResult != KERN_SUCCESS)
195 >                        fprintf(stderr, "IOServiceAddMatchingNotification() returned %d\n", kernResult);
196 >                media_arrived(MEDIA_CD, mediaArrivedIterator);
197 >
198 >                io_iterator_t mediaRemovedIterator;
199 >                kernResult = IOServiceAddMatchingNotification(notificationPort,
200 >                        kIOTerminatedNotification,
201 >                        matchingDictionary,
202 >                        (IOServiceMatchingCallback)media_removed,
203 >                        (void *)MEDIA_CD, &mediaRemovedIterator);
204 >                if (kernResult != KERN_SUCCESS)
205 >                        fprintf(stderr, "IOServiceAddMatchingNotification() returned %d\n", kernResult);
206 >                media_removed(MEDIA_CD, mediaRemovedIterator);
207 >        }
208 >
209 >        if (loopSource == NULL) {
210 >                // add a dummy runloop source to prevent premature return from CFRunLoopRun
211 >                CFRunLoopSourceContext context = { 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, dummy };
212 >                dummySource = CFRunLoopSourceCreate(NULL, 0, &context);
213 >                CFRunLoopAddSource(media_poll_loop, dummySource, kCFRunLoopDefaultMode);
214 >        }
215 >
216 >        CFRunLoopRun();
217 >
218 >        if (dummySource != NULL)
219 >                CFRelease(dummySource);
220 >        return NULL;
221   }
222  
223  
# Line 159 | Line 253 | void DarwinAddFloppyPrefs(void)
253                  return;
254          }
255  
162
256          // Iterate through each floppy
257          while ( nextFloppy = IOIteratorNext(allFloppies))
258          {
259                  char            bsdPath[MAXPATHLEN];
167                CFTypeRef       bsdPathAsCFString =
168                                                IORegistryEntryCreateCFProperty(nextFloppy,
169                                                                                                                CFSTR(kIOBSDNameKey),
170                                                                                                                kCFAllocatorDefault, 0);
260                  long            size;
261                  CFTypeRef       sizeAsCFNumber =
262                                                  IORegistryEntryCreateCFProperty(nextFloppy,
# Line 184 | Line 273 | void DarwinAddFloppyPrefs(void)
273                                  continue;
274                          }
275                  }
276 <                else
277 <                        bug("Couldn't get kIOMediaSizeKey of device");
278 <                
279 <                
191 <                *bsdPath = '\0';
192 <                if ( bsdPathAsCFString )
193 <                {
194 <                        size_t devPathLength;
195 <
196 <                        strcpy(bsdPath, "/dev/");
197 <                        devPathLength = strlen(bsdPath);
198 <
199 <                        if ( CFStringGetCString((const __CFString *)bsdPathAsCFString,
200 <                                                                         bsdPath + devPathLength,
201 <                                                                         MAXPATHLEN - devPathLength,
202 <                                                                         kCFStringEncodingASCII) )
203 <                        {
204 <                                D(bug("Floppy BSD path: %s\n", bsdPath));
205 <                                PrefsAddString("floppy", bsdPath);
206 <                        }
207 <                        else
208 <                                D(bug("Could not get BSD device path for floppy\n"));
276 >                else {
277 >                        D(puts("Couldn't get kIOMediaSizeKey of device"));
278 >                        continue; // if kIOMediaSizeKey is unavailable, we shouldn't use it anyway
279 >                }
280  
281 <                        CFRelease(bsdPathAsCFString);
281 >                if (get_device_path(nextFloppy, bsdPath, sizeof(bsdPath)) == KERN_SUCCESS) {
282 >                        PrefsAddString("floppy", bsdPath);
283 >                } else {
284 >                        D(bug("Could not get BSD device path for floppy\n"));
285                  }
212                else
213                        D(bug("Cannot determine bsdPath for floppy\n"));
286          }
287  
288          IOObjectRelease(nextFloppy);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines