Restore people accidently removed from credits in r26487
[kugel-rb.git] / apps / plugin.c
blobd626ef6488c873e1fd065c31abded8c77d48e0fa
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 #ifdef HAVE_LCD_BITMAP
181 is_diacritic,
182 #endif
183 font_get_bits,
184 font_load,
185 font_get,
186 font_getstringsize,
187 font_get_width,
188 screen_clear_area,
189 gui_scrollbar_draw,
190 #endif /* HAVE_LCD_BITMAP */
191 get_codepage_name,
193 backlight_on,
194 backlight_off,
195 backlight_set_timeout,
196 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
197 backlight_set_brightness,
198 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
200 #if CONFIG_CHARGING
201 backlight_set_timeout_plugged,
202 #endif
203 is_backlight_on,
204 splash,
205 splashf,
207 #ifdef HAVE_REMOTE_LCD
208 /* remote lcd */
209 lcd_remote_set_contrast,
210 lcd_remote_clear_display,
211 lcd_remote_puts,
212 lcd_remote_puts_scroll,
213 lcd_remote_stop_scroll,
214 lcd_remote_set_drawmode,
215 lcd_remote_get_drawmode,
216 lcd_remote_setfont,
217 lcd_remote_getstringsize,
218 lcd_remote_drawpixel,
219 lcd_remote_drawline,
220 lcd_remote_hline,
221 lcd_remote_vline,
222 lcd_remote_drawrect,
223 lcd_remote_fillrect,
224 lcd_remote_mono_bitmap_part,
225 lcd_remote_mono_bitmap,
226 lcd_remote_putsxy,
227 lcd_remote_puts_style,
228 lcd_remote_puts_scroll_style,
229 &lcd_remote_framebuffer[0][0],
230 lcd_remote_update,
231 lcd_remote_update_rect,
233 remote_backlight_on,
234 remote_backlight_off,
235 remote_backlight_set_timeout,
236 #if CONFIG_CHARGING
237 remote_backlight_set_timeout_plugged,
238 #endif
239 #endif /* HAVE_REMOTE_LCD */
240 #if NB_SCREENS == 2
241 {&screens[SCREEN_MAIN], &screens[SCREEN_REMOTE]},
242 #else
243 {&screens[SCREEN_MAIN]},
244 #endif
245 #if defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
246 lcd_remote_set_foreground,
247 lcd_remote_get_foreground,
248 lcd_remote_set_background,
249 lcd_remote_get_background,
250 lcd_remote_bitmap_part,
251 lcd_remote_bitmap,
252 #endif
253 viewport_set_defaults,
254 #ifdef HAVE_LCD_BITMAP
255 viewportmanager_theme_enable,
256 viewportmanager_theme_undo,
257 #endif
259 /* list */
260 gui_synclist_init,
261 gui_synclist_set_nb_items,
262 gui_synclist_set_icon_callback,
263 gui_synclist_get_nb_items,
264 gui_synclist_get_sel_pos,
265 gui_synclist_draw,
266 gui_synclist_select_item,
267 gui_synclist_add_item,
268 gui_synclist_del_item,
269 gui_synclist_limit_scroll,
270 gui_synclist_do_button,
271 gui_synclist_set_title,
272 gui_syncyesno_run,
273 simplelist_info_init,
274 simplelist_show_list,
276 /* button */
277 button_get,
278 button_get_w_tmo,
279 button_status,
280 #ifdef HAVE_BUTTON_DATA
281 button_get_data,
282 button_status_wdata,
283 #endif
284 button_clear_queue,
285 button_queue_count,
286 #ifdef HAS_BUTTON_HOLD
287 button_hold,
288 #endif
289 #ifdef HAVE_TOUCHSCREEN
290 touchscreen_set_mode,
291 #endif
293 #ifdef HAVE_BUTTON_LIGHT
294 buttonlight_set_timeout,
295 buttonlight_off,
296 buttonlight_on,
297 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
298 buttonlight_set_brightness,
299 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
300 #endif /* HAVE_BUTTON_LIGHT */
302 /* file */
303 open_utf8,
304 (open_func)open_wrapper,
305 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
306 close_wrapper,
307 #else
308 PREFIX(close),
309 #endif
310 (read_func)PREFIX(read),
311 PREFIX(lseek),
312 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
313 (creat_func)creat_wrapper,
314 #else
315 (creat_func)PREFIX(creat),
316 #endif
317 (write_func)PREFIX(write),
318 PREFIX(remove),
319 PREFIX(rename),
320 PREFIX(ftruncate),
321 PREFIX(filesize),
322 fdprintf,
323 read_line,
324 settings_parseline,
325 storage_sleep,
326 storage_spin,
327 storage_spindown,
328 #if USING_STORAGE_CALLBACK
329 register_storage_idle_func,
330 unregister_storage_idle_func,
331 #endif /* USING_STORAGE_CALLBACK */
332 reload_directory,
333 create_numbered_filename,
334 file_exists,
335 strip_extension,
336 crc_32,
338 /* dir */
339 opendir,
340 closedir,
341 readdir,
342 mkdir,
343 rmdir,
344 dir_exists,
346 /* kernel/ system */
347 #ifdef CPU_ARM
348 __div0,
349 #endif
350 PREFIX(sleep),
351 yield,
352 &current_tick,
353 default_event_handler,
354 default_event_handler_ex,
355 create_thread,
356 thread_exit,
357 thread_wait,
358 #if (CONFIG_CODEC == SWCODEC)
359 thread_thaw,
360 #ifdef HAVE_PRIORITY_SCHEDULING
361 thread_set_priority,
362 #endif
363 mutex_init,
364 mutex_lock,
365 mutex_unlock,
366 #endif
368 reset_poweroff_timer,
369 #ifndef SIMULATOR
370 system_memory_guard,
371 &cpu_frequency,
373 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
374 #ifdef CPU_BOOST_LOGGING
375 cpu_boost_,
376 #else
377 cpu_boost,
378 #endif
379 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
380 #endif /* !SIMULATOR */
381 #ifdef HAVE_SCHEDULER_BOOSTCTRL
382 trigger_cpu_boost,
383 cancel_cpu_boost,
384 #endif
385 #if NUM_CORES > 1
386 cpucache_flush,
387 cpucache_invalidate,
388 #endif
389 timer_register,
390 timer_unregister,
391 timer_set_period,
393 queue_init,
394 queue_delete,
395 queue_post,
396 queue_wait_w_tmo,
397 #if CONFIG_CODEC == SWCODEC
398 queue_enable_queue_send,
399 queue_empty,
400 queue_wait,
401 queue_send,
402 queue_reply,
403 #endif
404 usb_acknowledge,
405 #ifdef USB_ENABLE_HID
406 usb_hid_send,
407 #endif
408 #ifdef RB_PROFILE
409 profile_thread,
410 profstop,
411 __cyg_profile_func_enter,
412 __cyg_profile_func_exit,
413 #endif
414 add_event,
415 remove_event,
416 send_event,
418 #ifdef SIMULATOR
419 /* special simulator hooks */
420 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
421 sim_lcd_ex_init,
422 sim_lcd_ex_update_rect,
423 #endif
424 #endif
426 /* strings and memory */
427 snprintf,
428 vsnprintf,
429 strcpy,
430 strlcpy,
431 strlen,
432 strrchr,
433 strcmp,
434 strncmp,
435 strcasecmp,
436 strncasecmp,
437 memset,
438 memcpy,
439 memmove,
440 #ifndef SIMULATOR
441 _ctype_,
442 #endif
443 atoi,
444 strchr,
445 strcat,
446 strlcat,
447 memchr,
448 memcmp,
449 strcasestr,
450 strtok_r,
451 /* unicode stuff */
452 utf8decode,
453 iso_decode,
454 utf16LEdecode,
455 utf16BEdecode,
456 utf8encode,
457 utf8length,
458 utf8seek,
460 /* sound */
461 sound_set,
462 sound_default,
463 sound_min,
464 sound_max,
465 sound_unit,
466 sound_val2phys,
467 #ifdef AUDIOHW_HAVE_EQ
468 sound_enum_hw_eq_band_setting,
469 #endif
470 #ifndef SIMULATOR
471 mp3_play_data,
472 mp3_play_pause,
473 mp3_play_stop,
474 mp3_is_playing,
475 #if CONFIG_CODEC != SWCODEC
476 bitswap,
477 #endif
478 #endif
479 #if CONFIG_CODEC == SWCODEC
480 &audio_master_sampr_list[0],
481 &hw_freq_sampr[0],
482 pcm_apply_settings,
483 pcm_play_data,
484 pcm_play_stop,
485 pcm_set_frequency,
486 pcm_is_playing,
487 pcm_is_paused,
488 pcm_play_pause,
489 pcm_get_bytes_waiting,
490 pcm_calculate_peaks,
491 pcm_get_peak_buffer,
492 pcm_play_lock,
493 pcm_play_unlock,
494 pcmbuf_beep,
495 #ifdef HAVE_RECORDING
496 &rec_freq_sampr[0],
497 pcm_init_recording,
498 pcm_close_recording,
499 pcm_record_data,
500 pcm_stop_recording,
501 pcm_calculate_rec_peaks,
502 audio_set_recording_gain,
503 #endif /* HAVE_RECORDING */
504 #if INPUT_SRC_CAPS != 0
505 audio_set_output_source,
506 audio_set_input_source,
507 #endif
508 dsp_set_crossfeed,
509 dsp_set_eq,
510 dsp_dither_enable,
511 dsp_configure,
512 dsp_process,
513 dsp_input_count,
514 dsp_output_count,
515 #endif /* CONFIG_CODEC == SWCODEC */
517 /* playback control */
518 playlist_amount,
519 playlist_resume,
520 playlist_start,
521 playlist_add,
522 playlist_sync,
523 playlist_remove_all_tracks,
524 playlist_create,
525 playlist_insert_track,
526 playlist_insert_directory,
527 playlist_shuffle,
528 PREFIX(audio_play),
529 audio_stop,
530 audio_pause,
531 audio_resume,
532 audio_next,
533 audio_prev,
534 audio_ff_rewind,
535 audio_next_track,
536 audio_status,
537 audio_current_track,
538 audio_flush_and_reload_tracks,
539 audio_get_file_pos,
540 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
541 mpeg_get_last_header,
542 #endif
543 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
544 (CONFIG_CODEC == SWCODEC)
545 sound_set_pitch,
546 #endif
548 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
549 /* MAS communication */
550 mas_readmem,
551 mas_writemem,
552 mas_readreg,
553 mas_writereg,
554 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
555 mas_codec_writereg,
556 mas_codec_readreg,
557 i2c_begin,
558 i2c_end,
559 i2c_write,
560 #endif
561 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
563 /* menu */
564 do_menu,
565 /* statusbars */
566 &statusbars,
567 gui_syncstatusbar_draw,
568 /* options */
569 get_settings_list,
570 find_setting,
571 option_screen,
572 set_option,
573 set_bool_options,
574 set_int,
575 set_bool,
576 #ifdef HAVE_LCD_COLOR
577 set_color,
578 #endif
580 /* action handling */
581 get_custom_action,
582 get_action,
583 #ifdef HAVE_TOUCHSCREEN
584 action_get_touchscreen_press,
585 #endif
586 action_userabort,
588 /* power */
589 battery_level,
590 battery_level_safe,
591 battery_time,
592 #ifndef SIMULATOR
593 battery_voltage,
594 #endif
595 #if CONFIG_CHARGING
596 charger_inserted,
597 # if CONFIG_CHARGING >= CHARGING_MONITOR
598 charging_state,
599 # endif
600 #endif
601 #ifdef HAVE_USB_POWER
602 usb_powered,
603 #endif
605 /* misc */
606 #if !defined(SIMULATOR)
607 &errno,
608 #endif
609 srand,
610 rand,
611 (qsort_func)qsort,
612 kbd_input,
613 get_time,
614 set_time,
615 #if CONFIG_RTC
616 mktime,
617 #endif
618 plugin_get_buffer,
619 plugin_get_audio_buffer,
620 plugin_tsr,
621 plugin_get_current_filename,
622 #ifdef PLUGIN_USE_IRAM
623 plugin_iram_init,
624 #endif
625 #if defined(DEBUG) || defined(SIMULATOR)
626 debugf,
627 #endif
628 #ifdef ROCKBOX_HAS_LOGF
629 _logf,
630 #endif
631 &global_settings,
632 &global_status,
633 talk_disable,
634 #if CONFIG_CODEC == SWCODEC
635 codec_thread_do_callback,
636 codec_load_file,
637 get_codec_filename,
638 find_array_ptr,
639 remove_array_ptr,
640 #if defined(HAVE_RECORDING) && (defined(HAVE_LINE_IN) || defined(HAVE_MIC_IN))
641 round_value_to_list32,
642 #endif
643 #endif /* CONFIG_CODEC == SWCODEC */
644 get_metadata,
645 mp3info,
646 count_mp3_frames,
647 create_xing_header,
648 find_next_frame,
649 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
650 peak_meter_scale_value,
651 peak_meter_set_use_dbfs,
652 peak_meter_get_use_dbfs,
653 #endif
654 #ifdef HAVE_LCD_BITMAP
655 read_bmp_file,
656 read_bmp_fd,
657 #ifdef HAVE_JPEG
658 read_jpeg_file,
659 read_jpeg_fd,
660 #endif
661 screen_dump_set_hook,
662 #endif
663 show_logo,
664 tree_get_context,
665 set_current_file,
666 set_dirfilter,
668 #ifdef HAVE_WHEEL_POSITION
669 wheel_status,
670 wheel_send_events,
671 #endif
673 #ifdef IRIVER_H100_SERIES
674 /* Routines for the iriver_flash -plugin. */
675 detect_original_firmware,
676 detect_flashed_ramimage,
677 detect_flashed_romimage,
678 #endif
679 led,
680 #if (CONFIG_CODEC == SWCODEC)
681 bufopen,
682 bufalloc,
683 bufclose,
684 bufseek,
685 bufadvance,
686 bufread,
687 bufgetdata,
688 bufgettail,
689 bufcuttail,
691 buf_get_offset,
692 buf_handle_offset,
693 buf_request_buffer_handle,
694 buf_set_base_handle,
695 buf_used,
696 #endif
698 #ifdef HAVE_TAGCACHE
699 tagcache_search,
700 tagcache_search_set_uniqbuf,
701 tagcache_search_add_filter,
702 tagcache_get_next,
703 tagcache_retrieve,
704 tagcache_search_finish,
705 tagcache_get_numeric,
706 #ifdef HAVE_TC_RAMCACHE
707 tagcache_fill_tags,
708 #endif
709 #endif
711 #ifdef HAVE_ALBUMART
712 search_albumart_files,
713 #endif
715 #ifdef HAVE_SEMAPHORE_OBJECTS
716 semaphore_init,
717 semaphore_wait,
718 semaphore_release,
719 #endif
721 rbversion,
722 /* new stuff at the end, sort into place next time
723 the API gets incompatible */
726 int plugin_load(const char* plugin, const void* parameter)
728 int rc, i;
729 struct plugin_header *hdr;
730 #ifdef SIMULATOR
731 void *pd;
732 #else /* !SIMULATOR */
733 int fd;
734 ssize_t readsize;
735 #if NUM_CORES > 1
736 unsigned my_core;
737 #endif
738 #endif /* !SIMULATOR */
740 #if LCD_DEPTH > 1
741 fb_data* old_backdrop;
742 #endif
744 if (pfn_tsr_exit != NULL) /* if we have a resident old plugin: */
746 if (pfn_tsr_exit(!strcmp(current_plugin, plugin)) == false )
748 /* not allowing another plugin to load */
749 return PLUGIN_OK;
751 pfn_tsr_exit = NULL;
752 plugin_loaded = false;
755 splash(0, ID2P(LANG_WAIT));
756 strcpy(current_plugin, plugin);
758 #ifdef SIMULATOR
759 hdr = sim_plugin_load((char *)plugin, &pd);
760 if (pd == NULL) {
761 splashf(HZ*2, str(LANG_PLUGIN_CANT_OPEN), plugin);
762 return -1;
764 if (hdr == NULL
765 || hdr->magic != PLUGIN_MAGIC
766 || hdr->target_id != TARGET_ID) {
767 sim_plugin_close(pd);
768 splash(HZ*2, str(LANG_PLUGIN_WRONG_MODEL));
769 return -1;
771 if (hdr->api_version > PLUGIN_API_VERSION
772 || hdr->api_version < PLUGIN_MIN_API_VERSION) {
773 sim_plugin_close(pd);
774 splash(HZ*2, str(LANG_PLUGIN_WRONG_VERSION));
775 return -1;
777 #else
778 fd = open(plugin, O_RDONLY);
779 if (fd < 0) {
780 splashf(HZ*2, str(LANG_PLUGIN_CANT_OPEN), plugin);
781 return fd;
783 #if NUM_CORES > 1
784 /* Make sure COP cache is flushed and invalidated before loading */
785 my_core = switch_core(CURRENT_CORE ^ 1);
786 cpucache_invalidate();
787 switch_core(my_core);
788 #endif
790 readsize = read(fd, pluginbuf, PLUGIN_BUFFER_SIZE);
791 close(fd);
793 if (readsize < 0) {
794 splashf(HZ*2, str(LANG_READ_FAILED), plugin);
795 return -1;
797 hdr = (struct plugin_header *)pluginbuf;
799 if ((unsigned)readsize <= sizeof(struct plugin_header)
800 || hdr->magic != PLUGIN_MAGIC
801 || hdr->target_id != TARGET_ID
802 || hdr->load_addr != pluginbuf
803 || hdr->end_addr > pluginbuf + PLUGIN_BUFFER_SIZE) {
804 splash(HZ*2, str(LANG_PLUGIN_WRONG_MODEL));
805 return -1;
807 if (hdr->api_version > PLUGIN_API_VERSION
808 || hdr->api_version < PLUGIN_MIN_API_VERSION) {
809 splash(HZ*2, str(LANG_PLUGIN_WRONG_VERSION));
810 return -1;
812 plugin_size = hdr->end_addr - pluginbuf;
814 /* zero out bss area only, above guards end of pluginbuf */
815 if (plugin_size > readsize)
816 memset(pluginbuf + readsize, 0, plugin_size - readsize);
817 #endif
819 *(hdr->api) = &rockbox_api;
820 plugin_loaded = true;
823 #if defined HAVE_LCD_BITMAP && LCD_DEPTH > 1
824 old_backdrop = lcd_get_backdrop();
825 #endif
826 lcd_clear_display();
827 lcd_update();
829 #ifdef HAVE_REMOTE_LCD
830 lcd_remote_clear_display();
831 lcd_remote_update();
832 #endif
834 FOR_NB_SCREENS(i)
835 viewportmanager_theme_enable(i, false, NULL);
837 cpucache_invalidate();
839 #ifdef HAVE_TOUCHSCREEN
840 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
841 #endif
843 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
844 open_files = 0;
845 #endif
847 rc = hdr->entry_point(parameter);
849 /* Go back to the global setting in case the plugin changed it */
850 #ifdef HAVE_TOUCHSCREEN
851 touchscreen_set_mode(global_settings.touch_mode);
852 #endif
854 button_clear_queue();
856 #ifdef HAVE_LCD_BITMAP
857 lcd_setfont(FONT_UI);
858 #if LCD_DEPTH > 1
859 lcd_set_backdrop(old_backdrop);
860 #ifdef HAVE_LCD_COLOR
861 lcd_set_drawinfo(DRMODE_SOLID, global_settings.fg_color,
862 global_settings.bg_color);
863 #else
864 lcd_set_drawinfo(DRMODE_SOLID, LCD_DEFAULT_FG, LCD_DEFAULT_BG);
865 #endif
866 #else /* LCD_DEPTH == 1 */
867 lcd_set_drawmode(DRMODE_SOLID);
868 #endif /* LCD_DEPTH */
869 #endif /* HAVE_LCD_BITMAP */
872 #ifdef HAVE_REMOTE_LCD
873 #if LCD_REMOTE_DEPTH > 1
874 lcd_remote_set_drawinfo(DRMODE_SOLID, LCD_REMOTE_DEFAULT_FG,
875 LCD_REMOTE_DEFAULT_BG);
876 #else
877 lcd_remote_set_drawmode(DRMODE_SOLID);
878 #endif
879 #endif
881 lcd_clear_display();
882 #ifdef HAVE_LCD_REMOTE
883 lcd_remote_clear_display();
884 #endif
886 FOR_NB_SCREENS(i)
887 viewportmanager_theme_undo(i, false);
889 if (pfn_tsr_exit == NULL)
890 plugin_loaded = false;
892 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
893 if(open_files != 0 && !plugin_loaded)
895 int fd;
896 logf("Plugin '%s' leaks file handles", plugin);
898 static const char *lines[] =
899 { ID2P(LANG_PLUGIN_ERROR),
900 "#leak-file-handles" };
901 static const struct text_message message={ lines, 2 };
902 button_clear_queue(); /* Empty the keyboard buffer */
903 gui_syncyesno_run(&message, NULL, NULL);
905 for(fd=0; fd < MAX_OPEN_FILES; fd++)
906 if(open_files & (1<<fd))
907 close_wrapper(fd);
909 #endif
911 sim_plugin_close(pd);
913 if (rc == PLUGIN_ERROR)
914 splash(HZ*2, str(LANG_PLUGIN_ERROR));
916 return rc;
919 /* Returns a pointer to the portion of the plugin buffer that is not already
920 being used. If no plugin is loaded, returns the entire plugin buffer */
921 void* plugin_get_buffer(size_t *buffer_size)
923 int buffer_pos;
925 if (plugin_loaded)
927 if (plugin_size >= PLUGIN_BUFFER_SIZE)
928 return NULL;
930 *buffer_size = PLUGIN_BUFFER_SIZE-plugin_size;
931 buffer_pos = plugin_size;
933 else
935 *buffer_size = PLUGIN_BUFFER_SIZE;
936 buffer_pos = 0;
939 return &pluginbuf[buffer_pos];
942 /* Returns a pointer to the mp3 buffer.
943 Playback gets stopped, to avoid conflicts.
944 Talk buffer is stolen as well.
946 void* plugin_get_audio_buffer(size_t *buffer_size)
948 #if CONFIG_CODEC == SWCODEC
949 return audio_get_buffer(true, buffer_size);
950 #else
951 audio_stop();
952 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
953 *buffer_size = audiobufend - audiobuf;
954 return audiobuf;
955 #endif
958 #ifdef PLUGIN_USE_IRAM
959 /* Initializes plugin IRAM */
960 void plugin_iram_init(char *iramstart, char *iramcopy, size_t iram_size,
961 char *iedata, size_t iedata_size)
963 /* We need to stop audio playback in order to use codec IRAM */
964 audio_hard_stop();
965 memcpy(iramstart, iramcopy, iram_size);
966 memset(iedata, 0, iedata_size);
967 memset(iramcopy, 0, iram_size);
968 #if NUM_CORES > 1
969 /* writeback cleared iedata and iramcopy areas */
970 cpucache_flush();
971 #endif
973 #endif /* PLUGIN_USE_IRAM */
975 /* The plugin wants to stay resident after leaving its main function, e.g.
976 runs from timer or own thread. The callback is registered to later
977 instruct it to free its resources before a new plugin gets loaded. */
978 void plugin_tsr(bool (*exit_callback)(bool))
980 pfn_tsr_exit = exit_callback; /* remember the callback for later */
983 char *plugin_get_current_filename(void)
985 return current_plugin;
988 static int open_wrapper(const char* pathname, int flags, ...)
990 /* we don't have an 'open' function. it's a define. and we need
991 * the real file_open, hence PREFIX() doesn't work here */
992 int fd;
993 #ifdef SIMULATOR
994 if (flags & O_CREAT)
996 va_list ap;
997 va_start(ap, flags);
998 fd = sim_open(pathname, flags, va_arg(ap, unsigned int));
999 va_end(ap);
1001 else
1002 fd = sim_open(pathname, flags);
1003 #else
1004 fd = file_open(pathname,flags);
1005 #endif
1007 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1008 if(fd >= 0)
1009 open_files |= 1<<fd;
1010 #endif
1011 return fd;
1014 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1015 static int close_wrapper(int fd)
1017 if((~open_files) & (1<<fd))
1019 logf("double close from plugin");
1021 if(fd >= 0)
1022 open_files &= (~(1<<fd));
1024 return PREFIX(close)(fd);
1027 static int creat_wrapper(const char *pathname, mode_t mode)
1029 int fd = PREFIX(creat)(pathname, mode);
1031 if(fd >= 0)
1032 open_files |= (1<<fd);
1034 return fd;
1036 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */