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
);
97 static off_t
app_filesize(int fd
)
103 #if defined(HAVE_PLUGIN_CHECK_OPEN_CLOSE) && (MAX_OPEN_FILES>32)
104 #warning "MAX_OPEN_FILES>32, disabling plugin file open/close checking"
105 #undef HAVE_PLUGIN_CHECK_OPEN_CLOSE
108 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
109 static unsigned int open_files
;
112 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
113 static unsigned char pluginbuf
[PLUGIN_BUFFER_SIZE
];
114 void sim_lcd_ex_init(unsigned long (*getpixel
)(int, int));
115 void sim_lcd_ex_update_rect(int x
, int y
, int width
, int height
);
117 extern unsigned char pluginbuf
[];
121 /* for actual plugins only, not for codecs */
122 static int plugin_size
= 0;
123 static bool (*pfn_tsr_exit
)(bool reenter
) = NULL
; /* TSR exit callback */
124 static char current_plugin
[MAX_PATH
];
125 /* NULL if no plugin is loaded, otherwise the handle that lc_open() returned */
126 static void *current_plugin_handle
;
128 char *plugin_get_current_filename(void);
130 /* Some wrappers used to monitor open and close and detect leaks*/
131 static int open_wrapper(const char* pathname
, int flags
, ...);
132 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
133 static int close_wrapper(int fd
);
134 static int creat_wrapper(const char *pathname
, mode_t mode
);
137 static const struct plugin_api rockbox_api
= {
140 #ifdef HAVE_LCD_CONTRAST
152 #ifdef HAVE_LCD_CHARCELLS
154 lcd_get_locked_pattern
,
162 &lcd_framebuffer
[0][0],
166 screen_helper_setfont
,
173 lcd_mono_bitmap_part
,
186 lcd_bitmap_transparent_part
,
187 lcd_bitmap_transparent
,
190 #if defined(TOSHIBA_GIGABEAT_F) || defined(SANSA_E200) || defined(SANSA_C200) \
191 || defined(IRIVER_H10) || defined(COWON_D2) || defined(PHILIPS_HDD1630) \
192 || defined(SANSA_FUZE) || defined(SANSA_E200V2) || defined(SANSA_FUZEV2) \
193 || defined(TOSHIBA_GIGABEAT_S) || defined(PHILIPS_SA9200)
196 #endif /* MEMORYSIZE > 2 */
197 #elif (LCD_DEPTH < 4) && (CONFIG_PLATFORM & PLATFORM_NATIVE)
200 #endif /* LCD_DEPTH */
201 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
203 lcd_pal256_update_pal
,
206 lcd_puts_scroll_style
,
207 #ifdef HAVE_LCD_INVERT
208 lcd_set_invert_display
,
209 #endif /* HAVE_LCD_INVERT */
210 #if defined(HAVE_LCD_MODES)
213 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
217 #ifdef HAVE_LCD_BITMAP
228 #endif /* HAVE_LCD_BITMAP */
233 backlight_set_timeout
,
234 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
235 backlight_set_brightness
,
236 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
239 backlight_set_timeout_plugged
,
245 #ifdef HAVE_REMOTE_LCD
247 lcd_remote_set_contrast
,
248 lcd_remote_clear_display
,
250 lcd_remote_puts_scroll
,
251 lcd_remote_stop_scroll
,
252 lcd_remote_set_drawmode
,
253 lcd_remote_get_drawmode
,
255 lcd_remote_getstringsize
,
256 lcd_remote_drawpixel
,
262 lcd_remote_mono_bitmap_part
,
263 lcd_remote_mono_bitmap
,
265 lcd_remote_puts_style
,
266 lcd_remote_puts_scroll_style
,
267 &lcd_remote_framebuffer
[0][0],
269 lcd_remote_update_rect
,
272 remote_backlight_off
,
273 remote_backlight_set_timeout
,
275 remote_backlight_set_timeout_plugged
,
277 #endif /* HAVE_REMOTE_LCD */
279 {&screens
[SCREEN_MAIN
], &screens
[SCREEN_REMOTE
]},
281 {&screens
[SCREEN_MAIN
]},
283 #if defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
284 lcd_remote_set_foreground
,
285 lcd_remote_get_foreground
,
286 lcd_remote_set_background
,
287 lcd_remote_get_background
,
288 lcd_remote_bitmap_part
,
291 viewport_set_defaults
,
292 #ifdef HAVE_LCD_BITMAP
293 viewportmanager_theme_enable
,
294 viewportmanager_theme_undo
,
299 gui_synclist_set_nb_items
,
300 gui_synclist_set_icon_callback
,
301 gui_synclist_get_nb_items
,
302 gui_synclist_get_sel_pos
,
304 gui_synclist_select_item
,
305 gui_synclist_add_item
,
306 gui_synclist_del_item
,
307 gui_synclist_limit_scroll
,
308 gui_synclist_do_button
,
309 gui_synclist_set_title
,
311 simplelist_info_init
,
312 simplelist_show_list
,
318 #ifdef HAVE_BUTTON_DATA
324 #ifdef HAS_BUTTON_HOLD
327 #ifdef HAVE_TOUCHSCREEN
328 touchscreen_set_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
,
447 #if CONFIG_CODEC == SWCODEC
448 queue_enable_queue_send
,
455 #ifdef USB_ENABLE_HID
461 __cyg_profile_func_enter
,
462 __cyg_profile_func_exit
,
468 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
469 /* special simulator hooks */
470 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
472 sim_lcd_ex_update_rect
,
476 /* strings and memory */
490 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
510 /* the buflib memory management library */
515 buflib_alloc_maximum
,
530 #ifdef AUDIOHW_HAVE_EQ
531 sound_enum_hw_eq_band_setting
,
533 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
538 #if CONFIG_CODEC != SWCODEC
542 #if CONFIG_CODEC == SWCODEC
543 &audio_master_sampr_list
[0],
552 pcm_get_bytes_waiting
,
558 #ifdef HAVE_RECORDING
564 pcm_calculate_rec_peaks
,
565 audio_set_recording_gain
,
566 #endif /* HAVE_RECORDING */
567 #if INPUT_SRC_CAPS != 0
568 audio_set_output_source
,
569 audio_set_input_source
,
579 mixer_channel_status
,
580 mixer_channel_get_buffer
,
581 mixer_channel_calculate_peaks
,
582 mixer_channel_play_data
,
583 mixer_channel_play_pause
,
585 mixer_channel_set_amplitude
,
586 mixer_channel_get_bytes_waiting
,
591 /* playback control */
597 playlist_remove_all_tracks
,
599 playlist_insert_track
,
600 playlist_insert_directory
,
612 audio_flush_and_reload_tracks
,
614 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
615 mpeg_get_last_header
,
617 #if ((CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
618 (CONFIG_CODEC == SWCODEC)) && defined (HAVE_PITCHSCREEN)
622 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
623 /* MAS communication */
628 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
635 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
641 gui_syncstatusbar_draw
,
651 #ifdef HAVE_LCD_COLOR
655 /* action handling */
658 #ifdef HAVE_TOUCHSCREEN
659 action_get_touchscreen_press
,
670 # if CONFIG_CHARGING >= CHARGING_MONITOR
674 #ifdef HAVE_USB_POWER
679 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
692 plugin_get_audio_buffer
,
694 plugin_get_current_filename
,
695 #if defined(DEBUG) || defined(SIMULATOR)
698 #ifdef ROCKBOX_HAS_LOGF
704 #if CONFIG_CODEC == SWCODEC
705 codec_thread_do_callback
,
712 round_value_to_list32
,
713 #endif /* CONFIG_CODEC == SWCODEC */
719 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
720 peak_meter_scale_value
,
721 peak_meter_set_use_dbfs
,
722 peak_meter_get_use_dbfs
,
724 #ifdef HAVE_LCD_BITMAP
731 screen_dump_set_hook
,
740 #ifdef HAVE_WHEEL_POSITION
745 #ifdef IRIVER_H100_SERIES
746 /* Routines for the iriver_flash -plugin. */
747 detect_original_firmware
,
748 detect_flashed_ramimage
,
749 detect_flashed_romimage
,
752 #if (CONFIG_CODEC == SWCODEC)
770 tagcache_search_set_uniqbuf
,
771 tagcache_search_add_filter
,
774 tagcache_search_finish
,
775 tagcache_get_numeric
,
776 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
782 search_albumart_files
,
785 #ifdef HAVE_SEMAPHORE_OBJECTS
793 /* new stuff at the end, sort into place next time
794 the API gets incompatible */
796 commit_discard_idcache
,
799 int plugin_load(const char* plugin
, const void* parameter
)
801 struct plugin_header
*p_hdr
;
802 struct lc_header
*hdr
;
804 if (current_plugin_handle
&& pfn_tsr_exit
)
805 { /* if we have a resident old plugin and a callback */
806 if (pfn_tsr_exit(!strcmp(current_plugin
, plugin
)) == false )
808 /* not allowing another plugin to load */
811 lc_close(current_plugin_handle
);
812 current_plugin_handle
= pfn_tsr_exit
= NULL
;
815 splash(0, ID2P(LANG_WAIT
));
816 strcpy(current_plugin
, plugin
);
818 current_plugin_handle
= lc_open(plugin
, pluginbuf
, PLUGIN_BUFFER_SIZE
);
819 if (current_plugin_handle
== NULL
) {
820 splashf(HZ
*2, str(LANG_PLUGIN_CANT_OPEN
), plugin
);
824 p_hdr
= lc_get_header(current_plugin_handle
);
826 hdr
= p_hdr
? &p_hdr
->lc_hdr
: NULL
;
830 || hdr
->magic
!= PLUGIN_MAGIC
831 || hdr
->target_id
!= TARGET_ID
832 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
833 || hdr
->load_addr
!= pluginbuf
834 || hdr
->end_addr
> pluginbuf
+ PLUGIN_BUFFER_SIZE
838 lc_close(current_plugin_handle
);
839 splash(HZ
*2, str(LANG_PLUGIN_WRONG_MODEL
));
842 if (hdr
->api_version
> PLUGIN_API_VERSION
843 || hdr
->api_version
< PLUGIN_MIN_API_VERSION
)
845 lc_close(current_plugin_handle
);
846 splash(HZ
*2, str(LANG_PLUGIN_WRONG_VERSION
));
849 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
850 plugin_size
= hdr
->end_addr
- pluginbuf
;
855 *(p_hdr
->api
) = &rockbox_api
;
860 #ifdef HAVE_REMOTE_LCD
861 lcd_remote_clear_display();
864 push_current_activity(ACTIVITY_PLUGIN
);
865 /* some plugins assume the entry cache doesn't move and save pointers to it
866 * they should be fixed properly instead of this lock */
867 tree_lock_cache(tree_get_context());
870 viewportmanager_theme_enable(i
, false, NULL
);
872 #ifdef HAVE_TOUCHSCREEN
873 touchscreen_set_mode(TOUCHSCREEN_BUTTON
);
876 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
880 int rc
= p_hdr
->entry_point(parameter
);
882 tree_unlock_cache(tree_get_context());
883 pop_current_activity();
886 { /* close handle if plugin is no tsr one */
887 lc_close(current_plugin_handle
);
888 current_plugin_handle
= NULL
;
891 /* Go back to the global setting in case the plugin changed it */
892 #ifdef HAVE_TOUCHSCREEN
893 touchscreen_set_mode(global_settings
.touch_mode
);
896 #ifdef HAVE_LCD_BITMAP
897 screen_helper_setfont(FONT_UI
);
899 #ifdef HAVE_LCD_COLOR
900 lcd_set_drawinfo(DRMODE_SOLID
, global_settings
.fg_color
,
901 global_settings
.bg_color
);
903 lcd_set_drawinfo(DRMODE_SOLID
, LCD_DEFAULT_FG
, LCD_DEFAULT_BG
);
905 #else /* LCD_DEPTH == 1 */
906 lcd_set_drawmode(DRMODE_SOLID
);
907 #endif /* LCD_DEPTH */
908 #endif /* HAVE_LCD_BITMAP */
911 #ifdef HAVE_REMOTE_LCD
912 #if LCD_REMOTE_DEPTH > 1
913 lcd_remote_set_drawinfo(DRMODE_SOLID
, LCD_REMOTE_DEFAULT_FG
,
914 LCD_REMOTE_DEFAULT_BG
);
916 lcd_remote_set_drawmode(DRMODE_SOLID
);
921 #ifdef HAVE_REMOTE_LCD
922 lcd_remote_clear_display();
926 viewportmanager_theme_undo(i
, true);
928 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
929 if(open_files
!= 0 && !current_plugin_handle
)
932 logf("Plugin '%s' leaks file handles", plugin
);
934 static const char *lines
[] =
935 { ID2P(LANG_PLUGIN_ERROR
),
936 "#leak-file-handles" };
937 static const struct text_message message
={ lines
, 2 };
938 button_clear_queue(); /* Empty the keyboard buffer */
939 gui_syncyesno_run(&message
, NULL
, NULL
);
941 for(fd
=0; fd
< MAX_OPEN_FILES
; fd
++)
942 if(open_files
& (1<<fd
))
947 if (rc
== PLUGIN_ERROR
)
948 splash(HZ
*2, str(LANG_PLUGIN_ERROR
));
953 /* Returns a pointer to the portion of the plugin buffer that is not already
954 being used. If no plugin is loaded, returns the entire plugin buffer */
955 void* plugin_get_buffer(size_t *buffer_size
)
959 if (current_plugin_handle
)
961 if (plugin_size
>= PLUGIN_BUFFER_SIZE
)
964 *buffer_size
= PLUGIN_BUFFER_SIZE
-plugin_size
;
965 buffer_pos
= plugin_size
;
969 *buffer_size
= PLUGIN_BUFFER_SIZE
;
973 return &pluginbuf
[buffer_pos
];
976 /* Returns a pointer to the mp3 buffer.
977 Playback gets stopped, to avoid conflicts.
978 Talk buffer is stolen as well.
980 void* plugin_get_audio_buffer(size_t *buffer_size
)
983 return audio_get_buffer(true, buffer_size
);
986 /* The plugin wants to stay resident after leaving its main function, e.g.
987 runs from timer or own thread. The callback is registered to later
988 instruct it to free its resources before a new plugin gets loaded. */
989 void plugin_tsr(bool (*exit_callback
)(bool))
991 pfn_tsr_exit
= exit_callback
; /* remember the callback for later */
994 char *plugin_get_current_filename(void)
996 return current_plugin
;
999 static int open_wrapper(const char* pathname
, int flags
, ...)
1001 /* we don't have an 'open' function. it's a define. and we need
1002 * the real file_open, hence PREFIX() doesn't work here */
1004 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
1005 if (flags
& O_CREAT
)
1008 va_start(ap
, flags
);
1009 fd
= open(pathname
, flags
, va_arg(ap
, unsigned int));
1013 fd
= open(pathname
, flags
);
1015 fd
= file_open(pathname
,flags
);
1018 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1020 open_files
|= 1<<fd
;
1025 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1026 static int close_wrapper(int fd
)
1028 if((~open_files
) & (1<<fd
))
1030 logf("double close from plugin");
1033 open_files
&= (~(1<<fd
));
1035 return PREFIX(close
)(fd
);
1038 static int creat_wrapper(const char *pathname
, mode_t mode
)
1040 int fd
= PREFIX(creat
)(pathname
, mode
);
1043 open_files
|= (1<<fd
);
1047 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */