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