sad

simple audio daemon
git clone git://git.2f30.org/sad
Log | Files | Refs | LICENSE

commit 229ea70bcecc63cdd6c1ed1867d082ce02abcb01
parent 9629bb7fcfb64d9d523bd135f5b850c967c0be31
Author: sin <sin@2f30.org>
Date:   Thu, 25 Dec 2014 17:44:59 +0000

Implement prev command

Diffstat:
Mcmd.c | 24++++++++++++++++++++++++
Mplaylist.c | 21+++++++++++++++++++++
Msad.h | 1+
3 files changed, 46 insertions(+), 0 deletions(-)

diff --git a/cmd.c b/cmd.c @@ -134,6 +134,30 @@ cmdplay(int fd, int argc, char **argv) static void cmdprev(int fd, int argc, char **argv) { + Song *s, *prev; + + if (argc != 1) { + dprintf(fd, "ERR \"usage: prev\"\n"); + return; + } + + s = getcursong(); + if (!s) { + dprintf(fd, "ERR \"no song active\"\n"); + return; + } + + prev = getprevsong(s); + if (!prev) { + dprintf(fd, "ERR \"invalid song id\"\n"); + return; + } + + decoder->close(); + s->state = NONE; + prev->state = PREPARE; + putcursong(prev); + dprintf(fd, "OK\n"); } static void diff --git a/playlist.c b/playlist.c @@ -79,6 +79,27 @@ getnextsong(Song *cur) } Song * +getprevsong(Song *cur) +{ + Song *s; + int i; + + for (i = 0; i < playlist.nsongs; i++) { + s = &playlist.songs[i]; + if (s->id == cur->id) { + break; + } + } + if (i == playlist.nsongs) + return NULL; + if (i == 0) + s = &playlist.songs[playlist.nsongs - 1]; + else + s = &playlist.songs[i - 1]; + return s; +} + +Song * getcursong(void) { return playlist.cursong; diff --git a/sad.h b/sad.h @@ -58,6 +58,7 @@ Song *addplaylist(const char *); Song *findsong(const char *); Song *findsongid(int); Song *getnextsong(Song *); +Song *getprevsong(Song *); Song *getcursong(void); void putcursong(Song *); void dumpplaylist(int);