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],
164 screen_helper_setfont
,
171 lcd_mono_bitmap_part
,
184 lcd_bitmap_transparent_part
,
185 lcd_bitmap_transparent
,
188 #if defined(TOSHIBA_GIGABEAT_F) || defined(SANSA_E200) || defined(SANSA_C200) \
189 || defined(IRIVER_H10) || defined(COWON_D2) || defined(PHILIPS_HDD1630) \
190 || defined(SANSA_FUZE) || defined(SANSA_E200V2) || defined(SANSA_FUZEV2) \
191 || defined(TOSHIBA_GIGABEAT_S) || defined(PHILIPS_SA9200)
194 #endif /* MEMORYSIZE > 2 */
195 #elif (LCD_DEPTH < 4) && (CONFIG_PLATFORM & PLATFORM_NATIVE)
198 #endif /* LCD_DEPTH */
199 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
201 lcd_pal256_update_pal
,
204 lcd_puts_scroll_style
,
205 #ifdef HAVE_LCD_INVERT
206 lcd_set_invert_display
,
207 #endif /* HAVE_LCD_INVERT */
208 #if defined(HAVE_LCD_MODES)
211 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
215 #ifdef HAVE_LCD_BITMAP
226 #endif /* HAVE_LCD_BITMAP */
231 backlight_set_timeout
,
232 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
233 backlight_set_brightness
,
234 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
237 backlight_set_timeout_plugged
,
243 #ifdef HAVE_REMOTE_LCD
245 lcd_remote_set_contrast
,
246 lcd_remote_clear_display
,
248 lcd_remote_puts_scroll
,
249 lcd_remote_stop_scroll
,
250 lcd_remote_set_drawmode
,
251 lcd_remote_get_drawmode
,
253 lcd_remote_getstringsize
,
254 lcd_remote_drawpixel
,
260 lcd_remote_mono_bitmap_part
,
261 lcd_remote_mono_bitmap
,
263 lcd_remote_puts_style
,
264 lcd_remote_puts_scroll_style
,
265 &lcd_remote_static_framebuffer
[0][0],
267 lcd_remote_update_rect
,
270 remote_backlight_off
,
271 remote_backlight_set_timeout
,
273 remote_backlight_set_timeout_plugged
,
275 #endif /* HAVE_REMOTE_LCD */
277 {&screens
[SCREEN_MAIN
], &screens
[SCREEN_REMOTE
]},
279 {&screens
[SCREEN_MAIN
]},
281 #if defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
282 lcd_remote_set_foreground
,
283 lcd_remote_get_foreground
,
284 lcd_remote_set_background
,
285 lcd_remote_get_background
,
286 lcd_remote_bitmap_part
,
289 viewport_set_defaults
,
290 #ifdef HAVE_LCD_BITMAP
291 viewportmanager_theme_enable
,
292 viewportmanager_theme_undo
,
293 viewport_set_fullscreen
,
298 gui_synclist_set_nb_items
,
299 gui_synclist_set_icon_callback
,
300 gui_synclist_get_nb_items
,
301 gui_synclist_get_sel_pos
,
303 gui_synclist_select_item
,
304 gui_synclist_add_item
,
305 gui_synclist_del_item
,
306 gui_synclist_limit_scroll
,
307 gui_synclist_do_button
,
308 gui_synclist_set_title
,
310 simplelist_info_init
,
311 simplelist_show_list
,
317 #ifdef HAVE_BUTTON_DATA
323 #ifdef HAS_BUTTON_HOLD
326 #ifdef HAVE_TOUCHSCREEN
327 touchscreen_set_mode
,
328 touchscreen_get_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
,
433 commit_discard_idcache
,
448 #if CONFIG_CODEC == SWCODEC
449 queue_enable_queue_send
,
456 #ifdef USB_ENABLE_HID
462 __cyg_profile_func_enter
,
463 __cyg_profile_func_exit
,
469 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
470 /* special simulator hooks */
471 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
473 sim_lcd_ex_update_rect
,
477 /* strings and memory */
491 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
511 /* the buflib memory management library */
516 buflib_alloc_maximum
,
531 #ifdef AUDIOHW_HAVE_EQ
532 sound_enum_hw_eq_band_setting
,
534 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
539 #if CONFIG_CODEC != SWCODEC
543 #if CONFIG_CODEC == SWCODEC
544 &audio_master_sampr_list
[0],
553 pcm_get_bytes_waiting
,
559 #ifdef HAVE_RECORDING
565 pcm_calculate_rec_peaks
,
566 audio_set_recording_gain
,
567 #endif /* HAVE_RECORDING */
568 #if INPUT_SRC_CAPS != 0
569 audio_set_output_source
,
570 audio_set_input_source
,
572 dsp_set_crossfeed_type
,
575 #ifdef HAVE_PITCHCONTROL
582 mixer_channel_status
,
583 mixer_channel_get_buffer
,
584 mixer_channel_calculate_peaks
,
585 mixer_channel_play_data
,
586 mixer_channel_play_pause
,
588 mixer_channel_set_amplitude
,
589 mixer_channel_get_bytes_waiting
,
590 mixer_channel_set_buffer_hook
,
594 #endif /* CONFIG_CODEC == SWCODEC */
595 /* playback control */
598 playlist_resume_track
,
602 playlist_remove_all_tracks
,
604 playlist_insert_track
,
605 playlist_insert_directory
,
617 audio_flush_and_reload_tracks
,
619 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
620 mpeg_get_last_header
,
622 #if ((CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
623 (CONFIG_CODEC == SWCODEC)) && defined (HAVE_PITCHCONTROL)
627 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
628 /* MAS communication */
633 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
640 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
646 gui_syncstatusbar_draw
,
656 #ifdef HAVE_LCD_COLOR
660 /* action handling */
663 #ifdef HAVE_TOUCHSCREEN
664 action_get_touchscreen_press
,
675 # if CONFIG_CHARGING >= CHARGING_MONITOR
679 #ifdef HAVE_USB_POWER
684 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
697 plugin_get_audio_buffer
,
699 plugin_get_current_filename
,
700 #if defined(DEBUG) || defined(SIMULATOR)
703 #ifdef ROCKBOX_HAS_LOGF
709 #if CONFIG_CODEC == SWCODEC
710 codec_thread_do_callback
,
717 round_value_to_list32
,
718 #endif /* CONFIG_CODEC == SWCODEC */
724 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
725 peak_meter_scale_value
,
726 peak_meter_set_use_dbfs
,
727 peak_meter_get_use_dbfs
,
729 #ifdef HAVE_LCD_BITMAP
736 screen_dump_set_hook
,
745 #ifdef HAVE_WHEEL_POSITION
750 #ifdef IRIVER_H100_SERIES
751 /* Routines for the iriver_flash -plugin. */
752 detect_original_firmware
,
753 detect_flashed_ramimage
,
754 detect_flashed_romimage
,
757 #if (CONFIG_CODEC == SWCODEC)
775 tagcache_search_set_uniqbuf
,
776 tagcache_search_add_filter
,
779 tagcache_search_finish
,
780 tagcache_get_numeric
,
781 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
787 search_albumart_files
,
790 #ifdef HAVE_SEMAPHORE_OBJECTS
798 /* new stuff at the end, sort into place next time
799 the API gets incompatible */
803 int plugin_load(const char* plugin
, const void* parameter
)
805 struct plugin_header
*p_hdr
;
806 struct lc_header
*hdr
;
808 if (current_plugin_handle
&& pfn_tsr_exit
)
809 { /* if we have a resident old plugin and a callback */
810 if (pfn_tsr_exit(!strcmp(current_plugin
, plugin
)) == false )
812 /* not allowing another plugin to load */
815 lc_close(current_plugin_handle
);
816 current_plugin_handle
= pfn_tsr_exit
= NULL
;
819 splash(0, ID2P(LANG_WAIT
));
820 strcpy(current_plugin
, plugin
);
822 current_plugin_handle
= lc_open(plugin
, pluginbuf
, PLUGIN_BUFFER_SIZE
);
823 if (current_plugin_handle
== NULL
) {
824 splashf(HZ
*2, str(LANG_PLUGIN_CANT_OPEN
), plugin
);
828 p_hdr
= lc_get_header(current_plugin_handle
);
830 hdr
= p_hdr
? &p_hdr
->lc_hdr
: NULL
;
834 || hdr
->magic
!= PLUGIN_MAGIC
835 || hdr
->target_id
!= TARGET_ID
836 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
837 || hdr
->load_addr
!= pluginbuf
838 || hdr
->end_addr
> pluginbuf
+ PLUGIN_BUFFER_SIZE
842 lc_close(current_plugin_handle
);
843 splash(HZ
*2, str(LANG_PLUGIN_WRONG_MODEL
));
846 if (hdr
->api_version
> PLUGIN_API_VERSION
847 || hdr
->api_version
< PLUGIN_MIN_API_VERSION
)
849 lc_close(current_plugin_handle
);
850 splash(HZ
*2, str(LANG_PLUGIN_WRONG_VERSION
));
853 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
854 plugin_size
= hdr
->end_addr
- pluginbuf
;
859 *(p_hdr
->api
) = &rockbox_api
;
864 #ifdef HAVE_REMOTE_LCD
865 lcd_remote_clear_display();
868 push_current_activity(ACTIVITY_PLUGIN
);
869 /* some plugins assume the entry cache doesn't move and save pointers to it
870 * they should be fixed properly instead of this lock */
871 tree_lock_cache(tree_get_context());
874 viewportmanager_theme_enable(i
, false, NULL
);
876 #ifdef HAVE_TOUCHSCREEN
877 touchscreen_set_mode(TOUCHSCREEN_BUTTON
);
880 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
884 int rc
= p_hdr
->entry_point(parameter
);
886 tree_unlock_cache(tree_get_context());
887 pop_current_activity();
890 { /* close handle if plugin is no tsr one */
891 lc_close(current_plugin_handle
);
892 current_plugin_handle
= NULL
;
895 /* Go back to the global setting in case the plugin changed it */
896 #ifdef HAVE_TOUCHSCREEN
897 touchscreen_set_mode(global_settings
.touch_mode
);
900 #ifdef HAVE_LCD_BITMAP
901 screen_helper_setfont(FONT_UI
);
903 #ifdef HAVE_LCD_COLOR
904 lcd_set_drawinfo(DRMODE_SOLID
, global_settings
.fg_color
,
905 global_settings
.bg_color
);
907 lcd_set_drawinfo(DRMODE_SOLID
, LCD_DEFAULT_FG
, LCD_DEFAULT_BG
);
909 #else /* LCD_DEPTH == 1 */
910 lcd_set_drawmode(DRMODE_SOLID
);
911 #endif /* LCD_DEPTH */
912 #endif /* HAVE_LCD_BITMAP */
915 #ifdef HAVE_REMOTE_LCD
916 #if LCD_REMOTE_DEPTH > 1
917 lcd_remote_set_drawinfo(DRMODE_SOLID
, LCD_REMOTE_DEFAULT_FG
,
918 LCD_REMOTE_DEFAULT_BG
);
920 lcd_remote_set_drawmode(DRMODE_SOLID
);
925 #ifdef HAVE_REMOTE_LCD
926 lcd_remote_clear_display();
930 viewportmanager_theme_undo(i
, true);
932 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
933 if(open_files
!= 0 && !current_plugin_handle
)
936 logf("Plugin '%s' leaks file handles", plugin
);
938 static const char *lines
[] =
939 { ID2P(LANG_PLUGIN_ERROR
),
940 "#leak-file-handles" };
941 static const struct text_message message
={ lines
, 2 };
942 button_clear_queue(); /* Empty the keyboard buffer */
943 gui_syncyesno_run(&message
, NULL
, NULL
);
945 for(fd
=0; fd
< MAX_OPEN_FILES
; fd
++)
946 if(open_files
& (1<<fd
))
951 if (rc
== PLUGIN_ERROR
)
952 splash(HZ
*2, str(LANG_PLUGIN_ERROR
));
957 /* Returns a pointer to the portion of the plugin buffer that is not already
958 being used. If no plugin is loaded, returns the entire plugin buffer */
959 void* plugin_get_buffer(size_t *buffer_size
)
963 if (current_plugin_handle
)
965 if (plugin_size
>= PLUGIN_BUFFER_SIZE
)
968 *buffer_size
= PLUGIN_BUFFER_SIZE
-plugin_size
;
969 buffer_pos
= plugin_size
;
973 *buffer_size
= PLUGIN_BUFFER_SIZE
;
977 return &pluginbuf
[buffer_pos
];
980 /* Returns a pointer to the mp3 buffer.
981 Playback gets stopped, to avoid conflicts.
982 Talk buffer is stolen as well.
984 void* plugin_get_audio_buffer(size_t *buffer_size
)
987 return audio_get_buffer(true, buffer_size
);
990 /* The plugin wants to stay resident after leaving its main function, e.g.
991 runs from timer or own thread. The callback is registered to later
992 instruct it to free its resources before a new plugin gets loaded. */
993 void plugin_tsr(bool (*exit_callback
)(bool))
995 pfn_tsr_exit
= exit_callback
; /* remember the callback for later */
998 char *plugin_get_current_filename(void)
1000 return current_plugin
;
1003 static int open_wrapper(const char* pathname
, int flags
, ...)
1005 /* we don't have an 'open' function. it's a define. and we need
1006 * the real file_open, hence PREFIX() doesn't work here */
1008 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
1009 if (flags
& O_CREAT
)
1012 va_start(ap
, flags
);
1013 fd
= open(pathname
, flags
, va_arg(ap
, unsigned int));
1017 fd
= open(pathname
, flags
);
1019 fd
= file_open(pathname
,flags
);
1022 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1024 open_files
|= 1<<fd
;
1029 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1030 static int close_wrapper(int fd
)
1032 if((~open_files
) & (1<<fd
))
1034 logf("double close from plugin");
1037 open_files
&= (~(1<<fd
));
1039 return PREFIX(close
)(fd
);
1042 static int creat_wrapper(const char *pathname
, mode_t mode
)
1046 int fd
= PREFIX(creat
)(pathname
, mode
);
1049 open_files
|= (1<<fd
);
1053 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */