root/modules/luascript.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. chdk_luai_numdiv
  2. chdk_luai_nummod
  3. lua_script_disable_yield_hook
  4. lua_script_enable_yield_hook
  5. lua_create_usb_msg
  6. lua_script_reset
  7. lua_script_error_ptp
  8. lua_count_hook
  9. lua_script_error
  10. lua_script_finish
  11. lua_script_start
  12. lua_script_start_file
  13. lua_script_run
  14. lua_run_restore
  15. lua_get_key_arg
  16. on_off_value_from_lua_arg
  17. luaCB_set_curve_state
  18. luaCB_get_curve_state
  19. luaCB_set_curve_file
  20. luaCB_get_curve_file
  21. luaCB_set_aelock
  22. luaCB_set_aflock
  23. luaCB_set_mf
  24. luaCB_get_sd_over_modes
  25. luaCB_shoot
  26. action_stack_AS_LUA_SLEEP
  27. sleep_delay
  28. luaCB_sleep
  29. luaCB_keyfunc
  30. luaCB_cls
  31. luaCB_set_console_layout
  32. luaCB_set_console_autoredraw
  33. luaCB_console_redraw
  34. luaCB_get_partitionInfo
  35. luaCB_swap_partitions
  36. luaCB_get_av96
  37. luaCB_get_bv96
  38. luaCB_get_day_seconds
  39. luaCB_get_disk_size
  40. luaCB_get_dofinfo
  41. luaCB_get_free_disk_space
  42. luaCB_get_focus
  43. luaCB_get_iso_market
  44. luaCB_get_iso_mode
  45. luaCB_get_iso_real
  46. luaCB_get_jpg_count
  47. luaCB_get_prop
  48. luaCB_get_prop_str
  49. luaCB_get_raw_support
  50. luaCB_get_raw_count
  51. luaCB_get_sv96
  52. luaCB_get_tick_count
  53. luaCB_get_exp_count
  54. luaCB_get_image_dir
  55. luaCB_get_tv96
  56. luaCB_get_user_av_id
  57. luaCB_get_user_av96
  58. luaCB_get_min_av96
  59. luaCB_get_max_av96
  60. luaCB_get_current_av96
  61. luaCB_get_current_tv96
  62. luaCB_get_current_delta_sv96
  63. luaCB_get_current_base_sv96
  64. luaCB_get_nd_value_ev96
  65. luaCB_get_nd_current_ev96
  66. luaCB_get_imager_active
  67. luaCB_get_canon_image_format
  68. luaCB_get_canon_raw_support
  69. luaCB_get_user_tv_id
  70. luaCB_get_user_tv96
  71. luaCB_get_vbatt
  72. luaCB_get_zoom
  73. luaCB_get_parameter_data
  74. luaCB_get_flash_params_count
  75. luaCB_set_av96_direct
  76. luaCB_set_av96
  77. luaCB_set_canon_image_format
  78. luaCB_set_focus_interlock_bypass
  79. luaCB_set_focus
  80. luaCB_set_iso_mode
  81. luaCB_set_iso_real
  82. luaCB_set_led
  83. luaCB_set_nd_filter
  84. luaCB_set_prop
  85. luaCB_set_prop_str
  86. luaCB_set_raw_nr
  87. luaCB_get_raw_nr
  88. luaCB_set_raw
  89. luaCB_get_raw
  90. luaCB_set_sv96
  91. luaCB_set_tv96_direct
  92. luaCB_set_tv96
  93. luaCB_set_user_av_by_id_rel
  94. luaCB_set_user_av_by_id
  95. luaCB_set_user_av96
  96. luaCB_set_user_tv_by_id_rel
  97. luaCB_set_user_tv_by_id
  98. luaCB_set_user_tv96
  99. luaCB_set_zoom_speed
  100. luaCB_set_zoom_rel
  101. luaCB_set_zoom
  102. action_stack_AS_LUA_WAIT_CLICK
  103. luaCB_wait_click
  104. luaCB_is_pressed
  105. luaCB_is_key
  106. luaCB_set_exit_key
  107. luaCB_wheel_right
  108. luaCB_wheel_left
  109. luaCB_md_af_led_control
  110. luaCB_md_get_cell_diff
  111. luaCB_md_get_cell_val
  112. luaCB_md_detect_motion
  113. return_string_selected
  114. action_stack_AS_WAIT_MODULE
  115. luaCB_file_browser
  116. luaCB_textbox
  117. luaCB_draw_pixel
  118. luaCB_draw_line
  119. luaCB_draw_rect
  120. luaCB_draw_rect_filled
  121. luaCB_draw_ellipse
  122. luaCB_draw_ellipse_filled
  123. luaCB_draw_string
  124. luaCB_draw_clear
  125. luaCB_get_gui_screen_width
  126. luaCB_get_gui_screen_height
  127. luaCB_autostarted
  128. luaCB_get_autostart
  129. luaCB_set_autostart
  130. luaCB_get_usb_power
  131. luaCB_set_remote_timing
  132. luaCB_usb_force_active
  133. luaCB_usb_sync_wait
  134. luaCB_enter_alt
  135. luaCB_exit_alt
  136. luaCB_get_alt_mode
  137. luaCB_shut_down
  138. luaCB_print_screen
  139. luaCB_get_movie_status
  140. luaCB_set_movie_status
  141. luaCB_get_video_button
  142. luaCB_get_video_recording
  143. luaCB_get_drive_mode
  144. luaCB_get_focus_mode
  145. luaCB_get_focus_state
  146. luaCB_get_focus_ok
  147. luaCB_get_flash_mode
  148. luaCB_get_shooting
  149. luaCB_get_flash_ready
  150. luaCB_get_IS_mode
  151. luaCB_get_orientation_sensor
  152. luaCB_get_zoom_steps
  153. luaCB_get_nd_present
  154. luaCB_get_propset
  155. luaCB_get_ev
  156. luaCB_set_ev
  157. luaCB_get_histo_range
  158. luaCB_shot_histo_enable
  159. luaCB_shot_histo_write_to_file
  160. luaCB_get_live_histo
  161. luaCB_play_sound
  162. luaCB_get_temperature
  163. luaCB_get_time
  164. luaCB_set_clock
  165. luaCB_peek
  166. luaCB_poke
  167. luaCB_bitand
  168. luaCB_bitor
  169. luaCB_bitxor
  170. luaCB_bitshl
  171. luaCB_bitshri
  172. luaCB_bitshru
  173. luaCB_bitnot
  174. set_string_field
  175. set_number_field
  176. luaCB_get_buildinfo
  177. luaCB_get_mode
  178. luaCB_set_raw_develop
  179. luaCB_raw_merge_start
  180. luaCB_raw_merge_add_file
  181. luaCB_raw_merge_end
  182. luaCB_set_backlight
  183. luaCB_set_lcd_display
  184. luaCB_set_draw_title_line
  185. luaCB_get_draw_title_line
  186. levent_id_from_lua_arg
  187. levent_index_from_id_lua_arg
  188. luaCB_get_levent_def
  189. luaCB_get_levent_index
  190. luaCB_get_levent_def_by_index
  191. luaCB_post_levent_to_ui
  192. luaCB_post_levent_for_npt
  193. luaCB_set_levent_active
  194. luaCB_set_levent_script_mode
  195. luaCB_set_capture_mode_canon
  196. luaCB_set_capture_mode
  197. luaCB_is_capture_mode_valid
  198. luaCB_set_record
  199. luaCB_switch_mode_usb
  200. luaCB_force_analog_av
  201. pack_native_args
  202. luaCB_call_func_ptr
  203. luaCB_call_event_proc
  204. luaCB_reboot
  205. luaCB_get_config_value
  206. luaCB_set_config_value
  207. luaCB_set_config_autosave
  208. luaCB_save_config_file
  209. luaCB_load_config_file
  210. luaCB_set_file_attributes
  211. action_stack_AS_SCRIPT_READ_USB_MSG
  212. action_stack_AS_SCRIPT_WRITE_USB_MSG
  213. luaCB_read_usb_msg
  214. luaCB_write_usb_msg
  215. set_meminfo_num
  216. luaCB_get_meminfo
  217. luaCB_set_yield
  218. luaCB_get_usb_capture_support
  219. luaCB_init_usb_capture
  220. luaCB_get_usb_capture_target
  221. luaCB_set_usb_capture_timeout
  222. luaCB_iso_to_sv96
  223. luaCB_sv96_to_iso
  224. luaCB_iso_real_to_market
  225. luaCB_iso_market_to_real
  226. luaCB_sv96_real_to_market
  227. luaCB_sv96_market_to_real
  228. luaCB_aperture_to_av96
  229. luaCB_av96_to_aperture
  230. luaCB_usec_to_tv96
  231. luaCB_tv96_to_usec
  232. luaCB_seconds_to_tv96
  233. luaCB_shoot_hook_set
  234. luaCB_shoot_hook_is_ready
  235. luaCB_shoot_hook_continue
  236. luaCB_shoot_hook_count
  237. register_shoot_hook_fn
  238. register_shoot_hooks
  239. register_lua_funcs
  240. lua_set_variable
  241. 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"  // for L->nCcalls, baseCcalls
  51 
  52 #ifdef THUMB_FW
  53 // provide div and mod behavior similar to preivous CPUs for digic 6, instead of triggering exception handler
  54 // used in lua core, see luaconf.h
  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; // 1 first 'resume', 0 = resuming from yield
  84 static int run_start_tick; // tick count at start of this kbd_task iteration
  85 static unsigned run_hook_count; // number of calls to the count hook this kbd_task iteration
  86 #define YIELD_CHECK_COUNT 100 // check for yield every N vm instructions
  87 #define YIELD_MAX_COUNT_DEFAULT 25 // 25 checks = 2500 vm instructions
  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 // create a ptp message from the given stack index
 101 // incompatible types will return a TYPE_UNSUPPORTED message
 102 static ptp_script_msg *lua_create_usb_msg( lua_State* L, int index, unsigned msgtype) {
 103     // TODO maybe we should just pass the lua type constants
 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; // nothing on the stack, no message generated
 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         // TODO this uses usb_msg_table_to_string to serialize the table
 128         // the default format is described in
 129         // http://chdk.setepontos.com/index.php?topic=4338.msg62606#msg62606
 130         // other formats can be implemented by overriding this function in your lua code
 131         case LUA_TTABLE: {
 132             int result;
 133             lua_script_disable_yield_hook(); // don't want to yield while converting
 134             lua_getglobal(L, "usb_msg_table_to_string"); // push function
 135             lua_pushvalue(L, index); // copy specified index to top of stack
 136             result = lua_pcall(L,1,1,0); // this will leave an error message as a string on the stack if call fails
 137             lua_script_enable_yield_hook();
 138             if( result ) {
 139                 // if called from lua, throw a normal error
 140                 if( msgtype == PTP_CHDK_S_MSGTYPE_USER ) {
 141                     luaL_error(L,lua_tostring(L,-1));
 142                     return NULL; // not reached
 143                 } else { // if it's a return, convert the message to an ERR
 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             // an empty table is returned as an empty string by default
 151             // a non-string should never show up here
 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); // return type name as message data
 163             datasize = strlen(data);
 164     }
 165     return ptp_script_create_msg(msgtype,datatype,datasize,data);
 166 }
 167 
 168 void lua_script_reset()
 169 {
 170     // in PTP, clean up gui state to ensure not left in script handler
 171     if(lua_script_is_ptp) {
 172         if(ptp_saved_alt_state) {
 173             enter_alt(0); // return to regular "alt" from script GUI mode
 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 // ensure lua_state is closed for compiletime errors
 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 // TODO more stuff from script.c should be moved here
 248 void lua_script_finish(lua_State *L) 
 249 {
 250     if(lua_script_is_ptp) {
 251         // send all return values as RET messages
 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             // if the queue is full return values will be silently discarded
 256             // incompatible types will be returned as TYPE_UNSUPPORTED to preserve expected number and order of return values
 257             if(msg) {
 258                 ptp_script_write_msg(msg); 
 259                 // create_usb_msg may convert the message to an error
 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         // put ui in script alt state to allow key presses to be sent to script
 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 // run a timeslice of lua script
 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         // yielded
 327     }
 328     else if (Lres != 0)
 329     {
 330         return lua_script_error(Lt,1);
 331     }
 332     else 
 333     {
 334         // finished normally, add ptp result
 335         lua_script_finish(Lt);
 336         // Display 'Finished message', unless running from PTP
 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 // run the "restore" function at the end of a script
 346 // Mimic uBasic logic, return 0 to trigger script interrupt immediately
 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 // get key ID of key name at arg, throw error if invalid
 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   get a value where boolean or 0/!0 are accepted for on/off.
 371   normal lua toboolean will convert 0 to true, but ubasic and c users 
 372   will expect 0 to be off
 373   intentional HACK: numbers greater than 1 are returned as is
 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();  // 1: enable AELock
 415   else UnlockAE();       // 0: disable unlock AE
 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();  // 1: enable AFLock
 423   else UnlockAF();       // 0: disable unlock AF
 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();  // 1: enable
 431   else val=UnlockMF();       // 0: disable
 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 // Process a sleep function from the stack
 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     /* delay of -1 signals indefinite (actually 1 day) delay */
 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 // for press,release and click
 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 val=get_prop(id)
 635 get propcase value identified by id
 636 the propcase is read as a short and sign extended to an int
 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 val=get_prop_str(prop_id,length)
 646 get the value of a propertycase as a string
 647 numeric values may be extracted using string.byte or or the binstr.lua module
 648 returns the value as a string, or false if the underlying propcase call returned non-zero
 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 b=get_raw_support()
 670 return true if in rec mode and raw data is available in the current capture mode
 671 NOTE
 672 currently returns true in dedicated video modes, even if still shooting is not possible
 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 // get minimum valid av96 value (widest aperture), or nil if in playback or no iris
 731 static int luaCB_get_min_av96( lua_State* L )
 732 {
 733   short av=shooting_get_min_av96(); 
 734   if(av < 0) { // -1 = Av not available
 735     lua_pushnil(L);
 736   } else {
 737     lua_pushnumber( L, av );
 738   }
 739   return 1;
 740 }
 741 
 742 // get maximum valid av96 value (smallest aperture), or nil if in playback or no iris
 743 static int luaCB_get_max_av96( lua_State* L )
 744 {
 745   short av=shooting_get_max_av96(); 
 746   if(av < 0) { // -1 = Av not available
 747     lua_pushnil(L);
 748   } else {
 749     lua_pushnumber( L, av );
 750   }
 751   return 1;
 752 }
 753 
 754 // get current av96 value - actual current value, not from half press propcase
 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 // get current tv96 value - actual current value, not from half press propcase
 762 // returns nil if image sensor not active (playback, sleep mode, etc)
 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 // get live view "delta sv" value: APEX*96 offset from base value
 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 // get live view "drive base sv" value: APEX*96 base value
 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 // get the exposure value of the ND filter, or 0 if not present
 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 // get the current ND value: 0 if out or not present, or nd_value if in
 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 // return true if sensor is enabled (live view on), false if not (playback, rec with display off, display off power save)
 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 // return current canon image format as bitmask 1 = jpg, 2 = raw, 3 = raw+jpg
 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 // does cam support canon raw?
 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     // return nil
 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     // for convenience, params that fit in a number are returned in one as a second result
 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 // set current canon image format as bitmask 1 = jpg, 2 = raw, 3 = raw+jpg
 898 // returns true if support format, false if not
 899 // NOTE: this setting is lost in shooting mode and play/rec switches
 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     // if sd override not available now, fail immediately without calling set_focus
 916     // to avoid unexpected results with SET_LATER
 917     if(!shooting_can_focus())
 918     {
 919         lua_pushboolean(L, 0);
 920         return 1;
 921     }
 922     // NOTE duplicated in modules/luascript.c and lib/ubasic/ubasic.c
 923     // in AF lock or MF (canon or set by MF functions), set focus now
 924     if (shooting_get_prop(camera_info.props.af_lock) 
 925       || shooting_get_focus_mode()
 926       || camera_info.state.mode_video)  // TODO video needs to be investigated, carried over from old code
 927     {
 928       shooting_set_focus(sd, SET_NOW);
 929     }
 930     else
 931     {
 932       // in an AF mode, set later
 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 set_prop(id,value)
 971 the value is treated as a short
 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 status=set_prop_str(prop_id,value)
 981 set propertycase value as a string. Length is taken from the string
 982 numeric propcase values may be assembled by setting byte values using string.char or the binstr module
 983 status: boolean - true if the underlying propcase call returns 0, otherwise false
 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 // Wait for a button to be pressed and released (or the timeout to expire)
1096 static int action_stack_AS_LUA_WAIT_CLICK()
1097 {
1098     // Check key pressed or timeout
1099     if ((get_tick_count() >= action_top(2)) || camera_info.state.kbd_last_clicked)
1100     {
1101         // If timed out set key state to "no_key", otherwise key pressed so set last checked time
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     // Reset 'clicked' key if it has not changed since last time
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     // Set up for wait or click testing
1127     action_push(delay);
1128     action_push_func(action_stack_AS_LUA_WAIT_CLICK);
1129 
1130     // Check for short delay or key already pressed by calling action stack routine once now
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   // TODO can't check if valid for this camera
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;        // Enable MD testing with AF LED
1176         // clear previous values
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;        // Disable MD testing
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     // arg 8 is the return value in ubasic. We
1208     // ignore it here. - AUJ
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     // Reconnect button input to script - will also signal action stack
1233     // that file browser / textbox is finished and return last selected file
1234     // to script caller
1235     camera_info.state.state_kbd_script_run = SCRIPT_STATE_RAN;
1236 
1237     // Push selected file as script return value
1238     lua_pushstring( Lt, (str && str[0])? str : NULL );
1239 }
1240 
1241 static int action_stack_AS_WAIT_MODULE()
1242 {
1243     // state_kbd_script_run is set to 0 when the file browser is started from a Lua script
1244     // it is reset back to 1 when the file browser exits and control is returned back to
1245     // the script
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     // Disconnect button input from script so buttons will work in file browser
1255     camera_info.state.state_kbd_script_run = SCRIPT_STATE_INACTIVE;
1256     // Push file browser action onto stack - will loop doing nothing until file browser exits
1257     action_push_func(action_stack_AS_WAIT_MODULE);
1258     // Switch to file browser gui mode. Path can be supplied in call or defaults to "A" (root directory).
1259     libfselect->file_select(LANG_STR_FILE_BROWSER, luaL_optstring( L, 1, "A" ), "A", return_string_selected);
1260     // Yield the script so that the action stack will process the AS_FILE_BROWSER action
1261     return lua_yield(L, 0);
1262 }
1263 
1264 static int luaCB_textbox( lua_State* L ) {
1265     // Disconnect button input from script so buttons will work in the textbox
1266     camera_info.state.state_kbd_script_run = SCRIPT_STATE_INACTIVE;
1267     // Switch to textbox gui mode. Text box prompt should be passed as param.
1268     int rv = libtextbox->textbox_init((int)luaL_optstring( L, 1, "Text box" ),   //title
1269         (int)luaL_optstring( L, 2, "Enter text" ), //message
1270         luaL_optstring( L, 3, ""  ),               //default string
1271         luaL_optnumber( L, 4, 30),                 //max size of a text
1272         return_string_selected, 0);
1273     if (rv)
1274     {
1275         // Push textbox action onto stack - will loop doing nothing until textbox exits
1276         action_push_func(action_stack_AS_WAIT_MODULE);
1277     }
1278     else
1279         return_string_selected(0);
1280 
1281     // Yield the script so that the action stack will process the AS_TEXTBOX action
1282     return lua_yield(L, 0);
1283 }
1284 
1285 // begin lua draw fuctions
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 // end lua draw functions
1370 
1371 // bitmap dimensions, size matches coordinates of draw functions
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 // enable USB High Perfomance timer
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 // enable shared USB port between ptp and precision sync
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 // set next shot to wait for USB sync ( 5V - 0V transition )
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   // if alt explicitly changed by script, set as 'saved' state
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   // if alt explicitly changed by script, set as 'saved' state
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 // optional parameter is 0 for soft shutdown (default) or 1 for hard/immediate
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 histogram,total=get_live_histo()
1625 returns a histogram of Y values from the viewport buffer (downsampled by HISTO_STEP_SIZE)
1626 histogram[Y value] = count, so it is zero based unlike a normal lua array
1627 total is the total number of pixels, may vary depending on viewport size
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; // do something insane if users passes bad value
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 set_clock(year, month, day, hour, minute, second)
1691 
1692 sets camera clock, including RTC
1693 values are as they appear in camera UI, full year, month and day start at 1
1694 does not change DST state, time set is time displayed
1695 also updates tick_count_offset
1696 no validation in CHDK
1697 */
1698 static int luaCB_set_clock( lua_State* L )
1699 {
1700     set_clock(luaL_checknumber(L,1), // year, like 2020
1701             luaL_checknumber(L,2), // month, 1-12
1702             luaL_checknumber(L,3), // day, 1-31
1703             luaL_checknumber(L,4), // hour
1704             luaL_checknumber(L,5), // minute
1705             luaL_checknumber(L,6)); // second
1706     return 0;
1707 }
1708 
1709 /*
1710   val=peek(address[,size])
1711   return the value found at address in memory, or nil if address or size is invalid
1712   size is optional 1=byte 2=halfword 4=word. defaults is 4
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   status=poke(address,value[,size])
1747   writes value to address in memory
1748   size is optional 1=byte 2=halfword 4=word. defaults is 4
1749   returns true, or nil if address or size is invalid
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 // TODO sanity check file ?
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 // TODO sanity check file ? Get it from C
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 // Enable/disable LCD back light (input argument 1/0)
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 // Enable/disable LCD display (input argument 1/0)
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 // Enable/disable CHDK <ALT> & scriptname OSD items (input argument 1/0)
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 // get CHDK <ALT> & scriptname OSD display state (input argument 1/0)
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 // get the string or number passed in index and return it as an event id
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   // could check here if it is in the table, but even valid ones can crash
1954   // so we avoid searching the table if given a number
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   return the index of an event, given it's name or event id
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   name,id,param = get_levent_def(event)
1983   event is an event id (number) or name (string)
1984   returns nil if event is not found
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   index=get_levent_index(event)
2001   event is an event id (number) or name (string)
2002   returns index or nil if not found
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   name,id,param = get_levent_def_by_index(event_index)
2018   event_index is number index into the event table
2019   returns nil if event is not found
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   post_levent_*(event[,unk])
2036   post the event with PostLogicalEventToUI or PostLogicaEventForNotPowerType
2037   This sends the event. The difference between functions isn't clear.
2038   event is an event id (number) or name (string).
2039   unk is an optional number whose meaning is unknown, defaults to zero. 
2040     Based on code, other values would probably be a pointer.
2041     This is NOT the 3rd item in the event table.
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   set_levent_active(event,state)
2065   event is an event id (number) or name (string)
2066   state is a numeric or boolean state. true or non zero numbers turn on zero, false or nil turn off
2067   exact meaning is unknown, but it has something to do with the delivery of the specified event.
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   set_levent_script_mode(state)
2082   state is numeric or boolean state. true or non zero numbers turn on zero, false or nil turn off
2083   exact meaning is unknown, but it has something to do with the behavior of events and/or SetLogicalEventActive.
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   result=set_capture_mode_canon(value)
2093   where value is a valid PROPCASE_SHOOTING_MODE value for the current camera
2094   result is true if the camera is in rec mode
2095 */
2096 static int luaCB_set_capture_mode_canon( lua_State* L )
2097 {
2098   int modenum = luaL_checknumber(L,1);
2099   // if the value as negative, assume it is a mistakenly sign extended PROPCASE_SHOOTING_MODE value
2100   if(modenum < 0) 
2101     modenum &= 0xFFFF;
2102   lua_pushboolean( L, shooting_set_mode_canon(modenum) );
2103   return 1;
2104 }
2105 
2106 /*
2107  result=set_capture_mode(modenum)
2108  where modenum is a valid CHDK modemap value
2109  result is true if modenum is a valid modemap value, otherwise false
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  result=is_capture_mode_valid(modenum)
2120  where modenum is a valid CHDK modemap value
2121  result is true if modenum is a valid modemap value, otherwise false
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   set_record(state)
2132   if state is 0 (or false) the camera is set to play mode. If 1 or true, the camera is set to record mode.
2133   NOTE: this only begins the mode change. Script should wait until get_mode() reflects the change,
2134   before doing anything that requires the new mode. e.g.
2135   set_record(true)
2136   while not get_mode() do
2137     sleep(10)
2138   end
2139   uses switch_mode_usb if required
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 // switch mode (0 = playback, 1 = record)
2148 // only for when USB is connected
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   result=force_analog_av(state)
2157   force state of analog video connector detect bit
2158   where 0 = don't force, 1 = on, 2 = off
2159   result true if implemented by port (ANALOG_AV_FLAG defined), false if not
2160   can be used to enable video out while using the AV bit as remote input
2161   NOTE: video out affects display resolution on some cameras, which 
2162   may not be accounted for in features like PTP live view, zebra, histrogram etc
2163   forcing video out while PTP is in use may cause problems on some cameras
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 pack the lua args into a buffer to pass to the native code calling functions 
2173 currently only handles strings/numbers
2174 start is the stack index of the first arg
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 Native function call interface. Can be used to call canon eventprocs or arbitrary
2197 pointers.
2198 
2199 NOTE: this is preliminary, interface may change in later versions!
2200 All arguments must be strings or numbers.
2201 If the function expects to modify it's arguments via a pointer,
2202 then you must provide a number that is a valid pointer. 
2203 
2204 You can use the "AllocateMemory" eventproc to obtain buffers.
2205 
2206 If the function tries to write to a string passed from lua, Bad Things may happen.
2207 
2208 This is potentially dangerous, functions exist which can destroy the onboard firmware.
2209 */
2210 
2211 /*
2212 result=call_func_ptr(ptr,...)
2213 ptr: address of a valid ARM or Thumb function, which uses the normal C calling convention.
2214 result: R0 value after the call returns
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 Call an event procedure
2249 
2250 result=call_event_proc("EventprocName",...)
2251 result is the value returned by ExecuteEventProcedure, which is -1 if the eventproc is not found, 
2252 or the eventproc return value (which could also be -1)
2253 NOTE:
2254 Many eventprocs are not registered by default, but can be loaded by calling another event proc
2255 Some useful ones are
2256 SystemEventInit
2257     includes AllocateMemory, FreeMemory, sprintf, memcpy, Fut functions, log ...
2258 UI_RegistDebugEventProc
2259     includes capture mode functions, PTM_ functions and much more 
2260 RegisterProductTestEvent
2261     includes PT_ functions
2262 
2263 Others:
2264 RegisterShootSeqEvent
2265 RegisterNRTableEvent
2266 */
2267 
2268 // grab from lowlevel
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         // event proc name is first arg
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 result = reboot(["filename"])
2304 returns false on failure, does not return on success
2305 see lib/armutil/reboot.c for details
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);  //t[i+1]=configVal.pInt[i]
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) { //Default
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};  //initialize isXXX
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 msg = read_usb_msg([timeout])
2464 read a message from the CHDK ptp interface.
2465 Returns the next available message as a string, or nil if no messages are available
2466 If timeout is given and not zero, wait until a message is available or timeout expires
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 status = write_usb_msg(msg,[timeout])
2490 writes a message to the CHDK ptp interface
2491 msg may be nil, boolean, number, string or table (table has some restrictions, will be converted to string)
2492 returns true if the message was queued successfully, otherwise false
2493 if timeout is set and not zero, wait until message is written or timeout expires
2494 NOTE strings will not include a terminating NULL, must be handled by recipient
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   // TODO would it be better to either ignore this or return nil ?
2501   // a write_usb_msg(function_which_returns_no_value()) is an error in this case
2502   // replacing with nil might be more luaish
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   // for user messages, trying to create a message from an incompatible type throws an error
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 /* helper for meminfo to set table field only if valid */
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 meminfo=get_meminfo([heapname])
2537 get camera memory information
2538 heapname="combined", "system", "aram" or "exmem" 
2539 if not given, combined is returned
2540 meminfo is false if the requested heapname isn't valid ("exmem" when exmem is not enabled, or unknown)
2541 otherwise, a table of the form
2542 meminfo = {
2543     name -- heapname, as above
2544     chdk_malloc -- bool, this is the heap used by CHDK for malloc
2545     chdk_start -- number, load address of CHDK
2546     chdk_size -- number, size of CHDK image
2547     -- all the following are numbers, will not be set if not available
2548     start_address
2549     end_address
2550     total_size
2551     allocated_size
2552     allocated_peak
2553     allocated_count
2554     free_size
2555     free_block_max_size
2556     free_block_count
2557 }
2558 NOTES
2559 * under vxworks and cameras without GetMemInfo only the only valid fields
2560   for the system heap will be those defined by chdk and free_block_max_size
2561 * the meaning of fields may not correspond exactly between exmem and system
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; // not meaningful
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     // adjust start and size, if CHDK is loaded at heap start
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); // might not always use 13, but doesn't hurt
2599     set_string_field( L,"name", heapname );
2600     // CHDK allocates from all available heaps now
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 set scheduling parameters
2619 old_max_count,old_max_ms=set_yield(max_count,max_ms)
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 //static void register_func( lua_State* L, const char *name, void *func) {
2631 //  lua_pushcfunction( L, func );
2632 //  lua_setglobal( L, name );
2633 //}
2634 /*
2635 get remote capture supported types
2636 bitmask=get_usb_capture_support()
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 status=init_usb_capture(bitmask[,startline, numlines])
2646 bitmask = 0 clear usb capture mode
2647 
2648 lines only applies to raw
2649 startline defaults to 0
2650 numlines defaults to full buffer
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 selected=get_usb_capture_target()
2663 selected = bitmask passed to init, or 0 if capture not configured or timed out/canceled
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 set_remotecap_timeout([timeout])
2673 timeout:
2674 number of milliseconds remote capture waits for data of each type to be downloaded
2675 <=0 or no value resets to the default value
2676 If any data type is not downloaded before the timeout expires, remote capture is canceled
2677 and none of the subsequent data types will be returned
2678 following a timeout, RemoteCaptureIsReady and RemoteCaptureGetData will behave as if
2679 remote capture were not initialized
2680 If the timeout expires while a transfer is in progress, an error will be generated
2681 and the data may be incomplete or corrupt
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 // APEX conversion functions
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 // Shoot hooks
2760 
2761 /*
2762 hook.set(timeout)
2763 cause hook to block shooting process until timeout or script issues hook.continue
2764 0 clears
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 hook.is_ready()
2775 returns true if the hooked task is in the hook
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 hook.continue()
2786 allow the hooked task to leave the hook
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 n=hook.count()
2797 return the number of times the hook has been reached since script start
2798 note: counts regardless of whether hook is enabled
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     // NOTE these functions normally run in the spytask.
2954     // called from lua they will run from kbd task instead
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     // get levent definition by name or id, nil if not found
2971     FUNC(get_levent_def)
2972     // get levent definition by index, nil if out of range
2973     FUNC(get_levent_def_by_index)
2974     // get levent index from name or ID
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 //        register_shoot_hook_fn(L,i,luaCB_shoot_hook_wait_ready,"wait_ready");
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 // =========  MODULE INIT =================
3107 
3108 /***************** BEGIN OF AUXILARY PART *********************
3109   ATTENTION: DO NOT REMOVE OR CHANGE SIGNATURES IN THIS SECTION
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);        // Make value 1 based for Lua table index
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 /******************** Module Information structure ******************/
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,         // Module version
3162 
3163     ANY_CHDK_BRANCH, 0, OPT_ARCHITECTURE,   // Requirements of CHDK version
3164     ANY_PLATFORM_ALLOWED,       // Specify platform dependency
3165 
3166     -LANG_MODULE_LUA,           // Module name
3167     MTYPE_SCRIPT_LANG,          //Run Lua Scripts
3168 
3169     &_liblua.base,
3170 
3171     CONF_VERSION,               // CONF version
3172     CAM_SCREEN_VERSION,         // CAM SCREEN version
3173     CAM_SENSOR_VERSION,         // CAM SENSOR version
3174     CAM_INFO_VERSION,           // CAM INFO version
3175 
3176     0,
3177 };
3178 
3179 /*************** END OF AUXILARY PART *******************/

/* [<][>][^][v][top][bottom][index][help] */