Fix for ff/rw in long MP3 files.
[kugel-rb.git] / apps / plugin.c
blob6c018067ea3da8e545cc3b0251d2393ac7c1dd97
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Björn Stenberg
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 ****************************************************************************/
19 #include <stdbool.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <atoi.h>
23 #include <timefuncs.h>
24 #include <ctype.h>
25 #include "debug.h"
26 #include "button.h"
27 #include "lcd.h"
28 #include "dir.h"
29 #include "file.h"
30 #include "kernel.h"
31 #include "sprintf.h"
32 #include "logf.h"
33 #include "screens.h"
34 #include "misc.h"
35 #include "mas.h"
36 #include "plugin.h"
37 #include "lang.h"
38 #include "keyboard.h"
39 #include "mpeg.h"
40 #include "buffer.h"
41 #include "mp3_playback.h"
42 #include "backlight.h"
43 #include "ata.h"
44 #include "talk.h"
45 #include "mp3data.h"
46 #include "powermgmt.h"
47 #include "system.h"
48 #include "timer.h"
49 #include "sound.h"
50 #include "database.h"
51 #if (CONFIG_HWCODEC == MASNONE)
52 #include "pcm_playback.h"
53 #endif
55 #ifdef HAVE_LCD_BITMAP
56 #include "peakmeter.h"
57 #include "widgets.h"
58 #include "bmp.h"
59 #endif
61 #ifdef HAVE_REMOTE_LCD
62 #include "lcd-remote.h"
63 #endif
65 #ifdef SIMULATOR
66 static unsigned char pluginbuf[PLUGIN_BUFFER_SIZE];
67 void *sim_plugin_load(char *plugin, int *fd);
68 void sim_plugin_close(int fd);
69 #else
70 #define sim_plugin_close(x)
71 extern unsigned char pluginbuf[];
72 #include "bitswap.h"
73 #endif
75 /* for actual plugins only, not for codecs */
76 static bool plugin_loaded = false;
77 static int plugin_size = 0;
78 static void (*pfn_tsr_exit)(void) = NULL; /* TSR exit callback */
80 static int plugin_test(int api_version, int model, int memsize);
82 static const struct plugin_api rockbox_api = {
83 PLUGIN_API_VERSION,
85 plugin_test,
87 /* lcd */
88 lcd_set_contrast,
89 lcd_clear_display,
90 lcd_puts,
91 lcd_puts_scroll,
92 lcd_stop_scroll,
93 #ifdef HAVE_LCD_CHARCELLS
94 lcd_define_pattern,
95 lcd_get_locked_pattern,
96 lcd_unlock_pattern,
97 lcd_putc,
98 lcd_put_cursor,
99 lcd_remove_cursor,
100 PREFIX(lcd_icon),
101 #else
102 #ifndef SIMULATOR
103 lcd_roll,
104 #endif
105 lcd_set_drawmode,
106 lcd_get_drawmode,
107 lcd_setfont,
108 lcd_getstringsize,
109 lcd_drawpixel,
110 lcd_drawline,
111 lcd_hline,
112 lcd_vline,
113 lcd_drawrect,
114 lcd_fillrect,
115 lcd_mono_bitmap_part,
116 lcd_mono_bitmap,
117 #if LCD_DEPTH > 1
118 lcd_set_foreground,
119 lcd_get_foreground,
120 lcd_set_background,
121 lcd_get_background,
122 lcd_bitmap_part,
123 lcd_bitmap,
124 #endif
125 lcd_putsxy,
126 lcd_puts_style,
127 lcd_puts_scroll_style,
128 &lcd_framebuffer[0][0],
129 lcd_blit,
130 lcd_update,
131 lcd_update_rect,
132 scrollbar,
133 checkbox,
134 font_get,
135 font_getstringsize,
136 #endif
137 backlight_on,
138 backlight_off,
139 backlight_set_timeout,
140 splash,
141 #ifdef HAVE_REMOTE_LCD
142 /* remote lcd */
143 lcd_remote_set_contrast,
144 lcd_remote_clear_display,
145 lcd_remote_puts,
146 lcd_remote_puts_scroll,
147 lcd_remote_stop_scroll,
148 #ifndef SIMULATOR
149 lcd_remote_roll,
150 #endif
151 lcd_remote_set_drawmode,
152 lcd_remote_get_drawmode,
153 lcd_remote_setfont,
154 lcd_remote_getstringsize,
155 lcd_remote_drawpixel,
156 lcd_remote_drawline,
157 lcd_remote_hline,
158 lcd_remote_vline,
159 lcd_remote_drawrect,
160 lcd_remote_fillrect,
161 lcd_remote_mono_bitmap_part,
162 lcd_remote_mono_bitmap,
163 lcd_remote_putsxy,
164 lcd_remote_puts_style,
165 lcd_remote_puts_scroll_style,
166 &lcd_remote_framebuffer[0][0],
167 lcd_remote_update,
168 lcd_remote_update_rect,
170 lcd_remote_backlight_on,
171 lcd_remote_backlight_off,
172 #endif
173 /* button */
174 button_get,
175 button_get_w_tmo,
176 button_status,
177 button_clear_queue,
178 #if CONFIG_KEYPAD == IRIVER_H100_PAD
179 button_hold,
180 #endif
182 /* file */
183 (open_func)PREFIX(open),
184 close,
185 (read_func)read,
186 PREFIX(lseek),
187 (creat_func)PREFIX(creat),
188 (write_func)write,
189 PREFIX(remove),
190 PREFIX(rename),
191 PREFIX(ftruncate),
192 PREFIX(filesize),
193 fdprintf,
194 read_line,
195 settings_parseline,
196 #ifndef SIMULATOR
197 ata_sleep,
198 #endif
200 /* dir */
201 PREFIX(opendir),
202 PREFIX(closedir),
203 PREFIX(readdir),
204 PREFIX(mkdir),
206 /* kernel/ system */
207 PREFIX(sleep),
208 yield,
209 &current_tick,
210 default_event_handler,
211 default_event_handler_ex,
212 create_thread,
213 remove_thread,
214 reset_poweroff_timer,
215 #ifndef SIMULATOR
216 system_memory_guard,
217 &cpu_frequency,
218 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
219 cpu_boost,
220 #endif
221 timer_register,
222 timer_unregister,
223 timer_set_period,
224 #endif
226 /* strings and memory */
227 snprintf,
228 strcpy,
229 strncpy,
230 strlen,
231 strrchr,
232 strcmp,
233 strncmp,
234 strcasecmp,
235 strncasecmp,
236 memset,
237 memcpy,
238 _ctype_,
239 atoi,
240 strchr,
241 strcat,
242 memcmp,
243 strcasestr,
245 /* sound */
246 sound_set,
247 #ifndef SIMULATOR
248 mp3_play_data,
249 mp3_play_pause,
250 mp3_play_stop,
251 mp3_is_playing,
252 #if CONFIG_HWCODEC != MASNONE
253 bitswap,
254 #endif
255 #if CONFIG_HWCODEC == MASNONE
256 pcm_play_data,
257 pcm_play_stop,
258 pcm_set_frequency,
259 pcm_is_playing,
260 pcm_play_pause,
261 #endif
262 #endif
264 /* playback control */
265 PREFIX(audio_play),
266 audio_stop,
267 audio_pause,
268 audio_resume,
269 audio_next,
270 audio_prev,
271 audio_ff_rewind,
272 audio_next_track,
273 playlist_amount,
274 audio_status,
275 audio_has_changed_track,
276 audio_current_track,
277 audio_flush_and_reload_tracks,
278 audio_get_file_pos,
279 #if !defined(SIMULATOR) && (CONFIG_HWCODEC != MASNONE)
280 mpeg_get_last_header,
281 #endif
282 #if (CONFIG_HWCODEC == MAS3587F) || (CONFIG_HWCODEC == MAS3539F)
283 sound_set_pitch,
284 #endif
286 #if !defined(SIMULATOR) && (CONFIG_HWCODEC != MASNONE)
287 /* MAS communication */
288 mas_readmem,
289 mas_writemem,
290 mas_readreg,
291 mas_writereg,
292 #if (CONFIG_HWCODEC == MAS3587F) || (CONFIG_HWCODEC == MAS3539F)
293 mas_codec_writereg,
294 mas_codec_readreg,
295 #endif
296 #endif /* !simulator and HWCODEC != MASNONE */
298 /* tag database */
299 &tagdbheader,
300 &tagdb_fd,
301 &tagdb_initialized,
302 tagdb_init,
303 /* runtime database */
304 &rundbheader,
305 &rundb_fd,
306 &rundb_initialized,
308 /* misc */
309 srand,
310 rand,
311 (qsort_func)qsort,
312 kbd_input,
313 get_time,
314 set_time,
315 plugin_get_buffer,
316 plugin_get_audio_buffer,
317 plugin_tsr,
318 #if defined(DEBUG) || defined(SIMULATOR)
319 debugf,
320 #endif
321 #ifdef ROCKBOX_HAS_LOGF
322 logf,
323 #endif
324 &global_settings,
325 mp3info,
326 count_mp3_frames,
327 create_xing_header,
328 find_next_frame,
329 battery_level,
330 battery_level_safe,
331 #if (CONFIG_HWCODEC == MAS3587F) || (CONFIG_HWCODEC == MAS3539F)
332 peak_meter_scale_value,
333 peak_meter_set_use_dbfs,
334 peak_meter_get_use_dbfs,
335 #endif
336 #ifdef HAVE_LCD_BITMAP
337 read_bmp_file,
338 #endif
340 /* new stuff at the end, sort into place next time
341 the API gets incompatible */
345 int plugin_load(const char* plugin, void* parameter)
347 enum plugin_status (*plugin_start)(struct plugin_api* api, void* param);
348 int rc;
349 #ifndef SIMULATOR
350 char buf[64];
351 #endif
352 int fd;
354 #ifdef HAVE_LCD_BITMAP
355 int xm,ym;
356 #endif
358 if (pfn_tsr_exit != NULL) /* if we have a resident old plugin: */
360 pfn_tsr_exit(); /* force it to exit now */
361 pfn_tsr_exit = NULL;
364 #ifdef HAVE_LCD_BITMAP
365 lcd_clear_display();
366 xm = lcd_getxmargin();
367 ym = lcd_getymargin();
368 lcd_setmargins(0,0);
369 lcd_update();
370 #else
371 lcd_clear_display();
372 #endif
373 #ifdef SIMULATOR
374 plugin_start = sim_plugin_load((char *)plugin, &fd);
375 if(!plugin_start)
376 return -1;
377 #else
378 fd = open(plugin, O_RDONLY);
379 if (fd < 0) {
380 snprintf(buf, sizeof buf, str(LANG_PLUGIN_CANT_OPEN), plugin);
381 splash(HZ*2, true, buf);
382 return fd;
385 /* zero out plugin buffer to ensure a properly zeroed bss area */
386 memset(pluginbuf, 0, PLUGIN_BUFFER_SIZE);
388 plugin_start = (void*)&pluginbuf;
389 plugin_size = read(fd, plugin_start, PLUGIN_BUFFER_SIZE);
390 close(fd);
391 if (plugin_size < 0) {
392 /* read error */
393 snprintf(buf, sizeof buf, str(LANG_READ_FAILED), plugin);
394 splash(HZ*2, true, buf);
395 return -1;
397 if (plugin_size == 0) {
398 /* loaded a 0-byte plugin, implying it's not for this model */
399 splash(HZ*2, true, str(LANG_PLUGIN_WRONG_MODEL));
400 return -1;
402 #endif
404 plugin_loaded = true;
406 invalidate_icache();
408 rc = plugin_start((struct plugin_api*) &rockbox_api, parameter);
409 /* explicitly casting the pointer here to avoid touching every plugin. */
411 button_clear_queue();
412 #ifdef HAVE_LCD_BITMAP
413 #if LCD_DEPTH > 1
414 lcd_set_drawinfo(DRMODE_SOLID, LCD_BLACK, LCD_WHITE);
415 #else /* LCD_DEPTH == 1 */
416 lcd_set_drawmode(DRMODE_SOLID);
417 #endif /* LCD_DEPTH */
418 #endif /* HAVE_LCD_BITMAP */
420 plugin_loaded = false;
422 switch (rc) {
423 case PLUGIN_OK:
424 break;
426 case PLUGIN_USB_CONNECTED:
427 return PLUGIN_USB_CONNECTED;
429 case PLUGIN_WRONG_API_VERSION:
430 splash(HZ*2, true, str(LANG_PLUGIN_WRONG_VERSION));
431 break;
433 case PLUGIN_WRONG_MODEL:
434 splash(HZ*2, true, str(LANG_PLUGIN_WRONG_MODEL));
435 break;
437 default:
438 splash(HZ*2, true, str(LANG_PLUGIN_ERROR));
439 break;
442 sim_plugin_close(fd);
444 #ifdef HAVE_LCD_BITMAP
445 /* restore margins */
446 lcd_setmargins(xm,ym);
447 #endif
449 return PLUGIN_OK;
452 /* Returns a pointer to the portion of the plugin buffer that is not already
453 being used. If no plugin is loaded, returns the entire plugin buffer */
454 void* plugin_get_buffer(int* buffer_size)
456 int buffer_pos;
458 if (plugin_loaded)
460 if (plugin_size >= PLUGIN_BUFFER_SIZE)
461 return NULL;
463 *buffer_size = PLUGIN_BUFFER_SIZE-plugin_size;
464 buffer_pos = plugin_size;
466 else
468 *buffer_size = PLUGIN_BUFFER_SIZE;
469 buffer_pos = 0;
472 return &pluginbuf[buffer_pos];
475 /* Returns a pointer to the mp3 buffer.
476 Playback gets stopped, to avoid conflicts. */
477 void* plugin_get_audio_buffer(int* buffer_size)
479 audio_stop();
480 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
481 *buffer_size = audiobufend - audiobuf;
482 return audiobuf;
485 /* The plugin wants to stay resident after leaving its main function, e.g.
486 runs from timer or own thread. The callback is registered to later
487 instruct it to free its resources before a new plugin gets loaded. */
488 void plugin_tsr(void (*exit_callback)(void))
490 pfn_tsr_exit = exit_callback; /* remember the callback for later */
494 static int plugin_test(int api_version, int model, int memsize)
496 if (api_version < PLUGIN_MIN_API_VERSION ||
497 api_version > PLUGIN_API_VERSION)
498 return PLUGIN_WRONG_API_VERSION;
500 if (model != MODEL)
501 return PLUGIN_WRONG_MODEL;
503 if (memsize != MEM)
504 return PLUGIN_WRONG_MODEL;
506 return PLUGIN_OK;