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