2010-02-17 21:52:19 +00:00
|
|
|
/* -*- mode: c++; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
|
|
|
|
* vim: ts=4 sw=4 noet ai cindent syntax=cpp
|
|
|
|
*
|
|
|
|
* Conky, a system monitor, based on torsmo
|
|
|
|
*
|
|
|
|
* Please see COPYING for details
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Pavel Labath et al.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "setting.hh"
|
|
|
|
|
|
|
|
namespace conky {
|
|
|
|
namespace priv {
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
* object is constructed. Therefore, we create it on the first call to
|
|
|
|
* config_setting_base constructor.
|
|
|
|
*/
|
|
|
|
config_settings_t *config_settings;
|
|
|
|
|
2010-02-25 20:11:39 +00:00
|
|
|
config_setting_base::config_setting_base(const std::string &name_)
|
|
|
|
: name(name_)
|
2010-02-17 21:52:19 +00:00
|
|
|
{
|
|
|
|
struct config_settings_constructor {
|
|
|
|
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)
|
|
|
|
throw std::logic_error("Setting with name '" + name + "' already registered");
|
|
|
|
}
|
2010-02-24 19:27:16 +00:00
|
|
|
|
|
|
|
void config_setting_base::lua_set(lua::state &l)
|
|
|
|
{
|
2010-02-25 15:24:50 +00:00
|
|
|
lua::stack_sentry s(l, -1);
|
2010-02-24 19:27:16 +00:00
|
|
|
l.checkstack(2);
|
|
|
|
|
2010-02-25 15:24:50 +00:00
|
|
|
l.getglobal("conky");
|
|
|
|
l.rawgetfield(-1, "config");
|
|
|
|
l.replace(-2);
|
2010-02-24 19:27:16 +00:00
|
|
|
l.insert(-2);
|
2010-02-25 15:24:50 +00:00
|
|
|
|
|
|
|
l.pushstring(name.c_str());
|
2010-02-24 19:27:16 +00:00
|
|
|
l.insert(-2);
|
2010-02-25 15:24:50 +00:00
|
|
|
|
|
|
|
l.settable(-3);
|
|
|
|
l.pop();
|
2010-02-24 19:27:16 +00:00
|
|
|
}
|
2010-02-17 21:52:19 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Performs the actual assignment of settings. Calls the setting-specific setter after
|
|
|
|
* some sanity-checking.
|
|
|
|
* stack on entry: | ..., new_config_table, key, value, old_value |
|
|
|
|
* stack on exit: | ..., new_config_table, key |
|
|
|
|
*/
|
2010-02-25 20:11:39 +00:00
|
|
|
void config_setting_base::process_setting(lua::state &l, bool init)
|
2010-02-17 21:52:19 +00:00
|
|
|
{
|
2010-02-25 15:24:50 +00:00
|
|
|
lua::stack_sentry s(l, -2);
|
2010-02-17 21:52:19 +00:00
|
|
|
|
2010-02-25 15:47:00 +00:00
|
|
|
lua::Type type = l.type(-3);
|
2010-02-17 21:52:19 +00:00
|
|
|
if(type != lua::TSTRING) {
|
|
|
|
NORM_ERR("invalid setting of type '%s'", l.type_name(type));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string name = l.tostring(-3);
|
|
|
|
auto iter = priv::config_settings->find(name);
|
|
|
|
if(iter == priv::config_settings->end()) {
|
|
|
|
NORM_ERR("Unknown setting '%s'", name.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-25 20:11:39 +00:00
|
|
|
iter->second->call_lua_setter(&l, init);
|
2010-02-25 15:24:50 +00:00
|
|
|
l.pushvalue(-2);
|
2010-02-17 21:52:19 +00:00
|
|
|
l.insert(-2);
|
2010-02-25 15:24:50 +00:00
|
|
|
l.rawset(-4);
|
2010-02-17 21:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called when user sets a new value for a setting
|
|
|
|
* stack on entry: | config_table, key, value |
|
|
|
|
* stack on exit: | |
|
|
|
|
*/
|
2010-02-25 20:11:39 +00:00
|
|
|
int config_setting_base::config__newindex(lua::state *l)
|
2010-02-17 21:52:19 +00:00
|
|
|
{
|
2010-02-25 15:24:50 +00:00
|
|
|
lua::stack_sentry s(*l, -3);
|
2010-02-17 21:52:19 +00:00
|
|
|
l->checkstack(1);
|
|
|
|
|
|
|
|
l->getmetatable(-3);
|
|
|
|
l->replace(-4);
|
|
|
|
|
2010-02-25 15:24:50 +00:00
|
|
|
l->pushvalue(-2);
|
|
|
|
l->rawget(-4);
|
|
|
|
process_setting(*l, false);
|
2010-02-17 21:52:19 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called after the initial loading of the config file. Performs the initial assignments.
|
|
|
|
* at least one setting should always be registered, so config_settings will not be null
|
|
|
|
* stack on entry: | ... |
|
|
|
|
* stack on exit: | ... |
|
|
|
|
*/
|
|
|
|
void check_config_settings(lua::state &l)
|
|
|
|
{
|
|
|
|
lua::stack_sentry s(l);
|
|
|
|
l.checkstack(6);
|
|
|
|
|
2010-02-25 15:24:50 +00:00
|
|
|
l.getglobal("conky"); {
|
|
|
|
l.rawgetfield(-1, "config"); {
|
2010-02-17 21:52:19 +00:00
|
|
|
if(l.type(-1) != lua::TTABLE)
|
|
|
|
throw std::runtime_error("conky.config must be a table");
|
|
|
|
|
|
|
|
// new conky.config table, containing only valid settings
|
2010-02-25 15:24:50 +00:00
|
|
|
l.newtable(); {
|
|
|
|
l.pushnil();
|
|
|
|
while(l.next(-3)) {
|
|
|
|
l.pushnil();
|
2010-02-25 20:11:39 +00:00
|
|
|
priv::config_setting_base::process_setting(l, true);
|
2010-02-25 15:24:50 +00:00
|
|
|
}
|
|
|
|
} l.replace(-2);
|
2010-02-17 21:52:19 +00:00
|
|
|
|
|
|
|
l.pushboolean(false);
|
|
|
|
l.rawsetfield(-2, "__metatable");
|
|
|
|
|
|
|
|
l.pushvalue(-1);
|
|
|
|
l.rawsetfield(-2, "__index");
|
|
|
|
|
2010-02-25 20:11:39 +00:00
|
|
|
l.pushfunction(&priv::config_setting_base::config__newindex);
|
2010-02-17 21:52:19 +00:00
|
|
|
l.rawsetfield(-2, "__newindex");
|
|
|
|
|
|
|
|
// conky.config will not be a table, but a userdata with some metamethods
|
|
|
|
// 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
|
2010-02-25 15:24:50 +00:00
|
|
|
l.newuserdata(1);
|
2010-02-17 21:52:19 +00:00
|
|
|
l.insert(-2);
|
2010-02-25 15:24:50 +00:00
|
|
|
l.setmetatable(-2);
|
|
|
|
} l.rawsetfield(-2, "config");
|
|
|
|
} l.pop();
|
2010-02-17 21:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////// example settings, remove after real settings are available ///////
|
2010-02-27 14:15:25 +00:00
|
|
|
config_setting<int, range_checking_accessors<int>> asdf("asdf", range_checking_accessors<int>(42, 47, 45, true));
|
2010-02-17 21:52:19 +00:00
|
|
|
}
|