Fix test_boost boost handling. Also show the number of loops per second.
[kugel-rb.git] / apps / plugin.c
blobc9f649b0961c3af2cc2f5481244276eec2d11749
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(SANSA_FUZEV2) \
156 || defined(TOSHIBA_GIGABEAT_S)
157 lcd_yuv_set_options,
158 #endif
159 #endif /* MEMORYSIZE > 2 */
160 #elif (LCD_DEPTH < 4) && !defined(SIMULATOR)
161 lcd_blit_mono,
162 lcd_blit_grey_phase,
163 #endif /* LCD_DEPTH */
164 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
165 lcd_blit_pal256,
166 lcd_pal256_update_pal,
167 #endif
168 lcd_puts_style,
169 lcd_puts_scroll_style,
170 #ifdef HAVE_LCD_INVERT
171 lcd_set_invert_display,
172 #endif /* HAVE_LCD_INVERT */
173 #if defined(HAVE_LCD_MODES)
174 lcd_set_mode,
175 #endif
176 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
177 &button_queue,
178 #endif
179 bidi_l2v,
180 font_get_bits,
181 font_load,
182 font_get,
183 font_getstringsize,
184 font_get_width,
185 screen_clear_area,
186 gui_scrollbar_draw,
187 #endif /* HAVE_LCD_BITMAP */
188 get_codepage_name,
190 backlight_on,
191 backlight_off,
192 backlight_set_timeout,
193 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
194 backlight_set_brightness,
195 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
197 #if CONFIG_CHARGING
198 backlight_set_timeout_plugged,
199 #endif
200 is_backlight_on,
201 splash,
202 splashf,
204 #ifdef HAVE_REMOTE_LCD
205 /* remote lcd */
206 lcd_remote_set_contrast,
207 lcd_remote_clear_display,
208 lcd_remote_puts,
209 lcd_remote_puts_scroll,
210 lcd_remote_stop_scroll,
211 lcd_remote_set_drawmode,
212 lcd_remote_get_drawmode,
213 lcd_remote_setfont,
214 lcd_remote_getstringsize,
215 lcd_remote_drawpixel,
216 lcd_remote_drawline,
217 lcd_remote_hline,
218 lcd_remote_vline,
219 lcd_remote_drawrect,
220 lcd_remote_fillrect,
221 lcd_remote_mono_bitmap_part,
222 lcd_remote_mono_bitmap,
223 lcd_remote_putsxy,
224 lcd_remote_puts_style,
225 lcd_remote_puts_scroll_style,
226 &lcd_remote_framebuffer[0][0],
227 lcd_remote_update,
228 lcd_remote_update_rect,
230 remote_backlight_on,
231 remote_backlight_off,
232 remote_backlight_set_timeout,
233 #if CONFIG_CHARGING
234 remote_backlight_set_timeout_plugged,
235 #endif
236 #endif /* HAVE_REMOTE_LCD */
237 #if NB_SCREENS == 2
238 {&screens[SCREEN_MAIN], &screens[SCREEN_REMOTE]},
239 #else
240 {&screens[SCREEN_MAIN]},
241 #endif
242 #if defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
243 lcd_remote_set_foreground,
244 lcd_remote_get_foreground,
245 lcd_remote_set_background,
246 lcd_remote_get_background,
247 lcd_remote_bitmap_part,
248 lcd_remote_bitmap,
249 #endif
250 viewport_set_defaults,
251 #ifdef HAVE_LCD_BITMAP
252 viewportmanager_theme_enable,
253 viewportmanager_theme_undo,
254 #endif
256 /* list */
257 gui_synclist_init,
258 gui_synclist_set_nb_items,
259 gui_synclist_set_icon_callback,
260 gui_synclist_get_nb_items,
261 gui_synclist_get_sel_pos,
262 gui_synclist_draw,
263 gui_synclist_select_item,
264 gui_synclist_add_item,
265 gui_synclist_del_item,
266 gui_synclist_limit_scroll,
267 gui_synclist_do_button,
268 gui_synclist_set_title,
269 gui_syncyesno_run,
270 simplelist_info_init,
271 simplelist_show_list,
273 /* button */
274 button_get,
275 button_get_w_tmo,
276 button_status,
277 #ifdef HAVE_BUTTON_DATA
278 button_get_data,
279 button_status_wdata,
280 #endif
281 button_clear_queue,
282 button_queue_count,
283 #ifdef HAS_BUTTON_HOLD
284 button_hold,
285 #endif
286 #ifdef HAVE_TOUCHSCREEN
287 touchscreen_set_mode,
288 #endif
290 #ifdef HAVE_BUTTON_LIGHT
291 buttonlight_set_timeout,
292 buttonlight_off,
293 buttonlight_on,
294 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
295 buttonlight_set_brightness,
296 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
297 #endif /* HAVE_BUTTON_LIGHT */
299 /* file */
300 open_utf8,
301 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
302 (open_func)open_wrapper,
303 close_wrapper,
304 #else
305 (open_func)PREFIX(open),
306 PREFIX(close),
307 #endif
308 (read_func)PREFIX(read),
309 PREFIX(lseek),
310 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
311 (creat_func)creat_wrapper,
312 #else
313 (creat_func)PREFIX(creat),
314 #endif
315 (write_func)PREFIX(write),
316 PREFIX(remove),
317 PREFIX(rename),
318 PREFIX(ftruncate),
319 PREFIX(filesize),
320 fdprintf,
321 read_line,
322 settings_parseline,
323 storage_sleep,
324 storage_spin,
325 storage_spindown,
326 #if USING_STORAGE_CALLBACK
327 register_storage_idle_func,
328 unregister_storage_idle_func,
329 #endif /* USING_STORAGE_CALLBACK */
330 reload_directory,
331 create_numbered_filename,
332 file_exists,
333 strip_extension,
334 crc_32,
336 /* dir */
337 opendir,
338 closedir,
339 readdir,
340 mkdir,
341 rmdir,
342 dir_exists,
344 /* kernel/ system */
345 #ifdef CPU_ARM
346 __div0,
347 #endif
348 PREFIX(sleep),
349 yield,
350 &current_tick,
351 default_event_handler,
352 default_event_handler_ex,
353 create_thread,
354 thread_exit,
355 thread_wait,
356 #if (CONFIG_CODEC == SWCODEC)
357 thread_thaw,
358 #ifdef HAVE_PRIORITY_SCHEDULING
359 thread_set_priority,
360 #endif
361 mutex_init,
362 mutex_lock,
363 mutex_unlock,
364 #endif
366 reset_poweroff_timer,
367 #ifndef SIMULATOR
368 system_memory_guard,
369 &cpu_frequency,
371 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
372 #ifdef CPU_BOOST_LOGGING
373 cpu_boost_,
374 #else
375 cpu_boost,
376 #endif
377 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
378 #endif /* !SIMULATOR */
379 #ifdef HAVE_SCHEDULER_BOOSTCTRL
380 trigger_cpu_boost,
381 cancel_cpu_boost,
382 #endif
383 #if NUM_CORES > 1
384 cpucache_flush,
385 cpucache_invalidate,
386 #endif
387 timer_register,
388 timer_unregister,
389 timer_set_period,
391 queue_init,
392 queue_delete,
393 queue_post,
394 queue_wait_w_tmo,
395 #if CONFIG_CODEC == SWCODEC
396 queue_enable_queue_send,
397 queue_empty,
398 queue_wait,
399 queue_send,
400 queue_reply,
401 #endif
402 usb_acknowledge,
403 #ifdef USB_ENABLE_HID
404 usb_hid_send,
405 #endif
406 #ifdef RB_PROFILE
407 profile_thread,
408 profstop,
409 __cyg_profile_func_enter,
410 __cyg_profile_func_exit,
411 #endif
412 add_event,
413 remove_event,
414 send_event,
416 #ifdef SIMULATOR
417 /* special simulator hooks */
418 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
419 sim_lcd_ex_init,
420 sim_lcd_ex_update_rect,
421 #endif
422 #endif
424 /* strings and memory */
425 snprintf,
426 vsnprintf,
427 strcpy,
428 strlcpy,
429 strlen,
430 strrchr,
431 strcmp,
432 strncmp,
433 strcasecmp,
434 strncasecmp,
435 memset,
436 memcpy,
437 memmove,
438 _ctype_,
439 atoi,
440 strchr,
441 strcat,
442 strlcat,
443 memchr,
444 memcmp,
445 strcasestr,
446 strtok_r,
447 /* unicode stuff */
448 utf8decode,
449 iso_decode,
450 utf16LEdecode,
451 utf16BEdecode,
452 utf8encode,
453 utf8length,
454 utf8seek,
456 /* sound */
457 sound_set,
458 sound_default,
459 sound_min,
460 sound_max,
461 sound_unit,
462 sound_val2phys,
463 #ifndef SIMULATOR
464 mp3_play_data,
465 mp3_play_pause,
466 mp3_play_stop,
467 mp3_is_playing,
468 #if CONFIG_CODEC != SWCODEC
469 bitswap,
470 #endif
471 #endif
472 #if CONFIG_CODEC == SWCODEC
473 &audio_master_sampr_list[0],
474 &hw_freq_sampr[0],
475 pcm_apply_settings,
476 pcm_play_data,
477 pcm_play_stop,
478 pcm_set_frequency,
479 pcm_is_playing,
480 pcm_is_paused,
481 pcm_play_pause,
482 pcm_get_bytes_waiting,
483 pcm_calculate_peaks,
484 pcm_get_peak_buffer,
485 pcm_play_lock,
486 pcm_play_unlock,
487 pcmbuf_beep,
488 #ifdef HAVE_RECORDING
489 &rec_freq_sampr[0],
490 pcm_init_recording,
491 pcm_close_recording,
492 pcm_record_data,
493 pcm_record_more,
494 pcm_stop_recording,
495 pcm_calculate_rec_peaks,
496 audio_set_recording_gain,
497 #endif /* HAVE_RECORDING */
498 #if INPUT_SRC_CAPS != 0
499 audio_set_output_source,
500 audio_set_input_source,
501 #endif
502 dsp_set_crossfeed,
503 dsp_set_eq,
504 dsp_dither_enable,
505 dsp_configure,
506 dsp_process,
507 dsp_input_count,
508 dsp_output_count,
509 #endif /* CONFIG_CODEC == SWCODEC */
511 /* playback control */
512 playlist_amount,
513 playlist_resume,
514 playlist_start,
515 playlist_add,
516 playlist_sync,
517 playlist_remove_all_tracks,
518 playlist_create,
519 playlist_insert_track,
520 playlist_insert_directory,
521 playlist_shuffle,
522 PREFIX(audio_play),
523 audio_stop,
524 audio_pause,
525 audio_resume,
526 audio_next,
527 audio_prev,
528 audio_ff_rewind,
529 audio_next_track,
530 audio_status,
531 audio_current_track,
532 audio_flush_and_reload_tracks,
533 audio_get_file_pos,
534 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
535 mpeg_get_last_header,
536 #endif
537 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
538 (CONFIG_CODEC == SWCODEC)
539 sound_set_pitch,
540 #endif
542 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
543 /* MAS communication */
544 mas_readmem,
545 mas_writemem,
546 mas_readreg,
547 mas_writereg,
548 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
549 mas_codec_writereg,
550 mas_codec_readreg,
551 i2c_begin,
552 i2c_end,
553 i2c_write,
554 #endif
555 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
557 /* menu */
558 do_menu,
559 /* statusbars */
560 &statusbars,
561 gui_syncstatusbar_draw,
562 /* options */
563 get_settings_list,
564 find_setting,
565 option_screen,
566 set_option,
567 set_bool_options,
568 set_int,
569 set_bool,
570 #ifdef HAVE_LCD_COLOR
571 set_color,
572 #endif
574 /* action handling */
575 get_custom_action,
576 get_action,
577 #ifdef HAVE_TOUCHSCREEN
578 action_get_touchscreen_press,
579 #endif
580 action_userabort,
582 /* power */
583 battery_level,
584 battery_level_safe,
585 battery_time,
586 #ifndef SIMULATOR
587 battery_voltage,
588 #endif
589 #if CONFIG_CHARGING
590 charger_inserted,
591 # if CONFIG_CHARGING >= CHARGING_MONITOR
592 charging_state,
593 # endif
594 #endif
595 #ifdef HAVE_USB_POWER
596 usb_powered,
597 #endif
599 /* misc */
600 #if !defined(SIMULATOR) || defined(__MINGW32__) || defined(__CYGWIN__)
601 &errno,
602 #endif
603 srand,
604 rand,
605 (qsort_func)qsort,
606 kbd_input,
607 get_time,
608 set_time,
609 #if CONFIG_RTC
610 mktime,
611 #endif
612 plugin_get_buffer,
613 plugin_get_audio_buffer,
614 plugin_tsr,
615 plugin_get_current_filename,
616 #ifdef PLUGIN_USE_IRAM
617 plugin_iram_init,
618 #endif
619 #if defined(DEBUG) || defined(SIMULATOR)
620 debugf,
621 #endif
622 #ifdef ROCKBOX_HAS_LOGF
623 _logf,
624 #endif
625 &global_settings,
626 &global_status,
627 talk_disable,
628 #if CONFIG_CODEC == SWCODEC
629 codec_thread_do_callback,
630 codec_load_file,
631 get_codec_filename,
632 #endif
633 get_metadata,
634 mp3info,
635 count_mp3_frames,
636 create_xing_header,
637 find_next_frame,
638 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
639 peak_meter_scale_value,
640 peak_meter_set_use_dbfs,
641 peak_meter_get_use_dbfs,
642 #endif
643 #ifdef HAVE_LCD_BITMAP
644 read_bmp_file,
645 read_bmp_fd,
646 #ifdef HAVE_JPEG
647 read_jpeg_file,
648 read_jpeg_fd,
649 #endif
650 screen_dump_set_hook,
651 #endif
652 show_logo,
653 tree_get_context,
654 set_current_file,
655 set_dirfilter,
657 #ifdef HAVE_WHEEL_POSITION
658 wheel_status,
659 wheel_send_events,
660 #endif
662 #ifdef IRIVER_H100_SERIES
663 /* Routines for the iriver_flash -plugin. */
664 detect_original_firmware,
665 detect_flashed_ramimage,
666 detect_flashed_romimage,
667 #endif
668 led,
669 #if (CONFIG_CODEC == SWCODEC)
670 bufopen,
671 bufalloc,
672 bufclose,
673 bufseek,
674 bufadvance,
675 bufread,
676 bufgetdata,
677 bufgettail,
678 bufcuttail,
680 buf_get_offset,
681 buf_handle_offset,
682 buf_request_buffer_handle,
683 buf_set_base_handle,
684 buf_used,
685 #endif
687 #ifdef HAVE_TAGCACHE
688 tagcache_search,
689 tagcache_search_set_uniqbuf,
690 tagcache_search_add_filter,
691 tagcache_get_next,
692 tagcache_retrieve,
693 tagcache_search_finish,
694 tagcache_get_numeric,
695 #ifdef HAVE_TC_RAMCACHE
696 tagcache_fill_tags,
697 #endif
698 #endif
700 #ifdef HAVE_ALBUMART
701 search_albumart_files,
702 #endif
704 #ifdef HAVE_SEMAPHORE_OBJECTS
705 semaphore_init,
706 semaphore_wait,
707 semaphore_release,
708 #endif
710 appsversion,
711 /* new stuff at the end, sort into place next time
712 the API gets incompatible */
715 int plugin_load(const char* plugin, const void* parameter)
717 int rc, i;
718 struct plugin_header *hdr;
719 #ifdef SIMULATOR
720 void *pd;
721 #else /* !SIMULATOR */
722 int fd;
723 ssize_t readsize;
724 #if NUM_CORES > 1
725 unsigned my_core;
726 #endif
727 #endif /* !SIMULATOR */
729 #if LCD_DEPTH > 1
730 fb_data* old_backdrop;
731 #endif
733 if (pfn_tsr_exit != NULL) /* if we have a resident old plugin: */
735 if (pfn_tsr_exit(!strcmp(current_plugin, plugin)) == false )
737 /* not allowing another plugin to load */
738 return PLUGIN_OK;
740 pfn_tsr_exit = NULL;
741 plugin_loaded = false;
744 splash(0, ID2P(LANG_WAIT));
745 strcpy(current_plugin, plugin);
747 #ifdef SIMULATOR
748 hdr = sim_plugin_load((char *)plugin, &pd);
749 if (pd == NULL) {
750 splashf(HZ*2, str(LANG_PLUGIN_CANT_OPEN), plugin);
751 return -1;
753 if (hdr == NULL
754 || hdr->magic != PLUGIN_MAGIC
755 || hdr->target_id != TARGET_ID) {
756 sim_plugin_close(pd);
757 splash(HZ*2, str(LANG_PLUGIN_WRONG_MODEL));
758 return -1;
760 if (hdr->api_version > PLUGIN_API_VERSION
761 || hdr->api_version < PLUGIN_MIN_API_VERSION) {
762 sim_plugin_close(pd);
763 splash(HZ*2, str(LANG_PLUGIN_WRONG_VERSION));
764 return -1;
766 #else
767 fd = open(plugin, O_RDONLY);
768 if (fd < 0) {
769 splashf(HZ*2, str(LANG_PLUGIN_CANT_OPEN), plugin);
770 return fd;
772 #if NUM_CORES > 1
773 /* Make sure COP cache is flushed and invalidated before loading */
774 my_core = switch_core(CURRENT_CORE ^ 1);
775 cpucache_invalidate();
776 switch_core(my_core);
777 #endif
779 readsize = read(fd, pluginbuf, PLUGIN_BUFFER_SIZE);
780 close(fd);
782 if (readsize < 0) {
783 splashf(HZ*2, str(LANG_READ_FAILED), plugin);
784 return -1;
786 hdr = (struct plugin_header *)pluginbuf;
788 if ((unsigned)readsize <= sizeof(struct plugin_header)
789 || hdr->magic != PLUGIN_MAGIC
790 || hdr->target_id != TARGET_ID
791 || hdr->load_addr != pluginbuf
792 || hdr->end_addr > pluginbuf + PLUGIN_BUFFER_SIZE) {
793 splash(HZ*2, str(LANG_PLUGIN_WRONG_MODEL));
794 return -1;
796 if (hdr->api_version > PLUGIN_API_VERSION
797 || hdr->api_version < PLUGIN_MIN_API_VERSION) {
798 splash(HZ*2, str(LANG_PLUGIN_WRONG_VERSION));
799 return -1;
801 plugin_size = hdr->end_addr - pluginbuf;
803 /* zero out bss area only, above guards end of pluginbuf */
804 if (plugin_size > readsize)
805 memset(pluginbuf + readsize, 0, plugin_size - readsize);
806 #endif
808 *(hdr->api) = &rockbox_api;
809 plugin_loaded = true;
812 #if defined HAVE_LCD_BITMAP && LCD_DEPTH > 1
813 old_backdrop = lcd_get_backdrop();
814 #endif
815 lcd_clear_display();
816 lcd_update();
818 #ifdef HAVE_REMOTE_LCD
819 lcd_remote_clear_display();
820 lcd_remote_update();
821 #endif
823 FOR_NB_SCREENS(i)
824 viewportmanager_theme_enable(i, false, NULL);
826 cpucache_invalidate();
828 #ifdef HAVE_TOUCHSCREEN
829 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
830 #endif
832 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
833 open_files = 0;
834 #endif
836 rc = hdr->entry_point(parameter);
838 /* Go back to the global setting in case the plugin changed it */
839 #ifdef HAVE_TOUCHSCREEN
840 touchscreen_set_mode(global_settings.touch_mode);
841 #endif
843 button_clear_queue();
845 #ifdef HAVE_LCD_BITMAP
846 lcd_setfont(FONT_UI);
847 #if LCD_DEPTH > 1
848 lcd_set_backdrop(old_backdrop);
849 #ifdef HAVE_LCD_COLOR
850 lcd_set_drawinfo(DRMODE_SOLID, global_settings.fg_color,
851 global_settings.bg_color);
852 #else
853 lcd_set_drawinfo(DRMODE_SOLID, LCD_DEFAULT_FG, LCD_DEFAULT_BG);
854 #endif
855 #else /* LCD_DEPTH == 1 */
856 lcd_set_drawmode(DRMODE_SOLID);
857 #endif /* LCD_DEPTH */
858 #endif /* HAVE_LCD_BITMAP */
861 #ifdef HAVE_REMOTE_LCD
862 #if LCD_REMOTE_DEPTH > 1
863 lcd_remote_set_drawinfo(DRMODE_SOLID, LCD_REMOTE_DEFAULT_FG,
864 LCD_REMOTE_DEFAULT_BG);
865 #else
866 lcd_remote_set_drawmode(DRMODE_SOLID);
867 #endif
868 #endif
870 lcd_clear_display();
871 #ifdef HAVE_LCD_REMOTE
872 lcd_remote_clear_display();
873 #endif
875 FOR_NB_SCREENS(i)
876 viewportmanager_theme_undo(i, false);
878 if (pfn_tsr_exit == NULL)
879 plugin_loaded = false;
881 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
882 if(open_files != 0 && !plugin_loaded)
884 int fd;
885 logf("Plugin '%s' leaks file handles", plugin);
887 static const char *lines[] =
888 { ID2P(LANG_PLUGIN_ERROR),
889 "#leak-file-handles" };
890 static const struct text_message message={ lines, 2 };
891 button_clear_queue(); /* Empty the keyboard buffer */
892 gui_syncyesno_run(&message, NULL, NULL);
894 for(fd=0; fd < MAX_OPEN_FILES; fd++)
895 if(open_files & (1<<fd))
896 close_wrapper(fd);
898 #endif
900 sim_plugin_close(pd);
902 if (rc == PLUGIN_ERROR)
903 splash(HZ*2, str(LANG_PLUGIN_ERROR));
905 return rc;
908 /* Returns a pointer to the portion of the plugin buffer that is not already
909 being used. If no plugin is loaded, returns the entire plugin buffer */
910 void* plugin_get_buffer(size_t *buffer_size)
912 int buffer_pos;
914 if (plugin_loaded)
916 if (plugin_size >= PLUGIN_BUFFER_SIZE)
917 return NULL;
919 *buffer_size = PLUGIN_BUFFER_SIZE-plugin_size;
920 buffer_pos = plugin_size;
922 else
924 *buffer_size = PLUGIN_BUFFER_SIZE;
925 buffer_pos = 0;
928 return &pluginbuf[buffer_pos];
931 /* Returns a pointer to the mp3 buffer.
932 Playback gets stopped, to avoid conflicts.
933 Talk buffer is stolen as well.
935 void* plugin_get_audio_buffer(size_t *buffer_size)
937 #if CONFIG_CODEC == SWCODEC
938 return audio_get_buffer(true, buffer_size);
939 #else
940 audio_stop();
941 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
942 *buffer_size = audiobufend - audiobuf;
943 return audiobuf;
944 #endif
947 #ifdef PLUGIN_USE_IRAM
948 /* Initializes plugin IRAM */
949 void plugin_iram_init(char *iramstart, char *iramcopy, size_t iram_size,
950 char *iedata, size_t iedata_size)
952 /* We need to stop audio playback in order to use codec IRAM */
953 audio_hard_stop();
954 memcpy(iramstart, iramcopy, iram_size);
955 memset(iedata, 0, iedata_size);
956 memset(iramcopy, 0, iram_size);
957 #if NUM_CORES > 1
958 /* writeback cleared iedata and iramcopy areas */
959 cpucache_flush();
960 #endif
962 #endif /* PLUGIN_USE_IRAM */
964 /* The plugin wants to stay resident after leaving its main function, e.g.
965 runs from timer or own thread. The callback is registered to later
966 instruct it to free its resources before a new plugin gets loaded. */
967 void plugin_tsr(bool (*exit_callback)(bool))
969 pfn_tsr_exit = exit_callback; /* remember the callback for later */
972 char *plugin_get_current_filename(void)
974 return current_plugin;
977 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
978 static int open_wrapper(const char* pathname, int flags)
980 int fd = PREFIX(open)(pathname,flags);
982 if(fd >= 0)
983 open_files |= 1<<fd;
985 return fd;
988 static int close_wrapper(int fd)
990 if((~open_files) & (1<<fd))
992 logf("double close from plugin");
994 if(fd >= 0)
995 open_files &= (~(1<<fd));
997 return PREFIX(close)(fd);
1000 static int creat_wrapper(const char *pathname)
1002 int fd = PREFIX(creat)(pathname);
1004 if(fd >= 0)
1005 open_files |= (1<<fd);
1007 return fd;
1009 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */