summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars-Dominik Braun <PromyLOPh@gmail.com>2008-06-15 21:23:37 +0200
committerLars-Dominik Braun <PromyLOPh@gmail.com>2008-06-15 21:23:37 +0200
commitc17911ba268a4a784c57baed1540d08ba8741e04 (patch)
tree8ea344fca6566c1e3fc8e43f21c53aea9f87774b
parent0c4f59a6a48ee5ebcc776e29ca8b49b3269ad7e6 (diff)
downloadpianobar-c17911ba268a4a784c57baed1540d08ba8741e04.tar.gz
pianobar-c17911ba268a4a784c57baed1540d08ba8741e04.tar.bz2
pianobar-c17911ba268a4a784c57baed1540d08ba8741e04.zip
client: Config file added, not yet documented
Some restructuring was necessary too.
-rw-r--r--libpiano/main.c4
-rw-r--r--src/Makefile.am2
-rw-r--r--src/main.c34
-rw-r--r--src/settings.c95
-rw-r--r--src/settings.h39
-rw-r--r--src/terminal.c50
-rw-r--r--src/terminal.h29
7 files changed, 240 insertions, 13 deletions
diff --git a/libpiano/main.c b/libpiano/main.c
index 9754e73..faf5317 100644
--- a/libpiano/main.c
+++ b/libpiano/main.c
@@ -47,10 +47,6 @@ void PianoInit (PianoHandle_t *ph) {
snprintf (ph->routeId, sizeof (ph->routeId), "%07liP", time (NULL)>>8);
/* at the moment we don't need publicity */
curl_easy_setopt (ph->curlHandle, CURLOPT_USERAGENT, PIANO_USERAGENT);
- /* set tor as control connection proxy */
- curl_easy_setopt (ph->curlHandle, CURLOPT_PROXY, "localhost:9050");
- curl_easy_setopt (ph->curlHandle, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4A);
- curl_easy_setopt (ph->curlHandle, CURLOPT_CONNECTTIMEOUT, 60);
}
/* free complete search result
diff --git a/src/Makefile.am b/src/Makefile.am
index 20b5b40..1a8f3cf 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,6 +1,6 @@
bin_PROGRAMS = pianobar
-pianobar_SOURCES = main.c
+pianobar_SOURCES = main.c terminal.c terminal.h settings.c settings.h
pianobar_CPPFLAGS = ${LIBCURL_CFLAGS} ${LIBAO_CFLAGS} ${LIBXML_CFLAGS} -I../libpiano
pianobar_LDADD = ${LIBCURL_LIBS} ${LIBAO_LIBS} ${LIBFAAD_LIBS} ${LIBREADLINE_LIBS} ${LIBXML_LIBS} -lpthread ../libpiano/libpiano.la
diff --git a/src/main.c b/src/main.c
index 05d1f4e..a7df2aa 100644
--- a/src/main.c
+++ b/src/main.c
@@ -31,10 +31,12 @@ THE SOFTWARE.
#include <neaacdec.h>
#include <pthread.h>
#include <unistd.h>
-#include <termios.h>
#include <poll.h>
#include <readline/readline.h>
+#include "terminal.h"
+#include "settings.h"
+
struct aacPlayer {
/* buffer */
char *buffer;
@@ -278,23 +280,38 @@ int main (int argc, char **argv) {
char doQuit = 0;
PianoSong_t *curSong = NULL;
PianoStation_t *curStation;
- struct termios termopts;
+ BarSettings_t bsettings;
/* init some things */
curl_global_init (CURL_GLOBAL_SSL);
xmlInitParser ();
ao_initialize();
+
+ BarSettingsInit (&bsettings);
+ readSettings (&bsettings);
+
+ if (bsettings.username == NULL) {
+ bsettings.username = readline ("Username: ");
+ }
+ if (bsettings.password == NULL) {
+ termSetEcho (0);
+ bsettings.password = readline ("Password: ");
+ termSetEcho (1);
+ }
+
PianoInit (&ph);
+ /* setup control proxy */
+ curl_easy_setopt (ph.curlHandle, CURLOPT_PROXY, bsettings.controlProxy);
+ curl_easy_setopt (ph.curlHandle, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4A);
+ curl_easy_setopt (ph.curlHandle, CURLOPT_CONNECTTIMEOUT, 60);
/* no buffering for stdin */
- tcgetattr (fileno (stdin), &termopts);
- termopts.c_lflag &= ~ICANON;
- tcsetattr(fileno (stdin), TCSANOW, &termopts);
- setvbuf (stdin, NULL, _IONBF, 1);
+ termSetBuffer (0);
- PianoConnect (&ph, argv[1], argv[2]);
+ printf ("Login...\n");
+ PianoConnect (&ph, bsettings.username, bsettings.password);
+ printf ("Get stations...\n");
PianoGetStations (&ph);
- printf ("webAuthToken: %s\nauthToken: %s\nlistenerId: %s\n", ph.user.webAuthToken, ph.user.authToken, ph.user.listenerId);
/* select station */
curStation = selectStation (&ph);
@@ -467,6 +484,7 @@ int main (int argc, char **argv) {
curl_global_cleanup ();
ao_shutdown();
xmlCleanupParser ();
+ BarSettingsDestroy (&bsettings);
return 0;
}
diff --git a/src/settings.c b/src/settings.c
new file mode 100644
index 0000000..2b1c313
--- /dev/null
+++ b/src/settings.c
@@ -0,0 +1,95 @@
+/*
+Copyright (c) 2008 Lars-Dominik Braun
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "settings.h"
+
+/* tries to guess your config dir; somehow conforming to
+ * http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
+ * @author PromyLOPh
+ * @added 2008-06-15
+ * @param name of the config file (can contain subdirs too)
+ * @param store the whole path here
+ * @param but only up to this size
+ * @return nothing
+ */
+void getXdgConfigDir (char *filename, char *retDir, size_t retDirN) {
+ char *xdgConfigDir = NULL;
+
+ if ((xdgConfigDir = getenv ("XDG_CONFIG_HOME")) != NULL &&
+ strlen (xdgConfigDir) > 0) {
+ /* special dir: $xdg_config_home */
+ snprintf (retDir, retDirN, "%s/%s", xdgConfigDir, filename);
+ } else {
+ if ((xdgConfigDir = getenv ("HOME")) != NULL &&
+ strlen (xdgConfigDir) > 0) {
+ /* standard config dir: $home/.config */
+ snprintf (retDir, retDirN, "%s/.config/%s", xdgConfigDir,
+ filename);
+ } else {
+ /* fallback: working dir */
+ snprintf (retDir, retDirN, "%s", filename);
+ }
+ }
+}
+
+void BarSettingsInit (BarSettings_t *settings) {
+ memset (settings, 0, sizeof (*settings));
+}
+
+void BarSettingsDestroy (BarSettings_t *settings) {
+ free (settings->controlProxy);
+ free (settings->username);
+ free (settings->password);
+}
+
+void readSettings (BarSettings_t *settings) {
+ char configfile[1024], key[256], val[256];
+ FILE *configfd;
+
+ getXdgConfigDir (PACKAGE "/config", configfile, sizeof (configfile));
+ if ((configfd = fopen (configfile, "r")) == NULL) {
+ printf ("config file at %s not found\n", configfile);
+ return;
+ }
+
+ while (!feof (configfd)) {
+ memset (val, 0, sizeof (*val));
+ memset (key, 0, sizeof (*key));
+ if (fscanf (configfd, "%255s = %255[^\n]", key, val) < 2) {
+ /* invalid config line */
+ continue;
+ }
+ if (strcmp ("control_proxy", key) == 0) {
+ settings->controlProxy = strdup (val);
+ } else if (strcmp ("user", key) == 0) {
+ settings->username = strdup (val);
+ } else if (strcmp ("password", key) == 0) {
+ settings->password = strdup (val);
+ }
+ }
+
+ fclose (configfd);
+}
diff --git a/src/settings.h b/src/settings.h
new file mode 100644
index 0000000..26ca527
--- /dev/null
+++ b/src/settings.h
@@ -0,0 +1,39 @@
+/*
+Copyright (c) 2008 Lars-Dominik Braun
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#ifndef _SETTINGS_H
+#define _SETTINGS_H
+
+struct BarSettings {
+ char *username;
+ char *password;
+ char *controlProxy; /* non-american listeners need this */
+};
+
+typedef struct BarSettings BarSettings_t;
+
+void BarSettingsInit (BarSettings_t *settings);
+void BarSettingsDestroy (BarSettings_t *settings);
+
+void readSettings (BarSettings_t *settings);
+
+#endif /* _SETTINGS_H */
diff --git a/src/terminal.c b/src/terminal.c
new file mode 100644
index 0000000..e3fd5d0
--- /dev/null
+++ b/src/terminal.c
@@ -0,0 +1,50 @@
+/*
+Copyright (c) 2008 Lars-Dominik Braun
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include <termios.h>
+#include <stdio.h>
+
+void termSetEcho (char enable) {
+ struct termios termopts;
+
+ tcgetattr (fileno (stdin), &termopts);
+ if (enable == 1) {
+ termopts.c_lflag |= ECHO;
+ } else {
+ termopts.c_lflag &= ~ECHO;
+ }
+ tcsetattr(fileno (stdin), TCSANOW, &termopts);
+}
+
+void termSetBuffer (char enable) {
+ struct termios termopts;
+
+ tcgetattr (fileno (stdin), &termopts);
+ if (enable == 1) {
+ termopts.c_lflag |= ICANON;
+ setlinebuf (stdin);
+ } else {
+ termopts.c_lflag &= ~ICANON;
+ setvbuf (stdin, NULL, _IONBF, 1);
+ }
+ tcsetattr(fileno (stdin), TCSANOW, &termopts);
+}
diff --git a/src/terminal.h b/src/terminal.h
new file mode 100644
index 0000000..397a5d6
--- /dev/null
+++ b/src/terminal.h
@@ -0,0 +1,29 @@
+/*
+Copyright (c) 2008 Lars-Dominik Braun
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#ifndef _TERMINAL_H
+#define _TERMINAL_H
+
+void termSetEcho (char enable);
+void termSetBuffer (char enable);
+
+#endif /* _TERMINAL_H */