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,
76 void Cursortocursor(Cursor*);
77 int mousechanged(void*);
78 void mouseredraw(void);
88 static Dirtab mousedir[]={
89 ".", {Qdir, 0, QTDIR}, 0, DMDIR|0555,
90 "cursor", {Qcursor}, 0, 0666,
91 "mouse", {Qmouse}, 0, 0666,
92 "mousein", {Qmousein}, 0, 0220,
93 "mousectl", {Qmousectl}, 0, 0220,
96 static uchar buttonmap[8] = {
97 0, 1, 2, 3, 4, 5, 6, 7,
100 static int scrollswap;
101 static ulong mousetime;
103 extern Memimage* gscreen;
112 Cursortocursor(&arrow);
116 mousedevgen(Chan *c, char *name, Dirtab *tab, int ntab, int i, Dir *dp)
120 rc = devgen(c, name, tab, ntab, i, dp);
122 dp->atime = mousetime;
126 static void mouseproc(void*);
135 Cursortocursor(&arrow);
137 mousetime = seconds();
139 kproc("mouse", mouseproc, 0);
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)
176 mode = openmode(omode);
177 switch((ulong)c->qid.path){
190 mouse.lastresize = mouse.resize;
196 c->aux = malloc(sizeof(Mousestate));
210 mousecreate(Chan*, char*, int, ulong)
221 if((c->qid.type&QTDIR)==0 && (c->flag&COPEN)){
223 if(c->qid.path == Qmouse)
225 else if(c->qid.path == Qmousein){
227 mouseinbuttons &= ~((Mousestate*)c->aux)->buttons;
228 free(c->aux); /* Mousestate */
232 if(--mouse.ref != 0){
239 Cursortocursor(&arrow);
246 mouseread(Chan *c, void *va, long n, vlong off)
250 static int map[8] = {0, 4, 2, 6, 1, 5, 3, 7 };
256 switch((ulong)c->qid.path){
258 return devdirread(c, va, n, mousedir, nelem(mousedir), mousedevgen);
267 BPLONG(p+0, curs.offset.x);
268 BPLONG(p+4, curs.offset.y);
269 memmove(p+8, curs.clr, 2*16);
270 memmove(p+40, curs.set, 2*16);
275 while(mousechanged(0) == 0)
276 sleep(&mouse.r, mousechanged, 0);
279 mousetime = seconds();
282 * No lock of the indices is necessary here, because ri is only
283 * updated by us, and there is only one mouse reader
284 * at a time. I suppose that more than one process
285 * could try to read the fd at one time, but such behavior
286 * is degenerate and already violates the calling
287 * conventions for sleep above.
289 if(mouse.ri != mouse.wi) {
290 m = mouse.queue[mouse.ri];
291 if(++mouse.ri == nelem(mouse.queue))
294 while(!canlock(&cursor))
295 tsleep(&up->sleep, return0, 0, TK2MS(1));
297 m = mouse.Mousestate;
301 b = buttonmap[m.buttons&7];
302 /* put buttons 4 and 5 back in */
303 b |= m.buttons & (3<<3);
309 sprint(buf, "m%11d %11d %11d %11lud ",
313 mouse.lastcounter = m.counter;
316 if(mouse.lastresize != mouse.resize){
317 mouse.lastresize = mouse.resize;
327 setbuttonmap(char* map)
329 int i, x, one, two, three;
331 one = two = three = 0;
332 for(i = 0; i < 3; i++){
340 else if(map[i] == '2'){
345 else if(map[i] == '3'){
356 memset(buttonmap, 0, 8);
357 for(i = 0; i < 8; i++){
370 mousewrite(Chan *c, void *va, long n, vlong)
381 switch((ulong)c->qid.path){
389 Cursortocursor(&arrow);
392 curs.offset.x = BGLONG(p+0);
393 curs.offset.y = BGLONG(p+4);
394 memmove(curs.clr, p+8, 2*16);
395 memmove(curs.set, p+40, 2*16);
396 Cursortocursor(&curs);
402 cb = parsecmd(va, n);
408 ct = lookupcmd(cb, mousectlmsg, nelem(mousectlmsg));
427 setbuttonmap(cb->f[1]);
449 pt.x = strtol(buf+1, &p, 0);
452 pt.y = strtol(p, &p, 0);
455 b = strtol(p, &p, 0);
456 msec = strtol(p, &p, 0);
458 msec = TK2MS(MACHP(0)->ticks);
459 m = (Mousestate*)c->aux;
464 mouseinbuttons = (m->buttons & b) | (mouseinbuttons & ~b);
465 b = mouse.buttons & ~b;
467 absmousetrack(pt.x, pt.y, b, msec);
469 mousetrack(pt.x, pt.y, b, msec);
478 pt.x = strtoul(buf+1, &p, 0);
481 pt.y = strtoul(p, 0, 0);
482 if(gscreen != nil && ptinrect(pt, gscreen->r)){
484 mousetrack(0, 0, mouse.buttons, TK2MS(MACHP(0)->ticks));
515 Cursortocursor(Cursor *c)
519 memmove(&cursor.Cursor, c, sizeof(Cursor));
529 return mouse.redraw != 0;
533 * process that redraws the cursor
550 drawactive(mouse.counter != counter);
551 counter = mouse.counter;
553 tsleep(&mouse.redrawr, shouldredraw, 0, 20*1000);
573 x = 6 + (mouse.acceleration>>2);
576 x = 9 + (mouse.acceleration>>1);
586 * called at interrupt level to update the structure and
587 * awaken any waiting procs.
590 mousetrack(int dx, int dy, int b, int msec)
592 if(mouse.acceleration){
596 absmousetrack(mouse.xy.x + dx, mouse.xy.y + dy, b, msec);
600 absmousetrack(int x, int y, int b, int msec)
607 if(x < gscreen->clipr.min.x)
608 x = gscreen->clipr.min.x;
609 if(x >= gscreen->clipr.max.x)
610 x = gscreen->clipr.max.x;
611 if(y < gscreen->clipr.min.y)
612 y = gscreen->clipr.min.y;
613 if(y >= gscreen->clipr.max.y)
614 y = gscreen->clipr.max.y;
618 lastb = mouse.buttons;
625 * if the queue fills, we discard the entire queue and don't
626 * queue any more events until a reader polls the mouse.
628 if(!mouse.qfull && lastb != b) { /* add to ring */
629 mouse.queue[mouse.wi] = mouse.Mousestate;
630 if(++mouse.wi == nelem(mouse.queue))
632 if(mouse.wi == mouse.ri)
641 * microsoft 3 button, 7 bit bytes
643 * byte 0 - 1 L R Y7 Y6 X7 X6
644 * byte 1 - 0 X5 X4 X3 X2 X1 X0
645 * byte 2 - 0 Y5 Y4 Y3 Y2 Y1 Y0
646 * byte 3 - 0 M x x x x x (optional)
648 * shift & right button is the same as middle button (for 2 button mice)
651 m3mouseputc(Queue*, int c)
656 static uchar b[] = { 0, 4, 1, 5, 0, 2, 1, 3 };
658 int dx, dy, newbuttons;
659 static ulong lasttick;
662 /* Resynchronize in stream with timing. */
664 if(TK2SEC(m - lasttick) > 2)
670 * an extra byte comes for middle button motion.
671 * only two possible values for the extra byte.
673 if(c == 0x00 || c == 0x20){
674 /* an extra byte gets sent for the middle button */
675 middle = (c&0x20) ? 2 : 0;
676 newbuttons = (mouse.buttons & ~2) | middle;
677 mousetrack(0, 0, newbuttons, TK2MS(MACHP(0)->ticks));
684 newbuttons = middle | b[(msg[0]>>4)&3 | (mouseshifted ? 4 : 0)];
685 x = (msg[0]&0x3)<<14;
686 dx = (x>>8) | msg[1];
687 x = (msg[0]&0xc)<<12;
688 dy = (x>>8) | msg[2];
689 mousetrack(dx, dy, newbuttons, TK2MS(MACHP(0)->ticks));
695 * microsoft intellimouse 3 buttons + scroll
696 * byte 0 - 1 L R Y7 Y6 X7 X6
697 * byte 1 - 0 X5 X4 X3 X2 X1 X0
698 * byte 2 - 0 Y5 Y4 Y3 Y2 Y1 Y0
699 * byte 3 - 0 0 M % % % %
701 * %: 0xf => U , 0x1 => D
709 m5mouseputc(Queue*, int c)
713 static ulong lasttick;
716 /* Resynchronize in stream with timing. */
718 if(TK2SEC(m - lasttick) > 2)
722 msg[nb++] = c & 0x7f;
724 schar dx,dy,newbuttons;
725 dx = msg[1] | (msg[0] & 0x3) << 6;
726 dy = msg[2] | (msg[0] & 0xc) << 4;
728 (msg[0] & 0x10) >> (mouseshifted ? 3 : 2)
729 | (msg[0] & 0x20) >> 5
730 | ( msg[3] == 0x10 ? 0x02 :
731 msg[3] == 0x0f ? ScrollUp :
732 msg[3] == 0x01 ? ScrollDown : 0 );
733 mousetrack(dx, dy, newbuttons, TK2MS(MACHP(0)->ticks));
740 * Logitech 5 byte packed binary mouse format, 8 bit bytes
742 * shift & right button is the same as middle button (for 2 button mice)
745 mouseputc(Queue*, int c)
749 static uchar b[] = {0, 4, 2, 6, 1, 5, 3, 7, 0, 2, 2, 6, 1, 3, 3, 7};
750 int dx, dy, newbuttons;
751 static ulong lasttick;
754 /* Resynchronize in stream with timing. */
756 if(TK2SEC(m - lasttick) > 2)
764 msg[nb] |= ~0xFF; /* sign extend */
766 newbuttons = b[((msg[0]&7)^7) | (mouseshifted ? 8 : 0)];
768 dy = -(msg[2]+msg[4]);
769 mousetrack(dx, dy, newbuttons, TK2MS(MACHP(0)->ticks));
778 return mouse.lastcounter != mouse.counter ||
779 mouse.lastresize != mouse.resize;
789 mouseaccelerate(int x)
791 mouse.acceleration = x;
792 if(mouse.acceleration < 3)
795 mouse.maxacc = mouse.acceleration;
799 * notify reader that screen has been resized
812 wakeup(&mouse.redrawr);