]> git.sesse.net Git - nageru/blob - theme.cpp
Remove an unused variable.
[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                 return;
447         }
448
449         signal_num = theme->map_signal(signal_num);
450
451         Mixer::BufferedFrame frame = global_mixer->get_buffered_frame(signal_num, 0);
452         const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
453
454         input->set_texture_num(0, userdata->tex_y[frame.field_number]);
455         input->set_texture_num(1, userdata->tex_cbcr[frame.field_number]);
456         input->set_width(userdata->last_width[frame.field_number]);
457         input->set_height(userdata->last_height[frame.field_number]);
458
459         // Hold on to the refcount so that we don't release the input frame
460         // until we're done rendering from it.
461         if (theme->used_input_frames_collector != nullptr) {
462                 theme->used_input_frames_collector->push_back(frame.frame);
463         }
464 }
465
466 Theme::Theme(const char *filename, ResourcePool *resource_pool, unsigned num_cards)
467         : resource_pool(resource_pool), num_cards(num_cards)
468 {
469         L = luaL_newstate();
470         luaL_openlibs(L);
471
472         register_class("EffectChain", EffectChain_funcs); 
473         register_class("LiveInputWrapper", LiveInputWrapper_funcs); 
474         register_class("ImageInput", ImageInput_funcs);
475         register_class("WhiteBalanceEffect", WhiteBalanceEffect_funcs);
476         register_class("ResampleEffect", ResampleEffect_funcs);
477         register_class("PaddingEffect", PaddingEffect_funcs);
478         register_class("IntegralPaddingEffect", IntegralPaddingEffect_funcs);
479         register_class("OverlayEffect", OverlayEffect_funcs);
480         register_class("ResizeEffect", ResizeEffect_funcs);
481         register_class("MixEffect", MixEffect_funcs);
482
483         // Run script.
484         lua_settop(L, 0);
485         if (luaL_dofile(L, filename)) {
486                 fprintf(stderr, "error: %s\n", lua_tostring(L, -1));
487                 lua_pop(L, 1);
488                 exit(1);
489         }
490         assert(lua_gettop(L) == 0);
491
492         // Ask it for the number of channels.
493         lua_getglobal(L, "num_channels");
494
495         if (lua_pcall(L, 0, 1, 0) != 0) {
496                 fprintf(stderr, "error running function `num_channels': %s\n", lua_tostring(L, -1));
497                 exit(1);
498         }
499
500         num_channels = luaL_checknumber(L, 1);
501         lua_pop(L, 1);
502         assert(lua_gettop(L) == 0);
503 }
504
505 void Theme::register_class(const char *class_name, const luaL_Reg *funcs)
506 {
507         assert(lua_gettop(L) == 0);
508         luaL_newmetatable(L, class_name);
509         lua_pushlightuserdata(L, this);
510         luaL_setfuncs(L, funcs, 1);
511         lua_pushvalue(L, -1);
512         lua_setfield(L, -2, "__index");
513         lua_setglobal(L, class_name);
514         assert(lua_gettop(L) == 0);
515 }
516
517 Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned height)
518 {
519         Chain chain;
520
521         unique_lock<mutex> lock(m);
522         assert(lua_gettop(L) == 0);
523         lua_getglobal(L, "get_chain");  /* function to be called */
524         lua_pushnumber(L, num);
525         lua_pushnumber(L, t);
526         lua_pushnumber(L, width);
527         lua_pushnumber(L, height);
528
529         this->used_input_frames_collector = &chain.input_frames;
530         if (lua_pcall(L, 4, 2, 0) != 0) {
531                 fprintf(stderr, "error running function `get_chain': %s\n", lua_tostring(L, -1));
532                 exit(1);
533         }
534         this->used_input_frames_collector = nullptr;
535
536         chain.chain = (EffectChain *)luaL_checkudata(L, -2, "EffectChain");
537         if (!lua_isfunction(L, -1)) {
538                 fprintf(stderr, "Argument #-1 should be a function\n");
539                 exit(1);
540         }
541         lua_pushvalue(L, -1);
542         shared_ptr<LuaRefWithDeleter> funcref(new LuaRefWithDeleter(&m, L, luaL_ref(L, LUA_REGISTRYINDEX)));
543         lua_pop(L, 2);
544         assert(lua_gettop(L) == 0);
545
546         chain.setup_chain = [this, funcref]{
547                 unique_lock<mutex> lock(m);
548
549                 // Set up state, including connecting signals.
550                 lua_rawgeti(L, LUA_REGISTRYINDEX, funcref->get());
551                 if (lua_pcall(L, 0, 0, 0) != 0) {
552                         fprintf(stderr, "error running chain setup callback: %s\n", lua_tostring(L, -1));
553                         exit(1);
554                 }
555                 assert(lua_gettop(L) == 0);
556         };
557
558         return chain;
559 }
560
561 std::string Theme::get_channel_name(unsigned channel)
562 {
563         unique_lock<mutex> lock(m);
564         lua_getglobal(L, "channel_name");
565         lua_pushnumber(L, channel);
566         if (lua_pcall(L, 1, 1, 0) != 0) {
567                 fprintf(stderr, "error running function `channel_nam': %s\n", lua_tostring(L, -1));
568                 exit(1);
569         }
570
571         std::string ret = lua_tostring(L, -1);
572         lua_pop(L, 1);
573         assert(lua_gettop(L) == 0);
574         return ret;
575 }
576
577 bool Theme::get_supports_set_wb(unsigned channel)
578 {
579         unique_lock<mutex> lock(m);
580         lua_getglobal(L, "supports_set_wb");
581         lua_pushnumber(L, channel);
582         if (lua_pcall(L, 1, 1, 0) != 0) {
583                 fprintf(stderr, "error running function `supports_set_wb': %s\n", lua_tostring(L, -1));
584                 exit(1);
585         }
586
587         bool ret = checkbool(L, -1);
588         lua_pop(L, 1);
589         assert(lua_gettop(L) == 0);
590         return ret;
591 }
592
593 void Theme::set_wb(unsigned channel, double r, double g, double b)
594 {
595         unique_lock<mutex> lock(m);
596         lua_getglobal(L, "set_wb");
597         lua_pushnumber(L, channel);
598         lua_pushnumber(L, r);
599         lua_pushnumber(L, g);
600         lua_pushnumber(L, b);
601         if (lua_pcall(L, 4, 0, 0) != 0) {
602                 fprintf(stderr, "error running function `set_wb': %s\n", lua_tostring(L, -1));
603                 exit(1);
604         }
605
606         assert(lua_gettop(L) == 0);
607 }
608
609 std::vector<std::string> Theme::get_transition_names(float t)
610 {
611         unique_lock<mutex> lock(m);
612         lua_getglobal(L, "get_transitions");
613         lua_pushnumber(L, t);
614         if (lua_pcall(L, 1, 1, 0) != 0) {
615                 fprintf(stderr, "error running function `get_transitions': %s\n", lua_tostring(L, -1));
616                 exit(1);
617         }
618
619         std::vector<std::string> ret;
620         lua_pushnil(L);
621         while (lua_next(L, -2) != 0) {
622                 ret.push_back(lua_tostring(L, -1));
623                 lua_pop(L, 1);
624         }
625         lua_pop(L, 1);
626         assert(lua_gettop(L) == 0);
627         return ret;
628 }       
629
630 int Theme::map_signal(int signal_num)
631 {
632         if (signal_num >= int(num_cards)) {
633                 if (signals_warned_about.insert(signal_num).second) {
634                         fprintf(stderr, "WARNING: Theme asked for input %d, but we only have %u card(s).\n", signal_num, num_cards);
635                         fprintf(stderr, "Mapping to card %d instead.\n", signal_num % num_cards);
636                 }
637                 signal_num %= num_cards;
638         }
639         return signal_num;
640 }
641
642 void Theme::transition_clicked(int transition_num, float t)
643 {
644         unique_lock<mutex> lock(m);
645         lua_getglobal(L, "transition_clicked");
646         lua_pushnumber(L, transition_num);
647         lua_pushnumber(L, t);
648
649         if (lua_pcall(L, 2, 0, 0) != 0) {
650                 fprintf(stderr, "error running function `transition_clicked': %s\n", lua_tostring(L, -1));
651                 exit(1);
652         }
653         assert(lua_gettop(L) == 0);
654 }
655
656 void Theme::channel_clicked(int preview_num)
657 {
658         unique_lock<mutex> lock(m);
659         lua_getglobal(L, "channel_clicked");
660         lua_pushnumber(L, preview_num);
661
662         if (lua_pcall(L, 1, 0, 0) != 0) {
663                 fprintf(stderr, "error running function `channel_clicked': %s\n", lua_tostring(L, -1));
664                 exit(1);
665         }
666         assert(lua_gettop(L) == 0);
667 }