Move layouts to single layout.h file & toggle layers via memcpy w/ XK_Cancel

This commit is contained in:
Miles Alan 2020-08-02 15:46:07 +02:00 committed by Hiltjo Posthuma
parent 0a3a153aa6
commit 6e5bf2d8e4
3 changed files with 87 additions and 61 deletions

View File

@ -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 },
@ -12,7 +14,6 @@ static Key keys[] = {
{ 0 }, /* New row */
{ 0, XK_a, 1 },
{ 0, XK_s, 1 },
{ 0, XK_d, 1 },
@ -52,7 +53,59 @@ static Key keys[] = {
{ "Enter", XK_Return, 2 },
};
static Key keys_symbols[40] = {
{ "1!", XK_1, 1 },
{ "2@", 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 },
};

View File

@ -1,58 +0,0 @@
static Key keys[] = {
{ "1!", XK_1, 1 },
{ "2@", 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 },
};

33
svkbd.c
View File

@ -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))
@ -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"
@ -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;
}
@ -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,"