Merge branch 'app-paths' into android-test-plugins
[kugel-rb.git] / apps / main.c
blobdd1f60a849e21f388e7315e10d3dd8108d1d6838
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 "config.h"
23 #include "gcc_extensions.h"
24 #include "storage.h"
25 #include "disk.h"
26 #include "fat.h"
27 #include "lcd.h"
28 #include "rtc.h"
29 #include "debug.h"
30 #include "led.h"
31 #include "kernel.h"
32 #include "button.h"
33 #include "tree.h"
34 #include "filetypes.h"
35 #include "panic.h"
36 #include "menu.h"
37 #include "system.h"
38 #include "usb.h"
39 #include "powermgmt.h"
40 #include "adc.h"
41 #include "i2c.h"
42 #ifndef DEBUG
43 #include "serial.h"
44 #endif
45 #include "audio.h"
46 #include "mp3_playback.h"
47 #include "thread.h"
48 #include "settings.h"
49 #include "backlight.h"
50 #include "status.h"
51 #include "debug_menu.h"
52 #include "font.h"
53 #include "language.h"
54 #include "wps.h"
55 #include "playlist.h"
56 #include "buffer.h"
57 #include "rolo.h"
58 #include "screens.h"
59 #include "usb_screen.h"
60 #include "power.h"
61 #include "talk.h"
62 #include "plugin.h"
63 #include "misc.h"
64 #include "dircache.h"
65 #ifdef HAVE_TAGCACHE
66 #include "tagcache.h"
67 #include "tagtree.h"
68 #endif
69 #include "lang.h"
70 #include "string.h"
71 #include "splash.h"
72 #include "eeprom_settings.h"
73 #include "scrobbler.h"
74 #include "icon.h"
75 #include "viewport.h"
76 #include "skin_engine/skin_engine.h"
77 #include "statusbar-skinned.h"
78 #include "bootchart.h"
79 #if defined(APPLICATION) && (CONFIG_PLATFORM & PLATFORM_ANDROID)
80 #include "notification.h"
81 #endif
83 #ifdef IPOD_ACCESSORY_PROTOCOL
84 #include "iap.h"
85 #endif
87 #if (CONFIG_CODEC == SWCODEC)
88 #include "playback.h"
89 #include "tdspeed.h"
90 #endif
91 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
92 #include "pcm_record.h"
93 #endif
95 #ifdef BUTTON_REC
96 #define SETTINGS_RESET BUTTON_REC
97 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
98 #define SETTINGS_RESET BUTTON_A
99 #endif
101 #if CONFIG_TUNER
102 #include "radio.h"
103 #endif
104 #if (CONFIG_STORAGE & STORAGE_MMC)
105 #include "ata_mmc.h"
106 #endif
108 #ifdef HAVE_REMOTE_LCD
109 #include "lcd-remote.h"
110 #endif
112 #if CONFIG_USBOTG == USBOTG_ISP1362
113 #include "isp1362.h"
114 #endif
116 #if CONFIG_USBOTG == USBOTG_M5636
117 #include "m5636.h"
118 #endif
120 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
121 #define MAIN_NORETURN_ATTR NORETURN_ATTR
122 #else
123 /* gcc adds an implicit 'return 0;' at the end of main(), causing a warning
124 * with noreturn attribute */
125 #define MAIN_NORETURN_ATTR
126 #endif
128 #if (CONFIG_PLATFORM & PLATFORM_SDL)
129 #include "sim_tasks.h"
130 #include "system-sdl.h"
131 #define HAVE_ARGV_MAIN
132 /* Don't use SDL_main on windows -> no more stdio redirection */
133 #if defined(WIN32)
134 #undef main
135 #endif
136 #endif
138 /*#define AUTOROCK*/ /* define this to check for "autostart.rock" on boot */
140 static void init(void);
141 /* main(), and various functions called by main() and init() may be
142 * be INIT_ATTR. These functions must not be called after the final call
143 * to root_menu() at the end of main()
144 * see definition of INIT_ATTR in config.h */
145 #ifdef HAVE_ARGV_MAIN
146 int main(int argc, char *argv[]) INIT_ATTR MAIN_NORETURN_ATTR ;
147 int main(int argc, char *argv[])
149 sys_handle_argv(argc, argv);
150 #else
151 int main(void) INIT_ATTR MAIN_NORETURN_ATTR;
152 int main(void)
154 #endif
155 int i;
156 CHART(">init");
157 init();
158 CHART("<init");
159 FOR_NB_SCREENS(i)
161 screens[i].clear_display();
162 screens[i].update();
164 #ifdef HAVE_LCD_BITMAP
165 list_init();
166 #endif
167 tree_gui_init();
168 /* Keep the order of this 3
169 * Must be done before any code uses the multi-screen API */
170 #ifdef HAVE_USBSTACK
171 /* All threads should be created and public queues registered by now */
172 usb_start_monitoring();
173 #endif
175 #ifdef AUTOROCK
177 char filename[MAX_PATH];
178 const char *file =
179 #ifdef APPLICATION
180 ROCKBOX_DIR
181 #else
182 PLUGIN_APPS_DIR
183 #endif
184 "/autostart.rock";
185 if(file_exists(file)) /* no complaint if it doesn't exist */
187 plugin_load(file, NULL); /* start if it does */
190 #endif /* #ifdef AUTOROCK */
192 global_status.last_volume_change = 0;
193 /* no calls INIT_ATTR functions after this point anymore!
194 * see definition of INIT_ATTR in config.h */
195 CHART(">root_menu");
196 root_menu();
199 static int init_dircache(bool preinit) INIT_ATTR;
200 static int init_dircache(bool preinit)
202 #ifdef HAVE_DIRCACHE
203 int result = 0;
204 bool clear = false;
206 if (preinit)
207 dircache_init();
209 if (!global_settings.dircache)
210 return 0;
212 # ifdef HAVE_EEPROM_SETTINGS
213 if (firmware_settings.initialized && firmware_settings.disk_clean
214 && preinit)
216 result = dircache_load();
218 if (result < 0)
220 firmware_settings.disk_clean = false;
221 if (global_status.dircache_size <= 0)
223 /* This will be in default language, settings are not
224 applied yet. Not really any easy way to fix that. */
225 splash(0, str(LANG_SCANNING_DISK));
226 clear = true;
229 dircache_build(global_status.dircache_size);
232 else
233 # endif
235 if (preinit)
236 return -1;
238 if (!dircache_is_enabled()
239 && !dircache_is_initializing())
241 if (global_status.dircache_size <= 0)
243 splash(0, str(LANG_SCANNING_DISK));
244 clear = true;
246 result = dircache_build(global_status.dircache_size);
249 if (result < 0)
251 /* Initialization of dircache failed. Manual action is
252 * necessary to enable dircache again.
254 splashf(0, "Dircache failed, disabled. Result: %d", result);
255 global_settings.dircache = false;
259 if (clear)
261 backlight_on();
262 show_logo();
263 global_status.dircache_size = dircache_get_cache_size();
264 status_save();
267 return result;
268 #else
269 (void)preinit;
270 return 0;
271 #endif
274 #ifdef HAVE_TAGCACHE
275 static void init_tagcache(void) INIT_ATTR;
276 static void init_tagcache(void)
278 bool clear = false;
279 #if CONFIG_CODEC == SWCODEC
280 long talked_tick = 0;
281 #endif
282 tagcache_init();
284 while (!tagcache_is_initialized())
286 int ret = tagcache_get_commit_step();
288 if (ret > 0)
290 #if CONFIG_CODEC == SWCODEC
291 /* hwcodec can't use voice here, as the database commit
292 * uses the audio buffer. */
293 if(global_settings.talk_menu
294 && (talked_tick == 0
295 || TIME_AFTER(current_tick, talked_tick+7*HZ)))
297 talked_tick = current_tick;
298 talk_id(LANG_TAGCACHE_INIT, false);
299 talk_number(ret, true);
300 talk_id(VOICE_OF, true);
301 talk_number(tagcache_get_max_commit_step(), true);
303 #endif
304 #ifdef HAVE_LCD_BITMAP
305 if (lang_is_rtl())
307 splashf(0, "[%d/%d] %s", ret, tagcache_get_max_commit_step(),
308 str(LANG_TAGCACHE_INIT));
310 else
312 splashf(0, "%s [%d/%d]", str(LANG_TAGCACHE_INIT), ret,
313 tagcache_get_max_commit_step());
315 #else
316 lcd_double_height(false);
317 lcd_putsf(0, 1, " DB [%d/%d]", ret,
318 tagcache_get_max_commit_step());
319 lcd_update();
320 #endif
321 clear = true;
323 sleep(HZ/4);
325 tagtree_init();
327 if (clear)
329 backlight_on();
330 show_logo();
333 #endif
335 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
337 static void init(void)
339 system_init();
340 kernel_init();
341 #ifdef APPLICATION
342 paths_init();
343 #endif
344 buffer_init();
345 enable_irq();
346 lcd_init();
347 #ifdef HAVE_REMOTE_LCD
348 lcd_remote_init();
349 #endif
350 font_init();
351 show_logo();
352 button_init();
353 backlight_init();
354 #if (CONFIG_PLATFORM & PLATFORM_SDL)
355 sim_tasks_init();
356 #endif
357 #if (CONFIG_PLATFORM & PLATFORM_ANDROID)
358 notification_init();
359 #endif
360 lang_init(core_language_builtin, language_strings,
361 LANG_LAST_INDEX_IN_ARRAY);
362 #ifdef DEBUG
363 debug_init();
364 #endif
365 /* Keep the order of this 3 (viewportmanager handles statusbars)
366 * Must be done before any code uses the multi-screen API */
367 gui_syncstatusbar_init(&statusbars);
368 gui_sync_skin_init();
369 sb_skin_init();
370 viewportmanager_init();
372 storage_init();
373 settings_reset();
374 settings_load(SETTINGS_ALL);
375 settings_apply(true);
376 init_dircache(true);
377 init_dircache(false);
378 #ifdef HAVE_TAGCACHE
379 init_tagcache();
380 #endif
381 sleep(HZ/2);
382 tree_mem_init();
383 filetype_init();
384 playlist_init();
385 theme_init_buffer();
387 #if CONFIG_CODEC != SWCODEC
388 mp3_init( global_settings.volume,
389 global_settings.bass,
390 global_settings.treble,
391 global_settings.balance,
392 global_settings.loudness,
393 global_settings.avc,
394 global_settings.channel_config,
395 global_settings.stereo_width,
396 global_settings.mdb_strength,
397 global_settings.mdb_harmonics,
398 global_settings.mdb_center,
399 global_settings.mdb_shape,
400 global_settings.mdb_enable,
401 global_settings.superbass);
403 /* audio_init must to know the size of voice buffer so init voice first */
404 talk_init();
405 #endif /* CONFIG_CODEC != SWCODEC */
407 scrobbler_init();
408 #if CONFIG_CODEC == SWCODEC && defined (HAVE_PITCHSCREEN)
409 tdspeed_init();
410 #endif /* CONFIG_CODEC == SWCODEC */
412 audio_init();
413 button_clear_queue(); /* Empty the keyboard buffer */
415 settings_apply_skins();
418 #else
420 static void init(void) INIT_ATTR;
421 static void init(void)
423 int rc;
424 bool mounted = false;
425 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
426 /* if nobody initialized ATA before, I consider this a cold start */
427 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
428 #endif
430 system_init();
431 #if defined(IPOD_VIDEO)
432 audiobufend=(unsigned char *)audiobufend_lds;
433 if(MEM==64 && probed_ramsize!=64)
435 audiobufend -= (32<<20);
437 #endif
438 kernel_init();
440 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
441 set_cpu_frequency(CPUFREQ_NORMAL);
442 #ifdef CPU_COLDFIRE
443 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
444 #endif
445 cpu_boost(true);
446 #endif
448 buffer_init();
450 settings_reset();
452 i2c_init();
454 power_init();
456 enable_irq();
457 #ifdef CPU_ARM
458 enable_fiq();
459 #endif
460 /* current_tick should be ticking by now */
461 CHART("ticking");
463 lcd_init();
464 #ifdef HAVE_REMOTE_LCD
465 lcd_remote_init();
466 #endif
467 font_init();
469 CHART(">show_logo");
470 show_logo();
471 CHART("<show_logo");
472 lang_init(core_language_builtin, language_strings,
473 LANG_LAST_INDEX_IN_ARRAY);
475 #ifdef DEBUG
476 debug_init();
477 #else
478 #ifdef HAVE_SERIAL
479 serial_setup();
480 #endif
481 #endif
483 #if CONFIG_RTC
484 rtc_init();
485 #endif
486 #ifdef HAVE_RTC_RAM
487 CHART(">settings_load(RTC)");
488 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
489 CHART("<settings_load(RTC)");
490 #endif
492 adc_init();
494 usb_init();
495 #if CONFIG_USBOTG == USBOTG_ISP1362
496 isp1362_init();
497 #elif CONFIG_USBOTG == USBOTG_M5636
498 m5636_init();
499 #endif
501 backlight_init();
503 button_init();
505 powermgmt_init();
507 #if CONFIG_TUNER
508 radio_init();
509 #endif
511 /* Keep the order of this 3 (viewportmanager handles statusbars)
512 * Must be done before any code uses the multi-screen API */
513 CHART(">gui_syncstatusbar_init");
514 gui_syncstatusbar_init(&statusbars);
515 CHART("<gui_syncstatusbar_init");
516 CHART(">sb_skin_init");
517 sb_skin_init();
518 CHART("<sb_skin_init");
519 CHART(">gui_sync_wps_init");
520 gui_sync_skin_init();
521 CHART("<gui_sync_wps_init");
522 CHART(">viewportmanager_init");
523 viewportmanager_init();
524 CHART("<viewportmanager_init");
526 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
527 /* charger_inserted() can't be used here because power_thread()
528 hasn't checked power_input_status() yet */
529 if (coldstart && (power_input_status() & POWER_INPUT_MAIN_CHARGER)
530 && !global_settings.car_adapter_mode
531 #ifdef ATA_POWER_PLAYERSTYLE
532 && !ide_powered() /* relies on probing result from bootloader */
533 #endif
536 rc = charging_screen(); /* display a "charging" screen */
537 if (rc == 1) /* charger removed */
538 power_off();
539 /* "On" pressed or USB connected: proceed */
540 show_logo(); /* again, to provide better visual feedback */
542 #endif
545 disk_init_subsystem();
546 CHART(">storage_init");
547 rc = storage_init();
548 CHART("<storage_init");
549 if(rc)
551 #ifdef HAVE_LCD_BITMAP
552 lcd_clear_display();
553 lcd_putsf(0, 1, "ATA error: %d", rc);
554 lcd_puts(0, 3, "Press ON to debug");
555 lcd_update();
556 while(!(button_get(true) & BUTTON_REL)); /*DO NOT CHANGE TO ACTION SYSTEM */
557 dbg_ports();
558 #endif
559 panicf("ata: %d", rc);
562 #ifdef HAVE_EEPROM_SETTINGS
563 CHART(">eeprom_settings_init");
564 eeprom_settings_init();
565 CHART("<eeprom_settings_init");
566 #endif
568 #ifndef HAVE_USBSTACK
569 usb_start_monitoring();
570 while (usb_detect() == USB_INSERTED)
572 #ifdef HAVE_EEPROM_SETTINGS
573 firmware_settings.disk_clean = false;
574 #endif
575 /* enter USB mode early, before trying to mount */
576 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
577 #if (CONFIG_STORAGE & STORAGE_MMC)
578 if (!mmc_touched() ||
579 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
580 #endif
582 gui_usb_screen_run(true);
583 mounted = true; /* mounting done @ end of USB mode */
585 #ifdef HAVE_USB_POWER
586 if (usb_powered()) /* avoid deadlock */
587 break;
588 #endif
590 #endif
592 if (!mounted)
594 CHART(">disk_mount_all");
595 rc = disk_mount_all();
596 CHART("<disk_mount_all");
597 if (rc<=0)
599 lcd_clear_display();
600 lcd_puts(0, 0, "No partition");
601 lcd_puts(0, 1, "found.");
602 #ifdef HAVE_LCD_BITMAP
603 lcd_puts(0, 2, "Insert USB cable");
604 lcd_puts(0, 3, "and fix it.");
605 #endif
606 lcd_update();
608 while(button_get(true) != SYS_USB_CONNECTED) {};
609 gui_usb_screen_run(true);
610 system_reboot();
614 #if defined(SETTINGS_RESET) || (CONFIG_KEYPAD == IPOD_4G_PAD) || \
615 (CONFIG_KEYPAD == IRIVER_H10_PAD)
616 #ifdef SETTINGS_RESET
617 /* Reset settings if holding the reset button. (Rec on Archos,
618 A on Gigabeat) */
619 if ((button_status() & SETTINGS_RESET) == SETTINGS_RESET)
620 #else
621 /* Reset settings if the hold button is turned on */
622 if (button_hold())
623 #endif
625 splash(HZ*2, str(LANG_RESET_DONE_CLEAR));
626 settings_reset();
628 else
629 #endif
631 CHART(">settings_load(ALL)");
632 settings_load(SETTINGS_ALL);
633 CHART("<settings_load(ALL)");
636 CHART(">init_dircache(true)");
637 rc = init_dircache(true);
638 CHART("<init_dircache(true)");
639 if (rc < 0)
641 #ifdef HAVE_TAGCACHE
642 remove(TAGCACHE_STATEFILE);
643 #endif
646 CHART(">settings_apply(true)");
647 settings_apply(true);
648 CHART("<settings_apply(true)");
649 CHART(">init_dircache(false)");
650 init_dircache(false);
651 CHART("<init_dircache(false)");
652 #ifdef HAVE_TAGCACHE
653 CHART(">init_tagcache");
654 init_tagcache();
655 CHART("<init_tagcache");
656 #endif
658 #ifdef HAVE_EEPROM_SETTINGS
659 if (firmware_settings.initialized)
661 /* In case we crash. */
662 firmware_settings.disk_clean = false;
663 CHART(">eeprom_settings_store");
664 eeprom_settings_store();
665 CHART("<eeprom_settings_store");
667 #endif
668 playlist_init();
669 tree_mem_init();
670 filetype_init();
671 scrobbler_init();
672 #if CONFIG_CODEC == SWCODEC && defined (HAVE_PITCHSCREEN)
673 tdspeed_init();
674 #endif /* CONFIG_CODEC == SWCODEC */
675 theme_init_buffer();
677 #if CONFIG_CODEC != SWCODEC
678 /* No buffer allocation (see buffer.c) may take place after the call to
679 audio_init() since the mpeg thread takes the rest of the buffer space */
680 mp3_init( global_settings.volume,
681 global_settings.bass,
682 global_settings.treble,
683 global_settings.balance,
684 global_settings.loudness,
685 global_settings.avc,
686 global_settings.channel_config,
687 global_settings.stereo_width,
688 global_settings.mdb_strength,
689 global_settings.mdb_harmonics,
690 global_settings.mdb_center,
691 global_settings.mdb_shape,
692 global_settings.mdb_enable,
693 global_settings.superbass);
695 /* audio_init must to know the size of voice buffer so init voice first */
696 talk_init();
697 #endif /* CONFIG_CODEC != SWCODEC */
699 CHART(">audio_init");
700 audio_init();
701 CHART("<audio_init");
703 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
704 pcm_rec_init();
705 #endif
707 /* runtime database has to be initialized after audio_init() */
708 cpu_boost(false);
710 #if CONFIG_CHARGING
711 car_adapter_mode_init();
712 #endif
713 #ifdef IPOD_ACCESSORY_PROTOCOL
714 iap_setup(global_settings.serial_bitrate);
715 #endif
716 #ifdef HAVE_ACCESSORY_SUPPLY
717 accessory_supply_set(global_settings.accessory_supply);
718 #endif
719 #ifdef HAVE_LINEOUT_POWEROFF
720 lineout_set(global_settings.lineout_active);
721 #endif
722 #ifdef HAVE_HOTSWAP_STORAGE_AS_MAIN
723 CHART("<check_bootfile(false)");
724 check_bootfile(false); /* remember write time and filesize */
725 CHART(">check_bootfile(false)");
726 #endif
727 CHART("<settings_apply_skins");
728 settings_apply_skins();
729 CHART(">settings_apply_skins");
732 #ifdef CPU_PP
733 void cop_main(void) MAIN_NORETURN_ATTR;
734 void cop_main(void)
736 /* This is the entry point for the coprocessor
737 Anyone not running an upgraded bootloader will never reach this point,
738 so it should not be assumed that the coprocessor be usable even on
739 platforms which support it.
741 A kernel thread is initially setup on the coprocessor and immediately
742 destroyed for purposes of continuity. The cop sits idle until at least
743 one thread exists on it. */
745 #if NUM_CORES > 1
746 system_init();
747 kernel_init();
748 /* This should never be reached */
749 #endif
750 while(1) {
751 sleep_core(COP);
754 #endif /* CPU_PP */
756 #endif /* SIMULATOR */