This source file includes following definitions.
- chdk_luai_numdiv
- chdk_luai_nummod
- lua_script_disable_yield_hook
- lua_script_enable_yield_hook
- lua_create_usb_msg
- lua_script_reset
- lua_script_error_ptp
- lua_count_hook
- lua_script_error
- lua_script_finish
- lua_script_start
- lua_script_start_file
- lua_script_run
- lua_run_restore
- lua_get_key_arg
- on_off_value_from_lua_arg
- luaCB_set_curve_state
- luaCB_get_curve_state
- luaCB_set_curve_file
- luaCB_get_curve_file
- luaCB_set_aelock
- luaCB_set_aflock
- luaCB_set_mf
- luaCB_get_sd_over_modes
- luaCB_shoot
- action_stack_AS_LUA_SLEEP
- sleep_delay
- luaCB_sleep
- luaCB_keyfunc
- luaCB_cls
- luaCB_set_console_layout
- luaCB_set_console_autoredraw
- luaCB_console_redraw
- luaCB_get_partitionInfo
- luaCB_swap_partitions
- luaCB_get_av96
- luaCB_get_bv96
- luaCB_get_day_seconds
- luaCB_get_disk_size
- luaCB_get_dofinfo
- luaCB_get_free_disk_space
- luaCB_get_focus
- luaCB_get_iso_market
- luaCB_get_iso_mode
- luaCB_get_iso_real
- luaCB_get_jpg_count
- luaCB_get_prop
- luaCB_get_prop_str
- luaCB_get_raw_support
- luaCB_get_raw_count
- luaCB_get_sv96
- luaCB_get_tick_count
- luaCB_get_exp_count
- luaCB_get_image_dir
- luaCB_get_tv96
- luaCB_get_user_av_id
- luaCB_get_user_av96
- luaCB_get_min_av96
- luaCB_get_max_av96
- luaCB_get_current_av96
- luaCB_get_current_tv96
- luaCB_get_current_delta_sv96
- luaCB_get_current_base_sv96
- luaCB_get_nd_value_ev96
- luaCB_get_nd_current_ev96
- luaCB_get_imager_active
- luaCB_get_canon_image_format
- luaCB_get_canon_raw_support
- luaCB_get_user_tv_id
- luaCB_get_user_tv96
- luaCB_get_vbatt
- luaCB_get_zoom
- luaCB_get_parameter_data
- luaCB_get_flash_params_count
- luaCB_set_av96_direct
- luaCB_set_av96
- luaCB_set_canon_image_format
- luaCB_set_focus_interlock_bypass
- luaCB_set_focus
- luaCB_set_iso_mode
- luaCB_set_iso_real
- luaCB_set_led
- luaCB_set_nd_filter
- luaCB_set_prop
- luaCB_set_prop_str
- luaCB_set_raw_nr
- luaCB_get_raw_nr
- luaCB_set_raw
- luaCB_get_raw
- luaCB_set_sv96
- luaCB_set_tv96_direct
- luaCB_set_tv96
- luaCB_set_user_av_by_id_rel
- luaCB_set_user_av_by_id
- luaCB_set_user_av96
- luaCB_set_user_tv_by_id_rel
- luaCB_set_user_tv_by_id
- luaCB_set_user_tv96
- luaCB_set_zoom_speed
- luaCB_set_zoom_rel
- luaCB_set_zoom
- action_stack_AS_LUA_WAIT_CLICK
- luaCB_wait_click
- luaCB_is_pressed
- luaCB_is_key
- luaCB_set_exit_key
- luaCB_wheel_right
- luaCB_wheel_left
- luaCB_md_af_led_control
- luaCB_md_get_cell_diff
- luaCB_md_get_cell_val
- luaCB_md_detect_motion
- return_string_selected
- action_stack_AS_WAIT_MODULE
- luaCB_file_browser
- luaCB_textbox
- luaCB_draw_pixel
- luaCB_draw_line
- luaCB_draw_rect
- luaCB_draw_rect_filled
- luaCB_draw_ellipse
- luaCB_draw_ellipse_filled
- luaCB_draw_string
- luaCB_draw_clear
- luaCB_get_gui_screen_width
- luaCB_get_gui_screen_height
- luaCB_autostarted
- luaCB_get_autostart
- luaCB_set_autostart
- luaCB_get_usb_power
- luaCB_set_remote_timing
- luaCB_usb_force_active
- luaCB_usb_sync_wait
- luaCB_enter_alt
- luaCB_exit_alt
- luaCB_get_alt_mode
- luaCB_shut_down
- luaCB_print_screen
- luaCB_get_movie_status
- luaCB_set_movie_status
- luaCB_get_video_button
- luaCB_get_video_recording
- luaCB_get_drive_mode
- luaCB_get_focus_mode
- luaCB_get_focus_state
- luaCB_get_focus_ok
- luaCB_get_flash_mode
- luaCB_get_shooting
- luaCB_get_flash_ready
- luaCB_get_IS_mode
- luaCB_get_orientation_sensor
- luaCB_get_zoom_steps
- luaCB_get_nd_present
- luaCB_get_propset
- luaCB_get_ev
- luaCB_set_ev
- luaCB_get_histo_range
- luaCB_shot_histo_enable
- luaCB_shot_histo_write_to_file
- luaCB_get_live_histo
- luaCB_play_sound
- luaCB_get_temperature
- luaCB_get_time
- luaCB_set_clock
- luaCB_peek
- luaCB_poke
- luaCB_bitand
- luaCB_bitor
- luaCB_bitxor
- luaCB_bitshl
- luaCB_bitshri
- luaCB_bitshru
- luaCB_bitnot
- set_string_field
- set_number_field
- luaCB_get_buildinfo
- luaCB_get_mode
- luaCB_set_raw_develop
- luaCB_raw_merge_start
- luaCB_raw_merge_add_file
- luaCB_raw_merge_end
- luaCB_set_backlight
- luaCB_set_lcd_display
- luaCB_set_draw_title_line
- luaCB_get_draw_title_line
- levent_id_from_lua_arg
- levent_index_from_id_lua_arg
- luaCB_get_levent_def
- luaCB_get_levent_index
- luaCB_get_levent_def_by_index
- luaCB_post_levent_to_ui
- luaCB_post_levent_for_npt
- luaCB_set_levent_active
- luaCB_set_levent_script_mode
- luaCB_set_capture_mode_canon
- luaCB_set_capture_mode
- luaCB_is_capture_mode_valid
- luaCB_set_record
- luaCB_switch_mode_usb
- luaCB_force_analog_av
- pack_native_args
- luaCB_call_func_ptr
- luaCB_call_event_proc
- luaCB_reboot
- luaCB_get_config_value
- luaCB_set_config_value
- luaCB_set_config_autosave
- luaCB_save_config_file
- luaCB_load_config_file
- luaCB_set_file_attributes
- action_stack_AS_SCRIPT_READ_USB_MSG
- action_stack_AS_SCRIPT_WRITE_USB_MSG
- luaCB_read_usb_msg
- luaCB_write_usb_msg
- set_meminfo_num
- luaCB_get_meminfo
- luaCB_set_yield
- luaCB_get_usb_capture_support
- luaCB_init_usb_capture
- luaCB_get_usb_capture_target
- luaCB_set_usb_capture_timeout
- luaCB_iso_to_sv96
- luaCB_sv96_to_iso
- luaCB_iso_real_to_market
- luaCB_iso_market_to_real
- luaCB_sv96_real_to_market
- luaCB_sv96_market_to_real
- luaCB_aperture_to_av96
- luaCB_av96_to_aperture
- luaCB_usec_to_tv96
- luaCB_tv96_to_usec
- luaCB_seconds_to_tv96
- luaCB_shoot_hook_set
- luaCB_shoot_hook_is_ready
- luaCB_shoot_hook_continue
- luaCB_shoot_hook_count
- register_shoot_hook_fn
- register_shoot_hooks
- register_lua_funcs
- lua_set_variable
- lua_set_as_ret
1 #include "camera_info.h"
2 #include "gui.h"
3 #include "gui_draw.h"
4 #include "script.h"
5 #include "script_key_funcs.h"
6 #include "conf.h"
7 #include "shot_histogram.h"
8 #include "raw.h"
9 #include "levent.h"
10 #include "console.h"
11 #include "action_stack.h"
12 #include "ptp_chdk.h"
13 #include "lang.h"
14 #include "gui_lang.h"
15 #include "histogram.h"
16 #include "shooting.h"
17 #include "autoiso.h"
18 #include "remotecap.h"
19 #include "battery.h"
20 #include "temperature.h"
21 #include "backlight.h"
22 #include "keyboard.h"
23 #include "shutdown.h"
24 #include "sound.h"
25 #include "modes.h"
26 #include "sd_card.h"
27 #include "clock.h"
28 #include "lens.h"
29 #include "properties.h"
30 #include "file_counter.h"
31 #include "debug_led.h"
32 #include "meminfo.h"
33 #include "callfunc.h"
34 #include "usb_remote.h"
35 #include "time.h"
36
37 #include "script_api.h"
38 #include "curves.h"
39 #include "motion_detector.h"
40 #include "raw_merge.h"
41 #include "gui_fselect.h"
42 #include "gui_tbox.h"
43 #include "module_def.h"
44 #include "luascript.h"
45 #include "script_shoot_hook.h"
46 #include "rawhookops.h"
47
48 #include "lualib.h"
49 #include "lauxlib.h"
50 #include "lstate.h"
51
52 #ifdef THUMB_FW
53
54
55 int chdk_luai_numdiv(int a, int b) {
56 if(!a) {
57 return 0;
58 }
59 if(b) {
60 return a/b;
61 }
62 if(a>0) {
63 return 0x7FFFFFFF;
64 } else {
65 return 0x80000000;
66 }
67 }
68 int chdk_luai_nummod(int a, int b) {
69 if(!b) {
70 return 0;
71 }
72 return a%b;
73 }
74 #endif
75
76 lua_State* L;
77 lua_State* Lt;
78
79 extern void register_lua_funcs( lua_State* L );
80
81 static int lua_script_is_ptp;
82 static int ptp_saved_alt_state;
83 static int run_first_resume;
84 static int run_start_tick;
85 static unsigned run_hook_count;
86 #define YIELD_CHECK_COUNT 100
87 #define YIELD_MAX_COUNT_DEFAULT 25
88 #define YIELD_MAX_MS_DEFAULT 10
89 static unsigned yield_max_count;
90 static unsigned yield_max_ms;
91 static int yield_hook_enabled;
92
93 static void lua_script_disable_yield_hook(void) {
94 yield_hook_enabled = 0;
95 }
96 static void lua_script_enable_yield_hook(void) {
97 yield_hook_enabled = 1;
98 }
99
100
101
102 static ptp_script_msg *lua_create_usb_msg( lua_State* L, int index, unsigned msgtype) {
103
104 unsigned datatype, datasize = 4;
105 int ivalue = 0;
106 void *data = &ivalue;
107 int ltype = lua_type(L,index);
108 switch(ltype) {
109 case LUA_TNONE:
110 return NULL;
111 break;
112 case LUA_TNIL:
113 datatype = PTP_CHDK_TYPE_NIL;
114 break;
115 case LUA_TBOOLEAN:
116 datatype = PTP_CHDK_TYPE_BOOLEAN;
117 ivalue = lua_toboolean(L,index);
118 break;
119 case LUA_TNUMBER:
120 datatype = PTP_CHDK_TYPE_INTEGER;
121 ivalue = lua_tonumber(L,index);
122 break;
123 case LUA_TSTRING:
124 datatype = PTP_CHDK_TYPE_STRING;
125 data = (char *)lua_tolstring(L,index,&datasize);
126 break;
127
128
129
130
131 case LUA_TTABLE: {
132 int result;
133 lua_script_disable_yield_hook();
134 lua_getglobal(L, "usb_msg_table_to_string");
135 lua_pushvalue(L, index);
136 result = lua_pcall(L,1,1,0);
137 lua_script_enable_yield_hook();
138 if( result ) {
139
140 if( msgtype == PTP_CHDK_S_MSGTYPE_USER ) {
141 luaL_error(L,lua_tostring(L,-1));
142 return NULL;
143 } else {
144 msgtype = PTP_CHDK_S_MSGTYPE_ERR;
145 datatype = PTP_CHDK_S_ERRTYPE_RUN;
146 data = (char *)lua_tolstring(L,-1,&datasize);
147 break;
148 }
149 }
150
151
152 if ( !lua_isstring(L,-1) ) {
153 return NULL;
154 }
155 datatype = PTP_CHDK_TYPE_TABLE;
156 data = (char *)lua_tolstring(L,-1,&datasize);
157 lua_pop(L,1);
158 }
159 break;
160 default:
161 datatype = PTP_CHDK_TYPE_UNSUPPORTED;
162 data = (char *)lua_typename(L,ltype);
163 datasize = strlen(data);
164 }
165 return ptp_script_create_msg(msgtype,datatype,datasize,data);
166 }
167
168 void lua_script_reset()
169 {
170
171 if(lua_script_is_ptp) {
172 if(ptp_saved_alt_state) {
173 enter_alt(0);
174 } else {
175 exit_alt();
176 }
177 }
178
179 script_shoot_hooks_reset();
180 lua_close( L );
181 L = 0;
182 }
183
184 void lua_script_error_ptp(int runtime, const char *err) {
185 if(runtime) {
186 ptp_script_write_error_msg(PTP_CHDK_S_ERRTYPE_RUN, err);
187 } else {
188 ptp_script_write_error_msg(PTP_CHDK_S_ERRTYPE_COMPILE, err);
189 lua_script_reset();
190 }
191 }
192
193 static void lua_count_hook(lua_State *L, __attribute__ ((unused))lua_Debug *ar)
194 {
195 run_hook_count++;
196 if( L->nCcalls > L->baseCcalls || !yield_hook_enabled )
197 return;
198 if(run_hook_count >= yield_max_count || (unsigned)(get_tick_count() - run_start_tick) >= yield_max_ms)
199 lua_yield( L, 0 );
200 }
201
202 int lua_script_error(lua_State *Lt,int runtime)
203 {
204 const char *err = lua_tostring( Lt, -1 );
205
206 if(err)
207 {
208 if(!*err)
209 {
210 script_console_add_error( (long)"ERROR: empty error message" );
211 }
212 else
213 {
214 script_console_add_error( (long)err );
215 }
216 }
217 else
218 {
219 script_console_add_error( (long)"ERROR: NULL error message" );
220 }
221
222 if (lua_script_is_ptp)
223 {
224 lua_script_error_ptp(runtime,err);
225 }
226 else
227 {
228 if (runtime)
229 {
230 if(conf.debug_lua_restart_on_error) {
231 script_end();
232 script_start_gui(0);
233 return SCRIPT_RUN_RUNNING;
234 }
235 }
236 else
237 {
238 lua_script_reset();
239 }
240 }
241
242 script_console_add_error(LANG_CONSOLE_TEXT_TERMINATED);
243 return SCRIPT_RUN_ERROR;
244 }
245
246
247
248 void lua_script_finish(lua_State *L)
249 {
250 if(lua_script_is_ptp) {
251
252 int i,end = lua_gettop(L);
253 for(i=1;i<=end; i++) {
254 ptp_script_msg *msg = lua_create_usb_msg(L,i,PTP_CHDK_S_MSGTYPE_RET);
255
256
257 if(msg) {
258 ptp_script_write_msg(msg);
259
260 if(msg->type != PTP_CHDK_S_MSGTYPE_RET) {
261 break;
262 }
263 } else {
264 ptp_script_write_error_msg(PTP_CHDK_S_ERRTYPE_RUN, "error creating return msg");
265 break;
266 }
267 }
268 }
269 }
270
271 int lua_script_start( char const* script, int ptp )
272 {
273 script_shoot_hooks_reset();
274 lua_script_is_ptp = ptp;
275 if(ptp) {
276 ptp_saved_alt_state = camera_info.state.gui_mode_alt;
277
278 enter_alt(1);
279 }
280 L = lua_open();
281 luaL_openlibs( L );
282 register_lua_funcs( L );
283
284 Lt = lua_newthread( L );
285 lua_setfield( L, LUA_REGISTRYINDEX, "Lt" );
286 if( luaL_loadstring( Lt, script ) != 0 )
287 {
288 lua_script_error(Lt,0);
289 return 0;
290 }
291 lua_sethook(Lt, lua_count_hook, LUA_MASKCOUNT, YIELD_CHECK_COUNT );
292 lua_script_enable_yield_hook();
293 run_first_resume = 1;
294 yield_max_count = YIELD_MAX_COUNT_DEFAULT;
295 yield_max_ms = YIELD_MAX_MS_DEFAULT;
296 return 1;
297 }
298
299 int lua_script_start_file(char const* filename)
300 {
301 static char loader[256];
302 char *wrapper = "";
303 if ((script_version.major == 1) && (script_version.minor == 3))
304 wrapper = "require'wrap13' ";
305 sprintf(loader, "%slocal s,e=loadfile'%s' collectgarbage() if not s then error(e) end s()", wrapper, filename);
306 return lua_script_start(loader, 0);
307 }
308
309
310 int lua_script_run(void)
311 {
312 int Lres;
313 int top;
314 if (run_first_resume) {
315 run_first_resume = 0;
316 top = 0;
317 } else {
318 top = lua_gettop(Lt);
319 }
320 run_start_tick = get_tick_count();
321 run_hook_count = 0;
322 Lres = lua_resume( Lt, top );
323
324 if (Lres == LUA_YIELD)
325 {
326
327 }
328 else if (Lres != 0)
329 {
330 return lua_script_error(Lt,1);
331 }
332 else
333 {
334
335 lua_script_finish(Lt);
336
337 if (lua_script_is_ptp == 0)
338 script_console_add_error(LANG_CONSOLE_TEXT_FINISHED);
339 return SCRIPT_RUN_ENDED;
340 }
341
342 return SCRIPT_RUN_RUNNING;
343 }
344
345
346
347 int lua_run_restore()
348 {
349 lua_getglobal(Lt, "restore");
350 if (lua_isfunction(Lt, -1)) {
351 if (lua_pcall( Lt, 0, 0, 0 )) {
352 script_console_add_line( (long)lua_tostring( Lt, -1 ) );
353 }
354 if (lua_script_is_ptp == 0)
355 script_console_add_error(LANG_CONSOLE_TEXT_FINISHED);
356 }
357 return 0;
358 }
359
360
361 static int lua_get_key_arg( lua_State * L, int narg )
362 {
363 int k = script_keyid_by_name( luaL_checkstring( L, narg ) );
364 if(!k)
365 luaL_error( L, "unknown key" );
366 return k;
367 }
368
369
370
371
372
373
374
375 static unsigned on_off_value_from_lua_arg( lua_State* L, int index)
376 {
377 if( lua_isboolean(L,index) ) {
378 return lua_toboolean(L,index);
379 }
380 else {
381 return luaL_checknumber(L,index);
382 }
383 }
384
385 static int luaCB_set_curve_state( lua_State* L )
386 {
387 libcurves->curve_set_mode(luaL_checknumber( L, 1 ));
388 return 0;
389 }
390
391 static int luaCB_get_curve_state( lua_State* L )
392 {
393 lua_pushnumber(L,conf.curve_enable);
394 return 1;
395 }
396
397 static int luaCB_set_curve_file( lua_State* L )
398 {
399 size_t l;
400 const char *s = luaL_checklstring(L, 1, &l);
401 libcurves->curve_set_file(s);
402 return 0;
403 }
404
405 static int luaCB_get_curve_file( lua_State* L )
406 {
407 lua_pushstring(L,conf.curve_file);
408 return 1;
409 }
410
411 static int luaCB_set_aelock(lua_State* L)
412 {
413 int val = on_off_value_from_lua_arg(L, 1);
414 if (val>0) DoAELock();
415 else UnlockAE();
416 return 0;
417 }
418
419 static int luaCB_set_aflock(lua_State* L)
420 {
421 int val = on_off_value_from_lua_arg(L, 1);
422 if (val>0) DoAFLock();
423 else UnlockAF();
424 return 0;
425 }
426
427 static int luaCB_set_mf(lua_State* L)
428 {
429 int val = on_off_value_from_lua_arg(L, 1);
430 if (val>0) val=DoMFLock();
431 else val=UnlockMF();
432 lua_pushnumber(L, val);
433 return 1;
434 }
435
436 static int luaCB_get_sd_over_modes( lua_State* L )
437 {
438 lua_pushnumber(L,sd_over_modes());
439 return 1;
440 }
441
442 static int luaCB_shoot( lua_State* L )
443 {
444 action_push_shoot(luaL_optnumber( L, 1, 1 ));
445 return lua_yield( L, 0 );
446 }
447
448
449 static int action_stack_AS_LUA_SLEEP()
450 {
451 if (get_tick_count() >= action_top(2))
452 {
453 action_pop_func(1);
454 return 1;
455 }
456 return 0;
457 }
458
459 static int sleep_delay(int delay)
460 {
461
462 if (delay == -1)
463 delay = 86400000;
464
465 if (delay > 0)
466 return delay + get_tick_count();
467
468 return 0;
469 }
470
471 static int luaCB_sleep( lua_State* L )
472 {
473 int delay = sleep_delay(luaL_checknumber( L, 1 ));
474
475 if (delay > 0)
476 {
477 action_push(delay);
478 action_push_func(action_stack_AS_LUA_SLEEP);
479 }
480
481 return lua_yield( L, 0 );
482 }
483
484
485 static int luaCB_keyfunc( lua_State* L )
486 {
487 void* func = lua_touserdata( L, lua_upvalueindex(1) );
488 ((void(*)(long))func)( lua_get_key_arg( L, 1 ) );
489 return lua_yield( L, 0 );
490 }
491
492 static int luaCB_cls( __attribute__ ((unused))lua_State* L )
493 {
494 console_clear();
495 return 0;
496 }
497
498 static int luaCB_set_console_layout( lua_State* L )
499 {
500 console_set_layout(luaL_checknumber( L, 1 ),luaL_checknumber( L, 2 ),luaL_checknumber( L, 3 ),luaL_checknumber( L, 4 ));
501 return 0;
502 }
503
504 static int luaCB_set_console_autoredraw( lua_State* L )
505 {
506 console_set_autoredraw(luaL_checknumber( L, 1 ));
507 return 0;
508 }
509
510 static int luaCB_console_redraw( __attribute__ ((unused))lua_State* L )
511 {
512 console_redraw();
513 return 0;
514 }
515
516 static int luaCB_get_partitionInfo( lua_State* L )
517 {
518 if (camera_info.cam_has_multipart)
519 {
520 lua_createtable(L, 0, 4);
521 SET_INT_FIELD("count", get_part_count());
522 SET_INT_FIELD("active", get_active_partition());
523 SET_INT_FIELD("type", get_part_type());
524 SET_INT_FIELD("size", GetTotalCardSpaceKb()>>10);
525 return 1;
526 }
527 return 0;
528 }
529
530 static int luaCB_swap_partitions( lua_State* L )
531 {
532 if (camera_info.cam_has_multipart)
533 {
534 int partNr;
535
536 if( lua_gettop(L)==1 )
537 {
538 partNr = luaL_checknumber(L, 1);
539 }
540 else
541 {
542 int partCount = get_part_count();
543 partNr = get_active_partition()+1;
544 if( partNr > partCount ) partNr = 1;
545 }
546 lua_pushboolean(L, swap_partitions(partNr));
547 return 1;
548 }
549 return 0;
550 }
551
552 static int luaCB_get_av96( lua_State* L )
553 {
554 lua_pushnumber( L, shooting_get_av96() );
555 return 1;
556 }
557
558 static int luaCB_get_bv96( lua_State* L )
559 {
560 lua_pushnumber( L, shooting_get_bv96() );
561 return 1;
562 }
563
564 static int luaCB_get_day_seconds( lua_State* L )
565 {
566 struct tm *ttm;
567 ttm = get_localtime();
568 lua_pushnumber( L, ttm->tm_hour * 3600 + ttm->tm_min * 60 + ttm->tm_sec );
569 return 1;
570 }
571
572 static int luaCB_get_disk_size( lua_State* L )
573 {
574 lua_pushnumber( L, GetTotalCardSpaceKb() );
575 return 1;
576 }
577
578 static int luaCB_get_dofinfo( lua_State* L )
579 {
580 shooting_update_dof_values();
581 lua_createtable(L, 0, 12);
582 SET_BOOL_FIELD("hyp_valid", (camera_info.dof_values.hyperfocal_valid!=0));
583 SET_BOOL_FIELD("focus_valid", (camera_info.dof_values.distance_valid!=0));
584 SET_INT_FIELD("aperture", camera_info.dof_values.aperture_value);
585 SET_INT_FIELD("coc", camera_info.circle_of_confusion);
586 SET_INT_FIELD("focal_length", camera_info.dof_values.focal_length);
587 SET_INT_FIELD("eff_focal_length", get_effective_focal_length(lens_get_zoom_point()));
588 SET_INT_FIELD("focus", camera_info.dof_values.subject_distance);
589 SET_INT_FIELD("near", camera_info.dof_values.near_limit);
590 SET_INT_FIELD("far", camera_info.dof_values.far_limit);
591 SET_INT_FIELD("hyp_dist", camera_info.dof_values.hyperfocal_distance);
592 SET_INT_FIELD("dof", camera_info.dof_values.depth_of_field);
593 SET_INT_FIELD("min_stack_dist", camera_info.dof_values.min_stack_distance);
594 return 1;
595 }
596
597 static int luaCB_get_free_disk_space( lua_State* L )
598 {
599 lua_pushnumber( L, GetFreeCardSpaceKb() );
600 return 1;
601 }
602
603 static int luaCB_get_focus( lua_State* L )
604 {
605 lua_pushnumber( L, shooting_get_subject_distance() );
606 return 1;
607 }
608
609 static int luaCB_get_iso_market( lua_State* L )
610 {
611 lua_pushnumber( L, shooting_get_iso_market() );
612 return 1;
613 }
614
615 static int luaCB_get_iso_mode( lua_State* L )
616 {
617 lua_pushnumber( L, shooting_get_iso_mode() );
618 return 1;
619 }
620
621 static int luaCB_get_iso_real( lua_State* L )
622 {
623 lua_pushnumber( L, shooting_get_iso_real() );
624 return 1;
625 }
626
627 static int luaCB_get_jpg_count( lua_State* L )
628 {
629 lua_pushnumber( L, GetJpgCount() );
630 return 1;
631 }
632
633
634
635
636
637
638 static int luaCB_get_prop( lua_State* L )
639 {
640 lua_pushnumber( L, shooting_get_prop( luaL_checknumber( L, 1 ) ) );
641 return 1;
642 }
643
644
645
646
647
648
649
650 static int luaCB_get_prop_str( lua_State* L ) {
651 void *buf;
652 unsigned size;
653 unsigned prop_id = luaL_checknumber( L, 1 );
654 size = luaL_checknumber( L, 2 );
655 buf = malloc(size);
656 if(!buf) {
657 return luaL_error( L, "malloc failed in luaCB_get_prop" );
658 }
659 if(get_property_case(prop_id,buf,size) == 0) {
660 lua_pushlstring( L, buf, size );
661 } else {
662 lua_pushboolean( L, 0);
663 }
664 free(buf);
665 return 1;
666 }
667
668
669
670
671
672
673
674 static int luaCB_get_raw_support( lua_State* L )
675 {
676 lua_pushboolean( L, is_raw_possible() && !camera_info.state.mode_play );
677 return 1;
678 }
679
680 static int luaCB_get_raw_count( lua_State* L )
681 {
682 lua_pushnumber( L, GetRawCount() );
683 return 1;
684 }
685
686 static int luaCB_get_sv96( lua_State* L )
687 {
688 lua_pushnumber( L, shooting_get_sv96_real() );
689 return 1;
690 }
691
692 static int luaCB_get_tick_count( lua_State* L )
693 {
694 lua_pushnumber( L, get_tick_count() );
695 return 1;
696 }
697
698 static int luaCB_get_exp_count( lua_State* L )
699 {
700 lua_pushnumber( L, get_exposure_counter() );
701 return 1;
702 }
703
704 static int luaCB_get_image_dir( lua_State* L )
705 {
706 char dir[32];
707 get_target_dir_name(dir);
708 lua_pushstring( L, dir );
709 return 1;
710 }
711
712 static int luaCB_get_tv96( lua_State* L )
713 {
714 lua_pushnumber( L, shooting_get_tv96() );
715 return 1;
716 }
717
718 static int luaCB_get_user_av_id( lua_State* L )
719 {
720 lua_pushnumber( L, shooting_get_user_av_id() );
721 return 1;
722 }
723
724 static int luaCB_get_user_av96( lua_State* L )
725 {
726 lua_pushnumber( L, shooting_get_user_av96() );
727 return 1;
728 }
729
730
731 static int luaCB_get_min_av96( lua_State* L )
732 {
733 short av=shooting_get_min_av96();
734 if(av < 0) {
735 lua_pushnil(L);
736 } else {
737 lua_pushnumber( L, av );
738 }
739 return 1;
740 }
741
742
743 static int luaCB_get_max_av96( lua_State* L )
744 {
745 short av=shooting_get_max_av96();
746 if(av < 0) {
747 lua_pushnil(L);
748 } else {
749 lua_pushnumber( L, av );
750 }
751 return 1;
752 }
753
754
755 static int luaCB_get_current_av96( lua_State* L )
756 {
757 lua_pushnumber( L, shooting_get_current_av96() );
758 return 1;
759 }
760
761
762
763 static int luaCB_get_current_tv96( lua_State* L )
764 {
765 long tv = shooting_get_current_tv96();
766 if( tv == SHOOTING_TV96_INVALID) {
767 lua_pushnil(L);
768 } else {
769 lua_pushnumber( L, tv);
770 }
771 return 1;
772 }
773
774
775 static int luaCB_get_current_delta_sv96( lua_State* L )
776 {
777 lua_pushnumber( L, shooting_get_current_delta_sv96() );
778 return 1;
779 }
780
781
782 static int luaCB_get_current_base_sv96( lua_State* L )
783 {
784 lua_pushnumber( L, shooting_get_current_base_sv96() );
785 return 1;
786 }
787
788
789
790 static int luaCB_get_nd_value_ev96( lua_State* L )
791 {
792 lua_pushnumber( L, shooting_get_nd_value_ev96() );
793 return 1;
794 }
795
796
797 static int luaCB_get_nd_current_ev96( lua_State* L )
798 {
799 lua_pushnumber( L, shooting_get_nd_current_ev96() );
800 return 1;
801 }
802
803
804 static int luaCB_get_imager_active( lua_State* L )
805 {
806 lua_pushboolean( L, shooting_get_imager_active() );
807 return 1;
808 }
809
810
811 static int luaCB_get_canon_image_format( lua_State* L )
812 {
813 lua_pushnumber( L, shooting_get_canon_image_format() );
814 return 1;
815 }
816
817
818 static int luaCB_get_canon_raw_support( lua_State* L )
819 {
820 lua_pushboolean(L, camera_info.cam_canon_raw);
821 return 1;
822 }
823
824 static int luaCB_get_user_tv_id( lua_State* L )
825 {
826 lua_pushnumber( L, shooting_get_user_tv_id() );
827 return 1;
828 }
829
830 static int luaCB_get_user_tv96( lua_State* L )
831 {
832 lua_pushnumber( L, shooting_get_user_tv96() );
833 return 1;
834 }
835
836 static int luaCB_get_vbatt( lua_State* L )
837 {
838 lua_pushnumber( L, stat_get_vbatt() );
839 return 1;
840 }
841
842 static int luaCB_get_zoom( lua_State* L )
843 {
844 lua_pushnumber( L, shooting_get_zoom() );
845 return 1;
846 }
847
848 static int luaCB_get_parameter_data( lua_State* L )
849 {
850 unsigned size;
851 unsigned id = luaL_checknumber( L, 1 );
852 unsigned val;
853
854 size = get_parameter_size(id);
855 if (size == 0) {
856
857 return 0;
858 }
859 if (size >= 1 && size <= 4) {
860 val = 0;
861 get_parameter_data( id, &val, size );
862 lua_pushlstring( L, (char *)&val, size );
863
864 lua_pushnumber( L, val );
865 return 2;
866 }
867 else {
868 char *buf = malloc(size);
869 if(!buf) {
870 luaL_error( L, "malloc failed in luaCB_get_parameter_data" );
871 }
872 get_parameter_data( id, buf, size );
873 lua_pushlstring( L, buf, size );
874 free(buf);
875 return 1;
876 }
877 }
878
879 static int luaCB_get_flash_params_count( lua_State* L )
880 {
881 lua_pushnumber( L, get_flash_params_count() );
882 return 1;
883 }
884
885 static int luaCB_set_av96_direct( lua_State* L )
886 {
887 shooting_set_av96_direct( luaL_checknumber( L, 1 ), shooting_in_progress()?SET_NOW:SET_LATER );
888 return 0;
889 }
890
891 static int luaCB_set_av96( lua_State* L )
892 {
893 shooting_set_av96( luaL_checknumber( L, 1 ), shooting_in_progress()?SET_NOW:SET_LATER );
894 return 0;
895 }
896
897
898
899
900 static int luaCB_set_canon_image_format( lua_State* L )
901 {
902 lua_pushboolean( L, shooting_set_canon_image_format(luaL_checknumber( L, 1 )) );
903 return 1;
904 }
905
906 static int luaCB_set_focus_interlock_bypass( lua_State* L )
907 {
908 set_focus_bypass(on_off_value_from_lua_arg( L, 1 ));
909 return 0;
910 }
911
912 static int luaCB_set_focus( lua_State* L )
913 {
914 int sd = luaL_checknumber( L, 1 );
915
916
917 if(!shooting_can_focus())
918 {
919 lua_pushboolean(L, 0);
920 return 1;
921 }
922
923
924 if (shooting_get_prop(camera_info.props.af_lock)
925 || shooting_get_focus_mode()
926 || camera_info.state.mode_video)
927 {
928 shooting_set_focus(sd, SET_NOW);
929 }
930 else
931 {
932
933 shooting_set_focus(sd, SET_LATER);
934 }
935 lua_pushboolean(L, 1);
936 return 1;
937 }
938
939 static int luaCB_set_iso_mode( lua_State* L )
940 {
941 shooting_set_iso_mode( luaL_checknumber( L, 1 ) );
942 return 0;
943 }
944
945 static int luaCB_set_iso_real( lua_State* L )
946 {
947 shooting_set_iso_real( luaL_checknumber( L, 1 ), shooting_in_progress()?SET_NOW:SET_LATER );
948 return 0;
949 }
950
951 static int luaCB_set_led( lua_State* L )
952 {
953 int to, to1, to2;
954 to = luaL_checknumber( L, 1 );
955 to1 = luaL_checknumber( L, 2 );
956 to2 = 200;
957 if( lua_isnumber( L, 3 ) )
958 to2 = lua_tonumber( L, 3 );
959 camera_set_led(to, to1, to2);
960 return 0;
961 }
962
963 static int luaCB_set_nd_filter( lua_State* L )
964 {
965 shooting_set_nd_filter_state( luaL_checknumber( L, 1 ), shooting_in_progress()?SET_NOW:SET_LATER );
966 return 0;
967 }
968
969
970
971
972
973 static int luaCB_set_prop( lua_State* L )
974 {
975 shooting_set_prop(luaL_checknumber( L, 1 ), luaL_checknumber( L, 2 ));
976 return 0;
977 }
978
979
980
981
982
983
984
985 static int luaCB_set_prop_str( lua_State *L ) {
986 int prop_id;
987 unsigned len;
988 const char *str;
989 prop_id = luaL_checknumber( L, 1 );
990 str = luaL_checklstring( L, 2, &len );
991 if(str && len > 0) {
992 lua_pushboolean( L, (set_property_case(prop_id,(void *)str,len) == 0));
993 } else {
994 return luaL_error( L, "invalid value");
995 }
996 return 1;
997 }
998
999 static int luaCB_set_raw_nr( lua_State* L )
1000 {
1001 conf.raw_nr = luaL_checknumber( L, 1 );
1002 return 0;
1003 }
1004
1005 static int luaCB_get_raw_nr( lua_State* L )
1006 {
1007 lua_pushnumber( L, conf.raw_nr );
1008 return 1;
1009 }
1010
1011 static int luaCB_set_raw( lua_State* L )
1012 {
1013 conf.save_raw = on_off_value_from_lua_arg( L, 1 );
1014 return 0;
1015 }
1016
1017 static int luaCB_get_raw( lua_State* L )
1018 {
1019 lua_pushboolean( L, conf.save_raw );
1020 return 1;
1021 }
1022
1023 static int luaCB_set_sv96( lua_State* L )
1024 {
1025 shooting_set_sv96(luaL_checknumber( L, 1 ), shooting_in_progress()?SET_NOW:SET_LATER );
1026 return 0;
1027 }
1028
1029 static int luaCB_set_tv96_direct( lua_State* L )
1030 {
1031 shooting_set_tv96_direct(luaL_checknumber( L, 1 ), shooting_in_progress()?SET_NOW:SET_LATER );
1032 return 0;
1033 }
1034
1035 static int luaCB_set_tv96( lua_State* L )
1036 {
1037 shooting_set_tv96(luaL_checknumber( L, 1 ), shooting_in_progress()?SET_NOW:SET_LATER );
1038 return 0;
1039 }
1040
1041 static int luaCB_set_user_av_by_id_rel( lua_State* L )
1042 {
1043 shooting_set_user_av_by_id_rel(luaL_checknumber( L, 1 ));
1044 return 0;
1045 }
1046
1047 static int luaCB_set_user_av_by_id( lua_State* L )
1048 {
1049 shooting_set_user_av_by_id(luaL_checknumber( L, 1 ));
1050 return 0;
1051 }
1052
1053 static int luaCB_set_user_av96( lua_State* L )
1054 {
1055 shooting_set_user_av96(luaL_checknumber( L, 1 ));
1056 return 0;
1057 }
1058
1059 static int luaCB_set_user_tv_by_id_rel( lua_State* L )
1060 {
1061 shooting_set_user_tv_by_id_rel(luaL_checknumber( L, 1 ));
1062 return 0;
1063 }
1064
1065 static int luaCB_set_user_tv_by_id( lua_State* L )
1066 {
1067 shooting_set_user_tv_by_id(luaL_checknumber( L, 1 ));
1068 return 0;
1069 }
1070
1071 static int luaCB_set_user_tv96( lua_State* L )
1072 {
1073 shooting_set_user_tv96(luaL_checknumber( L, 1 ));
1074 return 0;
1075 }
1076
1077 static int luaCB_set_zoom_speed( lua_State* L )
1078 {
1079 shooting_set_zoom_speed(luaL_checknumber( L, 1 ));
1080 return 0;
1081 }
1082
1083 static int luaCB_set_zoom_rel( lua_State* L )
1084 {
1085 shooting_set_zoom_rel(luaL_checknumber( L, 1 ));
1086 return 0;
1087 }
1088
1089 static int luaCB_set_zoom( lua_State* L )
1090 {
1091 shooting_set_zoom(luaL_checknumber( L, 1 ));
1092 return 0;
1093 }
1094
1095
1096 static int action_stack_AS_LUA_WAIT_CLICK()
1097 {
1098
1099 if ((get_tick_count() >= action_top(2)) || camera_info.state.kbd_last_clicked)
1100 {
1101
1102 if (!camera_info.state.kbd_last_clicked)
1103 camera_info.state.kbd_last_clicked=0xFFFF;
1104 else
1105 camera_info.state.kbd_last_checked_time = camera_info.state.kbd_last_clicked_time;
1106
1107 action_pop_func(1);
1108 return 1;
1109 }
1110
1111 return 0;
1112 }
1113
1114 static int luaCB_wait_click( lua_State* L )
1115 {
1116 int delay = luaL_optnumber( L, 1, 0 );
1117 if (delay == 0) delay = -1;
1118 delay = sleep_delay(delay);
1119
1120
1121 if (camera_info.state.kbd_last_clicked_time <= camera_info.state.kbd_last_checked_time)
1122 {
1123 camera_info.state.kbd_last_clicked = 0;
1124 }
1125
1126
1127 action_push(delay);
1128 action_push_func(action_stack_AS_LUA_WAIT_CLICK);
1129
1130
1131 if (action_stack_AS_LUA_WAIT_CLICK() == 0)
1132 {
1133 return lua_yield( L, 0 );
1134 }
1135
1136 return 0;
1137 }
1138
1139 static int luaCB_is_pressed( lua_State* L )
1140 {
1141 lua_pushboolean( L, script_key_is_pressed(lua_get_key_arg( L, 1 )));
1142 return 1;
1143 }
1144
1145 static int luaCB_is_key( lua_State* L )
1146 {
1147 lua_pushboolean( L, script_key_is_clicked(lua_get_key_arg( L, 1 )));
1148 return 1;
1149 }
1150
1151 static int luaCB_set_exit_key( lua_State* L )
1152 {
1153
1154 script_set_terminate_key(lua_get_key_arg( L, 1 ),luaL_checkstring( L, 1));
1155 return 0;
1156 }
1157
1158 static int luaCB_wheel_right( __attribute__ ((unused))lua_State* L )
1159 {
1160 JogDial_CW();
1161 return 0;
1162 }
1163
1164 static int luaCB_wheel_left( __attribute__ ((unused))lua_State* L )
1165 {
1166 JogDial_CCW();
1167 return 0;
1168 }
1169
1170 static int luaCB_md_af_led_control( lua_State* L )
1171 {
1172 camera_info.perf.md_af_on_delay = luaL_checknumber( L, 1 );
1173 camera_info.perf.md_af_on_time = luaL_checknumber( L, 2 );
1174 if ((camera_info.perf.md_af_on_time > 0) && (camera_info.cam_af_led >= 0)) {
1175 camera_info.perf.md_af_tuning = 1;
1176
1177 memset(&camera_info.perf.af_led,0,sizeof(camera_info.perf.af_led));
1178 camera_info.perf.af_led.min = 0xFFFFFFFF;
1179 } else {
1180 camera_info.perf.md_af_tuning = 0;
1181 }
1182 return 0;
1183 }
1184
1185 static int luaCB_md_get_cell_diff( lua_State* L )
1186 {
1187 lua_pushnumber( L, libmotiondetect->md_get_cell_diff(luaL_checknumber(L,1), luaL_checknumber(L,2)));
1188 return 1;
1189 }
1190
1191
1192 static int luaCB_md_get_cell_val( lua_State* L )
1193 {
1194 lua_pushnumber( L, libmotiondetect->md_get_cell_val(luaL_checknumber(L,1), luaL_checknumber(L,2)));
1195 return 1;
1196 }
1197
1198 static int luaCB_md_detect_motion( lua_State* L )
1199 {
1200 int columns = (luaL_optnumber(L,1,6));
1201 int rows = (luaL_optnumber(L,2,4));
1202 int pixel_measure_mode = (luaL_optnumber(L,3,1));
1203 int detection_timeout = (luaL_optnumber(L,4,10000));
1204 int measure_interval = (luaL_optnumber(L,5,7));
1205 int threshold = (luaL_optnumber(L,6,10));
1206 int draw_grid = (luaL_optnumber(L,7,1));
1207
1208
1209 int clipping_region_mode = (luaL_optnumber(L,9,0));
1210 int clipping_region_column1 = (luaL_optnumber(L,10,0));
1211 int clipping_region_row1 = (luaL_optnumber(L,11,0));
1212 int clipping_region_column2 = (luaL_optnumber(L,12,0));
1213 int clipping_region_row2 = (luaL_optnumber(L,13,0));
1214 int parameters = (luaL_optnumber(L,14,1));
1215 int pixels_step = (luaL_optnumber(L,15,6));
1216 int msecs_before_trigger = (luaL_optnumber(L,16,0));
1217
1218 if (libmotiondetect->md_init_motion_detector(
1219 columns, rows, pixel_measure_mode, detection_timeout,
1220 measure_interval, threshold, draw_grid,
1221 clipping_region_mode,
1222 clipping_region_column1, clipping_region_row1,
1223 clipping_region_column2, clipping_region_row2,
1224 parameters, pixels_step, msecs_before_trigger
1225 ))
1226 return lua_yield(L, 0);
1227 else
1228 return luaL_error( L, "md_init_motion_detector failed" );
1229 }
1230
1231 static void return_string_selected(const char *str) {
1232
1233
1234
1235 camera_info.state.state_kbd_script_run = SCRIPT_STATE_RAN;
1236
1237
1238 lua_pushstring( Lt, (str && str[0])? str : NULL );
1239 }
1240
1241 static int action_stack_AS_WAIT_MODULE()
1242 {
1243
1244
1245
1246 if (camera_info.state.state_kbd_script_run)
1247 {
1248 action_pop_func(0);
1249 }
1250 return 0;
1251 }
1252
1253 static int luaCB_file_browser( lua_State* L ) {
1254
1255 camera_info.state.state_kbd_script_run = SCRIPT_STATE_INACTIVE;
1256
1257 action_push_func(action_stack_AS_WAIT_MODULE);
1258
1259 libfselect->file_select(LANG_STR_FILE_BROWSER, luaL_optstring( L, 1, "A" ), "A", return_string_selected);
1260
1261 return lua_yield(L, 0);
1262 }
1263
1264 static int luaCB_textbox( lua_State* L ) {
1265
1266 camera_info.state.state_kbd_script_run = SCRIPT_STATE_INACTIVE;
1267
1268 int rv = libtextbox->textbox_init((int)luaL_optstring( L, 1, "Text box" ),
1269 (int)luaL_optstring( L, 2, "Enter text" ),
1270 luaL_optstring( L, 3, "" ),
1271 luaL_optnumber( L, 4, 30),
1272 return_string_selected, 0);
1273 if (rv)
1274 {
1275
1276 action_push_func(action_stack_AS_WAIT_MODULE);
1277 }
1278 else
1279 return_string_selected(0);
1280
1281
1282 return lua_yield(L, 0);
1283 }
1284
1285
1286 static int luaCB_draw_pixel( lua_State* L ) {
1287 coord x1=luaL_checknumber(L,1);
1288 coord y1=luaL_checknumber(L,2);
1289 color cl=get_script_color(luaL_checknumber(L,3));
1290 draw_pixel(x1,y1,cl);
1291 return 0;
1292 }
1293
1294 static int luaCB_draw_line( lua_State* L ) {
1295 coord x1=luaL_checknumber(L,1);
1296 coord y1=luaL_checknumber(L,2);
1297 coord x2=luaL_checknumber(L,3);
1298 coord y2=luaL_checknumber(L,4);
1299 color cl=get_script_color(luaL_checknumber(L,5));
1300 draw_line(x1,y1,x2,y2,cl);
1301 return 0;
1302 }
1303
1304 static int luaCB_draw_rect( lua_State* L ) {
1305 coord x1=luaL_checknumber(L,1);
1306 coord y1=luaL_checknumber(L,2);
1307 coord x2=luaL_checknumber(L,3);
1308 coord y2=luaL_checknumber(L,4);
1309 color cl=get_script_color(luaL_checknumber(L,5));
1310 int th=luaL_optnumber(L,6,1) & RECT_BORDER_MASK;
1311 draw_rectangle(x1,y1,x2,y2,MAKE_COLOR(cl,cl),th);
1312 return 0;
1313 }
1314
1315 static int luaCB_draw_rect_filled( lua_State* L ) {
1316 coord x1 =luaL_checknumber(L,1);
1317 coord y1 =luaL_checknumber(L,2);
1318 coord x2 =luaL_checknumber(L,3);
1319 coord y2 =luaL_checknumber(L,4);
1320 color clf=get_script_color(luaL_checknumber(L,5));
1321 color clb=get_script_color(luaL_checknumber(L,6));
1322 int th =luaL_optnumber(L,7,1) & RECT_BORDER_MASK;
1323 draw_rectangle(x1,y1,x2,y2,MAKE_COLOR(clb,clf),th|DRAW_FILLED);
1324 return 0;
1325 }
1326
1327 static int luaCB_draw_ellipse( lua_State* L ) {
1328 coord x1=luaL_checknumber(L,1);
1329 coord y1=luaL_checknumber(L,2);
1330 coord a=luaL_checknumber(L,3);
1331 coord b=luaL_checknumber(L,4);
1332 color cl=get_script_color(luaL_checknumber(L,5));
1333 draw_ellipse(x1,y1,a,b,cl,0);
1334 return 0;
1335 }
1336
1337 static int luaCB_draw_ellipse_filled( lua_State* L ) {
1338 coord x1=luaL_checknumber(L,1);
1339 coord y1=luaL_checknumber(L,2);
1340 coord a=luaL_checknumber(L,3);
1341 coord b=luaL_checknumber(L,4);
1342 color cl=get_script_color(luaL_checknumber(L,5));
1343 draw_ellipse(x1,y1,a,b,cl,DRAW_FILLED);
1344 return 0;
1345 }
1346
1347 static int luaCB_draw_string( lua_State* L )
1348 {
1349 coord x1 = luaL_checknumber(L,1);
1350 coord y1 = luaL_checknumber(L,2);
1351 const char *t = luaL_checkstring( L, 3 );
1352 color clf = get_script_color(luaL_checknumber(L,4));
1353 color clb = get_script_color(luaL_checknumber(L,5));
1354 int xsize = luaL_optnumber(L,6,1);
1355 int ysize = luaL_optnumber(L,7,xsize);
1356
1357 if ((xsize <= 1) && (ysize <= 1))
1358 draw_string(x1, y1, t, MAKE_COLOR(clb,clf));
1359 else
1360 draw_string_scaled(x1, y1, t, MAKE_COLOR(clb,clf), xsize, ysize);
1361
1362 return 0;
1363 }
1364
1365 static int luaCB_draw_clear( __attribute__ ((unused))lua_State* L ) {
1366 draw_restore();
1367 return 0;
1368 }
1369
1370
1371
1372 static int luaCB_get_gui_screen_width( lua_State* L )
1373 {
1374 lua_pushnumber( L, camera_screen.width );
1375 return 1;
1376 }
1377
1378 static int luaCB_get_gui_screen_height( lua_State* L )
1379 {
1380 lua_pushnumber( L, camera_screen.height );
1381 return 1;
1382 }
1383
1384 static int luaCB_autostarted( lua_State* L )
1385 {
1386 lua_pushboolean( L, camera_info.state.auto_started );
1387 return 1;
1388 }
1389
1390 static int luaCB_get_autostart( lua_State* L )
1391 {
1392 lua_pushnumber( L, conf.script_startup );
1393 return 1;
1394 }
1395
1396 static int luaCB_set_autostart( lua_State* L )
1397 {
1398 int to;
1399 to = luaL_checknumber( L, 1 );
1400 if ( to >= 0 && to <= 3 ) conf.script_startup = to;
1401 conf_save();
1402 return 0;
1403 }
1404
1405 static int luaCB_get_usb_power( lua_State* L )
1406 {
1407 lua_pushnumber( L, get_usb_power(luaL_optnumber( L, 1, 0 )) );
1408 return 1;
1409 }
1410
1411
1412 static int luaCB_set_remote_timing( lua_State* L )
1413 {
1414 int val= on_off_value_from_lua_arg(L,1);
1415 if (val > 0 )
1416 lua_pushboolean(L,start_usb_HPtimer(val));
1417 else
1418 lua_pushboolean(L,stop_usb_HPtimer());
1419 return 1;
1420 }
1421
1422
1423 static int luaCB_usb_force_active( lua_State* L )
1424 {
1425 lua_pushboolean(L,force_usb_state(on_off_value_from_lua_arg(L,1)));
1426 return 1;
1427 }
1428
1429
1430 static int luaCB_usb_sync_wait( lua_State* L )
1431 {
1432 usb_sync_wait_flag = on_off_value_from_lua_arg(L,1);
1433 return 0;
1434 }
1435
1436 static int luaCB_enter_alt( __attribute__ ((unused))lua_State* L )
1437 {
1438 enter_alt(1);
1439
1440 if(lua_script_is_ptp) {
1441 ptp_saved_alt_state = 1;
1442 }
1443 return 0;
1444 }
1445
1446 static int luaCB_exit_alt( __attribute__ ((unused))lua_State* L )
1447 {
1448 exit_alt();
1449
1450 if(lua_script_is_ptp) {
1451 ptp_saved_alt_state = 0;
1452 }
1453 return 0;
1454 }
1455
1456 static int luaCB_get_alt_mode( lua_State* L )
1457 {
1458 lua_pushboolean(L, (camera_info.state.gui_mode != 0));
1459 return 1 ;
1460 }
1461
1462
1463 static int luaCB_shut_down( lua_State* L )
1464 {
1465 if ( luaL_optnumber(L,1,0) == 1 )
1466 {
1467 shutdown();
1468 } else {
1469 camera_shutdown_in_a_second();
1470 }
1471 return 0;
1472 }
1473
1474 static int luaCB_print_screen( lua_State* L )
1475 {
1476 script_print_screen_statement( on_off_value_from_lua_arg( L, 1 ) );
1477 return 0;
1478 }
1479
1480 static int luaCB_get_movie_status( lua_State* L )
1481 {
1482 lua_pushnumber( L, get_movie_status() );
1483 return 1;
1484 }
1485
1486 static int luaCB_set_movie_status( lua_State* L )
1487 {
1488 set_movie_status( luaL_checknumber( L, 1 ) );
1489 return 0;
1490 }
1491
1492 static int luaCB_get_video_button( lua_State* L )
1493 {
1494 lua_pushboolean( L, camera_info.cam_has_video_button );
1495 return 1;
1496 }
1497
1498 static int luaCB_get_video_recording( lua_State* L )
1499 {
1500 lua_pushboolean( L, is_video_recording() );
1501 return 1;
1502 }
1503
1504 static int luaCB_get_drive_mode( lua_State* L )
1505 {
1506 lua_pushnumber( L, shooting_get_drive_mode() );
1507 return 1;
1508 }
1509
1510 static int luaCB_get_focus_mode( lua_State* L )
1511 {
1512 lua_pushnumber( L, shooting_get_real_focus_mode() );
1513 return 1;
1514 }
1515
1516 static int luaCB_get_focus_state( lua_State* L )
1517 {
1518 lua_pushnumber( L, shooting_get_focus_state() );
1519 return 1;
1520 }
1521
1522 static int luaCB_get_focus_ok( lua_State* L )
1523 {
1524 lua_pushboolean( L, shooting_get_focus_ok() );
1525 return 1;
1526 }
1527
1528 static int luaCB_get_flash_mode( lua_State* L )
1529 {
1530 lua_pushnumber( L, shooting_get_flash_mode() );
1531 return 1;
1532 }
1533
1534 static int luaCB_get_shooting( lua_State* L )
1535 {
1536 lua_pushboolean( L, shooting_in_progress() );
1537 return 1;
1538 }
1539
1540 static int luaCB_get_flash_ready( lua_State* L )
1541 {
1542 lua_pushboolean( L, shooting_is_flash() );
1543 return 1;
1544 }
1545
1546 static int luaCB_get_IS_mode( lua_State* L )
1547 {
1548 lua_pushnumber( L, shooting_get_is_mode() );
1549 return 1;
1550 }
1551
1552 static int luaCB_get_orientation_sensor( lua_State* L )
1553 {
1554 lua_pushnumber( L, shooting_get_prop(camera_info.props.orientation_sensor) );
1555 return 1;
1556 }
1557
1558 static int luaCB_get_zoom_steps( lua_State* L )
1559 {
1560 lua_pushnumber( L, zoom_points );
1561 return 1;
1562 }
1563
1564 static int luaCB_get_nd_present( lua_State* L )
1565 {
1566 int to;
1567 if (camera_info.cam_has_nd_filter == 0)
1568 {
1569 to = 0;
1570 }
1571 else
1572 {
1573 if (camera_info.cam_has_iris_diaphragm == 0)
1574 to = 1;
1575 else
1576 to = 2;
1577 }
1578 lua_pushnumber( L, to );
1579 return 1;
1580 }
1581
1582 static int luaCB_get_propset( lua_State* L )
1583 {
1584 lua_pushnumber( L, camera_info.props.propset );
1585 return 1;
1586 }
1587
1588 static int luaCB_get_ev( lua_State* L )
1589 {
1590 lua_pushnumber( L, shooting_get_ev_correction1() );
1591 return 1;
1592 }
1593
1594 static int luaCB_set_ev( lua_State* L )
1595 {
1596 int to;
1597 to = luaL_checknumber( L, 1 );
1598 shooting_set_prop(camera_info.props.ev_correction_1, to);
1599 shooting_set_prop(camera_info.props.ev_correction_2, to);
1600 return 0;
1601 }
1602
1603 static int luaCB_get_histo_range( lua_State* L )
1604 {
1605 int from = (luaL_checknumber(L,1));
1606 int to = (luaL_checknumber(L,2));
1607 lua_pushnumber( L, libshothisto->shot_histogram_get_range(from, to) );
1608 return 1;
1609 }
1610
1611 static int luaCB_shot_histo_enable( lua_State* L )
1612 {
1613 libshothisto->shot_histogram_set(on_off_value_from_lua_arg( L, 1 ));
1614 return 0;
1615 }
1616
1617 static int luaCB_shot_histo_write_to_file( __attribute__ ((unused))lua_State* L )
1618 {
1619 libshothisto->write_to_file();
1620 return 0;
1621 }
1622
1623
1624
1625
1626
1627
1628
1629 static int luaCB_get_live_histo( lua_State* L )
1630 {
1631 unsigned short *h = malloc(256*sizeof(short));
1632 if(!h) {
1633 return luaL_error(L,"malloc fail");
1634 }
1635 int total=live_histogram_read_y(h);
1636 lua_createtable(L, 0, 256);
1637 int i;
1638 for(i=0;i<256;i++) {
1639 lua_pushnumber(L,h[i]);
1640 lua_rawseti(L,-2,i);
1641 }
1642 free(h);
1643 lua_pushnumber(L,total);
1644 return 2;
1645 }
1646
1647 static int luaCB_play_sound( lua_State* L )
1648 {
1649 play_sound(luaL_checknumber( L, 1 ));
1650 return 0;
1651 }
1652
1653 static int luaCB_get_temperature( lua_State* L )
1654 {
1655 int which = (luaL_checknumber( L, 1 ));
1656 int temp = -100;
1657 switch (which)
1658 {
1659 case 0:
1660 temp = get_optical_temp();
1661 break;
1662 case 1:
1663 temp = get_ccd_temp();
1664 break;
1665 case 2:
1666 temp = get_battery_temp();
1667 break;
1668 }
1669 lua_pushnumber( L, temp );
1670 return 1;
1671 }
1672
1673 static int luaCB_get_time( lua_State* L )
1674 {
1675 int r = -1;
1676 static struct tm *ttm;
1677 ttm = get_localtime();
1678 const char *t = luaL_checkstring( L, 1 );
1679 if (strncmp("s", t, 1)==0) r = ttm->tm_sec;
1680 else if (strncmp("m", t, 1)==0) r = ttm->tm_min;
1681 else if (strncmp("h", t, 1)==0) r = ttm->tm_hour;
1682 else if (strncmp("D", t, 1)==0) r = ttm->tm_mday;
1683 else if (strncmp("M", t, 1)==0) r = ttm->tm_mon+1;
1684 else if (strncmp("Y", t, 1)==0) r = 1900+ttm->tm_year;
1685 lua_pushnumber( L, r );
1686 return 1;
1687 }
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698 static int luaCB_set_clock( lua_State* L )
1699 {
1700 set_clock(luaL_checknumber(L,1),
1701 luaL_checknumber(L,2),
1702 luaL_checknumber(L,3),
1703 luaL_checknumber(L,4),
1704 luaL_checknumber(L,5),
1705 luaL_checknumber(L,6));
1706 return 0;
1707 }
1708
1709
1710
1711
1712
1713
1714 static int luaCB_peek( lua_State* L )
1715 {
1716 unsigned addr = luaL_checknumber(L,1);
1717 unsigned size = luaL_optnumber(L, 2, 4);
1718 switch(size) {
1719 case 1:
1720 lua_pushnumber( L, *(unsigned char *)(addr) );
1721 break;
1722 case 2:
1723 if (addr & 0x1) {
1724 lua_pushnil(L);
1725 }
1726 else {
1727 lua_pushnumber( L, *(unsigned short *)(addr) );
1728 }
1729 break;
1730 case 4:
1731 if (addr & 0x3) {
1732 lua_pushnil(L);
1733 }
1734 else {
1735 lua_pushnumber( L, *(unsigned *)(addr) );
1736 }
1737 break;
1738 default:
1739 lua_pushnil(L);
1740
1741 }
1742 return 1;
1743 }
1744
1745
1746
1747
1748
1749
1750
1751 static int luaCB_poke( lua_State* L )
1752 {
1753 unsigned addr = luaL_checknumber(L,1);
1754 unsigned val = luaL_checknumber(L,2);
1755 unsigned size = luaL_optnumber(L, 3, 4);
1756 int status = 0;
1757 switch(size) {
1758 case 1:
1759 *(unsigned char *)(addr) = (unsigned char)val;
1760 status=1;
1761 break;
1762 case 2:
1763 if (!(addr & 0x1)) {
1764 *(unsigned short *)(addr) = (unsigned short)val;
1765 status=1;
1766 }
1767 break;
1768 case 4:
1769 if (!(addr & 0x3)) {
1770 *(unsigned *)(addr) = val;
1771 status=1;
1772 }
1773 break;
1774 }
1775 if(status) {
1776 lua_pushboolean(L,1);
1777 }
1778 else {
1779 lua_pushnil(L);
1780 }
1781 return 1;
1782 }
1783
1784 static int luaCB_bitand( lua_State* L )
1785 {
1786 int v1 = (luaL_checknumber(L,1));
1787 int v2 = (luaL_checknumber(L,2));
1788 lua_pushnumber( L, v1 & v2 );
1789 return 1;
1790 }
1791
1792 static int luaCB_bitor( lua_State* L )
1793 {
1794 int v1 = (luaL_checknumber(L,1));
1795 int v2 = (luaL_checknumber(L,2));
1796 lua_pushnumber( L, v1 | v2 );
1797 return 1;
1798 }
1799
1800 static int luaCB_bitxor( lua_State* L )
1801 {
1802 int v1 = (luaL_checknumber(L,1));
1803 int v2 = (luaL_checknumber(L,2));
1804 lua_pushnumber( L, v1 ^ v2 );
1805 return 1;
1806 }
1807
1808 static int luaCB_bitshl( lua_State* L )
1809 {
1810 int val = (luaL_checknumber(L,1));
1811 unsigned shift = (luaL_checknumber(L,2));
1812 lua_pushnumber( L, val << shift );
1813 return 1;
1814 }
1815
1816 static int luaCB_bitshri( lua_State* L )
1817 {
1818 int val = (luaL_checknumber(L,1));
1819 unsigned shift = (luaL_checknumber(L,2));
1820 lua_pushnumber( L, val >> shift );
1821 return 1;
1822 }
1823
1824 static int luaCB_bitshru( lua_State* L )
1825 {
1826 unsigned val = (luaL_checknumber(L,1));
1827 unsigned shift = (luaL_checknumber(L,2));
1828 lua_pushnumber( L, val >> shift );
1829 return 1;
1830 }
1831
1832 static int luaCB_bitnot( lua_State* L )
1833 {
1834 unsigned val = (luaL_checknumber(L,1));
1835 lua_pushnumber( L, ~val );
1836 return 1;
1837 }
1838
1839 void set_string_field(lua_State* L, const char *key, const char *val)
1840 {
1841 lua_pushstring(L, val);
1842 lua_setfield(L, -2, key);
1843 }
1844
1845 void set_number_field(lua_State* L, const char *key, int val)
1846 {
1847 lua_pushnumber(L, val);
1848 lua_setfield(L, -2, key);
1849 }
1850
1851 static int luaCB_get_buildinfo( lua_State* L )
1852 {
1853 lua_createtable(L, 0, 10);
1854 set_string_field( L,"platform", camera_info.platform );
1855 set_string_field( L,"platsub", camera_info.platformsub );
1856 set_string_field( L,"version", camera_info.chdk_ver );
1857 set_string_field( L,"build_number", camera_info.build_number );
1858 set_string_field( L,"build_revision", camera_info.build_svnrev );
1859 set_string_field( L,"build_date", camera_info.build_date );
1860 set_string_field( L,"build_time", camera_info.build_time );
1861 set_string_field( L,"os", camera_info.os );
1862 set_number_field( L,"platformid", conf.platformid );
1863 set_number_field( L,"digic", camera_info.cam_digic );
1864 return 1;
1865 }
1866
1867 static int luaCB_get_mode( lua_State* L )
1868 {
1869 lua_pushboolean( L, !camera_info.state.mode_play );
1870 lua_pushboolean( L, camera_info.state.mode_video );
1871 lua_pushnumber( L, camera_info.state.mode );
1872 return 3;
1873 }
1874
1875
1876 static int luaCB_set_raw_develop( lua_State* L )
1877 {
1878 raw_prepare_develop(luaL_optstring( L, 1, NULL ), 0);
1879 return 0;
1880 }
1881
1882 static int luaCB_raw_merge_start( lua_State* L )
1883 {
1884 int op = luaL_checknumber(L,1);
1885 if ((op == RAW_OPERATION_SUM || op == RAW_OPERATION_AVERAGE))
1886 {
1887 lua_pushboolean(L, librawop->raw_merge_start(op));
1888 return 1;
1889 }
1890 else {
1891 return luaL_argerror(L,1,"invalid raw merge op");
1892 }
1893 }
1894
1895
1896 static int luaCB_raw_merge_add_file( lua_State* L )
1897 {
1898 lua_pushboolean(L, librawop->raw_merge_add_file(luaL_checkstring( L, 1 )));
1899 return 1;
1900 }
1901
1902 static int luaCB_raw_merge_end( __attribute__ ((unused))lua_State* L )
1903 {
1904 librawop->raw_merge_end();
1905 return 0;
1906 }
1907
1908
1909 static int luaCB_set_backlight( lua_State* L )
1910 {
1911 int val = on_off_value_from_lua_arg(L,1);
1912
1913 if (val > 0) TurnOnBackLight();
1914 else TurnOffBackLight();
1915 return 0;
1916 }
1917
1918
1919 static int luaCB_set_lcd_display( lua_State* L )
1920 {
1921 int val = on_off_value_from_lua_arg(L,1);
1922
1923 if (val > 0) TurnOnDisplay();
1924 else TurnOffDisplay();
1925 return 0;
1926 }
1927
1928
1929 static int luaCB_set_draw_title_line( lua_State* L )
1930 {
1931 camera_info.state.osd_title_line= on_off_value_from_lua_arg(L,1);
1932 return 0;
1933 }
1934
1935
1936 static int luaCB_get_draw_title_line( lua_State* L )
1937 {
1938 lua_pushboolean( L, camera_info.state.osd_title_line );
1939 return 1;
1940 }
1941
1942
1943 static unsigned levent_id_from_lua_arg( lua_State* L, int index)
1944 {
1945 unsigned event_id;
1946 if (lua_type(L, index) == LUA_TSTRING) {
1947 const char *ev_name = lua_tostring(L, index);
1948 event_id = levent_id_for_name(ev_name);
1949 if (event_id == 0) {
1950 return luaL_error( L, "bad event name '%s'", ev_name );
1951 }
1952 }
1953
1954
1955 else if (lua_type(L,index) == LUA_TNUMBER){
1956 event_id = lua_tonumber(L,index);
1957 }
1958 else {
1959 return luaL_error( L, "expected event name or id" );
1960 }
1961 return event_id;
1962 }
1963
1964
1965
1966
1967
1968 static unsigned levent_index_from_id_lua_arg( lua_State* L, int index )
1969 {
1970 if (lua_type(L, index) == LUA_TSTRING) {
1971 return levent_index_for_name(lua_tostring(L, index));
1972 }
1973 else if (lua_type(L,index) == LUA_TNUMBER){
1974 return levent_index_for_id(lua_tonumber(L,index));
1975 }
1976 else {
1977 return luaL_error( L, "expected string or number" );
1978 }
1979 }
1980
1981
1982
1983
1984
1985
1986 static int luaCB_get_levent_def( lua_State* L )
1987 {
1988 unsigned event_index = levent_index_from_id_lua_arg(L,1);
1989 if (event_index == LEVENT_INVALID_INDEX) {
1990 lua_pushnil(L);
1991 return 1;
1992 }
1993 lua_pushstring(L, levent_table[event_index].name);
1994 lua_pushnumber(L, levent_table[event_index].id);
1995 lua_pushnumber(L, levent_table[event_index].param);
1996 return 3;
1997 }
1998
1999
2000
2001
2002
2003
2004 static int luaCB_get_levent_index( lua_State* L )
2005 {
2006 unsigned event_index = levent_index_from_id_lua_arg(L,1);
2007 if (event_index == LEVENT_INVALID_INDEX) {
2008 lua_pushnil(L);
2009 }
2010 else {
2011 lua_pushnumber(L, event_index);
2012 }
2013 return 1;
2014 }
2015
2016
2017
2018
2019
2020
2021 static int luaCB_get_levent_def_by_index( lua_State* L )
2022 {
2023 unsigned i = luaL_checknumber(L,1);
2024 if(i >= levent_count()) {
2025 lua_pushnil(L);
2026 return 1;
2027 }
2028 lua_pushstring(L, levent_table[i].name);
2029 lua_pushnumber(L, levent_table[i].id);
2030 lua_pushnumber(L, levent_table[i].param);
2031 return 3;
2032 }
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043 static int luaCB_post_levent_to_ui( lua_State* L )
2044 {
2045 unsigned event_id,arg;
2046
2047 event_id = levent_id_from_lua_arg(L,1);
2048 arg = luaL_optnumber(L, 2, 0);
2049 PostLogicalEventToUI(event_id,arg);
2050 return 0;
2051 }
2052
2053 static int luaCB_post_levent_for_npt( lua_State* L )
2054 {
2055 unsigned event_id,arg;
2056
2057 event_id = levent_id_from_lua_arg(L,1);
2058 arg = luaL_optnumber(L, 2, 0);
2059 PostLogicalEventForNotPowerType(event_id,arg);
2060 return 0;
2061 }
2062
2063
2064
2065
2066
2067
2068
2069 static int luaCB_set_levent_active( lua_State* L )
2070 {
2071 unsigned event_id;
2072 unsigned state;
2073
2074 event_id = levent_id_from_lua_arg(L,1);
2075 state = on_off_value_from_lua_arg(L,2);
2076 SetLogicalEventActive(event_id,state);
2077 return 0;
2078 }
2079
2080
2081
2082
2083
2084
2085 static int luaCB_set_levent_script_mode( lua_State* L )
2086 {
2087 SetScriptMode(on_off_value_from_lua_arg(L,1));
2088 return 0;
2089 }
2090
2091
2092
2093
2094
2095
2096 static int luaCB_set_capture_mode_canon( lua_State* L )
2097 {
2098 int modenum = luaL_checknumber(L,1);
2099
2100 if(modenum < 0)
2101 modenum &= 0xFFFF;
2102 lua_pushboolean( L, shooting_set_mode_canon(modenum) );
2103 return 1;
2104 }
2105
2106
2107
2108
2109
2110
2111 static int luaCB_set_capture_mode( lua_State* L )
2112 {
2113 int modenum = luaL_checknumber(L,1);
2114 lua_pushboolean( L, shooting_set_mode_chdk(modenum) );
2115 return 1;
2116 }
2117
2118
2119
2120
2121
2122
2123 static int luaCB_is_capture_mode_valid( lua_State* L )
2124 {
2125 int modenum = luaL_checknumber(L,1);
2126 lua_pushboolean( L, shooting_mode_chdk2canon(modenum) != -1 );
2127 return 1;
2128 }
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141 static int luaCB_set_record( lua_State* L )
2142 {
2143 shooting_set_playrec_mode(on_off_value_from_lua_arg(L,1));
2144 return 0;
2145 }
2146
2147
2148
2149 static int luaCB_switch_mode_usb( lua_State* L )
2150 {
2151 switch_mode_usb(on_off_value_from_lua_arg(L,1));
2152 return 0;
2153 }
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165 static int luaCB_force_analog_av( lua_State* L )
2166 {
2167 lua_pushboolean(L, kbd_force_analog_av(luaL_checknumber( L, 1 )));
2168 return 1;
2169 }
2170
2171
2172
2173
2174
2175
2176 static int pack_native_args( lua_State* L, unsigned start, unsigned *argbuf)
2177 {
2178 unsigned i;
2179 unsigned end = lua_gettop(L);
2180
2181 for(i = start; i <= end; i++,argbuf++) {
2182 if (lua_type(L, i) == LUA_TSTRING) {
2183 *argbuf=(unsigned)lua_tostring( L, i);
2184 }
2185 else if (lua_type(L, i) == LUA_TNUMBER) {
2186 *argbuf=lua_tonumber( L, i);
2187 }
2188 else {
2189 return 0;
2190 }
2191 }
2192 return 1;
2193 }
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216 static int luaCB_call_func_ptr( lua_State* L)
2217 {
2218 if (conf.script_allow_lua_native_calls)
2219 {
2220 unsigned *argbuf=NULL;
2221 unsigned n_args = lua_gettop(L)-1;
2222 void *fptr;
2223
2224 fptr=(void *)luaL_checknumber( L, 1 );
2225
2226 if (n_args)
2227 {
2228 argbuf=malloc(n_args * 4);
2229 if(!argbuf)
2230 {
2231 return luaL_error( L, "malloc fail" );
2232 }
2233 if(!pack_native_args(L, 2, argbuf))
2234 {
2235 free(argbuf);
2236 return luaL_error( L, "expected string or number" );
2237 }
2238 }
2239
2240 lua_pushnumber( L, call_func_ptr(fptr, argbuf, n_args) );
2241 free(argbuf);
2242 return 1;
2243 }
2244 return luaL_error( L, "native calls disabled" );
2245 }
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269 extern unsigned _ExecuteEventProcedure(const char *name,...);
2270 static int luaCB_call_event_proc( lua_State* L )
2271 {
2272 if (conf.script_allow_lua_native_calls)
2273 {
2274 const char *evpname;
2275 unsigned *argbuf;
2276 unsigned n_args = lua_gettop(L);
2277
2278 evpname=luaL_checkstring( L, 1 );
2279
2280 argbuf=malloc(n_args * 4);
2281 if (!argbuf)
2282 {
2283 return luaL_error( L, "malloc fail" );
2284 }
2285
2286
2287 *argbuf = (unsigned)evpname;
2288
2289 if(!pack_native_args(L,2,argbuf+1))
2290 {
2291 free(argbuf);
2292 return luaL_error( L, "expected string or number" );
2293 }
2294
2295 lua_pushnumber( L, call_func_ptr(_ExecuteEventProcedure,argbuf,n_args) );
2296 free(argbuf);
2297 return 1;
2298 }
2299 return luaL_error( L, "native calls disabled" );
2300 }
2301
2302
2303
2304
2305
2306
2307 static int luaCB_reboot( lua_State* L )
2308 {
2309 lua_pushboolean(L, reboot(luaL_optstring( L, 1, NULL )));
2310 return 1;
2311 }
2312
2313 static int luaCB_get_config_value( lua_State* L ) {
2314 unsigned int argc = lua_gettop(L);
2315 unsigned int id, i;
2316 int ret = 1;
2317 tConfigVal configVal;
2318
2319 if( argc>=1 ) {
2320 id = luaL_checknumber(L, 1);
2321 switch( conf_getValue(id, &configVal) ) {
2322 case CONF_VALUE:
2323 lua_pushnumber(L, configVal.numb);
2324 break;
2325 case CONF_INT_PTR:
2326 lua_createtable(L, 0, configVal.numb);
2327 for( i=0; i<(unsigned)configVal.numb; i++ ) {
2328 lua_pushinteger(L, configVal.pInt[i]);
2329 lua_rawseti(L, -2, i+1);
2330 }
2331 break;
2332 case CONF_CHAR_PTR:
2333 lua_pushstring(L, configVal.str);
2334 break;
2335 case CONF_OSD_POS:
2336 lua_pushnumber(L, configVal.pos.x);
2337 lua_pushnumber(L, configVal.pos.y); ret++;
2338 break;
2339 default:
2340 if( argc>=2) {
2341 ret = argc-1;
2342 } else {
2343 lua_pushnil(L);
2344 }
2345 break;
2346 }
2347 } else {
2348 lua_pushnil(L);
2349 }
2350 return ret;
2351 }
2352
2353 static int luaCB_set_config_value( lua_State* L ) {
2354 unsigned int argc = lua_gettop(L);
2355 unsigned int id, i, j;
2356 tConfigVal configVal = {0};
2357
2358 if( argc>=2 ) {
2359 id = luaL_checknumber(L, 1);
2360 for( i=2; i<=argc; i++) {
2361 switch( lua_type(L, i) ) {
2362 case LUA_TNUMBER:
2363 if( !configVal.isNumb ) {
2364 configVal.numb = luaL_checknumber(L, i);
2365 configVal.isNumb++;
2366 }
2367 switch( configVal.isPos ) {
2368 case 0: configVal.pos.x = luaL_checknumber(L, i); configVal.isPos++; break;
2369 case 1: configVal.pos.y = luaL_checknumber(L, i); configVal.isPos++; break;
2370 }
2371 break;
2372 case LUA_TSTRING:
2373 if( !configVal.isStr ) {
2374 configVal.str = (char*)luaL_checkstring(L, i);
2375 configVal.isStr++;
2376 }
2377 break;
2378 case LUA_TTABLE:
2379 if( !configVal.isPInt ) {
2380 configVal.numb = lua_objlen(L, i);
2381 if( configVal.pInt ) {
2382 free(configVal.pInt);
2383 configVal.pInt = NULL;
2384 }
2385 configVal.pInt = malloc(configVal.numb*sizeof(int));
2386 if( configVal.pInt ) {
2387 for( j=1; j<=(unsigned)configVal.numb; j++) {
2388 lua_rawgeti(L, i, j);
2389 configVal.pInt[j-1] = lua_tointeger(L, -1);
2390 lua_pop(L, 1);
2391 }
2392 }
2393 configVal.isPInt++;
2394 }
2395 break;
2396 }
2397 }
2398 lua_pushboolean(L, conf_setValue(id, configVal));
2399 if( configVal.pInt ) {
2400 free(configVal.pInt);
2401 configVal.pInt = NULL;
2402 }
2403 } else lua_pushboolean(L, 0);
2404 return 1;
2405 }
2406
2407 static int luaCB_set_config_autosave( lua_State* L ) {
2408 conf_setAutosave(on_off_value_from_lua_arg(L, 1));
2409 return 0;
2410 }
2411
2412 static int luaCB_save_config_file( lua_State* L ) {
2413 lua_pushboolean(L, save_config_file(luaL_checknumber(L, 1), luaL_optstring(L, 2, NULL)));
2414 return 1;
2415 }
2416
2417 static int luaCB_load_config_file( lua_State* L ) {
2418 lua_pushboolean(L, load_config_file(luaL_checknumber(L, 1), luaL_optstring(L, 2, NULL)));
2419 return 1;
2420 }
2421
2422 static int luaCB_set_file_attributes( lua_State* L ) {
2423 lua_pushnumber(L, SetFileAttributes(luaL_checkstring(L, 1), luaL_checknumber(L, 2)));
2424 return 1;
2425 }
2426
2427 static int action_stack_AS_SCRIPT_READ_USB_MSG()
2428 {
2429 ptp_script_msg *msg = ptp_script_read_msg();
2430
2431 if ((get_tick_count() >= action_top(2)) || msg)
2432 {
2433 if (msg && msg->data)
2434 {
2435 lua_pushlstring(Lt,msg->data,msg->size);
2436 }
2437 else
2438 {
2439 lua_pushnil(Lt);
2440 }
2441 action_pop_func(1);
2442 return 1;
2443 }
2444 return 0;
2445 }
2446
2447 static int action_stack_AS_SCRIPT_WRITE_USB_MSG()
2448 {
2449 ptp_script_msg *msg = (ptp_script_msg *)action_top(2);
2450
2451 int r = ptp_script_write_msg(msg);
2452
2453 if ((get_tick_count() >= action_top(3)) || r)
2454 {
2455 lua_pushboolean(Lt,r);
2456 action_pop_func(2);
2457 return 1;
2458 }
2459 return 0;
2460 }
2461
2462
2463
2464
2465
2466
2467
2468 static int luaCB_read_usb_msg( lua_State* L )
2469 {
2470 int timeout = sleep_delay(luaL_optnumber(L,1,0));
2471 if (timeout > 0)
2472 {
2473 action_push(timeout);
2474 action_push_func(action_stack_AS_SCRIPT_READ_USB_MSG);
2475 return lua_yield( L, 0 );
2476 }
2477 ptp_script_msg *msg = ptp_script_read_msg();
2478 if(msg)
2479 {
2480 lua_pushlstring(L,msg->data,msg->size);
2481 free(msg);
2482 return 1;
2483 }
2484 lua_pushnil(L);
2485 return 1;
2486 }
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496 static int luaCB_write_usb_msg( lua_State* L )
2497 {
2498 ptp_script_msg *msg;
2499 int timeout = sleep_delay(luaL_optnumber(L,2,0));
2500
2501
2502
2503 if (lua_gettop(L) < 1)
2504 {
2505 return luaL_error(L,"missing argument");
2506 }
2507 msg=lua_create_usb_msg(L,1,PTP_CHDK_S_MSGTYPE_USER);
2508
2509 if (msg->subtype == PTP_CHDK_TYPE_UNSUPPORTED)
2510 {
2511 free(msg);
2512 return luaL_error(L,"unsupported type");
2513 }
2514 if (!msg)
2515 {
2516 return luaL_error(L,"failed to create message");
2517 }
2518 if (timeout)
2519 {
2520 action_push(timeout);
2521 action_push((int)msg);
2522 action_push_func(action_stack_AS_SCRIPT_WRITE_USB_MSG);
2523 return lua_yield( L, 0 );
2524 }
2525 lua_pushboolean(L,ptp_script_write_msg(msg));
2526 return 1;
2527 }
2528
2529
2530 static void set_meminfo_num( lua_State* L,const char *name, int val) {
2531 if(val != -1) {
2532 set_number_field( L, name, val );
2533 }
2534 }
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563 static int luaCB_get_meminfo( lua_State* L ) {
2564 const char *heapname = luaL_optstring( L, 1, "combined" );
2565 cam_meminfo meminfo;
2566 if (strcmp(heapname,"combined") == 0) {
2567 GetCombinedMemInfo(&meminfo);
2568 meminfo.start_address=meminfo.end_address=-1;
2569 }
2570 else if (strcmp(heapname,"system") == 0)
2571 {
2572 GetMemInfo(&meminfo);
2573 }
2574 else if ((strcmp(heapname,"aram") == 0))
2575 {
2576 if(!GetARamInfo(&meminfo)) {
2577 lua_pushboolean(L,0);
2578 return 1;
2579 }
2580 }
2581 else if (strcmp(heapname,"exmem") == 0)
2582 {
2583 if(!GetExMemInfo(&meminfo)) {;
2584 lua_pushboolean(L,0);
2585 return 1;
2586 }
2587 }
2588 else
2589 {
2590 lua_pushboolean(L,0);
2591 return 1;
2592 }
2593
2594 if(meminfo.start_address == camera_info.text_start) {
2595 meminfo.start_address += camera_info.memisosize;
2596 meminfo.total_size -= camera_info.memisosize;
2597 }
2598 lua_createtable(L, 0, 13);
2599 set_string_field( L,"name", heapname );
2600
2601 lua_pushboolean( L, 1);
2602 lua_setfield(L, -2, "chdk_malloc");
2603 set_number_field( L, "chdk_start", camera_info.text_start);
2604 set_number_field( L, "chdk_size", camera_info.memisosize );
2605 set_meminfo_num( L, "start_address", meminfo.start_address );
2606 set_meminfo_num( L, "end_address", meminfo.end_address);
2607 set_meminfo_num( L, "total_size", meminfo.total_size);
2608 set_meminfo_num( L, "allocated_size", meminfo.allocated_size);
2609 set_meminfo_num( L, "allocated_peak", meminfo.allocated_peak);
2610 set_meminfo_num( L, "allocated_count", meminfo.allocated_count);
2611 set_meminfo_num( L, "free_size", meminfo.free_size);
2612 set_meminfo_num( L, "free_block_max_size", meminfo.free_block_max_size);
2613 set_meminfo_num( L, "free_block_count", meminfo.free_block_count);
2614 return 1;
2615 }
2616
2617
2618
2619
2620
2621 static int luaCB_set_yield( lua_State* L )
2622 {
2623 lua_pushnumber(L,yield_max_count);
2624 lua_pushnumber(L,yield_max_ms);
2625 yield_max_count = luaL_optnumber(L,1,YIELD_MAX_COUNT_DEFAULT);
2626 yield_max_ms = luaL_optnumber(L,2,YIELD_MAX_MS_DEFAULT);
2627 return 2;
2628 }
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638 static int luaCB_get_usb_capture_support( lua_State* L )
2639 {
2640 lua_pushnumber(L,remotecap_get_target_support());
2641 return 1;
2642 }
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652 static int luaCB_init_usb_capture( lua_State* L )
2653 {
2654 int what=luaL_checknumber(L, 1);
2655 int startline=luaL_optnumber(L, 2, 0);
2656 int numlines=luaL_optnumber(L, 3, 0);
2657 lua_pushboolean(L,remotecap_set_target(what,startline,numlines));
2658 return 1;
2659 }
2660
2661
2662
2663
2664
2665 static int luaCB_get_usb_capture_target( lua_State* L )
2666 {
2667 lua_pushnumber(L,remotecap_get_target());
2668 return 1;
2669 }
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683 static int luaCB_set_usb_capture_timeout( lua_State* L )
2684 {
2685 remotecap_set_timeout(luaL_optnumber(L,1,0));
2686 return 0;
2687 }
2688
2689
2690
2691
2692 static int luaCB_iso_to_sv96( lua_State* L )
2693 {
2694 lua_pushnumber(L, shooting_get_sv96_from_iso(luaL_checknumber(L, 1)));
2695 return 1;
2696 }
2697
2698 static int luaCB_sv96_to_iso( lua_State* L )
2699 {
2700 lua_pushnumber(L, shooting_get_iso_from_sv96(luaL_checknumber(L, 1)));
2701 return 1;
2702 }
2703
2704 static int luaCB_iso_real_to_market( lua_State* L )
2705 {
2706 lua_pushnumber(L, shooting_iso_real_to_market(luaL_checknumber(L, 1)));
2707 return 1;
2708 }
2709
2710 static int luaCB_iso_market_to_real( lua_State* L )
2711 {
2712 lua_pushnumber(L, shooting_iso_market_to_real(luaL_checknumber(L, 1)));
2713 return 1;
2714 }
2715
2716 static int luaCB_sv96_real_to_market( lua_State* L )
2717 {
2718 lua_pushnumber(L, shooting_sv96_real_to_market(luaL_checknumber(L, 1)));
2719 return 1;
2720 }
2721
2722 static int luaCB_sv96_market_to_real( lua_State* L )
2723 {
2724 lua_pushnumber(L, shooting_sv96_market_to_real(luaL_checknumber(L, 1)));
2725 return 1;
2726 }
2727
2728 static int luaCB_aperture_to_av96( lua_State* L )
2729 {
2730 lua_pushnumber(L, shooting_get_av96_from_aperture(luaL_checknumber(L, 1)));
2731 return 1;
2732 }
2733
2734 static int luaCB_av96_to_aperture( lua_State* L )
2735 {
2736 lua_pushnumber(L, shooting_get_aperture_from_av96(luaL_checknumber(L, 1)));
2737 return 1;
2738 }
2739
2740 static int luaCB_usec_to_tv96( lua_State* L )
2741 {
2742 lua_pushnumber(L, shooting_get_tv96_from_shutter_speed((float)luaL_checknumber(L, 1)/1000000.0));
2743 return 1;
2744 }
2745
2746 static int luaCB_tv96_to_usec( lua_State* L )
2747 {
2748 lua_pushnumber(L, (int)(shooting_get_shutter_speed_from_tv96(luaL_checknumber(L, 1)) * 1000000.0 + 0.5));
2749 return 1;
2750 }
2751
2752 static int luaCB_seconds_to_tv96( lua_State* L )
2753 {
2754 lua_pushnumber(L, shooting_get_tv96_from_shutter_speed((float)luaL_checknumber(L, 1)/(float)luaL_checknumber(L, 2)));
2755 return 1;
2756 }
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766 static int luaCB_shoot_hook_set( lua_State* L )
2767 {
2768 int hook = lua_tonumber( L, lua_upvalueindex(1) );
2769 script_shoot_hook_set(hook,luaL_checknumber(L, 1));
2770 return 0;
2771 }
2772
2773
2774
2775
2776
2777 static int luaCB_shoot_hook_is_ready( lua_State* L )
2778 {
2779 int hook = lua_tonumber( L, lua_upvalueindex(1) );
2780 lua_pushboolean(L,script_shoot_hook_ready(hook));
2781 return 1;
2782 }
2783
2784
2785
2786
2787
2788 static int luaCB_shoot_hook_continue( lua_State* L )
2789 {
2790 int hook = lua_tonumber( L, lua_upvalueindex(1) );
2791 script_shoot_hook_continue(hook);
2792 return 0;
2793 }
2794
2795
2796
2797
2798
2799
2800 static int luaCB_shoot_hook_count( lua_State* L )
2801 {
2802 int hook = lua_tonumber( L, lua_upvalueindex(1) );
2803 lua_pushnumber(L,script_shoot_hook_count(hook));
2804 return 1;
2805 }
2806
2807
2808
2809 #define FUNC( X ) { #X, luaCB_##X },
2810 static const luaL_Reg chdk_funcs[] = {
2811 FUNC(shoot)
2812 FUNC(sleep)
2813 FUNC(cls)
2814 FUNC(set_console_layout)
2815 FUNC(set_console_autoredraw)
2816 FUNC(console_redraw)
2817 FUNC(get_av96)
2818 FUNC(get_bv96)
2819 FUNC(get_day_seconds)
2820 FUNC(get_disk_size)
2821 FUNC(get_dofinfo)
2822 FUNC(get_free_disk_space)
2823 FUNC(get_focus)
2824 FUNC(get_iso_market)
2825 FUNC(get_iso_mode)
2826 FUNC(get_iso_real)
2827 FUNC(get_jpg_count)
2828 FUNC(get_prop)
2829 FUNC(get_prop_str)
2830 FUNC(get_raw_support)
2831 FUNC(get_raw_count)
2832 FUNC(get_raw_nr)
2833 FUNC(get_raw)
2834 FUNC(get_sv96)
2835 FUNC(get_tick_count)
2836 FUNC(get_tv96)
2837 FUNC(get_user_av_id)
2838 FUNC(get_user_av96)
2839 FUNC(get_user_tv_id)
2840 FUNC(get_user_tv96)
2841 FUNC(get_vbatt)
2842 FUNC(get_zoom)
2843 FUNC(get_exp_count)
2844 FUNC(get_image_dir)
2845 FUNC(get_flash_params_count)
2846 FUNC(get_parameter_data)
2847 FUNC(get_min_av96)
2848 FUNC(get_max_av96)
2849 FUNC(get_nd_value_ev96)
2850 FUNC(get_nd_current_ev96)
2851 FUNC(get_current_av96)
2852 FUNC(get_current_tv96)
2853 FUNC(get_current_delta_sv96)
2854 FUNC(get_current_base_sv96)
2855 FUNC(get_imager_active)
2856 FUNC(get_canon_image_format)
2857 FUNC(get_canon_raw_support)
2858
2859 FUNC(set_av96_direct)
2860 FUNC(set_av96)
2861 FUNC(set_canon_image_format)
2862 FUNC(set_focus)
2863 FUNC(set_focus_interlock_bypass)
2864 FUNC(set_iso_mode)
2865 FUNC(set_iso_real)
2866 FUNC(set_led)
2867 FUNC(set_nd_filter)
2868 FUNC(set_prop)
2869 FUNC(set_prop_str)
2870 FUNC(set_raw_nr)
2871 FUNC(set_raw)
2872 FUNC(set_sv96)
2873 FUNC(set_tv96_direct)
2874 FUNC(set_tv96)
2875 FUNC(set_user_av_by_id_rel)
2876 FUNC(set_user_av_by_id)
2877 FUNC(set_user_av96)
2878 FUNC(set_user_tv_by_id_rel)
2879 FUNC(set_user_tv_by_id)
2880 FUNC(set_user_tv96)
2881 FUNC(set_zoom_speed)
2882 FUNC(set_zoom_rel)
2883 FUNC(set_zoom)
2884
2885 FUNC(wait_click)
2886 FUNC(is_pressed)
2887 FUNC(is_key)
2888 FUNC(set_exit_key)
2889 FUNC(wheel_right)
2890 FUNC(wheel_left)
2891 FUNC(md_get_cell_diff)
2892 FUNC(md_get_cell_val)
2893 FUNC(md_detect_motion)
2894 FUNC(md_af_led_control)
2895 FUNC(autostarted)
2896 FUNC(get_autostart)
2897 FUNC(set_autostart)
2898 FUNC(get_usb_power)
2899 FUNC(set_remote_timing)
2900 FUNC(usb_force_active)
2901 FUNC(usb_sync_wait)
2902 FUNC(enter_alt)
2903 FUNC(exit_alt)
2904 FUNC(get_alt_mode)
2905 FUNC(shut_down)
2906 FUNC(print_screen)
2907
2908 FUNC(get_partitionInfo)
2909 FUNC(swap_partitions)
2910
2911 FUNC(get_focus_mode)
2912 FUNC(get_focus_state)
2913 FUNC(get_focus_ok)
2914 FUNC(get_propset)
2915 FUNC(get_zoom_steps)
2916 FUNC(get_drive_mode)
2917 FUNC(get_flash_mode)
2918 FUNC(get_shooting)
2919 FUNC(get_flash_ready)
2920 FUNC(get_IS_mode)
2921 FUNC(set_ev)
2922 FUNC(get_ev)
2923 FUNC(get_orientation_sensor)
2924 FUNC(get_nd_present)
2925 FUNC(get_movie_status)
2926 FUNC(set_movie_status)
2927 FUNC(get_video_button)
2928 FUNC(get_video_recording)
2929
2930 FUNC(get_histo_range)
2931 FUNC(shot_histo_enable)
2932 FUNC(shot_histo_write_to_file)
2933 FUNC(get_live_histo)
2934 FUNC(play_sound)
2935 FUNC(get_temperature)
2936 FUNC(peek)
2937 FUNC(poke)
2938 FUNC(bitand)
2939 FUNC(bitor)
2940 FUNC(bitxor)
2941 FUNC(bitshl)
2942 FUNC(bitshri)
2943 FUNC(bitshru)
2944 FUNC(bitnot)
2945
2946 FUNC(get_time)
2947 FUNC(set_clock)
2948
2949 FUNC(get_buildinfo)
2950 FUNC(get_mode)
2951
2952 FUNC(set_raw_develop)
2953
2954
2955 FUNC(raw_merge_start)
2956 FUNC(raw_merge_add_file)
2957 FUNC(raw_merge_end)
2958 FUNC(set_backlight)
2959 FUNC(set_lcd_display)
2960 FUNC(set_draw_title_line)
2961 FUNC(get_draw_title_line)
2962 FUNC(set_aelock)
2963 FUNC(set_aflock)
2964 FUNC(set_mf)
2965 FUNC(get_sd_over_modes)
2966 FUNC(set_curve_state)
2967 FUNC(get_curve_state)
2968 FUNC(set_curve_file)
2969 FUNC(get_curve_file)
2970
2971 FUNC(get_levent_def)
2972
2973 FUNC(get_levent_def_by_index)
2974
2975 FUNC(get_levent_index)
2976 FUNC(post_levent_to_ui)
2977 FUNC(post_levent_for_npt)
2978 FUNC(set_levent_active)
2979 FUNC(set_levent_script_mode)
2980
2981 FUNC(set_capture_mode)
2982 FUNC(set_capture_mode_canon)
2983 FUNC(is_capture_mode_valid)
2984
2985 FUNC(set_record)
2986
2987 FUNC(switch_mode_usb)
2988
2989 FUNC(force_analog_av)
2990
2991 FUNC(call_event_proc)
2992 FUNC(call_func_ptr)
2993 FUNC(reboot)
2994 FUNC(get_config_value)
2995 FUNC(set_config_value)
2996 FUNC(set_config_autosave)
2997 FUNC(save_config_file)
2998 FUNC(load_config_file)
2999 FUNC(set_file_attributes)
3000 FUNC(get_meminfo)
3001 FUNC(file_browser)
3002 FUNC(textbox)
3003 FUNC(draw_pixel)
3004 FUNC(draw_line)
3005 FUNC(draw_rect)
3006 FUNC(draw_rect_filled)
3007 FUNC(draw_ellipse)
3008 FUNC(draw_ellipse_filled)
3009 FUNC(draw_clear)
3010 FUNC(draw_string)
3011
3012 FUNC(get_gui_screen_width)
3013 FUNC(get_gui_screen_height)
3014
3015 FUNC(set_yield)
3016 FUNC(read_usb_msg)
3017 FUNC(write_usb_msg)
3018 FUNC(get_usb_capture_support)
3019 FUNC(init_usb_capture)
3020 FUNC(get_usb_capture_target)
3021 FUNC(set_usb_capture_timeout)
3022
3023 FUNC(iso_to_sv96)
3024 FUNC(sv96_to_iso)
3025 FUNC(iso_real_to_market)
3026 FUNC(iso_market_to_real)
3027 FUNC(sv96_real_to_market)
3028 FUNC(sv96_market_to_real)
3029 FUNC(aperture_to_av96)
3030 FUNC(av96_to_aperture)
3031 FUNC(usec_to_tv96)
3032 FUNC(tv96_to_usec)
3033 FUNC(seconds_to_tv96)
3034
3035 {NULL, NULL},
3036 };
3037
3038 void register_shoot_hook_fn(lua_State* L, int hook, void *hook_fn, const char *name)
3039 {
3040 lua_pushnumber( L, hook );
3041 lua_pushcclosure( L, hook_fn, 1 );
3042 lua_setfield( L, -2, name);
3043 }
3044
3045 void register_shoot_hooks( lua_State* L )
3046 {
3047 int i;
3048 for(i=0; i<SCRIPT_NUM_SHOOT_HOOKS;i++) {
3049 lua_createtable(L, 0, 4);
3050 register_shoot_hook_fn(L,i,luaCB_shoot_hook_set,"set");
3051
3052 register_shoot_hook_fn(L,i,luaCB_shoot_hook_is_ready,"is_ready");
3053 register_shoot_hook_fn(L,i,luaCB_shoot_hook_continue,"continue");
3054 register_shoot_hook_fn(L,i,luaCB_shoot_hook_count,"count");
3055 lua_setglobal( L, shoot_hook_names[i] );
3056 }
3057 }
3058
3059 void register_lua_funcs( lua_State* L )
3060 {
3061 const luaL_reg *r;
3062
3063 register_shoot_hooks( L );
3064 luaopen_rawop( L );
3065
3066 lua_pushlightuserdata( L, action_push_click );
3067 lua_pushcclosure( L, luaCB_keyfunc, 1 );
3068 lua_setglobal( L, "click" );
3069
3070 lua_pushlightuserdata( L, action_push_press );
3071 lua_pushcclosure( L, luaCB_keyfunc, 1 );
3072 lua_setglobal( L, "press" );
3073
3074 lua_pushlightuserdata( L, action_push_release );
3075 lua_pushcclosure( L, luaCB_keyfunc, 1 );
3076 lua_setglobal( L, "release" );
3077
3078 for(r=chdk_funcs;r->name;r++) {
3079 lua_pushcfunction( L, r->func );
3080 lua_setglobal( L, r->name );
3081 }
3082 (void)luaL_dostring(L,"function usb_msg_table_to_string(t)"
3083 " local v2s=function(v)"
3084 " local t=type(v)"
3085 " if t=='string' then return v end"
3086 " if t=='number' or t=='boolean' or t=='nil' then return tostring(v) end"
3087 " return '' end"
3088 " local r=''"
3089 " for k,v in pairs(t) do"
3090 " local s,vs=''"
3091 " if type(v)=='table' then"
3092 " for i=1,table.maxn(v) do"
3093 " s=s..'\\t'..v2s(v[i]) end"
3094 " else"
3095 " vs=v2s(v)"
3096 " if #vs then s=s..'\\t'..vs end"
3097 " end"
3098 " vs=v2s(k)"
3099 " if #vs>0 and #s>0 then r=r..vs..s..'\\n' end"
3100 " end"
3101 " return r"
3102 " end");
3103
3104 }
3105
3106
3107
3108
3109
3110
3111
3112 static void lua_set_variable(char *name, int value, int isBool, int isTable, int labelCount, const char **labels)
3113 {
3114 lua_pushstring( L, name );
3115 if (isTable)
3116 {
3117 lua_createtable(L, labelCount, 2);
3118 int i;
3119 for (i=0; i<labelCount; i++)
3120 {
3121 lua_pushstring(L,labels[i]);
3122 lua_rawseti(L,-2,i+1);
3123 }
3124 SET_INT_FIELD("index", value+1);
3125 SET_STR_FIELD("value", labels[value]);
3126 }
3127 else
3128 {
3129 if (isBool)
3130 lua_pushboolean( L, value );
3131 else
3132 lua_pushnumber( L, value );
3133 }
3134 lua_settable( L, LUA_GLOBALSINDEX );
3135 }
3136
3137 static void lua_set_as_ret(int md_ret) { lua_pushnumber(Lt,md_ret); }
3138
3139
3140
3141 libscriptapi_sym _liblua =
3142 {
3143 {
3144 0, 0, 0, 0, 0
3145 },
3146
3147 lua_script_start,
3148 lua_script_start_file,
3149 lua_script_run,
3150 lua_script_reset,
3151 lua_set_variable,
3152 lua_set_as_ret,
3153 lua_run_restore,
3154 script_shoot_hook_run,
3155 };
3156
3157 ModuleInfo _module_info =
3158 {
3159 MODULEINFO_V1_MAGICNUM,
3160 sizeof(ModuleInfo),
3161 SCRIPT_API_VERSION,
3162
3163 ANY_CHDK_BRANCH, 0, OPT_ARCHITECTURE,
3164 ANY_PLATFORM_ALLOWED,
3165
3166 -LANG_MODULE_LUA,
3167 MTYPE_SCRIPT_LANG,
3168
3169 &_liblua.base,
3170
3171 CONF_VERSION,
3172 CAM_SCREEN_VERSION,
3173 CAM_SENSOR_VERSION,
3174 CAM_INFO_VERSION,
3175
3176 0,
3177 };
3178
3179