Last active
August 29, 2015 14:24
-
-
Save unix-beard/8160b3f70bda4379c26a to your computer and use it in GitHub Desktop.
How to detect disk eject event on Mac OS X
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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