Hide symbols by default on 64 bit sim buildsto avoid clashing, fixes crashing on...
[kugel-rb.git] / apps / plugin.c
blobf0b86c08cbfe7a39b20878ac0cf82915bde18f72
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Björn Stenberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "plugin.h"
20 #include <ctype.h>
21 #include <string.h>
22 #include <sprintf.h>
23 #include <atoi.h>
24 #include "debug.h"
25 #include "i2c.h"
26 #include "lang.h"
27 #include "led.h"
28 #include "keyboard.h"
29 #include "buffer.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"
38 #if CONFIG_CHARGING
39 #include "power.h"
40 #endif
42 #ifdef HAVE_LCD_BITMAP
43 #include "scrollbar.h"
44 #include "peakmeter.h"
45 #include "bmp.h"
46 #include "bidi.h"
47 #endif
49 #ifdef SIMULATOR
50 static unsigned char pluginbuf[PLUGIN_BUFFER_SIZE];
51 void *sim_plugin_load(char *plugin, void **pd);
52 void sim_plugin_close(void *pd);
53 void sim_lcd_ex_init(int shades, unsigned long (*getpixel)(int, int));
54 void sim_lcd_ex_update_rect(int x, int y, int width, int height);
55 #else
56 #define sim_plugin_close(x)
57 extern unsigned char pluginbuf[];
58 #include "bitswap.h"
59 #endif
61 /* for actual plugins only, not for codecs */
62 static bool plugin_loaded = false;
63 static int plugin_size = 0;
64 static bool (*pfn_tsr_exit)(bool reenter) = NULL; /* TSR exit callback */
65 static char current_plugin[MAX_PATH];
67 char *plugin_get_current_filename(void);
69 extern struct thread_entry threads[MAXTHREADS];
71 static const struct plugin_api rockbox_api = {
73 /* lcd */
74 lcd_set_contrast,
75 lcd_update,
76 lcd_clear_display,
77 lcd_setmargins,
78 lcd_getstringsize,
79 lcd_putsxy,
80 lcd_puts,
81 lcd_puts_scroll,
82 lcd_stop_scroll,
83 #ifdef HAVE_LCD_CHARCELLS
84 lcd_define_pattern,
85 lcd_get_locked_pattern,
86 lcd_unlock_pattern,
87 lcd_putc,
88 lcd_put_cursor,
89 lcd_remove_cursor,
90 lcd_icon,
91 lcd_double_height,
92 #else
93 lcd_set_drawmode,
94 lcd_get_drawmode,
95 lcd_setfont,
96 lcd_drawpixel,
97 lcd_drawline,
98 lcd_hline,
99 lcd_vline,
100 lcd_drawrect,
101 lcd_fillrect,
102 lcd_mono_bitmap_part,
103 lcd_mono_bitmap,
104 #if LCD_DEPTH > 1
105 lcd_set_foreground,
106 lcd_get_foreground,
107 lcd_set_background,
108 lcd_get_background,
109 lcd_bitmap_part,
110 lcd_bitmap,
111 lcd_get_backdrop,
112 lcd_set_backdrop,
113 #endif
114 #if LCD_DEPTH == 16
115 lcd_bitmap_transparent_part,
116 lcd_bitmap_transparent,
117 #endif
118 bidi_l2v,
119 font_get_bits,
120 font_load,
121 lcd_puts_style,
122 lcd_puts_scroll_style,
123 &lcd_framebuffer[0][0],
124 lcd_blit,
125 lcd_update_rect,
126 gui_scrollbar_draw,
127 font_get,
128 font_getstringsize,
129 font_get_width,
130 screen_clear_area,
131 #endif
132 backlight_on,
133 backlight_off,
134 backlight_set_timeout,
135 #if CONFIG_CHARGING
136 backlight_set_timeout_plugged,
137 #endif
138 gui_syncsplash,
139 #ifdef HAVE_REMOTE_LCD
140 /* remote lcd */
141 lcd_remote_set_contrast,
142 lcd_remote_clear_display,
143 lcd_remote_puts,
144 lcd_remote_puts_scroll,
145 lcd_remote_stop_scroll,
146 lcd_remote_set_drawmode,
147 lcd_remote_get_drawmode,
148 lcd_remote_setfont,
149 lcd_remote_getstringsize,
150 lcd_remote_drawpixel,
151 lcd_remote_drawline,
152 lcd_remote_hline,
153 lcd_remote_vline,
154 lcd_remote_drawrect,
155 lcd_remote_fillrect,
156 lcd_remote_mono_bitmap_part,
157 lcd_remote_mono_bitmap,
158 lcd_remote_putsxy,
159 lcd_remote_puts_style,
160 lcd_remote_puts_scroll_style,
161 &lcd_remote_framebuffer[0][0],
162 lcd_remote_update,
163 lcd_remote_update_rect,
165 remote_backlight_on,
166 remote_backlight_off,
167 #endif
168 #if NB_SCREENS == 2
169 {&screens[SCREEN_MAIN], &screens[SCREEN_REMOTE]},
170 #else
171 {&screens[SCREEN_MAIN]},
172 #endif
173 #if defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
174 lcd_remote_set_foreground,
175 lcd_remote_get_foreground,
176 lcd_remote_set_background,
177 lcd_remote_get_background,
178 lcd_remote_bitmap_part,
179 lcd_remote_bitmap,
180 #endif
182 #if defined(HAVE_LCD_COLOR)
183 lcd_yuv_blit,
184 #endif
185 /* list */
186 gui_synclist_init,
187 gui_synclist_set_nb_items,
188 gui_synclist_set_icon_callback,
189 gui_synclist_get_nb_items,
190 gui_synclist_get_sel_pos,
191 gui_synclist_draw,
192 gui_synclist_select_item,
193 gui_synclist_add_item,
194 gui_synclist_del_item,
195 gui_synclist_limit_scroll,
196 gui_synclist_flash,
197 gui_synclist_do_button,
198 gui_synclist_set_title,
200 /* button */
201 button_get,
202 button_get_w_tmo,
203 button_status,
204 button_clear_queue,
205 #ifdef HAS_BUTTON_HOLD
206 button_hold,
207 #endif
209 /* file */
210 (open_func)PREFIX(open),
211 close,
212 (read_func)PREFIX(read),
213 PREFIX(lseek),
214 (creat_func)PREFIX(creat),
215 (write_func)PREFIX(write),
216 PREFIX(remove),
217 PREFIX(rename),
218 PREFIX(ftruncate),
219 PREFIX(filesize),
220 fdprintf,
221 read_line,
222 settings_parseline,
223 #ifndef SIMULATOR
224 ata_sleep,
225 ata_disk_is_active,
226 #endif
227 ata_spindown,
228 reload_directory,
229 create_numbered_filename,
231 /* dir */
232 opendir,
233 closedir,
234 readdir,
235 mkdir,
236 rmdir,
238 /* kernel/ system */
239 PREFIX(sleep),
240 yield,
241 #ifdef HAVE_PRIORITY_SCHEDULING
242 priority_yield,
243 #endif
244 &current_tick,
245 default_event_handler,
246 default_event_handler_ex,
247 threads,
248 create_thread,
249 remove_thread,
250 reset_poweroff_timer,
251 #ifndef SIMULATOR
252 system_memory_guard,
253 &cpu_frequency,
255 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
256 #ifdef CPU_BOOST_LOGGING
257 cpu_boost_,
258 #else
259 cpu_boost,
260 #endif
261 #endif
262 #endif
263 timer_register,
264 timer_unregister,
265 timer_set_period,
267 queue_init,
268 queue_delete,
269 queue_post,
270 queue_wait_w_tmo,
271 usb_acknowledge,
272 #ifdef RB_PROFILE
273 profile_thread,
274 profstop,
275 profile_func_enter,
276 profile_func_exit,
277 #endif
279 #ifdef SIMULATOR
280 /* special simulator hooks */
281 #if defined(HAVE_LCD_BITMAP) && LCD_DEPTH < 8
282 sim_lcd_ex_init,
283 sim_lcd_ex_update_rect,
284 #endif
285 #endif
287 /* strings and memory */
288 snprintf,
289 vsnprintf,
290 strcpy,
291 strncpy,
292 strlen,
293 strrchr,
294 strcmp,
295 strncmp,
296 strcasecmp,
297 strncasecmp,
298 memset,
299 memcpy,
300 memmove,
301 _ctype_,
302 atoi,
303 strchr,
304 strcat,
305 memchr,
306 memcmp,
307 strcasestr,
308 strtok_r,
309 /* unicode stuff */
310 utf8decode,
311 iso_decode,
312 utf16LEdecode,
313 utf16BEdecode,
314 utf8encode,
315 utf8length,
316 utf8seek,
318 /* sound */
319 #if CONFIG_CODEC == SWCODEC
320 sound_default,
321 #endif
322 sound_set,
324 sound_min,
325 sound_max,
326 #ifndef SIMULATOR
327 mp3_play_data,
328 mp3_play_pause,
329 mp3_play_stop,
330 mp3_is_playing,
331 #if CONFIG_CODEC != SWCODEC
332 bitswap,
333 #endif
334 #endif
335 #if CONFIG_CODEC == SWCODEC
336 &audio_master_sampr_list[0],
337 &hw_freq_sampr[0],
338 pcm_apply_settings,
339 pcm_play_data,
340 pcm_play_stop,
341 pcm_set_frequency,
342 pcm_is_playing,
343 pcm_is_paused,
344 pcm_play_pause,
345 pcm_get_bytes_waiting,
346 pcm_calculate_peaks,
347 #ifdef HAVE_RECORDING
348 &rec_freq_sampr[0],
349 pcm_init_recording,
350 pcm_close_recording,
351 pcm_record_data,
352 pcm_record_more,
353 pcm_stop_recording,
354 pcm_calculate_rec_peaks,
355 audio_set_recording_gain,
356 #endif /* HAVE_RECORDING */
357 #if INPUT_SRC_CAPS != 0
358 audio_set_output_source,
359 audio_set_input_source,
360 #endif
361 #endif /* CONFIG_CODEC == SWCODEC */
363 /* playback control */
364 playlist_amount,
365 playlist_resume,
366 playlist_start,
367 PREFIX(audio_play),
368 audio_stop,
369 audio_pause,
370 audio_resume,
371 audio_next,
372 audio_prev,
373 audio_ff_rewind,
374 audio_next_track,
375 audio_status,
376 audio_has_changed_track,
377 audio_current_track,
378 audio_flush_and_reload_tracks,
379 audio_get_file_pos,
380 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
381 mpeg_get_last_header,
382 #endif
383 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) || \
384 (CONFIG_CODEC == SWCODEC)
385 sound_set_pitch,
386 #endif
388 #if !defined(SIMULATOR) && (CONFIG_CODEC != SWCODEC)
389 /* MAS communication */
390 mas_readmem,
391 mas_writemem,
392 mas_readreg,
393 mas_writereg,
394 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
395 mas_codec_writereg,
396 mas_codec_readreg,
397 i2c_begin,
398 i2c_end,
399 i2c_write,
400 #endif
401 #endif /* !SIMULATOR && CONFIG_CODEC != SWCODEC */
403 /* menu */
404 do_menu,
405 /* statusbars */
406 &statusbars,
407 gui_syncstatusbar_draw,
408 /* options */
409 find_setting,
410 option_screen,
411 set_option,
412 set_bool_options,
413 set_int,
414 set_bool,
415 #ifdef HAVE_LCD_COLOR
416 set_color,
417 #endif
419 /* action handling */
420 get_custom_action,
421 get_action,
422 action_userabort,
424 /* power */
425 battery_level,
426 battery_level_safe,
427 battery_time,
428 #ifndef SIMULATOR
429 battery_voltage,
430 #endif
431 #if CONFIG_CHARGING
432 charger_inserted,
433 # if CONFIG_CHARGING == CHARGING_MONITOR
434 charging_state,
435 # endif
436 #endif
437 #ifdef HAVE_USB_POWER
438 usb_powered,
439 #endif
441 /* misc */
442 srand,
443 rand,
444 (qsort_func)qsort,
445 kbd_input,
446 get_time,
447 set_time,
448 #if CONFIG_RTC
449 mktime,
450 #endif
451 plugin_get_buffer,
452 plugin_get_audio_buffer,
453 plugin_tsr,
454 plugin_get_current_filename,
455 #ifdef IRAM_STEAL
456 plugin_iram_init,
457 #endif
458 #if defined(DEBUG) || defined(SIMULATOR)
459 debugf,
460 #endif
461 #ifdef ROCKBOX_HAS_LOGF
462 _logf,
463 #endif
464 &global_settings,
465 &global_status,
466 mp3info,
467 count_mp3_frames,
468 create_xing_header,
469 find_next_frame,
470 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
471 peak_meter_scale_value,
472 peak_meter_set_use_dbfs,
473 peak_meter_get_use_dbfs,
474 #endif
475 #ifdef HAVE_LCD_BITMAP
476 read_bmp_file,
477 screen_dump_set_hook,
478 #endif
479 show_logo,
480 tree_get_context,
481 set_current_file,
482 set_dirfilter,
484 #ifdef HAVE_WHEEL_POSITION
485 wheel_status,
486 wheel_send_events,
487 #endif
489 #ifdef IRIVER_H100_SERIES
490 /* Routines for the iriver_flash -plugin. */
491 detect_original_firmware,
492 detect_flashed_ramimage,
493 detect_flashed_romimage,
494 #endif
495 /* new stuff at the end, sort into place next time
496 the API gets incompatible */
498 #if (CONFIG_CODEC == SWCODEC)
499 spinlock_init,
500 spinlock_lock,
501 spinlock_unlock,
503 codec_load_file,
504 get_codec_filename,
505 get_metadata,
506 #endif
507 led,
509 #if defined(TOSHIBA_GIGABEAT_F) || defined(SANSA_E200)
510 lcd_yuv_set_options,
511 #endif
513 #ifdef CACHE_FUNCTIONS_AS_CALL
514 flush_icache,
515 invalidate_icache,
516 #endif
519 int plugin_load(const char* plugin, void* parameter)
521 int rc;
522 struct plugin_header *hdr;
523 #ifdef SIMULATOR
524 void *pd;
525 #else
526 int fd;
527 ssize_t readsize;
528 #endif
529 int xm, ym;
530 #ifdef HAVE_REMOTE_LCD
531 int rxm, rym;
532 #endif
534 #if LCD_DEPTH > 1
535 fb_data* old_backdrop;
536 #endif
538 if (pfn_tsr_exit != NULL) /* if we have a resident old plugin: */
540 if (pfn_tsr_exit(!strcmp(current_plugin, plugin)) == false )
542 /* not allowing another plugin to load */
543 return PLUGIN_OK;
545 pfn_tsr_exit = NULL;
546 plugin_loaded = false;
549 gui_syncsplash(0, ID2P(LANG_WAIT));
550 strcpy(current_plugin, plugin);
552 #ifdef SIMULATOR
553 hdr = sim_plugin_load((char *)plugin, &pd);
554 if (pd == NULL) {
555 gui_syncsplash(HZ*2, str(LANG_PLUGIN_CANT_OPEN), plugin);
556 return -1;
558 if (hdr == NULL
559 || hdr->magic != PLUGIN_MAGIC
560 || hdr->target_id != TARGET_ID) {
561 sim_plugin_close(pd);
562 gui_syncsplash(HZ*2, str(LANG_PLUGIN_WRONG_MODEL));
563 return -1;
565 if (hdr->api_version > PLUGIN_API_VERSION
566 || hdr->api_version < PLUGIN_MIN_API_VERSION) {
567 sim_plugin_close(pd);
568 gui_syncsplash(HZ*2, str(LANG_PLUGIN_WRONG_VERSION));
569 return -1;
571 #else
572 fd = open(plugin, O_RDONLY);
573 if (fd < 0) {
574 gui_syncsplash(HZ*2, str(LANG_PLUGIN_CANT_OPEN), plugin);
575 return fd;
578 readsize = read(fd, pluginbuf, PLUGIN_BUFFER_SIZE);
579 close(fd);
581 if (readsize < 0) {
582 gui_syncsplash(HZ*2, str(LANG_READ_FAILED), plugin);
583 return -1;
585 hdr = (struct plugin_header *)pluginbuf;
587 if ((unsigned)readsize <= sizeof(struct plugin_header)
588 || hdr->magic != PLUGIN_MAGIC
589 || hdr->target_id != TARGET_ID
590 || hdr->load_addr != pluginbuf
591 || hdr->end_addr > pluginbuf + PLUGIN_BUFFER_SIZE) {
592 gui_syncsplash(HZ*2, str(LANG_PLUGIN_WRONG_MODEL));
593 return -1;
595 if (hdr->api_version > PLUGIN_API_VERSION
596 || hdr->api_version < PLUGIN_MIN_API_VERSION) {
597 gui_syncsplash(HZ*2, str(LANG_PLUGIN_WRONG_VERSION));
598 return -1;
600 plugin_size = hdr->end_addr - pluginbuf;
602 /* zero out bss area only, above guards end of pluginbuf */
603 if (plugin_size > readsize)
604 memset(pluginbuf + readsize, 0, plugin_size - readsize);
605 #endif
607 plugin_loaded = true;
609 xm = lcd_getxmargin();
610 ym = lcd_getymargin();
611 lcd_setmargins(0,0);
613 #if defined HAVE_LCD_BITMAP && LCD_DEPTH > 1
614 old_backdrop = lcd_get_backdrop();
615 #endif
616 lcd_clear_display();
617 lcd_update();
619 #ifdef HAVE_REMOTE_LCD
620 rxm = lcd_remote_getxmargin();
621 rym = lcd_remote_getymargin();
622 lcd_remote_setmargins(0, 0);
623 lcd_remote_clear_display();
624 lcd_remote_update();
625 #endif
627 invalidate_icache();
629 rc = hdr->entry_point((struct plugin_api*) &rockbox_api, parameter);
630 /* explicitly casting the pointer here to avoid touching every plugin. */
632 button_clear_queue();
634 #ifdef HAVE_LCD_BITMAP
635 #if LCD_DEPTH > 1
636 lcd_set_backdrop(old_backdrop);
637 #ifdef HAVE_LCD_COLOR
638 lcd_set_drawinfo(DRMODE_SOLID, global_settings.fg_color,
639 global_settings.bg_color);
640 #else
641 lcd_set_drawinfo(DRMODE_SOLID, LCD_DEFAULT_FG, LCD_DEFAULT_BG);
642 #endif
643 #else /* LCD_DEPTH == 1 */
644 lcd_set_drawmode(DRMODE_SOLID);
645 #endif /* LCD_DEPTH */
646 #endif /* HAVE_LCD_BITMAP */
648 /* restore margins */
649 lcd_setmargins(xm,ym);
650 lcd_clear_display();
651 lcd_update();
653 #ifdef HAVE_REMOTE_LCD
654 #if LCD_REMOTE_DEPTH > 1
655 lcd_remote_set_drawinfo(DRMODE_SOLID, LCD_REMOTE_DEFAULT_FG,
656 LCD_REMOTE_DEFAULT_BG);
657 #else
658 lcd_remote_set_drawmode(DRMODE_SOLID);
659 #endif
660 lcd_remote_setmargins(rxm, rym);
661 lcd_remote_clear_display();
662 lcd_remote_update();
663 #endif
665 if (pfn_tsr_exit == NULL)
666 plugin_loaded = false;
668 sim_plugin_close(pd);
670 switch (rc) {
671 case PLUGIN_OK:
672 break;
674 case PLUGIN_USB_CONNECTED:
675 return PLUGIN_USB_CONNECTED;
677 default:
678 gui_syncsplash(HZ*2, str(LANG_PLUGIN_ERROR));
679 break;
681 return PLUGIN_OK;
684 /* Returns a pointer to the portion of the plugin buffer that is not already
685 being used. If no plugin is loaded, returns the entire plugin buffer */
686 void* plugin_get_buffer(size_t *buffer_size)
688 int buffer_pos;
690 if (plugin_loaded)
692 if (plugin_size >= PLUGIN_BUFFER_SIZE)
693 return NULL;
695 *buffer_size = PLUGIN_BUFFER_SIZE-plugin_size;
696 buffer_pos = plugin_size;
698 else
700 *buffer_size = PLUGIN_BUFFER_SIZE;
701 buffer_pos = 0;
704 return &pluginbuf[buffer_pos];
707 /* Returns a pointer to the mp3 buffer.
708 Playback gets stopped, to avoid conflicts.
709 Talk buffer is stolen as well.
711 void* plugin_get_audio_buffer(size_t *buffer_size)
713 #if CONFIG_CODEC == SWCODEC
714 return audio_get_buffer(true, buffer_size);
715 #else
716 audio_stop();
717 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
718 *buffer_size = audiobufend - audiobuf;
719 return audiobuf;
720 #endif
723 #ifdef IRAM_STEAL
724 /* Initializes plugin IRAM */
725 void plugin_iram_init(char *iramstart, char *iramcopy, size_t iram_size,
726 char *iedata, size_t iedata_size)
728 audio_iram_steal();
729 memcpy(iramstart, iramcopy, iram_size);
730 memset(iedata, 0, iedata_size);
731 memset(iramcopy, 0, iram_size);
732 #if NUM_CORES > 1
733 /* writeback cleared iedata and iramcopy areas */
734 flush_icache();
735 #endif
737 #endif /* IRAM_STEAL */
739 /* The plugin wants to stay resident after leaving its main function, e.g.
740 runs from timer or own thread. The callback is registered to later
741 instruct it to free its resources before a new plugin gets loaded. */
742 void plugin_tsr(bool (*exit_callback)(bool))
744 pfn_tsr_exit = exit_callback; /* remember the callback for later */
747 char *plugin_get_current_filename(void)
749 return current_plugin;