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