ref: 0c8b6da38ef0f1ac8d45d99e1b4f334091b9a252
parent: b1dc12bf2d11aa0ab8becc35d185cd586bb4b279
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Thu Feb 25 05:26:52 EST 2016
make it gapless
--- a/zuke.c
+++ b/zuke.c
@@ -256,7 +256,68 @@
threadexits(nil);
}
+static Player *playernext;
+static Player *playercurr;
+
+static int
+playerret(Player *player)
+{
+ return recvul(player->ev) == Everror ? -1 : 0;
+}
+
static void
+stop(Player *player)
+{
+ if(player == nil)
+ return;
+
+ if(player == playernext)
+ playernext = nil;
+ sendul(player->ctl, Cstop);
+}
+
+static void playerthread(void *player_);
+
+static Player *
+newplayer(int pcur, int loadnext)
+{
+ Player *player;
+
+ if(playernext != nil && loadnext){
+ if(pcur == playernext->pcur){
+ player = playernext;
+ playernext = nil;
+ goto done;
+ }
+ stop(playernext);
+ playernext = nil;
+ }
+
+ player = mallocz(sizeof(*player), 1);
+ player->ctl = chancreate(sizeof(ulong), 0);
+ player->ev = chancreate(sizeof(ulong), 0);
+ player->pcur = pcur;
+
+ threadcreate(playerthread, player, mainstacksize);
+ if(playerret(player) < 0)
+ return nil;
+
+done:
+ if(pcur < plnum-1 && playernext == nil && loadnext)
+ playernext = newplayer(pcur+1, 0);
+
+ return player;
+}
+
+static int
+start(Player *player)
+{
+ if(player != nil)
+ sendul(player->ctl, Cstart);
+ return -1;
+}
+
+static void
playerthread(void *player_)
{
char *buf;
@@ -361,12 +422,9 @@
break;
}
if(trycoverload && byteswritten >= Bps){
- freeimage(cover);
- cover = nil;
player->img = chancreate(sizeof(Image*), 0);
proccreate(coverload, player, 4096);
trycoverload = 0;
- redraw(screen, 0);
}
}
byteswritten += n;
@@ -377,8 +435,9 @@
if(n == 0){ /* seeking backwards or end of the song */
close(p[1]);
if(c != Cbackward){
- bytesfrom = 0;
- player->pcur++;
+ playercurr = newplayer((player->pcur+1) % plnum, 1);
+ start(playercurr);
+ goto stop;
}
goto restart;
}
@@ -398,65 +457,7 @@
threadexits(nil);
}
-static int
-playerret(Player *player)
-{
- return recvul(player->ev) == Everror ? -1 : 0;
-}
-
-static Player *playernext;
-
static void
-stop(Player *player)
-{
- if(player == nil)
- return;
-
- if(player == playernext)
- playernext = nil;
- sendul(player->ctl, Cstop);
-}
-
-static Player *
-newplayer(int pcur, int loadnext)
-{
- Player *player;
-
- if(playernext != nil && loadnext){
- if(pcur == playernext->pcur){
- player = playernext;
- playernext = nil;
- goto done;
- }
- stop(playernext);
- playernext = nil;
- }
-
- player = mallocz(sizeof(*player), 1);
- player->ctl = chancreate(sizeof(ulong), 0);
- player->ev = chancreate(sizeof(ulong), 0);
- player->pcur = pcur;
-
- threadcreate(playerthread, player, mainstacksize);
- if(playerret(player) < 0)
- return nil;
-
-done:
- if(pcur < plnum-1 && playernext == nil && loadnext)
- playernext = newplayer(pcur+1, 0);
-
- return player;
-}
-
-static int
-start(Player *player)
-{
- if(player != nil)
- sendul(player->ctl, Cstart);
- return -1;
-}
-
-static void
toggle(Player *player)
{
if(player != nil)
@@ -611,7 +612,6 @@
threadmain(int argc, char **argv)
{
char tmp[256];
- Player *player;
Point lastclick;
Rune key;
Mouse m;
@@ -660,7 +660,6 @@
srand(time(0));
pcurplaying = -1;
scrolling = oldscroll = usingscrollbar = 0;
- player = nil;
fmtinstall('P', positionfmt);
threadsetname("zuke");
@@ -702,9 +701,9 @@
}else{
pcur = scroll + (m.xy.y - screen->r.min.y)/f->height;
if(m.buttons == 4){
- stop(player);
- player = newplayer(pcur, 1);
- start(player);
+ stop(playercurr);
+ playercurr = newplayer(pcur, 1);
+ start(playercurr);
}
}
break;
@@ -714,10 +713,10 @@
case 2:
switch(key){
case Kleft:
- backward(player);
+ backward(playercurr);
break;
case Kright:
- forward(player);
+ forward(playercurr);
break;
case Kup:
pcur--;
@@ -738,44 +737,44 @@
pcur = 0;
break;
case 10:
- stop(player);
- player = newplayer(pcur, 1);
- start(player);
+ stop(playercurr);
+ playercurr = newplayer(pcur, 1);
+ start(playercurr);
break;
case 'q': case Kdel:
- stop(player);
+ stop(playercurr);
goto end;
case 'o':
pcur = pcurplaying;
break;
case '>':
- if(player == nil)
+ if(playercurr == nil)
break;
pcur = pcurplaying;
if(++pcur >= plnum)
pcur = 0;
- stop(player);
- player = newplayer(pcur, 1);
- start(player);
+ stop(playercurr);
+ playercurr = newplayer(pcur, 1);
+ start(playercurr);
break;
case '<':
- if(player == nil)
+ if(playercurr == nil)
break;
pcur = pcurplaying;
if(--pcur < 0)
pcur = plnum-1;
- stop(player);
- player = newplayer(pcur, 1);
- start(player);
+ stop(playercurr);
+ playercurr = newplayer(pcur, 1);
+ start(playercurr);
break;
case 's':
- stop(player);
- player = nil;
+ stop(playercurr);
+ playercurr = nil;
pcurplaying = -1;
redraw(screen, 1);
break;
case 'p':
- toggle(player);
+ toggle(playercurr);
break;
case '/': case '?': case 'n': case 'N':
search(key);