shithub: evdump

Download patch

ref: 3b0a35dd198d23c2d83a2426d61c7628ca5b82bf
parent: 4aab52143cce6e8105b4fd50311b21628c61cb3f
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Thu Nov 18 17:47:13 EST 2021

use unicode private use area to set all unmapped keys at once and map back

--- a/README.md
+++ b/README.md
@@ -2,27 +2,3 @@
 
 Finds the correct `/dev/kbmap` entry for a non-working key on your
 keyboard, in 9front.
-
-## Process
-
-1) after installing (`mk install`) run `aux/kbfind`.
-2) press the non-working key _once_
-3) press any other _working_ key _once_ (or Delete to exit)
-4) go to step 2
-
-If the key was not mapped before and is producing scancodes,
-eventually the program will print out the needed entry that needs to
-be put into `/dev/kbmap` for the key to work.  Replace the last `0` of
-that entry to whichever Rune you want your key to produce, converted
-to a number, see `/sys/include/keyboard.h` for some of the existing
-"special" keys.
-
-## How does this work
-
-Unmapped entries in `/dev/kbmap` have the Rune (third number) set to
-0.  `aux/kbfind` sets those entries to a custom Rune and awaits for
-key presses to be available on `/dev/kbd`.  If the custom Rune was
-found the entries currently enabled are cut into half to see whether
-the key would still produce the rune.  If not, the other half is
-enabled instead and the process is repeated, cutting the number of
-keys to test in half every time.
--- a/kbfind.c
+++ b/kbfind.c
@@ -11,78 +11,76 @@
 
 static K k[1280];
 static int nk, kbmap, kbd;
-static int k0, ke;
-static int ak0, ake;
-static int nohits, left;
 
 enum {
-	Kunmapped = 0xf789,
+	Base = 0xf0000,
+	End = 0xffffd
 };
 
-static void
-mapto(Rune r)
+static char *
+k2s(Rune r)
 {
-	int i;
+	static char s[4];
 
-	/* disable the range */
-	for(i = k0; i <= ke; i++)
-		fprint(kbmap, "%d\t%d\t%d\n", k[i].e, k[i].c, r);
-}
+	switch(r){
+	case Kack: return "Kack";
+	case Kalt: return "Kalt";
+	case Kaltgr: return "Kaltgr";
+	case Kbreak: return "Kbreak";
+	case Kbs: return "Kbs";
+	case Kcaps: return "Kcaps";
+	case Kctl: return "Kctl";
+	case Kdel: return "Kdel";
+	case Kdown: return "Kdown";
+	case Kend: return "Kend";
+	case Kenq: return "Kenq";
+	case Keof: return "Keof";
+	case Kesc: return "Kesc";
+	case Ketb: return "Ketb";
+	case Ketx: return "Ketx";
+	case Khome: return "Khome";
+	case Kins: return "Kins";
+	case Kleft: return "Kleft";
+	case Kmiddle: return "Kmiddle";
+	case Kmod4: return "Kmod4";
+	case Knack: return "Knack";
+	case Knum: return "Knum";
+	case Kpgdown: return "Kpgdown";
+	case Kpgup: return "Kpgup";
+	case Kprint: return "Kprint";
+	case Kright: return "Kright";
+	case Kscroll: return "Kscroll";
+	case Kscrollonedown: return "Kscrollonedown";
+	case Kscrolloneup: return "Kscrolloneup";
+	case Kshift: return "Kshift";
+	case Ksoh: return "Ksoh";
+	case Kstx: return "Kstx";
+	case Kup: return "Kup";
+	default:
+		if(r >= (KF|1) && r <= (KF|12)){
+			sprint(s, "F%d", r-KF);
+			return s;
+		}
+	}
 
-static void
-revert(void)
-{
-	mapto(0);
+	return nil;
 }
 
 static void
-hit(int unmapped)
+cleanup(void)
 {
-	char kbup[32];
+	int i;
 
-	/* disable mapping */
-	mapto(0);
-
-	if(unmapped){
-		nohits = 0;
-		/* got a hit */
-		if(k0 == ke){
-			/* only one left */
-			print("%d\t%d\t0\n", k[k0].e, k[k0].c);
-			/* skip key up so there isn't garbage printed out */
-			read(kbd, kbup, sizeof(kbup));
-			exits(nil);
-		}
-		/* reduce */
-		ake = ke;
-		ke = k0 + (ke-k0)/2;
-		ak0 = ke+1;
-	}else if(++nohits > 1){
-		nohits = 0;
-		if(k0 == ak0 && ke == ake){
-			/* give up */
-			fprint(2, "key doesn't work\n");
-			exits(nil);
-		}
-		/* try a different half */
-		k0 = ak0;
-		ke = ake;
-	}
-	if(left != ke-k0+1){
-		left = ke-k0+1;
-		fprint(2, "%d possible left\n", left);
-	}
-
-	/* enable the new range */
-	mapto(Kunmapped);
+	for(i = 0; i <= nk; i++)
+		fprint(kbmap, "%d\t%d\t%d\n", k[i].e, k[i].c, 0);
 }
 
 void
 main(int argc, char **argv)
 {
+	char *s, *x, buf[128], buf2[128];
 	Biobuf *b;
-	char *s, buf[128], buf2[128];
-	int n;
+	int i, n;
 	Rune r;
 
 	USED(argc); USED(argv);
@@ -101,13 +99,14 @@
 	if(nk < 1)
 		sysfatal("no keys to map");
 	Bterm(b);
-	fprint(2, "there are %d unmapped keys\n", nk);
-	ke = nk-1;
+	print("there are %d unmapped keys\n", nk);
 
 	kbd = -1;
 	if((kbmap = open("/dev/kbmap", OWRITE)) < 0 || (kbd = open("/dev/kbd", OREAD)) < 0)
 		sysfatal("%r");
-	mapto(Kunmapped);
+	atexit(cleanup);
+	for(i = 0; i <= nk; i++)
+		fprint(kbmap, "%d\t%d\t%d\n", k[i].e, k[i].c, Base+i);
 
 	buf2[0] = 0;
 	buf2[1] = 0;
@@ -130,13 +129,16 @@
 			while(*s){
 				s += chartorune(&r, s);
 				if(utfrune(buf2+1, r) == nil){
+					if((x = k2s(r)) != nil)
+						print("%s\n", x);
+					else if(r >= Base && r <= End && (i = r-Base) < nk)
+						print("%d\t%d\t%d\n", k[i].e, k[i].c, r);
+					else if(r != Runeerror)
+						print("%C\n", r);
+					else
+						print("unknown key: rune 0x%x\n", r);
 					if(r == Kdel)
 						goto end;
-					if(r == Kshift || r == Kalt || r == Kctl){
-						fprint(2, "please don't press shift/alt/ctl\n");
-						continue;
-					}
-					hit(r == Kunmapped);
 				}
 			}
 			break;