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_SDL)
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
117 #ifdef HAVE_LCD_CHARCELLS
119 lcd_get_locked_pattern
,
127 &lcd_framebuffer
[0][0],
138 lcd_mono_bitmap_part
,
151 lcd_bitmap_transparent_part
,
152 lcd_bitmap_transparent
,
155 #if defined(TOSHIBA_GIGABEAT_F) || defined(SANSA_E200) || defined(SANSA_C200) \
156 || defined(IRIVER_H10) || defined(COWON_D2) || defined(PHILIPS_HDD1630) \
157 || defined(SANSA_FUZE) || defined(SANSA_E200V2) || defined(SANSA_FUZEV2) \
158 || defined(TOSHIBA_GIGABEAT_S)
161 #endif /* MEMORYSIZE > 2 */
162 #elif (LCD_DEPTH < 4) && (CONFIG_PLATFORM & PLATFORM_NATIVE)
165 #endif /* LCD_DEPTH */
166 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
168 lcd_pal256_update_pal
,
171 lcd_puts_scroll_style
,
172 #ifdef HAVE_LCD_INVERT
173 lcd_set_invert_display
,
174 #endif /* HAVE_LCD_INVERT */
175 #if defined(HAVE_LCD_MODES)
178 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
182 #ifdef HAVE_LCD_BITMAP
192 #endif /* HAVE_LCD_BITMAP */
197 backlight_set_timeout
,
198 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
199 backlight_set_brightness
,
200 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
203 backlight_set_timeout_plugged
,
209 #ifdef HAVE_REMOTE_LCD
211 lcd_remote_set_contrast
,
212 lcd_remote_clear_display
,
214 lcd_remote_puts_scroll
,
215 lcd_remote_stop_scroll
,
216 lcd_remote_set_drawmode
,
217 lcd_remote_get_drawmode
,
219 lcd_remote_getstringsize
,
220 lcd_remote_drawpixel
,
226 lcd_remote_mono_bitmap_part
,
227 lcd_remote_mono_bitmap
,
229 lcd_remote_puts_style
,
230 lcd_remote_puts_scroll_style
,
231 &lcd_remote_framebuffer
[0][0],
233 lcd_remote_update_rect
,
236 remote_backlight_off
,
237 remote_backlight_set_timeout
,
239 remote_backlight_set_timeout_plugged
,
241 #endif /* HAVE_REMOTE_LCD */
243 {&screens
[SCREEN_MAIN
], &screens
[SCREEN_REMOTE
]},
245 {&screens
[SCREEN_MAIN
]},
247 #if defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
248 lcd_remote_set_foreground
,
249 lcd_remote_get_foreground
,
250 lcd_remote_set_background
,
251 lcd_remote_get_background
,
252 lcd_remote_bitmap_part
,
255 viewport_set_defaults
,
256 #ifdef HAVE_LCD_BITMAP
257 viewportmanager_theme_enable
,
258 viewportmanager_theme_undo
,
263 gui_synclist_set_nb_items
,
264 gui_synclist_set_icon_callback
,
265 gui_synclist_get_nb_items
,
266 gui_synclist_get_sel_pos
,
268 gui_synclist_select_item
,
269 gui_synclist_add_item
,
270 gui_synclist_del_item
,
271 gui_synclist_limit_scroll
,
272 gui_synclist_do_button
,
273 gui_synclist_set_title
,
275 simplelist_info_init
,
276 simplelist_show_list
,
282 #ifdef HAVE_BUTTON_DATA
288 #ifdef HAS_BUTTON_HOLD
291 #ifdef HAVE_TOUCHSCREEN
292 touchscreen_set_mode
,
295 #ifdef HAVE_BUTTON_LIGHT
296 buttonlight_set_timeout
,
299 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
300 buttonlight_set_brightness
,
301 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
302 #endif /* HAVE_BUTTON_LIGHT */
306 (open_func
)open_wrapper
,
307 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
312 (read_func
)PREFIX(read
),
314 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
315 (creat_func
)creat_wrapper
,
319 (write_func
)PREFIX(write
),
330 #if USING_STORAGE_CALLBACK
331 register_storage_idle_func
,
332 unregister_storage_idle_func
,
333 #endif /* USING_STORAGE_CALLBACK */
335 create_numbered_filename
,
350 #if defined(CPU_ARM) && CONFIG_PLATFORM & PLATFORM_NATIVE
356 default_event_handler
,
357 default_event_handler_ex
,
361 #if (CONFIG_CODEC == SWCODEC)
363 #ifdef HAVE_PRIORITY_SCHEDULING
371 reset_poweroff_timer
,
372 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
376 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
377 #ifdef CPU_BOOST_LOGGING
382 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
383 #endif /* PLATFORM_NATIVE */
384 #ifdef HAVE_SCHEDULER_BOOSTCTRL
405 #if CONFIG_CODEC == SWCODEC
406 queue_enable_queue_send
,
413 #ifdef USB_ENABLE_HID
419 __cyg_profile_func_enter
,
420 __cyg_profile_func_exit
,
426 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
427 /* special simulator hooks */
428 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
430 sim_lcd_ex_update_rect
,
434 /* strings and memory */
448 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
475 #ifdef AUDIOHW_HAVE_EQ
476 sound_enum_hw_eq_band_setting
,
478 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
483 #if CONFIG_CODEC != SWCODEC
487 #if CONFIG_CODEC == SWCODEC
488 &audio_master_sampr_list
[0],
497 pcm_get_bytes_waiting
,
503 #ifdef HAVE_RECORDING
509 pcm_calculate_rec_peaks
,
510 audio_set_recording_gain
,
511 #endif /* HAVE_RECORDING */
512 #if INPUT_SRC_CAPS != 0
513 audio_set_output_source
,
514 audio_set_input_source
,
523 #endif /* CONFIG_CODEC == SWCODEC */
525 /* playback control */
531 playlist_remove_all_tracks
,
533 playlist_insert_track
,
534 playlist_insert_directory
,
546 audio_flush_and_reload_tracks
,
548 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
549 mpeg_get_last_header
,
551 #if ((CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
552 (CONFIG_CODEC == SWCODEC)) && defined (HAVE_PITCHSCREEN)
556 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
557 /* MAS communication */
562 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
569 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
575 gui_syncstatusbar_draw
,
585 #ifdef HAVE_LCD_COLOR
589 /* action handling */
592 #ifdef HAVE_TOUCHSCREEN
593 action_get_touchscreen_press
,
601 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
606 # if CONFIG_CHARGING >= CHARGING_MONITOR
610 #ifdef HAVE_USB_POWER
615 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
628 plugin_get_audio_buffer
,
630 plugin_get_current_filename
,
631 #if defined(DEBUG) || defined(SIMULATOR)
634 #ifdef ROCKBOX_HAS_LOGF
640 #if CONFIG_CODEC == SWCODEC
641 codec_thread_do_callback
,
646 round_value_to_list32
,
647 #endif /* CONFIG_CODEC == SWCODEC */
653 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
654 peak_meter_scale_value
,
655 peak_meter_set_use_dbfs
,
656 peak_meter_get_use_dbfs
,
658 #ifdef HAVE_LCD_BITMAP
665 screen_dump_set_hook
,
672 #ifdef HAVE_WHEEL_POSITION
677 #ifdef IRIVER_H100_SERIES
678 /* Routines for the iriver_flash -plugin. */
679 detect_original_firmware
,
680 detect_flashed_ramimage
,
681 detect_flashed_romimage
,
684 #if (CONFIG_CODEC == SWCODEC)
697 buf_request_buffer_handle
,
704 tagcache_search_set_uniqbuf
,
705 tagcache_search_add_filter
,
708 tagcache_search_finish
,
709 tagcache_get_numeric
,
710 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
716 search_albumart_files
,
719 #ifdef HAVE_SEMAPHORE_OBJECTS
727 /* new stuff at the end, sort into place next time
728 the API gets incompatible */
731 int plugin_load(const char* plugin
, const void* parameter
)
734 struct plugin_header
*p_hdr
;
735 struct lc_header
*hdr
;
738 fb_data
* old_backdrop
;
741 if (current_plugin_handle
&& pfn_tsr_exit
)
742 { /* if we have a resident old plugin and a callback */
743 if (pfn_tsr_exit(!strcmp(current_plugin
, plugin
)) == false )
745 /* not allowing another plugin to load */
748 lc_close(current_plugin_handle
);
749 current_plugin_handle
= pfn_tsr_exit
= NULL
;
752 splash(0, ID2P(LANG_WAIT
));
753 strcpy(current_plugin
, plugin
);
755 current_plugin_handle
= lc_open(plugin
, pluginbuf
, PLUGIN_BUFFER_SIZE
);
756 if (current_plugin_handle
== NULL
) {
757 splashf(HZ
*2, str(LANG_PLUGIN_CANT_OPEN
), plugin
);
761 p_hdr
= lc_get_header(current_plugin_handle
);
763 hdr
= p_hdr
? &p_hdr
->lc_hdr
: NULL
;
767 || hdr
->magic
!= PLUGIN_MAGIC
768 || hdr
->target_id
!= TARGET_ID
769 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
770 || hdr
->load_addr
!= pluginbuf
771 || hdr
->end_addr
> pluginbuf
+ PLUGIN_BUFFER_SIZE
775 lc_close(current_plugin_handle
);
776 splash(HZ
*2, str(LANG_PLUGIN_WRONG_MODEL
));
779 if (hdr
->api_version
> PLUGIN_API_VERSION
780 || hdr
->api_version
< PLUGIN_MIN_API_VERSION
)
782 lc_close(current_plugin_handle
);
783 splash(HZ
*2, str(LANG_PLUGIN_WRONG_VERSION
));
786 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
787 plugin_size
= hdr
->end_addr
- pluginbuf
;
792 *(p_hdr
->api
) = &rockbox_api
;
794 #if defined HAVE_LCD_BITMAP && LCD_DEPTH > 1
795 old_backdrop
= lcd_get_backdrop();
800 #ifdef HAVE_REMOTE_LCD
801 lcd_remote_clear_display();
806 viewportmanager_theme_enable(i
, false, NULL
);
808 #ifdef HAVE_TOUCHSCREEN
809 touchscreen_set_mode(TOUCHSCREEN_BUTTON
);
812 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
816 rc
= p_hdr
->entry_point(parameter
);
819 { /* close handle if plugin is no tsr one */
820 lc_close(current_plugin_handle
);
821 current_plugin_handle
= NULL
;
824 /* Go back to the global setting in case the plugin changed it */
825 #ifdef HAVE_TOUCHSCREEN
826 touchscreen_set_mode(global_settings
.touch_mode
);
829 #ifdef HAVE_LCD_BITMAP
830 lcd_setfont(FONT_UI
);
832 lcd_set_backdrop(old_backdrop
);
833 #ifdef HAVE_LCD_COLOR
834 lcd_set_drawinfo(DRMODE_SOLID
, global_settings
.fg_color
,
835 global_settings
.bg_color
);
837 lcd_set_drawinfo(DRMODE_SOLID
, LCD_DEFAULT_FG
, LCD_DEFAULT_BG
);
839 #else /* LCD_DEPTH == 1 */
840 lcd_set_drawmode(DRMODE_SOLID
);
841 #endif /* LCD_DEPTH */
842 #endif /* HAVE_LCD_BITMAP */
845 #ifdef HAVE_REMOTE_LCD
846 #if LCD_REMOTE_DEPTH > 1
847 lcd_remote_set_drawinfo(DRMODE_SOLID
, LCD_REMOTE_DEFAULT_FG
,
848 LCD_REMOTE_DEFAULT_BG
);
850 lcd_remote_set_drawmode(DRMODE_SOLID
);
855 #ifdef HAVE_REMOTE_LCD
856 lcd_remote_clear_display();
860 viewportmanager_theme_undo(i
, false);
862 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
863 if(open_files
!= 0 && !current_plugin_handle
)
866 logf("Plugin '%s' leaks file handles", plugin
);
868 static const char *lines
[] =
869 { ID2P(LANG_PLUGIN_ERROR
),
870 "#leak-file-handles" };
871 static const struct text_message message
={ lines
, 2 };
872 button_clear_queue(); /* Empty the keyboard buffer */
873 gui_syncyesno_run(&message
, NULL
, NULL
);
875 for(fd
=0; fd
< MAX_OPEN_FILES
; fd
++)
876 if(open_files
& (1<<fd
))
881 if (rc
== PLUGIN_ERROR
)
882 splash(HZ
*2, str(LANG_PLUGIN_ERROR
));
887 /* Returns a pointer to the portion of the plugin buffer that is not already
888 being used. If no plugin is loaded, returns the entire plugin buffer */
889 void* plugin_get_buffer(size_t *buffer_size
)
893 if (current_plugin_handle
)
895 if (plugin_size
>= PLUGIN_BUFFER_SIZE
)
898 *buffer_size
= PLUGIN_BUFFER_SIZE
-plugin_size
;
899 buffer_pos
= plugin_size
;
903 *buffer_size
= PLUGIN_BUFFER_SIZE
;
907 return &pluginbuf
[buffer_pos
];
910 /* Returns a pointer to the mp3 buffer.
911 Playback gets stopped, to avoid conflicts.
912 Talk buffer is stolen as well.
914 void* plugin_get_audio_buffer(size_t *buffer_size
)
916 #if CONFIG_CODEC == SWCODEC
917 return audio_get_buffer(true, buffer_size
);
920 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
921 *buffer_size
= audiobufend
- audiobuf
;
926 /* The plugin wants to stay resident after leaving its main function, e.g.
927 runs from timer or own thread. The callback is registered to later
928 instruct it to free its resources before a new plugin gets loaded. */
929 void plugin_tsr(bool (*exit_callback
)(bool))
931 pfn_tsr_exit
= exit_callback
; /* remember the callback for later */
934 char *plugin_get_current_filename(void)
936 return current_plugin
;
939 static int open_wrapper(const char* pathname
, int flags
, ...)
941 /* we don't have an 'open' function. it's a define. and we need
942 * the real file_open, hence PREFIX() doesn't work here */
944 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
949 fd
= open(pathname
, flags
, va_arg(ap
, unsigned int));
953 fd
= open(pathname
, flags
);
955 fd
= file_open(pathname
,flags
);
958 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
965 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
966 static int close_wrapper(int fd
)
968 if((~open_files
) & (1<<fd
))
970 logf("double close from plugin");
973 open_files
&= (~(1<<fd
));
975 return PREFIX(close
)(fd
);
978 static int creat_wrapper(const char *pathname
, mode_t mode
)
980 int fd
= PREFIX(creat
)(pathname
, mode
);
983 open_files
|= (1<<fd
);
987 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */