shithub: zuke

Download patch

ref: dc2d4daff4bb9fe493b563cf7092dd54a9525574
parent: 1a53cab8dd1435c1b8db09c2497352c269928431
author: Sigrid Solveig Haflínudóttir <[email protected]>
date: Wed Nov 18 04:48:14 EST 2020

claim /dev/audio only when required

--- a/zuke.c
+++ b/zuke.c
@@ -39,7 +39,7 @@
 int mainstacksize = 32768;
 
 static int debug;
-static int audio;
+static int audio = -1;
 static Meta *pl;
 static int plnum;
 static char *plraw;
@@ -62,6 +62,8 @@
 static Rectangle seekbar;
 static int seekmx, newseekmx;
 static double seekoff; /* ms */
+static QLock audiolock;
+static int audioerr = 0;
 static char *covers[] = {"art", "folder", "cover", "Cover", "scans/CD", "Scans/Front", "Covers/Front"};
 
 static int Scrollwidth;
@@ -78,6 +80,27 @@
 	.item = menu3i,
 };
 
+static void
+audioon(void)
+{
+	qlock(&audiolock);
+	if(audio < 0 && (audio = open("/dev/audio", OWRITE)) < 0 && audioerr == 0){
+		fprint(2, "%r\n");
+		audioerr = 1;
+	}
+	qunlock(&audiolock);
+}
+
+static void
+audiooff(void)
+{
+	qlock(&audiolock);
+	close(audio);
+	audio = -1;
+	audioerr = 0;
+	qunlock(&audiolock);
+}
+
 #pragma varargck type "P" uvlong
 static int
 positionfmt(Fmt *f)
@@ -544,6 +567,7 @@
 			goto freeplayer;
 		if(n < 1)
 			goto next;
+		audioon();
 		boffset = iowrite(io, audio, buf, n);
 		noinit = 1;
 	}
@@ -571,8 +595,10 @@
 			goto stop;
 		}else if(r != 0){
 			if(c == Ctoggle){
+				audiooff();
 				if(recv(player->ctl, &c) < 0 || c == Cstop)
 					goto stop;
+				audioon();
 			}else if(c == Cseekrel){
 				boffset = MAX(0, boffset + player->seek*Bps);
 				n = 0;
@@ -584,8 +610,8 @@
 
 		boffset += n;
 		byteswritten = boffset;
-		if(iowrite(io, audio, buf, n) != n)
-			fprint(2, "player: %r\n");
+		audioon();
+		iowrite(io, audio, buf, n);
 		if(trycoverload){
 			trycoverload = 0;
 			player->img = chancreate(sizeof(Image*), 0);
@@ -935,9 +961,6 @@
 		fprint(2, "empty playlist\n");
 		sysfatal("empty");
 	}
-
-	if((audio = open("/dev/audio", OWRITE)) < 0)
-		sysfatal("audio: %r");
 
 	if(initdraw(nil, nil, "zuke") < 0)
 		sysfatal("initdraw: %r");