1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
31 #include "backlight.h"
32 #include "sound_menu.h"
34 #include "powermgmt.h"
37 #include "option_select.h"
43 #include "diacritic.h"
44 #include "filefuncs.h"
50 #ifdef HAVE_LCD_BITMAP
51 #include "scrollbar.h"
52 #include "peakmeter.h"
58 #include "usbstack/usb_hid.h"
61 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
62 #define PREFIX(_x_) sim_ ## _x_
67 #if defined(HAVE_PLUGIN_CHECK_OPEN_CLOSE) && (MAX_OPEN_FILES>32)
68 #warning "MAX_OPEN_FILES>32, disabling plugin file open/close checking"
69 #undef HAVE_PLUGIN_CHECK_OPEN_CLOSE
72 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
73 static unsigned int open_files
;
76 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
77 static unsigned char pluginbuf
[PLUGIN_BUFFER_SIZE
];
78 void *sim_plugin_load(char *plugin
, void **pd
);
79 void sim_plugin_close(void *pd
);
80 void sim_lcd_ex_init(unsigned long (*getpixel
)(int, int));
81 void sim_lcd_ex_update_rect(int x
, int y
, int width
, int height
);
83 #define sim_plugin_close(x)
84 extern unsigned char pluginbuf
[];
88 /* for actual plugins only, not for codecs */
89 static bool plugin_loaded
= false;
90 static int plugin_size
= 0;
91 static bool (*pfn_tsr_exit
)(bool reenter
) = NULL
; /* TSR exit callback */
92 static char current_plugin
[MAX_PATH
];
94 char *plugin_get_current_filename(void);
96 /* Some wrappers used to monitor open and close and detect leaks*/
97 static int open_wrapper(const char* pathname
, int flags
, ...);
98 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
99 static int close_wrapper(int fd
);
100 static int creat_wrapper(const char *pathname
, mode_t mode
);
103 static const struct plugin_api rockbox_api
= {
106 #ifdef HAVE_LCD_CONTRAST
116 #ifdef HAVE_LCD_CHARCELLS
118 lcd_get_locked_pattern
,
126 &lcd_framebuffer
[0][0],
137 lcd_mono_bitmap_part
,
150 lcd_bitmap_transparent_part
,
151 lcd_bitmap_transparent
,
154 #if defined(TOSHIBA_GIGABEAT_F) || defined(SANSA_E200) || defined(SANSA_C200) \
155 || defined(IRIVER_H10) || defined(COWON_D2) || defined(PHILIPS_HDD1630) \
156 || defined(SANSA_FUZE) || defined(SANSA_E200V2) || defined(SANSA_FUZEV2) \
157 || defined(TOSHIBA_GIGABEAT_S)
160 #endif /* MEMORYSIZE > 2 */
161 #elif (LCD_DEPTH < 4) && (CONFIG_PLATFORM & PLATFORM_NATIVE)
164 #endif /* LCD_DEPTH */
165 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
167 lcd_pal256_update_pal
,
170 lcd_puts_scroll_style
,
171 #ifdef HAVE_LCD_INVERT
172 lcd_set_invert_display
,
173 #endif /* HAVE_LCD_INVERT */
174 #if defined(HAVE_LCD_MODES)
177 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
181 #ifdef HAVE_LCD_BITMAP
191 #endif /* HAVE_LCD_BITMAP */
196 backlight_set_timeout
,
197 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
198 backlight_set_brightness
,
199 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
202 backlight_set_timeout_plugged
,
208 #ifdef HAVE_REMOTE_LCD
210 lcd_remote_set_contrast
,
211 lcd_remote_clear_display
,
213 lcd_remote_puts_scroll
,
214 lcd_remote_stop_scroll
,
215 lcd_remote_set_drawmode
,
216 lcd_remote_get_drawmode
,
218 lcd_remote_getstringsize
,
219 lcd_remote_drawpixel
,
225 lcd_remote_mono_bitmap_part
,
226 lcd_remote_mono_bitmap
,
228 lcd_remote_puts_style
,
229 lcd_remote_puts_scroll_style
,
230 &lcd_remote_framebuffer
[0][0],
232 lcd_remote_update_rect
,
235 remote_backlight_off
,
236 remote_backlight_set_timeout
,
238 remote_backlight_set_timeout_plugged
,
240 #endif /* HAVE_REMOTE_LCD */
242 {&screens
[SCREEN_MAIN
], &screens
[SCREEN_REMOTE
]},
244 {&screens
[SCREEN_MAIN
]},
246 #if defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
247 lcd_remote_set_foreground
,
248 lcd_remote_get_foreground
,
249 lcd_remote_set_background
,
250 lcd_remote_get_background
,
251 lcd_remote_bitmap_part
,
254 viewport_set_defaults
,
255 #ifdef HAVE_LCD_BITMAP
256 viewportmanager_theme_enable
,
257 viewportmanager_theme_undo
,
262 gui_synclist_set_nb_items
,
263 gui_synclist_set_icon_callback
,
264 gui_synclist_get_nb_items
,
265 gui_synclist_get_sel_pos
,
267 gui_synclist_select_item
,
268 gui_synclist_add_item
,
269 gui_synclist_del_item
,
270 gui_synclist_limit_scroll
,
271 gui_synclist_do_button
,
272 gui_synclist_set_title
,
274 simplelist_info_init
,
275 simplelist_show_list
,
281 #ifdef HAVE_BUTTON_DATA
287 #ifdef HAS_BUTTON_HOLD
290 #ifdef HAVE_TOUCHSCREEN
291 touchscreen_set_mode
,
294 #ifdef HAVE_BUTTON_LIGHT
295 buttonlight_set_timeout
,
298 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
299 buttonlight_set_brightness
,
300 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
301 #endif /* HAVE_BUTTON_LIGHT */
305 (open_func
)open_wrapper
,
306 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
311 (read_func
)PREFIX(read
),
313 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
314 (creat_func
)creat_wrapper
,
316 (creat_func
)PREFIX(creat
),
318 (write_func
)PREFIX(write
),
329 #if USING_STORAGE_CALLBACK
330 register_storage_idle_func
,
331 unregister_storage_idle_func
,
332 #endif /* USING_STORAGE_CALLBACK */
334 create_numbered_filename
,
348 #if defined(CPU_ARM) && CONFIG_PLATFORM & PLATFORM_NATIVE
354 default_event_handler
,
355 default_event_handler_ex
,
359 #if (CONFIG_CODEC == SWCODEC)
361 #ifdef HAVE_PRIORITY_SCHEDULING
369 reset_poweroff_timer
,
370 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
374 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
375 #ifdef CPU_BOOST_LOGGING
380 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
381 #endif /* PLATFORM_NATIVE */
382 #ifdef HAVE_SCHEDULER_BOOSTCTRL
398 #if CONFIG_CODEC == SWCODEC
399 queue_enable_queue_send
,
406 #ifdef USB_ENABLE_HID
412 __cyg_profile_func_enter
,
413 __cyg_profile_func_exit
,
419 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
420 /* special simulator hooks */
421 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
423 sim_lcd_ex_update_rect
,
427 /* strings and memory */
441 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
468 #ifdef AUDIOHW_HAVE_EQ
469 sound_enum_hw_eq_band_setting
,
471 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
476 #if CONFIG_CODEC != SWCODEC
480 #if CONFIG_CODEC == SWCODEC
481 &audio_master_sampr_list
[0],
490 pcm_get_bytes_waiting
,
496 #ifdef HAVE_RECORDING
502 pcm_calculate_rec_peaks
,
503 audio_set_recording_gain
,
504 #endif /* HAVE_RECORDING */
505 #if INPUT_SRC_CAPS != 0
506 audio_set_output_source
,
507 audio_set_input_source
,
516 #endif /* CONFIG_CODEC == SWCODEC */
518 /* playback control */
524 playlist_remove_all_tracks
,
526 playlist_insert_track
,
527 playlist_insert_directory
,
539 audio_flush_and_reload_tracks
,
541 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
542 mpeg_get_last_header
,
544 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
545 (CONFIG_CODEC == SWCODEC)
549 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
550 /* MAS communication */
555 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
562 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
568 gui_syncstatusbar_draw
,
578 #ifdef HAVE_LCD_COLOR
582 /* action handling */
585 #ifdef HAVE_TOUCHSCREEN
586 action_get_touchscreen_press
,
594 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
599 # if CONFIG_CHARGING >= CHARGING_MONITOR
603 #ifdef HAVE_USB_POWER
608 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
621 plugin_get_audio_buffer
,
623 plugin_get_current_filename
,
624 #ifdef PLUGIN_USE_IRAM
627 #if defined(DEBUG) || defined(SIMULATOR)
630 #ifdef ROCKBOX_HAS_LOGF
636 #if CONFIG_CODEC == SWCODEC
637 codec_thread_do_callback
,
642 round_value_to_list32
,
643 #endif /* CONFIG_CODEC == SWCODEC */
649 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
650 peak_meter_scale_value
,
651 peak_meter_set_use_dbfs
,
652 peak_meter_get_use_dbfs
,
654 #ifdef HAVE_LCD_BITMAP
661 screen_dump_set_hook
,
668 #ifdef HAVE_WHEEL_POSITION
673 #ifdef IRIVER_H100_SERIES
674 /* Routines for the iriver_flash -plugin. */
675 detect_original_firmware
,
676 detect_flashed_ramimage
,
677 detect_flashed_romimage
,
680 #if (CONFIG_CODEC == SWCODEC)
693 buf_request_buffer_handle
,
700 tagcache_search_set_uniqbuf
,
701 tagcache_search_add_filter
,
704 tagcache_search_finish
,
705 tagcache_get_numeric
,
706 #ifdef HAVE_TC_RAMCACHE
712 search_albumart_files
,
715 #ifdef HAVE_SEMAPHORE_OBJECTS
723 /* new stuff at the end, sort into place next time
724 the API gets incompatible */
727 int plugin_load(const char* plugin
, const void* parameter
)
730 struct plugin_header
*hdr
;
731 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
733 #else /* PLATFOR_NATIVE */
739 #endif /* CONFIG_PLATFORM */
742 fb_data
* old_backdrop
;
745 if (pfn_tsr_exit
!= NULL
) /* if we have a resident old plugin: */
747 if (pfn_tsr_exit(!strcmp(current_plugin
, plugin
)) == false )
749 /* not allowing another plugin to load */
753 plugin_loaded
= false;
756 splash(0, ID2P(LANG_WAIT
));
757 strcpy(current_plugin
, plugin
);
759 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
760 hdr
= sim_plugin_load((char *)plugin
, &pd
);
762 splashf(HZ
*2, str(LANG_PLUGIN_CANT_OPEN
), plugin
);
766 || hdr
->magic
!= PLUGIN_MAGIC
767 || hdr
->target_id
!= TARGET_ID
) {
768 sim_plugin_close(pd
);
769 splash(HZ
*2, str(LANG_PLUGIN_WRONG_MODEL
));
772 if (hdr
->api_version
> PLUGIN_API_VERSION
773 || hdr
->api_version
< PLUGIN_MIN_API_VERSION
) {
774 sim_plugin_close(pd
);
775 splash(HZ
*2, str(LANG_PLUGIN_WRONG_VERSION
));
779 fd
= open(plugin
, O_RDONLY
);
781 splashf(HZ
*2, str(LANG_PLUGIN_CANT_OPEN
), plugin
);
785 /* Make sure COP cache is flushed and invalidated before loading */
786 my_core
= switch_core(CURRENT_CORE
^ 1);
787 cpucache_invalidate();
788 switch_core(my_core
);
791 readsize
= read(fd
, pluginbuf
, PLUGIN_BUFFER_SIZE
);
795 splashf(HZ
*2, str(LANG_READ_FAILED
), plugin
);
798 hdr
= (struct plugin_header
*)pluginbuf
;
800 if ((unsigned)readsize
<= sizeof(struct plugin_header
)
801 || hdr
->magic
!= PLUGIN_MAGIC
802 || hdr
->target_id
!= TARGET_ID
803 || hdr
->load_addr
!= pluginbuf
804 || hdr
->end_addr
> pluginbuf
+ PLUGIN_BUFFER_SIZE
) {
805 splash(HZ
*2, str(LANG_PLUGIN_WRONG_MODEL
));
808 if (hdr
->api_version
> PLUGIN_API_VERSION
809 || hdr
->api_version
< PLUGIN_MIN_API_VERSION
) {
810 splash(HZ
*2, str(LANG_PLUGIN_WRONG_VERSION
));
813 plugin_size
= hdr
->end_addr
- pluginbuf
;
815 /* zero out bss area only, above guards end of pluginbuf */
816 if (plugin_size
> readsize
)
817 memset(pluginbuf
+ readsize
, 0, plugin_size
- readsize
);
820 *(hdr
->api
) = &rockbox_api
;
821 plugin_loaded
= true;
824 #if defined HAVE_LCD_BITMAP && LCD_DEPTH > 1
825 old_backdrop
= lcd_get_backdrop();
830 #ifdef HAVE_REMOTE_LCD
831 lcd_remote_clear_display();
836 viewportmanager_theme_enable(i
, false, NULL
);
838 cpucache_invalidate();
840 #ifdef HAVE_TOUCHSCREEN
841 touchscreen_set_mode(TOUCHSCREEN_BUTTON
);
844 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
848 rc
= hdr
->entry_point(parameter
);
850 /* Go back to the global setting in case the plugin changed it */
851 #ifdef HAVE_TOUCHSCREEN
852 touchscreen_set_mode(global_settings
.touch_mode
);
855 button_clear_queue();
857 #ifdef HAVE_LCD_BITMAP
858 lcd_setfont(FONT_UI
);
860 lcd_set_backdrop(old_backdrop
);
861 #ifdef HAVE_LCD_COLOR
862 lcd_set_drawinfo(DRMODE_SOLID
, global_settings
.fg_color
,
863 global_settings
.bg_color
);
865 lcd_set_drawinfo(DRMODE_SOLID
, LCD_DEFAULT_FG
, LCD_DEFAULT_BG
);
867 #else /* LCD_DEPTH == 1 */
868 lcd_set_drawmode(DRMODE_SOLID
);
869 #endif /* LCD_DEPTH */
870 #endif /* HAVE_LCD_BITMAP */
873 #ifdef HAVE_REMOTE_LCD
874 #if LCD_REMOTE_DEPTH > 1
875 lcd_remote_set_drawinfo(DRMODE_SOLID
, LCD_REMOTE_DEFAULT_FG
,
876 LCD_REMOTE_DEFAULT_BG
);
878 lcd_remote_set_drawmode(DRMODE_SOLID
);
883 #ifdef HAVE_REMOTE_LCD
884 lcd_remote_clear_display();
888 viewportmanager_theme_undo(i
, false);
890 if (pfn_tsr_exit
== NULL
)
891 plugin_loaded
= false;
893 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
894 if(open_files
!= 0 && !plugin_loaded
)
897 logf("Plugin '%s' leaks file handles", plugin
);
899 static const char *lines
[] =
900 { ID2P(LANG_PLUGIN_ERROR
),
901 "#leak-file-handles" };
902 static const struct text_message message
={ lines
, 2 };
903 button_clear_queue(); /* Empty the keyboard buffer */
904 gui_syncyesno_run(&message
, NULL
, NULL
);
906 for(fd
=0; fd
< MAX_OPEN_FILES
; fd
++)
907 if(open_files
& (1<<fd
))
912 sim_plugin_close(pd
);
914 if (rc
== PLUGIN_ERROR
)
915 splash(HZ
*2, str(LANG_PLUGIN_ERROR
));
920 /* Returns a pointer to the portion of the plugin buffer that is not already
921 being used. If no plugin is loaded, returns the entire plugin buffer */
922 void* plugin_get_buffer(size_t *buffer_size
)
928 if (plugin_size
>= PLUGIN_BUFFER_SIZE
)
931 *buffer_size
= PLUGIN_BUFFER_SIZE
-plugin_size
;
932 buffer_pos
= plugin_size
;
936 *buffer_size
= PLUGIN_BUFFER_SIZE
;
940 return &pluginbuf
[buffer_pos
];
943 /* Returns a pointer to the mp3 buffer.
944 Playback gets stopped, to avoid conflicts.
945 Talk buffer is stolen as well.
947 void* plugin_get_audio_buffer(size_t *buffer_size
)
949 #if CONFIG_CODEC == SWCODEC
950 return audio_get_buffer(true, buffer_size
);
953 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
954 *buffer_size
= audiobufend
- audiobuf
;
959 #ifdef PLUGIN_USE_IRAM
960 /* Initializes plugin IRAM */
961 void plugin_iram_init(char *iramstart
, char *iramcopy
, size_t iram_size
,
962 char *iedata
, size_t iedata_size
)
964 /* We need to stop audio playback in order to use codec IRAM */
966 memcpy(iramstart
, iramcopy
, iram_size
);
967 memset(iedata
, 0, iedata_size
);
968 memset(iramcopy
, 0, iram_size
);
970 /* writeback cleared iedata and iramcopy areas */
974 #endif /* PLUGIN_USE_IRAM */
976 /* The plugin wants to stay resident after leaving its main function, e.g.
977 runs from timer or own thread. The callback is registered to later
978 instruct it to free its resources before a new plugin gets loaded. */
979 void plugin_tsr(bool (*exit_callback
)(bool))
981 pfn_tsr_exit
= exit_callback
; /* remember the callback for later */
984 char *plugin_get_current_filename(void)
986 return current_plugin
;
989 static int open_wrapper(const char* pathname
, int flags
, ...)
991 /* we don't have an 'open' function. it's a define. and we need
992 * the real file_open, hence PREFIX() doesn't work here */
994 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
999 fd
= sim_open(pathname
, flags
, va_arg(ap
, unsigned int));
1003 fd
= sim_open(pathname
, flags
);
1005 fd
= file_open(pathname
,flags
);
1008 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1010 open_files
|= 1<<fd
;
1015 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1016 static int close_wrapper(int fd
)
1018 if((~open_files
) & (1<<fd
))
1020 logf("double close from plugin");
1023 open_files
&= (~(1<<fd
));
1025 return PREFIX(close
)(fd
);
1028 static int creat_wrapper(const char *pathname
, mode_t mode
)
1030 int fd
= PREFIX(creat
)(pathname
, mode
);
1033 open_files
|= (1<<fd
);
1037 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */