Created
January 11, 2017 23:02
-
-
Save SeijiEmery/914ed54b87352e56f38ce4e63d7fa6ee to your computer and use it in GitHub Desktop.
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
| // This is a snippet from https://github.com/SeijiEmery/GLSandbox/blob/master/fsevents-test/main.cpp | |
| void fsEventCallback ( | |
| ConstFSEventStreamRef streamRef, | |
| void *clientCallBackInfo, | |
| size_t numEvents, | |
| void *eventPaths, | |
| const FSEventStreamEventFlags eventFlags[], | |
| const FSEventStreamEventId eventIds[] | |
| ) { | |
| char **paths = (char **)eventPaths; | |
| for (auto i = 0; i < numEvents; ++i) { | |
| printf("Change %llu in %s, flags %d\n", eventIds[i], paths[i], eventFlags[i]); | |
| // GlobalFileChangeWatcher is a c++ class that does further processing (recursively scans local files, etc) | |
| // This code just uses osx services to determine when _something_ changed (ie. a directory path), and determine | |
| // _what_ changed and how to handle that (file type, extensions, are we using it, etc) is done by GFCW and uses | |
| // recursive stat calls. | |
| GlobalFileChangeWatcher::getInstance().update_dir(paths[i]); | |
| } | |
| } | |
| struct FSDirWatcher { | |
| CFStringRef dir_path; | |
| FSEventStreamRef stream; | |
| CFAbsoluteTime latency = 3.0; | |
| FSDirWatcher (const char * path) { | |
| printf("setup event stream listener on '%s'\n", path); | |
| GlobalFileChangeWatcher::getInstance().add_dir(path); | |
| dir_path = CFStringCreateWithCString(kCFAllocatorDefault, path, kCFStringEncodingUTF8); | |
| CFArrayRef pathsToWatch = CFArrayCreate(kCFAllocatorDefault, (const void**)&dir_path, 1, nullptr); | |
| stream = FSEventStreamCreate(kCFAllocatorDefault, | |
| (FSEventStreamCallback)&fsEventCallback, | |
| nullptr, | |
| pathsToWatch, | |
| kFSEventStreamEventIdSinceNow, | |
| latency, | |
| kFSEventStreamCreateFlagNone); | |
| CFRelease(pathsToWatch); | |
| FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); | |
| FSEventStreamStart(stream); | |
| } | |
| ~FSDirWatcher () { | |
| FSEventStreamStop(stream); | |
| CFRelease(dir_path); | |
| } | |
| }; | |
| typedef std::shared_ptr<FSDirWatcher> FSDirWatcherRef; | |
| int main(int argc, const char * argv[]) { | |
| CFRunLoopGetCurrent(); | |
| std::vector<FSDirWatcherRef> watchers { | |
| std::make_shared<FSDirWatcher> ( "/Users/semery/misc-projects/GLSandbox/" ), | |
| std::make_shared<FSDirWatcher> ( "/Users/semery/Library/Application Support/GLSandbox/" ) | |
| }; | |
| CFRunLoopRun(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment