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 ****************************************************************************/
32 #include "backlight.h"
33 #include "sound_menu.h"
35 #include "powermgmt.h"
38 #include "option_select.h"
49 #ifdef HAVE_LCD_BITMAP
50 #include "scrollbar.h"
51 #include "peakmeter.h"
57 #include "usbstack/usb_hid.h"
61 #define PREFIX(_x_) sim_ ## _x_
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
71 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
72 static unsigned int open_files
;
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
);
82 #define sim_plugin_close(x)
83 extern unsigned char pluginbuf
[];
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
);
102 static const struct plugin_api rockbox_api
= {
105 #ifdef HAVE_LCD_CONTRAST
115 #ifdef HAVE_LCD_CHARCELLS
117 lcd_get_locked_pattern
,
125 &lcd_framebuffer
[0][0],
136 lcd_mono_bitmap_part
,
149 lcd_bitmap_transparent_part
,
150 lcd_bitmap_transparent
,
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)
158 #endif /* MEMORYSIZE > 2 */
159 #elif (LCD_DEPTH < 4) && !defined(SIMULATOR)
162 #endif /* LCD_DEPTH */
163 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
165 lcd_pal256_update_pal
,
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)
175 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
186 #endif /* HAVE_LCD_BITMAP */
191 backlight_set_timeout
,
192 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
193 backlight_set_brightness
,
194 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
197 backlight_set_timeout_plugged
,
203 #ifdef HAVE_REMOTE_LCD
205 lcd_remote_set_contrast
,
206 lcd_remote_clear_display
,
208 lcd_remote_puts_scroll
,
209 lcd_remote_stop_scroll
,
210 lcd_remote_set_drawmode
,
211 lcd_remote_get_drawmode
,
213 lcd_remote_getstringsize
,
214 lcd_remote_drawpixel
,
220 lcd_remote_mono_bitmap_part
,
221 lcd_remote_mono_bitmap
,
223 lcd_remote_puts_style
,
224 lcd_remote_puts_scroll_style
,
225 &lcd_remote_framebuffer
[0][0],
227 lcd_remote_update_rect
,
230 remote_backlight_off
,
231 remote_backlight_set_timeout
,
233 remote_backlight_set_timeout_plugged
,
235 #endif /* HAVE_REMOTE_LCD */
237 {&screens
[SCREEN_MAIN
], &screens
[SCREEN_REMOTE
]},
239 {&screens
[SCREEN_MAIN
]},
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
,
249 viewport_set_defaults
,
253 gui_synclist_set_nb_items
,
254 gui_synclist_set_icon_callback
,
255 gui_synclist_get_nb_items
,
256 gui_synclist_get_sel_pos
,
258 gui_synclist_select_item
,
259 gui_synclist_add_item
,
260 gui_synclist_del_item
,
261 gui_synclist_limit_scroll
,
262 gui_synclist_do_button
,
263 gui_synclist_set_title
,
265 simplelist_info_init
,
266 simplelist_show_list
,
272 #ifdef HAVE_BUTTON_DATA
278 #ifdef HAS_BUTTON_HOLD
281 #ifdef HAVE_TOUCHSCREEN
282 touchscreen_set_mode
,
285 #ifdef HAVE_BUTTON_LIGHT
286 buttonlight_set_timeout
,
289 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
290 buttonlight_set_brightness
,
291 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
292 #endif /* HAVE_BUTTON_LIGHT */
295 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
296 (open_func
)open_wrapper
,
299 (open_func
)PREFIX(open
),
302 (read_func
)PREFIX(read
),
304 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
305 (creat_func
)creat_wrapper
,
307 (creat_func
)PREFIX(creat
),
309 (write_func
)PREFIX(write
),
320 #if USING_STORAGE_CALLBACK
321 register_storage_idle_func
,
322 unregister_storage_idle_func
,
323 #endif /* USING_STORAGE_CALLBACK */
325 create_numbered_filename
,
344 default_event_handler
,
345 default_event_handler_ex
,
349 #if (CONFIG_CODEC == SWCODEC)
351 #ifdef HAVE_PRIORITY_SCHEDULING
359 reset_poweroff_timer
,
364 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
365 #ifdef CPU_BOOST_LOGGING
370 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
371 #endif /* !SIMULATOR */
372 #ifdef HAVE_SCHEDULER_BOOSTCTRL
388 #if CONFIG_CODEC == SWCODEC
389 queue_enable_queue_send
,
396 #ifdef USB_ENABLE_HID
402 __cyg_profile_func_enter
,
403 __cyg_profile_func_exit
,
410 /* special simulator hooks */
411 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
413 sim_lcd_ex_update_rect
,
417 /* strings and memory */
460 #if CONFIG_CODEC != SWCODEC
464 #if CONFIG_CODEC == SWCODEC
465 &audio_master_sampr_list
[0],
474 pcm_get_bytes_waiting
,
479 #ifdef HAVE_RECORDING
486 pcm_calculate_rec_peaks
,
487 audio_set_recording_gain
,
488 #endif /* HAVE_RECORDING */
489 #if INPUT_SRC_CAPS != 0
490 audio_set_output_source
,
491 audio_set_input_source
,
500 #endif /* CONFIG_CODEC == SWCODEC */
502 /* playback control */
508 playlist_remove_all_tracks
,
510 playlist_insert_track
,
511 playlist_insert_directory
,
523 audio_flush_and_reload_tracks
,
525 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
526 mpeg_get_last_header
,
528 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
529 (CONFIG_CODEC == SWCODEC)
533 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
534 /* MAS communication */
539 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
546 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
552 gui_syncstatusbar_draw
,
561 #ifdef HAVE_LCD_COLOR
565 /* action handling */
568 #ifdef HAVE_TOUCHSCREEN
569 action_get_touchscreen_press
,
582 # if CONFIG_CHARGING >= CHARGING_MONITOR
586 #ifdef HAVE_USB_POWER
601 plugin_get_audio_buffer
,
603 plugin_get_current_filename
,
604 #ifdef PLUGIN_USE_IRAM
607 #if defined(DEBUG) || defined(SIMULATOR)
610 #ifdef ROCKBOX_HAS_LOGF
616 #if CONFIG_CODEC == SWCODEC
617 codec_thread_do_callback
,
626 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
627 peak_meter_scale_value
,
628 peak_meter_set_use_dbfs
,
629 peak_meter_get_use_dbfs
,
631 #ifdef HAVE_LCD_BITMAP
638 screen_dump_set_hook
,
645 #ifdef HAVE_WHEEL_POSITION
650 #ifdef IRIVER_H100_SERIES
651 /* Routines for the iriver_flash -plugin. */
652 detect_original_firmware
,
653 detect_flashed_ramimage
,
654 detect_flashed_romimage
,
657 #if (CONFIG_CODEC == SWCODEC)
670 buf_request_buffer_handle
,
677 tagcache_search_set_uniqbuf
,
678 tagcache_search_add_filter
,
681 tagcache_search_finish
,
682 tagcache_get_numeric
,
683 #ifdef HAVE_TC_RAMCACHE
689 search_albumart_files
,
692 #ifdef HAVE_SEMAPHORE_OBJECTS
699 /* new stuff at the end, sort into place next time
700 the API gets incompatible */
701 #if (CONFIG_CODEC == SWCODEC)
706 #ifdef HAVE_LCD_BITMAP
707 viewportmanager_theme_enable
,
708 viewportmanager_theme_undo
,
710 #if !defined(SIMULATOR) || defined(__MINGW32__) || defined(__CYGWIN__)
716 int plugin_load(const char* plugin
, const void* parameter
)
719 struct plugin_header
*hdr
;
722 #else /* !SIMULATOR */
728 #endif /* !SIMULATOR */
731 fb_data
* old_backdrop
;
734 if (pfn_tsr_exit
!= NULL
) /* if we have a resident old plugin: */
736 if (pfn_tsr_exit(!strcmp(current_plugin
, plugin
)) == false )
738 /* not allowing another plugin to load */
742 plugin_loaded
= false;
745 splash(0, ID2P(LANG_WAIT
));
746 strcpy(current_plugin
, plugin
);
749 hdr
= sim_plugin_load((char *)plugin
, &pd
);
751 splashf(HZ
*2, str(LANG_PLUGIN_CANT_OPEN
), plugin
);
755 || hdr
->magic
!= PLUGIN_MAGIC
756 || hdr
->target_id
!= TARGET_ID
) {
757 sim_plugin_close(pd
);
758 splash(HZ
*2, str(LANG_PLUGIN_WRONG_MODEL
));
761 if (hdr
->api_version
> PLUGIN_API_VERSION
762 || hdr
->api_version
< PLUGIN_MIN_API_VERSION
) {
763 sim_plugin_close(pd
);
764 splash(HZ
*2, str(LANG_PLUGIN_WRONG_VERSION
));
768 fd
= open(plugin
, O_RDONLY
);
770 splashf(HZ
*2, str(LANG_PLUGIN_CANT_OPEN
), plugin
);
774 /* Make sure COP cache is flushed and invalidated before loading */
775 my_core
= switch_core(CURRENT_CORE
^ 1);
776 cpucache_invalidate();
777 switch_core(my_core
);
780 readsize
= read(fd
, pluginbuf
, PLUGIN_BUFFER_SIZE
);
784 splashf(HZ
*2, str(LANG_READ_FAILED
), plugin
);
787 hdr
= (struct plugin_header
*)pluginbuf
;
789 if ((unsigned)readsize
<= sizeof(struct plugin_header
)
790 || hdr
->magic
!= PLUGIN_MAGIC
791 || hdr
->target_id
!= TARGET_ID
792 || hdr
->load_addr
!= pluginbuf
793 || hdr
->end_addr
> pluginbuf
+ PLUGIN_BUFFER_SIZE
) {
794 splash(HZ
*2, str(LANG_PLUGIN_WRONG_MODEL
));
797 if (hdr
->api_version
> PLUGIN_API_VERSION
798 || hdr
->api_version
< PLUGIN_MIN_API_VERSION
) {
799 splash(HZ
*2, str(LANG_PLUGIN_WRONG_VERSION
));
802 plugin_size
= hdr
->end_addr
- pluginbuf
;
804 /* zero out bss area only, above guards end of pluginbuf */
805 if (plugin_size
> readsize
)
806 memset(pluginbuf
+ readsize
, 0, plugin_size
- readsize
);
809 *(hdr
->api
) = &rockbox_api
;
810 plugin_loaded
= true;
813 #if defined HAVE_LCD_BITMAP && LCD_DEPTH > 1
814 old_backdrop
= lcd_get_backdrop();
819 #ifdef HAVE_REMOTE_LCD
820 lcd_remote_clear_display();
825 viewportmanager_theme_enable(i
, false, NULL
);
827 cpucache_invalidate();
829 #ifdef HAVE_TOUCHSCREEN
830 touchscreen_set_mode(TOUCHSCREEN_BUTTON
);
833 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
837 rc
= hdr
->entry_point(parameter
);
839 /* Go back to the global setting in case the plugin changed it */
840 #ifdef HAVE_TOUCHSCREEN
841 touchscreen_set_mode(global_settings
.touch_mode
);
844 button_clear_queue();
846 #ifdef HAVE_LCD_BITMAP
847 lcd_setfont(FONT_UI
);
849 lcd_set_backdrop(old_backdrop
);
850 #ifdef HAVE_LCD_COLOR
851 lcd_set_drawinfo(DRMODE_SOLID
, global_settings
.fg_color
,
852 global_settings
.bg_color
);
854 lcd_set_drawinfo(DRMODE_SOLID
, LCD_DEFAULT_FG
, LCD_DEFAULT_BG
);
856 #else /* LCD_DEPTH == 1 */
857 lcd_set_drawmode(DRMODE_SOLID
);
858 #endif /* LCD_DEPTH */
859 #endif /* HAVE_LCD_BITMAP */
862 #ifdef HAVE_REMOTE_LCD
863 #if LCD_REMOTE_DEPTH > 1
864 lcd_remote_set_drawinfo(DRMODE_SOLID
, LCD_REMOTE_DEFAULT_FG
,
865 LCD_REMOTE_DEFAULT_BG
);
867 lcd_remote_set_drawmode(DRMODE_SOLID
);
872 #ifdef HAVE_LCD_REMOTE
873 lcd_remote_clear_display();
877 viewportmanager_theme_undo(i
, false);
879 if (pfn_tsr_exit
== NULL
)
880 plugin_loaded
= false;
882 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
883 if(open_files
!= 0 && !plugin_loaded
)
886 logf("Plugin '%s' leaks file handles", plugin
);
888 static const char *lines
[] =
889 { ID2P(LANG_PLUGIN_ERROR
),
890 "#leak-file-handles" };
891 static const struct text_message message
={ lines
, 2 };
892 button_clear_queue(); /* Empty the keyboard buffer */
893 gui_syncyesno_run(&message
, NULL
, NULL
);
895 for(fd
=0; fd
< MAX_OPEN_FILES
; fd
++)
896 if(open_files
& (1<<fd
))
901 sim_plugin_close(pd
);
903 if (rc
== PLUGIN_ERROR
)
904 splash(HZ
*2, str(LANG_PLUGIN_ERROR
));
909 /* Returns a pointer to the portion of the plugin buffer that is not already
910 being used. If no plugin is loaded, returns the entire plugin buffer */
911 void* plugin_get_buffer(size_t *buffer_size
)
917 if (plugin_size
>= PLUGIN_BUFFER_SIZE
)
920 *buffer_size
= PLUGIN_BUFFER_SIZE
-plugin_size
;
921 buffer_pos
= plugin_size
;
925 *buffer_size
= PLUGIN_BUFFER_SIZE
;
929 return &pluginbuf
[buffer_pos
];
932 /* Returns a pointer to the mp3 buffer.
933 Playback gets stopped, to avoid conflicts.
934 Talk buffer is stolen as well.
936 void* plugin_get_audio_buffer(size_t *buffer_size
)
938 #if CONFIG_CODEC == SWCODEC
939 return audio_get_buffer(true, buffer_size
);
942 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
943 *buffer_size
= audiobufend
- audiobuf
;
948 #ifdef PLUGIN_USE_IRAM
949 /* Initializes plugin IRAM */
950 void plugin_iram_init(char *iramstart
, char *iramcopy
, size_t iram_size
,
951 char *iedata
, size_t iedata_size
)
953 /* We need to stop audio playback in order to use codec IRAM */
955 memcpy(iramstart
, iramcopy
, iram_size
);
956 memset(iedata
, 0, iedata_size
);
957 memset(iramcopy
, 0, iram_size
);
959 /* writeback cleared iedata and iramcopy areas */
963 #endif /* PLUGIN_USE_IRAM */
965 /* The plugin wants to stay resident after leaving its main function, e.g.
966 runs from timer or own thread. The callback is registered to later
967 instruct it to free its resources before a new plugin gets loaded. */
968 void plugin_tsr(bool (*exit_callback
)(bool))
970 pfn_tsr_exit
= exit_callback
; /* remember the callback for later */
973 char *plugin_get_current_filename(void)
975 return current_plugin
;
978 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
979 static int open_wrapper(const char* pathname
, int flags
)
981 int fd
= PREFIX(open
)(pathname
,flags
);
989 static int close_wrapper(int fd
)
991 if((~open_files
) & (1<<fd
))
993 logf("double close from plugin");
996 open_files
&= (~(1<<fd
));
998 return PREFIX(close
)(fd
);
1001 static int creat_wrapper(const char *pathname
)
1003 int fd
= PREFIX(creat
)(pathname
);
1006 open_files
|= (1<<fd
);
1010 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */