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],
177 screen_helper_setfont
,
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
239 #endif /* HAVE_LCD_BITMAP */
244 backlight_set_timeout
,
245 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
246 backlight_set_brightness
,
247 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
250 backlight_set_timeout_plugged
,
256 #ifdef HAVE_REMOTE_LCD
258 lcd_remote_set_contrast
,
259 lcd_remote_clear_display
,
261 lcd_remote_puts_scroll
,
262 lcd_remote_stop_scroll
,
263 lcd_remote_set_drawmode
,
264 lcd_remote_get_drawmode
,
266 lcd_remote_getstringsize
,
267 lcd_remote_drawpixel
,
273 lcd_remote_mono_bitmap_part
,
274 lcd_remote_mono_bitmap
,
276 lcd_remote_puts_style
,
277 lcd_remote_puts_scroll_style
,
278 &lcd_remote_framebuffer
[0][0],
280 lcd_remote_update_rect
,
283 remote_backlight_off
,
284 remote_backlight_set_timeout
,
286 remote_backlight_set_timeout_plugged
,
288 #endif /* HAVE_REMOTE_LCD */
290 {&screens
[SCREEN_MAIN
], &screens
[SCREEN_REMOTE
]},
292 {&screens
[SCREEN_MAIN
]},
294 #if defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
295 lcd_remote_set_foreground
,
296 lcd_remote_get_foreground
,
297 lcd_remote_set_background
,
298 lcd_remote_get_background
,
299 lcd_remote_bitmap_part
,
302 viewport_set_defaults
,
303 #ifdef HAVE_LCD_BITMAP
304 viewportmanager_theme_enable
,
305 viewportmanager_theme_undo
,
310 gui_synclist_set_nb_items
,
311 gui_synclist_set_icon_callback
,
312 gui_synclist_get_nb_items
,
313 gui_synclist_get_sel_pos
,
315 gui_synclist_select_item
,
316 gui_synclist_add_item
,
317 gui_synclist_del_item
,
318 gui_synclist_limit_scroll
,
319 gui_synclist_do_button
,
320 gui_synclist_set_title
,
322 simplelist_info_init
,
323 simplelist_show_list
,
329 #ifdef HAVE_BUTTON_DATA
335 #ifdef HAS_BUTTON_HOLD
338 #ifdef HAVE_TOUCHSCREEN
339 touchscreen_set_mode
,
342 #ifdef HAVE_BUTTON_LIGHT
343 buttonlight_set_timeout
,
346 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
347 buttonlight_set_brightness
,
348 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
349 #endif /* HAVE_BUTTON_LIGHT */
353 (open_func
)open_wrapper
,
354 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
359 (read_func
)PREFIX(read
),
361 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
362 (creat_func
)creat_wrapper
,
366 (write_func
)PREFIX(write
),
377 #if USING_STORAGE_CALLBACK
378 register_storage_idle_func
,
379 unregister_storage_idle_func
,
380 #endif /* USING_STORAGE_CALLBACK */
382 create_numbered_filename
,
389 (opendir_func
)PREFIX(opendir
),
390 (closedir_func
)PREFIX(closedir
),
391 (readdir_func
)PREFIX(readdir
),
402 #if defined(CPU_ARM) && CONFIG_PLATFORM & PLATFORM_NATIVE
408 default_event_handler
,
409 default_event_handler_ex
,
414 #if (CONFIG_CODEC == SWCODEC)
416 #ifdef HAVE_PRIORITY_SCHEDULING
424 reset_poweroff_timer
,
425 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
429 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
430 #ifdef CPU_BOOST_LOGGING
435 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
436 #endif /* PLATFORM_NATIVE */
437 #ifdef HAVE_SCHEDULER_BOOSTCTRL
458 #if CONFIG_CODEC == SWCODEC
459 queue_enable_queue_send
,
466 #ifdef USB_ENABLE_HID
472 __cyg_profile_func_enter
,
473 __cyg_profile_func_exit
,
479 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
480 /* special simulator hooks */
481 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
483 sim_lcd_ex_update_rect
,
487 /* strings and memory */
501 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
521 /* the buflib memory management library */
526 buflib_alloc_maximum
,
541 #ifdef AUDIOHW_HAVE_EQ
542 sound_enum_hw_eq_band_setting
,
544 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
549 #if CONFIG_CODEC != SWCODEC
553 #if CONFIG_CODEC == SWCODEC
554 &audio_master_sampr_list
[0],
563 pcm_get_bytes_waiting
,
569 #ifdef HAVE_RECORDING
575 pcm_calculate_rec_peaks
,
576 audio_set_recording_gain
,
577 #endif /* HAVE_RECORDING */
578 #if INPUT_SRC_CAPS != 0
579 audio_set_output_source
,
580 audio_set_input_source
,
590 mixer_channel_status
,
591 mixer_channel_get_buffer
,
592 mixer_channel_calculate_peaks
,
593 mixer_channel_play_data
,
594 mixer_channel_play_pause
,
596 mixer_channel_set_amplitude
,
597 mixer_channel_get_bytes_waiting
,
602 /* playback control */
608 playlist_remove_all_tracks
,
610 playlist_insert_track
,
611 playlist_insert_directory
,
623 audio_flush_and_reload_tracks
,
625 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
626 mpeg_get_last_header
,
628 #if ((CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
629 (CONFIG_CODEC == SWCODEC)) && defined (HAVE_PITCHSCREEN)
633 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
634 /* MAS communication */
639 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
646 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
652 gui_syncstatusbar_draw
,
662 #ifdef HAVE_LCD_COLOR
666 /* action handling */
669 #ifdef HAVE_TOUCHSCREEN
670 action_get_touchscreen_press
,
678 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
683 # if CONFIG_CHARGING >= CHARGING_MONITOR
687 #ifdef HAVE_USB_POWER
692 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
705 plugin_get_audio_buffer
,
707 plugin_get_current_filename
,
708 #if defined(DEBUG) || defined(SIMULATOR)
711 #ifdef ROCKBOX_HAS_LOGF
717 #if CONFIG_CODEC == SWCODEC
718 codec_thread_do_callback
,
725 round_value_to_list32
,
726 #endif /* CONFIG_CODEC == SWCODEC */
732 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
733 peak_meter_scale_value
,
734 peak_meter_set_use_dbfs
,
735 peak_meter_get_use_dbfs
,
737 #ifdef HAVE_LCD_BITMAP
744 screen_dump_set_hook
,
753 #ifdef HAVE_WHEEL_POSITION
758 #ifdef IRIVER_H100_SERIES
759 /* Routines for the iriver_flash -plugin. */
760 detect_original_firmware
,
761 detect_flashed_ramimage
,
762 detect_flashed_romimage
,
765 #if (CONFIG_CODEC == SWCODEC)
783 tagcache_search_set_uniqbuf
,
784 tagcache_search_add_filter
,
787 tagcache_search_finish
,
788 tagcache_get_numeric
,
789 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
795 search_albumart_files
,
798 #ifdef HAVE_SEMAPHORE_OBJECTS
806 /* new stuff at the end, sort into place next time
807 the API gets incompatible */
810 int plugin_load(const char* plugin
, const void* parameter
)
812 struct plugin_header
*p_hdr
;
813 struct lc_header
*hdr
;
815 if (current_plugin_handle
&& pfn_tsr_exit
)
816 { /* if we have a resident old plugin and a callback */
817 if (pfn_tsr_exit(!strcmp(current_plugin
, plugin
)) == false )
819 /* not allowing another plugin to load */
822 lc_close(current_plugin_handle
);
823 current_plugin_handle
= pfn_tsr_exit
= NULL
;
826 splash(0, ID2P(LANG_WAIT
));
827 strcpy(current_plugin
, plugin
);
829 current_plugin_handle
= lc_open(plugin
, pluginbuf
, PLUGIN_BUFFER_SIZE
);
830 if (current_plugin_handle
== NULL
) {
831 splashf(HZ
*2, str(LANG_PLUGIN_CANT_OPEN
), plugin
);
835 p_hdr
= lc_get_header(current_plugin_handle
);
837 hdr
= p_hdr
? &p_hdr
->lc_hdr
: NULL
;
841 || hdr
->magic
!= PLUGIN_MAGIC
842 || hdr
->target_id
!= TARGET_ID
843 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
844 || hdr
->load_addr
!= pluginbuf
845 || hdr
->end_addr
> pluginbuf
+ PLUGIN_BUFFER_SIZE
849 lc_close(current_plugin_handle
);
850 splash(HZ
*2, str(LANG_PLUGIN_WRONG_MODEL
));
853 if (hdr
->api_version
> PLUGIN_API_VERSION
854 || hdr
->api_version
< PLUGIN_MIN_API_VERSION
)
856 lc_close(current_plugin_handle
);
857 splash(HZ
*2, str(LANG_PLUGIN_WRONG_VERSION
));
860 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
861 plugin_size
= hdr
->end_addr
- pluginbuf
;
866 *(p_hdr
->api
) = &rockbox_api
;
871 #ifdef HAVE_REMOTE_LCD
872 lcd_remote_clear_display();
875 push_current_activity(ACTIVITY_PLUGIN
);
876 /* some plugins assume the entry cache doesn't move and save pointers to it
877 * they should be fixed properly instead of this lock */
878 tree_lock_cache(tree_get_context());
881 viewportmanager_theme_enable(i
, false, NULL
);
883 #ifdef HAVE_TOUCHSCREEN
884 touchscreen_set_mode(TOUCHSCREEN_BUTTON
);
887 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
891 int rc
= p_hdr
->entry_point(parameter
);
893 tree_unlock_cache(tree_get_context());
894 pop_current_activity();
897 { /* close handle if plugin is no tsr one */
898 lc_close(current_plugin_handle
);
899 current_plugin_handle
= NULL
;
902 /* Go back to the global setting in case the plugin changed it */
903 #ifdef HAVE_TOUCHSCREEN
904 touchscreen_set_mode(global_settings
.touch_mode
);
907 #ifdef HAVE_LCD_BITMAP
908 screen_helper_setfont(FONT_UI
);
910 #ifdef HAVE_LCD_COLOR
911 lcd_set_drawinfo(DRMODE_SOLID
, global_settings
.fg_color
,
912 global_settings
.bg_color
);
914 lcd_set_drawinfo(DRMODE_SOLID
, LCD_DEFAULT_FG
, LCD_DEFAULT_BG
);
916 #else /* LCD_DEPTH == 1 */
917 lcd_set_drawmode(DRMODE_SOLID
);
918 #endif /* LCD_DEPTH */
919 #endif /* HAVE_LCD_BITMAP */
922 #ifdef HAVE_REMOTE_LCD
923 #if LCD_REMOTE_DEPTH > 1
924 lcd_remote_set_drawinfo(DRMODE_SOLID
, LCD_REMOTE_DEFAULT_FG
,
925 LCD_REMOTE_DEFAULT_BG
);
927 lcd_remote_set_drawmode(DRMODE_SOLID
);
932 #ifdef HAVE_REMOTE_LCD
933 lcd_remote_clear_display();
937 viewportmanager_theme_undo(i
, true);
939 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
940 if(open_files
!= 0 && !current_plugin_handle
)
943 logf("Plugin '%s' leaks file handles", plugin
);
945 static const char *lines
[] =
946 { ID2P(LANG_PLUGIN_ERROR
),
947 "#leak-file-handles" };
948 static const struct text_message message
={ lines
, 2 };
949 button_clear_queue(); /* Empty the keyboard buffer */
950 gui_syncyesno_run(&message
, NULL
, NULL
);
952 for(fd
=0; fd
< MAX_OPEN_FILES
; fd
++)
953 if(open_files
& (1<<fd
))
958 if (rc
== PLUGIN_ERROR
)
959 splash(HZ
*2, str(LANG_PLUGIN_ERROR
));
964 /* Returns a pointer to the portion of the plugin buffer that is not already
965 being used. If no plugin is loaded, returns the entire plugin buffer */
966 void* plugin_get_buffer(size_t *buffer_size
)
970 if (current_plugin_handle
)
972 if (plugin_size
>= PLUGIN_BUFFER_SIZE
)
975 *buffer_size
= PLUGIN_BUFFER_SIZE
-plugin_size
;
976 buffer_pos
= plugin_size
;
980 *buffer_size
= PLUGIN_BUFFER_SIZE
;
984 return &pluginbuf
[buffer_pos
];
987 /* Returns a pointer to the mp3 buffer.
988 Playback gets stopped, to avoid conflicts.
989 Talk buffer is stolen as well.
991 void* plugin_get_audio_buffer(size_t *buffer_size
)
994 return audio_get_buffer(true, buffer_size
);
997 /* The plugin wants to stay resident after leaving its main function, e.g.
998 runs from timer or own thread. The callback is registered to later
999 instruct it to free its resources before a new plugin gets loaded. */
1000 void plugin_tsr(bool (*exit_callback
)(bool))
1002 pfn_tsr_exit
= exit_callback
; /* remember the callback for later */
1005 char *plugin_get_current_filename(void)
1007 return current_plugin
;
1010 static int open_wrapper(const char* pathname
, int flags
, ...)
1012 /* we don't have an 'open' function. it's a define. and we need
1013 * the real file_open, hence PREFIX() doesn't work here */
1015 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
1016 if (flags
& O_CREAT
)
1019 va_start(ap
, flags
);
1020 fd
= open(pathname
, flags
, va_arg(ap
, unsigned int));
1024 fd
= open(pathname
, flags
);
1026 fd
= file_open(pathname
,flags
);
1029 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1031 open_files
|= 1<<fd
;
1036 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1037 static int close_wrapper(int fd
)
1039 if((~open_files
) & (1<<fd
))
1041 logf("double close from plugin");
1044 open_files
&= (~(1<<fd
));
1046 return PREFIX(close
)(fd
);
1049 static int creat_wrapper(const char *pathname
, mode_t mode
)
1051 int fd
= PREFIX(creat
)(pathname
, mode
);
1054 open_files
|= (1<<fd
);
1058 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */