Calibrate iPod Classic battery gauge a bit better
[kugel-rb.git] / apps / plugin.c
blob690aee9bf30a2f24d4d1a2d2ae6f27d511125ab8
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_exit,
411 thread_wait,
412 #if (CONFIG_CODEC == SWCODEC)
413 thread_thaw,
414 #ifdef HAVE_PRIORITY_SCHEDULING
415 thread_set_priority,
416 #endif
417 mutex_init,
418 mutex_lock,
419 mutex_unlock,
420 #endif
422 reset_poweroff_timer,
423 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
424 system_memory_guard,
425 &cpu_frequency,
427 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
428 #ifdef CPU_BOOST_LOGGING
429 cpu_boost_,
430 #else
431 cpu_boost,
432 #endif
433 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
434 #endif /* PLATFORM_NATIVE */
435 #ifdef HAVE_SCHEDULER_BOOSTCTRL
436 trigger_cpu_boost,
437 cancel_cpu_boost,
438 #endif
440 cpucache_flush,
441 cpucache_invalidate,
443 lc_open,
444 lc_open_from_mem,
445 lc_get_header,
446 lc_close,
448 timer_register,
449 timer_unregister,
450 timer_set_period,
452 queue_init,
453 queue_delete,
454 queue_post,
455 queue_wait_w_tmo,
456 #if CONFIG_CODEC == SWCODEC
457 queue_enable_queue_send,
458 queue_empty,
459 queue_wait,
460 queue_send,
461 queue_reply,
462 #endif
463 usb_acknowledge,
464 #ifdef USB_ENABLE_HID
465 usb_hid_send,
466 #endif
467 #ifdef RB_PROFILE
468 profile_thread,
469 profstop,
470 __cyg_profile_func_enter,
471 __cyg_profile_func_exit,
472 #endif
473 add_event,
474 remove_event,
475 send_event,
477 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
478 /* special simulator hooks */
479 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
480 sim_lcd_ex_init,
481 sim_lcd_ex_update_rect,
482 #endif
483 #endif
485 /* strings and memory */
486 snprintf,
487 vsnprintf,
488 strcpy,
489 strlcpy,
490 strlen,
491 strrchr,
492 strcmp,
493 strncmp,
494 strcasecmp,
495 strncasecmp,
496 memset,
497 memcpy,
498 memmove,
499 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
500 _ctype_,
501 #endif
502 atoi,
503 strchr,
504 strcat,
505 strlcat,
506 memchr,
507 memcmp,
508 strcasestr,
509 strtok_r,
510 /* unicode stuff */
511 utf8decode,
512 iso_decode,
513 utf16LEdecode,
514 utf16BEdecode,
515 utf8encode,
516 utf8length,
517 utf8seek,
519 /* sound */
520 sound_set,
521 sound_default,
522 sound_min,
523 sound_max,
524 sound_unit,
525 sound_val2phys,
526 #ifdef AUDIOHW_HAVE_EQ
527 sound_enum_hw_eq_band_setting,
528 #endif
529 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
530 mp3_play_data,
531 mp3_play_pause,
532 mp3_play_stop,
533 mp3_is_playing,
534 #if CONFIG_CODEC != SWCODEC
535 bitswap,
536 #endif
537 #endif
538 #if CONFIG_CODEC == SWCODEC
539 &audio_master_sampr_list[0],
540 &hw_freq_sampr[0],
541 pcm_apply_settings,
542 pcm_play_data,
543 pcm_play_stop,
544 pcm_set_frequency,
545 pcm_is_playing,
546 pcm_is_paused,
547 pcm_play_pause,
548 pcm_get_bytes_waiting,
549 pcm_calculate_peaks,
550 pcm_get_peak_buffer,
551 pcm_play_lock,
552 pcm_play_unlock,
553 pcmbuf_beep,
554 #ifdef HAVE_RECORDING
555 &rec_freq_sampr[0],
556 pcm_init_recording,
557 pcm_close_recording,
558 pcm_record_data,
559 pcm_stop_recording,
560 pcm_calculate_rec_peaks,
561 audio_set_recording_gain,
562 #endif /* HAVE_RECORDING */
563 #if INPUT_SRC_CAPS != 0
564 audio_set_output_source,
565 audio_set_input_source,
566 #endif
567 dsp_set_crossfeed,
568 dsp_set_eq,
569 dsp_dither_enable,
570 dsp_configure,
571 dsp_process,
572 dsp_input_count,
573 dsp_output_count,
574 #endif /* CONFIG_CODEC == SWCODEC */
576 /* playback control */
577 playlist_amount,
578 playlist_resume,
579 playlist_start,
580 playlist_add,
581 playlist_sync,
582 playlist_remove_all_tracks,
583 playlist_create,
584 playlist_insert_track,
585 playlist_insert_directory,
586 playlist_shuffle,
587 audio_play,
588 audio_stop,
589 audio_pause,
590 audio_resume,
591 audio_next,
592 audio_prev,
593 audio_ff_rewind,
594 audio_next_track,
595 audio_status,
596 audio_current_track,
597 audio_flush_and_reload_tracks,
598 audio_get_file_pos,
599 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
600 mpeg_get_last_header,
601 #endif
602 #if ((CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
603 (CONFIG_CODEC == SWCODEC)) && defined (HAVE_PITCHSCREEN)
604 sound_set_pitch,
605 #endif
607 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
608 /* MAS communication */
609 mas_readmem,
610 mas_writemem,
611 mas_readreg,
612 mas_writereg,
613 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
614 mas_codec_writereg,
615 mas_codec_readreg,
616 i2c_begin,
617 i2c_end,
618 i2c_write,
619 #endif
620 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
622 /* menu */
623 do_menu,
624 /* statusbars */
625 &statusbars,
626 gui_syncstatusbar_draw,
628 /* options */
629 get_settings_list,
630 find_setting,
631 option_screen,
632 set_option,
633 set_bool_options,
634 set_int,
635 set_bool,
636 #ifdef HAVE_LCD_COLOR
637 set_color,
638 #endif
640 /* action handling */
641 get_custom_action,
642 get_action,
643 #ifdef HAVE_TOUCHSCREEN
644 action_get_touchscreen_press,
645 #endif
646 action_userabort,
648 /* power */
649 battery_level,
650 battery_level_safe,
651 battery_time,
652 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
653 battery_voltage,
654 #endif
655 #if CONFIG_CHARGING
656 charger_inserted,
657 # if CONFIG_CHARGING >= CHARGING_MONITOR
658 charging_state,
659 # endif
660 #endif
661 #ifdef HAVE_USB_POWER
662 usb_powered,
663 #endif
665 /* misc */
666 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
667 &errno,
668 #endif
669 srand,
670 rand,
671 (qsort_func)qsort,
672 kbd_input,
673 get_time,
674 set_time,
675 #if CONFIG_RTC
676 mktime,
677 #endif
678 plugin_get_buffer,
679 plugin_get_audio_buffer,
680 plugin_tsr,
681 plugin_get_current_filename,
682 #if defined(DEBUG) || defined(SIMULATOR)
683 debugf,
684 #endif
685 #ifdef ROCKBOX_HAS_LOGF
686 _logf,
687 #endif
688 &global_settings,
689 &global_status,
690 talk_disable,
691 #if CONFIG_CODEC == SWCODEC
692 codec_thread_do_callback,
693 codec_load_file,
694 get_codec_filename,
695 find_array_ptr,
696 remove_array_ptr,
697 round_value_to_list32,
698 #endif /* CONFIG_CODEC == SWCODEC */
699 get_metadata,
700 mp3info,
701 count_mp3_frames,
702 create_xing_header,
703 find_next_frame,
704 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
705 peak_meter_scale_value,
706 peak_meter_set_use_dbfs,
707 peak_meter_get_use_dbfs,
708 #endif
709 #ifdef HAVE_LCD_BITMAP
710 read_bmp_file,
711 read_bmp_fd,
712 #ifdef HAVE_JPEG
713 read_jpeg_file,
714 read_jpeg_fd,
715 #endif
716 screen_dump_set_hook,
717 #endif
718 show_logo,
719 tree_get_context,
720 set_current_file,
721 set_dirfilter,
723 #ifdef HAVE_WHEEL_POSITION
724 wheel_status,
725 wheel_send_events,
726 #endif
728 #ifdef IRIVER_H100_SERIES
729 /* Routines for the iriver_flash -plugin. */
730 detect_original_firmware,
731 detect_flashed_ramimage,
732 detect_flashed_romimage,
733 #endif
734 led,
735 #if (CONFIG_CODEC == SWCODEC)
736 bufopen,
737 bufalloc,
738 bufclose,
739 bufseek,
740 bufadvance,
741 bufread,
742 bufgetdata,
743 bufgettail,
744 bufcuttail,
746 buf_get_offset,
747 buf_handle_offset,
748 buf_request_buffer_handle,
749 buf_set_base_handle,
750 buf_used,
751 #endif
753 #ifdef HAVE_TAGCACHE
754 tagcache_search,
755 tagcache_search_set_uniqbuf,
756 tagcache_search_add_filter,
757 tagcache_get_next,
758 tagcache_retrieve,
759 tagcache_search_finish,
760 tagcache_get_numeric,
761 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
762 tagcache_fill_tags,
763 #endif
764 #endif
766 #ifdef HAVE_ALBUMART
767 search_albumart_files,
768 #endif
770 #ifdef HAVE_SEMAPHORE_OBJECTS
771 semaphore_init,
772 semaphore_wait,
773 semaphore_release,
774 #endif
776 rbversion,
778 /* new stuff at the end, sort into place next time
779 the API gets incompatible */
782 int plugin_load(const char* plugin, const void* parameter)
784 int rc, i;
785 struct plugin_header *p_hdr;
786 struct lc_header *hdr;
788 #if LCD_DEPTH > 1
789 fb_data* old_backdrop;
790 #endif
792 if (current_plugin_handle && pfn_tsr_exit)
793 { /* if we have a resident old plugin and a callback */
794 if (pfn_tsr_exit(!strcmp(current_plugin, plugin)) == false )
796 /* not allowing another plugin to load */
797 return PLUGIN_OK;
799 lc_close(current_plugin_handle);
800 current_plugin_handle = pfn_tsr_exit = NULL;
803 splash(0, ID2P(LANG_WAIT));
804 strcpy(current_plugin, plugin);
806 current_plugin_handle = lc_open(plugin, pluginbuf, PLUGIN_BUFFER_SIZE);
807 if (current_plugin_handle == NULL) {
808 splashf(HZ*2, str(LANG_PLUGIN_CANT_OPEN), plugin);
809 return -1;
812 p_hdr = lc_get_header(current_plugin_handle);
814 hdr = p_hdr ? &p_hdr->lc_hdr : NULL;
817 if (hdr == NULL
818 || hdr->magic != PLUGIN_MAGIC
819 || hdr->target_id != TARGET_ID
820 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
821 || hdr->load_addr != pluginbuf
822 || hdr->end_addr > pluginbuf + PLUGIN_BUFFER_SIZE
823 #endif
826 lc_close(current_plugin_handle);
827 splash(HZ*2, str(LANG_PLUGIN_WRONG_MODEL));
828 return -1;
830 if (hdr->api_version > PLUGIN_API_VERSION
831 || hdr->api_version < PLUGIN_MIN_API_VERSION)
833 lc_close(current_plugin_handle);
834 splash(HZ*2, str(LANG_PLUGIN_WRONG_VERSION));
835 return -1;
837 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
838 plugin_size = hdr->end_addr - pluginbuf;
839 #else
840 plugin_size = 0;
841 #endif
843 *(p_hdr->api) = &rockbox_api;
845 #if defined HAVE_LCD_BITMAP && LCD_DEPTH > 1
846 old_backdrop = lcd_get_backdrop();
847 #endif
848 lcd_clear_display();
849 lcd_update();
851 #ifdef HAVE_REMOTE_LCD
852 lcd_remote_clear_display();
853 lcd_remote_update();
854 #endif
856 FOR_NB_SCREENS(i)
857 viewportmanager_theme_enable(i, false, NULL);
859 #ifdef HAVE_TOUCHSCREEN
860 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
861 #endif
863 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
864 open_files = 0;
865 #endif
867 rc = p_hdr->entry_point(parameter);
869 if (!pfn_tsr_exit)
870 { /* close handle if plugin is no tsr one */
871 lc_close(current_plugin_handle);
872 current_plugin_handle = NULL;
875 /* Go back to the global setting in case the plugin changed it */
876 #ifdef HAVE_TOUCHSCREEN
877 touchscreen_set_mode(global_settings.touch_mode);
878 #endif
880 #ifdef HAVE_LCD_BITMAP
881 lcd_setfont(FONT_UI);
882 #if LCD_DEPTH > 1
883 lcd_set_backdrop(old_backdrop);
884 #ifdef HAVE_LCD_COLOR
885 lcd_set_drawinfo(DRMODE_SOLID, global_settings.fg_color,
886 global_settings.bg_color);
887 #else
888 lcd_set_drawinfo(DRMODE_SOLID, LCD_DEFAULT_FG, LCD_DEFAULT_BG);
889 #endif
890 #else /* LCD_DEPTH == 1 */
891 lcd_set_drawmode(DRMODE_SOLID);
892 #endif /* LCD_DEPTH */
893 #endif /* HAVE_LCD_BITMAP */
896 #ifdef HAVE_REMOTE_LCD
897 #if LCD_REMOTE_DEPTH > 1
898 lcd_remote_set_drawinfo(DRMODE_SOLID, LCD_REMOTE_DEFAULT_FG,
899 LCD_REMOTE_DEFAULT_BG);
900 #else
901 lcd_remote_set_drawmode(DRMODE_SOLID);
902 #endif
903 #endif
905 lcd_clear_display();
906 #ifdef HAVE_REMOTE_LCD
907 lcd_remote_clear_display();
908 #endif
910 FOR_NB_SCREENS(i)
911 viewportmanager_theme_undo(i, false);
913 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
914 if(open_files != 0 && !current_plugin_handle)
916 int fd;
917 logf("Plugin '%s' leaks file handles", plugin);
919 static const char *lines[] =
920 { ID2P(LANG_PLUGIN_ERROR),
921 "#leak-file-handles" };
922 static const struct text_message message={ lines, 2 };
923 button_clear_queue(); /* Empty the keyboard buffer */
924 gui_syncyesno_run(&message, NULL, NULL);
926 for(fd=0; fd < MAX_OPEN_FILES; fd++)
927 if(open_files & (1<<fd))
928 close_wrapper(fd);
930 #endif
932 if (rc == PLUGIN_ERROR)
933 splash(HZ*2, str(LANG_PLUGIN_ERROR));
935 return rc;
938 /* Returns a pointer to the portion of the plugin buffer that is not already
939 being used. If no plugin is loaded, returns the entire plugin buffer */
940 void* plugin_get_buffer(size_t *buffer_size)
942 int buffer_pos;
944 if (current_plugin_handle)
946 if (plugin_size >= PLUGIN_BUFFER_SIZE)
947 return NULL;
949 *buffer_size = PLUGIN_BUFFER_SIZE-plugin_size;
950 buffer_pos = plugin_size;
952 else
954 *buffer_size = PLUGIN_BUFFER_SIZE;
955 buffer_pos = 0;
958 return &pluginbuf[buffer_pos];
961 /* Returns a pointer to the mp3 buffer.
962 Playback gets stopped, to avoid conflicts.
963 Talk buffer is stolen as well.
965 void* plugin_get_audio_buffer(size_t *buffer_size)
967 #if CONFIG_CODEC == SWCODEC
968 return audio_get_buffer(true, buffer_size);
969 #else
970 audio_stop();
971 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
972 *buffer_size = audiobufend - audiobuf;
973 return audiobuf;
974 #endif
977 /* The plugin wants to stay resident after leaving its main function, e.g.
978 runs from timer or own thread. The callback is registered to later
979 instruct it to free its resources before a new plugin gets loaded. */
980 void plugin_tsr(bool (*exit_callback)(bool))
982 pfn_tsr_exit = exit_callback; /* remember the callback for later */
985 char *plugin_get_current_filename(void)
987 return current_plugin;
990 static int open_wrapper(const char* pathname, int flags, ...)
992 /* we don't have an 'open' function. it's a define. and we need
993 * the real file_open, hence PREFIX() doesn't work here */
994 int fd;
995 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
996 if (flags & O_CREAT)
998 va_list ap;
999 va_start(ap, flags);
1000 fd = open(pathname, flags, va_arg(ap, unsigned int));
1001 va_end(ap);
1003 else
1004 fd = open(pathname, flags);
1005 #else
1006 fd = file_open(pathname,flags);
1007 #endif
1009 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1010 if(fd >= 0)
1011 open_files |= 1<<fd;
1012 #endif
1013 return fd;
1016 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1017 static int close_wrapper(int fd)
1019 if((~open_files) & (1<<fd))
1021 logf("double close from plugin");
1023 if(fd >= 0)
1024 open_files &= (~(1<<fd));
1026 return PREFIX(close)(fd);
1029 static int creat_wrapper(const char *pathname, mode_t mode)
1031 int fd = PREFIX(creat)(pathname, mode);
1033 if(fd >= 0)
1034 open_files |= (1<<fd);
1036 return fd;
1038 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */