Add a browse (remote) custom statusbar item in the theme settings.
[kugel-rb.git] / apps / main.c
blob8b6c1bbfbe11d963a02baef4a5729cd7d78806f2
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"
76 #include "statusbar-skinned.h"
78 #ifdef IPOD_ACCESSORY_PROTOCOL
79 #include "iap.h"
80 #endif
82 #if (CONFIG_CODEC == SWCODEC)
83 #include "playback.h"
84 #include "tdspeed.h"
85 #endif
86 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
87 #include "pcm_record.h"
88 #endif
90 #ifdef BUTTON_REC
91 #define SETTINGS_RESET BUTTON_REC
92 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
93 #define SETTINGS_RESET BUTTON_A
94 #endif
96 #if CONFIG_TUNER
97 #include "radio.h"
98 #endif
99 #if (CONFIG_STORAGE & STORAGE_MMC)
100 #include "ata_mmc.h"
101 #endif
103 #ifdef HAVE_REMOTE_LCD
104 #include "lcd-remote.h"
105 #endif
107 #if CONFIG_USBOTG == USBOTG_ISP1362
108 #include "isp1362.h"
109 #endif
111 #if CONFIG_USBOTG == USBOTG_M5636
112 #include "m5636.h"
113 #endif
115 #ifdef SIMULATOR
116 #include "sim_tasks.h"
117 #include "system-sdl.h"
118 #endif
120 /*#define AUTOROCK*/ /* define this to check for "autostart.rock" on boot */
122 const char appsversion[]=APPSVERSION;
124 static void init(void);
126 #ifdef SIMULATOR
127 void app_main(void)
128 #else
129 static void app_main(void)
130 #endif
132 int i;
133 init();
134 FOR_NB_SCREENS(i)
136 screens[i].clear_display();
137 screens[i].update();
139 #ifdef HAVE_LCD_BITMAP
140 list_init();
141 #endif
142 tree_gui_init();
143 viewportmanager_init();
144 #ifdef HAVE_USBSTACK
145 /* All threads should be created and public queues registered by now */
146 usb_start_monitoring();
147 #endif
149 #ifdef AUTOROCK
151 static const char filename[] = PLUGIN_APPS_DIR "/autostart.rock";
153 if(file_exists(filename)) /* no complaint if it doesn't exist */
155 plugin_load((char*)filename, NULL); /* start if it does */
158 #endif /* #ifdef AUTOROCK */
160 root_menu();
163 static int init_dircache(bool preinit)
165 #ifdef HAVE_DIRCACHE
166 int result = 0;
167 bool clear = false;
169 if (preinit)
170 dircache_init();
172 if (!global_settings.dircache)
173 return 0;
175 # ifdef HAVE_EEPROM_SETTINGS
176 if (firmware_settings.initialized && firmware_settings.disk_clean
177 && preinit)
179 result = dircache_load();
181 if (result < 0)
183 firmware_settings.disk_clean = false;
184 if (global_status.dircache_size <= 0)
186 /* This will be in default language, settings are not
187 applied yet. Not really any easy way to fix that. */
188 splash(0, str(LANG_SCANNING_DISK));
189 clear = true;
192 dircache_build(global_status.dircache_size);
195 else
196 # endif
198 if (preinit)
199 return -1;
201 if (!dircache_is_enabled()
202 && !dircache_is_initializing())
204 if (global_status.dircache_size <= 0)
206 splash(0, str(LANG_SCANNING_DISK));
207 clear = true;
209 result = dircache_build(global_status.dircache_size);
212 if (result < 0)
214 /* Initialization of dircache failed. Manual action is
215 * necessary to enable dircache again.
217 splashf(0, "Dircache failed, disabled. Result: %d", result);
218 global_settings.dircache = false;
222 if (clear)
224 backlight_on();
225 show_logo();
226 global_status.dircache_size = dircache_get_cache_size();
227 status_save();
230 return result;
231 #else
232 (void)preinit;
233 return 0;
234 #endif
237 #ifdef HAVE_TAGCACHE
238 static void init_tagcache(void)
240 bool clear = false;
242 tagcache_init();
244 while (!tagcache_is_initialized())
246 int ret = tagcache_get_commit_step();
248 if (ret > 0)
250 #if CONFIG_CODEC == SWCODEC
251 /* hwcodec can't use voice here, as the database commit
252 * uses the audio buffer. */
253 static long talked_tick = 0;
254 if(global_settings.talk_menu
255 && (talked_tick == 0
256 || TIME_AFTER(current_tick, talked_tick+7*HZ)))
258 talked_tick = current_tick;
259 talk_id(LANG_TAGCACHE_INIT, false);
260 talk_number(ret, true);
261 talk_id(VOICE_OF, true);
262 talk_number(tagcache_get_max_commit_step(), true);
264 #endif
265 #ifdef HAVE_LCD_BITMAP
266 if (lang_is_rtl())
268 splashf(0, "[%d/%d] %s", ret, tagcache_get_max_commit_step(),
269 str(LANG_TAGCACHE_INIT));
271 else
273 splashf(0, "%s [%d/%d]", str(LANG_TAGCACHE_INIT), ret,
274 tagcache_get_max_commit_step());
276 #else
277 lcd_double_height(false);
278 lcd_putsf(0, 1, " DB [%d/%d]", ret,
279 tagcache_get_max_commit_step());
280 lcd_update();
281 #endif
282 clear = true;
284 sleep(HZ/4);
286 tagtree_init();
288 if (clear)
290 backlight_on();
291 show_logo();
294 #endif
296 #ifdef SIMULATOR
298 static void init(void)
300 kernel_init();
301 buffer_init();
302 enable_irq();
303 lcd_init();
304 #ifdef HAVE_REMOTE_LCD
305 lcd_remote_init();
306 #endif
307 font_init();
308 show_logo();
309 button_init();
310 backlight_init();
311 sim_tasks_init();
312 lang_init(core_language_builtin, language_strings,
313 LANG_LAST_INDEX_IN_ARRAY);
314 #ifdef DEBUG
315 debug_init();
316 #endif
317 /* Must be done before any code uses the multi-screen API */
318 gui_syncstatusbar_init(&statusbars);
319 storage_init();
320 settings_reset();
321 settings_load(SETTINGS_ALL);
322 gui_sync_wps_init();
323 sb_skin_init();
324 settings_apply(true);
325 init_dircache(true);
326 init_dircache(false);
327 #ifdef HAVE_TAGCACHE
328 init_tagcache();
329 #endif
330 sleep(HZ/2);
331 tree_mem_init();
332 filetype_init();
333 playlist_init();
335 #if CONFIG_CODEC != SWCODEC
336 mp3_init( global_settings.volume,
337 global_settings.bass,
338 global_settings.treble,
339 global_settings.balance,
340 global_settings.loudness,
341 global_settings.avc,
342 global_settings.channel_config,
343 global_settings.stereo_width,
344 global_settings.mdb_strength,
345 global_settings.mdb_harmonics,
346 global_settings.mdb_center,
347 global_settings.mdb_shape,
348 global_settings.mdb_enable,
349 global_settings.superbass);
351 /* audio_init must to know the size of voice buffer so init voice first */
352 talk_init();
353 #endif /* CONFIG_CODEC != SWCODEC */
355 scrobbler_init();
356 #if CONFIG_CODEC == SWCODEC
357 tdspeed_init();
358 #endif /* CONFIG_CODEC == SWCODEC */
360 audio_init();
361 button_clear_queue(); /* Empty the keyboard buffer */
364 #else
366 static void init(void)
368 int rc;
369 bool mounted = false;
370 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
371 /* if nobody initialized ATA before, I consider this a cold start */
372 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
373 #endif
375 system_init();
376 kernel_init();
378 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
379 set_cpu_frequency(CPUFREQ_NORMAL);
380 #ifdef CPU_COLDFIRE
381 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
382 #endif
383 cpu_boost(true);
384 #endif
386 buffer_init();
388 settings_reset();
390 i2c_init();
392 power_init();
394 enable_irq();
395 #ifdef CPU_ARM
396 enable_fiq();
397 #endif
398 lcd_init();
399 #ifdef HAVE_REMOTE_LCD
400 lcd_remote_init();
401 #endif
402 font_init();
404 show_logo();
405 lang_init(core_language_builtin, language_strings,
406 LANG_LAST_INDEX_IN_ARRAY);
408 #ifdef DEBUG
409 debug_init();
410 #else
411 #ifdef HAVE_SERIAL
412 serial_setup();
413 #endif
414 #endif
416 #if CONFIG_RTC
417 rtc_init();
418 #endif
419 #ifdef HAVE_RTC_RAM
420 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
421 #endif
423 adc_init();
425 usb_init();
426 #if CONFIG_USBOTG == USBOTG_ISP1362
427 isp1362_init();
428 #elif CONFIG_USBOTG == USBOTG_M5636
429 m5636_init();
430 #endif
432 backlight_init();
434 button_init();
436 powermgmt_init();
438 #if CONFIG_TUNER
439 radio_init();
440 #endif
442 /* Must be done before any code uses the multi-screen API */
443 gui_syncstatusbar_init(&statusbars);
445 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
446 /* charger_inserted() can't be used here because power_thread()
447 hasn't checked power_input_status() yet */
448 if (coldstart && (power_input_status() & POWER_INPUT_MAIN_CHARGER)
449 && !global_settings.car_adapter_mode
450 #ifdef ATA_POWER_PLAYERSTYLE
451 && !ide_powered() /* relies on probing result from bootloader */
452 #endif
455 rc = charging_screen(); /* display a "charging" screen */
456 if (rc == 1) /* charger removed */
457 power_off();
458 /* "On" pressed or USB connected: proceed */
459 show_logo(); /* again, to provide better visual feedback */
461 #endif
463 rc = storage_init();
464 if(rc)
466 #ifdef HAVE_LCD_BITMAP
467 lcd_clear_display();
468 lcd_putsf(0, 1, "ATA error: %d", rc);
469 lcd_puts(0, 3, "Press ON to debug");
470 lcd_update();
471 while(!(button_get(true) & BUTTON_REL)); /*DO NOT CHANGE TO ACTION SYSTEM */
472 dbg_ports();
473 #endif
474 panicf("ata: %d", rc);
477 #ifdef HAVE_EEPROM_SETTINGS
478 eeprom_settings_init();
479 #endif
481 #ifndef HAVE_USBSTACK
482 usb_start_monitoring();
483 while (usb_detect() == USB_INSERTED)
485 #ifdef HAVE_EEPROM_SETTINGS
486 firmware_settings.disk_clean = false;
487 #endif
488 /* enter USB mode early, before trying to mount */
489 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
490 #if (CONFIG_STORAGE & STORAGE_MMC)
491 if (!mmc_touched() ||
492 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
493 #endif
495 usb_screen();
496 mounted = true; /* mounting done @ end of USB mode */
498 #ifdef HAVE_USB_POWER
499 if (usb_powered()) /* avoid deadlock */
500 break;
501 #endif
503 #endif
505 if (!mounted)
507 rc = disk_mount_all();
508 if (rc<=0)
510 lcd_clear_display();
511 lcd_puts(0, 0, "No partition");
512 lcd_puts(0, 1, "found.");
513 #ifdef HAVE_LCD_BITMAP
514 lcd_puts(0, 2, "Insert USB cable");
515 lcd_puts(0, 3, "and fix it.");
516 #endif
517 lcd_update();
519 while(button_get(true) != SYS_USB_CONNECTED) {};
520 usb_screen();
521 system_reboot();
525 #if defined(SETTINGS_RESET) || (CONFIG_KEYPAD == IPOD_4G_PAD) || \
526 (CONFIG_KEYPAD == IRIVER_H10_PAD)
527 #ifdef SETTINGS_RESET
528 /* Reset settings if holding the reset button. (Rec on Archos,
529 A on Gigabeat) */
530 if ((button_status() & SETTINGS_RESET) == SETTINGS_RESET)
531 #else
532 /* Reset settings if the hold button is turned on */
533 if (button_hold())
534 #endif
536 splash(HZ*2, str(LANG_RESET_DONE_CLEAR));
537 settings_reset();
539 else
540 #endif
541 settings_load(SETTINGS_ALL);
543 if (init_dircache(true) < 0)
545 #ifdef HAVE_TAGCACHE
546 remove(TAGCACHE_STATEFILE);
547 #endif
550 gui_sync_wps_init();
551 sb_skin_init();
552 settings_apply(true);
553 init_dircache(false);
554 #ifdef HAVE_TAGCACHE
555 init_tagcache();
556 #endif
558 #ifdef HAVE_EEPROM_SETTINGS
559 if (firmware_settings.initialized)
561 /* In case we crash. */
562 firmware_settings.disk_clean = false;
563 eeprom_settings_store();
565 #endif
566 playlist_init();
567 tree_mem_init();
568 filetype_init();
569 scrobbler_init();
570 #if CONFIG_CODEC == SWCODEC
571 tdspeed_init();
572 #endif /* CONFIG_CODEC == SWCODEC */
574 #if CONFIG_CODEC != SWCODEC
575 /* No buffer allocation (see buffer.c) may take place after the call to
576 audio_init() since the mpeg thread takes the rest of the buffer space */
577 mp3_init( global_settings.volume,
578 global_settings.bass,
579 global_settings.treble,
580 global_settings.balance,
581 global_settings.loudness,
582 global_settings.avc,
583 global_settings.channel_config,
584 global_settings.stereo_width,
585 global_settings.mdb_strength,
586 global_settings.mdb_harmonics,
587 global_settings.mdb_center,
588 global_settings.mdb_shape,
589 global_settings.mdb_enable,
590 global_settings.superbass);
592 /* audio_init must to know the size of voice buffer so init voice first */
593 talk_init();
594 #endif /* CONFIG_CODEC != SWCODEC */
596 audio_init();
598 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
599 pcm_rec_init();
600 #endif
602 /* runtime database has to be initialized after audio_init() */
603 cpu_boost(false);
605 #if CONFIG_CHARGING
606 car_adapter_mode_init();
607 #endif
608 #ifdef IPOD_ACCESSORY_PROTOCOL
609 iap_setup(global_settings.serial_bitrate);
610 #endif
611 #ifdef HAVE_ACCESSORY_SUPPLY
612 accessory_supply_set(global_settings.accessory_supply);
613 #endif
616 #ifdef CPU_PP
617 void cop_main(void)
619 /* This is the entry point for the coprocessor
620 Anyone not running an upgraded bootloader will never reach this point,
621 so it should not be assumed that the coprocessor be usable even on
622 platforms which support it.
624 A kernel thread is initially setup on the coprocessor and immediately
625 destroyed for purposes of continuity. The cop sits idle until at least
626 one thread exists on it. */
628 /* 3G doesn't have Rolo or dual core support yet */
629 #if NUM_CORES > 1
630 system_init();
631 kernel_init();
632 /* This should never be reached */
633 #endif
634 while(1) {
635 sleep_core(COP);
638 #endif /* CPU_PP */
640 int main(void)
642 app_main();
644 while(1) {
645 #if (CONFIG_LED == LED_REAL)
646 led(true); sleep(HZ/10);
647 led(false); sleep(HZ/10);
648 #endif
650 return 0;
652 #endif