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 */
49 Mousestate queue[16]; /* circular buffer of click events */
50 int ri; /* read index into queue */
51 int wi; /* write index into queue */
52 uchar qfull; /* queue is full */
63 static Cmdtab mousectlmsg[] =
65 CMbuttonmap, "buttonmap", 0,
66 CMscrollswap, "scrollswap", 0,
76 void Cursortocursor(Cursor*);
77 int mousechanged(void*);
79 static void mouseclock(void);
89 static Dirtab mousedir[]={
90 ".", {Qdir, 0, QTDIR}, 0, DMDIR|0555,
91 "cursor", {Qcursor}, 0, 0666,
92 "mouse", {Qmouse}, 0, 0666,
93 "mousein", {Qmousein}, 0, 0220,
94 "mousectl", {Qmousectl}, 0, 0220,
97 static uchar buttonmap[8] = {
98 0, 1, 2, 3, 4, 5, 6, 7,
100 static int mouseswap;
101 static int scrollswap;
102 static ulong mousetime;
104 extern Memimage* gscreen;
105 extern ulong kerndate;
114 Cursortocursor(&arrow);
115 /* redraw cursor about 30 times per second */
116 addclock0link(mouseclock, 33);
120 mousedevgen(Chan *c, char *name, Dirtab *tab, int ntab, int i, Dir *dp)
124 rc = devgen(c, name, tab, ntab, i, dp);
126 dp->atime = mousetime;
137 Cursortocursor(&arrow);
139 mousetime = seconds();
143 mouseattach(char *spec)
147 return devattach('m', spec);
151 mousewalk(Chan *c, Chan *nc, char **name, int nname)
156 * We use devgen() and not mousedevgen() here
157 * see "Ugly problem" in dev.c/devwalk()
159 wq = devwalk(c, nc, name, nname, mousedir, nelem(mousedir), devgen);
160 if(wq != nil && wq->clone != c && wq->clone != nil && (wq->clone->qid.type&QTDIR)==0)
166 mousestat(Chan *c, uchar *db, int n)
168 return devstat(c, db, n, mousedir, nelem(mousedir), mousedevgen);
172 mouseopen(Chan *c, int omode)
174 switch((ulong)c->qid.path){
187 mouse.lastresize = mouse.resize;
197 c->mode = openmode(omode);
204 mousecreate(Chan*, char*, int, ulong)
215 if((c->qid.type&QTDIR)==0 && (c->flag&COPEN)){
217 if(c->qid.path == Qmouse)
219 else if(c->qid.path == Qmousein){
223 if(--mouse.ref == 0){
226 Cursortocursor(&arrow);
235 mouseread(Chan *c, void *va, long n, vlong off)
239 static int map[8] = {0, 4, 2, 6, 1, 5, 3, 7 };
245 switch((ulong)c->qid.path){
247 return devdirread(c, va, n, mousedir, nelem(mousedir), mousedevgen);
256 BPLONG(p+0, curs.offset.x);
257 BPLONG(p+4, curs.offset.y);
258 memmove(p+8, curs.clr, 2*16);
259 memmove(p+40, curs.set, 2*16);
264 while(mousechanged(0) == 0)
265 sleep(&mouse.r, mousechanged, 0);
268 mousetime = seconds();
271 * No lock of the indices is necessary here, because ri is only
272 * updated by us, and there is only one mouse reader
273 * at a time. I suppose that more than one process
274 * could try to read the fd at one time, but such behavior
275 * is degenerate and already violates the calling
276 * conventions for sleep above.
278 if(mouse.ri != mouse.wi) {
279 m = mouse.queue[mouse.ri];
280 if(++mouse.ri == nelem(mouse.queue))
283 while(!canlock(&cursor))
284 tsleep(&up->sleep, return0, 0, TK2MS(1));
286 m = mouse.Mousestate;
290 b = buttonmap[m.buttons&7];
291 /* put buttons 4 and 5 back in */
292 b |= m.buttons & (3<<3);
298 sprint(buf, "m%11d %11d %11d %11lud ",
302 mouse.lastcounter = m.counter;
305 if(mouse.lastresize != mouse.resize){
306 mouse.lastresize = mouse.resize;
316 setbuttonmap(char* map)
318 int i, x, one, two, three;
320 one = two = three = 0;
321 for(i = 0; i < 3; i++){
329 else if(map[i] == '2'){
334 else if(map[i] == '3'){
345 memset(buttonmap, 0, 8);
346 for(i = 0; i < 8; i++){
359 mousewrite(Chan *c, void *va, long n, vlong)
369 switch((ulong)c->qid.path){
377 Cursortocursor(&arrow);
380 curs.offset.x = BGLONG(p+0);
381 curs.offset.y = BGLONG(p+4);
382 memmove(curs.clr, p+8, 2*16);
383 memmove(curs.set, p+40, 2*16);
384 Cursortocursor(&curs);
394 cb = parsecmd(va, n);
400 ct = lookupcmd(cb, mousectlmsg, nelem(mousectlmsg));
419 setbuttonmap(cb->f[1]);
437 pt.x = strtol(buf+1, &p, 0);
440 pt.y = strtol(p, &p, 0);
443 b = strtol(p, &p, 0);
444 msec = strtol(p, &p, 0);
446 msec = TK2MS(MACHP(0)->ticks);
448 absmousetrack(pt.x, pt.y, b, msec);
450 mousetrack(pt.x, pt.y, b, msec);
459 pt.x = strtoul(buf+1, &p, 0);
462 pt.y = strtoul(p, 0, 0);
464 if(ptinrect(pt, gscreen->r)){
500 Cursortocursor(Cursor *c)
503 memmove(&cursor.Cursor, c, sizeof(Cursor));
510 * called by the clock routine to redraw the cursor
516 mousetrack(mouse.dx, mouse.dy, mouse.buttons, TK2MS(MACHP(0)->ticks));
521 if(mouse.redraw && canlock(&cursor)){
524 mouse.redraw = cursoron(0);
546 x = 6 + (mouse.acceleration>>2);
549 x = 9 + (mouse.acceleration>>1);
559 * called at interrupt level to update the structure and
560 * awaken any waiting procs.
563 mousetrack(int dx, int dy, int b, int msec)
570 if(mouse.acceleration){
575 if(x < gscreen->clipr.min.x)
576 x = gscreen->clipr.min.x;
577 if(x >= gscreen->clipr.max.x)
578 x = gscreen->clipr.max.x;
580 if(y < gscreen->clipr.min.y)
581 y = gscreen->clipr.min.y;
582 if(y >= gscreen->clipr.max.y)
583 y = gscreen->clipr.max.y;
585 lastb = mouse.buttons;
593 * if the queue fills, we discard the entire queue and don't
594 * queue any more events until a reader polls the mouse.
596 if(!mouse.qfull && lastb != b) { /* add to ring */
597 mouse.queue[mouse.wi] = mouse.Mousestate;
598 if(++mouse.wi == nelem(mouse.queue))
600 if(mouse.wi == mouse.ri)
608 absmousetrack(int x, int y, int b, int msec)
615 if(x < gscreen->clipr.min.x)
616 x = gscreen->clipr.min.x;
617 if(x >= gscreen->clipr.max.x)
618 x = gscreen->clipr.max.x;
619 if(y < gscreen->clipr.min.y)
620 y = gscreen->clipr.min.y;
621 if(y >= gscreen->clipr.max.y)
622 y = gscreen->clipr.max.y;
624 lastb = mouse.buttons;
632 * if the queue fills, we discard the entire queue and don't
633 * queue any more events until a reader polls the mouse.
635 if(!mouse.qfull && lastb != b) { /* add to ring */
636 mouse.queue[mouse.wi] = mouse.Mousestate;
637 if(++mouse.wi == nelem(mouse.queue))
639 if(mouse.wi == mouse.ri)
647 * microsoft 3 button, 7 bit bytes
649 * byte 0 - 1 L R Y7 Y6 X7 X6
650 * byte 1 - 0 X5 X4 X3 X2 X1 X0
651 * byte 2 - 0 Y5 Y4 Y3 Y2 Y1 Y0
652 * byte 3 - 0 M x x x x x (optional)
654 * shift & right button is the same as middle button (for 2 button mice)
657 m3mouseputc(Queue*, int c)
662 static uchar b[] = { 0, 4, 1, 5, 0, 2, 1, 3 };
664 int dx, dy, newbuttons;
665 static ulong lasttick;
668 /* Resynchronize in stream with timing. */
670 if(TK2SEC(m - lasttick) > 2)
676 * an extra byte comes for middle button motion.
677 * only two possible values for the extra byte.
679 if(c == 0x00 || c == 0x20){
680 /* an extra byte gets sent for the middle button */
681 middle = (c&0x20) ? 2 : 0;
682 newbuttons = (mouse.buttons & ~2) | middle;
683 mousetrack(0, 0, newbuttons, TK2MS(MACHP(0)->ticks));
690 newbuttons = middle | b[(msg[0]>>4)&3 | (mouseshifted ? 4 : 0)];
691 x = (msg[0]&0x3)<<14;
692 dx = (x>>8) | msg[1];
693 x = (msg[0]&0xc)<<12;
694 dy = (x>>8) | msg[2];
695 mousetrack(dx, dy, newbuttons, TK2MS(MACHP(0)->ticks));
701 * microsoft intellimouse 3 buttons + scroll
702 * byte 0 - 1 L R Y7 Y6 X7 X6
703 * byte 1 - 0 X5 X4 X3 X2 X1 X0
704 * byte 2 - 0 Y5 Y4 Y3 Y2 Y1 Y0
705 * byte 3 - 0 0 M % % % %
707 * %: 0xf => U , 0x1 => D
715 m5mouseputc(Queue*, int c)
719 static ulong lasttick;
722 /* Resynchronize in stream with timing. */
724 if(TK2SEC(m - lasttick) > 2)
728 msg[nb++] = c & 0x7f;
730 schar dx,dy,newbuttons;
731 dx = msg[1] | (msg[0] & 0x3) << 6;
732 dy = msg[2] | (msg[0] & 0xc) << 4;
734 (msg[0] & 0x10) >> (mouseshifted ? 3 : 2)
735 | (msg[0] & 0x20) >> 5
736 | ( msg[3] == 0x10 ? 0x02 :
737 msg[3] == 0x0f ? ScrollUp :
738 msg[3] == 0x01 ? ScrollDown : 0 );
739 mousetrack(dx, dy, newbuttons, TK2MS(MACHP(0)->ticks));
746 * Logitech 5 byte packed binary mouse format, 8 bit bytes
748 * shift & right button is the same as middle button (for 2 button mice)
751 mouseputc(Queue*, int c)
755 static uchar b[] = {0, 4, 2, 6, 1, 5, 3, 7, 0, 2, 2, 6, 1, 3, 3, 7};
756 int dx, dy, newbuttons;
757 static ulong lasttick;
760 /* Resynchronize in stream with timing. */
762 if(TK2SEC(m - lasttick) > 2)
770 msg[nb] |= ~0xFF; /* sign extend */
772 newbuttons = b[((msg[0]&7)^7) | (mouseshifted ? 8 : 0)];
774 dy = -(msg[2]+msg[4]);
775 mousetrack(dx, dy, newbuttons, TK2MS(MACHP(0)->ticks));
784 return mouse.lastcounter != mouse.counter ||
785 mouse.lastresize != mouse.resize;
795 mouseaccelerate(int x)
797 mouse.acceleration = x;
798 if(mouse.acceleration < 3)
801 mouse.maxacc = mouse.acceleration;
805 * notify reader that screen has been resized