Use new MDCT library for libfaad. Speeds up AAC-LC by 2.5MHz.
[kugel-rb.git] / apps / plugin.c
blob46ab6e6429c7ae3795f4c85468b2179cf1d9b1d3
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Björn Stenberg
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 ****************************************************************************/
21 #include "plugin.h"
22 #include <ctype.h>
23 #include <string.h>
24 #include <sprintf.h>
25 #include <stdlib.h>
26 #include "debug.h"
27 #include "i2c.h"
28 #include "lang.h"
29 #include "led.h"
30 #include "keyboard.h"
31 #include "buffer.h"
32 #include "backlight.h"
33 #include "sound_menu.h"
34 #include "mp3data.h"
35 #include "powermgmt.h"
36 #include "splash.h"
37 #include "logf.h"
38 #include "option_select.h"
39 #include "talk.h"
40 #include "version.h"
41 #include "storage.h"
42 #include "pcmbuf.h"
43 #include "errno.h"
45 #if CONFIG_CHARGING
46 #include "power.h"
47 #endif
49 #ifdef HAVE_LCD_BITMAP
50 #include "scrollbar.h"
51 #include "peakmeter.h"
52 #include "bmp.h"
53 #include "bidi.h"
54 #endif
56 #ifdef USB_ENABLE_HID
57 #include "usbstack/usb_hid.h"
58 #endif
60 #ifdef SIMULATOR
61 #define PREFIX(_x_) sim_ ## _x_
62 #else
63 #define PREFIX
64 #endif
66 #if defined(HAVE_PLUGIN_CHECK_OPEN_CLOSE) && (MAX_OPEN_FILES>32)
67 #warning "MAX_OPEN_FILES>32, disabling plugin file open/close checking"
68 #undef HAVE_PLUGIN_CHECK_OPEN_CLOSE
69 #endif
71 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
72 static unsigned int open_files;
73 #endif
75 #ifdef SIMULATOR
76 static unsigned char pluginbuf[PLUGIN_BUFFER_SIZE];
77 void *sim_plugin_load(char *plugin, void **pd);
78 void sim_plugin_close(void *pd);
79 void sim_lcd_ex_init(unsigned long (*getpixel)(int, int));
80 void sim_lcd_ex_update_rect(int x, int y, int width, int height);
81 #else
82 #define sim_plugin_close(x)
83 extern unsigned char pluginbuf[];
84 #include "bitswap.h"
85 #endif
87 /* for actual plugins only, not for codecs */
88 static bool plugin_loaded = false;
89 static int plugin_size = 0;
90 static bool (*pfn_tsr_exit)(bool reenter) = NULL; /* TSR exit callback */
91 static char current_plugin[MAX_PATH];
93 char *plugin_get_current_filename(void);
95 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
96 /* Some wrappers used to monitor open and close and detect leaks*/
97 static int open_wrapper(const char* pathname, int flags);
98 static int close_wrapper(int fd);
99 static int creat_wrapper(const char *pathname);
100 #endif
102 static const struct plugin_api rockbox_api = {
104 /* lcd */
105 #ifdef HAVE_LCD_CONTRAST
106 lcd_set_contrast,
107 #endif
108 lcd_update,
109 lcd_clear_display,
110 lcd_getstringsize,
111 lcd_putsxy,
112 lcd_puts,
113 lcd_puts_scroll,
114 lcd_stop_scroll,
115 #ifdef HAVE_LCD_CHARCELLS
116 lcd_define_pattern,
117 lcd_get_locked_pattern,
118 lcd_unlock_pattern,
119 lcd_putc,
120 lcd_put_cursor,
121 lcd_remove_cursor,
122 lcd_icon,
123 lcd_double_height,
124 #else
125 &lcd_framebuffer[0][0],
126 lcd_update_rect,
127 lcd_set_drawmode,
128 lcd_get_drawmode,
129 lcd_setfont,
130 lcd_drawpixel,
131 lcd_drawline,
132 lcd_hline,
133 lcd_vline,
134 lcd_drawrect,
135 lcd_fillrect,
136 lcd_mono_bitmap_part,
137 lcd_mono_bitmap,
138 #if LCD_DEPTH > 1
139 lcd_set_foreground,
140 lcd_get_foreground,
141 lcd_set_background,
142 lcd_get_background,
143 lcd_bitmap_part,
144 lcd_bitmap,
145 lcd_get_backdrop,
146 lcd_set_backdrop,
147 #endif
148 #if LCD_DEPTH == 16
149 lcd_bitmap_transparent_part,
150 lcd_bitmap_transparent,
151 #if MEMORYSIZE > 2
152 lcd_blit_yuv,
153 #if defined(TOSHIBA_GIGABEAT_F) || defined(SANSA_E200) || defined(SANSA_C200) \
154 || defined(IRIVER_H10) || defined(COWON_D2) || defined(PHILIPS_HDD1630) \
155 || defined(SANSA_FUZE) || defined(SANSA_E200V2) || defined(TOSHIBA_GIGABEAT_S)
156 lcd_yuv_set_options,
157 #endif
158 #endif /* MEMORYSIZE > 2 */
159 #elif (LCD_DEPTH < 4) && !defined(SIMULATOR)
160 lcd_blit_mono,
161 lcd_blit_grey_phase,
162 #endif /* LCD_DEPTH */
163 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
164 lcd_blit_pal256,
165 lcd_pal256_update_pal,
166 #endif
167 lcd_puts_style,
168 lcd_puts_scroll_style,
169 #ifdef HAVE_LCD_INVERT
170 lcd_set_invert_display,
171 #endif /* HAVE_LCD_INVERT */
172 #if defined(HAVE_LCD_MODES)
173 lcd_set_mode,
174 #endif
175 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
176 &button_queue,
177 #endif
178 bidi_l2v,
179 font_get_bits,
180 font_load,
181 font_get,
182 font_getstringsize,
183 font_get_width,
184 screen_clear_area,
185 gui_scrollbar_draw,
186 #endif /* HAVE_LCD_BITMAP */
187 get_codepage_name,
189 backlight_on,
190 backlight_off,
191 backlight_set_timeout,
192 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
193 backlight_set_brightness,
194 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
196 #if CONFIG_CHARGING
197 backlight_set_timeout_plugged,
198 #endif
199 is_backlight_on,
200 splash,
201 splashf,
203 #ifdef HAVE_REMOTE_LCD
204 /* remote lcd */
205 lcd_remote_set_contrast,
206 lcd_remote_clear_display,
207 lcd_remote_puts,
208 lcd_remote_puts_scroll,
209 lcd_remote_stop_scroll,
210 lcd_remote_set_drawmode,
211 lcd_remote_get_drawmode,
212 lcd_remote_setfont,
213 lcd_remote_getstringsize,
214 lcd_remote_drawpixel,
215 lcd_remote_drawline,
216 lcd_remote_hline,
217 lcd_remote_vline,
218 lcd_remote_drawrect,
219 lcd_remote_fillrect,
220 lcd_remote_mono_bitmap_part,
221 lcd_remote_mono_bitmap,
222 lcd_remote_putsxy,
223 lcd_remote_puts_style,
224 lcd_remote_puts_scroll_style,
225 &lcd_remote_framebuffer[0][0],
226 lcd_remote_update,
227 lcd_remote_update_rect,
229 remote_backlight_on,
230 remote_backlight_off,
231 remote_backlight_set_timeout,
232 #if CONFIG_CHARGING
233 remote_backlight_set_timeout_plugged,
234 #endif
235 #endif /* HAVE_REMOTE_LCD */
236 #if NB_SCREENS == 2
237 {&screens[SCREEN_MAIN], &screens[SCREEN_REMOTE]},
238 #else
239 {&screens[SCREEN_MAIN]},
240 #endif
241 #if defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
242 lcd_remote_set_foreground,
243 lcd_remote_get_foreground,
244 lcd_remote_set_background,
245 lcd_remote_get_background,
246 lcd_remote_bitmap_part,
247 lcd_remote_bitmap,
248 #endif
249 viewport_set_defaults,
250 #ifdef HAVE_LCD_BITMAP
251 viewportmanager_theme_enable,
252 viewportmanager_theme_undo,
253 #endif
255 /* list */
256 gui_synclist_init,
257 gui_synclist_set_nb_items,
258 gui_synclist_set_icon_callback,
259 gui_synclist_get_nb_items,
260 gui_synclist_get_sel_pos,
261 gui_synclist_draw,
262 gui_synclist_select_item,
263 gui_synclist_add_item,
264 gui_synclist_del_item,
265 gui_synclist_limit_scroll,
266 gui_synclist_do_button,
267 gui_synclist_set_title,
268 gui_syncyesno_run,
269 simplelist_info_init,
270 simplelist_show_list,
272 /* button */
273 button_get,
274 button_get_w_tmo,
275 button_status,
276 #ifdef HAVE_BUTTON_DATA
277 button_get_data,
278 button_status_wdata,
279 #endif
280 button_clear_queue,
281 button_queue_count,
282 #ifdef HAS_BUTTON_HOLD
283 button_hold,
284 #endif
285 #ifdef HAVE_TOUCHSCREEN
286 touchscreen_set_mode,
287 #endif
289 #ifdef HAVE_BUTTON_LIGHT
290 buttonlight_set_timeout,
291 buttonlight_off,
292 buttonlight_on,
293 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
294 buttonlight_set_brightness,
295 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
296 #endif /* HAVE_BUTTON_LIGHT */
298 /* file */
299 open_utf8,
300 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
301 (open_func)open_wrapper,
302 close_wrapper,
303 #else
304 (open_func)PREFIX(open),
305 PREFIX(close),
306 #endif
307 (read_func)PREFIX(read),
308 PREFIX(lseek),
309 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
310 (creat_func)creat_wrapper,
311 #else
312 (creat_func)PREFIX(creat),
313 #endif
314 (write_func)PREFIX(write),
315 PREFIX(remove),
316 PREFIX(rename),
317 PREFIX(ftruncate),
318 PREFIX(filesize),
319 fdprintf,
320 read_line,
321 settings_parseline,
322 storage_sleep,
323 storage_spin,
324 storage_spindown,
325 #if USING_STORAGE_CALLBACK
326 register_storage_idle_func,
327 unregister_storage_idle_func,
328 #endif /* USING_STORAGE_CALLBACK */
329 reload_directory,
330 create_numbered_filename,
331 file_exists,
332 strip_extension,
333 crc_32,
335 /* dir */
336 opendir,
337 closedir,
338 readdir,
339 mkdir,
340 rmdir,
341 dir_exists,
343 /* kernel/ system */
344 #ifdef CPU_ARM
345 __div0,
346 #endif
347 PREFIX(sleep),
348 yield,
349 &current_tick,
350 default_event_handler,
351 default_event_handler_ex,
352 create_thread,
353 thread_exit,
354 thread_wait,
355 #if (CONFIG_CODEC == SWCODEC)
356 thread_thaw,
357 #ifdef HAVE_PRIORITY_SCHEDULING
358 thread_set_priority,
359 #endif
360 mutex_init,
361 mutex_lock,
362 mutex_unlock,
363 #endif
365 reset_poweroff_timer,
366 #ifndef SIMULATOR
367 system_memory_guard,
368 &cpu_frequency,
370 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
371 #ifdef CPU_BOOST_LOGGING
372 cpu_boost_,
373 #else
374 cpu_boost,
375 #endif
376 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
377 #endif /* !SIMULATOR */
378 #ifdef HAVE_SCHEDULER_BOOSTCTRL
379 trigger_cpu_boost,
380 cancel_cpu_boost,
381 #endif
382 #if NUM_CORES > 1
383 cpucache_flush,
384 cpucache_invalidate,
385 #endif
386 timer_register,
387 timer_unregister,
388 timer_set_period,
390 queue_init,
391 queue_delete,
392 queue_post,
393 queue_wait_w_tmo,
394 #if CONFIG_CODEC == SWCODEC
395 queue_enable_queue_send,
396 queue_empty,
397 queue_wait,
398 queue_send,
399 queue_reply,
400 #endif
401 usb_acknowledge,
402 #ifdef USB_ENABLE_HID
403 usb_hid_send,
404 #endif
405 #ifdef RB_PROFILE
406 profile_thread,
407 profstop,
408 __cyg_profile_func_enter,
409 __cyg_profile_func_exit,
410 #endif
411 add_event,
412 remove_event,
413 send_event,
415 #ifdef SIMULATOR
416 /* special simulator hooks */
417 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
418 sim_lcd_ex_init,
419 sim_lcd_ex_update_rect,
420 #endif
421 #endif
423 /* strings and memory */
424 snprintf,
425 vsnprintf,
426 strcpy,
427 strlcpy,
428 strlen,
429 strrchr,
430 strcmp,
431 strncmp,
432 strcasecmp,
433 strncasecmp,
434 memset,
435 memcpy,
436 memmove,
437 _ctype_,
438 atoi,
439 strchr,
440 strcat,
441 strlcat,
442 memchr,
443 memcmp,
444 strcasestr,
445 strtok_r,
446 /* unicode stuff */
447 utf8decode,
448 iso_decode,
449 utf16LEdecode,
450 utf16BEdecode,
451 utf8encode,
452 utf8length,
453 utf8seek,
455 /* sound */
456 sound_set,
457 sound_default,
458 sound_min,
459 sound_max,
460 sound_unit,
461 sound_val2phys,
462 #ifndef SIMULATOR
463 mp3_play_data,
464 mp3_play_pause,
465 mp3_play_stop,
466 mp3_is_playing,
467 #if CONFIG_CODEC != SWCODEC
468 bitswap,
469 #endif
470 #endif
471 #if CONFIG_CODEC == SWCODEC
472 &audio_master_sampr_list[0],
473 &hw_freq_sampr[0],
474 pcm_apply_settings,
475 pcm_play_data,
476 pcm_play_stop,
477 pcm_set_frequency,
478 pcm_is_playing,
479 pcm_is_paused,
480 pcm_play_pause,
481 pcm_get_bytes_waiting,
482 pcm_calculate_peaks,
483 pcm_get_peak_buffer,
484 pcm_play_lock,
485 pcm_play_unlock,
486 pcmbuf_beep,
487 #ifdef HAVE_RECORDING
488 &rec_freq_sampr[0],
489 pcm_init_recording,
490 pcm_close_recording,
491 pcm_record_data,
492 pcm_record_more,
493 pcm_stop_recording,
494 pcm_calculate_rec_peaks,
495 audio_set_recording_gain,
496 #endif /* HAVE_RECORDING */
497 #if INPUT_SRC_CAPS != 0
498 audio_set_output_source,
499 audio_set_input_source,
500 #endif
501 dsp_set_crossfeed,
502 dsp_set_eq,
503 dsp_dither_enable,
504 dsp_configure,
505 dsp_process,
506 dsp_input_count,
507 dsp_output_count,
508 #endif /* CONFIG_CODEC == SWCODEC */
510 /* playback control */
511 playlist_amount,
512 playlist_resume,
513 playlist_start,
514 playlist_add,
515 playlist_sync,
516 playlist_remove_all_tracks,
517 playlist_create,
518 playlist_insert_track,
519 playlist_insert_directory,
520 playlist_shuffle,
521 PREFIX(audio_play),
522 audio_stop,
523 audio_pause,
524 audio_resume,
525 audio_next,
526 audio_prev,
527 audio_ff_rewind,
528 audio_next_track,
529 audio_status,
530 audio_current_track,
531 audio_flush_and_reload_tracks,
532 audio_get_file_pos,
533 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
534 mpeg_get_last_header,
535 #endif
536 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
537 (CONFIG_CODEC == SWCODEC)
538 sound_set_pitch,
539 #endif
541 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
542 /* MAS communication */
543 mas_readmem,
544 mas_writemem,
545 mas_readreg,
546 mas_writereg,
547 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
548 mas_codec_writereg,
549 mas_codec_readreg,
550 i2c_begin,
551 i2c_end,
552 i2c_write,
553 #endif
554 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
556 /* menu */
557 do_menu,
558 /* statusbars */
559 &statusbars,
560 gui_syncstatusbar_draw,
561 /* options */
562 get_settings_list,
563 find_setting,
564 option_screen,
565 set_option,
566 set_bool_options,
567 set_int,
568 set_bool,
569 #ifdef HAVE_LCD_COLOR
570 set_color,
571 #endif
573 /* action handling */
574 get_custom_action,
575 get_action,
576 #ifdef HAVE_TOUCHSCREEN
577 action_get_touchscreen_press,
578 #endif
579 action_userabort,
581 /* power */
582 battery_level,
583 battery_level_safe,
584 battery_time,
585 #ifndef SIMULATOR
586 battery_voltage,
587 #endif
588 #if CONFIG_CHARGING
589 charger_inserted,
590 # if CONFIG_CHARGING >= CHARGING_MONITOR
591 charging_state,
592 # endif
593 #endif
594 #ifdef HAVE_USB_POWER
595 usb_powered,
596 #endif
598 /* misc */
599 #if !defined(SIMULATOR) || defined(__MINGW32__) || defined(__CYGWIN__)
600 &errno,
601 #endif
602 srand,
603 rand,
604 (qsort_func)qsort,
605 kbd_input,
606 get_time,
607 set_time,
608 #if CONFIG_RTC
609 mktime,
610 #endif
611 plugin_get_buffer,
612 plugin_get_audio_buffer,
613 plugin_tsr,
614 plugin_get_current_filename,
615 #ifdef PLUGIN_USE_IRAM
616 plugin_iram_init,
617 #endif
618 #if defined(DEBUG) || defined(SIMULATOR)
619 debugf,
620 #endif
621 #ifdef ROCKBOX_HAS_LOGF
622 _logf,
623 #endif
624 &global_settings,
625 &global_status,
626 talk_disable,
627 #if CONFIG_CODEC == SWCODEC
628 codec_thread_do_callback,
629 codec_load_file,
630 get_codec_filename,
631 #endif
632 get_metadata,
633 mp3info,
634 count_mp3_frames,
635 create_xing_header,
636 find_next_frame,
637 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
638 peak_meter_scale_value,
639 peak_meter_set_use_dbfs,
640 peak_meter_get_use_dbfs,
641 #endif
642 #ifdef HAVE_LCD_BITMAP
643 read_bmp_file,
644 read_bmp_fd,
645 #ifdef HAVE_JPEG
646 read_jpeg_file,
647 read_jpeg_fd,
648 #endif
649 screen_dump_set_hook,
650 #endif
651 show_logo,
652 tree_get_context,
653 set_current_file,
654 set_dirfilter,
656 #ifdef HAVE_WHEEL_POSITION
657 wheel_status,
658 wheel_send_events,
659 #endif
661 #ifdef IRIVER_H100_SERIES
662 /* Routines for the iriver_flash -plugin. */
663 detect_original_firmware,
664 detect_flashed_ramimage,
665 detect_flashed_romimage,
666 #endif
667 led,
668 #if (CONFIG_CODEC == SWCODEC)
669 bufopen,
670 bufalloc,
671 bufclose,
672 bufseek,
673 bufadvance,
674 bufread,
675 bufgetdata,
676 bufgettail,
677 bufcuttail,
679 buf_get_offset,
680 buf_handle_offset,
681 buf_request_buffer_handle,
682 buf_set_base_handle,
683 buf_used,
684 #endif
686 #ifdef HAVE_TAGCACHE
687 tagcache_search,
688 tagcache_search_set_uniqbuf,
689 tagcache_search_add_filter,
690 tagcache_get_next,
691 tagcache_retrieve,
692 tagcache_search_finish,
693 tagcache_get_numeric,
694 #ifdef HAVE_TC_RAMCACHE
695 tagcache_fill_tags,
696 #endif
697 #endif
699 #ifdef HAVE_ALBUMART
700 search_albumart_files,
701 #endif
703 #ifdef HAVE_SEMAPHORE_OBJECTS
704 semaphore_init,
705 semaphore_wait,
706 semaphore_release,
707 #endif
709 appsversion,
710 /* new stuff at the end, sort into place next time
711 the API gets incompatible */
714 int plugin_load(const char* plugin, const void* parameter)
716 int rc, i;
717 struct plugin_header *hdr;
718 #ifdef SIMULATOR
719 void *pd;
720 #else /* !SIMULATOR */
721 int fd;
722 ssize_t readsize;
723 #if NUM_CORES > 1
724 unsigned my_core;
725 #endif
726 #endif /* !SIMULATOR */
728 #if LCD_DEPTH > 1
729 fb_data* old_backdrop;
730 #endif
732 if (pfn_tsr_exit != NULL) /* if we have a resident old plugin: */
734 if (pfn_tsr_exit(!strcmp(current_plugin, plugin)) == false )
736 /* not allowing another plugin to load */
737 return PLUGIN_OK;
739 pfn_tsr_exit = NULL;
740 plugin_loaded = false;
743 splash(0, ID2P(LANG_WAIT));
744 strcpy(current_plugin, plugin);
746 #ifdef SIMULATOR
747 hdr = sim_plugin_load((char *)plugin, &pd);
748 if (pd == NULL) {
749 splashf(HZ*2, str(LANG_PLUGIN_CANT_OPEN), plugin);
750 return -1;
752 if (hdr == NULL
753 || hdr->magic != PLUGIN_MAGIC
754 || hdr->target_id != TARGET_ID) {
755 sim_plugin_close(pd);
756 splash(HZ*2, str(LANG_PLUGIN_WRONG_MODEL));
757 return -1;
759 if (hdr->api_version > PLUGIN_API_VERSION
760 || hdr->api_version < PLUGIN_MIN_API_VERSION) {
761 sim_plugin_close(pd);
762 splash(HZ*2, str(LANG_PLUGIN_WRONG_VERSION));
763 return -1;
765 #else
766 fd = open(plugin, O_RDONLY);
767 if (fd < 0) {
768 splashf(HZ*2, str(LANG_PLUGIN_CANT_OPEN), plugin);
769 return fd;
771 #if NUM_CORES > 1
772 /* Make sure COP cache is flushed and invalidated before loading */
773 my_core = switch_core(CURRENT_CORE ^ 1);
774 cpucache_invalidate();
775 switch_core(my_core);
776 #endif
778 readsize = read(fd, pluginbuf, PLUGIN_BUFFER_SIZE);
779 close(fd);
781 if (readsize < 0) {
782 splashf(HZ*2, str(LANG_READ_FAILED), plugin);
783 return -1;
785 hdr = (struct plugin_header *)pluginbuf;
787 if ((unsigned)readsize <= sizeof(struct plugin_header)
788 || hdr->magic != PLUGIN_MAGIC
789 || hdr->target_id != TARGET_ID
790 || hdr->load_addr != pluginbuf
791 || hdr->end_addr > pluginbuf + PLUGIN_BUFFER_SIZE) {
792 splash(HZ*2, str(LANG_PLUGIN_WRONG_MODEL));
793 return -1;
795 if (hdr->api_version > PLUGIN_API_VERSION
796 || hdr->api_version < PLUGIN_MIN_API_VERSION) {
797 splash(HZ*2, str(LANG_PLUGIN_WRONG_VERSION));
798 return -1;
800 plugin_size = hdr->end_addr - pluginbuf;
802 /* zero out bss area only, above guards end of pluginbuf */
803 if (plugin_size > readsize)
804 memset(pluginbuf + readsize, 0, plugin_size - readsize);
805 #endif
807 *(hdr->api) = &rockbox_api;
808 plugin_loaded = true;
811 #if defined HAVE_LCD_BITMAP && LCD_DEPTH > 1
812 old_backdrop = lcd_get_backdrop();
813 #endif
814 lcd_clear_display();
815 lcd_update();
817 #ifdef HAVE_REMOTE_LCD
818 lcd_remote_clear_display();
819 lcd_remote_update();
820 #endif
822 FOR_NB_SCREENS(i)
823 viewportmanager_theme_enable(i, false, NULL);
825 cpucache_invalidate();
827 #ifdef HAVE_TOUCHSCREEN
828 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
829 #endif
831 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
832 open_files = 0;
833 #endif
835 rc = hdr->entry_point(parameter);
837 /* Go back to the global setting in case the plugin changed it */
838 #ifdef HAVE_TOUCHSCREEN
839 touchscreen_set_mode(global_settings.touch_mode);
840 #endif
842 button_clear_queue();
844 #ifdef HAVE_LCD_BITMAP
845 lcd_setfont(FONT_UI);
846 #if LCD_DEPTH > 1
847 lcd_set_backdrop(old_backdrop);
848 #ifdef HAVE_LCD_COLOR
849 lcd_set_drawinfo(DRMODE_SOLID, global_settings.fg_color,
850 global_settings.bg_color);
851 #else
852 lcd_set_drawinfo(DRMODE_SOLID, LCD_DEFAULT_FG, LCD_DEFAULT_BG);
853 #endif
854 #else /* LCD_DEPTH == 1 */
855 lcd_set_drawmode(DRMODE_SOLID);
856 #endif /* LCD_DEPTH */
857 #endif /* HAVE_LCD_BITMAP */
860 #ifdef HAVE_REMOTE_LCD
861 #if LCD_REMOTE_DEPTH > 1
862 lcd_remote_set_drawinfo(DRMODE_SOLID, LCD_REMOTE_DEFAULT_FG,
863 LCD_REMOTE_DEFAULT_BG);
864 #else
865 lcd_remote_set_drawmode(DRMODE_SOLID);
866 #endif
867 #endif
869 lcd_clear_display();
870 #ifdef HAVE_LCD_REMOTE
871 lcd_remote_clear_display();
872 #endif
874 FOR_NB_SCREENS(i)
875 viewportmanager_theme_undo(i, false);
877 if (pfn_tsr_exit == NULL)
878 plugin_loaded = false;
880 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
881 if(open_files != 0 && !plugin_loaded)
883 int fd;
884 logf("Plugin '%s' leaks file handles", plugin);
886 static const char *lines[] =
887 { ID2P(LANG_PLUGIN_ERROR),
888 "#leak-file-handles" };
889 static const struct text_message message={ lines, 2 };
890 button_clear_queue(); /* Empty the keyboard buffer */
891 gui_syncyesno_run(&message, NULL, NULL);
893 for(fd=0; fd < MAX_OPEN_FILES; fd++)
894 if(open_files & (1<<fd))
895 close_wrapper(fd);
897 #endif
899 sim_plugin_close(pd);
901 if (rc == PLUGIN_ERROR)
902 splash(HZ*2, str(LANG_PLUGIN_ERROR));
904 return rc;
907 /* Returns a pointer to the portion of the plugin buffer that is not already
908 being used. If no plugin is loaded, returns the entire plugin buffer */
909 void* plugin_get_buffer(size_t *buffer_size)
911 int buffer_pos;
913 if (plugin_loaded)
915 if (plugin_size >= PLUGIN_BUFFER_SIZE)
916 return NULL;
918 *buffer_size = PLUGIN_BUFFER_SIZE-plugin_size;
919 buffer_pos = plugin_size;
921 else
923 *buffer_size = PLUGIN_BUFFER_SIZE;
924 buffer_pos = 0;
927 return &pluginbuf[buffer_pos];
930 /* Returns a pointer to the mp3 buffer.
931 Playback gets stopped, to avoid conflicts.
932 Talk buffer is stolen as well.
934 void* plugin_get_audio_buffer(size_t *buffer_size)
936 #if CONFIG_CODEC == SWCODEC
937 return audio_get_buffer(true, buffer_size);
938 #else
939 audio_stop();
940 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
941 *buffer_size = audiobufend - audiobuf;
942 return audiobuf;
943 #endif
946 #ifdef PLUGIN_USE_IRAM
947 /* Initializes plugin IRAM */
948 void plugin_iram_init(char *iramstart, char *iramcopy, size_t iram_size,
949 char *iedata, size_t iedata_size)
951 /* We need to stop audio playback in order to use codec IRAM */
952 audio_hard_stop();
953 memcpy(iramstart, iramcopy, iram_size);
954 memset(iedata, 0, iedata_size);
955 memset(iramcopy, 0, iram_size);
956 #if NUM_CORES > 1
957 /* writeback cleared iedata and iramcopy areas */
958 cpucache_flush();
959 #endif
961 #endif /* PLUGIN_USE_IRAM */
963 /* The plugin wants to stay resident after leaving its main function, e.g.
964 runs from timer or own thread. The callback is registered to later
965 instruct it to free its resources before a new plugin gets loaded. */
966 void plugin_tsr(bool (*exit_callback)(bool))
968 pfn_tsr_exit = exit_callback; /* remember the callback for later */
971 char *plugin_get_current_filename(void)
973 return current_plugin;
976 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
977 static int open_wrapper(const char* pathname, int flags)
979 int fd = PREFIX(open)(pathname,flags);
981 if(fd >= 0)
982 open_files |= 1<<fd;
984 return fd;
987 static int close_wrapper(int fd)
989 if((~open_files) & (1<<fd))
991 logf("double close from plugin");
993 if(fd >= 0)
994 open_files &= (~(1<<fd));
996 return PREFIX(close)(fd);
999 static int creat_wrapper(const char *pathname)
1001 int fd = PREFIX(creat)(pathname);
1003 if(fd >= 0)
1004 open_files |= (1<<fd);
1006 return fd;
1008 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */