diff options
-rw-r--r-- | apps/plugins/viewer.c | 85 |
1 files changed, 79 insertions, 6 deletions
diff --git a/apps/plugins/viewer.c b/apps/plugins/viewer.c index 9180fa6dcd..dd8ac3de14 100644 --- a/apps/plugins/viewer.c +++ b/apps/plugins/viewer.c | |||
@@ -23,7 +23,7 @@ | |||
23 | 23 | ||
24 | PLUGIN_HEADER | 24 | PLUGIN_HEADER |
25 | 25 | ||
26 | #define SETTINGS_FILE "/.rockbox/viewers/viewer.cfg" | 26 | #define SETTINGS_FILE "/.rockbox/viewers/viewer.dat" /* binary file, so dont use .cfg */ |
27 | 27 | ||
28 | #define WRAP_TRIM 44 /* Max number of spaces to trim (arbitrary) */ | 28 | #define WRAP_TRIM 44 /* Max number of spaces to trim (arbitrary) */ |
29 | #define MAX_COLUMNS 64 /* Max displayable string len (over-estimate) */ | 29 | #define MAX_COLUMNS 64 /* Max displayable string len (over-estimate) */ |
@@ -37,6 +37,8 @@ PLUGIN_HEADER | |||
37 | #define BOTTOM_SECTOR (buffer + 2*(SMALL_BLOCK_SIZE)) | 37 | #define BOTTOM_SECTOR (buffer + 2*(SMALL_BLOCK_SIZE)) |
38 | #define SCROLLBAR_WIDTH 6 | 38 | #define SCROLLBAR_WIDTH 6 |
39 | 39 | ||
40 | #define MAX_BOOKMARKED_FILES ((signed)BUFFER_SIZE/(signed)sizeof(struct bookmarked_file_info)) | ||
41 | |||
40 | /* Out-Of-Bounds test for any pointer to data in the buffer */ | 42 | /* Out-Of-Bounds test for any pointer to data in the buffer */ |
41 | #define BUFFER_OOB(p) ((p) < buffer || (p) >= buffer_end) | 43 | #define BUFFER_OOB(p) ((p) < buffer || (p) >= buffer_end) |
42 | 44 | ||
@@ -166,6 +168,11 @@ PLUGIN_HEADER | |||
166 | 168 | ||
167 | #endif | 169 | #endif |
168 | 170 | ||
171 | struct bookmarked_file_info { | ||
172 | long file_position; | ||
173 | int top_ptr_pos; | ||
174 | char filename[MAX_PATH]; | ||
175 | }; | ||
169 | struct preferences { | 176 | struct preferences { |
170 | enum { | 177 | enum { |
171 | WRAP=0, | 178 | WRAP=0, |
@@ -220,6 +227,9 @@ struct preferences { | |||
220 | } scroll_mode; | 227 | } scroll_mode; |
221 | 228 | ||
222 | int autoscroll_speed; | 229 | int autoscroll_speed; |
230 | /* stuff for the bookmarking */ | ||
231 | signed int bookmarked_files_count; | ||
232 | struct bookmarked_file_info bookmarks[MAX_BOOKMARKED_FILES]; | ||
223 | } prefs; | 233 | } prefs; |
224 | 234 | ||
225 | static unsigned char buffer[BUFFER_SIZE + 1]; | 235 | static unsigned char buffer[BUFFER_SIZE + 1]; |
@@ -244,6 +254,7 @@ static struct plugin_api* rb; | |||
244 | static struct font *pf; | 254 | static struct font *pf; |
245 | #endif | 255 | #endif |
246 | 256 | ||
257 | |||
247 | int glyph_width(int ch) | 258 | int glyph_width(int ch) |
248 | { | 259 | { |
249 | if (ch == 0) | 260 | if (ch == 0) |
@@ -993,11 +1004,26 @@ static void viewer_reset_settings(void) | |||
993 | prefs.scrollbar_mode = SB_OFF; | 1004 | prefs.scrollbar_mode = SB_OFF; |
994 | #endif | 1005 | #endif |
995 | prefs.autoscroll_speed = 1; | 1006 | prefs.autoscroll_speed = 1; |
1007 | prefs.bookmarked_files_count = 0; | ||
1008 | } | ||
1009 | static int get_bookmarked_position(char* filename) | ||
1010 | { | ||
1011 | int i; | ||
1012 | if (prefs.bookmarked_files_count > MAX_BOOKMARKED_FILES) | ||
1013 | prefs.bookmarked_files_count = MAX_BOOKMARKED_FILES; | ||
1014 | |||
1015 | for (i=0; i < prefs.bookmarked_files_count; i++) | ||
1016 | { | ||
1017 | if (!rb->strcmp(filename, prefs.bookmarks[i].filename)) | ||
1018 | return i; | ||
1019 | } | ||
1020 | return -1; | ||
996 | } | 1021 | } |
997 | 1022 | ||
998 | static void viewer_load_settings(void) /* same name as global, but not the same file.. */ | 1023 | static void viewer_load_settings(void) /* same name as global, but not the same file.. */ |
999 | { | 1024 | { |
1000 | int settings_fd; | 1025 | int settings_fd; |
1026 | int bookmarked_pos; | ||
1001 | 1027 | ||
1002 | settings_fd=rb->open(SETTINGS_FILE, O_RDONLY); | 1028 | settings_fd=rb->open(SETTINGS_FILE, O_RDONLY); |
1003 | if (settings_fd < 0) | 1029 | if (settings_fd < 0) |
@@ -1016,23 +1042,70 @@ static void viewer_load_settings(void) /* same name as global, but not the same | |||
1016 | 1042 | ||
1017 | init_need_scrollbar(); | 1043 | init_need_scrollbar(); |
1018 | 1044 | ||
1019 | file_pos=0; | 1045 | bookmarked_pos = get_bookmarked_position(file_name); |
1046 | |||
1047 | if (bookmarked_pos < 0) | ||
1048 | { | ||
1049 | file_pos = 0; | ||
1050 | screen_top_ptr = buffer; | ||
1051 | } | ||
1052 | else | ||
1053 | { | ||
1054 | file_pos = prefs.bookmarks[bookmarked_pos].file_position; | ||
1055 | screen_top_ptr = buffer + prefs.bookmarks[bookmarked_pos].top_ptr_pos; | ||
1056 | } | ||
1020 | buffer_end = BUFFER_END(); /* Update whenever file_pos changes */ | 1057 | buffer_end = BUFFER_END(); /* Update whenever file_pos changes */ |
1021 | 1058 | ||
1022 | screen_top_ptr = buffer; | ||
1023 | if (BUFFER_OOB(screen_top_ptr)) { | 1059 | if (BUFFER_OOB(screen_top_ptr)) { |
1024 | screen_top_ptr = buffer; | 1060 | screen_top_ptr = buffer; |
1025 | } | 1061 | } |
1026 | 1062 | ||
1027 | fill_buffer(file_pos, buffer, BUFFER_SIZE); | 1063 | fill_buffer(file_pos, buffer, BUFFER_SIZE); |
1028 | } | 1064 | } |
1065 | void viewer_rearrange_bookmarks(void) | ||
1066 | { | ||
1067 | int this_item = -1; | ||
1068 | struct bookmarked_file_info *buf = (struct bookmarked_file_info*)buffer; /* grab the text buffer */ | ||
1069 | /* copy the bookmakred file list to the text buffer, | ||
1070 | put this file at the top, then dump the filenames back and write to the fd */ | ||
1071 | rb->memcpy(buf,&prefs.bookmarks, sizeof(struct bookmarked_file_info)*MAX_BOOKMARKED_FILES); | ||
1072 | this_item = get_bookmarked_position(file_name); | ||
1073 | |||
1074 | /* regardless of its position in the list, put it on top */ | ||
1075 | rb->strcpy(prefs.bookmarks[0].filename, file_name); | ||
1076 | prefs.bookmarks[0].file_position = file_pos; | ||
1077 | prefs.bookmarks[0].top_ptr_pos = screen_top_ptr - buffer; | ||
1078 | |||
1079 | if (this_item < 0) | ||
1080 | { | ||
1081 | /* not in the list yet, so put it first then dump the list back */ | ||
1082 | if (prefs.bookmarked_files_count == MAX_BOOKMARKED_FILES) | ||
1083 | { | ||
1084 | /* bookmark list is full! dump the lru file*/ | ||
1085 | rb->memcpy(&prefs.bookmarks[1],buf, sizeof(struct bookmarked_file_info)*(MAX_BOOKMARKED_FILES-1)); | ||
1086 | } | ||
1087 | else | ||
1088 | { | ||
1089 | rb->memcpy(&prefs.bookmarks[1],buf, sizeof(struct bookmarked_file_info)*(prefs.bookmarked_files_count)); | ||
1090 | prefs.bookmarked_files_count++; | ||
1091 | } | ||
1092 | } | ||
1093 | else | ||
1094 | { | ||
1095 | /* dump up to this item, skip 1 then dummp the rest */ | ||
1096 | rb->memcpy(&prefs.bookmarks[1],buf, sizeof(struct bookmarked_file_info)*(this_item)); | ||
1097 | rb->memcpy(&prefs.bookmarks[this_item+1],&buf[this_item+1], | ||
1098 | sizeof(struct bookmarked_file_info)*(MAX_BOOKMARKED_FILES-this_item-1)); | ||
1099 | } | ||
1100 | } | ||
1029 | 1101 | ||
1030 | static void viewer_save_settings(void)/* same name as global, but not the same file.. */ | 1102 | static void viewer_save_settings(void)/* same name as global, but not the same file.. */ |
1031 | { | 1103 | { |
1032 | int settings_fd; | 1104 | int settings_fd; |
1033 | |||
1034 | settings_fd = rb->creat(SETTINGS_FILE, O_WRONLY); /* create the settings file */ | 1105 | settings_fd = rb->creat(SETTINGS_FILE, O_WRONLY); /* create the settings file */ |
1035 | 1106 | ||
1107 | viewer_rearrange_bookmarks(); | ||
1108 | |||
1036 | rb->write (settings_fd, &prefs, sizeof(struct preferences)); | 1109 | rb->write (settings_fd, &prefs, sizeof(struct preferences)); |
1037 | rb->close(settings_fd); | 1110 | rb->close(settings_fd); |
1038 | } | 1111 | } |
@@ -1251,7 +1324,7 @@ enum plugin_status plugin_start(struct plugin_api* api, void* file) | |||
1251 | 1324 | ||
1252 | viewer_reset_settings(); /* load defaults first */ | 1325 | viewer_reset_settings(); /* load defaults first */ |
1253 | viewer_load_settings(); /* .. then try to load from disk */ | 1326 | viewer_load_settings(); /* .. then try to load from disk */ |
1254 | 1327 | ||
1255 | viewer_draw(col); | 1328 | viewer_draw(col); |
1256 | 1329 | ||
1257 | while (!done) { | 1330 | while (!done) { |
@@ -1265,7 +1338,7 @@ enum plugin_status plugin_start(struct plugin_api* api, void* file) | |||
1265 | old_tick = *rb->current_tick; | 1338 | old_tick = *rb->current_tick; |
1266 | } | 1339 | } |
1267 | } | 1340 | } |
1268 | 1341 | ||
1269 | button = rb->button_get_w_tmo(HZ/10); | 1342 | button = rb->button_get_w_tmo(HZ/10); |
1270 | switch (button) { | 1343 | switch (button) { |
1271 | case VIEWER_MENU: | 1344 | case VIEWER_MENU: |