]> git.sesse.net Git - nageru/blob - theme.cpp
Rework signal connection in preparations for deinterlacing.
[nageru] / theme.cpp
1 #include "theme.h"
2
3 #include <assert.h>
4 #include <lauxlib.h>
5 #include <lua.h>
6 #include <lualib.h>
7 #include <movit/effect.h>
8 #include <movit/effect_chain.h>
9 #include <movit/image_format.h>
10 #include <movit/mix_effect.h>
11 #include <movit/overlay_effect.h>
12 #include <movit/padding_effect.h>
13 #include <movit/resample_effect.h>
14 #include <movit/resize_effect.h>
15 #include <movit/white_balance_effect.h>
16 #include <movit/ycbcr.h>
17 #include <movit/ycbcr_input.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <cstddef>
21 #include <new>
22 #include <utility>
23 #include <memory>
24
25 #include "defs.h"
26 #include "image_input.h"
27 #include "mixer.h"
28
29 namespace movit {
30 class ResourcePool;
31 }  // namespace movit
32
33 using namespace std;
34 using namespace movit;
35
36 extern Mixer *global_mixer;
37
38 namespace {
39
40 vector<LiveInputWrapper *> live_inputs;
41
42 class LuaRefWithDeleter {
43 public:
44         LuaRefWithDeleter(mutex *m, lua_State *L, int ref) : m(m), L(L), ref(ref) {}
45         ~LuaRefWithDeleter() {
46                 unique_lock<mutex> lock(*m);
47                 luaL_unref(L, LUA_REGISTRYINDEX, ref);
48         }
49         int get() const { return ref; }
50
51 private:
52         LuaRefWithDeleter(const LuaRefWithDeleter &) = delete;
53
54         mutex *m;
55         lua_State *L;
56         int ref;
57 };
58
59 template<class T, class... Args>
60 int wrap_lua_object(lua_State* L, const char *class_name, Args&&... args)
61 {
62         // Construct the C++ object and put it on the stack.
63         void *mem = lua_newuserdata(L, sizeof(T));
64         new(mem) T(std::forward<Args>(args)...);
65
66         // Look up the metatable named <class_name>, and set it on the new object.
67         luaL_getmetatable(L, class_name);
68         lua_setmetatable(L, -2);
69
70         return 1;
71 }
72
73 Theme *get_theme_updata(lua_State* L)
74 {       
75         luaL_checktype(L, lua_upvalueindex(1), LUA_TLIGHTUSERDATA);
76         return (Theme *)lua_touserdata(L, lua_upvalueindex(1));
77 }
78
79 Effect *get_effect(lua_State *L, int idx)
80 {
81         if (luaL_testudata(L, idx, "WhiteBalanceEffect") ||
82             luaL_testudata(L, idx, "ResampleEffect") ||
83             luaL_testudata(L, idx, "PaddingEffect") ||
84             luaL_testudata(L, idx, "IntegralPaddingEffect") ||
85             luaL_testudata(L, idx, "OverlayEffect") ||
86             luaL_testudata(L, idx, "ResizeEffect") ||
87             luaL_testudata(L, idx, "MixEffect") ||
88             luaL_testudata(L, idx, "ImageInput")) {
89                 return (Effect *)lua_touserdata(L, idx);
90         }
91         luaL_error(L, "Error: Index #%d was not an Effect type\n", idx);
92         return nullptr;
93 }
94
95 bool checkbool(lua_State* L, int idx)
96 {
97         luaL_checktype(L, idx, LUA_TBOOLEAN);
98         return lua_toboolean(L, idx);
99 }
100
101 std::string checkstdstring(lua_State *L, int index)
102 {
103         size_t len;
104         const char* cstr = lua_tolstring(L, index, &len);
105         return std::string(cstr, len);
106 }
107
108 int EffectChain_new(lua_State* L)
109 {
110         assert(lua_gettop(L) == 2);
111         int aspect_w = luaL_checknumber(L, 1);
112         int aspect_h = luaL_checknumber(L, 2);
113
114         return wrap_lua_object<EffectChain>(L, "EffectChain", aspect_w, aspect_h);
115 }
116
117 int EffectChain_add_live_input(lua_State* L)
118 {
119         assert(lua_gettop(L) == 2);
120         Theme *theme = get_theme_updata(L);
121         EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
122         bool override_bounce = checkbool(L, 2);
123         return wrap_lua_object<LiveInputWrapper>(L, "LiveInputWrapper", theme, chain, override_bounce);
124 }
125
126 int EffectChain_add_effect(lua_State* L)
127 {
128         assert(lua_gettop(L) >= 2);
129         EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
130
131         // TODO: Better error reporting.
132         Effect *effect = get_effect(L, 2);
133         if (lua_gettop(L) == 2) {
134                 if (effect->num_inputs() == 0) {
135                         chain->add_input((Input *)effect);
136                 } else {
137                         chain->add_effect(effect);
138                 }
139         } else {
140                 vector<Effect *> inputs;
141                 for (int idx = 3; idx <= lua_gettop(L); ++idx) {
142                         if (luaL_testudata(L, idx, "LiveInputWrapper")) {
143                                 LiveInputWrapper *input = (LiveInputWrapper *)lua_touserdata(L, idx);
144                                 inputs.push_back(input->get_input());
145                         } else {
146                                 inputs.push_back(get_effect(L, idx));
147                         }
148                 }
149                 chain->add_effect(effect, inputs);
150         }
151
152         lua_settop(L, 2);  // Return the effect itself.
153
154         // Make sure Lua doesn't garbage-collect it away.
155         lua_pushvalue(L, -1);
156         luaL_ref(L, LUA_REGISTRYINDEX);  // TODO: leak?
157
158         return 1;
159 }
160
161 int EffectChain_finalize(lua_State* L)
162 {
163         assert(lua_gettop(L) == 2);
164         EffectChain *chain = (EffectChain *)luaL_checkudata(L, 1, "EffectChain");
165         bool is_main_chain = checkbool(L, 2);
166
167         // Add outputs as needed.
168         // NOTE: If you change any details about the output format, you will need to
169         // also update what's given to the muxer (HTTPD::Mux constructor) and
170         // what's put in the H.264 stream (sps_rbsp()).
171         ImageFormat inout_format;
172         inout_format.color_space = COLORSPACE_REC_709;
173
174         // Output gamma is tricky. We should output Rec. 709 for TV, except that
175         // we expect to run with web players and others that don't really care and
176         // just output with no conversion. So that means we'll need to output sRGB,
177         // even though H.264 has no setting for that (we use “unspecified”).
178         inout_format.gamma_curve = GAMMA_sRGB;
179
180         if (is_main_chain) {
181                 YCbCrFormat output_ycbcr_format;
182                 // We actually output 4:2:0 in the end, but chroma subsampling
183                 // happens in a pass not run by Movit (see Mixer::subsample_chroma()).
184                 output_ycbcr_format.chroma_subsampling_x = 1;
185                 output_ycbcr_format.chroma_subsampling_y = 1;
186
187                 // Rec. 709 would be the sane thing to do, but it seems many players
188                 // (e.g. MPlayer and VLC) just default to BT.601 coefficients no matter
189                 // what (see discussions in e.g. https://trac.ffmpeg.org/ticket/4978).
190                 // We _do_ set the right flags, though, so that a player that works
191                 // properly doesn't have to guess.
192                 output_ycbcr_format.luma_coefficients = YCBCR_REC_601;
193                 output_ycbcr_format.full_range = false;
194                 output_ycbcr_format.num_levels = 256;
195
196                 chain->add_ycbcr_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED, output_ycbcr_format, YCBCR_OUTPUT_SPLIT_Y_AND_CBCR);
197                 chain->set_dither_bits(8);
198                 chain->set_output_origin(OUTPUT_ORIGIN_TOP_LEFT);
199         }
200         chain->add_output(inout_format, OUTPUT_ALPHA_FORMAT_POSTMULTIPLIED);
201
202         chain->finalize();
203         return 0;
204 }
205
206 int LiveInputWrapper_connect_signal(lua_State* L)
207 {
208         assert(lua_gettop(L) == 2);
209         LiveInputWrapper *input = (LiveInputWrapper *)luaL_checkudata(L, 1, "LiveInputWrapper");
210         int signal_num = luaL_checknumber(L, 2);
211         input->connect_signal(signal_num);
212         return 0;
213 }
214
215 int ImageInput_new(lua_State* L)
216 {
217         assert(lua_gettop(L) == 1);
218         std::string filename = checkstdstring(L, 1);
219         return wrap_lua_object<ImageInput>(L, "ImageInput", filename);
220 }
221
222 int WhiteBalanceEffect_new(lua_State* L)
223 {
224         assert(lua_gettop(L) == 0);
225         return wrap_lua_object<WhiteBalanceEffect>(L, "WhiteBalanceEffect");
226 }
227
228 int ResampleEffect_new(lua_State* L)
229 {
230         assert(lua_gettop(L) == 0);
231         return wrap_lua_object<ResampleEffect>(L, "ResampleEffect");
232 }
233
234 int PaddingEffect_new(lua_State* L)
235 {
236         assert(lua_gettop(L) == 0);
237         return wrap_lua_object<PaddingEffect>(L, "PaddingEffect");
238 }
239
240 int IntegralPaddingEffect_new(lua_State* L)
241 {
242         assert(lua_gettop(L) == 0);
243         return wrap_lua_object<IntegralPaddingEffect>(L, "IntegralPaddingEffect");
244 }
245
246 int OverlayEffect_new(lua_State* L)
247 {
248         assert(lua_gettop(L) == 0);
249         return wrap_lua_object<OverlayEffect>(L, "OverlayEffect");
250 }
251
252 int ResizeEffect_new(lua_State* L)
253 {
254         assert(lua_gettop(L) == 0);
255         return wrap_lua_object<ResizeEffect>(L, "ResizeEffect");
256 }
257
258 int MixEffect_new(lua_State* L)
259 {
260         assert(lua_gettop(L) == 0);
261         return wrap_lua_object<MixEffect>(L, "MixEffect");
262 }
263
264 int Effect_set_float(lua_State *L)
265 {
266         assert(lua_gettop(L) == 3);
267         Effect *effect = (Effect *)get_effect(L, 1);
268         std::string key = checkstdstring(L, 2);
269         float value = luaL_checknumber(L, 3);
270         if (!effect->set_float(key, value)) {
271                 luaL_error(L, "Effect refused set_float(\"%s\", %d) (invalid key?)", key.c_str(), int(value));
272         }
273         return 0;
274 }
275
276 int Effect_set_int(lua_State *L)
277 {
278         assert(lua_gettop(L) == 3);
279         Effect *effect = (Effect *)get_effect(L, 1);
280         std::string key = checkstdstring(L, 2);
281         float value = luaL_checknumber(L, 3);
282         if (!effect->set_int(key, value)) {
283                 luaL_error(L, "Effect refused set_int(\"%s\", %d) (invalid key?)", key.c_str(), int(value));
284         }
285         return 0;
286 }
287
288 int Effect_set_vec3(lua_State *L)
289 {
290         assert(lua_gettop(L) == 5);
291         Effect *effect = (Effect *)get_effect(L, 1);
292         std::string key = checkstdstring(L, 2);
293         float v[3];
294         v[0] = luaL_checknumber(L, 3);
295         v[1] = luaL_checknumber(L, 4);
296         v[2] = luaL_checknumber(L, 5);
297         if (!effect->set_vec3(key, v)) {
298                 luaL_error(L, "Effect refused set_vec3(\"%s\", %f, %f, %f) (invalid key?)", key.c_str(),
299                         v[0], v[1], v[2]);
300         }
301         return 0;
302 }
303
304 int Effect_set_vec4(lua_State *L)
305 {
306         assert(lua_gettop(L) == 6);
307         Effect *effect = (Effect *)get_effect(L, 1);
308         std::string key = checkstdstring(L, 2);
309         float v[4];
310         v[0] = luaL_checknumber(L, 3);
311         v[1] = luaL_checknumber(L, 4);
312         v[2] = luaL_checknumber(L, 5);
313         v[3] = luaL_checknumber(L, 6);
314         if (!effect->set_vec4(key, v)) {
315                 luaL_error(L, "Effect refused set_vec4(\"%s\", %f, %f, %f, %f) (invalid key?)", key.c_str(),
316                         v[0], v[1], v[2], v[3]);
317         }
318         return 0;
319 }
320
321 const luaL_Reg EffectChain_funcs[] = {
322         { "new", EffectChain_new },
323         { "add_live_input", EffectChain_add_live_input },
324         { "add_effect", EffectChain_add_effect },
325         { "finalize", EffectChain_finalize },
326         { NULL, NULL }
327 };
328
329 const luaL_Reg LiveInputWrapper_funcs[] = {
330         { "connect_signal", LiveInputWrapper_connect_signal },
331         { NULL, NULL }
332 };
333
334 const luaL_Reg ImageInput_funcs[] = {
335         { "new", ImageInput_new },
336         { "set_float", Effect_set_float },
337         { "set_int", Effect_set_int },
338         { "set_vec3", Effect_set_vec3 },
339         { "set_vec4", Effect_set_vec4 },
340         { NULL, NULL }
341 };
342
343 const luaL_Reg WhiteBalanceEffect_funcs[] = {
344         { "new", WhiteBalanceEffect_new },
345         { "set_float", Effect_set_float },
346         { "set_int", Effect_set_int },
347         { "set_vec3", Effect_set_vec3 },
348         { "set_vec4", Effect_set_vec4 },
349         { NULL, NULL }
350 };
351
352 const luaL_Reg ResampleEffect_funcs[] = {
353         { "new", ResampleEffect_new },
354         { "set_float", Effect_set_float },
355         { "set_int", Effect_set_int },
356         { "set_vec3", Effect_set_vec3 },
357         { "set_vec4", Effect_set_vec4 },
358         { NULL, NULL }
359 };
360
361 const luaL_Reg PaddingEffect_funcs[] = {
362         { "new", PaddingEffect_new },
363         { "set_float", Effect_set_float },
364         { "set_int", Effect_set_int },
365         { "set_vec3", Effect_set_vec3 },
366         { "set_vec4", Effect_set_vec4 },
367         { NULL, NULL }
368 };
369
370 const luaL_Reg IntegralPaddingEffect_funcs[] = {
371         { "new", IntegralPaddingEffect_new },
372         { "set_float", Effect_set_float },
373         { "set_int", Effect_set_int },
374         { "set_vec3", Effect_set_vec3 },
375         { "set_vec4", Effect_set_vec4 },
376         { NULL, NULL }
377 };
378
379 const luaL_Reg OverlayEffect_funcs[] = {
380         { "new", OverlayEffect_new },
381         { "set_float", Effect_set_float },
382         { "set_int", Effect_set_int },
383         { "set_vec3", Effect_set_vec3 },
384         { "set_vec4", Effect_set_vec4 },
385         { NULL, NULL }
386 };
387
388 const luaL_Reg ResizeEffect_funcs[] = {
389         { "new", ResizeEffect_new },
390         { "set_float", Effect_set_float },
391         { "set_int", Effect_set_int },
392         { "set_vec3", Effect_set_vec3 },
393         { "set_vec4", Effect_set_vec4 },
394         { NULL, NULL }
395 };
396
397 const luaL_Reg MixEffect_funcs[] = {
398         { "new", MixEffect_new },
399         { "set_float", Effect_set_float },
400         { "set_int", Effect_set_int },
401         { "set_vec3", Effect_set_vec3 },
402         { "set_vec4", Effect_set_vec4 },
403         { NULL, NULL }
404 };
405
406 }  // namespace
407
408 LiveInputWrapper::LiveInputWrapper(Theme *theme, EffectChain *chain, bool override_bounce)
409         : theme(theme)
410 {
411         ImageFormat inout_format;
412         inout_format.color_space = COLORSPACE_sRGB;
413
414         // Gamma curve depends on the input signal, and we don't really get any
415         // indications. A camera would be expected to do Rec. 709, but
416         // I haven't checked if any do in practice. However, computers _do_ output
417         // in sRGB gamma (ie., they don't convert from sRGB to Rec. 709), and
418         // I wouldn't really be surprised if most non-professional cameras do, too.
419         // So we pick sRGB as the least evil here.
420         inout_format.gamma_curve = GAMMA_sRGB;
421
422         // The Blackmagic driver docs claim that the device outputs Y'CbCr
423         // according to Rec. 601, but practical testing indicates it definitely
424         // is Rec. 709 (at least up to errors attributable to rounding errors).
425         // Perhaps 601 was only to indicate the subsampling positions, not the
426         // colorspace itself? Tested with a Lenovo X1 gen 3 as input.
427         YCbCrFormat input_ycbcr_format;
428         input_ycbcr_format.chroma_subsampling_x = 2;
429         input_ycbcr_format.chroma_subsampling_y = 1;
430         input_ycbcr_format.cb_x_position = 0.0;
431         input_ycbcr_format.cr_x_position = 0.0;
432         input_ycbcr_format.cb_y_position = 0.5;
433         input_ycbcr_format.cr_y_position = 0.5;
434         input_ycbcr_format.luma_coefficients = YCBCR_REC_709;
435         input_ycbcr_format.full_range = false;
436
437         if (override_bounce) {
438                 input = new NonBouncingYCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
439         } else {
440                 input = new YCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
441         }
442         chain->add_input(input);
443 }
444
445 void LiveInputWrapper::connect_signal(int signal_num)
446 {
447         if (global_mixer == nullptr) {
448                 return;
449         }
450
451         signal_num = theme->map_signal(signal_num);
452
453         Mixer::BufferedFrame frame = global_mixer->get_buffered_frame(signal_num, 0);
454         const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
455
456         input->set_texture_num(0, userdata->tex_y[frame.field_number]);
457         input->set_texture_num(1, userdata->tex_cbcr[frame.field_number]);
458         input->set_width(userdata->last_width[frame.field_number]);
459         input->set_height(userdata->last_height[frame.field_number]);
460
461         // Hold on to the refcount so that we don't release the input frame
462         // until we're done rendering from it.
463         if (theme->used_input_frames_collector != nullptr) {
464                 theme->used_input_frames_collector->push_back(frame.frame);
465         }
466 }
467
468 Theme::Theme(const char *filename, ResourcePool *resource_pool, unsigned num_cards)
469         : resource_pool(resource_pool), num_cards(num_cards)
470 {
471         L = luaL_newstate();
472         luaL_openlibs(L);
473
474         register_class("EffectChain", EffectChain_funcs); 
475         register_class("LiveInputWrapper", LiveInputWrapper_funcs); 
476         register_class("ImageInput", ImageInput_funcs);
477         register_class("WhiteBalanceEffect", WhiteBalanceEffect_funcs);
478         register_class("ResampleEffect", ResampleEffect_funcs);
479         register_class("PaddingEffect", PaddingEffect_funcs);
480         register_class("IntegralPaddingEffect", IntegralPaddingEffect_funcs);
481         register_class("OverlayEffect", OverlayEffect_funcs);
482         register_class("ResizeEffect", ResizeEffect_funcs);
483         register_class("MixEffect", MixEffect_funcs);
484
485         // Run script.
486         lua_settop(L, 0);
487         if (luaL_dofile(L, filename)) {
488                 fprintf(stderr, "error: %s\n", lua_tostring(L, -1));
489                 lua_pop(L, 1);
490                 exit(1);
491         }
492         assert(lua_gettop(L) == 0);
493
494         // Ask it for the number of channels.
495         lua_getglobal(L, "num_channels");
496
497         if (lua_pcall(L, 0, 1, 0) != 0) {
498                 fprintf(stderr, "error running function `num_channels': %s\n", lua_tostring(L, -1));
499                 exit(1);
500         }
501
502         num_channels = luaL_checknumber(L, 1);
503         lua_pop(L, 1);
504         assert(lua_gettop(L) == 0);
505 }
506
507 void Theme::register_class(const char *class_name, const luaL_Reg *funcs)
508 {
509         assert(lua_gettop(L) == 0);
510         luaL_newmetatable(L, class_name);
511         lua_pushlightuserdata(L, this);
512         luaL_setfuncs(L, funcs, 1);
513         lua_pushvalue(L, -1);
514         lua_setfield(L, -2, "__index");
515         lua_setglobal(L, class_name);
516         assert(lua_gettop(L) == 0);
517 }
518
519 Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned height)
520 {
521         Chain chain;
522
523         unique_lock<mutex> lock(m);
524         assert(lua_gettop(L) == 0);
525         lua_getglobal(L, "get_chain");  /* function to be called */
526         lua_pushnumber(L, num);
527         lua_pushnumber(L, t);
528         lua_pushnumber(L, width);
529         lua_pushnumber(L, height);
530
531         this->used_input_frames_collector = &chain.input_frames;
532         if (lua_pcall(L, 4, 2, 0) != 0) {
533                 fprintf(stderr, "error running function `get_chain': %s\n", lua_tostring(L, -1));
534                 exit(1);
535         }
536         this->used_input_frames_collector = nullptr;
537
538         chain.chain = (EffectChain *)luaL_checkudata(L, -2, "EffectChain");
539         if (!lua_isfunction(L, -1)) {
540                 fprintf(stderr, "Argument #-1 should be a function\n");
541                 exit(1);
542         }
543         lua_pushvalue(L, -1);
544         shared_ptr<LuaRefWithDeleter> funcref(new LuaRefWithDeleter(&m, L, luaL_ref(L, LUA_REGISTRYINDEX)));
545         lua_pop(L, 2);
546         assert(lua_gettop(L) == 0);
547
548         chain.setup_chain = [this, funcref]{
549                 unique_lock<mutex> lock(m);
550
551                 // Set up state, including connecting signals.
552                 lua_rawgeti(L, LUA_REGISTRYINDEX, funcref->get());
553                 if (lua_pcall(L, 0, 0, 0) != 0) {
554                         fprintf(stderr, "error running chain setup callback: %s\n", lua_tostring(L, -1));
555                         exit(1);
556                 }
557                 assert(lua_gettop(L) == 0);
558         };
559
560         return chain;
561 }
562
563 std::string Theme::get_channel_name(unsigned channel)
564 {
565         unique_lock<mutex> lock(m);
566         lua_getglobal(L, "channel_name");
567         lua_pushnumber(L, channel);
568         if (lua_pcall(L, 1, 1, 0) != 0) {
569                 fprintf(stderr, "error running function `channel_nam': %s\n", lua_tostring(L, -1));
570                 exit(1);
571         }
572
573         std::string ret = lua_tostring(L, -1);
574         lua_pop(L, 1);
575         assert(lua_gettop(L) == 0);
576         return ret;
577 }
578
579 bool Theme::get_supports_set_wb(unsigned channel)
580 {
581         unique_lock<mutex> lock(m);
582         lua_getglobal(L, "supports_set_wb");
583         lua_pushnumber(L, channel);
584         if (lua_pcall(L, 1, 1, 0) != 0) {
585                 fprintf(stderr, "error running function `supports_set_wb': %s\n", lua_tostring(L, -1));
586                 exit(1);
587         }
588
589         bool ret = checkbool(L, -1);
590         lua_pop(L, 1);
591         assert(lua_gettop(L) == 0);
592         return ret;
593 }
594
595 void Theme::set_wb(unsigned channel, double r, double g, double b)
596 {
597         unique_lock<mutex> lock(m);
598         lua_getglobal(L, "set_wb");
599         lua_pushnumber(L, channel);
600         lua_pushnumber(L, r);
601         lua_pushnumber(L, g);
602         lua_pushnumber(L, b);
603         if (lua_pcall(L, 4, 0, 0) != 0) {
604                 fprintf(stderr, "error running function `set_wb': %s\n", lua_tostring(L, -1));
605                 exit(1);
606         }
607
608         assert(lua_gettop(L) == 0);
609 }
610
611 std::vector<std::string> Theme::get_transition_names(float t)
612 {
613         unique_lock<mutex> lock(m);
614         lua_getglobal(L, "get_transitions");
615         lua_pushnumber(L, t);
616         if (lua_pcall(L, 1, 1, 0) != 0) {
617                 fprintf(stderr, "error running function `get_transitions': %s\n", lua_tostring(L, -1));
618                 exit(1);
619         }
620
621         std::vector<std::string> ret;
622         lua_pushnil(L);
623         while (lua_next(L, -2) != 0) {
624                 ret.push_back(lua_tostring(L, -1));
625                 lua_pop(L, 1);
626         }
627         lua_pop(L, 1);
628         assert(lua_gettop(L) == 0);
629         return ret;
630 }       
631
632 int Theme::map_signal(int signal_num)
633 {
634         if (signal_num >= int(num_cards)) {
635                 if (signals_warned_about.insert(signal_num).second) {
636                         fprintf(stderr, "WARNING: Theme asked for input %d, but we only have %u card(s).\n", signal_num, num_cards);
637                         fprintf(stderr, "Mapping to card %d instead.\n", signal_num % num_cards);
638                 }
639                 signal_num %= num_cards;
640         }
641         return signal_num;
642 }
643
644 void Theme::transition_clicked(int transition_num, float t)
645 {
646         unique_lock<mutex> lock(m);
647         lua_getglobal(L, "transition_clicked");
648         lua_pushnumber(L, transition_num);
649         lua_pushnumber(L, t);
650
651         if (lua_pcall(L, 2, 0, 0) != 0) {
652                 fprintf(stderr, "error running function `transition_clicked': %s\n", lua_tostring(L, -1));
653                 exit(1);
654         }
655         assert(lua_gettop(L) == 0);
656 }
657
658 void Theme::channel_clicked(int preview_num)
659 {
660         unique_lock<mutex> lock(m);
661         lua_getglobal(L, "channel_clicked");
662         lua_pushnumber(L, preview_num);
663
664         if (lua_pcall(L, 1, 0, 0) != 0) {
665                 fprintf(stderr, "error running function `channel_clicked': %s\n", lua_tostring(L, -1));
666                 exit(1);
667         }
668         assert(lua_gettop(L) == 0);
669 }