commit 19213688d10f61e37d6aa53ac6497bc1e11c0014
parent da06f514df730a6a5a994954d17b970a2f6c1f1e
Author: Maarten van Gompel <proycon@anaproy.nl>
Date: Tue, 24 Aug 2021 14:26:34 +0200
allow supplying layers through command line parameter or env var
Diffstat:
3 files changed, 70 insertions(+), 19 deletions(-)
diff --git a/keyboard.h b/keyboard.h
@@ -1,3 +1,5 @@
+#define MAX_LAYERS 25
+
enum key_type;
enum key_modifier_type;
struct clr_scheme;
@@ -80,8 +82,8 @@ struct kbd {
struct layout *prevlayout;
size_t layer_index;
- enum layout_ids *layers;
- struct layout **layouts;
+ struct layout *layouts;
+ enum layout_id *layers;
struct drwsurf *surf;
struct zwp_virtual_keyboard_v1 *vkbd;
@@ -91,7 +93,7 @@ static inline void draw_inset(struct drwsurf *d, uint32_t x, uint32_t y,
uint32_t width, uint32_t height, uint32_t border,
uint32_t color);
-static void kbd_init(struct kbd *kb);
+static void kbd_init(struct kbd *kb, struct layout * layouts, char * layer_names_list);
static void kbd_init_layout(struct layout *l, uint32_t width, uint32_t height);
static struct key *kbd_get_key(struct kbd *kb, uint32_t x, uint32_t y);
static void kbd_unpress_key(struct kbd *kb, uint32_t time);
@@ -132,8 +134,60 @@ kbd_get_rows(struct layout *l) {
}
-void kbd_init(struct kbd *kb) {
- kb->layout = kb->layouts[kb->layer_index];
+void kbd_init(struct kbd *kb, struct layout * layouts, char * layer_names_list) {
+ char *s;
+ int i;
+ bool found;
+
+ fprintf(stderr, "Initializing keyboard\n");
+
+ kb->layouts = layouts;
+
+ struct layout * l = layouts;
+ for (i = 0; i < NumLayouts - 1; i++);
+ fprintf(stderr, "Found %d layers\n",i);
+
+ kb->layer_index = 0;
+
+ if (layer_names_list) {
+ uint8_t numlayers = 0;
+ kb->layers = malloc(MAX_LAYERS * sizeof(enum layout_id));
+ s = strtok(layer_names_list, ",");
+ while (s != NULL) {
+ if (numlayers + 1 == MAX_LAYERS) {
+ fprintf(stderr, "too many layers specified");
+ exit(3);
+ }
+ found = false;
+ for (i = 0; i < NumLayouts - 1; i++) {
+ if (kb->layouts[i].name && strcmp(kb->layouts[i].name, s) == 0) {
+ fprintf(stderr, "layer #%d = %s\n", numlayers + 1, s);
+ kb->layers[numlayers++] = i;
+ found = true;
+ break;
+ }
+ }
+ if (!found) {
+ fprintf(stderr, "No such layer: %s\n", s);
+ exit(3);
+ }
+ s = strtok(NULL,",");
+ }
+ kb->layers[numlayers] = NumLayouts; //mark the end of the sequence
+ if (numlayers == 0) {
+ fprintf(stderr, "No layers defined\n");
+ exit(3);
+ }
+ }
+
+ i = 0;
+ enum layout_id lid = kb->layers[0];
+ while (lid != NumLayouts) {
+ lid = kb->layers[++i];
+ }
+ fprintf(stderr, "Found %d layers\n",i);
+
+ kb->layout = &kb->layouts[kb->layers[kb->layer_index]];
/* upload keymap */
create_and_upload_keymap(kb->layout->keymap_name, 0, 0);
@@ -142,7 +196,7 @@ void kbd_init(struct kbd *kb) {
void
kbd_init_layout(struct layout *l, uint32_t width, uint32_t height) {
uint32_t x = 0, y = 0;
- fprintf(stderr, "Init layout\n");
+ fprintf(stderr, "Init layout %s\n", l->name);
uint8_t rows = kbd_get_rows(l);
l->keyheight = height / rows;
@@ -157,8 +211,6 @@ kbd_init_layout(struct layout *l, uint32_t width, uint32_t height) {
} else if (k->width > 0) {
k->x = x;
k->y = y;
- fprintf(stderr, "(%d/%f)*%f -> %s\n", width, rowlength, k->width,
- k->label);
k->w = ((double)width / rowlength) * k->width;
x += k->w;
}
@@ -291,7 +343,7 @@ kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) {
if (kb->layers[kb->layer_index] == NumLayouts) {
kb->layer_index = 0;
}
- kbd_switch_layout(kb, kb->layouts[kb->layer_index]);
+ kbd_switch_layout(kb, &kb->layouts[kb->layers[kb->layer_index]]);
break;
case BackLayer:
//switch to the previously active layout
@@ -351,7 +403,7 @@ kbd_print_key_stdout(struct kbd *kb, struct key *k) {
if (!handled) {
if ((kb->mods & Shift) || (kb->mods & CapsLock))
printf("%s", k->shift_label);
- else if (!(kb->mods & Ctrl) && !(kb->mods & Alt) && (!kb->mods & Super))
+ else if (!(kb->mods & Ctrl) && !(kb->mods & Alt) && !(kb->mods & Super))
printf("%s", k->label);
}
fflush(stdout);
@@ -362,7 +414,7 @@ kbd_draw_key(struct kbd *kb, struct key *k, bool pressed) {
struct drwsurf *d = kb->surf;
const char *label = (kb->mods & Shift) ? k->shift_label : k->label;
fprintf(stderr, "Draw key +%d+%d %dx%d -> %s\n", k->x, k->y, k->w, k->h,
- k->label);
+ label);
struct clr_scheme *scheme = (k->scheme == 0) ? &(kb->scheme) : &(kb->scheme1);
Color *fill = pressed ? &scheme->high : &scheme->fg;
draw_inset(d, k->x, k->y, k->w, k->h, KBD_KEY_BORDER, fill->color);
diff --git a/layout.mobintl.h b/layout.mobintl.h
@@ -10,7 +10,7 @@
#define KBD_KEY_BORDER 2
/* layout declarations */
-enum layout_ids {
+enum layout_id {
Full = 0,
Special,
Simple,
@@ -58,7 +58,7 @@ enum layout_ids {
};
/* layers is an ordered list of layouts, used to cycle through */
-enum layout_ids layers[] = {
+static enum layout_id layers[] = {
Full, //First layout is the default layout on startup
Special,
Simple,
@@ -138,7 +138,6 @@ static struct layout layouts[NumLayouts] = {
/* keyboard settings */
static struct kbd keyboard = {
/* default layout */
- .layouts = &layouts,
.layers = &layers,
.layer_index = 0,
.scheme =
@@ -256,7 +255,7 @@ static struct key keys_full[] = {
{",", "'", 1.0, Code, KEY_COMMA},
{"", "", 4.0, Code, KEY_SPACE},
{".", "?", 1.0, Code, KEY_DOT},
- {"Entr", "Entr", 2.0, Code, KEY_ENTER, .scheme = 1},
+ {"Enter", "Enter", 2.0, Code, KEY_ENTER, .scheme = 1},
/* end of layout */
{"", "", 0.0, Last},
@@ -316,7 +315,7 @@ static struct key keys_special[] = {
{",", "'", 1.0, Code, KEY_COMMA},
{"", "", 4.0, Code, KEY_SPACE},
{".", "?", 1.0, Code, KEY_DOT},
- {"Entr", "Entr", 2.0, Code, KEY_ENTER, .scheme = 1},
+ {"Enter", "Enter", 2.0, Code, KEY_ENTER, .scheme = 1},
/* end of layout */
{"", "", 0.0, Last},
@@ -431,7 +430,7 @@ static struct key keys_cyrillic[] = {
{",", "'", 1.0, Code, KEY_EQUAL},
{"", "", 4.0, Code, KEY_SPACE},
{".", "?", 1.0, Code, KEY_SLASH},
- {"Entr", "Entr", 2.0, Code, KEY_ENTER, .scheme = 1},
+ {"Enter", "Enter", 2.0, Code, KEY_ENTER, .scheme = 1},
/* end of layout */
{"", "", 0.0, Last},
diff --git a/main.c b/main.c
@@ -312,7 +312,7 @@ create_and_upload_keymap(const char *name, uint32_t comp_unichr,
void
usage(char *argv0)
{
- fprintf(stderr, "usage: %s [-hov] [-H height] [-fn font] [-l layers] [-s initial_layer]\n", argv0);
+ fprintf(stderr, "usage: %s [-hov] [-H height] [-fn font] [-l layers]\n", argv0);
fprintf(stderr, "Options:\n");
fprintf(stderr, " -D - Enable debug\n");
fprintf(stderr, " -o - Print press keys to standard output\n");
@@ -393,7 +393,7 @@ main(int argc, char **argv) {
keyboard.vkbd =
zwp_virtual_keyboard_manager_v1_create_virtual_keyboard(vkbd_mgr, seat);
- kbd_init(&keyboard);
+ kbd_init(&keyboard, &layouts, layer_names_list);
/* assign kbd state */
keyboard.surf = &draw_surf;