From 2aba0437a6ab9097ea61f658337fcf470fca73af Mon Sep 17 00:00:00 2001 From: Qball Cow Date: Tue, 23 Aug 2005 12:01:37 +0000 Subject: Saving state of output-device in state-file. (This is a temporary solution, rewrite of state-file is planned for 0.13) git-svn-id: https://svn.musicpd.org/mpd/trunk@3449 09075e82-0dd4-0310-85a5-a0d7c8717e4f --- TODO | 1 + src/audio.c | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/audio.h | 4 ++ src/main.c | 7 ++++ 4 files changed, 143 insertions(+) diff --git a/TODO b/TODO index 8d127ff4f..c8b23f470 100644 --- a/TODO +++ b/TODO @@ -14,6 +14,7 @@ *) audio output *) add support for saving and restoring audioOutput state to the state_file + (solution implemented by Qball) *) mixer *) add sun support diff --git a/src/audio.c b/src/audio.c index 277ab6cf0..8f7200d46 100644 --- a/src/audio.c +++ b/src/audio.c @@ -23,11 +23,29 @@ #include "sig_handlers.h" #include "command.h" #include "playerData.h" +#include "utils.h" #include #include #include #include +#include +#include +#include +#include + + +/* crappy code by qball */ +#define AUDIO_STATE_FILE_DEVICE_START "audio_device_start" +#define AUDIO_STATE_FILE_DEVICE_ID "audio_device_id: " +#define AUDIO_STATE_FILE_DEVICE_ENABLED "audio_device_enabled: " +#define AUDIO_STATE_FILE_DEVICE_NAME "audio_device_name: " +#define AUDIO_STATE_FILE_DEVICE_END "audio_device_end" + +#define AUDIO_BUFFER_SIZE 2*MAXPATHLEN +/* /crappy code */ + + static AudioFormat audio_format; @@ -396,3 +414,116 @@ void printAudioDevices(FILE * fp) { (int)pdAudioDevicesEnabled[i]); } } + + +/* more qball crappy code */ + +static char * getStateFile() { + ConfigParam * param = parseConfigFilePath(CONF_STATE_FILE, 0); + + if(!param) return NULL; + + return param->value; +} + + + +void saveAudioDevicesState() { + int i; + char * stateFile = getStateFile(); + + if(stateFile) { + FILE * fp; + + while(!(fp = fopen(stateFile,"a")) && errno==EINTR); + if(!fp) { + ERROR("problems opening state file \"%s\" for " + "writing: %s\n", stateFile, + strerror(errno)); + return; + } + for(i = 0; i < audioOutputArraySize; i++) { + myfprintf(fp, "%s\n", AUDIO_STATE_FILE_DEVICE_START); + myfprintf(fp, "%s%i\n", AUDIO_STATE_FILE_DEVICE_ID, i); + myfprintf(fp, "%s%s\n", AUDIO_STATE_FILE_DEVICE_NAME, audioOutputArray[i]->name); + myfprintf(fp, "%s%i\n", AUDIO_STATE_FILE_DEVICE_ENABLED,(int)pdAudioDevicesEnabled[i]); + myfprintf(fp, "%s\n", AUDIO_STATE_FILE_DEVICE_END); + } + while(fclose(fp) && errno==EINTR); + } +} + +void readAudioDevicesState() { + char * stateFile = getStateFile(); + FILE *fp; + struct stat st; + if(stateFile) { + char buffer[AUDIO_BUFFER_SIZE]; + + + + + if(stat(stateFile,&st)<0) { + DEBUG("failed to stat state file\n"); + return; + } + if(!S_ISREG(st.st_mode)) { + ERROR("state file \"%s\" is not a regular " + "file\n",stateFile); + exit(EXIT_FAILURE); + } + + fp = fopen(stateFile,"r"); + if(!fp) { + ERROR("problems opening state file \"%s\" for " + "reading: %s\n", stateFile, + strerror(errno)); + exit(EXIT_FAILURE); + } + + while(myFgets(buffer,AUDIO_BUFFER_SIZE,fp)) { + if(strncmp(buffer,AUDIO_STATE_FILE_DEVICE_START, strlen(AUDIO_STATE_FILE_DEVICE_START))==0) { + char *name = NULL; + int id = -1; + int enabled = 1; + if(!myFgets(buffer,AUDIO_BUFFER_SIZE,fp)) { + ERROR("error parsing state file \"%s\"\n", stateFile); + exit(EXIT_FAILURE); + } + while(strcmp(buffer,AUDIO_STATE_FILE_DEVICE_END)) { + if(strncmp(buffer,AUDIO_STATE_FILE_DEVICE_ID, strlen(AUDIO_STATE_FILE_DEVICE_ID)) == 0 ) { + if(strlen(buffer) > strlen(AUDIO_STATE_FILE_DEVICE_ID)) + { + id = atoi(&buffer[strlen(AUDIO_STATE_FILE_DEVICE_ID)]); + } + } + if(strncmp(buffer,AUDIO_STATE_FILE_DEVICE_ENABLED, strlen(AUDIO_STATE_FILE_DEVICE_ENABLED)) == 0 ) { + if(strlen(buffer) > strlen(AUDIO_STATE_FILE_DEVICE_ENABLED)) + { + enabled = atoi(&buffer[strlen(AUDIO_STATE_FILE_DEVICE_ENABLED)]); + } + } + if(!myFgets(buffer,AUDIO_BUFFER_SIZE,fp)) { + ERROR("error parsing state file \"%s\"\n", stateFile); + exit(EXIT_FAILURE); + } + } + if(id != -1) + { + /* search for same name here, can we trust id? */ + if(id < audioOutputArraySize) + { + pdAudioDevicesEnabled[id] = enabled; + } + } + if(name != NULL) + { + free(name); + } + } + } + + fclose(fp); + } +} + diff --git a/src/audio.h b/src/audio.h index 87c265568..f1a63ee69 100644 --- a/src/audio.h +++ b/src/audio.h @@ -75,4 +75,8 @@ int disableAudioDevice(FILE * fp, int device); void printAudioDevices(FILE * fp); +/* qball's crappy code */ +void readAudioDevicesState(); +void saveAudioDevicesState(); + #endif diff --git a/src/main.c b/src/main.c index cda6d1077..67c76a981 100644 --- a/src/main.c +++ b/src/main.c @@ -491,6 +491,9 @@ int main(int argc, char * argv[]) { initSigHandlers(); readPlaylistState(); + /* qball crappy code */ + readAudioDevicesState(); + while(COMMAND_RETURN_KILL!=doIOForInterfaces()) { if(COMMAND_RETURN_KILL==handlePendingSignals()) break; syncPlayerAndPlaylist(); @@ -499,6 +502,10 @@ int main(int argc, char * argv[]) { } savePlaylistState(); + /* qball crappy code */ + saveAudioDevicesState(); + + playerKill(); freeAllInterfaces(); -- cgit v1.2.3