code: drawterm

Download patch

ref: 354d513a481471aed9458886ec942aa9c8bd1dc9
parent: 289afc11ba1a173e7de67d4e5c0850531de478d2
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Mon Jul 18 07:04:49 EDT 2016

devmouse: implement swap, scrollswap and buttonmap mousectl

--- a/kern/devmouse.c
+++ b/kern/devmouse.c
@@ -27,18 +27,37 @@
 	},
 };
 
+static uchar	buttonmap[8] = {0,1,2,3,4,5,6,7,};
+static int	mouseswap;
+static int	scrollswap;
+
 static int	mousechanged(void*);
 
-enum{
+enum {
+	CMbuttonmap,
+	CMscrollswap,
+	CMswap,
+};
+
+static Cmdtab mousectlmsg[] = 
+{
+	CMbuttonmap,	"buttonmap",	0,
+	CMscrollswap,	"scrollswap",	0,
+	CMswap,		"swap",		1,
+};
+
+enum {
 	Qdir,
 	Qcursor,
-	Qmouse
+	Qmouse,
+	Qmousectl
 };
 
 Dirtab mousedir[]={
 	".",		{Qdir, 0, QTDIR},	0,	DMDIR|0555,	
-	"cursor",	{Qcursor},	0,			0666,
-	"mouse",	{Qmouse},	0,			0666,
+	"cursor",	{Qcursor},		0,	0666,
+	"mouse",	{Qmouse},		0,	0666,
+	"mousectl",	{Qmousectl},		0,	0222,
 };
 
 #define	NMOUSE	(sizeof(mousedir)/sizeof(Dirtab))
@@ -108,16 +127,14 @@
 	}
 }
 
-
 long
 mouseread(Chan *c, void *va, long n, vlong offset)
 {
 	char buf[4*12+1];
 	uchar *p;
-	int i, nn;
+	int i, nn, b;
 	ulong msec;
-/*	static int map[8] = {0, 4, 2, 6, 1, 5, 3, 7 };	*/
-
+	
 	p = va;
 	switch((long)c->qid.path){
 	case Qdir:
@@ -165,10 +182,19 @@
 			i = (i+1)%Mousequeue;
 			nn--;
 		}
+		b = buttonmap[mouse.queue[i].buttons&7];
+		/* put buttons 4 and 5 back in */
+		b |= mouse.queue[i].buttons & (3<<3);
+		if(scrollswap){
+			if(b == 8)
+				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,
-			mouse.queue[i].buttons,
+			b,
 			mouse.queue[i].msec);
 		mouse.ri = (i+1)%Mousequeue;
 		unlock(&mouse.lk);
@@ -180,6 +206,49 @@
 	return 0;
 }
 
+static void
+setbuttonmap(char* map)
+{
+	int i, x, one, two, three;
+
+	one = two = three = 0;
+	for(i = 0; i < 3; i++){
+		if(map[i] == 0)
+			error(Ebadarg);
+		if(map[i] == '1'){
+			if(one)
+				error(Ebadarg);
+			one = 1<<i;
+		}
+		else if(map[i] == '2'){
+			if(two)
+				error(Ebadarg);
+			two = 1<<i;
+		}
+		else if(map[i] == '3'){
+			if(three)
+				error(Ebadarg);
+			three = 1<<i;
+		}
+		else
+			error(Ebadarg);
+	}
+	if(map[i])
+		error(Ebadarg);
+
+	memset(buttonmap, 0, 8);
+	for(i = 0; i < 8; i++){
+		x = 0;
+		if(i & 1)
+			x |= one;
+		if(i & 2)
+			x |= two;
+		if(i & 4)
+			x |= three;
+		buttonmap[x] = i;
+	}
+}
+
 long
 mousewrite(Chan *c, void *va, long n, vlong offset)
 {
@@ -186,6 +255,8 @@
 	char *p;
 	Point pt;
 	char buf[64];
+	Cmdbuf *cb;
+	Cmdtab *ct;
 
 	USED(offset);
 
@@ -223,6 +294,37 @@
 		if(ptinrect(pt, gscreen->r))
 			mouseset(pt);
 		return n;
+
+	case Qmousectl:
+		cb = parsecmd(va,n);
+		if(waserror()){
+			free(cb);
+			nexterror();
+		}
+		ct = lookupcmd(cb, mousectlmsg, nelem(mousectlmsg));
+		switch(ct->index){
+		case CMswap:
+			if(mouseswap)
+				setbuttonmap("123");
+			else
+				setbuttonmap("321");
+			mouseswap ^= 1;
+			break;
+
+		case CMscrollswap:
+			scrollswap ^= 1;
+			break;
+
+		case CMbuttonmap:
+			if(cb->nf==1)
+				setbuttonmap("123");
+			else
+				setbuttonmap(cb->f[1]);
+			break;
+		}
+		free(cb);
+		poperror();
+		return n;
 	}
 
 	error(Egreg);
@@ -257,4 +359,5 @@
 	devremove,
 	devwstat,
 };
+