Recently Written · git

sad

sad (2f30.org simple audio daemon) with a Cygwin/OSS port on branch cygwin

git clone https://github.com/equwal/sad

Log | Files | Refs


commit 6191ba9fb545bdeacc2a2720a53268625677ed1f
sin <sin@2f30.org>
2014-12-28 11:18:33 +0000

Add empty command

 PROTOCOL  |  6 ++++--
 cmd.c     | 15 +++++++++++++++
 library.c |  8 +++++++-
 sad.h     |  2 +-
 4 files changed, 27 insertions(+), 4 deletions(-)
diff --git a/PROTOCOL b/PROTOCOL
index 25be901..bd156f9 100644
--- a/PROTOCOL
+++ b/PROTOCOL
@@ -37,8 +37,10 @@ Add the given song ID from the library to the playlist.  If not ID
 is provided, all songs in the library are added.
 
 clear
-Clear playlist and stop playback.  The next file added will have a song ID
-equal to 0.
+Clear playlist and stop playback.
+
+empty
+Empty the library, clear playlist and stop playback.
 
 remove [ID]
 Delete the song with the given ID or the currently selected song from
diff --git a/cmd.c b/cmd.c
index e72dc3c..15d6e26 100644
--- a/cmd.c
+++ b/cmd.c
@@ -194,6 +194,20 @@ cmdclear(int fd, char *arg)
 	dprintf(fd, "OK\n");
 }
 
+static void
+cmdempty(int fd, char *arg)
+{
+	if (arg[0]) {
+		dprintf(fd, "ERR unexpected argument\n");
+		return;
+	}
+
+	stopsong(getcursong());
+	clearplaylist();
+	emptylibrary();
+	dprintf(fd, "OK\n");
+}
+
 static void
 cmddelete(int fd, char *arg)
 {
@@ -292,6 +306,7 @@ static Cmd cmds[] = {
 	{ "stop",     cmdstop     },
 	{ "add",      cmdadd      },
 	{ "clear",    cmdclear    },
+	{ "empty",    cmdempty    },
 	{ "delete",   cmddelete   },
 	{ "playlist", cmdplaylist },
 	{ "library",  cmdlibrary  },
diff --git a/library.c b/library.c
index 7775f2e..dca2a2a 100644
--- a/library.c
+++ b/library.c
@@ -103,8 +103,14 @@ searchlibrary(int fd, const char *search)
 }
 
 void
-clearlibrary(void)
+emptylibrary(void)
 {
+	int i;
+
+	for (i = 0; i < library.nsongs; i++) {
+		free(library.songs[i]);
+		library.songs[i] = NULL;
+	}
 	library.nsongs = 0;
 	rollingid = 0;
 }
diff --git a/sad.h b/sad.h
index 4bcd3a6..45172e8 100644
--- a/sad.h
+++ b/sad.h
@@ -78,7 +78,7 @@ Song *addlibrary(const char *);
 Song *findsongid(int);
 void dumplibrary(int);
 int searchlibrary(int, const char *);
-void clearlibrary(void);
+void emptylibrary(void);
 
 /* wav.c */
 extern Decoder wavdecoder;