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 */
38 int track; /* dx & dy updated */
39 int redraw; /* update cursor on screen */
40 ulong lastcounter; /* value when /dev/mouse read */
50 Mousestate queue[16]; /* circular buffer of click events */
51 int ri; /* read index into queue */
52 int wi; /* write index into queue */
53 uchar qfull; /* queue is full */
64 static Cmdtab mousectlmsg[] =
66 CMbuttonmap, "buttonmap", 0,
67 CMscrollswap, "scrollswap", 0,
77 void Cursortocursor(Cursor*);
78 int mousechanged(void*);
80 static void mouseclock(void);
90 static Dirtab mousedir[]={
91 ".", {Qdir, 0, QTDIR}, 0, DMDIR|0555,
92 "cursor", {Qcursor}, 0, 0666,
93 "mouse", {Qmouse}, 0, 0666,
94 "mousein", {Qmousein}, 0, 0220,
95 "mousectl", {Qmousectl}, 0, 0220,
98 static uchar buttonmap[8] = {
99 0, 1, 2, 3, 4, 5, 6, 7,
101 static int mouseswap;
102 static int scrollswap;
103 static ulong mousetime;
105 extern Memimage* gscreen;
106 extern ulong kerndate;
115 Cursortocursor(&arrow);
116 /* redraw cursor about 30 times per second */
117 addclock0link(mouseclock, 33);
121 mousedevgen(Chan *c, char *name, Dirtab *tab, int ntab, int i, Dir *dp)
125 rc = devgen(c, name, tab, ntab, i, dp);
127 dp->atime = mousetime;
138 Cursortocursor(&arrow);
140 mousetime = seconds();
144 mouseattach(char *spec)
148 return devattach('m', spec);
152 mousewalk(Chan *c, Chan *nc, char **name, int nname)
157 * We use devgen() and not mousedevgen() here
158 * see "Ugly problem" in dev.c/devwalk()
160 wq = devwalk(c, nc, name, nname, mousedir, nelem(mousedir), devgen);
161 if(wq != nil && wq->clone != c && wq->clone != nil && (wq->clone->qid.type&QTDIR)==0)
167 mousestat(Chan *c, uchar *db, int n)
169 return devstat(c, db, n, mousedir, nelem(mousedir), mousedevgen);
173 mouseopen(Chan *c, int omode)
175 switch((ulong)c->qid.path){
188 mouse.lastresize = mouse.resize;
205 c->mode = openmode(omode);
212 mousecreate(Chan*, char*, int, ulong)
223 if((c->qid.type&QTDIR)==0 && (c->flag&COPEN)){
225 if(c->qid.path == Qmouse)
227 else if(c->qid.path == Qmousein){
232 if(--mouse.ref == 0){
235 Cursortocursor(&arrow);
244 mouseread(Chan *c, void *va, long n, vlong off)
248 static int map[8] = {0, 4, 2, 6, 1, 5, 3, 7 };
254 switch((ulong)c->qid.path){
256 return devdirread(c, va, n, mousedir, nelem(mousedir), mousedevgen);
265 BPLONG(p+0, curs.offset.x);
266 BPLONG(p+4, curs.offset.y);
267 memmove(p+8, curs.clr, 2*16);
268 memmove(p+40, curs.set, 2*16);
273 while(mousechanged(0) == 0)
274 sleep(&mouse.r, mousechanged, 0);
277 mousetime = seconds();
280 * No lock of the indices is necessary here, because ri is only
281 * updated by us, and there is only one mouse reader
282 * at a time. I suppose that more than one process
283 * could try to read the fd at one time, but such behavior
284 * is degenerate and already violates the calling
285 * conventions for sleep above.
287 if(mouse.ri != mouse.wi) {
288 m = mouse.queue[mouse.ri];
289 if(++mouse.ri == nelem(mouse.queue))
292 while(!canlock(&cursor))
293 tsleep(&up->sleep, return0, 0, TK2MS(1));
295 m = mouse.Mousestate;
299 b = buttonmap[m.buttons&7];
300 /* put buttons 4 and 5 back in */
301 b |= m.buttons & (3<<3);
307 sprint(buf, "m%11d %11d %11d %11lud ",
311 mouse.lastcounter = m.counter;
314 if(mouse.lastresize != mouse.resize){
315 mouse.lastresize = mouse.resize;
325 setbuttonmap(char* map)
327 int i, x, one, two, three;
329 one = two = three = 0;
330 for(i = 0; i < 3; i++){
338 else if(map[i] == '2'){
343 else if(map[i] == '3'){
354 memset(buttonmap, 0, 8);
355 for(i = 0; i < 8; i++){
368 mousewrite(Chan *c, void *va, long n, vlong)
378 switch((ulong)c->qid.path){
386 Cursortocursor(&arrow);
389 curs.offset.x = BGLONG(p+0);
390 curs.offset.y = BGLONG(p+4);
391 memmove(curs.clr, p+8, 2*16);
392 memmove(curs.set, p+40, 2*16);
393 Cursortocursor(&curs);
403 cb = parsecmd(va, n);
409 ct = lookupcmd(cb, mousectlmsg, nelem(mousectlmsg));
428 setbuttonmap(cb->f[1]);
446 pt.x = strtol(buf+1, &p, 0);
449 pt.y = strtol(p, &p, 0);
452 b = strtol(p, &p, 0);
453 msec = strtol(p, &p, 0);
455 msec = TK2MS(MACHP(0)->ticks);
457 absmousetrack(pt.x, pt.y, b, msec);
459 mousetrack(pt.x, pt.y, b, msec);
468 pt.x = strtoul(buf+1, &p, 0);
471 pt.y = strtoul(p, 0, 0);
473 if(ptinrect(pt, gscreen->r)){
509 Cursortocursor(Cursor *c)
512 memmove(&cursor.Cursor, c, sizeof(Cursor));
519 * called by the clock routine to redraw the cursor
525 mousetrack(mouse.dx, mouse.dy, mouse.buttons, TK2MS(MACHP(0)->ticks));
530 if(mouse.redraw && canlock(&cursor)){
533 mouse.redraw = cursoron(0);
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)
579 if(mouse.acceleration){
584 if(x < gscreen->clipr.min.x)
585 x = gscreen->clipr.min.x;
586 if(x >= gscreen->clipr.max.x)
587 x = gscreen->clipr.max.x;
589 if(y < gscreen->clipr.min.y)
590 y = gscreen->clipr.min.y;
591 if(y >= gscreen->clipr.max.y)
592 y = gscreen->clipr.max.y;
594 lastb = mouse.buttons;
602 * if the queue fills, we discard the entire queue and don't
603 * queue any more events until a reader polls the mouse.
605 if(!mouse.qfull && lastb != b) { /* add to ring */
606 mouse.queue[mouse.wi] = mouse.Mousestate;
607 if(++mouse.wi == nelem(mouse.queue))
609 if(mouse.wi == mouse.ri)
617 absmousetrack(int x, int y, int b, int msec)
624 if(x < gscreen->clipr.min.x)
625 x = gscreen->clipr.min.x;
626 if(x >= gscreen->clipr.max.x)
627 x = gscreen->clipr.max.x;
628 if(y < gscreen->clipr.min.y)
629 y = gscreen->clipr.min.y;
630 if(y >= gscreen->clipr.max.y)
631 y = gscreen->clipr.max.y;
633 lastb = mouse.buttons;
641 * if the queue fills, we discard the entire queue and don't
642 * queue any more events until a reader polls the mouse.
644 if(!mouse.qfull && lastb != b) { /* add to ring */
645 mouse.queue[mouse.wi] = mouse.Mousestate;
646 if(++mouse.wi == nelem(mouse.queue))
648 if(mouse.wi == mouse.ri)
656 * microsoft 3 button, 7 bit bytes
658 * byte 0 - 1 L R Y7 Y6 X7 X6
659 * byte 1 - 0 X5 X4 X3 X2 X1 X0
660 * byte 2 - 0 Y5 Y4 Y3 Y2 Y1 Y0
661 * byte 3 - 0 M x x x x x (optional)
663 * shift & right button is the same as middle button (for 2 button mice)
666 m3mouseputc(Queue*, int c)
671 static uchar b[] = { 0, 4, 1, 5, 0, 2, 1, 3 };
673 int dx, dy, newbuttons;
674 static ulong lasttick;
677 /* Resynchronize in stream with timing. */
679 if(TK2SEC(m - lasttick) > 2)
685 * an extra byte comes for middle button motion.
686 * only two possible values for the extra byte.
688 if(c == 0x00 || c == 0x20){
689 /* an extra byte gets sent for the middle button */
690 middle = (c&0x20) ? 2 : 0;
691 newbuttons = (mouse.buttons & ~2) | middle;
692 mousetrack(0, 0, newbuttons, TK2MS(MACHP(0)->ticks));
699 newbuttons = middle | b[(msg[0]>>4)&3 | (mouseshifted ? 4 : 0)];
700 x = (msg[0]&0x3)<<14;
701 dx = (x>>8) | msg[1];
702 x = (msg[0]&0xc)<<12;
703 dy = (x>>8) | msg[2];
704 mousetrack(dx, dy, newbuttons, TK2MS(MACHP(0)->ticks));
710 * microsoft intellimouse 3 buttons + scroll
711 * byte 0 - 1 L R Y7 Y6 X7 X6
712 * byte 1 - 0 X5 X4 X3 X2 X1 X0
713 * byte 2 - 0 Y5 Y4 Y3 Y2 Y1 Y0
714 * byte 3 - 0 0 M % % % %
716 * %: 0xf => U , 0x1 => D
724 m5mouseputc(Queue*, int c)
728 static ulong lasttick;
731 /* Resynchronize in stream with timing. */
733 if(TK2SEC(m - lasttick) > 2)
737 msg[nb++] = c & 0x7f;
739 schar dx,dy,newbuttons;
740 dx = msg[1] | (msg[0] & 0x3) << 6;
741 dy = msg[2] | (msg[0] & 0xc) << 4;
743 (msg[0] & 0x10) >> (mouseshifted ? 3 : 2)
744 | (msg[0] & 0x20) >> 5
745 | ( msg[3] == 0x10 ? 0x02 :
746 msg[3] == 0x0f ? ScrollUp :
747 msg[3] == 0x01 ? ScrollDown : 0 );
748 mousetrack(dx, dy, newbuttons, TK2MS(MACHP(0)->ticks));
755 * Logitech 5 byte packed binary mouse format, 8 bit bytes
757 * shift & right button is the same as middle button (for 2 button mice)
760 mouseputc(Queue*, int c)
764 static uchar b[] = {0, 4, 2, 6, 1, 5, 3, 7, 0, 2, 2, 6, 1, 3, 3, 7};
765 int dx, dy, newbuttons;
766 static ulong lasttick;
769 /* Resynchronize in stream with timing. */
771 if(TK2SEC(m - lasttick) > 2)
779 msg[nb] |= ~0xFF; /* sign extend */
781 newbuttons = b[((msg[0]&7)^7) | (mouseshifted ? 8 : 0)];
783 dy = -(msg[2]+msg[4]);
784 mousetrack(dx, dy, newbuttons, TK2MS(MACHP(0)->ticks));
793 return mouse.lastcounter != mouse.counter ||
794 mouse.lastresize != mouse.resize;
804 mouseaccelerate(int x)
806 mouse.acceleration = x;
807 if(mouse.acceleration < 3)
810 mouse.maxacc = mouse.acceleration;
814 * notify reader that screen has been resized