]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libdraw/keyboard.c
libthread: remove unused internal functions and old xinc assembler files
[plan9front.git] / sys / src / libdraw / keyboard.c
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <thread.h>
5 #include <keyboard.h>
6
7 void
8 closekeyboard(Keyboardctl *kc)
9 {
10         if(kc == nil)
11                 return;
12         close(kc->ctlfd);
13         close(kc->consfd);
14         kc->consfd = kc->ctlfd = -1;
15         threadint(kc->pid);
16 }
17
18 static
19 void
20 _ioproc(void *arg)
21 {
22         int m, n;
23         char buf[20];
24         Rune r;
25         Keyboardctl *kc;
26
27         kc = arg;
28         threadsetname("kbdproc");
29         n = 0;
30 loop:
31         while(kc->consfd >= 0){
32                 while(n>0 && fullrune(buf, n)){
33                         m = chartorune(&r, buf);
34                         n -= m;
35                         memmove(buf, buf+m, n);
36                         if(send(kc->c, &r) < 0)
37                                 goto loop;
38                 }
39                 if((m = read(kc->consfd, buf+n, sizeof buf-n)) <= 0)
40                         goto loop;
41                 n += m;
42         }
43         chanfree(kc->c);
44         free(kc->file);
45         free(kc);
46 }
47
48 Keyboardctl*
49 initkeyboard(char *file)
50 {
51         Keyboardctl *kc;
52         char *t;
53
54         kc = mallocz(sizeof(Keyboardctl), 1);
55         if(kc == nil)
56                 return nil;
57         if(file == nil)
58                 file = "/dev/cons";
59         kc->file = strdup(file);
60         kc->consfd = open(file, ORDWR|OCEXEC);
61         t = malloc(strlen(file)+16);
62         if(kc->consfd<0 || t==nil){
63 Error1:
64                 free(kc);
65                 return nil;
66         }
67         sprint(t, "%sctl", file);
68         kc->ctlfd = open(t, OWRITE|OCEXEC);
69         if(kc->ctlfd < 0){
70                 fprint(2, "initkeyboard: can't open %s: %r\n", t);
71 Error2:
72                 close(kc->consfd);
73                 free(t);
74                 goto Error1;
75         }
76         if(ctlkeyboard(kc, "rawon") < 0){
77                 fprint(2, "initkeyboard: can't turn on raw mode on %s: %r\n", t);
78                 close(kc->ctlfd);
79                 goto Error2;
80         }
81         free(t);
82         kc->c = chancreate(sizeof(Rune), 20);
83         kc->pid = proccreate(_ioproc, kc, 4096);
84         return kc;
85 }
86
87 int
88 ctlkeyboard(Keyboardctl *kc, char *m)
89 {
90         return write(kc->ctlfd, m, strlen(m));
91 }