diff options
author | koekeishiya <aasvi93@hotmail.com> | 2018-07-19 22:00:05 +0200 |
---|---|---|
committer | koekeishiya <aasvi93@hotmail.com> | 2018-07-19 22:00:05 +0200 |
commit | e2b0e15880f903f8efe9b6b458450eafa44c99c6 (patch) | |
tree | f8cdfd930f9e4c9806252d0f1d856e5e6804f363 /src/hotload.c | |
parent | 14e1c4237d8bb8e6716952c2a7e29e7232c6df9d (diff) | |
download | skhd-e2b0e15880f903f8efe9b6b458450eafa44c99c6.tar.gz skhd-e2b0e15880f903f8efe9b6b458450eafa44c99c6.zip |
hotloader: code cleanup
Diffstat (limited to '')
-rw-r--r-- | src/hotload.c | 66 |
1 files changed, 38 insertions, 28 deletions
diff --git a/src/hotload.c b/src/hotload.c index e5f633f..23774ff 100644 --- a/src/hotload.c +++ b/src/hotload.c @@ -96,7 +96,8 @@ void hotloader_add_file(struct hotloader *hotloader, char *file) .filename = file_name(real_path) }; hotloader->watch_list[hotloader->watch_count++] = watch_info; - printf("hotload: watching file '%s' in directory '%s'\n", watch_info.filename, watch_info.directory); + printf("hotload: watching file '%s' in directory '%s'\n", + watch_info.filename, watch_info.directory); } else { fprintf(stderr, "hotload: could not watch file '%s'\n", file); } @@ -105,10 +106,7 @@ void hotloader_add_file(struct hotloader *hotloader, char *file) bool hotloader_begin(struct hotloader *hotloader, hotloader_callback *callback) { - if ((hotloader->enabled) || - (!hotloader->watch_count)) { - return false; - } + if (hotloader->enabled || !hotloader->watch_count) return false; CFStringRef string_refs[hotloader->watch_count]; for (unsigned index = 0; index < hotloader->watch_count; ++index) { @@ -117,13 +115,18 @@ bool hotloader_begin(struct hotloader *hotloader, hotloader_callback *callback) kCFStringEncodingUTF8); } - FSEventStreamContext context = {}; - context.info = (void *) hotloader; + FSEventStreamContext context = { + .info = hotloader + }; + + hotloader->path = (CFArrayRef) CFArrayCreate(NULL, + (const void **) string_refs, + hotloader->watch_count, + &kCFTypeArrayCallBacks); + + hotloader->flags = kFSEventStreamCreateFlagNoDefer | + kFSEventStreamCreateFlagFileEvents; - hotloader->enabled = true; - hotloader->callback = callback; - hotloader->path = (CFArrayRef) CFArrayCreate(NULL, (const void **) string_refs, hotloader->watch_count, &kCFTypeArrayCallBacks); - hotloader->flags = kFSEventStreamCreateFlagNoDefer | kFSEventStreamCreateFlagFileEvents; hotloader->stream = FSEventStreamCreate(NULL, hotloader_handler, &context, @@ -131,28 +134,35 @@ bool hotloader_begin(struct hotloader *hotloader, hotloader_callback *callback) kFSEventStreamEventIdSinceNow, 0.5, hotloader->flags); - FSEventStreamScheduleWithRunLoop(hotloader->stream, CFRunLoopGetMain(), kCFRunLoopDefaultMode); + + FSEventStreamScheduleWithRunLoop(hotloader->stream, + CFRunLoopGetMain(), + kCFRunLoopDefaultMode); + + hotloader->callback = callback; FSEventStreamStart(hotloader->stream); + hotloader->enabled = true; + return true; } void hotloader_end(struct hotloader *hotloader) { - if (hotloader->enabled) { - FSEventStreamStop(hotloader->stream); - FSEventStreamInvalidate(hotloader->stream); - FSEventStreamRelease(hotloader->stream); - - CFIndex count = CFArrayGetCount(hotloader->path); - for (unsigned index = 0; index < count; ++index) { - CFStringRef string_ref = (CFStringRef) CFArrayGetValueAtIndex(hotloader->path, index); - free(hotloader->watch_list[index].absolutepath); - free(hotloader->watch_list[index].directory); - free(hotloader->watch_list[index].filename); - CFRelease(string_ref); - } - - CFRelease(hotloader->path); - memset(hotloader, 0, sizeof(struct hotloader)); + if (!hotloader->enabled) return; + + FSEventStreamStop(hotloader->stream); + FSEventStreamInvalidate(hotloader->stream); + FSEventStreamRelease(hotloader->stream); + + CFIndex count = CFArrayGetCount(hotloader->path); + for (unsigned index = 0; index < count; ++index) { + CFTypeRef string_ref = CFArrayGetValueAtIndex(hotloader->path, index); + free(hotloader->watch_list[index].absolutepath); + free(hotloader->watch_list[index].directory); + free(hotloader->watch_list[index].filename); + CFRelease(string_ref); } + + CFRelease(hotloader->path); + memset(hotloader, 0, sizeof(struct hotloader)); } |