lcd drivers: Convert lcd_[remote_]framebuffer to a pointer
[maemo-rb.git] / apps / plugin.c
blob18bb47f64d717bd146e59a8d58c1e65c357d73e1
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_static_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_static_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 touchscreen_get_mode,
330 #endif
332 #ifdef HAVE_BUTTON_LIGHT
333 buttonlight_set_timeout,
334 buttonlight_off,
335 buttonlight_on,
336 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
337 buttonlight_set_brightness,
338 #endif /* HAVE_BUTTONLIGHT_BRIGHTNESS */
339 #endif /* HAVE_BUTTON_LIGHT */
341 /* file */
342 open_utf8,
343 (open_func)open_wrapper,
344 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
345 close_wrapper,
346 #else
347 PREFIX(close),
348 #endif
349 (read_func)PREFIX(read),
350 PREFIX(lseek),
351 #ifdef HAVE_PLUGIN_CHECK_OPEN_CLOSE
352 (creat_func)creat_wrapper,
353 #else
354 PREFIX(creat),
355 #endif
356 (write_func)PREFIX(write),
357 PREFIX(remove),
358 PREFIX(rename),
359 PREFIX(ftruncate),
360 PREFIX(filesize),
361 fdprintf,
362 read_line,
363 settings_parseline,
364 storage_sleep,
365 storage_spin,
366 storage_spindown,
367 #if USING_STORAGE_CALLBACK
368 register_storage_idle_func,
369 unregister_storage_idle_func,
370 #endif /* USING_STORAGE_CALLBACK */
371 reload_directory,
372 create_numbered_filename,
373 file_exists,
374 strip_extension,
375 crc_32,
376 filetype_get_attr,
378 /* dir */
379 (opendir_func)opendir,
380 (closedir_func)closedir,
381 (readdir_func)readdir,
382 mkdir,
383 rmdir,
384 dir_exists,
385 dir_get_info,
387 /* browsing */
388 browse_context_init,
389 rockbox_browse,
391 /* kernel/ system */
392 #if defined(CPU_ARM) && CONFIG_PLATFORM & PLATFORM_NATIVE
393 __div0,
394 #endif
395 sleep,
396 yield,
397 &current_tick,
398 default_event_handler,
399 default_event_handler_ex,
400 create_thread,
401 thread_self,
402 thread_exit,
403 thread_wait,
404 #if (CONFIG_CODEC == SWCODEC)
405 thread_thaw,
406 #ifdef HAVE_PRIORITY_SCHEDULING
407 thread_set_priority,
408 #endif
409 mutex_init,
410 mutex_lock,
411 mutex_unlock,
412 #endif
414 reset_poweroff_timer,
415 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
416 system_memory_guard,
417 &cpu_frequency,
419 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
420 #ifdef CPU_BOOST_LOGGING
421 cpu_boost_,
422 #else
423 cpu_boost,
424 #endif
425 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
426 #endif /* PLATFORM_NATIVE */
427 #ifdef HAVE_SCHEDULER_BOOSTCTRL
428 trigger_cpu_boost,
429 cancel_cpu_boost,
430 #endif
432 commit_dcache,
433 commit_discard_dcache,
434 commit_discard_idcache,
436 lc_open,
437 lc_open_from_mem,
438 lc_get_header,
439 lc_close,
441 timer_register,
442 timer_unregister,
443 timer_set_period,
445 queue_init,
446 queue_delete,
447 queue_post,
448 queue_wait_w_tmo,
449 #if CONFIG_CODEC == SWCODEC
450 queue_enable_queue_send,
451 queue_empty,
452 queue_wait,
453 queue_send,
454 queue_reply,
455 #endif
456 usb_acknowledge,
457 #ifdef USB_ENABLE_HID
458 usb_hid_send,
459 #endif
460 #ifdef RB_PROFILE
461 profile_thread,
462 profstop,
463 __cyg_profile_func_enter,
464 __cyg_profile_func_exit,
465 #endif
466 add_event,
467 remove_event,
468 send_event,
470 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
471 /* special simulator hooks */
472 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
473 sim_lcd_ex_init,
474 sim_lcd_ex_update_rect,
475 #endif
476 #endif
478 /* strings and memory */
479 snprintf,
480 vsnprintf,
481 strcpy,
482 strlcpy,
483 strlen,
484 strrchr,
485 strcmp,
486 strncmp,
487 strcasecmp,
488 strncasecmp,
489 memset,
490 memcpy,
491 memmove,
492 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
493 _ctype_,
494 #endif
495 atoi,
496 strchr,
497 strcat,
498 strlcat,
499 memchr,
500 memcmp,
501 strcasestr,
502 strtok_r,
503 /* unicode stuff */
504 utf8decode,
505 iso_decode,
506 utf16LEdecode,
507 utf16BEdecode,
508 utf8encode,
509 utf8length,
510 utf8seek,
512 /* the buflib memory management library */
513 buflib_init,
514 buflib_available,
515 buflib_alloc,
516 buflib_alloc_ex,
517 buflib_alloc_maximum,
518 buflib_buffer_in,
519 buflib_buffer_out,
520 buflib_free,
521 buflib_shrink,
522 buflib_get_data,
523 buflib_get_name,
525 /* sound */
526 sound_set,
527 sound_default,
528 sound_min,
529 sound_max,
530 sound_unit,
531 sound_val2phys,
532 #ifdef AUDIOHW_HAVE_EQ
533 sound_enum_hw_eq_band_setting,
534 #endif
535 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
536 mp3_play_data,
537 mp3_play_pause,
538 mp3_play_stop,
539 mp3_is_playing,
540 #if CONFIG_CODEC != SWCODEC
541 bitswap,
542 #endif
543 #endif
544 #if CONFIG_CODEC == SWCODEC
545 &audio_master_sampr_list[0],
546 &hw_freq_sampr[0],
547 pcm_apply_settings,
548 pcm_play_data,
549 pcm_play_stop,
550 pcm_set_frequency,
551 pcm_is_playing,
552 pcm_is_paused,
553 pcm_play_pause,
554 pcm_get_bytes_waiting,
555 pcm_calculate_peaks,
556 pcm_get_peak_buffer,
557 pcm_play_lock,
558 pcm_play_unlock,
559 beep_play,
560 #ifdef HAVE_RECORDING
561 &rec_freq_sampr[0],
562 pcm_init_recording,
563 pcm_close_recording,
564 pcm_record_data,
565 pcm_stop_recording,
566 pcm_calculate_rec_peaks,
567 audio_set_recording_gain,
568 #endif /* HAVE_RECORDING */
569 #if INPUT_SRC_CAPS != 0
570 audio_set_output_source,
571 audio_set_input_source,
572 #endif
573 dsp_set_crossfeed,
574 dsp_set_eq,
575 dsp_dither_enable,
576 dsp_configure,
577 dsp_process,
578 dsp_input_count,
579 dsp_output_count,
581 mixer_channel_status,
582 mixer_channel_get_buffer,
583 mixer_channel_calculate_peaks,
584 mixer_channel_play_data,
585 mixer_channel_play_pause,
586 mixer_channel_stop,
587 mixer_channel_set_amplitude,
588 mixer_channel_get_bytes_waiting,
590 system_sound_play,
591 keyclick_click,
592 #endif
593 /* playback control */
594 playlist_amount,
595 playlist_resume,
596 playlist_start,
597 playlist_add,
598 playlist_sync,
599 playlist_remove_all_tracks,
600 playlist_create,
601 playlist_insert_track,
602 playlist_insert_directory,
603 playlist_shuffle,
604 audio_play,
605 audio_stop,
606 audio_pause,
607 audio_resume,
608 audio_next,
609 audio_prev,
610 audio_ff_rewind,
611 audio_next_track,
612 audio_status,
613 audio_current_track,
614 audio_flush_and_reload_tracks,
615 audio_get_file_pos,
616 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
617 mpeg_get_last_header,
618 #endif
619 #if ((CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
620 (CONFIG_CODEC == SWCODEC)) && defined (HAVE_PITCHSCREEN)
621 sound_set_pitch,
622 #endif
624 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
625 /* MAS communication */
626 mas_readmem,
627 mas_writemem,
628 mas_readreg,
629 mas_writereg,
630 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
631 mas_codec_writereg,
632 mas_codec_readreg,
633 i2c_begin,
634 i2c_end,
635 i2c_write,
636 #endif
637 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
639 /* menu */
640 do_menu,
641 /* statusbars */
642 &statusbars,
643 gui_syncstatusbar_draw,
645 /* options */
646 get_settings_list,
647 find_setting,
648 option_screen,
649 set_option,
650 set_bool_options,
651 set_int,
652 set_bool,
653 #ifdef HAVE_LCD_COLOR
654 set_color,
655 #endif
657 /* action handling */
658 get_custom_action,
659 get_action,
660 #ifdef HAVE_TOUCHSCREEN
661 action_get_touchscreen_press,
662 #endif
663 action_userabort,
665 /* power */
666 battery_level,
667 battery_level_safe,
668 battery_time,
669 battery_voltage,
670 #if CONFIG_CHARGING
671 charger_inserted,
672 # if CONFIG_CHARGING >= CHARGING_MONITOR
673 charging_state,
674 # endif
675 #endif
676 #ifdef HAVE_USB_POWER
677 usb_powered,
678 #endif
680 /* misc */
681 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
682 &errno,
683 #endif
684 srand,
685 rand,
686 (qsort_func)qsort,
687 kbd_input,
688 get_time,
689 set_time,
690 #if CONFIG_RTC
691 mktime,
692 #endif
693 plugin_get_buffer,
694 plugin_get_audio_buffer,
695 plugin_tsr,
696 plugin_get_current_filename,
697 #if defined(DEBUG) || defined(SIMULATOR)
698 debugf,
699 #endif
700 #ifdef ROCKBOX_HAS_LOGF
701 _logf,
702 #endif
703 &global_settings,
704 &global_status,
705 talk_disable,
706 #if CONFIG_CODEC == SWCODEC
707 codec_thread_do_callback,
708 codec_load_file,
709 codec_run_proc,
710 codec_close,
711 get_codec_filename,
712 find_array_ptr,
713 remove_array_ptr,
714 round_value_to_list32,
715 #endif /* CONFIG_CODEC == SWCODEC */
716 get_metadata,
717 mp3info,
718 count_mp3_frames,
719 create_xing_header,
720 find_next_frame,
721 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
722 peak_meter_scale_value,
723 peak_meter_set_use_dbfs,
724 peak_meter_get_use_dbfs,
725 #endif
726 #ifdef HAVE_LCD_BITMAP
727 read_bmp_file,
728 read_bmp_fd,
729 #ifdef HAVE_JPEG
730 read_jpeg_file,
731 read_jpeg_fd,
732 #endif
733 screen_dump_set_hook,
734 #endif
735 show_logo,
736 tree_get_context,
737 tree_get_entries,
738 tree_get_entry_at,
739 set_current_file,
740 set_dirfilter,
742 #ifdef HAVE_WHEEL_POSITION
743 wheel_status,
744 wheel_send_events,
745 #endif
747 #ifdef IRIVER_H100_SERIES
748 /* Routines for the iriver_flash -plugin. */
749 detect_original_firmware,
750 detect_flashed_ramimage,
751 detect_flashed_romimage,
752 #endif
753 led,
754 #if (CONFIG_CODEC == SWCODEC)
755 bufopen,
756 bufalloc,
757 bufclose,
758 bufseek,
759 bufadvance,
760 bufread,
761 bufgetdata,
762 bufgettail,
763 bufcuttail,
765 buf_handle_offset,
766 buf_set_base_handle,
767 buf_used,
768 #endif
770 #ifdef HAVE_TAGCACHE
771 tagcache_search,
772 tagcache_search_set_uniqbuf,
773 tagcache_search_add_filter,
774 tagcache_get_next,
775 tagcache_retrieve,
776 tagcache_search_finish,
777 tagcache_get_numeric,
778 #if defined(HAVE_TC_RAMCACHE) && defined(HAVE_DIRCACHE)
779 tagcache_fill_tags,
780 #endif
781 #endif
783 #ifdef HAVE_ALBUMART
784 search_albumart_files,
785 #endif
787 #ifdef HAVE_SEMAPHORE_OBJECTS
788 semaphore_init,
789 semaphore_wait,
790 semaphore_release,
791 #endif
793 rbversion,
795 /* new stuff at the end, sort into place next time
796 the API gets incompatible */
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 */