~ft/zuke

d24b93d9572b1272e0649468789458358656c7d0 — Sigrid Haflínudóttir 7 months ago 713f33c
free /dev/audio when not needed
1 files changed, 18 insertions(+), 5 deletions(-)

M zuke.c
M zuke.c => zuke.c +18 -5
@@ 49,7 49,6 @@ static int plrawsize;
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 @@ start(Player *player)
	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 @@ playerthread(void *player_)
	ulong c;
	int p[2], fd, pid, n, got, noinit, trycoverload;
	u64int bytesfrom, bf;
	int audio;

	threadsetname("player");
	player = player_;


@@ 431,6 440,7 @@ playerthread(void *player_)
	trycoverload = 1;
	io = nil;
	pid = -1;
	audio = -1;

restart:
	if((fd = open(getmeta(player->pcur)->path, OREAD)) < 0){


@@ 470,6 480,7 @@ restart:
			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 @@ restart:
		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 @@ restart:
		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 @@ restart:
	}

stop:
	if(audio >= 0)
		close(audio);
	if(player->img != nil)
		freeimage(recvp(player->img));
freeplayer:


@@ 861,10 878,6 @@ threadmain(int argc, char **argv)
		break;
	}ARGEND;

	audio = open("/dev/audio", OWRITE);
	if(audio < 0)
		sysfatal("audio: %r");

	readplist();
	if(plnum < 1){
		fprint(2, "empty playlist\n");