Simulate the effects of sector caching a bit. Bypass I/O yield if a byte counter...
[Rockbox.git] / apps / plugin.c
blob9067097591dec8f923a465ba8342fcef7b186f25
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 "plugin.h"
20 #include <ctype.h>
21 #include <string.h>
22 #include <sprintf.h>
23 #include <atoi.h>
24 #include "debug.h"
25 #include "i2c.h"
26 #include "lang.h"
27 #include "led.h"
28 #include "keyboard.h"
29 #include "buffer.h"
30 #include "backlight.h"
31 #include "sound_menu.h"
32 #include "mp3data.h"
33 #include "powermgmt.h"
34 #include "splash.h"
35 #include "logf.h"
36 #include "option_select.h"
37 #include "talk.h"
39 #if CONFIG_CHARGING
40 #include "power.h"
41 #endif
43 #ifdef HAVE_LCD_BITMAP
44 #include "scrollbar.h"
45 #include "peakmeter.h"
46 #include "bmp.h"
47 #include "bidi.h"
48 #endif
50 #ifdef SIMULATOR
51 static unsigned char pluginbuf[PLUGIN_BUFFER_SIZE];
52 void *sim_plugin_load(char *plugin, void **pd);
53 void sim_plugin_close(void *pd);
54 void sim_lcd_ex_init(int shades, unsigned long (*getpixel)(int, int));
55 void sim_lcd_ex_update_rect(int x, int y, int width, int height);
56 #else
57 #define sim_plugin_close(x)
58 extern unsigned char pluginbuf[];
59 #include "bitswap.h"
60 #endif
62 /* for actual plugins only, not for codecs */
63 static bool plugin_loaded = false;
64 static int plugin_size = 0;
65 static bool (*pfn_tsr_exit)(bool reenter) = NULL; /* TSR exit callback */
66 static char current_plugin[MAX_PATH];
68 char *plugin_get_current_filename(void);
70 extern struct thread_entry threads[MAXTHREADS];
72 static const struct plugin_api rockbox_api = {
74 /* lcd */
75 lcd_set_contrast,
76 lcd_update,
77 lcd_clear_display,
78 lcd_setmargins,
79 lcd_getstringsize,
80 lcd_putsxy,
81 lcd_puts,
82 lcd_puts_scroll,
83 lcd_stop_scroll,
84 #ifdef HAVE_LCD_CHARCELLS
85 lcd_define_pattern,
86 lcd_get_locked_pattern,
87 lcd_unlock_pattern,
88 lcd_putc,
89 lcd_put_cursor,
90 lcd_remove_cursor,
91 lcd_icon,
92 lcd_double_height,
93 #else
94 lcd_set_drawmode,
95 lcd_get_drawmode,
96 lcd_setfont,
97 lcd_drawpixel,
98 lcd_drawline,
99 lcd_hline,
100 lcd_vline,
101 lcd_drawrect,
102 lcd_fillrect,
103 lcd_mono_bitmap_part,
104 lcd_mono_bitmap,
105 #if LCD_DEPTH > 1
106 lcd_set_foreground,
107 lcd_get_foreground,
108 lcd_set_background,
109 lcd_get_background,
110 lcd_bitmap_part,
111 lcd_bitmap,
112 lcd_get_backdrop,
113 lcd_set_backdrop,
114 #endif
115 #if LCD_DEPTH == 16
116 lcd_bitmap_transparent_part,
117 lcd_bitmap_transparent,
118 #endif
119 bidi_l2v,
120 font_get_bits,
121 font_load,
122 lcd_puts_style,
123 lcd_puts_scroll_style,
124 &lcd_framebuffer[0][0],
125 lcd_blit,
126 lcd_update_rect,
127 gui_scrollbar_draw,
128 font_get,
129 font_getstringsize,
130 font_get_width,
131 screen_clear_area,
132 #endif
133 backlight_on,
134 backlight_off,
135 backlight_set_timeout,
136 #if CONFIG_CHARGING
137 backlight_set_timeout_plugged,
138 #endif
139 gui_syncsplash,
140 #ifdef HAVE_REMOTE_LCD
141 /* remote lcd */
142 lcd_remote_set_contrast,
143 lcd_remote_clear_display,
144 lcd_remote_setmargins,
145 lcd_remote_puts,
146 lcd_remote_puts_scroll,
147 lcd_remote_stop_scroll,
148 lcd_remote_set_drawmode,
149 lcd_remote_get_drawmode,
150 lcd_remote_setfont,
151 lcd_remote_getstringsize,
152 lcd_remote_drawpixel,
153 lcd_remote_drawline,
154 lcd_remote_hline,
155 lcd_remote_vline,
156 lcd_remote_drawrect,
157 lcd_remote_fillrect,
158 lcd_remote_mono_bitmap_part,
159 lcd_remote_mono_bitmap,
160 lcd_remote_putsxy,
161 lcd_remote_puts_style,
162 lcd_remote_puts_scroll_style,
163 &lcd_remote_framebuffer[0][0],
164 lcd_remote_update,
165 lcd_remote_update_rect,
167 remote_backlight_on,
168 remote_backlight_off,
169 #endif
170 #if NB_SCREENS == 2
171 {&screens[SCREEN_MAIN], &screens[SCREEN_REMOTE]},
172 #else
173 {&screens[SCREEN_MAIN]},
174 #endif
175 #if defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
176 lcd_remote_set_foreground,
177 lcd_remote_get_foreground,
178 lcd_remote_set_background,
179 lcd_remote_get_background,
180 lcd_remote_bitmap_part,
181 lcd_remote_bitmap,
182 #endif
184 #if defined(HAVE_LCD_COLOR)
185 lcd_yuv_blit,
186 #endif
187 #if defined(TOSHIBA_GIGABEAT_F) || defined(SANSA_E200) || defined(SANSA_C200)
188 lcd_yuv_set_options,
189 #endif
190 /* list */
191 gui_synclist_init,
192 gui_synclist_set_nb_items,
193 gui_synclist_set_icon_callback,
194 gui_synclist_get_nb_items,
195 gui_synclist_get_sel_pos,
196 gui_synclist_draw,
197 gui_synclist_select_item,
198 gui_synclist_add_item,
199 gui_synclist_del_item,
200 gui_synclist_limit_scroll,
201 gui_synclist_do_button,
202 gui_synclist_set_title,
204 /* button */
205 button_get,
206 button_get_w_tmo,
207 button_status,
208 button_clear_queue,
209 button_queue_count,
210 #ifdef HAS_BUTTON_HOLD
211 button_hold,
212 #endif
214 /* file */
215 (open_func)PREFIX(open),
216 PREFIX(close),
217 (read_func)PREFIX(read),
218 PREFIX(lseek),
219 (creat_func)PREFIX(creat),
220 (write_func)PREFIX(write),
221 PREFIX(remove),
222 PREFIX(rename),
223 PREFIX(ftruncate),
224 PREFIX(filesize),
225 fdprintf,
226 read_line,
227 settings_parseline,
228 #ifndef SIMULATOR
229 ata_sleep,
230 ata_disk_is_active,
231 #endif
232 ata_spindown,
233 reload_directory,
234 create_numbered_filename,
236 /* dir */
237 opendir,
238 closedir,
239 readdir,
240 mkdir,
241 rmdir,
243 /* kernel/ system */
244 PREFIX(sleep),
245 yield,
246 #ifdef HAVE_PRIORITY_SCHEDULING
247 priority_yield,
248 #endif
249 &current_tick,
250 default_event_handler,
251 default_event_handler_ex,
252 threads,
253 create_thread,
254 remove_thread,
255 reset_poweroff_timer,
256 #ifndef SIMULATOR
257 system_memory_guard,
258 &cpu_frequency,
260 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
261 #ifdef CPU_BOOST_LOGGING
262 cpu_boost_,
263 #else
264 cpu_boost,
265 #endif
266 #endif
267 #endif
268 timer_register,
269 timer_unregister,
270 timer_set_period,
272 queue_init,
273 queue_delete,
274 queue_post,
275 queue_wait_w_tmo,
276 usb_acknowledge,
277 #ifdef RB_PROFILE
278 profile_thread,
279 profstop,
280 profile_func_enter,
281 profile_func_exit,
282 #endif
284 #ifdef SIMULATOR
285 /* special simulator hooks */
286 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
287 sim_lcd_ex_init,
288 sim_lcd_ex_update_rect,
289 #endif
290 #endif
292 /* strings and memory */
293 snprintf,
294 vsnprintf,
295 strcpy,
296 strncpy,
297 strlen,
298 strrchr,
299 strcmp,
300 strncmp,
301 strcasecmp,
302 strncasecmp,
303 memset,
304 memcpy,
305 memmove,
306 _ctype_,
307 atoi,
308 strchr,
309 strcat,
310 memchr,
311 memcmp,
312 strcasestr,
313 strtok_r,
314 /* unicode stuff */
315 utf8decode,
316 iso_decode,
317 utf16LEdecode,
318 utf16BEdecode,
319 utf8encode,
320 utf8length,
321 utf8seek,
323 /* sound */
324 #if CONFIG_CODEC == SWCODEC
325 sound_default,
326 #endif
327 sound_set,
329 sound_min,
330 sound_max,
331 #ifndef SIMULATOR
332 mp3_play_data,
333 mp3_play_pause,
334 mp3_play_stop,
335 mp3_is_playing,
336 #if CONFIG_CODEC != SWCODEC
337 bitswap,
338 #endif
339 #endif
340 #if CONFIG_CODEC == SWCODEC
341 &audio_master_sampr_list[0],
342 &hw_freq_sampr[0],
343 pcm_apply_settings,
344 pcm_play_data,
345 pcm_play_stop,
346 pcm_set_frequency,
347 pcm_is_playing,
348 pcm_is_paused,
349 pcm_play_pause,
350 pcm_get_bytes_waiting,
351 pcm_calculate_peaks,
352 #ifdef HAVE_RECORDING
353 &rec_freq_sampr[0],
354 pcm_init_recording,
355 pcm_close_recording,
356 pcm_record_data,
357 pcm_record_more,
358 pcm_stop_recording,
359 pcm_calculate_rec_peaks,
360 audio_set_recording_gain,
361 #endif /* HAVE_RECORDING */
362 #if INPUT_SRC_CAPS != 0
363 audio_set_output_source,
364 audio_set_input_source,
365 #endif
366 #endif /* CONFIG_CODEC == SWCODEC */
368 /* playback control */
369 playlist_amount,
370 playlist_resume,
371 playlist_start,
372 PREFIX(audio_play),
373 audio_stop,
374 audio_pause,
375 audio_resume,
376 audio_next,
377 audio_prev,
378 audio_ff_rewind,
379 audio_next_track,
380 audio_status,
381 audio_has_changed_track,
382 audio_current_track,
383 audio_flush_and_reload_tracks,
384 audio_get_file_pos,
385 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
386 mpeg_get_last_header,
387 #endif
388 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
389 (CONFIG_CODEC == SWCODEC)
390 sound_set_pitch,
391 #endif
393 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
394 /* MAS communication */
395 mas_readmem,
396 mas_writemem,
397 mas_readreg,
398 mas_writereg,
399 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
400 mas_codec_writereg,
401 mas_codec_readreg,
402 i2c_begin,
403 i2c_end,
404 i2c_write,
405 #endif
406 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
408 /* menu */
409 do_menu,
410 /* statusbars */
411 &statusbars,
412 gui_syncstatusbar_draw,
413 /* options */
414 find_setting,
415 option_screen,
416 set_option,
417 set_bool_options,
418 set_int,
419 set_bool,
420 #ifdef HAVE_LCD_COLOR
421 set_color,
422 #endif
424 /* action handling */
425 get_custom_action,
426 get_action,
427 action_userabort,
429 /* power */
430 battery_level,
431 battery_level_safe,
432 battery_time,
433 #ifndef SIMULATOR
434 battery_voltage,
435 #endif
436 #if CONFIG_CHARGING
437 charger_inserted,
438 # if CONFIG_CHARGING == CHARGING_MONITOR
439 charging_state,
440 # endif
441 #endif
442 #ifdef HAVE_USB_POWER
443 usb_powered,
444 #endif
446 /* misc */
447 srand,
448 rand,
449 (qsort_func)qsort,
450 kbd_input,
451 get_time,
452 set_time,
453 #if CONFIG_RTC
454 mktime,
455 #endif
456 plugin_get_buffer,
457 plugin_get_audio_buffer,
458 plugin_tsr,
459 plugin_get_current_filename,
460 #ifdef PLUGIN_USE_IRAM
461 plugin_iram_init,
462 #endif
463 #if defined(DEBUG) || defined(SIMULATOR)
464 debugf,
465 #endif
466 #ifdef ROCKBOX_HAS_LOGF
467 _logf,
468 #endif
469 &global_settings,
470 &global_status,
471 talk_disable,
472 #if CONFIG_CODEC == SWCODEC
473 codec_load_file,
474 get_codec_filename,
475 get_metadata,
476 #endif
477 mp3info,
478 count_mp3_frames,
479 create_xing_header,
480 find_next_frame,
481 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
482 peak_meter_scale_value,
483 peak_meter_set_use_dbfs,
484 peak_meter_get_use_dbfs,
485 #endif
486 #ifdef HAVE_LCD_BITMAP
487 read_bmp_file,
488 screen_dump_set_hook,
489 #endif
490 show_logo,
491 tree_get_context,
492 set_current_file,
493 set_dirfilter,
495 #ifdef HAVE_WHEEL_POSITION
496 wheel_status,
497 wheel_send_events,
498 #endif
500 #ifdef IRIVER_H100_SERIES
501 /* Routines for the iriver_flash -plugin. */
502 detect_original_firmware,
503 detect_flashed_ramimage,
504 detect_flashed_romimage,
505 #endif
506 led,
507 #ifdef CACHE_FUNCTIONS_AS_CALL
508 flush_icache,
509 invalidate_icache,
510 #endif
511 /* new stuff at the end, sort into place next time
512 the API gets incompatible */
514 #if (CONFIG_CODEC == SWCODEC)
515 mutex_init,
516 mutex_lock,
517 mutex_unlock,
518 #endif
520 thread_wait,
522 #ifdef PROC_NEEDS_CACHEALIGN
523 align_buffer,
524 #endif
526 file_exists,
527 dir_exists,
529 #ifdef HAVE_REMOTE_LCD
530 remote_backlight_set_timeout,
531 #if CONFIG_CHARGING
532 remote_backlight_set_timeout_plugged,
533 #endif
534 #endif /* HAVE_REMOTE_LCD */
537 int plugin_load(const char* plugin, void* parameter)
539 int rc;
540 struct plugin_header *hdr;
541 #ifdef SIMULATOR
542 void *pd;
543 #else
544 int fd;
545 ssize_t readsize;
546 #endif
547 int xm, ym;
548 #ifdef HAVE_REMOTE_LCD
549 int rxm, rym;
550 #endif
552 #if LCD_DEPTH > 1
553 fb_data* old_backdrop;
554 #endif
556 if (pfn_tsr_exit != NULL) /* if we have a resident old plugin: */
558 if (pfn_tsr_exit(!strcmp(current_plugin, plugin)) == false )
560 /* not allowing another plugin to load */
561 return PLUGIN_OK;
563 pfn_tsr_exit = NULL;
564 plugin_loaded = false;
567 gui_syncsplash(0, ID2P(LANG_WAIT));
568 strcpy(current_plugin, plugin);
570 #ifdef SIMULATOR
571 hdr = sim_plugin_load((char *)plugin, &pd);
572 if (pd == NULL) {
573 gui_syncsplash(HZ*2, str(LANG_PLUGIN_CANT_OPEN), plugin);
574 return -1;
576 if (hdr == NULL
577 || hdr->magic != PLUGIN_MAGIC
578 || hdr->target_id != TARGET_ID) {
579 sim_plugin_close(pd);
580 gui_syncsplash(HZ*2, str(LANG_PLUGIN_WRONG_MODEL));
581 return -1;
583 if (hdr->api_version > PLUGIN_API_VERSION
584 || hdr->api_version < PLUGIN_MIN_API_VERSION) {
585 sim_plugin_close(pd);
586 gui_syncsplash(HZ*2, str(LANG_PLUGIN_WRONG_VERSION));
587 return -1;
589 #else
590 fd = open(plugin, O_RDONLY);
591 if (fd < 0) {
592 gui_syncsplash(HZ*2, str(LANG_PLUGIN_CANT_OPEN), plugin);
593 return fd;
596 readsize = read(fd, pluginbuf, PLUGIN_BUFFER_SIZE);
597 close(fd);
599 if (readsize < 0) {
600 gui_syncsplash(HZ*2, str(LANG_READ_FAILED), plugin);
601 return -1;
603 hdr = (struct plugin_header *)pluginbuf;
605 if ((unsigned)readsize <= sizeof(struct plugin_header)
606 || hdr->magic != PLUGIN_MAGIC
607 || hdr->target_id != TARGET_ID
608 || hdr->load_addr != pluginbuf
609 || hdr->end_addr > pluginbuf + PLUGIN_BUFFER_SIZE) {
610 gui_syncsplash(HZ*2, str(LANG_PLUGIN_WRONG_MODEL));
611 return -1;
613 if (hdr->api_version > PLUGIN_API_VERSION
614 || hdr->api_version < PLUGIN_MIN_API_VERSION) {
615 gui_syncsplash(HZ*2, str(LANG_PLUGIN_WRONG_VERSION));
616 return -1;
618 plugin_size = hdr->end_addr - pluginbuf;
620 /* zero out bss area only, above guards end of pluginbuf */
621 if (plugin_size > readsize)
622 memset(pluginbuf + readsize, 0, plugin_size - readsize);
623 #endif
625 plugin_loaded = true;
627 xm = lcd_getxmargin();
628 ym = lcd_getymargin();
629 lcd_setmargins(0,0);
631 #if defined HAVE_LCD_BITMAP && LCD_DEPTH > 1
632 old_backdrop = lcd_get_backdrop();
633 #endif
634 lcd_clear_display();
635 lcd_update();
637 #ifdef HAVE_REMOTE_LCD
638 rxm = lcd_remote_getxmargin();
639 rym = lcd_remote_getymargin();
640 lcd_remote_setmargins(0, 0);
641 lcd_remote_clear_display();
642 lcd_remote_update();
643 #endif
645 invalidate_icache();
647 rc = hdr->entry_point((struct plugin_api*) &rockbox_api, parameter);
648 /* explicitly casting the pointer here to avoid touching every plugin. */
650 button_clear_queue();
652 #ifdef HAVE_LCD_BITMAP
653 #if LCD_DEPTH > 1
654 lcd_set_backdrop(old_backdrop);
655 #ifdef HAVE_LCD_COLOR
656 lcd_set_drawinfo(DRMODE_SOLID, global_settings.fg_color,
657 global_settings.bg_color);
658 #else
659 lcd_set_drawinfo(DRMODE_SOLID, LCD_DEFAULT_FG, LCD_DEFAULT_BG);
660 #endif
661 #else /* LCD_DEPTH == 1 */
662 lcd_set_drawmode(DRMODE_SOLID);
663 #endif /* LCD_DEPTH */
664 #endif /* HAVE_LCD_BITMAP */
666 /* restore margins */
667 lcd_setmargins(xm,ym);
668 lcd_clear_display();
669 lcd_update();
671 #ifdef HAVE_REMOTE_LCD
672 #if LCD_REMOTE_DEPTH > 1
673 lcd_remote_set_drawinfo(DRMODE_SOLID, LCD_REMOTE_DEFAULT_FG,
674 LCD_REMOTE_DEFAULT_BG);
675 #else
676 lcd_remote_set_drawmode(DRMODE_SOLID);
677 #endif
678 lcd_remote_setmargins(rxm, rym);
679 lcd_remote_clear_display();
682 lcd_remote_update();
685 #endif
687 if (pfn_tsr_exit == NULL)
688 plugin_loaded = false;
690 sim_plugin_close(pd);
692 switch (rc) {
693 case PLUGIN_OK:
694 break;
696 case PLUGIN_USB_CONNECTED:
697 return PLUGIN_USB_CONNECTED;
699 default:
700 gui_syncsplash(HZ*2, str(LANG_PLUGIN_ERROR));
701 break;
703 return PLUGIN_OK;
706 /* Returns a pointer to the portion of the plugin buffer that is not already
707 being used. If no plugin is loaded, returns the entire plugin buffer */
708 void* plugin_get_buffer(size_t *buffer_size)
710 int buffer_pos;
712 if (plugin_loaded)
714 if (plugin_size >= PLUGIN_BUFFER_SIZE)
715 return NULL;
717 *buffer_size = PLUGIN_BUFFER_SIZE-plugin_size;
718 buffer_pos = plugin_size;
720 else
722 *buffer_size = PLUGIN_BUFFER_SIZE;
723 buffer_pos = 0;
726 return &pluginbuf[buffer_pos];
729 /* Returns a pointer to the mp3 buffer.
730 Playback gets stopped, to avoid conflicts.
731 Talk buffer is stolen as well.
733 void* plugin_get_audio_buffer(size_t *buffer_size)
735 #if CONFIG_CODEC == SWCODEC
736 return audio_get_buffer(true, buffer_size);
737 #else
738 audio_stop();
739 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
740 *buffer_size = audiobufend - audiobuf;
741 return audiobuf;
742 #endif
745 #ifdef PLUGIN_USE_IRAM
746 /* Initializes plugin IRAM */
747 void plugin_iram_init(char *iramstart, char *iramcopy, size_t iram_size,
748 char *iedata, size_t iedata_size)
750 /* We need to stop audio playback in order to use codec IRAM */
751 audio_hard_stop();
752 memcpy(iramstart, iramcopy, iram_size);
753 memset(iedata, 0, iedata_size);
754 memset(iramcopy, 0, iram_size);
755 #if NUM_CORES > 1
756 /* writeback cleared iedata and iramcopy areas */
757 flush_icache();
758 #endif
760 #endif /* PLUGIN_USE_IRAM */
762 /* The plugin wants to stay resident after leaving its main function, e.g.
763 runs from timer or own thread. The callback is registered to later
764 instruct it to free its resources before a new plugin gets loaded. */
765 void plugin_tsr(bool (*exit_callback)(bool))
767 pfn_tsr_exit = exit_callback; /* remember the callback for later */
770 char *plugin_get_current_filename(void)
772 return current_plugin;