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"
44 #include "diacritic.h"
50 #ifdef HAVE_LCD_BITMAP
51 #include "scrollbar.h"
52 #include "peakmeter.h"
58 #include "usbstack/usb_hid.h"
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
;
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 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
97 /* Some wrappers used to monitor open and close and detect leaks*/
98 static int open_wrapper(const char* pathname
, int flags
);
99 static int close_wrapper(int fd
);
100 static int creat_wrapper(const char *pathname
);
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) && !defined(SIMULATOR)
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)
188 #endif /* HAVE_LCD_BITMAP */
193 backlight_set_timeout
,
194 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
195 backlight_set_brightness
,
196 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
199 backlight_set_timeout_plugged
,
205 #ifdef HAVE_REMOTE_LCD
207 lcd_remote_set_contrast
,
208 lcd_remote_clear_display
,
210 lcd_remote_puts_scroll
,
211 lcd_remote_stop_scroll
,
212 lcd_remote_set_drawmode
,
213 lcd_remote_get_drawmode
,
215 lcd_remote_getstringsize
,
216 lcd_remote_drawpixel
,
222 lcd_remote_mono_bitmap_part
,
223 lcd_remote_mono_bitmap
,
225 lcd_remote_puts_style
,
226 lcd_remote_puts_scroll_style
,
227 &lcd_remote_framebuffer
[0][0],
229 lcd_remote_update_rect
,
232 remote_backlight_off
,
233 remote_backlight_set_timeout
,
235 remote_backlight_set_timeout_plugged
,
237 #endif /* HAVE_REMOTE_LCD */
239 {&screens
[SCREEN_MAIN
], &screens
[SCREEN_REMOTE
]},
241 {&screens
[SCREEN_MAIN
]},
243 #if defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
244 lcd_remote_set_foreground
,
245 lcd_remote_get_foreground
,
246 lcd_remote_set_background
,
247 lcd_remote_get_background
,
248 lcd_remote_bitmap_part
,
251 viewport_set_defaults
,
252 #ifdef HAVE_LCD_BITMAP
253 viewportmanager_theme_enable
,
254 viewportmanager_theme_undo
,
259 gui_synclist_set_nb_items
,
260 gui_synclist_set_icon_callback
,
261 gui_synclist_get_nb_items
,
262 gui_synclist_get_sel_pos
,
264 gui_synclist_select_item
,
265 gui_synclist_add_item
,
266 gui_synclist_del_item
,
267 gui_synclist_limit_scroll
,
268 gui_synclist_do_button
,
269 gui_synclist_set_title
,
271 simplelist_info_init
,
272 simplelist_show_list
,
278 #ifdef HAVE_BUTTON_DATA
284 #ifdef HAS_BUTTON_HOLD
287 #ifdef HAVE_TOUCHSCREEN
288 touchscreen_set_mode
,
291 #ifdef HAVE_BUTTON_LIGHT
292 buttonlight_set_timeout
,
295 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
296 buttonlight_set_brightness
,
297 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
298 #endif /* HAVE_BUTTON_LIGHT */
302 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
303 (open_func
)open_wrapper
,
306 (open_func
)PREFIX(open
),
309 (read_func
)PREFIX(read
),
311 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
312 (creat_func
)creat_wrapper
,
314 (creat_func
)PREFIX(creat
),
316 (write_func
)PREFIX(write
),
327 #if USING_STORAGE_CALLBACK
328 register_storage_idle_func
,
329 unregister_storage_idle_func
,
330 #endif /* USING_STORAGE_CALLBACK */
332 create_numbered_filename
,
352 default_event_handler
,
353 default_event_handler_ex
,
357 #if (CONFIG_CODEC == SWCODEC)
359 #ifdef HAVE_PRIORITY_SCHEDULING
367 reset_poweroff_timer
,
372 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
373 #ifdef CPU_BOOST_LOGGING
378 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
379 #endif /* !SIMULATOR */
380 #ifdef HAVE_SCHEDULER_BOOSTCTRL
396 #if CONFIG_CODEC == SWCODEC
397 queue_enable_queue_send
,
404 #ifdef USB_ENABLE_HID
410 __cyg_profile_func_enter
,
411 __cyg_profile_func_exit
,
418 /* special simulator hooks */
419 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
421 sim_lcd_ex_update_rect
,
425 /* strings and memory */
469 #if CONFIG_CODEC != SWCODEC
473 #if CONFIG_CODEC == SWCODEC
474 &audio_master_sampr_list
[0],
483 pcm_get_bytes_waiting
,
489 #ifdef HAVE_RECORDING
496 pcm_calculate_rec_peaks
,
497 audio_set_recording_gain
,
498 #endif /* HAVE_RECORDING */
499 #if INPUT_SRC_CAPS != 0
500 audio_set_output_source
,
501 audio_set_input_source
,
510 #endif /* CONFIG_CODEC == SWCODEC */
512 /* playback control */
518 playlist_remove_all_tracks
,
520 playlist_insert_track
,
521 playlist_insert_directory
,
533 audio_flush_and_reload_tracks
,
535 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
536 mpeg_get_last_header
,
538 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
539 (CONFIG_CODEC == SWCODEC)
543 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
544 /* MAS communication */
549 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
556 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
562 gui_syncstatusbar_draw
,
571 #ifdef HAVE_LCD_COLOR
575 /* action handling */
578 #ifdef HAVE_TOUCHSCREEN
579 action_get_touchscreen_press
,
592 # if CONFIG_CHARGING >= CHARGING_MONITOR
596 #ifdef HAVE_USB_POWER
601 #if !defined(SIMULATOR) || defined(__MINGW32__) || defined(__CYGWIN__)
614 plugin_get_audio_buffer
,
616 plugin_get_current_filename
,
617 #ifdef PLUGIN_USE_IRAM
620 #if defined(DEBUG) || defined(SIMULATOR)
623 #ifdef ROCKBOX_HAS_LOGF
629 #if CONFIG_CODEC == SWCODEC
630 codec_thread_do_callback
,
639 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
640 peak_meter_scale_value
,
641 peak_meter_set_use_dbfs
,
642 peak_meter_get_use_dbfs
,
644 #ifdef HAVE_LCD_BITMAP
651 screen_dump_set_hook
,
658 #ifdef HAVE_WHEEL_POSITION
663 #ifdef IRIVER_H100_SERIES
664 /* Routines for the iriver_flash -plugin. */
665 detect_original_firmware
,
666 detect_flashed_ramimage
,
667 detect_flashed_romimage
,
670 #if (CONFIG_CODEC == SWCODEC)
683 buf_request_buffer_handle
,
690 tagcache_search_set_uniqbuf
,
691 tagcache_search_add_filter
,
694 tagcache_search_finish
,
695 tagcache_get_numeric
,
696 #ifdef HAVE_TC_RAMCACHE
702 search_albumart_files
,
705 #ifdef HAVE_SEMAPHORE_OBJECTS
712 /* new stuff at the end, sort into place next time
713 the API gets incompatible */
715 #ifdef HAVE_LCD_BITMAP
720 int plugin_load(const char* plugin
, const void* parameter
)
723 struct plugin_header
*hdr
;
726 #else /* !SIMULATOR */
732 #endif /* !SIMULATOR */
735 fb_data
* old_backdrop
;
738 if (pfn_tsr_exit
!= NULL
) /* if we have a resident old plugin: */
740 if (pfn_tsr_exit(!strcmp(current_plugin
, plugin
)) == false )
742 /* not allowing another plugin to load */
746 plugin_loaded
= false;
749 splash(0, ID2P(LANG_WAIT
));
750 strcpy(current_plugin
, plugin
);
753 hdr
= sim_plugin_load((char *)plugin
, &pd
);
755 splashf(HZ
*2, str(LANG_PLUGIN_CANT_OPEN
), plugin
);
759 || hdr
->magic
!= PLUGIN_MAGIC
760 || hdr
->target_id
!= TARGET_ID
) {
761 sim_plugin_close(pd
);
762 splash(HZ
*2, str(LANG_PLUGIN_WRONG_MODEL
));
765 if (hdr
->api_version
> PLUGIN_API_VERSION
766 || hdr
->api_version
< PLUGIN_MIN_API_VERSION
) {
767 sim_plugin_close(pd
);
768 splash(HZ
*2, str(LANG_PLUGIN_WRONG_VERSION
));
772 fd
= open(plugin
, O_RDONLY
);
774 splashf(HZ
*2, str(LANG_PLUGIN_CANT_OPEN
), plugin
);
778 /* Make sure COP cache is flushed and invalidated before loading */
779 my_core
= switch_core(CURRENT_CORE
^ 1);
780 cpucache_invalidate();
781 switch_core(my_core
);
784 readsize
= read(fd
, pluginbuf
, PLUGIN_BUFFER_SIZE
);
788 splashf(HZ
*2, str(LANG_READ_FAILED
), plugin
);
791 hdr
= (struct plugin_header
*)pluginbuf
;
793 if ((unsigned)readsize
<= sizeof(struct plugin_header
)
794 || hdr
->magic
!= PLUGIN_MAGIC
795 || hdr
->target_id
!= TARGET_ID
796 || hdr
->load_addr
!= pluginbuf
797 || hdr
->end_addr
> pluginbuf
+ PLUGIN_BUFFER_SIZE
) {
798 splash(HZ
*2, str(LANG_PLUGIN_WRONG_MODEL
));
801 if (hdr
->api_version
> PLUGIN_API_VERSION
802 || hdr
->api_version
< PLUGIN_MIN_API_VERSION
) {
803 splash(HZ
*2, str(LANG_PLUGIN_WRONG_VERSION
));
806 plugin_size
= hdr
->end_addr
- pluginbuf
;
808 /* zero out bss area only, above guards end of pluginbuf */
809 if (plugin_size
> readsize
)
810 memset(pluginbuf
+ readsize
, 0, plugin_size
- readsize
);
813 *(hdr
->api
) = &rockbox_api
;
814 plugin_loaded
= true;
817 #if defined HAVE_LCD_BITMAP && LCD_DEPTH > 1
818 old_backdrop
= lcd_get_backdrop();
823 #ifdef HAVE_REMOTE_LCD
824 lcd_remote_clear_display();
829 viewportmanager_theme_enable(i
, false, NULL
);
831 cpucache_invalidate();
833 #ifdef HAVE_TOUCHSCREEN
834 touchscreen_set_mode(TOUCHSCREEN_BUTTON
);
837 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
841 rc
= hdr
->entry_point(parameter
);
843 /* Go back to the global setting in case the plugin changed it */
844 #ifdef HAVE_TOUCHSCREEN
845 touchscreen_set_mode(global_settings
.touch_mode
);
848 button_clear_queue();
850 #ifdef HAVE_LCD_BITMAP
851 lcd_setfont(FONT_UI
);
853 lcd_set_backdrop(old_backdrop
);
854 #ifdef HAVE_LCD_COLOR
855 lcd_set_drawinfo(DRMODE_SOLID
, global_settings
.fg_color
,
856 global_settings
.bg_color
);
858 lcd_set_drawinfo(DRMODE_SOLID
, LCD_DEFAULT_FG
, LCD_DEFAULT_BG
);
860 #else /* LCD_DEPTH == 1 */
861 lcd_set_drawmode(DRMODE_SOLID
);
862 #endif /* LCD_DEPTH */
863 #endif /* HAVE_LCD_BITMAP */
866 #ifdef HAVE_REMOTE_LCD
867 #if LCD_REMOTE_DEPTH > 1
868 lcd_remote_set_drawinfo(DRMODE_SOLID
, LCD_REMOTE_DEFAULT_FG
,
869 LCD_REMOTE_DEFAULT_BG
);
871 lcd_remote_set_drawmode(DRMODE_SOLID
);
876 #ifdef HAVE_LCD_REMOTE
877 lcd_remote_clear_display();
881 viewportmanager_theme_undo(i
, false);
883 if (pfn_tsr_exit
== NULL
)
884 plugin_loaded
= false;
886 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
887 if(open_files
!= 0 && !plugin_loaded
)
890 logf("Plugin '%s' leaks file handles", plugin
);
892 static const char *lines
[] =
893 { ID2P(LANG_PLUGIN_ERROR
),
894 "#leak-file-handles" };
895 static const struct text_message message
={ lines
, 2 };
896 button_clear_queue(); /* Empty the keyboard buffer */
897 gui_syncyesno_run(&message
, NULL
, NULL
);
899 for(fd
=0; fd
< MAX_OPEN_FILES
; fd
++)
900 if(open_files
& (1<<fd
))
905 sim_plugin_close(pd
);
907 if (rc
== PLUGIN_ERROR
)
908 splash(HZ
*2, str(LANG_PLUGIN_ERROR
));
913 /* Returns a pointer to the portion of the plugin buffer that is not already
914 being used. If no plugin is loaded, returns the entire plugin buffer */
915 void* plugin_get_buffer(size_t *buffer_size
)
921 if (plugin_size
>= PLUGIN_BUFFER_SIZE
)
924 *buffer_size
= PLUGIN_BUFFER_SIZE
-plugin_size
;
925 buffer_pos
= plugin_size
;
929 *buffer_size
= PLUGIN_BUFFER_SIZE
;
933 return &pluginbuf
[buffer_pos
];
936 /* Returns a pointer to the mp3 buffer.
937 Playback gets stopped, to avoid conflicts.
938 Talk buffer is stolen as well.
940 void* plugin_get_audio_buffer(size_t *buffer_size
)
942 #if CONFIG_CODEC == SWCODEC
943 return audio_get_buffer(true, buffer_size
);
946 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
947 *buffer_size
= audiobufend
- audiobuf
;
952 #ifdef PLUGIN_USE_IRAM
953 /* Initializes plugin IRAM */
954 void plugin_iram_init(char *iramstart
, char *iramcopy
, size_t iram_size
,
955 char *iedata
, size_t iedata_size
)
957 /* We need to stop audio playback in order to use codec IRAM */
959 memcpy(iramstart
, iramcopy
, iram_size
);
960 memset(iedata
, 0, iedata_size
);
961 memset(iramcopy
, 0, iram_size
);
963 /* writeback cleared iedata and iramcopy areas */
967 #endif /* PLUGIN_USE_IRAM */
969 /* The plugin wants to stay resident after leaving its main function, e.g.
970 runs from timer or own thread. The callback is registered to later
971 instruct it to free its resources before a new plugin gets loaded. */
972 void plugin_tsr(bool (*exit_callback
)(bool))
974 pfn_tsr_exit
= exit_callback
; /* remember the callback for later */
977 char *plugin_get_current_filename(void)
979 return current_plugin
;
982 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
983 static int open_wrapper(const char* pathname
, int flags
)
985 int fd
= PREFIX(open
)(pathname
,flags
);
993 static int close_wrapper(int fd
)
995 if((~open_files
) & (1<<fd
))
997 logf("double close from plugin");
1000 open_files
&= (~(1<<fd
));
1002 return PREFIX(close
)(fd
);
1005 static int creat_wrapper(const char *pathname
)
1007 int fd
= PREFIX(creat
)(pathname
);
1010 open_files
|= (1<<fd
);
1014 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */