Skip to content

Commit c7aff2a

Browse files
committed
Change default layer names used by modifiers for consistency.
e.g alt = layer(A) -> alt = layer(alt) Additionally, left hand modifier names are shorthand for both associated keycodes as opposed to just the left one. e.g: control = esc is equivalent to: leftcontrol = esc rightcontrol = esc
1 parent 6819c1c commit c7aff2a

File tree

11 files changed

+238
-149
lines changed

11 files changed

+238
-149
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@ following config:
132132

133133
[main]
134134

135-
leftshift = oneshot(S)
136-
leftalt = oneshot(A)
137-
rightalt = oneshot(G)
138-
rightshift = oneshot(A)
139-
leftmeta = oneshot(M)
140-
rightmeta = oneshot(M)
135+
shift = oneshot(shift)
136+
meta = oneshot(meta)
137+
control = oneshot(control)
138+
139+
leftalt = oneshot(alt)
140+
rightalt = oneshot(altgr)
141141

142142
capslock = overload(C, esc)
143143
insert = S-insert

keyd.1.gz

55 Bytes
Binary file not shown.

man.md

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ for the duration of the corresponding key stroke.
135135
The *layout* is a special kind of layer from which mappings are drawn if no
136136
other layers are active. By default all keys are mapped to themselves within a
137137
layout. Every config has at least one layout called *main*, but additional
138-
layouts may be defined and subsequently activated using the `layout()` action.
138+
layouts may be defined and subsequently activated using the `layout()` action.
139139

140140
Layouts also have the additional property of being affected by the active modifier
141141
set. That is, unlike layouts, key sequences mapped within them are not
@@ -185,19 +185,30 @@ By default each modifier key is mapped to an eponymously named modifier layer.
185185

186186
Thus the above config can be shortened to:
187187

188-
[A]
188+
[alt]
189189

190190
1 = C-A-f1
191191

192-
since leftalt and rightalt are already assigned to `layer(A)`.
192+
since leftalt and rightalt are already assigned to `layer(alt)`.
193193

194-
Additionally, any set of valid modifiers is also a valid layer. For
195-
example, the layer `M-C` corresponds to a layer which behaves like the
196-
modifiers meta and control, which means the following:
194+
Additionally, left hand values which are modifier names are expanded to both
195+
associated keycodes.
196+
197+
E.G
198+
control = esc
199+
200+
is the equivalent of
201+
202+
rightcontrol = esc
203+
leftcontrol = esc
204+
205+
Finally any set of valid modifiers is also a valid layer. For example, the
206+
layer `M-C` corresponds to a layer which behaves like the modifiers meta and
207+
control, which means the following:
197208

198209
capslock = layer(M-A)
199210

200-
will cause capslock to behave as meta and alt when held.
211+
will cause capslock to behave as meta and alt when held.
201212

202213
### Lookup Rules
203214

@@ -239,7 +250,7 @@ distinct from `layout()` which should be used for letter layouts.
239250

240251
: Activates the given layer while held and emits the given key sequence when
241252
tapped. A timeout in milliseconds may optionally be supplied to disambiguate
242-
a tap and a hold.
253+
a tap and a hold.
243254

244255
If a timeout is present depression of the corresponding key is only interpreted
245256
as a layer activation in the event that it is sustained for more than
@@ -319,9 +330,6 @@ Invert the behaviour of the shift key without breaking modifier behaviour.
319330
*
320331

321332
[main]
322-
leftshift = layer(shift)
323-
rightshift = layer(shift)
324-
325333
1 = !
326334
2 = @
327335
3 = #
@@ -333,7 +341,7 @@ Invert the behaviour of the shift key without breaking modifier behaviour.
333341
9 = (
334342
0 = )
335343

336-
[shift:S]
344+
[shift]
337345
0 = 0
338346
1 = 1
339347
2 = 2
@@ -353,10 +361,12 @@ activates it until it is pressed again, and holding it produces expected
353361
behaviour.
354362

355363
[main]
364+
356365
leftcontrol = oneshot(control)
357366
rightcontrol = oneshot(control)
358367

359368
[control:C]
369+
360370
toggle(control)
361371

362372
# Example 4
@@ -365,14 +375,12 @@ Meta behaves as normal except when \` is pressed, after which the alt_tab layer
365375
is activated for the duration of the leftmeta keypress. Subsequent actuations
366376
of \` will thus produce A-tab instead of M-\`.
367377

368-
[main]
369-
leftmeta = layer(meta)
370-
rightmeta = layer(meta)
378+
[meta]
371379

372-
[meta:M]
373380
` = swap(alt_tab, A-tab)
374381

