From 26cb39f67c0855ef353a13e522f0fe69ce5a206b Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Sun, 29 Aug 2010 15:45:38 +0200 Subject: [PATCH] Make lua_load a lua setting --- src/conky.cc | 14 +---------- src/llua.cc | 67 +++++++++++++++++++++++++++++++++++++++++----------- src/llua.h | 4 ---- 3 files changed, 54 insertions(+), 31 deletions(-) diff --git a/src/conky.cc b/src/conky.cc index aaebe46d..4ab9bcbc 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -2566,7 +2566,6 @@ void clean_up_without_threads(void *memtofree1, void* memtofree2) #endif #ifdef BUILD_LUA llua_shutdown_hook(); - llua_close(); #endif /* BUILD_LUA */ #if defined BUILD_WEATHER_XOAP || defined BUILD_RSS xmlCleanupParser(); @@ -2897,17 +2896,6 @@ char load_config_file(const char *f) break; } #ifdef BUILD_LUA - CONF("lua_load") { - if (value) { - char *ptr = strtok(value, " "); - while (ptr) { - llua_load(ptr); - ptr = strtok(NULL, " "); - } - } else { - CONF_ERR; - } - } #ifdef BUILD_X11 CONF("lua_draw_hook_pre") { if (value) { @@ -3390,7 +3378,7 @@ int main(int argc, char **argv) "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" - " mpd_port=-47};\n" + " lua_load='asdf qq/q'};\n" ); l.call(0, 0); conky::set_config_settings(l); diff --git a/src/llua.cc b/src/llua.cc index 13749153..6aed0606 100644 --- a/src/llua.cc +++ b/src/llua.cc @@ -42,6 +42,8 @@ void llua_rm_notifies(void); static int llua_block_notify = 0; #endif /* HAVE_SYS_INOTIFY_H */ +static void llua_load(const char *script); + #define MIN(a, b) ( (a) < (b) ? (a) : (b) ) static char *draw_pre_hook = 0; @@ -51,6 +53,57 @@ static char *shutdown_hook = 0; lua_State *lua_L = NULL; +namespace { + class lua_load_setting: public conky::simple_config_setting { + typedef conky::simple_config_setting Base; + + protected: + void lua_setter(lua::state &l, bool init) + { + lua::stack_sentry s(l, -2); + + Base::lua_setter(l, init); + + if(init) { + std::string files = do_convert(l, -1).first; + while(not files.empty()) { + std::string::size_type pos = files.find(' '); + if(pos > 0) { + std::string file(files, 0, pos); + llua_load(file.c_str()); + } + files.erase(0, pos==std::string::npos ? pos : pos+1); + } + } + + ++s; + } + + void cleanup(lua::state &l) + { + lua::stack_sentry s(l, -1); + +#ifdef HAVE_SYS_INOTIFY_H + llua_rm_notifies(); +#endif /* HAVE_SYS_INOTIFY_H */ + free_and_zero(draw_pre_hook); + free_and_zero(draw_post_hook); + free_and_zero(startup_hook); + free_and_zero(shutdown_hook); + if(!lua_L) return; + lua_close(lua_L); + lua_L = NULL; + } + + public: + lua_load_setting() + : Base("lua_load", std::string(), false) + {} + }; + + lua_load_setting lua_load; +} + static int llua_conky_parse(lua_State *L) { int n = lua_gettop(L); /* number of arguments */ @@ -319,20 +372,6 @@ static int llua_getnumber(const char *args, double *ret) return 0; } -void llua_close(void) -{ -#ifdef HAVE_SYS_INOTIFY_H - llua_rm_notifies(); -#endif /* HAVE_SYS_INOTIFY_H */ - free_and_zero(draw_pre_hook); - free_and_zero(draw_post_hook); - free_and_zero(startup_hook); - free_and_zero(shutdown_hook); - if(!lua_L) return; - lua_close(lua_L); - lua_L = NULL; -} - #ifdef HAVE_SYS_INOTIFY_H struct _lua_notify_s { int wd; diff --git a/src/llua.h b/src/llua.h index e523532f..67a64450 100644 --- a/src/llua.h +++ b/src/llua.h @@ -39,10 +39,6 @@ extern "C" { #define LUAPREFIX "conky_" -/* load a lua script */ -void llua_load(const char *script); -/* close lua stuff */ -void llua_close(void); #ifdef HAVE_SYS_INOTIFY_H /* check our lua inotify status */ void llua_inotify_query(int wd, int mask);