shithub: zuke

Download patch

ref: d24b93d9572b1272e0649468789458358656c7d0
parent: 713f33ceb0403944112457ad0173032da9e71f39
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Tue Jun 9 08:24:03 EDT 2020

free /dev/audio when not needed

--- a/zuke.c
+++ b/zuke.c
@@ -49,7 +49,6 @@
 static int volume;
 static Player *playernext;
 static Player *playercurr;
-static int audio;
 static u64int byteswritten;
 static int pcur, pcurplaying;
 static int scroll, scrollsz;
@@ -411,6 +410,15 @@
 	return -1;
 }
 
+static int
+openaudio(int audio)
+{
+	while(audio < 0 && (audio = open("/dev/audio", OWRITE)) < 0)
+		sleep(1000);
+
+	return audio;
+}
+
 static void
 playerthread(void *player_)
 {
@@ -421,6 +429,7 @@
 	ulong c;
 	int p[2], fd, pid, n, got, noinit, trycoverload;
 	u64int bytesfrom, bf;
+	int audio;
 
 	threadsetname("player");
 	player = player_;
@@ -431,6 +440,7 @@
 	trycoverload = 1;
 	io = nil;
 	pid = -1;
+	audio = -1;
 
 restart:
 	if((fd = open(getmeta(player->pcur)->path, OREAD)) < 0){
@@ -470,6 +480,7 @@
 			c = recvul(player->ctl);
 		if(c != Cstart)
 			goto freeplayer;
+		audio = openaudio(audio);
 		iowrite(io, audio, buf, got);
 		byteswritten = got;
 		bytesfrom = 0;
@@ -501,9 +512,12 @@
 		if(c == Cstop || c == -1)
 			goto stop;
 		if(c == Ctoggle){
+			close(audio);
+			audio = -1;
 			c = recvul(player->ctl);
 			if(c == Cstop)
 				goto stop;
+			audio = openaudio(audio);
 		}else if(c == Cforward){
 			bytesfrom = bf + Seekbytes;
 		}else if(c == Cforwardfast){
@@ -522,6 +536,7 @@
 		if(bytesfrom <= byteswritten){
 			if(bytesfrom == byteswritten)
 				bytesfrom = 0;
+			audio = openaudio(audio);
 			if(iowrite(io, audio, buf, n) != n){
 				fprint(2, "failed to write %d bytes: %r\n", n);
 				break;
@@ -549,6 +564,8 @@
 	}
 
 stop:
+	if(audio >= 0)
+		close(audio);
 	if(player->img != nil)
 		freeimage(recvp(player->img));
 freeplayer:
@@ -860,10 +877,6 @@
 		usage();
 		break;
 	}ARGEND;
-
-	audio = open("/dev/audio", OWRITE);
-	if(audio < 0)
-		sysfatal("audio: %r");
 
 	readplist();
 	if(plnum < 1){