#include #include #include #define TEMP "/n/temp" #define STACKSIZE 4096 struct { QLock; int mfd; int rfd, wfd; QLock prs; int pressed; int b; int x; int y; long t; vlong nsec; } b2; static void tproc(void *); static void wproc(void *); void threadmain(int argc, char *argv[]) { char *file = "/dev/mouse"; int n, b, x, y; long t; char buf[50]; b2.mfd = open("/dev/mouse", ORDWR|OCEXEC); if (b2.mfd < 0) sysfatal("open /dev/mouse: %r"); if (bind("#|", TEMP, MREPL) < 0) sysfatal("bind pipe %s: %r", TEMP); if (bind(TEMP "/data", "/dev/mouse", MREPL) < 0) sysfatal("bind %s /dev/mouse: %r", TEMP "/data"); b2.rfd = open(TEMP "/data1", OREAD); if (b2.rfd < 0) sysfatal("open %s: %r", TEMP "/data1"); b2.wfd = open(TEMP "/data1", OWRITE); if (b2.wfd < 0) sysfatal("open %s: %r", TEMP "/data1"); qlock(&b2.prs); proccreate(wproc, 0, STACKSIZE); proccreate(tproc, 0, STACKSIZE); while (1) { n = read(b2.mfd, buf, 49); x = atoi(buf + 1 + 0*12); y = atoi(buf + 1 + 1*12); b = atoi(buf + 1 + 2*12); t = atoi(buf + 1 + 3*12); qlock(&b2); if (b & 0x02) { if (b2.pressed == 0) { qunlock(&b2.prs); b2.pressed = 1; } b2.t = t; b2.x = x; b2.y = y; b2.b = b; b2.nsec = nsec(); } if (b2.pressed) b |= 0x02; sprint(buf, "m%11d %11d %11d %11lud", x, y, b, t); write(b2.wfd, buf, 49); qunlock(&b2); } } static void wproc(void *) { int n; char buf[50]; while (1) { n = read(b2.rfd, buf, sizeof(buf)); if (n < 0) break; write(b2.mfd, buf, n); } } static void tproc(void *) { vlong t1, t2; char buf[50]; while (1) { qlock(&b2.prs); qlock(&b2); sprint(buf, "m%11d %11d %11d %11lud", b2.x, b2.y, b2.b | 0x02, b2.t); write(b2.wfd, buf, 49); qunlock(&b2); /* delay between first and second event (400ms) */ sleep(400); qlock(&b2); b2.nsec = nsec(); qunlock(&b2); while (1) { /* delay between events (150ms) */ sleep(80); qlock(&b2); t2 = nsec(); if ((t2 - b2.nsec) > 150000000L) { b2.nsec = t2; b2.pressed = 0; sprint(buf, "m%11d %11d %11d %11lud", b2.x, b2.y, b2.b & ~0x02, b2.t); write(b2.wfd, buf, 49); qunlock(&b2); break; } qunlock(&b2); } } }