Fix oops. Too late now.
[maemo-rb.git] / apps / plugin.c
blob25280750ce8635b6b02f046a96e3b6b70126f168
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 battery_voltage,
668 #if CONFIG_CHARGING
669 charger_inserted,
670 # if CONFIG_CHARGING >= CHARGING_MONITOR
671 charging_state,
672 # endif
673 #endif
674 #ifdef HAVE_USB_POWER
675 usb_powered,
676 #endif
678 /* misc */
679 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
680 &errno,
681 #endif
682 srand,
683 rand,
684 (qsort_func)qsort,
685 kbd_input,
686 get_time,
687 set_time,
688 #if CONFIG_RTC
689 mktime,
690 #endif
691 plugin_get_buffer,
692 plugin_get_audio_buffer,
693 plugin_tsr,
694 plugin_get_current_filename,
695 #if defined(DEBUG) || defined(SIMULATOR)
696 debugf,
697 #endif
698 #ifdef ROCKBOX_HAS_LOGF
699 _logf,
700 #endif
701 &global_settings,
702 &global_status,
703 talk_disable,
704 #if CONFIG_CODEC == SWCODEC
705 codec_thread_do_callback,
706 codec_load_file,
707 codec_run_proc,
708 codec_close,
709 get_codec_filename,
710 find_array_ptr,
711 remove_array_ptr,
712 round_value_to_list32,
713 #endif /* CONFIG_CODEC == SWCODEC */
714 get_metadata,
715 mp3info,
716 count_mp3_frames,
717 create_xing_header,
718 find_next_frame,
719 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
720 peak_meter_scale_value,
721 peak_meter_set_use_dbfs,
722 peak_meter_get_use_dbfs,
723 #endif
724 #ifdef HAVE_LCD_BITMAP
725 read_bmp_file,
726 read_bmp_fd,
727 #ifdef HAVE_JPEG
728 read_jpeg_file,
729 read_jpeg_fd,
730 #endif
731 screen_dump_set_hook,
732 #endif
733 show_logo,
734 tree_get_context,
735 tree_get_entries,
736 tree_get_entry_at,
737 set_current_file,
738 set_dirfilter,
740 #ifdef HAVE_WHEEL_POSITION
741 wheel_status,
742 wheel_send_events,
743 #endif
745 #ifdef IRIVER_H100_SERIES
746 /* Routines for the iriver_flash -plugin. */
747 detect_original_firmware,
748 detect_flashed_ramimage,
749 detect_flashed_romimage,
750 #endif
751 led,
752 #if (CONFIG_CODEC == SWCODEC)
753 bufopen,
754 bufalloc,
755 bufclose,
756 bufseek,
757 bufadvance,
758 bufread,
759 bufgetdata,
760 bufgettail,
761 bufcuttail,
763 buf_handle_offset,
764 buf_set_base_handle,
765 buf_used,
766 #endif
768 #ifdef HAVE_TAGCACHE
769 tagcache_search,
770 tagcache_search_set_uniqbuf,
771 tagcache_search_add_filter,
772 tagcache_get_next,
773 tagcache_retrieve,
774 tagcache_search_finish,
775 tagcache_get_numeric,
776 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
777 tagcache_fill_tags,
778 #endif
779 #endif
781 #ifdef HAVE_ALBUMART
782 search_albumart_files,
783 #endif
785 #ifdef HAVE_SEMAPHORE_OBJECTS
786 semaphore_init,
787 semaphore_wait,
788 semaphore_release,
789 #endif
791 rbversion,
793 /* new stuff at the end, sort into place next time
794 the API gets incompatible */
796 commit_discard_idcache,
799 int plugin_load(const char* plugin, const void* parameter)
801 struct plugin_header *p_hdr;
802 struct lc_header *hdr;
804 if (current_plugin_handle && pfn_tsr_exit)
805 { /* if we have a resident old plugin and a callback */
806 if (pfn_tsr_exit(!strcmp(current_plugin, plugin)) == false )
808 /* not allowing another plugin to load */
809 return PLUGIN_OK;
811 lc_close(current_plugin_handle);
812 current_plugin_handle = pfn_tsr_exit = NULL;
815 splash(0, ID2P(LANG_WAIT));
816 strcpy(current_plugin, plugin);
818 current_plugin_handle = lc_open(plugin, pluginbuf, PLUGIN_BUFFER_SIZE);
819 if (current_plugin_handle == NULL) {
820 splashf(HZ*2, str(LANG_PLUGIN_CANT_OPEN), plugin);
821 return -1;
824 p_hdr = lc_get_header(current_plugin_handle);
826 hdr = p_hdr ? &p_hdr->lc_hdr : NULL;
829 if (hdr == NULL
830 || hdr->magic != PLUGIN_MAGIC
831 || hdr->target_id != TARGET_ID
832 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
833 || hdr->load_addr != pluginbuf
834 || hdr->end_addr > pluginbuf + PLUGIN_BUFFER_SIZE
835 #endif
838 lc_close(current_plugin_handle);
839 splash(HZ*2, str(LANG_PLUGIN_WRONG_MODEL));
840 return -1;
842 if (hdr->api_version > PLUGIN_API_VERSION
843 || hdr->api_version < PLUGIN_MIN_API_VERSION)
845 lc_close(current_plugin_handle);
846 splash(HZ*2, str(LANG_PLUGIN_WRONG_VERSION));
847 return -1;
849 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
850 plugin_size = hdr->end_addr - pluginbuf;
851 #else
852 plugin_size = 0;
853 #endif
855 *(p_hdr->api) = &rockbox_api;
857 lcd_clear_display();
858 lcd_update();
860 #ifdef HAVE_REMOTE_LCD
861 lcd_remote_clear_display();
862 lcd_remote_update();
863 #endif
864 push_current_activity(ACTIVITY_PLUGIN);
865 /* some plugins assume the entry cache doesn't move and save pointers to it
866 * they should be fixed properly instead of this lock */
867 tree_lock_cache(tree_get_context());
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 int rc = p_hdr->entry_point(parameter);
882 tree_unlock_cache(tree_get_context());
883 pop_current_activity();
885 if (!pfn_tsr_exit)
886 { /* close handle if plugin is no tsr one */
887 lc_close(current_plugin_handle);
888 current_plugin_handle = NULL;
891 /* Go back to the global setting in case the plugin changed it */
892 #ifdef HAVE_TOUCHSCREEN
893 touchscreen_set_mode(global_settings.touch_mode);
894 #endif
896 #ifdef HAVE_LCD_BITMAP
897 screen_helper_setfont(FONT_UI);
898 #if LCD_DEPTH > 1
899 #ifdef HAVE_LCD_COLOR
900 lcd_set_drawinfo(DRMODE_SOLID, global_settings.fg_color,
901 global_settings.bg_color);
902 #else
903 lcd_set_drawinfo(DRMODE_SOLID, LCD_DEFAULT_FG, LCD_DEFAULT_BG);
904 #endif
905 #else /* LCD_DEPTH == 1 */
906 lcd_set_drawmode(DRMODE_SOLID);
907 #endif /* LCD_DEPTH */
908 #endif /* HAVE_LCD_BITMAP */
911 #ifdef HAVE_REMOTE_LCD
912 #if LCD_REMOTE_DEPTH > 1
913 lcd_remote_set_drawinfo(DRMODE_SOLID, LCD_REMOTE_DEFAULT_FG,
914 LCD_REMOTE_DEFAULT_BG);
915 #else
916 lcd_remote_set_drawmode(DRMODE_SOLID);
917 #endif
918 #endif
920 lcd_clear_display();
921 #ifdef HAVE_REMOTE_LCD
922 lcd_remote_clear_display();
923 #endif
925 FOR_NB_SCREENS(i)
926 viewportmanager_theme_undo(i, true);
928 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
929 if(open_files != 0 && !current_plugin_handle)
931 int fd;
932 logf("Plugin '%s' leaks file handles", plugin);
934 static const char *lines[] =
935 { ID2P(LANG_PLUGIN_ERROR),
936 "#leak-file-handles" };
937 static const struct text_message message={ lines, 2 };
938 button_clear_queue(); /* Empty the keyboard buffer */
939 gui_syncyesno_run(&message, NULL, NULL);
941 for(fd=0; fd < MAX_OPEN_FILES; fd++)
942 if(open_files & (1<<fd))
943 close_wrapper(fd);
945 #endif
947 if (rc == PLUGIN_ERROR)
948 splash(HZ*2, str(LANG_PLUGIN_ERROR));
950 return rc;
953 /* Returns a pointer to the portion of the plugin buffer that is not already
954 being used. If no plugin is loaded, returns the entire plugin buffer */
955 void* plugin_get_buffer(size_t *buffer_size)
957 int buffer_pos;
959 if (current_plugin_handle)
961 if (plugin_size >= PLUGIN_BUFFER_SIZE)
962 return NULL;
964 *buffer_size = PLUGIN_BUFFER_SIZE-plugin_size;
965 buffer_pos = plugin_size;
967 else
969 *buffer_size = PLUGIN_BUFFER_SIZE;
970 buffer_pos = 0;
973 return &pluginbuf[buffer_pos];
976 /* Returns a pointer to the mp3 buffer.
977 Playback gets stopped, to avoid conflicts.
978 Talk buffer is stolen as well.
980 void* plugin_get_audio_buffer(size_t *buffer_size)
982 audio_stop();
983 return audio_get_buffer(true, buffer_size);
986 /* The plugin wants to stay resident after leaving its main function, e.g.
987 runs from timer or own thread. The callback is registered to later
988 instruct it to free its resources before a new plugin gets loaded. */
989 void plugin_tsr(bool (*exit_callback)(bool))
991 pfn_tsr_exit = exit_callback; /* remember the callback for later */
994 char *plugin_get_current_filename(void)
996 return current_plugin;
999 static int open_wrapper(const char* pathname, int flags, ...)
1001 /* we don't have an 'open' function. it's a define. and we need
1002 * the real file_open, hence PREFIX() doesn't work here */
1003 int fd;
1004 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
1005 if (flags & O_CREAT)
1007 va_list ap;
1008 va_start(ap, flags);
1009 fd = open(pathname, flags, va_arg(ap, unsigned int));
1010 va_end(ap);
1012 else
1013 fd = open(pathname, flags);
1014 #else
1015 fd = file_open(pathname,flags);
1016 #endif
1018 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1019 if(fd >= 0)
1020 open_files |= 1<<fd;
1021 #endif
1022 return fd;
1025 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1026 static int close_wrapper(int fd)
1028 if((~open_files) & (1<<fd))
1030 logf("double close from plugin");
1032 if(fd >= 0)
1033 open_files &= (~(1<<fd));
1035 return PREFIX(close)(fd);
1038 static int creat_wrapper(const char *pathname, mode_t mode)
1040 int fd = PREFIX(creat)(pathname, mode);
1042 if(fd >= 0)
1043 open_files |= (1<<fd);
1045 return fd;
1047 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */