mpegplayer:
[kugel-rb.git] / apps / main.c
blob6d2609b486b83365bbed1b19f48c9cc347e8decd
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 int main(void) __attribute__((noreturn));
131 int main(void)
132 #endif
134 int i;
135 init();
136 FOR_NB_SCREENS(i)
138 screens[i].clear_display();
139 screens[i].update();
141 #ifdef HAVE_LCD_BITMAP
142 list_init();
143 #endif
144 tree_gui_init();
145 /* Keep the order of this 3
146 * Must be done before any code uses the multi-screen API */
147 #ifdef HAVE_USBSTACK
148 /* All threads should be created and public queues registered by now */
149 usb_start_monitoring();
150 #endif
152 #ifdef AUTOROCK
154 static const char filename[] = PLUGIN_APPS_DIR "/autostart.rock";
156 if(file_exists(filename)) /* no complaint if it doesn't exist */
158 plugin_load((char*)filename, NULL); /* start if it does */
161 #endif /* #ifdef AUTOROCK */
163 global_status.last_volume_change = 0;
164 root_menu();
167 static int init_dircache(bool preinit)
169 #ifdef HAVE_DIRCACHE
170 int result = 0;
171 bool clear = false;
173 if (preinit)
174 dircache_init();
176 if (!global_settings.dircache)
177 return 0;
179 # ifdef HAVE_EEPROM_SETTINGS
180 if (firmware_settings.initialized && firmware_settings.disk_clean
181 && preinit)
183 result = dircache_load();
185 if (result < 0)
187 firmware_settings.disk_clean = false;
188 if (global_status.dircache_size <= 0)
190 /* This will be in default language, settings are not
191 applied yet. Not really any easy way to fix that. */
192 splash(0, str(LANG_SCANNING_DISK));
193 clear = true;
196 dircache_build(global_status.dircache_size);
199 else
200 # endif
202 if (preinit)
203 return -1;
205 if (!dircache_is_enabled()
206 && !dircache_is_initializing())
208 if (global_status.dircache_size <= 0)
210 splash(0, str(LANG_SCANNING_DISK));
211 clear = true;
213 result = dircache_build(global_status.dircache_size);
216 if (result < 0)
218 /* Initialization of dircache failed. Manual action is
219 * necessary to enable dircache again.
221 splashf(0, "Dircache failed, disabled. Result: %d", result);
222 global_settings.dircache = false;
226 if (clear)
228 backlight_on();
229 show_logo();
230 global_status.dircache_size = dircache_get_cache_size();
231 status_save();
234 return result;
235 #else
236 (void)preinit;
237 return 0;
238 #endif
241 #ifdef HAVE_TAGCACHE
242 static void init_tagcache(void)
244 bool clear = false;
245 #if CONFIG_CODEC == SWCODEC
246 long talked_tick = 0;
247 #endif
248 tagcache_init();
250 while (!tagcache_is_initialized())
252 int ret = tagcache_get_commit_step();
254 if (ret > 0)
256 #if CONFIG_CODEC == SWCODEC
257 /* hwcodec can't use voice here, as the database commit
258 * uses the audio buffer. */
259 if(global_settings.talk_menu
260 && (talked_tick == 0
261 || TIME_AFTER(current_tick, talked_tick+7*HZ)))
263 talked_tick = current_tick;
264 talk_id(LANG_TAGCACHE_INIT, false);
265 talk_number(ret, true);
266 talk_id(VOICE_OF, true);
267 talk_number(tagcache_get_max_commit_step(), true);
269 #endif
270 #ifdef HAVE_LCD_BITMAP
271 if (lang_is_rtl())
273 splashf(0, "[%d/%d] %s", ret, tagcache_get_max_commit_step(),
274 str(LANG_TAGCACHE_INIT));
276 else
278 splashf(0, "%s [%d/%d]", str(LANG_TAGCACHE_INIT), ret,
279 tagcache_get_max_commit_step());
281 #else
282 lcd_double_height(false);
283 lcd_putsf(0, 1, " DB [%d/%d]", ret,
284 tagcache_get_max_commit_step());
285 lcd_update();
286 #endif
287 clear = true;
289 sleep(HZ/4);
291 tagtree_init();
293 if (clear)
295 backlight_on();
296 show_logo();
299 #endif
301 #ifdef SIMULATOR
303 static void init(void)
305 kernel_init();
306 buffer_init();
307 enable_irq();
308 lcd_init();
309 #ifdef HAVE_REMOTE_LCD
310 lcd_remote_init();
311 #endif
312 font_init();
313 show_logo();
314 button_init();
315 backlight_init();
316 sim_tasks_init();
317 lang_init(core_language_builtin, language_strings,
318 LANG_LAST_INDEX_IN_ARRAY);
319 #ifdef DEBUG
320 debug_init();
321 #endif
322 /* Keep the order of this 3 (viewportmanager handles statusbars)
323 * Must be done before any code uses the multi-screen API */
324 gui_syncstatusbar_init(&statusbars);
325 sb_skin_init();
326 viewportmanager_init();
328 gui_sync_wps_init();
329 storage_init();
330 settings_reset();
331 settings_load(SETTINGS_ALL);
332 settings_apply(false);
333 init_dircache(true);
334 init_dircache(false);
335 #ifdef HAVE_TAGCACHE
336 init_tagcache();
337 #endif
338 sleep(HZ/2);
339 tree_mem_init();
340 filetype_init();
341 playlist_init();
343 #if CONFIG_CODEC != SWCODEC
344 mp3_init( global_settings.volume,
345 global_settings.bass,
346 global_settings.treble,
347 global_settings.balance,
348 global_settings.loudness,
349 global_settings.avc,
350 global_settings.channel_config,
351 global_settings.stereo_width,
352 global_settings.mdb_strength,
353 global_settings.mdb_harmonics,
354 global_settings.mdb_center,
355 global_settings.mdb_shape,
356 global_settings.mdb_enable,
357 global_settings.superbass);
359 /* audio_init must to know the size of voice buffer so init voice first */
360 talk_init();
361 #endif /* CONFIG_CODEC != SWCODEC */
363 scrobbler_init();
364 #if CONFIG_CODEC == SWCODEC
365 tdspeed_init();
366 #endif /* CONFIG_CODEC == SWCODEC */
368 audio_init();
369 button_clear_queue(); /* Empty the keyboard buffer */
371 settings_apply(true);
374 #else
376 static void init(void)
378 int rc;
379 bool mounted = false;
380 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
381 /* if nobody initialized ATA before, I consider this a cold start */
382 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
383 #endif
385 system_init();
386 kernel_init();
388 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
389 set_cpu_frequency(CPUFREQ_NORMAL);
390 #ifdef CPU_COLDFIRE
391 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
392 #endif
393 cpu_boost(true);
394 #endif
396 buffer_init();
398 settings_reset();
400 i2c_init();
402 power_init();
404 enable_irq();
405 #ifdef CPU_ARM
406 enable_fiq();
407 #endif
408 lcd_init();
409 #ifdef HAVE_REMOTE_LCD
410 lcd_remote_init();
411 #endif
412 font_init();
414 show_logo();
415 lang_init(core_language_builtin, language_strings,
416 LANG_LAST_INDEX_IN_ARRAY);
418 #ifdef DEBUG
419 debug_init();
420 #else
421 #ifdef HAVE_SERIAL
422 serial_setup();
423 #endif
424 #endif
426 #if CONFIG_RTC
427 rtc_init();
428 #endif
429 #ifdef HAVE_RTC_RAM
430 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
431 #endif
433 adc_init();
435 usb_init();
436 #if CONFIG_USBOTG == USBOTG_ISP1362
437 isp1362_init();
438 #elif CONFIG_USBOTG == USBOTG_M5636
439 m5636_init();
440 #endif
442 backlight_init();
444 button_init();
446 powermgmt_init();
448 #if CONFIG_TUNER
449 radio_init();
450 #endif
452 /* Keep the order of this 3 (viewportmanager handles statusbars)
453 * Must be done before any code uses the multi-screen API */
454 gui_syncstatusbar_init(&statusbars);
455 sb_skin_init();
456 viewportmanager_init();
458 gui_sync_wps_init();
460 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
461 /* charger_inserted() can't be used here because power_thread()
462 hasn't checked power_input_status() yet */
463 if (coldstart && (power_input_status() & POWER_INPUT_MAIN_CHARGER)
464 && !global_settings.car_adapter_mode
465 #ifdef ATA_POWER_PLAYERSTYLE
466 && !ide_powered() /* relies on probing result from bootloader */
467 #endif
470 rc = charging_screen(); /* display a "charging" screen */
471 if (rc == 1) /* charger removed */
472 power_off();
473 /* "On" pressed or USB connected: proceed */
474 show_logo(); /* again, to provide better visual feedback */
476 #endif
478 rc = storage_init();
479 if(rc)
481 #ifdef HAVE_LCD_BITMAP
482 lcd_clear_display();
483 lcd_putsf(0, 1, "ATA error: %d", rc);
484 lcd_puts(0, 3, "Press ON to debug");
485 lcd_update();
486 while(!(button_get(true) & BUTTON_REL)); /*DO NOT CHANGE TO ACTION SYSTEM */
487 dbg_ports();
488 #endif
489 panicf("ata: %d", rc);
492 #ifdef HAVE_EEPROM_SETTINGS
493 eeprom_settings_init();
494 #endif
496 #ifndef HAVE_USBSTACK
497 usb_start_monitoring();
498 while (usb_detect() == USB_INSERTED)
500 #ifdef HAVE_EEPROM_SETTINGS
501 firmware_settings.disk_clean = false;
502 #endif
503 /* enter USB mode early, before trying to mount */
504 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
505 #if (CONFIG_STORAGE & STORAGE_MMC)
506 if (!mmc_touched() ||
507 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
508 #endif
510 gui_usb_screen_run();
511 mounted = true; /* mounting done @ end of USB mode */
513 #ifdef HAVE_USB_POWER
514 if (usb_powered()) /* avoid deadlock */
515 break;
516 #endif
518 #endif
520 if (!mounted)
522 rc = disk_mount_all();
523 if (rc<=0)
525 lcd_clear_display();
526 lcd_puts(0, 0, "No partition");
527 lcd_puts(0, 1, "found.");
528 #ifdef HAVE_LCD_BITMAP
529 lcd_puts(0, 2, "Insert USB cable");
530 lcd_puts(0, 3, "and fix it.");
531 #endif
532 lcd_update();
534 while(button_get(true) != SYS_USB_CONNECTED) {};
535 gui_usb_screen_run();
536 system_reboot();
540 #if defined(SETTINGS_RESET) || (CONFIG_KEYPAD == IPOD_4G_PAD) || \
541 (CONFIG_KEYPAD == IRIVER_H10_PAD)
542 #ifdef SETTINGS_RESET
543 /* Reset settings if holding the reset button. (Rec on Archos,
544 A on Gigabeat) */
545 if ((button_status() & SETTINGS_RESET) == SETTINGS_RESET)
546 #else
547 /* Reset settings if the hold button is turned on */
548 if (button_hold())
549 #endif
551 splash(HZ*2, str(LANG_RESET_DONE_CLEAR));
552 settings_reset();
554 else
555 #endif
556 settings_load(SETTINGS_ALL);
558 if (init_dircache(true) < 0)
560 #ifdef HAVE_TAGCACHE
561 remove(TAGCACHE_STATEFILE);
562 #endif
565 settings_apply(false);
566 init_dircache(false);
567 #ifdef HAVE_TAGCACHE
568 init_tagcache();
569 #endif
571 #ifdef HAVE_EEPROM_SETTINGS
572 if (firmware_settings.initialized)
574 /* In case we crash. */
575 firmware_settings.disk_clean = false;
576 eeprom_settings_store();
578 #endif
579 playlist_init();
580 tree_mem_init();
581 filetype_init();
582 scrobbler_init();
583 #if CONFIG_CODEC == SWCODEC
584 tdspeed_init();
585 #endif /* CONFIG_CODEC == SWCODEC */
587 #if CONFIG_CODEC != SWCODEC
588 /* No buffer allocation (see buffer.c) may take place after the call to
589 audio_init() since the mpeg thread takes the rest of the buffer space */
590 mp3_init( global_settings.volume,
591 global_settings.bass,
592 global_settings.treble,
593 global_settings.balance,
594 global_settings.loudness,
595 global_settings.avc,
596 global_settings.channel_config,
597 global_settings.stereo_width,
598 global_settings.mdb_strength,
599 global_settings.mdb_harmonics,
600 global_settings.mdb_center,
601 global_settings.mdb_shape,
602 global_settings.mdb_enable,
603 global_settings.superbass);
605 /* audio_init must to know the size of voice buffer so init voice first */
606 talk_init();
607 #endif /* CONFIG_CODEC != SWCODEC */
609 audio_init();
611 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
612 pcm_rec_init();
613 #endif
615 /* runtime database has to be initialized after audio_init() */
616 cpu_boost(false);
618 #if CONFIG_CHARGING
619 car_adapter_mode_init();
620 #endif
621 #ifdef IPOD_ACCESSORY_PROTOCOL
622 iap_setup(global_settings.serial_bitrate);
623 #endif
624 #ifdef HAVE_ACCESSORY_SUPPLY
625 accessory_supply_set(global_settings.accessory_supply);
626 #endif
627 #ifdef HAVE_HOTSWAP_STORAGE_AS_MAIN
628 check_bootfile(false); /* remember write time and filesize */
629 #endif
631 settings_apply(true);
634 #ifdef CPU_PP
635 void cop_main(void)
637 /* This is the entry point for the coprocessor
638 Anyone not running an upgraded bootloader will never reach this point,
639 so it should not be assumed that the coprocessor be usable even on
640 platforms which support it.
642 A kernel thread is initially setup on the coprocessor and immediately
643 destroyed for purposes of continuity. The cop sits idle until at least
644 one thread exists on it. */
646 /* 3G doesn't have Rolo or dual core support yet */
647 #if NUM_CORES > 1
648 system_init();
649 kernel_init();
650 /* This should never be reached */
651 #endif
652 while(1) {
653 sleep_core(COP);
656 #endif /* CPU_PP */
658 #endif