mirror of
https://github.com/Llewellynvdm/conky.git
synced 2024-12-28 13:00:45 +00:00
Change setting setter call order, add cleanup function
Call setting setters in the order in which they were registered. Add a cleanup function, called in the reverse registration order.
This commit is contained in:
parent
1d20229d74
commit
ba79c58f37
@ -3983,7 +3983,7 @@ int main(int argc, char **argv)
|
|||||||
" own_window_hints='above, undecorated,,below'};\n"
|
" own_window_hints='above, undecorated,,below'};\n"
|
||||||
);
|
);
|
||||||
l.call(0, 0);
|
l.call(0, 0);
|
||||||
conky::check_config_settings(l);
|
conky::set_config_settings(l);
|
||||||
std::cout << "config.alignment = " << text_alignment.get(l) << std::endl;
|
std::cout << "config.alignment = " << text_alignment.get(l) << std::endl;
|
||||||
l.pushstring("X");
|
l.pushstring("X");
|
||||||
text_alignment.lua_set(l);
|
text_alignment.lua_set(l);
|
||||||
|
@ -70,7 +70,7 @@ namespace conky {
|
|||||||
int data_source_astext(lua::state *l)
|
int data_source_astext(lua::state *l)
|
||||||
{
|
{
|
||||||
std::string x = get_data_source(l).get_text();
|
std::string x = get_data_source(l).get_text();
|
||||||
l->pushstring(x.c_str());
|
l->pushstring(x);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
194
src/setting.cc
194
src/setting.cc
@ -25,26 +25,82 @@
|
|||||||
|
|
||||||
#include "setting.hh"
|
#include "setting.hh"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
namespace conky {
|
namespace conky {
|
||||||
namespace priv {
|
|
||||||
|
namespace {
|
||||||
|
typedef std::unordered_map<std::string, priv::config_setting_base *> settings_map;
|
||||||
|
typedef std::vector<priv::config_setting_base *> settings_vector;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* We cannot construct this object statically, because order of object construction in
|
* We cannot construct this object statically, because order of object construction in
|
||||||
* different modules is not defined, so config_setting_base could be called before this
|
* different modules is not defined, so config_setting_base could be called before this
|
||||||
* object is constructed. Therefore, we create it on the first call to
|
* object is constructed. Therefore, we create it on the first call to
|
||||||
* config_setting_base constructor.
|
* config_setting_base constructor.
|
||||||
*/
|
*/
|
||||||
config_settings_t *config_settings;
|
settings_map *settings;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Returns the setting record corresponding to the value at the specified index. If the
|
||||||
|
* value is not valid, returns NULL and prints an error.
|
||||||
|
*/
|
||||||
|
priv::config_setting_base* get_setting(lua::state &l, int index)
|
||||||
|
{
|
||||||
|
lua::Type type = l.type(index);
|
||||||
|
if(type != lua::TSTRING) {
|
||||||
|
NORM_ERR("invalid setting of type '%s'", l.type_name(type));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string &name = l.tostring(index);
|
||||||
|
auto iter = settings->find(name);
|
||||||
|
if(iter == settings->end()) {
|
||||||
|
NORM_ERR("Unknown setting '%s'", name.c_str());
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return iter->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns a vector of all settings, sorted in order of registration
|
||||||
|
settings_vector make_settings_vector()
|
||||||
|
{
|
||||||
|
settings_vector ret;
|
||||||
|
ret.reserve(settings->size());
|
||||||
|
|
||||||
|
for(auto i = settings->begin(); i != settings->end(); ++i)
|
||||||
|
ret.push_back(i->second);
|
||||||
|
sort(ret.begin(), ret.end(), &priv::config_setting_base::seq_compare);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Returns the seq_no for the new setting object. Also constructs settings object
|
||||||
|
* if needed.
|
||||||
|
*/
|
||||||
|
size_t get_next_seq_no()
|
||||||
|
{
|
||||||
|
struct settings_constructor {
|
||||||
|
settings_constructor() { settings = new settings_map; }
|
||||||
|
~settings_constructor() { delete settings; settings = NULL; }
|
||||||
|
};
|
||||||
|
static settings_constructor constructor;
|
||||||
|
|
||||||
|
return settings->size();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace priv {
|
||||||
|
|
||||||
config_setting_base::config_setting_base(const std::string &name_)
|
config_setting_base::config_setting_base(const std::string &name_)
|
||||||
: name(name_)
|
: name(name_), seq_no(get_next_seq_no())
|
||||||
{
|
{
|
||||||
struct config_settings_constructor {
|
bool inserted = settings->insert({name, this}).second;
|
||||||
config_settings_constructor() { priv::config_settings = new config_settings_t; }
|
|
||||||
~config_settings_constructor() { delete config_settings; config_settings = NULL; }
|
|
||||||
};
|
|
||||||
static config_settings_constructor constructor;
|
|
||||||
|
|
||||||
bool inserted = config_settings->insert({name, this}).second;
|
|
||||||
if(not inserted)
|
if(not inserted)
|
||||||
throw std::logic_error("Setting with name '" + name + "' already registered");
|
throw std::logic_error("Setting with name '" + name + "' already registered");
|
||||||
}
|
}
|
||||||
@ -59,10 +115,7 @@ namespace conky {
|
|||||||
l.replace(-2);
|
l.replace(-2);
|
||||||
l.insert(-2);
|
l.insert(-2);
|
||||||
|
|
||||||
l.pushstring(name.c_str());
|
l.setfield(-2, name.c_str());
|
||||||
l.insert(-2);
|
|
||||||
|
|
||||||
l.settable(-3);
|
|
||||||
l.pop();
|
l.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,26 +123,17 @@ namespace conky {
|
|||||||
* Performs the actual assignment of settings. Calls the setting-specific setter after
|
* Performs the actual assignment of settings. Calls the setting-specific setter after
|
||||||
* some sanity-checking.
|
* some sanity-checking.
|
||||||
* stack on entry: | ..., new_config_table, key, value, old_value |
|
* stack on entry: | ..., new_config_table, key, value, old_value |
|
||||||
* stack on exit: | ..., new_config_table, key |
|
* stack on exit: | ..., new_config_table |
|
||||||
*/
|
*/
|
||||||
void config_setting_base::process_setting(lua::state &l, bool init)
|
void config_setting_base::process_setting(lua::state &l, bool init)
|
||||||
{
|
{
|
||||||
lua::stack_sentry s(l, -2);
|
lua::stack_sentry s(l, -3);
|
||||||
|
|
||||||
lua::Type type = l.type(-3);
|
config_setting_base *ptr = get_setting(l, -3);
|
||||||
if(type != lua::TSTRING) {
|
if(not ptr)
|
||||||
NORM_ERR("invalid setting of type '%s'", l.type_name(type));
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
std::string name = l.tostring(-3);
|
ptr->lua_setter(l, init);
|
||||||
auto iter = priv::config_settings->find(name);
|
|
||||||
if(iter == priv::config_settings->end()) {
|
|
||||||
NORM_ERR("Unknown setting '%s'", name.c_str());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
iter->second->lua_setter(l, init);
|
|
||||||
l.pushvalue(-2);
|
l.pushvalue(-2);
|
||||||
l.insert(-2);
|
l.insert(-2);
|
||||||
l.rawset(-4);
|
l.rawset(-4);
|
||||||
@ -114,33 +158,22 @@ namespace conky {
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Called after the initial loading of the config file. Performs the initial assignments.
|
* conky.config will not be a table, but a userdata with some metamethods we do this
|
||||||
* at least one setting should always be registered, so config_settings will not be null
|
* because we want to control access to the settings we use the metatable for storing the
|
||||||
|
* settings, that means having a setting whose name starts with "__" is a bad idea
|
||||||
* stack on entry: | ... |
|
* stack on entry: | ... |
|
||||||
* stack on exit: | ... |
|
* stack on exit: | ... new_config_table |
|
||||||
*/
|
*/
|
||||||
void check_config_settings(lua::state &l)
|
void config_setting_base::make_conky_config(lua::state &l)
|
||||||
{
|
{
|
||||||
lua::stack_sentry s(l);
|
lua::stack_sentry s(l);
|
||||||
l.checkstack(6);
|
l.checkstack(3);
|
||||||
|
|
||||||
l.getglobal("conky"); {
|
l.newuserdata(1);
|
||||||
l.rawgetfield(-1, "config"); {
|
|
||||||
if(l.type(-1) != lua::TTABLE)
|
|
||||||
throw std::runtime_error("conky.config must be a table");
|
|
||||||
|
|
||||||
// new conky.config table, containing only valid settings
|
|
||||||
l.newtable(); {
|
l.newtable(); {
|
||||||
l.pushnil();
|
|
||||||
while(l.next(-3)) {
|
|
||||||
l.pushnil();
|
|
||||||
priv::config_setting_base::process_setting(l, true);
|
|
||||||
}
|
|
||||||
} l.replace(-2);
|
|
||||||
|
|
||||||
l.pushboolean(false);
|
l.pushboolean(false);
|
||||||
l.rawsetfield(-2, "__metatable");
|
l.rawsetfield(-2, "__metatable");
|
||||||
|
|
||||||
@ -149,16 +182,69 @@ namespace conky {
|
|||||||
|
|
||||||
l.pushfunction(&priv::config_setting_base::config__newindex);
|
l.pushfunction(&priv::config_setting_base::config__newindex);
|
||||||
l.rawsetfield(-2, "__newindex");
|
l.rawsetfield(-2, "__newindex");
|
||||||
|
} l.setmetatable(-2);
|
||||||
|
|
||||||
// conky.config will not be a table, but a userdata with some metamethods
|
++s;
|
||||||
// we do this because we want to control access to the settings
|
}
|
||||||
// we use the metatable for storing the settings, that means having a setting
|
}
|
||||||
// whose name stars with "__" is a bad idea
|
|
||||||
l.newuserdata(1);
|
void set_config_settings(lua::state &l)
|
||||||
l.insert(-2);
|
{
|
||||||
l.setmetatable(-2);
|
lua::stack_sentry s(l);
|
||||||
} l.rawsetfield(-2, "config");
|
l.checkstack(6);
|
||||||
|
|
||||||
|
// Force creation of settings map. In the off chance we have no settings.
|
||||||
|
get_next_seq_no();
|
||||||
|
|
||||||
|
l.getglobal("conky"); {
|
||||||
|
if(l.type(-1) != lua::TTABLE)
|
||||||
|
throw std::runtime_error("conky must be a table");
|
||||||
|
|
||||||
|
l.rawgetfield(-1, "config"); {
|
||||||
|
if(l.type(-1) != lua::TTABLE)
|
||||||
|
throw std::runtime_error("conky.config must be a table");
|
||||||
|
|
||||||
|
priv::config_setting_base::make_conky_config(l);
|
||||||
|
l.rawsetfield(-3, "config");
|
||||||
|
|
||||||
|
l.rawgetfield(-2, "config"); l.getmetatable(-1); l.replace(-2); {
|
||||||
|
const settings_vector &v = make_settings_vector();
|
||||||
|
|
||||||
|
for(size_t i = 0; i < v.size(); ++i) {
|
||||||
|
l.pushstring(v[i]->name);
|
||||||
|
l.rawgetfield(-3, v[i]->name.c_str());
|
||||||
|
l.pushnil();
|
||||||
|
priv::config_setting_base::process_setting(l, true);
|
||||||
|
}
|
||||||
} l.pop();
|
} l.pop();
|
||||||
|
|
||||||
|
// print error messages for unknown settings
|
||||||
|
l.pushnil();
|
||||||
|
while(l.next(-2)) {
|
||||||
|
l.pop();
|
||||||
|
get_setting(l, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
} l.pop();
|
||||||
|
} l.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
void cleanup_config_settings(lua::state &l)
|
||||||
|
{
|
||||||
|
lua::stack_sentry s(l);
|
||||||
|
l.checkstack(2);
|
||||||
|
|
||||||
|
l.getglobal("conky");
|
||||||
|
l.rawgetfield(-1, "config");
|
||||||
|
l.replace(-2);
|
||||||
|
|
||||||
|
const settings_vector &v = make_settings_vector();
|
||||||
|
for(size_t i = v.size(); i > 0; --i) {
|
||||||
|
l.getfield(-1, v[i-1]->name.c_str());
|
||||||
|
v[i-1]->cleanup(l);
|
||||||
|
}
|
||||||
|
|
||||||
|
l.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
/////////// example settings, remove after real settings are available ///////
|
/////////// example settings, remove after real settings are available ///////
|
||||||
|
@ -27,14 +27,27 @@
|
|||||||
#include <limits>
|
#include <limits>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <unordered_map>
|
|
||||||
|
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include "luamm.hh"
|
#include "luamm.hh"
|
||||||
|
|
||||||
namespace conky {
|
namespace conky {
|
||||||
|
|
||||||
void check_config_settings(lua::state &l);
|
/*
|
||||||
|
* Checks settings, and does initial calls to the setters.
|
||||||
|
* Should be called after reading the user config.
|
||||||
|
* stack on entry: | ... |
|
||||||
|
* stack on exit: | ... |
|
||||||
|
*/
|
||||||
|
void set_config_settings(lua::state &l);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Calls cleanup functions.
|
||||||
|
* Should be called before exit/restart.
|
||||||
|
* stack on entry: | ... |
|
||||||
|
* stack on exit: | ... |
|
||||||
|
*/
|
||||||
|
void cleanup_config_settings(lua::state &l);
|
||||||
|
|
||||||
template<typename T,
|
template<typename T,
|
||||||
bool is_integral = std::is_integral<T>::value,
|
bool is_integral = std::is_integral<T>::value,
|
||||||
@ -120,6 +133,7 @@ namespace conky {
|
|||||||
private:
|
private:
|
||||||
static void process_setting(lua::state &l, bool init);
|
static void process_setting(lua::state &l, bool init);
|
||||||
static int config__newindex(lua::state *l);
|
static int config__newindex(lua::state *l);
|
||||||
|
static void make_conky_config(lua::state &l);
|
||||||
|
|
||||||
// copying is a REALLY bad idea
|
// copying is a REALLY bad idea
|
||||||
config_setting_base(const config_setting_base &) = delete;
|
config_setting_base(const config_setting_base &) = delete;
|
||||||
@ -134,8 +148,19 @@ namespace conky {
|
|||||||
*/
|
*/
|
||||||
virtual void lua_setter(lua::state &l, bool init) = 0;
|
virtual void lua_setter(lua::state &l, bool init) = 0;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Called on exit/restart.
|
||||||
|
* stack on entry: | ... new_value |
|
||||||
|
* stack on exit: | ... |
|
||||||
|
*/
|
||||||
|
virtual void cleanup(lua::state &l) { l.pop(); }
|
||||||
|
|
||||||
public:
|
public:
|
||||||
const std::string name;
|
const std::string name;
|
||||||
|
const size_t seq_no;
|
||||||
|
|
||||||
|
static bool seq_compare(const config_setting_base *a, const config_setting_base *b)
|
||||||
|
{ return a->seq_no < b->seq_no; }
|
||||||
|
|
||||||
explicit config_setting_base(const std::string &name_);
|
explicit config_setting_base(const std::string &name_);
|
||||||
virtual ~config_setting_base() {}
|
virtual ~config_setting_base() {}
|
||||||
@ -147,12 +172,9 @@ namespace conky {
|
|||||||
*/
|
*/
|
||||||
void lua_set(lua::state &l);
|
void lua_set(lua::state &l);
|
||||||
|
|
||||||
friend void conky::check_config_settings(lua::state &l);
|
friend void conky::set_config_settings(lua::state &l);
|
||||||
|
friend void conky::cleanup_config_settings(lua::state &l);
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef std::unordered_map<std::string, config_setting_base *> config_settings_t;
|
|
||||||
|
|
||||||
extern config_settings_t *config_settings;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If you need some very exotic setting, derive it from this class. Otherwise, scroll down.
|
// If you need some very exotic setting, derive it from this class. Otherwise, scroll down.
|
||||||
|
Loading…
Reference in New Issue
Block a user