source: branches/release-1_0/core/script.c @ 1721

Revision 1721, 25.7 KB checked in by philmoz, 15 months ago (diff)

Minor changes:

  • Property svn:eol-style set to native
Line 
1#include "stdlib.h"
2#include "keyboard.h"
3#include "platform.h"
4#include "core.h"
5#include "gui.h"
6#include "gui_draw.h"
7#include "conf.h"
8#include "script.h"
9#include "console.h"
10#include "action_stack.h"
11#include "motion_detector.h"
12#include "shot_histogram.h"
13#include "lang.h"
14#include "gui_lang.h"
15#include "kbd.h"
16
17#ifdef OPT_LUA
18#include "lauxlib.h"
19#endif
20
21//-------------------------------------------------------------------
22
23const char *script_source_str=NULL; // ptr to content of script
24char cfg_name[100] = "\0";          // buffer to make string "DATAPATH/scriptname.cfg"
25char cfg_set_name[100] = "\0";      // buffer to make string "DATAPATH/scriptname_PARAMSET"
26
27static const char *lua_script_default =
28    "--[[\n"
29    "@title Default Script\n"
30    "]]\n"
31#if defined(VER_CHDK)
32    "chdk_def_lang=1\n"
33#else
34    "chdk_def_lang=2\n"
35#endif
36    "langs     = {}\n"
37    "langs[1]  = {[\"name\"]=\"ENGLISH\",  [\"font_cp\"]=0,  [\"hint\"]=\"CHDK language changed to english\"}\n"
38    "langs[2]  = {[\"name\"]=\"GERMAN\",   [\"font_cp\"]=2,  [\"hint\"]=\"CHDK Sprache auf deutsch geäert\"}\n"
39    "langs[13] = {[\"name\"]=\"RUSSIAN\",  [\"font_cp\"]=1,  [\"hint\"]=\"CHDK language changed to russian\"}\n"
40   
41    "function get_cam_language()\n"
42        "local l\n"
43        "if get_propset()==1 then\n"
44            "l=get_prop(196)/256\n"
45            "if l>7 then l=l+1 end\n"
46            "if l>22 then l=l+1 end\n"
47        "else\n"
48            "l=get_prop(61)/256\n"
49        "end\n"
50        "return l+1\n"
51    "end\n"
52   
53    "function get_chdk_language()\n"
54        "local l=0\n"
55        "local lf=get_config_value(64)\n"
56        "if lf==\"\" then\n"
57            "l=chdk_def_lang\n"
58        "else\n"
59            "for i,v in ipairs(langs) do\n"
60                "if string.find(lf, v[\"name\"]..\".LNG\")~=nil then\n"
61                    "l=i\n"
62                    "break\n"
63                "end\n"
64            "end\n"
65        "end\n"
66        "return l\n"
67    "end\n"
68   
69    "function file_exists(name)\n"
70         "local f=io.open(name,\"r\")\n"
71         "if f~=nil then io.close(f) return true else return false end\n"
72    "end\n"
73   
74   
75    "chdk_lang=get_chdk_language()\n"
76    "cam_lang=get_cam_language()\n"
77   
78    "if cam_lang~=chdk_lang then\n"
79        "if chdk_lang==0 or cam_lang==chdk_def_lang then\n"
80            "set_config_value(64,\"\")\n"
81            "set_config_value(65,langs[chdk_def_lang].font_cp)\n"
82            "print(langs[chdk_def_lang].hint)\n"
83        "elseif langs[cam_lang]~=nil then\n"
84            "if file_exists(\"A/CHDK/LANG/\"..langs[cam_lang].name..\".LNG\") then\n"
85                "set_config_value(64,\"A/CHDK/LANG/\"..langs[cam_lang].name..\".LNG\")\n"
86                "set_config_value(65,langs[cam_lang].font_cp)\n"
87                "print(langs[cam_lang].hint)\n"
88            "else\n"
89                "print(langs[cam_lang].name..\".LNG is missing\")\n"
90            "end\n"
91        "else\n"
92            "print(\"unknown language id (\"..cam_lang..\")\")\n"
93        "end\n"
94    "else\n"
95        "print(\";)\")\n"
96    "end\n";
97
98// ================ SCRIPT PARAMETERS ==========
99char script_title[36];                                      // Title of current script
100
101//
102// 1. Values of script parameters are stored in conf.script_vars
103// 2. Encoding scheme is: array[VAR-'a'] = value
104
105char script_params[SCRIPT_NUM_PARAMS][28];                  // Parameter title
106int script_param_order[SCRIPT_NUM_PARAMS];                  // Ordered as_in_script list of variables ( [idx] = id_of_var )
107                                                            // to display in same order in script
108static char script_params_update[SCRIPT_NUM_PARAMS];        // Flag is such parameter exist
109static int script_loaded_params[SCRIPT_NUM_PARAMS];         // Copy of original values of parameters
110                                                            // (detect are they changed or not)
111
112static long running_script_stack_name = -1;                 // ID of action_stack, which used to control script processing
113
114//-------------------------------------------------------------------
115static void process_title(const char *title) {
116    register const char *ptr = title;
117    register int i=0;
118
119    while (ptr[0]==' ' || ptr[0]=='\t') ++ptr; // whitespaces
120    while (i<(sizeof(script_title)-1) && ptr[i] && ptr[i]!='\r' && ptr[i]!='\n') {
121        script_title[i]=ptr[i];
122        ++i;
123    }
124    script_title[i]=0;
125}
126
127//-------------------------------------------------------------------
128// Process one entry "@param VAR TITLE"
129//      param = ptr right after descriptor (should point to var)
130//      update = 0 - get
131//               1 - check is such parameter exists in loaded script
132// RESULT: script_params[VAR] - parameter title
133//         script_params_update[VAR]
134// RETURN VALUE: 0 if err, 1..26 = id of var
135//-------------------------------------------------------------------
136static int process_param(const char *param, int update) {
137    register const char *ptr = param;
138    register int n, i=0;
139
140    while (ptr[0]==' ' || ptr[0]=='\t') ++ptr; // whitespaces
141    if (ptr[0] && (ptr[0]>='a' && ptr[0]<='a'+SCRIPT_NUM_PARAMS) && (ptr[1]==' ' || ptr[1]=='\t')) {
142        n=ptr[0]-'a';
143        ptr+=2;
144        while (ptr[0]==' ' || ptr[0]=='\t') ++ptr; // whitespaces
145        script_params_update[n] = 1;
146        while (i<(sizeof(script_params[0])-1) && ptr[i] && ptr[i]!='\r' && ptr[i]!='\n') {
147            if (update) {
148                if (script_params[n][i]!=ptr[i])
149                    { script_params_update[n] = 0; break; }
150            }
151            else
152                script_params[n][i]=ptr[i];
153            ++i;
154        }
155        if (!update) script_params[n][i]=0;
156        n++;
157    } else n=0; // ??? else produce error message   
158    return n; // n=1 if '@param a' was processed, n=2 for 'b' ... n=26 for 'z'. n=0 if failed.
159}
160
161//-------------------------------------------------------------------
162// Process one entry "@default VAR VALUE"
163//      param = ptr right after descriptor (should point to var)
164//      update = 0 - get
165//               1 - only if updated
166//-------------------------------------------------------------------
167static void process_default(const char *param, char update) {
168    register const char *ptr = param;
169    register int n;
170
171    while (ptr[0]==' ' || ptr[0]=='\t') ++ptr; // whitespaces
172    if (ptr[0] && (ptr[0]>='a' && ptr[0]<='a'+SCRIPT_NUM_PARAMS) && (ptr[1]==' ' || ptr[1]=='\t')) {
173        n=ptr[0]-'a';
174        ptr+=2;
175        if (!update || script_params_update[n])
176        {
177            conf.script_vars[n] = strtol(ptr, NULL, 0);
178            script_loaded_params[n] = conf.script_vars[n];
179        }
180    } // ??? else produce error message
181}
182
183//-------------------------------------------------------------------
184// PURPOSE: Parse script for @xxx
185// PARAMETERS:  fn - full path of script
186//              update_vars - 1 process "@defaults", 0 do not
187// RESULTS:  script_title
188//           script_params
189//           script_params_order
190//           script_loaded_params, conf.script_vars
191//-------------------------------------------------------------------
192static void script_scan(const char *fn, int update_vars) {
193    register const char *ptr = script_source_str;
194    register int i, j=0, n;
195    char *c;
196
197    c=strrchr(fn, '/');
198    strncpy(script_title, (c)?c+1:fn, sizeof(script_title));
199    script_title[sizeof(script_title)-1]=0;
200    for (i=0; i<SCRIPT_NUM_PARAMS; ++i) {
201        script_params[i][0]=0;
202        script_param_order[i]=0;
203    }
204
205    while (ptr[0]) {
206        while (ptr[0]==' ' || ptr[0]=='\t') ++ptr; // whitespaces
207        if (ptr[0]=='@') {
208            if (strncmp("@title", ptr, 6)==0) {
209                ptr+=6;
210                process_title(ptr);
211            } else if (strncmp("@param", ptr, 6)==0) {
212                ptr+=6;
213                n=process_param(ptr, 0); // n=1 if '@param a' was processed, n=2 for 'b' ... n=26 for 'z'. n=0 if failed.
214                if (n>0 && n<=SCRIPT_NUM_PARAMS) {
215                  script_param_order[j]=n;
216                  j++;
217                }
218            } else if (update_vars && strncmp("@default", ptr, 8)==0) {
219                ptr+=8;
220                process_default(ptr, 0);
221            }
222        }
223        while (ptr[0] && ptr[0]!='\n') ++ptr; // unless end of line
224        if (ptr[0]) ++ptr;
225    }
226
227    /*for (i=0; i<SCRIPT_NUM_PARAMS; ++i) {
228        if (script_params[i][0]) break;
229    }
230    if (i==SCRIPT_NUM_PARAMS) { // there was no @param in script
231        for (i=0; i<3; ++i) {
232            strcpy(script_params[i], "Var. ? value");
233            script_params[i][5]='a'+i;
234        }
235    }*/
236}
237
238//-------------------------------------------------------------------
239// PURPOSE:     Create filename of paramset
240// PARAMETERS:  fn - full path of script
241//              param_set - >=0 = num of paramset, <0 = made .cfg
242// RESULT:  name at cfg_set_name or cfg_name (depending on param_set)
243//-------------------------------------------------------------------
244void set_params_values_name(const char *fn, int param_set)
245{
246    int shift;
247    register char *ptr = (param_set >= 0 ? cfg_set_name : cfg_name);
248    const char *name;
249   
250    if (fn == NULL || fn[0] == 0) { ptr[0] = 0; return; }
251   
252    strncpy(ptr, SCRIPT_DATA_PATH, 100); ptr[99]=0;
253    shift = strlen(SCRIPT_DATA_PATH);
254    name = strrchr(fn, '/');
255    if (name) name++; else name=fn;
256    strncpy(ptr+shift, name, 100-shift); ptr[99]=0;
257    shift = strlen(ptr); if (shift >= 100) shift=99;
258
259    if (param_set >= 0)
260        sprintf(ptr+shift-4, "_%d\0", param_set);
261    else
262        strcpy(ptr+shift-3, "cfg\0");
263}
264
265//-------------------------------------------------------------------
266//-------------------------------------------------------------------
267// PURPOSE:     Load parameters from paramset for script
268// PARAMETERS:  fn - full path of script
269//              update_vars - if true load parameters,
270//                                  otherwise do not
271//              read_param_set - if true load required paramset to conf.script_param_set
272//                                  from .cfg, otherwise use paramset 0
273// NOTES:       touched conf.script_param_set
274//-------------------------------------------------------------------
275int load_params_values(const char *fn, int update_vars, int read_param_set)
276{
277    int i, fd=-1, rcnt;
278    register const char *ptr;   
279    struct stat st;
280    char *buf;
281   
282    if (fn == NULL || fn[0] == 0) return 0;
283    if (read_param_set)
284    {
285        set_params_values_name(fn, -1);
286        // find param set
287        fd = open(cfg_name, O_RDONLY, 0777);
288        if (fd >= 0)
289        {
290            buf=umalloc(16);
291            rcnt = read(fd, buf, 15);
292            buf[rcnt] = 0;
293            close(fd);
294            conf.script_param_set = strtol(buf, NULL, 0);
295            ufree(buf);
296        } else conf.script_param_set = 0;
297    }
298    set_params_values_name(fn, conf.script_param_set);
299    if (!update_vars) return 0;
300   
301    // open and read file
302    if (stat(cfg_set_name,&st) != 0)
303        return 0;
304    buf=umalloc(st.st_size+1);
305    if(!buf)
306        return 0;
307    fd = open(cfg_set_name, O_RDONLY, 0777);
308    if (fd < 0) {
309        ufree(buf);
310        return 0;
311    }
312    rcnt = read(fd, buf, st.st_size);
313    buf[rcnt] = 0;
314    close(fd);
315
316    for(i = 0; i < SCRIPT_NUM_PARAMS; ++i) script_params_update[i]=0;
317    ptr = buf;
318
319    while (ptr[0])
320    {
321        while (ptr[0]==' ' || ptr[0]=='\t') ++ptr; // whitespaces
322        if (ptr[0]=='@')
323        {
324            if (strncmp("@param", ptr, 6) == 0)
325            {
326                ptr+=6;
327                process_param(ptr, 1);
328            } else if (strncmp("@default", ptr, 8)==0) {
329                ptr+=8;
330                process_default(ptr, 1);
331            }
332        }
333        while (ptr[0] && ptr[0]!='\n') ++ptr; // unless end of line
334        if (ptr[0]) ++ptr;
335    }
336    ufree(buf);
337    return 1;
338}
339
340//-------------------------------------------------------------------
341// PURPOSE:     Save parameters to paramset for script
342//                  Use: conf.script_file, conf.script_param_set
343// PARAMETERS:  unconditional - if 0 then save only if values are changed
344//                                  (script_loaded_params[i] != conf.script_vars[i])
345//                              otherwise mean "save always"
346//
347// NOTE:    1. create SCRIPT_DATA_PATH/scriptname.cfg
348//                      which store # of last saved paramset
349//          2. create SCRIPT_DATA_PATH/scriptname_PARAMSETNUM
350//                      which contain @param/@defaults pair with values
351//-------------------------------------------------------------------
352void save_params_values(int unconditional)
353{
354    int i, n, fd, changed=0;
355    char *buf,*p;
356    for(i = 0; i < SCRIPT_NUM_PARAMS; i++)
357    {
358        if (script_loaded_params[i] != conf.script_vars[i]) changed++;
359        script_loaded_params[i] = conf.script_vars[i];
360    }
361    if (!unconditional && !changed) return;
362
363    if (cfg_name[0] == 0) set_params_values_name(conf.script_file, -1);
364    fd = open(cfg_name, O_WRONLY|O_CREAT, 0777);
365    if (fd >= 0)
366    {
367        char s[20];
368        sprintf(s, " %d\n", conf.script_param_set);
369        write(fd, s, strlen(s));
370        close(fd);
371    }
372   
373    // open and read file
374    set_params_values_name(conf.script_file, conf.script_param_set);
375
376    // max possible params * (param description + some extra for @default etc)
377    buf=umalloc(SCRIPT_NUM_PARAMS*(28 + 20));
378    if(!buf)
379        return;
380
381    fd = open(cfg_set_name, O_WRONLY|O_CREAT, 0777);
382    if (fd < 0) {
383        ufree(buf);
384        return;
385    }
386    buf[0] = 0;
387    p=buf;
388    for(n = 0; n < SCRIPT_NUM_PARAMS; ++n)
389    {
390        if (script_params[n][0] != 0)
391        {
392            p+=sprintf(p,"@param %c %s\n@default %c %d\n",'a'+n,script_params[n],'a'+n,conf.script_vars[n]);
393        }
394    }
395    write(fd, buf, strlen(buf));
396    close(fd);
397    ufree(buf);
398}
399
400
401
402//-------------------------------------------------------------------
403// PURPOSE: Load script to memory ( and free malloc previous if exists)
404// PARAMETERS:  fn - full path of script
405//              saved_params -  0= reset all parameters to defaults
406//                              1= load last stored paramset, if script was switched
407//                              2= load paramset #0
408//
409// RESULTS: conf.script_file
410//          script_source_str - loaded script file content
411//
412// NOTES:  1. Try to load fn, if fn or file is empty - SCRIPT_DEFAULT_FILENAME,
413//                if default not exists - use builtin
414//         2. Update conf.script_file, title, submenu
415//-------------------------------------------------------------------
416void script_load(const char *fn, int saved_params) {
417    int i, update_vars;
418    FILE *fd = NULL;
419    struct stat st;
420   
421//    save_params_values(0);
422
423    if(script_source_str && script_source_str != lua_script_default)
424        free((void *)script_source_str);
425
426    script_source_str = lua_script_default;
427    update_vars = (strcmp(fn, conf.script_file) != 0) || !saved_params || (saved_params == 2);  // update if new file
428
429    if (!fn[0]) { // load internal script
430        if (!conf.script_file[0]) { // internal script was used last time
431            fd = fopen(SCRIPT_DEFAULT_FILENAME, "rb");
432            if (fd) {
433                fn = SCRIPT_DEFAULT_FILENAME;
434                update_vars = 1;
435            }
436        }
437    } else {
438        fd = fopen(fn, "rb");
439        if (!fd) {
440            conf.script_file[0]=0;
441            update_vars = 1;
442        }
443    }
444
445    // zero size = default script
446    if(stat(fn,&st) != 0 || st.st_size == 0) {
447        conf.script_file[0]=0;
448        update_vars = 1;
449        if(fd) {
450            fclose(fd);
451            fd=0;
452        }
453    }
454
455
456    if (fd){
457        int rcnt;
458        char *buf;
459
460        buf = malloc(st.st_size+2);
461        if(!buf) {
462            fclose(fd);
463            return;
464        }
465
466        // TODO we could process the script here to reduce size
467        // or compile for lua
468        rcnt = fread(buf, 1, st.st_size,fd);
469        if (rcnt > 0){
470            buf[rcnt] = '\n';
471            buf[rcnt+1] = 0;
472            script_source_str = buf;
473            strcpy(conf.script_file, fn);
474        }
475        else {
476            free(buf);
477        }
478        fclose(fd);
479    }
480
481    if (update_vars) {
482        for (i=0; i<SCRIPT_NUM_PARAMS; ++i) {
483            conf.script_vars[i] = 0;
484            script_loaded_params[i] = 0;
485        }
486    }
487    script_scan(conf.script_file, update_vars);
488    if (saved_params)
489        load_params_values(conf.script_file, update_vars, (saved_params!=2));
490    gui_update_script_submenu();
491}
492
493//-------------------------------------------------------------------
494static int  print_screen_p;             // print_screen predicate: 0-off 1-on.
495static int  print_screen_d = -1;        // print_screen file descriptor.
496char print_screen_file[25];
497
498void script_print_screen_init()
499{
500    print_screen_p = 0;
501    if (print_screen_d >= 0) {
502        close(print_screen_d);
503        print_screen_d = -1;
504    }
505}
506
507void script_print_screen_end()
508{
509    if (print_screen_d >= 0) {
510        close(print_screen_d);
511        print_screen_d = -1;
512        print_screen_p = 0;
513    }
514}
515
516void script_print_screen_statement(int val)
517{
518  // Negative values for 'val' parameter will append to log file,
519  // positive values will truncate the log file
520  int flag_trunc = O_TRUNC;
521
522  print_screen_p = val;
523  if (val) {
524    if (print_screen_d>=0) close(print_screen_d);
525    if (val<0) {
526       flag_trunc = 0;
527       val = -val;
528    }
529    while (val > 9999) val -= 10000;
530    sprintf(print_screen_file, "A/CHDK/LOGS/LOG_%04d.TXT", val);
531    print_screen_d = open(print_screen_file, O_WRONLY|O_CREAT|flag_trunc, 0777);
532    if (print_screen_d>=0) lseek(print_screen_d,0,SEEK_END);
533  }
534}
535
536void script_console_add_line(const char *str)
537{
538    console_add_line(str);
539
540    if (print_screen_p && (print_screen_d >= 0)) {
541        char nl = '\n';
542        // TODO this should be uncached memory
543        write(print_screen_d, str, strlen(str) );
544        write(print_screen_d, &nl, 1);
545    }
546}
547
548static int is_lua( const char* script_file)
549{
550  int len;
551  char const* s;
552 
553  s = script_file;
554  len = strlen( s );
555  return !s[0] || (len >= 4 && ( s[len-1] == 'a' || s[len-1] == 'A' )
556    && ( s[len-2] == 'u' || s[len-2] == 'U' )
557    && ( s[len-3] == 'l' || s[len-3] == 'L' )
558    && s[len-4] == '.');
559}
560
561void script_wait_and_end(void)
562{
563    script_console_add_line("PRESS SHUTTER TO CLOSE");
564
565    // We're not running any more, but we have scheduled stuff that
566    // needs to finish. So keep the script marked as running, but don't
567    // call any more scripting functions.
568    state_kbd_script_run = 3;
569}
570
571static void process_script()
572{   // Note: This function is called from an action stack for AS_SCRIPT_RUN.
573
574    if (state_kbd_script_run != 3) {
575#ifdef OPT_LUA
576        if( L ) {
577            lua_script_run();
578        } else
579#endif
580        {
581#ifdef OPT_UBASIC
582            ubasic_run();
583            if (ubasic_finished()) {
584                script_console_add_line(lang_str(LANG_CONSOLE_TEXT_FINISHED));
585                action_pop();
586                script_end();
587            }   
588#endif
589        }
590    }
591}
592
593static int script_action_stack(long p)
594{
595    // process stack operations
596    switch (p) {
597        case AS_SCRIPT_RUN:
598            if (state_kbd_script_run)
599                process_script();
600            else
601                action_pop();
602            break;
603        case AS_FILE_BROWSER:
604            // state_kbd_script_run is set to 0 when the file browser is started from a Lua script
605            // it is reset back to 1 when the file browser exits and control is returned back to
606            // the script
607            if (state_kbd_script_run)
608            {
609                action_pop();
610#ifdef OPT_LUA
611                if (L)
612                {
613                    // Last selected file is returned by this function in gui_fselect.c
614                    extern char* gui_fselect_result();
615                    // Send file name back to script caller
616                    lua_pushstring( Lt, gui_fselect_result() );
617                }
618#endif
619            }
620            break;
621        case AS_MOTION_DETECTOR:
622            if(md_detect_motion()==0)
623            {
624                action_pop();
625#ifdef OPT_LUA
626                if (L)
627                {
628                       // We need to recover the motion detector's
629                       // result and push
630                       // it onto the thread's stack.
631                       lua_pushnumber( Lt, md_get_result() );
632                } else
633#endif
634                {
635#ifdef OPT_UBASIC
636                    ubasic_set_md_ret(md_get_result());
637#endif
638                }
639            }
640            break;
641#if defined(OPT_LUA) && defined(CAM_CHDK_PTP)
642        case AS_SCRIPT_READ_USB_MSG:
643            if(L) { // only lua supported for now
644                ptp_script_msg *msg = ptp_script_read_msg();
645                if(action_process_delay(2) || msg) {
646                    if(msg) {
647                        lua_pushlstring( Lt,msg->data,msg->size);
648                    } else {
649                        lua_pushnil(Lt);
650                    }
651                    action_clear_delay();
652                    action_pop();
653                    action_pop();
654                }
655            }
656            break;
657        case AS_SCRIPT_WRITE_USB_MSG:
658            if(L) { // only lua supported for now
659                ptp_script_msg *msg = (ptp_script_msg *)action_get_prev(2);
660                int r = ptp_script_write_msg(msg);
661                if(action_process_delay(3) || r) {
662                    action_clear_delay();
663                    action_pop();
664                    action_pop();
665                    action_pop();
666                    lua_pushboolean(Lt,r);
667                }
668            }
669            break;
670#endif
671        default:
672            if (!action_stack_standard(p) && !state_kbd_script_run)
673            {
674                /*finished();*/
675                action_pop();
676                script_end();
677            }
678            break;
679    }
680   
681    return 1;
682}
683
684long script_stack_start()
685{
686    running_script_stack_name = action_stack_create(&script_action_stack, AS_SCRIPT_RUN);
687    return running_script_stack_name;
688}
689
690int script_is_running()
691{
692    return !action_stack_is_finished(running_script_stack_name);
693}
694
695void script_end()
696{
697    script_print_screen_end();
698#ifdef OPT_LUA
699    if( L ) {
700      lua_script_reset();
701    } else
702#endif
703    {
704#ifdef OPT_UBASIC
705      ubasic_end();
706#endif
707    }
708        md_close_motion_detector();
709        shot_histogram_set(0);
710    kbd_key_release_all();
711    state_kbd_script_run = 0;
712
713    conf_update_prevent_shutdown();
714
715    vid_bitmap_refresh();
716}
717
718long script_start_gui( int autostart )
719{
720    int i;
721
722    shot_histogram_set(0);
723    if (autostart)
724        auto_started = 1;
725    else
726        auto_started = 0;
727
728    kbd_last_clicked = 0;
729
730    /*if (!autostart)*/ kbd_key_release_all();
731
732    console_clear();
733    script_print_screen_init();
734
735    if (conf.script_param_save) {
736        save_params_values(0);
737    }
738    if( autostart )
739        script_console_add_line("***Autostart***");
740    else
741        script_console_add_line(lang_str(LANG_CONSOLE_TEXT_STARTED));
742
743    if( is_lua( conf.script_file ) ) {
744#ifdef OPT_LUA
745        if( !lua_script_start(script_source_str,0) ) {
746            return -1;
747        }
748        for (i=0; i<SCRIPT_NUM_PARAMS; ++i) {
749            if( script_params[i][0] ) {
750                char var = 'a'+i;
751                lua_pushlstring( L, &var, 1 );
752                lua_pushnumber( L, conf.script_vars[i] );
753                lua_settable( L, LUA_GLOBALSINDEX );
754            }
755        }
756#else
757        char msg[64];
758        sprintf(msg,lang_str(LANG_CONSOLE_SCRIPT_DISABLED_IN_BUILD),"Lua");
759        console_add_line(msg);
760        return -1;
761#endif
762    } else
763    { // ubasic
764#ifdef OPT_UBASIC
765        ubasic_init(script_source_str);
766
767        for (i=0; i<SCRIPT_NUM_PARAMS; ++i) {
768            ubasic_set_variable(i, conf.script_vars[i]);
769        }
770#else
771        char msg[64];
772        sprintf(msg,lang_str(LANG_CONSOLE_SCRIPT_DISABLED_IN_BUILD),"UBASIC");
773        console_add_line(msg);
774        return -1;
775#endif
776    }
777
778    state_kbd_script_run = 1;
779
780    conf_update_prevent_shutdown();
781
782    return script_stack_start();
783}
784
785#if defined(OPT_LUA) && defined(CAM_CHDK_PTP)
786long script_start_ptp( char *script )
787{
788  if (!lua_script_start(script,1)) return -1;
789  state_kbd_script_run = 1;
790  kbd_set_block(1);
791  auto_started = 0;
792  return script_stack_start();
793}
794#endif
795
796int script_key_is_pressed(int k)
797{
798    if (k==0xFF)
799        return get_usb_power(1);
800    if (k > 0)
801        return kbd_is_key_pressed(k);
802    return 0;
803}
804
805int script_key_is_clicked(int k)
806{
807    if (k==0xFF)
808        return get_usb_power(1);
809    if (k > 0)
810        return (kbd_last_clicked == k);
811    return 0;
812}
813
814static const struct Keynames {
815    int keyid;
816    char *keyname;
817} keynames[] = {
818    { KEY_UP,           "up"         },
819    { KEY_DOWN,         "down"       },
820    { KEY_LEFT,         "left"       },
821    { KEY_RIGHT,        "right"      },
822    { KEY_SET,          "set"        },
823    { KEY_SHOOT_HALF,   "shoot_half" },
824    { KEY_SHOOT_FULL,   "shoot_full" },
825    { KEY_SHOOT_FULL_ONLY,   "shoot_full_only" },
826    { KEY_ZOOM_IN,      "zoom_in"    },
827    { KEY_ZOOM_OUT,     "zoom_out"   },
828    { KEY_MENU,         "menu"       },
829    { KEY_DISPLAY,      "display"    },
830    { KEY_PRINT,        "print"      },
831    { KEY_ERASE,        "erase"      },
832    { KEY_ISO,          "iso"        },
833    { KEY_FLASH,        "flash"      },
834    { KEY_MF,           "mf"         },
835    { KEY_MACRO,        "macro"      },
836    { KEY_VIDEO,        "video"      },
837    { KEY_TIMER,        "timer"      },
838    { KEY_EXPO_CORR,    "expo_corr"  },
839    { KEY_MICROPHONE,   "fe"         },
840    { KEY_ZOOM_ASSIST,  "zoom_assist"},
841    { KEY_AE_LOCK,      "ae_lock"    },
842    { KEY_METERING,     "metering_mode"},
843    { 0xFF,             "remote"     },
844    { 0xFFFF,           "no_key"     },
845};
846
847int script_keyid_by_name (const char *n)
848{
849    int i;
850    for (i=0;i<sizeof(keynames)/sizeof(keynames[0]);i++)
851    if (strcmp(keynames[i].keyname,n) == 0)
852        return keynames[i].keyid;
853    return 0;
854}
Note: See TracBrowser for help on using the repository browser.