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
,
317 (creat_func
)PREFIX(creat
),
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
,
349 #if defined(CPU_ARM) && CONFIG_PLATFORM & PLATFORM_NATIVE
355 default_event_handler
,
356 default_event_handler_ex
,
360 #if (CONFIG_CODEC == SWCODEC)
362 #ifdef HAVE_PRIORITY_SCHEDULING
370 reset_poweroff_timer
,
371 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
375 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
376 #ifdef CPU_BOOST_LOGGING
381 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
382 #endif /* PLATFORM_NATIVE */
383 #ifdef HAVE_SCHEDULER_BOOSTCTRL
387 #ifdef HAVE_CPUCACHE_FLUSH
390 #ifdef HAVE_CPUCACHE_INVALIDATE
401 #if CONFIG_CODEC == SWCODEC
402 queue_enable_queue_send
,
409 #ifdef USB_ENABLE_HID
415 __cyg_profile_func_enter
,
416 __cyg_profile_func_exit
,
422 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
423 /* special simulator hooks */
424 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
426 sim_lcd_ex_update_rect
,
430 /* strings and memory */
444 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
471 #ifdef AUDIOHW_HAVE_EQ
472 sound_enum_hw_eq_band_setting
,
474 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
479 #if CONFIG_CODEC != SWCODEC
483 #if CONFIG_CODEC == SWCODEC
484 &audio_master_sampr_list
[0],
493 pcm_get_bytes_waiting
,
499 #ifdef HAVE_RECORDING
505 pcm_calculate_rec_peaks
,
506 audio_set_recording_gain
,
507 #endif /* HAVE_RECORDING */
508 #if INPUT_SRC_CAPS != 0
509 audio_set_output_source
,
510 audio_set_input_source
,
519 #endif /* CONFIG_CODEC == SWCODEC */
521 /* playback control */
527 playlist_remove_all_tracks
,
529 playlist_insert_track
,
530 playlist_insert_directory
,
542 audio_flush_and_reload_tracks
,
544 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
545 mpeg_get_last_header
,
547 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
548 (CONFIG_CODEC == SWCODEC)
552 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
553 /* MAS communication */
558 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
565 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
571 gui_syncstatusbar_draw
,
581 #ifdef HAVE_LCD_COLOR
585 /* action handling */
588 #ifdef HAVE_TOUCHSCREEN
589 action_get_touchscreen_press
,
597 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
602 # if CONFIG_CHARGING >= CHARGING_MONITOR
606 #ifdef HAVE_USB_POWER
611 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
624 plugin_get_audio_buffer
,
626 plugin_get_current_filename
,
627 #if defined(DEBUG) || defined(SIMULATOR)
630 #ifdef ROCKBOX_HAS_LOGF
636 #if CONFIG_CODEC == SWCODEC
637 codec_thread_do_callback
,
642 round_value_to_list32
,
643 #endif /* CONFIG_CODEC == SWCODEC */
649 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
650 peak_meter_scale_value
,
651 peak_meter_set_use_dbfs
,
652 peak_meter_get_use_dbfs
,
654 #ifdef HAVE_LCD_BITMAP
661 screen_dump_set_hook
,
668 #ifdef HAVE_WHEEL_POSITION
673 #ifdef IRIVER_H100_SERIES
674 /* Routines for the iriver_flash -plugin. */
675 detect_original_firmware
,
676 detect_flashed_ramimage
,
677 detect_flashed_romimage
,
680 #if (CONFIG_CODEC == SWCODEC)
693 buf_request_buffer_handle
,
700 tagcache_search_set_uniqbuf
,
701 tagcache_search_add_filter
,
704 tagcache_search_finish
,
705 tagcache_get_numeric
,
706 #ifdef HAVE_TC_RAMCACHE
712 search_albumart_files
,
715 #ifdef HAVE_SEMAPHORE_OBJECTS
723 /* new stuff at the end, sort into place next time
724 the API gets incompatible */
728 int plugin_load(const char* plugin
, const void* parameter
)
731 struct plugin_header
*hdr
;
734 fb_data
* old_backdrop
;
737 if (current_plugin_handle
&& pfn_tsr_exit
)
738 { /* if we have a resident old plugin and a callback */
739 if (pfn_tsr_exit(!strcmp(current_plugin
, plugin
)) == false )
741 /* not allowing another plugin to load */
744 lc_close(current_plugin_handle
);
745 current_plugin_handle
= pfn_tsr_exit
= NULL
;
748 splash(0, ID2P(LANG_WAIT
));
749 strcpy(current_plugin
, plugin
);
751 current_plugin_handle
= lc_open(plugin
, pluginbuf
, PLUGIN_BUFFER_SIZE
);
752 if (current_plugin_handle
== NULL
) {
753 splashf(HZ
*2, str(LANG_PLUGIN_CANT_OPEN
), plugin
);
757 hdr
= lc_get_header(current_plugin_handle
);
760 || hdr
->magic
!= PLUGIN_MAGIC
761 || hdr
->target_id
!= TARGET_ID
762 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
763 || hdr
->load_addr
!= pluginbuf
764 || hdr
->end_addr
> pluginbuf
+ PLUGIN_BUFFER_SIZE
768 lc_close(current_plugin_handle
);
769 splash(HZ
*2, str(LANG_PLUGIN_WRONG_MODEL
));
772 if (hdr
->api_version
> PLUGIN_API_VERSION
773 || hdr
->api_version
< PLUGIN_MIN_API_VERSION
)
775 lc_close(current_plugin_handle
);
776 splash(HZ
*2, str(LANG_PLUGIN_WRONG_VERSION
));
779 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
780 plugin_size
= hdr
->end_addr
- pluginbuf
;
785 *(hdr
->api
) = &rockbox_api
;
787 #if defined HAVE_LCD_BITMAP && LCD_DEPTH > 1
788 old_backdrop
= lcd_get_backdrop();
793 #ifdef HAVE_REMOTE_LCD
794 lcd_remote_clear_display();
799 viewportmanager_theme_enable(i
, false, NULL
);
801 #ifdef HAVE_TOUCHSCREEN
802 touchscreen_set_mode(TOUCHSCREEN_BUTTON
);
805 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
809 rc
= hdr
->entry_point(parameter
);
812 { /* close handle if plugin is no tsr one */
813 lc_close(current_plugin_handle
);
814 current_plugin_handle
= NULL
;
817 /* Go back to the global setting in case the plugin changed it */
818 #ifdef HAVE_TOUCHSCREEN
819 touchscreen_set_mode(global_settings
.touch_mode
);
822 button_clear_queue();
824 #ifdef HAVE_LCD_BITMAP
825 lcd_setfont(FONT_UI
);
827 lcd_set_backdrop(old_backdrop
);
828 #ifdef HAVE_LCD_COLOR
829 lcd_set_drawinfo(DRMODE_SOLID
, global_settings
.fg_color
,
830 global_settings
.bg_color
);
832 lcd_set_drawinfo(DRMODE_SOLID
, LCD_DEFAULT_FG
, LCD_DEFAULT_BG
);
834 #else /* LCD_DEPTH == 1 */
835 lcd_set_drawmode(DRMODE_SOLID
);
836 #endif /* LCD_DEPTH */
837 #endif /* HAVE_LCD_BITMAP */
840 #ifdef HAVE_REMOTE_LCD
841 #if LCD_REMOTE_DEPTH > 1
842 lcd_remote_set_drawinfo(DRMODE_SOLID
, LCD_REMOTE_DEFAULT_FG
,
843 LCD_REMOTE_DEFAULT_BG
);
845 lcd_remote_set_drawmode(DRMODE_SOLID
);
850 #ifdef HAVE_REMOTE_LCD
851 lcd_remote_clear_display();
855 viewportmanager_theme_undo(i
, false);
857 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
858 if(open_files
!= 0 && !current_plugin_handle
)
861 logf("Plugin '%s' leaks file handles", plugin
);
863 static const char *lines
[] =
864 { ID2P(LANG_PLUGIN_ERROR
),
865 "#leak-file-handles" };
866 static const struct text_message message
={ lines
, 2 };
867 button_clear_queue(); /* Empty the keyboard buffer */
868 gui_syncyesno_run(&message
, NULL
, NULL
);
870 for(fd
=0; fd
< MAX_OPEN_FILES
; fd
++)
871 if(open_files
& (1<<fd
))
876 if (rc
== PLUGIN_ERROR
)
877 splash(HZ
*2, str(LANG_PLUGIN_ERROR
));
882 /* Returns a pointer to the portion of the plugin buffer that is not already
883 being used. If no plugin is loaded, returns the entire plugin buffer */
884 void* plugin_get_buffer(size_t *buffer_size
)
888 if (current_plugin_handle
)
890 if (plugin_size
>= PLUGIN_BUFFER_SIZE
)
893 *buffer_size
= PLUGIN_BUFFER_SIZE
-plugin_size
;
894 buffer_pos
= plugin_size
;
898 *buffer_size
= PLUGIN_BUFFER_SIZE
;
902 return &pluginbuf
[buffer_pos
];
905 /* Returns a pointer to the mp3 buffer.
906 Playback gets stopped, to avoid conflicts.
907 Talk buffer is stolen as well.
909 void* plugin_get_audio_buffer(size_t *buffer_size
)
911 #if CONFIG_CODEC == SWCODEC
912 return audio_get_buffer(true, buffer_size
);
915 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
916 *buffer_size
= audiobufend
- audiobuf
;
921 /* The plugin wants to stay resident after leaving its main function, e.g.
922 runs from timer or own thread. The callback is registered to later
923 instruct it to free its resources before a new plugin gets loaded. */
924 void plugin_tsr(bool (*exit_callback
)(bool))
926 pfn_tsr_exit
= exit_callback
; /* remember the callback for later */
929 char *plugin_get_current_filename(void)
931 return current_plugin
;
934 static int open_wrapper(const char* pathname
, int flags
, ...)
936 /* we don't have an 'open' function. it's a define. and we need
937 * the real file_open, hence PREFIX() doesn't work here */
939 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
944 fd
= open(pathname
, flags
, va_arg(ap
, unsigned int));
948 fd
= open(pathname
, flags
);
950 fd
= file_open(pathname
,flags
);
953 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
960 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
961 static int close_wrapper(int fd
)
963 if((~open_files
) & (1<<fd
))
965 logf("double close from plugin");
968 open_files
&= (~(1<<fd
));
970 return PREFIX(close
)(fd
);
973 static int creat_wrapper(const char *pathname
, mode_t mode
)
975 int fd
= PREFIX(creat
)(pathname
, mode
);
978 open_files
|= (1<<fd
);
982 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */