static/const/#include/tab police on various files
[kugel-rb.git] / apps / main.c
blobd473e6673174cf421700335c5fcc7b1142535f06
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 "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 static void fourhertz_tick_task(void)
126 static long last_fire = 0;
127 if (TIME_AFTER(current_tick, last_fire+HZ/4))
129 queue_post(&button_queue, SYS_FOURHERTZ, 0);
130 last_fire = current_tick;
134 #ifdef SIMULATOR
135 void app_main(void)
136 #else
137 static void app_main(void)
138 #endif
140 int i;
141 init();
142 FOR_NB_SCREENS(i)
144 screens[i].clear_display();
145 screens[i].update();
147 tree_gui_init();
148 #ifdef HAVE_TOUCHSCREEN
149 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
150 #endif
151 tick_add_task(fourhertz_tick_task);
152 viewportmanager_set_statusbar(true);
153 add_event(GUI_EVENT_STATUSBAR_TOGGLE, false,
154 viewportmanager_statusbar_changed);
155 root_menu();
158 static int init_dircache(bool preinit)
160 #ifdef HAVE_DIRCACHE
161 int result = 0;
162 bool clear = false;
164 if (preinit)
165 dircache_init();
167 if (!global_settings.dircache)
168 return 0;
170 # ifdef HAVE_EEPROM_SETTINGS
171 if (firmware_settings.initialized && firmware_settings.disk_clean
172 && preinit)
174 result = dircache_load();
176 if (result < 0)
178 firmware_settings.disk_clean = false;
179 if (global_status.dircache_size <= 0)
181 /* This will be in default language, settings are not
182 applied yet. Not really any easy way to fix that. */
183 splash(0, str(LANG_SCANNING_DISK));
184 clear = true;
187 dircache_build(global_status.dircache_size);
190 else
191 # endif
193 if (preinit)
194 return -1;
196 if (!dircache_is_enabled()
197 && !dircache_is_initializing())
199 if (global_status.dircache_size <= 0)
201 splash(0, str(LANG_SCANNING_DISK));
202 clear = true;
204 result = dircache_build(global_status.dircache_size);
207 if (result < 0)
209 /* Initialization of dircache failed. Manual action is
210 * necessary to enable dircache again.
212 splashf(0, "Dircache failed, disabled. Result: %d", result);
213 global_settings.dircache = false;
218 if (clear)
220 backlight_on();
221 show_logo();
222 global_status.dircache_size = dircache_get_cache_size();
223 status_save();
226 return result;
227 #else
228 (void)preinit;
229 return 0;
230 #endif
233 #ifdef HAVE_TAGCACHE
234 static void init_tagcache(void)
236 bool clear = false;
238 tagcache_init();
240 while (!tagcache_is_initialized())
242 #ifdef HAVE_LCD_CHARCELLS
243 char buf[32];
244 #endif
245 int ret = tagcache_get_commit_step();
247 if (ret > 0)
249 #if CONFIG_CODEC == SWCODEC
250 /* hwcodec can't use voice here, as the database commit
251 * uses the audio buffer. */
252 static long talked_tick = 0;
253 if(global_settings.talk_menu
254 && (talked_tick == 0
255 || TIME_AFTER(current_tick, talked_tick+7*HZ)))
257 talked_tick = current_tick;
258 talk_id(LANG_TAGCACHE_INIT, false);
259 talk_number(ret, true);
260 talk_id(VOICE_OF, true);
261 talk_number(tagcache_get_max_commit_step(), true);
263 #endif
264 #ifdef HAVE_LCD_BITMAP
265 splashf(0, "%s [%d/%d]",
266 str(LANG_TAGCACHE_INIT), ret,
267 tagcache_get_max_commit_step());
268 #else
269 lcd_double_height(false);
270 snprintf(buf, sizeof(buf), " DB [%d/%d]", ret,
271 tagcache_get_max_commit_step());
272 lcd_puts(0, 1, buf);
273 lcd_update();
274 #endif
275 clear = true;
277 sleep(HZ/4);
279 tagtree_init();
281 if (clear)
283 backlight_on();
284 show_logo();
287 #endif
289 #ifdef SIMULATOR
291 static void init(void)
293 kernel_init();
294 buffer_init();
295 enable_irq();
296 lcd_init();
297 #ifdef HAVE_REMOTE_LCD
298 lcd_remote_init();
299 #endif
300 font_init();
301 show_logo();
302 button_init();
303 backlight_init();
304 lang_init();
305 #ifdef DEBUG
306 debug_init();
307 #endif
308 /* Must be done before any code uses the multi-screen APi */
309 gui_syncstatusbar_init(&statusbars);
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 cuesheet_init();
348 audio_init();
349 button_clear_queue(); /* Empty the keyboard buffer */
352 #else
354 static void init(void)
356 int rc;
357 bool mounted = false;
358 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
359 /* if nobody initialized ATA before, I consider this a cold start */
360 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
361 #endif
363 system_init();
364 kernel_init();
366 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
367 set_cpu_frequency(CPUFREQ_NORMAL);
368 #ifdef CPU_COLDFIRE
369 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
370 #endif
371 cpu_boost(true);
372 #endif
374 buffer_init();
376 settings_reset();
378 i2c_init();
380 power_init();
382 enable_irq();
383 #ifdef CPU_ARM
384 enable_fiq();
385 #endif
386 lcd_init();
387 #ifdef HAVE_REMOTE_LCD
388 lcd_remote_init();
389 #endif
390 font_init();
392 show_logo();
393 lang_init();
395 #ifdef DEBUG
396 debug_init();
397 #else
398 #if !defined(HAVE_FMADC) && !(CONFIG_STORAGE & STORAGE_MMC)
399 serial_setup();
400 #endif
401 #endif
403 #if CONFIG_RTC
404 rtc_init();
405 #endif
406 #ifdef HAVE_RTC_RAM
407 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
408 #endif
410 adc_init();
412 usb_init();
413 #if CONFIG_USBOTG == USBOTG_ISP1362
414 isp1362_init();
415 #elif CONFIG_USBOTG == USBOTG_M5636
416 m5636_init();
417 #endif
419 backlight_init();
421 button_init();
423 powermgmt_init();
425 #if CONFIG_TUNER
426 radio_init();
427 #endif
429 /* Must be done before any code uses the multi-screen APi */
430 gui_syncstatusbar_init(&statusbars);
432 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
433 if (coldstart && charger_inserted()
434 && !global_settings.car_adapter_mode
435 #ifdef ATA_POWER_PLAYERSTYLE
436 && !ide_powered() /* relies on probing result from bootloader */
437 #endif
440 rc = charging_screen(); /* display a "charging" screen */
441 if (rc == 1) /* charger removed */
442 power_off();
443 /* "On" pressed or USB connected: proceed */
444 show_logo(); /* again, to provide better visual feedback */
446 #endif
448 rc = storage_init();
449 if(rc)
451 #ifdef HAVE_LCD_BITMAP
452 char str[32];
453 lcd_clear_display();
454 snprintf(str, 31, "ATA error: %d", rc);
455 lcd_puts(0, 1, str);
456 lcd_puts(0, 3, "Press ON to debug");
457 lcd_update();
458 while(!(button_get(true) & BUTTON_REL)); /*DO NOT CHANGE TO ACTION SYSTEM */
459 dbg_ports();
460 #endif
461 panicf("ata: %d", rc);
464 #ifdef HAVE_EEPROM_SETTINGS
465 eeprom_settings_init();
466 #endif
468 usb_start_monitoring();
469 #ifndef HAVE_USBSTACK
470 while (usb_detect() == USB_INSERTED)
472 #ifdef HAVE_EEPROM_SETTINGS
473 firmware_settings.disk_clean = false;
474 #endif
475 /* enter USB mode early, before trying to mount */
476 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
477 #if (CONFIG_STORAGE & STORAGE_MMC)
478 if (!mmc_touched() ||
479 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
480 #endif
482 usb_screen();
483 mounted = true; /* mounting done @ end of USB mode */
485 #ifdef HAVE_USB_POWER
486 if (usb_powered()) /* avoid deadlock */
487 break;
488 #endif
490 #endif
492 if (!mounted)
494 rc = disk_mount_all();
495 if (rc<=0)
497 lcd_clear_display();
498 lcd_puts(0, 0, "No partition");
499 lcd_puts(0, 1, "found.");
500 #ifdef HAVE_LCD_BITMAP
501 lcd_puts(0, 2, "Insert USB cable");
502 lcd_puts(0, 3, "and fix it.");
503 #endif
504 lcd_update();
506 while(button_get(true) != SYS_USB_CONNECTED) {};
507 usb_screen();
508 system_reboot();
512 #if defined(SETTINGS_RESET) || (CONFIG_KEYPAD == IPOD_4G_PAD) || \
513 (CONFIG_KEYPAD == IRIVER_H10_PAD)
514 #ifdef SETTINGS_RESET
515 /* Reset settings if holding the reset button. (Rec on Archos,
516 A on Gigabeat) */
517 if ((button_status() & SETTINGS_RESET) == SETTINGS_RESET)
518 #else
519 /* Reset settings if the hold button is turned on */
520 if (button_hold())
521 #endif
523 splash(HZ*2, str(LANG_RESET_DONE_CLEAR));
524 settings_reset();
526 else
527 #endif
528 settings_load(SETTINGS_ALL);
530 if (init_dircache(true) < 0)
532 #ifdef HAVE_TAGCACHE
533 remove(TAGCACHE_STATEFILE);
534 #endif
537 gui_sync_wps_init();
538 settings_apply(true);
539 init_dircache(false);
540 #ifdef HAVE_TAGCACHE
541 init_tagcache();
542 #endif
544 #ifdef HAVE_EEPROM_SETTINGS
545 if (firmware_settings.initialized)
547 /* In case we crash. */
548 firmware_settings.disk_clean = false;
549 eeprom_settings_store();
551 #endif
552 status_init();
553 playlist_init();
554 tree_mem_init();
555 filetype_init();
556 scrobbler_init();
557 cuesheet_init();
559 #if CONFIG_CODEC != SWCODEC
560 /* No buffer allocation (see buffer.c) may take place after the call to
561 audio_init() since the mpeg thread takes the rest of the buffer space */
562 mp3_init( global_settings.volume,
563 global_settings.bass,
564 global_settings.treble,
565 global_settings.balance,
566 global_settings.loudness,
567 global_settings.avc,
568 global_settings.channel_config,
569 global_settings.stereo_width,
570 global_settings.mdb_strength,
571 global_settings.mdb_harmonics,
572 global_settings.mdb_center,
573 global_settings.mdb_shape,
574 global_settings.mdb_enable,
575 global_settings.superbass);
577 /* audio_init must to know the size of voice buffer so init voice first */
578 talk_init();
579 #endif /* CONFIG_CODEC != SWCODEC */
581 audio_init();
583 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
584 pcm_rec_init();
585 #endif
587 /* runtime database has to be initialized after audio_init() */
588 cpu_boost(false);
590 #ifdef AUTOROCK
592 static const char filename[] = PLUGIN_APPS_DIR "/autostart.rock";
594 if(file_exists(filename)) /* no complaint if it doesn't exist */
596 plugin_load((char*)filename, NULL); /* start if it does */
599 #endif /* #ifdef AUTOROCK */
601 #if CONFIG_CHARGING
602 car_adapter_mode_init();
603 #endif
604 #ifdef IPOD_ACCESSORY_PROTOCOL
605 iap_setup(global_settings.serial_bitrate);
606 #endif
607 #ifdef HAVE_ACCESSORY_SUPPLY
608 accessory_supply_set(global_settings.accessory_supply);
609 #endif
612 #ifdef CPU_PP
613 void cop_main(void)
615 /* This is the entry point for the coprocessor
616 Anyone not running an upgraded bootloader will never reach this point,
617 so it should not be assumed that the coprocessor be usable even on
618 platforms which support it.
620 A kernel thread is initially setup on the coprocessor and immediately
621 destroyed for purposes of continuity. The cop sits idle until at least
622 one thread exists on it. */
624 /* 3G doesn't have Rolo or dual core support yet */
625 #if NUM_CORES > 1
626 system_init();
627 kernel_init();
628 /* This should never be reached */
629 #endif
630 while(1) {
631 sleep_core(COP);
634 #endif /* CPU_PP */
636 int main(void)
638 app_main();
640 while(1) {
641 #if (CONFIG_LED == LED_REAL)
642 led(true); sleep(HZ/10);
643 led(false); sleep(HZ/10);
644 #endif
646 return 0;
648 #endif