source: branches/reyalp-ptp-live/core/script.c @ 1720

Revision 1720, 26.0 KB checked in by reyalp, 15 months ago (diff)

Merged revision(s) 1714-1719 from trunk

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