shithub: drawterm

Download patch

ref: 09820aa68b89b8c8093f2e28e088c0c8a4e10f1a
parent: ae6b4b42ff8ce93c060df1610bb762860a7f211c
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sat Dec 10 10:47:48 EST 2016

devmouse: provide absmousetrack() for os specific code instead of exposing te mouse info state

--- a/gui-osx/screen.c
+++ b/gui-osx/screen.c
@@ -31,7 +31,6 @@
 #define topLeft(r)  (((Point *) &(r))[0])
 #define botRight(r) (((Point *) &(r))[1])
 
-extern int mousequeue;
 static int depth;
 Boolean   gDone;
 RgnHandle gCursorRegionHdl;
@@ -269,32 +268,10 @@
 	}
 }
 
-void
+static void
 sendbuttons(int b, int x, int y)
 {
-	int i;
-	lock(&mouse.lk);
-	i = mouse.wi;
-	if(mousequeue) {
-		if(i == mouse.ri || mouse.lastb != b || mouse.trans) {
-			mouse.wi = (i+1)%Mousequeue;
-			if(mouse.wi == mouse.ri)
-				mouse.ri = (mouse.ri+1)%Mousequeue;
-			mouse.trans = mouse.lastb != b;
-		} else {
-			i = (i-1+Mousequeue)%Mousequeue;
-		}
-	} else {
-		mouse.wi = (i+1)%Mousequeue;
-		mouse.ri = i;
-	}
-	mouse.queue[i].xy.x = x;
-	mouse.queue[i].xy.y = y;
-	mouse.queue[i].buttons = b;
-	mouse.queue[i].msec = ticks();
-	mouse.lastb = b;
-	unlock(&mouse.lk);
-	wakeup(&mouse.r);
+	absmousetrack(x, y, b, ticks());
 }
 
 static Ptr fullScreenRestore;
--- a/gui-win32/screen.c
+++ b/gui-win32/screen.c
@@ -19,7 +19,6 @@
 Memimage	*gscreen;
 Screeninfo	screen;
 
-extern int mousequeue;
 static int depth;
 
 static	HINSTANCE	inst;
@@ -210,8 +209,6 @@
 	readybit = 1;
 	wakeup(&rend);
 
-	screen.reshaped = 0;
-
 	while(GetMessage(&msg, NULL, 0, 0)) {
 		TranslateMessage(&msg);
 		DispatchMessage(&msg);
@@ -387,28 +384,7 @@
 			else
 				b |= 4;
 		}
-		lock(&mouse.lk);
-		i = mouse.wi;
-		if(mousequeue) {
-			if(i == mouse.ri || mouse.lastb != b || mouse.trans) {
-				mouse.wi = (i+1)%Mousequeue;
-				if(mouse.wi == mouse.ri)
-					mouse.ri = (mouse.ri+1)%Mousequeue;
-				mouse.trans = mouse.lastb != b;
-			} else {
-				i = (i-1+Mousequeue)%Mousequeue;
-			}
-		} else {
-			mouse.wi = (i+1)%Mousequeue;
-			mouse.ri = i;
-		}
-		mouse.queue[i].xy.x = x;
-		mouse.queue[i].xy.y = y;
-		mouse.queue[i].buttons = b;
-		mouse.queue[i].msec = ticks();
-		mouse.lastb = b;
-		unlock(&mouse.lk);
-		wakeup(&mouse.r);
+		absmousetrack(x, y, b, ticks());
 		break;
 
 	case WM_CHAR:
--- a/gui-x11/x11.c
+++ b/gui-x11/x11.c
@@ -112,8 +112,6 @@
 static uchar			map7to8[128][2];
 static Colormap			xcmap;		/* Default shared colormap  */
 
-extern int mousequeue;
-
 /* for copy/paste, lifted from plan9ports */
 static Atom clipboard; 
 static Atom utf8string;
@@ -969,25 +967,7 @@
 	if(s & Button5Mask)
 		ms.buttons |= 16;
 
-	lock(&mouse.lk);
-	i = mouse.wi;
-	if(mousequeue) {
-		if(i == mouse.ri || mouse.lastb != ms.buttons || mouse.trans) {
-			mouse.wi = (i+1)%Mousequeue;
-			if(mouse.wi == mouse.ri)
-				mouse.ri = (mouse.ri+1)%Mousequeue;
-			mouse.trans = mouse.lastb != ms.buttons;
-		} else {
-			i = (i-1+Mousequeue)%Mousequeue;
-		}
-	} else {
-		mouse.wi = (i+1)%Mousequeue;
-		mouse.ri = i;
-	}
-	mouse.queue[i] = ms;
-	mouse.lastb = ms.buttons;
-	unlock(&mouse.lk);
-	wakeup(&mouse.r);
+	absmousetrack(ms.x, ms.y, ms.buttons, ms.msec);
 }
 
 void
--- a/kern/devmouse.c
+++ b/kern/devmouse.c
@@ -8,8 +8,6 @@
 #include	"memdraw.h"
 #include	"screen.h"
 
-int	mousequeue = 1;
-
 Mouseinfo	mouse;
 Cursorinfo	cursor;
 Cursorinfo      arrow = {
@@ -96,13 +94,9 @@
 			error(Eperm);
 		break;
 	case Qmouse:
-		lock(&mouse.lk);
-		if(mouse.open){
-			unlock(&mouse.lk);
+		if(tas(&mouse.open) != 0)
 			error(Einuse);
-		}
-		mouse.open = 1;
-		unlock(&mouse.lk);
+		mouse.lastcounter = mouse.state.counter;
 		break;
 	}
 	c->mode = openmode(omode);
@@ -119,9 +113,7 @@
 
 	switch((long)c->qid.path) {
 	case Qmouse:
-		lock(&mouse.lk);
 		mouse.open = 0;
-		unlock(&mouse.lk);
 		cursor = arrow;
 		setcursor();
 	}
@@ -131,9 +123,9 @@
 mouseread(Chan *c, void *va, long n, vlong offset)
 {
 	char buf[4*12+1];
+	Mousestate m;
 	uchar *p;
-	int i, nn, b;
-	ulong msec;
+	int b;
 	
 	p = va;
 	switch((long)c->qid.path){
@@ -158,46 +150,27 @@
 		while(mousechanged(0) == 0)
 			sleep(&mouse.r, mousechanged, 0);
 
-		lock(&screen.lk);
-		if(screen.reshaped) {
-			screen.reshaped = 0;
-			sprint(buf, "t%11d %11d", 0, ticks());
-			if(n > 1+2*12)
-				n = 1+2*12;
-			memmove(va, buf, n);
-			unlock(&screen.lk);
-			return n;
-		}
-		unlock(&screen.lk);
-
 		lock(&mouse.lk);
-		i = mouse.ri;
-		nn = (mouse.wi + Mousequeue - i) % Mousequeue;
-		if(nn < 1)
-			panic("empty mouse queue");
-		msec = ticks();
-		while(nn > 1) {
-			if(mouse.queue[i].msec + Mousewindow > msec)
-				break;
-			i = (i+1)%Mousequeue;
-			nn--;
-		}
-		b = buttonmap[mouse.queue[i].buttons&7];
+		if(mouse.ri != mouse.wi)
+			m = mouse.queue[mouse.ri++ % nelem(mouse.queue)];
+		else
+			m = mouse.state;
+		unlock(&mouse.lk);
+
+		b = buttonmap[m.buttons&7];
 		/* put buttons 4 and 5 back in */
-		b |= mouse.queue[i].buttons & (3<<3);
-		if(scrollswap){
-			if(b == 8)
+		b |= m.buttons & (3<<3);
+		if (scrollswap) {
+			if (b == 8)
 				b = 16;
-			else if(b == 16)
+			else if (b == 16)
 				b = 8;
 		}
-		sprint(buf, "m%11d %11d %11d %11d",
-			mouse.queue[i].xy.x,
-			mouse.queue[i].xy.y,
-			b,
-			mouse.queue[i].msec);
-		mouse.ri = (i+1)%Mousequeue;
-		unlock(&mouse.lk);
+		sprint(buf, "m%11d %11d %11d %11lud ",
+			m.xy.x, m.xy.y, b, m.msec);
+
+		mouse.lastcounter = m.counter;
+
 		if(n > 1+4*12)
 			n = 1+4*12;
 		memmove(va, buf, n);
@@ -335,8 +308,42 @@
 mousechanged(void *a)
 {
 	USED(a);
+	return mouse.lastcounter != mouse.state.counter;
+}
 
-	return mouse.ri != mouse.wi || screen.reshaped;
+void
+absmousetrack(int x, int y, int b, int msec)
+{
+	int lastb;
+
+	if(gscreen==nil)
+		return;
+
+	if(x < gscreen->clipr.min.x)
+		x = gscreen->clipr.min.x;
+	if(x >= gscreen->clipr.max.x)
+		x = gscreen->clipr.max.x-1;
+	if(y < gscreen->clipr.min.y)
+		y = gscreen->clipr.min.y;
+	if(y >= gscreen->clipr.max.y)
+		y = gscreen->clipr.max.y-1;
+
+	lock(&mouse.lk);
+	mouse.state.xy = Pt(x, y);
+	lastb = mouse.state.buttons;
+	mouse.state.buttons = b;
+	mouse.state.msec = msec;
+	mouse.state.counter++;
+
+	/*
+	 * if the queue fills, don't queue any more events until a
+	 * reader polls the mouse.
+	 */
+	if(b != lastb && (mouse.wi-mouse.ri) < nelem(mouse.queue))
+		mouse.queue[mouse.wi++ % nelem(mouse.queue)] = mouse.state;
+	unlock(&mouse.lk);
+
+	wakeup(&mouse.r);
 }
 
 Dev mousedevtab = {
--- a/kern/screen.h
+++ b/kern/screen.h
@@ -3,23 +3,22 @@
 typedef struct Cursorinfo Cursorinfo;
 typedef struct Screeninfo Screeninfo;
 
-#define Mousequeue 16		/* queue can only have Mousequeue-1 elements */
-#define Mousewindow 500		/* mouse event window in millisec */
-
 struct Mousestate {
-	int	buttons;
 	Point	xy;
+	int	buttons;
+	ulong	counter;
 	ulong	msec;
 };
 
 struct Mouseinfo {
-	Lock	lk;
-	Mousestate queue[Mousequeue];
-	int	ri, wi;
-	int	lastb;
-	int	trans;
-	int	open;
-	Rendez	r;
+	Lock		lk;
+	Mousestate	state;
+	ulong		lastcounter;
+	Rendez		r;
+	int		open;
+	Mousestate	queue[16];	/* circular buffer of click events */
+	ulong		ri;		/* read index into queue */
+	ulong		wi;		/* write index into queue */
 };
 
 struct Cursorinfo {
@@ -30,11 +29,9 @@
 };
 
 struct Screeninfo {
-	Lock		lk;
-	Memimage	*newsoft;
-	int		reshaped;
-	int		depth;
-	int		dibtype;
+	Lock	lk;
+	int	depth;
+	int	dibtype;
 };
 
 extern	Memimage *gscreen;
@@ -55,6 +52,8 @@
 uchar*	attachscreen(Rectangle*, ulong*, int*, int*, int*);
 
 extern	QLock drawlock;
-#define	ishwimage(i)	1
+#define	ishwimage(i)	0
 
 void	terminit(void);
+
+void	absmousetrack(int, int, int, int);