disable device tree during autodetection to give some (small) visual feedback.
[Rockbox.git] / apps / main.c
blob8af488d0b167e78de957d2acc67cb22448f00405
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 "config.h"
21 #include "ata.h"
22 #include "disk.h"
23 #include "fat.h"
24 #include "lcd.h"
25 #include "rtc.h"
26 #include "debug.h"
27 #include "led.h"
28 #include "kernel.h"
29 #include "button.h"
30 #include "tree.h"
31 #include "filetypes.h"
32 #include "panic.h"
33 #include "menu.h"
34 #include "system.h"
35 #include "usb.h"
36 #include "powermgmt.h"
37 #include "adc.h"
38 #include "i2c.h"
39 #ifndef DEBUG
40 #include "serial.h"
41 #endif
42 #include "audio.h"
43 #include "mp3_playback.h"
44 #include "thread.h"
45 #include "settings.h"
46 #include "backlight.h"
47 #include "status.h"
48 #include "debug_menu.h"
49 #include "version.h"
50 #include "sprintf.h"
51 #include "font.h"
52 #include "language.h"
53 #include "gwps.h"
54 #include "playlist.h"
55 #include "buffer.h"
56 #include "rolo.h"
57 #include "screens.h"
58 #include "power.h"
59 #include "talk.h"
60 #include "plugin.h"
61 #include "misc.h"
62 #include "dircache.h"
63 #ifdef HAVE_TAGCACHE
64 #include "tagcache.h"
65 #include "tagtree.h"
66 #endif
67 #include "lang.h"
68 #include "string.h"
69 #include "splash.h"
70 #include "eeprom_settings.h"
71 #include "scrobbler.h"
72 #include "icon.h"
74 #if (CONFIG_CODEC == SWCODEC)
75 #include "playback.h"
76 #endif
77 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
78 #include "pcm_record.h"
79 #endif
81 #ifdef BUTTON_REC
82 #define SETTINGS_RESET BUTTON_REC
83 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
84 #define SETTINGS_RESET BUTTON_A
85 #endif
87 #if CONFIG_TUNER
88 #include "radio.h"
89 #endif
90 #ifdef HAVE_MMC
91 #include "ata_mmc.h"
92 #endif
94 #ifdef HAVE_REMOTE_LCD
95 #include "lcd-remote.h"
96 #endif
98 #if CONFIG_USBOTG == USBOTG_ISP1362
99 #include "isp1362.h"
100 #endif
102 #if CONFIG_USBOTG == USBOTG_M5636
103 #include "m5636.h"
104 #endif
106 #include "cuesheet.h"
108 #ifdef SIMULATOR
109 #include "system-sdl.h"
110 #endif
112 /*#define AUTOROCK*/ /* define this to check for "autostart.rock" on boot */
114 const char appsversion[]=APPSVERSION;
116 static void init(void);
118 #ifdef SIMULATOR
119 void app_main(void)
120 #else
121 static void app_main(void)
122 #endif
124 int i;
125 init();
126 FOR_NB_SCREENS(i)
128 screens[i].clear_display();
129 screens[i].update();
131 tree_gui_init();
132 #ifdef HAVE_TOUCHPAD
133 touchpad_set_mode(TOUCHPAD_BUTTON);
134 #endif
135 root_menu();
138 static int init_dircache(bool preinit)
140 #ifdef HAVE_DIRCACHE
141 int result = 0;
142 bool clear = false;
144 if (preinit)
145 dircache_init();
147 if (!global_settings.dircache)
148 return 0;
150 # ifdef HAVE_EEPROM_SETTINGS
151 if (firmware_settings.initialized && firmware_settings.disk_clean
152 && preinit)
154 result = dircache_load();
156 if (result < 0)
158 firmware_settings.disk_clean = false;
159 if (global_status.dircache_size <= 0)
161 /* This will be in default language, settings are not
162 applied yet. Not really any easy way to fix that. */
163 gui_syncsplash(0, str(LANG_SCANNING_DISK));
164 clear = true;
167 dircache_build(global_status.dircache_size);
170 else
171 # endif
173 if (preinit)
174 return -1;
176 if (!dircache_is_enabled()
177 && !dircache_is_initializing())
179 if (global_status.dircache_size <= 0)
181 gui_syncsplash(0, str(LANG_SCANNING_DISK));
182 clear = true;
184 result = dircache_build(global_status.dircache_size);
187 if (result < 0)
189 /* Initialization of dircache failed. Manual action is
190 * necessary to enable dircache again.
192 gui_syncsplash(0, "Dircache failed, disabled. Result: %d", result);
193 global_settings.dircache = false;
198 if (clear)
200 backlight_on();
201 show_logo();
202 global_status.dircache_size = dircache_get_cache_size();
203 status_save();
206 return result;
207 #else
208 (void)preinit;
209 return 0;
210 #endif
213 #ifdef HAVE_TAGCACHE
214 static void init_tagcache(void)
216 bool clear = false;
218 tagcache_init();
220 while (!tagcache_is_initialized())
222 #ifdef HAVE_LCD_CHARCELLS
223 char buf[32];
224 #endif
225 int ret = tagcache_get_commit_step();
227 if (ret > 0)
229 #if CONFIG_CODEC == SWCODEC
230 /* hwcodec can't use voice here, as the database commit
231 * uses the audio buffer. */
232 static long talked_tick = 0;
233 if(global_settings.talk_menu
234 && (talked_tick == 0
235 || TIME_AFTER(current_tick, talked_tick+7*HZ)))
237 talked_tick = current_tick;
238 talk_id(LANG_TAGCACHE_INIT, false);
239 talk_number(ret, true);
240 talk_id(VOICE_OF, true);
241 talk_number(tagcache_get_max_commit_step(), true);
243 #endif
244 #ifdef HAVE_LCD_BITMAP
245 gui_syncsplash(0, "%s [%d/%d]",
246 str(LANG_TAGCACHE_INIT), ret,
247 tagcache_get_max_commit_step());
248 #else
249 lcd_double_height(false);
250 snprintf(buf, sizeof(buf), " DB [%d/%d]", ret,
251 tagcache_get_max_commit_step());
252 lcd_puts(0, 1, buf);
253 lcd_update();
254 #endif
255 clear = true;
257 sleep(HZ/4);
259 tagtree_init();
261 if (clear)
263 backlight_on();
264 show_logo();
267 #endif
269 #ifdef SIMULATOR
271 static void init(void)
273 kernel_init();
274 buffer_init();
275 enable_irq();
276 lcd_init();
277 #ifdef HAVE_REMOTE_LCD
278 lcd_remote_init();
279 #endif
280 font_init();
281 show_logo();
282 button_init();
283 backlight_init();
284 lang_init();
285 #ifdef DEBUG
286 debug_init();
287 #endif
288 /* Must be done before any code uses the multi-screen APi */
289 screen_access_init();
290 gui_syncstatusbar_init(&statusbars);
291 ata_init();
292 settings_reset();
293 settings_load(SETTINGS_ALL);
294 gui_sync_wps_init();
295 settings_apply(true);
296 init_dircache(true);
297 init_dircache(false);
298 #ifdef HAVE_TAGCACHE
299 init_tagcache();
300 #endif
301 sleep(HZ/2);
302 tree_mem_init();
303 filetype_init();
304 playlist_init();
306 #if CONFIG_CODEC != SWCODEC
307 mp3_init( global_settings.volume,
308 global_settings.bass,
309 global_settings.treble,
310 global_settings.balance,
311 global_settings.loudness,
312 global_settings.avc,
313 global_settings.channel_config,
314 global_settings.stereo_width,
315 global_settings.mdb_strength,
316 global_settings.mdb_harmonics,
317 global_settings.mdb_center,
318 global_settings.mdb_shape,
319 global_settings.mdb_enable,
320 global_settings.superbass);
322 /* audio_init must to know the size of voice buffer so init voice first */
323 talk_init();
324 #endif /* CONFIG_CODEC != SWCODEC */
326 scrobbler_init();
327 cuesheet_init();
329 audio_init();
330 button_clear_queue(); /* Empty the keyboard buffer */
333 #else
335 static void init(void)
337 int rc;
338 bool mounted = false;
339 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
340 /* if nobody initialized ATA before, I consider this a cold start */
341 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
342 #endif
344 system_init();
345 kernel_init();
347 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
348 set_cpu_frequency(CPUFREQ_NORMAL);
349 #ifdef CPU_COLDFIRE
350 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
351 #endif
352 cpu_boost(true);
353 #endif
355 buffer_init();
357 settings_reset();
359 i2c_init();
361 power_init();
363 enable_irq();
364 #ifdef CPU_ARM
365 enable_fiq();
366 #endif
367 lcd_init();
368 #ifdef HAVE_REMOTE_LCD
369 lcd_remote_init();
370 #endif
371 font_init();
373 show_logo();
374 lang_init();
376 #ifdef DEBUG
377 debug_init();
378 #else
379 #if !defined(HAVE_FMADC) && !defined(HAVE_MMC)
380 serial_setup();
381 #endif
382 #endif
384 #if CONFIG_RTC
385 rtc_init();
386 #endif
387 #ifdef HAVE_RTC_RAM
388 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
389 #endif
391 adc_init();
393 usb_init();
394 #if CONFIG_USBOTG == USBOTG_ISP1362
395 isp1362_init();
396 #elif CONFIG_USBOTG == USBOTG_M5636
397 m5636_init();
398 #endif
400 backlight_init();
402 button_init();
404 powermgmt_init();
406 #if CONFIG_TUNER
407 radio_init();
408 #endif
410 /* Must be done before any code uses the multi-screen APi */
411 screen_access_init();
412 gui_syncstatusbar_init(&statusbars);
414 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
415 if (coldstart && charger_inserted()
416 && !global_settings.car_adapter_mode
417 #ifdef ATA_POWER_PLAYERSTYLE
418 && !ide_powered() /* relies on probing result from bootloader */
419 #endif
422 rc = charging_screen(); /* display a "charging" screen */
423 if (rc == 1) /* charger removed */
424 power_off();
425 /* "On" pressed or USB connected: proceed */
426 show_logo(); /* again, to provide better visual feedback */
428 #endif
430 rc = ata_init();
431 if(rc)
433 #ifdef HAVE_LCD_BITMAP
434 char str[32];
435 lcd_clear_display();
436 snprintf(str, 31, "ATA error: %d", rc);
437 lcd_puts(0, 1, str);
438 lcd_puts(0, 3, "Press ON to debug");
439 lcd_update();
440 while(!(button_get(true) & BUTTON_REL)); /*DO NOT CHANGE TO ACTION SYSTEM */
441 dbg_ports();
442 #endif
443 panicf("ata: %d", rc);
446 #ifdef HAVE_EEPROM_SETTINGS
447 eeprom_settings_init();
448 #endif
450 usb_start_monitoring();
451 #ifndef HAVE_USBSTACK
452 while (usb_detect() == USB_INSERTED)
454 #ifdef HAVE_EEPROM_SETTINGS
455 firmware_settings.disk_clean = false;
456 #endif
457 /* enter USB mode early, before trying to mount */
458 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
459 #ifdef HAVE_MMC
460 if (!mmc_touched() ||
461 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
462 #endif
464 usb_screen();
465 mounted = true; /* mounting done @ end of USB mode */
467 #ifdef HAVE_USB_POWER
468 if (usb_powered()) /* avoid deadlock */
469 break;
470 #endif
472 #endif
474 if (!mounted)
476 rc = disk_mount_all();
477 if (rc<=0)
479 lcd_clear_display();
480 lcd_puts(0, 0, "No partition");
481 lcd_puts(0, 1, "found.");
482 #ifdef HAVE_LCD_BITMAP
483 lcd_puts(0, 2, "Insert USB cable");
484 lcd_puts(0, 3, "and fix it.");
485 #endif
486 lcd_update();
488 while(button_get(true) != SYS_USB_CONNECTED) {};
489 usb_screen();
490 system_reboot();
494 #if defined(SETTINGS_RESET) || (CONFIG_KEYPAD == IPOD_4G_PAD) || \
495 (CONFIG_KEYPAD == IRIVER_H10_PAD)
496 #ifdef SETTINGS_RESET
497 /* Reset settings if holding the reset button. (Rec on Archos,
498 A on Gigabeat) */
499 if ((button_status() & SETTINGS_RESET) == SETTINGS_RESET)
500 #else
501 /* Reset settings if the hold button is turned on */
502 if (button_hold())
503 #endif
505 gui_syncsplash(HZ*2, str(LANG_RESET_DONE_CLEAR));
506 settings_reset();
508 else
509 #endif
510 settings_load(SETTINGS_ALL);
512 if (init_dircache(true) < 0)
514 #ifdef HAVE_TAGCACHE
515 remove(TAGCACHE_STATEFILE);
516 #endif
519 gui_sync_wps_init();
520 settings_apply(true);
521 init_dircache(false);
522 #ifdef HAVE_TAGCACHE
523 init_tagcache();
524 #endif
526 #ifdef HAVE_EEPROM_SETTINGS
527 if (firmware_settings.initialized)
529 /* In case we crash. */
530 firmware_settings.disk_clean = false;
531 eeprom_settings_store();
533 #endif
534 status_init();
535 playlist_init();
536 tree_mem_init();
537 filetype_init();
538 scrobbler_init();
539 cuesheet_init();
541 #if CONFIG_CODEC != SWCODEC
542 /* No buffer allocation (see buffer.c) may take place after the call to
543 audio_init() since the mpeg thread takes the rest of the buffer space */
544 mp3_init( global_settings.volume,
545 global_settings.bass,
546 global_settings.treble,
547 global_settings.balance,
548 global_settings.loudness,
549 global_settings.avc,
550 global_settings.channel_config,
551 global_settings.stereo_width,
552 global_settings.mdb_strength,
553 global_settings.mdb_harmonics,
554 global_settings.mdb_center,
555 global_settings.mdb_shape,
556 global_settings.mdb_enable,
557 global_settings.superbass);
559 /* audio_init must to know the size of voice buffer so init voice first */
560 talk_init();
561 #endif /* CONFIG_CODEC != SWCODEC */
563 audio_init();
565 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
566 pcm_rec_init();
567 #endif
569 /* runtime database has to be initialized after audio_init() */
570 cpu_boost(false);
572 #ifdef AUTOROCK
574 static const char filename[] = PLUGIN_APPS_DIR "/autostart.rock";
576 if(file_exists(filename)) /* no complaint if it doesn't exist */
578 plugin_load((char*)filename, NULL); /* start if it does */
581 #endif /* #ifdef AUTOROCK */
583 #if CONFIG_CHARGING
584 car_adapter_mode_init();
585 #endif
586 #ifdef HAVE_ACCESSORY_SUPPLY
587 accessory_supply_set(global_settings.accessory_supply);
588 #endif
591 #ifdef CPU_PP
592 void cop_main(void)
594 /* This is the entry point for the coprocessor
595 Anyone not running an upgraded bootloader will never reach this point,
596 so it should not be assumed that the coprocessor be usable even on
597 platforms which support it.
599 A kernel thread is initially setup on the coprocessor and immediately
600 destroyed for purposes of continuity. The cop sits idle until at least
601 one thread exists on it. */
603 /* 3G doesn't have Rolo or dual core support yet */
604 #if NUM_CORES > 1
605 system_init();
606 kernel_init();
607 /* This should never be reached */
608 #endif
609 while(1) {
610 sleep_core(COP);
613 #endif /* CPU_PP */
615 int main(void)
617 app_main();
619 while(1) {
620 #if (CONFIG_LED == LED_REAL)
621 led(true); sleep(HZ/10);
622 led(false); sleep(HZ/10);
623 #endif
625 return 0;
627 #endif