| 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 | |
|---|
| 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 | |
|---|
| 26 | // Forward references |
|---|
| 27 | void script_end(); |
|---|
| 28 | |
|---|
| 29 | // External references |
|---|
| 30 | extern const char* script_source_str; |
|---|
| 31 | extern char script_params[SCRIPT_NUM_PARAMS][28]; |
|---|
| 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 | //======================================================= |
|---|
| 95 | // SCRIPT FUNCTIONS |
|---|
| 96 | //======================================================= |
|---|
| 97 | |
|---|
| 98 | // Stack process function for running current script |
|---|
| 99 | static int action_stack_AS_SCRIPT_RUN() |
|---|
| 100 | { |
|---|
| 101 | if (camera_info.state.state_kbd_script_run) |
|---|
| 102 | { |
|---|
| 103 | int rv = libscriptapi->script_run(); |
|---|
| 104 | if (rv != SCRIPT_RUN_RUNNING) |
|---|
| 105 | { |
|---|
| 106 | // Script language is responsible for displaying 'Finished' or error |
|---|
| 107 | // messages so all we need to do is shutdown the script engine |
|---|
| 108 | script_end(); |
|---|
| 109 | return 1; |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | else |
|---|
| 113 | { |
|---|
| 114 | action_pop_func(0); |
|---|
| 115 | return 1; |
|---|
| 116 | } |
|---|
| 117 | return 0; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | long script_stack_start() |
|---|
| 121 | { |
|---|
| 122 | camera_info.state.state_kbd_script_run = SCRIPT_STATE_RAN; |
|---|
| 123 | running_script_stack_name = action_stack_create(&action_stack_AS_SCRIPT_RUN); |
|---|
| 124 | return running_script_stack_name; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | int script_is_running() |
|---|
| 128 | { |
|---|
| 129 | return !action_stack_is_finished(running_script_stack_name); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | //------------------------------------------------------------------- |
|---|
| 133 | |
|---|
| 134 | // Main button processing for CHDK Script mode |
|---|
| 135 | static int gui_script_kbd_process() |
|---|
| 136 | { |
|---|
| 137 | // Stop a script if the shutter button pressed in Script mode |
|---|
| 138 | if (kbd_is_key_clicked(KEY_SHOOT_FULL)) |
|---|
| 139 | { |
|---|
| 140 | script_console_add_line(LANG_CONSOLE_TEXT_INTERRUPTED); |
|---|
| 141 | if (camera_info.state.state_kbd_script_run == SCRIPT_STATE_INTERRUPTED) |
|---|
| 142 | script_end(); |
|---|
| 143 | else |
|---|
| 144 | { |
|---|
| 145 | camera_info.state.state_kbd_script_run = SCRIPT_STATE_INTERRUPTED; |
|---|
| 146 | if (libscriptapi->run_restore() == 0) |
|---|
| 147 | script_end(); |
|---|
| 148 | } |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | return 0; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | //------------------------------------------------------------------- |
|---|
| 155 | void gui_script_draw() |
|---|
| 156 | { |
|---|
| 157 | extern void gui_chdk_draw(); |
|---|
| 158 | gui_chdk_draw(); |
|---|
| 159 | |
|---|
| 160 | if ((mode_get()&MODE_MASK) == MODE_REC || (mode_get()&MODE_MASK) == MODE_PLAY) |
|---|
| 161 | { |
|---|
| 162 | static int show_md_grid=0; |
|---|
| 163 | if (camera_info.state.state_kbd_script_run) show_md_grid=5; |
|---|
| 164 | if (show_md_grid) |
|---|
| 165 | { |
|---|
| 166 | --show_md_grid; |
|---|
| 167 | libmotiondetect->md_draw_grid(); |
|---|
| 168 | } |
|---|
| 169 | } |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | // GUI handler for Script mode |
|---|
| 173 | gui_handler scriptGuiHandler = { GUI_MODE_SCRIPT, gui_script_draw, gui_script_kbd_process, 0, 0 }; |
|---|
| 174 | |
|---|
| 175 | static gui_handler *old_gui_handler = 0; |
|---|
| 176 | |
|---|
| 177 | //------------------------------------------------------------------- |
|---|
| 178 | |
|---|
| 179 | // Terminate a script, either because of error, or the script finished |
|---|
| 180 | void script_end() |
|---|
| 181 | { |
|---|
| 182 | // Tell other code that script has ended |
|---|
| 183 | camera_info.state.state_kbd_script_run = SCRIPT_STATE_INACTIVE; |
|---|
| 184 | |
|---|
| 185 | script_print_screen_end(); |
|---|
| 186 | |
|---|
| 187 | // Restore old handler - prevent calling MD draw after module unloaded |
|---|
| 188 | if (old_gui_handler) |
|---|
| 189 | { |
|---|
| 190 | gui_set_mode(old_gui_handler); |
|---|
| 191 | old_gui_handler = 0; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | // Reset script language module |
|---|
| 195 | libscriptapi->script_reset(); |
|---|
| 196 | |
|---|
| 197 | // Kill off the action_stack for the script, since we've just reset the script |
|---|
| 198 | // language and unloaded the MD module, we don't need to let the stack empty |
|---|
| 199 | // itself. |
|---|
| 200 | action_stack_kill(running_script_stack_name); |
|---|
| 201 | running_script_stack_name = -1; |
|---|
| 202 | |
|---|
| 203 | shot_histogram_set(0); |
|---|
| 204 | kbd_key_release_all(); |
|---|
| 205 | |
|---|
| 206 | conf_update_prevent_shutdown(); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | long script_start_gui( int autostart ) |
|---|
| 210 | { |
|---|
| 211 | int i; |
|---|
| 212 | |
|---|
| 213 | shot_histogram_set(0); |
|---|
| 214 | camera_info.state.auto_started = autostart; |
|---|
| 215 | |
|---|
| 216 | // Keyboard init |
|---|
| 217 | camera_info.state.kbd_last_clicked = 0; |
|---|
| 218 | camera_info.state.kbd_last_checked_time = get_tick_count(); |
|---|
| 219 | kbd_key_release_all(); |
|---|
| 220 | |
|---|
| 221 | // Close old console, will be re-opened when first line added |
|---|
| 222 | console_close(); |
|---|
| 223 | script_print_screen_init(); |
|---|
| 224 | |
|---|
| 225 | save_params_values(0); |
|---|
| 226 | |
|---|
| 227 | script_console_add_line((autostart)?LANG_CONSOLE_TEXT_AUTOSTARTED:LANG_CONSOLE_TEXT_STARTED); |
|---|
| 228 | |
|---|
| 229 | module_set_script_lang(conf.script_file); |
|---|
| 230 | if ( !libscriptapi->script_start(script_source_str,0) ) |
|---|
| 231 | { |
|---|
| 232 | return -1; |
|---|
| 233 | } |
|---|
| 234 | for (i=0; i<SCRIPT_NUM_PARAMS; ++i) |
|---|
| 235 | { |
|---|
| 236 | if( script_params[i][0] ) |
|---|
| 237 | { |
|---|
| 238 | libscriptapi->set_variable(i, conf.script_vars[i]); |
|---|
| 239 | } |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | conf_update_prevent_shutdown(); |
|---|
| 243 | |
|---|
| 244 | old_gui_handler = gui_set_mode(&scriptGuiHandler); |
|---|
| 245 | |
|---|
| 246 | return script_stack_start(); |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | //------------------------------------------------------------------- |
|---|