From 4e45bdc20c859dcf8994037db2461d90dee7004a Mon Sep 17 00:00:00 2001 From: koekeishiya Date: Tue, 12 Sep 2017 14:47:09 +0200 Subject: finalize hotloader api; self-contained --- src/hotload.c | 38 +++++++++++++++++++++++++++++++------- src/hotload.h | 10 ++-------- src/skhd.c | 17 ++++------------- 3 files changed, 37 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/hotload.c b/src/hotload.c index 5f8e5de..fc06a9b 100644 --- a/src/hotload.c +++ b/src/hotload.c @@ -6,6 +6,13 @@ #define internal static +#define FSEVENT_CALLBACK(name) void name(ConstFSEventStreamRef stream,\ + void *context,\ + size_t count,\ + void *paths,\ + const FSEventStreamEventFlags *flags,\ + const FSEventStreamEventId *ids) + internal char * copy_string(const char *s) { @@ -34,18 +41,19 @@ file_name(const char *file) return name; } -bool hotloader_watched_file(struct hotloader *hotloader, char *absolutepath) +internal struct watch_info * +hotloader_watched_file(struct hotloader *hotloader, char *absolutepath) { - bool success = false; - for(unsigned index = 0; success == 0 && index < hotloader->watch_count; ++index) { - struct watched_file *watch_info = &hotloader->watch_list[index]; + struct watch_info *result = NULL; + for(unsigned index = 0; result == NULL && index < hotloader->watch_count; ++index) { + struct watched_file *watch_info = hotloader->watch_list + index; char *directory = file_directory(absolutepath); char *filename = file_name(absolutepath); if(strcmp(watch_info->directory, directory) == 0) { if(strcmp(watch_info->filename, filename) == 0) { - success = true; + result = watch_info; } } @@ -53,7 +61,22 @@ bool hotloader_watched_file(struct hotloader *hotloader, char *absolutepath) free(directory); } - return success; + return result; +} + +internal FSEVENT_CALLBACK(hotloader_handler) +{ + /* NOTE(koekeishiya): We sometimes get two events upon file save. */ + struct hotloader *hotloader = (struct hotloader *) context; + char **files = (char **) paths; + + struct watched_file *watch_info; + for(unsigned index = 0; index < count; ++index) { + char *absolutepath = files[index]; + if((watch_info = hotloader_watched_file(hotloader, absolutepath))) { + hotloader->callback(absolutepath, watch_info->directory, watch_info->filename); + } + } } void hotloader_add_file(struct hotloader *hotloader, const char *file) @@ -85,10 +108,11 @@ bool hotloader_begin(struct hotloader *hotloader, hotloader_callback *callback) context.info = (void *) hotloader; 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, - callback, + hotloader_handler, &context, hotloader->path, kFSEventStreamEventIdSinceNow, diff --git a/src/hotload.h b/src/hotload.h index 8d012f2..5948a21 100644 --- a/src/hotload.h +++ b/src/hotload.h @@ -4,12 +4,7 @@ #include #include -#define HOTLOADER_CALLBACK(name) void name(ConstFSEventStreamRef stream,\ - void *context,\ - size_t count,\ - void *paths,\ - const FSEventStreamEventFlags *flags,\ - const FSEventStreamEventId *ids) +#define HOTLOADER_CALLBACK(name) void name(char *absolutepath, char *directory, char *filename) typedef HOTLOADER_CALLBACK(hotloader_callback); struct watched_file @@ -25,14 +20,13 @@ struct hotloader CFArrayRef path; bool enabled; + hotloader_callback *callback; struct watched_file watch_list[32]; unsigned watch_count; }; bool hotloader_begin(struct hotloader *hotloader, hotloader_callback *callback); void hotloader_end(struct hotloader *hotloader); - void hotloader_add_file(struct hotloader *hotloader, const char *file); -bool hotloader_watched_file(struct hotloader *hotloader, char *absolutepath); #endif diff --git a/src/skhd.c b/src/skhd.c index d915926..bd8841d 100644 --- a/src/skhd.c +++ b/src/skhd.c @@ -76,19 +76,10 @@ parse_config_helper(char *absolutepath) } } -internal HOTLOADER_CALLBACK(hotloader_handler) +internal HOTLOADER_CALLBACK(config_handler) { - /* NOTE(koekeishiya): We sometimes get two events upon file save. */ - struct hotloader *hotloader = (struct hotloader *) context; - char **files = (char **) paths; - - for(unsigned index = 0; index < count; ++index) { - char *absolutepath = files[index]; - if(hotloader_watched_file(hotloader, absolutepath)) { - free_hotkeys(&hotkey_map); - parse_config_helper(absolutepath); - } - } + free_hotkeys(&hotkey_map); + parse_config_helper(absolutepath); } internal EVENT_TAP_CALLBACK(key_handler) @@ -210,7 +201,7 @@ int main(int argc, char **argv) struct hotloader hotloader = {}; hotloader_add_file(&hotloader, config_file); - hotloader_begin(&hotloader, hotloader_handler); + hotloader_begin(&hotloader, config_handler); CFRunLoopRun(); -- cgit v1.2.3