Correct the BBX clause (no visual changes)
[kugel-rb/myfork.git] / apps / main.c
blob20cec9bbfaa9c17cda56aedc12baf10a66e185cd
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 #ifdef HAVE_TOUCHSCREEN
140 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
141 #endif
142 viewportmanager_set_statusbar(VP_SB_ALLSCREENS);
143 add_event(GUI_EVENT_STATUSBAR_TOGGLE, false,
144 viewportmanager_statusbar_changed);
145 #ifdef HAVE_USBSTACK
146 /* All threads should be created and public queues registered by now */
147 usb_start_monitoring();
148 #endif
149 root_menu();
152 static int init_dircache(bool preinit)
154 #ifdef HAVE_DIRCACHE
155 int result = 0;
156 bool clear = false;
158 if (preinit)
159 dircache_init();
161 if (!global_settings.dircache)
162 return 0;
164 # ifdef HAVE_EEPROM_SETTINGS
165 if (firmware_settings.initialized && firmware_settings.disk_clean
166 && preinit)
168 result = dircache_load();
170 if (result < 0)
172 firmware_settings.disk_clean = false;
173 if (global_status.dircache_size <= 0)
175 /* This will be in default language, settings are not
176 applied yet. Not really any easy way to fix that. */
177 splash(0, str(LANG_SCANNING_DISK));
178 clear = true;
181 dircache_build(global_status.dircache_size);
184 else
185 # endif
187 if (preinit)
188 return -1;
190 if (!dircache_is_enabled()
191 && !dircache_is_initializing())
193 if (global_status.dircache_size <= 0)
195 splash(0, str(LANG_SCANNING_DISK));
196 clear = true;
198 result = dircache_build(global_status.dircache_size);
201 if (result < 0)
203 /* Initialization of dircache failed. Manual action is
204 * necessary to enable dircache again.
206 splashf(0, "Dircache failed, disabled. Result: %d", result);
207 global_settings.dircache = false;
212 if (clear)
214 backlight_on();
215 show_logo();
216 global_status.dircache_size = dircache_get_cache_size();
217 status_save();
220 return result;
221 #else
222 (void)preinit;
223 return 0;
224 #endif
227 #ifdef HAVE_TAGCACHE
228 static void init_tagcache(void)
230 bool clear = false;
232 tagcache_init();
234 while (!tagcache_is_initialized())
236 #ifdef HAVE_LCD_CHARCELLS
237 char buf[32];
238 #endif
239 int ret = tagcache_get_commit_step();
241 if (ret > 0)
243 #if CONFIG_CODEC == SWCODEC
244 /* hwcodec can't use voice here, as the database commit
245 * uses the audio buffer. */
246 static long talked_tick = 0;
247 if(global_settings.talk_menu
248 && (talked_tick == 0
249 || TIME_AFTER(current_tick, talked_tick+7*HZ)))
251 talked_tick = current_tick;
252 talk_id(LANG_TAGCACHE_INIT, false);
253 talk_number(ret, true);
254 talk_id(VOICE_OF, true);
255 talk_number(tagcache_get_max_commit_step(), true);
257 #endif
258 #ifdef HAVE_LCD_BITMAP
259 splashf(0, "%s [%d/%d]",
260 str(LANG_TAGCACHE_INIT), ret,
261 tagcache_get_max_commit_step());
262 #else
263 lcd_double_height(false);
264 snprintf(buf, sizeof(buf), " DB [%d/%d]", ret,
265 tagcache_get_max_commit_step());
266 lcd_puts(0, 1, buf);
267 lcd_update();
268 #endif
269 clear = true;
271 sleep(HZ/4);
273 tagtree_init();
275 if (clear)
277 backlight_on();
278 show_logo();
281 #endif
283 #ifdef SIMULATOR
285 static void init(void)
287 kernel_init();
288 buffer_init();
289 enable_irq();
290 lcd_init();
291 #ifdef HAVE_REMOTE_LCD
292 lcd_remote_init();
293 #endif
294 font_init();
295 show_logo();
296 button_init();
297 backlight_init();
298 sim_tasks_init();
299 lang_init();
300 #ifdef DEBUG
301 debug_init();
302 #endif
303 /* Must be done before any code uses the multi-screen APi */
304 gui_syncstatusbar_init(&statusbars);
305 storage_init();
306 settings_reset();
307 settings_load(SETTINGS_ALL);
308 gui_sync_wps_init();
309 settings_apply(true);
310 init_dircache(true);
311 init_dircache(false);
312 #ifdef HAVE_TAGCACHE
313 init_tagcache();
314 #endif
315 sleep(HZ/2);
316 tree_mem_init();
317 filetype_init();
318 playlist_init();
320 #if CONFIG_CODEC != SWCODEC
321 mp3_init( global_settings.volume,
322 global_settings.bass,
323 global_settings.treble,
324 global_settings.balance,
325 global_settings.loudness,
326 global_settings.avc,
327 global_settings.channel_config,
328 global_settings.stereo_width,
329 global_settings.mdb_strength,
330 global_settings.mdb_harmonics,
331 global_settings.mdb_center,
332 global_settings.mdb_shape,
333 global_settings.mdb_enable,
334 global_settings.superbass);
336 /* audio_init must to know the size of voice buffer so init voice first */
337 talk_init();
338 #endif /* CONFIG_CODEC != SWCODEC */
340 scrobbler_init();
341 cuesheet_init();
343 audio_init();
344 button_clear_queue(); /* Empty the keyboard buffer */
347 #else
349 static void init(void)
351 int rc;
352 bool mounted = false;
353 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
354 /* if nobody initialized ATA before, I consider this a cold start */
355 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
356 #endif
358 system_init();
359 kernel_init();
361 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
362 set_cpu_frequency(CPUFREQ_NORMAL);
363 #ifdef CPU_COLDFIRE
364 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
365 #endif
366 cpu_boost(true);
367 #endif
369 buffer_init();
371 settings_reset();
373 i2c_init();
375 power_init();
377 enable_irq();
378 #ifdef CPU_ARM
379 enable_fiq();
380 #endif
381 lcd_init();
382 #ifdef HAVE_REMOTE_LCD
383 lcd_remote_init();
384 #endif
385 font_init();
387 show_logo();
388 lang_init();
390 #ifdef DEBUG
391 debug_init();
392 #else
393 #ifdef HAVE_SERIAL
394 serial_setup();
395 #endif
396 #endif
398 #if CONFIG_RTC
399 rtc_init();
400 #endif
401 #ifdef HAVE_RTC_RAM
402 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
403 #endif
405 adc_init();
407 usb_init();
408 #if CONFIG_USBOTG == USBOTG_ISP1362
409 isp1362_init();
410 #elif CONFIG_USBOTG == USBOTG_M5636
411 m5636_init();
412 #endif
414 backlight_init();
416 button_init();
418 powermgmt_init();
420 #if CONFIG_TUNER
421 radio_init();
422 #endif
424 /* Must be done before any code uses the multi-screen APi */
425 gui_syncstatusbar_init(&statusbars);
427 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
428 if (coldstart && charger_inserted()
429 && !global_settings.car_adapter_mode
430 #ifdef ATA_POWER_PLAYERSTYLE
431 && !ide_powered() /* relies on probing result from bootloader */
432 #endif
435 rc = charging_screen(); /* display a "charging" screen */
436 if (rc == 1) /* charger removed */
437 power_off();
438 /* "On" pressed or USB connected: proceed */
439 show_logo(); /* again, to provide better visual feedback */
441 #endif
443 rc = storage_init();
444 if(rc)
446 #ifdef HAVE_LCD_BITMAP
447 char str[32];
448 lcd_clear_display();
449 snprintf(str, 31, "ATA error: %d", rc);
450 lcd_puts(0, 1, str);
451 lcd_puts(0, 3, "Press ON to debug");
452 lcd_update();
453 while(!(button_get(true) & BUTTON_REL)); /*DO NOT CHANGE TO ACTION SYSTEM */
454 dbg_ports();
455 #endif
456 panicf("ata: %d", rc);
459 #ifdef HAVE_EEPROM_SETTINGS
460 eeprom_settings_init();
461 #endif
463 #ifndef HAVE_USBSTACK
464 usb_start_monitoring();
465 while (usb_detect() == USB_INSERTED)
467 #ifdef HAVE_EEPROM_SETTINGS
468 firmware_settings.disk_clean = false;
469 #endif
470 /* enter USB mode early, before trying to mount */
471 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
472 #if (CONFIG_STORAGE & STORAGE_MMC)
473 if (!mmc_touched() ||
474 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
475 #endif
477 usb_screen();
478 mounted = true; /* mounting done @ end of USB mode */
480 #ifdef HAVE_USB_POWER
481 if (usb_powered()) /* avoid deadlock */
482 break;
483 #endif
485 #endif
487 if (!mounted)
489 rc = disk_mount_all();
490 if (rc<=0)
492 lcd_clear_display();
493 lcd_puts(0, 0, "No partition");
494 lcd_puts(0, 1, "found.");
495 #ifdef HAVE_LCD_BITMAP
496 lcd_puts(0, 2, "Insert USB cable");
497 lcd_puts(0, 3, "and fix it.");
498 #endif
499 lcd_update();
501 while(button_get(true) != SYS_USB_CONNECTED) {};
502 usb_screen();
503 system_reboot();
507 #if defined(SETTINGS_RESET) || (CONFIG_KEYPAD == IPOD_4G_PAD) || \
508 (CONFIG_KEYPAD == IRIVER_H10_PAD)
509 #ifdef SETTINGS_RESET
510 /* Reset settings if holding the reset button. (Rec on Archos,
511 A on Gigabeat) */
512 if ((button_status() & SETTINGS_RESET) == SETTINGS_RESET)
513 #else
514 /* Reset settings if the hold button is turned on */
515 if (button_hold())
516 #endif
518 splash(HZ*2, str(LANG_RESET_DONE_CLEAR));
519 settings_reset();
521 else
522 #endif
523 settings_load(SETTINGS_ALL);
525 if (init_dircache(true) < 0)
527 #ifdef HAVE_TAGCACHE
528 remove(TAGCACHE_STATEFILE);
529 #endif
532 gui_sync_wps_init();
533 settings_apply(true);
534 init_dircache(false);
535 #ifdef HAVE_TAGCACHE
536 init_tagcache();
537 #endif
539 #ifdef HAVE_EEPROM_SETTINGS
540 if (firmware_settings.initialized)
542 /* In case we crash. */
543 firmware_settings.disk_clean = false;
544 eeprom_settings_store();
546 #endif
547 status_init();
548 playlist_init();
549 tree_mem_init();
550 filetype_init();
551 scrobbler_init();
552 cuesheet_init();
554 #if CONFIG_CODEC != SWCODEC
555 /* No buffer allocation (see buffer.c) may take place after the call to
556 audio_init() since the mpeg thread takes the rest of the buffer space */
557 mp3_init( global_settings.volume,
558 global_settings.bass,
559 global_settings.treble,
560 global_settings.balance,
561 global_settings.loudness,
562 global_settings.avc,
563 global_settings.channel_config,
564 global_settings.stereo_width,
565 global_settings.mdb_strength,
566 global_settings.mdb_harmonics,
567 global_settings.mdb_center,
568 global_settings.mdb_shape,
569 global_settings.mdb_enable,
570 global_settings.superbass);
572 /* audio_init must to know the size of voice buffer so init voice first */
573 talk_init();
574 #endif /* CONFIG_CODEC != SWCODEC */
576 audio_init();
578 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
579 pcm_rec_init();
580 #endif
582 /* runtime database has to be initialized after audio_init() */
583 cpu_boost(false);
585 #ifdef AUTOROCK
587 static const char filename[] = PLUGIN_APPS_DIR "/autostart.rock";
589 if(file_exists(filename)) /* no complaint if it doesn't exist */
591 plugin_load((char*)filename, NULL); /* start if it does */
594 #endif /* #ifdef AUTOROCK */
596 #if CONFIG_CHARGING
597 car_adapter_mode_init();
598 #endif
599 #ifdef IPOD_ACCESSORY_PROTOCOL
600 iap_setup(global_settings.serial_bitrate);
601 #endif
602 #ifdef HAVE_ACCESSORY_SUPPLY
603 accessory_supply_set(global_settings.accessory_supply);
604 #endif
607 #ifdef CPU_PP
608 void cop_main(void)
610 /* This is the entry point for the coprocessor
611 Anyone not running an upgraded bootloader will never reach this point,
612 so it should not be assumed that the coprocessor be usable even on
613 platforms which support it.
615 A kernel thread is initially setup on the coprocessor and immediately
616 destroyed for purposes of continuity. The cop sits idle until at least
617 one thread exists on it. */
619 /* 3G doesn't have Rolo or dual core support yet */
620 #if NUM_CORES > 1
621 system_init();
622 kernel_init();
623 /* This should never be reached */
624 #endif
625 while(1) {
626 sleep_core(COP);
629 #endif /* CPU_PP */
631 int main(void)
633 app_main();
635 while(1) {
636 #if (CONFIG_LED == LED_REAL)
637 led(true); sleep(HZ/10);
638 led(false); sleep(HZ/10);
639 #endif
641 return 0;
643 #endif