Correct battery conditional and disk access icon
[kugel-rb.git] / apps / main.c
blob4c7d48e2d95a4a48319c12cfeb3f35b0a7217086
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 "storage.h"
24 #include "disk.h"
25 #include "fat.h"
26 #include "lcd.h"
27 #include "rtc.h"
28 #include "debug.h"
29 #include "led.h"
30 #include "kernel.h"
31 #include "button.h"
32 #include "tree.h"
33 #include "filetypes.h"
34 #include "panic.h"
35 #include "menu.h"
36 #include "system.h"
37 #include "usb.h"
38 #include "powermgmt.h"
39 #include "adc.h"
40 #include "i2c.h"
41 #ifndef DEBUG
42 #include "serial.h"
43 #endif
44 #include "audio.h"
45 #include "mp3_playback.h"
46 #include "thread.h"
47 #include "settings.h"
48 #include "backlight.h"
49 #include "status.h"
50 #include "debug_menu.h"
51 #include "version.h"
52 #include "sprintf.h"
53 #include "font.h"
54 #include "language.h"
55 #include "wps.h"
56 #include "playlist.h"
57 #include "buffer.h"
58 #include "rolo.h"
59 #include "screens.h"
60 #include "usb_screen.h"
61 #include "power.h"
62 #include "talk.h"
63 #include "plugin.h"
64 #include "misc.h"
65 #include "dircache.h"
66 #ifdef HAVE_TAGCACHE
67 #include "tagcache.h"
68 #include "tagtree.h"
69 #endif
70 #include "lang.h"
71 #include "string.h"
72 #include "splash.h"
73 #include "eeprom_settings.h"
74 #include "scrobbler.h"
75 #include "icon.h"
76 #include "viewport.h"
77 #include "statusbar-skinned.h"
79 #ifdef IPOD_ACCESSORY_PROTOCOL
80 #include "iap.h"
81 #endif
83 #if (CONFIG_CODEC == SWCODEC)
84 #include "playback.h"
85 #include "tdspeed.h"
86 #endif
87 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
88 #include "pcm_record.h"
89 #endif
91 #ifdef BUTTON_REC
92 #define SETTINGS_RESET BUTTON_REC
93 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
94 #define SETTINGS_RESET BUTTON_A
95 #endif
97 #if CONFIG_TUNER
98 #include "radio.h"
99 #endif
100 #if (CONFIG_STORAGE & STORAGE_MMC)
101 #include "ata_mmc.h"
102 #endif
104 #ifdef HAVE_REMOTE_LCD
105 #include "lcd-remote.h"
106 #endif
108 #if CONFIG_USBOTG == USBOTG_ISP1362
109 #include "isp1362.h"
110 #endif
112 #if CONFIG_USBOTG == USBOTG_M5636
113 #include "m5636.h"
114 #endif
116 #ifdef SIMULATOR
117 #include "sim_tasks.h"
118 #include "system-sdl.h"
119 #endif
121 /*#define AUTOROCK*/ /* define this to check for "autostart.rock" on boot */
123 const char appsversion[]=APPSVERSION;
125 static void init(void);
127 #ifdef SIMULATOR
128 void app_main(void)
129 #else
130 static void app_main(void)
131 #endif
133 int i;
134 init();
135 FOR_NB_SCREENS(i)
137 screens[i].clear_display();
138 screens[i].update();
140 #ifdef HAVE_LCD_BITMAP
141 list_init();
142 #endif
143 tree_gui_init();
144 viewportmanager_init();
145 #ifdef HAVE_USBSTACK
146 /* All threads should be created and public queues registered by now */
147 usb_start_monitoring();
148 #endif
150 #ifdef AUTOROCK
152 static const char filename[] = PLUGIN_APPS_DIR "/autostart.rock";
154 if(file_exists(filename)) /* no complaint if it doesn't exist */
156 plugin_load((char*)filename, NULL); /* start if it does */
159 #endif /* #ifdef AUTOROCK */
161 root_menu();
164 static int init_dircache(bool preinit)
166 #ifdef HAVE_DIRCACHE
167 int result = 0;
168 bool clear = false;
170 if (preinit)
171 dircache_init();
173 if (!global_settings.dircache)
174 return 0;
176 # ifdef HAVE_EEPROM_SETTINGS
177 if (firmware_settings.initialized && firmware_settings.disk_clean
178 && preinit)
180 result = dircache_load();
182 if (result < 0)
184 firmware_settings.disk_clean = false;
185 if (global_status.dircache_size <= 0)
187 /* This will be in default language, settings are not
188 applied yet. Not really any easy way to fix that. */
189 splash(0, str(LANG_SCANNING_DISK));
190 clear = true;
193 dircache_build(global_status.dircache_size);
196 else
197 # endif
199 if (preinit)
200 return -1;
202 if (!dircache_is_enabled()
203 && !dircache_is_initializing())
205 if (global_status.dircache_size <= 0)
207 splash(0, str(LANG_SCANNING_DISK));
208 clear = true;
210 result = dircache_build(global_status.dircache_size);
213 if (result < 0)
215 /* Initialization of dircache failed. Manual action is
216 * necessary to enable dircache again.
218 splashf(0, "Dircache failed, disabled. Result: %d", result);
219 global_settings.dircache = false;
223 if (clear)
225 backlight_on();
226 show_logo();
227 global_status.dircache_size = dircache_get_cache_size();
228 status_save();
231 return result;
232 #else
233 (void)preinit;
234 return 0;
235 #endif
238 #ifdef HAVE_TAGCACHE
239 static void init_tagcache(void)
241 bool clear = false;
242 #if CONFIG_CODEC == SWCODEC
243 long talked_tick = 0;
244 #endif
245 tagcache_init();
247 while (!tagcache_is_initialized())
249 int ret = tagcache_get_commit_step();
251 if (ret > 0)
253 #if CONFIG_CODEC == SWCODEC
254 /* hwcodec can't use voice here, as the database commit
255 * uses the audio buffer. */
256 if(global_settings.talk_menu
257 && (talked_tick == 0
258 || TIME_AFTER(current_tick, talked_tick+7*HZ)))
260 talked_tick = current_tick;
261 talk_id(LANG_TAGCACHE_INIT, false);
262 talk_number(ret, true);
263 talk_id(VOICE_OF, true);
264 talk_number(tagcache_get_max_commit_step(), true);
266 #endif
267 #ifdef HAVE_LCD_BITMAP
268 if (lang_is_rtl())
270 splashf(0, "[%d/%d] %s", ret, tagcache_get_max_commit_step(),
271 str(LANG_TAGCACHE_INIT));
273 else
275 splashf(0, "%s [%d/%d]", str(LANG_TAGCACHE_INIT), ret,
276 tagcache_get_max_commit_step());
278 #else
279 lcd_double_height(false);
280 lcd_putsf(0, 1, " DB [%d/%d]", ret,
281 tagcache_get_max_commit_step());
282 lcd_update();
283 #endif
284 clear = true;
286 sleep(HZ/4);
288 tagtree_init();
290 if (clear)
292 backlight_on();
293 show_logo();
296 #endif
298 #ifdef SIMULATOR
300 static void init(void)
302 kernel_init();
303 buffer_init();
304 enable_irq();
305 lcd_init();
306 #ifdef HAVE_REMOTE_LCD
307 lcd_remote_init();
308 #endif
309 font_init();
310 show_logo();
311 button_init();
312 backlight_init();
313 sim_tasks_init();
314 lang_init(core_language_builtin, language_strings,
315 LANG_LAST_INDEX_IN_ARRAY);
316 #ifdef DEBUG
317 debug_init();
318 #endif
319 /* Must be done before any code uses the multi-screen API */
320 gui_syncstatusbar_init(&statusbars);
321 storage_init();
322 settings_reset();
323 settings_load(SETTINGS_ALL);
324 gui_sync_wps_init();
325 sb_skin_init();
326 settings_apply(true);
327 init_dircache(true);
328 init_dircache(false);
329 #ifdef HAVE_TAGCACHE
330 init_tagcache();
331 #endif
332 sleep(HZ/2);
333 tree_mem_init();
334 filetype_init();
335 playlist_init();
337 #if CONFIG_CODEC != SWCODEC
338 mp3_init( global_settings.volume,
339 global_settings.bass,
340 global_settings.treble,
341 global_settings.balance,
342 global_settings.loudness,
343 global_settings.avc,
344 global_settings.channel_config,
345 global_settings.stereo_width,
346 global_settings.mdb_strength,
347 global_settings.mdb_harmonics,
348 global_settings.mdb_center,
349 global_settings.mdb_shape,
350 global_settings.mdb_enable,
351 global_settings.superbass);
353 /* audio_init must to know the size of voice buffer so init voice first */
354 talk_init();
355 #endif /* CONFIG_CODEC != SWCODEC */
357 scrobbler_init();
358 #if CONFIG_CODEC == SWCODEC
359 tdspeed_init();
360 #endif /* CONFIG_CODEC == SWCODEC */
362 audio_init();
363 button_clear_queue(); /* Empty the keyboard buffer */
366 #else
368 static void init(void)
370 int rc;
371 bool mounted = false;
372 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
373 /* if nobody initialized ATA before, I consider this a cold start */
374 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
375 #endif
377 system_init();
378 kernel_init();
380 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
381 set_cpu_frequency(CPUFREQ_NORMAL);
382 #ifdef CPU_COLDFIRE
383 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
384 #endif
385 cpu_boost(true);
386 #endif
388 buffer_init();
390 settings_reset();
392 i2c_init();
394 power_init();
396 enable_irq();
397 #ifdef CPU_ARM
398 enable_fiq();
399 #endif
400 lcd_init();
401 #ifdef HAVE_REMOTE_LCD
402 lcd_remote_init();
403 #endif
404 font_init();
406 show_logo();
407 lang_init(core_language_builtin, language_strings,
408 LANG_LAST_INDEX_IN_ARRAY);
410 #ifdef DEBUG
411 debug_init();
412 #else
413 #ifdef HAVE_SERIAL
414 serial_setup();
415 #endif
416 #endif
418 #if CONFIG_RTC
419 rtc_init();
420 #endif
421 #ifdef HAVE_RTC_RAM
422 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
423 #endif
425 adc_init();
427 usb_init();
428 #if CONFIG_USBOTG == USBOTG_ISP1362
429 isp1362_init();
430 #elif CONFIG_USBOTG == USBOTG_M5636
431 m5636_init();
432 #endif
434 backlight_init();
436 button_init();
438 powermgmt_init();
440 #if CONFIG_TUNER
441 radio_init();
442 #endif
444 /* Must be done before any code uses the multi-screen API */
445 gui_syncstatusbar_init(&statusbars);
447 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
448 /* charger_inserted() can't be used here because power_thread()
449 hasn't checked power_input_status() yet */
450 if (coldstart && (power_input_status() & POWER_INPUT_MAIN_CHARGER)
451 && !global_settings.car_adapter_mode
452 #ifdef ATA_POWER_PLAYERSTYLE
453 && !ide_powered() /* relies on probing result from bootloader */
454 #endif
457 rc = charging_screen(); /* display a "charging" screen */
458 if (rc == 1) /* charger removed */
459 power_off();
460 /* "On" pressed or USB connected: proceed */
461 show_logo(); /* again, to provide better visual feedback */
463 #endif
465 rc = storage_init();
466 if(rc)
468 #ifdef HAVE_LCD_BITMAP
469 lcd_clear_display();
470 lcd_putsf(0, 1, "ATA error: %d", rc);
471 lcd_puts(0, 3, "Press ON to debug");
472 lcd_update();
473 while(!(button_get(true) & BUTTON_REL)); /*DO NOT CHANGE TO ACTION SYSTEM */
474 dbg_ports();
475 #endif
476 panicf("ata: %d", rc);
479 #ifdef HAVE_EEPROM_SETTINGS
480 eeprom_settings_init();
481 #endif
483 #ifndef HAVE_USBSTACK
484 usb_start_monitoring();
485 while (usb_detect() == USB_INSERTED)
487 #ifdef HAVE_EEPROM_SETTINGS
488 firmware_settings.disk_clean = false;
489 #endif
490 /* enter USB mode early, before trying to mount */
491 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
492 #if (CONFIG_STORAGE & STORAGE_MMC)
493 if (!mmc_touched() ||
494 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
495 #endif
497 gui_usb_screen_run();
498 mounted = true; /* mounting done @ end of USB mode */
500 #ifdef HAVE_USB_POWER
501 if (usb_powered()) /* avoid deadlock */
502 break;
503 #endif
505 #endif
507 if (!mounted)
509 rc = disk_mount_all();
510 if (rc<=0)
512 lcd_clear_display();
513 lcd_puts(0, 0, "No partition");
514 lcd_puts(0, 1, "found.");
515 #ifdef HAVE_LCD_BITMAP
516 lcd_puts(0, 2, "Insert USB cable");
517 lcd_puts(0, 3, "and fix it.");
518 #endif
519 lcd_update();
521 while(button_get(true) != SYS_USB_CONNECTED) {};
522 gui_usb_screen_run();
523 system_reboot();
527 #if defined(SETTINGS_RESET) || (CONFIG_KEYPAD == IPOD_4G_PAD) || \
528 (CONFIG_KEYPAD == IRIVER_H10_PAD)
529 #ifdef SETTINGS_RESET
530 /* Reset settings if holding the reset button. (Rec on Archos,
531 A on Gigabeat) */
532 if ((button_status() & SETTINGS_RESET) == SETTINGS_RESET)
533 #else
534 /* Reset settings if the hold button is turned on */
535 if (button_hold())
536 #endif
538 splash(HZ*2, str(LANG_RESET_DONE_CLEAR));
539 settings_reset();
541 else
542 #endif
543 settings_load(SETTINGS_ALL);
545 if (init_dircache(true) < 0)
547 #ifdef HAVE_TAGCACHE
548 remove(TAGCACHE_STATEFILE);
549 #endif
552 gui_sync_wps_init();
553 sb_skin_init();
554 settings_apply(true);
555 init_dircache(false);
556 #ifdef HAVE_TAGCACHE
557 init_tagcache();
558 #endif
560 #ifdef HAVE_EEPROM_SETTINGS
561 if (firmware_settings.initialized)
563 /* In case we crash. */
564 firmware_settings.disk_clean = false;
565 eeprom_settings_store();
567 #endif
568 playlist_init();
569 tree_mem_init();
570 filetype_init();
571 scrobbler_init();
572 #if CONFIG_CODEC == SWCODEC
573 tdspeed_init();
574 #endif /* CONFIG_CODEC == SWCODEC */
576 #if CONFIG_CODEC != SWCODEC
577 /* No buffer allocation (see buffer.c) may take place after the call to
578 audio_init() since the mpeg thread takes the rest of the buffer space */
579 mp3_init( global_settings.volume,
580 global_settings.bass,
581 global_settings.treble,
582 global_settings.balance,
583 global_settings.loudness,
584 global_settings.avc,
585 global_settings.channel_config,
586 global_settings.stereo_width,
587 global_settings.mdb_strength,
588 global_settings.mdb_harmonics,
589 global_settings.mdb_center,
590 global_settings.mdb_shape,
591 global_settings.mdb_enable,
592 global_settings.superbass);
594 /* audio_init must to know the size of voice buffer so init voice first */
595 talk_init();
596 #endif /* CONFIG_CODEC != SWCODEC */
598 audio_init();
600 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
601 pcm_rec_init();
602 #endif
604 /* runtime database has to be initialized after audio_init() */
605 cpu_boost(false);
607 #if CONFIG_CHARGING
608 car_adapter_mode_init();
609 #endif
610 #ifdef IPOD_ACCESSORY_PROTOCOL
611 iap_setup(global_settings.serial_bitrate);
612 #endif
613 #ifdef HAVE_ACCESSORY_SUPPLY
614 accessory_supply_set(global_settings.accessory_supply);
615 #endif
618 #ifdef CPU_PP
619 void cop_main(void)
621 /* This is the entry point for the coprocessor
622 Anyone not running an upgraded bootloader will never reach this point,
623 so it should not be assumed that the coprocessor be usable even on
624 platforms which support it.
626 A kernel thread is initially setup on the coprocessor and immediately
627 destroyed for purposes of continuity. The cop sits idle until at least
628 one thread exists on it. */
630 /* 3G doesn't have Rolo or dual core support yet */
631 #if NUM_CORES > 1
632 system_init();
633 kernel_init();
634 /* This should never be reached */
635 #endif
636 while(1) {
637 sleep_core(COP);
640 #endif /* CPU_PP */
642 int main(void)
644 app_main();
646 while(1) {
647 #if (CONFIG_LED == LED_REAL)
648 led(true); sleep(HZ/10);
649 led(false); sleep(HZ/10);
650 #endif
652 return 0;
654 #endif