Skip to content

Instantly share code, notes, and snippets.

@unix-beard
Last active August 29, 2015 14:24
Show Gist options
  • Save unix-beard/8160b3f70bda4379c26a to your computer and use it in GitHub Desktop.
Save unix-beard/8160b3f70bda4379c26a to your computer and use it in GitHub Desktop.
How to detect disk eject event on Mac OS X
#include <DiskArbitration/DiskArbitration.h>
void disk_event(DADiskRef disk, void *context)
{
printf("Disk removed: %s\n", DADiskGetBSDName(disk));
CFRunLoopStop(CFRunLoopGetCurrent());
}
int main()
{
DASessionRef session = DASessionCreate(NULL);
DARegisterDiskDisappearedCallback(session, NULL, disk_event, NULL);
//DARegisterDiskAppearedCallback(session, kDADiskDescriptionMatchVolumeMountable, got_disk, NULL);
DASessionScheduleWithRunLoop(session, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
/* Start the run loop. (Don't do this if you already have a running Core Foundation or Cocoa run loop.) */
SInt32 res = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 10, false);
printf("%d\n", res);
switch (res)
{
case kCFRunLoopRunStopped:
case kCFRunLoopRunFinished: printf("Loop stopped\n"); break;
case kCFRunLoopRunTimedOut: printf("Loop timed out\n"); break;
}
DASessionUnscheduleFromRunLoop(session, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
/* Clean up a session. */
CFRelease(session);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment