This source file includes following definitions.
- script_print_screen_init
- script_print_screen_end
- script_print_screen_statement
- script_console_add_line
- script_console_add_error
- action_stack_AS_SCRIPT_RUN
- script_stack_start
- script_is_running
- script_set_terminate_key
- script_get_alt_text
- script_check_terminate
- gui_script_kbd_process
- gui_script_draw
- script_wait_terminate
- script_end
- 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;
22
23
24
25 static int script_terminate_key = KEY_SHOOT_FULL;
26 static char script_terminate_key_name[20];
27
28 static int script_terminate_request = 0;
29
30
31 void script_end();
32
33
34
35
36
37
38 static int print_screen_p;
39 static int print_screen_d = -1;
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
63
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
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);
97 script_console_add_line(str_id);
98 }
99
100
101
102
103
104
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
113
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;
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
161
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
171
172 static int gui_script_kbd_process()
173 {
174
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
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
216
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
224 while(camera_info.state.state_kbd_script_run != SCRIPT_STATE_INACTIVE) {
225 msleep(10);
226 }
227 }
228
229
230 void script_end()
231 {
232
233 camera_info.state.state_kbd_script_run = SCRIPT_STATE_INACTIVE;
234 camera_info.state.osd_title_line = 1 ;
235
236
237 script_terminate_key = KEY_SHOOT_FULL ;
238
239 script_print_screen_end();
240 console_set_autoredraw(1);
241
242
243 if (old_gui_handler)
244 {
245 extern gui_handler defaultGuiHandler;
246
247
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
259 libscriptapi->script_reset();
260
261
262
263
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);
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
282 stop_usb_HPtimer();
283
284
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
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