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
,
250 #ifdef HAVE_LCD_BITMAP
251 viewportmanager_theme_enable
,
252 viewportmanager_theme_undo
,
257 gui_synclist_set_nb_items
,
258 gui_synclist_set_icon_callback
,
259 gui_synclist_get_nb_items
,
260 gui_synclist_get_sel_pos
,
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
,
269 simplelist_info_init
,
270 simplelist_show_list
,
276 #ifdef HAVE_BUTTON_DATA
282 #ifdef HAS_BUTTON_HOLD
285 #ifdef HAVE_TOUCHSCREEN
286 touchscreen_set_mode
,
289 #ifdef HAVE_BUTTON_LIGHT
290 buttonlight_set_timeout
,
293 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
294 buttonlight_set_brightness
,
295 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
296 #endif /* HAVE_BUTTON_LIGHT */
300 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
301 (open_func
)open_wrapper
,
304 (open_func
)PREFIX(open
),
307 (read_func
)PREFIX(read
),
309 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
310 (creat_func
)creat_wrapper
,
312 (creat_func
)PREFIX(creat
),
314 (write_func
)PREFIX(write
),
325 #if USING_STORAGE_CALLBACK
326 register_storage_idle_func
,
327 unregister_storage_idle_func
,
328 #endif /* USING_STORAGE_CALLBACK */
330 create_numbered_filename
,
350 default_event_handler
,
351 default_event_handler_ex
,
355 #if (CONFIG_CODEC == SWCODEC)
357 #ifdef HAVE_PRIORITY_SCHEDULING
365 reset_poweroff_timer
,
370 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
371 #ifdef CPU_BOOST_LOGGING
376 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
377 #endif /* !SIMULATOR */
378 #ifdef HAVE_SCHEDULER_BOOSTCTRL
394 #if CONFIG_CODEC == SWCODEC
395 queue_enable_queue_send
,
402 #ifdef USB_ENABLE_HID
408 __cyg_profile_func_enter
,
409 __cyg_profile_func_exit
,
416 /* special simulator hooks */
417 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
419 sim_lcd_ex_update_rect
,
423 /* strings and memory */
467 #if CONFIG_CODEC != SWCODEC
471 #if CONFIG_CODEC == SWCODEC
472 &audio_master_sampr_list
[0],
481 pcm_get_bytes_waiting
,
487 #ifdef HAVE_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
,
508 #endif /* CONFIG_CODEC == SWCODEC */
510 /* playback control */
516 playlist_remove_all_tracks
,
518 playlist_insert_track
,
519 playlist_insert_directory
,
531 audio_flush_and_reload_tracks
,
533 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
534 mpeg_get_last_header
,
536 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
537 (CONFIG_CODEC == SWCODEC)
541 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
542 /* MAS communication */
547 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
554 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
560 gui_syncstatusbar_draw
,
569 #ifdef HAVE_LCD_COLOR
573 /* action handling */
576 #ifdef HAVE_TOUCHSCREEN
577 action_get_touchscreen_press
,
590 # if CONFIG_CHARGING >= CHARGING_MONITOR
594 #ifdef HAVE_USB_POWER
599 #if !defined(SIMULATOR) || defined(__MINGW32__) || defined(__CYGWIN__)
612 plugin_get_audio_buffer
,
614 plugin_get_current_filename
,
615 #ifdef PLUGIN_USE_IRAM
618 #if defined(DEBUG) || defined(SIMULATOR)
621 #ifdef ROCKBOX_HAS_LOGF
627 #if CONFIG_CODEC == SWCODEC
628 codec_thread_do_callback
,
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
,
642 #ifdef HAVE_LCD_BITMAP
649 screen_dump_set_hook
,
656 #ifdef HAVE_WHEEL_POSITION
661 #ifdef IRIVER_H100_SERIES
662 /* Routines for the iriver_flash -plugin. */
663 detect_original_firmware
,
664 detect_flashed_ramimage
,
665 detect_flashed_romimage
,
668 #if (CONFIG_CODEC == SWCODEC)
681 buf_request_buffer_handle
,
688 tagcache_search_set_uniqbuf
,
689 tagcache_search_add_filter
,
692 tagcache_search_finish
,
693 tagcache_get_numeric
,
694 #ifdef HAVE_TC_RAMCACHE
700 search_albumart_files
,
703 #ifdef HAVE_SEMAPHORE_OBJECTS
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
)
717 struct plugin_header
*hdr
;
720 #else /* !SIMULATOR */
726 #endif /* !SIMULATOR */
729 fb_data
* old_backdrop
;
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 */
740 plugin_loaded
= false;
743 splash(0, ID2P(LANG_WAIT
));
744 strcpy(current_plugin
, plugin
);
747 hdr
= sim_plugin_load((char *)plugin
, &pd
);
749 splashf(HZ
*2, str(LANG_PLUGIN_CANT_OPEN
), plugin
);
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
));
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
));
766 fd
= open(plugin
, O_RDONLY
);
768 splashf(HZ
*2, str(LANG_PLUGIN_CANT_OPEN
), plugin
);
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
);
778 readsize
= read(fd
, pluginbuf
, PLUGIN_BUFFER_SIZE
);
782 splashf(HZ
*2, str(LANG_READ_FAILED
), plugin
);
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
));
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
));
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
);
807 *(hdr
->api
) = &rockbox_api
;
808 plugin_loaded
= true;
811 #if defined HAVE_LCD_BITMAP && LCD_DEPTH > 1
812 old_backdrop
= lcd_get_backdrop();
817 #ifdef HAVE_REMOTE_LCD
818 lcd_remote_clear_display();
823 viewportmanager_theme_enable(i
, false, NULL
);
825 cpucache_invalidate();
827 #ifdef HAVE_TOUCHSCREEN
828 touchscreen_set_mode(TOUCHSCREEN_BUTTON
);
831 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
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
);
842 button_clear_queue();
844 #ifdef HAVE_LCD_BITMAP
845 lcd_setfont(FONT_UI
);
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
);
852 lcd_set_drawinfo(DRMODE_SOLID
, LCD_DEFAULT_FG
, LCD_DEFAULT_BG
);
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
);
865 lcd_remote_set_drawmode(DRMODE_SOLID
);
870 #ifdef HAVE_LCD_REMOTE
871 lcd_remote_clear_display();
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
)
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
))
899 sim_plugin_close(pd
);
901 if (rc
== PLUGIN_ERROR
)
902 splash(HZ
*2, str(LANG_PLUGIN_ERROR
));
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
)
915 if (plugin_size
>= PLUGIN_BUFFER_SIZE
)
918 *buffer_size
= PLUGIN_BUFFER_SIZE
-plugin_size
;
919 buffer_pos
= plugin_size
;
923 *buffer_size
= PLUGIN_BUFFER_SIZE
;
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
);
940 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
941 *buffer_size
= audiobufend
- audiobuf
;
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 */
953 memcpy(iramstart
, iramcopy
, iram_size
);
954 memset(iedata
, 0, iedata_size
);
955 memset(iramcopy
, 0, iram_size
);
957 /* writeback cleared iedata and iramcopy areas */
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
);
987 static int close_wrapper(int fd
)
989 if((~open_files
) & (1<<fd
))
991 logf("double close from plugin");
994 open_files
&= (~(1<<fd
));
996 return PREFIX(close
)(fd
);
999 static int creat_wrapper(const char *pathname
)
1001 int fd
= PREFIX(creat
)(pathname
);
1004 open_files
|= (1<<fd
);
1008 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */