4 * OSX support functions for handling the IOKit registry.
5 * Currently this manages access to CD/DVD drives + media, plus HID devices.
7 * The HID part is much simpler...
9 * Copyright (c) 2008 Nathan Keynes.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
22 #include <glib/gmem.h>
23 #include <glib/gstrfuncs.h>
24 #include <sys/param.h>
30 #include <CoreFoundation/CFRunLoop.h>
31 #include <IOKit/IOMessage.h>
32 #include <IOKit/hid/IOHIDLib.h>
33 #include "osx_iokit.h"
37 static IONotificationPortRef notify_port = 0;
38 static io_iterator_t iokit_iterators[4] = {0,0,0,0};
40 struct osx_cdrom_drive {
41 io_string_t ioservice_path;
42 io_string_t vendor_name;
43 io_string_t product_name;
44 char media_path[MAXPATHLEN]; // BSD device path if media present, otherwise the empty string.
45 io_iterator_t media_load_iterator;
46 io_iterator_t media_unload_iterator;
47 int media_fh; // BSD device handle if open, otherwise -1
48 media_changed_callback_t media_changed;
49 void *media_changed_user_data;
52 static gboolean get_bsdname_for_iomedia( io_object_t iomedia, char *buf, int buflen );
54 /***************** IOKit Callbacks ******************/
57 * Called from IOKit for any IOMessages on an IOMedia. Currently the only message
58 * we're interested in is service termination.
60 static void osx_cdrom_media_notify( void *ref, io_service_t service, uint32_t msgType,
63 if( msgType == kIOMessageServiceIsTerminated ) {
64 osx_cdrom_drive_t drive = (osx_cdrom_drive_t)ref;
65 if( drive->media_changed != NULL ) {
66 drive->media_changed( drive, FALSE, drive->media_changed_user_data );
68 if( drive->media_fh != -1 ) {
69 close(drive->media_fh);
72 drive->media_path[0] = '\0';
73 IOObjectRelease( drive->media_unload_iterator );
78 * Called from IOKit when an IOMedia is inserted that we have be interested in.
79 * FIXME: Can the matcher be restricted to descendents of the drive node? currently
80 * we watch for all IOMedia events and compare the device path to see if it's one we
82 * FIXME: We assume for now that a drive has at most one piece of media at a time.
83 * If this isn't the case, the system may get a little confused.
85 static void osx_cdrom_media_inserted( void *ref, io_iterator_t iterator )
87 osx_cdrom_drive_t drive = (osx_cdrom_drive_t)ref;
90 while( (object = IOIteratorNext(iterator)) != 0 ) {
91 io_string_t iopath = "";
92 IORegistryEntryGetPath( object, kIOServicePlane, iopath );
93 if( drive != NULL && g_str_has_prefix(iopath, drive->ioservice_path ) &&
94 get_bsdname_for_iomedia(object, drive->media_path, sizeof(drive->media_path)) ) {
95 // A disc was inserted within the drive of interest
96 if( drive->media_fh != -1 ) {
97 close(drive->media_fh);
101 if( drive->media_changed != NULL ) {
102 drive->media_changed(drive, TRUE, drive->media_changed_user_data);
104 // Add a notification listener to get removal events.
105 IOServiceAddInterestNotification( notify_port, object, kIOGeneralInterest,
106 osx_cdrom_media_notify, drive, &drive->media_unload_iterator );
109 IOObjectRelease( object );
113 static void osx_drives_changed( void *ref, io_iterator_t iterator )
116 while( (object = IOIteratorNext(iterator)) != 0 ) {
117 IOObjectRelease(object);
122 /******************** Support functions *********************/
125 * Determine the BSD device name (ie "/dev/rdisk1") for a given IO object.
126 * @return TRUE if the device name was retrieved, FALSE if the request failed.
128 static gboolean get_bsdname_for_iomedia( io_object_t iomedia, char *buf, int buflen )
130 gboolean result = FALSE;
131 CFTypeRef pathRef = IORegistryEntryCreateCFProperty(iomedia, CFSTR(kIOBSDNameKey),
132 kCFAllocatorDefault, 0 );
135 strcpy( buf, _PATH_DEV "r" );
136 pathlen = strlen(buf);
137 if( CFStringGetCString( pathRef, buf + pathlen, buflen-pathlen,
138 kCFStringEncodingASCII ) != noErr ) {
146 static gboolean osx_cdrom_drive_get_name( io_object_t object, char *vendor, int vendor_len,
147 char *product, int product_len )
149 gboolean result = FALSE;
150 CFMutableDictionaryRef props = 0;
151 if( IORegistryEntryCreateCFProperties(object, &props, kCFAllocatorDefault, kNilOptions) == KERN_SUCCESS ) {
152 CFDictionaryRef dict =
153 (CFDictionaryRef)CFDictionaryGetValue(props, CFSTR(kIOPropertyDeviceCharacteristicsKey));
155 CFTypeRef value = CFDictionaryGetValue(dict, CFSTR(kIOPropertyVendorNameKey));
156 if( value && CFGetTypeID(value) == CFStringGetTypeID() ) {
157 CFStringGetCString( (CFStringRef)value, vendor, vendor_len, kCFStringEncodingUTF8 );
162 value = CFDictionaryGetValue(dict, CFSTR(kIOPropertyProductNameKey));
163 if ( value && CFGetTypeID(value) == CFStringGetTypeID() ) {
164 CFStringGetCString( (CFStringRef)value, product, product_len, kCFStringEncodingUTF8 );
177 * Construct and initialize a new osx_cdrom_drive object, including registering
178 * it's media inserted notification.
180 static osx_cdrom_drive_t osx_cdrom_drive_new( io_object_t device )
182 osx_cdrom_drive_t drive = g_malloc0(sizeof(struct osx_cdrom_drive));
184 IORegistryEntryGetPath( device, kIOServicePlane, drive->ioservice_path );
185 osx_cdrom_drive_get_name( device, drive->vendor_name, sizeof(drive->vendor_name),
186 drive->product_name, sizeof(drive->product_name) );
187 drive->media_path[0] = '\0';
188 drive->media_changed = NULL;
189 drive->media_changed_user_data = NULL;
190 drive->media_fh = -1;
192 IOServiceAddMatchingNotification( notify_port, kIOFirstPublishNotification,
193 IOServiceMatching("IOMedia"),
194 osx_cdrom_media_inserted, drive,
195 &drive->media_load_iterator );
196 osx_cdrom_media_inserted( drive, drive->media_load_iterator );
200 /************************ Exported functions *************************/
202 osx_cdrom_drive_t osx_cdrom_open_drive( const char *devname )
204 io_object_t object = IORegistryEntryFromPath( kIOMasterPortDefault, devname );
205 if( object == MACH_PORT_NULL ) {
209 osx_cdrom_drive_t drive = osx_cdrom_drive_new( object );
210 IOObjectRelease( object );
214 void osx_cdrom_set_media_changed_callback( osx_cdrom_drive_t drive,
215 media_changed_callback_t callback,
218 drive->media_changed = callback;
219 drive->media_changed_user_data = user_data;
222 void osx_cdrom_close_drive( osx_cdrom_drive_t drive )
224 IOObjectRelease( drive->media_load_iterator );
225 IOObjectRelease( drive->media_unload_iterator );
226 if( drive->media_fh != -1 ) {
227 close(drive->media_fh);
228 drive->media_fh = -1;
233 int osx_cdrom_get_media_handle( osx_cdrom_drive_t drive )
235 if( drive->media_fh == -1 ) {
236 if( drive->media_path[0] != '\0' ) {
237 drive->media_fh = open( drive->media_path, O_RDONLY|O_NONBLOCK );
240 return drive->media_fh;
243 void osx_cdrom_release_media_handle( osx_cdrom_drive_t drive )
245 if( drive->media_fh != -1 ) {
246 close( drive->media_fh );
247 drive->media_fh = -1;
251 static io_object_t iterator_find_cdrom( io_object_t iterator, find_drive_callback_t callback, void *user_data )
254 while( (object = IOIteratorNext(iterator)) != 0 ) {
255 io_string_t iopath = "";
256 char product[256], vendor[256];
257 IORegistryEntryGetPath( object, kIOServicePlane, iopath );
258 osx_cdrom_drive_get_name( object, vendor, sizeof(vendor), product, sizeof(product) );
259 if( callback( object, vendor, product, iopath, user_data ) ) {
260 IOObjectRelease(iterator);
263 IOObjectRelease(object);
265 IOObjectRelease(iterator);
271 * Search for a CD or DVD drive (instance of IODVDServices or IOCompactDiscServices).
272 * The callback will be called repeatedly until either it returns TRUE, or all drives
273 * have been iterated over.
275 * @return an IO registry entry for the matched drive, or 0 if no drives matched.
277 * Note: Use of IOCompactDiscServices is somewhat tentative since I don't have a Mac
278 * with a CD-Rom drive.
280 io_object_t find_cdrom_drive( find_drive_callback_t callback, void *user_data )
282 mach_port_t master_port;
283 CFMutableDictionaryRef match;
284 io_iterator_t services;
287 if( IOMasterPort( MACH_PORT_NULL, &master_port ) != KERN_SUCCESS ) {
288 return 0; // Failed to get the master port?
291 match = IOServiceMatching("IODVDServices");
292 if( IOServiceGetMatchingServices(master_port, match, &services) != kIOReturnSuccess ) {
296 result = iterator_find_cdrom( services, callback, user_data );
301 match = IOServiceMatching("IOCompactDiscServices");
302 if( IOServiceGetMatchingServices(master_port, match, &services) != kIOReturnSuccess ) {
305 return iterator_find_cdrom( services, callback, user_data );
309 // *********************** Notification management ************************/
311 static void osx_hid_inserted( void *ref, io_iterator_t iterator )
314 while( (object = IOIteratorNext(iterator)) != 0 ) {
315 io_string_t iopath = "";
316 IORegistryEntryGetPath( object, kIOServicePlane, iopath );
317 IOObjectRelease( object );
321 gboolean osx_register_iokit_notifications()
323 notify_port = IONotificationPortCreate( kIOMasterPortDefault );
324 CFRunLoopSourceRef runloop_source = IONotificationPortGetRunLoopSource( notify_port );
325 CFRunLoopAddSource( CFRunLoopGetCurrent(), runloop_source, kCFRunLoopCommonModes );
327 // Drive notifications
328 if( IOServiceAddMatchingNotification( notify_port, kIOFirstPublishNotification,
329 IOServiceMatching("IOCompactDiscServies"),
330 osx_drives_changed, NULL, &iokit_iterators[0] ) != kIOReturnSuccess ) {
331 ERROR( "IOServiceAddMatchingNotification failed" );
333 osx_drives_changed(NULL, iokit_iterators[0]);
334 if( IOServiceAddMatchingNotification( notify_port, kIOFirstPublishNotification,
335 IOServiceMatching("IODVDServies"),
336 osx_drives_changed, NULL, &iokit_iterators[1] ) != kIOReturnSuccess ) {
337 ERROR( "IOServiceAddMatchingNotification failed" );
339 osx_drives_changed(NULL, iokit_iterators[1]);
341 if( IOServiceAddMatchingNotification( notify_port, kIOFirstPublishNotification,
342 IOServiceMatching(kIOHIDDeviceKey),
343 osx_hid_inserted, NULL, &iokit_iterators[2] ) != kIOReturnSuccess ) {
344 ERROR( "IOServiceAddMatchingNotification failed" );
346 osx_hid_inserted(NULL, iokit_iterators[2]);
350 void osx_unregister_iokit_notifications()
352 CFRunLoopSourceRef runloop_source = IONotificationPortGetRunLoopSource( notify_port );
353 CFRunLoopRemoveSource( CFRunLoopGetCurrent(), runloop_source, kCFRunLoopCommonModes );
354 IONotificationPortDestroy( notify_port );
.