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