Use a more natural guard for the callback definition
[kugel-rb.git] / apps / plugins / dict.c
blobbb98df7501543e60175bed01d548d2ed8e56a2a6
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Tomas Salfischberger
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "plugin.h"
24 PLUGIN_HEADER
26 /* screen info */
27 static int display_columns, display_lines;
29 /* Some lenghts */
30 #define WORDLEN 32 /* has to be the same in rdf2binary.c */
32 /* Struct packing */
33 #ifdef __GNUC__
34 #define STRUCT_PACKED __attribute__((packed))
35 #else
36 #define STRUCT_PACKED
37 #pragma pack (push, 2)
38 #endif
40 /* The word struct :) */
41 struct stWord
43 char word[WORDLEN];
44 long offset;
45 } STRUCT_PACKED;
47 /* A funtion to get width and height etc (from viewer.c) */
48 void init_screen(void)
50 #ifdef HAVE_LCD_BITMAP
51 int w,h;
53 rb->lcd_getstringsize("o", &w, &h);
54 display_lines = LCD_HEIGHT / h;
55 display_columns = LCD_WIDTH / w;
56 #else
58 display_lines = 2;
59 display_columns = 11;
60 #endif
63 /* global vars for pl_malloc() */
64 void *bufptr;
65 ssize_t bufleft;
67 /* simple function to "allocate" memory in pluginbuffer. */
68 void *pl_malloc(ssize_t size)
70 void *ptr;
71 ptr = bufptr;
73 if (bufleft < size)
75 return NULL;
77 else
79 bufptr += size;
80 bufleft -= size;
81 return ptr;
85 /* init function for pl_malloc() */
86 void pl_malloc_init(void)
88 bufptr = rb->plugin_get_buffer((size_t *)&bufleft);
91 /* for endian problems */
92 #ifdef ROCKBOX_BIG_ENDIAN
93 #define reverse(x) x
94 #else
95 long reverse (long N) {
96 unsigned char B[4];
97 B[0] = (N & 0x000000FF) >> 0;
98 B[1] = (N & 0x0000FF00) >> 8;
99 B[2] = (N & 0x00FF0000) >> 16;
100 B[3] = (N & 0xFF000000) >> 24;
101 return ((B[0] << 24) | (B[1] << 16) | (B[2] << 8) | (B[3] << 0));
103 #endif
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 #else
154 #define LP_QUIT BUTTON_OFF
155 #endif
157 /* data files */
158 #define DICT_INDEX PLUGIN_APPS_DIR "/dict.index"
159 #define DICT_DESC PLUGIN_APPS_DIR "/dict.desc"
161 /* the main plugin function */
162 enum plugin_status plugin_start(const void* parameter)
164 char searchword[WORDLEN]; /* word to search for */
165 char *description; /* pointer to description buffer */
166 char *output; /* pointer to output buffer */
167 char *ptr, *space;
168 struct stWord word; /* the struct to read into */
169 int fIndex, fData; /* files */
170 int filesize, high, low, probe;
171 int lines, len, outputted, next;
173 /* plugin stuff */
174 (void)parameter;
176 /* get screen info */
177 init_screen();
179 /* get pl_malloc() buffer ready. */
180 pl_malloc_init();
182 /* init description buffer (size is because we don't have scrolling)*/
183 description = (char *)pl_malloc(display_columns * display_lines);
184 if (description == NULL)
186 DEBUGF("Err: failed to allocate description buffer.");
187 return PLUGIN_ERROR;
190 /* init output buffer */
191 output = (char *)pl_malloc(display_columns);
192 if (output == NULL)
194 DEBUGF("Err: failed to allocate output buffer.");
195 return PLUGIN_ERROR;
198 /* "clear" input buffer */
199 searchword[0] = '\0';
201 rb->kbd_input(searchword, sizeof(searchword)); /* get the word to search */
203 fIndex = rb->open(DICT_INDEX, O_RDONLY); /* index file */
204 if (fIndex < 0)
206 DEBUGF("Err: Failed to open index file.\n");
207 rb->splash(HZ*2, "Failed to open index.");
208 return PLUGIN_ERROR;
211 filesize = rb->filesize(fIndex); /* get filesize */
213 DEBUGF("Filesize: %d bytes = %d words \n", filesize,
214 (filesize / (int)sizeof(struct stWord)));
216 /* for the searching algorithm */
217 high = filesize / sizeof( struct stWord );
218 low = -1;
220 while (high - low > 1)
222 probe = (high + low) / 2;
224 /* Jump to word pointed by probe, and read it. */
225 rb->lseek(fIndex, sizeof(struct stWord) * probe, SEEK_SET);
226 rb->read(fIndex, &word, sizeof(struct stWord));
228 /* jump according to the found word. */
229 if (rb->strcasecmp(searchword, word.word) < 0)
231 high = probe;
233 else
235 low = probe;
239 /* read in the word */
240 rb->lseek(fIndex, sizeof(struct stWord) * low, SEEK_SET);
241 rb->read(fIndex, &word, sizeof(struct stWord));
243 /* Check if we found something */
244 if (low == -1 || rb->strcasecmp(searchword, word.word) != 0)
246 DEBUGF("Not found.\n");
247 rb->splash(HZ*2, "Not found.");
248 rb->close(fIndex);
249 return PLUGIN_OK;
252 DEBUGF("Found %s at offset %ld\n", word.word, reverse(word.offset));
254 /* now open the description file */
255 fData = rb->open(DICT_DESC, O_RDONLY);
256 if (fData < 0)
258 DEBUGF("Err: Failed to open description file.\n");
259 rb->splash(HZ*2, "Failed to open descriptions.");
260 rb->close(fIndex);
261 return PLUGIN_ERROR;
264 /* seek to the right offset */
265 rb->lseek(fData, (off_t)reverse(word.offset), SEEK_SET);
267 /* Read in the description */
268 rb->read_line(fData, description, display_columns * display_lines);
270 /* And print it to debug. */
271 DEBUGF("Description: %s\n", description);
273 /* get pointer to first char */
274 ptr = description;
276 lines = 0;
277 outputted = 0;
278 len = rb->strlen(description);
280 /* clear screen */
281 rb->lcd_clear_display();
283 /* for large screens display the searched word. */
284 if(display_lines > 4)
286 rb->lcd_puts(0, lines, searchword);
287 lines++;
290 /* TODO: Scroll, or just stop when there are to much lines. */
291 while (1)
293 /* copy one lcd line */
294 rb->strlcpy(output, ptr, display_columns + 1);
296 /* typecast to kill a warning... */
297 if((int)rb->strlen(ptr) < display_columns)
299 rb->lcd_puts(0, lines, output);
300 lines++;
301 break;
305 /* get the last spacechar */
306 space = rb->strrchr(output, ' ');
308 if (space != NULL)
310 *space = '\0';
311 next = (space - (char*)output) + 1;
313 else
315 next = display_columns;
318 /* put the line on screen */
319 rb->lcd_puts(0, lines, output);
321 /* get output count */
322 outputted += rb->strlen(output);
324 if (outputted < len)
326 /* set pointer to the next part */
327 ptr += next;
328 lines++;
330 else
332 break;
335 rb->lcd_update();
337 /* wait for keypress */
338 while(rb->button_get(true) != LP_QUIT)
340 /* do nothing */
341 /* maybe define some keys for navigation here someday. */
344 rb->close(fIndex);
345 rb->close(fData);
346 return PLUGIN_OK;