FS#10066 by Michael Chicoine: Sansa AMS bootloader "Hold" switch detection.
[kugel-rb.git] / apps / main.c
blob642ec5be0890c2bd07aa7e6379a0ede133f7f13e
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 /* charger_inserted() can't be used here because power_thread()
426 hasn't checked power_input_status() yet */
427 if (coldstart && (power_input_status() & POWER_INPUT_MAIN_CHARGER)
428 && !global_settings.car_adapter_mode
429 #ifdef ATA_POWER_PLAYERSTYLE
430 && !ide_powered() /* relies on probing result from bootloader */
431 #endif
434 rc = charging_screen(); /* display a "charging" screen */
435 if (rc == 1) /* charger removed */
436 power_off();
437 /* "On" pressed or USB connected: proceed */
438 show_logo(); /* again, to provide better visual feedback */
440 #endif
442 rc = storage_init();
443 if(rc)
445 #ifdef HAVE_LCD_BITMAP
446 char str[32];
447 lcd_clear_display();
448 snprintf(str, 31, "ATA error: %d", rc);
449 lcd_puts(0, 1, str);
450 lcd_puts(0, 3, "Press ON to debug");
451 lcd_update();
452 while(!(button_get(true) & BUTTON_REL)); /*DO NOT CHANGE TO ACTION SYSTEM */
453 dbg_ports();
454 #endif
455 panicf("ata: %d", rc);
458 #ifdef HAVE_EEPROM_SETTINGS
459 eeprom_settings_init();
460 #endif
462 #ifndef HAVE_USBSTACK
463 usb_start_monitoring();
464 while (usb_detect() == USB_INSERTED)
466 #ifdef HAVE_EEPROM_SETTINGS
467 firmware_settings.disk_clean = false;
468 #endif
469 /* enter USB mode early, before trying to mount */
470 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
471 #if (CONFIG_STORAGE & STORAGE_MMC)
472 if (!mmc_touched() ||
473 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
474 #endif
476 usb_screen();
477 mounted = true; /* mounting done @ end of USB mode */
479 #ifdef HAVE_USB_POWER
480 if (usb_powered()) /* avoid deadlock */
481 break;
482 #endif
484 #endif
486 if (!mounted)
488 rc = disk_mount_all();
489 if (rc<=0)
491 lcd_clear_display();
492 lcd_puts(0, 0, "No partition");
493 lcd_puts(0, 1, "found.");
494 #ifdef HAVE_LCD_BITMAP
495 lcd_puts(0, 2, "Insert USB cable");
496 lcd_puts(0, 3, "and fix it.");
497 #endif
498 lcd_update();
500 while(button_get(true) != SYS_USB_CONNECTED) {};
501 usb_screen();
502 system_reboot();
506 #if defined(SETTINGS_RESET) || (CONFIG_KEYPAD == IPOD_4G_PAD) || \
507 (CONFIG_KEYPAD == IRIVER_H10_PAD)
508 #ifdef SETTINGS_RESET
509 /* Reset settings if holding the reset button. (Rec on Archos,
510 A on Gigabeat) */
511 if ((button_status() & SETTINGS_RESET) == SETTINGS_RESET)
512 #else
513 /* Reset settings if the hold button is turned on */
514 if (button_hold())
515 #endif
517 splash(HZ*2, str(LANG_RESET_DONE_CLEAR));
518 settings_reset();
520 else
521 #endif
522 settings_load(SETTINGS_ALL);
524 if (init_dircache(true) < 0)
526 #ifdef HAVE_TAGCACHE
527 remove(TAGCACHE_STATEFILE);
528 #endif
531 gui_sync_wps_init();
532 settings_apply(true);
533 init_dircache(false);
534 #ifdef HAVE_TAGCACHE
535 init_tagcache();
536 #endif
538 #ifdef HAVE_EEPROM_SETTINGS
539 if (firmware_settings.initialized)
541 /* In case we crash. */
542 firmware_settings.disk_clean = false;
543 eeprom_settings_store();
545 #endif
546 status_init();
547 playlist_init();
548 tree_mem_init();
549 filetype_init();
550 scrobbler_init();
551 cuesheet_init();
553 #if CONFIG_CODEC != SWCODEC
554 /* No buffer allocation (see buffer.c) may take place after the call to
555 audio_init() since the mpeg thread takes the rest of the buffer space */
556 mp3_init( global_settings.volume,
557 global_settings.bass,
558 global_settings.treble,
559 global_settings.balance,
560 global_settings.loudness,
561 global_settings.avc,
562 global_settings.channel_config,
563 global_settings.stereo_width,
564 global_settings.mdb_strength,
565 global_settings.mdb_harmonics,
566 global_settings.mdb_center,
567 global_settings.mdb_shape,
568 global_settings.mdb_enable,
569 global_settings.superbass);
571 /* audio_init must to know the size of voice buffer so init voice first */
572 talk_init();
573 #endif /* CONFIG_CODEC != SWCODEC */
575 audio_init();
577 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
578 pcm_rec_init();
579 #endif
581 /* runtime database has to be initialized after audio_init() */
582 cpu_boost(false);
584 #ifdef AUTOROCK
586 static const char filename[] = PLUGIN_APPS_DIR "/autostart.rock";
588 if(file_exists(filename)) /* no complaint if it doesn't exist */
590 plugin_load((char*)filename, NULL); /* start if it does */
593 #endif /* #ifdef AUTOROCK */
595 #if CONFIG_CHARGING
596 car_adapter_mode_init();
597 #endif
598 #ifdef IPOD_ACCESSORY_PROTOCOL
599 iap_setup(global_settings.serial_bitrate);
600 #endif
601 #ifdef HAVE_ACCESSORY_SUPPLY
602 accessory_supply_set(global_settings.accessory_supply);
603 #endif
606 #ifdef CPU_PP
607 void cop_main(void)
609 /* This is the entry point for the coprocessor
610 Anyone not running an upgraded bootloader will never reach this point,
611 so it should not be assumed that the coprocessor be usable even on
612 platforms which support it.
614 A kernel thread is initially setup on the coprocessor and immediately
615 destroyed for purposes of continuity. The cop sits idle until at least
616 one thread exists on it. */
618 /* 3G doesn't have Rolo or dual core support yet */
619 #if NUM_CORES > 1
620 system_init();
621 kernel_init();
622 /* This should never be reached */
623 #endif
624 while(1) {
625 sleep_core(COP);
628 #endif /* CPU_PP */
630 int main(void)
632 app_main();
634 while(1) {
635 #if (CONFIG_LED == LED_REAL)
636 led(true); sleep(HZ/10);
637 led(false); sleep(HZ/10);
638 #endif
640 return 0;
642 #endif