diff options
Diffstat (limited to 'apps/plugins/viewer.c')
-rw-r--r-- | apps/plugins/viewer.c | 99 |
1 files changed, 50 insertions, 49 deletions
diff --git a/apps/plugins/viewer.c b/apps/plugins/viewer.c index 6c440c5a5d..7ab0d39447 100644 --- a/apps/plugins/viewer.c +++ b/apps/plugins/viewer.c | |||
@@ -22,7 +22,7 @@ | |||
22 | 22 | ||
23 | PLUGIN_HEADER | 23 | PLUGIN_HEADER |
24 | 24 | ||
25 | #define SETTINGS_FILE "/.rockbox/viewers/viewer.dat" | 25 | #define SETTINGS_FILE "/.rockbox/viewers/viewer.dat" |
26 | 26 | ||
27 | #define WRAP_TRIM 44 /* Max number of spaces to trim (arbitrary) */ | 27 | #define WRAP_TRIM 44 /* Max number of spaces to trim (arbitrary) */ |
28 | #define MAX_COLUMNS 64 /* Max displayable string len (over-estimate) */ | 28 | #define MAX_COLUMNS 64 /* Max displayable string len (over-estimate) */ |
@@ -117,10 +117,11 @@ PLUGIN_HEADER | |||
117 | #define VIEWER_COLUMN_LEFT (BUTTON_ON | BUTTON_LEFT) | 117 | #define VIEWER_COLUMN_LEFT (BUTTON_ON | BUTTON_LEFT) |
118 | #define VIEWER_COLUMN_RIGHT (BUTTON_ON | BUTTON_RIGHT) | 118 | #define VIEWER_COLUMN_RIGHT (BUTTON_ON | BUTTON_RIGHT) |
119 | 119 | ||
120 | /* iPods with the 4G pad */ | ||
120 | #elif (CONFIG_KEYPAD == IPOD_4G_PAD) | 121 | #elif (CONFIG_KEYPAD == IPOD_4G_PAD) |
121 | #define VIEWER_QUIT BUTTON_MENU | 122 | #define VIEWER_QUIT BUTTON_MENU |
122 | #define VIEWER_PAGE_UP BUTTON_SCROLL_FWD | 123 | #define VIEWER_PAGE_UP BUTTON_SCROLL_BACK |
123 | #define VIEWER_PAGE_DOWN BUTTON_SCROLL_BACK | 124 | #define VIEWER_PAGE_DOWN BUTTON_SCROLL_FWD |
124 | #define VIEWER_SCREEN_LEFT BUTTON_LEFT | 125 | #define VIEWER_SCREEN_LEFT BUTTON_LEFT |
125 | #define VIEWER_SCREEN_RIGHT BUTTON_RIGHT | 126 | #define VIEWER_SCREEN_RIGHT BUTTON_RIGHT |
126 | #define VIEWER_MODE_WRAP (BUTTON_SELECT | BUTTON_LEFT) | 127 | #define VIEWER_MODE_WRAP (BUTTON_SELECT | BUTTON_LEFT) |
@@ -287,9 +288,9 @@ static unsigned char* find_next_line(const unsigned char* cur_line, bool *is_sho | |||
287 | bool first_chars; | 288 | bool first_chars; |
288 | unsigned char c; | 289 | unsigned char c; |
289 | 290 | ||
290 | if (is_short != NULL) | 291 | if (is_short != NULL) |
291 | *is_short = true; | 292 | *is_short = true; |
292 | 293 | ||
293 | if BUFFER_OOB(cur_line) | 294 | if BUFFER_OOB(cur_line) |
294 | return NULL; | 295 | return NULL; |
295 | 296 | ||
@@ -297,7 +298,7 @@ static unsigned char* find_next_line(const unsigned char* cur_line, bool *is_sho | |||
297 | search_len = MAX_WIDTH; | 298 | search_len = MAX_WIDTH; |
298 | } | 299 | } |
299 | else { /* view_mode == NARROW */ | 300 | else { /* view_mode == NARROW */ |
300 | search_len = crop_at_width(cur_line) - cur_line; | 301 | search_len = crop_at_width(cur_line) - cur_line; |
301 | } | 302 | } |
302 | 303 | ||
303 | size = BUFFER_OOB(cur_line+search_len) ? buffer_end-cur_line : search_len; | 304 | size = BUFFER_OOB(cur_line+search_len) ? buffer_end-cur_line : search_len; |
@@ -326,7 +327,7 @@ static unsigned char* find_next_line(const unsigned char* cur_line, bool *is_sho | |||
326 | } | 327 | } |
327 | if (j==0) /* i=1 is intentional */ | 328 | if (j==0) /* i=1 is intentional */ |
328 | for (i=0; i<par_indent_spaces; i++) | 329 | for (i=0; i<par_indent_spaces; i++) |
329 | ADVANCE_COUNTERS(' '); | 330 | ADVANCE_COUNTERS(' '); |
330 | } | 331 | } |
331 | if (!first_chars) spaces++; | 332 | if (!first_chars) spaces++; |
332 | break; | 333 | break; |
@@ -339,7 +340,7 @@ static unsigned char* find_next_line(const unsigned char* cur_line, bool *is_sho | |||
339 | return (unsigned char*) next_line; | 340 | return (unsigned char*) next_line; |
340 | break; | 341 | break; |
341 | } | 342 | } |
342 | 343 | ||
343 | newlines++; | 344 | newlines++; |
344 | size += spaces -1; | 345 | size += spaces -1; |
345 | if (BUFFER_OOB(cur_line+size) || size > 2*search_len) | 346 | if (BUFFER_OOB(cur_line+size) || size > 2*search_len) |
@@ -360,9 +361,9 @@ static unsigned char* find_next_line(const unsigned char* cur_line, bool *is_sho | |||
360 | } | 361 | } |
361 | newlines=0; | 362 | newlines=0; |
362 | } else if (spaces) { | 363 | } else if (spaces) { |
363 | /* REFLOW, multiple spaces between words: count only | 364 | /* REFLOW, multiple spaces between words: count only |
364 | * one. If more are needed, they will be added | 365 | * one. If more are needed, they will be added |
365 | * while drawing. */ | 366 | * while drawing. */ |
366 | search_len = size; | 367 | search_len = size; |
367 | spaces=0; | 368 | spaces=0; |
368 | ADVANCE_COUNTERS(' '); | 369 | ADVANCE_COUNTERS(' '); |
@@ -370,7 +371,7 @@ static unsigned char* find_next_line(const unsigned char* cur_line, bool *is_sho | |||
370 | size = search_len = j; | 371 | size = search_len = j; |
371 | break; | 372 | break; |
372 | } | 373 | } |
373 | } | 374 | } |
374 | first_chars = false; | 375 | first_chars = false; |
375 | ADVANCE_COUNTERS(c); | 376 | ADVANCE_COUNTERS(c); |
376 | break; | 377 | break; |
@@ -418,7 +419,7 @@ static unsigned char* find_next_line(const unsigned char* cur_line, bool *is_sho | |||
418 | 419 | ||
419 | if (is_short) | 420 | if (is_short) |
420 | *is_short = false; | 421 | *is_short = false; |
421 | 422 | ||
422 | return (unsigned char*) next_line; | 423 | return (unsigned char*) next_line; |
423 | } | 424 | } |
424 | 425 | ||
@@ -514,7 +515,7 @@ static int read_and_synch(int direction) | |||
514 | /* direction: 1 for down (i.e., further into file), -1 for up */ | 515 | /* direction: 1 for down (i.e., further into file), -1 for up */ |
515 | int move_size, move_vector, offset; | 516 | int move_size, move_vector, offset; |
516 | unsigned char *fill_buf; | 517 | unsigned char *fill_buf; |
517 | 518 | ||
518 | if (direction == -1) /* up */ { | 519 | if (direction == -1) /* up */ { |
519 | move_size = SMALL_BLOCK_SIZE; | 520 | move_size = SMALL_BLOCK_SIZE; |
520 | offset = 0; | 521 | offset = 0; |
@@ -668,7 +669,7 @@ static void viewer_draw(int col) | |||
668 | break; | 669 | break; |
669 | } | 670 | } |
670 | } | 671 | } |
671 | 672 | ||
672 | if (col != -1) | 673 | if (col != -1) |
673 | if (k > col) { | 674 | if (k > col) { |
674 | scratch_buffer[k] = 0; | 675 | scratch_buffer[k] = 0; |
@@ -693,10 +694,10 @@ static void viewer_draw(int col) | |||
693 | case ' ': | 694 | case ' ': |
694 | case 0: | 695 | case 0: |
695 | if ((j==0) && (word_mode==WRAP)) | 696 | if ((j==0) && (word_mode==WRAP)) |
696 | /* special case: indent the paragraph, | 697 | /* special case: indent the paragraph, |
697 | * don't count spaces */ | 698 | * don't count spaces */ |
698 | indent_spaces = par_indent_spaces; | 699 | indent_spaces = par_indent_spaces; |
699 | else if (!multiple_spacing) | 700 | else if (!multiple_spacing) |
700 | spaces++; | 701 | spaces++; |
701 | multiple_spacing = true; | 702 | multiple_spacing = true; |
702 | break; | 703 | break; |
@@ -708,10 +709,10 @@ static void viewer_draw(int col) | |||
708 | } | 709 | } |
709 | } | 710 | } |
710 | if (multiple_spacing) spaces--; | 711 | if (multiple_spacing) spaces--; |
711 | 712 | ||
712 | if (spaces) { | 713 | if (spaces) { |
713 | /* total number of spaces to insert between words */ | 714 | /* total number of spaces to insert between words */ |
714 | extra_spaces = (draw_columns-width) / glyph_width[' '] | 715 | extra_spaces = (draw_columns-width) / glyph_width[' '] |
715 | - indent_spaces; | 716 | - indent_spaces; |
716 | /* number of spaces between each word*/ | 717 | /* number of spaces between each word*/ |
717 | spaces_per_word = extra_spaces / spaces; | 718 | spaces_per_word = extra_spaces / spaces; |
@@ -720,7 +721,7 @@ static void viewer_draw(int col) | |||
720 | if (spaces_per_word > 2) { /* too much spacing is awful */ | 721 | if (spaces_per_word > 2) { /* too much spacing is awful */ |
721 | spaces_per_word = 3; | 722 | spaces_per_word = 3; |
722 | extra_spaces = 0; | 723 | extra_spaces = 0; |
723 | } | 724 | } |
724 | } else { /* this doesn't matter much... no spaces anyway */ | 725 | } else { /* this doesn't matter much... no spaces anyway */ |
725 | spaces_per_word = extra_spaces = 0; | 726 | spaces_per_word = extra_spaces = 0; |
726 | } | 727 | } |
@@ -728,7 +729,7 @@ static void viewer_draw(int col) | |||
728 | spaces_per_word = 1; | 729 | spaces_per_word = 1; |
729 | extra_spaces = 0; | 730 | extra_spaces = 0; |
730 | } | 731 | } |
731 | 732 | ||
732 | multiple_spacing = false; | 733 | multiple_spacing = false; |
733 | for (j=k=spaces=0; j < line_len; j++) { | 734 | for (j=k=spaces=0; j < line_len; j++) { |
734 | if (k == MAX_COLUMNS) | 735 | if (k == MAX_COLUMNS) |
@@ -742,7 +743,7 @@ static void viewer_draw(int col) | |||
742 | for (j=0; j<par_indent_spaces; j++) | 743 | for (j=0; j<par_indent_spaces; j++) |
743 | scratch_buffer[k++] = ' '; | 744 | scratch_buffer[k++] = ' '; |
744 | j=0; | 745 | j=0; |
745 | } | 746 | } |
746 | else if (!multiple_spacing) { | 747 | else if (!multiple_spacing) { |
747 | for (width = spaces<extra_spaces ? -1:0; width < spaces_per_word; width++) | 748 | for (width = spaces<extra_spaces ? -1:0; width < spaces_per_word; width++) |
748 | scratch_buffer[k++] = ' '; | 749 | scratch_buffer[k++] = ' '; |
@@ -756,13 +757,13 @@ static void viewer_draw(int col) | |||
756 | break; | 757 | break; |
757 | } | 758 | } |
758 | } | 759 | } |
759 | 760 | ||
760 | if (col != -1) | 761 | if (col != -1) |
761 | if (k > col) { | 762 | if (k > col) { |
762 | scratch_buffer[k] = 0; | 763 | scratch_buffer[k] = 0; |
763 | rb->lcd_puts(left_col, i, scratch_buffer + col); | 764 | rb->lcd_puts(left_col, i, scratch_buffer + col); |
764 | } | 765 | } |
765 | } | 766 | } |
766 | else { /* line_mode != JOIN && line_mode != REFLOW */ | 767 | else { /* line_mode != JOIN && line_mode != REFLOW */ |
767 | if (col != -1) | 768 | if (col != -1) |
768 | if (line_len > col) { | 769 | if (line_len > col) { |
@@ -848,16 +849,16 @@ static bool viewer_init(void) | |||
848 | pf = rb->font_get(FONT_UI); | 849 | pf = rb->font_get(FONT_UI); |
849 | if (pf->width != NULL) | 850 | if (pf->width != NULL) |
850 | { /* variable pitch font -- fill structure from font width data */ | 851 | { /* variable pitch font -- fill structure from font width data */ |
851 | ch = pf->defaultchar - pf->firstchar; | 852 | ch = pf->defaultchar - pf->firstchar; |
852 | rb->memset(glyph_width, pf->width[ch], 256); | 853 | rb->memset(glyph_width, pf->width[ch], 256); |
853 | idx = pf->firstchar; | 854 | idx = pf->firstchar; |
854 | rb->memcpy(&glyph_width[idx], pf->width, pf->size); | 855 | rb->memcpy(&glyph_width[idx], pf->width, pf->size); |
855 | idx += pf->size; | 856 | idx += pf->size; |
856 | rb->memset(&glyph_width[idx], pf->width[ch], 256-idx); | 857 | rb->memset(&glyph_width[idx], pf->width[ch], 256-idx); |
857 | } | 858 | } |
858 | else /* fixed pitch font -- same width for all glyphs */ | 859 | else /* fixed pitch font -- same width for all glyphs */ |
859 | rb->memset(glyph_width, pf->maxwidth, 256); | 860 | rb->memset(glyph_width, pf->maxwidth, 256); |
860 | 861 | ||
861 | display_lines = LCD_HEIGHT / pf->height; | 862 | display_lines = LCD_HEIGHT / pf->height; |
862 | display_columns = LCD_WIDTH; | 863 | display_columns = LCD_WIDTH; |
863 | #else | 864 | #else |
@@ -916,16 +917,16 @@ typedef struct { | |||
916 | 917 | ||
917 | static void viewer_load_settings(void) | 918 | static void viewer_load_settings(void) |
918 | { | 919 | { |
919 | int settings_fd, file_name_len, req_line_len, line_len; | 920 | int settings_fd, file_name_len, req_line_len, line_len; |
920 | char line[1024]; | 921 | char line[1024]; |
921 | 922 | ||
922 | settings_fd=rb->open(SETTINGS_FILE, O_RDONLY); | 923 | settings_fd=rb->open(SETTINGS_FILE, O_RDONLY); |
923 | if (settings_fd < 0) return; | 924 | if (settings_fd < 0) return; |
924 | 925 | ||
925 | file_name_len = rb->strlen(file_name); | 926 | file_name_len = rb->strlen(file_name); |
926 | req_line_len = file_name_len + sizeof(viewer_settings_string); | 927 | req_line_len = file_name_len + sizeof(viewer_settings_string); |
927 | while ((line_len = rb->read_line(settings_fd, line, sizeof(line))) > 0) { | 928 | while ((line_len = rb->read_line(settings_fd, line, sizeof(line))) > 0) { |
928 | if ((line_len == req_line_len) && | 929 | if ((line_len == req_line_len) && |
929 | (rb->strncasecmp(line, file_name, file_name_len) == 0)) { | 930 | (rb->strncasecmp(line, file_name, file_name_len) == 0)) { |
930 | /* found a match, load stored values */ | 931 | /* found a match, load stored values */ |
931 | viewer_settings_string *prefs = (void*) &line[file_name_len]; | 932 | viewer_settings_string *prefs = (void*) &line[file_name_len]; |
@@ -933,27 +934,27 @@ static void viewer_load_settings(void) | |||
933 | #ifdef HAVE_LCD_BITMAP | 934 | #ifdef HAVE_LCD_BITMAP |
934 | /* view mode will be initialized later anyways */ | 935 | /* view mode will be initialized later anyways */ |
935 | for (view_mode=0; view_mode<VIEW_MODES; ++view_mode) | 936 | for (view_mode=0; view_mode<VIEW_MODES; ++view_mode) |
936 | scrollbar_mode[view_mode] = | 937 | scrollbar_mode[view_mode] = |
937 | rb->atoi(prefs->scrollbar_mode[view_mode]); | 938 | rb->atoi(prefs->scrollbar_mode[view_mode]); |
938 | 939 | ||
939 | page_mode = rb->atoi(prefs->page_mode); | 940 | page_mode = rb->atoi(prefs->page_mode); |
940 | #endif | 941 | #endif |
941 | 942 | ||
942 | word_mode = rb->atoi(prefs->word_mode); | 943 | word_mode = rb->atoi(prefs->word_mode); |
943 | line_mode = rb->atoi(prefs->line_mode); | 944 | line_mode = rb->atoi(prefs->line_mode); |
944 | view_mode = rb->atoi(prefs->view_mode); | 945 | view_mode = rb->atoi(prefs->view_mode); |
945 | 946 | ||
946 | init_need_scrollbar(); | 947 | init_need_scrollbar(); |
947 | /* the following settings are safety checked | 948 | /* the following settings are safety checked |
948 | * (file may have changed on disk) | 949 | * (file may have changed on disk) |
949 | */ | 950 | */ |
950 | file_pos = rb->atoi(prefs->file_pos); /* should be atol() */ | 951 | file_pos = rb->atoi(prefs->file_pos); /* should be atol() */ |
951 | if (file_pos > file_size) { | 952 | if (file_pos > file_size) { |
952 | file_pos = 0; | 953 | file_pos = 0; |
953 | break; | 954 | break; |
954 | } | 955 | } |
955 | buffer_end = BUFFER_END(); /* Update whenever file_pos changes */ | 956 | buffer_end = BUFFER_END(); /* Update whenever file_pos changes */ |
956 | 957 | ||
957 | screen_top_ptr = buffer + rb->atoi(prefs->screen_top_ptr); | 958 | screen_top_ptr = buffer + rb->atoi(prefs->screen_top_ptr); |
958 | if (BUFFER_OOB(screen_top_ptr)) { | 959 | if (BUFFER_OOB(screen_top_ptr)) { |
959 | screen_top_ptr = buffer; | 960 | screen_top_ptr = buffer; |
@@ -962,16 +963,16 @@ static void viewer_load_settings(void) | |||
962 | } | 963 | } |
963 | } | 964 | } |
964 | rb->close(settings_fd); | 965 | rb->close(settings_fd); |
965 | 966 | ||
966 | fill_buffer(file_pos, buffer, BUFFER_SIZE); | 967 | fill_buffer(file_pos, buffer, BUFFER_SIZE); |
967 | } | 968 | } |
968 | 969 | ||
969 | static void viewer_save_settings(void) | 970 | static void viewer_save_settings(void) |
970 | { | 971 | { |
971 | int settings_fd, file_name_len, req_line_len, line_len; | 972 | int settings_fd, file_name_len, req_line_len, line_len; |
972 | char line[1024]; | 973 | char line[1024]; |
973 | viewer_settings_string prefs; | 974 | viewer_settings_string prefs; |
974 | 975 | ||
975 | settings_fd=rb->open(SETTINGS_FILE, O_RDWR | O_CREAT); | 976 | settings_fd=rb->open(SETTINGS_FILE, O_RDWR | O_CREAT); |
976 | DEBUGF("SETTINGS_FILE: %d\n", settings_fd); | 977 | DEBUGF("SETTINGS_FILE: %d\n", settings_fd); |
977 | if (settings_fd < 0) return; | 978 | if (settings_fd < 0) return; |
@@ -979,16 +980,16 @@ static void viewer_save_settings(void) | |||
979 | file_name_len = rb->strlen(file_name); | 980 | file_name_len = rb->strlen(file_name); |
980 | req_line_len = file_name_len + sizeof(viewer_settings_string); | 981 | req_line_len = file_name_len + sizeof(viewer_settings_string); |
981 | while ((line_len = rb->read_line(settings_fd, line, sizeof(line))) > 0) { | 982 | while ((line_len = rb->read_line(settings_fd, line, sizeof(line))) > 0) { |
982 | if ((line_len == req_line_len) && | 983 | if ((line_len == req_line_len) && |
983 | (rb->strncasecmp(line, file_name, file_name_len) == 0)) { | 984 | (rb->strncasecmp(line, file_name, file_name_len) == 0)) { |
984 | /* found a match, reposition file pointer to overwrite this line */ | 985 | /* found a match, reposition file pointer to overwrite this line */ |
985 | rb->lseek(settings_fd, -line_len, SEEK_CUR); | 986 | rb->lseek(settings_fd, -line_len, SEEK_CUR); |
986 | break; | 987 | break; |
987 | } | 988 | } |
988 | } | 989 | } |
989 | 990 | ||
990 | /* fill structure in order to prevent overwriting with 0s (snprintf | 991 | /* fill structure in order to prevent overwriting with 0s (snprintf |
991 | * intentionally overflows so that no terminating NULLs are written | 992 | * intentionally overflows so that no terminating NULLs are written |
992 | * to disk). */ | 993 | * to disk). */ |
993 | rb->snprintf(prefs.word_mode, 3, "%2d", word_mode); | 994 | rb->snprintf(prefs.word_mode, 3, "%2d", word_mode); |
994 | rb->snprintf(prefs.line_mode, 3, "%2d", line_mode); | 995 | rb->snprintf(prefs.line_mode, 3, "%2d", line_mode); |
@@ -997,18 +998,18 @@ static void viewer_save_settings(void) | |||
997 | rb->snprintf(prefs.screen_top_ptr, 12, "%11d", screen_top_ptr-buffer); | 998 | rb->snprintf(prefs.screen_top_ptr, 12, "%11d", screen_top_ptr-buffer); |
998 | #ifdef HAVE_LCD_BITMAP | 999 | #ifdef HAVE_LCD_BITMAP |
999 | /* view_mode is not needed anymore */ | 1000 | /* view_mode is not needed anymore */ |
1000 | for (view_mode=0; view_mode<VIEW_MODES; ++view_mode) | 1001 | for (view_mode=0; view_mode<VIEW_MODES; ++view_mode) |
1001 | rb->snprintf(prefs.scrollbar_mode[view_mode], 3, | 1002 | rb->snprintf(prefs.scrollbar_mode[view_mode], 3, |
1002 | "%2d", scrollbar_mode[view_mode]); | 1003 | "%2d", scrollbar_mode[view_mode]); |
1003 | 1004 | ||
1004 | rb->snprintf(prefs.page_mode, 3, "%2d", page_mode); | 1005 | rb->snprintf(prefs.page_mode, 3, "%2d", page_mode); |
1005 | #endif | 1006 | #endif |
1006 | prefs.EOL = '\n'; | 1007 | prefs.EOL = '\n'; |
1007 | 1008 | ||
1008 | rb->write(settings_fd, file_name, file_name_len); | 1009 | rb->write(settings_fd, file_name, file_name_len); |
1009 | rb->write(settings_fd, &prefs, sizeof(prefs)); | 1010 | rb->write(settings_fd, &prefs, sizeof(prefs)); |
1010 | rb->close(settings_fd); | 1011 | rb->close(settings_fd); |
1011 | } | 1012 | } |
1012 | 1013 | ||
1013 | static void viewer_exit(void *parameter) | 1014 | static void viewer_exit(void *parameter) |
1014 | { | 1015 | { |