fix FS#10550. Start autostart.rock after gui_syncstatusbar_init().
[kugel-rb.git] / apps / main.c
blob6aa16e5cb6cddb7ea4c5f766edd1f56b8cf34420
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 "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"
77 #ifdef IPOD_ACCESSORY_PROTOCOL
78 #include "iap.h"
79 #endif
81 #if (CONFIG_CODEC == SWCODEC)
82 #include "playback.h"
83 #include "tdspeed.h"
84 #endif
85 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
86 #include "pcm_record.h"
87 #endif
89 #ifdef BUTTON_REC
90 #define SETTINGS_RESET BUTTON_REC
91 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
92 #define SETTINGS_RESET BUTTON_A
93 #endif
95 #if CONFIG_TUNER
96 #include "radio.h"
97 #endif
98 #if (CONFIG_STORAGE & STORAGE_MMC)
99 #include "ata_mmc.h"
100 #endif
102 #ifdef HAVE_REMOTE_LCD
103 #include "lcd-remote.h"
104 #endif
106 #if CONFIG_USBOTG == USBOTG_ISP1362
107 #include "isp1362.h"
108 #endif
110 #if CONFIG_USBOTG == USBOTG_M5636
111 #include "m5636.h"
112 #endif
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 gui_syncstatusbar_init(&statusbars);
140 viewportmanager_init();
141 #ifdef HAVE_USBSTACK
142 /* All threads should be created and public queues registered by now */
143 usb_start_monitoring();
144 #endif
146 #ifdef AUTOROCK
148 static const char filename[] = PLUGIN_APPS_DIR "/autostart.rock";
150 if(file_exists(filename)) /* no complaint if it doesn't exist */
152 plugin_load((char*)filename, NULL); /* start if it does */
155 #endif /* #ifdef AUTOROCK */
157 root_menu();
160 static int init_dircache(bool preinit)
162 #ifdef HAVE_DIRCACHE
163 int result = 0;
164 bool clear = false;
166 if (preinit)
167 dircache_init();
169 if (!global_settings.dircache)
170 return 0;
172 # ifdef HAVE_EEPROM_SETTINGS
173 if (firmware_settings.initialized && firmware_settings.disk_clean
174 && preinit)
176 result = dircache_load();
178 if (result < 0)
180 firmware_settings.disk_clean = false;
181 if (global_status.dircache_size <= 0)
183 /* This will be in default language, settings are not
184 applied yet. Not really any easy way to fix that. */
185 splash(0, str(LANG_SCANNING_DISK));
186 clear = true;
189 dircache_build(global_status.dircache_size);
192 else
193 # endif
195 if (preinit)
196 return -1;
198 if (!dircache_is_enabled()
199 && !dircache_is_initializing())
201 if (global_status.dircache_size <= 0)
203 splash(0, str(LANG_SCANNING_DISK));
204 clear = true;
206 result = dircache_build(global_status.dircache_size);
209 if (result < 0)
211 /* Initialization of dircache failed. Manual action is
212 * necessary to enable dircache again.
214 splashf(0, "Dircache failed, disabled. Result: %d", result);
215 global_settings.dircache = false;
219 if (clear)
221 backlight_on();
222 show_logo();
223 global_status.dircache_size = dircache_get_cache_size();
224 status_save();
227 return result;
228 #else
229 (void)preinit;
230 return 0;
231 #endif
234 #ifdef HAVE_TAGCACHE
235 static void init_tagcache(void)
237 bool clear = false;
239 tagcache_init();
241 while (!tagcache_is_initialized())
243 #ifdef HAVE_LCD_CHARCELLS
244 char buf[32];
245 #endif
246 int ret = tagcache_get_commit_step();
248 if (ret > 0)
250 #if CONFIG_CODEC == SWCODEC
251 /* hwcodec can't use voice here, as the database commit
252 * uses the audio buffer. */
253 static long talked_tick = 0;
254 if(global_settings.talk_menu
255 && (talked_tick == 0
256 || TIME_AFTER(current_tick, talked_tick+7*HZ)))
258 talked_tick = current_tick;
259 talk_id(LANG_TAGCACHE_INIT, false);
260 talk_number(ret, true);
261 talk_id(VOICE_OF, true);
262 talk_number(tagcache_get_max_commit_step(), true);
264 #endif
265 #ifdef HAVE_LCD_BITMAP
266 splashf(0, "%s [%d/%d]",
267 str(LANG_TAGCACHE_INIT), ret,
268 tagcache_get_max_commit_step());
269 #else
270 lcd_double_height(false);
271 snprintf(buf, sizeof(buf), " DB [%d/%d]", ret,
272 tagcache_get_max_commit_step());
273 lcd_puts(0, 1, buf);
274 lcd_update();
275 #endif
276 clear = true;
278 sleep(HZ/4);
280 tagtree_init();
282 if (clear)
284 backlight_on();
285 show_logo();
288 #endif
290 #ifdef SIMULATOR
292 static void init(void)
294 kernel_init();
295 buffer_init();
296 enable_irq();
297 lcd_init();
298 #ifdef HAVE_REMOTE_LCD
299 lcd_remote_init();
300 #endif
301 font_init();
302 show_logo();
303 button_init();
304 backlight_init();
305 sim_tasks_init();
306 lang_init();
307 #ifdef DEBUG
308 debug_init();
309 #endif
310 storage_init();
311 settings_reset();
312 settings_load(SETTINGS_ALL);
313 gui_sync_wps_init();
314 settings_apply(true);
315 init_dircache(true);
316 init_dircache(false);
317 #ifdef HAVE_TAGCACHE
318 init_tagcache();
319 #endif
320 sleep(HZ/2);
321 tree_mem_init();
322 filetype_init();
323 playlist_init();
325 #if CONFIG_CODEC != SWCODEC
326 mp3_init( global_settings.volume,
327 global_settings.bass,
328 global_settings.treble,
329 global_settings.balance,
330 global_settings.loudness,
331 global_settings.avc,
332 global_settings.channel_config,
333 global_settings.stereo_width,
334 global_settings.mdb_strength,
335 global_settings.mdb_harmonics,
336 global_settings.mdb_center,
337 global_settings.mdb_shape,
338 global_settings.mdb_enable,
339 global_settings.superbass);
341 /* audio_init must to know the size of voice buffer so init voice first */
342 talk_init();
343 #endif /* CONFIG_CODEC != SWCODEC */
345 scrobbler_init();
346 #if CONFIG_CODEC == SWCODEC
347 tdspeed_init();
348 #endif /* CONFIG_CODEC == SWCODEC */
350 audio_init();
351 button_clear_queue(); /* Empty the keyboard buffer */
354 #else
356 static void init(void)
358 int rc;
359 bool mounted = false;
360 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
361 /* if nobody initialized ATA before, I consider this a cold start */
362 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
363 #endif
365 system_init();
366 kernel_init();
368 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
369 set_cpu_frequency(CPUFREQ_NORMAL);
370 #ifdef CPU_COLDFIRE
371 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
372 #endif
373 cpu_boost(true);
374 #endif
376 buffer_init();
378 settings_reset();
380 i2c_init();
382 power_init();
384 enable_irq();
385 #ifdef CPU_ARM
386 enable_fiq();
387 #endif
388 lcd_init();
389 #ifdef HAVE_REMOTE_LCD
390 lcd_remote_init();
391 #endif
392 font_init();
394 show_logo();
395 lang_init();
397 #ifdef DEBUG
398 debug_init();
399 #else
400 #ifdef HAVE_SERIAL
401 serial_setup();
402 #endif
403 #endif
405 #if CONFIG_RTC
406 rtc_init();
407 #endif
408 #ifdef HAVE_RTC_RAM
409 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
410 #endif
412 adc_init();
414 usb_init();
415 #if CONFIG_USBOTG == USBOTG_ISP1362
416 isp1362_init();
417 #elif CONFIG_USBOTG == USBOTG_M5636
418 m5636_init();
419 #endif
421 backlight_init();
423 button_init();
425 powermgmt_init();
427 #if CONFIG_TUNER
428 radio_init();
429 #endif
431 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
432 /* charger_inserted() can't be used here because power_thread()
433 hasn't checked power_input_status() yet */
434 if (coldstart && (power_input_status() & POWER_INPUT_MAIN_CHARGER)
435 && !global_settings.car_adapter_mode
436 #ifdef ATA_POWER_PLAYERSTYLE
437 && !ide_powered() /* relies on probing result from bootloader */
438 #endif
441 rc = charging_screen(); /* display a "charging" screen */
442 if (rc == 1) /* charger removed */
443 power_off();
444 /* "On" pressed or USB connected: proceed */
445 show_logo(); /* again, to provide better visual feedback */
447 #endif
449 rc = storage_init();
450 if(rc)
452 #ifdef HAVE_LCD_BITMAP
453 char str[32];
454 lcd_clear_display();
455 snprintf(str, 31, "ATA error: %d", rc);
456 lcd_puts(0, 1, str);
457 lcd_puts(0, 3, "Press ON to debug");
458 lcd_update();
459 while(!(button_get(true) & BUTTON_REL)); /*DO NOT CHANGE TO ACTION SYSTEM */
460 dbg_ports();
461 #endif
462 panicf("ata: %d", rc);
465 #ifdef HAVE_EEPROM_SETTINGS
466 eeprom_settings_init();
467 #endif
469 #ifndef HAVE_USBSTACK
470 usb_start_monitoring();
471 while (usb_detect() == USB_INSERTED)
473 #ifdef HAVE_EEPROM_SETTINGS
474 firmware_settings.disk_clean = false;
475 #endif
476 /* enter USB mode early, before trying to mount */
477 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
478 #if (CONFIG_STORAGE & STORAGE_MMC)
479 if (!mmc_touched() ||
480 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
481 #endif
483 usb_screen();
484 mounted = true; /* mounting done @ end of USB mode */
486 #ifdef HAVE_USB_POWER
487 if (usb_powered()) /* avoid deadlock */
488 break;
489 #endif
491 #endif
493 if (!mounted)
495 rc = disk_mount_all();
496 if (rc<=0)
498 lcd_clear_display();
499 lcd_puts(0, 0, "No partition");
500 lcd_puts(0, 1, "found.");
501 #ifdef HAVE_LCD_BITMAP
502 lcd_puts(0, 2, "Insert USB cable");
503 lcd_puts(0, 3, "and fix it.");
504 #endif
505 lcd_update();
507 while(button_get(true) != SYS_USB_CONNECTED) {};
508 usb_screen();
509 system_reboot();
513 #if defined(SETTINGS_RESET) || (CONFIG_KEYPAD == IPOD_4G_PAD) || \
514 (CONFIG_KEYPAD == IRIVER_H10_PAD)
515 #ifdef SETTINGS_RESET
516 /* Reset settings if holding the reset button. (Rec on Archos,
517 A on Gigabeat) */
518 if ((button_status() & SETTINGS_RESET) == SETTINGS_RESET)
519 #else
520 /* Reset settings if the hold button is turned on */
521 if (button_hold())
522 #endif
524 splash(HZ*2, str(LANG_RESET_DONE_CLEAR));
525 settings_reset();
527 else
528 #endif
529 settings_load(SETTINGS_ALL);
531 if (init_dircache(true) < 0)
533 #ifdef HAVE_TAGCACHE
534 remove(TAGCACHE_STATEFILE);
535 #endif
538 gui_sync_wps_init();
539 settings_apply(true);
540 init_dircache(false);
541 #ifdef HAVE_TAGCACHE
542 init_tagcache();
543 #endif
545 #ifdef HAVE_EEPROM_SETTINGS
546 if (firmware_settings.initialized)
548 /* In case we crash. */
549 firmware_settings.disk_clean = false;
550 eeprom_settings_store();
552 #endif
553 status_init();
554 playlist_init();
555 tree_mem_init();
556 filetype_init();
557 scrobbler_init();
558 #if CONFIG_CODEC == SWCODEC
559 tdspeed_init();
560 #endif /* CONFIG_CODEC == SWCODEC */
562 #if CONFIG_CODEC != SWCODEC
563 /* No buffer allocation (see buffer.c) may take place after the call to
564 audio_init() since the mpeg thread takes the rest of the buffer space */
565 mp3_init( global_settings.volume,
566 global_settings.bass,
567 global_settings.treble,
568 global_settings.balance,
569 global_settings.loudness,
570 global_settings.avc,
571 global_settings.channel_config,
572 global_settings.stereo_width,
573 global_settings.mdb_strength,
574 global_settings.mdb_harmonics,
575 global_settings.mdb_center,
576 global_settings.mdb_shape,
577 global_settings.mdb_enable,
578 global_settings.superbass);
580 /* audio_init must to know the size of voice buffer so init voice first */
581 talk_init();
582 #endif /* CONFIG_CODEC != SWCODEC */
584 audio_init();
586 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
587 pcm_rec_init();
588 #endif
590 /* runtime database has to be initialized after audio_init() */
591 cpu_boost(false);
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