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 449264217b6dcedde28902c039a01f8b41d5f71d
sin <sin@2f30.org>
2015-01-02 16:12:13 +0000

Remove the concept of library entirely

 Makefile        |   1 -
 PROTOCOL        |  19 ++------
 cmd.c           |  60 +++---------------------
 library.c       | 138 --------------------------------------------------------
 playlist.c      |  89 ++++++++++++++++++++++++++++++++++--
 sad.h           |  19 ++------
 scripts/sad-add |   9 +---
 7 files changed, 99 insertions(+), 236 deletions(-)
diff --git a/Makefile b/Makefile
index d17eaeb..d9d7281 100644
--- a/Makefile
+++ b/Makefile
@@ -12,7 +12,6 @@ OBJ =\
 	cmd.o\
 	decoder.o\
 	fifo.o\
-	library.o\
 	mp3.o\
 	notify.o\
 	output.o\
diff --git a/PROTOCOL b/PROTOCOL
index 43e62e3..20a65ac 100644
--- a/PROTOCOL
+++ b/PROTOCOL
@@ -29,15 +29,12 @@ Play previous song.
 stop
 Stop playback.
 
-add ID
-Add the given song ID from the library to the playlist.
+add path
+Add the given song path to the playlist.
 
 clear
 Clear playlist and stop playback.
 
-empty
-Empty the library, clear playlist and stop playback.
-
 remove [ID]
 Remove the song with the given ID or if ID is not given, the currently
 selected song from the playlist.
@@ -46,20 +43,10 @@ playlist
 Dump the current playlist.  The output has the following form:
 "%d: %s\n", id, filepath
 
-library
-Dump the library.  The output has the following form:
-"%d: %s\n", id, filepath
-
 search regex
-Search for a song matching the given regex in the library.  At the moment it
+Search for a song matching the given regex in the playlist.  At the moment it
 will only consider file names.
 
-learn path
-Add the path to library.
-
-forget ID
-Forget the song with the given ID from the library.
-
 close
 Force the daemon to close the client connection.
 
diff --git a/cmd.c b/cmd.c
index 48a99b5..97f412f 100644
--- a/cmd.c
+++ b/cmd.c
@@ -201,20 +201,13 @@ static void
 cmdadd(int fd, char *arg)
 {
 	const char *errstr;
-	int id;
 
 	if (!arg[0]) {
-		dprintf(fd, "ERR expected song id\n");
+		dprintf(fd, "ERR expected file path\n");
 		return;
 	}
 
-	id = strtonum(arg, 0, INT_MAX, &errstr);
-	if (errstr) {
-		dprintf(fd, "ERR invalid song id\n");
-		return;
-	}
-
-	if (!addplaylist(id)) {
+	if (!addplaylist(arg)) {
 		dprintf(fd, "ERR cannot add song to playlist\n");
 		return;
 	}
@@ -235,20 +228,6 @@ 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
 cmdremove(int fd, char *arg)
 {
@@ -290,31 +269,6 @@ cmdplaylist(int fd, char *arg)
 	dprintf(fd, "OK\n");
 }
 
-static void
-cmdlibrary(int fd, char *arg)
-{
-	if (arg[0]) {
-		dprintf(fd, "ERR unexpected argument\n");
-		return;
-	}
-	dumplibrary(fd);
-	dprintf(fd, "OK\n");
-}
-
-static void
-cmdlearn(int fd, char *arg)
-{
-	if (!arg[0]) {
-		dprintf(fd, "ERR expected argument file path\n");
-		return;
-	}
-	if (!addlibrary(arg)) {
-		dprintf(fd, "ERR failed to add song to library\n");
-		return;
-	}
-	dprintf(fd, "OK\n");
-}
-
 static void
 cmdclose(int fd, char *arg)
 {	if (arg[0]) {
@@ -352,11 +306,12 @@ static void
 cmdsearch(int fd, char *arg)
 {
 	if (!arg[0]) {
-		dprintf(fd, "ERR expectede search string\n");
+		dprintf(fd, "ERR expected search string\n");
 		return;
 	}
-	if (searchlibrary(fd, arg) != -1)
-		dprintf(fd, "OK\n");
+	if (searchplaylist(fd, arg) < 0)
+		dprintf(fd, "ERR failed to search through playlist\n");
+	dprintf(fd, "OK\n");
 }
 
 static void
@@ -425,11 +380,8 @@ static Cmd cmds[] = {
 	{ "stop",       cmdstop       },
 	{ "add",        cmdadd        },
 	{ "clear",      cmdclear      },
-	{ "empty",      cmdempty      },
 	{ "remove",     cmdremove     },
 	{ "playlist",   cmdplaylist   },
-	{ "library",    cmdlibrary    },
-	{ "learn",      cmdlearn      },
 	{ "close",      cmdclose      },
 	{ "kill",       cmdkill       },
 	{ "ping",       cmdping       },
diff --git a/library.c b/library.c
deleted file mode 100644
index a857b5e..0000000
--- a/library.c
+++ /dev/null
@@ -1,138 +0,0 @@
-#include <sys/select.h>
-#include <sys/types.h>
-
-#include <err.h>
-#include <limits.h>
-#include <regex.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "sad.h"
-
-static Library  library;
-static int      rollingid;
-
-Song *
-addlibrary(const char *path)
-{
-	Decoder *d;
-	Song    *s;
-
-	if (access(path, F_OK) < 0)
-		return NULL;
-
-	s = findsong(path);
-	if (s)
-		return s;
-
-	d = matchdecoder(path);
-	if (!d)
-		return NULL;
-
-	if (!library.nsongs || library.nsongs + 1 > library.maxsongs) {
-		library.maxsongs += 4096;
-		library.songs = reallocarray(library.songs, library.maxsongs, sizeof(Song *));
-		if (!library.songs)
-			err(1, "reallocarray");
-	}
-
-	s = malloc(sizeof(*s));
-	if (!s)
-		err(1, "malloc");
-
-	library.songs[library.nsongs] = s;
-	strlcpy(s->path, path, sizeof(s->path));
-	s->id = rollingid++;
-	s->state = NONE;
-	s->decoder = d;
-	library.nsongs++;
-	return s;
-}
-
-Song *
-findsong(const char *path)
-{
-	Song *s;
-	int   i;
-
-	for (i = 0; i < library.nsongs; i++) {
-		s = library.songs[i];
-		if (!strcmp(s->path, path))
-			return s;
-	}
-	return NULL;
-}
-
-Song *
-findsongid(int id)
-{
-	Song *s;
-	int   i;
-
-	for (i = 0; i < library.nsongs; i++) {
-		s = library.songs[i];
-		if (s->id == id)
-			return s;
-	}
-	return NULL;
-}
-
-void
-dumplibrary(int fd)
-{
-	Song *s;
-	int   i;
-
-	for (i = 0; i < library.nsongs; i++) {
-		s = library.songs[i];
-		dprintf(fd, "%d: %s\n", s->id, s->path);
-	}
-}
-
-static int
-wregcomp(int fd, regex_t *preg, const char *regex, int cflags)
-{
-        char errbuf[BUFSIZ] = "";
-        int r;
-
-        if ((r = regcomp(preg, regex, cflags)) == 0)
-                return r;
-
-        regerror(r, preg, errbuf, sizeof(errbuf));
-        dprintf(fd, "ERR invalid regex: %s\n", errbuf);
-        return r;
-}
-
-int
-searchlibrary(int fd, const char *search)
-{
-	Song *s;
-	int   i;
-	regex_t re;
-
-	if (wregcomp(fd, &re, search, REG_EXTENDED | REG_ICASE))
-		return -1; /* invalid regex */
-
-	for (i = 0; i < library.nsongs; i++) {
-		s = library.songs[i];
-		if (!regexec(&re, s->path, 0, NULL, REG_NOSUB))
-			dprintf(fd, "%d: %s\n", s->id, s->path);
-	}
-	regfree(&re);
-	return 0;
-}
-
-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/playlist.c b/playlist.c
index 8f77e3d..a29392b 100644
--- a/playlist.c
+++ b/playlist.c
@@ -3,10 +3,12 @@
 
 #include <err.h>
 #include <limits.h>
+#include <regex.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
+#include <unistd.h>
 
 #include "sad.h"
 
@@ -14,12 +16,16 @@ static Playlist playlist;
 static int      rollingid;
 
 Song *
-addplaylist(int id)
+addplaylist(const char *path)
 {
-	Song *s;
+	Song    *s;
+	Decoder *d;
 
-	s = findsongid(id);
-	if (!s)
+	if (access(path, F_OK) < 0)
+		return NULL;
+
+	d = matchdecoder(path);
+	if (!d)
 		return NULL;
 
 	if (!playlist.nsongs || playlist.nsongs + 1 > playlist.maxsongs) {
@@ -29,7 +35,15 @@ addplaylist(int id)
 			err(1, "reallocarray");
 	}
 
+	s = malloc(sizeof(*s));
+	if (!s)
+		err(1, "malloc");
+
 	playlist.songs[playlist.nsongs] = s;
+	strlcpy(s->path, path, sizeof(s->path));
+	s->id = rollingid++;
+	s->state = NONE;
+	s->decoder = d;
 	if (!playlist.nsongs)
 		playlist.cursong = s;
 	playlist.nsongs++;
@@ -59,6 +73,34 @@ rmplaylist(int id)
 	return 0;
 }
 
+Song *
+findsong(const char *path)
+{
+	Song *s;
+	int   i;
+
+	for (i = 0; i < playlist.nsongs; i++) {
+		s = playlist.songs[i];
+		if (!strcmp(s->path, path))
+			return s;
+	}
+	return NULL;
+}
+
+Song *
+findsongid(int id)
+{
+	Song *s;
+	int   i;
+
+	for (i = 0; i < playlist.nsongs; i++) {
+		s = playlist.songs[i];
+		if (s->id == id)
+			return s;
+	}
+	return NULL;
+}
+
 Song *
 getnextsong(void)
 {
@@ -128,6 +170,12 @@ dumpplaylist(int fd)
 void
 clearplaylist(void)
 {
+	int i;
+
+	for (i = 0; i < playlist.nsongs; i++) {
+		free(playlist.songs[i]);
+		playlist.songs[i] = NULL;
+	}
 	playlist.nsongs = 0;
 	rollingid = 0;
 	playlist.cursong = NULL;
@@ -175,3 +223,36 @@ playlistmode(int mode)
 {
 	playlist.mode = mode;
 }
+
+static int
+wregcomp(int fd, regex_t *preg, const char *regex, int cflags)
+{
+        char errbuf[BUFSIZ] = "";
+        int r;
+
+        if ((r = regcomp(preg, regex, cflags)) == 0)
+                return r;
+
+        regerror(r, preg, errbuf, sizeof(errbuf));
+        dprintf(fd, "ERR invalid regex: %s\n", errbuf);
+        return r;
+}
+
+int
+searchplaylist(int fd, const char *search)
+{
+	Song *s;
+	int   i;
+	regex_t re;
+
+	if (wregcomp(fd, &re, search, REG_EXTENDED | REG_ICASE))
+		return -1; /* invalid regex */
+
+	for (i = 0; i < playlist.nsongs; i++) {
+		s = playlist.songs[i];
+		if (!regexec(&re, s->path, 0, NULL, REG_NOSUB))
+			dprintf(fd, "%d: %s\n", s->id, s->path);
+	}
+	regfree(&re);
+	return 0;
+}
diff --git a/sad.h b/sad.h
index bd5a449..444bf47 100644
--- a/sad.h
+++ b/sad.h
@@ -63,12 +63,6 @@ typedef struct {
 	int    mode;
 } Playlist;
 
-typedef struct {
-	Song **songs;
-	size_t nsongs;
-	size_t maxsongs;
-} Library;
-
 typedef struct {
 	int   event;
 	char *name;
@@ -83,8 +77,10 @@ extern int      fdmax;
 int docmd(int);
 
 /* playlist.c */
-Song *addplaylist(int);
+Song *addplaylist(const char *);
 int rmplaylist(int);
+Song *findsong(const char *);
+Song *findsongid(int);
 Song *getnextsong(void);
 Song *getprevsong(void);
 Song *getcursong(void);
@@ -95,14 +91,7 @@ Song *picknextsong(void);
 void playsong(Song *);
 void stopsong(Song *);
 void playlistmode(int);
-
-/* library.c */
-Song *addlibrary(const char *);
-Song *findsong(const char *);
-Song *findsongid(int);
-void dumplibrary(int);
-int searchlibrary(int, const char *);
-void emptylibrary(void);
+int searchplaylist(int, const char *);
 
 /* wav.c */
 extern Decoder wavdecoder;
diff --git a/scripts/sad-add b/scripts/sad-add
index 8afa147..a0f193d 100755
--- a/scripts/sad-add
+++ b/scripts/sad-add
@@ -13,15 +13,8 @@ while read -r p; do
 		-iname "*.ogg" -or \
 		-iname "*.flac" -or \
 		-iname "*.wav" \) \
-		-exec echo learn {} \; | \
+		-exec printf 'add %s\n' {} \; | \
 		nc -U "$sock"
 done <<!__EOF__
 $paths
 !__EOF__
-
-# add library to playlist.
-echo "library" | nc -U "$sock" | while read -r song; do
-	if test "$song" = "OK"; then continue; fi
-	id=$(printf '%s' "$song" | cut -d ':' -f 1)
-	echo "add" "$id"
-done | nc -U "$sock"