375382
[alt_tab:A]
383+
376384
tab = A-S-tab
377385
` = A-tab
378386

src/config.c

Lines changed: 79 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ static int parse_kvp(char *s, char **key, char **value)
5050
char *last_space = NULL;
5151
char *c = s;
5252

53-
if (*c == '=') //Allow the first character to be = as a special case.
53+
/* Allow the first character to be = as a special case. */
54+
if (*c == '=')
5455
c++;
5556

5657
while (*c) {
@@ -263,35 +264,60 @@ static void parse_id_section(struct config *config, struct ini_section *section)
263264
}
264265
}
265266

267+
struct config *create_config(const char *name)
268+
{
269+
size_t i;
270+
struct layer *main;
271+
struct config *config;
272+
273+
config = calloc(1, sizeof(struct config));
274+
strncpy(config->name, name, sizeof(config->name)-1);
275+
276+
main = create_layer("main", 0, 1);
277+
278+
config->nr_layers = 0;
279+
config->layers[config->nr_layers++] = main;
280+
281+
for (i = 0; i < sizeof(modifier_table)/sizeof(modifier_table[0]); i++) {
282+
struct modifier_table_ent *m = &modifier_table[i];
283+
284+
struct layer *layer;
285+
struct descriptor d;
286+
287+
layer = create_layer(m->name, m->mask, 0);
288+
289+
d.op = OP_LAYER;
290+
d.args[0].layer = layer;
291+
292+
layer_set_descriptor(main, m->code1, &d);
293+
layer_set_descriptor(main, m->code2, &d);
294+
295+
config->layers[config->nr_layers++] = layer;
296+
}
297+
298+
return config;
299+
}
300+
266301
/*
267302
* Ownership of the input string is forfeit, it will eventually be freed when
268303
* the config is destroyed.
269304
*/
270305

271-
static int parse_config(const char *config_name, char *str, struct config *config)
306+
static int parse_config(struct config *config, char *str)
272307
{
273308
size_t i, j;
274309

275310
struct ini ini;
276311

277312
struct ini_section *layer_sections[MAX_LAYERS];
278313
struct layer *layers[MAX_LAYERS];
279-
size_t nr_layers = 0;
280-
314+
size_t n = 0;
281315

282316
if (ini_parse(str, &ini, NULL) < 0) {
283-
fprintf(stderr, "ERROR: %s is not a valid config (missing [main]?)\n", config_name);
317+
fprintf(stderr, "ERROR: %s is not a valid config\n", config->name);
284318
return -1;
285319
}
286320

287-
config->nr_layers = 1;
288-
config->nr_device_ids = 0;
289-
290-
config->layers[0] = create_layer("main", 0, 1);
291-
292-
assert(strlen(config_name) < MAX_CONFIG_NAME);
293-
strcpy(config->name, config_name);
294-
295321
/*
296322
* First pass, create layers so they are available
297323
* for lookup during descriptor parsing.
@@ -311,42 +337,46 @@ static int parse_config(const char *config_name, char *str, struct config *confi
311337
if (parse_header(section->name, name, type) < 0) {
312338
fprintf(stderr,
313339
"ERROR %s:%zu: Invalid header.\n",
314-
config_name,
340+
config->name,
315341
section->lnum);
316342
continue;
317343
}
318344

319345
layer = lookup_layer(name, (void*)config);
320346

321-
if (!layer) { //If the layer doesn't exist, create it.
322-
if (!strcmp(type, "layout")) {
347+
if (!layer) { /* If the layer doesn't exist, create it. */
348+
if (!strcmp(type, "layout"))
323349
layer = create_layer(name, 0, 1);
324-
} else if (!parse_modset(type, &mods)) {
350+
else if (!parse_modset(type, &mods))
325351
layer = create_layer(name, mods, 0);
326-
} else if (strcmp(type, "")) {
352+
else if (strcmp(type, "")) {
327353
fprintf(stderr,
328354
"WARNING %s:%zu: \"%s\" is not a valid layer type "
329355
" (must be \"layout\" or a valid modifier set).\n",
330-
config_name, section->lnum, type);
356+
config->name, section->lnum, type);
331357
continue;
332358
}
333359

334360
config->layers[config->nr_layers++] = layer;
335361
}
336362

337-
layers[nr_layers] = layer;
338-
layer_sections[nr_layers++] = section;
363+
layers[n] = layer;
364+
layer_sections[n] = section;
365+
366+
n++;
339367
}
340368

341-
/* Parse each entry section entry and build the layer keymap. */
369+
/* Parse each section entry and build the layer keymap. */
342370

343-
for (i = 0; i < nr_layers; i++) {
371+
for (i = 0; i < n; i++) {
344372
struct ini_section *section = layer_sections[i];
345373
struct layer *layer = layers[i];
346374

347375
/* Populate the layer described by the section. */
348376

349377
for (j = 0; j < section->nr_entries; j++) {
378+
size_t i;
379+
int is_mod;
350380
struct ini_entry *ent = &section->entries[j];
351381

352382
char *k, *v;
@@ -357,97 +387,58 @@ static int parse_config(const char *config_name, char *str, struct config *confi
357387
if (parse_kvp(ent->line, &k, &v) < 0) {
358388
fprintf(stderr,
359389
"ERROR %s:%zu: Invalid key value pair.\n",
360-
config_name, ent->lnum);
361-
continue;
362-
}
363-
364-
code = lookup_keycode(k);
365-
366-
if (!code) {
367-
fprintf(stderr,
368-
"ERROR %s:%zu: %s is not a valid key.\n",
369-
config_name, ent->lnum, k);
390+
config->name, ent->lnum);
370391
continue;
371392
}
372393

373394
if (parse_descriptor(v, &desc, lookup_layer, config) < 0) {
374-
fprintf(stderr, "ERROR %s:%zu: %s\n", config_name,
395+
fprintf(stderr, "ERROR %s:%zu: %s\n", config->name,
375396
ent->lnum, errstr);
376397
continue;
377398
}
378399

379-
layer_set_descriptor(layer, code, &desc);
380-
}
381-
}
382-
383-
return 0;
384-
}
400+
is_mod = 0;
401+
for (i = 0; i < sizeof(modifier_table)/sizeof(modifier_table[0]); i++) {
402+
struct modifier_table_ent *m = &modifier_table[i];
385403

386-
static void post_process_config(const struct config *config)
387-
{
388-
size_t i;
389-
uint16_t code;
390-
391-
/*
392-
* Convert all modifier keycodes into their corresponding layer
393-
* counterparts for consistency. This allows us to avoid explicitly
394-
* accounting for modifier layer/modifier keycode overlap within the
395-
* remapping logic and provides the user the ability to remap stock
396-
* modifiers using their eponymous layer names.
397-
*/
404+
if (!strcmp(m->name, k)) {
405+
layer_set_descriptor(layer, m->code1, &desc);
406+
layer_set_descriptor(layer, m->code2, &desc);
398407

399-
for (i = 0; i < config->nr_layers; i++) {
400-
struct layer *layer = config->layers[i];
401-
402-
for (code = 0; code < KEY_MAX; code++) {
403-
struct descriptor *d = layer_get_descriptor(layer, code);
404-
405-
if (d && d->op == OP_KEYSEQ) {
406-
uint16_t key = d->args[0].sequence.code;
407-
struct layer *modlayer = NULL;
408-
409-
switch(key) {
410-
case KEY_RIGHTSHIFT:
411-
case KEY_LEFTSHIFT:
412-
modlayer = lookup_layer("S", (void*)config);
413-
break;
414-
case KEY_RIGHTALT:
415-
modlayer = lookup_layer("G", (void*)config);
416-
break;
417-
case KEY_RIGHTCTRL:
418-
case KEY_LEFTCTRL:
419-
modlayer = lookup_layer("C", (void*)config);
420-
break;
421-
case KEY_RIGHTMETA:
422-
case KEY_LEFTMETA:
423-
modlayer = lookup_layer("M", (void*)config);
424-
break;
425-
case KEY_LEFTALT:
426-
modlayer = lookup_layer("A", (void*)config);
408+
is_mod++;
427409
break;
428410
}
411+
}
429412

430-
if (modlayer) {
431-
d->op = OP_LAYER;
432-
d->args[0].layer = modlayer;
433-
}
413+
if (is_mod)
414+
continue;
415+
416+
code = lookup_keycode(k);
417+
418+
if (!code) {
419+
fprintf(stderr,
420+
"ERROR %s:%zu: %s is not a valid key.\n",
421+
config->name, ent->lnum, k);
422+
continue;
434423
}
435424

425+
layer_set_descriptor(layer, code, &desc);
426+
436427
}
437428
}
429+
430+
return 0;
438431
}
439432

440433
struct config *add_config(const char *config_name, char *str)
441434
{
442-
struct config *config = malloc(sizeof(struct config));
435+
struct config *config = create_config(config_name);
443436

444-
if (parse_config(config_name, str, config) < 0) {
437+
if (parse_config(config, str) < 0) {
445438
free(config);
446439
return NULL;
447440
}
448441

449-
post_process_config(config);
450-
451442
config->next = configs;
452443
configs = config;
453444

0 commit comments

Comments
 (0)