]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libdraw/mouse.c
libthread: remove unused internal functions and old xinc assembler files
[plan9front.git] / sys / src / libdraw / mouse.c
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <thread.h>
5 #include <cursor.h>
6 #include <mouse.h>
7
8 void
9 moveto(Mousectl *m, Point pt)
10 {
11         fprint(m->mfd, "m%d %d", pt.x, pt.y);
12         m->xy = pt;
13 }
14
15 void
16 closemouse(Mousectl *mc)
17 {
18         if(mc == nil)
19                 return;
20         close(mc->mfd);
21         close(mc->cfd);
22         mc->mfd = mc->cfd = -1;
23         threadint(mc->pid);
24 }
25
26 int
27 readmouse(Mousectl *mc)
28 {
29         if(mc->image)
30                 flushimage(mc->image->display, 1);
31         if(recv(mc->c, &mc->Mouse) < 0){
32                 fprint(2, "readmouse: %r\n");
33                 return -1;
34         }
35         return 0;
36 }
37
38 static
39 void
40 _ioproc(void *arg)
41 {
42         int n, nerr, one;
43         char buf[1+5*12];
44         Mouse m;
45         Mousectl *mc;
46
47         mc = arg;
48         threadsetname("mouseproc");
49         memset(&m, 0, sizeof m);
50         nerr = 0;
51         while(mc->mfd >= 0){
52                 n = read(mc->mfd, buf, sizeof buf);
53                 if(n != 1+4*12){
54                         yield();        /* if error is due to exiting, we'll exit here */
55                         if(mc->mfd < 0)
56                                 break;
57                         fprint(2, "mouse: bad count %d not 49: %r\n", n);
58                         if(n<0 || ++nerr>10)
59                                 threadexits("read error");
60                         continue;
61                 }
62                 nerr = 0;
63                 switch(buf[0]){
64                 case 'r':
65                         one = 1;
66                         if(send(mc->resizec, &one) < 0)
67                                 continue;
68                         /* fall through */
69                 case 'm':
70                         m.xy.x = atoi(buf+1+0*12);
71                         m.xy.y = atoi(buf+1+1*12);
72                         m.buttons = atoi(buf+1+2*12);
73                         m.msec = atoi(buf+1+3*12);
74                         if(send(mc->c, &m) < 0)
75                                 continue;
76                         /*
77                          * mc->Mouse is updated after send so it doesn't have wrong value if we block during send.
78                          * This means that programs should receive into mc->Mouse (see readmouse() above) if
79                          * they want full synchrony.
80                          */
81                         mc->Mouse = m;
82                         break;
83                 }
84         }
85         free(mc->file);
86         chanfree(mc->c);
87         chanfree(mc->resizec);
88         free(mc);
89 }
90
91 Mousectl*
92 initmouse(char *file, Image *i)
93 {
94         Mousectl *mc;
95         char *t, *sl;
96
97         mc = mallocz(sizeof(Mousectl), 1);
98         if(file == nil)
99                 file = "/dev/mouse";
100         mc->file = strdup(file);
101         mc->mfd = open(file, ORDWR|OCEXEC);
102         if(mc->mfd<0 && strcmp(file, "/dev/mouse")==0){
103                 bind("#m", "/dev", MAFTER);
104                 mc->mfd = open(file, ORDWR|OCEXEC);
105         }
106         if(mc->mfd < 0){
107                 free(mc);
108                 return nil;
109         }
110         t = malloc(strlen(file)+16);
111         if (t == nil) {
112                 close(mc->mfd);
113                 free(mc);
114                 return nil;
115         }
116         strcpy(t, file);
117         sl = utfrrune(t, '/');
118         if(sl)
119                 strcpy(sl, "/cursor");
120         else
121                 strcpy(t, "/dev/cursor");
122         mc->cfd = open(t, ORDWR|OCEXEC);
123         free(t);
124         mc->image = i;
125         mc->c = chancreate(sizeof(Mouse), 0);
126         mc->resizec = chancreate(sizeof(int), 2);
127         mc->pid = proccreate(_ioproc, mc, 4096);
128         return mc;
129 }
130
131 void
132 setcursor(Mousectl *mc, Cursor *c)
133 {
134         char curs[2*4+2*2*16];
135
136         if(c == nil)
137                 write(mc->cfd, curs, 0);
138         else{
139                 BPLONG(curs+0*4, c->offset.x);
140                 BPLONG(curs+1*4, c->offset.y);
141                 memmove(curs+2*4, c->clr, 2*2*16);
142                 write(mc->cfd, curs, sizeof curs);
143         }
144 }