commit 576bd14b53828465140c54268f90ba7a7aa4110e
parent ee2d4c384161b6914d43ddfbb7a1a0996d8e1648
Author: sin <sin@2f30.org>
Date: Fri, 26 Dec 2014 23:41:09 +0000
Fix play semantics
Diffstat:
M | cmd.c | | | 61 | +++++++++++++++++++++++++++++++++++++++---------------------- |
M | playlist.c | | | 3 | +++ |
M | sad.h | | | 1 | + |
3 files changed, 43 insertions(+), 22 deletions(-)
diff --git a/cmd.c b/cmd.c
@@ -51,12 +51,15 @@ cmdnext(int fd, char *arg)
s = getcursong();
if (!s) {
- dprintf(fd, "ERR no song active\n");
+ dprintf(fd, "ERR playlist is empty\n");
return;
}
- s->decoder->close();
- s->state = NONE;
+ if (s->state != NONE) {
+ s->decoder->close();
+ s->state = NONE;
+ }
+
next = getnextsong();
next->state = PREPARE;
putcursong(next);
@@ -82,7 +85,7 @@ cmdpause(int fd, char *arg)
s = getcursong();
if (!s) {
- dprintf(fd, "ERR no song is active\n");
+ dprintf(fd, "ERR playlist is empty\n");
return;
}
@@ -95,6 +98,9 @@ cmdpause(int fd, char *arg)
if (pause == 0)
s->state = PLAYING;
break;
+ case NONE:
+ dprintf(fd, "ERR no song is active\n");
+ return;
}
printf("Song %s with id %d is %s\n",
s->path, s->id, s->state == PAUSED ? "paused" : "playing");
@@ -107,24 +113,28 @@ cmdplay(int fd, char *arg)
Song *s, *cur;
int id;
- if (!arg[0]) {
- dprintf(fd, "ERR expected song id\n");
- return;
- }
-
- id = atoi(arg);
- s = findsongid(id);
- if (!s) {
- dprintf(fd, "ERR invalid song id\n");
+ cur = getcursong();
+ if (!cur) {
+ dprintf(fd, "ERR playlist is empty\n");
return;
}
- cur = getcursong();
- if (cur) {
+ if (cur->state != NONE) {
cur->decoder->close();
cur->state = NONE;
}
+ if (arg[0]) {
+ id = atoi(arg);
+ s = findsongid(id);
+ if (!s) {
+ dprintf(fd, "ERR invalid song id\n");
+ return;
+ }
+ } else {
+ s = cur;
+ }
+
s->state = PREPARE;
putcursong(s);
printf("Song %s with %d playing\n",
@@ -143,12 +153,15 @@ cmdprev(int fd, char *arg)
s = getcursong();
if (!s) {
- dprintf(fd, "ERR no song active\n");
+ dprintf(fd, "ERR playlist is empty\n");
return;
}
- s->decoder->close();
- s->state = NONE;
+ if (s->state != NONE) {
+ s->decoder->close();
+ s->state = NONE;
+ }
+
prev = getprevsong();
prev->state = PREPARE;
putcursong(prev);
@@ -167,11 +180,15 @@ cmdstop(int fd, char *arg)
s = getcursong();
if (!s) {
- dprintf(fd, "ERR no song is active\n");
+ dprintf(fd, "ERR playlist is empty\n");
return;
}
- s->decoder->close();
- s->state = NONE;
+
+ if (s->state != NONE) {
+ s->decoder->close();
+ s->state = NONE;
+ }
+
dprintf(fd, "OK\n");
}
@@ -209,7 +226,7 @@ cmdclear(int fd, char *arg)
}
s = getcursong();
- if (s) {
+ if (s && s->state != NONE) {
s->decoder->close();
s->state = NONE;
}
diff --git a/playlist.c b/playlist.c
@@ -39,6 +39,8 @@ addplaylist(const char *path)
s->id = rollingid++;
s->state = 0;
s->decoder = d;
+ if (!s->id)
+ playlist.cursong = s;
playlist.nsongs++;
return s;
}
@@ -175,4 +177,5 @@ clearplaylist(void)
{
playlist.nsongs = 0;
rollingid = 0;
+ playlist.cursong = NULL;
}
diff --git a/sad.h b/sad.h
@@ -42,6 +42,7 @@ typedef struct {
typedef struct {
Song **songs;
Song *cursong;
+ Song *nextsong;
size_t nsongs;
size_t maxsongs;
} Playlist;