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