Fix silly mistake
[maemo-rb.git] / apps / plugin.c
blob7c75a43080601c2e193cd5f91b1f428ada9b1077
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 screen_helper_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_unload,
234 font_get,
235 font_getstringsize,
236 font_get_width,
237 screen_clear_area,
238 gui_scrollbar_draw,
239 #endif /* HAVE_LCD_BITMAP */
240 get_codepage_name,
242 backlight_on,
243 backlight_off,
244 backlight_set_timeout,
245 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
246 backlight_set_brightness,
247 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
249 #if CONFIG_CHARGING
250 backlight_set_timeout_plugged,
251 #endif
252 is_backlight_on,
253 splash,
254 splashf,
256 #ifdef HAVE_REMOTE_LCD
257 /* remote lcd */
258 lcd_remote_set_contrast,
259 lcd_remote_clear_display,
260 lcd_remote_puts,
261 lcd_remote_puts_scroll,
262 lcd_remote_stop_scroll,
263 lcd_remote_set_drawmode,
264 lcd_remote_get_drawmode,
265 lcd_remote_setfont,
266 lcd_remote_getstringsize,
267 lcd_remote_drawpixel,
268 lcd_remote_drawline,
269 lcd_remote_hline,
270 lcd_remote_vline,
271 lcd_remote_drawrect,
272 lcd_remote_fillrect,
273 lcd_remote_mono_bitmap_part,
274 lcd_remote_mono_bitmap,
275 lcd_remote_putsxy,
276 lcd_remote_puts_style,
277 lcd_remote_puts_scroll_style,
278 &lcd_remote_framebuffer[0][0],
279 lcd_remote_update,
280 lcd_remote_update_rect,
282 remote_backlight_on,
283 remote_backlight_off,
284 remote_backlight_set_timeout,
285 #if CONFIG_CHARGING
286 remote_backlight_set_timeout_plugged,
287 #endif
288 #endif /* HAVE_REMOTE_LCD */
289 #if NB_SCREENS == 2
290 {&screens[SCREEN_MAIN], &screens[SCREEN_REMOTE]},
291 #else
292 {&screens[SCREEN_MAIN]},
293 #endif
294 #if defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
295 lcd_remote_set_foreground,
296 lcd_remote_get_foreground,
297 lcd_remote_set_background,
298 lcd_remote_get_background,
299 lcd_remote_bitmap_part,
300 lcd_remote_bitmap,
301 #endif
302 viewport_set_defaults,
303 #ifdef HAVE_LCD_BITMAP
304 viewportmanager_theme_enable,
305 viewportmanager_theme_undo,
306 #endif
308 /* list */
309 gui_synclist_init,
310 gui_synclist_set_nb_items,
311 gui_synclist_set_icon_callback,
312 gui_synclist_get_nb_items,
313 gui_synclist_get_sel_pos,
314 gui_synclist_draw,
315 gui_synclist_select_item,
316 gui_synclist_add_item,
317 gui_synclist_del_item,
318 gui_synclist_limit_scroll,
319 gui_synclist_do_button,
320 gui_synclist_set_title,
321 gui_syncyesno_run,
322 simplelist_info_init,
323 simplelist_show_list,
325 /* button */
326 button_get,
327 button_get_w_tmo,
328 button_status,
329 #ifdef HAVE_BUTTON_DATA
330 button_get_data,
331 button_status_wdata,
332 #endif
333 button_clear_queue,
334 button_queue_count,
335 #ifdef HAS_BUTTON_HOLD
336 button_hold,
337 #endif
338 #ifdef HAVE_TOUCHSCREEN
339 touchscreen_set_mode,
340 #endif
342 #ifdef HAVE_BUTTON_LIGHT
343 buttonlight_set_timeout,
344 buttonlight_off,
345 buttonlight_on,
346 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
347 buttonlight_set_brightness,
348 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
349 #endif /* HAVE_BUTTON_LIGHT */
351 /* file */
352 open_utf8,
353 (open_func)open_wrapper,
354 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
355 close_wrapper,
356 #else
357 PREFIX(close),
358 #endif
359 (read_func)PREFIX(read),
360 PREFIX(lseek),
361 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
362 (creat_func)creat_wrapper,
363 #else
364 PREFIX(creat),
365 #endif
366 (write_func)PREFIX(write),
367 PREFIX(remove),
368 PREFIX(rename),
369 PREFIX(ftruncate),
370 PREFIX(filesize),
371 fdprintf,
372 read_line,
373 settings_parseline,
374 storage_sleep,
375 storage_spin,
376 storage_spindown,
377 #if USING_STORAGE_CALLBACK
378 register_storage_idle_func,
379 unregister_storage_idle_func,
380 #endif /* USING_STORAGE_CALLBACK */
381 reload_directory,
382 create_numbered_filename,
383 file_exists,
384 strip_extension,
385 crc_32,
386 filetype_get_attr,
388 /* dir */
389 (opendir_func)PREFIX(opendir),
390 (closedir_func)PREFIX(closedir),
391 (readdir_func)PREFIX(readdir),
392 PREFIX(mkdir),
393 PREFIX(rmdir),
394 dir_exists,
395 dir_get_info,
397 /* browsing */
398 browse_context_init,
399 rockbox_browse,
401 /* kernel/ system */
402 #if defined(CPU_ARM) && CONFIG_PLATFORM & PLATFORM_NATIVE
403 __div0,
404 #endif
405 sleep,
406 yield,
407 &current_tick,
408 default_event_handler,
409 default_event_handler_ex,
410 create_thread,
411 thread_self,
412 thread_exit,
413 thread_wait,
414 #if (CONFIG_CODEC == SWCODEC)
415 thread_thaw,
416 #ifdef HAVE_PRIORITY_SCHEDULING
417 thread_set_priority,
418 #endif
419 mutex_init,
420 mutex_lock,
421 mutex_unlock,
422 #endif
424 reset_poweroff_timer,
425 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
426 system_memory_guard,
427 &cpu_frequency,
429 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
430 #ifdef CPU_BOOST_LOGGING
431 cpu_boost_,
432 #else
433 cpu_boost,
434 #endif
435 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
436 #endif /* PLATFORM_NATIVE */
437 #ifdef HAVE_SCHEDULER_BOOSTCTRL
438 trigger_cpu_boost,
439 cancel_cpu_boost,
440 #endif
442 cpucache_flush,
443 cpucache_invalidate,
445 lc_open,
446 lc_open_from_mem,
447 lc_get_header,
448 lc_close,
450 timer_register,
451 timer_unregister,
452 timer_set_period,
454 queue_init,
455 queue_delete,
456 queue_post,
457 queue_wait_w_tmo,
458 #if CONFIG_CODEC == SWCODEC
459 queue_enable_queue_send,
460 queue_empty,
461 queue_wait,
462 queue_send,
463 queue_reply,
464 #endif
465 usb_acknowledge,
466 #ifdef USB_ENABLE_HID
467 usb_hid_send,
468 #endif
469 #ifdef RB_PROFILE
470 profile_thread,
471 profstop,
472 __cyg_profile_func_enter,
473 __cyg_profile_func_exit,
474 #endif
475 add_event,
476 remove_event,
477 send_event,
479 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
480 /* special simulator hooks */
481 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
482 sim_lcd_ex_init,
483 sim_lcd_ex_update_rect,
484 #endif
485 #endif
487 /* strings and memory */
488 snprintf,
489 vsnprintf,
490 strcpy,
491 strlcpy,
492 strlen,
493 strrchr,
494 strcmp,
495 strncmp,
496 strcasecmp,
497 strncasecmp,
498 memset,
499 memcpy,
500 memmove,
501 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
502 _ctype_,
503 #endif
504 atoi,
505 strchr,
506 strcat,
507 strlcat,
508 memchr,
509 memcmp,
510 strcasestr,
511 strtok_r,
512 /* unicode stuff */
513 utf8decode,
514 iso_decode,
515 utf16LEdecode,
516 utf16BEdecode,
517 utf8encode,
518 utf8length,
519 utf8seek,
521 /* sound */
522 sound_set,
523 sound_default,
524 sound_min,
525 sound_max,
526 sound_unit,
527 sound_val2phys,
528 #ifdef AUDIOHW_HAVE_EQ
529 sound_enum_hw_eq_band_setting,
530 #endif
531 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
532 mp3_play_data,
533 mp3_play_pause,
534 mp3_play_stop,
535 mp3_is_playing,
536 #if CONFIG_CODEC != SWCODEC
537 bitswap,
538 #endif
539 #endif
540 #if CONFIG_CODEC == SWCODEC
541 &audio_master_sampr_list[0],
542 &hw_freq_sampr[0],
543 pcm_apply_settings,
544 pcm_play_data,
545 pcm_play_stop,
546 pcm_set_frequency,
547 pcm_is_playing,
548 pcm_is_paused,
549 pcm_play_pause,
550 pcm_get_bytes_waiting,
551 pcm_calculate_peaks,
552 pcm_get_peak_buffer,
553 pcm_play_lock,
554 pcm_play_unlock,
555 beep_play,
556 #ifdef HAVE_RECORDING
557 &rec_freq_sampr[0],
558 pcm_init_recording,
559 pcm_close_recording,
560 pcm_record_data,
561 pcm_stop_recording,
562 pcm_calculate_rec_peaks,
563 audio_set_recording_gain,
564 #endif /* HAVE_RECORDING */
565 #if INPUT_SRC_CAPS != 0
566 audio_set_output_source,
567 audio_set_input_source,
568 #endif
569 dsp_set_crossfeed,
570 dsp_set_eq,
571 dsp_dither_enable,
572 dsp_configure,
573 dsp_process,
574 dsp_input_count,
575 dsp_output_count,
577 mixer_channel_status,
578 mixer_channel_get_buffer,
579 mixer_channel_calculate_peaks,
580 mixer_channel_play_data,
581 mixer_channel_play_pause,
582 mixer_channel_stop,
583 mixer_channel_set_amplitude,
584 mixer_channel_get_bytes_waiting,
586 system_sound_play,
587 keyclick_click,
588 #endif
589 /* playback control */
590 playlist_amount,
591 playlist_resume,
592 playlist_start,
593 playlist_add,
594 playlist_sync,
595 playlist_remove_all_tracks,
596 playlist_create,
597 playlist_insert_track,
598 playlist_insert_directory,
599 playlist_shuffle,
600 audio_play,
601 audio_stop,
602 audio_pause,
603 audio_resume,
604 audio_next,
605 audio_prev,
606 audio_ff_rewind,
607 audio_next_track,
608 audio_status,
609 audio_current_track,
610 audio_flush_and_reload_tracks,
611 audio_get_file_pos,
612 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
613 mpeg_get_last_header,
614 #endif
615 #if ((CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
616 (CONFIG_CODEC == SWCODEC)) && defined (HAVE_PITCHSCREEN)
617 sound_set_pitch,
618 #endif
620 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
621 /* MAS communication */
622 mas_readmem,
623 mas_writemem,
624 mas_readreg,
625 mas_writereg,
626 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
627 mas_codec_writereg,
628 mas_codec_readreg,
629 i2c_begin,
630 i2c_end,
631 i2c_write,
632 #endif
633 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
635 /* menu */
636 do_menu,
637 /* statusbars */
638 &statusbars,
639 gui_syncstatusbar_draw,
641 /* options */
642 get_settings_list,
643 find_setting,
644 option_screen,
645 set_option,
646 set_bool_options,
647 set_int,
648 set_bool,
649 #ifdef HAVE_LCD_COLOR
650 set_color,
651 #endif
653 /* action handling */
654 get_custom_action,
655 get_action,
656 #ifdef HAVE_TOUCHSCREEN
657 action_get_touchscreen_press,
658 #endif
659 action_userabort,
661 /* power */
662 battery_level,
663 battery_level_safe,
664 battery_time,
665 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
666 battery_voltage,
667 #endif
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 set_current_file,
736 set_dirfilter,
738 #ifdef HAVE_WHEEL_POSITION
739 wheel_status,
740 wheel_send_events,
741 #endif
743 #ifdef IRIVER_H100_SERIES
744 /* Routines for the iriver_flash -plugin. */
745 detect_original_firmware,
746 detect_flashed_ramimage,
747 detect_flashed_romimage,
748 #endif
749 led,
750 #if (CONFIG_CODEC == SWCODEC)
751 bufopen,
752 bufalloc,
753 bufclose,
754 bufseek,
755 bufadvance,
756 bufread,
757 bufgetdata,
758 bufgettail,
759 bufcuttail,
761 buf_handle_offset,
762 buf_set_base_handle,
763 buf_used,
764 #endif
766 #ifdef HAVE_TAGCACHE
767 tagcache_search,
768 tagcache_search_set_uniqbuf,
769 tagcache_search_add_filter,
770 tagcache_get_next,
771 tagcache_retrieve,
772 tagcache_search_finish,
773 tagcache_get_numeric,
774 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
775 tagcache_fill_tags,
776 #endif
777 #endif
779 #ifdef HAVE_ALBUMART
780 search_albumart_files,
781 #endif
783 #ifdef HAVE_SEMAPHORE_OBJECTS
784 semaphore_init,
785 semaphore_wait,
786 semaphore_release,
787 #endif
789 rbversion,
791 /* new stuff at the end, sort into place next time
792 the API gets incompatible */
793 tree_get_entries,
794 tree_get_entry_at,
796 /* the buflib memory management library */
797 buflib_init,
798 buflib_available,
799 buflib_alloc,
800 buflib_alloc_ex,
801 buflib_alloc_maximum,
802 buflib_buffer_in,
803 buflib_buffer_out,
804 buflib_free,
805 buflib_shrink,
806 buflib_get_data,
807 buflib_get_name,
810 int plugin_load(const char* plugin, const void* parameter)
812 int rc, i;
813 struct plugin_header *p_hdr;
814 struct lc_header *hdr;
816 if (current_plugin_handle && pfn_tsr_exit)
817 { /* if we have a resident old plugin and a callback */
818 if (pfn_tsr_exit(!strcmp(current_plugin, plugin)) == false )
820 /* not allowing another plugin to load */
821 return PLUGIN_OK;
823 lc_close(current_plugin_handle);
824 current_plugin_handle = pfn_tsr_exit = NULL;
827 splash(0, ID2P(LANG_WAIT));
828 strcpy(current_plugin, plugin);
830 current_plugin_handle = lc_open(plugin, pluginbuf, PLUGIN_BUFFER_SIZE);
831 if (current_plugin_handle == NULL) {
832 splashf(HZ*2, str(LANG_PLUGIN_CANT_OPEN), plugin);
833 return -1;
836 p_hdr = lc_get_header(current_plugin_handle);
838 hdr = p_hdr ? &p_hdr->lc_hdr : NULL;
841 if (hdr == NULL
842 || hdr->magic != PLUGIN_MAGIC
843 || hdr->target_id != TARGET_ID
844 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
845 || hdr->load_addr != pluginbuf
846 || hdr->end_addr > pluginbuf + PLUGIN_BUFFER_SIZE
847 #endif
850 lc_close(current_plugin_handle);
851 splash(HZ*2, str(LANG_PLUGIN_WRONG_MODEL));
852 return -1;
854 if (hdr->api_version > PLUGIN_API_VERSION
855 || hdr->api_version < PLUGIN_MIN_API_VERSION)
857 lc_close(current_plugin_handle);
858 splash(HZ*2, str(LANG_PLUGIN_WRONG_VERSION));
859 return -1;
861 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
862 plugin_size = hdr->end_addr - pluginbuf;
863 #else
864 plugin_size = 0;
865 #endif
867 *(p_hdr->api) = &rockbox_api;
869 lcd_clear_display();
870 lcd_update();
872 #ifdef HAVE_REMOTE_LCD
873 lcd_remote_clear_display();
874 lcd_remote_update();
875 #endif
876 push_current_activity(ACTIVITY_PLUGIN);
877 /* some plugins assume the entry cache doesn't move and save pointers to it
878 * they should be fixed properly instead of this lock */
879 tree_lock_cache(tree_get_context());
881 FOR_NB_SCREENS(i)
882 viewportmanager_theme_enable(i, false, NULL);
884 #ifdef HAVE_TOUCHSCREEN
885 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
886 #endif
888 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
889 open_files = 0;
890 #endif
892 rc = p_hdr->entry_point(parameter);
894 tree_unlock_cache(tree_get_context());
895 pop_current_activity();
897 if (!pfn_tsr_exit)
898 { /* close handle if plugin is no tsr one */
899 lc_close(current_plugin_handle);
900 current_plugin_handle = NULL;
903 /* Go back to the global setting in case the plugin changed it */
904 #ifdef HAVE_TOUCHSCREEN
905 touchscreen_set_mode(global_settings.touch_mode);
906 #endif
908 #ifdef HAVE_LCD_BITMAP
909 screen_helper_setfont(FONT_UI);
910 #if LCD_DEPTH > 1
911 #ifdef HAVE_LCD_COLOR
912 lcd_set_drawinfo(DRMODE_SOLID, global_settings.fg_color,
913 global_settings.bg_color);
914 #else
915 lcd_set_drawinfo(DRMODE_SOLID, LCD_DEFAULT_FG, LCD_DEFAULT_BG);
916 #endif
917 #else /* LCD_DEPTH == 1 */
918 lcd_set_drawmode(DRMODE_SOLID);
919 #endif /* LCD_DEPTH */
920 #endif /* HAVE_LCD_BITMAP */
923 #ifdef HAVE_REMOTE_LCD
924 #if LCD_REMOTE_DEPTH > 1
925 lcd_remote_set_drawinfo(DRMODE_SOLID, LCD_REMOTE_DEFAULT_FG,
926 LCD_REMOTE_DEFAULT_BG);
927 #else
928 lcd_remote_set_drawmode(DRMODE_SOLID);
929 #endif
930 #endif
932 lcd_clear_display();
933 #ifdef HAVE_REMOTE_LCD
934 lcd_remote_clear_display();
935 #endif
937 FOR_NB_SCREENS(i)
938 viewportmanager_theme_undo(i, true);
940 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
941 if(open_files != 0 && !current_plugin_handle)
943 int fd;
944 logf("Plugin '%s' leaks file handles", plugin);
946 static const char *lines[] =
947 { ID2P(LANG_PLUGIN_ERROR),
948 "#leak-file-handles" };
949 static const struct text_message message={ lines, 2 };
950 button_clear_queue(); /* Empty the keyboard buffer */
951 gui_syncyesno_run(&message, NULL, NULL);
953 for(fd=0; fd < MAX_OPEN_FILES; fd++)
954 if(open_files & (1<<fd))
955 close_wrapper(fd);
957 #endif
959 if (rc == PLUGIN_ERROR)
960 splash(HZ*2, str(LANG_PLUGIN_ERROR));
962 return rc;
965 /* Returns a pointer to the portion of the plugin buffer that is not already
966 being used. If no plugin is loaded, returns the entire plugin buffer */
967 void* plugin_get_buffer(size_t *buffer_size)
969 int buffer_pos;
971 if (current_plugin_handle)
973 if (plugin_size >= PLUGIN_BUFFER_SIZE)
974 return NULL;
976 *buffer_size = PLUGIN_BUFFER_SIZE-plugin_size;
977 buffer_pos = plugin_size;
979 else
981 *buffer_size = PLUGIN_BUFFER_SIZE;
982 buffer_pos = 0;
985 return &pluginbuf[buffer_pos];
988 /* Returns a pointer to the mp3 buffer.
989 Playback gets stopped, to avoid conflicts.
990 Talk buffer is stolen as well.
992 void* plugin_get_audio_buffer(size_t *buffer_size)
994 audio_stop();
995 return audio_get_buffer(true, buffer_size);
998 /* The plugin wants to stay resident after leaving its main function, e.g.
999 runs from timer or own thread. The callback is registered to later
1000 instruct it to free its resources before a new plugin gets loaded. */
1001 void plugin_tsr(bool (*exit_callback)(bool))
1003 pfn_tsr_exit = exit_callback; /* remember the callback for later */
1006 char *plugin_get_current_filename(void)
1008 return current_plugin;
1011 static int open_wrapper(const char* pathname, int flags, ...)
1013 /* we don't have an 'open' function. it's a define. and we need
1014 * the real file_open, hence PREFIX() doesn't work here */
1015 int fd;
1016 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
1017 if (flags & O_CREAT)
1019 va_list ap;
1020 va_start(ap, flags);
1021 fd = open(pathname, flags, va_arg(ap, unsigned int));
1022 va_end(ap);
1024 else
1025 fd = open(pathname, flags);
1026 #else
1027 fd = file_open(pathname,flags);
1028 #endif
1030 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1031 if(fd >= 0)
1032 open_files |= 1<<fd;
1033 #endif
1034 return fd;
1037 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1038 static int close_wrapper(int fd)
1040 if((~open_files) & (1<<fd))
1042 logf("double close from plugin");
1044 if(fd >= 0)
1045 open_files &= (~(1<<fd));
1047 return PREFIX(close)(fd);
1050 static int creat_wrapper(const char *pathname, mode_t mode)
1052 int fd = PREFIX(creat)(pathname, mode);
1054 if(fd >= 0)
1055 open_files |= (1<<fd);
1057 return fd;
1059 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */