summaryrefslogtreecommitdiff
path: root/client/lua.cc
diff options
context:
space:
mode:
authordeva <deva>2009-02-12 14:42:12 +0000
committerdeva <deva>2009-02-12 14:42:12 +0000
commit9be2869c6ebad21621e10b3bd9e82dc811b75d2d (patch)
tree499aa5af9dfd80aae4c07437f2f55f21241e5891 /client/lua.cc
parent22e0b0ddd78f13b2648befe892d9ec6c5b1b1229 (diff)
Added formatlanguage attribute to metawidget, and implemented lua parser for it.
Diffstat (limited to 'client/lua.cc')
-rw-r--r--client/lua.cc92
1 files changed, 76 insertions, 16 deletions
diff --git a/client/lua.cc b/client/lua.cc
index 2dcd279..5e6850b 100644
--- a/client/lua.cc
+++ b/client/lua.cc
@@ -175,9 +175,10 @@ static int _setVisible(lua_State *L)
}
-LUA::LUA(MacroWindow *macrowindow)
+LUA::LUA(QVector< Widget *> *widgets, QVector< Widget *> *auxwidgets)
{
- this->macrowindow = macrowindow;
+ this->widgets = widgets;
+ this->auxwidgets = auxwidgets;
L = luaL_newstate();
if(L == NULL) {
@@ -204,36 +205,36 @@ LUA::~LUA()
QString LUA::getValue(QString name)
{
- Widget *widget = macrowindow->getWidget(name);
+ Widget *widget = getWidget(name);
if(widget) return widget->getValue();
return "";
}
void LUA::setValue(QString name, QString value)
{
- Widget *widget = macrowindow->getWidget(name);
+ Widget *widget = getWidget(name);
if(widget) widget->setValue(value);
}
void LUA::enable(QString name)
{
- Widget *widget = macrowindow->getWidget(name);
+ Widget *widget = getWidget(name);
if(widget) widget->enable();
}
void LUA::disable(QString name)
{
- Widget *widget = macrowindow->getWidget(name);
+ Widget *widget = getWidget(name);
if(widget) widget->disable();
}
void LUA::setVisible(QString name, bool value)
{
- Widget *widget = macrowindow->getWidget(name);
+ Widget *widget = getWidget(name);
if(widget) widget->setVisibility(value);
}
-bool LUA::run(QString program, QString name, QString value)
+bool LUA::runValidator(QString program, QString name, QString value)
{
if(L == NULL) {
error("LUA state not initialized!");
@@ -245,10 +246,6 @@ bool LUA::run(QString program, QString name, QString value)
name.toStdString().c_str(),
value.toStdString().c_str() );
- if(macrowindow->luaprograms.contains(program) == false) return false;
-
- // printf("%s\n", macrowindow->luaprograms.value(program).toStdString().c_str());
-
lua_pushstring(L, value.toStdString().c_str());
lua_setglobal(L, "value");
@@ -257,11 +254,10 @@ bool LUA::run(QString program, QString name, QString value)
int top = lua_gettop(L);
- QString luaprogram = macrowindow->luaprograms.value(program);
if(luaL_loadbuffer(L,
- luaprogram.toStdString().c_str(),
- luaprogram.size(),
- program.toStdString().c_str())) {
+ program.toStdString().c_str(),
+ program.size(),
+ name.toStdString().c_str())) {
error(lua_tostring(L, lua_gettop(L)));
return false;
}
@@ -288,7 +284,71 @@ bool LUA::run(QString program, QString name, QString value)
return res;
}
+QString LUA::runParser(QString program)
+{
+ if(L == NULL) {
+ error("LUA state not initialized!");
+ return false;
+ }
+
+ printf("Running %s\n", program.toStdString().c_str());
+
+ int top = lua_gettop(L);
+
+ if(luaL_loadbuffer(L,
+ program.toStdString().c_str(),
+ program.size(),
+ "parser")) {
+ error(lua_tostring(L, lua_gettop(L)));
+ return false;
+ }
+
+ // Run the loaded code
+ if(lua_pcall(L, 0, LUA_MULTRET, 0)) {
+ error(lua_tostring(L, lua_gettop(L)));
+ return false;
+ }
+
+ if(top != lua_gettop(L) - 1) {
+ error("Program did not return a single value.\n");
+ return false;
+ }
+
+ if(lua_isstring(L, lua_gettop(L)) == false) {
+ error("Program did not return a boolean value.\n");
+ return false;
+ }
+
+ QString res = lua_tostring(L, lua_gettop(L));
+ lua_pop(L, 1);
+
+ return res;
+}
+
void LUA::error(QString message)
{
printf("LUA ERROR: %s\n", message.toStdString().c_str());
}
+
+Widget *LUA::getWidget(QString name)
+{
+ QVector< Widget* >::iterator i = widgets->begin();
+ while (i != widgets->end()) {
+ Widget* w = *i;
+ if(name == w->getName()) return w;
+ i++;
+ }
+
+ if(auxwidgets) {
+ QVector< Widget* >::iterator j = auxwidgets->begin();
+ while (j != auxwidgets->end()) {
+ Widget* w = *j;
+ if(name == w->getName()) return w;
+ j++;
+ }
+ }
+
+ printf("WARNING: Widget %s not found\n", name.toStdString().c_str());
+
+ return NULL;
+}