Submit FS#12196. Adds support for embedded album art (jpg) with APEv2 tags.
[kugel-rb.git] / apps / plugin.c
blobb47dd951eaa1e721cb2da04a19638a6eeeec4b46
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 defined (SIMULATOR)
63 #define PREFIX(_x_) sim_ ## _x_
64 #elif defined (APPLICATION)
65 #define PREFIX(_x_) app_ ## _x_
66 #else
67 #define PREFIX
68 #endif
70 #if defined (APPLICATION)
71 /* For symmetry reasons (we want app_ and sim_ to behave similarly), some
72 * wrappers are needed */
73 static int app_close(int fd)
75 return close(fd);
78 static ssize_t app_read(int fd, void *buf, size_t count)
80 return read(fd,buf,count);
83 static off_t app_lseek(int fd, off_t offset, int whence)
85 return lseek(fd,offset,whence);
88 static ssize_t app_write(int fd, const void *buf, size_t count)
90 return write(fd,buf,count);
93 static int app_ftruncate(int fd, off_t length)
95 return ftruncate(fd,length);
98 static off_t app_filesize(int fd)
100 return filesize(fd);
103 static int app_closedir(DIR *dirp)
105 return closedir(dirp);
108 static struct dirent *app_readdir(DIR *dirp)
110 return readdir(dirp);
112 #endif
114 #if defined(HAVE_PLUGIN_CHECK_OPEN_CLOSE) && (MAX_OPEN_FILES>32)
115 #warning "MAX_OPEN_FILES>32, disabling plugin file open/close checking"
116 #undef HAVE_PLUGIN_CHECK_OPEN_CLOSE
117 #endif
119 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
120 static unsigned int open_files;
121 #endif
123 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
124 static unsigned char pluginbuf[PLUGIN_BUFFER_SIZE];
125 void sim_lcd_ex_init(unsigned long (*getpixel)(int, int));
126 void sim_lcd_ex_update_rect(int x, int y, int width, int height);
127 #else
128 extern unsigned char pluginbuf[];
129 #include "bitswap.h"
130 #endif
132 /* for actual plugins only, not for codecs */
133 static int plugin_size = 0;
134 static bool (*pfn_tsr_exit)(bool reenter) = NULL; /* TSR exit callback */
135 static char current_plugin[MAX_PATH];
136 /* NULL if no plugin is loaded, otherwise the handle that lc_open() returned */
137 static void *current_plugin_handle;
139 char *plugin_get_current_filename(void);
141 /* Some wrappers used to monitor open and close and detect leaks*/
142 static int open_wrapper(const char* pathname, int flags, ...);
143 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
144 static int close_wrapper(int fd);
145 static int creat_wrapper(const char *pathname, mode_t mode);
146 #endif
148 static const struct plugin_api rockbox_api = {
150 /* lcd */
151 #ifdef HAVE_LCD_CONTRAST
152 lcd_set_contrast,
153 #endif
154 lcd_update,
155 lcd_clear_display,
156 lcd_getstringsize,
157 lcd_putsxy,
158 lcd_putsxyf,
159 lcd_puts,
160 lcd_putsf,
161 lcd_puts_scroll,
162 lcd_stop_scroll,
163 #ifdef HAVE_LCD_CHARCELLS
164 lcd_define_pattern,
165 lcd_get_locked_pattern,
166 lcd_unlock_pattern,
167 lcd_putc,
168 lcd_put_cursor,
169 lcd_remove_cursor,
170 lcd_icon,
171 lcd_double_height,
172 #else
173 &lcd_framebuffer[0][0],
174 lcd_update_rect,
175 lcd_set_drawmode,
176 lcd_get_drawmode,
177 lcd_setfont,
178 lcd_drawpixel,
179 lcd_drawline,
180 lcd_hline,
181 lcd_vline,
182 lcd_drawrect,
183 lcd_fillrect,
184 lcd_mono_bitmap_part,
185 lcd_mono_bitmap,
186 #if LCD_DEPTH > 1
187 lcd_set_foreground,
188 lcd_get_foreground,
189 lcd_set_background,
190 lcd_get_background,
191 lcd_bitmap_part,
192 lcd_bitmap,
193 lcd_get_backdrop,
194 lcd_set_backdrop,
195 #endif
196 #if LCD_DEPTH == 16
197 lcd_bitmap_transparent_part,
198 lcd_bitmap_transparent,
199 #if MEMORYSIZE > 2
200 lcd_blit_yuv,
201 #if defined(TOSHIBA_GIGABEAT_F) || defined(SANSA_E200) || defined(SANSA_C200) \
202 || defined(IRIVER_H10) || defined(COWON_D2) || defined(PHILIPS_HDD1630) \
203 || defined(SANSA_FUZE) || defined(SANSA_E200V2) || defined(SANSA_FUZEV2) \
204 || defined(TOSHIBA_GIGABEAT_S) || defined(PHILIPS_SA9200)
205 lcd_yuv_set_options,
206 #endif
207 #endif /* MEMORYSIZE > 2 */
208 #elif (LCD_DEPTH < 4) && (CONFIG_PLATFORM & PLATFORM_NATIVE)
209 lcd_blit_mono,
210 lcd_blit_grey_phase,
211 #endif /* LCD_DEPTH */
212 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
213 lcd_blit_pal256,
214 lcd_pal256_update_pal,
215 #endif
216 lcd_puts_style,
217 lcd_puts_scroll_style,
218 #ifdef HAVE_LCD_INVERT
219 lcd_set_invert_display,
220 #endif /* HAVE_LCD_INVERT */
221 #if defined(HAVE_LCD_MODES)
222 lcd_set_mode,
223 #endif
224 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
225 &button_queue,
226 #endif
227 bidi_l2v,
228 #ifdef HAVE_LCD_BITMAP
229 is_diacritic,
230 #endif
231 font_get_bits,
232 font_load,
233 font_get,
234 font_getstringsize,
235 font_get_width,
236 screen_clear_area,
237 gui_scrollbar_draw,
238 #endif /* HAVE_LCD_BITMAP */
239 get_codepage_name,
241 backlight_on,
242 backlight_off,
243 backlight_set_timeout,
244 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
245 backlight_set_brightness,
246 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
248 #if CONFIG_CHARGING
249 backlight_set_timeout_plugged,
250 #endif
251 is_backlight_on,
252 splash,
253 splashf,
255 #ifdef HAVE_REMOTE_LCD
256 /* remote lcd */
257 lcd_remote_set_contrast,
258 lcd_remote_clear_display,
259 lcd_remote_puts,
260 lcd_remote_puts_scroll,
261 lcd_remote_stop_scroll,
262 lcd_remote_set_drawmode,
263 lcd_remote_get_drawmode,
264 lcd_remote_setfont,
265 lcd_remote_getstringsize,
266 lcd_remote_drawpixel,
267 lcd_remote_drawline,
268 lcd_remote_hline,
269 lcd_remote_vline,
270 lcd_remote_drawrect,
271 lcd_remote_fillrect,
272 lcd_remote_mono_bitmap_part,
273 lcd_remote_mono_bitmap,
274 lcd_remote_putsxy,
275 lcd_remote_puts_style,
276 lcd_remote_puts_scroll_style,
277 &lcd_remote_framebuffer[0][0],
278 lcd_remote_update,
279 lcd_remote_update_rect,
281 remote_backlight_on,
282 remote_backlight_off,
283 remote_backlight_set_timeout,
284 #if CONFIG_CHARGING
285 remote_backlight_set_timeout_plugged,
286 #endif
287 #endif /* HAVE_REMOTE_LCD */
288 #if NB_SCREENS == 2
289 {&screens[SCREEN_MAIN], &screens[SCREEN_REMOTE]},
290 #else
291 {&screens[SCREEN_MAIN]},
292 #endif
293 #if defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
294 lcd_remote_set_foreground,
295 lcd_remote_get_foreground,
296 lcd_remote_set_background,
297 lcd_remote_get_background,
298 lcd_remote_bitmap_part,
299 lcd_remote_bitmap,
300 #endif
301 viewport_set_defaults,
302 #ifdef HAVE_LCD_BITMAP
303 viewportmanager_theme_enable,
304 viewportmanager_theme_undo,
305 #endif
307 /* list */
308 gui_synclist_init,
309 gui_synclist_set_nb_items,
310 gui_synclist_set_icon_callback,
311 gui_synclist_get_nb_items,
312 gui_synclist_get_sel_pos,
313 gui_synclist_draw,
314 gui_synclist_select_item,
315 gui_synclist_add_item,
316 gui_synclist_del_item,
317 gui_synclist_limit_scroll,
318 gui_synclist_do_button,
319 gui_synclist_set_title,
320 gui_syncyesno_run,
321 simplelist_info_init,
322 simplelist_show_list,
324 /* button */
325 button_get,
326 button_get_w_tmo,
327 button_status,
328 #ifdef HAVE_BUTTON_DATA
329 button_get_data,
330 button_status_wdata,
331 #endif
332 button_clear_queue,
333 button_queue_count,
334 #ifdef HAS_BUTTON_HOLD
335 button_hold,
336 #endif
337 #ifdef HAVE_TOUCHSCREEN
338 touchscreen_set_mode,
339 #endif
341 #ifdef HAVE_BUTTON_LIGHT
342 buttonlight_set_timeout,
343 buttonlight_off,
344 buttonlight_on,
345 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
346 buttonlight_set_brightness,
347 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
348 #endif /* HAVE_BUTTON_LIGHT */
350 /* file */
351 open_utf8,
352 (open_func)open_wrapper,
353 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
354 close_wrapper,
355 #else
356 PREFIX(close),
357 #endif
358 (read_func)PREFIX(read),
359 PREFIX(lseek),
360 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
361 (creat_func)creat_wrapper,
362 #else
363 PREFIX(creat),
364 #endif
365 (write_func)PREFIX(write),
366 PREFIX(remove),
367 PREFIX(rename),
368 PREFIX(ftruncate),
369 PREFIX(filesize),
370 fdprintf,
371 read_line,
372 settings_parseline,
373 storage_sleep,
374 storage_spin,
375 storage_spindown,
376 #if USING_STORAGE_CALLBACK
377 register_storage_idle_func,
378 unregister_storage_idle_func,
379 #endif /* USING_STORAGE_CALLBACK */
380 reload_directory,
381 create_numbered_filename,
382 file_exists,
383 strip_extension,
384 crc_32,
385 filetype_get_attr,
387 /* dir */
388 (opendir_func)PREFIX(opendir),
389 (closedir_func)PREFIX(closedir),
390 (readdir_func)PREFIX(readdir),
391 PREFIX(mkdir),
392 PREFIX(rmdir),
393 dir_exists,
394 dir_get_info,
396 /* browsing */
397 browse_context_init,
398 rockbox_browse,
400 /* kernel/ system */
401 #if defined(CPU_ARM) && CONFIG_PLATFORM & PLATFORM_NATIVE
402 __div0,
403 #endif
404 sleep,
405 yield,
406 &current_tick,
407 default_event_handler,
408 default_event_handler_ex,
409 create_thread,
410 thread_self,
411 thread_exit,
412 thread_wait,
413 #if (CONFIG_CODEC == SWCODEC)
414 thread_thaw,
415 #ifdef HAVE_PRIORITY_SCHEDULING
416 thread_set_priority,
417 #endif
418 mutex_init,
419 mutex_lock,
420 mutex_unlock,
421 #endif
423 reset_poweroff_timer,
424 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
425 system_memory_guard,
426 &cpu_frequency,
428 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
429 #ifdef CPU_BOOST_LOGGING
430 cpu_boost_,
431 #else
432 cpu_boost,
433 #endif
434 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
435 #endif /* PLATFORM_NATIVE */
436 #ifdef HAVE_SCHEDULER_BOOSTCTRL
437 trigger_cpu_boost,
438 cancel_cpu_boost,
439 #endif
441 cpucache_flush,
442 cpucache_invalidate,
444 lc_open,
445 lc_open_from_mem,
446 lc_get_header,
447 lc_close,
449 timer_register,
450 timer_unregister,
451 timer_set_period,
453 queue_init,
454 queue_delete,
455 queue_post,
456 queue_wait_w_tmo,
457 #if CONFIG_CODEC == SWCODEC
458 queue_enable_queue_send,
459 queue_empty,
460 queue_wait,
461 queue_send,
462 queue_reply,
463 #endif
464 usb_acknowledge,
465 #ifdef USB_ENABLE_HID
466 usb_hid_send,
467 #endif
468 #ifdef RB_PROFILE
469 profile_thread,
470 profstop,
471 __cyg_profile_func_enter,
472 __cyg_profile_func_exit,
473 #endif
474 add_event,
475 remove_event,
476 send_event,
478 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
479 /* special simulator hooks */
480 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
481 sim_lcd_ex_init,
482 sim_lcd_ex_update_rect,
483 #endif
484 #endif
486 /* strings and memory */
487 snprintf,
488 vsnprintf,
489 strcpy,
490 strlcpy,
491 strlen,
492 strrchr,
493 strcmp,
494 strncmp,
495 strcasecmp,
496 strncasecmp,
497 memset,
498 memcpy,
499 memmove,
500 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
501 _ctype_,
502 #endif
503 atoi,
504 strchr,
505 strcat,
506 strlcat,
507 memchr,
508 memcmp,
509 strcasestr,
510 strtok_r,
511 /* unicode stuff */
512 utf8decode,
513 iso_decode,
514 utf16LEdecode,
515 utf16BEdecode,
516 utf8encode,
517 utf8length,
518 utf8seek,
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,
575 #endif /* CONFIG_CODEC == SWCODEC */
577 /* playback control */
578 playlist_amount,
579 playlist_resume,
580 playlist_start,
581 playlist_add,
582 playlist_sync,
583 playlist_remove_all_tracks,
584 playlist_create,
585 playlist_insert_track,
586 playlist_insert_directory,
587 playlist_shuffle,
588 audio_play,
589 audio_stop,
590 audio_pause,
591 audio_resume,
592 audio_next,
593 audio_prev,
594 audio_ff_rewind,
595 audio_next_track,
596 audio_status,
597 audio_current_track,
598 audio_flush_and_reload_tracks,
599 audio_get_file_pos,
600 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
601 mpeg_get_last_header,
602 #endif
603 #if ((CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
604 (CONFIG_CODEC == SWCODEC)) && defined (HAVE_PITCHSCREEN)
605 sound_set_pitch,
606 #endif
608 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
609 /* MAS communication */
610 mas_readmem,
611 mas_writemem,
612 mas_readreg,
613 mas_writereg,
614 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
615 mas_codec_writereg,
616 mas_codec_readreg,
617 i2c_begin,
618 i2c_end,
619 i2c_write,
620 #endif
621 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
623 /* menu */
624 do_menu,
625 /* statusbars */
626 &statusbars,
627 gui_syncstatusbar_draw,
629 /* options */
630 get_settings_list,
631 find_setting,
632 option_screen,
633 set_option,
634 set_bool_options,
635 set_int,
636 set_bool,
637 #ifdef HAVE_LCD_COLOR
638 set_color,
639 #endif
641 /* action handling */
642 get_custom_action,
643 get_action,
644 #ifdef HAVE_TOUCHSCREEN
645 action_get_touchscreen_press,
646 #endif
647 action_userabort,
649 /* power */
650 battery_level,
651 battery_level_safe,
652 battery_time,
653 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
654 battery_voltage,
655 #endif
656 #if CONFIG_CHARGING
657 charger_inserted,
658 # if CONFIG_CHARGING >= CHARGING_MONITOR
659 charging_state,
660 # endif
661 #endif
662 #ifdef HAVE_USB_POWER
663 usb_powered,
664 #endif
666 /* misc */
667 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
668 &errno,
669 #endif
670 srand,
671 rand,
672 (qsort_func)qsort,
673 kbd_input,
674 get_time,
675 set_time,
676 #if CONFIG_RTC
677 mktime,
678 #endif
679 plugin_get_buffer,
680 plugin_get_audio_buffer,
681 plugin_tsr,
682 plugin_get_current_filename,
683 #if defined(DEBUG) || defined(SIMULATOR)
684 debugf,
685 #endif
686 #ifdef ROCKBOX_HAS_LOGF
687 _logf,
688 #endif
689 &global_settings,
690 &global_status,
691 talk_disable,
692 #if CONFIG_CODEC == SWCODEC
693 codec_thread_do_callback,
694 codec_load_file,
695 codec_run_proc,
696 codec_close,
697 get_codec_filename,
698 find_array_ptr,
699 remove_array_ptr,
700 round_value_to_list32,
701 #endif /* CONFIG_CODEC == SWCODEC */
702 get_metadata,
703 mp3info,
704 count_mp3_frames,
705 create_xing_header,
706 find_next_frame,
707 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
708 peak_meter_scale_value,
709 peak_meter_set_use_dbfs,
710 peak_meter_get_use_dbfs,
711 #endif
712 #ifdef HAVE_LCD_BITMAP
713 read_bmp_file,
714 read_bmp_fd,
715 #ifdef HAVE_JPEG
716 read_jpeg_file,
717 read_jpeg_fd,
718 #endif
719 screen_dump_set_hook,
720 #endif
721 show_logo,
722 tree_get_context,
723 set_current_file,
724 set_dirfilter,
726 #ifdef HAVE_WHEEL_POSITION
727 wheel_status,
728 wheel_send_events,
729 #endif
731 #ifdef IRIVER_H100_SERIES
732 /* Routines for the iriver_flash -plugin. */
733 detect_original_firmware,
734 detect_flashed_ramimage,
735 detect_flashed_romimage,
736 #endif
737 led,
738 #if (CONFIG_CODEC == SWCODEC)
739 bufopen,
740 bufalloc,
741 bufclose,
742 bufseek,
743 bufadvance,
744 bufread,
745 bufgetdata,
746 bufgettail,
747 bufcuttail,
749 buf_handle_offset,
750 buf_set_base_handle,
751 buf_used,
752 #endif
754 #ifdef HAVE_TAGCACHE
755 tagcache_search,
756 tagcache_search_set_uniqbuf,
757 tagcache_search_add_filter,
758 tagcache_get_next,
759 tagcache_retrieve,
760 tagcache_search_finish,
761 tagcache_get_numeric,
762 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
763 tagcache_fill_tags,
764 #endif
765 #endif
767 #ifdef HAVE_ALBUMART
768 search_albumart_files,
769 #endif
771 #ifdef HAVE_SEMAPHORE_OBJECTS
772 semaphore_init,
773 semaphore_wait,
774 semaphore_release,
775 #endif
777 rbversion,
779 /* new stuff at the end, sort into place next time
780 the API gets incompatible */
781 #if CONFIG_CODEC == SWCODEC
782 mixer_channel_status,
783 mixer_channel_get_buffer,
784 mixer_channel_calculate_peaks,
785 mixer_channel_play_data,
786 mixer_channel_play_pause,
787 mixer_channel_stop,
788 mixer_channel_set_amplitude,
789 mixer_channel_get_bytes_waiting,
790 system_sound_play,
791 keyclick_click,
792 #endif
795 int plugin_load(const char* plugin, const void* parameter)
797 int rc, i;
798 struct plugin_header *p_hdr;
799 struct lc_header *hdr;
801 #if LCD_DEPTH > 1
802 fb_data* old_backdrop;
803 #endif
805 if (current_plugin_handle && pfn_tsr_exit)
806 { /* if we have a resident old plugin and a callback */
807 if (pfn_tsr_exit(!strcmp(current_plugin, plugin)) == false )
809 /* not allowing another plugin to load */
810 return PLUGIN_OK;
812 lc_close(current_plugin_handle);
813 current_plugin_handle = pfn_tsr_exit = NULL;
816 splash(0, ID2P(LANG_WAIT));
817 strcpy(current_plugin, plugin);
819 current_plugin_handle = lc_open(plugin, pluginbuf, PLUGIN_BUFFER_SIZE);
820 if (current_plugin_handle == NULL) {
821 splashf(HZ*2, str(LANG_PLUGIN_CANT_OPEN), plugin);
822 return -1;
825 p_hdr = lc_get_header(current_plugin_handle);
827 hdr = p_hdr ? &p_hdr->lc_hdr : NULL;
830 if (hdr == NULL
831 || hdr->magic != PLUGIN_MAGIC
832 || hdr->target_id != TARGET_ID
833 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
834 || hdr->load_addr != pluginbuf
835 || hdr->end_addr > pluginbuf + PLUGIN_BUFFER_SIZE
836 #endif
839 lc_close(current_plugin_handle);
840 splash(HZ*2, str(LANG_PLUGIN_WRONG_MODEL));
841 return -1;
843 if (hdr->api_version > PLUGIN_API_VERSION
844 || hdr->api_version < PLUGIN_MIN_API_VERSION)
846 lc_close(current_plugin_handle);
847 splash(HZ*2, str(LANG_PLUGIN_WRONG_VERSION));
848 return -1;
850 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
851 plugin_size = hdr->end_addr - pluginbuf;
852 #else
853 plugin_size = 0;
854 #endif
856 *(p_hdr->api) = &rockbox_api;
858 #if defined HAVE_LCD_BITMAP && LCD_DEPTH > 1
859 old_backdrop = lcd_get_backdrop();
860 #endif
861 lcd_clear_display();
862 lcd_update();
864 #ifdef HAVE_REMOTE_LCD
865 lcd_remote_clear_display();
866 lcd_remote_update();
867 #endif
869 FOR_NB_SCREENS(i)
870 viewportmanager_theme_enable(i, false, NULL);
872 #ifdef HAVE_TOUCHSCREEN
873 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
874 #endif
876 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
877 open_files = 0;
878 #endif
880 rc = p_hdr->entry_point(parameter);
882 if (!pfn_tsr_exit)
883 { /* close handle if plugin is no tsr one */
884 lc_close(current_plugin_handle);
885 current_plugin_handle = NULL;
888 /* Go back to the global setting in case the plugin changed it */
889 #ifdef HAVE_TOUCHSCREEN
890 touchscreen_set_mode(global_settings.touch_mode);
891 #endif
893 #ifdef HAVE_LCD_BITMAP
894 lcd_setfont(FONT_UI);
895 #if LCD_DEPTH > 1
896 lcd_set_backdrop(old_backdrop);
897 #ifdef HAVE_LCD_COLOR
898 lcd_set_drawinfo(DRMODE_SOLID, global_settings.fg_color,
899 global_settings.bg_color);
900 #else
901 lcd_set_drawinfo(DRMODE_SOLID, LCD_DEFAULT_FG, LCD_DEFAULT_BG);
902 #endif
903 #else /* LCD_DEPTH == 1 */
904 lcd_set_drawmode(DRMODE_SOLID);
905 #endif /* LCD_DEPTH */
906 #endif /* HAVE_LCD_BITMAP */
909 #ifdef HAVE_REMOTE_LCD
910 #if LCD_REMOTE_DEPTH > 1
911 lcd_remote_set_drawinfo(DRMODE_SOLID, LCD_REMOTE_DEFAULT_FG,
912 LCD_REMOTE_DEFAULT_BG);
913 #else
914 lcd_remote_set_drawmode(DRMODE_SOLID);
915 #endif
916 #endif
918 lcd_clear_display();
919 #ifdef HAVE_REMOTE_LCD
920 lcd_remote_clear_display();
921 #endif
923 FOR_NB_SCREENS(i)
924 viewportmanager_theme_undo(i, false);
926 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
927 if(open_files != 0 && !current_plugin_handle)
929 int fd;
930 logf("Plugin '%s' leaks file handles", plugin);
932 static const char *lines[] =
933 { ID2P(LANG_PLUGIN_ERROR),
934 "#leak-file-handles" };
935 static const struct text_message message={ lines, 2 };
936 button_clear_queue(); /* Empty the keyboard buffer */
937 gui_syncyesno_run(&message, NULL, NULL);
939 for(fd=0; fd < MAX_OPEN_FILES; fd++)
940 if(open_files & (1<<fd))
941 close_wrapper(fd);
943 #endif
945 if (rc == PLUGIN_ERROR)
946 splash(HZ*2, str(LANG_PLUGIN_ERROR));
948 return rc;
951 /* Returns a pointer to the portion of the plugin buffer that is not already
952 being used. If no plugin is loaded, returns the entire plugin buffer */
953 void* plugin_get_buffer(size_t *buffer_size)
955 int buffer_pos;
957 if (current_plugin_handle)
959 if (plugin_size >= PLUGIN_BUFFER_SIZE)
960 return NULL;
962 *buffer_size = PLUGIN_BUFFER_SIZE-plugin_size;
963 buffer_pos = plugin_size;
965 else
967 *buffer_size = PLUGIN_BUFFER_SIZE;
968 buffer_pos = 0;
971 return &pluginbuf[buffer_pos];
974 /* Returns a pointer to the mp3 buffer.
975 Playback gets stopped, to avoid conflicts.
976 Talk buffer is stolen as well.
978 void* plugin_get_audio_buffer(size_t *buffer_size)
980 #if CONFIG_CODEC == SWCODEC
981 return audio_get_buffer(true, buffer_size);
982 #else
983 audio_stop();
984 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
985 *buffer_size = audiobufend - audiobuf;
986 return audiobuf;
987 #endif
990 /* The plugin wants to stay resident after leaving its main function, e.g.
991 runs from timer or own thread. The callback is registered to later
992 instruct it to free its resources before a new plugin gets loaded. */
993 void plugin_tsr(bool (*exit_callback)(bool))
995 pfn_tsr_exit = exit_callback; /* remember the callback for later */
998 char *plugin_get_current_filename(void)
1000 return current_plugin;
1003 static int open_wrapper(const char* pathname, int flags, ...)
1005 /* we don't have an 'open' function. it's a define. and we need
1006 * the real file_open, hence PREFIX() doesn't work here */
1007 int fd;
1008 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
1009 if (flags & O_CREAT)
1011 va_list ap;
1012 va_start(ap, flags);
1013 fd = open(pathname, flags, va_arg(ap, unsigned int));
1014 va_end(ap);
1016 else
1017 fd = open(pathname, flags);
1018 #else
1019 fd = file_open(pathname,flags);
1020 #endif
1022 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1023 if(fd >= 0)
1024 open_files |= 1<<fd;
1025 #endif
1026 return fd;
1029 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1030 static int close_wrapper(int fd)
1032 if((~open_files) & (1<<fd))
1034 logf("double close from plugin");
1036 if(fd >= 0)
1037 open_files &= (~(1<<fd));
1039 return PREFIX(close)(fd);
1042 static int creat_wrapper(const char *pathname, mode_t mode)
1044 int fd = PREFIX(creat)(pathname, mode);
1046 if(fd >= 0)
1047 open_files |= (1<<fd);
1049 return fd;
1051 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */