summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars-Dominik Braun <lars@6xq.net>2012-09-14 21:38:33 +0200
committerLars-Dominik Braun <lars@6xq.net>2012-09-22 16:39:05 +0200
commit1c1628706034eac85b062a028bacc85bc18efc22 (patch)
tree77b748cce19e2741e3ddd9c763b60f73512709cb
parentfe1ffbd7257b4c0dd43a825912bcd65732df6db2 (diff)
downloadpianobar-1c1628706034eac85b062a028bacc85bc18efc22.tar.gz
pianobar-1c1628706034eac85b062a028bacc85bc18efc22.tar.bz2
pianobar-1c1628706034eac85b062a028bacc85bc18efc22.zip
Save station and volume to statefile
… and restore both on startup. To disable run `ln -sv /dev/null ~/.config/pianobar/state`. Setting an invalid autostart_station in the config file prevents automatic station selection on startup. Closes #305.
-rw-r--r--src/main.c3
-rw-r--r--src/settings.c303
-rw-r--r--src/settings.h3
3 files changed, 171 insertions, 138 deletions
diff --git a/src/main.c b/src/main.c
index f011c5a..1d24167 100644
--- a/src/main.c
+++ b/src/main.c
@@ -402,6 +402,9 @@ int main (int argc, char **argv) {
close (app.input.fds[1]);
}
+ /* write statefile */
+ BarSettingsWrite (app.curStation, &app.settings);
+
PianoDestroy (&app.ph);
PianoDestroyPlaylist (app.songHistory);
PianoDestroyPlaylist (app.playlist);
diff --git a/src/settings.c b/src/settings.c
index 6385175..670bcc0 100644
--- a/src/settings.c
+++ b/src/settings.c
@@ -1,5 +1,5 @@
/*
-Copyright (c) 2008-2011
+Copyright (c) 2008-2012
Lars-Dominik Braun <lars@6xq.net>
Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -113,7 +113,8 @@ void BarSettingsDestroy (BarSettings_t *settings) {
* @return nothing yet
*/
void BarSettingsRead (BarSettings_t *settings) {
- char configfile[PATH_MAX], key[256], val[256];
+ char *configfiles[] = {PACKAGE "/state", PACKAGE "/config"},
+ path[PATH_MAX], key[256], val[256];
FILE *configfd;
static const char *formatMsgPrefix = "format_msg_";
@@ -163,152 +164,155 @@ void BarSettingsRead (BarSettings_t *settings) {
settings->keys[i] = dispatchActions[i].defaultKey;
}
- BarGetXdgConfigDir (PACKAGE "/config", configfile, sizeof (configfile));
- if ((configfd = fopen (configfile, "r")) == NULL) {
- return;
- }
-
- /* read config file */
- while (1) {
- char lwhite, rwhite;
- int scanRet = fscanf (configfd, "%255s%c=%c%255[^\n]", key, &lwhite, &rwhite, val);
- if (scanRet == EOF) {
- break;
- } else if (scanRet != 4 || lwhite != ' ' || rwhite != ' ') {
- /* invalid config line */
+ /* read config files */
+ for (size_t j = 0; j < sizeof (configfiles) / sizeof (*configfiles); j++) {
+ BarGetXdgConfigDir (configfiles[j], path, sizeof (path));
+ if ((configfd = fopen (path, "r")) == NULL) {
continue;
}
- if (streq ("control_proxy", key)) {
- settings->controlProxy = strdup (val);
- } else if (streq ("proxy", key)) {
- settings->proxy = strdup (val);
- } else if (streq ("user", key)) {
- settings->username = strdup (val);
- } else if (streq ("password", key)) {
- settings->password = strdup (val);
- } else if (streq ("rpc_host", key)) {
- free (settings->rpcHost);
- settings->rpcHost = strdup (val);
- } else if (streq ("partner_user", key)) {
- free (settings->partnerUser);
- settings->partnerUser = strdup (val);
- } else if (streq ("partner_password", key)) {
- free (settings->partnerPassword);
- settings->partnerPassword = strdup (val);
- } else if (streq ("device", key)) {
- free (settings->device);
- settings->device = strdup (val);
- } else if (streq ("encrypt_password", key)) {
- free (settings->outkey);
- settings->outkey = strdup (val);
- } else if (streq ("decrypt_password", key)) {
- free (settings->inkey);
- settings->inkey = strdup (val);
- } else if (memcmp ("act_", key, 4) == 0) {
- size_t i;
- /* keyboard shortcuts */
- for (i = 0; i < BAR_KS_COUNT; i++) {
- if (streq (dispatchActions[i].configKey, key)) {
- if (streq (val, "disabled")) {
- settings->keys[i] = BAR_KS_DISABLED;
- } else {
- settings->keys[i] = val[0];
+
+ while (1) {
+ char lwhite, rwhite;
+ int scanRet = fscanf (configfd, "%255s%c=%c%255[^\n]", key, &lwhite, &rwhite, val);
+ if (scanRet == EOF) {
+ break;
+ } else if (scanRet != 4 || lwhite != ' ' || rwhite != ' ') {
+ /* invalid config line */
+ continue;
+ }
+ if (streq ("control_proxy", key)) {
+ settings->controlProxy = strdup (val);
+ } else if (streq ("proxy", key)) {
+ settings->proxy = strdup (val);
+ } else if (streq ("user", key)) {
+ settings->username = strdup (val);
+ } else if (streq ("password", key)) {
+ settings->password = strdup (val);
+ } else if (streq ("rpc_host", key)) {
+ free (settings->rpcHost);
+ settings->rpcHost = strdup (val);
+ } else if (streq ("partner_user", key)) {
+ free (settings->partnerUser);
+ settings->partnerUser = strdup (val);
+ } else if (streq ("partner_password", key)) {
+ free (settings->partnerPassword);
+ settings->partnerPassword = strdup (val);
+ } else if (streq ("device", key)) {
+ free (settings->device);
+ settings->device = strdup (val);
+ } else if (streq ("encrypt_password", key)) {
+ free (settings->outkey);
+ settings->outkey = strdup (val);
+ } else if (streq ("decrypt_password", key)) {
+ free (settings->inkey);
+ settings->inkey = strdup (val);
+ } else if (memcmp ("act_", key, 4) == 0) {
+ size_t i;
+ /* keyboard shortcuts */
+ for (i = 0; i < BAR_KS_COUNT; i++) {
+ if (streq (dispatchActions[i].configKey, key)) {
+ if (streq (val, "disabled")) {
+ settings->keys[i] = BAR_KS_DISABLED;
+ } else {
+ settings->keys[i] = val[0];
+ }
+ break;
}
- break;
}
- }
- } else if (streq ("audio_quality", key)) {
- if (streq (val, "low")) {
- settings->audioQuality = PIANO_AQ_LOW;
- } else if (streq (val, "medium")) {
- settings->audioQuality = PIANO_AQ_MEDIUM;
- } else if (streq (val, "high")) {
- settings->audioQuality = PIANO_AQ_HIGH;
- }
- } else if (streq ("autostart_station", key)) {
- settings->autostartStation = strdup (val);
- } else if (streq ("event_command", key)) {
- settings->eventCmd = strdup (val);
- } else if (streq ("history", key)) {
- settings->history = atoi (val);
- } else if (streq ("sort", key)) {
- size_t i;
- static const char *mapping[] = {"name_az",
- "name_za",
- "quickmix_01_name_az",
- "quickmix_01_name_za",
- "quickmix_10_name_az",
- "quickmix_10_name_za",
- };
- for (i = 0; i < BAR_SORT_COUNT; i++) {
- if (streq (mapping[i], val)) {
- settings->sortOrder = i;
- break;
+ } else if (streq ("audio_quality", key)) {
+ if (streq (val, "low")) {
+ settings->audioQuality = PIANO_AQ_LOW;
+ } else if (streq (val, "medium")) {
+ settings->audioQuality = PIANO_AQ_MEDIUM;
+ } else if (streq (val, "high")) {
+ settings->audioQuality = PIANO_AQ_HIGH;
}
- }
- } else if (streq ("love_icon", key)) {
- free (settings->loveIcon);
- settings->loveIcon = strdup (val);
- } else if (streq ("ban_icon", key)) {
- free (settings->banIcon);
- settings->banIcon = strdup (val);
- } else if (streq ("at_icon", key)) {
- free (settings->atIcon);
- settings->atIcon = strdup (val);
- } else if (streq ("volume", key)) {
- settings->volume = atoi (val);
- } else if (streq ("format_nowplaying_song", key)) {
- free (settings->npSongFormat);
- settings->npSongFormat = strdup (val);
- } else if (streq ("format_nowplaying_station", key)) {
- free (settings->npStationFormat);
- settings->npStationFormat = strdup (val);
- } else if (streq ("format_list_song", key)) {
- free (settings->listSongFormat);
- settings->listSongFormat = strdup (val);
- } else if (streq ("fifo", key)) {
- free (settings->fifo);
- settings->fifo = strdup (val);
- } else if (streq ("autoselect", key)) {
- settings->autoselect = atoi (val);
- } else if (streq ("tls_fingerprint", key)) {
- /* expects 40 byte hex-encoded sha1 */
- if (strlen (val) == 40) {
- for (size_t i = 0; i < 20; i++) {
- char hex[3];
- memcpy (hex, &val[i*2], 2);
- hex[2] = '\0';
- settings->tlsFingerprint[i] = strtol (hex, NULL, 16);
+ } else if (streq ("autostart_station", key)) {
+ free (settings->autostartStation);
+ settings->autostartStation = strdup (val);
+ } else if (streq ("event_command", key)) {
+ settings->eventCmd = strdup (val);
+ } else if (streq ("history", key)) {
+ settings->history = atoi (val);
+ } else if (streq ("sort", key)) {
+ size_t i;
+ static const char *mapping[] = {"name_az",
+ "name_za",
+ "quickmix_01_name_az",
+ "quickmix_01_name_za",
+ "quickmix_10_name_az",
+ "quickmix_10_name_za",
+ };
+ for (i = 0; i < BAR_SORT_COUNT; i++) {
+ if (streq (mapping[i], val)) {
+ settings->sortOrder = i;
+ break;
+ }
}
- }
- } else if (strncmp (formatMsgPrefix, key,
- strlen (formatMsgPrefix)) == 0) {
- static const char *mapping[] = {"none", "info", "nowplaying",
- "time", "err", "question", "list"};
- const char *typeStart = key + strlen (formatMsgPrefix);
- for (size_t i = 0; i < sizeof (mapping) / sizeof (*mapping); i++) {
- if (streq (typeStart, mapping[i])) {
- const char *formatPos = strstr (val, "%s");
-
- /* keep default if there is no format character */
- if (formatPos != NULL) {
- BarMsgFormatStr_t *format = &settings->msgFormat[i];
+ } else if (streq ("love_icon", key)) {
+ free (settings->loveIcon);
+ settings->loveIcon = strdup (val);
+ } else if (streq ("ban_icon", key)) {
+ free (settings->banIcon);
+ settings->banIcon = strdup (val);
+ } else if (streq ("at_icon", key)) {
+ free (settings->atIcon);
+ settings->atIcon = strdup (val);
+ } else if (streq ("volume", key)) {
+ settings->volume = atoi (val);
+ } else if (streq ("format_nowplaying_song", key)) {
+ free (settings->npSongFormat);
+ settings->npSongFormat = strdup (val);
+ } else if (streq ("format_nowplaying_station", key)) {
+ free (settings->npStationFormat);
+ settings->npStationFormat = strdup (val);
+ } else if (streq ("format_list_song", key)) {
+ free (settings->listSongFormat);
+ settings->listSongFormat = strdup (val);
+ } else if (streq ("fifo", key)) {
+ free (settings->fifo);
+ settings->fifo = strdup (val);
+ } else if (streq ("autoselect", key)) {
+ settings->autoselect = atoi (val);
+ } else if (streq ("tls_fingerprint", key)) {
+ /* expects 40 byte hex-encoded sha1 */
+ if (strlen (val) == 40) {
+ for (size_t i = 0; i < 20; i++) {
+ char hex[3];
+ memcpy (hex, &val[i*2], 2);
+ hex[2] = '\0';
+ settings->tlsFingerprint[i] = strtol (hex, NULL, 16);
+ }
+ }
+ } else if (strncmp (formatMsgPrefix, key,
+ strlen (formatMsgPrefix)) == 0) {
+ static const char *mapping[] = {"none", "info", "nowplaying",
+ "time", "err", "question", "list"};
+ const char *typeStart = key + strlen (formatMsgPrefix);
+ for (size_t i = 0; i < sizeof (mapping) / sizeof (*mapping); i++) {
+ if (streq (typeStart, mapping[i])) {
+ const char *formatPos = strstr (val, "%s");
+
+ /* keep default if there is no format character */
+ if (formatPos != NULL) {
+ BarMsgFormatStr_t *format = &settings->msgFormat[i];
- free (format->prefix);
- free (format->postfix);
+ free (format->prefix);
+ free (format->postfix);
- const size_t prefixLen = formatPos - val;
- format->prefix = calloc (prefixLen + 1,
- sizeof (*format->prefix));
- memcpy (format->prefix, val, prefixLen);
+ const size_t prefixLen = formatPos - val;
+ format->prefix = calloc (prefixLen + 1,
+ sizeof (*format->prefix));
+ memcpy (format->prefix, val, prefixLen);
- const size_t postfixLen = strlen (val) -
- (formatPos-val) - 2;
- format->postfix = calloc (postfixLen + 1,
- sizeof (*format->postfix));
- memcpy (format->postfix, formatPos+2, postfixLen);
+ const size_t postfixLen = strlen (val) -
+ (formatPos-val) - 2;
+ format->postfix = calloc (postfixLen + 1,
+ sizeof (*format->postfix));
+ memcpy (format->postfix, formatPos+2, postfixLen);
+ }
+ break;
}
- break;
}
}
}
@@ -324,3 +328,26 @@ void BarSettingsRead (BarSettings_t *settings) {
fclose (configfd);
}
+
+/* write statefile
+ */
+void BarSettingsWrite (PianoStation_t *station, BarSettings_t *settings) {
+ char path[PATH_MAX];
+ FILE *fd;
+
+ assert (settings != NULL);
+
+ BarGetXdgConfigDir (PACKAGE "/state", path, sizeof (path));
+ if ((fd = fopen (path, "w")) == NULL) {
+ return;
+ }
+
+ fputs ("# do not edit this file\n", fd);
+ fprintf (fd, "volume = %i\n", settings->volume);
+ if (station != NULL) {
+ fprintf (fd, "autostart_station = %s\n", station->id);
+ }
+
+ fclose (fd);
+}
+
diff --git a/src/settings.h b/src/settings.h
index 488a2fb..0f4c6de 100644
--- a/src/settings.h
+++ b/src/settings.h
@@ -105,9 +105,12 @@ typedef struct {
BarMsgFormatStr_t msgFormat[MSG_COUNT];
} BarSettings_t;
+#include <piano.h>
+
void BarSettingsInit (BarSettings_t *);
void BarSettingsDestroy (BarSettings_t *);
void BarSettingsRead (BarSettings_t *);
+void BarSettingsWrite (PianoStation_t *, BarSettings_t *);
void BarGetXdgConfigDir (const char *, char *, size_t);
#endif /* _SETTINGS_H */