commit ea282270f29883d8afe6a5544791f83c4d2d6bbb
parent 6191ba9fb545bdeacc2a2720a53268625677ed1f
Author: sin <sin@2f30.org>
Date: Sun, 28 Dec 2014 12:03:55 +0000
Add enable/disable output commands
Diffstat:
M | cmd.c | | | 69 | ++++++++++++++++++++++++++++++++++++++++++++++++++------------------- |
M | mp3.c | | | 1 | - |
M | output.c | | | 103 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------- |
M | sad.h | | | 3 | ++- |
M | vorbis.c | | | 2 | -- |
M | wav.c | | | 2 | -- |
6 files changed, 127 insertions(+), 53 deletions(-)
diff --git a/cmd.c b/cmd.c
@@ -296,26 +296,57 @@ cmdversion(int fd, char *arg)
dprintf(fd, "version 0.0\nOK\n");
}
+static void
+cmdenable(int fd, char *arg)
+{
+ if (!arg[0]) {
+ dprintf(fd, "ERR expected output name\n");
+ return;
+ }
+ if (enableoutput(arg) < 0) {
+ dprintf(fd, "ERR failed to enable output %s\n", arg);
+ return;
+ }
+ dprintf(fd, "OK\n");
+}
+
+
+static void
+cmddisable(int fd, char *arg)
+{
+ if (!arg[0]) {
+ dprintf(fd, "ERR expected output name\n");
+ return;
+ }
+ if (disableoutput(arg) < 0) {
+ dprintf(fd, "ERR failed to disable output %s\n", arg);
+ return;
+ }
+ dprintf(fd, "OK\n");
+}
+
static Cmd cmds[] = {
- { "status", cmdstatus },
- { "volume", cmdvolume },
- { "next", cmdnext },
- { "pause", cmdpause },
- { "play", cmdplay },
- { "prev", cmdprev },
- { "stop", cmdstop },
- { "add", cmdadd },
- { "clear", cmdclear },
- { "empty", cmdempty },
- { "delete", cmddelete },
- { "playlist", cmdplaylist },
- { "library", cmdlibrary },
- { "learn", cmdlearn },
- { "close", cmdclose },
- { "kill", cmdkill },
- { "ping", cmdping },
- { "search", cmdsearch },
- { "version", cmdversion }
+ { "status", cmdstatus },
+ { "volume", cmdvolume },
+ { "next", cmdnext },
+ { "pause", cmdpause },
+ { "play", cmdplay },
+ { "prev", cmdprev },
+ { "stop", cmdstop },
+ { "add", cmdadd },
+ { "clear", cmdclear },
+ { "empty", cmdempty },
+ { "delete", cmddelete },
+ { "playlist", cmdplaylist },
+ { "library", cmdlibrary },
+ { "learn", cmdlearn },
+ { "close", cmdclose },
+ { "kill", cmdkill },
+ { "ping", cmdping },
+ { "search", cmdsearch },
+ { "version", cmdversion },
+ { "enable", cmdenable },
+ { "disable", cmddisable },
};
/* shamelessly taken from isakmpd ui.c */
diff --git a/mp3.c b/mp3.c
@@ -44,7 +44,6 @@ mp3open(const char *name)
goto err0;
}
- setinputfmt(mpg123_encsize(encoding) * 8, rate, channels);
return 0;
err0:
mpg123_close(hdl);
diff --git a/output.c b/output.c
@@ -14,15 +14,12 @@ typedef struct {
long rate;
int channels;
int enabled;
+ int active;
Output *output;
} Outputdesc;
#include "config.h"
-static int inbits;
-static long inrate;
-static int inchannels;
-
int
openoutput(const char *name)
{
@@ -31,17 +28,19 @@ openoutput(const char *name)
for (i = 0; i < LEN(Outputdescs); i++) {
desc = &Outputdescs[i];
- if (!desc->enabled)
- continue;
if (strcmp(desc->name, name))
continue;
+ if (desc->active)
+ return 0;
if (desc->output->open(desc->bits,
desc->rate,
desc->channels) < 0) {
- printf("Disabling %s output\n",
- desc->name);
- desc->enabled = 0;
+ desc->active = 0;
return -1;
+ } else {
+ printf("Opened %s output\n", desc->name);
+ desc->active = 1;
+ return 0;
}
}
return -1;
@@ -50,14 +49,15 @@ openoutput(const char *name)
int
openoutputs(void)
{
+ Outputdesc *desc;
int i, r = 0;
for (i = 0; i < LEN(Outputdescs); i++) {
- if (openoutput(Outputdescs[i].name) < 0)
+ desc = &Outputdescs[i];
+ if (!desc->enabled)
+ continue;
+ if (openoutput(desc->name) < 0)
r = -1;
- else
- printf("Opened %s output\n",
- Outputdescs[i].name);
}
return r;
}
@@ -70,11 +70,18 @@ closeoutput(const char *name)
for (i = 0; i < LEN(Outputdescs); i++) {
desc = &Outputdescs[i];
- if (!desc->enabled)
- continue;
if (strcmp(desc->name, name))
continue;
- return desc->output->close();
+ if (!desc->active)
+ return 0;
+ if (desc->output->close() < 0) {
+ desc->active = 1;
+ return -1;
+ } else {
+ printf("Closed %s output\n", desc->name);
+ desc->active = 0;
+ return 0;
+ }
}
return -1;
}
@@ -82,14 +89,16 @@ closeoutput(const char *name)
int
closeoutputs(void)
{
+ Outputdesc *desc;
int i, r = 0;
- for (i = 0; i < LEN(Outputdescs); i++)
- if (closeoutput(Outputdescs[i].name) < 0)
+ for (i = 0; i < LEN(Outputdescs); i++) {
+ desc = &Outputdescs[i];
+ if (!desc->enabled)
+ continue;
+ if (closeoutput(desc->name) < 0)
r = -1;
- else
- printf("Closed %s output\n",
- Outputdescs[i].name);
+ }
return r;
}
@@ -101,7 +110,7 @@ playoutput(void *buf, size_t nbytes)
for (i = 0; i < LEN(Outputdescs); i++) {
desc = &Outputdescs[i];
- if (!desc->enabled)
+ if (!desc->active)
continue;
if (desc->output->play(buf, nbytes) < 0)
r = -1;
@@ -117,7 +126,7 @@ setvol(int vol)
for (i = 0; i < LEN(Outputdescs); i++) {
desc = &Outputdescs[i];
- if (!desc->enabled)
+ if (!desc->active)
continue;
if (desc->output->vol(vol) < 0)
r = -1;
@@ -125,10 +134,48 @@ setvol(int vol)
return r;
}
-void
-setinputfmt(int bits, long rate, int channels)
+int
+enableoutput(const char *name)
+{
+ Outputdesc *desc;
+ int i, r;
+
+ for (i = 0; i < LEN(Outputdescs); i++) {
+ desc = &Outputdescs[i];
+ if (strcmp(desc->name, name))
+ continue;
+ if (desc->active)
+ return -1;
+ if (openoutput(desc->name) < 0) {
+ desc->enabled = 0;
+ return -1;
+ } else {
+ desc->enabled = 1;
+ return 0;
+ }
+ }
+ return -1;
+}
+
+int
+disableoutput(const char *name)
{
- inbits = bits;
- inrate = rate;
- inchannels = channels;
+ Outputdesc *desc;
+ int i;
+
+ for (i = 0; i < LEN(Outputdescs); i++) {
+ desc = &Outputdescs[i];
+ if (strcmp(desc->name, name))
+ continue;
+ if (!desc->active)
+ return -1;
+ if (closeoutput(desc->name) < 0) {
+ desc->enabled = 1;
+ return -1;
+ } else {
+ desc->enabled = 0;
+ return 0;
+ }
+ }
+ return -1;
}
diff --git a/sad.h b/sad.h
@@ -108,6 +108,7 @@ int openoutput(const char *);
int openoutputs(void);
int closeoutput(const char *);
int closeoutputs(void);
+int enableoutput(const char *);
+int disableoutput(const char *);
int playoutput(void *, size_t);
int setvol(int);
-void setinputfmt(int, long, int);
diff --git a/vorbis.c b/vorbis.c
@@ -34,8 +34,6 @@ vorbisopen(const char *name)
goto err0;
}
- setinputfmt(16, vi->rate, vi->channels);
-
return 0;
err0:
diff --git a/wav.c b/wav.c
@@ -51,8 +51,6 @@ wavopen(const char *name)
goto err0;
}
- setinputfmt(bits, sfinfo.samplerate, sfinfo.channels);
-
return 0;
err0:
sf_close(sf);