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) */
111 Channel *kbdreqchan; /* chan(Req*) */
112 Channel *consreqchan; /* chan(Req*) */
114 Channel *ctlchan; /* chan(int) */
116 Channel *rawchan; /* chan(Rune) */
117 Channel *runechan; /* chan(Rune) */
119 Channel *conschan; /* chan(char*) */
120 Channel *kbdchan; /* chan(char*) */
121 Channel *intchan; /* chan(int) */
124 * The codes at 0x79 and 0x7b are produced by the PFU Happy Hacking keyboard.
125 * A 'standard' keyboard doesn't produce anything above 0x58.
129 [0x00] 0, 0x1b, '1', '2', '3', '4', '5', '6',
130 [0x08] '7', '8', '9', '0', '-', '=', '\b', '\t',
131 [0x10] 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
132 [0x18] 'o', 'p', '[', ']', '\n', Kctl, 'a', 's',
133 [0x20] 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
134 [0x28] '\'', '`', Kshift, '\\', 'z', 'x', 'c', 'v',
135 [0x30] 'b', 'n', 'm', ',', '.', '/', Kshift, '*',
136 [0x38] Kalt, ' ', Kctl, KF|1, KF|2, KF|3, KF|4, KF|5,
137 [0x40] KF|6, KF|7, KF|8, KF|9, KF|10, Knum, Kscroll,'7',
138 [0x48] '8', '9', '-', '4', '5', '6', '+', '1',
139 [0x50] '2', '3', '0', '.', 0, 0, 0, KF|11,
140 [0x58] KF|12, 0, 0, 0, 0, 0, 0, 0,
141 [0x60] 0, 0, 0, 0, 0, 0, 0, 0,
142 [0x68] 0, 0, 0, 0, 0, 0, 0, 0,
143 [0x70] 0, 0, 0, 0, 0, 0, 0, 0,
144 [0x78] 0, Kdown, 0, Kup, 0, 0, 0, 0,
147 Rune kbtabshift[Nscan] =
149 [0x00] 0, 0x1b, '!', '@', '#', '$', '%', '^',
150 [0x08] '&', '*', '(', ')', '_', '+', '\b', '\t',
151 [0x10] 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
152 [0x18] 'O', 'P', '{', '}', '\n', Kctl, 'A', 'S',
153 [0x20] 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
154 [0x28] '"', '~', Kshift, '|', 'Z', 'X', 'C', 'V',
155 [0x30] 'B', 'N', 'M', '<', '>', '?', Kshift, '*',
156 [0x38] Kalt, ' ', Kctl, KF|1, KF|2, KF|3, KF|4, KF|5,
157 [0x40] KF|6, KF|7, KF|8, KF|9, KF|10, Knum, Kscroll,'7',
158 [0x48] '8', '9', '-', '4', '5', '6', '+', '1',
159 [0x50] '2', '3', '0', '.', 0, 0, 0, KF|11,
160 [0x58] KF|12, 0, 0, 0, 0, 0, 0, 0,
161 [0x60] 0, 0, 0, 0, 0, 0, 0, 0,
162 [0x68] 0, 0, 0, 0, 0, 0, 0, 0,
163 [0x70] 0, 0, 0, 0, 0, 0, 0, 0,
164 [0x78] 0, Kdown, 0, Kup, 0, 0, 0, 0,
167 Rune kbtabesc1[Nscan] =
169 [0x00] 0, 0, 0, 0, 0, 0, 0, 0,
170 [0x08] 0, 0, 0, 0, 0, 0, 0, 0,
171 [0x10] 0, 0, 0, 0, 0, 0, 0, 0,
172 [0x18] 0, 0, 0, 0, '\n', Kctl, 0, 0,
173 [0x20] 0, 0, 0, 0, 0, 0, 0, 0,
174 [0x28] 0, 0, 0, 0, 0, 0, 0, 0,
175 [0x30] 0, 0, 0, 0, 0, '/', 0, Kprint,
176 [0x38] Kaltgr, 0, 0, 0, 0, 0, 0, 0,
177 [0x40] 0, 0, 0, 0, 0, 0, Kbreak, Khome,
178 [0x48] Kup, Kpgup, 0, Kleft, 0, Kright, 0, Kend,
179 [0x50] Kdown, Kpgdown,Kins, Kdel, 0, 0, 0, 0,
180 [0x58] 0, 0, 0, 0, 0, 0, 0, 0,
181 [0x60] 0, 0, 0, 0, 0, 0, 0, 0,
182 [0x68] 0, 0, 0, 0, 0, 0, 0, 0,
183 [0x70] 0, 0, 0, 0, 0, 0, 0, 0,
184 [0x78] 0, Kup, 0, 0, 0, 0, 0, 0,
187 Rune kbtabshiftesc1[Nscan] =
189 [0x00] 0, 0, 0, 0, 0, 0, 0, 0,
190 [0x08] 0, 0, 0, 0, 0, 0, 0, 0,
191 [0x10] 0, 0, 0, 0, 0, 0, 0, 0,
192 [0x18] 0, 0, 0, 0, 0, 0, 0, 0,
193 [0x20] 0, 0, 0, 0, 0, 0, 0, 0,
194 [0x28] 0, 0, 0, 0, 0, 0, 0, 0,
195 [0x30] 0, 0, 0, 0, 0, 0, 0, 0,
196 [0x38] 0, 0, 0, 0, 0, 0, 0, 0,
197 [0x40] 0, 0, 0, 0, 0, 0, 0, 0,
198 [0x48] Kup, 0, 0, 0, 0, 0, 0, 0,
199 [0x50] 0, 0, 0, 0, 0, 0, 0, 0,
200 [0x58] 0, 0, 0, 0, 0, 0, 0, 0,
201 [0x60] 0, 0, 0, 0, 0, 0, 0, 0,
202 [0x68] 0, 0, 0, 0, 0, 0, 0, 0,
203 [0x70] 0, 0, 0, 0, 0, 0, 0, 0,
204 [0x78] 0, Kup, 0, 0, 0, 0, 0, 0,
207 Rune kbtabctrlesc1[Nscan] =
209 [0x00] 0, 0, 0, 0, 0, 0, 0, 0,
210 [0x08] 0, 0, 0, 0, 0, 0, 0, 0,
211 [0x10] 0, 0, 0, 0, 0, 0, 0, 0,
212 [0x18] 0, 0, 0, 0, 0, 0, 0, 0,
213 [0x20] 0, 0, 0, 0, 0, 0, 0, 0,
214 [0x28] 0, 0, 0, 0, 0, 0, 0, 0,
215 [0x30] 0, 0, 0, 0, 0, 0, 0, 0,
216 [0x38] 0, 0, 0, 0, 0, 0, 0, 0,
217 [0x40] 0, 0, 0, 0, 0, 0, 0, 0,
218 [0x48] Kup, 0, 0, 0, 0, 0, 0, 0,
219 [0x50] 0, 0, 0, 0, 0, 0, 0, 0,
220 [0x58] 0, 0, 0, 0, 0, 0, 0, 0,
221 [0x60] 0, 0, 0, 0, 0, 0, 0, 0,
222 [0x68] 0, 0, 0, 0, 0, 0, 0, 0,
223 [0x70] 0, 0, 0, 0, 0, 0, 0, 0,
224 [0x78] 0, Kup, 0, 0, 0, 0, 0, 0,
227 Rune kbtabaltgr[Nscan] =
229 [0x00] 0, 0, 0, 0, 0, 0, 0, 0,
230 [0x08] 0, 0, 0, 0, 0, 0, 0, 0,
231 [0x10] 0, 0, 0, 0, 0, 0, 0, 0,
232 [0x18] 0, 0, 0, 0, '\n', Kctl, 0, 0,
233 [0x20] 0, 0, 0, 0, 0, 0, 0, 0,
234 [0x28] 0, 0, Kshift, 0, 0, 0, 0, 0,
235 [0x30] 0, 0, 0, 0, 0, '/', 0, Kprint,
236 [0x38] Kaltgr, 0, 0, 0, 0, 0, 0, 0,
237 [0x40] 0, 0, 0, 0, 0, 0, Kbreak, Khome,
238 [0x48] Kup, Kpgup, 0, Kleft, 0, Kright, 0, Kend,
239 [0x50] Kdown, Kpgdown,Kins, Kdel, 0, 0, 0, 0,
240 [0x58] 0, 0, 0, 0, 0, 0, 0, 0,
241 [0x60] 0, 0, 0, 0, 0, 0, 0, 0,
242 [0x68] 0, 0, 0, 0, 0, 0, 0, 0,
243 [0x70] 0, 0, 0, 0, 0, 0, 0, 0,
244 [0x78] 0, Kup, 0, 0, 0, 0, 0, 0,
247 Rune kbtabctl[Nscan] =
249 [0x00] 0, '
\e', '
\11', '
\12', '
\13', '
\14', '
\15', '
\16',
250 [0x08] '
\17', '
\18', '
\19', '
\10', '
\r', '
\1d', '\b', '\t',
251 [0x10] '
\11', '
\17', '
\ 5', '
\12', '
\14', '
\19', '
\15', '\t',
252 [0x18] '
\ f', '
\10', '
\e', '
\1d', '\n', Kctl, '
\ 1', '
\13',
253 [0x20] '
\ 4', '
\ 6', '
\a', '\b', '\n', '
\v', '
\f', '
\e',
254 [0x28] '
\a', 0, Kshift, '
\1c', '
\1a', '
\18', '
\ 3', '
\16',
255 [0x30] '
\ 2', '
\ e', '
\r', '
\f', '
\ e', '
\ f', Kshift, '\n',
256 [0x38] Kalt, 0, Kctl, '
\ 5', '
\ 6', '
\a', '
\ 4', '
\ 5',
257 [0x40] '
\ 6', '
\a', '
\f', '
\r', '
\ e', '
\ 5', '
\ 6', '
\17',
258 [0x48] '
\18', '
\19', '
\r', '
\14', '
\15', '
\16', '
\v', '
\11',
259 [0x50] '
\12', '
\13', '
\10', '
\ e', 0, 0, 0, '
\ f',
260 [0x58] '
\f', 0, 0, 0, 0, 0, 0, 0,
261 [0x60] 0, 0, 0, 0, 0, 0, 0, 0,
262 [0x68] 0, 0, 0, 0, 0, 0, 0, 0,
263 [0x70] 0, 0, 0, 0, 0, 0, 0, 0,
264 [0x78] 0, '
\a', 0, '\b', 0, 0, 0, 0,
267 Rune kbtabshiftaltgr[Nscan] =
269 [0x00] 0, 0, 0, 0, 0, 0, 0, 0,
270 [0x08] 0, 0, 0, 0, 0, 0, 0, 0,
271 [0x10] 0, 0, 0, 0, 0, 0, 0, 0,
272 [0x18] 0, 0, 0, 0, 0, 0, 0, 0,
273 [0x20] 0, 0, 0, 0, 0, 0, 0, 0,
274 [0x28] 0, 0, 0, 0, 0, 0, 0, 0,
275 [0x30] 0, 0, 0, 0, 0, 0, 0, 0,
276 [0x38] 0, 0, 0, 0, 0, 0, 0, 0,
277 [0x40] 0, 0, 0, 0, 0, 0, 0, 0,
278 [0x48] 0, 0, 0, 0, 0, 0, 0, 0,
279 [0x50] 0, 0, 0, 0, 0, 0, 0, 0,
280 [0x58] 0, 0, 0, 0, 0, 0, 0, 0,
281 [0x60] 0, 0, 0, 0, 0, 0, 0, 0,
282 [0x68] 0, 0, 0, 0, 0, 0, 0, 0,
283 [0x70] 0, 0, 0, 0, 0, 0, 0, 0,
284 [0x78] 0, 0, 0, 0, 0, 0, 0, 0,
290 static char *buf = nil;
292 buf = smprint("%s/%s", mntpt, file);
297 eopen(char *name, int mode)
301 fd = open(name, mode);
303 fprint(2, "%s: warning: can't open %s: %r\n", argv0, name);
315 if((fd = eopen(dev("reboot"), OWRITE)) < 0)
317 fprint(fd, "reboot\n");
325 write(notefd, "hangup", 6);
327 write(killfd, "hangup", 6);
332 * Scan code processing
335 kbdputsc(Scan *scan, int c)
340 * e0's is the first of a 2 character sequence, e1 and e2 the first
341 * of a 3 character sequence (on the safari)
346 } else if(c == 0xe1 || c == 0xe2){
349 } else if(c == 0xe0){
354 key.down = (c & 0x80) == 0;
360 /* qemu workarround: emulate e0 for numpad */
361 if(c != 0 && strchr("GHIKMOPQRS", c) != nil)
362 scan->esc1 |= !scan->num;
364 if(scan->esc1 && scan->ctl && kbtabctrlesc1[c] != 0)
365 key.r = kbtabctrlesc1[c];
366 else if(scan->esc1 && scan->shift && kbtabshiftesc1[c] != 0)
367 key.r = kbtabshiftesc1[c];
369 key.r = kbtabesc1[c];
370 else if(scan->shift && scan->altgr && kbtabshiftaltgr[c] != 0)
371 key.r = kbtabshiftaltgr[c];
373 key.r = kbtabshift[c];
375 key.r = kbtabaltgr[c];
381 if(scan->esc1 || kbtab[c] == 0)
386 if(scan->caps && key.r<='z' && key.r>='a')
389 if(scan->ctl && scan->alt && key.r == Kdel)
397 scan->shift = key.down;
400 scan->ctl = key.down;
403 scan->altgr = key.down;
406 scan->alt = key.down;
409 scan->num ^= key.down;
412 scan->caps ^= key.down;
419 kbdin(Scan *a, char *p, int n)
425 if(n > 0 && p[n-1] != 0){
427 * old format as used by bitsy keyboard:
428 * just a string of characters, no keyup
436 p += chartorune(&k.r, p);
450 chartorune(&k.r, p+1);
454 k.down = (p[0] == 'r');
456 * assign button according to keymap.
458 for(i=0; i<Nscan; i++){
459 if((a->shift && kbtabshift[i] == k.r) || (kbtab[i] == k.r)){
468 else if(k.r == Kaltgr)
475 chartorune(&k.r, p+1);
476 nbsend(runechan, &k.r);
485 if(nbsendp(kbdchan, s) <= 0)
494 setleds(Scan *scan, int leds)
498 if(ledsfd < 0 || scan->leds == leds)
501 snprint(buf, sizeof(buf), "%d", leds);
502 pwrite(ledsfd, buf, strlen(buf), 0);
507 * Read scan codes from scanfd
516 threadsetname("scanproc");
518 memset(&scan, 0, sizeof scan);
519 while((n = read(scanfd, buf, sizeof buf)) > 0){
521 kbdputsc(&scan, buf[i]);
522 setleds(&scan, (scan.num<<1) | (scan.caps<<2));
535 threadsetname("kbdiproc");
537 memset(&scan, 0, sizeof scan);
538 while((n = read(kbdifd, buf, sizeof buf)) > 0)
539 kbdin(&scan, buf, n);
545 utfconv(Rune *r, int n)
550 l = runenlen(r, n) + 1;
552 for(p = s; n > 0; r++, n--)
553 p += runetochar(p, r);
559 * Read key events from keychan and produce characters to
560 * rawchan and keystate in kbdchan.
570 threadsetname("keyproc");
574 while(recv(keychan, &key) > 0){
575 if(msinfd >= 0 && key.r >= Kmouse+1 && key.r <= Kmouse+5){
576 i = 1<<(key.r-(Kmouse+1));
581 fprint(msinfd, "m%11d %11d %11d", 0, 0, mouseb);
582 continue; /* ignored when mapped to mouse button */
586 for(i=0; i<nb && rb[i+1] != key.b; i++)
589 while(i < nb && rb[i+1] == key.b){
590 memmove(rb+i+1, rb+i+2, (nb-i+1) * sizeof(rb[0]));
594 } else if(i == nb && nb < nelem(rb)-1 && key.b){
600 s = utfconv(rb, nb+1);
601 if(nbsendp(kbdchan, s) <= 0)
607 fprint(mctlfd, "buttonmap 132");
609 fprint(mctlfd, "swap");
610 fprint(mctlfd, "swap");
613 fprint(mctlfd, "twitch");
616 if(key.down && key.r)
617 send(rawchan, &key.r);
622 * Read characters from consfd (serial console)
627 char *p, *e, *x, buf[64];
631 threadsetname("consproc");
635 e = buf + sizeof(buf);
636 while((n = read(consfd, p, e - p)) > 0){
638 while(p < x && fullrune(p, x - p)){
639 p += chartorune(&r, p);
641 if(r == 021 || r == 023) /* XON/XOFF */
661 nextrune(Channel *ch, Rune *r)
663 while(recv(ch, r) > 0){
670 /* ignore modifiers */
675 /* composing escapes */
684 * Read runes from rawchan, possibly compose special characters
685 * and output the new runes to runechan
691 char *ld; /* must be seen before using this conversion */
692 char *si; /* options for last input characters */
693 Rune *so; /* the corresponding Rune for each si entry */
701 threadsetname("runeproc");
704 while((i = nextrune(rawchan, &r)) >= 0){
718 * emulators like qemu and vmware use Ctrl+Alt to lock
719 * keyboard input so dont confuse them for a compose
725 if(nextrune(rawchan, &r))
728 if(r == 'x' || r == 'X'){
729 i = (r == 'X') ? 4 : 6;
732 if(nextrune(rawchan, &rr))
734 if(rr >= '0' && rr <= '9')
735 r = (r << 4) | (rr - '0');
736 else if(rr >= 'a' && rr <= 'f')
737 r = (r << 4) | (10 + (rr - 'a'));
738 else if(rr >= 'A' && rr <= 'F')
739 r = (r << 4) | (10 + (rr - 'A'));
743 if((i == 0 || rr == ';') && r != 0 && r <= Runemax)
746 if(nextrune(rawchan, &rr))
748 for(i = 0; i<nelem(tab); i++){
749 if(tab[i].ld[0] != r)
751 if(tab[i].ld[1] == 0)
753 if(tab[i].ld[1] == rr){
754 nextrune(rawchan, &rr);
758 if(i == nelem(tab) || rr == 0)
760 for(j = 0; tab[i].si[j]; j++){
761 if(tab[i].si[j] != rr)
771 * Need to do this in a separate proc because if process we're interrupting
772 * is dying and trying to print tombstone, kernel is blocked holding p->debug lock.
777 threadsetname("intrproc");
780 if(recv(intchan, nil) > 0)
781 write(notefd, "interrupt", 9);
786 * Cook lines for cons
798 threadsetname("lineproc");
807 if(nbsend(intchan, ¬efd) <= 0)
810 case '\0': /* flush */
813 case Kbs: /* ^H: erase character */
814 case Knack: /* ^U: erase line */
815 case Ketb: /* ^W: erase word */
821 if(r == Ketb && utfrune(" \t", rb[nr]))
825 case Keof: /* ^D: eof */
835 } while(!done && nr < nelem(rb));
837 if(nbsendp(conschan, s) <= 0)
843 * Reads Tread and Tflush requests from reqchan and responds
844 * to them with data received on the string channel.
849 enum { AREQ, ASTR, AEND };
856 threadsetname("reqproc");
866 a[AREQ].op = CHANRCV;
867 a[AREQ].c = ac[0]; /* chan(Req*) */
870 a[ASTR].c = ac[1]; /* chan(char*) */
873 a[AEND].op = CHANEND;
876 a[ASTR].op = s != nil ? CHANNOP : CHANRCV;
880 if(r->ifcall.type == Tflush){
883 for(rr = &q; *rr; rr=xx){
885 if(*rr == r->oldreq){
886 if((*rr = *xx) == nil)
888 respond(r->oldreq, Eintr);
894 } else if(r->ifcall.type != Tread){
907 while(s != nil && q != nil){
909 if((q = q->aux) == nil)
915 if(r->fid->qid.path == Qkbd)
916 e++; /* send terminating \0 if its kbd file */
919 m = r->ifcall.count - r->ofcall.count;
921 if(r->ofcall.count > 0){
927 memmove((char*)r->ofcall.data + r->ofcall.count, p, n);
928 r->ofcall.count += n;
932 s = nbrecvp(a[ASTR].c);
945 * Keep track of rawing state and distribute the runes from
946 * runechan to the right channels depending on the state.
951 Channel *cook, *aconsr[2], *akbdr[2];
952 enum { ACTL, ARUNE, AEND };
958 threadsetname("ctlproc");
961 proccreate(kbdiproc, nil, STACK); /* kbdifd -> kbdin() */
963 proccreate(scanproc, nil, STACK); /* scanfd -> keychan */
965 proccreate(consproc, nil, STACK); /* consfd -> runechan */
967 proccreate(intrproc, nil, STACK); /* intchan -> notefd */
969 threadcreate(keyproc, nil, STACK); /* keychan -> rawchan, kbdchan */
970 threadcreate(runeproc, nil, STACK); /* rawchan -> runechan */
972 aconsr[0] = consreqchan;
973 aconsr[1] = conschan;
974 threadcreate(reqproc, aconsr, STACK); /* consreqchan,conschan -> respond */
976 akbdr[0] = kbdreqchan;
978 threadcreate(reqproc, akbdr, STACK); /* kbdreqchan,kbdchan -> respond */
980 cook = chancreate(sizeof(Rune), 0);
981 threadcreate(lineproc, cook, STACK); /* cook -> conschan */
987 a[ACTL].op = CHANRCV;
989 a[ARUNE].c = runechan;
991 a[ARUNE].op = CHANRCV;
993 a[AEND].op = CHANEND;
1001 if(raw = (c == Rawon)){
1009 s = emalloc9p(UTFmax+2);
1011 s[1+runetochar(s+1, &r)] = 0;
1012 if(nbsendp(kbdchan, s) <= 0)
1017 s = emalloc9p(UTFmax+1);
1018 s[runetochar(s, &r)] = 0;
1019 if(nbsendp(conschan, s) <= 0)
1030 * Keyboard layout maps
1034 kbmapent(int t, int sc)
1036 static Rune *tabs[] = {
1042 /* 5 */ kbtabctrlesc1,
1043 /* 6 */ kbtabshiftesc1,
1044 /* 7 */ kbtabshiftaltgr,
1046 if(t >= 0 && t < nelem(tabs) && sc >= 0 && sc < Nscan)
1047 return &tabs[t][sc];
1058 off = req->ifcall.offset/(sizeof(tmp)-1);
1061 if(rp = kbmapent(t, sc))
1062 sprint(tmp, "%11d %11d %11d\n", t, sc, *rp);
1066 if(req->ifcall.count < n)
1067 n = req->ifcall.count;
1068 req->ofcall.count = n;
1069 memmove(req->ofcall.data, tmp, n);
1076 static Rune o = Spec|0x60, tab[] = {
1077 Kshift, Kbreak, Kctl, Kalt,
1078 Kcaps, Knum, Kmiddle, Kaltgr,
1080 if(r >= o && r < o+nelem(tab))
1086 kbmapwrite(Req *req)
1088 char line[100], *lp, *b;
1094 b = req->ifcall.data;
1095 l = req->ifcall.count;
1098 strcpy(line, f->aux);
1099 lp = line+strlen(line);
1105 if(lp[-1] == '\n' || lp == &line[sizeof(line)-1]) {
1109 respond(req, Ebadarg);
1112 if(*line == '\n' || *line == '#'){
1117 while(*lp == ' ' || *lp == '\t')
1119 t = strtoul(line, &lp, 0);
1120 sc = strtoul(lp, &lp, 0);
1121 while(*lp == ' ' || *lp == '\t')
1123 if((rp = kbmapent(t, sc)) == nil)
1126 if(*lp == '\'' && lp[1])
1127 chartorune(&r, lp+1);
1128 else if(*lp == '^' && lp[1]){
1129 chartorune(&r, lp+1);
1130 if(0x40 <= r && r < 0x60)
1134 }else if(*lp == 'M' && ('1' <= lp[1] && lp[1] <= '5'))
1135 r = Kmouse+lp[1]-'0';
1136 else if(*lp>='0' && *lp<='9') /* includes 0x... */
1137 r = strtoul(lp, &lp, 0);
1146 f->aux = lp = emalloc9p(l+1);
1147 memmove(lp, line, l);
1150 req->ofcall.count = req->ifcall.count;
1161 static char user[64];
1167 if((fd = open("/dev/user", OREAD)) < 0)
1168 strcpy(user, "none");
1170 n = read(fd, user, (sizeof user)-1);
1173 strcpy(user, "none");
1181 fillstat(ulong qid, Dir *d)
1185 memset(d, 0, sizeof *d);
1186 d->uid = getauser();
1187 d->gid = getauser();
1189 d->qid = (Qid){qid, 0, 0};
1193 d->qid.type = t->type;
1203 spec = r->ifcall.aname;
1204 if(spec && spec[0]){
1205 respond(r, Ebadspec);
1208 r->fid->qid = (Qid){Qroot, 0, QTDIR};
1209 r->ofcall.qid = r->fid->qid;
1216 fillstat((ulong)r->fid->qid.path, &r->d);
1217 r->d.name = estrdup9p(r->d.name);
1218 r->d.uid = estrdup9p(r->d.uid);
1219 r->d.gid = estrdup9p(r->d.gid);
1220 r->d.muid = estrdup9p(r->d.muid);
1225 fswalk1(Fid *fid, char *name, Qid *qid)
1230 path = fid->qid.path;
1233 if (strcmp(name, "..") == 0) {
1234 *qid = (Qid){Qroot, 0, QTDIR};
1238 for(i = fid->qid.path; i<Nqid; i++){
1239 if(strcmp(name, qtab[i].name) != 0)
1241 *qid = (Qid){i, 0, 0};
1256 static int need[4] = { 4, 2, 6, 1 };
1261 t = qtab + f->qid.path;
1262 n = need[r->ifcall.mode & 3]<<6;
1263 if((n & t->mode) != n)
1267 switch((ulong)f->qid.path){
1284 readtopdir(Fid*, uchar *buf, long off, int cnt, int blen)
1292 for (i = 1; i < Nqid; i++){
1294 m = convD2M(&d, &buf[n], blen-n);
1296 if(m <= BIT16SZ || m > cnt)
1312 switch((ulong)f->qid.path){
1317 r->ofcall.count = readtopdir(f, (void*)r->ofcall.data, r->ifcall.offset,
1318 r->ifcall.count, r->ifcall.count);
1321 sendp(kbdreqchan, r);
1324 sendp(consreqchan, r);
1342 n = r->ifcall.count;
1343 switch((ulong)f->qid.path){
1349 if(write(1, p, n) != n){
1356 if(n >= 5 && memcmp(p, "rawon", 5) == 0)
1357 sendul(ctlchan, Rawon);
1358 else if(n >= 6 && memcmp(p, "rawoff", 6) == 0)
1359 sendul(ctlchan, Rawoff);
1361 respond(r, Ebadarg);
1371 f->aux = emalloc9p(sizeof(Scan));
1372 memset(f->aux, 0, sizeof(Scan));
1374 if(f->qid.path == Qkbin){
1376 kbdputsc((Scan*)f->aux, (uchar)p[i]);
1378 kbdin((Scan*)f->aux, p, n);
1387 r->ofcall.count = n;
1394 switch((ulong)r->oldreq->fid->qid.path) {
1396 sendp(kbdreqchan, r);
1399 sendp(consreqchan, r);
1406 fsdestroyfid(Fid *f)
1411 switch((ulong)f->qid.path){
1424 if(--consctlopen == 0)
1425 sendul(ctlchan, Rawoff);
1431 procopen(int pid, char *name, int mode)
1435 snprint(buf, sizeof(buf), "/proc/%d/%s", pid, name);
1436 return eopen(buf, mode);
1448 if((fd = procopen(getpid(), "ctl", OWRITE)) < 0)
1451 /* get higher than normal priority */
1452 fprint(fd, "pri 16\n");
1454 /* always present in physical memory */
1455 fprint(fd, "noswap\n");
1457 /* dont let anybody kill us */
1458 if(d = dirfstat(fd)){
1460 nd.mode = d->mode & ~0222;
1471 killfd = procopen(getpid(), "notepg", OWRITE);
1473 proccreate(ctlproc, nil, STACK);
1491 .destroyfid= fsdestroyfid,
1498 fprint(2, "usage: %s [ -qdD ] [ -s sname ] [ -m mntpnt ] [ file ]\n", argv0);
1503 threadmain(int argc, char** argv)
1513 sname = EARGF(usage());
1516 mntpt = EARGF(usage());
1526 consfd = eopen(*argv, OREAD);
1528 kbdifd = open(dev("kbd"), OREAD);
1530 scanfd = eopen(dev("scancode"), OREAD);
1531 ledsfd = eopen(dev("leds"), OWRITE);
1532 mctlfd = eopen(dev("mousectl"), OWRITE);
1533 msinfd = eopen(dev("mousein"), OWRITE);
1536 notefd = procopen(getpid(), "notepg", OWRITE);
1538 consreqchan = chancreate(sizeof(Req*), 0);
1539 kbdreqchan = chancreate(sizeof(Req*), 0);
1541 keychan = chancreate(sizeof(Key), 8);
1542 ctlchan = chancreate(sizeof(int), 0);
1543 rawchan = chancreate(sizeof(Rune), 0);
1544 runechan = chancreate(sizeof(Rune), 32);
1545 conschan = chancreate(sizeof(char*), 16);
1546 kbdchan = chancreate(sizeof(char*), 16);
1547 intchan = chancreate(sizeof(int), 0);
1549 threadpostmountsrv(&fs, sname, mntpt, MBEFORE);