]> git.sesse.net Git - nageru/blob - theme.cpp
Add some maybe-helpful comments.
[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 LiveInputWrapper_get_width(lua_State* L)
214 {
215         assert(lua_gettop(L) == 1);
216         LiveInputWrapper *input = (LiveInputWrapper *)luaL_checkudata(L, 1, "LiveInputWrapper");
217         lua_pushnumber(L, input->get_width());
218         return 1;
219 }
220
221 int LiveInputWrapper_get_height(lua_State* L)
222 {
223         assert(lua_gettop(L) == 1);
224         LiveInputWrapper *input = (LiveInputWrapper *)luaL_checkudata(L, 1, "LiveInputWrapper");
225         lua_pushnumber(L, input->get_height());
226         return 1;
227 }
228
229 int ImageInput_new(lua_State* L)
230 {
231         assert(lua_gettop(L) == 1);
232         std::string filename = checkstdstring(L, 1);
233         return wrap_lua_object<ImageInput>(L, "ImageInput", filename);
234 }
235
236 int WhiteBalanceEffect_new(lua_State* L)
237 {
238         assert(lua_gettop(L) == 0);
239         return wrap_lua_object<WhiteBalanceEffect>(L, "WhiteBalanceEffect");
240 }
241
242 int ResampleEffect_new(lua_State* L)
243 {
244         assert(lua_gettop(L) == 0);
245         return wrap_lua_object<ResampleEffect>(L, "ResampleEffect");
246 }
247
248 int PaddingEffect_new(lua_State* L)
249 {
250         assert(lua_gettop(L) == 0);
251         return wrap_lua_object<PaddingEffect>(L, "PaddingEffect");
252 }
253
254 int IntegralPaddingEffect_new(lua_State* L)
255 {
256         assert(lua_gettop(L) == 0);
257         return wrap_lua_object<IntegralPaddingEffect>(L, "IntegralPaddingEffect");
258 }
259
260 int OverlayEffect_new(lua_State* L)
261 {
262         assert(lua_gettop(L) == 0);
263         return wrap_lua_object<OverlayEffect>(L, "OverlayEffect");
264 }
265
266 int ResizeEffect_new(lua_State* L)
267 {
268         assert(lua_gettop(L) == 0);
269         return wrap_lua_object<ResizeEffect>(L, "ResizeEffect");
270 }
271
272 int MixEffect_new(lua_State* L)
273 {
274         assert(lua_gettop(L) == 0);
275         return wrap_lua_object<MixEffect>(L, "MixEffect");
276 }
277
278 int Effect_set_float(lua_State *L)
279 {
280         assert(lua_gettop(L) == 3);
281         Effect *effect = (Effect *)get_effect(L, 1);
282         std::string key = checkstdstring(L, 2);
283         float value = luaL_checknumber(L, 3);
284         if (!effect->set_float(key, value)) {
285                 luaL_error(L, "Effect refused set_float(\"%s\", %d) (invalid key?)", key.c_str(), int(value));
286         }
287         return 0;
288 }
289
290 int Effect_set_int(lua_State *L)
291 {
292         assert(lua_gettop(L) == 3);
293         Effect *effect = (Effect *)get_effect(L, 1);
294         std::string key = checkstdstring(L, 2);
295         float value = luaL_checknumber(L, 3);
296         if (!effect->set_int(key, value)) {
297                 luaL_error(L, "Effect refused set_int(\"%s\", %d) (invalid key?)", key.c_str(), int(value));
298         }
299         return 0;
300 }
301
302 int Effect_set_vec3(lua_State *L)
303 {
304         assert(lua_gettop(L) == 5);
305         Effect *effect = (Effect *)get_effect(L, 1);
306         std::string key = checkstdstring(L, 2);
307         float v[3];
308         v[0] = luaL_checknumber(L, 3);
309         v[1] = luaL_checknumber(L, 4);
310         v[2] = luaL_checknumber(L, 5);
311         if (!effect->set_vec3(key, v)) {
312                 luaL_error(L, "Effect refused set_vec3(\"%s\", %f, %f, %f) (invalid key?)", key.c_str(),
313                         v[0], v[1], v[2]);
314         }
315         return 0;
316 }
317
318 int Effect_set_vec4(lua_State *L)
319 {
320         assert(lua_gettop(L) == 6);
321         Effect *effect = (Effect *)get_effect(L, 1);
322         std::string key = checkstdstring(L, 2);
323         float v[4];
324         v[0] = luaL_checknumber(L, 3);
325         v[1] = luaL_checknumber(L, 4);
326         v[2] = luaL_checknumber(L, 5);
327         v[3] = luaL_checknumber(L, 6);
328         if (!effect->set_vec4(key, v)) {
329                 luaL_error(L, "Effect refused set_vec4(\"%s\", %f, %f, %f, %f) (invalid key?)", key.c_str(),
330                         v[0], v[1], v[2], v[3]);
331         }
332         return 0;
333 }
334
335 const luaL_Reg EffectChain_funcs[] = {
336         { "new", EffectChain_new },
337         { "add_live_input", EffectChain_add_live_input },
338         { "add_effect", EffectChain_add_effect },
339         { "finalize", EffectChain_finalize },
340         { NULL, NULL }
341 };
342
343 const luaL_Reg LiveInputWrapper_funcs[] = {
344         { "connect_signal", LiveInputWrapper_connect_signal },
345         // These are only valid during calls to get_chain and the setup chain callback.
346         { "get_width", LiveInputWrapper_get_width },
347         { "get_height", LiveInputWrapper_get_height },
348         { NULL, NULL }
349 };
350
351 const luaL_Reg ImageInput_funcs[] = {
352         { "new", ImageInput_new },
353         { "set_float", Effect_set_float },
354         { "set_int", Effect_set_int },
355         { "set_vec3", Effect_set_vec3 },
356         { "set_vec4", Effect_set_vec4 },
357         { NULL, NULL }
358 };
359
360 const luaL_Reg WhiteBalanceEffect_funcs[] = {
361         { "new", WhiteBalanceEffect_new },
362         { "set_float", Effect_set_float },
363         { "set_int", Effect_set_int },
364         { "set_vec3", Effect_set_vec3 },
365         { "set_vec4", Effect_set_vec4 },
366         { NULL, NULL }
367 };
368
369 const luaL_Reg ResampleEffect_funcs[] = {
370         { "new", ResampleEffect_new },
371         { "set_float", Effect_set_float },
372         { "set_int", Effect_set_int },
373         { "set_vec3", Effect_set_vec3 },
374         { "set_vec4", Effect_set_vec4 },
375         { NULL, NULL }
376 };
377
378 const luaL_Reg PaddingEffect_funcs[] = {
379         { "new", PaddingEffect_new },
380         { "set_float", Effect_set_float },
381         { "set_int", Effect_set_int },
382         { "set_vec3", Effect_set_vec3 },
383         { "set_vec4", Effect_set_vec4 },
384         { NULL, NULL }
385 };
386
387 const luaL_Reg IntegralPaddingEffect_funcs[] = {
388         { "new", IntegralPaddingEffect_new },
389         { "set_float", Effect_set_float },
390         { "set_int", Effect_set_int },
391         { "set_vec3", Effect_set_vec3 },
392         { "set_vec4", Effect_set_vec4 },
393         { NULL, NULL }
394 };
395
396 const luaL_Reg OverlayEffect_funcs[] = {
397         { "new", OverlayEffect_new },
398         { "set_float", Effect_set_float },
399         { "set_int", Effect_set_int },
400         { "set_vec3", Effect_set_vec3 },
401         { "set_vec4", Effect_set_vec4 },
402         { NULL, NULL }
403 };
404
405 const luaL_Reg ResizeEffect_funcs[] = {
406         { "new", ResizeEffect_new },
407         { "set_float", Effect_set_float },
408         { "set_int", Effect_set_int },
409         { "set_vec3", Effect_set_vec3 },
410         { "set_vec4", Effect_set_vec4 },
411         { NULL, NULL }
412 };
413
414 const luaL_Reg MixEffect_funcs[] = {
415         { "new", MixEffect_new },
416         { "set_float", Effect_set_float },
417         { "set_int", Effect_set_int },
418         { "set_vec3", Effect_set_vec3 },
419         { "set_vec4", Effect_set_vec4 },
420         { NULL, NULL }
421 };
422
423 }  // namespace
424
425 LiveInputWrapper::LiveInputWrapper(Theme *theme, EffectChain *chain, bool override_bounce)
426         : theme(theme)
427 {
428         ImageFormat inout_format;
429         inout_format.color_space = COLORSPACE_sRGB;
430
431         // Gamma curve depends on the input signal, and we don't really get any
432         // indications. A camera would be expected to do Rec. 709, but
433         // I haven't checked if any do in practice. However, computers _do_ output
434         // in sRGB gamma (ie., they don't convert from sRGB to Rec. 709), and
435         // I wouldn't really be surprised if most non-professional cameras do, too.
436         // So we pick sRGB as the least evil here.
437         inout_format.gamma_curve = GAMMA_sRGB;
438
439         // The Blackmagic driver docs claim that the device outputs Y'CbCr
440         // according to Rec. 601, but practical testing indicates it definitely
441         // is Rec. 709 (at least up to errors attributable to rounding errors).
442         // Perhaps 601 was only to indicate the subsampling positions, not the
443         // colorspace itself? Tested with a Lenovo X1 gen 3 as input.
444         YCbCrFormat input_ycbcr_format;
445         input_ycbcr_format.chroma_subsampling_x = 2;
446         input_ycbcr_format.chroma_subsampling_y = 1;
447         input_ycbcr_format.cb_x_position = 0.0;
448         input_ycbcr_format.cr_x_position = 0.0;
449         input_ycbcr_format.cb_y_position = 0.5;
450         input_ycbcr_format.cr_y_position = 0.5;
451         input_ycbcr_format.luma_coefficients = YCBCR_REC_709;
452         input_ycbcr_format.full_range = false;
453
454         if (override_bounce) {
455                 input = new NonBouncingYCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
456         } else {
457                 input = new YCbCrInput(inout_format, input_ycbcr_format, WIDTH, HEIGHT, YCBCR_INPUT_SPLIT_Y_AND_CBCR);
458         }
459         chain->add_input(input);
460 }
461
462 void LiveInputWrapper::connect_signal(int signal_num)
463 {
464         if (global_mixer == nullptr) {
465                 // No data yet.
466                 return;
467         }
468
469         signal_num = theme->map_signal(signal_num);
470
471         BufferedFrame frame = theme->input_state->buffered_frames[signal_num][0];
472         const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
473
474         input->set_texture_num(0, userdata->tex_y[frame.field_number]);
475         input->set_texture_num(1, userdata->tex_cbcr[frame.field_number]);
476         input->set_width(userdata->last_width[frame.field_number]);
477         input->set_height(userdata->last_height[frame.field_number]);
478 }
479
480 unsigned LiveInputWrapper::get_width() const
481 {
482         if (last_connected_signal_num == -1) {
483                 return 0;
484         }
485         BufferedFrame frame = theme->input_state->buffered_frames[last_connected_signal_num][0];
486         const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
487         return userdata->last_width[frame.field_number];
488 }
489
490 unsigned LiveInputWrapper::get_height() const
491 {
492         if (last_connected_signal_num == -1) {
493                 return 0;
494         }
495         BufferedFrame frame = theme->input_state->buffered_frames[last_connected_signal_num][0];
496         const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
497         return userdata->last_height[frame.field_number];
498 }
499
500 Theme::Theme(const char *filename, ResourcePool *resource_pool, unsigned num_cards)
501         : resource_pool(resource_pool), num_cards(num_cards)
502 {
503         L = luaL_newstate();
504         luaL_openlibs(L);
505
506         register_class("EffectChain", EffectChain_funcs); 
507         register_class("LiveInputWrapper", LiveInputWrapper_funcs); 
508         register_class("ImageInput", ImageInput_funcs);
509         register_class("WhiteBalanceEffect", WhiteBalanceEffect_funcs);
510         register_class("ResampleEffect", ResampleEffect_funcs);
511         register_class("PaddingEffect", PaddingEffect_funcs);
512         register_class("IntegralPaddingEffect", IntegralPaddingEffect_funcs);
513         register_class("OverlayEffect", OverlayEffect_funcs);
514         register_class("ResizeEffect", ResizeEffect_funcs);
515         register_class("MixEffect", MixEffect_funcs);
516
517         // Run script.
518         lua_settop(L, 0);
519         if (luaL_dofile(L, filename)) {
520                 fprintf(stderr, "error: %s\n", lua_tostring(L, -1));
521                 lua_pop(L, 1);
522                 exit(1);
523         }
524         assert(lua_gettop(L) == 0);
525
526         // Ask it for the number of channels.
527         lua_getglobal(L, "num_channels");
528
529         if (lua_pcall(L, 0, 1, 0) != 0) {
530                 fprintf(stderr, "error running function `num_channels': %s\n", lua_tostring(L, -1));
531                 exit(1);
532         }
533
534         num_channels = luaL_checknumber(L, 1);
535         lua_pop(L, 1);
536         assert(lua_gettop(L) == 0);
537 }
538
539 void Theme::register_class(const char *class_name, const luaL_Reg *funcs)
540 {
541         assert(lua_gettop(L) == 0);
542         luaL_newmetatable(L, class_name);  // mt = {}
543         lua_pushlightuserdata(L, this);
544         luaL_setfuncs(L, funcs, 1);        // for (name,f in funcs) { mt[name] = f, with upvalue {theme} }
545         lua_pushvalue(L, -1);
546         lua_setfield(L, -2, "__index");    // mt.__index = mt
547         lua_setglobal(L, class_name);      // ClassName = mt
548         assert(lua_gettop(L) == 0);
549 }
550
551 Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned height, InputState input_state) 
552 {
553         Chain chain;
554
555         unique_lock<mutex> lock(m);
556         assert(lua_gettop(L) == 0);
557         lua_getglobal(L, "get_chain");  /* function to be called */
558         lua_pushnumber(L, num);
559         lua_pushnumber(L, t);
560         lua_pushnumber(L, width);
561         lua_pushnumber(L, height);
562
563         this->input_state = &input_state;
564         if (lua_pcall(L, 4, 2, 0) != 0) {
565                 fprintf(stderr, "error running function `get_chain': %s\n", lua_tostring(L, -1));
566                 exit(1);
567         }
568
569         chain.chain = (EffectChain *)luaL_checkudata(L, -2, "EffectChain");
570         if (!lua_isfunction(L, -1)) {
571                 fprintf(stderr, "Argument #-1 should be a function\n");
572                 exit(1);
573         }
574         lua_pushvalue(L, -1);
575         shared_ptr<LuaRefWithDeleter> funcref(new LuaRefWithDeleter(&m, L, luaL_ref(L, LUA_REGISTRYINDEX)));
576         lua_pop(L, 2);
577         assert(lua_gettop(L) == 0);
578
579         chain.setup_chain = [this, funcref, input_state]{
580                 unique_lock<mutex> lock(m);
581
582                 this->input_state = &input_state;
583
584                 // Set up state, including connecting signals.
585                 lua_rawgeti(L, LUA_REGISTRYINDEX, funcref->get());
586                 if (lua_pcall(L, 0, 0, 0) != 0) {
587                         fprintf(stderr, "error running chain setup callback: %s\n", lua_tostring(L, -1));
588                         exit(1);
589                 }
590                 assert(lua_gettop(L) == 0);
591         };
592
593         // TODO: Can we do better, e.g. by running setup_chain() and seeing what it references?
594         // Actually, setup_chain does maybe hold all the references we need now anyway?
595         for (unsigned card_index = 0; card_index < num_cards; ++card_index) {
596                 for (unsigned frame_num = 0; frame_num < FRAME_HISTORY_LENGTH; ++frame_num) {
597                         chain.input_frames.push_back(input_state.buffered_frames[card_index][frame_num].frame);
598                 }
599         }
600
601         return chain;
602 }
603
604 std::string Theme::get_channel_name(unsigned channel)
605 {
606         unique_lock<mutex> lock(m);
607         lua_getglobal(L, "channel_name");
608         lua_pushnumber(L, channel);
609         if (lua_pcall(L, 1, 1, 0) != 0) {
610                 fprintf(stderr, "error running function `channel_nam': %s\n", lua_tostring(L, -1));
611                 exit(1);
612         }
613
614         std::string ret = lua_tostring(L, -1);
615         lua_pop(L, 1);
616         assert(lua_gettop(L) == 0);
617         return ret;
618 }
619
620 bool Theme::get_supports_set_wb(unsigned channel)
621 {
622         unique_lock<mutex> lock(m);
623         lua_getglobal(L, "supports_set_wb");
624         lua_pushnumber(L, channel);
625         if (lua_pcall(L, 1, 1, 0) != 0) {
626                 fprintf(stderr, "error running function `supports_set_wb': %s\n", lua_tostring(L, -1));
627                 exit(1);
628         }
629
630         bool ret = checkbool(L, -1);
631         lua_pop(L, 1);
632         assert(lua_gettop(L) == 0);
633         return ret;
634 }
635
636 void Theme::set_wb(unsigned channel, double r, double g, double b)
637 {
638         unique_lock<mutex> lock(m);
639         lua_getglobal(L, "set_wb");
640         lua_pushnumber(L, channel);
641         lua_pushnumber(L, r);
642         lua_pushnumber(L, g);
643         lua_pushnumber(L, b);
644         if (lua_pcall(L, 4, 0, 0) != 0) {
645                 fprintf(stderr, "error running function `set_wb': %s\n", lua_tostring(L, -1));
646                 exit(1);
647         }
648
649         assert(lua_gettop(L) == 0);
650 }
651
652 std::vector<std::string> Theme::get_transition_names(float t)
653 {
654         unique_lock<mutex> lock(m);
655         lua_getglobal(L, "get_transitions");
656         lua_pushnumber(L, t);
657         if (lua_pcall(L, 1, 1, 0) != 0) {
658                 fprintf(stderr, "error running function `get_transitions': %s\n", lua_tostring(L, -1));
659                 exit(1);
660         }
661
662         std::vector<std::string> ret;
663         lua_pushnil(L);
664         while (lua_next(L, -2) != 0) {
665                 ret.push_back(lua_tostring(L, -1));
666                 lua_pop(L, 1);
667         }
668         lua_pop(L, 1);
669         assert(lua_gettop(L) == 0);
670         return ret;
671 }       
672
673 int Theme::map_signal(int signal_num)
674 {
675         if (signal_num >= int(num_cards)) {
676                 if (signals_warned_about.insert(signal_num).second) {
677                         fprintf(stderr, "WARNING: Theme asked for input %d, but we only have %u card(s).\n", signal_num, num_cards);
678                         fprintf(stderr, "Mapping to card %d instead.\n", signal_num % num_cards);
679                 }
680                 signal_num %= num_cards;
681         }
682         return signal_num;
683 }
684
685 void Theme::transition_clicked(int transition_num, float t)
686 {
687         unique_lock<mutex> lock(m);
688         lua_getglobal(L, "transition_clicked");
689         lua_pushnumber(L, transition_num);
690         lua_pushnumber(L, t);
691
692         if (lua_pcall(L, 2, 0, 0) != 0) {
693                 fprintf(stderr, "error running function `transition_clicked': %s\n", lua_tostring(L, -1));
694                 exit(1);
695         }
696         assert(lua_gettop(L) == 0);
697 }
698
699 void Theme::channel_clicked(int preview_num)
700 {
701         unique_lock<mutex> lock(m);
702         lua_getglobal(L, "channel_clicked");
703         lua_pushnumber(L, preview_num);
704
705         if (lua_pcall(L, 1, 0, 0) != 0) {
706                 fprintf(stderr, "error running function `channel_clicked': %s\n", lua_tostring(L, -1));
707                 exit(1);
708         }
709         assert(lua_gettop(L) == 0);
710 }