shithub: zuke

Download patch

ref: 782faaf96e6a6ff865a836c3859f8388210a6dd8
parent: 22a548c0ca2d9531706c7aa3c8b9fd980bc1d3e4
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Mon Feb 22 10:06:37 EST 2016

seeking with left/right key; fix current track highlighting; disable cover images for now

--- a/zuke.c
+++ b/zuke.c
@@ -14,8 +14,12 @@
 	Cstart = 1,
 	Cstop,
 	Ctoggle,
+	Cforward,
+	Cbackward,
 
 	Relbufsz = 32768,
+
+	Seekbytes = 44100*2*2 * 10, /* 10 seconds */
 };
 
 static Meta *pl;
@@ -75,22 +79,13 @@
 	for(i = scroll; i < plnum; i++){
 		if(p.y > screen->r.max.y)
 			break;
-		if(pcurplaying == i){
-			Point right, left;
-			left.y = right.y = p.y - 1;
-			left.x = p.x;
-			right.x = screen->r.max.x;
-			line(screen, left, right, 0, 0, 0, colb, sp);
-			left.y = right.y = p.y + f->height;
-			line(screen, left, right, 0, 0, 0, colb, sp);
-		}
 
 		if(pcur == i){
-			col = cola;
 			sel = screen->r;
 			sel.min.y = p.y;
 			sel.max.y = p.y + f->height;
 			draw(screen, sel, colb, nil, ZP);
+			col = cola;
 		}else{
 			col = colb;
 		}
@@ -115,6 +110,16 @@
 
 		replclipr(screen, 0, r);
 
+		if(pcurplaying == i){
+			Point right, left;
+			left.y = right.y = p.y - 1;
+			left.x = screen->r.min.x;
+			right.x = screen->r.max.x;
+			line(screen, left, right, 0, 0, 0, colb, sp);
+			left.y = right.y = p.y + f->height;
+			line(screen, left, right, 0, 0, 0, colb, sp);
+		}
+
 		p.y += f->height;
 	}
 
@@ -145,7 +150,7 @@
 	m = m_;
 	freeimage(cover);
 	cover = nil;
-	redraw(screen, 0);
+	redraw(screen, 1);
 	if(m->imagefmt == nil || m->imagereader != 0)
 		return;
 	if(strcmp(m->imagefmt, "image/png") == 0)
@@ -190,7 +195,7 @@
 
 	if(pid > 0 && pid2 > 0){
 		cover = readimage(display, p2[1], 0);
-		redraw(screen, 0);
+		redraw(screen, 1);
 	}
 	postnote(PNGROUP, pid, "kill");
 	postnote(PNGROUP, pid2, "kill");
@@ -206,12 +211,15 @@
 	Ioproc *io;
 	ulong c;
 	int p[2], fd, pid, n, noinit;
+	u64int bytesfrom;
 
 	player = player_;
 	noinit = 0;
+	bytesfrom = 0;
+	c = 0;
 	buf = nil;
 
-next:
+restart:
 	pipe(p);
 	if((pid = rfork(RFPROC|RFFDG|RFREND|RFNOTEG)) == 0){
 		dup(p[0], 1);
@@ -238,13 +246,17 @@
 			return;
 		buf = malloc(Relbufsz);
 		byteswritten = 0;
+		bytesfrom = 0;
+		c = 0;
+		noinit = 1;
 	}
 
 	pcurplaying = player->pcur;
-	redraw(screen, 0);
-	threadcreate(coverload, &pl[pcurplaying], 4096);
+	redraw(screen, 1);
+	//proccreate(coverload, &pl[pcurplaying], 4096);
 
 	io = ioproc();
+	byteswritten = 0;
 	while((n = ioread(io, p[1], buf, Relbufsz)) > 0){
 		c = nbrecvul(player->ctl);
 		if(c == Cstop)
@@ -253,21 +265,38 @@
 			c = recvul(player->ctl);
 			if(c == Cstop)
 				goto stop;
-		}
-		if(iowrite(io, audio, buf, n) != n)
+		}else if(c == Cforward){
+			bytesfrom = byteswritten + Seekbytes;
+		}else if(c == Cbackward){ /* to seek backwards we need to restart playback */
+			bytesfrom = byteswritten >= Seekbytes ? byteswritten - Seekbytes : 0;
+			n = 0; /* not an error */
 			break;
+		}
+
+		c = 0;
+		if(bytesfrom <= byteswritten){
+			if(iowrite(io, audio, buf, n) != n){
+				fprint(2, "failed to write %d bytes: %r\n", n);
+				break;
+			}
+		}
 		byteswritten += n;
 	}
 	closeioproc(io);
+	io = nil;
 
-	if(n == 0){ /* end of the song, need to skip to the next one */
+	if(n == 0){ /* seeking backwards or end of the song */
 		close(p[1]);
-		player->pcur++;
-		noinit = 1;
-		goto next;
+		postnote(PNGROUP, pid, "kill");
+		if(c != Cbackward){
+			bytesfrom = 0;
+			player->pcur++;
+		}
+		goto restart;
 	}
 
 stop:
+	closeioproc(io);
 	close(p[1]);
 	postnote(PNGROUP, pid, "kill");
 	free(buf);
@@ -313,6 +342,20 @@
 }
 
 static void
+backward(Player *player)
+{
+	if(player != nil)
+		sendul(player->ctl, Cbackward);
+}
+
+static void
+forward(Player *player)
+{
+	if(player != nil)
+		sendul(player->ctl, Cforward);
+}
+
+static void
 readplist(void)
 {
 	Meta *m;
@@ -520,6 +563,12 @@
 			break;
 		case 2:
 			switch(key){
+			case Kleft:
+				backward(player);
+				break;
+			case Kright:
+				forward(player);
+				break;
 			case Kup:
 				pcur--;
 				break;