ref: e6ab6580d5ee2874ac33ca763bb6e0ae53dc35c5
parent: 3b0a35dd198d23c2d83a2426d61c7628ca5b82bf
author: Sigrid Solveig Haflínudóttir <[email protected]>
date: Sun Nov 21 11:18:07 EST 2021
evdump, no more kbfind
--- a/README.md
+++ b/README.md
@@ -1,4 +1,3 @@
-# aux/kbfind
+# evdump
-Finds the correct `/dev/kbmap` entry for a non-working key on your
-keyboard, in 9front.
+Dumps input events in 9front.
--- /dev/null
+++ b/evdump.c
@@ -1,0 +1,271 @@
+#include <u.h>
+#include <libc.h>
+#include <ctype.h>
+#include <bio.h>
+#include <draw.h>
+#include <keyboard.h>
+#include <mouse.h>
+#include <thread.h>
+
+typedef struct {
+ u8int e;
+ u8int c;
+ Rune r;
+}K;
+
+enum {
+ Kmbase = 0xf0000,
+};
+
+static K k[10*128];
+static int nk;
+static int kbd;
+static Biobuf *kbmap;
+
+static char *
+k2s(Rune 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";
+ case KF|1: return "F1";
+ case KF|2: return "F2";
+ case KF|3: return "F3";
+ case KF|4: return "F4";
+ case KF|5: return "F5";
+ case KF|6: return "F6";
+ case KF|7: return "F7";
+ case KF|8: return "F8";
+ case KF|9: return "F9";
+ case KF|10: return "F10";
+ case KF|11: return "F11";
+ case KF|12: return "F12";
+ case Kmouse|1: return "Kmouse1";
+ case Kmouse|2: return "Kmouse2";
+ case Kmouse|3: return "Kmouse3";
+ case Kmouse|4: return "Kmouse4";
+ case Kmouse|5: return "Kmouse5";
+ case '\n': return "\\n";
+ }
+
+ return nil;
+}
+
+static void
+kmreset(void)
+{
+ int i;
+
+ for(i = 0; i <= nk; i++)
+ Bprint(kbmap, "%d\t%d\t%d\n", k[i].e, k[i].c, k[i].r);
+ Bflush(kbmap);
+}
+
+static void
+kmset(void)
+{
+ int i;
+
+ for(i = 0; i <= nk; i++)
+ Bprint(kbmap, "%d\t%d\t%d\n", k[i].e, k[i].c, Kmbase+i);
+ Bflush(kbmap);
+}
+
+static void
+key(Rune r, char *type)
+{
+ char *s, t[32];
+ Rune c;
+ K q;
+
+ if(r < Kmbase || r >= Kmbase+nk){
+ if((s = k2s(r)) != nil)
+ snprint(t, sizeof(t), "%s", s);
+ else if((r < 0x80 && isprint(r)) || r >= 0x20)
+ snprint(t, sizeof(t), "%C (0x%x)", r, r);
+ else
+ snprint(t, sizeof(t), "0x%x", r);
+ print("unexpected key %s %s\n", type, t);
+ return;
+ }
+ q = k[r-Kmbase];
+ c = q.r;
+ if((s = k2s(c)) != nil)
+ snprint(t, sizeof(t), "%s", s);
+ else if((c < 0x80 && isprint(c)) || c >= 0x20)
+ snprint(t, sizeof(t), "%C (0x%x)", c, c);
+ else
+ snprint(t, sizeof(t), "0x%x", c);
+
+ print("key %s %s: %d %d %d\n", type, t, q.e, q.c, q.r);
+}
+
+static void
+kbproc(void *)
+{
+ char *s, buf[128], buf2[128];
+ int kbd, n;
+ Rune r;
+
+ threadsetname("kbproc");
+ if((kbd = open("/dev/kbd", OREAD)) < 0)
+ sysfatal("/dev/kbd: %r");
+
+ buf2[0] = 0;
+ buf2[1] = 0;
+ buf[0] = 0;
+ for(;;){
+ if(buf[0] != 0){
+ n = strlen(buf)+1;
+ memmove(buf, buf+n, sizeof(buf)-n);
+ }
+ if(buf[0] == 0){
+ n = read(kbd, buf, sizeof(buf)-1);
+ if(n <= 0)
+ break;
+ buf[n-1] = 0;
+ buf[n] = 0;
+ }
+
+ switch(buf[0]){
+ case 'k':
+ for(s = buf+1; *s;){
+ s += chartorune(&r, s);
+ if(utfrune(buf2+1, r) == nil)
+ key(r, "down");
+ }
+ break;
+ case 'K':
+ for(s = buf2+1; *s;){
+ s += chartorune(&r, s);
+ if(utfrune(buf+1, r) == nil)
+ key(r, "up");
+ }
+ break;
+ case 'c':
+ if(chartorune(&r, buf+1) > 0 && r != Runeerror)
+ key(r, "repeat");
+ default:
+ continue;
+ }
+
+ strcpy(buf2, buf);
+ }
+
+ close(kbd);
+
+ threadexits(nil);
+}
+
+static void
+usage(void)
+{
+ fprint(2, "usage: %s\n", argv0);
+ threadexitsall("usage");
+}
+
+void
+threadmain(int argc, char **argv)
+{
+ Mousectl *mctl;
+ char tmp[32];
+ Mouse m;
+ char *s;
+ enum { Cmouse, Cresize, Numchan };
+ Alt a[Numchan+1] = {
+ [Cmouse] = { nil, &m, CHANRCV },
+ [Cresize] = { nil, nil, CHANRCV },
+ { nil, nil, CHANEND },
+ };
+
+ ARGBEGIN{
+ default:
+ usage();
+ }ARGEND
+
+ if(argc != 0)
+ usage();
+
+ if((kbmap = Bopen("/dev/kbmap", OREAD)) == nil)
+ sysfatal("%r");
+ for(nk = 0; nk < nelem(k); nk++){
+ if((s = Brdline(kbmap, '\n')) == nil)
+ break;
+ k[nk].e = strtoul(s, &s, 10);
+ k[nk].c = strtoul(s, &s, 10);
+ k[nk].r = strtoul(s, &s, 10);
+ }
+ Bterm(kbmap);
+
+ if((kbmap = Bopen("/dev/kbmap", OWRITE)) == nil)
+ sysfatal("%r");
+ atexit(kmreset);
+
+ snprint(tmp, sizeof(tmp), "-pid %d -dx %d -dy %d", getpid(), 256, 256);
+ newwindow(tmp);
+
+ if(initdraw(nil, nil, "evdump") < 0)
+ sysfatal("initdraw: %r");
+ if((mctl = initmouse(nil, screen)) == nil)
+ sysfatal("initmouse: %r");
+ a[Cmouse].c = mctl->c;
+ a[Cresize].c = mctl->resizec;
+
+ proccreate(kbproc, nil, mainstacksize);
+
+ for (;;) {
+ draw(screen, screen->r, display->black, nil, ZP);
+
+ switch (alt(a)) {
+ case -1:
+ goto end;
+
+ case Cmouse:
+ print("mouse buttons 0x%x x %d y %d\n", m.buttons, m.xy.x, m.xy.y);
+ break;
+
+ case Cresize:
+ getwindow(display, Refnone);
+ print(
+ "resize min %d %d max %d %d\n",
+ screen->r.min.x, screen->r.min.y,
+ screen->r.max.x, screen->r.max.y
+ );
+ break;
+ }
+ }
+
+end:
+ threadexitsall(nil);
+}
--- a/kbfind.c
+++ /dev/null
@@ -1,161 +1,0 @@
-#include <u.h>
-#include <libc.h>
-#include <bio.h>
-#include <keyboard.h>
-
-typedef struct {
- int e;
- int c;
- int r;
-}K;
-
-static K k[1280];
-static int nk, kbmap, kbd;
-
-enum {
- Base = 0xf0000,
- End = 0xffffd
-};
-
-static char *
-k2s(Rune r)
-{
- static char s[4];
-
- 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;
- }
- }
-
- return nil;
-}
-
-static void
-cleanup(void)
-{
- int i;
-
- 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;
- int i, n;
- Rune r;
-
- USED(argc); USED(argv);
-
- if((b = Bopen("/dev/kbmap", OREAD)) == nil)
- sysfatal("%r");
- for(nk = 0; nk < nelem(k);){
- if((s = Brdline(b, '\n')) == nil)
- break;
- k[nk].e = strtol(s, &s, 10);
- k[nk].c = strtol(s, &s, 10);
- k[nk].r = strtol(s, &s, 10);
- if(k[nk].r == 0)
- nk++;
- }
- if(nk < 1)
- sysfatal("no keys to map");
- Bterm(b);
- 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");
- 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;
- buf[0] = 0;
- for(;;){
- if(buf[0] != 0){
- n = strlen(buf)+1;
- memmove(buf, buf+n, sizeof(buf)-n);
- }
- if(buf[0] == 0){
- n = read(kbd, buf, sizeof(buf)-1);
- if(n <= 0)
- break;
- buf[n-1] = 0;
- }
-
- switch(buf[0]){
- case 'k':
- s = buf+1;
- 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;
- }
- }
- break;
-
- case 'K':
- s = buf2+1;
- while(*s)
- s += chartorune(&r, s);
- break;
-
- default:
- continue;
- }
-
- strcpy(buf2, buf);
- }
-
-end:
- exits(nil);
-}
--- a/mkfile
+++ b/mkfile
@@ -1,6 +1,7 @@
</$objtype/mkfile
-BIN=/$objtype/bin/aux
-TARG=kbfind
-OFILES=kbfind.$O
-default:V: all
+
+BIN=/$objtype/bin
+TARG=evdump
+OFILES=evdump.$O
+
</sys/src/cmd/mkone