Fix red introduced with r20610.
[kugel-rb/myfork.git] / apps / main.c
blob54ff191a3aaa67d9204909287ad142b11d9e3eac
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 "gwps.h"
56 #include "playlist.h"
57 #include "buffer.h"
58 #include "rolo.h"
59 #include "screens.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"
76 #ifdef IPOD_ACCESSORY_PROTOCOL
77 #include "iap.h"
78 #endif
80 #if (CONFIG_CODEC == SWCODEC)
81 #include "playback.h"
82 #endif
83 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
84 #include "pcm_record.h"
85 #endif
87 #ifdef BUTTON_REC
88 #define SETTINGS_RESET BUTTON_REC
89 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
90 #define SETTINGS_RESET BUTTON_A
91 #endif
93 #if CONFIG_TUNER
94 #include "radio.h"
95 #endif
96 #if (CONFIG_STORAGE & STORAGE_MMC)
97 #include "ata_mmc.h"
98 #endif
100 #ifdef HAVE_REMOTE_LCD
101 #include "lcd-remote.h"
102 #endif
104 #if CONFIG_USBOTG == USBOTG_ISP1362
105 #include "isp1362.h"
106 #endif
108 #if CONFIG_USBOTG == USBOTG_M5636
109 #include "m5636.h"
110 #endif
112 #include "cuesheet.h"
114 #ifdef SIMULATOR
115 #include "sim_tasks.h"
116 #include "system-sdl.h"
117 #endif
119 /*#define AUTOROCK*/ /* define this to check for "autostart.rock" on boot */
121 const char appsversion[]=APPSVERSION;
123 static void init(void);
125 #ifdef SIMULATOR
126 void app_main(void)
127 #else
128 static void app_main(void)
129 #endif
131 int i;
132 init();
133 FOR_NB_SCREENS(i)
135 screens[i].clear_display();
136 screens[i].update();
138 tree_gui_init();
139 viewportmanager_set_statusbar(VP_SB_ALLSCREENS);
140 add_event(GUI_EVENT_STATUSBAR_TOGGLE, false,
141 viewportmanager_statusbar_changed);
142 #ifdef HAVE_USBSTACK
143 /* All threads should be created and public queues registered by now */
144 usb_start_monitoring();
145 #endif
146 root_menu();
149 static int init_dircache(bool preinit)
151 #ifdef HAVE_DIRCACHE
152 int result = 0;
153 bool clear = false;
155 if (preinit)
156 dircache_init();
158 if (!global_settings.dircache)
159 return 0;
161 # ifdef HAVE_EEPROM_SETTINGS
162 if (firmware_settings.initialized && firmware_settings.disk_clean
163 && preinit)
165 result = dircache_load();
167 if (result < 0)
169 firmware_settings.disk_clean = false;
170 if (global_status.dircache_size <= 0)
172 /* This will be in default language, settings are not
173 applied yet. Not really any easy way to fix that. */
174 splash(0, str(LANG_SCANNING_DISK));
175 clear = true;
178 dircache_build(global_status.dircache_size);
181 else
182 # endif
184 if (preinit)
185 return -1;
187 if (!dircache_is_enabled()
188 && !dircache_is_initializing())
190 if (global_status.dircache_size <= 0)
192 splash(0, str(LANG_SCANNING_DISK));
193 clear = true;
195 result = dircache_build(global_status.dircache_size);
198 if (result < 0)
200 /* Initialization of dircache failed. Manual action is
201 * necessary to enable dircache again.
203 splashf(0, "Dircache failed, disabled. Result: %d", result);
204 global_settings.dircache = false;
209 if (clear)
211 backlight_on();
212 show_logo();
213 global_status.dircache_size = dircache_get_cache_size();
214 status_save();
217 return result;
218 #else
219 (void)preinit;
220 return 0;
221 #endif
224 #ifdef HAVE_TAGCACHE
225 static void init_tagcache(void)
227 bool clear = false;
229 tagcache_init();
231 while (!tagcache_is_initialized())
233 #ifdef HAVE_LCD_CHARCELLS
234 char buf[32];
235 #endif
236 int ret = tagcache_get_commit_step();
238 if (ret > 0)
240 #if CONFIG_CODEC == SWCODEC
241 /* hwcodec can't use voice here, as the database commit
242 * uses the audio buffer. */
243 static long talked_tick = 0;
244 if(global_settings.talk_menu
245 && (talked_tick == 0
246 || TIME_AFTER(current_tick, talked_tick+7*HZ)))
248 talked_tick = current_tick;
249 talk_id(LANG_TAGCACHE_INIT, false);
250 talk_number(ret, true);
251 talk_id(VOICE_OF, true);
252 talk_number(tagcache_get_max_commit_step(), true);
254 #endif
255 #ifdef HAVE_LCD_BITMAP
256 splashf(0, "%s [%d/%d]",
257 str(LANG_TAGCACHE_INIT), ret,
258 tagcache_get_max_commit_step());
259 #else
260 lcd_double_height(false);
261 snprintf(buf, sizeof(buf), " DB [%d/%d]", ret,
262 tagcache_get_max_commit_step());
263 lcd_puts(0, 1, buf);
264 lcd_update();
265 #endif
266 clear = true;
268 sleep(HZ/4);
270 tagtree_init();
272 if (clear)
274 backlight_on();
275 show_logo();
278 #endif
280 #ifdef SIMULATOR
282 static void init(void)
284 kernel_init();
285 buffer_init();
286 enable_irq();
287 lcd_init();
288 #ifdef HAVE_REMOTE_LCD
289 lcd_remote_init();
290 #endif
291 font_init();
292 show_logo();
293 button_init();
294 backlight_init();
295 sim_tasks_init();
296 lang_init();
297 #ifdef DEBUG
298 debug_init();
299 #endif
300 /* Must be done before any code uses the multi-screen APi */
301 gui_syncstatusbar_init(&statusbars);
302 storage_init();
303 settings_reset();
304 settings_load(SETTINGS_ALL);
305 gui_sync_wps_init();
306 settings_apply(true);
307 init_dircache(true);
308 init_dircache(false);
309 #ifdef HAVE_TAGCACHE
310 init_tagcache();
311 #endif
312 sleep(HZ/2);
313 tree_mem_init();
314 filetype_init();
315 playlist_init();
317 #if CONFIG_CODEC != SWCODEC
318 mp3_init( global_settings.volume,
319 global_settings.bass,
320 global_settings.treble,
321 global_settings.balance,
322 global_settings.loudness,
323 global_settings.avc,
324 global_settings.channel_config,
325 global_settings.stereo_width,
326 global_settings.mdb_strength,
327 global_settings.mdb_harmonics,
328 global_settings.mdb_center,
329 global_settings.mdb_shape,
330 global_settings.mdb_enable,
331 global_settings.superbass);
333 /* audio_init must to know the size of voice buffer so init voice first */
334 talk_init();
335 #endif /* CONFIG_CODEC != SWCODEC */
337 scrobbler_init();
338 cuesheet_init();
340 audio_init();
341 button_clear_queue(); /* Empty the keyboard buffer */
344 #else
346 static void init(void)
348 int rc;
349 bool mounted = false;
350 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
351 /* if nobody initialized ATA before, I consider this a cold start */
352 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
353 #endif
355 system_init();
356 kernel_init();
358 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
359 set_cpu_frequency(CPUFREQ_NORMAL);
360 #ifdef CPU_COLDFIRE
361 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
362 #endif
363 cpu_boost(true);
364 #endif
366 buffer_init();
368 settings_reset();
370 i2c_init();
372 power_init();
374 enable_irq();
375 #ifdef CPU_ARM
376 enable_fiq();
377 #endif
378 lcd_init();
379 #ifdef HAVE_REMOTE_LCD
380 lcd_remote_init();
381 #endif
382 font_init();
384 show_logo();
385 lang_init();
387 #ifdef DEBUG
388 debug_init();
389 #else
390 #ifdef HAVE_SERIAL
391 serial_setup();
392 #endif
393 #endif
395 #if CONFIG_RTC
396 rtc_init();
397 #endif
398 #ifdef HAVE_RTC_RAM
399 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
400 #endif
402 adc_init();
404 usb_init();
405 #if CONFIG_USBOTG == USBOTG_ISP1362
406 isp1362_init();
407 #elif CONFIG_USBOTG == USBOTG_M5636
408 m5636_init();
409 #endif
411 backlight_init();
413 button_init();
415 powermgmt_init();
417 #if CONFIG_TUNER
418 radio_init();
419 #endif
421 /* Must be done before any code uses the multi-screen APi */
422 gui_syncstatusbar_init(&statusbars);
424 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
425 if (coldstart && charger_inserted()
426 && !global_settings.car_adapter_mode
427 #ifdef ATA_POWER_PLAYERSTYLE
428 && !ide_powered() /* relies on probing result from bootloader */
429 #endif
432 rc = charging_screen(); /* display a "charging" screen */
433 if (rc == 1) /* charger removed */
434 power_off();
435 /* "On" pressed or USB connected: proceed */
436 show_logo(); /* again, to provide better visual feedback */
438 #endif
440 rc = storage_init();
441 if(rc)
443 #ifdef HAVE_LCD_BITMAP
444 char str[32];
445 lcd_clear_display();
446 snprintf(str, 31, "ATA error: %d", rc);
447 lcd_puts(0, 1, str);
448 lcd_puts(0, 3, "Press ON to debug");
449 lcd_update();
450 while(!(button_get(true) & BUTTON_REL)); /*DO NOT CHANGE TO ACTION SYSTEM */
451 dbg_ports();
452 #endif
453 panicf("ata: %d", rc);
456 #ifdef HAVE_EEPROM_SETTINGS
457 eeprom_settings_init();
458 #endif
460 #ifndef HAVE_USBSTACK
461 usb_start_monitoring();
462 while (usb_detect() == USB_INSERTED)
464 #ifdef HAVE_EEPROM_SETTINGS
465 firmware_settings.disk_clean = false;
466 #endif
467 /* enter USB mode early, before trying to mount */
468 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
469 #if (CONFIG_STORAGE & STORAGE_MMC)
470 if (!mmc_touched() ||
471 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
472 #endif
474 usb_screen();
475 mounted = true; /* mounting done @ end of USB mode */
477 #ifdef HAVE_USB_POWER
478 if (usb_powered()) /* avoid deadlock */
479 break;
480 #endif
482 #endif
484 if (!mounted)
486 rc = disk_mount_all();
487 if (rc<=0)
489 lcd_clear_display();
490 lcd_puts(0, 0, "No partition");
491 lcd_puts(0, 1, "found.");
492 #ifdef HAVE_LCD_BITMAP
493 lcd_puts(0, 2, "Insert USB cable");
494 lcd_puts(0, 3, "and fix it.");
495 #endif
496 lcd_update();
498 while(button_get(true) != SYS_USB_CONNECTED) {};
499 usb_screen();
500 system_reboot();
504 #if defined(SETTINGS_RESET) || (CONFIG_KEYPAD == IPOD_4G_PAD) || \
505 (CONFIG_KEYPAD == IRIVER_H10_PAD)
506 #ifdef SETTINGS_RESET
507 /* Reset settings if holding the reset button. (Rec on Archos,
508 A on Gigabeat) */
509 if ((button_status() & SETTINGS_RESET) == SETTINGS_RESET)
510 #else
511 /* Reset settings if the hold button is turned on */
512 if (button_hold())
513 #endif
515 splash(HZ*2, str(LANG_RESET_DONE_CLEAR));
516 settings_reset();
518 else
519 #endif
520 settings_load(SETTINGS_ALL);
522 if (init_dircache(true) < 0)
524 #ifdef HAVE_TAGCACHE
525 remove(TAGCACHE_STATEFILE);
526 #endif
529 gui_sync_wps_init();
530 settings_apply(true);
531 init_dircache(false);
532 #ifdef HAVE_TAGCACHE
533 init_tagcache();
534 #endif
536 #ifdef HAVE_EEPROM_SETTINGS
537 if (firmware_settings.initialized)
539 /* In case we crash. */
540 firmware_settings.disk_clean = false;
541 eeprom_settings_store();
543 #endif
544 status_init();
545 playlist_init();
546 tree_mem_init();
547 filetype_init();
548 scrobbler_init();
549 cuesheet_init();
551 #if CONFIG_CODEC != SWCODEC
552 /* No buffer allocation (see buffer.c) may take place after the call to
553 audio_init() since the mpeg thread takes the rest of the buffer space */
554 mp3_init( global_settings.volume,
555 global_settings.bass,
556 global_settings.treble,
557 global_settings.balance,
558 global_settings.loudness,
559 global_settings.avc,
560 global_settings.channel_config,
561 global_settings.stereo_width,
562 global_settings.mdb_strength,
563 global_settings.mdb_harmonics,
564 global_settings.mdb_center,
565 global_settings.mdb_shape,
566 global_settings.mdb_enable,
567 global_settings.superbass);
569 /* audio_init must to know the size of voice buffer so init voice first */
570 talk_init();
571 #endif /* CONFIG_CODEC != SWCODEC */
573 audio_init();
575 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
576 pcm_rec_init();
577 #endif
579 /* runtime database has to be initialized after audio_init() */
580 cpu_boost(false);
582 #ifdef AUTOROCK
584 static const char filename[] = PLUGIN_APPS_DIR "/autostart.rock";
586 if(file_exists(filename)) /* no complaint if it doesn't exist */
588 plugin_load((char*)filename, NULL); /* start if it does */
591 #endif /* #ifdef AUTOROCK */
593 #if CONFIG_CHARGING
594 car_adapter_mode_init();
595 #endif
596 #ifdef IPOD_ACCESSORY_PROTOCOL
597 iap_setup(global_settings.serial_bitrate);
598 #endif
599 #ifdef HAVE_ACCESSORY_SUPPLY
600 accessory_supply_set(global_settings.accessory_supply);
601 #endif
604 #ifdef CPU_PP
605 void cop_main(void)
607 /* This is the entry point for the coprocessor
608 Anyone not running an upgraded bootloader will never reach this point,
609 so it should not be assumed that the coprocessor be usable even on
610 platforms which support it.
612 A kernel thread is initially setup on the coprocessor and immediately
613 destroyed for purposes of continuity. The cop sits idle until at least
614 one thread exists on it. */
616 /* 3G doesn't have Rolo or dual core support yet */
617 #if NUM_CORES > 1
618 system_init();
619 kernel_init();
620 /* This should never be reached */
621 #endif
622 while(1) {
623 sleep_core(COP);
626 #endif /* CPU_PP */
628 int main(void)
630 app_main();
632 while(1) {
633 #if (CONFIG_LED == LED_REAL)
634 led(true); sleep(HZ/10);
635 led(false); sleep(HZ/10);
636 #endif
638 return 0;
640 #endif