shithub: orca

Download patch

ref: 54d976397b07916da9c97407423eff7cee67cc54
parent: b3a6ad3b98ad52019f403dcbe0ec1d0cdc925574
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Thu Feb 13 06:26:32 EST 2020

plan9: minimize frame time deviations, also display it in microseconds

--- a/plan9.c
+++ b/plan9.c
@@ -42,6 +42,7 @@
 static Channel *cchan;
 static Field copyfield;
 static int altdown;
+static long framedev; /* frame deviation >= 1µs */
 
 static char *menu3i[] = {
 	"load",
@@ -158,9 +159,19 @@
 orcathread(void *drawchan)
 {
 	vlong start, end, n, oldn;
-	int w, h, oldbpm;
+	vlong processold, processnew;
+	int w, h, oldbpm, fd;
+	char tmp[64];
 
 	threadsetname("orca/sim");
+
+	snprint(tmp, sizeof(tmp), "/proc/%d/ctl", getpid());
+	if((fd = open(tmp, OWRITE)) >= 0){
+		fprint(fd, "pri 13\n");
+		close(fd);
+	}
+
+	processnew = nanosec();
 	for (;;) {
 		start = nanosec();
 		w = field.width;
@@ -169,9 +180,11 @@
 		oevent_list_clear(&events);
 		orca_run(field.buffer, mbuf.buffer, h, w, tick, &events, 0);
 
+		processold = processnew;
+		processnew = nanosec();
 		process(&events);
 		tick++;
-		nbsendul(drawchan, Msgredraw);
+		sendul(drawchan, Msgredraw);
 
 		oldn = start;
 		end = start + 1;
@@ -178,7 +191,7 @@
 		oldbpm = 0;
 		for (n = start; pause || n < end; n = nanosec()) {
 			if (bpm != oldbpm) {
-				end = start + (15000000000.0 / (double)bpm); /* 10^9 * 60 / 4 */
+				end = start + 15000000000LL/bpm; /* 10^9 * 60 / 4 */
 				oldbpm = bpm;
 			}
 
@@ -187,9 +200,15 @@
 				end -= oldn - n;
 
 			oldn = n;
-			yield();
-			sleep(5);
+			if (pause || end - n > 750000000LL)
+				sleep(70);
+			else if (end - n > 25000000LL)
+				sleep(20);
+			else if (end - n > 10000000LL)
+				sleep(1);
 		}
+
+		framedev = ((processnew - processold) - (15000000000LL / bpm)) / 1000LL;
 	}
 }
 
@@ -249,7 +268,7 @@
 	/* bpm */
 	p.y -= font->height;
 	p.x += charw * (len + 3);
-	runesprint(linebuf, "%d", bpm);
+	len = runesprint(linebuf, "%d", bpm);
 	runestring(screen, p, display->white, ZP, font, linebuf);
 
 	/* filename */
@@ -256,6 +275,12 @@
 	p.y += font->height;
 	string(screen, p, display->white, ZP, font, filename);
 
+	/* frame deviation */
+	p.y -= font->height;
+	p.x += charw * (len + 3);
+	runesprint(linebuf, "%ldµs", labs(framedev));
+	runestring(screen, p, display->white, ZP, font, linebuf);
+
 	/* cursor bg */
 	p = top;
 	p.x += curx * charw;
@@ -363,7 +388,7 @@
 	Key key;
 	Rune r;
 
-	threadsetname("orca/kbd");
+	threadsetname("kbdproc");
 	if ((kfd = open("/dev/kbd", OREAD)) < 0)
 		sysfatal("can't open kbd: %r");
 
@@ -459,7 +484,7 @@
 	a[0].c = mctl->c;
 	a[1].c = mctl->resizec;
 	a[2].c = kchan;
-	a[3].c = chancreate(sizeof(ulong), 0);
+	a[3].c = chancreate(sizeof(ulong), 20);
 	a[4].c = cchan;
 
 	curbg = allocimage(display, Rect(0, 0, 1, 1), RGBA32, 1, setalpha(DYellow, 128));
@@ -476,7 +501,7 @@
 	mbuf_reusable_ensure_size(&mbuf, h, w);
 	oevent_list_init(&events);
 
-	threadcreate(orcathread, a[3].c, mainstacksize);
+	proccreate(orcathread, a[3].c, mainstacksize);
 	shiftdown = 0;
 	altdown = 0;