Skip to content

Commit

Permalink
implement key and modifier alias koekeishiya#288
Browse files Browse the repository at this point in the history
  • Loading branch information
erics118 committed Jul 7, 2023
2 parents 686de2d + 9757d56 commit 03b7e55
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 110 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,25 @@ command = command is executed through '$SHELL -c' and
an EOL character signifies the end of the bind.
```

Aliases can also be used anywhere a modifier or a key is expected:
```
# alias as modifier
.alias $hyper cmd + alt + ctrl
$hyper - t : open -a Terminal.app
# alias as key
.alias $capslock 0x39
ctrl - $capslock : open -a Notes.app
# alias as mod-key
.alias $exclamation_mark shift - 1
$hyper - $exclamation_mark : open -a "System Preferences.app"
# alias within alias
.alias $terminal_key $hyper + shift - t
$terminal_key : open -a Terminal.app
```

General options that configure the behaviour of **skhd**:
```
# specify a file that should be included as an additional config-file.
Expand Down
19 changes: 19 additions & 0 deletions examples/skhdrc
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,22 @@ cmd + shift - return : ~/Scripts/qtb.sh

# open mpv
cmd - m : open -na /Applications/mpv.app $(pbpaste)

###################
# using aliases

# alias as modifier
.alias $hyper cmd + alt + ctrl
$hyper - t : open -a Terminal.app

# alias as key
.alias $capslock 0x39
ctrl - $capslock : open -a Notes.app

# alias as mod-key
.alias $exclamation_mark shift - 1
$hyper - $exclamation_mark : open -a "System Preferences.app"

# alias within alias
.alias $terminal_key $hyper + shift - t
$terminal_key : open -a Terminal.app
14 changes: 12 additions & 2 deletions src/hashtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ void *table_find(struct table *table, void *key)
return bucket ? bucket->value : NULL;
}

void table_add(struct table *table, void *key, void *value)
void table_newkeyvalue(struct table *table, void *key, void *value, bool do_replace)
{
struct bucket **bucket = table_get_bucket(table, key);
if (*bucket) {
if (!(*bucket)->value) {
if (do_replace || !(*bucket)->value) {
(*bucket)->value = value;
}
} else {
Expand All @@ -101,6 +101,16 @@ void table_add(struct table *table, void *key, void *value)
}
}

void table_add(struct table *table, void *key, void *value)
{
table_newkeyvalue(table, key, value, false);
}

void table_replace(struct table *table, void *key, void *value)
{
table_newkeyvalue(table, key, value, true);
}

void *table_remove(struct table *table, void *key)
{
void *result = NULL;
Expand Down
22 changes: 16 additions & 6 deletions src/hotkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,20 +219,30 @@ next:;
free(mode);
}

if (mode_count) {
free(modes);
buf_free(freed_pointers);
}
free(modes);
buf_free(freed_pointers);
}

void free_blacklist(struct table *blacklst)
{
int count;
void **items = table_reset(blacklst, &count);
for (int index = 0; index < count; ++index) {
char *item = (char *) items[index];
free(item);
free(items[index]);
}

free(items);
}

void free_alias_map(struct table *alias_map)
{
int count;
void **items = table_reset(alias_map, &count);
for (int index = 0; index < count; ++index) {
free(items[index]);
}

free(items);
}

static void
Expand Down
2 changes: 2 additions & 0 deletions src/hotkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ enum hotkey_flag
Hotkey_Flag_LControl = (1 << 10),
Hotkey_Flag_RControl = (1 << 11),
Hotkey_Flag_Fn = (1 << 12),
Hotkey_Flag_Modifier = ((Hotkey_Flag_Fn << 1) - 1),
Hotkey_Flag_Passthrough = (1 << 13),
Hotkey_Flag_Activate = (1 << 14),
Hotkey_Flag_NX = (1 << 15),
Expand Down Expand Up @@ -117,6 +118,7 @@ bool intercept_systemkey(CGEventRef event, struct hotkey *eventkey);
bool find_and_exec_hotkey(struct hotkey *k, struct table *t, struct mode **m, struct carbon_event *carbon);
void free_mode_map(struct table *mode_map);
void free_blacklist(struct table *blacklst);
void free_alias_map(struct table *alias_map);

void init_shell(void);

Expand Down
Loading

0 comments on commit 03b7e55

Please sign in to comment.