-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathckLua.cpp
More file actions
300 lines (235 loc) · 6.27 KB
/
Copy pathckLua.cpp
File metadata and controls
300 lines (235 loc) · 6.27 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include "ckLua.h"
#include <ctime>
#include <iostream>
#include <Windows.h>
#include "LibCorsairRGB\LibCorsairRGB.h"
#include "keymapParser.h"
// Wrap callback pointers to right object
ckLua* context = NULL;
void _keyDown(int key, int flags) {
if (context != NULL)
context->keyDownCallback(key, flags);
}
void _keyUp(int key, int flags) {
if (context != NULL)
context->keyUpCallback(key, flags);
}
// Wrap lua functions
int _setLed(lua_State* L) { if (context != NULL) return context->setLed(L); return 0; }
int _setUPS(lua_State* L) { if (context != NULL) return context->setUPS(L); return 0; }
int _setScript(lua_State* L){ if (context != NULL) return context->setScript(L); return 0; }
int _setKeymap(lua_State* L){ if (context != NULL) return context->setKeymap(L); return 0; }
int _luaPanic(lua_State* L) { if (context != NULL) return context->luaPanic(L); return 0; }
// Use cout (redirected) instead of whatever lualib is using
int _luaPrint(lua_State* L) {
int nargs = lua_gettop(L);
for (int i = 1; i <= nargs; i++) {
if (lua_isstring(L, i)) {
std::cout << lua_tostring(L, i);
if (i < nargs + 1)
std::cout << '\t';
}
}
lua_pop(L, nargs);
std::cout << std::endl;
return 0;
}
jmp_buf jumpBuf;
ckLua::ckLua():
deltaTime(0),
colorChange(false),
targetFPS(60),
terminate(false),
luaError(false),
altgrdown(false),
altgrup(false) {
context = this;
lastUpdateTime = std::clock();
// Create listener
inputListener = InputListener(&_keyDown, &_keyUp);
int status = lcrgb_initialise();
if (status != 0) {
printf("Failed to intialise LibCorsairRGB\n");
return;
}
atexit(lcrgb_deinitialise);
}
ckLua::~ckLua() {
lua_close(L);
}
void ckLua::kill() {
terminate = true;
}
void ckLua::run(bool lowLevelHook) {
// Only hook if low level. Not used, keeping for reference
/*if (lowLevelHook)
inputListener.hook();*/
while (!terminate) {
if (beginFrame()) {
setjmp(jumpBuf); // Set jump buffer if we fail
if (luaError) continue;
// Call update function in lua
lua_getglobal(L, "update");
lua_pushnumber(L, deltaTime);
lua_call(L, 1, 0);
// Only flush colors if colors has changed
if (colorChange) {
lcrgb_flush_light_buffer();
colorChange = false;
}
}
// Message loop: needed to forward message to currently running application.
// Without this, only this program would receive keyboard input
MSG msg;
if (MsgWaitForMultipleObjects(0, NULL, FALSE, 17, QS_ALLINPUT) == WAIT_OBJECT_0)
{
while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
// Only intercept if not low level
if (!lowLevelHook)
inputListener.interceptMessage(msg);
}
}
}
// Cleanly unhook listener. Not used, keeping for reference
/*if (lowLevelHook)
inputListener.unhook();*/
}
// Check if time to update
bool ckLua::beginFrame() {
std::clock_t now = std::clock();
if ((deltaTime = (now - lastUpdateTime) / 1000.0f) >= 1.0f / targetFPS) {
lastUpdateTime = std::clock();
return true;
}
return false;
}
// Callback from hook
void ckLua::keyDownCallback(int key, int flags) {
setjmp(jumpBuf); // Set jump buffer if we fail
if (luaError) return;
// Setup call to lua
lua_getglobal(L, "keyDown");
lua_pushinteger(L, key);
lua_pushnumber(L, keymap[key].x);
lua_pushnumber(L, keymap[key].y);
lua_pushstring(L, keymap[key].name.c_str());
lua_call(L, 4, 0);
}
// Callback from hook
void ckLua::keyUpCallback(int key, int flags) {
setjmp(jumpBuf); // Set jump buffer if we fail
if (luaError) return;
// Set up call to lua
lua_getglobal(L, "keyUp");
lua_pushnumber(L, keymap[key].x);
lua_pushnumber(L, keymap[key].y);
lua_pushstring(L, keymap[key].name.c_str());
lua_call(L, 3, 0);
}
void ckLua::changeScript(const std::string& fileName) {
// Reopen state if needed
if (L)
lua_close(L);
L = lua_open();
luaL_openlibs(L);
// Register global callbacks
lua_register(L, "setLed", _setLed);
lua_register(L, "setUPS", _setUPS);
lua_register(L, "loadKeymap", _setKeymap);
lua_register(L, "loadScript", _setScript);
lua_register(L, "print", _luaPrint);
lua_atpanic(L, _luaPanic);
luaError = false;
setjmp(jumpBuf);
if (luaError) return;
std::cout << "Loading script: " << fileName << std::endl;
luaL_dofile(L, fileName.c_str());
lua_getglobal(L, "create");
lua_call(L, 0, 0);
}
void ckLua::changeKeyConfig(const std::string& fileName) {
std::cout << "Loading keymap: " << fileName << std::endl;
keymap = KeymapParser::parseFile(fileName);
}
//
// Lua callbacks down here
//
int ckLua::setLed(lua_State* L) {
int n = lua_gettop(L);
int x, y, r, g, b;
std::string key;
// Determine which call was made
switch (n) {
// Set color based on position
case 5:
// Get values from stack
x = (int) lua_tonumber(L, 1);
y = (int) lua_tonumber(L, 2);
r = (int) lua_tonumber(L, 3);
g = (int) lua_tonumber(L, 4);
b = (int) lua_tonumber(L, 5);
// Remove values from stack
lua_pop(L, 5);
lcrgb_set_position(x, y, r, g, b);
colorChange = true;
break;
// Set color based on key code
case 4:
// Get values from stack
key = lua_tostring(L, 1);
r = (int) lua_tonumber(L, 2);
g = (int) lua_tonumber(L, 3);
b = (int) lua_tonumber(L, 4);
// Remove values from stack
lua_pop(L, 4);
int keycode = 0xFF;
for each (std::pair<int, ckKey> k in keymap) {
if (k.second.name == key) {
lcrgb_set_position((int) k.second.x, (int) k.second.y, r, g, b);
colorChange = true;
break;
}
}
break;
}
return 0;
}
int ckLua::setUPS(lua_State* L) {
int n = lua_gettop(L);
if (n > 1) return 0;
targetFPS = (int) lua_tonumber(L, 1);
lua_pop(L, 1);
// Clamp values
if (targetFPS > 60)
targetFPS = 60;
else if (targetFPS <= 0)
targetFPS = 1;
return 0;
}
int ckLua::setScript(lua_State* L){
int n = lua_gettop(L);
if (n > 1) return 0;
changeScript(lua_tostring(L, 1));
lua_pop(L, 1);
return 0;
}
int ckLua::setKeymap(lua_State* L) {
int n = lua_gettop(L);
if (n > 1) return 0;
keymap = KeymapParser::parseFile(lua_tostring(L, 1));
lua_pop(L, 1);
return 0;
}
int ckLua::luaPanic(lua_State* L) {
std::string err = lua_tostring(L, -1);
lua_pop(L, 1);
std::cout << std::endl;
std::cout << "Lua error: \n\t" << err << std::endl;
std::cout << std::endl;
luaError = true;
longjmp(jumpBuf, 1);
return 0;
}