commit ff92a920863ffb15f1e5c0594b19a46e19a05fc3
parent a38ec393b5cca1a78a797a2eeb2c22ce6f291841
Author: Maarten van Gompel <proycon@anaproy.nl>
Date: Thu, 26 Aug 2021 01:02:42 +0200
refactored keymap loading
Diffstat:
5 files changed, 43 insertions(+), 29 deletions(-)
diff --git a/keyboard.c b/keyboard.c
@@ -21,7 +21,7 @@ kbd_switch_layout(struct kbd *kb, struct layout *l) {
if ((!kb->prevlayout) ||
(strcmp(kb->prevlayout->keymap_name, kb->layout->keymap_name) != 0)) {
fprintf(stderr, "Switching to keymap %s\n", kb->layout->keymap_name);
- create_and_upload_keymap(kb->layout->keymap_name, 0, 0);
+ create_and_upload_keymap(kb, kb->layout->keymap_name, 0, 0);
}
kbd_draw_layout(kb);
}
@@ -96,7 +96,7 @@ void kbd_init(struct kbd *kb, struct layout * layouts, char * layer_names_list)
kb->prevlayout = kb->layout;
/* upload keymap */
- create_and_upload_keymap(kb->layout->keymap_name, 0, 0);
+ create_and_upload_keymap(kb, kb->layout->keymap_name, 0, 0);
}
void
@@ -246,7 +246,7 @@ kbd_press_key(struct kbd *kb, struct key *k, uint32_t time) {
kb->last_press = k;
kbd_draw_key(kb, k, true);
if (kb->debug) fprintf(stderr, "pressing copy key\n");
- create_and_upload_keymap(kb->layout->keymap_name, k->code, k->code_mod);
+ create_and_upload_keymap(kb, kb->layout->keymap_name, k->code, k->code_mod);
zwp_virtual_keyboard_v1_modifiers(kb->vkbd, kb->mods, 0, 0, 0);
zwp_virtual_keyboard_v1_key(kb->vkbd, time, 127, // COMP key
WL_KEYBOARD_KEY_STATE_PRESSED);
@@ -351,10 +351,22 @@ draw_inset(struct drwsurf *ds, uint32_t x, uint32_t y, uint32_t width,
}
void
-create_and_upload_keymap(const char *name, uint32_t comp_unichr,
+create_and_upload_keymap(struct kbd * kb, const char *name, uint32_t comp_unichr,
uint32_t comp_shift_unichr) {
- const char *keymap_str = get_keymap(name, comp_unichr, comp_shift_unichr);
- size_t keymap_size = strlen(keymap_str) + 1;
+ int keymap_index = -1;
+ for (int i = 0; i < NUMKEYMAPS; i++) {
+ if (!strcmp(keymap_names[i], name)) {
+ keymap_index = i;
+ }
+ }
+ if (keymap_index == -1) {
+ fprintf(stderr,"No such keymap defined: %s\n", name);
+ exit(9);
+ }
+ const char * keymap_template = keymaps[keymap_index];
+ const size_t keymap_size = strlen(keymap_template) + 64;
+ char *keymap_str = malloc(keymap_size);
+ sprintf(keymap_str, keymap_template, comp_unichr, comp_shift_unichr);
int keymap_fd = os_create_anonymous_file(keymap_size);
if (keymap_fd < 0) {
die("could not create keymap fd\n");
@@ -364,8 +376,11 @@ create_and_upload_keymap(const char *name, uint32_t comp_unichr,
if (ptr == (void *)-1) {
die("could not map keymap data\n");
}
+ if (kb->vkbd == NULL) {
+ die("kb.vkbd = NULL\n");
+ }
strcpy(ptr, keymap_str);
zwp_virtual_keyboard_v1_keymap(
- keyboard.vkbd, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, keymap_fd, keymap_size);
+ kb->vkbd, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, keymap_fd, keymap_size);
free((void *)keymap_str);
}
diff --git a/keyboard.h b/keyboard.h
@@ -114,12 +114,11 @@ uint8_t kbd_get_rows(struct layout *l);
double kbd_get_row_length(struct key *k);
void kbd_switch_layout(struct kbd *kb, struct layout *l);
-void create_and_upload_keymap(const char *name, uint32_t comp_unichr,
+void create_and_upload_keymap(struct kbd *kb, const char *name, uint32_t comp_unichr,
uint32_t comp_shift_unichr);
#ifndef LAYOUT
#error "make sure to define LAYOUT"
#endif
#include LAYOUT
-
#endif
diff --git a/keymap.mobintl.h b/keymap.mobintl.h
@@ -1,8 +1,11 @@
-char *
-get_keymap(const char *name, uint32_t comp_unichr, uint32_t comp_shift_unichr) {
- char *keymap = malloc(65000);
- if (strcmp(name, "latin") == 0) {
- sprintf(keymap, "xkb_keymap {\
+#define NUMKEYMAPS 3
+
+static const char * keymap_names[] = {"latin","cyrillic","arabic"};
+
+static const char * keymaps[NUMKEYMAPS] = {
+
+ //LATIN
+ "xkb_keymap {\
xkb_keycodes \"(unnamed)\" {\
minimum = 8;\
maximum = 255;\
@@ -1465,10 +1468,8 @@ xkb_symbols \"(unnamed)\" {\
\
};\
",
- comp_unichr, comp_shift_unichr);
- return keymap;
- } else if (strcmp(name, "cyrillic") == 0) {
- sprintf(keymap, "xkb_keymap {\
+ //CYRILLIC
+ "xkb_keymap {\
xkb_keycodes \"(unnamed)\" {\
minimum = 8;\
maximum = 255;\
@@ -2930,10 +2931,8 @@ xkb_symbols \"(unnamed)\" {\
\
};\
",
- comp_unichr, comp_shift_unichr);
- return keymap;
- } else if (strcmp(name, "arabic") == 0) {
- sprintf(keymap, "xkb_keymap {\
+ //ARABIC
+ "xkb_keymap {\
xkb_keycodes \"(unnamed)\" {\
minimum = 8;\
maximum = 255;\
@@ -4390,10 +4389,6 @@ xkb_symbols \"(unnamed)\" {\
modifier_map Mod4 { <LWIN>, <RWIN>, <SUPR>, <HYPR> };\
modifier_map Mod5 { <LVL3>, <MDSW> };\
};\
-};", comp_unichr, comp_shift_unichr);
- return keymap;
- } else {
- fprintf(stderr, "No such keymap: '%s'", name);
- exit(2);
- }
-}
+};"
+};
+
diff --git a/layout.mobintl.h b/layout.mobintl.h
@@ -9,6 +9,8 @@
/* spacing between keys */
#define KBD_KEY_BORDER 2
+#include "keymap.mobintl.h"
+
/* layout declarations */
enum layout_id {
Full = 0,
diff --git a/main.c b/main.c
@@ -399,6 +399,9 @@ main(int argc, char **argv) {
keyboard.vkbd =
zwp_virtual_keyboard_manager_v1_create_virtual_keyboard(vkbd_mgr, seat);
+ if (keyboard.vkbd == NULL) {
+ die("failed to init virtual keyboard_manager\n");
+ }
kbd_init(&keyboard, (struct layout *) &layouts, layer_names_list);