[hackers] [PATCH v2 02/14] Move layouts to single layout.h file & toggle layers via memcpy w/ XK_Cancel

From: Maarten van Gompel <proycon_AT_anaproy.nl>
Date: Sun, 2 Aug 2020 15:46:07 +0200

From: Miles Alan <m_AT_milesalan.com>

---
 layout.en.h => layout.sxmo.h | 57 +++++++++++++++++++++++++++++++++--
 layout.symbols.h             | 58 ------------------------------------
 svkbd.c                      | 33 +++++++++++++++++++-
 3 files changed, 87 insertions(+), 61 deletions(-)
 rename layout.en.h => layout.sxmo.h (52%)
 delete mode 100644 layout.symbols.h
diff --git a/layout.en.h b/layout.sxmo.h
similarity index 52%
rename from layout.en.h
rename to layout.sxmo.h
index 2c8cd57..cd4afb6 100644
--- a/layout.en.h
+++ b/layout.sxmo.h
_AT_@ -1,4 +1,6 @@
-static Key keys[] = {
+static Key keys[40] = { NULL };
+
+static Key keys_en[40] = {
         { 0, XK_q, 1 },
         { 0, XK_w, 1 },
         { 0, XK_e, 1 },
_AT_@ -12,7 +14,6 @@ static Key keys[] = {
         { 0 }, /* New row */
-
         { 0, XK_a, 1 },
         { 0, XK_s, 1 },
         { 0, XK_d, 1 },
_AT_@ -52,7 +53,59 @@ static Key keys[] = {
         { "Enter", XK_Return, 2 },
 };
+static Key keys_symbols[40] = {
+  { "1!", XK_1, 1 },
+  { "2_AT_", XK_2, 1 },
+  { "3#", XK_3, 1 },
+  { "4$", XK_4, 1 },
+  { "5%", XK_5, 1 },
+  { "6^", XK_6, 1 },
+  { "7&", XK_7, 1 },
+  { "8*", XK_8, 1 },
+  { "9(", XK_9, 1 },
+  { "0)", XK_0, 1 },
+
+  { 0 }, /* New row */
+
+  { "'\"", XK_apostrophe, 1 },
+  { "`~", XK_grave, 1 },
+  { "-_", XK_minus, 1 },
+  { "=+", XK_plus, 1 },
+  { "[{", XK_bracketleft, 1 },
+  { "]}", XK_bracketright, 1 },
+  { ",<", XK_comma, 1 },
+  { ".>", XK_period, 1 },
+  { "/?", XK_slash, 1 },
+  { "\\", XK_backslash, 1 },
+
+  { 0 }, /* New row */
+
+  { "|", XK_Shift_L|XK_bar, 1 },
+  { "L", XK_Left, 1 },
+  { "R", XK_Right, 1 },
+  { "Ctrl-C", XK_Shift_L|XK_bar, 1 },
+  { "Ctrl-L", XK_Shift_L|XK_bar, 1 },
+  { "Ctrl-V", XK_Shift_L|XK_bar, 1 },
+  { "Ctrl-D", XK_Shift_L|XK_bar, 1 },
+  { "TAB", XK_Tab, 1 },
+  { "<-", XK_BackSpace, 2 },
+
+  { 0 }, /* New row */
+  { "Layer 1", XK_Cancel, 1},
+  { "Shift", XK_Shift_L, 1 },
+  /*{ "L", XK_Left, 1 },*/
+  { "D", XK_Down, 1 },
+  { "U", XK_Up, 1 },
+  /*{ "R", XK_Right, 1 },*/
+  { "", XK_space, 2 },
+  { "Esc", XK_Escape, 1 },
+  { "Ctrl", XK_Control_L, 1 },
+  /*{ "Alt", XK_Alt_L, 1 },*/
+  { "Enter", XK_Return, 2 },
+};
+
 Buttonmod buttonmods[] = {
         { XK_Shift_L, Button2 },
         { XK_Alt_L, Button3 },
 };
+
diff --git a/layout.symbols.h b/layout.symbols.h
deleted file mode 100644
index 84d17a8..0000000
--- a/layout.symbols.h
+++ /dev/null
_AT_@ -1,58 +0,0 @@
-static Key keys[] = {
-
-
-  { "1!", XK_1, 1 },
-  { "2_AT_", XK_2, 1 },
-  { "3#", XK_3, 1 },
-  { "4$", XK_4, 1 },
-  { "5%", XK_5, 1 },
-  { "6^", XK_6, 1 },
-  { "7&", XK_7, 1 },
-  { "8*", XK_8, 1 },
-  { "9(", XK_9, 1 },
-  { "0)", XK_0, 1 },
-
-  { 0 }, /* New row */
-
-  { "'\"", XK_apostrophe, 1 },
-  { "`~", XK_grave, 1 },
-  { "-_", XK_minus, 1 },
-  { "=+", XK_plus, 1 },
-  { "[{", XK_bracketleft, 1 },
-  { "]}", XK_bracketright, 1 },
-  { ",<", XK_comma, 1 },
-  { ".>", XK_period, 1 },
-  { "/?", XK_slash, 1 },
-  { "\\", XK_backslash, 1 },
-
-  { 0 }, /* New row */
-
-  { "|", XK_Shift_L|XK_bar, 1 },
-  { "L", XK_Left, 1 },
-  { "R", XK_Right, 1 },
-  { "Ctrl-C", XK_Shift_L|XK_bar, 1 },
-  { "Ctrl-L", XK_Shift_L|XK_bar, 1 },
-  { "Ctrl-V", XK_Shift_L|XK_bar, 1 },
-  { "Ctrl-D", XK_Shift_L|XK_bar, 1 },
-  { "TAB", XK_Tab, 1 },
-  { "<-", XK_BackSpace, 2 },
-
-  { 0 }, /* New row */
-  { "Layer 1", XK_Cancel, 1},
-  { "Shift", XK_Shift_L, 1 },
-  /*{ "L", XK_Left, 1 },*/
-  { "D", XK_Down, 1 },
-  { "U", XK_Up, 1 },
-  /*{ "R", XK_Right, 1 },*/
-  { "", XK_space, 2 },
-  { "Esc", XK_Escape, 1 },
-  { "Ctrl", XK_Control_L, 1 },
-  /*{ "Alt", XK_Alt_L, 1 },*/
-  { "Enter", XK_Return, 2 },
-};
-
-Buttonmod buttonmods[] = {
-	{ XK_Shift_L, Button2 },
-	{ XK_Alt_L, Button3 },
-};
-
diff --git a/svkbd.c b/svkbd.c
index 337f769..7dfb27f 100644
--- a/svkbd.c
+++ b/svkbd.c
_AT_@ -13,6 +13,7 @@
 #include <X11/Xutil.h>
 #include <X11/Xproto.h>
 #include <X11/extensions/XTest.h>
+#include <signal.h>
 /* macros */
 #define MAX(a, b)       ((a) > (b) ? (a) : (b))
_AT_@ -96,8 +97,10 @@ static Bool running = True, isdock = False;
 static KeySym pressedmod = 0;
 static int rows = 0, ww = 0, wh = 0, wx = 0, wy = 0;
 static char *name = "svkbd";
+static int terminate = 0;
 Bool ispressing = False;
+Bool baselayer = True;
 /* configuration, allows nested code to access above variables */
 #include "config.h"
_AT_@ -393,7 +396,10 @@ unpress(Key *k, KeySym mod) {
 	if(k != NULL) {
 		switch(k->keysym) {
 		case XK_Cancel:
-			exit(0);
+			togglelayer();
+			break;
+		case XK_Break:
+		  running = False;
 		default:
 			break;
 		}
_AT_@ -580,11 +586,36 @@ usage(char *argv0) {
 	exit(1);
 }
+void
+togglelayer() {
+	memcpy(&keys, baselayer ? &keys_symbols : &keys_en, sizeof(keys_en));
+	updatekeys();
+	drawkeyboard();
+	baselayer = !baselayer;
+}
+
+void
+sigterm(int sig)
+{
+	// E.g. Since sometimes we might use svkbd, to kill svkbd - e.g. in
+	// terminal or script (pkill svkbd), .. that signal might register before
+	// the keyup event is processed so X thinks the key is held down forever..
+	// so here we keyup every key & exit (XK_Break) to keyup cleanup properly
+	int i;
+	for(i = 0; i < LENGTH(keys); i++) {
+	  XTestFakeKeyEvent(dpy, XKeysymToKeycode(dpy, keys[i].keysym), False, 0);
+	}
+	running = False;
+	//XTestFakeKeyEvent(dpy, XK_Break, False, 0);
+}
+
 int
 main(int argc, char *argv[]) {
 	int i, xr, yr, bitm;
 	unsigned int wr, hr;
+	signal(SIGTERM, sigterm);
+	memcpy(&keys, &keys_en, sizeof(keys_en));
 	for (i = 1; argv[i]; i++) {
 		if(!strcmp(argv[i], "-v")) {
 			die("svkbd-"VERSION", © 2006-2016 svkbd engineers,"
--
2.27.0
Received on Sun Aug 02 2020 - 15:46:07 CEST

This archive was generated by hypermail 2.3.0 : Sun Aug 02 2020 - 16:36:32 CEST