diff options
author | koekeishiya <aasvi93@hotmail.com> | 2017-09-12 11:29:13 +0200 |
---|---|---|
committer | koekeishiya <aasvi93@hotmail.com> | 2017-09-12 11:29:13 +0200 |
commit | 3e935e4fd23c06cdf67bd315280a0b1d637ad339 (patch) | |
tree | 3a735e336cd226df5a15b2ca2469d08c5be2607d | |
parent | e7737919e49e9b14649d5df4edb1534584527c40 (diff) | |
download | skhd-3e935e4fd23c06cdf67bd315280a0b1d637ad339.tar.gz skhd-3e935e4fd23c06cdf67bd315280a0b1d637ad339.zip |
code cleanup
-rw-r--r-- | src/hotload.c | 28 | ||||
-rw-r--r-- | src/hotload.h | 5 | ||||
-rw-r--r-- | src/skhd.c | 175 |
3 files changed, 102 insertions, 106 deletions
diff --git a/src/hotload.c b/src/hotload.c index 8f39b15..c0e8279 100644 --- a/src/hotload.c +++ b/src/hotload.c @@ -16,7 +16,8 @@ copy_string(const char *s) return result; } -char *file_directory(const char *file) +internal char * +file_directory(const char *file) { char *last_slash = strrchr(file, '/'); *last_slash = '\0'; @@ -25,13 +26,36 @@ char *file_directory(const char *file) return directory; } -char *file_name(const char *file) +internal char * +file_name(const char *file) { char *last_slash = strrchr(file, '/'); char *name = copy_string(last_slash + 1); return name; } +bool 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]; + + 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; + } + } + + free(filename); + free(directory); + } + + return success; +} + void hotloader_add_file(struct hotloader *hotloader, const char *file) { if(!hotloader->enabled) { diff --git a/src/hotload.h b/src/hotload.h index 9b2d666..8d012f2 100644 --- a/src/hotload.h +++ b/src/hotload.h @@ -31,9 +31,8 @@ struct hotloader 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); -char *file_directory(const char *file); -char *file_name(const char *file); +void hotloader_add_file(struct hotloader *hotloader, const char *file); +bool hotloader_watched_file(struct hotloader *hotloader, char *absolutepath); #endif @@ -30,6 +30,15 @@ extern bool CGSIsSecureEventInputSet(); #define secure_keyboard_entry_enabled CGSIsSecureEventInputSet +#if 0 +#define BEGIN_TIMED_BLOCK() \ + clock_t timed_block_begin = clock() +#define END_TIMED_BLOCK() \ + clock_t timed_block_end = clock(); \ + double timed_block_elapsed = (timed_block_end -timed_block_begin) / (double)CLOCKS_PER_SEC; \ + printf("elapsed time: %f\n", timed_block_elapsed) +#endif + internal unsigned major_version = 0; internal unsigned minor_version = 0; internal unsigned patch_version = 7; @@ -46,88 +55,65 @@ error(const char *format, ...) exit(EXIT_FAILURE); } -internal bool -watched_io_file(struct hotloader *hotloader, char *absolutepath) +internal void +warn(const char *format, ...) { - bool success = false; - for(unsigned index = 0; success == 0 && index < hotloader->watch_count; ++index) { - struct watched_file *watch_info = &hotloader->watch_list[index]; - - char *directory = file_directory(absolutepath); - char *filename = file_name(absolutepath); + va_list args; + va_start(args, format); + vfprintf(stderr, format, args); + va_end(args); +} - if(strcmp(watch_info->directory, directory) == 0) { - if(strcmp(watch_info->filename, filename) == 0) { - success = true; - } +internal void +parse_config_helper(char *absolutepath) +{ + struct parser parser; + if(parser_init(&parser, absolutepath)) { + parse_config(&parser, &hotkey_map); + if(parser.error) { + free_hotkeys(&hotkey_map); } - - free(filename); - free(directory); + parser_destroy(&parser); + } else { + warn("skhd: could not open file '%s'\n", absolutepath); } - - return success; } internal HOTLOADER_CALLBACK(hotloader_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(watched_io_file(hotloader, absolutepath)) { - /* TODO(koekeishiya): We sometimes get two events upon file save. - * Filter the duplicated event or something ?? */ - struct parser parser; - if(parser_init(&parser, absolutepath)) { - free_hotkeys(&hotkey_map); - parse_config(&parser, &hotkey_map); - if(parser.error) { - free_hotkeys(&hotkey_map); - } - parser_destroy(&parser); - } + if(hotloader_watched_file(hotloader, absolutepath)) { + free_hotkeys(&hotkey_map); + parse_config_helper(absolutepath); } } } -#if 0 -#define BEGIN_TIMED_BLOCK() \ - clock_t timed_block_begin = clock() -#define END_TIMED_BLOCK() \ - clock_t timed_block_end = clock(); \ - double timed_block_elapsed = (timed_block_end -timed_block_begin) / (double)CLOCKS_PER_SEC; \ - printf("elapsed time: %f\n", timed_block_elapsed) -#endif - - - internal EVENT_TAP_CALLBACK(key_handler) { - switch(type) - { - case kCGEventTapDisabledByTimeout: - case kCGEventTapDisabledByUserInput: - { - printf("skhd: restarting event-tap\n"); - struct event_tap *event_tap = (struct event_tap *) reference; - CGEventTapEnable(event_tap->handle, 1); - } break; - case kCGEventKeyDown: - { - uint32_t flags = CGEventGetFlags(event); - uint32_t key = CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode); - struct hotkey eventkey = { .flags = 0, .key = key }; - cgeventflags_to_hotkeyflags(flags, &eventkey); - bool result = find_and_exec_hotkey(&eventkey, &hotkey_map); - if(result) { - return NULL; - } - } break; - default: {} break; + switch(type) { + case kCGEventTapDisabledByTimeout: + case kCGEventTapDisabledByUserInput: + printf("skhd: restarting event-tap\n"); + struct event_tap *event_tap = (struct event_tap *) reference; + CGEventTapEnable(event_tap->handle, 1); + break; + case kCGEventKeyDown: + uint32_t flags = CGEventGetFlags(event); + uint32_t key = CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode); + struct hotkey eventkey = { .flags = 0, .key = key }; + cgeventflags_to_hotkeyflags(flags, &eventkey); + bool result = find_and_exec_hotkey(&eventkey, &hotkey_map); + if(result) { + return NULL; + } + break; } - return event; } @@ -136,25 +122,21 @@ parse_arguments(int argc, char **argv) { int option; const char *short_option = "vc:"; - struct option long_option[] = - { + struct option long_option[] = { { "version", no_argument, NULL, 'v' }, { "config", required_argument, NULL, 'c' }, { NULL, 0, NULL, 0 } }; while((option = getopt_long(argc, argv, short_option, long_option, NULL)) != -1) { - switch(option) - { - case 'v': - { - printf("skhd version %d.%d.%d\n", major_version, minor_version, patch_version); - return true; - } break; - case 'c': - { - config_file = strdup(optarg); - } break; + switch(option) { + case 'v': + printf("skhd version %d.%d.%d\n", major_version, minor_version, patch_version); + return true; + break; + case 'c': + config_file = strdup(optarg); + break; } } @@ -164,7 +146,7 @@ parse_arguments(int argc, char **argv) internal bool check_privileges() { - bool result = false; + bool result; const void *keys[] = { kAXTrustedCheckOptionPrompt }; const void *values[] = { kCFBooleanTrue }; @@ -183,16 +165,14 @@ check_privileges() internal void set_config_path() { - if(!config_file) { - char *home = getenv("HOME"); - if(home) { - int length = strlen(home) + strlen("/.skhdrc"); - config_file = (char *) malloc(length + 1); - strcpy(config_file, home); - strcat(config_file, "/.skhdrc"); - } else { - config_file = strdup(".skhdrc"); - } + char *home = getenv("HOME"); + if(home) { + int length = strlen(home) + strlen("/.skhdrc"); + config_file = (char *) malloc(length + 1); + strcpy(config_file, home); + strcat(config_file, "/.skhdrc"); + } else { + config_file = strdup(".skhdrc"); } } @@ -214,25 +194,18 @@ int main(int argc, char **argv) error("skhd: must be run with accessibility access.\n"); } - signal(SIGCHLD, SIG_IGN); - set_config_path(); - printf("skhd: using config '%s'\n", config_file); + if(!config_file) { + set_config_path(); + } table_init(&hotkey_map, - 128, + 131, (table_hash_func) hash_hotkey, (table_compare_func) same_hotkey); - struct parser parser; - if(parser_init(&parser, config_file)) { - parse_config(&parser, &hotkey_map); - if(parser.error) { - free_hotkeys(&hotkey_map); - } - parser_destroy(&parser); - } else { - error("skhd: could not open file '%s'\n", config_file); - } + printf("skhd: using config '%s'\n", config_file); + parse_config_helper(config_file); + signal(SIGCHLD, SIG_IGN); struct event_tap event_tap; event_tap.mask = (1 << kCGEventKeyDown); |