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