diff options
-rw-r--r-- | conf.c | 27 | ||||
-rw-r--r-- | conf.h | 5 | ||||
-rw-r--r-- | deadbeef.h | 12 | ||||
-rw-r--r-- | plugins.c | 6 | ||||
-rw-r--r-- | plugins/cdda/cdda.c | 76 | ||||
-rw-r--r-- | plugins/hotkeys/hotkeys.c | 41 | ||||
-rw-r--r-- | plugins/lastfm/lastfm.c | 40 |
7 files changed, 65 insertions, 142 deletions
@@ -21,13 +21,7 @@ #include <stdlib.h> #include "conf.h" -struct conf_item_t { - char *key; - char *value; - struct conf_item_t *next; -}; - -static struct conf_item_t *conf_items; +static DB_conf_item_t *conf_items; int conf_load (void) { @@ -86,7 +80,7 @@ conf_free (void) { const char * conf_get_str (const char *key, const char *def) { - for (struct conf_item_t *it = conf_items; it; it = it->next) { + for (DB_conf_item_t *it = conf_items; it; it = it->next) { if (!strcasecmp (key, it->key)) { return it->value; } @@ -106,17 +100,28 @@ conf_get_int (const char *key, int def) { return v ? atoi (v) : def; } +DB_conf_item_t * +conf_find (const char *group, DB_conf_item_t *prev) { + int l = strlen (group); + for (DB_conf_item_t *it = prev ? prev->next : conf_items; it; it = it->next) { + if (!strncasecmp (group, it->key, l)) { + return it; + } + } + return NULL; +} + void conf_set_str (const char *key, const char *val) { - for (struct conf_item_t *it = conf_items; it; it = it->next) { + for (DB_conf_item_t *it = conf_items; it; it = it->next) { if (!strcasecmp (key, it->key)) { free (it->value); it->value = strdup (val); return; } } - struct conf_item_t *it = malloc (sizeof (struct conf_item_t)); - memset (it, 0, sizeof (struct conf_item_t)); + DB_conf_item_t *it = malloc (sizeof (DB_conf_item_t)); + memset (it, 0, sizeof (DB_conf_item_t)); it->next = conf_items; it->key = strdup (key); it->value = strdup (val); @@ -18,6 +18,8 @@ #ifndef __CONF_H #define __CONF_H +#include "deadbeef.h" + int conf_load (void); @@ -39,4 +41,7 @@ conf_get_int (const char *key, int def); void conf_set_str (const char *key, const char *val); +DB_conf_item_t * +conf_find (const char *group, DB_conf_item_t *prev); + #endif // __CONF_H @@ -100,6 +100,12 @@ typedef struct { DB_playItem_t *song; } DB_event_song_t; +typedef struct DB_conf_item_s { + char *key; + char *value; + struct DB_conf_item_s *next; +} DB_conf_item_t; + // event callback type typedef int (*DB_callback_t)(DB_event_t *, uintptr_t data); @@ -218,6 +224,12 @@ typedef struct { int64_t (*fgetlength) (DB_FILE *stream); // message passing int (*sendmessage) (uint32_t id, uintptr_t ctx, uint32_t p1, uint32_t p2); + // configuration access + const char * (*conf_get_str) (const char *key, const char *def); + float (*conf_get_float) (const char *key, float def); + int (*conf_get_int) (const char *key, int def); + void (*conf_set_str) (const char *key, const char *val); + DB_conf_item_t * (*conf_find) (const char *group, DB_conf_item_t *prev); } DB_functions_t; // base plugin interface @@ -109,6 +109,12 @@ static DB_functions_t deadbeef_api = { .fgetlength = vfs_fgetlength, // message passing .sendmessage = messagepump_push, + // configuration access + .conf_get_str = conf_get_str, + .conf_get_float = conf_get_float, + .conf_get_int = conf_get_int, + .conf_set_str = conf_set_str, + .conf_find = conf_find, }; DB_functions_t *deadbeef = &deadbeef_api; diff --git a/plugins/cdda/cdda.c b/plugins/cdda/cdda.c index 576d1cbf..0d85e0b7 100644 --- a/plugins/cdda/cdda.c +++ b/plugins/cdda/cdda.c @@ -76,76 +76,12 @@ trim (char* s) static int read_config () { - char param[ 256 ]; - char config[1024]; - char *key, *value; - snprintf (config, 1024, "%s/cdaudio", deadbeef->get_config_dir()); - - FILE *cfg_file = fopen (config, "rt"); - if (!cfg_file) { - trace ("cdaudio: failed open %s\n", config); - return -1; - } - - while ( fgets( param, sizeof(param), cfg_file ) ) - { - param[ strlen( param )-1 ] = 0; //terminating \n - if (param[0] == '#' || param[0] == 0) - continue; - - char *colon = strchr (param, ':'); - if (!colon) - { - trace ("cdaudio: malformed config string: %s\n", param); - continue; - } - *(colon++) = 0; - key = trim (param); - value = trim (colon + 1); - - if (0 == strcmp (key, "cddb")) - { - if (0 == strcmp (value, "on")) - use_cddb = 1; - else if (0 == strcmp (value, "off")) - use_cddb = 0; - else - { - use_cddb = 0; - trace ("cdaudio: warning, wrong value %s\n", value); - } - } - else if (0 == strcmp (key, "cddb_server")) - { - server[0] = 0; - strncat (server, value, sizeof (server)); - } - else if (0 == strcmp (key, "cddb_port")) - { - port = atoi (value); - } - else if (0 == strcmp (key, "cddb_proxy")) - { - proxy[0] = 0; - strncat (proxy, value, sizeof (server)); - } - else if (0 == strcmp (key, "cddb_proxy_port")) - { - proxy_port = atoi (value); - } - else if (0 == strcmp (key, "proto")) - { - if (0 == strcmp (value, "cddb")) - proto_cddb = 1; - else if (0 == strcmp (value, "http")) - proto_cddb = 0; - else - trace ("cdaudio: unknown protocol \"%s\"\n", value); - } - else - trace ("cdaudio: warning, unknown option %s\n", key); - } - fclose (cfg_file); + use_cddb = deadbeef->conf_get_int ("cdda.freedb.enable", 1); + strncpy (server, deadbeef->conf_get_str ("cdda.freedb.host", "freedb.org"), sizeof (server)-1); + port = deadbeef->conf_get_int ("cdda.freedb.port", 888); + strncpy (proxy, deadbeef->conf_get_str ("cdda.freedb.proxy", ""), sizeof (proxy)-1); + proxy_port = deadbeef->conf_get_int ("cdda.freedb.proxy_port", 8080); + proto_cddb = deadbeef->conf_get_int ("cdda.protocol", 1); // 1 is cddb, 0 is http } static int diff --git a/plugins/hotkeys/hotkeys.c b/plugins/hotkeys/hotkeys.c index e0ea637c..5cde339c 100644 --- a/plugins/hotkeys/hotkeys.c +++ b/plugins/hotkeys/hotkeys.c @@ -103,22 +103,22 @@ trim(char* s) static void cmd_seek_fwd() { - deadbeef->playback_set_pos( deadbeef->playback_get_pos() + 5 ); + deadbeef->playback_set_pos( deadbeef->playback_get_pos() + 5 ); } static void cmd_seek_back() { - deadbeef->playback_set_pos( deadbeef->playback_get_pos() - 5 ); + deadbeef->playback_set_pos( deadbeef->playback_get_pos() - 5 ); } static void cmd_volume_up() { - deadbeef->volume_set_db( deadbeef->volume_get_db() + 2 ); + deadbeef->volume_set_db( deadbeef->volume_get_db() + 2 ); } static void cmd_volume_down() { - deadbeef->volume_set_db( deadbeef->volume_get_db() - 2 ); + deadbeef->volume_set_db( deadbeef->volume_get_db() - 2 ); } static command_func_t @@ -160,35 +160,27 @@ get_command( const char* command ) static int read_config( Display *disp ) { - char param[ 256 ]; - char config[1024]; - snprintf (config, 1024, "%s/hotkeys", deadbeef->get_config_dir()); - FILE *cfg_file = fopen (config, "rt"); - if (!cfg_file) { - fprintf (stderr, "hotkeys: failed open %s\n", config); - return -1; - } - - int line_nr = 0; - - while ( fgets( param, sizeof(param), cfg_file ) ) - { - line_nr++; + DB_conf_item_t *item = deadbeef->conf_find ("hotkeys.", NULL); + while (item) { +// fprintf (stderr, "hotkeys: adding %s %s\n", item->key, item->value); if ( command_count == MAX_COMMAND_COUNT ) { - fprintf( stderr, "hotkeys: [Config line %d] Maximum count (%d) of commands exceeded\n", line_nr, MAX_COMMAND_COUNT ); + fprintf( stderr, "hotkeys: maximum number (%d) of commands exceeded\n", MAX_COMMAND_COUNT ); break; } command_t *cmd_entry = &commands[ command_count ]; cmd_entry->modifier = 0; cmd_entry->keycode = 0; + + size_t l = strlen (item->value); + char param[l+1]; + memcpy (param, item->value, l+1); - param[ strlen( param )-1 ] = 0; //terminating \n char* colon = strchr( param, ':' ); if ( !colon ) { - fprintf( stderr, "hotkeys: [Config line %d] Wrong config line\n", line_nr ); + fprintf( stderr, "hotkeys: bad config option %s %s\n", item->key, item->value); continue; } char* command = colon+1; @@ -235,7 +227,7 @@ read_config( Display *disp ) } if ( !cmd_entry->keycode ) { - fprintf( stderr, "hotkeys: [Config line %d] Unknown key: <%s>\n", line_nr, key ); + fprintf( stderr, "hotkeys: Unknown key: <%s> while parsing %s %s\n", key, item->key, item->value ); continue; } } @@ -244,7 +236,7 @@ read_config( Display *disp ) if ( cmd_entry->keycode == 0 ) { - fprintf( stderr, "hotkeys: [Config line %d] Key not specified\n", line_nr ); + fprintf( stderr, "hotkeys: Key not found while parsing %s %s\n", item->key, item->value); continue; } @@ -252,10 +244,11 @@ read_config( Display *disp ) cmd_entry->func = get_command( command ); if ( !cmd_entry->func ) { - fprintf( stderr, "hotkeys: [Config line %d] Unknown command <%s>\n", line_nr, command ); + fprintf( stderr, "hotkeys: Unknown command <%s> while parsing %s %s\n", command, item->key, item->value); continue; } command_count++; + item = deadbeef->conf_find ("hotkeys.", item); } } diff --git a/plugins/lastfm/lastfm.c b/plugins/lastfm/lastfm.c index 77ae5657..bd4577c9 100644 --- a/plugins/lastfm/lastfm.c +++ b/plugins/lastfm/lastfm.c @@ -705,8 +705,6 @@ lastfm_start (void) { // subscribe to frameupdate event deadbeef->ev_subscribe (DB_PLUGIN (&plugin), DB_EV_SONGSTARTED, DB_CALLBACK (lastfm_songstarted), 0); deadbeef->ev_subscribe (DB_PLUGIN (&plugin), DB_EV_SONGFINISHED, DB_CALLBACK (lastfm_songfinished), 0); - // load login/pass - char config[1024]; // {{{ lastfm v2 auth #if 0 @@ -725,41 +723,9 @@ lastfm_start (void) { #endif // }}} - snprintf (config, 1024, "%s/lastfm", deadbeef->get_config_dir ()); - FILE *fp = fopen (config, "rt"); - if (!fp) { - trace ("lastfm: failed open %s\n", config); - return -1; - } - if (!fgets (lfm_user, 50, fp)) { - trace ("lastfm: failed to read login from %s\n", config); - fclose (fp); - return -1; - } - if (!fgets (lfm_pass, 50, fp)) { - trace ("lastfm: failed to read pass from %s\n", config); - fclose (fp); - return -1; - } - fclose (fp); - // remove trailing garbage - int l; - char *p; - l = strlen (lfm_user); - p = lfm_user+l-1; - while (p >= lfm_user && *p < 0x20) { - p--; - } - p++; - *p = 0; - l = strlen (lfm_pass); - p = lfm_pass+l-1; - while (p >= lfm_pass && *p < 0x20) { - p--; - } - p++; - *p = 0; - + // load login/pass + strcpy (lfm_user, deadbeef->conf_get_str ("lastfm.login", "")); + strcpy (lfm_pass, deadbeef->conf_get_str ("lastfm.password", "")); return 0; } |