diff options
Diffstat (limited to '')
-rw-r--r-- | examples/skhdrc | 39 | ||||
-rw-r--r-- | src/hotkey.c | 37 | ||||
-rw-r--r-- | src/skhd.c | 2 |
3 files changed, 51 insertions, 27 deletions
diff --git a/examples/skhdrc b/examples/skhdrc index 411ceb3..2b0e6d8 100644 --- a/examples/skhdrc +++ b/examples/skhdrc @@ -34,23 +34,40 @@ # an EOL character signifies the end of the bind. # # -# NOTE(koekeishiya): Modal operations +# NOTE(koekeishiya): A mode is declared according to the following rules: # -# defines a new mode 'switcher' with an on_enter command -# :: switcher : chunkc border::color 0xff24ccaa +# mode_decl = '::' <name> '@' ':' <command> | '::' <name> ':' <command> | +# '::' <name> '@' | '::' <name> # -# defines a new mode named 'test' -# :: test +# name = desired name for this mode, # -# from 'default' mode, activate mode 'test' -# cmd - x ; test +# @ = capture keypresses regardless of being bound to an action # -# from 'test' mode, activate mode 'default' -# test < cmd - x ; default +# command = command is executed through '$SHELL -c' and +# follows valid shell syntax. if the $SHELL environment +# variable is not set, it will default to '/bin/bash'. +# when bash is used, the ';' delimeter can be specified +# to chain commands. # -# launch a new terminal instance when in either 'default' or 'test' mode -# default, test < cmd - return : open -na /Applications/Terminal.app +# to allow a command to extend into multiple lines, +# prepend '\' at the end of the previous line. +# +# an EOL character signifies the end of the bind. +# add an on_enter command to the default mode +# :: default : chunkc border::color 0xff775759 +# +# defines a new mode 'test' with an on_enter command, that captures keypresses +# :: test @ : chunkc border::color 0xff24ccaa +# +# from 'default' mode, activate mode 'test' +# cmd - x ; test +# +# from 'test' mode, activate mode 'default' +# test < cmd - x ; default +# +# launch a new terminal instance when in either 'default' or 'test' mode +# default, test < cmd - return : open -na /Applications/Terminal.app # open terminal, blazingly fast compared to iTerm/Hyper cmd - return : open -na /Applications/Kitty.app diff --git a/src/hotkey.c b/src/hotkey.c index a6d03b9..40894f1 100644 --- a/src/hotkey.c +++ b/src/hotkey.c @@ -83,7 +83,7 @@ unsigned long hash_mode(char *key) return hash; } -internal bool +internal void fork_and_exec(char *command) { local_persist char arg[] = "-c"; @@ -99,26 +99,32 @@ fork_and_exec(char *command) int status_code = execvp(exec[0], exec); exit(status_code); } +} - return true; +internal inline bool +passthrough(struct hotkey *hotkey) +{ + return !has_flags(hotkey, Hotkey_Flag_Passthrough); } -bool find_and_exec_hotkey(struct hotkey *eventkey, struct table *mode_map, struct mode **current_mode) +internal inline struct hotkey * +find_hotkey(struct mode *mode, struct hotkey *hotkey) { - struct hotkey *hotkey; - if ((hotkey = table_find(&(*current_mode)->hotkey_map, eventkey))) { - bool capture = (*current_mode)->capture || !has_flags(hotkey, Hotkey_Flag_Passthrough); - char *command = hotkey->command; + return table_find(&mode->hotkey_map, hotkey); +} - if (has_flags(hotkey, Hotkey_Flag_Activate)) { - *current_mode = table_find(mode_map, hotkey->command); - command = (*current_mode)->command; - if (!command) return capture; +bool find_and_exec_hotkey(struct hotkey *k, struct table *t, struct mode **m) +{ + bool c = (*m)->capture; + for (struct hotkey *h = find_hotkey(*m, k); h; c |= passthrough(h), h = 0) { + char *cmd = h->command; + if (has_flags(h, Hotkey_Flag_Activate)) { + *m = table_find(t, cmd); + cmd = (*m)->command; } - - if (fork_and_exec(command)) return capture; + if (cmd) fork_and_exec(cmd); } - return false; + return c; } void free_mode_map(struct table *mode_map) @@ -137,7 +143,7 @@ void free_mode_map(struct table *mode_map) for (int i = 0; i < buf_len(freed_pointers); ++i) { if (freed_pointers[i] == hotkey) { - continue; + goto next; } } @@ -145,6 +151,7 @@ void free_mode_map(struct table *mode_map) buf_free(hotkey->mode_list); free(hotkey->command); free(hotkey); +next:; } if (hk_count) free(hotkeys); @@ -29,7 +29,7 @@ extern bool CGSIsSecureEventInputSet(); #define secure_keyboard_entry_enabled CGSIsSecureEventInputSet -#if 0 +#if 1 #define BEGIN_TIMED_BLOCK() \ clock_t timed_block_begin = clock() #define END_TIMED_BLOCK() \ |