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"
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 /* Some wrappers used to monitor open and close and detect leaks*/
96 static int open_wrapper(const char* pathname
, int flags
, ...);
97 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
98 static int close_wrapper(int fd
);
99 static int creat_wrapper(const char *pathname
, mode_t mode
);
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(SANSA_FUZEV2) \
156 || defined(TOSHIBA_GIGABEAT_S)
159 #endif /* MEMORYSIZE > 2 */
160 #elif (LCD_DEPTH < 4) && !defined(SIMULATOR)
163 #endif /* LCD_DEPTH */
164 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
166 lcd_pal256_update_pal
,
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)
176 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
187 #endif /* HAVE_LCD_BITMAP */
192 backlight_set_timeout
,
193 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
194 backlight_set_brightness
,
195 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
198 backlight_set_timeout_plugged
,
204 #ifdef HAVE_REMOTE_LCD
206 lcd_remote_set_contrast
,
207 lcd_remote_clear_display
,
209 lcd_remote_puts_scroll
,
210 lcd_remote_stop_scroll
,
211 lcd_remote_set_drawmode
,
212 lcd_remote_get_drawmode
,
214 lcd_remote_getstringsize
,
215 lcd_remote_drawpixel
,
221 lcd_remote_mono_bitmap_part
,
222 lcd_remote_mono_bitmap
,
224 lcd_remote_puts_style
,
225 lcd_remote_puts_scroll_style
,
226 &lcd_remote_framebuffer
[0][0],
228 lcd_remote_update_rect
,
231 remote_backlight_off
,
232 remote_backlight_set_timeout
,
234 remote_backlight_set_timeout_plugged
,
236 #endif /* HAVE_REMOTE_LCD */
238 {&screens
[SCREEN_MAIN
], &screens
[SCREEN_REMOTE
]},
240 {&screens
[SCREEN_MAIN
]},
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
,
250 viewport_set_defaults
,
251 #ifdef HAVE_LCD_BITMAP
252 viewportmanager_theme_enable
,
253 viewportmanager_theme_undo
,
258 gui_synclist_set_nb_items
,
259 gui_synclist_set_icon_callback
,
260 gui_synclist_get_nb_items
,
261 gui_synclist_get_sel_pos
,
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
,
270 simplelist_info_init
,
271 simplelist_show_list
,
277 #ifdef HAVE_BUTTON_DATA
283 #ifdef HAS_BUTTON_HOLD
286 #ifdef HAVE_TOUCHSCREEN
287 touchscreen_set_mode
,
290 #ifdef HAVE_BUTTON_LIGHT
291 buttonlight_set_timeout
,
294 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
295 buttonlight_set_brightness
,
296 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
297 #endif /* HAVE_BUTTON_LIGHT */
301 (open_func
)open_wrapper
,
302 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
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 */
713 #ifdef HAVE_LCD_BITMAP
718 int plugin_load(const char* plugin
, const void* parameter
)
721 struct plugin_header
*hdr
;
724 #else /* !SIMULATOR */
730 #endif /* !SIMULATOR */
733 fb_data
* old_backdrop
;
736 if (pfn_tsr_exit
!= NULL
) /* if we have a resident old plugin: */
738 if (pfn_tsr_exit(!strcmp(current_plugin
, plugin
)) == false )
740 /* not allowing another plugin to load */
744 plugin_loaded
= false;
747 splash(0, ID2P(LANG_WAIT
));
748 strcpy(current_plugin
, plugin
);
751 hdr
= sim_plugin_load((char *)plugin
, &pd
);
753 splashf(HZ
*2, str(LANG_PLUGIN_CANT_OPEN
), plugin
);
757 || hdr
->magic
!= PLUGIN_MAGIC
758 || hdr
->target_id
!= TARGET_ID
) {
759 sim_plugin_close(pd
);
760 splash(HZ
*2, str(LANG_PLUGIN_WRONG_MODEL
));
763 if (hdr
->api_version
> PLUGIN_API_VERSION
764 || hdr
->api_version
< PLUGIN_MIN_API_VERSION
) {
765 sim_plugin_close(pd
);
766 splash(HZ
*2, str(LANG_PLUGIN_WRONG_VERSION
));
770 fd
= open(plugin
, O_RDONLY
);
772 splashf(HZ
*2, str(LANG_PLUGIN_CANT_OPEN
), plugin
);
776 /* Make sure COP cache is flushed and invalidated before loading */
777 my_core
= switch_core(CURRENT_CORE
^ 1);
778 cpucache_invalidate();
779 switch_core(my_core
);
782 readsize
= read(fd
, pluginbuf
, PLUGIN_BUFFER_SIZE
);
786 splashf(HZ
*2, str(LANG_READ_FAILED
), plugin
);
789 hdr
= (struct plugin_header
*)pluginbuf
;
791 if ((unsigned)readsize
<= sizeof(struct plugin_header
)
792 || hdr
->magic
!= PLUGIN_MAGIC
793 || hdr
->target_id
!= TARGET_ID
794 || hdr
->load_addr
!= pluginbuf
795 || hdr
->end_addr
> pluginbuf
+ PLUGIN_BUFFER_SIZE
) {
796 splash(HZ
*2, str(LANG_PLUGIN_WRONG_MODEL
));
799 if (hdr
->api_version
> PLUGIN_API_VERSION
800 || hdr
->api_version
< PLUGIN_MIN_API_VERSION
) {
801 splash(HZ
*2, str(LANG_PLUGIN_WRONG_VERSION
));
804 plugin_size
= hdr
->end_addr
- pluginbuf
;
806 /* zero out bss area only, above guards end of pluginbuf */
807 if (plugin_size
> readsize
)
808 memset(pluginbuf
+ readsize
, 0, plugin_size
- readsize
);
811 *(hdr
->api
) = &rockbox_api
;
812 plugin_loaded
= true;
815 #if defined HAVE_LCD_BITMAP && LCD_DEPTH > 1
816 old_backdrop
= lcd_get_backdrop();
821 #ifdef HAVE_REMOTE_LCD
822 lcd_remote_clear_display();
827 viewportmanager_theme_enable(i
, false, NULL
);
829 cpucache_invalidate();
831 #ifdef HAVE_TOUCHSCREEN
832 touchscreen_set_mode(TOUCHSCREEN_BUTTON
);
835 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
839 rc
= hdr
->entry_point(parameter
);
841 /* Go back to the global setting in case the plugin changed it */
842 #ifdef HAVE_TOUCHSCREEN
843 touchscreen_set_mode(global_settings
.touch_mode
);
846 button_clear_queue();
848 #ifdef HAVE_LCD_BITMAP
849 lcd_setfont(FONT_UI
);
851 lcd_set_backdrop(old_backdrop
);
852 #ifdef HAVE_LCD_COLOR
853 lcd_set_drawinfo(DRMODE_SOLID
, global_settings
.fg_color
,
854 global_settings
.bg_color
);
856 lcd_set_drawinfo(DRMODE_SOLID
, LCD_DEFAULT_FG
, LCD_DEFAULT_BG
);
858 #else /* LCD_DEPTH == 1 */
859 lcd_set_drawmode(DRMODE_SOLID
);
860 #endif /* LCD_DEPTH */
861 #endif /* HAVE_LCD_BITMAP */
864 #ifdef HAVE_REMOTE_LCD
865 #if LCD_REMOTE_DEPTH > 1
866 lcd_remote_set_drawinfo(DRMODE_SOLID
, LCD_REMOTE_DEFAULT_FG
,
867 LCD_REMOTE_DEFAULT_BG
);
869 lcd_remote_set_drawmode(DRMODE_SOLID
);
874 #ifdef HAVE_LCD_REMOTE
875 lcd_remote_clear_display();
879 viewportmanager_theme_undo(i
, false);
881 if (pfn_tsr_exit
== NULL
)
882 plugin_loaded
= false;
884 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
885 if(open_files
!= 0 && !plugin_loaded
)
888 logf("Plugin '%s' leaks file handles", plugin
);
890 static const char *lines
[] =
891 { ID2P(LANG_PLUGIN_ERROR
),
892 "#leak-file-handles" };
893 static const struct text_message message
={ lines
, 2 };
894 button_clear_queue(); /* Empty the keyboard buffer */
895 gui_syncyesno_run(&message
, NULL
, NULL
);
897 for(fd
=0; fd
< MAX_OPEN_FILES
; fd
++)
898 if(open_files
& (1<<fd
))
903 sim_plugin_close(pd
);
905 if (rc
== PLUGIN_ERROR
)
906 splash(HZ
*2, str(LANG_PLUGIN_ERROR
));
911 /* Returns a pointer to the portion of the plugin buffer that is not already
912 being used. If no plugin is loaded, returns the entire plugin buffer */
913 void* plugin_get_buffer(size_t *buffer_size
)
919 if (plugin_size
>= PLUGIN_BUFFER_SIZE
)
922 *buffer_size
= PLUGIN_BUFFER_SIZE
-plugin_size
;
923 buffer_pos
= plugin_size
;
927 *buffer_size
= PLUGIN_BUFFER_SIZE
;
931 return &pluginbuf
[buffer_pos
];
934 /* Returns a pointer to the mp3 buffer.
935 Playback gets stopped, to avoid conflicts.
936 Talk buffer is stolen as well.
938 void* plugin_get_audio_buffer(size_t *buffer_size
)
940 #if CONFIG_CODEC == SWCODEC
941 return audio_get_buffer(true, buffer_size
);
944 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
945 *buffer_size
= audiobufend
- audiobuf
;
950 #ifdef PLUGIN_USE_IRAM
951 /* Initializes plugin IRAM */
952 void plugin_iram_init(char *iramstart
, char *iramcopy
, size_t iram_size
,
953 char *iedata
, size_t iedata_size
)
955 /* We need to stop audio playback in order to use codec IRAM */
957 memcpy(iramstart
, iramcopy
, iram_size
);
958 memset(iedata
, 0, iedata_size
);
959 memset(iramcopy
, 0, iram_size
);
961 /* writeback cleared iedata and iramcopy areas */
965 #endif /* PLUGIN_USE_IRAM */
967 /* The plugin wants to stay resident after leaving its main function, e.g.
968 runs from timer or own thread. The callback is registered to later
969 instruct it to free its resources before a new plugin gets loaded. */
970 void plugin_tsr(bool (*exit_callback
)(bool))
972 pfn_tsr_exit
= exit_callback
; /* remember the callback for later */
975 char *plugin_get_current_filename(void)
977 return current_plugin
;
980 static int open_wrapper(const char* pathname
, int flags
, ...)
982 /* we don't have an 'open' function. it's a define. and we need
983 * the real file_open, hence PREFIX() doesn't work here */
990 fd
= sim_open(pathname
, flags
, va_arg(ap
, unsigned int));
994 fd
= sim_open(pathname
, flags
);
996 fd
= file_open(pathname
,flags
);
999 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1001 open_files
|= 1<<fd
;
1006 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1007 static int close_wrapper(int fd
)
1009 if((~open_files
) & (1<<fd
))
1011 logf("double close from plugin");
1014 open_files
&= (~(1<<fd
));
1016 return PREFIX(close
)(fd
);
1019 static int creat_wrapper(const char *pathname
, mode_t mode
)
1021 int fd
= PREFIX(creat
)(pathname
, mode
);
1024 open_files
|= (1<<fd
);
1028 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */