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