1
0
mirror of https://github.com/SuperBFG7/ympd synced 2025-03-04 10:38:18 +00:00

Feat: improve state file handling - separate files per state setting

This commit is contained in:
jcorporation 2018-10-01 20:13:38 +01:00
parent f9dc014adc
commit 7a4e796df7
3 changed files with 108 additions and 45 deletions

View File

@ -57,6 +57,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) {
int je, int_buf1, int_buf2, int_rc; int je, int_buf1, int_buf2, int_rc;
float float_buf; float float_buf;
char *p_charbuf1, *p_charbuf2, *p_charbuf3, *p_charbuf4; char *p_charbuf1, *p_charbuf2, *p_charbuf3, *p_charbuf4;
char p_char[4];
enum mpd_cmd_ids cmd_id; enum mpd_cmd_ids cmd_id;
struct pollfd fds[1]; struct pollfd fds[1];
int pollrc; int pollrc;
@ -112,26 +113,28 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) {
n = mympd_put_state(mpd.buf, &mpd.song_id, &mpd.next_song_id, &mpd.last_song_id, &mpd.queue_version, &mpd.queue_length); n = mympd_put_state(mpd.buf, &mpd.song_id, &mpd.next_song_id, &mpd.last_song_id, &mpd.queue_version, &mpd.queue_length);
break; break;
case MPD_API_SETTINGS_SET: case MPD_API_SETTINGS_SET:
je = json_scanf(msg.p, msg.len, "{data: {notificationWeb: %B, notificationPage: %B, jukeboxMode: %d, jukeboxPlaylist: %Q, jukeboxQueueLength: %d}}", je = json_scanf(msg.p, msg.len, "{data: {notificationWeb: %B}}", &mympd_state.notificationWeb);
&mympd_state.notificationWeb, if (je == 1)
&mympd_state.notificationPage, mympd_state_set("notificationWeb", (mympd_state.notificationWeb == true ? "true" : "false"));
&mympd_state.jukeboxMode,
&mympd_state.jukeboxPlaylist, je = json_scanf(msg.p, msg.len, "{data: {notificationPage: %B}}", &mympd_state.notificationPage);
&mympd_state.jukeboxQueueLength); if (je == 1)
if (je == 5) { mympd_state_set("notificationPage", (mympd_state.notificationPage == true ? "true" : "false"));
char tmpfile[400];
char statefile[400]; je = json_scanf(msg.p, msg.len, "{data: {jukeboxMode: %d}}", &mympd_state.jukeboxMode);
snprintf(tmpfile, 400, "%s/tmp/mympd.state", config.varlibdir ); if (je == 1) {
snprintf(statefile, 400, "%s/mympd.state", config.varlibdir ); snprintf(p_char, 4, "%d", mympd_state.jukeboxMode);
json_fprintf(tmpfile, "{notificationWeb: %B, notificationPage: %B, jukeboxMode: %d, jukeboxPlaylist: %Q, jukeboxQueueLength: %d}", mympd_state_set("jukeboxMode", p_char);
mympd_state.notificationWeb, }
mympd_state.notificationPage,
mympd_state.jukeboxMode, je = json_scanf(msg.p, msg.len, "{data: {jukeboxPlaylist: %Q}}", &mympd_state.jukeboxPlaylist);
mympd_state.jukeboxPlaylist, if (je == 1)
mympd_state.jukeboxQueueLength); mympd_state_set("jukeboxPlaylist", mympd_state.jukeboxPlaylist);
rename(tmpfile, statefile);
if (mympd_state.jukeboxMode > 0) je = json_scanf(msg.p, msg.len, "{data: {jukeboxQueueLength: %d}}", &mympd_state.jukeboxQueueLength);
mympd_jukebox(); if (je == 1) {
snprintf(p_char, 4, "%d", mympd_state.jukeboxQueueLength);
mympd_state_set("jukeboxQueueLength", p_char);
} }
je = json_scanf(msg.p, msg.len, "{data: {random: %u}}", &uint_buf1); je = json_scanf(msg.p, msg.len, "{data: {random: %u}}", &uint_buf1);
@ -173,6 +176,8 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) {
} }
free(p_charbuf1); free(p_charbuf1);
} }
if (mympd_state.jukeboxMode > 0)
mympd_jukebox();
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}"); n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
break; break;
case MPD_API_WELCOME: case MPD_API_WELCOME:
@ -1163,6 +1168,47 @@ int mympd_put_welcome(char *buffer) {
return len; return len;
} }
bool mympd_state_get(char *name, char *value) {
char cfgfile[400];
char *line;
size_t n = 0;
ssize_t read;
snprintf(cfgfile, 400, "%s/state/%s", config.varlibdir, name);
FILE *fp = fopen(cfgfile, "r");
if (fp == NULL) {
printf("Error opening %s", cfgfile);
return false;
}
read = getline(&line, &n, fp);
snprintf(value, 400, "%s", line);
#ifdef DEBUG
printf("State %s: %s\n", name, value);
#endif
fclose(fp);
if (read > 0)
return true;
else
return false;
}
bool mympd_state_set(char *name, char *value) {
char tmpfile[400];
char cfgfile[400];
snprintf(cfgfile, 400, "%s/state/%s", config.varlibdir, name);
snprintf(tmpfile, 400, "%s/tmp/%s", config.varlibdir, name);
FILE *fp = fopen(tmpfile, "w");
if (fp == NULL) {
printf("Error opening %s", tmpfile);
return false;
}
fprintf(fp, value);
fclose(fp);
rename(tmpfile, cfgfile);
return true;
}
int mympd_put_settings(char *buffer) { int mympd_put_settings(char *buffer) {
struct mpd_status *status; struct mpd_status *status;
char *replaygain = strdup(""); char *replaygain = strdup("");

View File

@ -201,6 +201,8 @@ void mympd_last_played_song_uri(const char *uri);
void mympd_last_played_song_id(int song_id); void mympd_last_played_song_id(int song_id);
void mympd_get_sticker(const char *uri, t_sticker *sticker); void mympd_get_sticker(const char *uri, t_sticker *sticker);
void mympd_jukebox(); void mympd_jukebox();
bool mympd_state_get(char *name, char *value);
bool mympd_state_set(char *name, char *value);
int mympd_smartpls_save(char *smartpltype, char *playlist, char *tag, char *searchstr, int maxentries, int timerange); int mympd_smartpls_save(char *smartpltype, char *playlist, char *tag, char *searchstr, int maxentries, int timerange);
int mympd_smartpls_put(char *buffer, char *playlist); int mympd_smartpls_put(char *buffer, char *playlist);
int mympd_smartpls_update_all(); int mympd_smartpls_update_all();

View File

@ -180,13 +180,50 @@ static int inihandler(void* user, const char* section, const char* name, const c
return 1; return 1;
} }
void read_statefiles() {
char *crap;
char value[400];
if (mympd_state_get("notificationWeb", value)) {
if (strcmp(value, "true") == 0)
mympd_state.notificationWeb = true;
else
mympd_state.notificationWeb = false;
}
else
mympd_state.notificationWeb = false;
if (mympd_state_get("notificationPage", value)) {
if (strcmp(value, "true") == 0)
mympd_state.notificationPage = true;
else
mympd_state.notificationPage = false;
}
else
mympd_state.notificationPage = true;
if (mympd_state_get("jukeboxMode", value))
mympd_state.jukeboxMode = strtol(value, &crap, 10);
else
mympd_state.jukeboxMode = 0;
if (mympd_state_get("jukeboxPlaylist", value))
mympd_state.jukeboxPlaylist = value;
else
mympd_state.jukeboxPlaylist = "Database";
if (mympd_state_get("jukeboxQueueLength", value))
mympd_state.jukeboxQueueLength = strtol(value, &crap, 10);
else
mympd_state.jukeboxQueueLength = 1;
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
struct mg_mgr mgr; struct mg_mgr mgr;
struct mg_connection *nc; struct mg_connection *nc;
struct mg_connection *nc_http; struct mg_connection *nc_http;
struct mg_bind_opts bind_opts; struct mg_bind_opts bind_opts;
const char *err; const char *err;
char statefile[400];
//defaults //defaults
config.mpdhost = "127.0.0.1"; config.mpdhost = "127.0.0.1";
@ -241,29 +278,7 @@ int main(int argc, char **argv) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
snprintf(statefile, 400, "%s/mympd.state", config.varlibdir); read_statefiles();
if (access(statefile, F_OK ) != -1 ) {
char *content = json_fread(statefile);
int je = json_scanf(content, strlen(content), "{notificationWeb: %B, notificationPage: %B, jukeboxMode: %d, jukeboxPlaylist: %Q, jukeboxQueueLength: %d}",
&mympd_state.notificationWeb,
&mympd_state.notificationPage,
&mympd_state.jukeboxMode,
&mympd_state.jukeboxPlaylist,
&mympd_state.jukeboxQueueLength);
if (je != 5) {
mympd_state.notificationWeb = false;
mympd_state.notificationPage = true;
mympd_state.jukeboxMode = 0;
mympd_state.jukeboxPlaylist = "Database";
mympd_state.jukeboxQueueLength = 1;
}
} else {
mympd_state.notificationWeb = false;
mympd_state.notificationPage = true;
mympd_state.jukeboxMode = 0;
mympd_state.jukeboxPlaylist = "Database";
mympd_state.jukeboxQueueLength = 1;
}
signal(SIGTERM, signal_handler); signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler); signal(SIGINT, signal_handler);