Introduce names for header and row color in the tables so that they are the same...
[kugel-rb.git] / apps / main.c
blob49542e8b014a567b05ff9c0830218ebd7b3ad7e4
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
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 storage_init();
300 settings_reset();
301 settings_load(SETTINGS_ALL);
302 gui_sync_wps_init();
303 settings_apply(true);
304 init_dircache(true);
305 init_dircache(false);
306 #ifdef HAVE_TAGCACHE
307 init_tagcache();
308 #endif
309 sleep(HZ/2);
310 tree_mem_init();
311 filetype_init();
312 playlist_init();
314 #if CONFIG_CODEC != SWCODEC
315 mp3_init( global_settings.volume,
316 global_settings.bass,
317 global_settings.treble,
318 global_settings.balance,
319 global_settings.loudness,
320 global_settings.avc,
321 global_settings.channel_config,
322 global_settings.stereo_width,
323 global_settings.mdb_strength,
324 global_settings.mdb_harmonics,
325 global_settings.mdb_center,
326 global_settings.mdb_shape,
327 global_settings.mdb_enable,
328 global_settings.superbass);
330 /* audio_init must to know the size of voice buffer so init voice first */
331 talk_init();
332 #endif /* CONFIG_CODEC != SWCODEC */
334 scrobbler_init();
335 #if CONFIG_CODEC == SWCODEC
336 tdspeed_init();
337 #endif /* CONFIG_CODEC == SWCODEC */
339 audio_init();
340 button_clear_queue(); /* Empty the keyboard buffer */
343 #else
345 static void init(void)
347 int rc;
348 bool mounted = false;
349 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
350 /* if nobody initialized ATA before, I consider this a cold start */
351 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
352 #endif
354 system_init();
355 kernel_init();
357 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
358 set_cpu_frequency(CPUFREQ_NORMAL);
359 #ifdef CPU_COLDFIRE
360 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
361 #endif
362 cpu_boost(true);
363 #endif
365 buffer_init();
367 settings_reset();
369 i2c_init();
371 power_init();
373 enable_irq();
374 #ifdef CPU_ARM
375 enable_fiq();
376 #endif
377 lcd_init();
378 #ifdef HAVE_REMOTE_LCD
379 lcd_remote_init();
380 #endif
381 font_init();
383 show_logo();
384 lang_init();
386 #ifdef DEBUG
387 debug_init();
388 #else
389 #ifdef HAVE_SERIAL
390 serial_setup();
391 #endif
392 #endif
394 #if CONFIG_RTC
395 rtc_init();
396 #endif
397 #ifdef HAVE_RTC_RAM
398 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
399 #endif
401 adc_init();
403 usb_init();
404 #if CONFIG_USBOTG == USBOTG_ISP1362
405 isp1362_init();
406 #elif CONFIG_USBOTG == USBOTG_M5636
407 m5636_init();
408 #endif
410 backlight_init();
412 button_init();
414 powermgmt_init();
416 #if CONFIG_TUNER
417 radio_init();
418 #endif
420 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
421 /* charger_inserted() can't be used here because power_thread()
422 hasn't checked power_input_status() yet */
423 if (coldstart && (power_input_status() & POWER_INPUT_MAIN_CHARGER)
424 && !global_settings.car_adapter_mode
425 #ifdef ATA_POWER_PLAYERSTYLE
426 && !ide_powered() /* relies on probing result from bootloader */
427 #endif
430 rc = charging_screen(); /* display a "charging" screen */
431 if (rc == 1) /* charger removed */
432 power_off();
433 /* "On" pressed or USB connected: proceed */
434 show_logo(); /* again, to provide better visual feedback */
436 #endif
438 rc = storage_init();
439 if(rc)
441 #ifdef HAVE_LCD_BITMAP
442 char str[32];
443 lcd_clear_display();
444 snprintf(str, 31, "ATA error: %d", rc);
445 lcd_puts(0, 1, str);
446 lcd_puts(0, 3, "Press ON to debug");
447 lcd_update();
448 while(!(button_get(true) & BUTTON_REL)); /*DO NOT CHANGE TO ACTION SYSTEM */
449 dbg_ports();
450 #endif
451 panicf("ata: %d", rc);
454 #ifdef HAVE_EEPROM_SETTINGS
455 eeprom_settings_init();
456 #endif
458 #ifndef HAVE_USBSTACK
459 usb_start_monitoring();
460 while (usb_detect() == USB_INSERTED)
462 #ifdef HAVE_EEPROM_SETTINGS
463 firmware_settings.disk_clean = false;
464 #endif
465 /* enter USB mode early, before trying to mount */
466 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
467 #if (CONFIG_STORAGE & STORAGE_MMC)
468 if (!mmc_touched() ||
469 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
470 #endif
472 usb_screen();
473 mounted = true; /* mounting done @ end of USB mode */
475 #ifdef HAVE_USB_POWER
476 if (usb_powered()) /* avoid deadlock */
477 break;
478 #endif
480 #endif
482 if (!mounted)
484 rc = disk_mount_all();
485 if (rc<=0)
487 lcd_clear_display();
488 lcd_puts(0, 0, "No partition");
489 lcd_puts(0, 1, "found.");
490 #ifdef HAVE_LCD_BITMAP
491 lcd_puts(0, 2, "Insert USB cable");
492 lcd_puts(0, 3, "and fix it.");
493 #endif
494 lcd_update();
496 while(button_get(true) != SYS_USB_CONNECTED) {};
497 usb_screen();
498 system_reboot();
502 #if defined(SETTINGS_RESET) || (CONFIG_KEYPAD == IPOD_4G_PAD) || \
503 (CONFIG_KEYPAD == IRIVER_H10_PAD)
504 #ifdef SETTINGS_RESET
505 /* Reset settings if holding the reset button. (Rec on Archos,
506 A on Gigabeat) */
507 if ((button_status() & SETTINGS_RESET) == SETTINGS_RESET)
508 #else
509 /* Reset settings if the hold button is turned on */
510 if (button_hold())
511 #endif
513 splash(HZ*2, str(LANG_RESET_DONE_CLEAR));
514 settings_reset();
516 else
517 #endif
518 settings_load(SETTINGS_ALL);
520 if (init_dircache(true) < 0)
522 #ifdef HAVE_TAGCACHE
523 remove(TAGCACHE_STATEFILE);
524 #endif
527 gui_sync_wps_init();
528 settings_apply(true);
529 init_dircache(false);
530 #ifdef HAVE_TAGCACHE
531 init_tagcache();
532 #endif
534 #ifdef HAVE_EEPROM_SETTINGS
535 if (firmware_settings.initialized)
537 /* In case we crash. */
538 firmware_settings.disk_clean = false;
539 eeprom_settings_store();
541 #endif
542 status_init();
543 playlist_init();
544 tree_mem_init();
545 filetype_init();
546 scrobbler_init();
547 #if CONFIG_CODEC == SWCODEC
548 tdspeed_init();
549 #endif /* CONFIG_CODEC == SWCODEC */
551 #if CONFIG_CODEC != SWCODEC
552 /* No buffer allocation (see buffer.c) may take place after the call to
553 audio_init() since the mpeg thread takes the rest of the buffer space */
554 mp3_init( global_settings.volume,
555 global_settings.bass,
556 global_settings.treble,
557 global_settings.balance,
558 global_settings.loudness,
559 global_settings.avc,
560 global_settings.channel_config,
561 global_settings.stereo_width,
562 global_settings.mdb_strength,
563 global_settings.mdb_harmonics,
564 global_settings.mdb_center,
565 global_settings.mdb_shape,
566 global_settings.mdb_enable,
567 global_settings.superbass);
569 /* audio_init must to know the size of voice buffer so init voice first */
570 talk_init();
571 #endif /* CONFIG_CODEC != SWCODEC */
573 audio_init();
575 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
576 pcm_rec_init();
577 #endif
579 /* runtime database has to be initialized after audio_init() */
580 cpu_boost(false);
582 #ifdef AUTOROCK
584 static const char filename[] = PLUGIN_APPS_DIR "/autostart.rock";
586 if(file_exists(filename)) /* no complaint if it doesn't exist */
588 plugin_load((char*)filename, NULL); /* start if it does */
591 #endif /* #ifdef AUTOROCK */
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