| 1 | #include "stdlib.h" |
|---|
| 2 | #include "keyboard.h" |
|---|
| 3 | #include "platform.h" |
|---|
| 4 | #include "core.h" |
|---|
| 5 | #include "conf.h" |
|---|
| 6 | #include "gui.h" |
|---|
| 7 | #include "font.h" |
|---|
| 8 | #include "gui_draw.h" |
|---|
| 9 | #include "gui_batt.h" |
|---|
| 10 | #include "gui_read.h" |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include "module_load.h" |
|---|
| 14 | |
|---|
| 15 | extern void gui_read_kbd_process_menu_btn(); |
|---|
| 16 | |
|---|
| 17 | int *conf_reader_autoscroll; |
|---|
| 18 | int *conf_reader_autoscroll_delay; |
|---|
| 19 | int *conf_reader_pos; |
|---|
| 20 | int *conf_reader_wrap_by_words; |
|---|
| 21 | color *conf_reader_color; |
|---|
| 22 | char *conf_reader_file; |
|---|
| 23 | char *conf_menu_rbf_file; |
|---|
| 24 | |
|---|
| 25 | gui_handler GUI_MODE_READ = |
|---|
| 26 | /*GUI_MODE_READ*/ { gui_read_draw, gui_read_kbd_process, gui_read_kbd_process_menu_btn, 0, GUI_MODE_MAGICNUM }; |
|---|
| 27 | |
|---|
| 28 | //------------------------------------------------------------------- |
|---|
| 29 | static int read_file; |
|---|
| 30 | static int read_file_size; |
|---|
| 31 | static int read_on_screen; |
|---|
| 32 | static int read_to_draw; |
|---|
| 33 | static coord x, y, h, w; |
|---|
| 34 | #define READ_BUFFER_SIZE 100 |
|---|
| 35 | static char buffer[READ_BUFFER_SIZE+1]; |
|---|
| 36 | static long last_time; |
|---|
| 37 | static int xx, yy; |
|---|
| 38 | static int pause; |
|---|
| 39 | |
|---|
| 40 | static int reader_is_active; // Flag raised when reader is succesfully runned |
|---|
| 41 | // purpose: we shouldn't process "leave" sequence if we call unload module but reader was not runed yet |
|---|
| 42 | |
|---|
| 43 | //------------------------------------------------------------------- |
|---|
| 44 | static void gui_read_draw_batt() { |
|---|
| 45 | sprintf(buffer, "Batt:%3d%%", get_batt_perc()); |
|---|
| 46 | draw_txt_string((screen_width-camera_info.ts_button_border)/FONT_WIDTH-2-1-1-9, 0, buffer, MAKE_COLOR(COLOR_BLACK, COLOR_WHITE)); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | //------------------------------------------------------------------- |
|---|
| 50 | static void gui_read_draw_clock() { |
|---|
| 51 | unsigned long t; |
|---|
| 52 | static struct tm *ttm; |
|---|
| 53 | |
|---|
| 54 | t = time(NULL); |
|---|
| 55 | ttm = localtime(&t); |
|---|
| 56 | sprintf(buffer, "%2u:%02u", ttm->tm_hour, ttm->tm_min); |
|---|
| 57 | draw_txt_string((screen_width-camera_info.ts_button_border)/FONT_WIDTH-2-1-1-9-2-5, 0, buffer, MAKE_COLOR(COLOR_BLACK, COLOR_WHITE)); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | //------------------------------------------------------------------- |
|---|
| 61 | static void gui_read_draw_scroll_indicator() { |
|---|
| 62 | draw_txt_char((screen_width-camera_info.ts_button_border)/FONT_WIDTH-2, 0, (*conf_reader_autoscroll)?((pause)?'\x05':'\x04'):'\x03', MAKE_COLOR(COLOR_BLACK, COLOR_WHITE)); //title infoline |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | //------------------------------------------------------------------- |
|---|
| 66 | int gui_read_init(const char* file) { |
|---|
| 67 | static struct STD_stat st; |
|---|
| 68 | read_file = safe_open(file, O_RDONLY, 0777); |
|---|
| 69 | if (strcmp(file, conf_reader_file)!=0) { |
|---|
| 70 | *conf_reader_pos = 0; |
|---|
| 71 | strcpy(conf_reader_file, file); |
|---|
| 72 | } |
|---|
| 73 | read_on_screen = 0; |
|---|
| 74 | read_file_size = (read_file>=0 && safe_stat((char*)file, &st)==0)?st.st_size:0; |
|---|
| 75 | if (read_file_size<=*conf_reader_pos) { |
|---|
| 76 | *conf_reader_pos = 0; |
|---|
| 77 | } |
|---|
| 78 | pause = 0; |
|---|
| 79 | read_to_draw = 1; |
|---|
| 80 | x=camera_info.ts_button_border+6; |
|---|
| 81 | y=FONT_HEIGHT; |
|---|
| 82 | w=screen_width-camera_info.ts_button_border*2-6-6-8; |
|---|
| 83 | h=screen_height-y; |
|---|
| 84 | last_time = get_tick_count(); |
|---|
| 85 | |
|---|
| 86 | reader_is_active=1; |
|---|
| 87 | gui_set_mode((unsigned int)&GUI_MODE_READ); |
|---|
| 88 | |
|---|
| 89 | draw_filled_rect(0, 0, screen_width-1, y-1, MAKE_COLOR(COLOR_BLACK, COLOR_BLACK)); |
|---|
| 90 | draw_filled_rect(0, y, screen_width-1, screen_height-1, MAKE_COLOR((*conf_reader_color>>8)&0xFF, (*conf_reader_color>>8)&0xFF)); |
|---|
| 91 | |
|---|
| 92 | gui_read_draw_scroll_indicator(); |
|---|
| 93 | gui_read_draw_batt(); |
|---|
| 94 | |
|---|
| 95 | return (read_file >= 0); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | //------------------------------------------------------------------- |
|---|
| 99 | static void read_goto_next_line() { |
|---|
| 100 | draw_filled_rect(xx, yy, x+w-1, yy+rbf_font_height()-1, MAKE_COLOR(*conf_reader_color>>8, *conf_reader_color>>8)); |
|---|
| 101 | xx = x; |
|---|
| 102 | yy += rbf_font_height(); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | //------------------------------------------------------------------- |
|---|
| 106 | static int read_fit_next_char(int ch) { |
|---|
| 107 | return (xx+rbf_char_width(ch) < x+w); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | //------------------------------------------------------------------- |
|---|
| 111 | void gui_read_draw(int enforce_redraw) { |
|---|
| 112 | if (*conf_reader_autoscroll && !pause && get_tick_count()-last_time >= *conf_reader_autoscroll_delay*1000 && (*conf_reader_pos+read_on_screen)<read_file_size) { |
|---|
| 113 | *conf_reader_pos += read_on_screen; |
|---|
| 114 | read_to_draw = 1; |
|---|
| 115 | } |
|---|
| 116 | if (read_to_draw) { |
|---|
| 117 | int n, i, ii, ll, new_word=1; |
|---|
| 118 | |
|---|
| 119 | xx=x; yy=y; |
|---|
| 120 | |
|---|
| 121 | lseek(read_file, *conf_reader_pos, SEEK_SET); |
|---|
| 122 | read_on_screen=0; |
|---|
| 123 | |
|---|
| 124 | while (yy<=y+h-rbf_font_height()) { |
|---|
| 125 | n=read(read_file, buffer, READ_BUFFER_SIZE); |
|---|
| 126 | if (n==0) { |
|---|
| 127 | read_goto_next_line(); |
|---|
| 128 | if (yy < y+h) |
|---|
| 129 | draw_filled_rect(x, yy, x+w-1, y+h-1, MAKE_COLOR(*conf_reader_color>>8, *conf_reader_color>>8)); |
|---|
| 130 | break; |
|---|
| 131 | } |
|---|
| 132 | i=0; |
|---|
| 133 | while (i<n && yy<=y+h-rbf_font_height()) { |
|---|
| 134 | switch (buffer[i]) { |
|---|
| 135 | case '\r': |
|---|
| 136 | new_word = 1; |
|---|
| 137 | break; |
|---|
| 138 | case '\n': |
|---|
| 139 | read_goto_next_line(); |
|---|
| 140 | new_word = 1; |
|---|
| 141 | break; |
|---|
| 142 | case '\t': |
|---|
| 143 | buffer[i] = ' '; |
|---|
| 144 | // no break here |
|---|
| 145 | default: |
|---|
| 146 | if (*conf_reader_wrap_by_words) { |
|---|
| 147 | if (buffer[i] == ' ') { |
|---|
| 148 | new_word = 1; |
|---|
| 149 | if (xx==x) //ignore leading spaces |
|---|
| 150 | break; |
|---|
| 151 | } else if (new_word) { |
|---|
| 152 | new_word = 0; |
|---|
| 153 | for (ii=i, ll=0; ii<n && buffer[ii]!=' ' && buffer[ii]!='\t' && buffer[ii]!='\r' && buffer[ii]!='\n'; ++ii) { |
|---|
| 154 | ll+=rbf_char_width(buffer[ii]); |
|---|
| 155 | } |
|---|
| 156 | if (ii==n) { |
|---|
| 157 | memcpy(buffer, buffer+i, n-i); |
|---|
| 158 | n=ii=n-i; |
|---|
| 159 | read_on_screen+=i; |
|---|
| 160 | i=0; |
|---|
| 161 | n+=read(read_file, buffer+n, READ_BUFFER_SIZE-n); |
|---|
| 162 | for (; ii<n && buffer[ii]!=' ' && buffer[ii]!='\t' && buffer[ii]!='\r' && buffer[ii]!='\n'; ++ii) { |
|---|
| 163 | ll+=rbf_char_width(buffer[ii]); |
|---|
| 164 | } |
|---|
| 165 | } |
|---|
| 166 | if (xx+ll>=x+w && ll<w) { |
|---|
| 167 | read_goto_next_line(); |
|---|
| 168 | continue; |
|---|
| 169 | } |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | if (!read_fit_next_char(buffer[i])) { |
|---|
| 173 | read_goto_next_line(); |
|---|
| 174 | continue; |
|---|
| 175 | } |
|---|
| 176 | xx+=rbf_draw_char(xx, yy, buffer[i], *conf_reader_color); |
|---|
| 177 | break; |
|---|
| 178 | } |
|---|
| 179 | ++i; |
|---|
| 180 | if (xx >= x+w) { |
|---|
| 181 | xx = x; |
|---|
| 182 | yy += rbf_font_height(); |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | read_on_screen+=i; |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | sprintf(buffer, "(%3d%%) %d/%d ", (read_file_size)?(*conf_reader_pos*100/read_file_size):0, *conf_reader_pos, read_file_size); |
|---|
| 189 | buffer[screen_width/FONT_WIDTH]=0; |
|---|
| 190 | draw_txt_string((camera_info.ts_button_border/FONT_WIDTH), 0, buffer, MAKE_COLOR(COLOR_BLACK, COLOR_WHITE)); //title infoline |
|---|
| 191 | |
|---|
| 192 | // scrollbar |
|---|
| 193 | if (read_file_size) { |
|---|
| 194 | i=h-1 -1; // full height |
|---|
| 195 | n=i*read_on_screen/read_file_size; // bar height |
|---|
| 196 | if (n<20) n=20; |
|---|
| 197 | i=(i-n)**conf_reader_pos/read_file_size; // top pos |
|---|
| 198 | draw_filled_rect(x+w+6+2, y+1, x+w+6+6, y+1+i, MAKE_COLOR(COLOR_BLACK, COLOR_BLACK)); |
|---|
| 199 | draw_filled_rect(x+w+6+2, y+i+n, x+w+6+6, y+h-1-1, MAKE_COLOR(COLOR_BLACK, COLOR_BLACK)); |
|---|
| 200 | draw_filled_rect(x+w+6+2, y+1+i, x+w+6+6, y+i+n, MAKE_COLOR(COLOR_WHITE, COLOR_WHITE)); |
|---|
| 201 | } else { |
|---|
| 202 | draw_filled_rect((x+w)*FONT_WIDTH+2, y*FONT_HEIGHT+1, |
|---|
| 203 | (x+w)*FONT_WIDTH+6, (y+h)*FONT_HEIGHT-1-1, MAKE_COLOR(COLOR_BLACK, COLOR_BLACK)); |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | read_to_draw = 0; |
|---|
| 207 | last_time = get_tick_count(); |
|---|
| 208 | } |
|---|
| 209 | gui_read_draw_batt(); |
|---|
| 210 | gui_read_draw_clock(); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | //------------------------------------------------------------------- |
|---|
| 214 | void gui_read_kbd_process() { |
|---|
| 215 | switch (kbd_get_autoclicked_key() | get_jogdial_direction()) { |
|---|
| 216 | case JOGDIAL_LEFT: |
|---|
| 217 | case KEY_ZOOM_OUT: |
|---|
| 218 | case KEY_UP: |
|---|
| 219 | case KEY_LEFT: |
|---|
| 220 | if (*conf_reader_pos>0) { |
|---|
| 221 | *conf_reader_pos -= 45*15; |
|---|
| 222 | if (*conf_reader_pos<0) *conf_reader_pos=0; |
|---|
| 223 | read_to_draw = 1; |
|---|
| 224 | } |
|---|
| 225 | break; |
|---|
| 226 | case JOGDIAL_RIGHT: |
|---|
| 227 | case KEY_ZOOM_IN: |
|---|
| 228 | case KEY_DOWN: |
|---|
| 229 | case KEY_RIGHT: |
|---|
| 230 | case KEY_SHOOT_HALF: |
|---|
| 231 | if ((*conf_reader_pos+read_on_screen)<read_file_size) { |
|---|
| 232 | *conf_reader_pos += read_on_screen; |
|---|
| 233 | read_to_draw = 1; |
|---|
| 234 | } |
|---|
| 235 | break; |
|---|
| 236 | case KEY_SET: |
|---|
| 237 | break; |
|---|
| 238 | case KEY_DISPLAY: |
|---|
| 239 | pause = !pause; |
|---|
| 240 | gui_read_draw_scroll_indicator(); |
|---|
| 241 | last_time = get_tick_count(); |
|---|
| 242 | break; |
|---|
| 243 | case KEY_MENU: |
|---|
| 244 | gui_read_kbd_leave(); |
|---|
| 245 | break; |
|---|
| 246 | } |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | extern int module_idx; |
|---|
| 250 | |
|---|
| 251 | //------------------------------------------------------------------- |
|---|
| 252 | // Menu button handled for text reader |
|---|
| 253 | void gui_read_kbd_process_menu_btn() |
|---|
| 254 | { |
|---|
| 255 | gui_read_kbd_process(); |
|---|
| 256 | gui_default_kbd_process_menu_btn(); |
|---|
| 257 | module_async_unload(module_idx); |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | void gui_read_kbd_leave() |
|---|
| 261 | { |
|---|
| 262 | if ( !reader_is_active ) |
|---|
| 263 | return; |
|---|
| 264 | |
|---|
| 265 | reader_is_active = 0; |
|---|
| 266 | if (!rbf_load(conf_menu_rbf_file)) |
|---|
| 267 | rbf_load_from_8x16(current_font); |
|---|
| 268 | rbf_set_codepage(FONT_CP_WIN); |
|---|
| 269 | if (read_file >= 0) { |
|---|
| 270 | close(read_file); |
|---|
| 271 | read_file=-1; |
|---|
| 272 | } |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | // ========= MODULE INIT ================= |
|---|
| 276 | #include "module_load.h" |
|---|
| 277 | int module_idx=-1; |
|---|
| 278 | |
|---|
| 279 | /***************** BEGIN OF AUXILARY PART ********************* |
|---|
| 280 | ATTENTION: DO NOT REMOVE OR CHANGE SIGNATURES IN THIS SECTION |
|---|
| 281 | **************************************************************/ |
|---|
| 282 | |
|---|
| 283 | void* MODULE_EXPORT_LIST[] = { |
|---|
| 284 | /* 0 */ (void*)EXPORTLIST_MAGIC_NUMBER, |
|---|
| 285 | /* 1 */ (void*)0 |
|---|
| 286 | }; |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | //--------------------------------------------------------- |
|---|
| 290 | // PURPOSE: Bind module symbols with chdk. |
|---|
| 291 | // Required function |
|---|
| 292 | // PARAMETERS: pointer to chdk list of export |
|---|
| 293 | // RETURN VALUE: 1 error, 0 ok |
|---|
| 294 | //--------------------------------------------------------- |
|---|
| 295 | int _module_loader( void** chdk_export_list ) |
|---|
| 296 | { |
|---|
| 297 | if ( (unsigned int)chdk_export_list[0] != EXPORTLIST_MAGIC_NUMBER ) |
|---|
| 298 | return 1; |
|---|
| 299 | |
|---|
| 300 | tConfigVal configVal; |
|---|
| 301 | CONF_BIND_COLOR( 31, conf_reader_color ); |
|---|
| 302 | CONF_BIND_STR( 38, conf_reader_file ); |
|---|
| 303 | CONF_BIND_INT( 39, conf_reader_pos ); |
|---|
| 304 | CONF_BIND_INT( 43, conf_reader_autoscroll ); |
|---|
| 305 | CONF_BIND_INT( 44, conf_reader_autoscroll_delay ); |
|---|
| 306 | CONF_BIND_INT( 61, conf_reader_wrap_by_words ); |
|---|
| 307 | CONF_BIND_STR( 66, conf_menu_rbf_file ); |
|---|
| 308 | |
|---|
| 309 | return 0; |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | //--------------------------------------------------------- |
|---|
| 315 | // PURPOSE: Finalize module operations (close allocs, etc) |
|---|
| 316 | // RETURN VALUE: 0-ok, 1-fail |
|---|
| 317 | //--------------------------------------------------------- |
|---|
| 318 | int _module_unloader() |
|---|
| 319 | { |
|---|
| 320 | // We should make "leave sequence" to restore font settings |
|---|
| 321 | gui_read_kbd_leave(); |
|---|
| 322 | |
|---|
| 323 | GUI_MODE_READ.magicnum = 0; //sanity clean to prevent accidentaly assign/restore guimode to unloaded module |
|---|
| 324 | |
|---|
| 325 | return 0; |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | //--------------------------------------------------------- |
|---|
| 330 | // PURPOSE: Default action for simple modules (direct run) |
|---|
| 331 | // NOTE: Please comment this function if no default action and this library module |
|---|
| 332 | //--------------------------------------------------------- |
|---|
| 333 | int _module_run(int moduleidx, int argn, int* arguments) |
|---|
| 334 | { |
|---|
| 335 | module_idx=moduleidx; |
|---|
| 336 | |
|---|
| 337 | if ( argn!=1 || arguments[0]==0) { |
|---|
| 338 | module_async_unload(moduleidx); |
|---|
| 339 | return 1; |
|---|
| 340 | } |
|---|
| 341 | |
|---|
| 342 | char* fn=(char*)arguments[0]; |
|---|
| 343 | gui_read_init(fn); |
|---|
| 344 | |
|---|
| 345 | return 0; |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | /******************** Module Information structure ******************/ |
|---|
| 350 | |
|---|
| 351 | struct ModuleInfo _module_info = { MODULEINFO_V1_MAGICNUM, |
|---|
| 352 | sizeof(struct ModuleInfo), |
|---|
| 353 | |
|---|
| 354 | ANY_CHDK_BRANCH, 0, // Requirements of CHDK version |
|---|
| 355 | ANY_PLATFORM_ALLOWED, // Specify platform dependency |
|---|
| 356 | MODULEINFO_FLAG_SYSTEM, // flag |
|---|
| 357 | (int32_t)"Text reader", // Module name |
|---|
| 358 | 1, 0, // Module version |
|---|
| 359 | 0 |
|---|
| 360 | }; |
|---|
| 361 | |
|---|
| 362 | /*************** END OF AUXILARY PART *******************/ |
|---|