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 defined (SIMULATOR)
63 #define PREFIX(_x_) sim_ ## _x_
64 #elif defined (APPLICATION)
65 #define PREFIX(_x_) app_ ## _x_
70 #if defined (APPLICATION)
71 /* For symmetry reasons (we want app_ and sim_ to behave similarly), some
72 * wrappers are needed */
73 static int app_close(int fd
)
78 static ssize_t
app_read(int fd
, void *buf
, size_t count
)
80 return read(fd
,buf
,count
);
83 static off_t
app_lseek(int fd
, off_t offset
, int whence
)
85 return lseek(fd
,offset
,whence
);
88 static ssize_t
app_write(int fd
, const void *buf
, size_t count
)
90 return write(fd
,buf
,count
);
93 static int app_ftruncate(int fd
, off_t length
)
95 return ftruncate(fd
,length
);
98 static off_t
app_filesize(int fd
)
103 static int app_closedir(DIR *dirp
)
105 return closedir(dirp
);
108 static struct dirent
*app_readdir(DIR *dirp
)
110 return readdir(dirp
);
114 #if defined(HAVE_PLUGIN_CHECK_OPEN_CLOSE) && (MAX_OPEN_FILES>32)
115 #warning "MAX_OPEN_FILES>32, disabling plugin file open/close checking"
116 #undef HAVE_PLUGIN_CHECK_OPEN_CLOSE
119 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
120 static unsigned int open_files
;
123 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
124 static unsigned char pluginbuf
[PLUGIN_BUFFER_SIZE
];
125 void sim_lcd_ex_init(unsigned long (*getpixel
)(int, int));
126 void sim_lcd_ex_update_rect(int x
, int y
, int width
, int height
);
128 extern unsigned char pluginbuf
[];
132 /* for actual plugins only, not for codecs */
133 static int plugin_size
= 0;
134 static bool (*pfn_tsr_exit
)(bool reenter
) = NULL
; /* TSR exit callback */
135 static char current_plugin
[MAX_PATH
];
136 /* NULL if no plugin is loaded, otherwise the handle that lc_open() returned */
137 static void *current_plugin_handle
;
139 char *plugin_get_current_filename(void);
141 /* Some wrappers used to monitor open and close and detect leaks*/
142 static int open_wrapper(const char* pathname
, int flags
, ...);
143 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
144 static int close_wrapper(int fd
);
145 static int creat_wrapper(const char *pathname
, mode_t mode
);
148 static const struct plugin_api rockbox_api
= {
151 #ifdef HAVE_LCD_CONTRAST
163 #ifdef HAVE_LCD_CHARCELLS
165 lcd_get_locked_pattern
,
173 &lcd_framebuffer
[0][0],
184 lcd_mono_bitmap_part
,
197 lcd_bitmap_transparent_part
,
198 lcd_bitmap_transparent
,
201 #if defined(TOSHIBA_GIGABEAT_F) || defined(SANSA_E200) || defined(SANSA_C200) \
202 || defined(IRIVER_H10) || defined(COWON_D2) || defined(PHILIPS_HDD1630) \
203 || defined(SANSA_FUZE) || defined(SANSA_E200V2) || defined(SANSA_FUZEV2) \
204 || defined(TOSHIBA_GIGABEAT_S) || defined(PHILIPS_SA9200)
207 #endif /* MEMORYSIZE > 2 */
208 #elif (LCD_DEPTH < 4) && (CONFIG_PLATFORM & PLATFORM_NATIVE)
211 #endif /* LCD_DEPTH */
212 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
214 lcd_pal256_update_pal
,
217 lcd_puts_scroll_style
,
218 #ifdef HAVE_LCD_INVERT
219 lcd_set_invert_display
,
220 #endif /* HAVE_LCD_INVERT */
221 #if defined(HAVE_LCD_MODES)
224 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
228 #ifdef HAVE_LCD_BITMAP
238 #endif /* HAVE_LCD_BITMAP */
243 backlight_set_timeout
,
244 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
245 backlight_set_brightness
,
246 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
249 backlight_set_timeout_plugged
,
255 #ifdef HAVE_REMOTE_LCD
257 lcd_remote_set_contrast
,
258 lcd_remote_clear_display
,
260 lcd_remote_puts_scroll
,
261 lcd_remote_stop_scroll
,
262 lcd_remote_set_drawmode
,
263 lcd_remote_get_drawmode
,
265 lcd_remote_getstringsize
,
266 lcd_remote_drawpixel
,
272 lcd_remote_mono_bitmap_part
,
273 lcd_remote_mono_bitmap
,
275 lcd_remote_puts_style
,
276 lcd_remote_puts_scroll_style
,
277 &lcd_remote_framebuffer
[0][0],
279 lcd_remote_update_rect
,
282 remote_backlight_off
,
283 remote_backlight_set_timeout
,
285 remote_backlight_set_timeout_plugged
,
287 #endif /* HAVE_REMOTE_LCD */
289 {&screens
[SCREEN_MAIN
], &screens
[SCREEN_REMOTE
]},
291 {&screens
[SCREEN_MAIN
]},
293 #if defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
294 lcd_remote_set_foreground
,
295 lcd_remote_get_foreground
,
296 lcd_remote_set_background
,
297 lcd_remote_get_background
,
298 lcd_remote_bitmap_part
,
301 viewport_set_defaults
,
302 #ifdef HAVE_LCD_BITMAP
303 viewportmanager_theme_enable
,
304 viewportmanager_theme_undo
,
309 gui_synclist_set_nb_items
,
310 gui_synclist_set_icon_callback
,
311 gui_synclist_get_nb_items
,
312 gui_synclist_get_sel_pos
,
314 gui_synclist_select_item
,
315 gui_synclist_add_item
,
316 gui_synclist_del_item
,
317 gui_synclist_limit_scroll
,
318 gui_synclist_do_button
,
319 gui_synclist_set_title
,
321 simplelist_info_init
,
322 simplelist_show_list
,
328 #ifdef HAVE_BUTTON_DATA
334 #ifdef HAS_BUTTON_HOLD
337 #ifdef HAVE_TOUCHSCREEN
338 touchscreen_set_mode
,
341 #ifdef HAVE_BUTTON_LIGHT
342 buttonlight_set_timeout
,
345 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
346 buttonlight_set_brightness
,
347 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
348 #endif /* HAVE_BUTTON_LIGHT */
352 (open_func
)open_wrapper
,
353 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
358 (read_func
)PREFIX(read
),
360 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
361 (creat_func
)creat_wrapper
,
365 (write_func
)PREFIX(write
),
376 #if USING_STORAGE_CALLBACK
377 register_storage_idle_func
,
378 unregister_storage_idle_func
,
379 #endif /* USING_STORAGE_CALLBACK */
381 create_numbered_filename
,
388 (opendir_func
)PREFIX(opendir
),
389 (closedir_func
)PREFIX(closedir
),
390 (readdir_func
)PREFIX(readdir
),
401 #if defined(CPU_ARM) && CONFIG_PLATFORM & PLATFORM_NATIVE
407 default_event_handler
,
408 default_event_handler_ex
,
413 #if (CONFIG_CODEC == SWCODEC)
415 #ifdef HAVE_PRIORITY_SCHEDULING
423 reset_poweroff_timer
,
424 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
428 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
429 #ifdef CPU_BOOST_LOGGING
434 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
435 #endif /* PLATFORM_NATIVE */
436 #ifdef HAVE_SCHEDULER_BOOSTCTRL
457 #if CONFIG_CODEC == SWCODEC
458 queue_enable_queue_send
,
465 #ifdef USB_ENABLE_HID
471 __cyg_profile_func_enter
,
472 __cyg_profile_func_exit
,
478 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
479 /* special simulator hooks */
480 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
482 sim_lcd_ex_update_rect
,
486 /* strings and memory */
500 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
527 #ifdef AUDIOHW_HAVE_EQ
528 sound_enum_hw_eq_band_setting
,
530 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
535 #if CONFIG_CODEC != SWCODEC
539 #if CONFIG_CODEC == SWCODEC
540 &audio_master_sampr_list
[0],
549 pcm_get_bytes_waiting
,
555 #ifdef HAVE_RECORDING
561 pcm_calculate_rec_peaks
,
562 audio_set_recording_gain
,
563 #endif /* HAVE_RECORDING */
564 #if INPUT_SRC_CAPS != 0
565 audio_set_output_source
,
566 audio_set_input_source
,
575 #endif /* CONFIG_CODEC == SWCODEC */
577 /* playback control */
583 playlist_remove_all_tracks
,
585 playlist_insert_track
,
586 playlist_insert_directory
,
598 audio_flush_and_reload_tracks
,
600 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
601 mpeg_get_last_header
,
603 #if ((CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
604 (CONFIG_CODEC == SWCODEC)) && defined (HAVE_PITCHSCREEN)
608 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
609 /* MAS communication */
614 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
621 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
627 gui_syncstatusbar_draw
,
637 #ifdef HAVE_LCD_COLOR
641 /* action handling */
644 #ifdef HAVE_TOUCHSCREEN
645 action_get_touchscreen_press
,
653 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
658 # if CONFIG_CHARGING >= CHARGING_MONITOR
662 #ifdef HAVE_USB_POWER
667 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
680 plugin_get_audio_buffer
,
682 plugin_get_current_filename
,
683 #if defined(DEBUG) || defined(SIMULATOR)
686 #ifdef ROCKBOX_HAS_LOGF
692 #if CONFIG_CODEC == SWCODEC
693 codec_thread_do_callback
,
700 round_value_to_list32
,
701 #endif /* CONFIG_CODEC == SWCODEC */
707 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
708 peak_meter_scale_value
,
709 peak_meter_set_use_dbfs
,
710 peak_meter_get_use_dbfs
,
712 #ifdef HAVE_LCD_BITMAP
719 screen_dump_set_hook
,
726 #ifdef HAVE_WHEEL_POSITION
731 #ifdef IRIVER_H100_SERIES
732 /* Routines for the iriver_flash -plugin. */
733 detect_original_firmware
,
734 detect_flashed_ramimage
,
735 detect_flashed_romimage
,
738 #if (CONFIG_CODEC == SWCODEC)
756 tagcache_search_set_uniqbuf
,
757 tagcache_search_add_filter
,
760 tagcache_search_finish
,
761 tagcache_get_numeric
,
762 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
768 search_albumart_files
,
771 #ifdef HAVE_SEMAPHORE_OBJECTS
779 /* new stuff at the end, sort into place next time
780 the API gets incompatible */
783 int plugin_load(const char* plugin
, const void* parameter
)
786 struct plugin_header
*p_hdr
;
787 struct lc_header
*hdr
;
790 fb_data
* old_backdrop
;
793 if (current_plugin_handle
&& pfn_tsr_exit
)
794 { /* if we have a resident old plugin and a callback */
795 if (pfn_tsr_exit(!strcmp(current_plugin
, plugin
)) == false )
797 /* not allowing another plugin to load */
800 lc_close(current_plugin_handle
);
801 current_plugin_handle
= pfn_tsr_exit
= NULL
;
804 splash(0, ID2P(LANG_WAIT
));
805 strcpy(current_plugin
, plugin
);
807 current_plugin_handle
= lc_open(plugin
, pluginbuf
, PLUGIN_BUFFER_SIZE
);
808 if (current_plugin_handle
== NULL
) {
809 splashf(HZ
*2, str(LANG_PLUGIN_CANT_OPEN
), plugin
);
813 p_hdr
= lc_get_header(current_plugin_handle
);
815 hdr
= p_hdr
? &p_hdr
->lc_hdr
: NULL
;
819 || hdr
->magic
!= PLUGIN_MAGIC
820 || hdr
->target_id
!= TARGET_ID
821 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
822 || hdr
->load_addr
!= pluginbuf
823 || hdr
->end_addr
> pluginbuf
+ PLUGIN_BUFFER_SIZE
827 lc_close(current_plugin_handle
);
828 splash(HZ
*2, str(LANG_PLUGIN_WRONG_MODEL
));
831 if (hdr
->api_version
> PLUGIN_API_VERSION
832 || hdr
->api_version
< PLUGIN_MIN_API_VERSION
)
834 lc_close(current_plugin_handle
);
835 splash(HZ
*2, str(LANG_PLUGIN_WRONG_VERSION
));
838 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
839 plugin_size
= hdr
->end_addr
- pluginbuf
;
844 *(p_hdr
->api
) = &rockbox_api
;
846 #if defined HAVE_LCD_BITMAP && LCD_DEPTH > 1
847 old_backdrop
= lcd_get_backdrop();
852 #ifdef HAVE_REMOTE_LCD
853 lcd_remote_clear_display();
858 viewportmanager_theme_enable(i
, false, NULL
);
860 #ifdef HAVE_TOUCHSCREEN
861 touchscreen_set_mode(TOUCHSCREEN_BUTTON
);
864 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
868 rc
= p_hdr
->entry_point(parameter
);
871 { /* close handle if plugin is no tsr one */
872 lc_close(current_plugin_handle
);
873 current_plugin_handle
= NULL
;
876 /* Go back to the global setting in case the plugin changed it */
877 #ifdef HAVE_TOUCHSCREEN
878 touchscreen_set_mode(global_settings
.touch_mode
);
881 #ifdef HAVE_LCD_BITMAP
882 lcd_setfont(FONT_UI
);
884 lcd_set_backdrop(old_backdrop
);
885 #ifdef HAVE_LCD_COLOR
886 lcd_set_drawinfo(DRMODE_SOLID
, global_settings
.fg_color
,
887 global_settings
.bg_color
);
889 lcd_set_drawinfo(DRMODE_SOLID
, LCD_DEFAULT_FG
, LCD_DEFAULT_BG
);
891 #else /* LCD_DEPTH == 1 */
892 lcd_set_drawmode(DRMODE_SOLID
);
893 #endif /* LCD_DEPTH */
894 #endif /* HAVE_LCD_BITMAP */
897 #ifdef HAVE_REMOTE_LCD
898 #if LCD_REMOTE_DEPTH > 1
899 lcd_remote_set_drawinfo(DRMODE_SOLID
, LCD_REMOTE_DEFAULT_FG
,
900 LCD_REMOTE_DEFAULT_BG
);
902 lcd_remote_set_drawmode(DRMODE_SOLID
);
907 #ifdef HAVE_REMOTE_LCD
908 lcd_remote_clear_display();
912 viewportmanager_theme_undo(i
, false);
914 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
915 if(open_files
!= 0 && !current_plugin_handle
)
918 logf("Plugin '%s' leaks file handles", plugin
);
920 static const char *lines
[] =
921 { ID2P(LANG_PLUGIN_ERROR
),
922 "#leak-file-handles" };
923 static const struct text_message message
={ lines
, 2 };
924 button_clear_queue(); /* Empty the keyboard buffer */
925 gui_syncyesno_run(&message
, NULL
, NULL
);
927 for(fd
=0; fd
< MAX_OPEN_FILES
; fd
++)
928 if(open_files
& (1<<fd
))
933 if (rc
== PLUGIN_ERROR
)
934 splash(HZ
*2, str(LANG_PLUGIN_ERROR
));
939 /* Returns a pointer to the portion of the plugin buffer that is not already
940 being used. If no plugin is loaded, returns the entire plugin buffer */
941 void* plugin_get_buffer(size_t *buffer_size
)
945 if (current_plugin_handle
)
947 if (plugin_size
>= PLUGIN_BUFFER_SIZE
)
950 *buffer_size
= PLUGIN_BUFFER_SIZE
-plugin_size
;
951 buffer_pos
= plugin_size
;
955 *buffer_size
= PLUGIN_BUFFER_SIZE
;
959 return &pluginbuf
[buffer_pos
];
962 /* Returns a pointer to the mp3 buffer.
963 Playback gets stopped, to avoid conflicts.
964 Talk buffer is stolen as well.
966 void* plugin_get_audio_buffer(size_t *buffer_size
)
968 #if CONFIG_CODEC == SWCODEC
969 return audio_get_buffer(true, buffer_size
);
972 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
973 *buffer_size
= audiobufend
- audiobuf
;
978 /* The plugin wants to stay resident after leaving its main function, e.g.
979 runs from timer or own thread. The callback is registered to later
980 instruct it to free its resources before a new plugin gets loaded. */
981 void plugin_tsr(bool (*exit_callback
)(bool))
983 pfn_tsr_exit
= exit_callback
; /* remember the callback for later */
986 char *plugin_get_current_filename(void)
988 return current_plugin
;
991 static int open_wrapper(const char* pathname
, int flags
, ...)
993 /* we don't have an 'open' function. it's a define. and we need
994 * the real file_open, hence PREFIX() doesn't work here */
996 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
1000 va_start(ap
, flags
);
1001 fd
= open(pathname
, flags
, va_arg(ap
, unsigned int));
1005 fd
= open(pathname
, flags
);
1007 fd
= file_open(pathname
,flags
);
1010 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1012 open_files
|= 1<<fd
;
1017 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1018 static int close_wrapper(int fd
)
1020 if((~open_files
) & (1<<fd
))
1022 logf("double close from plugin");
1025 open_files
&= (~(1<<fd
));
1027 return PREFIX(close
)(fd
);
1030 static int creat_wrapper(const char *pathname
, mode_t mode
)
1032 int fd
= PREFIX(creat
)(pathname
, mode
);
1035 open_files
|= (1<<fd
);
1039 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */