-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathLuaScript.cc
More file actions
134 lines (114 loc) · 3.69 KB
/
Copy pathLuaScript.cc
File metadata and controls
134 lines (114 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
Source part of project WeJoy
Copyright (C) 2023 Johannes Bergmark
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 <https://www.gnu.org/licenses/>.
*/
#include "LuaScript.h"
#include "lua_key_codes.h"
#include <errno.h>
#include <cstring>
LuaScript::LuaScript(const std::string& filename) {
L = luaL_newstate();
luaL_openlibs(L);
LUA_GLOBAL_KEYS::init_global_keys(L);
if(luaL_loadfile(L, filename.c_str()) || lua_pcall(L, 0, 0, 0))
{
std::cout<<"Error: failed to load ("<<filename<<")"<<std::endl;
const char* s = strerror(errno);
//std::cout << s << std::endl;
std::cout << lua_tostring(L, -1) << std::endl << s << std::endl;
L = 0;
return;
}
}
LuaScript::~LuaScript()
{
if(L) lua_close(L);
}
bool LuaScript::doFunctionExist(const std::string& functionName)
{
lua_getglobal(L, functionName.c_str());
return lua_isfunction(L, -1);
}
void LuaScript::printError(const std::string& variableName, const std::string& reason) {
std::cout<<"Error: can't get ["<<variableName<<"]. "<<reason<<std::endl;
}
/*
std::vector<int> LuaScript::getIntVector(const std::string& name) {
std::vector<int> v;
lua_gettostack(name.c_str());
if(lua_isnil(L, -1)) { // array is not found
return std::vector<int>();
}
lua_pushnil(L);
while(lua_next(L, -2)) {
v.push_back((int)lua_tonumber(L, -1));
lua_pop(L, 1);
}
clean();
return v;
}
std::vector<std::string> LuaScript::getStringVector(const std::string& name) {
std::vector<std::string> v;
lua_gettostack(name.c_str());
if(lua_isnil(L, -1)) { // array is not found
return std::vector<std::string>();
}
lua_pushnil(L);
while(lua_next(L, -2)) {
v.push_back((std::string)lua_tostring(L, -1));
lua_pop(L, 1);
}
clean();
return v;
}
std::vector<std::string> LuaScript::getTableKeys(const std::string& name)
{
std::vector<std::string> keys;
lua_getglobal(L, name.c_str());
if (!lua_istable(L, -1))
{
lua_pop(L, 1);
return keys;
}
lua_pushnil(L);
while (lua_next(L, -2) != 0)
{
const char *key = lua_tostring(L, -2);
if(key) keys.push_back(key);
lua_pop(L, 1);
}
lua_pop(L, 1);
return keys;
}
*/
void LuaScript::call_device_function(const std::string& str_func, int event_type, int event_code, int event_value)
{
lua_getglobal(L, str_func.c_str());
lua_pushnumber(L, event_type);
lua_pushnumber(L, event_code);
lua_pushnumber(L, event_value);
lua_pcall(L, 3, 0, 0); //Lua handle, number of arguments, number of return values, error code
}
//void LuaScript::call_device_function(const device_function& df, int value)
void LuaScript::call_device_function(const std::string& str_func, int value)
{
//lua_getglobal(L, df.lfCall.c_str());
lua_getglobal(L, str_func.c_str());
lua_pushnumber(L, value);
lua_pcall(L, 1, 0, 0); //Lua handle, number of arguments, number of return values, error code
}
void LuaScript::pushcfunction(int (*_func)(lua_State*), const std::string& _name)
{
lua_pushcfunction(L, _func);
lua_setglobal(L, _name.c_str());
}