Fix FS#10486 - "fuze screen not drawn correctly on backlight turn-on if playlist...
[kugel-rb/myfork.git] / apps / main.c
blobd0fba3fcce227e499c2b5913a5d27db001bc26b0
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"
76 #ifdef IPOD_ACCESSORY_PROTOCOL
77 #include "iap.h"
78 #endif
80 #if (CONFIG_CODEC == SWCODEC)
81 #include "playback.h"
82 #include "tdspeed.h"
83 #endif
84 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
85 #include "pcm_record.h"
86 #endif
88 #ifdef BUTTON_REC
89 #define SETTINGS_RESET BUTTON_REC
90 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
91 #define SETTINGS_RESET BUTTON_A
92 #endif
94 #if CONFIG_TUNER
95 #include "radio.h"
96 #endif
97 #if (CONFIG_STORAGE & STORAGE_MMC)
98 #include "ata_mmc.h"
99 #endif
101 #ifdef HAVE_REMOTE_LCD
102 #include "lcd-remote.h"
103 #endif
105 #if CONFIG_USBOTG == USBOTG_ISP1362
106 #include "isp1362.h"
107 #endif
109 #if CONFIG_USBOTG == USBOTG_M5636
110 #include "m5636.h"
111 #endif
113 #ifdef SIMULATOR
114 #include "sim_tasks.h"
115 #include "system-sdl.h"
116 #endif
118 /*#define AUTOROCK*/ /* define this to check for "autostart.rock" on boot */
120 const char appsversion[]=APPSVERSION;
122 static void init(void);
124 #ifdef SIMULATOR
125 void app_main(void)
126 #else
127 static void app_main(void)
128 #endif
130 int i;
131 init();
132 FOR_NB_SCREENS(i)
134 screens[i].clear_display();
135 screens[i].update();
137 tree_gui_init();
138 viewportmanager_set_statusbar(VP_SB_ALLSCREENS);
139 add_event(GUI_EVENT_STATUSBAR_TOGGLE, false,
140 viewportmanager_statusbar_changed);
141 #ifdef HAVE_USBSTACK
142 /* All threads should be created and public queues registered by now */
143 usb_start_monitoring();
144 #endif
145 root_menu();
148 static int init_dircache(bool preinit)
150 #ifdef HAVE_DIRCACHE
151 int result = 0;
152 bool clear = false;
154 if (preinit)
155 dircache_init();
157 if (!global_settings.dircache)
158 return 0;
160 # ifdef HAVE_EEPROM_SETTINGS
161 if (firmware_settings.initialized && firmware_settings.disk_clean
162 && preinit)
164 result = dircache_load();
166 if (result < 0)
168 firmware_settings.disk_clean = false;
169 if (global_status.dircache_size <= 0)
171 /* This will be in default language, settings are not
172 applied yet. Not really any easy way to fix that. */
173 splash(0, str(LANG_SCANNING_DISK));
174 clear = true;
177 dircache_build(global_status.dircache_size);
180 else
181 # endif
183 if (preinit)
184 return -1;
186 if (!dircache_is_enabled()
187 && !dircache_is_initializing())
189 if (global_status.dircache_size <= 0)
191 splash(0, str(LANG_SCANNING_DISK));
192 clear = true;
194 result = dircache_build(global_status.dircache_size);
197 if (result < 0)
199 /* Initialization of dircache failed. Manual action is
200 * necessary to enable dircache again.
202 splashf(0, "Dircache failed, disabled. Result: %d", result);
203 global_settings.dircache = false;
208 if (clear)
210 backlight_on();
211 show_logo();
212 global_status.dircache_size = dircache_get_cache_size();
213 status_save();
216 return result;
217 #else
218 (void)preinit;
219 return 0;
220 #endif
223 #ifdef HAVE_TAGCACHE
224 static void init_tagcache(void)
226 bool clear = false;
228 tagcache_init();
230 while (!tagcache_is_initialized())
232 #ifdef HAVE_LCD_CHARCELLS
233 char buf[32];
234 #endif
235 int ret = tagcache_get_commit_step();
237 if (ret > 0)
239 #if CONFIG_CODEC == SWCODEC
240 /* hwcodec can't use voice here, as the database commit
241 * uses the audio buffer. */
242 static long talked_tick = 0;
243 if(global_settings.talk_menu
244 && (talked_tick == 0
245 || TIME_AFTER(current_tick, talked_tick+7*HZ)))
247 talked_tick = current_tick;
248 talk_id(LANG_TAGCACHE_INIT, false);
249 talk_number(ret, true);
250 talk_id(VOICE_OF, true);
251 talk_number(tagcache_get_max_commit_step(), true);
253 #endif
254 #ifdef HAVE_LCD_BITMAP
255 splashf(0, "%s [%d/%d]",
256 str(LANG_TAGCACHE_INIT), ret,
257 tagcache_get_max_commit_step());
258 #else
259 lcd_double_height(false);
260 snprintf(buf, sizeof(buf), " DB [%d/%d]", ret,
261 tagcache_get_max_commit_step());
262 lcd_puts(0, 1, buf);
263 lcd_update();
264 #endif
265 clear = true;
267 sleep(HZ/4);
269 tagtree_init();
271 if (clear)
273 backlight_on();
274 show_logo();
277 #endif
279 #ifdef SIMULATOR
281 static void init(void)
283 kernel_init();
284 buffer_init();
285 enable_irq();
286 lcd_init();
287 #ifdef HAVE_REMOTE_LCD
288 lcd_remote_init();
289 #endif
290 font_init();
291 show_logo();
292 button_init();
293 backlight_init();
294 sim_tasks_init();
295 lang_init();
296 #ifdef DEBUG
297 debug_init();
298 #endif
299 /* Must be done before any code uses the multi-screen APi */
300 gui_syncstatusbar_init(&statusbars);
301 storage_init();
302 settings_reset();
303 settings_load(SETTINGS_ALL);
304 gui_sync_wps_init();
305 settings_apply(true);
306 init_dircache(true);
307 init_dircache(false);
308 #ifdef HAVE_TAGCACHE
309 init_tagcache();
310 #endif
311 sleep(HZ/2);
312 tree_mem_init();
313 filetype_init();
314 playlist_init();
316 #if CONFIG_CODEC != SWCODEC
317 mp3_init( global_settings.volume,
318 global_settings.bass,
319 global_settings.treble,
320 global_settings.balance,
321 global_settings.loudness,
322 global_settings.avc,
323 global_settings.channel_config,
324 global_settings.stereo_width,
325 global_settings.mdb_strength,
326 global_settings.mdb_harmonics,
327 global_settings.mdb_center,
328 global_settings.mdb_shape,
329 global_settings.mdb_enable,
330 global_settings.superbass);
332 /* audio_init must to know the size of voice buffer so init voice first */
333 talk_init();
334 #endif /* CONFIG_CODEC != SWCODEC */
336 scrobbler_init();
337 #if CONFIG_CODEC == SWCODEC
338 tdspeed_init();
339 #endif /* CONFIG_CODEC == SWCODEC */
341 audio_init();
342 button_clear_queue(); /* Empty the keyboard buffer */
345 #else
347 static void init(void)
349 int rc;
350 bool mounted = false;
351 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
352 /* if nobody initialized ATA before, I consider this a cold start */
353 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
354 #endif
356 system_init();
357 kernel_init();
359 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
360 set_cpu_frequency(CPUFREQ_NORMAL);
361 #ifdef CPU_COLDFIRE
362 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
363 #endif
364 cpu_boost(true);
365 #endif
367 buffer_init();
369 settings_reset();
371 i2c_init();
373 power_init();
375 enable_irq();
376 #ifdef CPU_ARM
377 enable_fiq();
378 #endif
379 lcd_init();
380 #ifdef HAVE_REMOTE_LCD
381 lcd_remote_init();
382 #endif
383 font_init();
385 show_logo();
386 lang_init();
388 #ifdef DEBUG
389 debug_init();
390 #else
391 #ifdef HAVE_SERIAL
392 serial_setup();
393 #endif
394 #endif
396 #if CONFIG_RTC
397 rtc_init();
398 #endif
399 #ifdef HAVE_RTC_RAM
400 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
401 #endif
403 adc_init();
405 usb_init();
406 #if CONFIG_USBOTG == USBOTG_ISP1362
407 isp1362_init();
408 #elif CONFIG_USBOTG == USBOTG_M5636
409 m5636_init();
410 #endif
412 backlight_init();
414 button_init();
416 powermgmt_init();
418 #if CONFIG_TUNER
419 radio_init();
420 #endif
422 /* Must be done before any code uses the multi-screen APi */
423 gui_syncstatusbar_init(&statusbars);
425 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
426 /* charger_inserted() can't be used here because power_thread()
427 hasn't checked power_input_status() yet */
428 if (coldstart && (power_input_status() & POWER_INPUT_MAIN_CHARGER)
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 #if CONFIG_CODEC == SWCODEC
553 tdspeed_init();
554 #endif /* CONFIG_CODEC == SWCODEC */
556 #if CONFIG_CODEC != SWCODEC
557 /* No buffer allocation (see buffer.c) may take place after the call to
558 audio_init() since the mpeg thread takes the rest of the buffer space */
559 mp3_init( global_settings.volume,
560 global_settings.bass,
561 global_settings.treble,
562 global_settings.balance,
563 global_settings.loudness,
564 global_settings.avc,
565 global_settings.channel_config,
566 global_settings.stereo_width,
567 global_settings.mdb_strength,
568 global_settings.mdb_harmonics,
569 global_settings.mdb_center,
570 global_settings.mdb_shape,
571 global_settings.mdb_enable,
572 global_settings.superbass);
574 /* audio_init must to know the size of voice buffer so init voice first */
575 talk_init();
576 #endif /* CONFIG_CODEC != SWCODEC */
578 audio_init();
580 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
581 pcm_rec_init();
582 #endif
584 /* runtime database has to be initialized after audio_init() */
585 cpu_boost(false);
587 #ifdef AUTOROCK
589 static const char filename[] = PLUGIN_APPS_DIR "/autostart.rock";
591 if(file_exists(filename)) /* no complaint if it doesn't exist */
593 plugin_load((char*)filename, NULL); /* start if it does */
596 #endif /* #ifdef AUTOROCK */
598 #if CONFIG_CHARGING
599 car_adapter_mode_init();
600 #endif
601 #ifdef IPOD_ACCESSORY_PROTOCOL
602 iap_setup(global_settings.serial_bitrate);
603 #endif
604 #ifdef HAVE_ACCESSORY_SUPPLY
605 accessory_supply_set(global_settings.accessory_supply);
606 #endif
609 #ifdef CPU_PP
610 void cop_main(void)
612 /* This is the entry point for the coprocessor
613 Anyone not running an upgraded bootloader will never reach this point,
614 so it should not be assumed that the coprocessor be usable even on
615 platforms which support it.
617 A kernel thread is initially setup on the coprocessor and immediately
618 destroyed for purposes of continuity. The cop sits idle until at least
619 one thread exists on it. */
621 /* 3G doesn't have Rolo or dual core support yet */
622 #if NUM_CORES > 1
623 system_init();
624 kernel_init();
625 /* This should never be reached */
626 #endif
627 while(1) {
628 sleep_core(COP);
631 #endif /* CPU_PP */
633 int main(void)
635 app_main();
637 while(1) {
638 #if (CONFIG_LED == LED_REAL)
639 led(true); sleep(HZ/10);
640 led(false); sleep(HZ/10);
641 #endif
643 return 0;
645 #endif