shithub: rd

Download patch

ref: f3519cb0fa6379bd0bb074bcdb82bff9c2b8e05d
parent: ad801965ed1961ad306bd5611d3a01de34614f55
author: Yaroslav Kolomiiets <yarikos@gmail.com>
date: Fri Aug 5 11:41:12 EDT 2016

re-arrange kbd/mouse across the modules

--- a/dat.h
+++ b/dat.h
@@ -67,6 +67,7 @@
 
 void	initscreen(Rdp*);
 void	readkbd(Rdp*);
+void	sendkbd(Rdp*, Rune);
 void	readdevmouse(Rdp*);
 void	eresized(Rdp*, int);
 
--- a/egdi.c
+++ b/egdi.c
@@ -198,9 +198,11 @@
 		if(p == nil)
 			break;
 	}
-	lockdisplay(display);
+	if(display->locking)
+		lockdisplay(display);
 	flushimage(display, 1);
-	unlockdisplay(display);
+	if(display->locking)
+		unlockdisplay(display);
 	return;
 
 ErrNstd:
@@ -314,9 +316,11 @@
 	rs = rectaddpt(rs, screen->r.min);
 	ps = addpt(pt, screen->r.min);
 
-	lockdisplay(display);
+	if(display->locking)
+		lockdisplay(display);
 	draw(screen, rs, screen, nil, ps);
-	unlockdisplay(display);
+	if(display->locking)
+		unlockdisplay(display);
 
 	return p;
 }
@@ -358,13 +362,15 @@
 		return p;
 	}
 
-	lockdisplay(display);
+	if(display->locking)
+		lockdisplay(display);
 	if(ctl&Clipped)
 		replclipr(screen, screen->repl, rectaddpt(gc.clipr, screen->r.min));
 	draw(screen, rectaddpt(r, screen->r.min), img, nil, pt);
 	if(ctl&Clipped)
 		replclipr(screen, screen->repl, screen->r);
-	unlockdisplay(display);
+	if(display->locking)
+		unlockdisplay(display);
 
 	return p;
 }
--- a/kbd.c
+++ b/kbd.c
@@ -138,110 +138,88 @@
 }
 
 void
-readkbd(Rdp* c)
+sendkbd(Rdp* c, Rune r)
 {
-	char buf[256], k[10];
-	int ctlfd, fd, kr, kn, w;
 	uchar mod, sc;
-	Rune r;
 
-	if((fd = open("/dev/cons", OREAD)) < 0)
-		sysfatal("open %s: %r", buf);
-	if((ctlfd = open("/dev/consctl", OWRITE)) < 0)
-		sysfatal("open %s: %r", buf);
-	write(ctlfd, "rawon", 5);
+	if(r < nelem(rune2scan)){
+		sc = rune2scan[r].sc;
+		mod = rune2scan[r].mod;
+		kbdsendscan(c, sc, mod);
+		return;
+	}
 
-	kn = 0;
-	for(;;){
-		while(!fullrune(k, kn)){
-			kr = read(fd, k+kn, sizeof k - kn);
-			if(kr <= 0)
-				sysfatal("bad read from kbd");
-			kn += kr;
-		}
-		w = chartorune(&r, k);
-		kn -= w;
-		memmove(k, &k[w], kn);
-
-		if(r < nelem(rune2scan)){
-			sc = rune2scan[r].sc;
-			mod = rune2scan[r].mod;
-			kbdsendscan(c, sc, mod);
-			continue;
-		}
-
-		switch(r){
-		case Kins:
-			kbdsendscan(c, Sins, 0);
-			break;
-		case Kdel:
-			kbdsendscan(c, Sdel, 0);
-			break;
-		case Khome:
-			kbdsendscan(c, Shome, 0);
-			break;
-		case Kend:
-			kbdsendscan(c, Send, 0);
-			break;
-		case Kpgup:
-			kbdsendscan(c, Spgup, 0);
-			break;
-		case Kpgdown:
-			kbdsendscan(c, Spgdown, 0);
-			break;
-		case Kup:
-			kbdsendscan(c, Sup, 0);
-			break;
-		case Kdown:
-			kbdsendscan(c, Sdown,0 );
-			break;
-		case Kleft:
-			kbdsendscan(c, Sleft, 0);
-			break;
-		case Kright:
-			kbdsendscan(c, Sright, 0);
-			break;
-		case Kbrk:
-			exits("interrupt");
-			break;
-		case Kprint:
-			kbdsendscan(c, Sprint, 0);
-			break;
-		case KF|1:
-		case KF|2:
-		case KF|3:
-		case KF|4:
-		case KF|5:
-		case KF|6:
-		case KF|7:
-		case KF|8:
-		case KF|9:
-		case KF|10:
-			kbdsendscan(c, SF1+r-(KF|1), 0);
-			break;
-		case KF|11:
-			kbdsendscan(c, SF11, 0);
-			break;
-		case KF|12:
-			kbdsendscan(c, SF12, 0);
-			break;
-		case '0':
-			kbdsendscan(c, S0, 0);
-			break;
-		case '1':
-		case '2':
-		case '3':
-		case '4':
-		case '5':
-		case '6':
-		case '7':
-		case '8':
-		case '9':
-			kbdsendscan(c, S1+r-'1', 0);
-			break;
-		default:
-			kbdsendrune(c, r);
-			break;
-		}
+	switch(r){
+	case Kins:
+		kbdsendscan(c, Sins, 0);
+		break;
+	case Kdel:
+		kbdsendscan(c, Sdel, 0);
+		break;
+	case Khome:
+		kbdsendscan(c, Shome, 0);
+		break;
+	case Kend:
+		kbdsendscan(c, Send, 0);
+		break;
+	case Kpgup:
+		kbdsendscan(c, Spgup, 0);
+		break;
+	case Kpgdown:
+		kbdsendscan(c, Spgdown, 0);
+		break;
+	case Kup:
+		kbdsendscan(c, Sup, 0);
+		break;
+	case Kdown:
+		kbdsendscan(c, Sdown,0 );
+		break;
+	case Kleft:
+		kbdsendscan(c, Sleft, 0);
+		break;
+	case Kright:
+		kbdsendscan(c, Sright, 0);
+		break;
+	case Kbrk:
+		exits("interrupt");
+		break;
+	case Kprint:
+		kbdsendscan(c, Sprint, 0);
+		break;
+	case KF|1:
+	case KF|2:
+	case KF|3:
+	case KF|4:
+	case KF|5:
+	case KF|6:
+	case KF|7:
+	case KF|8:
+	case KF|9:
+	case KF|10:
+		kbdsendscan(c, SF1+r-(KF|1), 0);
+		break;
+	case KF|11:
+		kbdsendscan(c, SF11, 0);
+		break;
+	case KF|12:
+		kbdsendscan(c, SF12, 0);
+		break;
+	case '0':
+		kbdsendscan(c, S0, 0);
+		break;
+	case '1':
+	case '2':
+	case '3':
+	case '4':
+	case '5':
+	case '6':
+	case '7':
+	case '8':
+	case '9':
+		kbdsendscan(c, S1+r-'1', 0);
+		break;
+	default:
+		kbdsendrune(c, r);
+		break;
 	}
 }
--- a/mouse.c
+++ b/mouse.c
@@ -15,16 +15,14 @@
 	MouseBdown=	0x8000,
 };
 
-static int mfd = -1;
-
 static void
-sendmouse(Rdp* c, Mouse m, int f)
+sendmouse1(Rdp* c, Mouse m, int f)
 {
 	passinput(c, m.msec, InputMouse, f, m.xy.x, m.xy.y);
 }
 
-static void
-mouseevent(Rdp* c, Mouse m)
+void
+sendmouse(Rdp* c, Mouse m)
 {
 	static Mouse o;
 	int down;
@@ -33,62 +31,30 @@
 	m.xy = subpt(m.xy, screen->r.min);
 	switch(m.buttons){
 	case 8:
-		sendmouse(c, m, MouseB4|MouseBdown);
-		sendmouse(c, m, MouseB4);
+		sendmouse1(c, m, MouseB4|MouseBdown);
+		sendmouse1(c, m, MouseB4);
 		return;
 	case 16:
-		sendmouse(c, m, MouseB5|MouseBdown);
-		sendmouse(c, m, MouseB5);
+		sendmouse1(c, m, MouseB5|MouseBdown);
+		sendmouse1(c, m, MouseB5);
 		return;
 	}
 
 	if(!eqpt(m.xy, o.xy))
-		sendmouse(c, m, MouseMove);
+		sendmouse1(c, m, MouseMove);
 
 	chg = m.buttons ^ o.buttons;
 	if(chg&1){
 		down = (m.buttons&1)? MouseBdown : 0;
-		sendmouse(c, m, MouseB1|down);
+		sendmouse1(c, m, MouseB1|down);
 	}
 	if(chg&2){
 		down = (m.buttons&2)? MouseBdown : 0;
-		sendmouse(c, m, MouseB3|down);
+		sendmouse1(c, m, MouseB3|down);
 	}
 	if(chg&4){
 		down = (m.buttons&4)? MouseBdown : 0;
-		sendmouse(c, m, MouseB2|down);
+		sendmouse1(c, m, MouseB2|down);
 	}
 	o = m;
-}
-
-void
-readdevmouse(Rdp* c)
-{
-	Mouse m;
-	char ev[1+4*12];
-
-	if((mfd = open("/dev/mouse", ORDWR)) < 0)
-		sysfatal("open /dev/mouse: %r");
-
-	for(;;){
-		if(read(mfd, ev, sizeof ev) != sizeof ev)
-			sysfatal("mouse eof");
-		if(*ev == 'm'){
-			m.xy.x = atoi(ev+1+0*12);
-			m.xy.y = atoi(ev+1+1*12);
-			m.buttons = atoi(ev+1+2*12) & 0x1F;
-			m.msec = atoi(ev+1+3*12);
-			mouseevent(c, m);
-		}else
-			eresized(c, 1);
-	}
-}
-
-void
-warpmouse(int x, int y)
-{
-	if(mfd < 0)
-		return;
-
-	fprint(mfd, "m%d %d", x, y);
 }
