Add uppercase M4A file type to the mp4 parser. Technically Apple says to use lowerca...
[kugel-rb.git] / apps / main.c
blobceaa85f38ff5159d0c9af7be0567ab2bc60ecaf2
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 "statusbar-skinned.h"
77 #include "bootchart.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 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
117 #define MAIN_NORETURN_ATTR NORETURN_ATTR
118 #else
119 /* gcc adds an implicit 'return 0;' at the end of main(), causing a warning
120 * with noreturn attribute */
121 #define MAIN_NORETURN_ATTR
122 #endif
124 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
125 #include "sim_tasks.h"
126 #endif
128 #if (CONFIG_PLATFORM & PLATFORM_SDL)
129 #include "system-sdl.h"
130 #define HAVE_ARGV_MAIN
131 /* Don't use SDL_main on windows -> no more stdio redirection */
132 #if defined(WIN32)
133 #undef main
134 #endif
135 #endif
137 /*#define AUTOROCK*/ /* define this to check for "autostart.rock" on boot */
139 static void init(void);
140 /* main(), and various functions called by main() and init() may be
141 * be INIT_ATTR. These functions must not be called after the final call
142 * to root_menu() at the end of main()
143 * see definition of INIT_ATTR in config.h */
144 #ifdef HAVE_ARGV_MAIN
145 int main(int argc, char *argv[]) INIT_ATTR MAIN_NORETURN_ATTR ;
146 int main(int argc, char *argv[])
148 sys_handle_argv(argc, argv);
149 #else
150 int main(void) INIT_ATTR MAIN_NORETURN_ATTR;
151 int main(void)
153 #endif
154 int i;
155 CHART(">init");
156 init();
157 CHART("<init");
158 FOR_NB_SCREENS(i)
160 screens[i].clear_display();
161 screens[i].update();
163 #ifdef HAVE_LCD_BITMAP
164 list_init();
165 #endif
166 tree_gui_init();
167 /* Keep the order of this 3
168 * Must be done before any code uses the multi-screen API */
169 #ifdef HAVE_USBSTACK
170 /* All threads should be created and public queues registered by now */
171 usb_start_monitoring();
172 #endif
174 #ifdef AUTOROCK
176 char filename[MAX_PATH];
177 const char *file = get_user_file_path(
178 #ifdef APPLICATION
179 ROCKBOX_DIR
180 #else
181 PLUGIN_APPS_DIR
182 #endif
183 "/autostart.rock", NEED_WRITE|IS_FILE, filename, sizeof(filename));
184 if(file_exists(file)) /* no complaint if it doesn't exist */
186 plugin_load(file, NULL); /* start if it does */
189 #endif /* #ifdef AUTOROCK */
191 global_status.last_volume_change = 0;
192 /* no calls INIT_ATTR functions after this point anymore!
193 * see definition of INIT_ATTR in config.h */
194 CHART(">root_menu");
195 root_menu();
198 static int init_dircache(bool preinit) INIT_ATTR;
199 static int init_dircache(bool preinit)
201 #ifdef HAVE_DIRCACHE
202 int result = 0;
203 bool clear = false;
205 if (preinit)
206 dircache_init();
208 if (!global_settings.dircache)
209 return 0;
211 # ifdef HAVE_EEPROM_SETTINGS
212 if (firmware_settings.initialized && firmware_settings.disk_clean
213 && preinit)
215 result = dircache_load();
217 if (result < 0)
219 firmware_settings.disk_clean = false;
220 if (global_status.dircache_size <= 0)
222 /* This will be in default language, settings are not
223 applied yet. Not really any easy way to fix that. */
224 splash(0, str(LANG_SCANNING_DISK));
225 clear = true;
228 dircache_build(global_status.dircache_size);
231 else
232 # endif
234 if (preinit)
235 return -1;
237 if (!dircache_is_enabled()
238 && !dircache_is_initializing())
240 if (global_status.dircache_size <= 0)
242 splash(0, str(LANG_SCANNING_DISK));
243 clear = true;
245 result = dircache_build(global_status.dircache_size);
248 if (result < 0)
250 /* Initialization of dircache failed. Manual action is
251 * necessary to enable dircache again.
253 splashf(0, "Dircache failed, disabled. Result: %d", result);
254 global_settings.dircache = false;
258 if (clear)
260 backlight_on();
261 show_logo();
262 global_status.dircache_size = dircache_get_cache_size();
263 status_save();
266 return result;
267 #else
268 (void)preinit;
269 return 0;
270 #endif
273 #ifdef HAVE_TAGCACHE
274 static void init_tagcache(void) INIT_ATTR;
275 static void init_tagcache(void)
277 bool clear = false;
278 #if CONFIG_CODEC == SWCODEC
279 long talked_tick = 0;
280 #endif
281 tagcache_init();
283 while (!tagcache_is_initialized())
285 int ret = tagcache_get_commit_step();
287 if (ret > 0)
289 #if CONFIG_CODEC == SWCODEC
290 /* hwcodec can't use voice here, as the database commit
291 * uses the audio buffer. */
292 if(global_settings.talk_menu
293 && (talked_tick == 0
294 || TIME_AFTER(current_tick, talked_tick+7*HZ)))
296 talked_tick = current_tick;
297 talk_id(LANG_TAGCACHE_INIT, false);
298 talk_number(ret, true);
299 talk_id(VOICE_OF, true);
300 talk_number(tagcache_get_max_commit_step(), true);
302 #endif
303 #ifdef HAVE_LCD_BITMAP
304 if (lang_is_rtl())
306 splashf(0, "[%d/%d] %s", ret, tagcache_get_max_commit_step(),
307 str(LANG_TAGCACHE_INIT));
309 else
311 splashf(0, "%s [%d/%d]", str(LANG_TAGCACHE_INIT), ret,
312 tagcache_get_max_commit_step());
314 #else
315 lcd_double_height(false);
316 lcd_putsf(0, 1, " DB [%d/%d]", ret,
317 tagcache_get_max_commit_step());
318 lcd_update();
319 #endif
320 clear = true;
322 sleep(HZ/4);
324 tagtree_init();
326 if (clear)
328 backlight_on();
329 show_logo();
332 #endif
334 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
336 static void init(void)
338 #ifdef APPLICATION
339 paths_init();
340 #endif
341 system_init();
342 kernel_init();
343 buffer_init();
344 enable_irq();
345 lcd_init();
346 #ifdef HAVE_REMOTE_LCD
347 lcd_remote_init();
348 #endif
349 font_init();
350 show_logo();
351 button_init();
352 backlight_init();
353 sim_tasks_init();
354 lang_init(core_language_builtin, language_strings,
355 LANG_LAST_INDEX_IN_ARRAY);
356 #ifdef DEBUG
357 debug_init();
358 #endif
359 /* Keep the order of this 3 (viewportmanager handles statusbars)
360 * Must be done before any code uses the multi-screen API */
361 gui_syncstatusbar_init(&statusbars);
362 gui_sync_wps_init();
363 sb_skin_init();
364 viewportmanager_init();
366 storage_init();
367 settings_reset();
368 settings_load(SETTINGS_ALL);
369 settings_apply(true);
370 init_dircache(true);
371 init_dircache(false);
372 #ifdef HAVE_TAGCACHE
373 init_tagcache();
374 #endif
375 sleep(HZ/2);
376 tree_mem_init();
377 filetype_init();
378 playlist_init();
379 theme_init_buffer();
381 #if CONFIG_CODEC != SWCODEC
382 mp3_init( global_settings.volume,
383 global_settings.bass,
384 global_settings.treble,
385 global_settings.balance,
386 global_settings.loudness,
387 global_settings.avc,
388 global_settings.channel_config,
389 global_settings.stereo_width,
390 global_settings.mdb_strength,
391 global_settings.mdb_harmonics,
392 global_settings.mdb_center,
393 global_settings.mdb_shape,
394 global_settings.mdb_enable,
395 global_settings.superbass);
397 /* audio_init must to know the size of voice buffer so init voice first */
398 talk_init();
399 #endif /* CONFIG_CODEC != SWCODEC */
401 scrobbler_init();
402 #if CONFIG_CODEC == SWCODEC
403 tdspeed_init();
404 #endif /* CONFIG_CODEC == SWCODEC */
406 audio_init();
407 button_clear_queue(); /* Empty the keyboard buffer */
409 settings_apply_skins();
412 #else
414 static void init(void) INIT_ATTR;
415 static void init(void)
417 int rc;
418 bool mounted = false;
419 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
420 /* if nobody initialized ATA before, I consider this a cold start */
421 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
422 #endif
424 system_init();
425 kernel_init();
427 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
428 set_cpu_frequency(CPUFREQ_NORMAL);
429 #ifdef CPU_COLDFIRE
430 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
431 #endif
432 cpu_boost(true);
433 #endif
435 buffer_init();
437 settings_reset();
439 i2c_init();
441 power_init();
443 enable_irq();
444 #ifdef CPU_ARM
445 enable_fiq();
446 #endif
447 /* current_tick should be ticking by now */
448 CHART("ticking");
450 lcd_init();
451 #ifdef HAVE_REMOTE_LCD
452 lcd_remote_init();
453 #endif
454 font_init();
456 CHART(">show_logo");
457 show_logo();
458 CHART("<show_logo");
459 lang_init(core_language_builtin, language_strings,
460 LANG_LAST_INDEX_IN_ARRAY);
462 #ifdef DEBUG
463 debug_init();
464 #else
465 #ifdef HAVE_SERIAL
466 serial_setup();
467 #endif
468 #endif
470 #if CONFIG_RTC
471 rtc_init();
472 #endif
473 #ifdef HAVE_RTC_RAM
474 CHART(">settings_load(RTC)");
475 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
476 CHART("<settings_load(RTC)");
477 #endif
479 adc_init();
481 usb_init();
482 #if CONFIG_USBOTG == USBOTG_ISP1362
483 isp1362_init();
484 #elif CONFIG_USBOTG == USBOTG_M5636
485 m5636_init();
486 #endif
488 backlight_init();
490 button_init();
492 powermgmt_init();
494 #if CONFIG_TUNER
495 radio_init();
496 #endif
498 /* Keep the order of this 3 (viewportmanager handles statusbars)
499 * Must be done before any code uses the multi-screen API */
500 CHART(">gui_syncstatusbar_init");
501 gui_syncstatusbar_init(&statusbars);
502 CHART("<gui_syncstatusbar_init");
503 CHART(">sb_skin_init");
504 sb_skin_init();
505 CHART("<sb_skin_init");
506 CHART(">gui_sync_wps_init");
507 gui_sync_wps_init();
508 CHART("<gui_sync_wps_init");
509 CHART(">viewportmanager_init");
510 viewportmanager_init();
511 CHART("<viewportmanager_init");
513 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
514 /* charger_inserted() can't be used here because power_thread()
515 hasn't checked power_input_status() yet */
516 if (coldstart && (power_input_status() & POWER_INPUT_MAIN_CHARGER)
517 && !global_settings.car_adapter_mode
518 #ifdef ATA_POWER_PLAYERSTYLE
519 && !ide_powered() /* relies on probing result from bootloader */
520 #endif
523 rc = charging_screen(); /* display a "charging" screen */
524 if (rc == 1) /* charger removed */
525 power_off();
526 /* "On" pressed or USB connected: proceed */
527 show_logo(); /* again, to provide better visual feedback */
529 #endif
532 disk_init_subsystem();
533 CHART(">storage_init");
534 rc = storage_init();
535 CHART("<storage_init");
536 if(rc)
538 #ifdef HAVE_LCD_BITMAP
539 lcd_clear_display();
540 lcd_putsf(0, 1, "ATA error: %d", rc);
541 lcd_puts(0, 3, "Press ON to debug");
542 lcd_update();
543 while(!(button_get(true) & BUTTON_REL)); /*DO NOT CHANGE TO ACTION SYSTEM */
544 dbg_ports();
545 #endif
546 panicf("ata: %d", rc);
549 #ifdef HAVE_EEPROM_SETTINGS
550 CHART(">eeprom_settings_init");
551 eeprom_settings_init();
552 CHART("<eeprom_settings_init");
553 #endif
555 #ifndef HAVE_USBSTACK
556 usb_start_monitoring();
557 while (usb_detect() == USB_INSERTED)
559 #ifdef HAVE_EEPROM_SETTINGS
560 firmware_settings.disk_clean = false;
561 #endif
562 /* enter USB mode early, before trying to mount */
563 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
564 #if (CONFIG_STORAGE & STORAGE_MMC)
565 if (!mmc_touched() ||
566 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
567 #endif
569 gui_usb_screen_run();
570 mounted = true; /* mounting done @ end of USB mode */
572 #ifdef HAVE_USB_POWER
573 if (usb_powered()) /* avoid deadlock */
574 break;
575 #endif
577 #endif
579 if (!mounted)
581 CHART(">disk_mount_all");
582 rc = disk_mount_all();
583 CHART("<disk_mount_all");
584 if (rc<=0)
586 lcd_clear_display();
587 lcd_puts(0, 0, "No partition");
588 lcd_puts(0, 1, "found.");
589 #ifdef HAVE_LCD_BITMAP
590 lcd_puts(0, 2, "Insert USB cable");
591 lcd_puts(0, 3, "and fix it.");
592 #endif
593 lcd_update();
595 while(button_get(true) != SYS_USB_CONNECTED) {};
596 gui_usb_screen_run();
597 system_reboot();
601 #if defined(SETTINGS_RESET) || (CONFIG_KEYPAD == IPOD_4G_PAD) || \
602 (CONFIG_KEYPAD == IRIVER_H10_PAD)
603 #ifdef SETTINGS_RESET
604 /* Reset settings if holding the reset button. (Rec on Archos,
605 A on Gigabeat) */
606 if ((button_status() & SETTINGS_RESET) == SETTINGS_RESET)
607 #else
608 /* Reset settings if the hold button is turned on */
609 if (button_hold())
610 #endif
612 splash(HZ*2, str(LANG_RESET_DONE_CLEAR));
613 settings_reset();
615 else
616 #endif
618 CHART(">settings_load(ALL)");
619 settings_load(SETTINGS_ALL);
620 CHART("<settings_load(ALL)");
623 CHART(">init_dircache(true)");
624 rc = init_dircache(true);
625 CHART("<init_dircache(true)");
626 if (rc < 0)
628 #ifdef HAVE_TAGCACHE
629 remove(TAGCACHE_STATEFILE);
630 #endif
633 CHART(">settings_apply(true)");
634 settings_apply(true);
635 CHART("<settings_apply(true)");
636 CHART(">init_dircache(false)");
637 init_dircache(false);
638 CHART("<init_dircache(false)");
639 #ifdef HAVE_TAGCACHE
640 CHART(">init_tagcache");
641 init_tagcache();
642 CHART("<init_tagcache");
643 #endif
645 #ifdef HAVE_EEPROM_SETTINGS
646 if (firmware_settings.initialized)
648 /* In case we crash. */
649 firmware_settings.disk_clean = false;
650 CHART(">eeprom_settings_store");
651 eeprom_settings_store();
652 CHART("<eeprom_settings_store");
654 #endif
655 playlist_init();
656 tree_mem_init();
657 filetype_init();
658 scrobbler_init();
659 #if CONFIG_CODEC == SWCODEC
660 tdspeed_init();
661 #endif /* CONFIG_CODEC == SWCODEC */
662 theme_init_buffer();
664 #if CONFIG_CODEC != SWCODEC
665 /* No buffer allocation (see buffer.c) may take place after the call to
666 audio_init() since the mpeg thread takes the rest of the buffer space */
667 mp3_init( global_settings.volume,
668 global_settings.bass,
669 global_settings.treble,
670 global_settings.balance,
671 global_settings.loudness,
672 global_settings.avc,
673 global_settings.channel_config,
674 global_settings.stereo_width,
675 global_settings.mdb_strength,
676 global_settings.mdb_harmonics,
677 global_settings.mdb_center,
678 global_settings.mdb_shape,
679 global_settings.mdb_enable,
680 global_settings.superbass);
682 /* audio_init must to know the size of voice buffer so init voice first */
683 talk_init();
684 #endif /* CONFIG_CODEC != SWCODEC */
686 CHART(">audio_init");
687 audio_init();
688 CHART("<audio_init");
690 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
691 pcm_rec_init();
692 #endif
694 /* runtime database has to be initialized after audio_init() */
695 cpu_boost(false);
697 #if CONFIG_CHARGING
698 car_adapter_mode_init();
699 #endif
700 #ifdef IPOD_ACCESSORY_PROTOCOL
701 iap_setup(global_settings.serial_bitrate);
702 #endif
703 #ifdef HAVE_ACCESSORY_SUPPLY
704 accessory_supply_set(global_settings.accessory_supply);
705 #endif
706 #ifdef HAVE_LINEOUT_POWEROFF
707 lineout_set(global_settings.lineout_active);
708 #endif
709 #ifdef HAVE_HOTSWAP_STORAGE_AS_MAIN
710 CHART("<check_bootfile(false)");
711 check_bootfile(false); /* remember write time and filesize */
712 CHART(">check_bootfile(false)");
713 #endif
714 CHART("<settings_apply_skins");
715 settings_apply_skins();
716 CHART(">settings_apply_skins");
719 #ifdef CPU_PP
720 void cop_main(void) MAIN_NORETURN_ATTR;
721 void cop_main(void)
723 /* This is the entry point for the coprocessor
724 Anyone not running an upgraded bootloader will never reach this point,
725 so it should not be assumed that the coprocessor be usable even on
726 platforms which support it.
728 A kernel thread is initially setup on the coprocessor and immediately
729 destroyed for purposes of continuity. The cop sits idle until at least
730 one thread exists on it. */
732 #if NUM_CORES > 1
733 system_init();
734 kernel_init();
735 /* This should never be reached */
736 #endif
737 while(1) {
738 sleep_core(COP);
741 #endif /* CPU_PP */
743 #endif /* SIMULATOR */