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 "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;
23
24
25
26 static int script_terminate_key = KEY_SHOOT_FULL;
27 static char script_terminate_key_name[20];
28
29 static int script_terminate_request = 0;
30
31
32 void script_end();
33
34
35
36
37
38
39 static int print_screen_p;
40 static int print_screen_d = -1;
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
64
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
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);
98 script_console_add_line(str_id);
99 }
100
101
102
103
104
105
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
114
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;
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
162
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
172
173 static int gui_script_kbd_process()
174 {
175
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
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
217
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
225 while(camera_info.state.state_kbd_script_run != SCRIPT_STATE_INACTIVE) {
226 msleep(10);
227 }
228 }
229
230
231 void script_end()
232 {
233
234 camera_info.state.state_kbd_script_run = SCRIPT_STATE_INACTIVE;
235 camera_info.state.osd_title_line = 1 ;
236
237
238 script_terminate_key = KEY_SHOOT_FULL ;
239
240 script_print_screen_end();
241 console_set_autoredraw(1);
242
243
244 if (old_gui_handler)
245 {
246 extern gui_handler defaultGuiHandler;
247
248
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
260 libscriptapi->script_reset();
261
262
263
264
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);
272 conf_update_prevent_shutdown();
273 }
274
275 long script_start_gui( int autostart )
276 {
277 if (conf.script_file[0] == 0) return 0;
278
279 libshothisto->shot_histogram_set(0);
280 camera_info.state.auto_started = autostart;
281
282
283 stop_usb_HPtimer();
284
285
286 camera_info.state.kbd_last_clicked = 0;
287 camera_info.state.kbd_last_checked_time = get_tick_count();
288 kbd_key_release_all();
289
290
291 console_close();
292 script_print_screen_init();
293
294 save_params_values(0);
295
296 script_console_add_line((autostart)?LANG_CONSOLE_TEXT_AUTOSTARTED:LANG_CONSOLE_TEXT_STARTED);
297
298 module_set_script_lang(conf.script_file);
299 if ( !libscriptapi->script_start_file(conf.script_file) )
300 {
301 return -1;
302 }
303
304 sc_param *p = script_params;
305 while (p)
306 {
307 if (p->name != 0)
308 {
309 libscriptapi->set_variable(p->name, p->val, (p->range == 1), (p->data_type == DTYPE_TABLE), p->option_count, p->options);
310 }
311 p = p->next;
312 }
313
314 conf_update_prevent_shutdown();
315
316 old_gui_handler = gui_set_mode(&scriptGuiHandler);
317
318 return script_stack_start();
319 }
320
321