Fix warning and typos.
[maemo-rb.git] / apps / plugin.c
blobb9c2e7cb5b7e3a43e875c352ebc6741acc113567
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 /* the buflib memory management library */
522 buflib_init,
523 buflib_available,
524 buflib_alloc,
525 buflib_alloc_ex,
526 buflib_alloc_maximum,
527 buflib_buffer_in,
528 buflib_buffer_out,
529 buflib_free,
530 buflib_shrink,
531 buflib_get_data,
532 buflib_get_name,
534 /* sound */
535 sound_set,
536 sound_default,
537 sound_min,
538 sound_max,
539 sound_unit,
540 sound_val2phys,
541 #ifdef AUDIOHW_HAVE_EQ
542 sound_enum_hw_eq_band_setting,
543 #endif
544 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
545 mp3_play_data,
546 mp3_play_pause,
547 mp3_play_stop,
548 mp3_is_playing,
549 #if CONFIG_CODEC != SWCODEC
550 bitswap,
551 #endif
552 #endif
553 #if CONFIG_CODEC == SWCODEC
554 &audio_master_sampr_list[0],
555 &hw_freq_sampr[0],
556 pcm_apply_settings,
557 pcm_play_data,
558 pcm_play_stop,
559 pcm_set_frequency,
560 pcm_is_playing,
561 pcm_is_paused,
562 pcm_play_pause,
563 pcm_get_bytes_waiting,
564 pcm_calculate_peaks,
565 pcm_get_peak_buffer,
566 pcm_play_lock,
567 pcm_play_unlock,
568 beep_play,
569 #ifdef HAVE_RECORDING
570 &rec_freq_sampr[0],
571 pcm_init_recording,
572 pcm_close_recording,
573 pcm_record_data,
574 pcm_stop_recording,
575 pcm_calculate_rec_peaks,
576 audio_set_recording_gain,
577 #endif /* HAVE_RECORDING */
578 #if INPUT_SRC_CAPS != 0
579 audio_set_output_source,
580 audio_set_input_source,
581 #endif
582 dsp_set_crossfeed,
583 dsp_set_eq,
584 dsp_dither_enable,
585 dsp_configure,
586 dsp_process,
587 dsp_input_count,
588 dsp_output_count,
590 mixer_channel_status,
591 mixer_channel_get_buffer,
592 mixer_channel_calculate_peaks,
593 mixer_channel_play_data,
594 mixer_channel_play_pause,
595 mixer_channel_stop,
596 mixer_channel_set_amplitude,
597 mixer_channel_get_bytes_waiting,
599 system_sound_play,
600 keyclick_click,
601 #endif
602 /* playback control */
603 playlist_amount,
604 playlist_resume,
605 playlist_start,
606 playlist_add,
607 playlist_sync,
608 playlist_remove_all_tracks,
609 playlist_create,
610 playlist_insert_track,
611 playlist_insert_directory,
612 playlist_shuffle,
613 audio_play,
614 audio_stop,
615 audio_pause,
616 audio_resume,
617 audio_next,
618 audio_prev,
619 audio_ff_rewind,
620 audio_next_track,
621 audio_status,
622 audio_current_track,
623 audio_flush_and_reload_tracks,
624 audio_get_file_pos,
625 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
626 mpeg_get_last_header,
627 #endif
628 #if ((CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
629 (CONFIG_CODEC == SWCODEC)) && defined (HAVE_PITCHSCREEN)
630 sound_set_pitch,
631 #endif
633 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
634 /* MAS communication */
635 mas_readmem,
636 mas_writemem,
637 mas_readreg,
638 mas_writereg,
639 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
640 mas_codec_writereg,
641 mas_codec_readreg,
642 i2c_begin,
643 i2c_end,
644 i2c_write,
645 #endif
646 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
648 /* menu */
649 do_menu,
650 /* statusbars */
651 &statusbars,
652 gui_syncstatusbar_draw,
654 /* options */
655 get_settings_list,
656 find_setting,
657 option_screen,
658 set_option,
659 set_bool_options,
660 set_int,
661 set_bool,
662 #ifdef HAVE_LCD_COLOR
663 set_color,
664 #endif
666 /* action handling */
667 get_custom_action,
668 get_action,
669 #ifdef HAVE_TOUCHSCREEN
670 action_get_touchscreen_press,
671 #endif
672 action_userabort,
674 /* power */
675 battery_level,
676 battery_level_safe,
677 battery_time,
678 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
679 battery_voltage,
680 #endif
681 #if CONFIG_CHARGING
682 charger_inserted,
683 # if CONFIG_CHARGING >= CHARGING_MONITOR
684 charging_state,
685 # endif
686 #endif
687 #ifdef HAVE_USB_POWER
688 usb_powered,
689 #endif
691 /* misc */
692 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
693 &errno,
694 #endif
695 srand,
696 rand,
697 (qsort_func)qsort,
698 kbd_input,
699 get_time,
700 set_time,
701 #if CONFIG_RTC
702 mktime,
703 #endif
704 plugin_get_buffer,
705 plugin_get_audio_buffer,
706 plugin_tsr,
707 plugin_get_current_filename,
708 #if defined(DEBUG) || defined(SIMULATOR)
709 debugf,
710 #endif
711 #ifdef ROCKBOX_HAS_LOGF
712 _logf,
713 #endif
714 &global_settings,
715 &global_status,
716 talk_disable,
717 #if CONFIG_CODEC == SWCODEC
718 codec_thread_do_callback,
719 codec_load_file,
720 codec_run_proc,
721 codec_close,
722 get_codec_filename,
723 find_array_ptr,
724 remove_array_ptr,
725 round_value_to_list32,
726 #endif /* CONFIG_CODEC == SWCODEC */
727 get_metadata,
728 mp3info,
729 count_mp3_frames,
730 create_xing_header,
731 find_next_frame,
732 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
733 peak_meter_scale_value,
734 peak_meter_set_use_dbfs,
735 peak_meter_get_use_dbfs,
736 #endif
737 #ifdef HAVE_LCD_BITMAP
738 read_bmp_file,
739 read_bmp_fd,
740 #ifdef HAVE_JPEG
741 read_jpeg_file,
742 read_jpeg_fd,
743 #endif
744 screen_dump_set_hook,
745 #endif
746 show_logo,
747 tree_get_context,
748 tree_get_entries,
749 tree_get_entry_at,
750 set_current_file,
751 set_dirfilter,
753 #ifdef HAVE_WHEEL_POSITION
754 wheel_status,
755 wheel_send_events,
756 #endif
758 #ifdef IRIVER_H100_SERIES
759 /* Routines for the iriver_flash -plugin. */
760 detect_original_firmware,
761 detect_flashed_ramimage,
762 detect_flashed_romimage,
763 #endif
764 led,
765 #if (CONFIG_CODEC == SWCODEC)
766 bufopen,
767 bufalloc,
768 bufclose,
769 bufseek,
770 bufadvance,
771 bufread,
772 bufgetdata,
773 bufgettail,
774 bufcuttail,
776 buf_handle_offset,
777 buf_set_base_handle,
778 buf_used,
779 #endif
781 #ifdef HAVE_TAGCACHE
782 tagcache_search,
783 tagcache_search_set_uniqbuf,
784 tagcache_search_add_filter,
785 tagcache_get_next,
786 tagcache_retrieve,
787 tagcache_search_finish,
788 tagcache_get_numeric,
789 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
790 tagcache_fill_tags,
791 #endif
792 #endif
794 #ifdef HAVE_ALBUMART
795 search_albumart_files,
796 #endif
798 #ifdef HAVE_SEMAPHORE_OBJECTS
799 semaphore_init,
800 semaphore_wait,
801 semaphore_release,
802 #endif
804 rbversion,
806 /* new stuff at the end, sort into place next time
807 the API gets incompatible */
810 int plugin_load(const char* plugin, const void* parameter)
812 struct plugin_header *p_hdr;
813 struct lc_header *hdr;
815 if (current_plugin_handle && pfn_tsr_exit)
816 { /* if we have a resident old plugin and a callback */
817 if (pfn_tsr_exit(!strcmp(current_plugin, plugin)) == false )
819 /* not allowing another plugin to load */
820 return PLUGIN_OK;
822 lc_close(current_plugin_handle);
823 current_plugin_handle = pfn_tsr_exit = NULL;
826 splash(0, ID2P(LANG_WAIT));
827 strcpy(current_plugin, plugin);
829 current_plugin_handle = lc_open(plugin, pluginbuf, PLUGIN_BUFFER_SIZE);
830 if (current_plugin_handle == NULL) {
831 splashf(HZ*2, str(LANG_PLUGIN_CANT_OPEN), plugin);
832 return -1;
835 p_hdr = lc_get_header(current_plugin_handle);
837 hdr = p_hdr ? &p_hdr->lc_hdr : NULL;
840 if (hdr == NULL
841 || hdr->magic != PLUGIN_MAGIC
842 || hdr->target_id != TARGET_ID
843 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
844 || hdr->load_addr != pluginbuf
845 || hdr->end_addr > pluginbuf + PLUGIN_BUFFER_SIZE
846 #endif
849 lc_close(current_plugin_handle);
850 splash(HZ*2, str(LANG_PLUGIN_WRONG_MODEL));
851 return -1;
853 if (hdr->api_version > PLUGIN_API_VERSION
854 || hdr->api_version < PLUGIN_MIN_API_VERSION)
856 lc_close(current_plugin_handle);
857 splash(HZ*2, str(LANG_PLUGIN_WRONG_VERSION));
858 return -1;
860 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
861 plugin_size = hdr->end_addr - pluginbuf;
862 #else
863 plugin_size = 0;
864 #endif
866 *(p_hdr->api) = &rockbox_api;
868 lcd_clear_display();
869 lcd_update();
871 #ifdef HAVE_REMOTE_LCD
872 lcd_remote_clear_display();
873 lcd_remote_update();
874 #endif
875 push_current_activity(ACTIVITY_PLUGIN);
876 /* some plugins assume the entry cache doesn't move and save pointers to it
877 * they should be fixed properly instead of this lock */
878 tree_lock_cache(tree_get_context());
880 FOR_NB_SCREENS(i)
881 viewportmanager_theme_enable(i, false, NULL);
883 #ifdef HAVE_TOUCHSCREEN
884 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
885 #endif
887 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
888 open_files = 0;
889 #endif
891 int rc = p_hdr->entry_point(parameter);
893 tree_unlock_cache(tree_get_context());
894 pop_current_activity();
896 if (!pfn_tsr_exit)
897 { /* close handle if plugin is no tsr one */
898 lc_close(current_plugin_handle);
899 current_plugin_handle = NULL;
902 /* Go back to the global setting in case the plugin changed it */
903 #ifdef HAVE_TOUCHSCREEN
904 touchscreen_set_mode(global_settings.touch_mode);
905 #endif
907 #ifdef HAVE_LCD_BITMAP
908 screen_helper_setfont(FONT_UI);
909 #if LCD_DEPTH > 1
910 #ifdef HAVE_LCD_COLOR
911 lcd_set_drawinfo(DRMODE_SOLID, global_settings.fg_color,
912 global_settings.bg_color);
913 #else
914 lcd_set_drawinfo(DRMODE_SOLID, LCD_DEFAULT_FG, LCD_DEFAULT_BG);
915 #endif
916 #else /* LCD_DEPTH == 1 */
917 lcd_set_drawmode(DRMODE_SOLID);
918 #endif /* LCD_DEPTH */
919 #endif /* HAVE_LCD_BITMAP */
922 #ifdef HAVE_REMOTE_LCD
923 #if LCD_REMOTE_DEPTH > 1
924 lcd_remote_set_drawinfo(DRMODE_SOLID, LCD_REMOTE_DEFAULT_FG,
925 LCD_REMOTE_DEFAULT_BG);
926 #else
927 lcd_remote_set_drawmode(DRMODE_SOLID);
928 #endif
929 #endif
931 lcd_clear_display();
932 #ifdef HAVE_REMOTE_LCD
933 lcd_remote_clear_display();
934 #endif
936 FOR_NB_SCREENS(i)
937 viewportmanager_theme_undo(i, true);
939 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
940 if(open_files != 0 && !current_plugin_handle)
942 int fd;
943 logf("Plugin '%s' leaks file handles", plugin);
945 static const char *lines[] =
946 { ID2P(LANG_PLUGIN_ERROR),
947 "#leak-file-handles" };
948 static const struct text_message message={ lines, 2 };
949 button_clear_queue(); /* Empty the keyboard buffer */
950 gui_syncyesno_run(&message, NULL, NULL);
952 for(fd=0; fd < MAX_OPEN_FILES; fd++)
953 if(open_files & (1<<fd))
954 close_wrapper(fd);
956 #endif
958 if (rc == PLUGIN_ERROR)
959 splash(HZ*2, str(LANG_PLUGIN_ERROR));
961 return rc;
964 /* Returns a pointer to the portion of the plugin buffer that is not already
965 being used. If no plugin is loaded, returns the entire plugin buffer */
966 void* plugin_get_buffer(size_t *buffer_size)
968 int buffer_pos;
970 if (current_plugin_handle)
972 if (plugin_size >= PLUGIN_BUFFER_SIZE)
973 return NULL;
975 *buffer_size = PLUGIN_BUFFER_SIZE-plugin_size;
976 buffer_pos = plugin_size;
978 else
980 *buffer_size = PLUGIN_BUFFER_SIZE;
981 buffer_pos = 0;
984 return &pluginbuf[buffer_pos];
987 /* Returns a pointer to the mp3 buffer.
988 Playback gets stopped, to avoid conflicts.
989 Talk buffer is stolen as well.
991 void* plugin_get_audio_buffer(size_t *buffer_size)
993 audio_stop();
994 return audio_get_buffer(true, buffer_size);
997 /* The plugin wants to stay resident after leaving its main function, e.g.
998 runs from timer or own thread. The callback is registered to later
999 instruct it to free its resources before a new plugin gets loaded. */
1000 void plugin_tsr(bool (*exit_callback)(bool))
1002 pfn_tsr_exit = exit_callback; /* remember the callback for later */
1005 char *plugin_get_current_filename(void)
1007 return current_plugin;
1010 static int open_wrapper(const char* pathname, int flags, ...)
1012 /* we don't have an 'open' function. it's a define. and we need
1013 * the real file_open, hence PREFIX() doesn't work here */
1014 int fd;
1015 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
1016 if (flags & O_CREAT)
1018 va_list ap;
1019 va_start(ap, flags);
1020 fd = open(pathname, flags, va_arg(ap, unsigned int));
1021 va_end(ap);
1023 else
1024 fd = open(pathname, flags);
1025 #else
1026 fd = file_open(pathname,flags);
1027 #endif
1029 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1030 if(fd >= 0)
1031 open_files |= 1<<fd;
1032 #endif
1033 return fd;
1036 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
1037 static int close_wrapper(int fd)
1039 if((~open_files) & (1<<fd))
1041 logf("double close from plugin");
1043 if(fd >= 0)
1044 open_files &= (~(1<<fd));
1046 return PREFIX(close)(fd);
1049 static int creat_wrapper(const char *pathname, mode_t mode)
1051 int fd = PREFIX(creat)(pathname, mode);
1053 if(fd >= 0)
1054 open_files |= (1<<fd);
1056 return fd;
1058 #endif /* HAVE_PLUGIN_CHECK_OPEN_CLOSE */