Skip to content

Commit

Permalink
Allow for multiple chains per command
Browse files Browse the repository at this point in the history
  • Loading branch information
sambazley committed Dec 29, 2020
1 parent fe241b0 commit 061a467
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -20,6 +20,7 @@ The format of the configuration file supports a simple notation for mapping mult
XF86LaunchB
xdotool selectwindow | xsel -bi

super + {Left,Down,Up,Right}
super + {h,j,k,l}
bspc node -f {west,south,north,east}

Expand All @@ -29,6 +30,7 @@ The format of the configuration file supports a simple notation for mapping mult
super + {alt,ctrl,alt + ctrl} + XF86Eject
sudo systemctl {suspend,reboot,poweroff}

super + {_,shift + }{Left,Down,Up,Right}
super + {_,shift + }{h,j,k,l}
bspc node -{f,s} {west,south,north,east}

Expand Down
2 changes: 2 additions & 0 deletions doc/sxhkd.1.asciidoc
Expand Up @@ -113,6 +113,8 @@ In addition, the sequences can contain ranges of the form _A_-_Z_ where _A_ and

The underscore character represents an empty sequence element.

Multiple hotkeys can be provided to execute the same command. Each hotkey should be on a separate line with no indentation.

Author
------

Expand Down
39 changes: 32 additions & 7 deletions src/parse.c
Expand Up @@ -2376,7 +2376,8 @@ void load_config(const char *config_file)
err("Can't open configuration file: '%s'.\n", config_file);

char buf[3 * MAXLEN];
char chain[MAXLEN] = {0};
char **chains = 0;
int nchains = 0;
char command[2 * MAXLEN] = {0};
int offset = 0;
char first;
Expand All @@ -2392,10 +2393,22 @@ void load_config(const char *config_file)
char *end = rgraph(buf);
*(end + 1) = '\0';

if (isgraph(first))
snprintf(chain + offset, sizeof(chain) - offset, "%s", start);
else
if (isgraph(first)) {
char **chain;
size_t chain_len = end - start + offset + 2;
if (offset == 0) {
chains = realloc(chains, ++nchains * sizeof(char *));
chain = &chains[nchains - 1];
*chain = malloc(chain_len);
} else {
chain = &chains[nchains - 1];
*chain = realloc(*chain, chain_len);
}

snprintf(*chain + offset, chain_len - offset, "%s", start);
} else {
snprintf(command + offset, sizeof(command) - offset, "%s", start);
}

if (*end == PARTIAL_LINE) {
offset += end - start;
Expand All @@ -2404,14 +2417,26 @@ void load_config(const char *config_file)
offset = 0;
}

if (isspace(first) && strlen(chain) > 0 && strlen(command) > 0) {
process_hotkey(chain, command);
chain[0] = '\0';
if (isspace(first) && nchains > 0 && strlen(command) > 0) {
for (int i = 0; i < nchains; i++) {
process_hotkey(chains[i], command);
free(chains[i]);
}
free(chains);
chains = 0;
nchains = 0;
command[0] = '\0';
}
}
}

if (chains) {
for (int i = 0; i < nchains; i++) {
free(chains[i]);
}
free(chains);
}

fclose(cfg);
}

Expand Down

0 comments on commit 061a467

Please sign in to comment.