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_
66 #define PREFIX(_x_) _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
);
98 #if defined(HAVE_PLUGIN_CHECK_OPEN_CLOSE) && (MAX_OPEN_FILES>32)
99 #warning "MAX_OPEN_FILES>32, disabling plugin file open/close checking"
100 #undef HAVE_PLUGIN_CHECK_OPEN_CLOSE
103 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
104 static unsigned int open_files
;
107 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
108 static unsigned char pluginbuf
[PLUGIN_BUFFER_SIZE
];
109 void sim_lcd_ex_init(unsigned long (*getpixel
)(int, int));
110 void sim_lcd_ex_update_rect(int x
, int y
, int width
, int height
);
112 extern unsigned char pluginbuf
[];
116 /* for actual plugins only, not for codecs */
117 static int plugin_size
= 0;
118 static bool (*pfn_tsr_exit
)(bool reenter
) = NULL
; /* TSR exit callback */
119 static char current_plugin
[MAX_PATH
];
120 /* NULL if no plugin is loaded, otherwise the handle that lc_open() returned */
121 static void *current_plugin_handle
;
123 char *plugin_get_current_filename(void);
125 /* Some wrappers used to monitor open and close and detect leaks*/
126 static int open_wrapper(const char* pathname
, int flags
, ...);
127 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
128 static int close_wrapper(int fd
);
129 static int creat_wrapper(const char *pathname
, mode_t mode
);
132 static const struct plugin_api rockbox_api
= {
135 #ifdef HAVE_LCD_CONTRAST
147 #ifdef HAVE_LCD_CHARCELLS
149 lcd_get_locked_pattern
,
157 &lcd_static_framebuffer
[0][0],
161 screen_helper_setfont
,
168 lcd_mono_bitmap_part
,
181 lcd_bitmap_transparent_part
,
182 lcd_bitmap_transparent
,
185 #if defined(TOSHIBA_GIGABEAT_F) || defined(SANSA_E200) || defined(SANSA_C200) \
186 || defined(IRIVER_H10) || defined(COWON_D2) || defined(PHILIPS_HDD1630) \
187 || defined(SANSA_FUZE) || defined(SANSA_E200V2) || defined(SANSA_FUZEV2) \
188 || defined(TOSHIBA_GIGABEAT_S) || defined(PHILIPS_SA9200)
191 #endif /* MEMORYSIZE > 2 */
192 #elif (LCD_DEPTH < 4) && (CONFIG_PLATFORM & PLATFORM_NATIVE)
195 #endif /* LCD_DEPTH */
196 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
198 lcd_pal256_update_pal
,
201 lcd_puts_scroll_style
,
202 #ifdef HAVE_LCD_INVERT
203 lcd_set_invert_display
,
204 #endif /* HAVE_LCD_INVERT */
205 #if defined(HAVE_LCD_MODES)
208 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
212 #ifdef HAVE_LCD_BITMAP
223 #endif /* HAVE_LCD_BITMAP */
228 backlight_set_timeout
,
229 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
230 backlight_set_brightness
,
231 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
234 backlight_set_timeout_plugged
,
240 #ifdef HAVE_REMOTE_LCD
242 lcd_remote_set_contrast
,
243 lcd_remote_clear_display
,
245 lcd_remote_puts_scroll
,
246 lcd_remote_stop_scroll
,
247 lcd_remote_set_drawmode
,
248 lcd_remote_get_drawmode
,
250 lcd_remote_getstringsize
,
251 lcd_remote_drawpixel
,
257 lcd_remote_mono_bitmap_part
,
258 lcd_remote_mono_bitmap
,
260 lcd_remote_puts_style
,
261 lcd_remote_puts_scroll_style
,
262 &lcd_remote_static_framebuffer
[0][0],
264 lcd_remote_update_rect
,
267 remote_backlight_off
,
268 remote_backlight_set_timeout
,
270 remote_backlight_set_timeout_plugged
,
272 #endif /* HAVE_REMOTE_LCD */
274 {&screens
[SCREEN_MAIN
], &screens
[SCREEN_REMOTE
]},
276 {&screens
[SCREEN_MAIN
]},
278 #if defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
279 lcd_remote_set_foreground
,
280 lcd_remote_get_foreground
,
281 lcd_remote_set_background
,
282 lcd_remote_get_background
,
283 lcd_remote_bitmap_part
,
286 viewport_set_defaults
,
287 #ifdef HAVE_LCD_BITMAP
288 viewportmanager_theme_enable
,
289 viewportmanager_theme_undo
,
294 gui_synclist_set_nb_items
,
295 gui_synclist_set_icon_callback
,
296 gui_synclist_get_nb_items
,
297 gui_synclist_get_sel_pos
,
299 gui_synclist_select_item
,
300 gui_synclist_add_item
,
301 gui_synclist_del_item
,
302 gui_synclist_limit_scroll
,
303 gui_synclist_do_button
,
304 gui_synclist_set_title
,
306 simplelist_info_init
,
307 simplelist_show_list
,
313 #ifdef HAVE_BUTTON_DATA
319 #ifdef HAS_BUTTON_HOLD
322 #ifdef HAVE_TOUCHSCREEN
323 touchscreen_set_mode
,
324 touchscreen_get_mode
,
327 #ifdef HAVE_BUTTON_LIGHT
328 buttonlight_set_timeout
,
331 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
332 buttonlight_set_brightness
,
333 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
334 #endif /* HAVE_BUTTON_LIGHT */
338 (open_func
)open_wrapper
,
339 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
344 (read_func
)PREFIX(read
),
346 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
347 (creat_func
)creat_wrapper
,
351 (write_func
)PREFIX(write
),
362 #if USING_STORAGE_CALLBACK
363 register_storage_idle_func
,
364 unregister_storage_idle_func
,
365 #endif /* USING_STORAGE_CALLBACK */
367 create_numbered_filename
,
374 (opendir_func
)opendir
,
375 (closedir_func
)closedir
,
376 (readdir_func
)readdir
,
387 #if defined(CPU_ARM) && CONFIG_PLATFORM & PLATFORM_NATIVE
393 default_event_handler
,
394 default_event_handler_ex
,
399 #if (CONFIG_CODEC == SWCODEC)
401 #ifdef HAVE_PRIORITY_SCHEDULING
409 reset_poweroff_timer
,
410 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
414 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
415 #ifdef CPU_BOOST_LOGGING
420 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
421 #endif /* PLATFORM_NATIVE */
422 #ifdef HAVE_SCHEDULER_BOOSTCTRL
428 commit_discard_dcache
,
429 commit_discard_idcache
,
444 #if CONFIG_CODEC == SWCODEC
445 queue_enable_queue_send
,
452 #ifdef USB_ENABLE_HID
458 __cyg_profile_func_enter
,
459 __cyg_profile_func_exit
,
465 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
466 /* special simulator hooks */
467 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
469 sim_lcd_ex_update_rect
,
473 /* strings and memory */
487 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
507 /* the buflib memory management library */
512 buflib_alloc_maximum
,
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
,
568 dsp_set_crossfeed_type
,
571 #ifdef HAVE_PITCHCONTROL
578 mixer_channel_status
,
579 mixer_channel_get_buffer
,
580 mixer_channel_calculate_peaks
,
581 mixer_channel_play_data
,
582 mixer_channel_play_pause
,
584 mixer_channel_set_amplitude
,
585 mixer_channel_get_bytes_waiting
,
589 #endif /* CONFIG_CODEC == SWCODEC */
590 /* playback control */
596 playlist_remove_all_tracks
,
598 playlist_insert_track
,
599 playlist_insert_directory
,
611 audio_flush_and_reload_tracks
,
613 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
614 mpeg_get_last_header
,
616 #if ((CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
617 (CONFIG_CODEC == SWCODEC)) && defined (HAVE_PITCHCONTROL)
621 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
622 /* MAS communication */
627 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
634 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
640 gui_syncstatusbar_draw
,
650 #ifdef HAVE_LCD_COLOR
654 /* action handling */
657 #ifdef HAVE_TOUCHSCREEN
658 action_get_touchscreen_press
,
669 # if CONFIG_CHARGING >= CHARGING_MONITOR
673 #ifdef HAVE_USB_POWER
678 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
691 plugin_get_audio_buffer
,
693 plugin_get_current_filename
,
694 #if defined(DEBUG) || defined(SIMULATOR)
697 #ifdef ROCKBOX_HAS_LOGF
703 #if CONFIG_CODEC == SWCODEC
704 codec_thread_do_callback
,
711 round_value_to_list32
,
712 #endif /* CONFIG_CODEC == SWCODEC */
718 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
719 peak_meter_scale_value
,
720 peak_meter_set_use_dbfs
,
721 peak_meter_get_use_dbfs
,
723 #ifdef HAVE_LCD_BITMAP
730 screen_dump_set_hook
,
739 #ifdef HAVE_WHEEL_POSITION
744 #ifdef IRIVER_H100_SERIES
745 /* Routines for the iriver_flash -plugin. */
746 detect_original_firmware
,
747 detect_flashed_ramimage
,
748 detect_flashed_romimage
,
751 #if (CONFIG_CODEC == SWCODEC)
769 tagcache_search_set_uniqbuf
,
770 tagcache_search_add_filter
,
773 tagcache_search_finish
,
774 tagcache_get_numeric
,
775 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
781 search_albumart_files
,
784 #ifdef HAVE_SEMAPHORE_OBJECTS
792 /* new stuff at the end, sort into place next time
793 the API gets incompatible */
795 #ifdef HAVE_LCD_BITMAP
796 #if CONFIG_CODEC == SWCODEC
797 mixer_channel_set_buffer_hook
,
800 viewport_set_fullscreen
,
805 int plugin_load(const char* plugin
, const void* parameter
)
807 struct plugin_header
*p_hdr
;
808 struct lc_header
*hdr
;
810 if (current_plugin_handle
&& pfn_tsr_exit
)
811 { /* if we have a resident old plugin and a callback */
812 if (pfn_tsr_exit(!strcmp(current_plugin
, plugin
)) == false )
814 /* not allowing another plugin to load */
817 lc_close(current_plugin_handle
);
818 current_plugin_handle
= pfn_tsr_exit
= NULL
;
821 splash(0, ID2P(LANG_WAIT
));
822 strcpy(current_plugin
, plugin
);
824 current_plugin_handle
= lc_open(plugin
, pluginbuf
, PLUGIN_BUFFER_SIZE
);
825 if (current_plugin_handle
== NULL
) {
826 splashf(HZ
*2, str(LANG_PLUGIN_CANT_OPEN
), plugin
);
830 p_hdr
= lc_get_header(current_plugin_handle
);
832 hdr
= p_hdr
? &p_hdr
->lc_hdr
: NULL
;
836 || hdr
->magic
!= PLUGIN_MAGIC
837 || hdr
->target_id
!= TARGET_ID
838 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
839 || hdr
->load_addr
!= pluginbuf
840 || hdr
->end_addr
> pluginbuf
+ PLUGIN_BUFFER_SIZE
844 lc_close(current_plugin_handle
);
845 splash(HZ
*2, str(LANG_PLUGIN_WRONG_MODEL
));
848 if (hdr
->api_version
> PLUGIN_API_VERSION
849 || hdr
->api_version
< PLUGIN_MIN_API_VERSION
)
851 lc_close(current_plugin_handle
);
852 splash(HZ
*2, str(LANG_PLUGIN_WRONG_VERSION
));
855 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
856 plugin_size
= hdr
->end_addr
- pluginbuf
;
861 *(p_hdr
->api
) = &rockbox_api
;
866 #ifdef HAVE_REMOTE_LCD
867 lcd_remote_clear_display();
870 push_current_activity(ACTIVITY_PLUGIN
);
871 /* some plugins assume the entry cache doesn't move and save pointers to it
872 * they should be fixed properly instead of this lock */
873 tree_lock_cache(tree_get_context());
876 viewportmanager_theme_enable(i
, false, NULL
);
878 #ifdef HAVE_TOUCHSCREEN
879 touchscreen_set_mode(TOUCHSCREEN_BUTTON
);
882 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
886 int rc
= p_hdr
->entry_point(parameter
);
888 tree_unlock_cache(tree_get_context());
889 pop_current_activity();
892 { /* close handle if plugin is no tsr one */
893 lc_close(current_plugin_handle
);
894 current_plugin_handle
= NULL
;
897 /* Go back to the global setting in case the plugin changed it */
898 #ifdef HAVE_TOUCHSCREEN
899 touchscreen_set_mode(global_settings
.touch_mode
);
902 #ifdef HAVE_LCD_BITMAP
903 screen_helper_setfont(FONT_UI
);
905 #ifdef HAVE_LCD_COLOR
906 lcd_set_drawinfo(DRMODE_SOLID
, global_settings
.fg_color
,
907 global_settings
.bg_color
);
909 lcd_set_drawinfo(DRMODE_SOLID
, LCD_DEFAULT_FG
, LCD_DEFAULT_BG
);
911 #else /* LCD_DEPTH == 1 */
912 lcd_set_drawmode(DRMODE_SOLID
);
913 #endif /* LCD_DEPTH */
914 #endif /* HAVE_LCD_BITMAP */
917 #ifdef HAVE_REMOTE_LCD
918 #if LCD_REMOTE_DEPTH > 1
919 lcd_remote_set_drawinfo(DRMODE_SOLID
, LCD_REMOTE_DEFAULT_FG
,
920 LCD_REMOTE_DEFAULT_BG
);
922 lcd_remote_set_drawmode(DRMODE_SOLID
);
927 #ifdef HAVE_REMOTE_LCD
928 lcd_remote_clear_display();
932 viewportmanager_theme_undo(i
, true);
934 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
935 if(open_files
!= 0 && !current_plugin_handle
)
938 logf("Plugin '%s' leaks file handles", plugin
);
940 static const char *lines
[] =
941 { ID2P(LANG_PLUGIN_ERROR
),
942 "#leak-file-handles" };
943 static const struct text_message message
={ lines
, 2 };
944 button_clear_queue(); /* Empty the keyboard buffer */
945 gui_syncyesno_run(&message
, NULL
, NULL
);
947 for(fd
=0; fd
< MAX_OPEN_FILES
; fd
++)
948 if(open_files
& (1<<fd
))
953 if (rc
== PLUGIN_ERROR
)
954 splash(HZ
*2, str(LANG_PLUGIN_ERROR
));
959 /* Returns a pointer to the portion of the plugin buffer that is not already
960 being used. If no plugin is loaded, returns the entire plugin buffer */
961 void* plugin_get_buffer(size_t *buffer_size
)
965 if (current_plugin_handle
)
967 if (plugin_size
>= PLUGIN_BUFFER_SIZE
)
970 *buffer_size
= PLUGIN_BUFFER_SIZE
-plugin_size
;
971 buffer_pos
= plugin_size
;
975 *buffer_size
= PLUGIN_BUFFER_SIZE
;
979 return &pluginbuf
[buffer_pos
];
982 /* Returns a pointer to the mp3 buffer.
983 Playback gets stopped, to avoid conflicts.
984 Talk buffer is stolen as well.
986 void* plugin_get_audio_buffer(size_t *buffer_size
)
989 return audio_get_buffer(true, buffer_size
);
992 /* The plugin wants to stay resident after leaving its main function, e.g.
993 runs from timer or own thread. The callback is registered to later
994 instruct it to free its resources before a new plugin gets loaded. */
995 void plugin_tsr(bool (*exit_callback
)(bool))
997 pfn_tsr_exit
= exit_callback
; /* remember the callback for later */
1000 char *plugin_get_current_filename(void)
1002 return current_plugin
;
1005 static int open_wrapper(const char* pathname
, int flags
, ...)
1007 /* we don't have an 'open' function. it's a define. and we need
1008 * the real file_open, hence PREFIX() doesn't work here */
1010 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
1011 if (flags
& O_CREAT
)
1014 va_start(ap
, flags
);
1015 fd
= open(pathname
, flags
, va_arg(ap
, unsigned int));
1019 fd
= open(pathname
, flags
);
1021 fd
= file_open(pathname
,flags
);
1024 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1026 open_files
|= 1<<fd
;
1031 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1032 static int close_wrapper(int fd
)
1034 if((~open_files
) & (1<<fd
))
1036 logf("double close from plugin");
1039 open_files
&= (~(1<<fd
));
1041 return PREFIX(close
)(fd
);
1044 static int creat_wrapper(const char *pathname
, mode_t mode
)
1048 int fd
= PREFIX(creat
)(pathname
, mode
);
1051 open_files
|= (1<<fd
);
1055 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */