27 typedef struct Key Key;
28 typedef struct Scan Scan;
32 Rune b; /* button, unshifted key */
33 Rune r; /* rune, shifted key */
82 char Eshort[] = "read count too small";
83 char Ebadarg[] = "invalid argument";
84 char Eperm[] = "permission denied";
85 char Einuse[] = "file in use";
86 char Enonexist[] = "file does not exist";
87 char Ebadspec[] = "bad attach specifier";
88 char Ewalk[] = "walk in non directory";
89 char Ephase[] = "the front fell off";
90 char Eintr[] = "interrupted";
105 char *mntpt = "/dev";
109 Channel *keychan; /* chan(Key) */
110 Channel *mctlchan; /* chan(Key) */
112 Channel *kbdreqchan; /* chan(Req*) */
113 Channel *consreqchan; /* chan(Req*) */
115 Channel *ctlchan; /* chan(int) */
117 Channel *rawchan; /* chan(Rune) */
118 Channel *runechan; /* chan(Rune) */
120 Channel *conschan; /* chan(char*) */
121 Channel *kbdchan; /* chan(char*) */
122 Channel *intchan; /* chan(int) */
125 * The codes at 0x79 and 0x7b are produced by the PFU Happy Hacking keyboard.
126 * A 'standard' keyboard doesn't produce anything above 0x58.
130 [0x00] 0, 0x1b, '1', '2', '3', '4', '5', '6',
131 [0x08] '7', '8', '9', '0', '-', '=', '\b', '\t',
132 [0x10] 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
133 [0x18] 'o', 'p', '[', ']', '\n', Kctl, 'a', 's',
134 [0x20] 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
135 [0x28] '\'', '`', Kshift, '\\', 'z', 'x', 'c', 'v',
136 [0x30] 'b', 'n', 'm', ',', '.', '/', Kshift, '*',
137 [0x38] Kalt, ' ', Kctl, KF|1, KF|2, KF|3, KF|4, KF|5,
138 [0x40] KF|6, KF|7, KF|8, KF|9, KF|10, Knum, Kscroll,'7',
139 [0x48] '8', '9', '-', '4', '5', '6', '+', '1',
140 [0x50] '2', '3', '0', '.', 0, 0, 0, KF|11,
141 [0x58] KF|12, 0, 0, 0, 0, 0, 0, 0,
142 [0x60] 0, 0, 0, 0, 0, 0, 0, 0,
143 [0x68] 0, 0, 0, 0, 0, 0, 0, 0,
144 [0x70] 0, 0, 0, 0, 0, 0, 0, 0,
145 [0x78] 0, Kdown, 0, Kup, 0, 0, 0, 0,
148 Rune kbtabshift[Nscan] =
150 [0x00] 0, 0x1b, '!', '@', '#', '$', '%', '^',
151 [0x08] '&', '*', '(', ')', '_', '+', '\b', '\t',
152 [0x10] 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
153 [0x18] 'O', 'P', '{', '}', '\n', Kctl, 'A', 'S',
154 [0x20] 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
155 [0x28] '"', '~', Kshift, '|', 'Z', 'X', 'C', 'V',
156 [0x30] 'B', 'N', 'M', '<', '>', '?', Kshift, '*',
157 [0x38] Kalt, ' ', Kctl, KF|1, KF|2, KF|3, KF|4, KF|5,
158 [0x40] KF|6, KF|7, KF|8, KF|9, KF|10, Knum, Kscroll,'7',
159 [0x48] '8', '9', '-', '4', '5', '6', '+', '1',
160 [0x50] '2', '3', '0', '.', 0, 0, 0, KF|11,
161 [0x58] KF|12, 0, 0, 0, 0, 0, 0, 0,
162 [0x60] 0, 0, 0, 0, 0, 0, 0, 0,
163 [0x68] 0, 0, 0, 0, 0, 0, 0, 0,
164 [0x70] 0, 0, 0, 0, 0, 0, 0, 0,
165 [0x78] 0, Kdown, 0, Kup, 0, 0, 0, 0,
168 Rune kbtabesc1[Nscan] =
170 [0x00] 0, 0, 0, 0, 0, 0, 0, 0,
171 [0x08] 0, 0, 0, 0, 0, 0, 0, 0,
172 [0x10] 0, 0, 0, 0, 0, 0, 0, 0,
173 [0x18] 0, 0, 0, 0, '\n', Kctl, 0, 0,
174 [0x20] 0, 0, 0, 0, 0, 0, 0, 0,
175 [0x28] 0, 0, 0, 0, 0, 0, 0, 0,
176 [0x30] 0, 0, 0, 0, 0, '/', 0, Kprint,
177 [0x38] Kaltgr, 0, 0, 0, 0, 0, 0, 0,
178 [0x40] 0, 0, 0, 0, 0, 0, Kbreak, Khome,
179 [0x48] Kup, Kpgup, 0, Kleft, 0, Kright, 0, Kend,
180 [0x50] Kdown, Kpgdown,Kins, Kdel, 0, 0, 0, 0,
181 [0x58] 0, 0, 0, 0, 0, 0, 0, 0,
182 [0x60] 0, 0, 0, 0, 0, 0, 0, 0,
183 [0x68] 0, 0, 0, 0, 0, 0, 0, 0,
184 [0x70] 0, 0, 0, 0, 0, 0, 0, 0,
185 [0x78] 0, Kup, 0, 0, 0, 0, 0, 0,
188 Rune kbtabshiftesc1[Nscan] =
190 [0x00] 0, 0, 0, 0, 0, 0, 0, 0,
191 [0x08] 0, 0, 0, 0, 0, 0, 0, 0,
192 [0x10] 0, 0, 0, 0, 0, 0, 0, 0,
193 [0x18] 0, 0, 0, 0, 0, 0, 0, 0,
194 [0x20] 0, 0, 0, 0, 0, 0, 0, 0,
195 [0x28] 0, 0, 0, 0, 0, 0, 0, 0,
196 [0x30] 0, 0, 0, 0, 0, 0, 0, 0,
197 [0x38] 0, 0, 0, 0, 0, 0, 0, 0,
198 [0x40] 0, 0, 0, 0, 0, 0, 0, 0,
199 [0x48] Kup, 0, 0, 0, 0, 0, 0, 0,
200 [0x50] 0, 0, 0, 0, 0, 0, 0, 0,
201 [0x58] 0, 0, 0, 0, 0, 0, 0, 0,
202 [0x60] 0, 0, 0, 0, 0, 0, 0, 0,
203 [0x68] 0, 0, 0, 0, 0, 0, 0, 0,
204 [0x70] 0, 0, 0, 0, 0, 0, 0, 0,
205 [0x78] 0, Kup, 0, 0, 0, 0, 0, 0,
208 Rune kbtabctrlesc1[Nscan] =
210 [0x00] 0, 0, 0, 0, 0, 0, 0, 0,
211 [0x08] 0, 0, 0, 0, 0, 0, 0, 0,
212 [0x10] 0, 0, 0, 0, 0, 0, 0, 0,
213 [0x18] 0, 0, 0, 0, 0, 0, 0, 0,
214 [0x20] 0, 0, 0, 0, 0, 0, 0, 0,
215 [0x28] 0, 0, 0, 0, 0, 0, 0, 0,
216 [0x30] 0, 0, 0, 0, 0, 0, 0, 0,
217 [0x38] 0, 0, 0, 0, 0, 0, 0, 0,
218 [0x40] 0, 0, 0, 0, 0, 0, 0, 0,
219 [0x48] Kup, 0, 0, 0, 0, 0, 0, 0,
220 [0x50] 0, 0, 0, 0, 0, 0, 0, 0,
221 [0x58] 0, 0, 0, 0, 0, 0, 0, 0,
222 [0x60] 0, 0, 0, 0, 0, 0, 0, 0,
223 [0x68] 0, 0, 0, 0, 0, 0, 0, 0,
224 [0x70] 0, 0, 0, 0, 0, 0, 0, 0,
225 [0x78] 0, Kup, 0, 0, 0, 0, 0, 0,
228 Rune kbtabaltgr[Nscan] =
230 [0x00] 0, 0, 0, 0, 0, 0, 0, 0,
231 [0x08] 0, 0, 0, 0, 0, 0, 0, 0,
232 [0x10] 0, 0, 0, 0, 0, 0, 0, 0,
233 [0x18] 0, 0, 0, 0, '\n', Kctl, 0, 0,
234 [0x20] 0, 0, 0, 0, 0, 0, 0, 0,
235 [0x28] 0, 0, Kshift, 0, 0, 0, 0, 0,
236 [0x30] 0, 0, 0, 0, 0, '/', 0, Kprint,
237 [0x38] Kaltgr, 0, 0, 0, 0, 0, 0, 0,
238 [0x40] 0, 0, 0, 0, 0, 0, Kbreak, Khome,
239 [0x48] Kup, Kpgup, 0, Kleft, 0, Kright, 0, Kend,
240 [0x50] Kdown, Kpgdown,Kins, Kdel, 0, 0, 0, 0,
241 [0x58] 0, 0, 0, 0, 0, 0, 0, 0,
242 [0x60] 0, 0, 0, 0, 0, 0, 0, 0,
243 [0x68] 0, 0, 0, 0, 0, 0, 0, 0,
244 [0x70] 0, 0, 0, 0, 0, 0, 0, 0,
245 [0x78] 0, Kup, 0, 0, 0, 0, 0, 0,
248 Rune kbtabctl[Nscan] =
250 [0x00] 0, '
\e', '
\11', '
\12', '
\13', '
\14', '
\15', '
\16',
251 [0x08] '
\17', '
\18', '
\19', '
\10', '
\r', '
\1d', '\b', '\t',
252 [0x10] '
\11', '
\17', '
\ 5', '
\12', '
\14', '
\19', '
\15', '\t',
253 [0x18] '
\ f', '
\10', '
\e', '
\1d', '\n', Kctl, '
\ 1', '
\13',
254 [0x20] '
\ 4', '
\ 6', '
\a', '\b', '\n', '
\v', '
\f', '
\e',
255 [0x28] '
\a', 0, Kshift, '
\1c', '
\1a', '
\18', '
\ 3', '
\16',
256 [0x30] '
\ 2', '
\ e', '
\r', '
\f', '
\ e', '
\ f', Kshift, '\n',
257 [0x38] Kalt, 0, Kctl, '
\ 5', '
\ 6', '
\a', '
\ 4', '
\ 5',
258 [0x40] '
\ 6', '
\a', '
\f', '
\r', '
\ e', '
\ 5', '
\ 6', '
\17',
259 [0x48] '
\18', '
\19', '
\r', '
\14', '
\15', '
\16', '
\v', '
\11',
260 [0x50] '
\12', '
\13', '
\10', '
\ e', 0, 0, 0, '
\ f',
261 [0x58] '
\f', 0, 0, 0, 0, 0, 0, 0,
262 [0x60] 0, 0, 0, 0, 0, 0, 0, 0,
263 [0x68] 0, 0, 0, 0, 0, 0, 0, 0,
264 [0x70] 0, 0, 0, 0, 0, 0, 0, 0,
265 [0x78] 0, '
\a', 0, '\b', 0, 0, 0, 0,
268 Rune kbtabshiftaltgr[Nscan] =
270 [0x00] 0, 0, 0, 0, 0, 0, 0, 0,
271 [0x08] 0, 0, 0, 0, 0, 0, 0, 0,
272 [0x10] 0, 0, 0, 0, 0, 0, 0, 0,
273 [0x18] 0, 0, 0, 0, 0, 0, 0, 0,
274 [0x20] 0, 0, 0, 0, 0, 0, 0, 0,
275 [0x28] 0, 0, 0, 0, 0, 0, 0, 0,
276 [0x30] 0, 0, 0, 0, 0, 0, 0, 0,
277 [0x38] 0, 0, 0, 0, 0, 0, 0, 0,
278 [0x40] 0, 0, 0, 0, 0, 0, 0, 0,
279 [0x48] 0, 0, 0, 0, 0, 0, 0, 0,
280 [0x50] 0, 0, 0, 0, 0, 0, 0, 0,
281 [0x58] 0, 0, 0, 0, 0, 0, 0, 0,
282 [0x60] 0, 0, 0, 0, 0, 0, 0, 0,
283 [0x68] 0, 0, 0, 0, 0, 0, 0, 0,
284 [0x70] 0, 0, 0, 0, 0, 0, 0, 0,
285 [0x78] 0, 0, 0, 0, 0, 0, 0, 0,
291 static char *buf = nil;
293 buf = smprint("%s/%s", mntpt, file);
298 eopen(char *name, int mode)
302 fd = open(name, mode);
304 fprint(2, "%s: warning: can't open %s: %r\n", argv0, name);
316 if((fd = eopen(dev("reboot"), OWRITE)) < 0)
318 fprint(fd, "reboot\n");
326 write(notefd, "hangup", 6);
328 write(killfd, "hangup", 6);
333 * Scan code processing
336 kbdputsc(Scan *scan, int c)
341 * e0's is the first of a 2 character sequence, e1 and e2 the first
342 * of a 3 character sequence (on the safari)
347 } else if(c == 0xe1 || c == 0xe2){
350 } else if(c == 0xe0){
355 key.down = (c & 0x80) == 0;
361 /* qemu workarround: emulate e0 for numpad */
362 if(c != 0 && strchr("GHIKMOPQRS", c) != nil)
363 scan->esc1 |= !scan->num;
365 if(scan->esc1 && scan->ctl && kbtabctrlesc1[c] != 0)
366 key.r = kbtabctrlesc1[c];
367 else if(scan->esc1 && scan->shift && kbtabshiftesc1[c] != 0)
368 key.r = kbtabshiftesc1[c];
370 key.r = kbtabesc1[c];
371 else if(scan->shift && scan->altgr && kbtabshiftaltgr[c] != 0)
372 key.r = kbtabshiftaltgr[c];
374 key.r = kbtabshift[c];
376 key.r = kbtabaltgr[c];
382 if(scan->esc1 || kbtab[c] == 0)
387 if(scan->caps && key.r<='z' && key.r>='a')
390 if(scan->ctl && scan->alt && key.r == Kdel)
398 scan->shift = key.down;
401 scan->ctl = key.down;
404 scan->altgr = key.down;
407 scan->alt = key.down;
410 scan->num ^= key.down;
413 scan->caps ^= key.down;
420 kbdin(Scan *a, char *p, int n)
426 if(n > 0 && p[n-1] != 0){
428 * old format as used by bitsy keyboard:
429 * just a string of characters, no keyup
437 p += chartorune(&k.r, p);
451 chartorune(&k.r, p+1);
455 k.down = (p[0] == 'r');
456 for(i=0; i<Nscan; i++){
457 if(kbtab[i] == k.r || kbtabshift[i] == k.r || (i >= 16 && kbtabctl[i] == k.r)){
458 /* assign button from kbtab */
460 /* handle ^X forms */
461 if(k.r == kbtab[i] && kbtabctl[i] && !a->shift && !a->altgr && a->ctl)
466 /* button unknown to kbtab, use rune if no modifier keys are active */
467 if(k.b == 0 && !a->shift && !a->altgr && !a->ctl)
471 else if(k.r == Kaltgr)
479 chartorune(&k.r, p+1);
480 nbsend(runechan, &k.r);
489 if(nbsendp(kbdchan, s) <= 0)
498 setleds(Scan *scan, int leds)
502 if(ledsfd < 0 || scan->leds == leds)
505 snprint(buf, sizeof(buf), "%d", leds);
506 pwrite(ledsfd, buf, strlen(buf), 0);
511 * Read scan codes from scanfd
520 threadsetname("scanproc");
522 memset(&scan, 0, sizeof scan);
523 while((n = read(scanfd, buf, sizeof buf)) > 0){
525 kbdputsc(&scan, buf[i]);
526 setleds(&scan, (scan.num<<1) | (scan.caps<<2));
539 threadsetname("kbdiproc");
541 memset(&a, 0, sizeof(a));
542 while((n = read(kbdifd, buf, sizeof buf)) > 0)
549 utfconv(Rune *r, int n)
554 l = runenlen(r, n) + 1;
556 for(p = s; n > 0; r++, n--)
557 p += runetochar(p, r);
563 * Read key events from keychan and produce characters to
564 * rawchan and keystate in kbdchan.
574 threadsetname("keyproc");
577 while(recv(keychan, &key) > 0){
578 if(key.r >= Kmouse+1 && key.r <= Kmouse+5){
580 send(mctlchan, &key);
585 for(i=0; i<nb && rb[i+1] != key.b; i++)
588 while(i < nb && rb[i+1] == key.b){
589 memmove(rb+i+1, rb+i+2, (nb-i+1) * sizeof(rb[0]));
593 } else if(i == nb && nb < nelem(rb)-1 && key.b){
600 s = utfconv(rb, nb+1);
601 if(nbsendp(kbdchan, s) <= 0)
605 send(mctlchan, &key);
607 if(key.down && key.r)
608 send(rawchan, &key.r);
613 * Read characters from consfd (serial console)
618 char *p, *e, *x, buf[64];
622 threadsetname("consproc");
626 e = buf + sizeof(buf);
627 while((n = read(consfd, p, e - p)) > 0){
630 while((n = x - p) > 0){
635 p += chartorune(&r, p);
636 if(r == 021 || r == 023) /* XON/XOFF */
638 if(r == 0 || r == Runeerror){
658 nextrune(Channel *ch, Rune *r)
660 while(recv(ch, r) > 0){
667 /* ignore modifiers */
672 /* composing escapes */
681 * Read runes from rawchan, possibly compose special characters
682 * and output the new runes to runechan
688 char *ld; /* must be seen before using this conversion */
689 char *si; /* options for last input characters */
690 Rune *so; /* the corresponding Rune for each si entry */
698 threadsetname("runeproc");
701 while((i = nextrune(rawchan, &r)) >= 0){
715 * emulators like qemu and vmware use Ctrl+Alt to lock
716 * keyboard input so dont confuse them for a compose
722 if(nextrune(rawchan, &r))
725 if(r == 'x' || r == 'X'){
726 i = (r == 'X') ? 4 : 6;
729 if(nextrune(rawchan, &rr))
731 if(rr >= '0' && rr <= '9')
732 r = (r << 4) | (rr - '0');
733 else if(rr >= 'a' && rr <= 'f')
734 r = (r << 4) | (10 + (rr - 'a'));
735 else if(rr >= 'A' && rr <= 'F')
736 r = (r << 4) | (10 + (rr - 'A'));
740 if((i == 0 || rr == ';') && r != 0 && r <= Runemax)
743 if(nextrune(rawchan, &rr))
745 for(i = 0; i<nelem(tab); i++){
746 if(tab[i].ld[0] != r)
748 if(tab[i].ld[1] == 0)
750 if(tab[i].ld[1] == rr){
751 nextrune(rawchan, &rr);
755 if(i == nelem(tab) || rr == 0)
757 for(j = 0; tab[i].si[j]; j++){
758 if(tab[i].si[j] != rr)
768 * Need to do this in a separate proc because if process we're interrupting
769 * is dying and trying to print tombstone, kernel is blocked holding p->debug lock.
774 threadsetname("intrproc");
776 while(recv(intchan, nil) > 0)
777 write(notefd, "interrupt", 9);
781 * Process Kmouse keys and mouse button swap on shift,
782 * unblank screen by twiching.
790 threadsetname("mctlproc");
793 if(nbrecv(mctlchan, &key) <= 0){
795 fprint(mctlfd, "twitch");
796 if(recv(mctlchan, &key) <= 0)
800 if(mctlfd >= 0 && key.r == Kshift){
802 fprint(mctlfd, "buttonmap 132");
804 fprint(mctlfd, "swap");
805 fprint(mctlfd, "swap");
810 if(msinfd >= 0 && key.r >= Kmouse+1 && key.r <= Kmouse+5){
811 i = 1<<(key.r-(Kmouse+1));
816 fprint(msinfd, "m%11d %11d %11d", 0, 0, mouseb);
823 * Cook lines for cons
835 threadsetname("lineproc");
844 if(nbsend(intchan, ¬efd) <= 0)
847 case '\0': /* flush */
850 case Kbs: /* ^H: erase character */
851 case Knack: /* ^U: erase line */
852 case Ketb: /* ^W: erase word */
858 if(r == Ketb && utfrune(" \t", rb[nr]))
862 case Keof: /* ^D: eof */
872 } while(!done && nr < nelem(rb));
874 if(nbsendp(conschan, s) <= 0)
880 * Reads Tread and Tflush requests from reqchan and responds
881 * to them with data received on the string channel.
886 enum { AREQ, ASTR, AEND };
893 threadsetname("reqproc");
903 a[AREQ].op = CHANRCV;
904 a[AREQ].c = ac[0]; /* chan(Req*) */
907 a[ASTR].c = ac[1]; /* chan(char*) */
910 a[AEND].op = CHANEND;
913 a[ASTR].op = s != nil ? CHANNOP : CHANRCV;
917 if(r->ifcall.type == Tflush){
920 for(rr = &q; *rr; rr=xx){
922 if(*rr == r->oldreq){
923 if((*rr = *xx) == nil)
925 respond(r->oldreq, Eintr);
931 } else if(r->ifcall.type != Tread){
944 while(s != nil && q != nil){
946 if((q = q->aux) == nil)
952 if(r->fid->qid.path == Qkbd)
953 e++; /* send terminating \0 if its kbd file */
956 m = r->ifcall.count - r->ofcall.count;
958 if(r->ofcall.count > 0){
964 memmove((char*)r->ofcall.data + r->ofcall.count, p, n);
965 r->ofcall.count += n;
969 s = nbrecvp(a[ASTR].c);
982 * Keep track of rawing state and distribute the runes from
983 * runechan to the right channels depending on the state.
988 Channel *cook, *aconsr[2], *akbdr[2];
989 enum { ACTL, ARUNE, AEND };
995 threadsetname("ctlproc");
998 proccreate(kbdiproc, nil, STACK); /* kbdifd -> kbdin() */
999 if(mctlfd >= 0 || msinfd >= 0)
1000 proccreate(mctlproc, nil, STACK); /* mctlchan -> mctlfd, msinfd */
1002 proccreate(scanproc, nil, STACK); /* scanfd -> keychan */
1004 proccreate(consproc, nil, STACK); /* consfd -> runechan */
1006 proccreate(intrproc, nil, STACK); /* intchan -> notefd */
1008 threadcreate(keyproc, nil, STACK); /* keychan -> mctlchan, rawchan, kbdchan */
1009 threadcreate(runeproc, nil, STACK); /* rawchan -> runechan */
1011 aconsr[0] = consreqchan;
1012 aconsr[1] = conschan;
1013 threadcreate(reqproc, aconsr, STACK); /* consreqchan,conschan -> respond */
1015 akbdr[0] = kbdreqchan;
1017 threadcreate(reqproc, akbdr, STACK); /* kbdreqchan,kbdchan -> respond */
1019 cook = chancreate(sizeof(Rune), 0);
1020 threadcreate(lineproc, cook, STACK); /* cook -> conschan */
1024 a[ACTL].c = ctlchan;
1026 a[ACTL].op = CHANRCV;
1028 a[ARUNE].c = runechan;
1030 a[ARUNE].op = CHANRCV;
1032 a[AEND].op = CHANEND;
1040 if(raw = (c == Rawon)){
1048 s = emalloc9p(UTFmax+2);
1050 s[1+runetochar(s+1, &r)] = 0;
1051 if(nbsendp(kbdchan, s) <= 0)
1056 s = emalloc9p(UTFmax+1);
1057 s[runetochar(s, &r)] = 0;
1058 if(nbsendp(conschan, s) <= 0)
1069 * Keyboard layout maps
1073 kbmapent(int t, int sc)
1075 static Rune *tabs[] = {
1081 /* 5 */ kbtabctrlesc1,
1082 /* 6 */ kbtabshiftesc1,
1083 /* 7 */ kbtabshiftaltgr,
1085 if(t >= 0 && t < nelem(tabs) && sc >= 0 && sc < Nscan)
1086 return &tabs[t][sc];
1097 off = req->ifcall.offset/(sizeof(tmp)-1);
1100 if(rp = kbmapent(t, sc))
1101 sprint(tmp, "%11d %11d %11d\n", t, sc, *rp);
1105 if(req->ifcall.count < n)
1106 n = req->ifcall.count;
1107 req->ofcall.count = n;
1108 memmove(req->ofcall.data, tmp, n);
1115 static Rune o = Spec|0x60, tab[] = {
1116 Kshift, Kbreak, Kctl, Kalt,
1117 Kcaps, Knum, Kmiddle, Kaltgr,
1119 if(r >= o && r < o+nelem(tab))
1125 kbmapwrite(Req *req)
1127 char line[100], *lp, *b;
1133 b = req->ifcall.data;
1134 l = req->ifcall.count;
1137 strcpy(line, f->aux);
1138 lp = line+strlen(line);
1144 if(lp[-1] == '\n' || lp == &line[sizeof(line)-1]) {
1148 respond(req, Ebadarg);
1151 if(*line == '\n' || *line == '#'){
1156 while(*lp == ' ' || *lp == '\t')
1158 t = strtoul(line, &lp, 0);
1159 sc = strtoul(lp, &lp, 0);
1160 while(*lp == ' ' || *lp == '\t')
1162 if((rp = kbmapent(t, sc)) == nil)
1165 if(*lp == '\'' && lp[1])
1166 chartorune(&r, lp+1);
1167 else if(*lp == '^' && lp[1]){
1168 chartorune(&r, lp+1);
1169 if(0x40 <= r && r < 0x60)
1173 }else if(*lp == 'M' && ('1' <= lp[1] && lp[1] <= '5'))
1174 r = Kmouse+lp[1]-'0';
1175 else if(*lp>='0' && *lp<='9') /* includes 0x... */
1176 r = strtoul(lp, &lp, 0);
1185 f->aux = lp = emalloc9p(l+1);
1186 memmove(lp, line, l);
1189 req->ofcall.count = req->ifcall.count;
1200 static char user[64];
1206 if((fd = open("/dev/user", OREAD)) < 0)
1207 strcpy(user, "none");
1209 n = read(fd, user, (sizeof user)-1);
1212 strcpy(user, "none");
1220 fillstat(ulong qid, Dir *d)
1224 memset(d, 0, sizeof *d);
1225 d->uid = getauser();
1226 d->gid = getauser();
1228 d->qid = (Qid){qid, 0, 0};
1232 d->qid.type = t->type;
1242 spec = r->ifcall.aname;
1243 if(spec && spec[0]){
1244 respond(r, Ebadspec);
1247 r->fid->qid = (Qid){Qroot, 0, QTDIR};
1248 r->ofcall.qid = r->fid->qid;
1255 fillstat((ulong)r->fid->qid.path, &r->d);
1256 r->d.name = estrdup9p(r->d.name);
1257 r->d.uid = estrdup9p(r->d.uid);
1258 r->d.gid = estrdup9p(r->d.gid);
1259 r->d.muid = estrdup9p(r->d.muid);
1264 fswalk1(Fid *fid, char *name, Qid *qid)
1269 path = fid->qid.path;
1272 if (strcmp(name, "..") == 0) {
1273 *qid = (Qid){Qroot, 0, QTDIR};
1277 for(i = fid->qid.path; i<Nqid; i++){
1278 if(strcmp(name, qtab[i].name) != 0)
1280 *qid = (Qid){i, 0, 0};
1295 static int need[4] = { 4, 2, 6, 1 };
1300 t = qtab + f->qid.path;
1301 n = need[r->ifcall.mode & 3]<<6;
1302 if((n & t->mode) != n)
1306 switch((ulong)f->qid.path){
1323 readtopdir(Fid*, uchar *buf, long off, int cnt, int blen)
1331 for (i = 1; i < Nqid; i++){
1333 m = convD2M(&d, &buf[n], blen-n);
1335 if(m <= BIT16SZ || m > cnt)
1351 switch((ulong)f->qid.path){
1356 r->ofcall.count = readtopdir(f, (void*)r->ofcall.data, r->ifcall.offset,
1357 r->ifcall.count, r->ifcall.count);
1360 sendp(kbdreqchan, r);
1363 sendp(consreqchan, r);
1381 n = r->ifcall.count;
1382 switch((ulong)f->qid.path){
1388 if(write(1, p, n) != n){
1395 if(n >= 5 && memcmp(p, "rawon", 5) == 0)
1396 sendul(ctlchan, Rawon);
1397 else if(n >= 6 && memcmp(p, "rawoff", 6) == 0)
1398 sendul(ctlchan, Rawoff);
1400 respond(r, Ebadarg);
1408 f->aux = emalloc9p(sizeof(Scan));
1409 memset(f->aux, 0, sizeof(Scan));
1411 if(f->qid.path == Qkbin){
1413 kbdputsc((Scan*)f->aux, (uchar)p[i]);
1415 kbdin((Scan*)f->aux, p, n);
1424 r->ofcall.count = n;
1431 switch((ulong)r->oldreq->fid->qid.path) {
1433 sendp(kbdreqchan, r);
1436 sendp(consreqchan, r);
1443 fsdestroyfid(Fid *f)
1448 switch((ulong)f->qid.path){
1461 if(--consctlopen == 0)
1462 sendul(ctlchan, Rawoff);
1468 procopen(int pid, char *name, int mode)
1472 snprint(buf, sizeof(buf), "/proc/%d/%s", pid, name);
1473 return eopen(buf, mode);
1485 if((fd = procopen(getpid(), "ctl", OWRITE)) < 0)
1488 /* get higher than normal priority */
1489 fprint(fd, "pri 16\n");
1491 /* always present in physical memory */
1492 fprint(fd, "noswap\n");
1494 /* dont let anybody kill us */
1495 if(d = dirfstat(fd)){
1497 nd.mode = d->mode & ~0222;
1508 killfd = procopen(getpid(), "notepg", OWRITE);
1510 proccreate(ctlproc, nil, STACK);
1528 .destroyfid= fsdestroyfid,
1535 fprint(2, "usage: %s [ -qdD ] [ -s sname ] [ -m mntpnt ] [ file ]\n", argv0);
1540 threadmain(int argc, char** argv)
1550 sname = EARGF(usage());
1553 mntpt = EARGF(usage());
1563 consfd = eopen(*argv, OREAD);
1565 kbdifd = open(dev("kbd"), OREAD);
1567 scanfd = eopen(dev("scancode"), OREAD);
1568 ledsfd = eopen(dev("leds"), OWRITE);
1570 mctlfd = eopen(dev("mousectl"), OWRITE);
1571 msinfd = eopen(dev("mousein"), OWRITE);
1573 notefd = procopen(getpid(), "notepg", OWRITE);
1575 consreqchan = chancreate(sizeof(Req*), 0);
1576 kbdreqchan = chancreate(sizeof(Req*), 0);
1578 keychan = chancreate(sizeof(Key), 64);
1579 mctlchan = chancreate(sizeof(Key), 64);
1580 ctlchan = chancreate(sizeof(int), 0);
1581 rawchan = chancreate(sizeof(Rune), 0);
1582 runechan = chancreate(sizeof(Rune), 256);
1583 conschan = chancreate(sizeof(char*), 128);
1584 kbdchan = chancreate(sizeof(char*), 128);
1585 intchan = chancreate(sizeof(int), 0);
1587 threadpostmountsrv(&fs, sname, mntpt, MBEFORE);