--- moused.c.orig Mon Aug 30 00:43:44 1999 +++ moused.c Mon Nov 29 00:01:15 1999 @@ -73,6 +73,7 @@ #include #define MAX_CLICKTHRESHOLD 2000 /* 2 seconds */ +#define MAX_BUTTON2TIMEOUT 2000 /* 2 seconds */ #define TRUE 1 #define FALSE 0 @@ -361,6 +362,7 @@ int mremsfd; /* mouse remote server file descriptor */ int mremcfd; /* mouse remote client file descriptor */ long clickthreshold; /* double click speed in msec */ + long button2timeout; /* 3 button emulation timeout */ mousehw_t hw; /* mouse device hardware information */ mousemode_t mode; /* protocol information */ } rodent = { @@ -378,6 +380,7 @@ mremsfd : -1, mremcfd : -1, clickthreshold : 500, /* 0.5 sec */ + button2timeout : 200, /* 0.2 sec */ }; /* button status */ @@ -386,6 +389,50 @@ struct timeval tv; /* timestamp on the last `up' event */ } buttonstate[MOUSE_MAXBUTTON]; +/* state machine for 3 button emulation */ + +#define S0 0 /* start */ +#define S1 1 /* button 1 delayed down */ +#define S2 2 /* button 3 delayed down */ +#define S3 3 /* both buttons down -> button 2 down */ +#define S4 4 /* button 1 delayed up */ +#define S5 5 /* button 1 down */ +#define S6 6 /* button 3 down */ +#define S7 7 /* both buttons down */ +#define S8 8 /* button 3 delayed up */ +#define S9 9 /* button 1 or 3 up after S3 */ + +#define A(b1, b3) (((b1) ? 2 : 0) | ((b3) ? 1 : 0)) +#define A_TIMEOUT 4 + +static struct { + int s[A_TIMEOUT + 1]; + int buttons; + int mask; +} states[10] = { + /* S0 */ + { { S0, S2, S1, S3, S0 }, 0, ~(MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN) }, + /* S1 */ + { { S4, S2, S1, S3, S5 }, 0, ~MOUSE_BUTTON1DOWN }, + /* S2 */ + { { S8, S2, S1, S3, S6 }, 0, ~MOUSE_BUTTON3DOWN }, + /* S3 */ + { { S0, S9, S9, S3, S3 }, MOUSE_BUTTON2DOWN, ~0 }, + /* S4 */ + { { S0, S2, S1, S3, S0 }, MOUSE_BUTTON1DOWN, ~0 }, + /* S5 */ + { { S0, S2, S5, S7, S5 }, MOUSE_BUTTON1DOWN, ~0 }, + /* S6 */ + { { S0, S6, S1, S7, S6 }, MOUSE_BUTTON3DOWN, ~0 }, + /* S7 */ + { { S0, S6, S5, S7, S7 }, MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, ~0 }, + /* S8 */ + { { S0, S2, S1, S3, S0 }, MOUSE_BUTTON3DOWN, ~0 }, + /* S9 */ + { { S0, S9, S9, S3, S9 }, 0, ~(MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN) }, +}; +static int mouse_button_state; + static jmp_buf env; /* function prototypes */ @@ -401,8 +448,11 @@ static char *r_model(int model); static void r_init(void); static int r_protocol(u_char b, mousestatus_t *act); +static int r_statetrans(mousestatus_t *a1, mousestatus_t *a2, int trans); static int r_installmap(char *arg); static void r_map(mousestatus_t *act1, mousestatus_t *act2); +static void r_timestamp(mousestatus_t *act); +static void r_timeout(mousestatus_t *act); static void r_click(mousestatus_t *act); static void setmousespeed(int old, int new, unsigned cflag); @@ -426,13 +476,22 @@ int c; int i; - while((c = getopt(argc,argv,"3C:DF:I:PRS:cdfhi:l:m:p:r:st:w:z:")) != -1) + while((c = getopt(argc,argv,"3C:DE:F:I:PRS:cdfhi:l:m:p:r:st:w:z:")) != -1) switch(c) { case '3': rodent.flags |= Emulate3Button; break; + case 'E': + rodent.button2timeout = atoi(optarg); + if ((rodent.button2timeout < 0) || + (rodent.button2timeout > MAX_BUTTON2TIMEOUT)) { + warnx("invalid argument `%s'", optarg); + usage(); + } + break; + case 'c': rodent.flags |= ChordMiddle; break; @@ -691,11 +750,15 @@ moused(void) { struct mouse_info mouse; - mousestatus_t action; /* original mouse action */ + mousestatus_t action0; /* original mouse action */ + mousestatus_t action; /* interrim buffer */ mousestatus_t action2; /* mapped action */ + struct timeval timeout; fd_set fds; u_char b; FILE *fp; + int flags; + int c; if ((rodent.cfd = open("/dev/consolectl", O_RDWR, 0)) == -1) logerr(1, "cannot open /dev/consolectl", 0); @@ -713,6 +776,7 @@ } /* clear mouse data */ + bzero(&action0, sizeof(action0)); bzero(&action, sizeof(action)); bzero(&action2, sizeof(action2)); bzero(&buttonstate, sizeof(buttonstate)); @@ -723,30 +787,64 @@ extioctl = (ioctl(rodent.cfd, CONS_MOUSECTL, &mouse) == 0); /* process mouse data */ + mouse_button_state = S0; + timeout.tv_sec = 0; + timeout.tv_usec = 20000; /* 20 msec */ for (;;) { FD_ZERO(&fds); FD_SET(rodent.mfd, &fds); - if (rodent.mremsfd >= 0) FD_SET(rodent.mremsfd, &fds); - if (rodent.mremcfd >= 0) FD_SET(rodent.mremcfd, &fds); - - if (select(FD_SETSIZE, &fds, NULL, NULL, NULL) <= 0) + if (rodent.mremsfd >= 0) + FD_SET(rodent.mremsfd, &fds); + if (rodent.mremcfd >= 0) + FD_SET(rodent.mremcfd, &fds); + + c = select(FD_SETSIZE, &fds, NULL, NULL, + (rodent.flags & Emulate3Button) ? &timeout : NULL); + if (c < 0) { /* error */ logwarn("failed to read from mouse", 0); - - /* MouseRemote client connect/disconnect */ - if ((rodent.mremsfd >= 0) && FD_ISSET(rodent.mremsfd, &fds)) { - mremote_clientchg(TRUE); continue; - } - - if ((rodent.mremcfd >= 0) && FD_ISSET(rodent.mremcfd, &fds)) { - mremote_clientchg(FALSE); - continue; - } + } else if (c == 0) { /* timeout */ + /* assert(rodent.flags & Emulate3Button) */ + action0.button = action0.obutton; + action0.dx = action0.dy = action0.dz = 0; + action0.flags = flags = 0; + r_timeout(&action0); + if (action0.flags) { + debug("flags:%08x", action0.flags); + r_statetrans(&action0, &action, A_TIMEOUT); + debug("flags:%08x buttons:%08x obuttons:%08x", action.flags, + action.button, action.obutton); + } else + continue; + } else { + /* MouseRemote client connect/disconnect */ + if ((rodent.mremsfd >= 0) && FD_ISSET(rodent.mremsfd, &fds)) { + mremote_clientchg(TRUE); + continue; + } + if ((rodent.mremcfd >= 0) && FD_ISSET(rodent.mremcfd, &fds)) { + mremote_clientchg(FALSE); + continue; + } + /* mouse movement */ + if (read(rodent.mfd, &b, 1) == -1) + return; + if ((flags = r_protocol(b, &action0)) == 0) + continue; + r_timestamp(&action0); + r_statetrans(&action0, &action, + A(action0.button & MOUSE_BUTTON1DOWN, + action0.button & MOUSE_BUTTON3DOWN)); + debug("flags:%08x buttons:%08x obuttons:%08x", action.flags, + action.button, action.obutton); + } + action0.obutton = action0.button; + flags &= MOUSE_POSCHANGED; + flags |= action.obutton ^ action.button; + action.flags = flags; - /* mouse event */ - read(rodent.mfd, &b, 1); - if (r_protocol(b, &action)) { /* handler detected action */ + if (flags) { /* handler detected action */ r_map(&action, &action2); debug("activity : buttons 0x%08x dx %d dy %d dz %d", action2.button, action2.dx, action2.dy, action2.dz); @@ -822,8 +920,9 @@ usage(void) { fprintf(stderr, "%s\n%s\n%s\n", - "usage: moused [-3DRcdfs] [-I file] [-F rate] [-r resolution] [-S baudrate]", - " [-C threshold] [-m N=M] [-w N] [-z N] [-t ] -p ", + "usage: moused [-DRcdfs] [-I file] [-F rate] [-r resolution] [-S baudrate]", + " [-C threshold] [-m N=M] [-w N] [-z N] [-t ]", + " [-3 [-E timeout]] -p ", " moused [-d] -i -p "); exit(1); } @@ -1698,24 +1797,37 @@ act->flags = ((act->dx || act->dy || act->dz) ? MOUSE_POSCHANGED : 0) | (act->obutton ^ act->button); + return act->flags; +} + +static int +r_statetrans(mousestatus_t *a1, mousestatus_t *a2, int trans) +{ + int flags; + + a2->dx = a1->dx; + a2->dy = a1->dy; + a2->dz = a1->dz; + a2->obutton = a2->button; + a2->button = a1->button; + a2->flags = a1->flags; + if (rodent.flags & Emulate3Button) { - if (((act->flags & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN)) - == (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN)) - && ((act->button & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN)) - == (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN))) { - act->button &= ~(MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN); - act->button |= MOUSE_BUTTON2DOWN; - } else if ((act->obutton & MOUSE_BUTTON2DOWN) - && ((act->button & (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN)) - != (MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN))) { - act->button &= ~(MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN - | MOUSE_BUTTON3DOWN); + debug("state:%d, trans:%d -> state:%d", + mouse_button_state, trans, states[mouse_button_state].s[trans]); + mouse_button_state = states[mouse_button_state].s[trans]; + a2->button &= + ~(MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN); + a2->button &= states[mouse_button_state].mask; + a2->button |= states[mouse_button_state].buttons; + flags = a2->flags & MOUSE_POSCHANGED; + flags |= a2->obutton ^ a2->button; + if (flags & MOUSE_BUTTON2DOWN) { + a2->flags = flags & MOUSE_BUTTON2DOWN; + r_timestamp(a2); } - act->flags &= MOUSE_POSCHANGED; - act->flags |= act->obutton ^ act->button; + a2->flags = flags; } - - return act->flags; } /* phisical to logical button mapping */ @@ -1830,26 +1942,36 @@ } static void -r_click(mousestatus_t *act) +r_timestamp(mousestatus_t *act) { - struct mouse_info mouse; struct timeval tv; struct timeval tv1; struct timeval tv2; + struct timeval tv3; struct timezone tz; int button; int mask; int i; mask = act->flags & MOUSE_BUTTONS; +#if 0 if (mask == 0) return; +#endif gettimeofday(&tv1, &tz); + + /* double click threshold */ tv2.tv_sec = rodent.clickthreshold/1000; tv2.tv_usec = (rodent.clickthreshold%1000)*1000; timersub(&tv1, &tv2, &tv); debug("tv: %ld %ld", tv.tv_sec, tv.tv_usec); + + /* 3 button emulation timeout */ + tv2.tv_sec = rodent.button2timeout/1000; + tv2.tv_usec = (rodent.button2timeout%1000)*1000; + timersub(&tv1, &tv2, &tv3); + button = MOUSE_BUTTON1DOWN; for (i = 0; (i < MOUSE_MAXBUTTON) && (mask != 0); ++i) { if (mask & 1) { @@ -1858,16 +1980,89 @@ debug(" : %ld %ld", buttonstate[i].tv.tv_sec, buttonstate[i].tv.tv_usec); if (timercmp(&tv, &buttonstate[i].tv, >)) { - buttonstate[i].tv.tv_sec = 0; - buttonstate[i].tv.tv_usec = 0; buttonstate[i].count = 1; } else { ++buttonstate[i].count; } - mouse.u.event.value = buttonstate[i].count; + buttonstate[i].tv = tv1; } else { /* the button is up */ buttonstate[i].tv = tv1; + } + } else { + if (act->button & button) { + /* the button has been down */ + if (timercmp(&tv3, &buttonstate[i].tv, >)) { + buttonstate[i].count = 1; + buttonstate[i].tv = tv1; + act->flags |= button; + debug("button %d timeout", i + 1); + } + } else { + /* the button has been up */ + } + } + button <<= 1; + mask >>= 1; + } +} + +static void +r_timeout(mousestatus_t *act) +{ + struct timeval tv; + struct timeval tv1; + struct timeval tv2; + struct timezone tz; + int button; + int i; + + gettimeofday(&tv1, &tz); + tv2.tv_sec = rodent.button2timeout/1000; + tv2.tv_usec = (rodent.button2timeout%1000)*1000; + timersub(&tv1, &tv2, &tv); + button = MOUSE_BUTTON1DOWN; + for (i = 0; i < MOUSE_MAXBUTTON; ++i) { + if (act->button & button) { + /* the button has been down */ + if (timercmp(&tv, &buttonstate[i].tv, >)) { + buttonstate[i].count = 1; + buttonstate[i].tv = tv1; + act->flags |= button; + debug("button %d (down) timeout", i + 1); + } + } else { + /* the button has been up */ + if (timercmp(&tv, &buttonstate[i].tv, >)) { + buttonstate[i].count = 0; + buttonstate[i].tv = tv1; + act->flags |= button; + } + } + button <<= 1; + } +} + +static void +r_click(mousestatus_t *act) +{ + struct mouse_info mouse; + int button; + int mask; + int i; + + mask = act->flags & MOUSE_BUTTONS; + if (mask == 0) + return; + + button = MOUSE_BUTTON1DOWN; + for (i = 0; (i < MOUSE_MAXBUTTON) && (mask != 0); ++i) { + if (mask & 1) { + if (act->button & button) { + /* the button is down */ + mouse.u.event.value = buttonstate[i].count; + } else { + /* the button is up */ mouse.u.event.value = 0; } mouse.operation = MOUSE_BUTTON_EVENT; --- moused.8.orig Mon Aug 30 00:43:43 1999 +++ moused.8 Mon Nov 29 00:15:52 1999 @@ -38,7 +38,7 @@ .Nd pass mouse data to the console driver .Sh SYNOPSIS .Nm -.Op Fl 3DPRcdfs +.Op Fl DPRcdfs .Op Fl I Ar file .Op Fl F Ar rate .Op Fl r Ar resolution @@ -48,6 +48,7 @@ .Op Fl w Ar N .Op Fl z Ar target .Op Fl t Ar mousetype +.Op Fl 3 Op Fl E Ar timeout .Fl p Ar port .Pp .Nm @@ -106,6 +107,16 @@ to operate in the .Ar mousesystems mode. +.It Fl E Ar timeout +When the third button emulation is enabled +.Pq see above , +the +.Nm +daemon waits +.Ar timeout +msec at maximum before deciding if two buttons are pressed +simultaneously. +The default timeout is 200 msec. .It Fl F Ar rate Set the report rate (reports/sec) of the device if supported. .It Fl I Ar file