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_crossfeed_enable
,
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 */
796 int plugin_load(const char* plugin
, const void* parameter
)
798 struct plugin_header
*p_hdr
;
799 struct lc_header
*hdr
;
801 if (current_plugin_handle
&& pfn_tsr_exit
)
802 { /* if we have a resident old plugin and a callback */
803 if (pfn_tsr_exit(!strcmp(current_plugin
, plugin
)) == false )
805 /* not allowing another plugin to load */
808 lc_close(current_plugin_handle
);
809 current_plugin_handle
= pfn_tsr_exit
= NULL
;
812 splash(0, ID2P(LANG_WAIT
));
813 strcpy(current_plugin
, plugin
);
815 current_plugin_handle
= lc_open(plugin
, pluginbuf
, PLUGIN_BUFFER_SIZE
);
816 if (current_plugin_handle
== NULL
) {
817 splashf(HZ
*2, str(LANG_PLUGIN_CANT_OPEN
), plugin
);
821 p_hdr
= lc_get_header(current_plugin_handle
);
823 hdr
= p_hdr
? &p_hdr
->lc_hdr
: NULL
;
827 || hdr
->magic
!= PLUGIN_MAGIC
828 || hdr
->target_id
!= TARGET_ID
829 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
830 || hdr
->load_addr
!= pluginbuf
831 || hdr
->end_addr
> pluginbuf
+ PLUGIN_BUFFER_SIZE
835 lc_close(current_plugin_handle
);
836 splash(HZ
*2, str(LANG_PLUGIN_WRONG_MODEL
));
839 if (hdr
->api_version
> PLUGIN_API_VERSION
840 || hdr
->api_version
< PLUGIN_MIN_API_VERSION
)
842 lc_close(current_plugin_handle
);
843 splash(HZ
*2, str(LANG_PLUGIN_WRONG_VERSION
));
846 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
847 plugin_size
= hdr
->end_addr
- pluginbuf
;
852 *(p_hdr
->api
) = &rockbox_api
;
857 #ifdef HAVE_REMOTE_LCD
858 lcd_remote_clear_display();
861 push_current_activity(ACTIVITY_PLUGIN
);
862 /* some plugins assume the entry cache doesn't move and save pointers to it
863 * they should be fixed properly instead of this lock */
864 tree_lock_cache(tree_get_context());
867 viewportmanager_theme_enable(i
, false, NULL
);
869 #ifdef HAVE_TOUCHSCREEN
870 touchscreen_set_mode(TOUCHSCREEN_BUTTON
);
873 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
877 int rc
= p_hdr
->entry_point(parameter
);
879 tree_unlock_cache(tree_get_context());
880 pop_current_activity();
883 { /* close handle if plugin is no tsr one */
884 lc_close(current_plugin_handle
);
885 current_plugin_handle
= NULL
;
888 /* Go back to the global setting in case the plugin changed it */
889 #ifdef HAVE_TOUCHSCREEN
890 touchscreen_set_mode(global_settings
.touch_mode
);
893 #ifdef HAVE_LCD_BITMAP
894 screen_helper_setfont(FONT_UI
);
896 #ifdef HAVE_LCD_COLOR
897 lcd_set_drawinfo(DRMODE_SOLID
, global_settings
.fg_color
,
898 global_settings
.bg_color
);
900 lcd_set_drawinfo(DRMODE_SOLID
, LCD_DEFAULT_FG
, LCD_DEFAULT_BG
);
902 #else /* LCD_DEPTH == 1 */
903 lcd_set_drawmode(DRMODE_SOLID
);
904 #endif /* LCD_DEPTH */
905 #endif /* HAVE_LCD_BITMAP */
908 #ifdef HAVE_REMOTE_LCD
909 #if LCD_REMOTE_DEPTH > 1
910 lcd_remote_set_drawinfo(DRMODE_SOLID
, LCD_REMOTE_DEFAULT_FG
,
911 LCD_REMOTE_DEFAULT_BG
);
913 lcd_remote_set_drawmode(DRMODE_SOLID
);
918 #ifdef HAVE_REMOTE_LCD
919 lcd_remote_clear_display();
923 viewportmanager_theme_undo(i
, true);
925 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
926 if(open_files
!= 0 && !current_plugin_handle
)
929 logf("Plugin '%s' leaks file handles", plugin
);
931 static const char *lines
[] =
932 { ID2P(LANG_PLUGIN_ERROR
),
933 "#leak-file-handles" };
934 static const struct text_message message
={ lines
, 2 };
935 button_clear_queue(); /* Empty the keyboard buffer */
936 gui_syncyesno_run(&message
, NULL
, NULL
);
938 for(fd
=0; fd
< MAX_OPEN_FILES
; fd
++)
939 if(open_files
& (1<<fd
))
944 if (rc
== PLUGIN_ERROR
)
945 splash(HZ
*2, str(LANG_PLUGIN_ERROR
));
950 /* Returns a pointer to the portion of the plugin buffer that is not already
951 being used. If no plugin is loaded, returns the entire plugin buffer */
952 void* plugin_get_buffer(size_t *buffer_size
)
956 if (current_plugin_handle
)
958 if (plugin_size
>= PLUGIN_BUFFER_SIZE
)
961 *buffer_size
= PLUGIN_BUFFER_SIZE
-plugin_size
;
962 buffer_pos
= plugin_size
;
966 *buffer_size
= PLUGIN_BUFFER_SIZE
;
970 return &pluginbuf
[buffer_pos
];
973 /* Returns a pointer to the mp3 buffer.
974 Playback gets stopped, to avoid conflicts.
975 Talk buffer is stolen as well.
977 void* plugin_get_audio_buffer(size_t *buffer_size
)
980 return audio_get_buffer(true, buffer_size
);
983 /* The plugin wants to stay resident after leaving its main function, e.g.
984 runs from timer or own thread. The callback is registered to later
985 instruct it to free its resources before a new plugin gets loaded. */
986 void plugin_tsr(bool (*exit_callback
)(bool))
988 pfn_tsr_exit
= exit_callback
; /* remember the callback for later */
991 char *plugin_get_current_filename(void)
993 return current_plugin
;
996 static int open_wrapper(const char* pathname
, int flags
, ...)
998 /* we don't have an 'open' function. it's a define. and we need
999 * the real file_open, hence PREFIX() doesn't work here */
1001 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
1002 if (flags
& O_CREAT
)
1005 va_start(ap
, flags
);
1006 fd
= open(pathname
, flags
, va_arg(ap
, unsigned int));
1010 fd
= open(pathname
, flags
);
1012 fd
= file_open(pathname
,flags
);
1015 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1017 open_files
|= 1<<fd
;
1022 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1023 static int close_wrapper(int fd
)
1025 if((~open_files
) & (1<<fd
))
1027 logf("double close from plugin");
1030 open_files
&= (~(1<<fd
));
1032 return PREFIX(close
)(fd
);
1035 static int creat_wrapper(const char *pathname
, mode_t mode
)
1039 int fd
= PREFIX(creat
)(pathname
, mode
);
1042 open_files
|= (1<<fd
);
1046 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */