Use TIME_BEFORE() and redo a macro slightly.
[kugel-rb.git] / apps / main.c
bloba93231e0369f687ce2e552cb9ef4bdad63209f4c
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 #ifdef HAVE_LCD_BITMAP
139 list_init();
140 #endif
141 tree_gui_init();
142 viewportmanager_init();
143 #ifdef HAVE_USBSTACK
144 /* All threads should be created and public queues registered by now */
145 usb_start_monitoring();
146 #endif
148 #ifdef AUTOROCK
150 static const char filename[] = PLUGIN_APPS_DIR "/autostart.rock";
152 if(file_exists(filename)) /* no complaint if it doesn't exist */
154 plugin_load((char*)filename, NULL); /* start if it does */
157 #endif /* #ifdef AUTOROCK */
159 root_menu();
162 static int init_dircache(bool preinit)
164 #ifdef HAVE_DIRCACHE
165 int result = 0;
166 bool clear = false;
168 if (preinit)
169 dircache_init();
171 if (!global_settings.dircache)
172 return 0;
174 # ifdef HAVE_EEPROM_SETTINGS
175 if (firmware_settings.initialized && firmware_settings.disk_clean
176 && preinit)
178 result = dircache_load();
180 if (result < 0)
182 firmware_settings.disk_clean = false;
183 if (global_status.dircache_size <= 0)
185 /* This will be in default language, settings are not
186 applied yet. Not really any easy way to fix that. */
187 splash(0, str(LANG_SCANNING_DISK));
188 clear = true;
191 dircache_build(global_status.dircache_size);
194 else
195 # endif
197 if (preinit)
198 return -1;
200 if (!dircache_is_enabled()
201 && !dircache_is_initializing())
203 if (global_status.dircache_size <= 0)
205 splash(0, str(LANG_SCANNING_DISK));
206 clear = true;
208 result = dircache_build(global_status.dircache_size);
211 if (result < 0)
213 /* Initialization of dircache failed. Manual action is
214 * necessary to enable dircache again.
216 splashf(0, "Dircache failed, disabled. Result: %d", result);
217 global_settings.dircache = false;
221 if (clear)
223 backlight_on();
224 show_logo();
225 global_status.dircache_size = dircache_get_cache_size();
226 status_save();
229 return result;
230 #else
231 (void)preinit;
232 return 0;
233 #endif
236 #ifdef HAVE_TAGCACHE
237 static void init_tagcache(void)
239 bool clear = false;
241 tagcache_init();
243 while (!tagcache_is_initialized())
245 #ifdef HAVE_LCD_CHARCELLS
246 char buf[32];
247 #endif
248 int ret = tagcache_get_commit_step();
250 if (ret > 0)
252 #if CONFIG_CODEC == SWCODEC
253 /* hwcodec can't use voice here, as the database commit
254 * uses the audio buffer. */
255 static long talked_tick = 0;
256 if(global_settings.talk_menu
257 && (talked_tick == 0
258 || TIME_AFTER(current_tick, talked_tick+7*HZ)))
260 talked_tick = current_tick;
261 talk_id(LANG_TAGCACHE_INIT, false);
262 talk_number(ret, true);
263 talk_id(VOICE_OF, true);
264 talk_number(tagcache_get_max_commit_step(), true);
266 #endif
267 #ifdef HAVE_LCD_BITMAP
268 splashf(0, "%s [%d/%d]",
269 str(LANG_TAGCACHE_INIT), ret,
270 tagcache_get_max_commit_step());
271 #else
272 lcd_double_height(false);
273 snprintf(buf, sizeof(buf), " DB [%d/%d]", ret,
274 tagcache_get_max_commit_step());
275 lcd_puts(0, 1, buf);
276 lcd_update();
277 #endif
278 clear = true;
280 sleep(HZ/4);
282 tagtree_init();
284 if (clear)
286 backlight_on();
287 show_logo();
290 #endif
292 #ifdef SIMULATOR
294 static void init(void)
296 kernel_init();
297 buffer_init();
298 enable_irq();
299 lcd_init();
300 #ifdef HAVE_REMOTE_LCD
301 lcd_remote_init();
302 #endif
303 font_init();
304 show_logo();
305 button_init();
306 backlight_init();
307 sim_tasks_init();
308 lang_init();
309 #ifdef DEBUG
310 debug_init();
311 #endif
312 /* Must be done before any code uses the multi-screen API */
313 gui_syncstatusbar_init(&statusbars);
314 storage_init();
315 settings_reset();
316 settings_load(SETTINGS_ALL);
317 gui_sync_wps_init();
318 settings_apply(true);
319 init_dircache(true);
320 init_dircache(false);
321 #ifdef HAVE_TAGCACHE
322 init_tagcache();
323 #endif
324 sleep(HZ/2);
325 tree_mem_init();
326 filetype_init();
327 playlist_init();
329 #if CONFIG_CODEC != SWCODEC
330 mp3_init( global_settings.volume,
331 global_settings.bass,
332 global_settings.treble,
333 global_settings.balance,
334 global_settings.loudness,
335 global_settings.avc,
336 global_settings.channel_config,
337 global_settings.stereo_width,
338 global_settings.mdb_strength,
339 global_settings.mdb_harmonics,
340 global_settings.mdb_center,
341 global_settings.mdb_shape,
342 global_settings.mdb_enable,
343 global_settings.superbass);
345 /* audio_init must to know the size of voice buffer so init voice first */
346 talk_init();
347 #endif /* CONFIG_CODEC != SWCODEC */
349 scrobbler_init();
350 #if CONFIG_CODEC == SWCODEC
351 tdspeed_init();
352 #endif /* CONFIG_CODEC == SWCODEC */
354 audio_init();
355 button_clear_queue(); /* Empty the keyboard buffer */
358 #else
360 static void init(void)
362 int rc;
363 bool mounted = false;
364 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
365 /* if nobody initialized ATA before, I consider this a cold start */
366 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
367 #endif
369 system_init();
370 kernel_init();
372 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
373 set_cpu_frequency(CPUFREQ_NORMAL);
374 #ifdef CPU_COLDFIRE
375 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
376 #endif
377 cpu_boost(true);
378 #endif
380 buffer_init();
382 settings_reset();
384 i2c_init();
386 power_init();
388 enable_irq();
389 #ifdef CPU_ARM
390 enable_fiq();
391 #endif
392 lcd_init();
393 #ifdef HAVE_REMOTE_LCD
394 lcd_remote_init();
395 #endif
396 font_init();
398 show_logo();
399 lang_init();
401 #ifdef DEBUG
402 debug_init();
403 #else
404 #ifdef HAVE_SERIAL
405 serial_setup();
406 #endif
407 #endif
409 #if CONFIG_RTC
410 rtc_init();
411 #endif
412 #ifdef HAVE_RTC_RAM
413 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
414 #endif
416 adc_init();
418 usb_init();
419 #if CONFIG_USBOTG == USBOTG_ISP1362
420 isp1362_init();
421 #elif CONFIG_USBOTG == USBOTG_M5636
422 m5636_init();
423 #endif
425 backlight_init();
427 button_init();
429 powermgmt_init();
431 #if CONFIG_TUNER
432 radio_init();
433 #endif
435 /* Must be done before any code uses the multi-screen API */
436 gui_syncstatusbar_init(&statusbars);
438 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
439 /* charger_inserted() can't be used here because power_thread()
440 hasn't checked power_input_status() yet */
441 if (coldstart && (power_input_status() & POWER_INPUT_MAIN_CHARGER)
442 && !global_settings.car_adapter_mode
443 #ifdef ATA_POWER_PLAYERSTYLE
444 && !ide_powered() /* relies on probing result from bootloader */
445 #endif
448 rc = charging_screen(); /* display a "charging" screen */
449 if (rc == 1) /* charger removed */
450 power_off();
451 /* "On" pressed or USB connected: proceed */
452 show_logo(); /* again, to provide better visual feedback */
454 #endif
456 rc = storage_init();
457 if(rc)
459 #ifdef HAVE_LCD_BITMAP
460 char str[32];
461 lcd_clear_display();
462 snprintf(str, 31, "ATA error: %d", rc);
463 lcd_puts(0, 1, str);
464 lcd_puts(0, 3, "Press ON to debug");
465 lcd_update();
466 while(!(button_get(true) & BUTTON_REL)); /*DO NOT CHANGE TO ACTION SYSTEM */
467 dbg_ports();
468 #endif
469 panicf("ata: %d", rc);
472 #ifdef HAVE_EEPROM_SETTINGS
473 eeprom_settings_init();
474 #endif
476 #ifndef HAVE_USBSTACK
477 usb_start_monitoring();
478 while (usb_detect() == USB_INSERTED)
480 #ifdef HAVE_EEPROM_SETTINGS
481 firmware_settings.disk_clean = false;
482 #endif
483 /* enter USB mode early, before trying to mount */
484 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
485 #if (CONFIG_STORAGE & STORAGE_MMC)
486 if (!mmc_touched() ||
487 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
488 #endif
490 usb_screen();
491 mounted = true; /* mounting done @ end of USB mode */
493 #ifdef HAVE_USB_POWER
494 if (usb_powered()) /* avoid deadlock */
495 break;
496 #endif
498 #endif
500 if (!mounted)
502 rc = disk_mount_all();
503 if (rc<=0)
505 lcd_clear_display();
506 lcd_puts(0, 0, "No partition");
507 lcd_puts(0, 1, "found.");
508 #ifdef HAVE_LCD_BITMAP
509 lcd_puts(0, 2, "Insert USB cable");
510 lcd_puts(0, 3, "and fix it.");
511 #endif
512 lcd_update();
514 while(button_get(true) != SYS_USB_CONNECTED) {};
515 usb_screen();
516 system_reboot();
520 #if defined(SETTINGS_RESET) || (CONFIG_KEYPAD == IPOD_4G_PAD) || \
521 (CONFIG_KEYPAD == IRIVER_H10_PAD)
522 #ifdef SETTINGS_RESET
523 /* Reset settings if holding the reset button. (Rec on Archos,
524 A on Gigabeat) */
525 if ((button_status() & SETTINGS_RESET) == SETTINGS_RESET)
526 #else
527 /* Reset settings if the hold button is turned on */
528 if (button_hold())
529 #endif
531 splash(HZ*2, str(LANG_RESET_DONE_CLEAR));
532 settings_reset();
534 else
535 #endif
536 settings_load(SETTINGS_ALL);
538 if (init_dircache(true) < 0)
540 #ifdef HAVE_TAGCACHE
541 remove(TAGCACHE_STATEFILE);
542 #endif
545 gui_sync_wps_init();
546 settings_apply(true);
547 init_dircache(false);
548 #ifdef HAVE_TAGCACHE
549 init_tagcache();
550 #endif
552 #ifdef HAVE_EEPROM_SETTINGS
553 if (firmware_settings.initialized)
555 /* In case we crash. */
556 firmware_settings.disk_clean = false;
557 eeprom_settings_store();
559 #endif
560 status_init();
561 playlist_init();
562 tree_mem_init();
563 filetype_init();
564 scrobbler_init();
565 #if CONFIG_CODEC == SWCODEC
566 tdspeed_init();
567 #endif /* CONFIG_CODEC == SWCODEC */
569 #if CONFIG_CODEC != SWCODEC
570 /* No buffer allocation (see buffer.c) may take place after the call to
571 audio_init() since the mpeg thread takes the rest of the buffer space */
572 mp3_init( global_settings.volume,
573 global_settings.bass,
574 global_settings.treble,
575 global_settings.balance,
576 global_settings.loudness,
577 global_settings.avc,
578 global_settings.channel_config,
579 global_settings.stereo_width,
580 global_settings.mdb_strength,
581 global_settings.mdb_harmonics,
582 global_settings.mdb_center,
583 global_settings.mdb_shape,
584 global_settings.mdb_enable,
585 global_settings.superbass);
587 /* audio_init must to know the size of voice buffer so init voice first */
588 talk_init();
589 #endif /* CONFIG_CODEC != SWCODEC */
591 audio_init();
593 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
594 pcm_rec_init();
595 #endif
597 /* runtime database has to be initialized after audio_init() */
598 cpu_boost(false);
600 #if CONFIG_CHARGING
601 car_adapter_mode_init();
602 #endif
603 #ifdef IPOD_ACCESSORY_PROTOCOL
604 iap_setup(global_settings.serial_bitrate);
605 #endif
606 #ifdef HAVE_ACCESSORY_SUPPLY
607 accessory_supply_set(global_settings.accessory_supply);
608 #endif
611 #ifdef CPU_PP
612 void cop_main(void)
614 /* This is the entry point for the coprocessor
615 Anyone not running an upgraded bootloader will never reach this point,
616 so it should not be assumed that the coprocessor be usable even on
617 platforms which support it.
619 A kernel thread is initially setup on the coprocessor and immediately
620 destroyed for purposes of continuity. The cop sits idle until at least
621 one thread exists on it. */
623 /* 3G doesn't have Rolo or dual core support yet */
624 #if NUM_CORES > 1
625 system_init();
626 kernel_init();
627 /* This should never be reached */
628 #endif
629 while(1) {
630 sleep_core(COP);
633 #endif /* CPU_PP */
635 int main(void)
637 app_main();
639 while(1) {
640 #if (CONFIG_LED == LED_REAL)
641 led(true); sleep(HZ/10);
642 led(false); sleep(HZ/10);
643 #endif
645 return 0;
647 #endif