shithub: pplay

Download patch

ref: 85be368ed57da2a6f3761f158d77222b27d70420
parent: 66f0c6706650146103c229e1457d480cf5560cea
author: qwx <qwx@sciops.net>
date: Mon Oct 24 02:07:41 EDT 2022

wrap play/pause toggle

--- a/pplay.c
+++ b/pplay.c
@@ -15,7 +15,6 @@
 
 static Keyboardctl *kc;
 static Mousectl *mc;
-static int pause;
 static int cat;
 static int afd = -1;
 
@@ -57,6 +56,23 @@
 	}
 }
 
+static void
+toggleplay(void)
+{
+	static int play;
+
+	if(play ^= 1){
+		if((afd = cat ? 1 : open("/dev/audio", OWRITE)) < 0)
+			sysfatal("open: %r");
+		if(threadcreate(athread, nil, mainstacksize) < 0)
+			sysfatal("threadcreate: %r");
+	}else{
+		if(!cat)
+			close(afd);
+		afd = -1;
+	}
+}
+
 static char *
 prompt(void)
 {
@@ -145,10 +161,7 @@
 		{kc->c, &r, CHANRCV},
 		{nil, nil, CHANEND}
 	};
-	if((afd = cat ? 1 : open("/dev/audio", OWRITE)) < 0)
-		sysfatal("open: %r");
-	if(threadcreate(athread, nil, mainstacksize) < 0)
-		sysfatal("threadcreate: %r");
+	toggleplay();
 	for(;;){
 		switch(alt(a)){
 		case 0:
@@ -171,16 +184,7 @@
 			break;
 		case 2:
 			switch(r){
-			case ' ':
-				if(pause ^= 1){
-					if(!cat)
-						close(afd);
-					afd = -1;
-				}else if((afd = cat ? 1 : open("/dev/audio", OWRITE)) < 0)
-					sysfatal("open: %r");
-				else if(threadcreate(athread, nil, mainstacksize) < 0)
-					sysfatal("threadcreate: %r");
-				break;
+			case ' ': toggleplay(); break;
 			case 'b': setpos(loops); break;
 			case 'r': loops = 0; loope = filesz; update(); break;
 			case Kdel: