diff options
Diffstat (limited to 'apps/plugins/dict.c')
-rw-r--r-- | apps/plugins/dict.c | 212 |
1 files changed, 18 insertions, 194 deletions
diff --git a/apps/plugins/dict.c b/apps/plugins/dict.c index 8c262923b7..13fe8e3cc6 100644 --- a/apps/plugins/dict.c +++ b/apps/plugins/dict.c | |||
@@ -20,11 +20,11 @@ | |||
20 | ****************************************************************************/ | 20 | ****************************************************************************/ |
21 | 21 | ||
22 | #include "plugin.h" | 22 | #include "plugin.h" |
23 | #include "lib/simple_viewer.h" | ||
23 | 24 | ||
24 | PLUGIN_HEADER | 25 | PLUGIN_HEADER |
25 | 26 | ||
26 | /* screen info */ | 27 | #define MIN_DESC_BUF_SIZE 0x400 /* arbitrary minimum size for description */ |
27 | static int display_columns, display_lines; | ||
28 | 28 | ||
29 | /* Some lenghts */ | 29 | /* Some lenghts */ |
30 | #define WORDLEN 32 /* has to be the same in rdf2binary.c */ | 30 | #define WORDLEN 32 /* has to be the same in rdf2binary.c */ |
@@ -44,50 +44,6 @@ struct stWord | |||
44 | long offset; | 44 | long offset; |
45 | } STRUCT_PACKED; | 45 | } STRUCT_PACKED; |
46 | 46 | ||
47 | /* A funtion to get width and height etc (from viewer.c) */ | ||
48 | void init_screen(void) | ||
49 | { | ||
50 | #ifdef HAVE_LCD_BITMAP | ||
51 | int w,h; | ||
52 | |||
53 | rb->lcd_getstringsize("o", &w, &h); | ||
54 | display_lines = LCD_HEIGHT / h; | ||
55 | display_columns = LCD_WIDTH / w; | ||
56 | #else | ||
57 | |||
58 | display_lines = 2; | ||
59 | display_columns = 11; | ||
60 | #endif | ||
61 | } | ||
62 | |||
63 | /* global vars for pl_malloc() */ | ||
64 | void *bufptr; | ||
65 | size_t bufleft; | ||
66 | |||
67 | /* simple function to "allocate" memory in pluginbuffer. */ | ||
68 | void *pl_malloc(size_t size) | ||
69 | { | ||
70 | void *ptr; | ||
71 | ptr = bufptr; | ||
72 | |||
73 | if (bufleft < size) | ||
74 | { | ||
75 | return NULL; | ||
76 | } | ||
77 | else | ||
78 | { | ||
79 | bufptr += size; | ||
80 | bufleft -= size; | ||
81 | return ptr; | ||
82 | } | ||
83 | } | ||
84 | |||
85 | /* init function for pl_malloc() */ | ||
86 | void pl_malloc_init(void) | ||
87 | { | ||
88 | bufptr = rb->plugin_get_buffer(&bufleft); | ||
89 | } | ||
90 | |||
91 | /* for endian problems */ | 47 | /* for endian problems */ |
92 | #ifdef ROCKBOX_BIG_ENDIAN | 48 | #ifdef ROCKBOX_BIG_ENDIAN |
93 | #define reverse(x) x | 49 | #define reverse(x) x |
@@ -102,59 +58,6 @@ long reverse (long N) { | |||
102 | } | 58 | } |
103 | #endif | 59 | #endif |
104 | 60 | ||
105 | /* Button definitions */ | ||
106 | #if CONFIG_KEYPAD == PLAYER_PAD | ||
107 | #define LP_QUIT BUTTON_STOP | ||
108 | #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \ | ||
109 | (CONFIG_KEYPAD == IPOD_3G_PAD) || \ | ||
110 | (CONFIG_KEYPAD == IPOD_1G2G_PAD) | ||
111 | #define LP_QUIT BUTTON_MENU | ||
112 | #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD | ||
113 | #define LP_QUIT BUTTON_PLAY | ||
114 | #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD | ||
115 | #define LP_QUIT BUTTON_POWER | ||
116 | #elif CONFIG_KEYPAD == GIGABEAT_PAD | ||
117 | #define LP_QUIT BUTTON_POWER | ||
118 | #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \ | ||
119 | (CONFIG_KEYPAD == SANSA_C200_PAD) || \ | ||
120 | (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \ | ||
121 | (CONFIG_KEYPAD == SANSA_M200_PAD) | ||
122 | #define LP_QUIT BUTTON_POWER | ||
123 | #elif (CONFIG_KEYPAD == SANSA_FUZE_PAD) | ||
124 | #define LP_QUIT (BUTTON_HOME|BUTTON_REPEAT) | ||
125 | #elif CONFIG_KEYPAD == IRIVER_H10_PAD | ||
126 | #define LP_QUIT BUTTON_POWER | ||
127 | #elif CONFIG_KEYPAD == MROBE500_PAD | ||
128 | #define LP_QUIT BUTTON_POWER | ||
129 | #elif CONFIG_KEYPAD == MROBE100_PAD | ||
130 | #define LP_QUIT BUTTON_POWER | ||
131 | #elif CONFIG_KEYPAD == GIGABEAT_S_PAD | ||
132 | #define LP_QUIT BUTTON_BACK | ||
133 | #elif CONFIG_KEYPAD == IAUDIO_M3_PAD | ||
134 | #define LP_QUIT BUTTON_RC_REC | ||
135 | #elif CONFIG_KEYPAD == COWON_D2_PAD | ||
136 | #define LP_QUIT BUTTON_POWER | ||
137 | #elif CONFIG_KEYPAD == IAUDIO67_PAD | ||
138 | #define LP_QUIT BUTTON_POWER | ||
139 | #elif CONFIG_KEYPAD == CREATIVEZVM_PAD | ||
140 | #define LP_QUIT BUTTON_BACK | ||
141 | #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD | ||
142 | #define LP_QUIT BUTTON_POWER | ||
143 | #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD | ||
144 | #define LP_QUIT BUTTON_POWER | ||
145 | #elif CONFIG_KEYPAD == ONDAVX747_PAD | ||
146 | #define LP_QUIT BUTTON_POWER | ||
147 | #elif CONFIG_KEYPAD == ONDAVX777_PAD | ||
148 | #define LP_QUIT BUTTON_POWER | ||
149 | #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD | ||
150 | #define LP_QUIT BUTTON_LEFT | ||
151 | #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD | ||
152 | #define LP_QUIT BUTTON_CANCEL | ||
153 | #elif CONFIG_KEYPAD == MPIO_HD200_PAD | ||
154 | #define LP_QUIT (BUTTON_REC|BUTTON_PLAY) | ||
155 | #else | ||
156 | #define LP_QUIT BUTTON_OFF | ||
157 | #endif | ||
158 | 61 | ||
159 | /* data files */ | 62 | /* data files */ |
160 | #define DICT_INDEX PLUGIN_APPS_DIR "/dict.index" | 63 | #define DICT_INDEX PLUGIN_APPS_DIR "/dict.index" |
@@ -165,42 +68,32 @@ enum plugin_status plugin_start(const void* parameter) | |||
165 | { | 68 | { |
166 | char searchword[WORDLEN]; /* word to search for */ | 69 | char searchword[WORDLEN]; /* word to search for */ |
167 | char *description; /* pointer to description buffer */ | 70 | char *description; /* pointer to description buffer */ |
168 | char *output; /* pointer to output buffer */ | ||
169 | char *ptr, *space; | ||
170 | struct stWord word; /* the struct to read into */ | 71 | struct stWord word; /* the struct to read into */ |
171 | int fIndex, fData; /* files */ | 72 | int fIndex, fData; /* files */ |
172 | int filesize, high, low, probe; | 73 | int filesize, high, low, probe; |
173 | int lines, len, outputted, next; | 74 | char *buffer; |
75 | size_t buffer_size; | ||
174 | 76 | ||
175 | /* plugin stuff */ | 77 | /* plugin stuff */ |
176 | (void)parameter; | 78 | (void)parameter; |
177 | 79 | ||
178 | /* get screen info */ | 80 | /* allocate buffer. */ |
179 | init_screen(); | 81 | buffer = rb->plugin_get_buffer(&buffer_size); |
180 | 82 | if (buffer == NULL || buffer_size < MIN_DESC_BUF_SIZE) | |
181 | /* get pl_malloc() buffer ready. */ | ||
182 | pl_malloc_init(); | ||
183 | |||
184 | /* init description buffer (size is because we don't have scrolling)*/ | ||
185 | description = (char *)pl_malloc(display_columns * display_lines); | ||
186 | if (description == NULL) | ||
187 | { | 83 | { |
188 | DEBUGF("Err: failed to allocate description buffer."); | 84 | DEBUGF("Err: Failed to allocate buffer.\n"); |
85 | rb->splash(HZ*2, "Failed to allocate buffer."); | ||
189 | return PLUGIN_ERROR; | 86 | return PLUGIN_ERROR; |
190 | } | 87 | } |
191 | 88 | ||
192 | /* init output buffer */ | 89 | description = buffer; |
193 | output = (char *)pl_malloc(display_columns); | ||
194 | if (output == NULL) | ||
195 | { | ||
196 | DEBUGF("Err: failed to allocate output buffer."); | ||
197 | return PLUGIN_ERROR; | ||
198 | } | ||
199 | 90 | ||
200 | /* "clear" input buffer */ | 91 | /* "clear" input buffer */ |
201 | searchword[0] = '\0'; | 92 | searchword[0] = '\0'; |
202 | 93 | ||
203 | rb->kbd_input(searchword, sizeof(searchword)); /* get the word to search */ | 94 | /* get the word to search */ |
95 | if (rb->kbd_input(searchword, sizeof(searchword)) < 0) | ||
96 | return PLUGIN_OK; /* input cancelled */ | ||
204 | 97 | ||
205 | fIndex = rb->open(DICT_INDEX, O_RDONLY); /* index file */ | 98 | fIndex = rb->open(DICT_INDEX, O_RDONLY); /* index file */ |
206 | if (fIndex < 0) | 99 | if (fIndex < 0) |
@@ -241,13 +134,13 @@ enum plugin_status plugin_start(const void* parameter) | |||
241 | /* read in the word */ | 134 | /* read in the word */ |
242 | rb->lseek(fIndex, sizeof(struct stWord) * low, SEEK_SET); | 135 | rb->lseek(fIndex, sizeof(struct stWord) * low, SEEK_SET); |
243 | rb->read(fIndex, &word, sizeof(struct stWord)); | 136 | rb->read(fIndex, &word, sizeof(struct stWord)); |
137 | rb->close(fIndex); | ||
244 | 138 | ||
245 | /* Check if we found something */ | 139 | /* Check if we found something */ |
246 | if (low == -1 || rb->strcasecmp(searchword, word.word) != 0) | 140 | if (low == -1 || rb->strcasecmp(searchword, word.word) != 0) |
247 | { | 141 | { |
248 | DEBUGF("Not found.\n"); | 142 | DEBUGF("Not found.\n"); |
249 | rb->splash(HZ*2, "Not found."); | 143 | rb->splash(HZ*2, "Not found."); |
250 | rb->close(fIndex); | ||
251 | return PLUGIN_OK; | 144 | return PLUGIN_OK; |
252 | } | 145 | } |
253 | 146 | ||
@@ -259,7 +152,6 @@ enum plugin_status plugin_start(const void* parameter) | |||
259 | { | 152 | { |
260 | DEBUGF("Err: Failed to open description file.\n"); | 153 | DEBUGF("Err: Failed to open description file.\n"); |
261 | rb->splash(HZ*2, "Failed to open descriptions."); | 154 | rb->splash(HZ*2, "Failed to open descriptions."); |
262 | rb->close(fIndex); | ||
263 | return PLUGIN_ERROR; | 155 | return PLUGIN_ERROR; |
264 | } | 156 | } |
265 | 157 | ||
@@ -267,83 +159,15 @@ enum plugin_status plugin_start(const void* parameter) | |||
267 | rb->lseek(fData, (off_t)reverse(word.offset), SEEK_SET); | 159 | rb->lseek(fData, (off_t)reverse(word.offset), SEEK_SET); |
268 | 160 | ||
269 | /* Read in the description */ | 161 | /* Read in the description */ |
270 | rb->read_line(fData, description, display_columns * display_lines); | 162 | rb->read_line(fData, description, buffer_size); |
271 | 163 | ||
272 | /* And print it to debug. */ | 164 | /* And print it to debug. */ |
273 | DEBUGF("Description: %s\n", description); | 165 | DEBUGF("Description: %s\n", description); |
274 | 166 | ||
275 | /* get pointer to first char */ | 167 | rb->close(fData); |
276 | ptr = description; | ||
277 | |||
278 | lines = 0; | ||
279 | outputted = 0; | ||
280 | len = rb->strlen(description); | ||
281 | |||
282 | /* clear screen */ | ||
283 | rb->lcd_clear_display(); | ||
284 | |||
285 | /* for large screens display the searched word. */ | ||
286 | if(display_lines > 4) | ||
287 | { | ||
288 | rb->lcd_puts(0, lines, searchword); | ||
289 | lines++; | ||
290 | } | ||
291 | |||
292 | /* TODO: Scroll, or just stop when there are to much lines. */ | ||
293 | while (1) | ||
294 | { | ||
295 | /* copy one lcd line */ | ||
296 | rb->strlcpy(output, ptr, display_columns + 1); | ||
297 | |||
298 | /* typecast to kill a warning... */ | ||
299 | if((int)rb->strlen(ptr) < display_columns) | ||
300 | { | ||
301 | rb->lcd_puts(0, lines, output); | ||
302 | lines++; | ||
303 | break; | ||
304 | } | ||
305 | |||
306 | |||
307 | /* get the last spacechar */ | ||
308 | space = rb->strrchr(output, ' '); | ||
309 | |||
310 | if (space != NULL) | ||
311 | { | ||
312 | *space = '\0'; | ||
313 | next = (space - (char*)output) + 1; | ||
314 | } | ||
315 | else | ||
316 | { | ||
317 | next = display_columns; | ||
318 | } | ||
319 | |||
320 | /* put the line on screen */ | ||
321 | rb->lcd_puts(0, lines, output); | ||
322 | |||
323 | /* get output count */ | ||
324 | outputted += rb->strlen(output); | ||
325 | |||
326 | if (outputted < len) | ||
327 | { | ||
328 | /* set pointer to the next part */ | ||
329 | ptr += next; | ||
330 | lines++; | ||
331 | } | ||
332 | else | ||
333 | { | ||
334 | break; | ||
335 | } | ||
336 | } | ||
337 | rb->lcd_update(); | ||
338 | 168 | ||
339 | /* wait for keypress */ | 169 | /* display description. */ |
340 | while(rb->button_get(true) != LP_QUIT) | 170 | view_text(searchword, description); |
341 | { | ||
342 | /* do nothing */ | ||
343 | /* maybe define some keys for navigation here someday. */ | ||
344 | } | ||
345 | 171 | ||
346 | rb->close(fIndex); | ||
347 | rb->close(fData); | ||
348 | return PLUGIN_OK; | 172 | return PLUGIN_OK; |
349 | } | 173 | } |