Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for multiple chains per command #221

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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