shithub: zuke

Download patch

ref: 156a684852e11c64fe713e3834df1aa4eb79bf93
parent: 782faaf96e6a6ff865a836c3859f8388210a6dd8
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Mon Feb 22 13:32:46 EST 2016

zuke: report current position and total duration of the playing track

--- a/zuke.c
+++ b/zuke.c
@@ -19,7 +19,8 @@
 
 	Relbufsz = 32768,
 
-	Seekbytes = 44100*2*2 * 10, /* 10 seconds */
+	Bps = 44100*2*2, /* 44100KHz, stereo, u16 for a sample */
+	Seekbytes = Bps*10, /* 10 seconds */
 };
 
 static Meta *pl;
@@ -46,6 +47,26 @@
 static Mousectl *mctl;
 static Keyboardctl *kctl;
 
+#pragma varargck type "P" int
+static int
+positionfmt(Fmt *f)
+{
+	char *s, tmp[16];
+	u64int sec;
+
+	s = tmp;
+	sec = va_arg(f->args, int);
+	if(sec >= 3600){
+		s = seprint(s, tmp+sizeof(tmp), "%02lld:", sec/3600);
+		sec %= 3600;
+	}
+	s = seprint(s, tmp+sizeof(tmp), "%02lld:", sec/60);
+	sec %= 60;
+	seprint(s, tmp+sizeof(tmp), "%02lld", sec);
+
+	return fmtstrcpy(f, tmp);
+}
+
 static void
 redraw(Image *screen, int new)
 {
@@ -53,6 +74,7 @@
 	Point p, sp;
 	Rectangle sel, r;
 	int i, colwidth;
+	char tmp[32];
 
 	if(new && getwindow(display, Refnone) < 0)
 		sysfatal("getwindow: %r");
@@ -135,6 +157,17 @@
 		draw(screen, r, cover, nil, ZP);
 	}
 
+	if(pcurplaying >= 0){
+		snprint(tmp, sizeof(tmp), "%P/%P", (int)(byteswritten/Bps), pl[pcurplaying].duration/1000);
+		r = screen->r;
+		r.min.x = r.max.x - stringwidth(f, tmp) - 4;
+		r.min.y = r.max.y - f->height - 4;
+		draw(screen, r, display->black, nil, ZP);
+		r.min.x += 2;
+		r.min.y += 2;
+		string(screen, r.min, cola, sp, f, tmp);
+	}
+
 	flushimage(display, 1);
 }
 
@@ -245,19 +278,26 @@
 		if(c != Cstart)
 			return;
 		buf = malloc(Relbufsz);
-		byteswritten = 0;
 		bytesfrom = 0;
 		c = 0;
 		noinit = 1;
 	}
 
+	byteswritten = 0;
 	pcurplaying = player->pcur;
-	redraw(screen, 1);
+	if(c != Cbackward)
+		redraw(screen, 1);
 	//proccreate(coverload, &pl[pcurplaying], 4096);
 
 	io = ioproc();
-	byteswritten = 0;
-	while((n = ioread(io, p[1], buf, Relbufsz)) > 0){
+	while(1){
+		n = Relbufsz;
+		if(bytesfrom > byteswritten && n > bytesfrom-byteswritten)
+			n = bytesfrom-byteswritten;
+		n = ioread(io, p[1], buf, n);
+		if(n < 1)
+			break;
+
 		c = nbrecvul(player->ctl);
 		if(c == Cstop)
 			goto stop;
@@ -281,6 +321,8 @@
 			}
 		}
 		byteswritten += n;
+		if(bytesfrom == byteswritten || (bytesfrom < byteswritten && byteswritten/Bps > (byteswritten-n)/Bps))
+			redraw(screen, 0);
 	}
 	closeioproc(io);
 	io = nil;
@@ -534,6 +576,7 @@
 	srand(time(0));
 	pcurplaying = -1;
 	player = nil;
+	fmtinstall('P', positionfmt);
 	threadsetname("zuke");
 
 	snprint(tmp, sizeof(tmp), "/proc/%d/ctl", getpid());
@@ -621,6 +664,8 @@
 			case 's':
 				stop(player);
 				player = nil;
+				pcurplaying = -1;
+				redraw(screen, 1);
 				break;
 			case 'p':
 				toggle(player);