1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-16 01:57:09 +00:00

export functions for accessing data sources to lua

this adds some code to main() which tests these features and then exits, so it's normal that
conky doesn't work anymore.
This commit is contained in:
Pavel Labath 2010-02-12 18:39:01 +01:00
parent 00d8215e00
commit f59a198a04
4 changed files with 187 additions and 1 deletions

View File

@ -38,7 +38,7 @@ set(conky_sources colours.cc combine.cc common.cc conky.cc core.cc
diskio.cc entropy.cc exec.cc fs.cc mail.cc mixer.cc net_stat.cc template.cc
mboxscan.cc read_tcp.cc scroll.cc specials.cc tailhead.cc
temphelper.cc text_object.cc timeinfo.cc top.cc algebra.cc prioqueue.c proc.cc
user.cc luamm.cc data-source.cc)
user.cc luamm.cc data-source.cc lua-config.cc)
# add timed thread library
add_library(timed-thread timed-thread.cc)

View File

@ -94,6 +94,9 @@
#include "timeinfo.h"
#include "top.h"
#include <iostream>
#include "lua-config.hh"
/* check for OS and include appropriate headers */
#if defined(__linux__)
#include "linux.h"
@ -3945,6 +3948,9 @@ static const char *getopt_string = "vVqdDt:u:i:hc:p:"
#endif
;
int printme(lua::state *, const char *str)
{ fprintf(stderr, str); return 0;}
static const struct option longopts[] = {
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'V' },
@ -4302,6 +4308,53 @@ int main(int argc, char **argv)
}
}
//////////// XXX ////////////////////////////////
lua::state l;
try {
conky::export_symbols(l);
l.loadstring(
"print(conky.asnumber(conky.variables.asdf{}));\n"
"print(conky.astext(conky.variables.asdf{}));\n"
"print(conky.asnumber(conky.variables.zxcv{}));\n"
);
l.call(0, 0);
}
catch(std::exception &e) {
std::cerr << "caught exception: " << e.what() << std::endl;
}
#if 0
try {
lua::stack_sentry s(l);
l.pushvalue(lua::GLOBALSINDEX); ++s;
l.pushnil(); ++s;
while(l.next(-2)) {
lua::stack_sentry s2(l, 1);
if(l.isnumber(-2))
std::cout << l.tonumber(-2);
else if(l.isstring(-2))
std::cout << '"' << l.tostring(-2) << '"';
else
std::cout << l.type_name(l.type(-2));
std::cout << " -> ";
if(l.isnumber(-1))
std::cout << l.tonumber(-1);
else if(l.isstring(-1))
std::cout << '"' << l.tostring(-1) << '"';
else
std::cout << l.type_name(l.type(-1));
std::cout << std::endl;
}
}
catch(std::exception &e) {
std::cerr << "caught exception: " << e.what() << std::endl;
}
#endif
free(current_config);
return 0;
//////////// XXX ////////////////////////////////
#ifdef BUILD_WEATHER_XOAP
/* Load xoap keys, if existing */
load_xoap_keys();

100
src/lua-config.cc Normal file
View File

@ -0,0 +1,100 @@
/* -*- 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 "lua-config.hh"
#include "data-source.hh"
namespace conky {
namespace {
const char data_source_metatable[] = "conky::data_source_metatable";
data_source_base& get_data_source(lua::state *l)
{
if(l->gettop() != 1)
throw std::runtime_error("Wrong number of parameters");
l->rawgetfield(lua::REGISTRYINDEX, data_source_metatable);
if(not l->getmetatable(-2) or not l->rawequal(-1, -2))
throw std::runtime_error("Invalid parameter");
return **static_cast<std::shared_ptr<data_source_base> *>(l->touserdata(1));
}
int data_source_asnumber(lua::state *l)
{
double x = get_data_source(l).get_number();
l->pushnumber(x);
return 1;
}
int data_source_astext(lua::state *l)
{
std::string x = get_data_source(l).get_text();
l->pushstring(x.c_str());
return 1;
}
int create_data_source(lua::state *l, const data_sources_t::value_type &v)
{
l->createuserdata<std::shared_ptr<data_source_base>>(v.second(*l, v.first));
l->rawgetfield(lua::REGISTRYINDEX, data_source_metatable);
l->setmetatable(-2);
return 1;
}
}
void export_symbols(lua::state &l)
{
lua::stack_sentry s(l);
l.checkstack(3);
l.newmetatable(data_source_metatable); ++s; {
l.pushboolean(false); ++s;
l.rawsetfield(-2, "__metatable"); --s;
l.pushdestructor<std::shared_ptr<data_source_base>>(); ++s;
l.rawsetfield(-2, "__gc"); --s;
} l.pop(); --s;
l.newtable(); ++s; {
l.newtable(); ++s; {
const data_sources_t &ds = get_data_sources();
for(auto i = ds.begin(); i != ds.end(); ++i) {
l.pushfunction(std::bind(create_data_source,
std::placeholders::_1, std::cref(*i)
)); ++s;
l.rawsetfield(-2, i->first.c_str()); --s;
}
} l.rawsetfield(-2, "variables"); --s;
l.pushfunction(data_source_asnumber); ++s;
l.rawsetfield(-2, "asnumber"); --s;
l.pushfunction(data_source_astext); ++s;
l.rawsetfield(-2, "astext"); --s;
} l.setglobal("conky"); --s;
}
}

33
src/lua-config.hh Normal file
View File

@ -0,0 +1,33 @@
/* -*- 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/>.
*
*/
#ifndef LUA_CONFIG_HH
#define LUA_CONFIG_HH
#include "luamm.hh"
namespace conky {
void export_symbols(lua::state &l);
}
#endif /* LUA_CONFIG_HH */