root/core/script.c

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

DEFINITIONS

This source file includes following definitions.
  1. script_print_screen_init
  2. script_print_screen_end
  3. script_print_screen_statement
  4. script_console_add_line
  5. script_console_add_error
  6. action_stack_AS_SCRIPT_RUN
  7. script_stack_start
  8. script_is_running
  9. script_set_terminate_key
  10. script_get_alt_text
  11. script_check_terminate
  12. gui_script_kbd_process
  13. gui_script_draw
  14. script_wait_terminate
  15. script_end
  16. script_start_gui

   1 #include "camera_info.h"
   2 #include "keyboard.h"
   3 #include "modes.h"
   4 #include "viewport.h"
   5 #include "conf.h"
   6 #include "script.h"
   7 #include "console.h"
   8 #include "action_stack.h"
   9 #include "shot_histogram.h"
  10 #include "lang.h"
  11 #include "gui.h"
  12 #include "gui_lang.h"
  13 #include "ptp.h"
  14 #include "clock.h"
  15 #include "usb_remote.h"
  16 #include "script_api.h"
  17 #include "motion_detector.h"
  18 
  19 //-------------------------------------------------------------------
  20 
  21 static AS_ID running_script_stack_name = 0;     // ID of action_stack, which used to control script processing
  22 
  23 //-------------------------------------------------------------------
  24 // script terminate key, may be set from script
  25 static int script_terminate_key = KEY_SHOOT_FULL;
  26 static char script_terminate_key_name[20]; // TODO this is only here because there's no easy way to map key number back to name
  27 
  28 static int script_terminate_request = 0; // script abort requested from another task (ptp)
  29 
  30 // Forward references
  31 void script_end();
  32 
  33 //=======================================================
  34 //                 SCRIPT CONSOLE FUNCTIONS
  35 //=======================================================
  36 
  37 //-------------------------------------------------------------------
  38 static int  print_screen_p;             // print_screen predicate: 0=off, else is log file number. negative=append, postive=overwrite 
  39 static int  print_screen_d = -1;        // print_screen file descriptor.
  40 char print_screen_file[25];
  41 
  42 static void script_print_screen_init()
  43 {
  44     print_screen_p = 0;
  45     if (print_screen_d >= 0) {
  46         close(print_screen_d);
  47         print_screen_d = -1;
  48     }
  49 }
  50 
  51 static void script_print_screen_end()
  52 {
  53     if (print_screen_d >= 0) {
  54         close(print_screen_d);
  55         print_screen_d = -1;
  56         print_screen_p = 0;
  57     }
  58 }
  59 
  60 void script_print_screen_statement(int val)
  61 {
  62   // Negative values for 'val' parameter will append to log file,
  63   // positive values will truncate the log file
  64   int flag_trunc = O_TRUNC;
  65 
  66   print_screen_p = val;
  67   if (val) {
  68     if (print_screen_d>=0) close(print_screen_d);
  69     if (val<0) {
  70        flag_trunc = 0;
  71        val = -val;
  72     }
  73     while (val > 9999) val -= 10000;
  74     sprintf(print_screen_file, "A/CHDK/LOGS/LOG_%04d.TXT", val);
  75     print_screen_d = open(print_screen_file, O_WRONLY|O_CREAT|flag_trunc, 0777);
  76     if (print_screen_d>=0) lseek(print_screen_d,0,SEEK_END);
  77   }
  78   else script_print_screen_end() ;
  79 }
  80 
  81 void script_console_add_line(long str_id)
  82 {
  83     const char* str = lang_str(str_id);
  84     console_add_line(str);
  85 
  86     if (print_screen_p && (print_screen_d >= 0)) {
  87         char nl = '\n';
  88         // TODO this should be uncached memory
  89         write(print_screen_d, str, strlen(str) );
  90         write(print_screen_d, &nl, 1);
  91     }
  92 }
  93 
  94 void script_console_add_error(long str_id)
  95 {
  96     console_set_autoredraw(1);          // Force console display on
  97     script_console_add_line(str_id);
  98 }
  99 
 100 //=======================================================
 101 //                 SCRIPT FUNCTIONS
 102 //=======================================================
 103 
 104 // Stack process function for running current script
 105 static int action_stack_AS_SCRIPT_RUN()
 106 {
 107     if (camera_info.state.state_kbd_script_run)
 108     {
 109         int rv = libscriptapi->script_run();
 110         if (rv != SCRIPT_RUN_RUNNING)
 111         {
 112             // Script language is responsible for displaying 'Finished' or error
 113             // messages so all we need to do is shutdown the script engine
 114             script_end();
 115             return 1;
 116         }
 117     }
 118     else
 119     {
 120         action_pop_func(0);
 121         return 1;
 122     }
 123     return 0;
 124 }
 125 
 126 long script_stack_start()
 127 {
 128     script_terminate_request = 0; // ensure any previous terminate request is cleared
 129 
 130     camera_info.state.state_kbd_script_run = SCRIPT_STATE_RAN;
 131     running_script_stack_name = action_stack_create(&action_stack_AS_SCRIPT_RUN);
 132     return running_script_stack_name;
 133 }
 134 
 135 int script_is_running()
 136 {
 137     return !action_stack_is_finished(running_script_stack_name);
 138 }
 139 
 140 //-------------------------------------------------------------------
 141 void script_set_terminate_key(int key, const char *keyname)
 142 {
 143     script_terminate_key = key;
 144     strncpy(script_terminate_key_name,keyname,sizeof(script_terminate_key_name));
 145     script_terminate_key_name[sizeof(script_terminate_key_name)-1] = 0;
 146 }
 147 
 148 void script_get_alt_text(char *buf)
 149 {
 150     if ((script_terminate_key != KEY_SHOOT_FULL) && camera_info.state.state_kbd_script_run)
 151     {
 152         sprintf(buf,"<EXIT=%s>",script_terminate_key_name);
 153     }
 154     else
 155     {
 156         strcpy(buf,"<ALT>");
 157     }
 158 }
 159 
 160 // check if an external terminate request has been set
 161 // this is called directly from kbd_process, so it will be processed whether or not in alt mode
 162 void script_check_terminate(void)
 163 {
 164     if(camera_info.state.state_kbd_script_run && script_terminate_request) {
 165         script_console_add_error(LANG_CONSOLE_TEXT_TERMINATED);
 166         script_end();
 167     }
 168 }
 169 
 170 // Main button processing for CHDK Script mode
 171 
 172 static int gui_script_kbd_process()
 173 {
 174     // Stop a script if the shutter button pressed in Script mode
 175     if (kbd_is_key_clicked(script_terminate_key))
 176     {
 177         script_console_add_error(LANG_CONSOLE_TEXT_INTERRUPTED);
 178         if (camera_info.state.state_kbd_script_run == SCRIPT_STATE_INTERRUPTED)
 179             script_end();
 180         else
 181         {
 182             camera_info.state.state_kbd_script_run = SCRIPT_STATE_INTERRUPTED;
 183             if (libscriptapi->run_restore() == 0)
 184                 script_end();
 185         }
 186     }
 187 
 188     return 0;
 189 }
 190 
 191 //-------------------------------------------------------------------
 192 void gui_script_draw()
 193 {
 194     extern void gui_chdk_draw();
 195     gui_chdk_draw();
 196 
 197     if (camera_info.state.mode_rec || camera_info.state.mode_play)
 198     {
 199         static int show_md_grid=0;
 200         if (camera_info.state.state_kbd_script_run) show_md_grid=5;
 201         if (show_md_grid)
 202         {
 203             --show_md_grid;
 204             libmotiondetect->md_draw_grid();
 205         }
 206     }
 207 }
 208 
 209 // GUI handler for Script mode
 210 gui_handler scriptGuiHandler = { GUI_MODE_SCRIPT, gui_script_draw, gui_script_kbd_process, 0, 0, 0 };
 211 
 212 static gui_handler *old_gui_handler = 0;
 213 
 214 //-------------------------------------------------------------------
 215 // request the script end from an external task
 216 // NOTE this must NOT be called from kbd_task
 217 void script_wait_terminate(void)
 218 {
 219     if(camera_info.state.state_kbd_script_run == SCRIPT_STATE_INACTIVE) {
 220         return;
 221     }
 222     script_terminate_request = 1;
 223     // TODO timeout?
 224     while(camera_info.state.state_kbd_script_run != SCRIPT_STATE_INACTIVE) {
 225         msleep(10);
 226     }
 227 }
 228 
 229 // Terminate a script, either because of error, or the script finished
 230 void script_end()
 231 {
 232     // Tell other code that script has ended
 233     camera_info.state.state_kbd_script_run = SCRIPT_STATE_INACTIVE;
 234     camera_info.state.osd_title_line = 1 ;
 235 
 236     // reset the script terminate key
 237     script_terminate_key = KEY_SHOOT_FULL ;
 238 
 239     script_print_screen_end();
 240     console_set_autoredraw(1);          // Force console display on in case script turned it off
 241 
 242     // Restore old handler - prevent calling MD draw after module unloaded
 243     if (old_gui_handler)
 244     {
 245         extern gui_handler defaultGuiHandler; 
 246         // if script switched in or out of alt, try to preserve
 247         // TODO is there ever a situation where it should be something other than default or alt?
 248         if(old_gui_handler == &altGuiHandler && camera_info.state.gui_mode_none) {
 249             gui_set_mode(&defaultGuiHandler);
 250         } else if(old_gui_handler == &defaultGuiHandler && camera_info.state.gui_mode_alt) {
 251             gui_set_mode(&altGuiHandler);
 252         } else {
 253             gui_set_mode(old_gui_handler);
 254         }
 255         old_gui_handler = 0;
 256     }
 257 
 258     // Reset script language module
 259     libscriptapi->script_reset();
 260 
 261     // Kill off the action_stack for the script, since we've just reset the script
 262     // language and unloaded the MD module, we don't need to let the stack empty
 263     // itself.
 264     action_stack_kill(running_script_stack_name);
 265     running_script_stack_name = -1;
 266 
 267         libshothisto->shot_histogram_set(0);
 268     kbd_key_release_all();
 269 
 270     conf_setAutosave(1);    // Turn on autosave of config file in conf_setValue in case script turned it off
 271     conf_update_prevent_shutdown();
 272 }
 273 
 274 long script_start_gui( int autostart )
 275 {
 276     if (conf.script_file[0] == 0) return 0;
 277 
 278     libshothisto->shot_histogram_set(0);
 279     camera_info.state.auto_started = autostart;
 280 
 281     // Kill high speed USB timer if running
 282     stop_usb_HPtimer();
 283 
 284     // Keyboard init
 285     camera_info.state.kbd_last_clicked = 0;
 286     camera_info.state.kbd_last_checked_time = get_tick_count();
 287     kbd_key_release_all();
 288 
 289     // Close old console, will be re-opened when first line added
 290     console_close();
 291     script_print_screen_init();
 292 
 293     save_params_values(0);
 294 
 295     script_console_add_line((autostart)?LANG_CONSOLE_TEXT_AUTOSTARTED:LANG_CONSOLE_TEXT_STARTED);
 296 
 297     module_set_script_lang(conf.script_file);
 298     if ( !libscriptapi->script_start_file(conf.script_file) )
 299     {
 300         return -1;
 301     }
 302 
 303     sc_param *p = script_params;
 304     while (p)
 305     {
 306         if (p->name != 0)
 307         {
 308             libscriptapi->set_variable(p->name, p->val, (p->range == 1), (p->data_type == DTYPE_TABLE), p->option_count, p->options);
 309         }
 310         p = p->next;
 311     }
 312 
 313     conf_update_prevent_shutdown();
 314 
 315     old_gui_handler = gui_set_mode(&scriptGuiHandler);
 316 
 317     return script_stack_start();
 318 }
 319 
 320 //-------------------------------------------------------------------

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