26 typedef struct Key Key;
27 typedef struct Scan Scan;
78 char Eshort[] = "read count too small";
79 char Ebadarg[] = "invalid argument";
80 char Eperm[] = "permission denied";
81 char Einuse[] = "file in use";
82 char Enonexist[] = "file does not exist";
83 char Ebadspec[] = "bad attach specifier";
84 char Ewalk[] = "walk in non directory";
85 char Ephase[] = "the front fell off";
86 char Eintr[] = "interrupted";
97 Channel *keychan; /* chan(Key) */
99 Channel *kbdreqchan; /* chan(Req*) */
100 Channel *consreqchan; /* chan(Req*) */
102 Channel *ctlchan; /* chan(int) */
104 Channel *rawchan; /* chan(Rune) */
105 Channel *runechan; /* chan(Rune) */
107 Channel *conschan; /* chan(char*) */
108 Channel *kbdchan; /* chan(char*) */
111 * The codes at 0x79 and 0x7b are produced by the PFU Happy Hacking keyboard.
112 * A 'standard' keyboard doesn't produce anything above 0x58.
116 [0x00] 0, 0x1b, '1', '2', '3', '4', '5', '6',
117 [0x08] '7', '8', '9', '0', '-', '=', '\b', '\t',
118 [0x10] 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
119 [0x18] 'o', 'p', '[', ']', '\n', Kctl, 'a', 's',
120 [0x20] 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
121 [0x28] '\'', '`', Kshift, '\\', 'z', 'x', 'c', 'v',
122 [0x30] 'b', 'n', 'm', ',', '.', '/', Kshift, '*',
123 [0x38] Kalt, ' ', Kctl, KF|1, KF|2, KF|3, KF|4, KF|5,
124 [0x40] KF|6, KF|7, KF|8, KF|9, KF|10, Knum, Kscroll, '7',
125 [0x48] '8', '9', '-', '4', '5', '6', '+', '1',
126 [0x50] '2', '3', '0', '.', 0, 0, 0, KF|11,
127 [0x58] KF|12, 0, 0, 0, 0, 0, 0, 0,
128 [0x60] 0, 0, 0, 0, 0, 0, 0, 0,
129 [0x68] 0, 0, 0, 0, 0, 0, 0, 0,
130 [0x70] 0, 0, 0, 0, 0, 0, 0, 0,
131 [0x78] 0, Kdown, 0, Kup, 0, 0, 0, 0,
134 Rune kbtabshift[Nscan] =
136 [0x00] 0, 0x1b, '!', '@', '#', '$', '%', '^',
137 [0x08] '&', '*', '(', ')', '_', '+', '\b', '\t',
138 [0x10] 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
139 [0x18] 'O', 'P', '{', '}', '\n', Kctl, 'A', 'S',
140 [0x20] 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
141 [0x28] '"', '~', Kshift, '|', 'Z', 'X', 'C', 'V',
142 [0x30] 'B', 'N', 'M', '<', '>', '?', Kshift, '*',
143 [0x38] Kalt, ' ', Kctl, KF|1, KF|2, KF|3, KF|4, KF|5,
144 [0x40] KF|6, KF|7, KF|8, KF|9, KF|10, Knum, Kscroll, '7',
145 [0x48] '8', '9', '-', '4', '5', '6', '+', '1',
146 [0x50] '2', '3', '0', '.', 0, 0, 0, KF|11,
147 [0x58] KF|12, 0, 0, 0, 0, 0, 0, 0,
148 [0x60] 0, 0, 0, 0, 0, 0, 0, 0,
149 [0x68] 0, 0, 0, 0, 0, 0, 0, 0,
150 [0x70] 0, 0, 0, 0, 0, 0, 0, 0,
151 [0x78] 0, Kup, 0, Kup, 0, 0, 0, 0,
154 Rune kbtabesc1[Nscan] =
156 [0x00] 0, 0, 0, 0, 0, 0, 0, 0,
157 [0x08] 0, 0, 0, 0, 0, 0, 0, 0,
158 [0x10] 0, 0, 0, 0, 0, 0, 0, 0,
159 [0x18] 0, 0, 0, 0, '\n', Kctl, 0, 0,
160 [0x20] 0, 0, 0, 0, 0, 0, 0, 0,
161 [0x28] 0, 0, Kshift, 0, 0, 0, 0, 0,
162 [0x30] 0, 0, 0, 0, 0, '/', 0, Kprint,
163 [0x38] Kaltgr, 0, 0, 0, 0, 0, 0, 0,
164 [0x40] 0, 0, 0, 0, 0, 0, Kbreak, Khome,
165 [0x48] Kup, Kpgup, 0, Kleft, 0, Kright, 0, Kend,
166 [0x50] Kdown, Kpgdown, Kins, Kdel, 0, 0, 0, 0,
167 [0x58] 0, 0, 0, 0, 0, 0, 0, 0,
168 [0x60] 0, 0, 0, 0, 0, 0, 0, 0,
169 [0x68] 0, 0, 0, 0, 0, 0, 0, 0,
170 [0x70] 0, 0, 0, 0, 0, 0, 0, 0,
171 [0x78] 0, Kup, 0, 0, 0, 0, 0, 0,
174 Rune kbtabaltgr[Nscan] =
176 [0x00] 0, 0, 0, 0, 0, 0, 0, 0,
177 [0x08] 0, 0, 0, 0, 0, 0, 0, 0,
178 [0x10] 0, 0, 0, 0, 0, 0, 0, 0,
179 [0x18] 0, 0, 0, 0, '\n', Kctl, 0, 0,
180 [0x20] 0, 0, 0, 0, 0, 0, 0, 0,
181 [0x28] 0, 0, Kshift, 0, 0, 0, 0, 0,
182 [0x30] 0, 0, 0, 0, 0, '/', 0, Kprint,
183 [0x38] Kaltgr, 0, 0, 0, 0, 0, 0, 0,
184 [0x40] 0, 0, 0, 0, 0, 0, Kbreak, Khome,
185 [0x48] Kup, Kpgup, 0, Kleft, 0, Kright, 0, Kend,
186 [0x50] Kdown, Kpgdown, Kins, Kdel, 0, 0, 0, 0,
187 [0x58] 0, 0, 0, 0, 0, 0, 0, 0,
188 [0x60] 0, 0, 0, 0, 0, 0, 0, 0,
189 [0x68] 0, 0, 0, 0, 0, 0, 0, 0,
190 [0x70] 0, 0, 0, 0, 0, 0, 0, 0,
191 [0x78] 0, Kup, 0, 0, 0, 0, 0, 0,
194 Rune kbtabctl[Nscan] =
196 [0x00] 0, '
\e', '
\11', '
\12', '
\13', '
\14', '
\15', '
\16',
197 [0x08] '
\17', '
\18', '
\19', '
\10', '
\r', '
\1d', '\b', '\t',
198 [0x10] '
\11', '
\17', '
\ 5', '
\12', '
\14', '
\19', '
\15', '\t',
199 [0x18] '
\ f', '
\10', '
\e', '
\1d', '\n', Kctl, '
\ 1', '
\13',
200 [0x20] '
\ 4', '
\ 6', '
\a', '\b', '\n', '
\v', '
\f', '
\e',
201 [0x28] '
\a', 0, Kshift, '
\1c', '
\1a', '
\18', '
\ 3', '
\16',
202 [0x30] '
\ 2', '
\ e', '
\r', '
\f', '
\ e', '
\ f', Kshift, '\n',
203 [0x38] Kalt, 0, Kctl, '
\ 5', '
\ 6', '
\a', '
\ 4', '
\ 5',
204 [0x40] '
\ 6', '
\a', '
\f', '
\r', '
\ e', '
\ 5', '
\ 6', '
\17',
205 [0x48] '
\18', '
\19', '
\r', '
\14', '
\15', '
\16', '
\v', '
\11',
206 [0x50] '
\12', '
\13', '
\10', '
\ e', 0, 0, 0, '
\ f',
207 [0x58] '
\f', 0, 0, 0, 0, 0, 0, 0,
208 [0x60] 0, 0, 0, 0, 0, 0, 0, 0,
209 [0x68] 0, 0, 0, 0, 0, 0, 0, 0,
210 [0x70] 0, 0, 0, 0, 0, 0, 0, 0,
211 [0x78] 0, '
\a', 0, '\b', 0, 0, 0, 0,
217 * Scan code processing
220 kbdputsc(Scan *scan, int c)
225 * e0's is the first of a 2 character sequence, e1 the first
226 * of a 3 character sequence (on the safari)
231 } else if(c == 0xe1){
236 key.down = (c & 0x80) == 0;
243 key.r = kbtabesc1[key.c];
245 key.r = kbtabshift[key.c];
247 key.r = kbtabaltgr[key.c];
249 key.r = kbtabctl[key.c];
251 key.r = kbtab[key.c];
268 key.b = kbtab[key.c];
270 if(scan->caps && key.r<='z' && key.r>='a')
273 if(scan->ctl && scan->alt && key.r == Kdel)
285 scan->shift = key.down;
288 scan->ctl = key.down;
291 scan->altgr = key.down;
294 scan->alt = key.down;
297 scan->num ^= key.down;
300 scan->caps ^= key.down;
306 setleds(Scan *scan, int leds)
310 if(ledsfd < 0 || scan->leds == leds)
313 snprint(buf, sizeof(buf), "%d", leds);
314 pwrite(ledsfd, buf, strlen(buf), 0);
319 * Read scan codes from scanfd
328 threadsetname("scanproc");
330 memset(&scan, 0, sizeof scan);
331 while((n = read(scanfd, buf, sizeof buf)) > 0){
333 kbdputsc(&scan, buf[i]);
334 setleds(&scan, (scan.num<<1) | (scan.caps<<2));
339 utfconv(Rune *r, int n)
344 l = runenlen(r, n) + 1;
346 for(p = s; n > 0; r++, n--)
347 p += runetochar(p, r);
353 * Read key events from keychan and produce characters to
354 * rawchan and keystate in kbdchan. this way here is only
355 * one global keystate even if multiple keyboards are used.
366 threadsetname("keyproc");
369 while(recv(keychan, &key) > 0){
370 if(key.down && key.r)
371 send(rawchan, &key.r);
374 for(i=0; i<nb && cb[i] != key.c; i++)
377 while(i < nb && cb[i] == key.c){
378 memmove(cb+i, cb+i+1, (nb-i+1) * sizeof(cb[0]));
379 memmove(rb+i+1, rb+i+2, (nb-i+1) * sizeof(rb[0]));
383 } else if(i == nb && nb < nelem(cb) && key.b){
387 if(nb < nelem(cb) && key.r && key.b != key.r){
394 if(rb[0] && kbdopen){
395 s = utfconv(rb, nb+1);
396 if(nbsendp(kbdchan, s) <= 0)
403 * Read characters from consfd (serial console)
408 char *p, *e, *x, buf[64];
412 threadsetname("consproc");
416 e = buf + sizeof(buf);
417 while((n = read(consfd, p, e - p)) > 0){
419 while(p < x && fullrune(p, x - p)){
420 p += chartorune(&r, p);
438 nextrune(Channel *ch, Rune *r)
440 while(recv(ch, r) > 0){
447 /* ignore modifiers */
452 /* composing escapes */
461 * Read runes from rawchan, possibly compose special characters
462 * and output the new runes to runechan
468 char *ld; /* must be seen before using this conversion */
469 char *si; /* options for last input characters */
470 Rune *so; /* the corresponding Rune for each si entry */
478 threadsetname("runeproc");
481 while((i = nextrune(rawchan, &r)) >= 0){
495 * emulators like qemu and vmware use Ctrl+Alt to lock
496 * keyboard input so dont confuse them for a compose
502 if(nextrune(rawchan, &r))
507 for(i = 0; i<4; i++){
508 if(nextrune(rawchan, &rr))
511 if(rr >= '0' && rr <= '9')
513 else if(rr >= 'a' && rr <= 'f')
514 r |= 10 + (rr - 'a');
515 else if(rr >= 'A' && rr <= 'F')
516 r |= 10 + (rr - 'A');
523 if(nextrune(rawchan, &rr))
525 for(i = 0; i<nelem(tab); i++){
526 if(tab[i].ld[0] != r)
528 if(tab[i].ld[1] == 0)
530 if(tab[i].ld[1] == rr){
531 nextrune(rawchan, &rr);
535 if(i == nelem(tab) || rr == 0)
537 for(j = 0; tab[i].si[j]; j++){
538 if(tab[i].si[j] != rr)
548 * Cook lines for cons
560 threadsetname("lineproc");
568 case '\0': /* flush */
571 case '\b': /* backspace */
590 } while(!done && nr < nelem(rb));
592 if(nbsendp(conschan, s) <= 0)
598 * Reads Tread and Tflush requests from reqchan and responds
599 * to them with data received on the string channel.
604 enum { AREQ, ASTR, AEND };
611 threadsetname("reqproc");
620 a[AREQ].op = CHANRCV;
621 a[AREQ].c = ac[0]; /* chan(Req*) */
624 a[ASTR].c = ac[1]; /* chan(char*) */
627 a[AEND].op = CHANEND;
630 a[ASTR].op = s ? CHANNOP : CHANRCV;
634 if(r->ifcall.type == Tflush){
637 for(rr = &q; *rr; rr=xx){
639 if(*rr == r->oldreq){
640 if((*rr = *xx) == nil)
642 respond(r->oldreq, Eintr);
648 } else if(r->ifcall.type != Tread){
663 if((q = q->aux) == nil)
667 if(p == s && r->fid->qid.path == Qkbd)
668 e++; /* send terminating \0 if its kbd file */
670 if(n > r->ifcall.count)
674 memmove(r->ofcall.data, p, n);
688 * Keep track of rawing state and distribute the runes from
689 * runechan to the right channels depending on the state.
694 Channel *cook, *aconsr[2], *akbdr[2];
695 enum { ACTL, ARUNE, AEND };
701 threadsetname("ctlproc");
704 proccreate(scanproc, nil, STACK); /* scanfd -> keychan */
706 proccreate(consproc, nil, STACK); /* consfd -> runechan */
708 threadcreate(keyproc, nil, STACK); /* keychan -> rawchan, kbdchan */
709 threadcreate(runeproc, nil, STACK); /* rawchan -> runechan */
711 aconsr[0] = consreqchan;
712 aconsr[1] = conschan;
713 threadcreate(reqproc, aconsr, STACK); /* consreqchan,conschan -> respond */
715 akbdr[0] = kbdreqchan;
717 threadcreate(reqproc, akbdr, STACK); /* kbdreqchan,kbdchan -> respond */
719 cook = chancreate(sizeof(Rune), 0);
720 threadcreate(lineproc, cook, STACK); /* cook -> conschan */
726 a[ACTL].op = CHANRCV;
728 a[ARUNE].c = runechan;
730 a[ARUNE].op = CHANRCV;
732 a[AEND].op = CHANEND;
740 if(raw = (c == Rawon)){
748 s = emalloc9p(UTFmax+2);
750 s[1+runetochar(s+1, &r)] = 0;
751 if(nbsendp(kbdchan, s) <= 0)
756 s = emalloc9p(UTFmax+1);
757 s[runetochar(s, &r)] = 0;
758 if(nbsendp(conschan, s) <= 0)
769 * Keyboard layout maps
773 kbmapent(int t, int sc)
775 if(sc < 0 || sc >= Nscan)
783 return &kbtabshift[sc];
785 return &kbtabesc1[sc];
787 return &kbtabaltgr[sc];
789 return &kbtabctl[sc];
800 off = req->ifcall.offset/(sizeof(tmp)-1);
803 if(rp = kbmapent(t, sc))
804 sprint(tmp, "%11d %11d %11d\n", t, sc, *rp);
808 if(req->ifcall.count < n)
809 n = req->ifcall.count;
810 req->ofcall.count = n;
811 memmove(req->ofcall.data, tmp, n);
818 char line[100], *lp, *b;
824 b = req->ifcall.data;
825 l = req->ifcall.count;
828 strcpy(line, f->aux);
829 lp = line+strlen(line);
835 if(lp[-1] == '\n' || lp == &line[sizeof(line)-1]) {
839 respond(req, Ebadarg);
842 if(*line == '\n' || *line == '#'){
847 while(*lp == ' ' || *lp == '\t')
849 t = strtoul(line, &lp, 0);
850 sc = strtoul(lp, &lp, 0);
851 while(*lp == ' ' || *lp == '\t')
853 if((rp = kbmapent(t, sc)) == nil)
856 if(*lp == '\'' && lp[1])
857 chartorune(&r, lp+1);
858 else if(*lp == '^' && lp[1]){
859 chartorune(&r, lp+1);
860 if(0x40 <= r && r < 0x60)
864 }else if(*lp == 'M' && ('1' <= lp[1] && lp[1] <= '5'))
865 r = 0xF900+lp[1]-'0';
866 else if(*lp>='0' && *lp<='9') /* includes 0x... */
867 r = strtoul(lp, &lp, 0);
876 f->aux = lp = emalloc9p(l+1);
877 memmove(lp, line, l);
880 req->ofcall.count = req->ifcall.count;
891 static char user[64];
897 if((fd = open("/dev/user", OREAD)) < 0)
898 strcpy(user, "none");
900 n = read(fd, user, (sizeof user)-1);
903 strcpy(user, "none");
911 fillstat(ulong qid, Dir *d)
915 memset(d, 0, sizeof *d);
919 d->qid = (Qid){qid, 0, 0};
923 d->qid.type = t->type;
933 spec = r->ifcall.aname;
935 respond(r, Ebadspec);
938 r->fid->qid = (Qid){Qroot, 0, QTDIR};
939 r->ofcall.qid = r->fid->qid;
946 fillstat((ulong)r->fid->qid.path, &r->d);
947 r->d.name = estrdup9p(r->d.name);
948 r->d.uid = estrdup9p(r->d.uid);
949 r->d.gid = estrdup9p(r->d.gid);
950 r->d.muid = estrdup9p(r->d.muid);
955 fswalk1(Fid *fid, char *name, Qid *qid)
960 path = fid->qid.path;
963 if (strcmp(name, "..") == 0) {
964 *qid = (Qid){Qroot, 0, QTDIR};
968 for(i = fid->qid.path; i<Nqid; i++){
969 if(strcmp(name, qtab[i].name) != 0)
971 *qid = (Qid){i, 0, 0};
986 static int need[4] = { 4, 2, 6, 1 };
991 t = qtab + f->qid.path;
992 n = need[r->ifcall.mode & 3]<<6;
993 if((n & t->mode) != n)
997 switch((ulong)f->qid.path){
1014 readtopdir(Fid*, uchar *buf, long off, int cnt, int blen)
1022 for (i = 1; i < Nqid; i++){
1024 m = convD2M(&d, &buf[n], blen-n);
1026 if(m <= BIT16SZ || m > cnt)
1042 switch((ulong)f->qid.path){
1047 r->ofcall.count = readtopdir(f, (void*)r->ofcall.data, r->ifcall.offset,
1048 r->ifcall.count, r->ifcall.count);
1051 sendp(kbdreqchan, r);
1054 sendp(consreqchan, r);
1071 switch((ulong)f->qid.path){
1077 n = r->ifcall.count;
1078 if(write(1, r->ifcall.data, n) != n){
1082 r->ofcall.count = n;
1087 n = r->ifcall.count;
1088 if(n >= 5 && memcmp(p, "rawon", 5) == 0)
1089 sendul(ctlchan, Rawon);
1090 else if(n >= 6 && memcmp(p, "rawoff", 6) == 0)
1091 sendul(ctlchan, Rawoff);
1093 respond(r, Ebadarg);
1096 r->ofcall.count = n;
1101 f->aux = emalloc9p(sizeof(Scan));
1102 memset(f->aux, 0, sizeof(Scan));
1104 for(i=0; i<r->ifcall.count; i++)
1105 kbdputsc((Scan*)f->aux, (uchar)r->ifcall.data[i]);
1106 r->ofcall.count = i;
1120 switch((ulong)r->oldreq->fid->qid.path) {
1122 sendp(kbdreqchan, r);
1125 sendp(consreqchan, r);
1132 fsdestroyfid(Fid *f)
1137 switch((ulong)f->qid.path){
1149 if(--consctlopen == 0)
1150 sendul(ctlchan, Rawoff);
1158 threadexitsall(nil);
1169 .destroyfid= fsdestroyfid,
1181 if((fd = open("/dev/reboot", OWRITE)) < 0){
1182 fprint(2, "can't open /dev/reboot: %r\n");
1185 fprint(fd, "reboot\n");
1199 snprint(buf, sizeof(buf), "/proc/%d/ctl", getpid());
1200 if((fd = open(buf, OWRITE)) < 0){
1201 fprint(2, "can't open %s: %r\n", buf);
1205 /* get higher than normal priority */
1206 fprint(fd, "pri 16\n");
1208 /* always present in physical memory */
1209 fprint(fd, "noswap\n");
1211 /* dont let anybody kill us */
1212 if(d = dirfstat(fd)){
1214 nd.mode = d->mode & ~0222;
1226 fprint(2, "usage: %s [ -dD ] [ -s srv ] [ -m mntpnt ] [ file ]\n", argv0);
1231 threadmain(int argc, char** argv)
1233 char *mtpt = "/dev";
1246 srv = EARGF(usage());
1249 mtpt = EARGF(usage());
1255 if((scanfd = open("/dev/scancode", OREAD)) < 0)
1256 fprint(2, "%s: warning: can't open /dev/scancode: %r\n", argv0);
1257 if((ledsfd = open("/dev/leds", OWRITE)) < 0)
1258 fprint(2, "%s: warning: can't open /dev/leds: %r\n", argv0);
1261 if((consfd = open(*argv, OREAD)) < 0)
1262 fprint(2, "%s: warning: can't open %s: %r\n", argv0, *argv);
1264 consreqchan = chancreate(sizeof(Req*), 0);
1265 kbdreqchan = chancreate(sizeof(Req*), 0);
1267 keychan = chancreate(sizeof(Key), 8);
1268 ctlchan = chancreate(sizeof(int), 0);
1269 rawchan = chancreate(sizeof(Rune), 0);
1270 runechan = chancreate(sizeof(Rune), 32);
1271 conschan = chancreate(sizeof(char*), 16);
1272 kbdchan = chancreate(sizeof(char*), 16);
1275 procrfork(ctlproc, nil, STACK, RFNAMEG|RFNOTEG);
1276 threadpostmountsrv(&fs, srv, mtpt, MBEFORE);