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 ****************************************************************************/
30 #include "backlight.h"
31 #include "sound_menu.h"
33 #include "powermgmt.h"
36 #include "option_select.h"
42 #include "diacritic.h"
43 #include "filefuncs.h"
44 #include "load_code.h"
50 #ifdef HAVE_LCD_BITMAP
51 #include "scrollbar.h"
52 #include "peakmeter.h"
58 #include "usbstack/usb_hid.h"
61 #if defined (SIMULATOR)
62 #define PREFIX(_x_) sim_ ## _x_
63 #elif defined (APPLICATION)
64 #define PREFIX(_x_) app_ ## _x_
69 #if defined (APPLICATION)
70 /* For symmetry reasons (we want app_ and sim_ to behave similarly), some
71 * wrappers are needed */
72 static int app_close(int fd
)
77 static ssize_t
app_read(int fd
, void *buf
, size_t count
)
79 return read(fd
,buf
,count
);
82 static off_t
app_lseek(int fd
, off_t offset
, int whence
)
84 return lseek(fd
,offset
,whence
);
87 static ssize_t
app_write(int fd
, const void *buf
, size_t count
)
89 return write(fd
,buf
,count
);
92 static int app_ftruncate(int fd
, off_t length
)
94 return ftruncate(fd
,length
);
97 static off_t
app_filesize(int fd
)
103 #if defined(HAVE_PLUGIN_CHECK_OPEN_CLOSE) && (MAX_OPEN_FILES>32)
104 #warning "MAX_OPEN_FILES>32, disabling plugin file open/close checking"
105 #undef HAVE_PLUGIN_CHECK_OPEN_CLOSE
108 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
109 static unsigned int open_files
;
112 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
113 static unsigned char pluginbuf
[PLUGIN_BUFFER_SIZE
];
114 void sim_lcd_ex_init(unsigned long (*getpixel
)(int, int));
115 void sim_lcd_ex_update_rect(int x
, int y
, int width
, int height
);
117 extern unsigned char pluginbuf
[];
121 /* for actual plugins only, not for codecs */
122 static int plugin_size
= 0;
123 static bool (*pfn_tsr_exit
)(bool reenter
) = NULL
; /* TSR exit callback */
124 static char current_plugin
[MAX_PATH
];
125 /* NULL if no plugin is loaded, otherwise the handle that lc_open() returned */
126 static void *current_plugin_handle
;
128 char *plugin_get_current_filename(void);
130 /* Some wrappers used to monitor open and close and detect leaks*/
131 static int open_wrapper(const char* pathname
, int flags
, ...);
132 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
133 static int close_wrapper(int fd
);
134 static int creat_wrapper(const char *pathname
, mode_t mode
);
137 static const struct plugin_api rockbox_api
= {
140 #ifdef HAVE_LCD_CONTRAST
152 #ifdef HAVE_LCD_CHARCELLS
154 lcd_get_locked_pattern
,
162 &lcd_framebuffer
[0][0],
166 screen_helper_setfont
,
173 lcd_mono_bitmap_part
,
186 lcd_bitmap_transparent_part
,
187 lcd_bitmap_transparent
,
190 #if defined(TOSHIBA_GIGABEAT_F) || defined(SANSA_E200) || defined(SANSA_C200) \
191 || defined(IRIVER_H10) || defined(COWON_D2) || defined(PHILIPS_HDD1630) \
192 || defined(SANSA_FUZE) || defined(SANSA_E200V2) || defined(SANSA_FUZEV2) \
193 || defined(TOSHIBA_GIGABEAT_S) || defined(PHILIPS_SA9200)
196 #endif /* MEMORYSIZE > 2 */
197 #elif (LCD_DEPTH < 4) && (CONFIG_PLATFORM & PLATFORM_NATIVE)
200 #endif /* LCD_DEPTH */
201 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
203 lcd_pal256_update_pal
,
206 lcd_puts_scroll_style
,
207 #ifdef HAVE_LCD_INVERT
208 lcd_set_invert_display
,
209 #endif /* HAVE_LCD_INVERT */
210 #if defined(HAVE_LCD_MODES)
213 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
217 #ifdef HAVE_LCD_BITMAP
228 #endif /* HAVE_LCD_BITMAP */
233 backlight_set_timeout
,
234 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
235 backlight_set_brightness
,
236 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
239 backlight_set_timeout_plugged
,
245 #ifdef HAVE_REMOTE_LCD
247 lcd_remote_set_contrast
,
248 lcd_remote_clear_display
,
250 lcd_remote_puts_scroll
,
251 lcd_remote_stop_scroll
,
252 lcd_remote_set_drawmode
,
253 lcd_remote_get_drawmode
,
255 lcd_remote_getstringsize
,
256 lcd_remote_drawpixel
,
262 lcd_remote_mono_bitmap_part
,
263 lcd_remote_mono_bitmap
,
265 lcd_remote_puts_style
,
266 lcd_remote_puts_scroll_style
,
267 &lcd_remote_framebuffer
[0][0],
269 lcd_remote_update_rect
,
272 remote_backlight_off
,
273 remote_backlight_set_timeout
,
275 remote_backlight_set_timeout_plugged
,
277 #endif /* HAVE_REMOTE_LCD */
279 {&screens
[SCREEN_MAIN
], &screens
[SCREEN_REMOTE
]},
281 {&screens
[SCREEN_MAIN
]},
283 #if defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
284 lcd_remote_set_foreground
,
285 lcd_remote_get_foreground
,
286 lcd_remote_set_background
,
287 lcd_remote_get_background
,
288 lcd_remote_bitmap_part
,
291 viewport_set_defaults
,
292 #ifdef HAVE_LCD_BITMAP
293 viewportmanager_theme_enable
,
294 viewportmanager_theme_undo
,
299 gui_synclist_set_nb_items
,
300 gui_synclist_set_icon_callback
,
301 gui_synclist_get_nb_items
,
302 gui_synclist_get_sel_pos
,
304 gui_synclist_select_item
,
305 gui_synclist_add_item
,
306 gui_synclist_del_item
,
307 gui_synclist_limit_scroll
,
308 gui_synclist_do_button
,
309 gui_synclist_set_title
,
311 simplelist_info_init
,
312 simplelist_show_list
,
318 #ifdef HAVE_BUTTON_DATA
324 #ifdef HAS_BUTTON_HOLD
327 #ifdef HAVE_TOUCHSCREEN
328 touchscreen_set_mode
,
331 #ifdef HAVE_BUTTON_LIGHT
332 buttonlight_set_timeout
,
335 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
336 buttonlight_set_brightness
,
337 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
338 #endif /* HAVE_BUTTON_LIGHT */
342 (open_func
)open_wrapper
,
343 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
348 (read_func
)PREFIX(read
),
350 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
351 (creat_func
)creat_wrapper
,
355 (write_func
)PREFIX(write
),
366 #if USING_STORAGE_CALLBACK
367 register_storage_idle_func
,
368 unregister_storage_idle_func
,
369 #endif /* USING_STORAGE_CALLBACK */
371 create_numbered_filename
,
378 (opendir_func
)opendir
,
379 (closedir_func
)closedir
,
380 (readdir_func
)readdir
,
391 #if defined(CPU_ARM) && CONFIG_PLATFORM & PLATFORM_NATIVE
397 default_event_handler
,
398 default_event_handler_ex
,
403 #if (CONFIG_CODEC == SWCODEC)
405 #ifdef HAVE_PRIORITY_SCHEDULING
413 reset_poweroff_timer
,
414 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
418 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
419 #ifdef CPU_BOOST_LOGGING
424 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
425 #endif /* PLATFORM_NATIVE */
426 #ifdef HAVE_SCHEDULER_BOOSTCTRL
432 commit_discard_dcache
,
447 #if CONFIG_CODEC == SWCODEC
448 queue_enable_queue_send
,
455 #ifdef USB_ENABLE_HID
461 __cyg_profile_func_enter
,
462 __cyg_profile_func_exit
,
468 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
469 /* special simulator hooks */
470 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
472 sim_lcd_ex_update_rect
,
476 /* strings and memory */
490 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
510 /* the buflib memory management library */
515 buflib_alloc_maximum
,
530 #ifdef AUDIOHW_HAVE_EQ
531 sound_enum_hw_eq_band_setting
,
533 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
538 #if CONFIG_CODEC != SWCODEC
542 #if CONFIG_CODEC == SWCODEC
543 &audio_master_sampr_list
[0],
552 pcm_get_bytes_waiting
,
558 #ifdef HAVE_RECORDING
564 pcm_calculate_rec_peaks
,
565 audio_set_recording_gain
,
566 #endif /* HAVE_RECORDING */
567 #if INPUT_SRC_CAPS != 0
568 audio_set_output_source
,
569 audio_set_input_source
,
579 mixer_channel_status
,
580 mixer_channel_get_buffer
,
581 mixer_channel_calculate_peaks
,
582 mixer_channel_play_data
,
583 mixer_channel_play_pause
,
585 mixer_channel_set_amplitude
,
586 mixer_channel_get_bytes_waiting
,
591 /* playback control */
597 playlist_remove_all_tracks
,
599 playlist_insert_track
,
600 playlist_insert_directory
,
612 audio_flush_and_reload_tracks
,
614 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
615 mpeg_get_last_header
,
617 #if ((CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
618 (CONFIG_CODEC == SWCODEC)) && defined (HAVE_PITCHSCREEN)
622 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
623 /* MAS communication */
628 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
635 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
641 gui_syncstatusbar_draw
,
651 #ifdef HAVE_LCD_COLOR
655 /* action handling */
658 #ifdef HAVE_TOUCHSCREEN
659 action_get_touchscreen_press
,
667 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
672 # if CONFIG_CHARGING >= CHARGING_MONITOR
676 #ifdef HAVE_USB_POWER
681 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
694 plugin_get_audio_buffer
,
696 plugin_get_current_filename
,
697 #if defined(DEBUG) || defined(SIMULATOR)
700 #ifdef ROCKBOX_HAS_LOGF
706 #if CONFIG_CODEC == SWCODEC
707 codec_thread_do_callback
,
714 round_value_to_list32
,
715 #endif /* CONFIG_CODEC == SWCODEC */
721 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
722 peak_meter_scale_value
,
723 peak_meter_set_use_dbfs
,
724 peak_meter_get_use_dbfs
,
726 #ifdef HAVE_LCD_BITMAP
733 screen_dump_set_hook
,
742 #ifdef HAVE_WHEEL_POSITION
747 #ifdef IRIVER_H100_SERIES
748 /* Routines for the iriver_flash -plugin. */
749 detect_original_firmware
,
750 detect_flashed_ramimage
,
751 detect_flashed_romimage
,
754 #if (CONFIG_CODEC == SWCODEC)
772 tagcache_search_set_uniqbuf
,
773 tagcache_search_add_filter
,
776 tagcache_search_finish
,
777 tagcache_get_numeric
,
778 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
784 search_albumart_files
,
787 #ifdef HAVE_SEMAPHORE_OBJECTS
795 /* new stuff at the end, sort into place next time
796 the API gets incompatible */
798 commit_discard_idcache
,
801 int plugin_load(const char* plugin
, const void* parameter
)
803 struct plugin_header
*p_hdr
;
804 struct lc_header
*hdr
;
806 if (current_plugin_handle
&& pfn_tsr_exit
)
807 { /* if we have a resident old plugin and a callback */
808 if (pfn_tsr_exit(!strcmp(current_plugin
, plugin
)) == false )
810 /* not allowing another plugin to load */
813 lc_close(current_plugin_handle
);
814 current_plugin_handle
= pfn_tsr_exit
= NULL
;
817 splash(0, ID2P(LANG_WAIT
));
818 strcpy(current_plugin
, plugin
);
820 current_plugin_handle
= lc_open(plugin
, pluginbuf
, PLUGIN_BUFFER_SIZE
);
821 if (current_plugin_handle
== NULL
) {
822 splashf(HZ
*2, str(LANG_PLUGIN_CANT_OPEN
), plugin
);
826 p_hdr
= lc_get_header(current_plugin_handle
);
828 hdr
= p_hdr
? &p_hdr
->lc_hdr
: NULL
;
832 || hdr
->magic
!= PLUGIN_MAGIC
833 || hdr
->target_id
!= TARGET_ID
834 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
835 || hdr
->load_addr
!= pluginbuf
836 || hdr
->end_addr
> pluginbuf
+ PLUGIN_BUFFER_SIZE
840 lc_close(current_plugin_handle
);
841 splash(HZ
*2, str(LANG_PLUGIN_WRONG_MODEL
));
844 if (hdr
->api_version
> PLUGIN_API_VERSION
845 || hdr
->api_version
< PLUGIN_MIN_API_VERSION
)
847 lc_close(current_plugin_handle
);
848 splash(HZ
*2, str(LANG_PLUGIN_WRONG_VERSION
));
851 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
852 plugin_size
= hdr
->end_addr
- pluginbuf
;
857 *(p_hdr
->api
) = &rockbox_api
;
862 #ifdef HAVE_REMOTE_LCD
863 lcd_remote_clear_display();
866 push_current_activity(ACTIVITY_PLUGIN
);
867 /* some plugins assume the entry cache doesn't move and save pointers to it
868 * they should be fixed properly instead of this lock */
869 tree_lock_cache(tree_get_context());
872 viewportmanager_theme_enable(i
, false, NULL
);
874 #ifdef HAVE_TOUCHSCREEN
875 touchscreen_set_mode(TOUCHSCREEN_BUTTON
);
878 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
882 int rc
= p_hdr
->entry_point(parameter
);
884 tree_unlock_cache(tree_get_context());
885 pop_current_activity();
888 { /* close handle if plugin is no tsr one */
889 lc_close(current_plugin_handle
);
890 current_plugin_handle
= NULL
;
893 /* Go back to the global setting in case the plugin changed it */
894 #ifdef HAVE_TOUCHSCREEN
895 touchscreen_set_mode(global_settings
.touch_mode
);
898 #ifdef HAVE_LCD_BITMAP
899 screen_helper_setfont(FONT_UI
);
901 #ifdef HAVE_LCD_COLOR
902 lcd_set_drawinfo(DRMODE_SOLID
, global_settings
.fg_color
,
903 global_settings
.bg_color
);
905 lcd_set_drawinfo(DRMODE_SOLID
, LCD_DEFAULT_FG
, LCD_DEFAULT_BG
);
907 #else /* LCD_DEPTH == 1 */
908 lcd_set_drawmode(DRMODE_SOLID
);
909 #endif /* LCD_DEPTH */
910 #endif /* HAVE_LCD_BITMAP */
913 #ifdef HAVE_REMOTE_LCD
914 #if LCD_REMOTE_DEPTH > 1
915 lcd_remote_set_drawinfo(DRMODE_SOLID
, LCD_REMOTE_DEFAULT_FG
,
916 LCD_REMOTE_DEFAULT_BG
);
918 lcd_remote_set_drawmode(DRMODE_SOLID
);
923 #ifdef HAVE_REMOTE_LCD
924 lcd_remote_clear_display();
928 viewportmanager_theme_undo(i
, true);
930 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
931 if(open_files
!= 0 && !current_plugin_handle
)
934 logf("Plugin '%s' leaks file handles", plugin
);
936 static const char *lines
[] =
937 { ID2P(LANG_PLUGIN_ERROR
),
938 "#leak-file-handles" };
939 static const struct text_message message
={ lines
, 2 };
940 button_clear_queue(); /* Empty the keyboard buffer */
941 gui_syncyesno_run(&message
, NULL
, NULL
);
943 for(fd
=0; fd
< MAX_OPEN_FILES
; fd
++)
944 if(open_files
& (1<<fd
))
949 if (rc
== PLUGIN_ERROR
)
950 splash(HZ
*2, str(LANG_PLUGIN_ERROR
));
955 /* Returns a pointer to the portion of the plugin buffer that is not already
956 being used. If no plugin is loaded, returns the entire plugin buffer */
957 void* plugin_get_buffer(size_t *buffer_size
)
961 if (current_plugin_handle
)
963 if (plugin_size
>= PLUGIN_BUFFER_SIZE
)
966 *buffer_size
= PLUGIN_BUFFER_SIZE
-plugin_size
;
967 buffer_pos
= plugin_size
;
971 *buffer_size
= PLUGIN_BUFFER_SIZE
;
975 return &pluginbuf
[buffer_pos
];
978 /* Returns a pointer to the mp3 buffer.
979 Playback gets stopped, to avoid conflicts.
980 Talk buffer is stolen as well.
982 void* plugin_get_audio_buffer(size_t *buffer_size
)
985 return audio_get_buffer(true, buffer_size
);
988 /* The plugin wants to stay resident after leaving its main function, e.g.
989 runs from timer or own thread. The callback is registered to later
990 instruct it to free its resources before a new plugin gets loaded. */
991 void plugin_tsr(bool (*exit_callback
)(bool))
993 pfn_tsr_exit
= exit_callback
; /* remember the callback for later */
996 char *plugin_get_current_filename(void)
998 return current_plugin
;
1001 static int open_wrapper(const char* pathname
, int flags
, ...)
1003 /* we don't have an 'open' function. it's a define. and we need
1004 * the real file_open, hence PREFIX() doesn't work here */
1006 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
1007 if (flags
& O_CREAT
)
1010 va_start(ap
, flags
);
1011 fd
= open(pathname
, flags
, va_arg(ap
, unsigned int));
1015 fd
= open(pathname
, flags
);
1017 fd
= file_open(pathname
,flags
);
1020 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1022 open_files
|= 1<<fd
;
1027 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1028 static int close_wrapper(int fd
)
1030 if((~open_files
) & (1<<fd
))
1032 logf("double close from plugin");
1035 open_files
&= (~(1<<fd
));
1037 return PREFIX(close
)(fd
);
1040 static int creat_wrapper(const char *pathname
, mode_t mode
)
1042 int fd
= PREFIX(creat
)(pathname
, mode
);
1045 open_files
|= (1<<fd
);
1049 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */