source: trunk/core/luascript.c @ 1036

Revision 1036, 34.3 KB checked in by reyalP, 2 years ago (diff)

clean up key handling some more, fix broken MD in lua, make ubasic_test build again.

  • Property svn:eol-style set to native
Line 
1#include "luascript.h"
2#include "kbd.h"
3#include "platform.h"
4#include "script.h"
5#include "lua.h"
6#include "lualib.h"
7#include "lauxlib.h"
8#include "conf.h"
9#include "shot_histogram.h"
10#include "stdlib.h"
11#include "raw.h"
12#include "raw_merge.h"
13#include "levent.h"
14#include "console.h"
15#include "action_stack.h"
16#include "motion_detector.h"
17
18#include "../lib/lua/lstate.h"  // for L->nCcalls, baseCcalls
19
20lua_State* L;
21lua_State* Lt;
22int lua_keep_result;
23
24void *lua_consume_result()
25{
26  lua_State* r = L;
27  L = 0;
28  return r;
29}
30
31void lua_script_reset()
32{
33  if ( !lua_keep_result )
34  {
35    lua_close( L );
36    L = 0;
37  }
38  Lt = 0;
39}
40
41static void lua_count_hook(lua_State *L, lua_Debug *ar)
42{
43  if( L->nCcalls <= L->baseCcalls )
44    lua_yield( L, 0 );
45}
46
47int lua_script_start( char const* script )
48{
49  lua_keep_result = 0;
50  L = lua_open();
51  luaL_openlibs( L );
52  register_lua_funcs( L );
53
54  Lt = lua_newthread( L );
55  lua_setfield( L, LUA_REGISTRYINDEX, "Lt" );
56  if( luaL_loadstring( Lt, script ) != 0 ) {
57    script_console_add_line( lua_tostring( Lt, -1 ) );
58    lua_script_reset();
59    return 0;
60  }
61  lua_sethook(Lt, lua_count_hook, LUA_MASKCOUNT, 1000 );
62  return 1;
63}
64
65// run the "restore" function at the end of a script
66void lua_run_restore()
67{
68        lua_getglobal(Lt, "restore");
69        if (lua_isfunction(Lt, -1)) {
70                if (lua_pcall( Lt, 0, 0, 0 )) {
71                        script_console_add_line( lua_tostring( Lt, -1 ) );
72                }
73        }
74}
75
76// get key ID of key name at arg, throw error if invalid
77static int lua_get_key_arg( lua_State * L, int narg )
78{
79    int k = script_keyid_by_name( luaL_checkstring( L, narg ) );
80    if(!k)
81        luaL_error( L, "unknown key" );
82    return k;
83}
84
85#ifdef OPT_CURVES
86#include "curves.h"
87
88static int luaCB_set_curve_state( lua_State* L )
89{
90  int value;
91  value=luaL_checknumber( L, 1 );
92  curve_set_mode(value);
93  return 0;
94}
95#endif
96
97static int luaCB_set_aflock(lua_State* L)
98{
99  int val = luaL_checknumber(L, 1);
100  if (val>0) DoAFLock();  // 1: enable AFLock
101  else UnlockAF();       // 0: disable unlock AF
102  return 0;
103}
104
105
106static int luaCB_shoot( lua_State* L )
107{
108  action_push(AS_SHOOT);
109  return lua_yield( L, 0 );
110}
111
112static int luaCB_sleep( lua_State* L )
113{
114  action_push_delay( luaL_checknumber( L, 1 ) );
115  return lua_yield( L, 0 );
116}
117
118// for press,release and click
119static int luaCB_keyfunc( lua_State* L )
120{
121  void* func = lua_touserdata( L, lua_upvalueindex(1) );
122  ((void(*)(long))func)( lua_get_key_arg( L, 1 ) );
123  return lua_yield( L, 0 );
124}
125
126static int luaCB_cls( lua_State* L )
127{
128  console_clear();
129  return 0;
130}
131
132static int luaCB_set_console_layout( lua_State* L )
133{
134  console_set_layout(luaL_checknumber( L, 1 ),luaL_checknumber( L, 2 ),luaL_checknumber( L, 3 ),luaL_checknumber( L, 4 ));
135  return 0;
136}
137
138static int luaCB_set_console_autoredraw( lua_State* L )
139{
140  console_set_autoredraw(luaL_checknumber(L,1));
141  return 0;
142}
143
144static int luaCB_console_redraw( lua_State* L )
145{
146  console_redraw();
147  return 0;
148}
149static int luaCB_get_av96( lua_State* L )
150{
151  lua_pushnumber( L, shooting_get_av96() );
152  return 1;
153}
154
155static int luaCB_get_bv96( lua_State* L )
156{
157  lua_pushnumber( L, shooting_get_bv96() );
158  return 1;
159}
160
161static int luaCB_get_day_seconds( lua_State* L )
162{
163  lua_pushnumber( L, shooting_get_day_seconds() );
164  return 1;
165}
166
167static int luaCB_get_disk_size( lua_State* L )
168{
169  lua_pushnumber( L, GetTotalCardSpaceKb() );
170  return 1;
171}
172
173static int luaCB_get_dof( lua_State* L )
174{
175  lua_pushnumber( L, shooting_get_depth_of_field() );
176  return 1;
177}
178
179static int luaCB_get_far_limit( lua_State* L )
180{
181  lua_pushnumber( L, shooting_get_far_limit_of_acceptable_sharpness() );
182  return 1;
183}
184
185static int luaCB_get_free_disk_space( lua_State* L )
186{
187  lua_pushnumber( L, GetFreeCardSpaceKb() );
188  return 1;
189}
190
191static int luaCB_get_focus( lua_State* L )
192{
193  lua_pushnumber( L, shooting_get_subject_distance() );
194  return 1;
195}
196
197static int luaCB_get_hyp_dist( lua_State* L )
198{
199  lua_pushnumber( L, shooting_get_hyperfocal_distance() );
200  return 1;
201}
202
203static int luaCB_get_iso_market( lua_State* L )
204{
205  lua_pushnumber( L, shooting_get_iso_market() );
206  return 1;
207}
208
209static int luaCB_get_iso_mode( lua_State* L )
210{
211  lua_pushnumber( L, shooting_get_iso_mode() );
212  return 1;
213}
214
215static int luaCB_get_iso_real( lua_State* L )
216{
217  lua_pushnumber( L, shooting_get_iso_real() );
218  return 1;
219}
220
221static int luaCB_get_jpg_count( lua_State* L )
222{
223  lua_pushnumber( L, GetJpgCount() );
224  return 1;
225}
226
227static int luaCB_get_near_limit( lua_State* L )
228{
229  lua_pushnumber( L, shooting_get_near_limit_of_acceptable_sharpness() );
230  return 1;
231}
232
233static int luaCB_get_prop( lua_State* L )
234{
235  lua_pushnumber( L, shooting_get_prop( luaL_checknumber( L, 1 ) ) );
236  return 1;
237}
238
239static int luaCB_get_raw_count( lua_State* L )
240{
241  lua_pushnumber( L, GetRawCount() );
242  return 1;
243}
244
245static int luaCB_get_sv96( lua_State* L )
246{
247  lua_pushnumber( L, shooting_get_sv96() );
248  return 1;
249}
250
251static int luaCB_get_tick_count( lua_State* L )
252{
253  lua_pushnumber( L, shooting_get_tick_count() );
254  return 1;
255}
256
257static int luaCB_get_exp_count( lua_State* L )
258{
259  lua_pushnumber( L, get_exposure_counter() );
260  return 1;
261}
262
263static int luaCB_get_tv96( lua_State* L )
264{
265  lua_pushnumber( L, shooting_get_tv96() );
266  return 1;
267}
268
269static int luaCB_get_user_av_id( lua_State* L )
270{
271  lua_pushnumber( L, shooting_get_user_av_id() );
272  return 1;
273}
274
275static int luaCB_get_user_av96( lua_State* L )
276{
277  lua_pushnumber( L, shooting_get_user_av96() );
278  return 1;
279}
280
281static int luaCB_get_user_tv_id( lua_State* L )
282{
283  lua_pushnumber( L, shooting_get_user_tv_id() );
284  return 1;
285}
286
287static int luaCB_get_user_tv96( lua_State* L )
288{
289  lua_pushnumber( L, shooting_get_user_tv96() );
290  return 1;
291}
292
293static int luaCB_get_vbatt( lua_State* L )
294{
295  lua_pushnumber( L, stat_get_vbatt() );
296  return 1;
297}
298
299static int luaCB_get_zoom( lua_State* L )
300{
301  lua_pushnumber( L, shooting_get_zoom() );
302  return 1;
303}
304
305static int luaCB_get_parameter_data( lua_State* L )
306{
307  extern long* FlashParamsTable[];
308
309  unsigned size;
310  unsigned id = luaL_checknumber( L, 1 );
311  unsigned val;
312
313  if (id >= get_flash_params_count()) {
314    // return nil
315    return 0;
316  }
317
318  size = FlashParamsTable[id][1]>>16;
319  if (size == 0) {
320    // return nil
321    return 0;
322  }
323  if (size >= 1 && size <= 4) {
324    val = 0;
325    get_parameter_data( id, &val, size );
326    lua_pushlstring( L, (char *)&val, size );
327    // for convenience, params that fit in a number are returned in one as a second result
328    lua_pushnumber( L, val );
329    return 2;
330  }
331  else {
332    char *buf = malloc(size);
333    if(!buf) {
334      luaL_error( L, "malloc failed in luaCB_get_parameter_data" );
335    }
336    get_parameter_data( id, buf, size );
337    lua_pushlstring( L, buf, size );
338    free(buf);
339    return 1;
340  }
341}
342
343static int luaCB_get_flash_params_count( lua_State* L )
344{
345  lua_pushnumber( L, get_flash_params_count() );
346  return 1;
347}
348
349static int luaCB_set_av96_direct( lua_State* L )
350{
351  shooting_set_av96_direct( luaL_checknumber( L, 1 ), SET_LATER );
352  return 0;
353}
354
355static int luaCB_set_av96( lua_State* L )
356{
357  shooting_set_av96( luaL_checknumber( L, 1 ), SET_LATER );
358  return 0;
359}
360
361static int luaCB_set_focus( lua_State* L )
362{
363    int to = luaL_checknumber( L, 1 );
364    int m=mode_get()&MODE_SHOOTING_MASK;
365    int mode_video=MODE_IS_VIDEO(m);
366
367#if CAM_HAS_MANUAL_FOCUS
368    if (shooting_get_focus_mode() || (mode_video)) shooting_set_focus(to, SET_NOW);
369    else shooting_set_focus(to, SET_LATER);
370#else
371    if (mode_video) shooting_set_focus(to, SET_NOW);
372    else shooting_set_focus(to, SET_LATER);   
373#endif   
374  return 0;
375}
376
377static int luaCB_set_iso_mode( lua_State* L )
378{
379  shooting_set_iso_mode( luaL_checknumber( L, 1 ) );
380  return 0;
381}
382
383static int luaCB_set_iso_real( lua_State* L )
384{
385  shooting_set_iso_real( luaL_checknumber( L, 1 ), SET_LATER);
386  return 0;
387}
388
389static int luaCB_set_led( lua_State* L )
390{
391  int to, to1, to2;
392  to = luaL_checknumber( L, 1 );
393  to1 = luaL_checknumber( L, 2 );
394  to2 = 200;
395  if( lua_isnumber( L, 3 ) )
396    to2 = lua_tonumber( L, 3 );
397  camera_set_led(to, to1, to2);
398  return 0;
399}
400
401static int luaCB_set_nd_filter( lua_State* L )
402{
403  shooting_set_nd_filter_state( luaL_checknumber( L, 1 ), SET_LATER);
404  return 0;
405}
406
407static int luaCB_set_prop( lua_State* L )
408{
409  int to, to1;
410  to = luaL_checknumber( L, 1 );
411  to1 = luaL_checknumber( L, 2 );
412  shooting_set_prop(to, to1);
413  return 0;
414}
415
416static int luaCB_set_raw_nr( lua_State* L )
417{
418  camera_set_nr(luaL_checknumber( L, 1 ));
419  return 0;
420}
421
422static int luaCB_get_raw_nr( lua_State* L )
423{
424  lua_pushnumber( L, camera_get_nr() );
425  return 1;
426}
427
428static int luaCB_set_raw( lua_State* L )
429{
430  camera_set_raw(luaL_checknumber( L, 1 ));
431  return 0;
432}
433
434static int luaCB_get_raw( lua_State* L )
435{
436  lua_pushnumber( L, conf.save_raw );
437  return 1;
438}
439
440static int luaCB_set_sv96( lua_State* L )
441{
442  shooting_set_sv96(luaL_checknumber( L, 1 ), SET_LATER);
443  return 0;
444}
445
446static int luaCB_set_tv96_direct( lua_State* L )
447{
448  shooting_set_tv96_direct(luaL_checknumber( L, 1 ), SET_LATER);
449  return 0;
450}
451
452static int luaCB_set_tv96( lua_State* L )
453{
454  shooting_set_tv96(luaL_checknumber( L, 1 ), SET_LATER);
455  return 0;
456}
457
458static int luaCB_set_user_av_by_id_rel( lua_State* L )
459{
460  shooting_set_user_av_by_id_rel(luaL_checknumber( L, 1 ));
461  return 0;
462}
463
464static int luaCB_set_user_av_by_id( lua_State* L )
465{
466  shooting_set_user_av_by_id(luaL_checknumber( L, 1 ));
467  return 0;
468}
469
470static int luaCB_set_user_av96( lua_State* L )
471{
472  shooting_set_user_av96(luaL_checknumber( L, 1 ));
473  return 0;
474}
475
476static int luaCB_set_user_tv_by_id_rel( lua_State* L )
477{
478  shooting_set_user_tv_by_id_rel(luaL_checknumber( L, 1 ));
479  return 0;
480}
481
482static int luaCB_set_user_tv_by_id( lua_State* L )
483{
484  shooting_set_user_tv_by_id(luaL_checknumber( L, 1 ));
485  return 0;
486}
487
488static int luaCB_set_user_tv96( lua_State* L )
489{
490  shooting_set_user_tv96(luaL_checknumber( L, 1 ));
491  return 0;
492}
493
494static int luaCB_set_zoom_speed( lua_State* L )
495{
496  shooting_set_zoom_speed(luaL_checknumber( L, 1 ));
497  return 0;
498}
499
500static int luaCB_set_zoom_rel( lua_State* L )
501{
502  shooting_set_zoom_rel(luaL_checknumber( L, 1 ));
503  return 0;
504}
505
506static int luaCB_set_zoom( lua_State* L )
507{
508  shooting_set_zoom(luaL_checknumber( L, 1 ));
509  return 0;
510}
511
512static int luaCB_wait_click( lua_State* L )
513{
514  int timeout = luaL_optnumber( L, 1, 0 );
515  action_wait_for_click(timeout);
516  return lua_yield( L, 0 );
517}
518
519static int luaCB_is_pressed( lua_State* L )
520{
521  lua_pushboolean( L, script_key_is_pressed(lua_get_key_arg( L, 1 )));
522  return 1;
523}
524
525static int luaCB_is_key( lua_State* L )
526{
527  lua_pushboolean( L, script_key_is_clicked(lua_get_key_arg( L, 1 )));
528  return 1;
529}
530
531#if CAM_HAS_JOGDIAL
532static int luaCB_wheel_right( lua_State* L )
533{
534  JogDial_CW();
535  return 0;
536}
537
538static int luaCB_wheel_left( lua_State* L )
539{
540  JogDial_CCW();
541  return 0;
542}
543#endif
544
545static int luaCB_md_get_cell_diff( lua_State* L )
546{
547  lua_pushnumber( L, md_get_cell_diff(luaL_checknumber(L,1),
548                                      luaL_checknumber(L,2)));
549  return 1;
550}
551
552static int luaCB_md_detect_motion( lua_State* L )
553{
554  int columns = (luaL_optnumber(L,1,6));
555  int rows = (luaL_optnumber(L,2,4));
556  int pixel_measure_mode = (luaL_optnumber(L,3,1));
557  int detection_timeout = (luaL_optnumber(L,4,10000));
558  int measure_interval = (luaL_optnumber(L,5,7));
559  int threshold = (luaL_optnumber(L,6,10));
560  int draw_grid = (luaL_optnumber(L,7,1));
561   // arg 8 is the return value in ubasic. We
562   // ignore it here. - AUJ
563  int clipping_region_mode = (luaL_optnumber(L,9,0));
564  int clipping_region_column1 = (luaL_optnumber(L,10,0));
565  int clipping_region_row1 = (luaL_optnumber(L,11,0));
566  int clipping_region_column2 = (luaL_optnumber(L,12,0));
567  int clipping_region_row2 = (luaL_optnumber(L,13,0));
568  int parameters = (luaL_optnumber(L,14,1));
569  int pixels_step = (luaL_optnumber(L,15,6));
570  int msecs_before_trigger = (luaL_optnumber(L,16,0));
571  if(md_init_motion_detector(
572    columns, rows, pixel_measure_mode, detection_timeout,
573    measure_interval, threshold, draw_grid,
574    clipping_region_mode,
575    clipping_region_column1, clipping_region_row1,
576    clipping_region_column2, clipping_region_row2,
577    parameters, pixels_step, msecs_before_trigger
578  ))
579    return lua_yield(L, 0);
580  else
581    return luaL_error( L, "md_init_motion_detector failed" );
582}
583
584static int luaCB_autostarted( lua_State* L )
585{
586  lua_pushboolean( L, camera_get_script_autostart() );
587  return 1;
588}
589
590static int luaCB_get_autostart( lua_State* L )
591{
592  lua_pushnumber( L, conf.script_startup );
593  return 1;
594}
595
596static int luaCB_set_autostart( lua_State* L )
597{
598  int to;
599  to = luaL_checknumber( L, 1 );
600  if ( to >= 0 && to <= 2 ) conf.script_startup = to;
601  conf_save();
602  return 0;
603}
604
605static int luaCB_get_usb_power( lua_State* L )
606{
607  if (luaL_optnumber( L, 1, 0 )) lua_pushnumber( L, get_usb_power(1) );
608  else lua_pushnumber( L, get_usb_power(0) );
609  return 1;
610}
611
612static int luaCB_exit_alt( lua_State* L )
613{
614  exit_alt();
615  return 0;
616}
617
618// optional parameter is 0 for soft shutdown (default) or 1 for hard/immediate
619static int luaCB_shut_down( lua_State* L )
620{
621  if ( luaL_optnumber(L,1,0) == 1 )
622  {
623    shutdown();
624  } else {
625  camera_shutdown_in_a_second();
626  }
627  return 0;
628}
629
630static int luaCB_print_screen( lua_State* L )
631{
632 
633  if (lua_isboolean( L, 1 ))
634    script_print_screen_statement( lua_toboolean( L, 1 ) );
635  else
636    script_print_screen_statement( luaL_checknumber( L, 1 )+10000 );
637  return 0;
638}
639
640static int luaCB_get_movie_status( lua_State* L )
641{
642  lua_pushnumber( L, movie_status );
643  return 1;
644}
645
646static int luaCB_set_movie_status( lua_State* L )
647{
648  int to;
649  switch(luaL_checknumber( L, 1 )) {
650    case 1:
651      if (movie_status == 4) {
652        movie_status = 1;
653      }
654    break;
655    case 2:
656      if (movie_status == 1) {
657        movie_status = 4;
658      }
659    break;
660    case 3:
661      if (movie_status == 1 || movie_status == 4) {
662        movie_status = 5;
663      }
664    break;
665  }
666  return 0;
667}
668
669static int luaCB_get_drive_mode( lua_State* L )
670{
671  lua_pushnumber( L, shooting_get_drive_mode() );
672  return 1;
673}
674
675static int luaCB_get_focus_mode( lua_State* L )
676{
677  lua_pushnumber( L, shooting_get_prop(PROPCASE_FOCUS_MODE) );
678  return 1;
679}
680
681static int luaCB_get_flash_mode( lua_State* L )
682{
683  lua_pushnumber( L, shooting_get_prop(PROPCASE_FLASH_MODE) );
684  return 1;
685}
686
687static int luaCB_get_shooting( lua_State* L )
688{
689  lua_pushboolean( L, shooting_get_prop(PROPCASE_SHOOTING) );
690  return 1;
691}
692
693static int luaCB_get_flash_ready( lua_State* L )
694{
695  lua_pushboolean( L, shooting_get_prop(PROPCASE_IS_FLASH_READY) );
696  return 1;
697}
698
699static int luaCB_get_IS_mode( lua_State* L )
700{
701  lua_pushnumber( L, shooting_get_prop(PROPCASE_IS_MODE) );
702  return 1;
703}
704
705static int luaCB_get_orientation_sensor( lua_State* L )
706{
707  lua_pushnumber( L, shooting_get_prop(PROPCASE_ORIENTATION_SENSOR) );
708  return 1;
709}
710
711static int luaCB_get_zoom_steps( lua_State* L )
712{
713  lua_pushnumber( L, zoom_points );
714  return 1;
715}
716
717static int luaCB_get_nd_present( lua_State* L )
718{
719  int to;
720  #if !CAM_HAS_ND_FILTER
721  to = 0;
722  #endif
723  #if CAM_HAS_ND_FILTER && !CAM_HAS_IRIS_DIAPHRAGM
724  to = 1;
725  #endif
726  #if CAM_HAS_ND_FILTER && CAM_HAS_IRIS_DIAPHRAGM
727  to = 2;
728  #endif
729  lua_pushnumber( L, to );
730  return 1;
731}
732
733static int luaCB_get_propset( lua_State* L )
734{
735  lua_pushnumber( L, CAM_PROPSET );
736  return 1;
737}
738
739static int luaCB_get_ev( lua_State* L )
740{
741  lua_pushnumber( L, shooting_get_prop(PROPCASE_EV_CORRECTION_1) );
742  return 1;
743}
744
745static int luaCB_set_ev( lua_State* L )
746{
747  int to;
748  to = luaL_checknumber( L, 1 );
749  shooting_set_prop(PROPCASE_EV_CORRECTION_1, to);
750  shooting_set_prop(PROPCASE_EV_CORRECTION_2, to);
751  return 0;
752}
753
754static int luaCB_get_histo_range( lua_State* L )
755{
756  int from = (luaL_checknumber(L,1));
757  int to = (luaL_checknumber(L,2));
758  if (shot_histogram_isenabled()) lua_pushnumber( L, shot_histogram_get_range(from, to) );
759  else lua_pushnumber( L, -1 ); // TODO should probably return nil
760  return 1;
761}
762
763static int luaCB_shot_histo_enable( lua_State* L )
764{
765  shot_histogram_set(luaL_checknumber( L, 1 ));
766  return 0;
767}
768
769static int luaCB_play_sound( lua_State* L )
770{
771  play_sound(luaL_checknumber( L, 1 ));
772  return 0;
773}
774
775static int luaCB_get_temperature( lua_State* L )
776{
777  int which = (luaL_checknumber( L, 1 ));
778  int temp = -100; // do something insane if users passes bad value
779  switch (which)
780  {
781    case 0:
782      temp = get_optical_temp();
783      break;
784    case 1:
785      temp = get_ccd_temp();
786      break;
787    case 2:
788      temp = get_battery_temp();
789      break;
790  }
791  lua_pushnumber( L, temp );
792  return 1;
793}
794
795static int luaCB_get_time( lua_State* L )
796{
797  int r = -1;
798  unsigned long t2 = time(NULL);
799  static struct tm *ttm;
800  ttm = localtime(&t2);
801  const char *t = luaL_checkstring( L, 1 );
802  if (strncmp("s", t, 1)==0) r = ( L, ttm->tm_sec );
803  else if (strncmp("m", t, 1)==0) r = ( L, ttm->tm_min );
804  else if (strncmp("h", t, 1)==0) r = ( L, ttm->tm_hour );
805  else if (strncmp("D", t, 1)==0) r = ( L, ttm->tm_mday );
806  else if (strncmp("M", t, 1)==0) r = ( L, ttm->tm_mon+1 );
807  else if (strncmp("Y", t, 1)==0) r = ( L, 1900+ttm->tm_year );
808  lua_pushnumber( L, r );
809  return 1;
810}
811
812/*
813  val=peek(address[,size])
814  return the value found at address in memory, or nil if address or size is invalid
815  size is optional 1=byte 2=halfword 4=word. defaults is 4
816*/
817static int luaCB_peek( lua_State* L )
818{
819  unsigned addr = luaL_checknumber(L,1);
820  unsigned size = luaL_optnumber(L, 2, 4);
821  switch(size) {
822    case 1:
823      lua_pushnumber( L, *(unsigned char *)(addr) );
824    break;
825    case 2:
826      if (addr & 0x1) {
827        lua_pushnil(L);
828      }
829      else {
830        lua_pushnumber( L, *(unsigned short *)(addr) );
831      }
832    break;
833    case 4:
834      if (addr & 0x3) {
835        lua_pushnil(L);
836      }
837      else {
838        lua_pushnumber( L, *(unsigned *)(addr) );
839      }
840    break;
841    default:
842      lua_pushnil(L);
843
844  }
845  return 1;
846}
847
848/*
849  status=poke(address,value[,size])
850  writes value to address in memory
851  size is optional 1=byte 2=halfword 4=word. defaults is 4
852  returns true, or nil if address or size is invalid
853*/
854static int luaCB_poke( lua_State* L )
855{
856  unsigned addr = luaL_checknumber(L,1);
857  unsigned val = luaL_checknumber(L,2);
858  unsigned size = luaL_optnumber(L, 3, 4);
859  int status = 0;
860  switch(size) {
861    case 1:
862        *(unsigned char *)(addr) = (unsigned char)val;
863        status=1;
864    break;
865    case 2:
866      if (!(addr & 0x1)) {
867        *(unsigned short *)(addr) = (unsigned short)val;
868        status=1;
869      }
870    break;
871    case 4:
872      if (!(addr & 0x3)) {
873        *(unsigned *)(addr) = val;
874        status=1;
875      }
876    break;
877  }
878  if(status) {
879    lua_pushboolean(L,1);
880  }
881  else {
882    lua_pushnil(L);
883  }
884  return 1;
885}
886
887static int luaCB_bitand( lua_State* L )
888{
889  int v1 = (luaL_checknumber(L,1));
890  int v2 = (luaL_checknumber(L,2));
891  lua_pushnumber( L, v1 & v2 );
892  return 1;
893}
894
895static int luaCB_bitor( lua_State* L )
896{
897  int v1 = (luaL_checknumber(L,1));
898  int v2 = (luaL_checknumber(L,2));
899  lua_pushnumber( L, v1 | v2 );
900  return 1;
901}
902
903static int luaCB_bitxor( lua_State* L )
904{
905  int v1 = (luaL_checknumber(L,1));
906  int v2 = (luaL_checknumber(L,2));
907  lua_pushnumber( L, v1 ^ v2 );
908  return 1;
909}
910
911static int luaCB_bitshl( lua_State* L )
912{
913  int val = (luaL_checknumber(L,1));
914  unsigned shift = (luaL_checknumber(L,2));
915  lua_pushnumber( L, val << shift );
916  return 1;
917}
918
919static int luaCB_bitshri( lua_State* L )
920{
921  int val = (luaL_checknumber(L,1));
922  unsigned shift = (luaL_checknumber(L,2));
923  lua_pushnumber( L, val >> shift );
924  return 1;
925}
926
927static int luaCB_bitshru( lua_State* L )
928{
929  unsigned val = (luaL_checknumber(L,1));
930  unsigned shift = (luaL_checknumber(L,2));
931  lua_pushnumber( L, val >> shift );
932  return 1;
933}
934
935static int luaCB_bitnot( lua_State* L )
936{
937  unsigned val = (luaL_checknumber(L,1));
938  lua_pushnumber( L, ~val );
939  return 1;
940}
941
942static void set_string_field(lua_State* L, const char *key, const char *val)
943{
944  lua_pushstring(L, val);
945  lua_setfield(L, -2, key);
946}
947
948static int luaCB_get_buildinfo( lua_State* L )
949{
950  lua_createtable(L, 0, 8);
951  set_string_field( L,"platform", PLATFORM );
952  set_string_field( L,"platsub", PLATFORMSUB );
953  set_string_field( L,"version", HDK_VERSION );
954  set_string_field( L,"build_number", BUILD_NUMBER );
955  set_string_field( L,"build_date", __DATE__ );
956  set_string_field( L,"build_time", __TIME__ );
957#ifndef CAM_DRYOS
958  set_string_field( L,"os", "vxworks" );
959#else
960  set_string_field( L,"os", "dryos" );
961#endif
962  lua_pushnumber( L, PLATFORMID );
963  lua_setfield(L, -2, "platformid");
964  return 1;
965}
966
967static int luaCB_get_mode( lua_State* L )
968{
969  int m = mode_get();
970  lua_pushboolean( L, (m&MODE_MASK) != MODE_PLAY );
971  lua_pushboolean( L, MODE_IS_VIDEO(m) );
972  lua_pushnumber( L, m );
973  return 3;
974}
975
976// TODO sanity check file ?
977static int luaCB_set_raw_develop( lua_State* L )
978{
979  raw_prepare_develop(luaL_optstring( L, 1, NULL ));
980  return 0;
981}
982
983static int luaCB_raw_merge_start( lua_State* L )
984{
985  int op = luaL_checknumber(L,1);
986  if (op == RAW_OPERATION_SUM || op == RAW_OPERATION_AVERAGE) {
987    raw_merge_start(op);
988  }
989  else {
990    return luaL_argerror(L,1,"invalid raw merge op");
991  }
992  return 0;
993}
994
995// TODO sanity check file ?
996static int luaCB_raw_merge_add_file( lua_State* L )
997{
998  raw_merge_add_file(luaL_checkstring( L, 1 ));
999  return 0;
1000}
1001
1002static int luaCB_raw_merge_end( lua_State* L )
1003{
1004  raw_merge_end();
1005  return 0;
1006}
1007
1008// Enable/disable LCD back light (input argument 1/0)
1009static int luaCB_set_backlight( lua_State* L )
1010{
1011  int val = (luaL_checknumber(L,1));
1012
1013  if (val > 0) TurnOnBackLight();
1014  else TurnOffBackLight();
1015  return 0;
1016}
1017
1018// get the string or number passed in index and return it as an event id
1019static unsigned levent_id_from_lua_arg( lua_State* L, int index)
1020{
1021  unsigned event_id;
1022  if (lua_type(L, index) == LUA_TSTRING) {
1023    const char *ev_name = lua_tostring(L, index);
1024        event_id = levent_id_for_name(ev_name);
1025    if (event_id == 0) {
1026        return luaL_error( L, "bad event name '%s'", ev_name );
1027    }
1028  }
1029  // could check here if it is in the table, but even valid ones can crash
1030  // so we avoid searching the table if given a number
1031  else if (lua_type(L,index) == LUA_TNUMBER){
1032        event_id = lua_tonumber(L,index);
1033  }
1034  else {
1035    return luaL_error( L, "expected event name or id" );
1036  }
1037  return event_id;
1038}
1039
1040/*
1041  get a value where boolean or 0/!0 are accepted for on/off.
1042  normal lua toboolean will convert 0 to true, but ubasic and c users
1043  will expect 0 to be off
1044  intentional HACK: numbers greater than 1 are returned as is
1045*/
1046static unsigned on_off_value_from_lua_arg( lua_State* L, int index)
1047{
1048  if( lua_isboolean(L,index) ) {
1049        return lua_toboolean(L,index);
1050  }
1051  else {
1052        return luaL_checknumber(L,index);
1053  }
1054}
1055
1056/*
1057  return the index of an event, given it's name or event id
1058*/
1059static unsigned levent_index_from_id_lua_arg( lua_State* L, int index )
1060{
1061  if (lua_type(L, index) == LUA_TSTRING) {
1062        return levent_index_for_name(lua_tostring(L, index));
1063  }
1064  else if (lua_type(L,index) == LUA_TNUMBER){
1065        return levent_index_for_id(lua_tonumber(L,index));
1066  }
1067  else {
1068    return luaL_error( L, "expected string or number" );
1069  }
1070}
1071
1072/*
1073  name,id,param = get_levent_def(event)
1074  event is an event id (number) or name (string)
1075  returns nil if event is not found
1076*/
1077static int luaCB_get_levent_def( lua_State* L )
1078{
1079  unsigned event_index = levent_index_from_id_lua_arg(L,1);
1080  if (event_index == LEVENT_INVALID_INDEX) {
1081    lua_pushnil(L);
1082    return 1;
1083  }
1084  lua_pushstring(L, levent_table[event_index].name);
1085  lua_pushnumber(L, levent_table[event_index].id);
1086  lua_pushnumber(L, levent_table[event_index].param);
1087  return 3;
1088}
1089
1090/*
1091  index=get_levent_index(event)
1092  event is an event id (number) or name (string)
1093  returns index or nil if not found
1094*/
1095static int luaCB_get_levent_index( lua_State* L )
1096{
1097  unsigned event_index = levent_index_from_id_lua_arg(L,1);
1098  if (event_index == LEVENT_INVALID_INDEX) {
1099    lua_pushnil(L);
1100  }
1101  else {
1102    lua_pushnumber(L, event_index);
1103  }
1104  return 1;
1105}
1106
1107/*
1108  name,id,param = get_levent_def_by_index(event_index)
1109  event_index is number index into the event table
1110  returns nil if event is not found
1111*/
1112static int luaCB_get_levent_def_by_index( lua_State* L )
1113{
1114  unsigned i = luaL_checknumber(L,1);
1115  if(i >= levent_count()) {
1116        lua_pushnil(L);
1117    return 1;
1118  }
1119  lua_pushstring(L, levent_table[i].name);
1120  lua_pushnumber(L, levent_table[i].id);
1121  lua_pushnumber(L, levent_table[i].param);
1122  return 3;
1123}
1124
1125/*
1126  post_levent_*(event[,unk])
1127  post the event with PostLogicalEventToUI or PostLogicaEventForNotPowerType
1128  This sends the event. The difference between functions isn't clear.
1129  event is an event id (number) or name (string).
1130  unk is an optional number whose meaning is unknown, defaults to zero.
1131    Based on code, other values would probably be a pointer.
1132        This is NOT the 3rd item in the event table.
1133*/
1134static int luaCB_post_levent_to_ui( lua_State* L )
1135{
1136  unsigned event_id,arg;
1137
1138  event_id = levent_id_from_lua_arg(L,1);
1139  arg = luaL_optnumber(L, 2, 0);
1140  PostLogicalEventToUI(event_id,arg);
1141  return 0;
1142}
1143
1144static int luaCB_post_levent_for_npt( lua_State* L )
1145{
1146  unsigned event_id,arg;
1147
1148  event_id = levent_id_from_lua_arg(L,1);
1149  arg = luaL_optnumber(L, 2, 0);
1150  PostLogicalEventForNotPowerType(event_id,arg);
1151  return 0;
1152}
1153
1154/*
1155  set_levent_active(event,state)
1156  event is an event id (number) or name (string)
1157  state is a numeric or boolean state. true or non zero numbers turn on zero, false or nil turn off
1158  exact meaning is unknown, but it has something to do with the delivery of the specified event.
1159*/
1160static int luaCB_set_levent_active( lua_State* L )
1161{
1162  unsigned event_id;
1163  unsigned state;
1164
1165  event_id = levent_id_from_lua_arg(L,1);
1166  state = on_off_value_from_lua_arg(L,2);
1167  SetLogicalEventActive(event_id,state);
1168  return 0;
1169}
1170
1171/*
1172  set_levent_script_mode(state)
1173  state is numeric or boolean state. true or non zero numbers turn on zero, false or nil turn off
1174  exact meaning is unknown, but it has something to do with the behavior of events and/or SetLogicalEventActive.
1175*/
1176static int luaCB_set_levent_script_mode( lua_State* L )
1177{
1178  SetScriptMode(on_off_value_from_lua_arg(L,1));
1179  return 0;
1180}
1181
1182/*
1183  result=set_capture_mode_canon(value)
1184  where value is a valid PROPCASE_SHOOTING_MODE value for the current camera
1185  result is true if the camera is in rec mode
1186*/
1187static int luaCB_set_capture_mode_canon( lua_State* L )
1188{
1189  int modenum = luaL_checknumber(L,1);
1190  // if the value as negative, assume it is a mistakenly sign extended PROPCASE_SHOOTING_MODE value
1191  if(modenum < 0)
1192    modenum &= 0xFFFF;
1193  lua_pushboolean( L, shooting_set_mode_canon(modenum) );
1194  return 1;
1195}
1196
1197/*
1198 result=set_capture_mode(modenum)
1199 where modenum is a valid CHDK modemap value
1200 result is true if modenum is a valid modemap value, otherwise false
1201*/
1202static int luaCB_set_capture_mode( lua_State* L )
1203{
1204  int modenum = luaL_checknumber(L,1);
1205  lua_pushboolean( L, shooting_set_mode_chdk(modenum) );
1206  return 1;
1207}
1208
1209/*
1210 result=is_capture_mode_valid(modenum)
1211 where modenum is a valid CHDK modemap value
1212 result is true if modenum is a valid modemap value, otherwise false
1213*/
1214static int luaCB_is_capture_mode_valid( lua_State* L )
1215{
1216  int modenum = luaL_checknumber(L,1);
1217  lua_pushboolean( L, shooting_mode_chdk2canon(modenum) != -1 );
1218  return 1;
1219}
1220
1221/*
1222  set_record(state)
1223  if state is 0 (or false) the camera is set to play mode. If 1 or true, the camera is set to record mode.
1224  NOTE: this only begins the mode change. Script should wait until get_mode() reflects the change,
1225  before doing anything that requires the new mode. e.g.
1226  set_record(true)
1227  while not get_mode() do
1228        sleep(10)
1229  end
1230*/
1231static int luaCB_set_record( lua_State* L )
1232{
1233  if(on_off_value_from_lua_arg(L,1)) {
1234    levent_set_record();
1235  }
1236  else {
1237    levent_set_play();
1238  }
1239  return 0;
1240}
1241
1242// switch mode (0 = playback, 1 = record)
1243// only for when USB is connected
1244static int luaCB_switch_mode_usb( lua_State* L )
1245{
1246  int mode = luaL_checknumber(L,1);
1247
1248  if ( mode != 0 && mode != 1 )
1249  {
1250    return 0;
1251  }
1252
1253  return switch_mode_usb(mode);
1254}
1255
1256/*
1257pack the lua args into a buffer to pass to the native code calling functions
1258currently only handles strings/numbers
1259start is the stack index of the first arg
1260*/
1261#ifdef OPT_LUA_CALL_NATIVE
1262static int pack_native_args( lua_State* L, unsigned start, unsigned *argbuf)
1263{
1264  unsigned i;
1265  unsigned end = lua_gettop(L);
1266
1267  for(i = start; i <= end; i++,argbuf++) {
1268    if (lua_type(L, i) == LUA_TSTRING) {
1269        *argbuf=(unsigned)lua_tostring( L, i);
1270    }
1271    else if (lua_type(L, i) == LUA_TNUMBER) {
1272        *argbuf=lua_tonumber( L, i);
1273    }
1274    else {
1275      return 0;
1276    }
1277  }
1278  return 1;
1279}
1280
1281/*
1282Native function call interface. Can be used to call canon eventprocs or arbitrary
1283pointers.
1284
1285NOTE: this is preliminary, interface may change in later versions!
1286All arguments must be strings or numbers.
1287If the function expects to modify it's arguments via a pointer,
1288then you must provide a number that is a valid pointer.
1289
1290You can use the "AllocateMemory" eventproc to obtain buffers.
1291
1292If the function tries to write to a string passed from lua, Bad Things may happen.
1293
1294This is potentially dangerous, functions exist which can destroy the onboard firmware.
1295*/
1296
1297/*
1298result=call_func_ptr(ptr,...)
1299ptr: address of a valid ARM or Thumb function, which uses the normal C calling convention.
1300result: R0 value after the call returns
1301*/
1302static int luaCB_call_func_ptr( lua_State* L)
1303{
1304  unsigned *argbuf=NULL;
1305  unsigned i;
1306  unsigned n_args = lua_gettop(L)-1;
1307  void *fptr;
1308
1309  fptr=(void *)luaL_checknumber( L, 1 );
1310
1311  if (n_args) {
1312    argbuf=malloc(n_args * 4);
1313    if(!argbuf) {
1314      return luaL_error( L, "malloc fail" );
1315    }
1316    if(!pack_native_args(L, 2, argbuf)) {
1317      free(argbuf);
1318      return luaL_error( L, "expected string or number" );
1319    }
1320  }
1321 
1322  lua_pushnumber( L, call_func_ptr(fptr, argbuf, n_args) );
1323  free(argbuf);
1324  return 1;
1325}
1326
1327/*
1328Call an event procedure
1329
1330result=call_event_proc("EventprocName",...)
1331result is the value returned by ExecuteEventProcedure, which is -1 if the eventproc is not found,
1332or the eventproc return value (which could also be -1)
1333NOTE:
1334Many eventprocs are not registered by default, but can be loaded by calling another event proc
1335Some useful ones are
1336SystemEventInit
1337        includes AllocateMemory, FreeMemory, sprintf, memcpy, Fut functions, log ...
1338UI_RegistDebugEventProc
1339        includes capture mode functions, PTM_ functions and much more
1340RegisterProductTestEvent
1341        includes PT_ functions
1342
1343Others:
1344RegisterShootSeqEvent
1345RegisterNRTableEvent
1346*/
1347
1348// grab from lowlevel
1349extern unsigned _ExecuteEventProcedure(const char *name,...);
1350static int luaCB_call_event_proc( lua_State* L )
1351{
1352  const char *evpname;
1353  unsigned *argbuf;
1354  unsigned i;
1355  unsigned n_args = lua_gettop(L);
1356
1357  evpname=luaL_checkstring( L, 1 );
1358
1359  argbuf=malloc(n_args * 4);
1360  if (!argbuf) {
1361    return luaL_error( L, "malloc fail" );
1362  }
1363
1364  // event proc name is first arg
1365  *argbuf = (unsigned)evpname;
1366 
1367  if(!pack_native_args(L,2,argbuf+1)) {
1368    free(argbuf);
1369    return luaL_error( L, "expected string or number" );
1370  }
1371 
1372  lua_pushnumber( L, call_func_ptr(_ExecuteEventProcedure,argbuf,n_args) );
1373  free(argbuf);
1374  return 1;
1375}
1376
1377#endif // OPT_LUA_CALL_NATIVE
1378
1379/*
1380result = reboot(["filename"])
1381returns false on failure, does not return on success
1382see lib/armutil/reboot.c for details
1383*/
1384static int luaCB_reboot( lua_State* L )
1385{
1386        lua_pushboolean(L, reboot(luaL_optstring( L, 1, NULL )));
1387        return 1;
1388}
1389
1390void register_lua_funcs( lua_State* L )
1391{
1392#define FUNC( X )                       \
1393  lua_pushcfunction( L, luaCB_##X );    \
1394  lua_setglobal( L, #X )
1395
1396  FUNC(shoot);
1397  FUNC(sleep);
1398  FUNC(cls);
1399  FUNC(set_console_layout);
1400  FUNC(set_console_autoredraw);
1401  FUNC(console_redraw);
1402
1403  lua_pushlightuserdata( L, action_push_click );
1404  lua_pushcclosure( L, luaCB_keyfunc, 1 );
1405  lua_setglobal( L, "click" );
1406
1407  lua_pushlightuserdata( L, action_push_press );
1408  lua_pushcclosure( L, luaCB_keyfunc, 1 );
1409  lua_setglobal( L, "press" );
1410
1411  lua_pushlightuserdata( L, action_push_release );
1412  lua_pushcclosure( L, luaCB_keyfunc, 1 );
1413  lua_setglobal( L, "release" );
1414
1415  FUNC(get_av96);
1416  FUNC(get_av96);
1417  FUNC(get_bv96);
1418  FUNC(get_day_seconds);
1419  FUNC(get_disk_size);
1420  FUNC(get_dof);
1421  FUNC(get_far_limit);
1422  FUNC(get_free_disk_space);
1423  FUNC(get_focus);
1424  FUNC(get_hyp_dist);
1425  FUNC(get_iso_market);
1426  FUNC(get_iso_mode);
1427  FUNC(get_iso_real);
1428  FUNC(get_jpg_count);
1429  FUNC(get_near_limit);
1430  FUNC(get_prop);
1431  FUNC(get_raw_count);
1432  FUNC(get_raw_nr);
1433  FUNC(get_raw);
1434  FUNC(get_sv96);
1435  FUNC(get_tick_count);
1436  FUNC(get_tv96);
1437  FUNC(get_user_av_id);
1438  FUNC(get_user_av96);
1439  FUNC(get_user_tv_id);
1440  FUNC(get_user_tv96);
1441  FUNC(get_vbatt);
1442  FUNC(get_zoom);
1443  FUNC(get_exp_count);
1444  FUNC(get_flash_params_count);
1445  FUNC(get_parameter_data);
1446
1447  FUNC(set_av96_direct);
1448  FUNC(set_av96);
1449  FUNC(set_focus);
1450  FUNC(set_iso_mode);
1451  FUNC(set_iso_real);
1452  FUNC(set_led);
1453  FUNC(set_nd_filter);
1454  FUNC(set_prop);
1455  FUNC(set_raw_nr);
1456  FUNC(set_raw);
1457  FUNC(set_sv96);
1458  FUNC(set_tv96_direct);
1459  FUNC(set_tv96);
1460  FUNC(set_user_av_by_id_rel);
1461  FUNC(set_user_av_by_id);
1462  FUNC(set_user_av96);
1463  FUNC(set_user_tv_by_id_rel);
1464  FUNC(set_user_tv_by_id);
1465  FUNC(set_user_tv96);
1466  FUNC(set_zoom_speed);
1467  FUNC(set_zoom_rel);
1468  FUNC(set_zoom);
1469
1470  FUNC(wait_click);
1471  FUNC(is_pressed);
1472  FUNC(is_key);
1473#ifdef CAM_HAS_JOGDIAL
1474  FUNC(wheel_right);
1475  FUNC(wheel_left);
1476#endif
1477  FUNC(md_get_cell_diff);
1478  FUNC(md_detect_motion);
1479  FUNC(autostarted);
1480  FUNC(get_autostart);
1481  FUNC(set_autostart);
1482  FUNC(get_usb_power);
1483  FUNC(exit_alt);
1484  FUNC(shut_down);
1485  FUNC(print_screen);
1486
1487  FUNC(get_focus_mode);
1488  FUNC(get_propset);
1489  FUNC(get_zoom_steps);
1490  FUNC(get_drive_mode);
1491  FUNC(get_flash_mode);
1492  FUNC(get_shooting);
1493  FUNC(get_flash_ready);
1494  FUNC(get_IS_mode);
1495  FUNC(set_ev);
1496  FUNC(get_ev);
1497  FUNC(get_orientation_sensor);
1498  FUNC(get_nd_present);
1499  FUNC(get_movie_status);
1500  FUNC(set_movie_status);
1501 
1502  FUNC(get_histo_range);
1503  FUNC(shot_histo_enable);
1504  FUNC(play_sound);
1505  FUNC(get_temperature);
1506  FUNC(peek);
1507  FUNC(poke);
1508  FUNC(bitand);
1509  FUNC(bitor);
1510  FUNC(bitxor);
1511  FUNC(bitshl);
1512  FUNC(bitshri);
1513  FUNC(bitshru);
1514  FUNC(bitnot);
1515
1516  FUNC(get_time);
1517
1518  FUNC(get_buildinfo);
1519  FUNC(get_mode);
1520 
1521  FUNC(set_raw_develop);
1522  // NOTE these functions normally run in the spytask.
1523  // called from lua they will run from kbd task instead
1524  FUNC(raw_merge_start);
1525  FUNC(raw_merge_add_file);
1526  FUNC(raw_merge_end);
1527  FUNC(set_backlight);
1528   FUNC(set_aflock);
1529#ifdef OPT_CURVES
1530   FUNC(set_curve_state);
1531#endif
1532// get levent definition by name or id, nil if not found
1533   FUNC(get_levent_def);
1534// get levent definition by index, nil if out of range
1535   FUNC(get_levent_def_by_index);
1536// get levent index from name or ID
1537   FUNC(get_levent_index);
1538   FUNC(post_levent_to_ui);
1539   FUNC(post_levent_for_npt);
1540   FUNC(set_levent_active);
1541   FUNC(set_levent_script_mode);
1542
1543   FUNC(set_capture_mode);
1544   FUNC(set_capture_mode_canon);
1545   FUNC(is_capture_mode_valid);
1546
1547   FUNC(set_record);
1548
1549   FUNC(switch_mode_usb);
1550
1551#ifdef OPT_LUA_CALL_NATIVE
1552   FUNC(call_event_proc);
1553   FUNC(call_func_ptr);
1554#endif
1555   FUNC(reboot);
1556}
Note: See TracBrowser for help on using the repository browser.