12 typedef struct Mouseinfo Mouseinfo;
13 typedef struct Mousestate Mousestate;
17 Point xy; /* mouse.xy */
18 int buttons; /* mouse.buttons */
19 ulong counter; /* increments every update */
20 ulong msec; /* time of last event */
28 int track; /* dx & dy updated */
29 int redraw; /* update cursor on screen */
30 ulong lastcounter; /* value when /dev/mouse read */
37 Mousestate queue[16]; /* circular buffer of click events */
38 int ri; /* read index into queue */
39 int wi; /* write index into queue */
40 uchar qfull; /* queue is full */
48 void Cursortocursor(Cursor*);
49 int mousechanged(void*);
50 static void mouseclock(void);
58 static Dirtab mousedir[]={
59 ".", {Qdir, 0, QTDIR}, 0, DMDIR|0555,
60 "cursor", {Qcursor}, 0, 0666,
61 "mouse", {Qmouse}, 0, 0666,
64 static uchar buttonmap[8] = {
65 0, 1, 2, 3, 4, 5, 6, 7,
69 extern Memimage* gscreen;
70 extern void mousewarpnote(Point);
76 Cursortocursor(&arrow);
86 mouseattach(char *spec)
88 return devattach('m', spec);
92 mousewalk(Chan *c, Chan *nc, char **name, int nname)
96 wq = devwalk(c, nc, name, nname, mousedir, nelem(mousedir), devgen);
97 if(wq != nil && wq->clone != c && (wq->clone->qid.type&QTDIR)==0)
103 mousestat(Chan *c, uchar *db, int n)
105 return devstat(c, db, n, mousedir, nelem(mousedir), devgen);
109 mouseopen(Chan *c, int omode)
111 switch((ulong)c->qid.path){
129 c->mode = openmode(omode);
136 mousecreate(Chan*, char*, int, ulong)
144 if((c->qid.type&QTDIR)==0 && (c->flag&COPEN)){
146 if(c->qid.path == Qmouse)
148 if(--mouse.ref == 0){
151 Cursortocursor(&arrow);
160 mouseread(Chan *c, void *va, long n, vlong off)
164 static int map[8] = {0, 4, 2, 6, 1, 5, 3, 7 };
170 switch((ulong)c->qid.path){
172 return devdirread(c, va, n, mousedir, nelem(mousedir), devgen);
181 BPLONG(p+0, curs.offset.x);
182 BPLONG(p+4, curs.offset.y);
183 memmove(p+8, curs.clr, 2*16);
184 memmove(p+40, curs.set, 2*16);
189 while(mousechanged(0) == 0)
190 rendsleep(&mouse.r, mousechanged, 0);
195 * No lock of the indicies is necessary here, because ri is only
196 * updated by us, and there is only one mouse reader
197 * at a time. I suppose that more than one process
198 * could try to read the fd at one time, but such behavior
199 * is degenerate and already violates the calling
200 * conventions for sleep above.
202 if(mouse.ri != mouse.wi){
203 m = mouse.queue[mouse.ri];
204 if(++mouse.ri == nelem(mouse.queue))
209 m = mouse.Mousestate;
213 b = buttonmap[m.buttons&7];
214 /* put buttons 4 and 5 back in */
215 b |= m.buttons & (3<<3);
216 sprint(buf, "m%11d %11d %11d %11lud",
220 mouse.lastcounter = m.counter;
230 setbuttonmap(char* map)
232 int i, x, one, two, three;
234 one = two = three = 0;
235 for(i = 0; i < 3; i++){
243 else if(map[i] == '2'){
248 else if(map[i] == '3'){
259 memset(buttonmap, 0, 8);
260 for(i = 0; i < 8; i++){
273 mousewrite(Chan *c, void *va, long n, vlong)
280 switch((ulong)c->qid.path){
288 Cursortocursor(&arrow);
291 curs.offset.x = BGLONG(p+0);
292 curs.offset.y = BGLONG(p+4);
293 memmove(curs.clr, p+8, 2*16);
294 memmove(curs.set, p+40, 2*16);
295 Cursortocursor(&curs);
310 pt.x = strtoul(buf+1, &p, 0);
313 pt.y = strtoul(p, 0, 0);
315 if(ptinrect(pt, gscreen->r)){
316 mousetrack(pt.x, pt.y, mouse.buttons, nsec()/(1000*1000LL));
348 Cursortocursor(Cursor *c)
351 memmove(&cursor.Cursor, c, sizeof(Cursor));
372 x = 6 + (mouse.acceleration>>2);
375 x = 9 + (mouse.acceleration>>1);
391 mouse.redraw = cursoron(0);
397 * called at interrupt level to update the structure and
398 * awaken any waiting procs.
401 mousetrack(int x, int y, int b, int msec)
405 lastb = mouse.buttons;
413 * if the queue fills, we discard the entire queue and don't
414 * queue any more events until a reader polls the mouse.
416 if(!mouse.qfull && lastb != b){ /* add to ring */
417 mouse.queue[mouse.wi] = mouse.Mousestate;
418 if(++mouse.wi == nelem(mouse.queue))
420 if(mouse.wi == mouse.ri)
423 rendwakeup(&mouse.r);
430 return mouse.lastcounter != mouse.counter;
440 mouseaccelerate(int x)
442 mouse.acceleration = x;
443 if(mouse.acceleration < 3)
446 mouse.maxacc = mouse.acceleration;