2 #include "../port/lib.h"
6 #include "../port/error.h"
21 typedef struct Mouseinfo Mouseinfo;
22 typedef struct Mousestate Mousestate;
26 Point xy; /* mouse.xy */
27 int buttons; /* mouse.buttons */
28 ulong counter; /* increments every update */
29 ulong msec; /* time of last event */
36 int redraw; /* update cursor on screen */
37 Rendez redrawr; /* wait for cursor screen updates */
38 ulong lastcounter; /* value when /dev/mouse read */
46 Mousestate queue[16]; /* circular buffer of click events */
47 int ri; /* read index into queue */
48 int wi; /* write index into queue */
49 uchar qfull; /* queue is full */
61 static Cmdtab mousectlmsg[] =
63 CMbuttonmap, "buttonmap", 0,
64 CMscrollswap, "scrollswap", 0,
66 CMtwitch, "twitch", 0,
75 void Cursortocursor(Cursor*);
76 int mousechanged(void*);
77 void mouseredraw(void);
87 static Dirtab mousedir[]={
88 ".", {Qdir, 0, QTDIR}, 0, DMDIR|0555,
89 "cursor", {Qcursor}, 0, 0666,
90 "mouse", {Qmouse}, 0, 0666,
91 "mousein", {Qmousein}, 0, 0220,
92 "mousectl", {Qmousectl}, 0, 0220,
95 static uchar buttonmap[8] = {
96 0, 1, 2, 3, 4, 5, 6, 7,
99 static int scrollswap;
100 static ulong mousetime;
102 extern Memimage* gscreen;
111 Cursortocursor(&arrow);
115 mousedevgen(Chan *c, char *name, Dirtab *tab, int ntab, int i, Dir *dp)
119 rc = devgen(c, name, tab, ntab, i, dp);
121 dp->atime = mousetime;
125 static void mouseproc(void*);
134 Cursortocursor(&arrow);
136 mousetime = seconds();
138 kproc("mouse", mouseproc, 0);
142 mouseattach(char *spec)
146 return devattach('m', spec);
150 mousewalk(Chan *c, Chan *nc, char **name, int nname)
155 * We use devgen() and not mousedevgen() here
156 * see "Ugly problem" in dev.c/devwalk()
158 wq = devwalk(c, nc, name, nname, mousedir, nelem(mousedir), devgen);
159 if(wq != nil && wq->clone != c && wq->clone != nil && (wq->clone->qid.type&QTDIR)==0)
165 mousestat(Chan *c, uchar *db, int n)
167 return devstat(c, db, n, mousedir, nelem(mousedir), mousedevgen);
171 mouseopen(Chan *c, int omode)
173 switch((ulong)c->qid.path){
186 mouse.lastresize = mouse.resize;
196 c->mode = openmode(omode);
203 mousecreate(Chan*, char*, int, ulong)
214 if((c->qid.type&QTDIR)==0 && (c->flag&COPEN)){
216 if(c->qid.path == Qmouse)
218 else if(c->qid.path == Qmousein){
222 if(--mouse.ref != 0){
229 Cursortocursor(&arrow);
236 mouseread(Chan *c, void *va, long n, vlong off)
240 static int map[8] = {0, 4, 2, 6, 1, 5, 3, 7 };
246 switch((ulong)c->qid.path){
248 return devdirread(c, va, n, mousedir, nelem(mousedir), mousedevgen);
257 BPLONG(p+0, curs.offset.x);
258 BPLONG(p+4, curs.offset.y);
259 memmove(p+8, curs.clr, 2*16);
260 memmove(p+40, curs.set, 2*16);
265 while(mousechanged(0) == 0)
266 sleep(&mouse.r, mousechanged, 0);
269 mousetime = seconds();
272 * No lock of the indices is necessary here, because ri is only
273 * updated by us, and there is only one mouse reader
274 * at a time. I suppose that more than one process
275 * could try to read the fd at one time, but such behavior
276 * is degenerate and already violates the calling
277 * conventions for sleep above.
279 if(mouse.ri != mouse.wi) {
280 m = mouse.queue[mouse.ri];
281 if(++mouse.ri == nelem(mouse.queue))
284 while(!canlock(&cursor))
285 tsleep(&up->sleep, return0, 0, TK2MS(1));
287 m = mouse.Mousestate;
291 b = buttonmap[m.buttons&7];
292 /* put buttons 4 and 5 back in */
293 b |= m.buttons & (3<<3);
299 sprint(buf, "m%11d %11d %11d %11lud ",
303 mouse.lastcounter = m.counter;
306 if(mouse.lastresize != mouse.resize){
307 mouse.lastresize = mouse.resize;
317 setbuttonmap(char* map)
319 int i, x, one, two, three;
321 one = two = three = 0;
322 for(i = 0; i < 3; i++){
330 else if(map[i] == '2'){
335 else if(map[i] == '3'){
346 memset(buttonmap, 0, 8);
347 for(i = 0; i < 8; i++){
360 mousewrite(Chan *c, void *va, long n, vlong)
370 switch((ulong)c->qid.path){
378 Cursortocursor(&arrow);
381 curs.offset.x = BGLONG(p+0);
382 curs.offset.y = BGLONG(p+4);
383 memmove(curs.clr, p+8, 2*16);
384 memmove(curs.set, p+40, 2*16);
385 Cursortocursor(&curs);
391 cb = parsecmd(va, n);
397 ct = lookupcmd(cb, mousectlmsg, nelem(mousectlmsg));
416 setbuttonmap(cb->f[1]);
438 pt.x = strtol(buf+1, &p, 0);
441 pt.y = strtol(p, &p, 0);
444 b = strtol(p, &p, 0);
445 msec = strtol(p, &p, 0);
447 msec = TK2MS(MACHP(0)->ticks);
449 absmousetrack(pt.x, pt.y, b, msec);
451 mousetrack(pt.x, pt.y, b, msec);
460 pt.x = strtoul(buf+1, &p, 0);
463 pt.y = strtoul(p, 0, 0);
464 if(gscreen != nil && ptinrect(pt, gscreen->r)){
466 mousetrack(0, 0, mouse.buttons, TK2MS(MACHP(0)->ticks));
497 Cursortocursor(Cursor *c)
501 memmove(&cursor.Cursor, c, sizeof(Cursor));
511 return mouse.redraw != 0;
515 * process that redraws the cursor
532 drawactive(mouse.counter != counter);
533 counter = mouse.counter;
535 tsleep(&mouse.redrawr, shouldredraw, 0, 20*1000);
555 x = 6 + (mouse.acceleration>>2);
558 x = 9 + (mouse.acceleration>>1);
568 * called at interrupt level to update the structure and
569 * awaken any waiting procs.
572 mousetrack(int dx, int dy, int b, int msec)
574 if(mouse.acceleration){
578 absmousetrack(mouse.xy.x + dx, mouse.xy.y + dy, b, msec);
582 absmousetrack(int x, int y, int b, int msec)
589 if(x < gscreen->clipr.min.x)
590 x = gscreen->clipr.min.x;
591 if(x >= gscreen->clipr.max.x)
592 x = gscreen->clipr.max.x;
593 if(y < gscreen->clipr.min.y)
594 y = gscreen->clipr.min.y;
595 if(y >= gscreen->clipr.max.y)
596 y = gscreen->clipr.max.y;
598 lastb = mouse.buttons;
605 * if the queue fills, we discard the entire queue and don't
606 * queue any more events until a reader polls the mouse.
608 if(!mouse.qfull && lastb != b) { /* add to ring */
609 mouse.queue[mouse.wi] = mouse.Mousestate;
610 if(++mouse.wi == nelem(mouse.queue))
612 if(mouse.wi == mouse.ri)
621 * microsoft 3 button, 7 bit bytes
623 * byte 0 - 1 L R Y7 Y6 X7 X6
624 * byte 1 - 0 X5 X4 X3 X2 X1 X0
625 * byte 2 - 0 Y5 Y4 Y3 Y2 Y1 Y0
626 * byte 3 - 0 M x x x x x (optional)
628 * shift & right button is the same as middle button (for 2 button mice)
631 m3mouseputc(Queue*, int c)
636 static uchar b[] = { 0, 4, 1, 5, 0, 2, 1, 3 };
638 int dx, dy, newbuttons;
639 static ulong lasttick;
642 /* Resynchronize in stream with timing. */
644 if(TK2SEC(m - lasttick) > 2)
650 * an extra byte comes for middle button motion.
651 * only two possible values for the extra byte.
653 if(c == 0x00 || c == 0x20){
654 /* an extra byte gets sent for the middle button */
655 middle = (c&0x20) ? 2 : 0;
656 newbuttons = (mouse.buttons & ~2) | middle;
657 mousetrack(0, 0, newbuttons, TK2MS(MACHP(0)->ticks));
664 newbuttons = middle | b[(msg[0]>>4)&3 | (mouseshifted ? 4 : 0)];
665 x = (msg[0]&0x3)<<14;
666 dx = (x>>8) | msg[1];
667 x = (msg[0]&0xc)<<12;
668 dy = (x>>8) | msg[2];
669 mousetrack(dx, dy, newbuttons, TK2MS(MACHP(0)->ticks));
675 * microsoft intellimouse 3 buttons + scroll
676 * byte 0 - 1 L R Y7 Y6 X7 X6
677 * byte 1 - 0 X5 X4 X3 X2 X1 X0
678 * byte 2 - 0 Y5 Y4 Y3 Y2 Y1 Y0
679 * byte 3 - 0 0 M % % % %
681 * %: 0xf => U , 0x1 => D
689 m5mouseputc(Queue*, int c)
693 static ulong lasttick;
696 /* Resynchronize in stream with timing. */
698 if(TK2SEC(m - lasttick) > 2)
702 msg[nb++] = c & 0x7f;
704 schar dx,dy,newbuttons;
705 dx = msg[1] | (msg[0] & 0x3) << 6;
706 dy = msg[2] | (msg[0] & 0xc) << 4;
708 (msg[0] & 0x10) >> (mouseshifted ? 3 : 2)
709 | (msg[0] & 0x20) >> 5
710 | ( msg[3] == 0x10 ? 0x02 :
711 msg[3] == 0x0f ? ScrollUp :
712 msg[3] == 0x01 ? ScrollDown : 0 );
713 mousetrack(dx, dy, newbuttons, TK2MS(MACHP(0)->ticks));
720 * Logitech 5 byte packed binary mouse format, 8 bit bytes
722 * shift & right button is the same as middle button (for 2 button mice)
725 mouseputc(Queue*, int c)
729 static uchar b[] = {0, 4, 2, 6, 1, 5, 3, 7, 0, 2, 2, 6, 1, 3, 3, 7};
730 int dx, dy, newbuttons;
731 static ulong lasttick;
734 /* Resynchronize in stream with timing. */
736 if(TK2SEC(m - lasttick) > 2)
744 msg[nb] |= ~0xFF; /* sign extend */
746 newbuttons = b[((msg[0]&7)^7) | (mouseshifted ? 8 : 0)];
748 dy = -(msg[2]+msg[4]);
749 mousetrack(dx, dy, newbuttons, TK2MS(MACHP(0)->ticks));
758 return mouse.lastcounter != mouse.counter ||
759 mouse.lastresize != mouse.resize;
769 mouseaccelerate(int x)
771 mouse.acceleration = x;
772 if(mouse.acceleration < 3)
775 mouse.maxacc = mouse.acceleration;
779 * notify reader that screen has been resized
792 wakeup(&mouse.redrawr);