--- a/rd.c
+++ b/rd.c
@@ -2,6 +2,7 @@
 #include <libc.h>
 #include <auth.h>
 #include <draw.h>
+#include <mouse.h>
 #include "dat.h"
 #include "fns.h"
 
@@ -18,6 +19,8 @@
 char Esmall[]=	"buffer too small";
 char Ebignum[]=	"number too big";
 
+void	sendmouse(Rdp* c, Mouse m);
+
 static void
 usage(void)
 {
@@ -208,6 +211,68 @@
 }
 
 void
+readkbd(Rdp* c)
+{
+	char buf[256], k[10];
+	int ctlfd, fd, kr, kn, w;
+	Rune r;
+
+	if((fd = open("/dev/cons", OREAD)) < 0)
+		sysfatal("open %s: %r", buf);
+	if((ctlfd = open("/dev/consctl", OWRITE)) < 0)
+		sysfatal("open %s: %r", buf);
+	write(ctlfd, "rawon", 5);
+
+	kn = 0;
+	for(;;){
+		while(!fullrune(k, kn)){
+			kr = read(fd, k+kn, sizeof k - kn);
+			if(kr <= 0)
+				sysfatal("bad read from kbd");
+			kn += kr;
+		}
+		w = chartorune(&r, k);
+		kn -= w;
+		memmove(k, &k[w], kn);
+		sendkbd(c, r);
+	}
+}
+
+static int mfd = -1;
+
+void
+readdevmouse(Rdp* c)
+{
+	Mouse m;
+	char ev[1+4*12];
+
+	if((mfd = open("/dev/mouse", ORDWR)) < 0)
+		sysfatal("open /dev/mouse: %r");
+
+	for(;;){
+		if(read(mfd, ev, sizeof ev) != sizeof ev)
+			sysfatal("mouse eof");
+		if(*ev == 'm'){
+			m.xy.x = atoi(ev+1+0*12);
+			m.xy.y = atoi(ev+1+1*12);
+			m.buttons = atoi(ev+1+2*12) & 0x1F;
+			m.msec = atoi(ev+1+3*12);
+			sendmouse(c, m);
+		}else
+			eresized(c, 1);
+	}
+}
+
+void
+warpmouse(int x, int y)
+{
+	if(mfd < 0)
+		return;
+
+	fprint(mfd, "m%d %d", x, y);
+}
+
+void
 readnet(Rdp* c)
 {
 	Msg r;
@@ -298,7 +363,8 @@
 
 	rs = rectaddpt(Rpt(ZP, Pt(c->xsz, c->ysz)), screen->r.min);
 
-	lockdisplay(display);
+	if(display->locking)
+		lockdisplay(display);
 
 	if(img==nil || !eqrect(img->r, rs)){
 		if(img != nil)
@@ -329,7 +395,8 @@
 		nr--;
 	}
 	flushimage(display, 1);
-	unlockdisplay(display);
+	if(display->locking)
+		unlockdisplay(display);
 }
 
 void
--- a/wsys.c
+++ b/wsys.c
@@ -40,7 +40,8 @@
 	int fd;
 	Point d;
 
-	lockdisplay(display);
+	if(display->locking)
+		lockdisplay(display);
 	if(getwindow(display, Refnone) < 0)
 		sysfatal("resize failed: %r");
 
@@ -55,7 +56,8 @@
 	}
 	turnupdates(c, 0);
 	turnupdates(c, !ishidden());
-	unlockdisplay(display);
+	if(display->locking)
+		unlockdisplay(display);
 }
 void
 initsnarf(void)