config: Invert alias syntax

Swtich from

	<name> = <key>
to
	<key> = <name>

and confine keys to a single alias. This is arguably more intuitive
and reduces both config and implementation complexity.
master
Raheman Vaiya 4 years ago
parent b085898f16
commit 67a2af0f7f
  1. BIN
      data/keyd-application-mapper.1.gz
  2. BIN
      data/keyd.1.gz
  3. 36
      docs/keyd.scdoc
  4. 92
      src/config.c
  5. 10
      src/config.h

Binary file not shown.

Binary file not shown.

@ -279,21 +279,23 @@ E.G
## Aliases ## Aliases
A config file may include *aliases* which can be used as valid left hand values An *alias* may be assigned to a key and used as a valid left hand value.
in place of one or more keycodes. For example, the default alias *meta* is bound Multiple keys may be bound to a single alias, but _a given key may only be
to leftmeta and rightmeta. Thus the binding 'meta = a' is equivalent to the assigned to one alias at a time_. For example, the keys 'leftmeta' and
bindings 'leftmeta = a' and 'rightmeta = a'. 'rightmeta' are bound to the alias *meta* by default. Thus the binding 'meta =
a' is equivalent to the bindings 'leftmeta = a' and 'rightmeta = a'.
Aliases are defined in a special section called 'aliases' where each line takes the form: Aliases are defined in a special section called 'aliases' where each line takes
the form:
<name> = <key> <key> = <name>
and _<key>_ must be a valid key name. where _<key>_ must be a valid key name.
Note that <name> may itself be a valid key, in which case all references to the Note that <name> may itself be a valid key name, in which case all references
key will be replaced with the new key. When used judiciously, aliases can be to the key within the config file will be replaced with the new key. When used
used in conjunction with the include directive to share bindings between judiciously, aliases can be used in conjunction with the include directive to
keyboards with different physical layouts. share bindings between keyboards with different physical layouts.
For example: For example:
@ -318,16 +320,18 @@ For example:
004c:0267 004c:0267
[aliases] [aliases]
meta = leftalt leftalt = meta
meta = rightalt rightalt = meta
alt = leftmeta rightmeta = alt
alt = rightmeta leftmeta = alt
[main] [main]
include common include common
``` ```
Allows the user to define a set of universal bindings in /etc/keyd/common without having to explicitly account for the transposed meta and alt keys on an apple keyboard. Allows the user to define a set of universal bindings in /etc/keyd/common
without having to explicitly account for the transposed meta and alt keys within
the included config snippet.
## File Inclusion ## File Inclusion

@ -13,6 +13,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <libgen.h> #include <libgen.h>
@ -159,11 +160,9 @@ int set_layer_entry(const struct config *config, struct layer *layer,
size_t i; size_t i;
int found = 0; int found = 0;
for (i = 0; i < config->nr_aliases; i++) { for (i = 0; i < 256; i++) {
const struct alias *alias = &config->aliases[i]; if (!strcmp(config->aliases[i], key)) {
layer->keymap[i] = *d;
if (!strcmp(alias->name, key)) {
layer->keymap[alias->code] = *d;
found = 1; found = 1;
} }
} }
@ -284,18 +283,23 @@ static void config_init(struct config *config)
} }
for (i = 0; i < MAX_MOD; i++) { for (i = 0; i < MAX_MOD; i++) {
struct descriptor *ent1 = &km[modifier_table[i].code1]; const struct modifier_table_ent *mod = &modifier_table[i];
struct descriptor *ent2 = &km[modifier_table[i].code2];
struct descriptor *d1 = &km[mod->code1];
struct descriptor *d2 = &km[mod->code2];
int idx = config_get_layer_index(config, modifier_table[i].name); int idx = config_get_layer_index(config, mod->name);
assert(idx != -1); assert(idx != -1);
ent1->op = OP_LAYER; d1->op = OP_LAYER;
ent1->args[0].idx = idx; d1->args[0].idx = idx;
ent2->op = OP_LAYER; d2->op = OP_LAYER;
ent2->args[0].idx = idx; d2->args[0].idx = idx;
strcpy(config->aliases[mod->code1], mod->name);
strcpy(config->aliases[mod->code2], mod->name);
} }
/* In ms */ /* In ms */
@ -325,67 +329,28 @@ static void parse_globals(const char *path, struct config *config, struct ini_se
} }
} }
static void add_alias(struct config *config, const char *name, uint8_t code)
{
if (config->nr_aliases >= MAX_ALIASES) {
fprintf(stderr, "\tERROR: Max aliases (%d) exceeded\n",
MAX_ALIASES);
return;
}
struct alias *alias = &config->aliases[config->nr_aliases];
alias->name[sizeof(alias->name)-1] = 0;
strncpy(alias->name, name, sizeof(alias->name)-1);
alias->code = code;
config->nr_aliases++;
}
void create_modifier_aliases(struct config *config)
{
uint8_t aliased_mods[MAX_MOD] = { 0 };
size_t i;
for (i = 0; i < config->nr_aliases; i++) {
size_t j;
for (j = 0; j < MAX_MOD; j++) {
const struct modifier_table_ent *mod = &modifier_table[j];
const struct alias *alias = &config->aliases[i];
if (!strcmp(alias->name, mod->name))
aliased_mods[j] = 1;
}
}
for (i = 0; i < MAX_MOD; i++) {
const struct modifier_table_ent *mod = &modifier_table[i];
// Don't create modifier aliases for modifier names which are explicitly
// redefined by the user.
if (!aliased_mods[i]) {
add_alias(config, mod->name, mod->code1);
add_alias(config, mod->name, mod->code2);
}
}
}
static void parse_aliases(const char *path, struct config *config, struct ini_section *section) static void parse_aliases(const char *path, struct config *config, struct ini_section *section)
{ {
size_t i; size_t i;
for (i = 0; i < section->nr_entries;i++) { for (i = 0; i < section->nr_entries; i++) {
uint8_t code; uint8_t code;
struct ini_entry *ent = &section->entries[i]; struct ini_entry *ent = &section->entries[i];
if ((code = lookup_keycode(ent->val))) { if ((code = lookup_keycode(ent->key))) {
add_alias(config, ent->key, code); ssize_t len = strlen(ent->val);
if (len > MAX_ALIAS_LEN) {
fprintf(stderr,
"\tERROR: %s exceeds the maximum alias length (%d)\n",
ent->val, MAX_ALIAS_LEN);
} else {
strcpy(config->aliases[code], ent->val);
}
} else { } else {
fprintf(stderr, fprintf(stderr,
"\tERROR %s:%zd: Failed to define alias %s, %s is not a valid keycode\n", "\tERROR %s:%zd: Failed to define alias %s, %s is not a valid keycode\n",
path, ent->lnum, path, ent->lnum, ent->key, ent->val);
ent->key, ent->val);
} }
} }
} }
@ -422,7 +387,6 @@ int config_parse(struct config *config, const char *path)
} }
} }
create_modifier_aliases(config);
/* Populate each layer. */ /* Populate each layer. */
for (i = 0; i < ini->nr_sections; i++) { for (i = 0; i < ini->nr_sections; i++) {
size_t j; size_t j;

@ -14,19 +14,12 @@
#define MAX_AUX_DESCRIPTORS 64 #define MAX_AUX_DESCRIPTORS 64
#define MAX_ALIAS_LEN 32 #define MAX_ALIAS_LEN 32
#define MAX_ALIASES 64
#include "layer.h" #include "layer.h"
#include "descriptor.h" #include "descriptor.h"
#include "macro.h" #include "macro.h"
#include "command.h" #include "command.h"
struct alias {
char name[MAX_ALIAS_LEN];
uint8_t code;
};
struct config { struct config {
struct layer layers[MAX_LAYERS]; struct layer layers[MAX_LAYERS];
@ -34,9 +27,8 @@ struct config {
struct descriptor descriptors[MAX_AUX_DESCRIPTORS]; struct descriptor descriptors[MAX_AUX_DESCRIPTORS];
struct macro macros[MAX_MACROS]; struct macro macros[MAX_MACROS];
struct command commands[MAX_COMMANDS]; struct command commands[MAX_COMMANDS];
struct alias aliases[MAX_ALIASES]; char aliases[256][MAX_ALIAS_LEN+1];
size_t nr_aliases;
size_t nr_layers; size_t nr_layers;
size_t nr_macros; size_t nr_macros;
size_t nr_descriptors; size_t nr_descriptors;

Loading…
Cancel
Save