1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 Tomas Salfischberger
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
24 /* save the plugin api pointer. */
25 static struct plugin_api
* rb
;
27 static int display_columns
, display_lines
;
30 #define WORDLEN 32 /* has to be the same in rdf2binary.c */
34 #define STRUCT_PACKED __attribute__((packed))
37 #pragma pack (push, 2)
40 /* The word struct :) */
47 /* A funtion to get width and height etc (from viewer.c) */
48 void init_screen(void)
50 #ifdef HAVE_LCD_BITMAP
53 rb
->lcd_getstringsize("o", &w
, &h
);
54 display_lines
= LCD_HEIGHT
/ h
;
55 display_columns
= LCD_WIDTH
/ w
;
63 /* global vars for pl_malloc() */
67 /* simple function to "allocate" memory in pluginbuffer. */
68 void *pl_malloc(ssize_t size
)
84 /* init function for pl_malloc() */
85 void pl_malloc_init(void)
87 bufptr
= rb
->plugin_get_buffer((size_t *)&bufleft
);
90 /* for endian problems */
91 #ifdef ROCKBOX_BIG_ENDIAN
94 long reverse (long N
) {
96 B
[0] = (N
& 0x000000FF) >> 0;
97 B
[1] = (N
& 0x0000FF00) >> 8;
98 B
[2] = (N
& 0x00FF0000) >> 16;
99 B
[3] = (N
& 0xFF000000) >> 24;
100 return ((B
[0] << 24) | (B
[1] << 16) | (B
[2] << 8) | (B
[3] << 0));
104 /* Button definitions */
105 #if CONFIG_KEYPAD == PLAYER_PAD
106 #define LP_QUIT BUTTON_STOP
107 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
108 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
109 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
110 #define LP_QUIT BUTTON_MENU
111 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
112 #define LP_QUIT BUTTON_PLAY
113 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
114 #define LP_QUIT BUTTON_POWER
115 #elif CONFIG_KEYPAD == GIGABEAT_PAD
116 #define LP_QUIT BUTTON_POWER
117 #elif (CONFIG_KEYPAD == SANSA_E200_PAD) || \
118 (CONFIG_KEYPAD == SANSA_C200_PAD)
119 #define LP_QUIT BUTTON_POWER
120 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
121 #define LP_QUIT BUTTON_POWER
122 #elif CONFIG_KEYPAD == MROBE500_PAD
123 #define LP_QUIT BUTTON_POWER
125 #define LP_QUIT BUTTON_OFF
129 #define DICT_INDEX ROCKBOX_DIR "/rocks/apps/dict.index"
130 #define DICT_DESC ROCKBOX_DIR "/rocks/apps/dict.desc"
132 /* the main plugin function */
133 enum plugin_status
plugin_start(struct plugin_api
* api
, void* parameter
)
135 char searchword
[WORDLEN
]; /* word to search for */
136 char *description
; /* pointer to description buffer */
137 char *output
; /* pointer to output buffer */
139 struct stWord word
; /* the struct to read into */
140 int fIndex
, fData
; /* files */
141 int filesize
, high
, low
, probe
;
142 int lines
, len
, outputted
, next
;
148 /* get screen info */
151 /* get pl_malloc() buffer ready. */
154 /* init description buffer (size is because we don't have scrolling)*/
155 description
= (char *)pl_malloc(display_columns
* display_lines
);
156 if (description
== NULL
)
158 DEBUGF("Err: failed to allocate description buffer.");
162 /* init output buffer */
163 output
= (char *)pl_malloc(display_columns
);
166 DEBUGF("Err: failed to allocate output buffer.");
170 /* "clear" input buffer */
171 searchword
[0] = '\0';
173 rb
->kbd_input(searchword
, sizeof(searchword
)); /* get the word to search */
175 fIndex
= rb
->open(DICT_INDEX
, O_RDONLY
); /* index file */
178 DEBUGF("Err: Failed to open index file.\n");
179 rb
->splash(HZ
*2, "Failed to open index.");
183 filesize
= rb
->filesize(fIndex
); /* get filesize */
185 DEBUGF("Filesize: %d bytes = %d words \n", filesize
,
186 (filesize
/ (int)sizeof(struct stWord
)));
188 /* for the searching algorithm */
189 high
= filesize
/ sizeof( struct stWord
);
192 while (high
- low
> 1)
194 probe
= (high
+ low
) / 2;
196 /* Jump to word pointed by probe, and read it. */
197 rb
->lseek(fIndex
, sizeof(struct stWord
) * probe
, SEEK_SET
);
198 rb
->read(fIndex
, &word
, sizeof(struct stWord
));
200 /* jump according to the found word. */
201 if (rb
->strcasecmp(searchword
, word
.word
) < 0)
211 /* read in the word */
212 rb
->lseek(fIndex
, sizeof(struct stWord
) * low
, SEEK_SET
);
213 rb
->read(fIndex
, &word
, sizeof(struct stWord
));
215 /* Check if we found something */
216 if (low
== -1 || rb
->strcasecmp(searchword
, word
.word
) != 0)
218 DEBUGF("Not found.\n");
219 rb
->splash(HZ
*2, "Not found.");
224 DEBUGF("Found %s at offset %ld\n", word
.word
, reverse(word
.offset
));
226 /* now open the description file */
227 fData
= rb
->open(DICT_DESC
, O_RDONLY
);
230 DEBUGF("Err: Failed to open description file.\n");
231 rb
->splash(HZ
*2, "Failed to open descriptions.");
236 /* seek to the right offset */
237 rb
->lseek(fData
, (off_t
)reverse(word
.offset
), SEEK_SET
);
239 /* Read in the description */
240 rb
->read_line(fData
, description
, display_columns
* display_lines
);
242 /* And print it to debug. */
243 DEBUGF("Description: %s\n", description
);
245 /* get pointer to first char */
250 len
= rb
->strlen(description
);
253 rb
->lcd_clear_display();
255 /* for large screens display the searched word. */
256 if(display_lines
> 4)
258 rb
->lcd_puts(0, lines
, searchword
);
262 /* TODO: Scroll, or just stop when there are to much lines. */
265 /* copy one lcd line */
266 rb
->strncpy(output
, ptr
, display_columns
);
267 output
[display_columns
] = '\0';
269 /* typecast to kill a warning... */
270 if((int)rb
->strlen(ptr
) < display_columns
)
272 rb
->lcd_puts(0, lines
, output
);
278 /* get the last spacechar */
279 space
= rb
->strrchr(output
, ' ');
284 next
= (space
- (char*)output
) + 1;
288 next
= display_columns
;
291 /* put the line on screen */
292 rb
->lcd_puts(0, lines
, output
);
294 /* get output count */
295 outputted
+= rb
->strlen(output
);
299 /* set pointer to the next part */
310 /* wait for keypress */
311 while(rb
->button_get(true) != LP_QUIT
)
314 /* maybe define some keys for navigation here someday. */