#include #include #include #include #include "dat.h" #include "fns.h" enum { ScanEsc = 1, ScanBSpace = 14, ScanTab = 15, ScanQ = 16, ScanW = 17, ScanE = 18, ScanR = 19, ScanT = 20, ScanY = 21, ScanU = 22, ScanI = 23, ScanO = 24, ScanP = 25, ScanRBrace = 27, ScanEnter = 28, ScanLCtrl = 29, ScanA = 30, ScanS = 31, ScanD = 32 , ScanF = 33, ScanG = 34, ScanH = 35, ScanJ = 36, ScanK = 37, ScanL = 38, ScanLShift = 42, ScanBSlash = 43, ScanZ = 44, ScanX = 45, ScanC = 46, ScanV = 47, ScanB = 48, ScanN = 49, ScanM = 50, ScanPeriod = 52, ScanSlash = 53, ScanF1 = 59, ScanF11 = 87, ScanF12 = 88, ScanDelete = 211, }; enum { KeyUp = 0x8000, }; enum { Kbrk= Spec|0x61, }; struct { unsigned char key, mod; } rune2keycode[] = { ScanDelete, ScanLShift, ScanA, ScanLCtrl, ScanB, ScanLCtrl, ScanC, ScanLCtrl, ScanD, ScanLCtrl, ScanE, ScanLCtrl, ScanF, ScanLCtrl, ScanG, ScanLCtrl, ScanBSpace, 0, ScanTab, 0, ScanEnter, 0, ScanK, ScanLCtrl, ScanL, ScanLCtrl, ScanM, ScanLCtrl, ScanN, ScanLCtrl, ScanO, ScanLCtrl, ScanP, ScanLCtrl, ScanQ, ScanLCtrl, ScanR, ScanLCtrl, ScanS, ScanLCtrl, ScanT, ScanLCtrl, ScanU, ScanLCtrl, ScanV, ScanLCtrl, ScanW, ScanLCtrl, ScanX, ScanLCtrl, ScanY, ScanLCtrl, ScanZ, ScanLCtrl, ScanEsc, 0, ScanBSlash, ScanLCtrl, ScanRBrace, ScanLCtrl, ScanPeriod, ScanLCtrl, ScanSlash, ScanLCtrl, }; void kbdsendrune(Rune r) { passinput(time(nil), INPUT_EVENT_UNICODE, 0, r, 0); passinput(time(nil), INPUT_EVENT_UNICODE, KeyUp, r, 0); } void kbdsendscan(uchar s, int press, int release) { if(press) passinput(time(nil), INPUT_EVENT_SCANCODE, 0, s, 0); if(release) passinput(time(nil), INPUT_EVENT_SCANCODE, KeyUp, s, 0); } void readkbd(void) { char buf[256], k[10]; int ctlfd, fd, kr, kn, w; uchar m, sc; Rune r; if((fd = open("/dev/cons", OREAD)) < 0) sysfatal("open %s: %r", buf); if((ctlfd = open("/dev/consctl", OWRITE)) < 0) sysfatal("open %s: %r", buf); write(ctlfd, "rawon", 5); kn = 0; for(;;){ while(!fullrune(k, kn)){ kr = read(fd, k+kn, sizeof k - kn); if(kr <= 0) sysfatal("bad read from kbd"); kn += kr; } w = chartorune(&r, k); kn -= w; memmove(k, &k[w], kn); if(r < nelem(rune2keycode)){ sc = rune2keycode[r].key; m = rune2keycode[r].mod; if(m) kbdsendscan(m, 1, 0); kbdsendscan(sc, 1, 1); if(m) kbdsendscan(m, 0, 1); continue; } switch(r){ case '\n': kbdsendscan(ScanEnter, 1, 1); break; case '\t': kbdsendscan(ScanTab, 1, 1); break; case Kins: kbdsendscan(0xd2, 1, 1); break; case Kdel: kbdsendscan(0xd3, 1, 1); break; case Khome: kbdsendscan(0xc7, 1, 1); break; case Kend: kbdsendscan(0xcf, 1, 1); break; case Kpgup: kbdsendscan(0xc9, 1, 1); break; case Kpgdown: kbdsendscan(0xd1, 1, 1); break; case Kup: kbdsendscan(0xc8, 1, 1); break; case Kdown: kbdsendscan(0xd0, 1, 1); break; case Kleft: kbdsendscan(0xcb, 1, 1); break; case Kright: kbdsendscan(0xcd, 1, 1); break; case Kbrk: exits("interrupt"); break; case Kprint: break; case KF|1: case KF|2: case KF|3: case KF|4: case KF|5: case KF|6: case KF|7: case KF|8: case KF|9: case KF|10: kbdsendscan(ScanF1-1+r-KF, 1, 1); break; case KF|11: kbdsendscan(ScanF11, 1, 1); break; case KF|12: kbdsendscan(ScanF12, 1, 1); break; default: kbdsendrune(r); break; } } }