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"
45 #include "load_code.h"
51 #ifdef HAVE_LCD_BITMAP
52 #include "scrollbar.h"
53 #include "peakmeter.h"
59 #include "usbstack/usb_hid.h"
62 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
63 #define PREFIX(_x_) sim_ ## _x_
68 #if defined(HAVE_PLUGIN_CHECK_OPEN_CLOSE) && (MAX_OPEN_FILES>32)
69 #warning "MAX_OPEN_FILES>32, disabling plugin file open/close checking"
70 #undef HAVE_PLUGIN_CHECK_OPEN_CLOSE
73 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
74 static unsigned int open_files
;
77 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
78 static unsigned char pluginbuf
[PLUGIN_BUFFER_SIZE
];
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 extern unsigned char pluginbuf
[];
86 /* for actual plugins only, not for codecs */
87 static int plugin_size
= 0;
88 static bool (*pfn_tsr_exit
)(bool reenter
) = NULL
; /* TSR exit callback */
89 static char current_plugin
[MAX_PATH
];
90 /* NULL if no plugin is loaded, otherwise the handle that lc_open() returned */
91 static void *current_plugin_handle
;
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) && (CONFIG_PLATFORM & PLATFORM_NATIVE)
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)
180 #ifdef HAVE_LCD_BITMAP
190 #endif /* HAVE_LCD_BITMAP */
195 backlight_set_timeout
,
196 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
197 backlight_set_brightness
,
198 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
201 backlight_set_timeout_plugged
,
207 #ifdef HAVE_REMOTE_LCD
209 lcd_remote_set_contrast
,
210 lcd_remote_clear_display
,
212 lcd_remote_puts_scroll
,
213 lcd_remote_stop_scroll
,
214 lcd_remote_set_drawmode
,
215 lcd_remote_get_drawmode
,
217 lcd_remote_getstringsize
,
218 lcd_remote_drawpixel
,
224 lcd_remote_mono_bitmap_part
,
225 lcd_remote_mono_bitmap
,
227 lcd_remote_puts_style
,
228 lcd_remote_puts_scroll_style
,
229 &lcd_remote_framebuffer
[0][0],
231 lcd_remote_update_rect
,
234 remote_backlight_off
,
235 remote_backlight_set_timeout
,
237 remote_backlight_set_timeout_plugged
,
239 #endif /* HAVE_REMOTE_LCD */
241 {&screens
[SCREEN_MAIN
], &screens
[SCREEN_REMOTE
]},
243 {&screens
[SCREEN_MAIN
]},
245 #if defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
246 lcd_remote_set_foreground
,
247 lcd_remote_get_foreground
,
248 lcd_remote_set_background
,
249 lcd_remote_get_background
,
250 lcd_remote_bitmap_part
,
253 viewport_set_defaults
,
254 #ifdef HAVE_LCD_BITMAP
255 viewportmanager_theme_enable
,
256 viewportmanager_theme_undo
,
261 gui_synclist_set_nb_items
,
262 gui_synclist_set_icon_callback
,
263 gui_synclist_get_nb_items
,
264 gui_synclist_get_sel_pos
,
266 gui_synclist_select_item
,
267 gui_synclist_add_item
,
268 gui_synclist_del_item
,
269 gui_synclist_limit_scroll
,
270 gui_synclist_do_button
,
271 gui_synclist_set_title
,
273 simplelist_info_init
,
274 simplelist_show_list
,
280 #ifdef HAVE_BUTTON_DATA
286 #ifdef HAS_BUTTON_HOLD
289 #ifdef HAVE_TOUCHSCREEN
290 touchscreen_set_mode
,
293 #ifdef HAVE_BUTTON_LIGHT
294 buttonlight_set_timeout
,
297 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
298 buttonlight_set_brightness
,
299 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
300 #endif /* HAVE_BUTTON_LIGHT */
304 (open_func
)open_wrapper
,
305 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
310 (read_func
)PREFIX(read
),
312 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
313 (creat_func
)creat_wrapper
,
315 (creat_func
)PREFIX(creat
),
317 (write_func
)PREFIX(write
),
328 #if USING_STORAGE_CALLBACK
329 register_storage_idle_func
,
330 unregister_storage_idle_func
,
331 #endif /* USING_STORAGE_CALLBACK */
333 create_numbered_filename
,
347 #if defined(CPU_ARM) && CONFIG_PLATFORM & PLATFORM_NATIVE
353 default_event_handler
,
354 default_event_handler_ex
,
358 #if (CONFIG_CODEC == SWCODEC)
360 #ifdef HAVE_PRIORITY_SCHEDULING
368 reset_poweroff_timer
,
369 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
373 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
374 #ifdef CPU_BOOST_LOGGING
379 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
380 #endif /* PLATFORM_NATIVE */
381 #ifdef HAVE_SCHEDULER_BOOSTCTRL
397 #if CONFIG_CODEC == SWCODEC
398 queue_enable_queue_send
,
405 #ifdef USB_ENABLE_HID
411 __cyg_profile_func_enter
,
412 __cyg_profile_func_exit
,
418 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
419 /* special simulator hooks */
420 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
422 sim_lcd_ex_update_rect
,
426 /* strings and memory */
440 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
467 #ifdef AUDIOHW_HAVE_EQ
468 sound_enum_hw_eq_band_setting
,
470 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
475 #if CONFIG_CODEC != SWCODEC
479 #if CONFIG_CODEC == SWCODEC
480 &audio_master_sampr_list
[0],
489 pcm_get_bytes_waiting
,
495 #ifdef HAVE_RECORDING
501 pcm_calculate_rec_peaks
,
502 audio_set_recording_gain
,
503 #endif /* HAVE_RECORDING */
504 #if INPUT_SRC_CAPS != 0
505 audio_set_output_source
,
506 audio_set_input_source
,
515 #endif /* CONFIG_CODEC == SWCODEC */
517 /* playback control */
523 playlist_remove_all_tracks
,
525 playlist_insert_track
,
526 playlist_insert_directory
,
538 audio_flush_and_reload_tracks
,
540 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
541 mpeg_get_last_header
,
543 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
544 (CONFIG_CODEC == SWCODEC)
548 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
549 /* MAS communication */
554 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
561 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
567 gui_syncstatusbar_draw
,
577 #ifdef HAVE_LCD_COLOR
581 /* action handling */
584 #ifdef HAVE_TOUCHSCREEN
585 action_get_touchscreen_press
,
593 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
598 # if CONFIG_CHARGING >= CHARGING_MONITOR
602 #ifdef HAVE_USB_POWER
607 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
620 plugin_get_audio_buffer
,
622 plugin_get_current_filename
,
623 #ifdef PLUGIN_USE_IRAM
626 #if defined(DEBUG) || defined(SIMULATOR)
629 #ifdef ROCKBOX_HAS_LOGF
635 #if CONFIG_CODEC == SWCODEC
636 codec_thread_do_callback
,
641 round_value_to_list32
,
642 #endif /* CONFIG_CODEC == SWCODEC */
648 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
649 peak_meter_scale_value
,
650 peak_meter_set_use_dbfs
,
651 peak_meter_get_use_dbfs
,
653 #ifdef HAVE_LCD_BITMAP
660 screen_dump_set_hook
,
667 #ifdef HAVE_WHEEL_POSITION
672 #ifdef IRIVER_H100_SERIES
673 /* Routines for the iriver_flash -plugin. */
674 detect_original_firmware
,
675 detect_flashed_ramimage
,
676 detect_flashed_romimage
,
679 #if (CONFIG_CODEC == SWCODEC)
692 buf_request_buffer_handle
,
699 tagcache_search_set_uniqbuf
,
700 tagcache_search_add_filter
,
703 tagcache_search_finish
,
704 tagcache_get_numeric
,
705 #ifdef HAVE_TC_RAMCACHE
711 search_albumart_files
,
714 #ifdef HAVE_SEMAPHORE_OBJECTS
722 /* new stuff at the end, sort into place next time
723 the API gets incompatible */
726 int plugin_load(const char* plugin
, const void* parameter
)
729 struct plugin_header
*hdr
;
732 fb_data
* old_backdrop
;
735 if (current_plugin_handle
&& pfn_tsr_exit
)
736 { /* if we have a resident old plugin and a callback */
737 if (pfn_tsr_exit(!strcmp(current_plugin
, plugin
)) == false )
739 /* not allowing another plugin to load */
742 lc_close(current_plugin_handle
);
743 current_plugin_handle
= pfn_tsr_exit
= NULL
;
746 splash(0, ID2P(LANG_WAIT
));
747 strcpy(current_plugin
, plugin
);
749 current_plugin_handle
= lc_open(plugin
, pluginbuf
, PLUGIN_BUFFER_SIZE
);
750 if (current_plugin_handle
== NULL
) {
751 splashf(HZ
*2, str(LANG_PLUGIN_CANT_OPEN
), plugin
);
755 hdr
= lc_get_header(current_plugin_handle
);
758 || hdr
->magic
!= PLUGIN_MAGIC
759 || hdr
->target_id
!= TARGET_ID
760 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
761 || hdr
->load_addr
!= pluginbuf
762 || hdr
->end_addr
> pluginbuf
+ PLUGIN_BUFFER_SIZE
766 lc_close(current_plugin_handle
);
767 splash(HZ
*2, str(LANG_PLUGIN_WRONG_MODEL
));
770 if (hdr
->api_version
> PLUGIN_API_VERSION
771 || hdr
->api_version
< PLUGIN_MIN_API_VERSION
)
773 lc_close(current_plugin_handle
);
774 splash(HZ
*2, str(LANG_PLUGIN_WRONG_VERSION
));
777 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
778 plugin_size
= hdr
->end_addr
- pluginbuf
;
783 *(hdr
->api
) = &rockbox_api
;
785 #if defined HAVE_LCD_BITMAP && LCD_DEPTH > 1
786 old_backdrop
= lcd_get_backdrop();
791 #ifdef HAVE_REMOTE_LCD
792 lcd_remote_clear_display();
797 viewportmanager_theme_enable(i
, false, NULL
);
799 #ifdef HAVE_TOUCHSCREEN
800 touchscreen_set_mode(TOUCHSCREEN_BUTTON
);
803 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
807 rc
= hdr
->entry_point(parameter
);
810 { /* close handle if plugin is no tsr one */
811 lc_close(current_plugin_handle
);
812 current_plugin_handle
= NULL
;
815 /* Go back to the global setting in case the plugin changed it */
816 #ifdef HAVE_TOUCHSCREEN
817 touchscreen_set_mode(global_settings
.touch_mode
);
820 button_clear_queue();
822 #ifdef HAVE_LCD_BITMAP
823 lcd_setfont(FONT_UI
);
825 lcd_set_backdrop(old_backdrop
);
826 #ifdef HAVE_LCD_COLOR
827 lcd_set_drawinfo(DRMODE_SOLID
, global_settings
.fg_color
,
828 global_settings
.bg_color
);
830 lcd_set_drawinfo(DRMODE_SOLID
, LCD_DEFAULT_FG
, LCD_DEFAULT_BG
);
832 #else /* LCD_DEPTH == 1 */
833 lcd_set_drawmode(DRMODE_SOLID
);
834 #endif /* LCD_DEPTH */
835 #endif /* HAVE_LCD_BITMAP */
838 #ifdef HAVE_REMOTE_LCD
839 #if LCD_REMOTE_DEPTH > 1
840 lcd_remote_set_drawinfo(DRMODE_SOLID
, LCD_REMOTE_DEFAULT_FG
,
841 LCD_REMOTE_DEFAULT_BG
);
843 lcd_remote_set_drawmode(DRMODE_SOLID
);
848 #ifdef HAVE_REMOTE_LCD
849 lcd_remote_clear_display();
853 viewportmanager_theme_undo(i
, false);
855 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
856 if(open_files
!= 0 && !current_plugin_handle
)
859 logf("Plugin '%s' leaks file handles", plugin
);
861 static const char *lines
[] =
862 { ID2P(LANG_PLUGIN_ERROR
),
863 "#leak-file-handles" };
864 static const struct text_message message
={ lines
, 2 };
865 button_clear_queue(); /* Empty the keyboard buffer */
866 gui_syncyesno_run(&message
, NULL
, NULL
);
868 for(fd
=0; fd
< MAX_OPEN_FILES
; fd
++)
869 if(open_files
& (1<<fd
))
874 if (rc
== PLUGIN_ERROR
)
875 splash(HZ
*2, str(LANG_PLUGIN_ERROR
));
880 /* Returns a pointer to the portion of the plugin buffer that is not already
881 being used. If no plugin is loaded, returns the entire plugin buffer */
882 void* plugin_get_buffer(size_t *buffer_size
)
886 if (current_plugin_handle
)
888 if (plugin_size
>= PLUGIN_BUFFER_SIZE
)
891 *buffer_size
= PLUGIN_BUFFER_SIZE
-plugin_size
;
892 buffer_pos
= plugin_size
;
896 *buffer_size
= PLUGIN_BUFFER_SIZE
;
900 return &pluginbuf
[buffer_pos
];
903 /* Returns a pointer to the mp3 buffer.
904 Playback gets stopped, to avoid conflicts.
905 Talk buffer is stolen as well.
907 void* plugin_get_audio_buffer(size_t *buffer_size
)
909 #if CONFIG_CODEC == SWCODEC
910 return audio_get_buffer(true, buffer_size
);
913 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
914 *buffer_size
= audiobufend
- audiobuf
;
919 #ifdef PLUGIN_USE_IRAM
920 /* Initializes plugin IRAM */
921 void plugin_iram_init(char *iramstart
, char *iramcopy
, size_t iram_size
,
922 char *iedata
, size_t iedata_size
)
924 /* We need to stop audio playback in order to use codec IRAM */
926 memcpy(iramstart
, iramcopy
, iram_size
);
927 memset(iedata
, 0, iedata_size
);
928 memset(iramcopy
, 0, iram_size
);
930 /* writeback cleared iedata and iramcopy areas */
934 #endif /* PLUGIN_USE_IRAM */
936 /* The plugin wants to stay resident after leaving its main function, e.g.
937 runs from timer or own thread. The callback is registered to later
938 instruct it to free its resources before a new plugin gets loaded. */
939 void plugin_tsr(bool (*exit_callback
)(bool))
941 pfn_tsr_exit
= exit_callback
; /* remember the callback for later */
944 char *plugin_get_current_filename(void)
946 return current_plugin
;
949 static int open_wrapper(const char* pathname
, int flags
, ...)
951 /* we don't have an 'open' function. it's a define. and we need
952 * the real file_open, hence PREFIX() doesn't work here */
954 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
959 fd
= sim_open(pathname
, flags
, va_arg(ap
, unsigned int));
963 fd
= sim_open(pathname
, flags
);
965 fd
= file_open(pathname
,flags
);
968 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
975 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
976 static int close_wrapper(int fd
)
978 if((~open_files
) & (1<<fd
))
980 logf("double close from plugin");
983 open_files
&= (~(1<<fd
));
985 return PREFIX(close
)(fd
);
988 static int creat_wrapper(const char *pathname
, mode_t mode
)
990 int fd
= PREFIX(creat
)(pathname
, mode
);
993 open_files
|= (1<<fd
);
997 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */