Forgot to add a file as usual.
[Rockbox.git] / apps / main.c
blob2dd90ef5fe38c8df0aebbc502118a091ffd6aa77
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Björn Stenberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "config.h"
21 #include "ata.h"
22 #include "ata_idle_notify.h"
23 #include "disk.h"
24 #include "fat.h"
25 #include "lcd.h"
26 #include "rtc.h"
27 #include "debug.h"
28 #include "led.h"
29 #include "kernel.h"
30 #include "button.h"
31 #include "tree.h"
32 #include "filetypes.h"
33 #include "panic.h"
34 #include "menu.h"
35 #include "system.h"
36 #include "usb.h"
37 #include "powermgmt.h"
38 #include "adc.h"
39 #include "i2c.h"
40 #ifndef DEBUG
41 #include "serial.h"
42 #endif
43 #include "audio.h"
44 #include "mp3_playback.h"
45 #include "thread.h"
46 #include "settings.h"
47 #include "backlight.h"
48 #include "status.h"
49 #include "debug_menu.h"
50 #include "version.h"
51 #include "sprintf.h"
52 #include "font.h"
53 #include "language.h"
54 #include "gwps.h"
55 #include "playlist.h"
56 #include "buffer.h"
57 #include "rolo.h"
58 #include "screens.h"
59 #include "power.h"
60 #include "talk.h"
61 #include "plugin.h"
62 #include "misc.h"
63 #include "dircache.h"
64 #ifdef HAVE_TAGCACHE
65 #include "tagcache.h"
66 #include "tagtree.h"
67 #endif
68 #include "lang.h"
69 #include "string.h"
70 #include "splash.h"
71 #include "eeprom_settings.h"
72 #include "scrobbler.h"
73 #include "icon.h"
75 #if (CONFIG_CODEC == SWCODEC)
76 #include "playback.h"
77 #include "pcmbuf.h"
78 #else
79 #define pcmbuf_init()
80 #endif
81 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
82 #include "pcm_record.h"
83 #endif
85 #ifdef BUTTON_REC
86 #define SETTINGS_RESET BUTTON_REC
87 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
88 #define SETTINGS_RESET BUTTON_A
89 #endif
91 #if CONFIG_TUNER
92 #include "radio.h"
93 #endif
94 #ifdef HAVE_MMC
95 #include "ata_mmc.h"
96 #endif
98 #ifdef HAVE_REMOTE_LCD
99 #include "lcd-remote.h"
100 #endif
102 #if CONFIG_USBOTG == USBOTG_ISP1362
103 #include "isp1362.h"
104 #endif
106 #if CONFIG_USBOTG == USBOTG_M5636
107 #include "m5636.h"
108 #endif
110 #include "cuesheet.h"
112 /*#define AUTOROCK*/ /* define this to check for "autostart.rock" on boot */
114 const char appsversion[]=APPSVERSION;
116 static void init(void);
118 #ifdef SIMULATOR
119 void app_main(void)
120 #else
121 static void app_main(void)
122 #endif
124 init();
125 tree_gui_init();
126 root_menu();
129 static int init_dircache(bool preinit)
131 #ifdef HAVE_DIRCACHE
132 int result = 0;
133 bool clear = false;
135 if (preinit)
136 dircache_init();
138 if (!global_settings.dircache)
139 return 0;
141 # ifdef HAVE_EEPROM_SETTINGS
142 if (firmware_settings.initialized && firmware_settings.disk_clean
143 && preinit)
145 result = dircache_load();
147 if (result < 0)
149 firmware_settings.disk_clean = false;
150 if (global_status.dircache_size <= 0)
152 /* This will be in default language, settings are not
153 applied yet. Not really any easy way to fix that. */
154 gui_syncsplash(0, str(LANG_DIRCACHE_BUILDING));
155 clear = true;
158 dircache_build(global_status.dircache_size);
161 else
162 # endif
164 if (preinit)
165 return -1;
167 if (!dircache_is_enabled()
168 && !dircache_is_initializing())
170 if (global_status.dircache_size <= 0)
172 gui_syncsplash(0, str(LANG_DIRCACHE_BUILDING));
173 clear = true;
175 result = dircache_build(global_status.dircache_size);
178 if (result < 0)
180 /* Initialization of dircache failed. Manual action is
181 * necessary to enable dircache again.
183 gui_syncsplash(0, "Dircache failed, disabled. Result: %d", result);
184 global_settings.dircache = false;
189 if (clear)
191 backlight_on();
192 show_logo();
193 global_status.dircache_size = dircache_get_cache_size();
194 status_save();
197 return result;
198 #else
199 (void)preinit;
200 return 0;
201 #endif
204 #ifdef HAVE_TAGCACHE
205 static void init_tagcache(void)
207 bool clear = false;
209 tagcache_init();
211 while (!tagcache_is_initialized())
213 #ifdef HAVE_LCD_CHARCELLS
214 char buf[32];
215 #endif
216 int ret = tagcache_get_commit_step();
218 if (ret > 0)
220 #ifdef HAVE_LCD_BITMAP
221 gui_syncsplash(0, "%s [%d/%d]",
222 str(LANG_TAGCACHE_INIT), ret,
223 tagcache_get_max_commit_step());
224 #else
225 lcd_double_height(false);
226 snprintf(buf, sizeof(buf), " DB [%d/%d]", ret,
227 tagcache_get_max_commit_step());
228 lcd_puts(0, 1, buf);
229 lcd_update();
230 #endif
231 clear = true;
233 sleep(HZ/4);
235 tagtree_init();
237 if (clear)
239 backlight_on();
240 show_logo();
243 #endif
245 #ifdef SIMULATOR
247 static void init(void)
249 init_threads();
250 buffer_init();
251 lcd_init();
252 #ifdef HAVE_REMOTE_LCD
253 lcd_remote_init();
254 #endif
255 font_init();
256 show_logo();
257 button_init();
258 backlight_init();
259 lang_init();
260 #ifdef DEBUG
261 debug_init();
262 #endif
263 /* Must be done before any code uses the multi-screen APi */
264 screen_access_init();
265 gui_syncstatusbar_init(&statusbars);
266 settings_reset();
267 settings_load(SETTINGS_ALL);
268 gui_sync_wps_init();
269 settings_apply();
270 init_dircache(true);
271 init_dircache(false);
272 #ifdef HAVE_TAGCACHE
273 init_tagcache();
274 #endif
275 sleep(HZ/2);
276 tree_mem_init();
277 filetype_init();
278 playlist_init();
280 #if CONFIG_CODEC != SWCODEC
281 mp3_init( global_settings.volume,
282 global_settings.bass,
283 global_settings.treble,
284 global_settings.balance,
285 global_settings.loudness,
286 global_settings.avc,
287 global_settings.channel_config,
288 global_settings.stereo_width,
289 global_settings.mdb_strength,
290 global_settings.mdb_harmonics,
291 global_settings.mdb_center,
292 global_settings.mdb_shape,
293 global_settings.mdb_enable,
294 global_settings.superbass);
296 /* audio_init must to know the size of voice buffer so init voice first */
297 talk_init();
298 #endif /* CONFIG_CODEC != SWCODEC */
300 scrobbler_init();
301 cuesheet_init();
303 audio_init();
304 button_clear_queue(); /* Empty the keyboard buffer */
307 #else
309 static void init(void)
311 int rc;
312 bool mounted = false;
313 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
314 /* if nobody initialized ATA before, I consider this a cold start */
315 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
316 #endif
317 #ifdef CPU_PP
318 COP_CTL = PROC_WAKE;
319 #endif
320 system_init();
321 kernel_init();
323 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
324 set_cpu_frequency(CPUFREQ_NORMAL);
325 #ifdef CPU_COLDFIRE
326 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
327 #endif
328 cpu_boost(true);
329 #endif
331 buffer_init();
333 settings_reset();
335 power_init();
337 set_irq_level(0);
338 lcd_init();
339 #ifdef HAVE_REMOTE_LCD
340 lcd_remote_init();
341 #endif
342 font_init();
344 show_logo();
345 lang_init();
347 #ifdef DEBUG
348 debug_init();
349 #else
350 #if !defined(HAVE_FMADC) && !defined(HAVE_MMC)
351 serial_setup();
352 #endif
353 #endif
355 i2c_init();
357 #if CONFIG_RTC
358 rtc_init();
359 #endif
360 #ifdef HAVE_RTC_RAM
361 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
362 #endif
364 adc_init();
366 usb_init();
367 #if CONFIG_USBOTG == USBOTG_ISP1362
368 isp1362_init();
369 #elif CONFIG_USBOTG == USBOTG_M5636
370 m5636_init();
371 #endif
373 backlight_init();
375 button_init();
377 powermgmt_init();
379 #if CONFIG_TUNER
380 radio_init();
381 #endif
383 /* Must be done before any code uses the multi-screen APi */
384 screen_access_init();
385 gui_syncstatusbar_init(&statusbars);
387 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
388 if (coldstart && charger_inserted()
389 && !global_settings.car_adapter_mode
390 #ifdef ATA_POWER_PLAYERSTYLE
391 && !ide_powered() /* relies on probing result from bootloader */
392 #endif
395 rc = charging_screen(); /* display a "charging" screen */
396 if (rc == 1) /* charger removed */
397 power_off();
398 /* "On" pressed or USB connected: proceed */
399 show_logo(); /* again, to provide better visual feedback */
401 #endif
403 ata_idle_notify_init();
404 rc = ata_init();
405 if(rc)
407 #ifdef HAVE_LCD_BITMAP
408 char str[32];
409 lcd_clear_display();
410 snprintf(str, 31, "ATA error: %d", rc);
411 lcd_puts(0, 1, str);
412 lcd_puts(0, 3, "Press ON to debug");
413 lcd_update();
414 while(!(button_get(true) & BUTTON_REL)); /*DO NOT CHANGE TO ACTION SYSTEM */
415 dbg_ports();
416 #endif
417 panicf("ata: %d", rc);
420 #ifdef HAVE_EEPROM_SETTINGS
421 eeprom_settings_init();
422 #endif
424 usb_start_monitoring();
425 while (usb_detect())
427 #ifdef HAVE_EEPROM_SETTINGS
428 firmware_settings.disk_clean = false;
429 #endif
430 /* enter USB mode early, before trying to mount */
431 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
432 #ifdef HAVE_MMC
433 if (!mmc_touched() ||
434 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
435 #endif
437 usb_screen();
438 mounted = true; /* mounting done @ end of USB mode */
440 #ifdef HAVE_USB_POWER
441 if (usb_powered()) /* avoid deadlock */
442 break;
443 #endif
446 if (!mounted)
448 rc = disk_mount_all();
449 if (rc<=0)
451 lcd_clear_display();
452 lcd_puts(0, 0, "No partition");
453 lcd_puts(0, 1, "found.");
454 #ifdef HAVE_LCD_BITMAP
455 lcd_puts(0, 2, "Insert USB cable");
456 lcd_puts(0, 3, "and fix it.");
457 #endif
458 lcd_update();
460 while(button_get(true) != SYS_USB_CONNECTED) {};
461 usb_screen();
462 system_reboot();
466 #if defined(SETTINGS_RESET) || (CONFIG_KEYPAD == IPOD_4G_PAD) || \
467 (CONFIG_KEYPAD == IRIVER_H10_PAD)
468 #ifdef SETTINGS_RESET
469 /* Reset settings if holding the reset button. (Rec on Archos,
470 A on Gigabeat) */
471 if ((button_status() & SETTINGS_RESET) == SETTINGS_RESET)
472 #else
473 /* Reset settings if the hold button is turned on */
474 if (button_hold())
475 #endif
477 gui_syncsplash(HZ*2, str(LANG_RESET_DONE_CLEAR));
478 settings_reset();
480 else
481 #endif
482 settings_load(SETTINGS_ALL);
484 if (init_dircache(true) < 0)
486 #ifdef HAVE_TAGCACHE
487 remove(TAGCACHE_STATEFILE);
488 #endif
491 gui_sync_wps_init();
492 settings_apply();
493 init_dircache(false);
494 #ifdef HAVE_TAGCACHE
495 init_tagcache();
496 #endif
498 #ifdef HAVE_EEPROM_SETTINGS
499 if (firmware_settings.initialized)
501 /* In case we crash. */
502 firmware_settings.disk_clean = false;
503 eeprom_settings_store();
505 #endif
506 status_init();
507 playlist_init();
508 tree_mem_init();
509 filetype_init();
510 scrobbler_init();
511 cuesheet_init();
513 #if CONFIG_CODEC != SWCODEC
514 /* No buffer allocation (see buffer.c) may take place after the call to
515 audio_init() since the mpeg thread takes the rest of the buffer space */
516 mp3_init( global_settings.volume,
517 global_settings.bass,
518 global_settings.treble,
519 global_settings.balance,
520 global_settings.loudness,
521 global_settings.avc,
522 global_settings.channel_config,
523 global_settings.stereo_width,
524 global_settings.mdb_strength,
525 global_settings.mdb_harmonics,
526 global_settings.mdb_center,
527 global_settings.mdb_shape,
528 global_settings.mdb_enable,
529 global_settings.superbass);
531 /* audio_init must to know the size of voice buffer so init voice first */
532 talk_init();
533 #endif /* CONFIG_CODEC != SWCODEC */
535 audio_init();
537 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
538 pcm_rec_init();
539 #endif
541 /* runtime database has to be initialized after audio_init() */
542 cpu_boost(false);
544 #ifdef AUTOROCK
546 int fd;
547 static const char filename[] = PLUGIN_DIR "/autostart.rock";
549 fd = open(filename, O_RDONLY);
550 if(fd >= 0) /* no complaint if it doesn't exist */
552 close(fd);
553 plugin_load((char*)filename, NULL); /* start if it does */
556 #endif /* #ifdef AUTOROCK */
558 #if CONFIG_CHARGING
559 car_adapter_mode_init();
560 #endif
563 #ifdef CPU_PP
564 void cop_main(void)
566 /* This is the entry point for the coprocessor
567 Anyone not running an upgraded bootloader will never reach this point,
568 so it should not be assumed that the coprocessor be usable even on
569 platforms which support it.
571 A kernel thread runs on the coprocessor which waits for other threads to be
572 added, and gracefully handles RoLo */
574 #if CONFIG_CPU == PP5002
575 /* 3G doesn't have Rolo or dual core support yet */
576 while(1) {
577 COP_CTL = PROC_SLEEP;
579 #else
580 extern volatile unsigned char cpu_message;
582 system_init();
583 kernel_init();
585 while(cpu_message != COP_REBOOT) {
586 sleep(HZ);
588 rolo_restart_cop();
589 #endif /* PP5002 */
591 #endif /* CPU_PP */
593 int main(void)
595 app_main();
597 while(1) {
598 #if (CONFIG_LED == LED_REAL)
599 led(true); sleep(HZ/10);
600 led(false); sleep(HZ/10);
601 #endif
603 return 0;
605 #endif