From 663e99ee53e7a3d6e4ae737c0c1b4607c232e6c6 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Sun, 22 Aug 2010 12:17:09 +0200 Subject: [PATCH] Make mpd_{host,password,port} lua settings --- src/conky.cc | 63 ++------------------------ src/mpd.cc | 124 +++++++++++++++++++++++++++++++++++---------------- src/mpd.h | 6 --- 3 files changed, 89 insertions(+), 104 deletions(-) diff --git a/src/conky.cc b/src/conky.cc index 845b6dea..45c5f548 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -2592,10 +2592,6 @@ void clean_up(void *memtofree1, void* memtofree2) static void set_default_configurations(void) { -#ifdef BUILD_MPD - char *mpd_env_host; - char *mpd_env_port; -#endif /* BUILD_MPD */ update_uname(); total_run_times = 0; info.cpu_avg_samples = 2; @@ -2609,39 +2605,6 @@ static void set_default_configurations(void) top_io = 0; #endif top_running = 0; -#ifdef BUILD_MPD - mpd_env_host = getenv("MPD_HOST"); - mpd_env_port = getenv("MPD_PORT"); - - if (!mpd_env_host || !strlen(mpd_env_host)) { - mpd_set_host("localhost"); - } else { - /* MPD_HOST environment variable is set */ - char *mpd_hostpart = strchr(mpd_env_host, '@'); - if (!mpd_hostpart) { - mpd_set_host(mpd_env_host); - } else { - /* MPD_HOST contains a password */ - char *mpd_password = (char *)malloc(mpd_hostpart - mpd_env_host + 1); - snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host); - - if (!strlen(mpd_hostpart + 1)) { - mpd_set_host("localhost"); - } else { - mpd_set_host(mpd_hostpart + 1); - } - - mpd_set_password(mpd_password, 1); - free(mpd_password); - } - } - - - if (!mpd_env_port || mpd_set_port(mpd_env_port)) { - /* failed to set port from environment variable */ - mpd_set_port("6600"); - } -#endif /* BUILD_MPD */ #ifdef BUILD_XMMS2 info.xmms2.artist = NULL; info.xmms2.album = NULL; @@ -2898,27 +2861,6 @@ char load_config_file(const char *f) } } #endif -#ifdef BUILD_MPD - CONF("mpd_host") { - if (value) { - mpd_set_host(value); - } else { - CONF_ERR; - } - } - CONF("mpd_port") { - if (value && mpd_set_port(value)) { - CONF_ERR; - } - } - CONF("mpd_password") { - if (value) { - mpd_set_password(value, 0); - } else { - CONF_ERR; - } - } -#endif /* BUILD_MPD */ CONF("music_player_interval") { if (value) { info.music_player_interval = strtod(value, 0); @@ -3725,7 +3667,8 @@ int main(int argc, char **argv) "print(conky.variables.asdf{}.xxx);\n" "conky.config = { alignment='top_left', asdf=47, [42]=47, out_to_x=true,\n" " own_window_hints='above, skip_taskbar',\n" - " background_colour='pink', own_window=true, double_buffer=true};\n" + " background_colour='pink', own_window=true, double_buffer=true,\n" + " mpd_host='asdf'};\n" ); l.call(0, 0); conky::set_config_settings(l); @@ -3741,6 +3684,8 @@ int main(int argc, char **argv) "conky.config.alignment='asdf';\n" "print('config.alignment = ', conky.config.alignment);\n" "print('config.own_window_hints = ', conky.config.own_window_hints);\n" + "print('config.mpd_host = ', conky.config.mpd_host);\n" + "print('config.mpd_password = ', conky.config.mpd_password);\n" ); l.call(0, 0); diff --git a/src/mpd.cc b/src/mpd.cc index 8a6ad577..3075db66 100644 --- a/src/mpd.cc +++ b/src/mpd.cc @@ -35,13 +35,89 @@ #include "libmpdclient.h" #include "mpd.h" -/* server connection data */ -static char mpd_host[128]; -static char mpd_password[128]; -static int mpd_port; +namespace { -/* this is >0 if the current password was set from MPD_HOST */ -static int mpd_environment_password = 0; + /* this is true if the current host was set from MPD_HOST */ + bool mpd_environment_host = false; + + class mpd_host_setting: public conky::simple_config_setting { + typedef conky::simple_config_setting Base; + + protected: + virtual void lua_setter(lua::state &l, bool init); + + public: + mpd_host_setting() + : Base("mpd_host", "localhost", false) + {} + }; + + void mpd_host_setting::lua_setter(lua::state &l, bool init) + { + lua::stack_sentry s(l, -2); + + if(l.isnil(-2)) { + // get the value from environment + mpd_environment_host = true; + const char *t = getenv("MPD_HOST"); + if(t) { + l.checkstack(1); + const char *h = strchr(t, '@'); + if(h) { + if(h[1]) + l.pushstring(h+1); + } else + l.pushstring(t); + l.replace(-3); + } + + } + + Base::lua_setter(l, init); + + ++s; + } + + class mpd_password_setting: public conky::simple_config_setting { + typedef conky::simple_config_setting Base; + + protected: + virtual void lua_setter(lua::state &l, bool init); + + public: + mpd_password_setting() + : Base("mpd_password", std::string(), false) + {} + }; + + void mpd_password_setting::lua_setter(lua::state &l, bool init) + { + lua::stack_sentry s(l, -2); + + /* for security, dont use environment password when user specifies host in config */ + if(l.isnil(-2) && mpd_environment_host) { + // get the value from environment + const char *t = getenv("MPD_HOST"); + if(t) { + const char *p = strchr(t, '@'); + if(p) { + l.checkstack(1); + l.pushstring(t, p-t); + l.replace(-3); + } + } + + } + + Base::lua_setter(l, init); + + ++s; + } + + conky::range_config_setting mpd_port("mpd_port", 1, 65535, 6600, false); + mpd_host_setting mpd_host; + mpd_password_setting mpd_password; +} /* global mpd information */ static struct { @@ -65,36 +141,6 @@ static struct { /* number of users of the above struct */ static int refcount = 0; -void mpd_set_host(const char *host) -{ - snprintf(mpd_host, 128, "%s", host); - - if (mpd_environment_password) { - /* for security, dont use environment password when user specifies host in config */ - mpd_clear_password(); - } -} -void mpd_set_password(const char *password, int from_environment) -{ - snprintf(mpd_password, 128, "%s", password); - mpd_environment_password = from_environment; -} -void mpd_clear_password(void) -{ - *mpd_password = '\0'; - mpd_environment_password = 0; -} -int mpd_set_port(const char *port) -{ - int val; - - val = strtol(port, 0, 0); - if (val < 1 || val > 0xffff) - return 1; - mpd_port = val; - return 0; -} - void init_mpd(void) { if (!(refcount++)) /* first client */ @@ -161,10 +207,10 @@ bool mpd_process(thread_handle &handle) do { if (!conn) - conn = mpd_newConnection(mpd_host, mpd_port, 10); + conn = mpd_newConnection(mpd_host.get(*state).c_str(), mpd_port.get(*state), 10); - if (*mpd_password) { - mpd_sendPasswordCommand(conn, mpd_password); + if (mpd_password.get(*state).size()) { + mpd_sendPasswordCommand(conn, mpd_password.get(*state).c_str()); mpd_finishCommand(conn); } diff --git a/src/mpd.h b/src/mpd.h index c2506a84..9ec796e4 100644 --- a/src/mpd.h +++ b/src/mpd.h @@ -26,12 +26,6 @@ #ifndef MPD_H_ #define MPD_H_ -/* functions for setting the configuration values */ -void mpd_set_host(const char *); -void mpd_set_password(const char *, int); -void mpd_clear_password(void); -int mpd_set_port(const char *); - /* text object functions */ void init_mpd(void); void free_mpd(struct text_object *);