Oops, missed that when changing the default proxy to system ...
[Rockbox.git] / apps / plugin.c
blobbdb59e60054641cf216a79319f186c5ffd374614
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) && !defined(SIMULATOR)
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,
510 int plugin_load(const char* plugin, void* parameter)
512 int rc;
513 struct plugin_header *hdr;
514 #ifdef SIMULATOR
515 void *pd;
516 #else
517 int fd;
518 ssize_t readsize;
519 #endif
520 int xm, ym;
521 #ifdef HAVE_REMOTE_LCD
522 int rxm, rym;
523 #endif
525 #if LCD_DEPTH > 1
526 fb_data* old_backdrop;
527 #endif
529 if (pfn_tsr_exit != NULL) /* if we have a resident old plugin: */
531 if (pfn_tsr_exit(!strcmp(current_plugin, plugin)) == false )
533 /* not allowing another plugin to load */
534 return PLUGIN_OK;
536 pfn_tsr_exit = NULL;
537 plugin_loaded = false;
540 gui_syncsplash(0, ID2P(LANG_WAIT));
541 strcpy(current_plugin, plugin);
543 #ifdef SIMULATOR
544 hdr = sim_plugin_load((char *)plugin, &pd);
545 if (pd == NULL) {
546 gui_syncsplash(HZ*2, str(LANG_PLUGIN_CANT_OPEN), plugin);
547 return -1;
549 if (hdr == NULL
550 || hdr->magic != PLUGIN_MAGIC
551 || hdr->target_id != TARGET_ID) {
552 sim_plugin_close(pd);
553 gui_syncsplash(HZ*2, str(LANG_PLUGIN_WRONG_MODEL));
554 return -1;
556 if (hdr->api_version > PLUGIN_API_VERSION
557 || hdr->api_version < PLUGIN_MIN_API_VERSION) {
558 sim_plugin_close(pd);
559 gui_syncsplash(HZ*2, str(LANG_PLUGIN_WRONG_VERSION));
560 return -1;
562 #else
563 fd = open(plugin, O_RDONLY);
564 if (fd < 0) {
565 gui_syncsplash(HZ*2, str(LANG_PLUGIN_CANT_OPEN), plugin);
566 return fd;
569 readsize = read(fd, pluginbuf, PLUGIN_BUFFER_SIZE);
570 close(fd);
572 if (readsize < 0) {
573 gui_syncsplash(HZ*2, str(LANG_READ_FAILED), plugin);
574 return -1;
576 hdr = (struct plugin_header *)pluginbuf;
578 if ((unsigned)readsize <= sizeof(struct plugin_header)
579 || hdr->magic != PLUGIN_MAGIC
580 || hdr->target_id != TARGET_ID
581 || hdr->load_addr != pluginbuf
582 || hdr->end_addr > pluginbuf + PLUGIN_BUFFER_SIZE) {
583 gui_syncsplash(HZ*2, str(LANG_PLUGIN_WRONG_MODEL));
584 return -1;
586 if (hdr->api_version > PLUGIN_API_VERSION
587 || hdr->api_version < PLUGIN_MIN_API_VERSION) {
588 gui_syncsplash(HZ*2, str(LANG_PLUGIN_WRONG_VERSION));
589 return -1;
591 plugin_size = hdr->end_addr - pluginbuf;
593 /* zero out bss area only, above guards end of pluginbuf */
594 if (plugin_size > readsize)
595 memset(pluginbuf + readsize, 0, plugin_size - readsize);
596 #endif
598 plugin_loaded = true;
600 xm = lcd_getxmargin();
601 ym = lcd_getymargin();
602 lcd_setmargins(0,0);
604 #if defined HAVE_LCD_BITMAP && LCD_DEPTH > 1
605 old_backdrop = lcd_get_backdrop();
606 #endif
607 lcd_clear_display();
608 lcd_update();
610 #ifdef HAVE_REMOTE_LCD
611 rxm = lcd_remote_getxmargin();
612 rym = lcd_remote_getymargin();
613 lcd_remote_setmargins(0, 0);
614 lcd_remote_clear_display();
615 lcd_remote_update();
616 #endif
618 invalidate_icache();
620 rc = hdr->entry_point((struct plugin_api*) &rockbox_api, parameter);
621 /* explicitly casting the pointer here to avoid touching every plugin. */
623 button_clear_queue();
625 #ifdef HAVE_LCD_BITMAP
626 #if LCD_DEPTH > 1
627 lcd_set_backdrop(old_backdrop);
628 #ifdef HAVE_LCD_COLOR
629 lcd_set_drawinfo(DRMODE_SOLID, global_settings.fg_color,
630 global_settings.bg_color);
631 #else
632 lcd_set_drawinfo(DRMODE_SOLID, LCD_DEFAULT_FG, LCD_DEFAULT_BG);
633 #endif
634 #else /* LCD_DEPTH == 1 */
635 lcd_set_drawmode(DRMODE_SOLID);
636 #endif /* LCD_DEPTH */
637 #endif /* HAVE_LCD_BITMAP */
639 /* restore margins */
640 lcd_setmargins(xm,ym);
641 lcd_clear_display();
642 lcd_update();
644 #ifdef HAVE_REMOTE_LCD
645 #if LCD_REMOTE_DEPTH > 1
646 lcd_remote_set_drawinfo(DRMODE_SOLID, LCD_REMOTE_DEFAULT_FG,
647 LCD_REMOTE_DEFAULT_BG);
648 #else
649 lcd_remote_set_drawmode(DRMODE_SOLID);
650 #endif
651 lcd_remote_setmargins(rxm, rym);
652 lcd_remote_clear_display();
653 lcd_remote_update();
654 #endif
656 if (pfn_tsr_exit == NULL)
657 plugin_loaded = false;
659 sim_plugin_close(pd);
661 switch (rc) {
662 case PLUGIN_OK:
663 break;
665 case PLUGIN_USB_CONNECTED:
666 return PLUGIN_USB_CONNECTED;
668 default:
669 gui_syncsplash(HZ*2, str(LANG_PLUGIN_ERROR));
670 break;
672 return PLUGIN_OK;
675 /* Returns a pointer to the portion of the plugin buffer that is not already
676 being used. If no plugin is loaded, returns the entire plugin buffer */
677 void* plugin_get_buffer(size_t *buffer_size)
679 int buffer_pos;
681 if (plugin_loaded)
683 if (plugin_size >= PLUGIN_BUFFER_SIZE)
684 return NULL;
686 *buffer_size = PLUGIN_BUFFER_SIZE-plugin_size;
687 buffer_pos = plugin_size;
689 else
691 *buffer_size = PLUGIN_BUFFER_SIZE;
692 buffer_pos = 0;
695 return &pluginbuf[buffer_pos];
698 /* Returns a pointer to the mp3 buffer.
699 Playback gets stopped, to avoid conflicts.
700 Talk buffer is stolen as well.
702 void* plugin_get_audio_buffer(size_t *buffer_size)
704 #if CONFIG_CODEC == SWCODEC
705 return audio_get_buffer(true, buffer_size);
706 #else
707 audio_stop();
708 talk_buffer_steal(); /* we use the mp3 buffer, need to tell */
709 *buffer_size = audiobufend - audiobuf;
710 return audiobuf;
711 #endif
714 #ifdef IRAM_STEAL
715 /* Initializes plugin IRAM */
716 void plugin_iram_init(char *iramstart, char *iramcopy, size_t iram_size,
717 char *iedata, size_t iedata_size)
719 audio_iram_steal();
720 memcpy(iramstart, iramcopy, iram_size);
721 memset(iedata, 0, iedata_size);
722 memset(iramcopy, 0, iram_size);
723 #if NUM_CORES > 1
724 /* writeback cleared iedata and iramcopy areas */
725 flush_icache();
726 #endif
728 #endif /* IRAM_STEAL */
730 /* The plugin wants to stay resident after leaving its main function, e.g.
731 runs from timer or own thread. The callback is registered to later
732 instruct it to free its resources before a new plugin gets loaded. */
733 void plugin_tsr(bool (*exit_callback)(bool))
735 pfn_tsr_exit = exit_callback; /* remember the callback for later */
738 char *plugin_get_current_filename(void)
740 return current_plugin;