Gigabeat S: Fully enable access to hardware tone controls and 3-D effect feature...
[kugel-rb.git] / apps / plugin.c
blob90380a003999006e40062b7c5814cf462e0fb2c5
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
21 #include "plugin.h"
22 #include <ctype.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include "debug.h"
26 #include "i2c.h"
27 #include "lang.h"
28 #include "led.h"
29 #include "keyboard.h"
30 #include "buffer.h"
31 #include "backlight.h"
32 #include "sound_menu.h"
33 #include "mp3data.h"
34 #include "powermgmt.h"
35 #include "splash.h"
36 #include "logf.h"
37 #include "option_select.h"
38 #include "talk.h"
39 #include "version.h"
40 #include "storage.h"
41 #include "pcmbuf.h"
42 #include "errno.h"
43 #include "diacritic.h"
45 #if CONFIG_CHARGING
46 #include "power.h"
47 #endif
49 #ifdef HAVE_LCD_BITMAP
50 #include "scrollbar.h"
51 #include "peakmeter.h"
52 #include "bmp.h"
53 #include "bidi.h"
54 #endif
56 #ifdef USB_ENABLE_HID
57 #include "usbstack/usb_hid.h"
58 #endif
60 #ifdef SIMULATOR
61 #define PREFIX(_x_) sim_ ## _x_
62 #else
63 #define PREFIX
64 #endif
66 #if defined(HAVE_PLUGIN_CHECK_OPEN_CLOSE) && (MAX_OPEN_FILES>32)
67 #warning "MAX_OPEN_FILES>32, disabling plugin file open/close checking"
68 #undef HAVE_PLUGIN_CHECK_OPEN_CLOSE
69 #endif
71 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
72 static unsigned int open_files;
73 #endif
75 #ifdef SIMULATOR
76 static unsigned char pluginbuf[PLUGIN_BUFFER_SIZE];
77 void *sim_plugin_load(char *plugin, void **pd);
78 void sim_plugin_close(void *pd);
79 void sim_lcd_ex_init(unsigned long (*getpixel)(int, int));
80 void sim_lcd_ex_update_rect(int x, int y, int width, int height);
81 #else
82 #define sim_plugin_close(x)
83 extern unsigned char pluginbuf[];
84 #include "bitswap.h"
85 #endif
87 /* for actual plugins only, not for codecs */
88 static bool plugin_loaded = false;
89 static int plugin_size = 0;
90 static bool (*pfn_tsr_exit)(bool reenter) = NULL; /* TSR exit callback */
91 static char current_plugin[MAX_PATH];
93 char *plugin_get_current_filename(void);
95 /* Some wrappers used to monitor open and close and detect leaks*/
96 static int open_wrapper(const char* pathname, int flags, ...);
97 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
98 static int close_wrapper(int fd);
99 static int creat_wrapper(const char *pathname, mode_t mode);
100 #endif
102 static const struct plugin_api rockbox_api = {
104 /* lcd */
105 #ifdef HAVE_LCD_CONTRAST
106 lcd_set_contrast,
107 #endif
108 lcd_update,
109 lcd_clear_display,
110 lcd_getstringsize,
111 lcd_putsxy,
112 lcd_puts,
113 lcd_puts_scroll,
114 lcd_stop_scroll,
115 #ifdef HAVE_LCD_CHARCELLS
116 lcd_define_pattern,
117 lcd_get_locked_pattern,
118 lcd_unlock_pattern,
119 lcd_putc,
120 lcd_put_cursor,
121 lcd_remove_cursor,
122 lcd_icon,
123 lcd_double_height,
124 #else
125 &lcd_framebuffer[0][0],
126 lcd_update_rect,
127 lcd_set_drawmode,
128 lcd_get_drawmode,
129 lcd_setfont,
130 lcd_drawpixel,
131 lcd_drawline,
132 lcd_hline,
133 lcd_vline,
134 lcd_drawrect,
135 lcd_fillrect,
136 lcd_mono_bitmap_part,
137 lcd_mono_bitmap,
138 #if LCD_DEPTH > 1
139 lcd_set_foreground,
140 lcd_get_foreground,
141 lcd_set_background,
142 lcd_get_background,
143 lcd_bitmap_part,
144 lcd_bitmap,
145 lcd_get_backdrop,
146 lcd_set_backdrop,
147 #endif
148 #if LCD_DEPTH == 16
149 lcd_bitmap_transparent_part,
150 lcd_bitmap_transparent,
151 #if MEMORYSIZE > 2
152 lcd_blit_yuv,
153 #if defined(TOSHIBA_GIGABEAT_F) || defined(SANSA_E200) || defined(SANSA_C200) \
154 || defined(IRIVER_H10) || defined(COWON_D2) || defined(PHILIPS_HDD1630) \
155 || defined(SANSA_FUZE) || defined(SANSA_E200V2) || defined(SANSA_FUZEV2) \
156 || defined(TOSHIBA_GIGABEAT_S)
157 lcd_yuv_set_options,
158 #endif
159 #endif /* MEMORYSIZE > 2 */
160 #elif (LCD_DEPTH < 4) && !defined(SIMULATOR)
161 lcd_blit_mono,
162 lcd_blit_grey_phase,
163 #endif /* LCD_DEPTH */
164 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
165 lcd_blit_pal256,
166 lcd_pal256_update_pal,
167 #endif
168 lcd_puts_style,
169 lcd_puts_scroll_style,
170 #ifdef HAVE_LCD_INVERT
171 lcd_set_invert_display,
172 #endif /* HAVE_LCD_INVERT */
173 #if defined(HAVE_LCD_MODES)
174 lcd_set_mode,
175 #endif
176 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
177 &button_queue,
178 #endif
179 bidi_l2v,
180 font_get_bits,
181 font_load,
182 font_get,
183 font_getstringsize,
184 font_get_width,
185 screen_clear_area,
186 gui_scrollbar_draw,
187 #endif /* HAVE_LCD_BITMAP */
188 get_codepage_name,
190 backlight_on,
191 backlight_off,
192 backlight_set_timeout,
193 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
194 backlight_set_brightness,
195 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
197 #if CONFIG_CHARGING
198 backlight_set_timeout_plugged,
199 #endif
200 is_backlight_on,
201 splash,
202 splashf,
204 #ifdef HAVE_REMOTE_LCD
205 /* remote lcd */
206 lcd_remote_set_contrast,
207 lcd_remote_clear_display,
208 lcd_remote_puts,
209 lcd_remote_puts_scroll,
210 lcd_remote_stop_scroll,
211 lcd_remote_set_drawmode,
212 lcd_remote_get_drawmode,
213 lcd_remote_setfont,
214 lcd_remote_getstringsize,
215 lcd_remote_drawpixel,
216 lcd_remote_drawline,
217 lcd_remote_hline,
218 lcd_remote_vline,
219 lcd_remote_drawrect,
220 lcd_remote_fillrect,
221 lcd_remote_mono_bitmap_part,
222 lcd_remote_mono_bitmap,
223 lcd_remote_putsxy,
224 lcd_remote_puts_style,
225 lcd_remote_puts_scroll_style,
226 &lcd_remote_framebuffer[0][0],
227 lcd_remote_update,
228 lcd_remote_update_rect,
230 remote_backlight_on,
231 remote_backlight_off,
232 remote_backlight_set_timeout,
233 #if CONFIG_CHARGING
234 remote_backlight_set_timeout_plugged,
235 #endif
236 #endif /* HAVE_REMOTE_LCD */
237 #if NB_SCREENS == 2
238 {&screens[SCREEN_MAIN], &screens[SCREEN_REMOTE]},
239 #else
240 {&screens[SCREEN_MAIN]},
241 #endif
242 #if defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
243 lcd_remote_set_foreground,
244 lcd_remote_get_foreground,
245 lcd_remote_set_background,
246 lcd_remote_get_background,
247 lcd_remote_bitmap_part,
248 lcd_remote_bitmap,
249 #endif
250 viewport_set_defaults,
251 #ifdef HAVE_LCD_BITMAP
252 viewportmanager_theme_enable,
253 viewportmanager_theme_undo,
254 #endif
256 /* list */
257 gui_synclist_init,
258 gui_synclist_set_nb_items,
259 gui_synclist_set_icon_callback,
260 gui_synclist_get_nb_items,
261 gui_synclist_get_sel_pos,
262 gui_synclist_draw,
263 gui_synclist_select_item,
264 gui_synclist_add_item,
265 gui_synclist_del_item,
266 gui_synclist_limit_scroll,
267 gui_synclist_do_button,
268 gui_synclist_set_title,
269 gui_syncyesno_run,
270 simplelist_info_init,
271 simplelist_show_list,
273 /* button */
274 button_get,
275 button_get_w_tmo,
276 button_status,
277 #ifdef HAVE_BUTTON_DATA
278 button_get_data,
279 button_status_wdata,
280 #endif
281 button_clear_queue,
282 button_queue_count,
283 #ifdef HAS_BUTTON_HOLD
284 button_hold,
285 #endif
286 #ifdef HAVE_TOUCHSCREEN
287 touchscreen_set_mode,
288 #endif
290 #ifdef HAVE_BUTTON_LIGHT
291 buttonlight_set_timeout,
292 buttonlight_off,
293 buttonlight_on,
294 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
295 buttonlight_set_brightness,
296 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
297 #endif /* HAVE_BUTTON_LIGHT */
299 /* file */
300 open_utf8,
301 (open_func)open_wrapper,
302 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
303 close_wrapper,
304 #else
305 PREFIX(close),
306 #endif
307 (read_func)PREFIX(read),
308 PREFIX(lseek),
309 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
310 (creat_func)creat_wrapper,
311 #else
312 (creat_func)PREFIX(creat),
313 #endif
314 (write_func)PREFIX(write),
315 PREFIX(remove),
316 PREFIX(rename),
317 PREFIX(ftruncate),
318 PREFIX(filesize),
319 fdprintf,
320 read_line,
321 settings_parseline,
322 storage_sleep,
323 storage_spin,
324 storage_spindown,
325 #if USING_STORAGE_CALLBACK
326 register_storage_idle_func,
327 unregister_storage_idle_func,
328 #endif /* USING_STORAGE_CALLBACK */
329 reload_directory,
330 create_numbered_filename,
331 file_exists,
332 strip_extension,
333 crc_32,
335 /* dir */
336 opendir,
337 closedir,
338 readdir,
339 mkdir,
340 rmdir,
341 dir_exists,
343 /* kernel/ system */
344 #ifdef CPU_ARM
345 __div0,
346 #endif
347 PREFIX(sleep),
348 yield,
349 &current_tick,
350 default_event_handler,
351 default_event_handler_ex,
352 create_thread,
353 thread_exit,
354 thread_wait,
355 #if (CONFIG_CODEC == SWCODEC)
356 thread_thaw,
357 #ifdef HAVE_PRIORITY_SCHEDULING
358 thread_set_priority,
359 #endif
360 mutex_init,
361 mutex_lock,
362 mutex_unlock,
363 #endif
365 reset_poweroff_timer,
366 #ifndef SIMULATOR
367 system_memory_guard,
368 &cpu_frequency,
370 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
371 #ifdef CPU_BOOST_LOGGING
372 cpu_boost_,
373 #else
374 cpu_boost,
375 #endif
376 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
377 #endif /* !SIMULATOR */
378 #ifdef HAVE_SCHEDULER_BOOSTCTRL
379 trigger_cpu_boost,
380 cancel_cpu_boost,
381 #endif
382 #if NUM_CORES > 1
383 cpucache_flush,
384 cpucache_invalidate,
385 #endif
386 timer_register,
387 timer_unregister,
388 timer_set_period,
390 queue_init,
391 queue_delete,
392 queue_post,
393 queue_wait_w_tmo,
394 #if CONFIG_CODEC == SWCODEC
395 queue_enable_queue_send,
396 queue_empty,
397 queue_wait,
398 queue_send,
399 queue_reply,
400 #endif
401 usb_acknowledge,
402 #ifdef USB_ENABLE_HID
403 usb_hid_send,
404 #endif
405 #ifdef RB_PROFILE
406 profile_thread,
407 profstop,
408 __cyg_profile_func_enter,
409 __cyg_profile_func_exit,
410 #endif
411 add_event,
412 remove_event,
413 send_event,
415 #ifdef SIMULATOR
416 /* special simulator hooks */
417 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
418 sim_lcd_ex_init,
419 sim_lcd_ex_update_rect,
420 #endif
421 #endif
423 /* strings and memory */
424 snprintf,
425 vsnprintf,
426 strcpy,
427 strlcpy,
428 strlen,
429 strrchr,
430 strcmp,
431 strncmp,
432 strcasecmp,
433 strncasecmp,
434 memset,
435 memcpy,
436 memmove,
437 #ifndef SIMULATOR
438 _ctype_,
439 #endif
440 atoi,
441 strchr,
442 strcat,
443 strlcat,
444 memchr,
445 memcmp,
446 strcasestr,
447 strtok_r,
448 /* unicode stuff */
449 utf8decode,
450 iso_decode,
451 utf16LEdecode,
452 utf16BEdecode,
453 utf8encode,
454 utf8length,
455 utf8seek,
457 /* sound */
458 sound_set,
459 sound_default,
460 sound_min,
461 sound_max,
462 sound_unit,
463 sound_val2phys,
464 #ifndef SIMULATOR
465 mp3_play_data,
466 mp3_play_pause,
467 mp3_play_stop,
468 mp3_is_playing,
469 #if CONFIG_CODEC != SWCODEC
470 bitswap,
471 #endif
472 #endif
473 #if CONFIG_CODEC == SWCODEC
474 &audio_master_sampr_list[0],
475 &hw_freq_sampr[0],
476 pcm_apply_settings,
477 pcm_play_data,
478 pcm_play_stop,
479 pcm_set_frequency,
480 pcm_is_playing,
481 pcm_is_paused,
482 pcm_play_pause,
483 pcm_get_bytes_waiting,
484 pcm_calculate_peaks,
485 pcm_get_peak_buffer,
486 pcm_play_lock,
487 pcm_play_unlock,
488 pcmbuf_beep,
489 #ifdef HAVE_RECORDING
490 &rec_freq_sampr[0],
491 pcm_init_recording,
492 pcm_close_recording,
493 pcm_record_data,
494 pcm_record_more,
495 pcm_stop_recording,
496 pcm_calculate_rec_peaks,
497 audio_set_recording_gain,
498 #endif /* HAVE_RECORDING */
499 #if INPUT_SRC_CAPS != 0
500 audio_set_output_source,
501 audio_set_input_source,
502 #endif
503 dsp_set_crossfeed,
504 dsp_set_eq,
505 dsp_dither_enable,
506 dsp_configure,
507 dsp_process,
508 dsp_input_count,
509 dsp_output_count,
510 #endif /* CONFIG_CODEC == SWCODEC */
512 /* playback control */
513 playlist_amount,
514 playlist_resume,
515 playlist_start,
516 playlist_add,
517 playlist_sync,
518 playlist_remove_all_tracks,
519 playlist_create,
520 playlist_insert_track,
521 playlist_insert_directory,
522 playlist_shuffle,
523 PREFIX(audio_play),
524 audio_stop,
525 audio_pause,
526 audio_resume,
527 audio_next,
528 audio_prev,
529 audio_ff_rewind,
530 audio_next_track,
531 audio_status,
532 audio_current_track,
533 audio_flush_and_reload_tracks,
534 audio_get_file_pos,
535 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
536 mpeg_get_last_header,
537 #endif
538 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
539 (CONFIG_CODEC == SWCODEC)
540 sound_set_pitch,
541 #endif
543 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
544 /* MAS communication */
545 mas_readmem,
546 mas_writemem,
547 mas_readreg,
548 mas_writereg,
549 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
550 mas_codec_writereg,
551 mas_codec_readreg,
552 i2c_begin,
553 i2c_end,
554 i2c_write,
555 #endif
556 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
558 /* menu */
559 do_menu,
560 /* statusbars */
561 &statusbars,
562 gui_syncstatusbar_draw,
563 /* options */
564 get_settings_list,
565 find_setting,
566 option_screen,
567 set_option,
568 set_bool_options,
569 set_int,
570 set_bool,
571 #ifdef HAVE_LCD_COLOR
572 set_color,
573 #endif
575 /* action handling */
576 get_custom_action,
577 get_action,
578 #ifdef HAVE_TOUCHSCREEN
579 action_get_touchscreen_press,
580 #endif
581 action_userabort,
583 /* power */
584 battery_level,
585 battery_level_safe,
586 battery_time,
587 #ifndef SIMULATOR
588 battery_voltage,
589 #endif
590 #if CONFIG_CHARGING
591 charger_inserted,
592 # if CONFIG_CHARGING >= CHARGING_MONITOR
593 charging_state,
594 # endif
595 #endif
596 #ifdef HAVE_USB_POWER
597 usb_powered,
598 #endif
600 /* misc */
601 #if !defined(SIMULATOR)
602 &errno,
603 #endif
604 srand,
605 rand,
606 (qsort_func)qsort,
607 kbd_input,
608 get_time,
609 set_time,
610 #if CONFIG_RTC
611 mktime,
612 #endif
613 plugin_get_buffer,
614 plugin_get_audio_buffer,
615 plugin_tsr,
616 plugin_get_current_filename,
617 #ifdef PLUGIN_USE_IRAM
618 plugin_iram_init,
619 #endif
620 #if defined(DEBUG) || defined(SIMULATOR)
621 debugf,
622 #endif
623 #ifdef ROCKBOX_HAS_LOGF
624 _logf,
625 #endif
626 &global_settings,
627 &global_status,
628 talk_disable,
629 #if CONFIG_CODEC == SWCODEC
630 codec_thread_do_callback,
631 codec_load_file,
632 get_codec_filename,
633 #endif
634 get_metadata,
635 mp3info,
636 count_mp3_frames,
637 create_xing_header,
638 find_next_frame,
639 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
640 peak_meter_scale_value,
641 peak_meter_set_use_dbfs,
642 peak_meter_get_use_dbfs,
643 #endif
644 #ifdef HAVE_LCD_BITMAP
645 read_bmp_file,
646 read_bmp_fd,
647 #ifdef HAVE_JPEG
648 read_jpeg_file,
649 read_jpeg_fd,
650 #endif
651 screen_dump_set_hook,
652 #endif
653 show_logo,
654 tree_get_context,
655 set_current_file,
656 set_dirfilter,
658 #ifdef HAVE_WHEEL_POSITION
659 wheel_status,
660 wheel_send_events,
661 #endif
663 #ifdef IRIVER_H100_SERIES
664 /* Routines for the iriver_flash -plugin. */
665 detect_original_firmware,
666 detect_flashed_ramimage,
667 detect_flashed_romimage,
668 #endif
669 led,
670 #if (CONFIG_CODEC == SWCODEC)
671 bufopen,
672 bufalloc,
673 bufclose,
674 bufseek,
675 bufadvance,
676 bufread,
677 bufgetdata,
678 bufgettail,
679 bufcuttail,
681 buf_get_offset,
682 buf_handle_offset,
683 buf_request_buffer_handle,
684 buf_set_base_handle,
685 buf_used,
686 #endif
688 #ifdef HAVE_TAGCACHE
689 tagcache_search,
690 tagcache_search_set_uniqbuf,
691 tagcache_search_add_filter,
692 tagcache_get_next,
693 tagcache_retrieve,
694 tagcache_search_finish,
695 tagcache_get_numeric,
696 #ifdef HAVE_TC_RAMCACHE
697 tagcache_fill_tags,
698 #endif
699 #endif
701 #ifdef HAVE_ALBUMART
702 search_albumart_files,
703 #endif
705 #ifdef HAVE_SEMAPHORE_OBJECTS
706 semaphore_init,
707 semaphore_wait,
708 semaphore_release,
709 #endif
711 appsversion,
712 /* new stuff at the end, sort into place next time
713 the API gets incompatible */
715 #ifdef HAVE_LCD_BITMAP
716 is_diacritic,
717 #endif
719 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && \
720 (defined(HAVE_LINE_IN) || defined(HAVE_MIC_IN))
721 round_value_to_list32,
722 #endif
724 #ifdef AUDIOHW_HAVE_EQ
725 sound_enum_hw_eq_band_setting,
726 #endif
729 int plugin_load(const char* plugin, const void* parameter)
731 int rc, i;
732 struct plugin_header *hdr;
733 #ifdef SIMULATOR
734 void *pd;
735 #else /* !SIMULATOR */
736 int fd;
737 ssize_t readsize;
738 #if NUM_CORES > 1
739 unsigned my_core;
740 #endif
741 #endif /* !SIMULATOR */
743 #if LCD_DEPTH > 1
744 fb_data* old_backdrop;
745 #endif
747 if (pfn_tsr_exit != NULL) /* if we have a resident old plugin: */
749 if (pfn_tsr_exit(!strcmp(current_plugin, plugin)) == false )
751 /* not allowing another plugin to load */
752 return PLUGIN_OK;
754 pfn_tsr_exit = NULL;
755 plugin_loaded = false;
758 splash(0, ID2P(LANG_WAIT));
759 strcpy(current_plugin, plugin);
761 #ifdef SIMULATOR
762 hdr = sim_plugin_load((char *)plugin, &pd);
763 if (pd == NULL) {
764 splashf(HZ*2, str(LANG_PLUGIN_CANT_OPEN), plugin);
765 return -1;
767 if (hdr == NULL
768 || hdr->magic != PLUGIN_MAGIC
769 || hdr->target_id != TARGET_ID) {
770 sim_plugin_close(pd);
771 splash(HZ*2, str(LANG_PLUGIN_WRONG_MODEL));
772 return -1;
774 if (hdr->api_version > PLUGIN_API_VERSION
775 || hdr->api_version < PLUGIN_MIN_API_VERSION) {
776 sim_plugin_close(pd);
777 splash(HZ*2, str(LANG_PLUGIN_WRONG_VERSION));
778 return -1;
780 #else
781 fd = open(plugin, O_RDONLY);
782 if (fd < 0) {
783 splashf(HZ*2, str(LANG_PLUGIN_CANT_OPEN), plugin);
784 return fd;
786 #if NUM_CORES > 1
787 /* Make sure COP cache is flushed and invalidated before loading */
788 my_core = switch_core(CURRENT_CORE ^ 1);
789 cpucache_invalidate();
790 switch_core(my_core);
791 #endif
793 readsize = read(fd, pluginbuf, PLUGIN_BUFFER_SIZE);
794 close(fd);
796 if (readsize < 0) {
797 splashf(HZ*2, str(LANG_READ_FAILED), plugin);
798 return -1;
800 hdr = (struct plugin_header *)pluginbuf;
802 if ((unsigned)readsize <= sizeof(struct plugin_header)
803 || hdr->magic != PLUGIN_MAGIC
804 || hdr->target_id != TARGET_ID
805 || hdr->load_addr != pluginbuf
806 || hdr->end_addr > pluginbuf + PLUGIN_BUFFER_SIZE) {
807 splash(HZ*2, str(LANG_PLUGIN_WRONG_MODEL));
808 return -1;
810 if (hdr->api_version > PLUGIN_API_VERSION
811 || hdr->api_version < PLUGIN_MIN_API_VERSION) {
812 splash(HZ*2, str(LANG_PLUGIN_WRONG_VERSION));
813 return -1;
815 plugin_size = hdr->end_addr - pluginbuf;
817 /* zero out bss area only, above guards end of pluginbuf */
818 if (plugin_size > readsize)
819 memset(pluginbuf + readsize, 0, plugin_size - readsize);
820 #endif
822 *(hdr->api) = &rockbox_api;
823 plugin_loaded = true;
826 #if defined HAVE_LCD_BITMAP && LCD_DEPTH > 1
827 old_backdrop = lcd_get_backdrop();
828 #endif
829 lcd_clear_display();
830 lcd_update();
832 #ifdef HAVE_REMOTE_LCD
833 lcd_remote_clear_display();
834 lcd_remote_update();
835 #endif
837 FOR_NB_SCREENS(i)
838 viewportmanager_theme_enable(i, false, NULL);
840 cpucache_invalidate();
842 #ifdef HAVE_TOUCHSCREEN
843 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
844 #endif
846 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
847 open_files = 0;
848 #endif
850 rc = hdr->entry_point(parameter);
852 /* Go back to the global setting in case the plugin changed it */
853 #ifdef HAVE_TOUCHSCREEN
854 touchscreen_set_mode(global_settings.touch_mode);
855 #endif
857 button_clear_queue();
859 #ifdef HAVE_LCD_BITMAP
860 lcd_setfont(FONT_UI);
861 #if LCD_DEPTH > 1
862 lcd_set_backdrop(old_backdrop);
863 #ifdef HAVE_LCD_COLOR
864 lcd_set_drawinfo(DRMODE_SOLID, global_settings.fg_color,
865 global_settings.bg_color);
866 #else
867 lcd_set_drawinfo(DRMODE_SOLID, LCD_DEFAULT_FG, LCD_DEFAULT_BG);
868 #endif
869 #else /* LCD_DEPTH == 1 */
870 lcd_set_drawmode(DRMODE_SOLID);
871 #endif /* LCD_DEPTH */
872 #endif /* HAVE_LCD_BITMAP */
875 #ifdef HAVE_REMOTE_LCD
876 #if LCD_REMOTE_DEPTH > 1
877 lcd_remote_set_drawinfo(DRMODE_SOLID, LCD_REMOTE_DEFAULT_FG,
878 LCD_REMOTE_DEFAULT_BG);
879 #else
880 lcd_remote_set_drawmode(DRMODE_SOLID);
881 #endif
882 #endif
884 lcd_clear_display();
885 #ifdef HAVE_LCD_REMOTE
886 lcd_remote_clear_display();
887 #endif
889 FOR_NB_SCREENS(i)
890 viewportmanager_theme_undo(i, false);
892 if (pfn_tsr_exit == NULL)
893 plugin_loaded = false;
895 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
896 if(open_files != 0 && !plugin_loaded)
898 int fd;
899 logf("Plugin '%s' leaks file handles", plugin);
901 static const char *lines[] =
902 { ID2P(LANG_PLUGIN_ERROR),
903 "#leak-file-handles" };
904 static const struct text_message message={ lines, 2 };
905 button_clear_queue(); /* Empty the keyboard buffer */
906 gui_syncyesno_run(&message, NULL, NULL);
908 for(fd=0; fd < MAX_OPEN_FILES; fd++)
909 if(open_files & (1<<fd))
910 close_wrapper(fd);
912 #endif
914 sim_plugin_close(pd);
916 if (rc == PLUGIN_ERROR)
917 splash(HZ*2, str(LANG_PLUGIN_ERROR));
919 return rc;
922 /* Returns a pointer to the portion of the plugin buffer that is not already
923 being used. If no plugin is loaded, returns the entire plugin buffer */
924 void* plugin_get_buffer(size_t *buffer_size)
926 int buffer_pos;
928 if (plugin_loaded)
930 if (plugin_size >= PLUGIN_BUFFER_SIZE)
931 return NULL;
933 *buffer_size = PLUGIN_BUFFER_SIZE-plugin_size;
934 buffer_pos = plugin_size;
936 else
938 *buffer_size = PLUGIN_BUFFER_SIZE;
939 buffer_pos = 0;
942 return &pluginbuf[buffer_pos];
945 /* Returns a pointer to the mp3 buffer.
946 Playback gets stopped, to avoid conflicts.
947 Talk buffer is stolen as well.
949 void* plugin_get_audio_buffer(size_t *buffer_size)
951 #if CONFIG_CODEC == SWCODEC
952 return audio_get_buffer(true, buffer_size);
953 #else
954 audio_stop();
955 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
956 *buffer_size = audiobufend - audiobuf;
957 return audiobuf;
958 #endif
961 #ifdef PLUGIN_USE_IRAM
962 /* Initializes plugin IRAM */
963 void plugin_iram_init(char *iramstart, char *iramcopy, size_t iram_size,
964 char *iedata, size_t iedata_size)
966 /* We need to stop audio playback in order to use codec IRAM */
967 audio_hard_stop();
968 memcpy(iramstart, iramcopy, iram_size);
969 memset(iedata, 0, iedata_size);
970 memset(iramcopy, 0, iram_size);
971 #if NUM_CORES > 1
972 /* writeback cleared iedata and iramcopy areas */
973 cpucache_flush();
974 #endif
976 #endif /* PLUGIN_USE_IRAM */
978 /* The plugin wants to stay resident after leaving its main function, e.g.
979 runs from timer or own thread. The callback is registered to later
980 instruct it to free its resources before a new plugin gets loaded. */
981 void plugin_tsr(bool (*exit_callback)(bool))
983 pfn_tsr_exit = exit_callback; /* remember the callback for later */
986 char *plugin_get_current_filename(void)
988 return current_plugin;
991 static int open_wrapper(const char* pathname, int flags, ...)
993 /* we don't have an 'open' function. it's a define. and we need
994 * the real file_open, hence PREFIX() doesn't work here */
995 int fd;
996 #ifdef SIMULATOR
997 if (flags & O_CREAT)
999 va_list ap;
1000 va_start(ap, flags);
1001 fd = sim_open(pathname, flags, va_arg(ap, unsigned int));
1002 va_end(ap);
1004 else
1005 fd = sim_open(pathname, flags);
1006 #else
1007 fd = file_open(pathname,flags);
1008 #endif
1010 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1011 if(fd >= 0)
1012 open_files |= 1<<fd;
1013 #endif
1014 return fd;
1017 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1018 static int close_wrapper(int fd)
1020 if((~open_files) & (1<<fd))
1022 logf("double close from plugin");
1024 if(fd >= 0)
1025 open_files &= (~(1<<fd));
1027 return PREFIX(close)(fd);
1030 static int creat_wrapper(const char *pathname, mode_t mode)
1032 int fd = PREFIX(creat)(pathname, mode);
1034 if(fd >= 0)
1035 open_files |= (1<<fd);
1037 return fd;
1039 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */