diff --git a/data/keyd-application-mapper.1.gz b/data/keyd-application-mapper.1.gz index 8213c3f..a3e5b22 100644 Binary files a/data/keyd-application-mapper.1.gz and b/data/keyd-application-mapper.1.gz differ diff --git a/data/keyd.1.gz b/data/keyd.1.gz index 927d705..f6652fb 100644 Binary files a/data/keyd.1.gz and b/data/keyd.1.gz differ diff --git a/docs/keyd.scdoc b/docs/keyd.scdoc index 8446ce3..21006de 100644 --- a/docs/keyd.scdoc +++ b/docs/keyd.scdoc @@ -279,21 +279,23 @@ E.G ## Aliases -A config file may include *aliases* which can be used as valid left hand values -in place of one or more keycodes. For example, the default alias *meta* is bound -to leftmeta and rightmeta. Thus the binding 'meta = a' is equivalent to the -bindings 'leftmeta = a' and 'rightmeta = a'. +An *alias* may be assigned to a key and used as a valid left hand value. +Multiple keys may be bound to a single alias, but _a given key may only be +assigned to one alias at a time_. For example, the keys 'leftmeta' and +'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: - = + = -and __ must be a valid key name. +where __ must be a valid key name. -Note that may itself be a valid key, in which case all references to the -key will be replaced with the new key. When used judiciously, aliases can be -used in conjunction with the include directive to share bindings between -keyboards with different physical layouts. +Note that may itself be a valid key name, in which case all references +to the key within the config file will be replaced with the new key. When used +judiciously, aliases can be used in conjunction with the include directive to +share bindings between keyboards with different physical layouts. For example: @@ -318,16 +320,18 @@ For example: 004c:0267 [aliases] - meta = leftalt - meta = rightalt - alt = leftmeta - alt = rightmeta + leftalt = meta + rightalt = meta + rightmeta = alt + leftmeta = alt [main] 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 diff --git a/src/config.c b/src/config.c index 47d592a..7314f0a 100644 --- a/src/config.c +++ b/src/config.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -159,11 +160,9 @@ int set_layer_entry(const struct config *config, struct layer *layer, size_t i; int found = 0; - for (i = 0; i < config->nr_aliases; i++) { - const struct alias *alias = &config->aliases[i]; - - if (!strcmp(alias->name, key)) { - layer->keymap[alias->code] = *d; + for (i = 0; i < 256; i++) { + if (!strcmp(config->aliases[i], key)) { + layer->keymap[i] = *d; found = 1; } } @@ -284,18 +283,23 @@ static void config_init(struct config *config) } for (i = 0; i < MAX_MOD; i++) { - struct descriptor *ent1 = &km[modifier_table[i].code1]; - struct descriptor *ent2 = &km[modifier_table[i].code2]; + const struct modifier_table_ent *mod = &modifier_table[i]; + + 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); - ent1->op = OP_LAYER; - ent1->args[0].idx = idx; + d1->op = OP_LAYER; + d1->args[0].idx = idx; - ent2->op = OP_LAYER; - ent2->args[0].idx = idx; + d2->op = OP_LAYER; + d2->args[0].idx = idx; + + strcpy(config->aliases[mod->code1], mod->name); + strcpy(config->aliases[mod->code2], mod->name); } /* 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) { size_t i; - for (i = 0; i < section->nr_entries;i++) { + for (i = 0; i < section->nr_entries; i++) { uint8_t code; struct ini_entry *ent = §ion->entries[i]; - if ((code = lookup_keycode(ent->val))) { - add_alias(config, ent->key, code); + if ((code = lookup_keycode(ent->key))) { + 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 { fprintf(stderr, "\tERROR %s:%zd: Failed to define alias %s, %s is not a valid keycode\n", - path, ent->lnum, - ent->key, ent->val); + path, ent->lnum, ent->key, ent->val); } } } @@ -422,7 +387,6 @@ int config_parse(struct config *config, const char *path) } } - create_modifier_aliases(config); /* Populate each layer. */ for (i = 0; i < ini->nr_sections; i++) { size_t j; diff --git a/src/config.h b/src/config.h index 6c536d9..aecc973 100644 --- a/src/config.h +++ b/src/config.h @@ -14,19 +14,12 @@ #define MAX_AUX_DESCRIPTORS 64 #define MAX_ALIAS_LEN 32 -#define MAX_ALIASES 64 #include "layer.h" #include "descriptor.h" #include "macro.h" #include "command.h" - -struct alias { - char name[MAX_ALIAS_LEN]; - uint8_t code; -}; - struct config { struct layer layers[MAX_LAYERS]; @@ -34,9 +27,8 @@ struct config { struct descriptor descriptors[MAX_AUX_DESCRIPTORS]; struct macro macros[MAX_MACROS]; 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_macros; size_t nr_descriptors;