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
);
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
,
576 mixer_channel_status
,
577 mixer_channel_get_buffer
,
578 mixer_channel_calculate_peaks
,
579 mixer_channel_play_data
,
580 mixer_channel_play_pause
,
582 mixer_channel_set_amplitude
,
583 mixer_channel_get_bytes_waiting
,
588 /* playback control */
594 playlist_remove_all_tracks
,
596 playlist_insert_track
,
597 playlist_insert_directory
,
609 audio_flush_and_reload_tracks
,
611 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
612 mpeg_get_last_header
,
614 #if ((CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
615 (CONFIG_CODEC == SWCODEC)) && defined (HAVE_PITCHSCREEN)
619 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
620 /* MAS communication */
625 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
632 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
638 gui_syncstatusbar_draw
,
648 #ifdef HAVE_LCD_COLOR
652 /* action handling */
655 #ifdef HAVE_TOUCHSCREEN
656 action_get_touchscreen_press
,
667 # if CONFIG_CHARGING >= CHARGING_MONITOR
671 #ifdef HAVE_USB_POWER
676 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
689 plugin_get_audio_buffer
,
691 plugin_get_current_filename
,
692 #if defined(DEBUG) || defined(SIMULATOR)
695 #ifdef ROCKBOX_HAS_LOGF
701 #if CONFIG_CODEC == SWCODEC
702 codec_thread_do_callback
,
709 round_value_to_list32
,
710 #endif /* CONFIG_CODEC == SWCODEC */
716 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
717 peak_meter_scale_value
,
718 peak_meter_set_use_dbfs
,
719 peak_meter_get_use_dbfs
,
721 #ifdef HAVE_LCD_BITMAP
728 screen_dump_set_hook
,
737 #ifdef HAVE_WHEEL_POSITION
742 #ifdef IRIVER_H100_SERIES
743 /* Routines for the iriver_flash -plugin. */
744 detect_original_firmware
,
745 detect_flashed_ramimage
,
746 detect_flashed_romimage
,
749 #if (CONFIG_CODEC == SWCODEC)
767 tagcache_search_set_uniqbuf
,
768 tagcache_search_add_filter
,
771 tagcache_search_finish
,
772 tagcache_get_numeric
,
773 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
779 search_albumart_files
,
782 #ifdef HAVE_SEMAPHORE_OBJECTS
790 /* new stuff at the end, sort into place next time
791 the API gets incompatible */
794 int plugin_load(const char* plugin
, const void* parameter
)
796 struct plugin_header
*p_hdr
;
797 struct lc_header
*hdr
;
799 if (current_plugin_handle
&& pfn_tsr_exit
)
800 { /* if we have a resident old plugin and a callback */
801 if (pfn_tsr_exit(!strcmp(current_plugin
, plugin
)) == false )
803 /* not allowing another plugin to load */
806 lc_close(current_plugin_handle
);
807 current_plugin_handle
= pfn_tsr_exit
= NULL
;
810 splash(0, ID2P(LANG_WAIT
));
811 strcpy(current_plugin
, plugin
);
813 current_plugin_handle
= lc_open(plugin
, pluginbuf
, PLUGIN_BUFFER_SIZE
);
814 if (current_plugin_handle
== NULL
) {
815 splashf(HZ
*2, str(LANG_PLUGIN_CANT_OPEN
), plugin
);
819 p_hdr
= lc_get_header(current_plugin_handle
);
821 hdr
= p_hdr
? &p_hdr
->lc_hdr
: NULL
;
825 || hdr
->magic
!= PLUGIN_MAGIC
826 || hdr
->target_id
!= TARGET_ID
827 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
828 || hdr
->load_addr
!= pluginbuf
829 || hdr
->end_addr
> pluginbuf
+ PLUGIN_BUFFER_SIZE
833 lc_close(current_plugin_handle
);
834 splash(HZ
*2, str(LANG_PLUGIN_WRONG_MODEL
));
837 if (hdr
->api_version
> PLUGIN_API_VERSION
838 || hdr
->api_version
< PLUGIN_MIN_API_VERSION
)
840 lc_close(current_plugin_handle
);
841 splash(HZ
*2, str(LANG_PLUGIN_WRONG_VERSION
));
844 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
845 plugin_size
= hdr
->end_addr
- pluginbuf
;
850 *(p_hdr
->api
) = &rockbox_api
;
855 #ifdef HAVE_REMOTE_LCD
856 lcd_remote_clear_display();
859 push_current_activity(ACTIVITY_PLUGIN
);
860 /* some plugins assume the entry cache doesn't move and save pointers to it
861 * they should be fixed properly instead of this lock */
862 tree_lock_cache(tree_get_context());
865 viewportmanager_theme_enable(i
, false, NULL
);
867 #ifdef HAVE_TOUCHSCREEN
868 touchscreen_set_mode(TOUCHSCREEN_BUTTON
);
871 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
875 int rc
= p_hdr
->entry_point(parameter
);
877 tree_unlock_cache(tree_get_context());
878 pop_current_activity();
881 { /* close handle if plugin is no tsr one */
882 lc_close(current_plugin_handle
);
883 current_plugin_handle
= NULL
;
886 /* Go back to the global setting in case the plugin changed it */
887 #ifdef HAVE_TOUCHSCREEN
888 touchscreen_set_mode(global_settings
.touch_mode
);
891 #ifdef HAVE_LCD_BITMAP
892 screen_helper_setfont(FONT_UI
);
894 #ifdef HAVE_LCD_COLOR
895 lcd_set_drawinfo(DRMODE_SOLID
, global_settings
.fg_color
,
896 global_settings
.bg_color
);
898 lcd_set_drawinfo(DRMODE_SOLID
, LCD_DEFAULT_FG
, LCD_DEFAULT_BG
);
900 #else /* LCD_DEPTH == 1 */
901 lcd_set_drawmode(DRMODE_SOLID
);
902 #endif /* LCD_DEPTH */
903 #endif /* HAVE_LCD_BITMAP */
906 #ifdef HAVE_REMOTE_LCD
907 #if LCD_REMOTE_DEPTH > 1
908 lcd_remote_set_drawinfo(DRMODE_SOLID
, LCD_REMOTE_DEFAULT_FG
,
909 LCD_REMOTE_DEFAULT_BG
);
911 lcd_remote_set_drawmode(DRMODE_SOLID
);
916 #ifdef HAVE_REMOTE_LCD
917 lcd_remote_clear_display();
921 viewportmanager_theme_undo(i
, true);
923 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
924 if(open_files
!= 0 && !current_plugin_handle
)
927 logf("Plugin '%s' leaks file handles", plugin
);
929 static const char *lines
[] =
930 { ID2P(LANG_PLUGIN_ERROR
),
931 "#leak-file-handles" };
932 static const struct text_message message
={ lines
, 2 };
933 button_clear_queue(); /* Empty the keyboard buffer */
934 gui_syncyesno_run(&message
, NULL
, NULL
);
936 for(fd
=0; fd
< MAX_OPEN_FILES
; fd
++)
937 if(open_files
& (1<<fd
))
942 if (rc
== PLUGIN_ERROR
)
943 splash(HZ
*2, str(LANG_PLUGIN_ERROR
));
948 /* Returns a pointer to the portion of the plugin buffer that is not already
949 being used. If no plugin is loaded, returns the entire plugin buffer */
950 void* plugin_get_buffer(size_t *buffer_size
)
954 if (current_plugin_handle
)
956 if (plugin_size
>= PLUGIN_BUFFER_SIZE
)
959 *buffer_size
= PLUGIN_BUFFER_SIZE
-plugin_size
;
960 buffer_pos
= plugin_size
;
964 *buffer_size
= PLUGIN_BUFFER_SIZE
;
968 return &pluginbuf
[buffer_pos
];
971 /* Returns a pointer to the mp3 buffer.
972 Playback gets stopped, to avoid conflicts.
973 Talk buffer is stolen as well.
975 void* plugin_get_audio_buffer(size_t *buffer_size
)
978 return audio_get_buffer(true, buffer_size
);
981 /* The plugin wants to stay resident after leaving its main function, e.g.
982 runs from timer or own thread. The callback is registered to later
983 instruct it to free its resources before a new plugin gets loaded. */
984 void plugin_tsr(bool (*exit_callback
)(bool))
986 pfn_tsr_exit
= exit_callback
; /* remember the callback for later */
989 char *plugin_get_current_filename(void)
991 return current_plugin
;
994 static int open_wrapper(const char* pathname
, int flags
, ...)
996 /* we don't have an 'open' function. it's a define. and we need
997 * the real file_open, hence PREFIX() doesn't work here */
999 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
1000 if (flags
& O_CREAT
)
1003 va_start(ap
, flags
);
1004 fd
= open(pathname
, flags
, va_arg(ap
, unsigned int));
1008 fd
= open(pathname
, flags
);
1010 fd
= file_open(pathname
,flags
);
1013 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1015 open_files
|= 1<<fd
;
1020 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1021 static int close_wrapper(int fd
)
1023 if((~open_files
) & (1<<fd
))
1025 logf("double close from plugin");
1028 open_files
&= (~(1<<fd
));
1030 return PREFIX(close
)(fd
);
1033 static int creat_wrapper(const char *pathname
, mode_t mode
)
1035 int fd
= PREFIX(creat
)(pathname
, mode
);
1038 open_files
|= (1<<fd
);
1042 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */