shithub: zuke

Download patch

ref: 9f63a97f4e9d7768ac3b0923d50f43d2d62e2874
parent: 3379ca51d719bf638edcd1df66a112e62f268a17
author: Sigrid Haflínudóttir <[email protected]>
date: Thu Jul 30 04:58:20 EDT 2020

simplify seeking logic

--- a/zuke.c
+++ b/zuke.c
@@ -15,10 +15,7 @@
 	Cstart = 1,
 	Cstop,
 	Ctoggle,
-	Cforward,
-	Cbackward,
-	Cforwardfast,
-	Cbackwardfast,
+	Cseekrel,
 
 	Everror = 1,
 	Evready,
@@ -38,6 +35,7 @@
 	Channel *ctl;
 	Channel *ev;
 	Channel *img;
+	vlong seekbytes;
 	int pcur;
 };
 
@@ -433,7 +431,7 @@
 	ulong c;
 	int p[2], fd, pid, noinit, trycoverload;
 	long n, r;
-	u64int bytesfrom, bf;
+	vlong bytesfrom, bf;
 	Meta *cur;
 
 	threadsetname("player");
@@ -496,7 +494,7 @@
 	}
 
 	pcurplaying = player->pcur;
-	if(c != Cbackward && c != Cbackwardfast)
+	if(c != Cseekrel || player->seekbytes >= 0)
 		redraw(1);
 
 	while(1){
@@ -522,18 +520,12 @@
 			if(c == Ctoggle){
 				if(recv(player->ctl, &c) < 0 || c == Cstop)
 					goto stop;
-			}else if(c == Cforward){
-				bytesfrom = bf + Seekbytes;
-			}else if(c == Cforwardfast){
-				bytesfrom = bf + Seekbytesfast;
-			}else if(c == Cbackward){ /* to seek backwards we need to restart playback */
-				bytesfrom = bf >= Seekbytes ? bf - Seekbytes : 0;
-				n = 0; /* not an error */
-				break;
-			}else if(c == Cbackwardfast){
-				bytesfrom = bf >= Seekbytesfast ? bf - Seekbytesfast : 0;
-				n = 0; /* not an error */
-				break;
+			}else if(c == Cseekrel){
+				bytesfrom = MAX(0, bf + player->seekbytes);
+				if(player->seekbytes < 0){
+					n = 0; /* not an error */
+					break;
+				}
 			}else{ /* Cstop */
 				goto stop;
 			}
@@ -558,7 +550,7 @@
 	if(n < 1){ /* seeking backwards or end of the song */
 		close(p[1]);
 		p[1] = -1;
-		if(c != Cbackward && c != Cbackwardfast){
+		if(c != Cseekrel || player->seekbytes >= 0){
 next:
 			playercurr = nil;
 			playercurr = newplayer((player->pcur+1) % plnum, 1);
@@ -596,34 +588,15 @@
 }
 
 static void
-backward(Player *player)
+seekrel(Player *player, vlong off)
 {
-	if(player != nil)
-		sendul(player->ctl, Cbackward);
+	if(player != nil){
+		player->seekbytes = off;
+		sendul(player->ctl, Cseekrel);
+	}
 }
 
 static void
-forward(Player *player)
-{
-	if(player != nil)
-		sendul(player->ctl, Cforward);
-}
-
-static void
-backwardfast(Player *player)
-{
-	if(player != nil)
-		sendul(player->ctl, Cbackwardfast);
-}
-
-static void
-forwardfast(Player *player)
-{
-	if(player != nil)
-		sendul(player->ctl, Cforwardfast);
-}
-
-static void
 readplist(void)
 {
 	Meta *m;
@@ -997,16 +970,16 @@
 		case 2:
 			switch(key){
 			case Kleft:
-				backward(playercurr);
+				seekrel(playercurr, -(vlong)Seekbytes);
 				break;
 			case Kright:
-				forward(playercurr);
+				seekrel(playercurr, Seekbytes);
 				break;
 			case ',':
-				backwardfast(playercurr);
+				seekrel(playercurr, -(vlong)Seekbytesfast);
 				break;
 			case '.':
-				forwardfast(playercurr);
+				seekrel(playercurr, Seekbytesfast);
 				break;
 			case Kup:
 				pcur--;