shithub: orca

Download patch

ref: 1afac4d96677c1a88600c9e0fe01959f50e68083
parent: 8dc5d00e9e157ed86b4c98326ea09ea920080c3f
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Fri Feb 21 18:59:52 EST 2020

plan9: refactor key handling and a few other things

--- a/plan9.c
+++ b/plan9.c
@@ -8,8 +8,6 @@
 #include <keyboard.h>
 #include <thread.h>
 
-typedef struct Key Key;
-
 #define MIN(x,y) ((x)<=(y)?(x):(y))
 #define MAX(x,y) ((x)>=(y)?(x):(y))
 #define is_movement(c) (c == 'W' || c == 'N' || c == 'E' || c == 'S')
@@ -19,10 +17,9 @@
 	Txtoff = 16,
 	Coloff = 2,
 
-	Cchar = 0,
+	Cresize = 0,
 	Ckey,
 	Cmouse,
-	Cresize,
 	Credraw,
 	Numchan,
 
@@ -58,11 +55,6 @@
 	Mark_flag_selected = 1<<7,
 };
 
-struct Key {
-	int down;
-	Rune rune;
-};
-
 static int bpm = 120, apm = 120;
 static Point rulers = {8, 8};
 static int rulerstyle = Sfancy, dotstyle = Sfancy;
@@ -71,18 +63,18 @@
 static Rune cursor = '@';
 static vlong tick;
 static Point glyphsz;
-static Point cur, scroll;
+static Point cur, scroll, move = { .x = 1, .y = 1 };
 static Rectangle sel;
 static Field field;
 static Mbuf_reusable mbuf, mscr;
 static char filename[256];
-static Channel *cchan;
 static Field fscr, fsel;
-static bool altdown, pause, forward;
+static bool altdown, shiftdown, ctldown, pause, forward;
 static int mode = Minsert;
 static long framedev; /* frame deviation in ms */
 static char *ip, *udpport;
 static int udp = -1;
+static char *shellcmd;
 
 static struct {
 	struct {
@@ -214,8 +206,6 @@
 		cur.y = sel.max.y;
 }
 
-static char *shellcmd;
-
 static void
 runshell(void *x)
 {
@@ -453,7 +443,9 @@
 			else if (end - n > 25000000LL)
 				sleep(20);
 			else if (end - n > 10000000LL)
-				sleep(1);
+				sleep((end - n) / 1000000LL);
+			else
+				yield();
 		} while (!forward);
 
 		framedev = (processnew - processold - 15000000000LL/bpm)/1000000LL;
@@ -871,12 +863,10 @@
 }
 
 static void
-kbdproc(void *k)
+kbdproc(void *cchan)
 {
 	char buf[128], buf2[128], *s;
-	Channel *kchan;
 	int kfd, n;
-	Key key;
 	Rune r;
 
 	threadsetname("kbdproc");
@@ -883,7 +873,6 @@
 	if ((kfd = open("/dev/kbd", OREAD)) < 0)
 		sysfatal("/dev/kbd: %r");
 
-	kchan = k;
 	buf2[0] = 0;
 	buf2[1] = 0;
 	buf[0] = 0;
@@ -912,9 +901,14 @@
 			while (*s) {
 				s += chartorune(&r, s);
 				if (utfrune(buf2+1, r) == nil) {
-					key.down = 1;
-					key.rune = r;
-					nbsend(kchan, &key);
+					if (r == Kalt) {
+						altdown = true;
+					} else if (r == Kshift) {
+						shiftdown = true;
+					} else if (r == Kctl) {
+						ctldown = true;
+						move = Pt(rulers.x, rulers.y);
+					}
 				}
 			}
 			break;
@@ -924,9 +918,14 @@
 			while (*s) {
 				s += chartorune(&r, s);
 				if (utfrune(buf+1, r) == nil) {
-					key.down = 0;
-					key.rune = r;
-					nbsend(kchan, &key);
+					if (r == Kalt) {
+						altdown = false;
+					} else if (r == Kshift) {
+						shiftdown = false;
+					} else if (r == Kctl) {
+						ctldown = false;
+						move = Pt(1, 1);
+					}
 				}
 			}
 			break;
@@ -979,18 +978,15 @@
 threadmain(int argc, char **argv)
 {
 	Mousectl *mctl;
-	Key key;
 	Keyboardctl kctl;
+	Rune r;
 	Mouse m;
 	char tmp[256];
 	int oldw, oldh, w, h, n, oldbuttons;
 	long seed;
-	bool inverse;
-	Point move;
-	bool shiftdown, complete, ctldown;
+	bool inverse, complete;
 	Alt a[Numchan+1] = {
-		[Cchar] = { nil, &key.rune, CHANRCV },
-		[Ckey] = { nil, &key, CHANRCV },
+		[Ckey] = { nil, &r, CHANRCV },
 		[Cmouse] = { nil, &m, CHANRCV },
 		[Cresize] = { nil, nil, CHANRCV },
 		[Credraw] = { nil, nil, CHANRCV },
@@ -1060,16 +1056,16 @@
 		sysfatal("initdraw: %r");
 	if ((mctl = initmouse(nil, screen)) == nil)
 		sysfatal("initmouse: %r");
+	display->locking = 1;
+	unlockdisplay(display);
 
-	cchan = chancreate(sizeof(Rune), 20);
-	a[Cchar].c = cchan;
-	a[Ckey].c = chancreate(sizeof(Key), 20);
+	a[Ckey].c = chancreate(sizeof(Rune), 20);
 	a[Cmouse].c = mctl->c;
 	a[Cresize].c = mctl->resizec;
 	a[Credraw].c = chancreate(sizeof(ulong), 0);
 
 	proccreate(kbdproc, a[Ckey].c, mainstacksize);
-	kctl.c = cchan;
+	kctl.c = a[Ckey].c;
 
 	proccreate(stdinproc, nil, mainstacksize);
 
@@ -1076,7 +1072,7 @@
 	for (n = 0; n < Numcolors; n++) {
 		if (inverse)
 			theme[n] = ~theme[n] | 0xff;
-		color[n] = allocimage(display, Rect(0, 0, 1, 1), RGBA32, 1, setalpha(theme[n] & ~0xff, theme[n] & 0xff));
+		color[n] = allocimage(display, Rect(0, 0, 1, 1), RGB24, 1, theme[n]);
 	}
 	glyphsz.x = stringwidth(font, "@");
 	glyphsz.y = font->height;
@@ -1157,13 +1153,13 @@
 				n = menuhit(3, mctl, &menu3, nil);
 				snprint(tmp, sizeof(tmp), "%s", filename);
 				if (n == Menu3load) {
-					if (enter("load from:", tmp, sizeof(tmp), nil, &kctl, nil) > 0 && fieldload(tmp) == 0) {
+					if (enter("load from:", tmp, sizeof(tmp), mctl, &kctl, nil) > 0 && fieldload(tmp) == 0) {
 						w = field.width;
 						h = field.height;
 						snprint(filename, sizeof(filename), "%s", tmp);
 					}
 				} else if (n == Menu3save) {
-					if ((tmp[0] != 0 || enter("save to:", tmp, sizeof(tmp), nil, &kctl, nil) > 0) || fieldsave(tmp) == 0)
+					if ((tmp[0] != 0 || enter("save to:", tmp, sizeof(tmp), mctl, &kctl, nil) > 0) || fieldsave(tmp) == 0)
 						snprint(filename, sizeof(filename), "%s", tmp);
 				} else if (n == Menu3dotstyle) {
 					dotstyle = ++dotstyle % Numstyles;
@@ -1184,34 +1180,11 @@
 			scroll = ZP;
 			break;
 
-		case Ckey:
-			switch (key.rune) {
-			case Kshift:
-				shiftdown = key.down;
-				break;
-			case Kalt:
-				altdown = key.down;
-				break;
-			case Kctl:
-				ctldown = key.down;
-				move = Pt(ctldown ? rulers.x : 1, ctldown ? rulers.y : 1);
-				break;
-			case Kup:
-			case Kdown:
-			case Kleft:
-			case Kright:
-				break;
-			default:
-//				fprint(2, "unknown key down/up 0x%x\n", key.rune);
-				break;
-			}
-			break;
-
 		case Credraw:
 			break;
 
-		case Cchar:
-			switch (key.rune) {
+		case Ckey:
+			switch (r) {
 			case '\n': /* C-j */
 				/* FIXME bang it */
 				break;
@@ -1378,17 +1351,17 @@
 					break;
 				}
 			default:
-				if (key.rune == Kdel || key.rune == ' ')
-					key.rune = '.';
-				if (orca_is_valid_glyph(key.rune)) {
+				if (r == Kdel || r == ' ')
+					r = '.';
+				if (orca_is_valid_glyph(r)) {
 					if (mode != Mappend) {
-						selset(key.rune);
+						selset(r);
 					} else {
-						fieldset(cur.x, cur.y, key.rune);
+						fieldset(cur.x, cur.y, r);
 						curmove(1, 0);
 					}
 				} else {
-//					fprint(2, "unhandled key %04x\n", key.rune);
+//					fprint(2, "unhandled key %04x\n", r);
 					goto noredraw;
 				}
 				break;