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

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