One more *_filesize() to remove
[maemo-rb.git] / apps / plugin.c
blob5406610e23a01878eb330e9bb011715521d40150
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 "backlight.h"
31 #include "sound_menu.h"
32 #include "mp3data.h"
33 #include "powermgmt.h"
34 #include "splash.h"
35 #include "logf.h"
36 #include "option_select.h"
37 #include "talk.h"
38 #include "version.h"
39 #include "storage.h"
40 #include "pcmbuf.h"
41 #include "errno.h"
42 #include "diacritic.h"
43 #include "filefuncs.h"
44 #include "load_code.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 #if defined (SIMULATOR)
62 #define PREFIX(_x_) sim_ ## _x_
63 #elif defined (APPLICATION)
64 #define PREFIX(_x_) app_ ## _x_
65 #else
66 #define PREFIX
67 #endif
69 #if defined (APPLICATION)
70 /* For symmetry reasons (we want app_ and sim_ to behave similarly), some
71 * wrappers are needed */
72 static int app_close(int fd)
74 return close(fd);
77 static ssize_t app_read(int fd, void *buf, size_t count)
79 return read(fd,buf,count);
82 static off_t app_lseek(int fd, off_t offset, int whence)
84 return lseek(fd,offset,whence);
87 static ssize_t app_write(int fd, const void *buf, size_t count)
89 return write(fd,buf,count);
92 static int app_ftruncate(int fd, off_t length)
94 return ftruncate(fd,length);
96 #endif
98 #if defined(HAVE_PLUGIN_CHECK_OPEN_CLOSE) && (MAX_OPEN_FILES>32)
99 #warning "MAX_OPEN_FILES>32, disabling plugin file open/close checking"
100 #undef HAVE_PLUGIN_CHECK_OPEN_CLOSE
101 #endif
103 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
104 static unsigned int open_files;
105 #endif
107 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
108 static unsigned char pluginbuf[PLUGIN_BUFFER_SIZE];
109 void sim_lcd_ex_init(unsigned long (*getpixel)(int, int));
110 void sim_lcd_ex_update_rect(int x, int y, int width, int height);
111 #else
112 extern unsigned char pluginbuf[];
113 #include "bitswap.h"
114 #endif
116 /* for actual plugins only, not for codecs */
117 static int plugin_size = 0;
118 static bool (*pfn_tsr_exit)(bool reenter) = NULL; /* TSR exit callback */
119 static char current_plugin[MAX_PATH];
120 /* NULL if no plugin is loaded, otherwise the handle that lc_open() returned */
121 static void *current_plugin_handle;
123 char *plugin_get_current_filename(void);
125 /* Some wrappers used to monitor open and close and detect leaks*/
126 static int open_wrapper(const char* pathname, int flags, ...);
127 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
128 static int close_wrapper(int fd);
129 static int creat_wrapper(const char *pathname, mode_t mode);
130 #endif
132 static const struct plugin_api rockbox_api = {
134 /* lcd */
135 #ifdef HAVE_LCD_CONTRAST
136 lcd_set_contrast,
137 #endif
138 lcd_update,
139 lcd_clear_display,
140 lcd_getstringsize,
141 lcd_putsxy,
142 lcd_putsxyf,
143 lcd_puts,
144 lcd_putsf,
145 lcd_puts_scroll,
146 lcd_stop_scroll,
147 #ifdef HAVE_LCD_CHARCELLS
148 lcd_define_pattern,
149 lcd_get_locked_pattern,
150 lcd_unlock_pattern,
151 lcd_putc,
152 lcd_put_cursor,
153 lcd_remove_cursor,
154 lcd_icon,
155 lcd_double_height,
156 #else
157 &lcd_static_framebuffer[0][0],
158 lcd_update_rect,
159 lcd_set_drawmode,
160 lcd_get_drawmode,
161 screen_helper_setfont,
162 lcd_drawpixel,
163 lcd_drawline,
164 lcd_hline,
165 lcd_vline,
166 lcd_drawrect,
167 lcd_fillrect,
168 lcd_mono_bitmap_part,
169 lcd_mono_bitmap,
170 #if LCD_DEPTH > 1
171 lcd_set_foreground,
172 lcd_get_foreground,
173 lcd_set_background,
174 lcd_get_background,
175 lcd_bitmap_part,
176 lcd_bitmap,
177 lcd_get_backdrop,
178 lcd_set_backdrop,
179 #endif
180 #if LCD_DEPTH == 16
181 lcd_bitmap_transparent_part,
182 lcd_bitmap_transparent,
183 #if MEMORYSIZE > 2
184 lcd_blit_yuv,
185 #if defined(TOSHIBA_GIGABEAT_F) || defined(SANSA_E200) || defined(SANSA_C200) \
186 || defined(IRIVER_H10) || defined(COWON_D2) || defined(PHILIPS_HDD1630) \
187 || defined(SANSA_FUZE) || defined(SANSA_E200V2) || defined(SANSA_FUZEV2) \
188 || defined(TOSHIBA_GIGABEAT_S) || defined(PHILIPS_SA9200)
189 lcd_yuv_set_options,
190 #endif
191 #endif /* MEMORYSIZE > 2 */
192 #elif (LCD_DEPTH < 4) && (CONFIG_PLATFORM & PLATFORM_NATIVE)
193 lcd_blit_mono,
194 lcd_blit_grey_phase,
195 #endif /* LCD_DEPTH */
196 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
197 lcd_blit_pal256,
198 lcd_pal256_update_pal,
199 #endif
200 lcd_puts_style,
201 lcd_puts_scroll_style,
202 #ifdef HAVE_LCD_INVERT
203 lcd_set_invert_display,
204 #endif /* HAVE_LCD_INVERT */
205 #if defined(HAVE_LCD_MODES)
206 lcd_set_mode,
207 #endif
208 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
209 &button_queue,
210 #endif
211 bidi_l2v,
212 #ifdef HAVE_LCD_BITMAP
213 is_diacritic,
214 #endif
215 font_get_bits,
216 font_load,
217 font_unload,
218 font_get,
219 font_getstringsize,
220 font_get_width,
221 screen_clear_area,
222 gui_scrollbar_draw,
223 #endif /* HAVE_LCD_BITMAP */
224 get_codepage_name,
226 backlight_on,
227 backlight_off,
228 backlight_set_timeout,
229 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
230 backlight_set_brightness,
231 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
233 #if CONFIG_CHARGING
234 backlight_set_timeout_plugged,
235 #endif
236 is_backlight_on,
237 splash,
238 splashf,
240 #ifdef HAVE_REMOTE_LCD
241 /* remote lcd */
242 lcd_remote_set_contrast,
243 lcd_remote_clear_display,
244 lcd_remote_puts,
245 lcd_remote_puts_scroll,
246 lcd_remote_stop_scroll,
247 lcd_remote_set_drawmode,
248 lcd_remote_get_drawmode,
249 lcd_remote_setfont,
250 lcd_remote_getstringsize,
251 lcd_remote_drawpixel,
252 lcd_remote_drawline,
253 lcd_remote_hline,
254 lcd_remote_vline,
255 lcd_remote_drawrect,
256 lcd_remote_fillrect,
257 lcd_remote_mono_bitmap_part,
258 lcd_remote_mono_bitmap,
259 lcd_remote_putsxy,
260 lcd_remote_puts_style,
261 lcd_remote_puts_scroll_style,
262 &lcd_remote_static_framebuffer[0][0],
263 lcd_remote_update,
264 lcd_remote_update_rect,
266 remote_backlight_on,
267 remote_backlight_off,
268 remote_backlight_set_timeout,
269 #if CONFIG_CHARGING
270 remote_backlight_set_timeout_plugged,
271 #endif
272 #endif /* HAVE_REMOTE_LCD */
273 #if NB_SCREENS == 2
274 {&screens[SCREEN_MAIN], &screens[SCREEN_REMOTE]},
275 #else
276 {&screens[SCREEN_MAIN]},
277 #endif
278 #if defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
279 lcd_remote_set_foreground,
280 lcd_remote_get_foreground,
281 lcd_remote_set_background,
282 lcd_remote_get_background,
283 lcd_remote_bitmap_part,
284 lcd_remote_bitmap,
285 #endif
286 viewport_set_defaults,
287 #ifdef HAVE_LCD_BITMAP
288 viewportmanager_theme_enable,
289 viewportmanager_theme_undo,
290 #endif
292 /* list */
293 gui_synclist_init,
294 gui_synclist_set_nb_items,
295 gui_synclist_set_icon_callback,
296 gui_synclist_get_nb_items,
297 gui_synclist_get_sel_pos,
298 gui_synclist_draw,
299 gui_synclist_select_item,
300 gui_synclist_add_item,
301 gui_synclist_del_item,
302 gui_synclist_limit_scroll,
303 gui_synclist_do_button,
304 gui_synclist_set_title,
305 gui_syncyesno_run,
306 simplelist_info_init,
307 simplelist_show_list,
309 /* button */
310 button_get,
311 button_get_w_tmo,
312 button_status,
313 #ifdef HAVE_BUTTON_DATA
314 button_get_data,
315 button_status_wdata,
316 #endif
317 button_clear_queue,
318 button_queue_count,
319 #ifdef HAS_BUTTON_HOLD
320 button_hold,
321 #endif
322 #ifdef HAVE_TOUCHSCREEN
323 touchscreen_set_mode,
324 touchscreen_get_mode,
325 #endif
327 #ifdef HAVE_BUTTON_LIGHT
328 buttonlight_set_timeout,
329 buttonlight_off,
330 buttonlight_on,
331 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
332 buttonlight_set_brightness,
333 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
334 #endif /* HAVE_BUTTON_LIGHT */
336 /* file */
337 open_utf8,
338 (open_func)open_wrapper,
339 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
340 close_wrapper,
341 #else
342 PREFIX(close),
343 #endif
344 (read_func)PREFIX(read),
345 PREFIX(lseek),
346 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
347 (creat_func)creat_wrapper,
348 #else
349 PREFIX(creat),
350 #endif
351 (write_func)PREFIX(write),
352 PREFIX(remove),
353 PREFIX(rename),
354 PREFIX(ftruncate),
355 filesize,
356 fdprintf,
357 read_line,
358 settings_parseline,
359 storage_sleep,
360 storage_spin,
361 storage_spindown,
362 #if USING_STORAGE_CALLBACK
363 register_storage_idle_func,
364 unregister_storage_idle_func,
365 #endif /* USING_STORAGE_CALLBACK */
366 reload_directory,
367 create_numbered_filename,
368 file_exists,
369 strip_extension,
370 crc_32,
371 filetype_get_attr,
373 /* dir */
374 (opendir_func)opendir,
375 (closedir_func)closedir,
376 (readdir_func)readdir,
377 mkdir,
378 rmdir,
379 dir_exists,
380 dir_get_info,
382 /* browsing */
383 browse_context_init,
384 rockbox_browse,
386 /* kernel/ system */
387 #if defined(CPU_ARM) && CONFIG_PLATFORM & PLATFORM_NATIVE
388 __div0,
389 #endif
390 sleep,
391 yield,
392 &current_tick,
393 default_event_handler,
394 default_event_handler_ex,
395 create_thread,
396 thread_self,
397 thread_exit,
398 thread_wait,
399 #if (CONFIG_CODEC == SWCODEC)
400 thread_thaw,
401 #ifdef HAVE_PRIORITY_SCHEDULING
402 thread_set_priority,
403 #endif
404 mutex_init,
405 mutex_lock,
406 mutex_unlock,
407 #endif
409 reset_poweroff_timer,
410 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
411 system_memory_guard,
412 &cpu_frequency,
414 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
415 #ifdef CPU_BOOST_LOGGING
416 cpu_boost_,
417 #else
418 cpu_boost,
419 #endif
420 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
421 #endif /* PLATFORM_NATIVE */
422 #ifdef HAVE_SCHEDULER_BOOSTCTRL
423 trigger_cpu_boost,
424 cancel_cpu_boost,
425 #endif
427 commit_dcache,
428 commit_discard_dcache,
429 commit_discard_idcache,
431 lc_open,
432 lc_open_from_mem,
433 lc_get_header,
434 lc_close,
436 timer_register,
437 timer_unregister,
438 timer_set_period,
440 queue_init,
441 queue_delete,
442 queue_post,
443 queue_wait_w_tmo,
444 #if CONFIG_CODEC == SWCODEC
445 queue_enable_queue_send,
446 queue_empty,
447 queue_wait,
448 queue_send,
449 queue_reply,
450 #endif
451 usb_acknowledge,
452 #ifdef USB_ENABLE_HID
453 usb_hid_send,
454 #endif
455 #ifdef RB_PROFILE
456 profile_thread,
457 profstop,
458 __cyg_profile_func_enter,
459 __cyg_profile_func_exit,
460 #endif
461 add_event,
462 remove_event,
463 send_event,
465 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
466 /* special simulator hooks */
467 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
468 sim_lcd_ex_init,
469 sim_lcd_ex_update_rect,
470 #endif
471 #endif
473 /* strings and memory */
474 snprintf,
475 vsnprintf,
476 strcpy,
477 strlcpy,
478 strlen,
479 strrchr,
480 strcmp,
481 strncmp,
482 strcasecmp,
483 strncasecmp,
484 memset,
485 memcpy,
486 memmove,
487 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
488 _ctype_,
489 #endif
490 atoi,
491 strchr,
492 strcat,
493 strlcat,
494 memchr,
495 memcmp,
496 strcasestr,
497 strtok_r,
498 /* unicode stuff */
499 utf8decode,
500 iso_decode,
501 utf16LEdecode,
502 utf16BEdecode,
503 utf8encode,
504 utf8length,
505 utf8seek,
507 /* the buflib memory management library */
508 buflib_init,
509 buflib_available,
510 buflib_alloc,
511 buflib_alloc_ex,
512 buflib_alloc_maximum,
513 buflib_buffer_in,
514 buflib_buffer_out,
515 buflib_free,
516 buflib_shrink,
517 buflib_get_data,
518 buflib_get_name,
520 /* sound */
521 sound_set,
522 sound_default,
523 sound_min,
524 sound_max,
525 sound_unit,
526 sound_val2phys,
527 #ifdef AUDIOHW_HAVE_EQ
528 sound_enum_hw_eq_band_setting,
529 #endif
530 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
531 mp3_play_data,
532 mp3_play_pause,
533 mp3_play_stop,
534 mp3_is_playing,
535 #if CONFIG_CODEC != SWCODEC
536 bitswap,
537 #endif
538 #endif
539 #if CONFIG_CODEC == SWCODEC
540 &audio_master_sampr_list[0],
541 &hw_freq_sampr[0],
542 pcm_apply_settings,
543 pcm_play_data,
544 pcm_play_stop,
545 pcm_set_frequency,
546 pcm_is_playing,
547 pcm_is_paused,
548 pcm_play_pause,
549 pcm_get_bytes_waiting,
550 pcm_calculate_peaks,
551 pcm_get_peak_buffer,
552 pcm_play_lock,
553 pcm_play_unlock,
554 beep_play,
555 #ifdef HAVE_RECORDING
556 &rec_freq_sampr[0],
557 pcm_init_recording,
558 pcm_close_recording,
559 pcm_record_data,
560 pcm_stop_recording,
561 pcm_calculate_rec_peaks,
562 audio_set_recording_gain,
563 #endif /* HAVE_RECORDING */
564 #if INPUT_SRC_CAPS != 0
565 audio_set_output_source,
566 audio_set_input_source,
567 #endif
568 dsp_set_crossfeed,
569 dsp_set_eq,
570 dsp_dither_enable,
571 dsp_configure,
572 dsp_process,
573 dsp_input_count,
574 dsp_output_count,
576 mixer_channel_status,
577 mixer_channel_get_buffer,
578 mixer_channel_calculate_peaks,
579 mixer_channel_play_data,
580 mixer_channel_play_pause,
581 mixer_channel_stop,
582 mixer_channel_set_amplitude,
583 mixer_channel_get_bytes_waiting,
585 system_sound_play,
586 keyclick_click,
587 #endif
588 /* playback control */
589 playlist_amount,
590 playlist_resume,
591 playlist_start,
592 playlist_add,
593 playlist_sync,
594 playlist_remove_all_tracks,
595 playlist_create,
596 playlist_insert_track,
597 playlist_insert_directory,
598 playlist_shuffle,
599 audio_play,
600 audio_stop,
601 audio_pause,
602 audio_resume,
603 audio_next,
604 audio_prev,
605 audio_ff_rewind,
606 audio_next_track,
607 audio_status,
608 audio_current_track,
609 audio_flush_and_reload_tracks,
610 audio_get_file_pos,
611 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
612 mpeg_get_last_header,
613 #endif
614 #if ((CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
615 (CONFIG_CODEC == SWCODEC)) && defined (HAVE_PITCHSCREEN)
616 sound_set_pitch,
617 #endif
619 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
620 /* MAS communication */
621 mas_readmem,
622 mas_writemem,
623 mas_readreg,
624 mas_writereg,
625 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
626 mas_codec_writereg,
627 mas_codec_readreg,
628 i2c_begin,
629 i2c_end,
630 i2c_write,
631 #endif
632 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
634 /* menu */
635 do_menu,
636 /* statusbars */
637 &statusbars,
638 gui_syncstatusbar_draw,
640 /* options */
641 get_settings_list,
642 find_setting,
643 option_screen,
644 set_option,
645 set_bool_options,
646 set_int,
647 set_bool,
648 #ifdef HAVE_LCD_COLOR
649 set_color,
650 #endif
652 /* action handling */
653 get_custom_action,
654 get_action,
655 #ifdef HAVE_TOUCHSCREEN
656 action_get_touchscreen_press,
657 #endif
658 action_userabort,
660 /* power */
661 battery_level,
662 battery_level_safe,
663 battery_time,
664 battery_voltage,
665 #if CONFIG_CHARGING
666 charger_inserted,
667 # if CONFIG_CHARGING >= CHARGING_MONITOR
668 charging_state,
669 # endif
670 #endif
671 #ifdef HAVE_USB_POWER
672 usb_powered,
673 #endif
675 /* misc */
676 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
677 &errno,
678 #endif
679 srand,
680 rand,
681 (qsort_func)qsort,
682 kbd_input,
683 get_time,
684 set_time,
685 #if CONFIG_RTC
686 mktime,
687 #endif
688 plugin_get_buffer,
689 plugin_get_audio_buffer,
690 plugin_tsr,
691 plugin_get_current_filename,
692 #if defined(DEBUG) || defined(SIMULATOR)
693 debugf,
694 #endif
695 #ifdef ROCKBOX_HAS_LOGF
696 _logf,
697 #endif
698 &global_settings,
699 &global_status,
700 talk_disable,
701 #if CONFIG_CODEC == SWCODEC
702 codec_thread_do_callback,
703 codec_load_file,
704 codec_run_proc,
705 codec_close,
706 get_codec_filename,
707 find_array_ptr,
708 remove_array_ptr,
709 round_value_to_list32,
710 #endif /* CONFIG_CODEC == SWCODEC */
711 get_metadata,
712 mp3info,
713 count_mp3_frames,
714 create_xing_header,
715 find_next_frame,
716 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
717 peak_meter_scale_value,
718 peak_meter_set_use_dbfs,
719 peak_meter_get_use_dbfs,
720 #endif
721 #ifdef HAVE_LCD_BITMAP
722 read_bmp_file,
723 read_bmp_fd,
724 #ifdef HAVE_JPEG
725 read_jpeg_file,
726 read_jpeg_fd,
727 #endif
728 screen_dump_set_hook,
729 #endif
730 show_logo,
731 tree_get_context,
732 tree_get_entries,
733 tree_get_entry_at,
734 set_current_file,
735 set_dirfilter,
737 #ifdef HAVE_WHEEL_POSITION
738 wheel_status,
739 wheel_send_events,
740 #endif
742 #ifdef IRIVER_H100_SERIES
743 /* Routines for the iriver_flash -plugin. */
744 detect_original_firmware,
745 detect_flashed_ramimage,
746 detect_flashed_romimage,
747 #endif
748 led,
749 #if (CONFIG_CODEC == SWCODEC)
750 bufopen,
751 bufalloc,
752 bufclose,
753 bufseek,
754 bufadvance,
755 bufread,
756 bufgetdata,
757 bufgettail,
758 bufcuttail,
760 buf_handle_offset,
761 buf_set_base_handle,
762 buf_used,
763 #endif
765 #ifdef HAVE_TAGCACHE
766 tagcache_search,
767 tagcache_search_set_uniqbuf,
768 tagcache_search_add_filter,
769 tagcache_get_next,
770 tagcache_retrieve,
771 tagcache_search_finish,
772 tagcache_get_numeric,
773 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
774 tagcache_fill_tags,
775 #endif
776 #endif
778 #ifdef HAVE_ALBUMART
779 search_albumart_files,
780 #endif
782 #ifdef HAVE_SEMAPHORE_OBJECTS
783 semaphore_init,
784 semaphore_wait,
785 semaphore_release,
786 #endif
788 rbversion,
790 /* new stuff at the end, sort into place next time
791 the API gets incompatible */
794 int plugin_load(const char* plugin, const void* parameter)
796 struct plugin_header *p_hdr;
797 struct lc_header *hdr;
799 if (current_plugin_handle && pfn_tsr_exit)
800 { /* if we have a resident old plugin and a callback */
801 if (pfn_tsr_exit(!strcmp(current_plugin, plugin)) == false )
803 /* not allowing another plugin to load */
804 return PLUGIN_OK;
806 lc_close(current_plugin_handle);
807 current_plugin_handle = pfn_tsr_exit = NULL;
810 splash(0, ID2P(LANG_WAIT));
811 strcpy(current_plugin, plugin);
813 current_plugin_handle = lc_open(plugin, pluginbuf, PLUGIN_BUFFER_SIZE);
814 if (current_plugin_handle == NULL) {
815 splashf(HZ*2, str(LANG_PLUGIN_CANT_OPEN), plugin);
816 return -1;
819 p_hdr = lc_get_header(current_plugin_handle);
821 hdr = p_hdr ? &p_hdr->lc_hdr : NULL;
824 if (hdr == NULL
825 || hdr->magic != PLUGIN_MAGIC
826 || hdr->target_id != TARGET_ID
827 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
828 || hdr->load_addr != pluginbuf
829 || hdr->end_addr > pluginbuf + PLUGIN_BUFFER_SIZE
830 #endif
833 lc_close(current_plugin_handle);
834 splash(HZ*2, str(LANG_PLUGIN_WRONG_MODEL));
835 return -1;
837 if (hdr->api_version > PLUGIN_API_VERSION
838 || hdr->api_version < PLUGIN_MIN_API_VERSION)
840 lc_close(current_plugin_handle);
841 splash(HZ*2, str(LANG_PLUGIN_WRONG_VERSION));
842 return -1;
844 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
845 plugin_size = hdr->end_addr - pluginbuf;
846 #else
847 plugin_size = 0;
848 #endif
850 *(p_hdr->api) = &rockbox_api;
852 lcd_clear_display();
853 lcd_update();
855 #ifdef HAVE_REMOTE_LCD
856 lcd_remote_clear_display();
857 lcd_remote_update();
858 #endif
859 push_current_activity(ACTIVITY_PLUGIN);
860 /* some plugins assume the entry cache doesn't move and save pointers to it
861 * they should be fixed properly instead of this lock */
862 tree_lock_cache(tree_get_context());
864 FOR_NB_SCREENS(i)
865 viewportmanager_theme_enable(i, false, NULL);
867 #ifdef HAVE_TOUCHSCREEN
868 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
869 #endif
871 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
872 open_files = 0;
873 #endif
875 int rc = p_hdr->entry_point(parameter);
877 tree_unlock_cache(tree_get_context());
878 pop_current_activity();
880 if (!pfn_tsr_exit)
881 { /* close handle if plugin is no tsr one */
882 lc_close(current_plugin_handle);
883 current_plugin_handle = NULL;
886 /* Go back to the global setting in case the plugin changed it */
887 #ifdef HAVE_TOUCHSCREEN
888 touchscreen_set_mode(global_settings.touch_mode);
889 #endif
891 #ifdef HAVE_LCD_BITMAP
892 screen_helper_setfont(FONT_UI);
893 #if LCD_DEPTH > 1
894 #ifdef HAVE_LCD_COLOR
895 lcd_set_drawinfo(DRMODE_SOLID, global_settings.fg_color,
896 global_settings.bg_color);
897 #else
898 lcd_set_drawinfo(DRMODE_SOLID, LCD_DEFAULT_FG, LCD_DEFAULT_BG);
899 #endif
900 #else /* LCD_DEPTH == 1 */
901 lcd_set_drawmode(DRMODE_SOLID);
902 #endif /* LCD_DEPTH */
903 #endif /* HAVE_LCD_BITMAP */
906 #ifdef HAVE_REMOTE_LCD
907 #if LCD_REMOTE_DEPTH > 1
908 lcd_remote_set_drawinfo(DRMODE_SOLID, LCD_REMOTE_DEFAULT_FG,
909 LCD_REMOTE_DEFAULT_BG);
910 #else
911 lcd_remote_set_drawmode(DRMODE_SOLID);
912 #endif
913 #endif
915 lcd_clear_display();
916 #ifdef HAVE_REMOTE_LCD
917 lcd_remote_clear_display();
918 #endif
920 FOR_NB_SCREENS(i)
921 viewportmanager_theme_undo(i, true);
923 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
924 if(open_files != 0 && !current_plugin_handle)
926 int fd;
927 logf("Plugin '%s' leaks file handles", plugin);
929 static const char *lines[] =
930 { ID2P(LANG_PLUGIN_ERROR),
931 "#leak-file-handles" };
932 static const struct text_message message={ lines, 2 };
933 button_clear_queue(); /* Empty the keyboard buffer */
934 gui_syncyesno_run(&message, NULL, NULL);
936 for(fd=0; fd < MAX_OPEN_FILES; fd++)
937 if(open_files & (1<<fd))
938 close_wrapper(fd);
940 #endif
942 if (rc == PLUGIN_ERROR)
943 splash(HZ*2, str(LANG_PLUGIN_ERROR));
945 return rc;
948 /* Returns a pointer to the portion of the plugin buffer that is not already
949 being used. If no plugin is loaded, returns the entire plugin buffer */
950 void* plugin_get_buffer(size_t *buffer_size)
952 int buffer_pos;
954 if (current_plugin_handle)
956 if (plugin_size >= PLUGIN_BUFFER_SIZE)
957 return NULL;
959 *buffer_size = PLUGIN_BUFFER_SIZE-plugin_size;
960 buffer_pos = plugin_size;
962 else
964 *buffer_size = PLUGIN_BUFFER_SIZE;
965 buffer_pos = 0;
968 return &pluginbuf[buffer_pos];
971 /* Returns a pointer to the mp3 buffer.
972 Playback gets stopped, to avoid conflicts.
973 Talk buffer is stolen as well.
975 void* plugin_get_audio_buffer(size_t *buffer_size)
977 audio_stop();
978 return audio_get_buffer(true, buffer_size);
981 /* The plugin wants to stay resident after leaving its main function, e.g.
982 runs from timer or own thread. The callback is registered to later
983 instruct it to free its resources before a new plugin gets loaded. */
984 void plugin_tsr(bool (*exit_callback)(bool))
986 pfn_tsr_exit = exit_callback; /* remember the callback for later */
989 char *plugin_get_current_filename(void)
991 return current_plugin;
994 static int open_wrapper(const char* pathname, int flags, ...)
996 /* we don't have an 'open' function. it's a define. and we need
997 * the real file_open, hence PREFIX() doesn't work here */
998 int fd;
999 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
1000 if (flags & O_CREAT)
1002 va_list ap;
1003 va_start(ap, flags);
1004 fd = open(pathname, flags, va_arg(ap, unsigned int));
1005 va_end(ap);
1007 else
1008 fd = open(pathname, flags);
1009 #else
1010 fd = file_open(pathname,flags);
1011 #endif
1013 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1014 if(fd >= 0)
1015 open_files |= 1<<fd;
1016 #endif
1017 return fd;
1020 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1021 static int close_wrapper(int fd)
1023 if((~open_files) & (1<<fd))
1025 logf("double close from plugin");
1027 if(fd >= 0)
1028 open_files &= (~(1<<fd));
1030 return PREFIX(close)(fd);
1033 static int creat_wrapper(const char *pathname, mode_t mode)
1035 int fd = PREFIX(creat)(pathname, mode);
1037 if(fd >= 0)
1038 open_files |= (1<<fd);
1040 return fd;
1042 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */