Dithering option for mpegplayer on gigabeat-f/x and e200. Assembly IDCT for ARm just...
[kugel-rb.git] / apps / plugin.c
blobf56d5325371606b25b77f834b4afb9f73a4ea099
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Björn Stenberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "plugin.h"
20 #include <ctype.h>
21 #include <string.h>
22 #include <sprintf.h>
23 #include <atoi.h>
24 #include "debug.h"
25 #include "i2c.h"
26 #include "lang.h"
27 #include "led.h"
28 #include "keyboard.h"
29 #include "buffer.h"
30 #include "backlight.h"
31 #include "sound_menu.h"
32 #include "mp3data.h"
33 #include "powermgmt.h"
34 #include "splash.h"
35 #include "logf.h"
36 #include "option_select.h"
38 #if CONFIG_CHARGING
39 #include "power.h"
40 #endif
42 #ifdef HAVE_LCD_BITMAP
43 #include "scrollbar.h"
44 #include "peakmeter.h"
45 #include "bmp.h"
46 #include "bidi.h"
47 #endif
49 #ifdef SIMULATOR
50 static unsigned char pluginbuf[PLUGIN_BUFFER_SIZE];
51 void *sim_plugin_load(char *plugin, void **pd);
52 void sim_plugin_close(void *pd);
53 void sim_lcd_ex_init(int shades, unsigned long (*getpixel)(int, int));
54 void sim_lcd_ex_update_rect(int x, int y, int width, int height);
55 #else
56 #define sim_plugin_close(x)
57 extern unsigned char pluginbuf[];
58 #include "bitswap.h"
59 #endif
61 /* for actual plugins only, not for codecs */
62 static bool plugin_loaded = false;
63 static int plugin_size = 0;
64 static bool (*pfn_tsr_exit)(bool reenter) = NULL; /* TSR exit callback */
65 static char current_plugin[MAX_PATH];
67 char *plugin_get_current_filename(void);
69 extern struct thread_entry threads[MAXTHREADS];
71 static const struct plugin_api rockbox_api = {
73 /* lcd */
74 lcd_set_contrast,
75 lcd_update,
76 lcd_clear_display,
77 lcd_setmargins,
78 lcd_getstringsize,
79 lcd_putsxy,
80 lcd_puts,
81 lcd_puts_scroll,
82 lcd_stop_scroll,
83 #ifdef HAVE_LCD_CHARCELLS
84 lcd_define_pattern,
85 lcd_get_locked_pattern,
86 lcd_unlock_pattern,
87 lcd_putc,
88 lcd_put_cursor,
89 lcd_remove_cursor,
90 lcd_icon,
91 lcd_double_height,
92 #else
93 lcd_set_drawmode,
94 lcd_get_drawmode,
95 lcd_setfont,
96 lcd_drawpixel,
97 lcd_drawline,
98 lcd_hline,
99 lcd_vline,
100 lcd_drawrect,
101 lcd_fillrect,
102 lcd_mono_bitmap_part,
103 lcd_mono_bitmap,
104 #if LCD_DEPTH > 1
105 lcd_set_foreground,
106 lcd_get_foreground,
107 lcd_set_background,
108 lcd_get_background,
109 lcd_bitmap_part,
110 lcd_bitmap,
111 lcd_get_backdrop,
112 lcd_set_backdrop,
113 #endif
114 #if LCD_DEPTH == 16
115 lcd_bitmap_transparent_part,
116 lcd_bitmap_transparent,
117 #endif
118 bidi_l2v,
119 font_get_bits,
120 font_load,
121 lcd_puts_style,
122 lcd_puts_scroll_style,
123 &lcd_framebuffer[0][0],
124 lcd_blit,
125 lcd_update_rect,
126 gui_scrollbar_draw,
127 font_get,
128 font_getstringsize,
129 font_get_width,
130 screen_clear_area,
131 #endif
132 backlight_on,
133 backlight_off,
134 backlight_set_timeout,
135 #if CONFIG_CHARGING
136 backlight_set_timeout_plugged,
137 #endif
138 gui_syncsplash,
139 #ifdef HAVE_REMOTE_LCD
140 /* remote lcd */
141 lcd_remote_set_contrast,
142 lcd_remote_clear_display,
143 lcd_remote_puts,
144 lcd_remote_puts_scroll,
145 lcd_remote_stop_scroll,
146 lcd_remote_set_drawmode,
147 lcd_remote_get_drawmode,
148 lcd_remote_setfont,
149 lcd_remote_getstringsize,
150 lcd_remote_drawpixel,
151 lcd_remote_drawline,
152 lcd_remote_hline,
153 lcd_remote_vline,
154 lcd_remote_drawrect,
155 lcd_remote_fillrect,
156 lcd_remote_mono_bitmap_part,
157 lcd_remote_mono_bitmap,
158 lcd_remote_putsxy,
159 lcd_remote_puts_style,
160 lcd_remote_puts_scroll_style,
161 &lcd_remote_framebuffer[0][0],
162 lcd_remote_update,
163 lcd_remote_update_rect,
165 remote_backlight_on,
166 remote_backlight_off,
167 #endif
168 #if NB_SCREENS == 2
169 {&screens[SCREEN_MAIN], &screens[SCREEN_REMOTE]},
170 #else
171 {&screens[SCREEN_MAIN]},
172 #endif
173 #if defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
174 lcd_remote_set_foreground,
175 lcd_remote_get_foreground,
176 lcd_remote_set_background,
177 lcd_remote_get_background,
178 lcd_remote_bitmap_part,
179 lcd_remote_bitmap,
180 #endif
182 #if defined(HAVE_LCD_COLOR)
183 lcd_yuv_blit,
184 #endif
185 /* list */
186 gui_synclist_init,
187 gui_synclist_set_nb_items,
188 gui_synclist_set_icon_callback,
189 gui_synclist_get_nb_items,
190 gui_synclist_get_sel_pos,
191 gui_synclist_draw,
192 gui_synclist_select_item,
193 gui_synclist_add_item,
194 gui_synclist_del_item,
195 gui_synclist_limit_scroll,
196 gui_synclist_flash,
197 gui_synclist_do_button,
198 gui_synclist_set_title,
200 /* button */
201 button_get,
202 button_get_w_tmo,
203 button_status,
204 button_clear_queue,
205 #ifdef HAS_BUTTON_HOLD
206 button_hold,
207 #endif
209 /* file */
210 (open_func)PREFIX(open),
211 close,
212 (read_func)PREFIX(read),
213 PREFIX(lseek),
214 (creat_func)PREFIX(creat),
215 (write_func)PREFIX(write),
216 PREFIX(remove),
217 PREFIX(rename),
218 PREFIX(ftruncate),
219 PREFIX(filesize),
220 fdprintf,
221 read_line,
222 settings_parseline,
223 #ifndef SIMULATOR
224 ata_sleep,
225 ata_disk_is_active,
226 #endif
227 ata_spindown,
228 reload_directory,
229 create_numbered_filename,
231 /* dir */
232 opendir,
233 closedir,
234 readdir,
235 mkdir,
236 rmdir,
238 /* kernel/ system */
239 PREFIX(sleep),
240 yield,
241 #ifdef HAVE_PRIORITY_SCHEDULING
242 priority_yield,
243 #endif
244 &current_tick,
245 default_event_handler,
246 default_event_handler_ex,
247 threads,
248 create_thread,
249 remove_thread,
250 reset_poweroff_timer,
251 #ifndef SIMULATOR
252 system_memory_guard,
253 &cpu_frequency,
255 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
256 #ifdef CPU_BOOST_LOGGING
257 cpu_boost_,
258 #else
259 cpu_boost,
260 #endif
261 #endif
262 #endif
263 timer_register,
264 timer_unregister,
265 timer_set_period,
267 queue_init,
268 queue_delete,
269 queue_post,
270 queue_wait_w_tmo,
271 usb_acknowledge,
272 #ifdef RB_PROFILE
273 profile_thread,
274 profstop,
275 profile_func_enter,
276 profile_func_exit,
277 #endif
279 #ifdef SIMULATOR
280 /* special simulator hooks */
281 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
282 sim_lcd_ex_init,
283 sim_lcd_ex_update_rect,
284 #endif
285 #endif
287 /* strings and memory */
288 snprintf,
289 vsnprintf,
290 strcpy,
291 strncpy,
292 strlen,
293 strrchr,
294 strcmp,
295 strncmp,
296 strcasecmp,
297 strncasecmp,
298 memset,
299 memcpy,
300 memmove,
301 _ctype_,
302 atoi,
303 strchr,
304 strcat,
305 memchr,
306 memcmp,
307 strcasestr,
308 strtok_r,
309 /* unicode stuff */
310 utf8decode,
311 iso_decode,
312 utf16LEdecode,
313 utf16BEdecode,
314 utf8encode,
315 utf8length,
316 utf8seek,
318 /* sound */
319 #if CONFIG_CODEC == SWCODEC
320 sound_default,
321 #endif
322 sound_set,
324 sound_min,
325 sound_max,
326 #ifndef SIMULATOR
327 mp3_play_data,
328 mp3_play_pause,
329 mp3_play_stop,
330 mp3_is_playing,
331 #if CONFIG_CODEC != SWCODEC
332 bitswap,
333 #endif
334 #endif
335 #if CONFIG_CODEC == SWCODEC
336 &audio_master_sampr_list[0],
337 &hw_freq_sampr[0],
338 pcm_apply_settings,
339 pcm_play_data,
340 pcm_play_stop,
341 pcm_set_frequency,
342 pcm_is_playing,
343 pcm_is_paused,
344 pcm_play_pause,
345 pcm_get_bytes_waiting,
346 pcm_calculate_peaks,
347 #ifdef HAVE_RECORDING
348 &rec_freq_sampr[0],
349 pcm_init_recording,
350 pcm_close_recording,
351 pcm_record_data,
352 pcm_record_more,
353 pcm_stop_recording,
354 pcm_calculate_rec_peaks,
355 audio_set_recording_gain,
356 #endif /* HAVE_RECORDING */
357 #if INPUT_SRC_CAPS != 0
358 audio_set_output_source,
359 audio_set_input_source,
360 #endif
361 #endif /* CONFIG_CODEC == SWCODEC */
363 /* playback control */
364 playlist_amount,
365 playlist_resume,
366 playlist_start,
367 PREFIX(audio_play),
368 audio_stop,
369 audio_pause,
370 audio_resume,
371 audio_next,
372 audio_prev,
373 audio_ff_rewind,
374 audio_next_track,
375 audio_status,
376 audio_has_changed_track,
377 audio_current_track,
378 audio_flush_and_reload_tracks,
379 audio_get_file_pos,
380 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
381 mpeg_get_last_header,
382 #endif
383 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
384 (CONFIG_CODEC == SWCODEC)
385 sound_set_pitch,
386 #endif
388 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
389 /* MAS communication */
390 mas_readmem,
391 mas_writemem,
392 mas_readreg,
393 mas_writereg,
394 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
395 mas_codec_writereg,
396 mas_codec_readreg,
397 i2c_begin,
398 i2c_end,
399 i2c_write,
400 #endif
401 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
403 /* menu */
404 do_menu,
405 /* statusbars */
406 &statusbars,
407 gui_syncstatusbar_draw,
408 /* options */
409 find_setting,
410 option_screen,
411 set_option,
412 set_bool_options,
413 set_int,
414 set_bool,
415 #ifdef HAVE_LCD_COLOR
416 set_color,
417 #endif
419 /* action handling */
420 get_custom_action,
421 get_action,
422 action_userabort,
424 /* power */
425 battery_level,
426 battery_level_safe,
427 battery_time,
428 #ifndef SIMULATOR
429 battery_voltage,
430 #endif
431 #if CONFIG_CHARGING
432 charger_inserted,
433 # if CONFIG_CHARGING == CHARGING_MONITOR
434 charging_state,
435 # endif
436 #endif
437 #ifdef HAVE_USB_POWER
438 usb_powered,
439 #endif
441 /* misc */
442 srand,
443 rand,
444 (qsort_func)qsort,
445 kbd_input,
446 get_time,
447 set_time,
448 #if CONFIG_RTC
449 mktime,
450 #endif
451 plugin_get_buffer,
452 plugin_get_audio_buffer,
453 plugin_tsr,
454 plugin_get_current_filename,
455 #ifdef IRAM_STEAL
456 plugin_iram_init,
457 #endif
458 #if defined(DEBUG) || defined(SIMULATOR)
459 debugf,
460 #endif
461 #ifdef ROCKBOX_HAS_LOGF
462 _logf,
463 #endif
464 &global_settings,
465 &global_status,
466 mp3info,
467 count_mp3_frames,
468 create_xing_header,
469 find_next_frame,
470 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
471 peak_meter_scale_value,
472 peak_meter_set_use_dbfs,
473 peak_meter_get_use_dbfs,
474 #endif
475 #ifdef HAVE_LCD_BITMAP
476 read_bmp_file,
477 screen_dump_set_hook,
478 #endif
479 show_logo,
480 tree_get_context,
481 set_current_file,
482 set_dirfilter,
484 #ifdef HAVE_WHEEL_POSITION
485 wheel_status,
486 wheel_send_events,
487 #endif
489 #ifdef IRIVER_H100_SERIES
490 /* Routines for the iriver_flash -plugin. */
491 detect_original_firmware,
492 detect_flashed_ramimage,
493 detect_flashed_romimage,
494 #endif
495 /* new stuff at the end, sort into place next time
496 the API gets incompatible */
498 #if (CONFIG_CODEC == SWCODEC)
499 spinlock_init,
500 spinlock_lock,
501 spinlock_unlock,
503 codec_load_file,
504 get_codec_filename,
505 get_metadata,
506 #endif
507 led,
509 #if defined(TOSHIBA_GIGABEAT_F) || defined(SANSA_E200)
510 lcd_yuv_set_options,
511 #endif
514 int plugin_load(const char* plugin, void* parameter)
516 int rc;
517 struct plugin_header *hdr;
518 #ifdef SIMULATOR
519 void *pd;
520 #else
521 int fd;
522 ssize_t readsize;
523 #endif
524 int xm, ym;
525 #ifdef HAVE_REMOTE_LCD
526 int rxm, rym;
527 #endif
529 #if LCD_DEPTH > 1
530 fb_data* old_backdrop;
531 #endif
533 if (pfn_tsr_exit != NULL) /* if we have a resident old plugin: */
535 if (pfn_tsr_exit(!strcmp(current_plugin, plugin)) == false )
537 /* not allowing another plugin to load */
538 return PLUGIN_OK;
540 pfn_tsr_exit = NULL;
541 plugin_loaded = false;
544 gui_syncsplash(0, ID2P(LANG_WAIT));
545 strcpy(current_plugin, plugin);
547 #ifdef SIMULATOR
548 hdr = sim_plugin_load((char *)plugin, &pd);
549 if (pd == NULL) {
550 gui_syncsplash(HZ*2, str(LANG_PLUGIN_CANT_OPEN), plugin);
551 return -1;
553 if (hdr == NULL
554 || hdr->magic != PLUGIN_MAGIC
555 || hdr->target_id != TARGET_ID) {
556 sim_plugin_close(pd);
557 gui_syncsplash(HZ*2, str(LANG_PLUGIN_WRONG_MODEL));
558 return -1;
560 if (hdr->api_version > PLUGIN_API_VERSION
561 || hdr->api_version < PLUGIN_MIN_API_VERSION) {
562 sim_plugin_close(pd);
563 gui_syncsplash(HZ*2, str(LANG_PLUGIN_WRONG_VERSION));
564 return -1;
566 #else
567 fd = open(plugin, O_RDONLY);
568 if (fd < 0) {
569 gui_syncsplash(HZ*2, str(LANG_PLUGIN_CANT_OPEN), plugin);
570 return fd;
573 readsize = read(fd, pluginbuf, PLUGIN_BUFFER_SIZE);
574 close(fd);
576 if (readsize < 0) {
577 gui_syncsplash(HZ*2, str(LANG_READ_FAILED), plugin);
578 return -1;
580 hdr = (struct plugin_header *)pluginbuf;
582 if ((unsigned)readsize <= sizeof(struct plugin_header)
583 || hdr->magic != PLUGIN_MAGIC
584 || hdr->target_id != TARGET_ID
585 || hdr->load_addr != pluginbuf
586 || hdr->end_addr > pluginbuf + PLUGIN_BUFFER_SIZE) {
587 gui_syncsplash(HZ*2, str(LANG_PLUGIN_WRONG_MODEL));
588 return -1;
590 if (hdr->api_version > PLUGIN_API_VERSION
591 || hdr->api_version < PLUGIN_MIN_API_VERSION) {
592 gui_syncsplash(HZ*2, str(LANG_PLUGIN_WRONG_VERSION));
593 return -1;
595 plugin_size = hdr->end_addr - pluginbuf;
597 /* zero out bss area only, above guards end of pluginbuf */
598 if (plugin_size > readsize)
599 memset(pluginbuf + readsize, 0, plugin_size - readsize);
600 #endif
602 plugin_loaded = true;
604 xm = lcd_getxmargin();
605 ym = lcd_getymargin();
606 lcd_setmargins(0,0);
608 #if defined HAVE_LCD_BITMAP && LCD_DEPTH > 1
609 old_backdrop = lcd_get_backdrop();
610 #endif
611 lcd_clear_display();
612 lcd_update();
614 #ifdef HAVE_REMOTE_LCD
615 rxm = lcd_remote_getxmargin();
616 rym = lcd_remote_getymargin();
617 lcd_remote_setmargins(0, 0);
618 lcd_remote_clear_display();
619 lcd_remote_update();
620 #endif
622 invalidate_icache();
624 rc = hdr->entry_point((struct plugin_api*) &rockbox_api, parameter);
625 /* explicitly casting the pointer here to avoid touching every plugin. */
627 button_clear_queue();
629 #ifdef HAVE_LCD_BITMAP
630 #if LCD_DEPTH > 1
631 lcd_set_backdrop(old_backdrop);
632 #ifdef HAVE_LCD_COLOR
633 lcd_set_drawinfo(DRMODE_SOLID, global_settings.fg_color,
634 global_settings.bg_color);
635 #else
636 lcd_set_drawinfo(DRMODE_SOLID, LCD_DEFAULT_FG, LCD_DEFAULT_BG);
637 #endif
638 #else /* LCD_DEPTH == 1 */
639 lcd_set_drawmode(DRMODE_SOLID);
640 #endif /* LCD_DEPTH */
641 #endif /* HAVE_LCD_BITMAP */
643 /* restore margins */
644 lcd_setmargins(xm,ym);
645 lcd_clear_display();
646 lcd_update();
648 #ifdef HAVE_REMOTE_LCD
649 #if LCD_REMOTE_DEPTH > 1
650 lcd_remote_set_drawinfo(DRMODE_SOLID, LCD_REMOTE_DEFAULT_FG,
651 LCD_REMOTE_DEFAULT_BG);
652 #else
653 lcd_remote_set_drawmode(DRMODE_SOLID);
654 #endif
655 lcd_remote_setmargins(rxm, rym);
656 lcd_remote_clear_display();
657 lcd_remote_update();
658 #endif
660 if (pfn_tsr_exit == NULL)
661 plugin_loaded = false;
663 sim_plugin_close(pd);
665 switch (rc) {
666 case PLUGIN_OK:
667 break;
669 case PLUGIN_USB_CONNECTED:
670 return PLUGIN_USB_CONNECTED;
672 default:
673 gui_syncsplash(HZ*2, str(LANG_PLUGIN_ERROR));
674 break;
676 return PLUGIN_OK;
679 /* Returns a pointer to the portion of the plugin buffer that is not already
680 being used. If no plugin is loaded, returns the entire plugin buffer */
681 void* plugin_get_buffer(size_t *buffer_size)
683 int buffer_pos;
685 if (plugin_loaded)
687 if (plugin_size >= PLUGIN_BUFFER_SIZE)
688 return NULL;
690 *buffer_size = PLUGIN_BUFFER_SIZE-plugin_size;
691 buffer_pos = plugin_size;
693 else
695 *buffer_size = PLUGIN_BUFFER_SIZE;
696 buffer_pos = 0;
699 return &pluginbuf[buffer_pos];
702 /* Returns a pointer to the mp3 buffer.
703 Playback gets stopped, to avoid conflicts.
704 Talk buffer is stolen as well.
706 void* plugin_get_audio_buffer(size_t *buffer_size)
708 #if CONFIG_CODEC == SWCODEC
709 return audio_get_buffer(true, buffer_size);
710 #else
711 audio_stop();
712 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
713 *buffer_size = audiobufend - audiobuf;
714 return audiobuf;
715 #endif
718 #ifdef IRAM_STEAL
719 /* Initializes plugin IRAM */
720 void plugin_iram_init(char *iramstart, char *iramcopy, size_t iram_size,
721 char *iedata, size_t iedata_size)
723 audio_iram_steal();
724 memcpy(iramstart, iramcopy, iram_size);
725 memset(iedata, 0, iedata_size);
726 memset(iramcopy, 0, iram_size);
727 #if NUM_CORES > 1
728 /* writeback cleared iedata and iramcopy areas */
729 flush_icache();
730 #endif
732 #endif /* IRAM_STEAL */
734 /* The plugin wants to stay resident after leaving its main function, e.g.
735 runs from timer or own thread. The callback is registered to later
736 instruct it to free its resources before a new plugin gets loaded. */
737 void plugin_tsr(bool (*exit_callback)(bool))
739 pfn_tsr_exit = exit_callback; /* remember the callback for later */
742 char *plugin_get_current_filename(void)
744 return current_plugin;