From 0e703c3f275f6d4477f452acfb9cf9c73b9d9f1e Mon Sep 17 00:00:00 2001 From: koekeishiya Date: Sat, 23 Feb 2019 14:49:56 +0100 Subject: #59 reload config when input source changes --- src/hashtable.h | 5 ++++- src/locale.c | 15 ++++++++------- src/locale.h | 8 ++++++++ src/skhd.c | 19 +++++++++++++++++++ 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/hashtable.h b/src/hashtable.h index 42d0166..f593432 100644 --- a/src/hashtable.h +++ b/src/hashtable.h @@ -76,7 +76,10 @@ void table_free(struct table *table) bucket = next; } } - free(table->buckets); + if (table->buckets) { + free(table->buckets); + table->buckets = NULL; + } } void *table_find(struct table *table, void *key) diff --git a/src/locale.c b/src/locale.c index b4c61dc..3c03f6a 100644 --- a/src/locale.c +++ b/src/locale.c @@ -83,6 +83,13 @@ cfstring_from_keycode(UCKeyboardLayout *keyboard_layout, CGKeyCode keycode) return NULL; } +uint32_t keycode_from_char(char key) +{ + char lookup_key[] = { key, '\0' }; + uint32_t keycode = (uint32_t) table_find(&keymap_table, &lookup_key); + return keycode; +} + #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wint-to-void-pointer-cast" bool initialize_keycode_map(void) @@ -94,6 +101,7 @@ bool initialize_keycode_map(void) UCKeyboardLayout *keyboard_layout = (UCKeyboardLayout *) CFDataGetBytePtr(uchr); if (!keyboard_layout) return false; + table_free(&keymap_table); table_init(&keymap_table, 131, (table_hash_func) hash_keymap, @@ -113,10 +121,3 @@ bool initialize_keycode_map(void) return true; } #pragma clang diagnostic pop - -uint32_t keycode_from_char(char key) -{ - char lookup_key[] = { key, '\0' }; - uint32_t keycode = (uint32_t) table_find(&keymap_table, &lookup_key); - return keycode; -} diff --git a/src/locale.h b/src/locale.h index d00a72e..7a1ec6e 100644 --- a/src/locale.h +++ b/src/locale.h @@ -3,6 +3,14 @@ #include +#define CF_NOTIFICATION_CALLBACK(name) \ + void name(CFNotificationCenterRef center, \ + void *observer, \ + CFNotificationName name, \ + const void *object, \ + CFDictionaryRef userInfo) +typedef CF_NOTIFICATION_CALLBACK(cf_notification_callback); + bool initialize_keycode_map(void); uint32_t keycode_from_char(char key); diff --git a/src/skhd.c b/src/skhd.c index de73ccd..77168c8 100644 --- a/src/skhd.c +++ b/src/skhd.c @@ -87,11 +87,23 @@ parse_config_helper(char *absolutepath) internal HOTLOADER_CALLBACK(config_handler) { BEGIN_TIMED_BLOCK("hotload_config"); + debug("skhd: config-file has been modified.. reloading config\n"); free_mode_map(&mode_map); parse_config_helper(absolutepath); END_TIMED_BLOCK(); } +internal CF_NOTIFICATION_CALLBACK(keymap_handler) +{ + BEGIN_TIMED_BLOCK("keymap_changed"); + if (initialize_keycode_map()) { + debug("skhd: input source changed.. reloading config\n"); + free_mode_map(&mode_map); + parse_config_helper(config_file); + } + END_TIMED_BLOCK(); +} + internal EVENT_TAP_CALLBACK(key_handler) { switch (type) { @@ -232,6 +244,13 @@ int main(int argc, char **argv) use_default_config_path(); } + CFNotificationCenterAddObserver(CFNotificationCenterGetDistributedCenter(), + NULL, + &keymap_handler, + kTISNotifySelectedKeyboardInputSourceChanged, + NULL, + CFNotificationSuspensionBehaviorCoalesce); + signal(SIGCHLD, SIG_IGN); init_shell(); table_init(&mode_map, 13, (table_hash_func) hash_mode, (table_compare_func) same_mode); -- cgit v1.2.3