Packard Bell Vibe 500: change a not so lucky keymap for the bookmark delete.
[kugel-rb.git] / apps / plugin.c
blob2f54c0d3142291d2c9c1660549c6b052da26200a
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 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
97 /* Some wrappers used to monitor open and close and detect leaks*/
98 static int open_wrapper(const char* pathname, int flags);
99 static int close_wrapper(int fd);
100 static int creat_wrapper(const char *pathname);
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 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
303 (open_func)open_wrapper,
304 close_wrapper,
305 #else
306 (open_func)PREFIX(open),
307 PREFIX(close),
308 #endif
309 (read_func)PREFIX(read),
310 PREFIX(lseek),
311 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
312 (creat_func)creat_wrapper,
313 #else
314 (creat_func)PREFIX(creat),
315 #endif
316 (write_func)PREFIX(write),
317 PREFIX(remove),
318 PREFIX(rename),
319 PREFIX(ftruncate),
320 PREFIX(filesize),
321 fdprintf,
322 read_line,
323 settings_parseline,
324 storage_sleep,
325 storage_spin,
326 storage_spindown,
327 #if USING_STORAGE_CALLBACK
328 register_storage_idle_func,
329 unregister_storage_idle_func,
330 #endif /* USING_STORAGE_CALLBACK */
331 reload_directory,
332 create_numbered_filename,
333 file_exists,
334 strip_extension,
335 crc_32,
337 /* dir */
338 opendir,
339 closedir,
340 readdir,
341 mkdir,
342 rmdir,
343 dir_exists,
345 /* kernel/ system */
346 #ifdef CPU_ARM
347 __div0,
348 #endif
349 PREFIX(sleep),
350 yield,
351 &current_tick,
352 default_event_handler,
353 default_event_handler_ex,
354 create_thread,
355 thread_exit,
356 thread_wait,
357 #if (CONFIG_CODEC == SWCODEC)
358 thread_thaw,
359 #ifdef HAVE_PRIORITY_SCHEDULING
360 thread_set_priority,
361 #endif
362 mutex_init,
363 mutex_lock,
364 mutex_unlock,
365 #endif
367 reset_poweroff_timer,
368 #ifndef SIMULATOR
369 system_memory_guard,
370 &cpu_frequency,
372 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
373 #ifdef CPU_BOOST_LOGGING
374 cpu_boost_,
375 #else
376 cpu_boost,
377 #endif
378 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
379 #endif /* !SIMULATOR */
380 #ifdef HAVE_SCHEDULER_BOOSTCTRL
381 trigger_cpu_boost,
382 cancel_cpu_boost,
383 #endif
384 #if NUM_CORES > 1
385 cpucache_flush,
386 cpucache_invalidate,
387 #endif
388 timer_register,
389 timer_unregister,
390 timer_set_period,
392 queue_init,
393 queue_delete,
394 queue_post,
395 queue_wait_w_tmo,
396 #if CONFIG_CODEC == SWCODEC
397 queue_enable_queue_send,
398 queue_empty,
399 queue_wait,
400 queue_send,
401 queue_reply,
402 #endif
403 usb_acknowledge,
404 #ifdef USB_ENABLE_HID
405 usb_hid_send,
406 #endif
407 #ifdef RB_PROFILE
408 profile_thread,
409 profstop,
410 __cyg_profile_func_enter,
411 __cyg_profile_func_exit,
412 #endif
413 add_event,
414 remove_event,
415 send_event,
417 #ifdef SIMULATOR
418 /* special simulator hooks */
419 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
420 sim_lcd_ex_init,
421 sim_lcd_ex_update_rect,
422 #endif
423 #endif
425 /* strings and memory */
426 snprintf,
427 vsnprintf,
428 strcpy,
429 strlcpy,
430 strlen,
431 strrchr,
432 strcmp,
433 strncmp,
434 strcasecmp,
435 strncasecmp,
436 memset,
437 memcpy,
438 memmove,
439 _ctype_,
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) || defined(__MINGW32__) || defined(__CYGWIN__)
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
720 int plugin_load(const char* plugin, const void* parameter)
722 int rc, i;
723 struct plugin_header *hdr;
724 #ifdef SIMULATOR
725 void *pd;
726 #else /* !SIMULATOR */
727 int fd;
728 ssize_t readsize;
729 #if NUM_CORES > 1
730 unsigned my_core;
731 #endif
732 #endif /* !SIMULATOR */
734 #if LCD_DEPTH > 1
735 fb_data* old_backdrop;
736 #endif
738 if (pfn_tsr_exit != NULL) /* if we have a resident old plugin: */
740 if (pfn_tsr_exit(!strcmp(current_plugin, plugin)) == false )
742 /* not allowing another plugin to load */
743 return PLUGIN_OK;
745 pfn_tsr_exit = NULL;
746 plugin_loaded = false;
749 splash(0, ID2P(LANG_WAIT));
750 strcpy(current_plugin, plugin);
752 #ifdef SIMULATOR
753 hdr = sim_plugin_load((char *)plugin, &pd);
754 if (pd == NULL) {
755 splashf(HZ*2, str(LANG_PLUGIN_CANT_OPEN), plugin);
756 return -1;
758 if (hdr == NULL
759 || hdr->magic != PLUGIN_MAGIC
760 || hdr->target_id != TARGET_ID) {
761 sim_plugin_close(pd);
762 splash(HZ*2, str(LANG_PLUGIN_WRONG_MODEL));
763 return -1;
765 if (hdr->api_version > PLUGIN_API_VERSION
766 || hdr->api_version < PLUGIN_MIN_API_VERSION) {
767 sim_plugin_close(pd);
768 splash(HZ*2, str(LANG_PLUGIN_WRONG_VERSION));
769 return -1;
771 #else
772 fd = open(plugin, O_RDONLY);
773 if (fd < 0) {
774 splashf(HZ*2, str(LANG_PLUGIN_CANT_OPEN), plugin);
775 return fd;
777 #if NUM_CORES > 1
778 /* Make sure COP cache is flushed and invalidated before loading */
779 my_core = switch_core(CURRENT_CORE ^ 1);
780 cpucache_invalidate();
781 switch_core(my_core);
782 #endif
784 readsize = read(fd, pluginbuf, PLUGIN_BUFFER_SIZE);
785 close(fd);
787 if (readsize < 0) {
788 splashf(HZ*2, str(LANG_READ_FAILED), plugin);
789 return -1;
791 hdr = (struct plugin_header *)pluginbuf;
793 if ((unsigned)readsize <= sizeof(struct plugin_header)
794 || hdr->magic != PLUGIN_MAGIC
795 || hdr->target_id != TARGET_ID
796 || hdr->load_addr != pluginbuf
797 || hdr->end_addr > pluginbuf + PLUGIN_BUFFER_SIZE) {
798 splash(HZ*2, str(LANG_PLUGIN_WRONG_MODEL));
799 return -1;
801 if (hdr->api_version > PLUGIN_API_VERSION
802 || hdr->api_version < PLUGIN_MIN_API_VERSION) {
803 splash(HZ*2, str(LANG_PLUGIN_WRONG_VERSION));
804 return -1;
806 plugin_size = hdr->end_addr - pluginbuf;
808 /* zero out bss area only, above guards end of pluginbuf */
809 if (plugin_size > readsize)
810 memset(pluginbuf + readsize, 0, plugin_size - readsize);
811 #endif
813 *(hdr->api) = &rockbox_api;
814 plugin_loaded = true;
817 #if defined HAVE_LCD_BITMAP && LCD_DEPTH > 1
818 old_backdrop = lcd_get_backdrop();
819 #endif
820 lcd_clear_display();
821 lcd_update();
823 #ifdef HAVE_REMOTE_LCD
824 lcd_remote_clear_display();
825 lcd_remote_update();
826 #endif
828 FOR_NB_SCREENS(i)
829 viewportmanager_theme_enable(i, false, NULL);
831 cpucache_invalidate();
833 #ifdef HAVE_TOUCHSCREEN
834 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
835 #endif
837 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
838 open_files = 0;
839 #endif
841 rc = hdr->entry_point(parameter);
843 /* Go back to the global setting in case the plugin changed it */
844 #ifdef HAVE_TOUCHSCREEN
845 touchscreen_set_mode(global_settings.touch_mode);
846 #endif
848 button_clear_queue();
850 #ifdef HAVE_LCD_BITMAP
851 lcd_setfont(FONT_UI);
852 #if LCD_DEPTH > 1
853 lcd_set_backdrop(old_backdrop);
854 #ifdef HAVE_LCD_COLOR
855 lcd_set_drawinfo(DRMODE_SOLID, global_settings.fg_color,
856 global_settings.bg_color);
857 #else
858 lcd_set_drawinfo(DRMODE_SOLID, LCD_DEFAULT_FG, LCD_DEFAULT_BG);
859 #endif
860 #else /* LCD_DEPTH == 1 */
861 lcd_set_drawmode(DRMODE_SOLID);
862 #endif /* LCD_DEPTH */
863 #endif /* HAVE_LCD_BITMAP */
866 #ifdef HAVE_REMOTE_LCD
867 #if LCD_REMOTE_DEPTH > 1
868 lcd_remote_set_drawinfo(DRMODE_SOLID, LCD_REMOTE_DEFAULT_FG,
869 LCD_REMOTE_DEFAULT_BG);
870 #else
871 lcd_remote_set_drawmode(DRMODE_SOLID);
872 #endif
873 #endif
875 lcd_clear_display();
876 #ifdef HAVE_LCD_REMOTE
877 lcd_remote_clear_display();
878 #endif
880 FOR_NB_SCREENS(i)
881 viewportmanager_theme_undo(i, false);
883 if (pfn_tsr_exit == NULL)
884 plugin_loaded = false;
886 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
887 if(open_files != 0 && !plugin_loaded)
889 int fd;
890 logf("Plugin '%s' leaks file handles", plugin);
892 static const char *lines[] =
893 { ID2P(LANG_PLUGIN_ERROR),
894 "#leak-file-handles" };
895 static const struct text_message message={ lines, 2 };
896 button_clear_queue(); /* Empty the keyboard buffer */
897 gui_syncyesno_run(&message, NULL, NULL);
899 for(fd=0; fd < MAX_OPEN_FILES; fd++)
900 if(open_files & (1<<fd))
901 close_wrapper(fd);
903 #endif
905 sim_plugin_close(pd);
907 if (rc == PLUGIN_ERROR)
908 splash(HZ*2, str(LANG_PLUGIN_ERROR));
910 return rc;
913 /* Returns a pointer to the portion of the plugin buffer that is not already
914 being used. If no plugin is loaded, returns the entire plugin buffer */
915 void* plugin_get_buffer(size_t *buffer_size)
917 int buffer_pos;
919 if (plugin_loaded)
921 if (plugin_size >= PLUGIN_BUFFER_SIZE)
922 return NULL;
924 *buffer_size = PLUGIN_BUFFER_SIZE-plugin_size;
925 buffer_pos = plugin_size;
927 else
929 *buffer_size = PLUGIN_BUFFER_SIZE;
930 buffer_pos = 0;
933 return &pluginbuf[buffer_pos];
936 /* Returns a pointer to the mp3 buffer.
937 Playback gets stopped, to avoid conflicts.
938 Talk buffer is stolen as well.
940 void* plugin_get_audio_buffer(size_t *buffer_size)
942 #if CONFIG_CODEC == SWCODEC
943 return audio_get_buffer(true, buffer_size);
944 #else
945 audio_stop();
946 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
947 *buffer_size = audiobufend - audiobuf;
948 return audiobuf;
949 #endif
952 #ifdef PLUGIN_USE_IRAM
953 /* Initializes plugin IRAM */
954 void plugin_iram_init(char *iramstart, char *iramcopy, size_t iram_size,
955 char *iedata, size_t iedata_size)
957 /* We need to stop audio playback in order to use codec IRAM */
958 audio_hard_stop();
959 memcpy(iramstart, iramcopy, iram_size);
960 memset(iedata, 0, iedata_size);
961 memset(iramcopy, 0, iram_size);
962 #if NUM_CORES > 1
963 /* writeback cleared iedata and iramcopy areas */
964 cpucache_flush();
965 #endif
967 #endif /* PLUGIN_USE_IRAM */
969 /* The plugin wants to stay resident after leaving its main function, e.g.
970 runs from timer or own thread. The callback is registered to later
971 instruct it to free its resources before a new plugin gets loaded. */
972 void plugin_tsr(bool (*exit_callback)(bool))
974 pfn_tsr_exit = exit_callback; /* remember the callback for later */
977 char *plugin_get_current_filename(void)
979 return current_plugin;
982 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
983 static int open_wrapper(const char* pathname, int flags)
985 int fd = PREFIX(open)(pathname,flags);
987 if(fd >= 0)
988 open_files |= 1<<fd;
990 return fd;
993 static int close_wrapper(int fd)
995 if((~open_files) & (1<<fd))
997 logf("double close from plugin");
999 if(fd >= 0)
1000 open_files &= (~(1<<fd));
1002 return PREFIX(close)(fd);
1005 static int creat_wrapper(const char *pathname)
1007 int fd = PREFIX(creat)(pathname);
1009 if(fd >= 0)
1010 open_files |= (1<<fd);
1012 return fd;
1014 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */