WpsEditor: add linenumbrs WpsEditor: Add linenumbers to the WPS Code, and highlight...
[kugel-rb.git] / apps / main.c
blobbacaa51e01016b366afc13b4cb2c4ec30d062dc2
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 "ata.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 #if (CONFIG_CODEC == SWCODEC)
77 #include "playback.h"
78 #endif
79 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
80 #include "pcm_record.h"
81 #endif
83 #ifdef BUTTON_REC
84 #define SETTINGS_RESET BUTTON_REC
85 #elif (CONFIG_KEYPAD == GIGABEAT_PAD)
86 #define SETTINGS_RESET BUTTON_A
87 #endif
89 #if CONFIG_TUNER
90 #include "radio.h"
91 #endif
92 #ifdef HAVE_MMC
93 #include "ata_mmc.h"
94 #endif
96 #ifdef HAVE_REMOTE_LCD
97 #include "lcd-remote.h"
98 #endif
100 #if CONFIG_USBOTG == USBOTG_ISP1362
101 #include "isp1362.h"
102 #endif
104 #if CONFIG_USBOTG == USBOTG_M5636
105 #include "m5636.h"
106 #endif
108 #include "cuesheet.h"
110 #ifdef SIMULATOR
111 #include "system-sdl.h"
112 #endif
114 /*#define AUTOROCK*/ /* define this to check for "autostart.rock" on boot */
116 const char appsversion[]=APPSVERSION;
118 static void init(void);
120 #ifdef SIMULATOR
121 void app_main(void)
122 #else
123 static void app_main(void)
124 #endif
126 int i;
127 init();
128 FOR_NB_SCREENS(i)
130 screens[i].clear_display();
131 screens[i].update();
133 tree_gui_init();
134 #ifdef HAVE_TOUCHSCREEN
135 touchscreen_set_mode(TOUCHSCREEN_BUTTON);
136 #endif
137 root_menu();
140 static int init_dircache(bool preinit)
142 #ifdef HAVE_DIRCACHE
143 int result = 0;
144 bool clear = false;
146 if (preinit)
147 dircache_init();
149 if (!global_settings.dircache)
150 return 0;
152 # ifdef HAVE_EEPROM_SETTINGS
153 if (firmware_settings.initialized && firmware_settings.disk_clean
154 && preinit)
156 result = dircache_load();
158 if (result < 0)
160 firmware_settings.disk_clean = false;
161 if (global_status.dircache_size <= 0)
163 /* This will be in default language, settings are not
164 applied yet. Not really any easy way to fix that. */
165 splash(0, str(LANG_SCANNING_DISK));
166 clear = true;
169 dircache_build(global_status.dircache_size);
172 else
173 # endif
175 if (preinit)
176 return -1;
178 if (!dircache_is_enabled()
179 && !dircache_is_initializing())
181 if (global_status.dircache_size <= 0)
183 splash(0, str(LANG_SCANNING_DISK));
184 clear = true;
186 result = dircache_build(global_status.dircache_size);
189 if (result < 0)
191 /* Initialization of dircache failed. Manual action is
192 * necessary to enable dircache again.
194 splashf(0, "Dircache failed, disabled. Result: %d", result);
195 global_settings.dircache = false;
200 if (clear)
202 backlight_on();
203 show_logo();
204 global_status.dircache_size = dircache_get_cache_size();
205 status_save();
208 return result;
209 #else
210 (void)preinit;
211 return 0;
212 #endif
215 #ifdef HAVE_TAGCACHE
216 static void init_tagcache(void)
218 bool clear = false;
220 tagcache_init();
222 while (!tagcache_is_initialized())
224 #ifdef HAVE_LCD_CHARCELLS
225 char buf[32];
226 #endif
227 int ret = tagcache_get_commit_step();
229 if (ret > 0)
231 #if CONFIG_CODEC == SWCODEC
232 /* hwcodec can't use voice here, as the database commit
233 * uses the audio buffer. */
234 static long talked_tick = 0;
235 if(global_settings.talk_menu
236 && (talked_tick == 0
237 || TIME_AFTER(current_tick, talked_tick+7*HZ)))
239 talked_tick = current_tick;
240 talk_id(LANG_TAGCACHE_INIT, false);
241 talk_number(ret, true);
242 talk_id(VOICE_OF, true);
243 talk_number(tagcache_get_max_commit_step(), true);
245 #endif
246 #ifdef HAVE_LCD_BITMAP
247 splashf(0, "%s [%d/%d]",
248 str(LANG_TAGCACHE_INIT), ret,
249 tagcache_get_max_commit_step());
250 #else
251 lcd_double_height(false);
252 snprintf(buf, sizeof(buf), " DB [%d/%d]", ret,
253 tagcache_get_max_commit_step());
254 lcd_puts(0, 1, buf);
255 lcd_update();
256 #endif
257 clear = true;
259 sleep(HZ/4);
261 tagtree_init();
263 if (clear)
265 backlight_on();
266 show_logo();
269 #endif
271 #ifdef SIMULATOR
273 static void init(void)
275 kernel_init();
276 buffer_init();
277 enable_irq();
278 lcd_init();
279 #ifdef HAVE_REMOTE_LCD
280 lcd_remote_init();
281 #endif
282 font_init();
283 show_logo();
284 button_init();
285 backlight_init();
286 lang_init();
287 #ifdef DEBUG
288 debug_init();
289 #endif
290 /* Must be done before any code uses the multi-screen APi */
291 gui_syncstatusbar_init(&statusbars);
292 ata_init();
293 settings_reset();
294 settings_load(SETTINGS_ALL);
295 gui_sync_wps_init();
296 settings_apply(true);
297 init_dircache(true);
298 init_dircache(false);
299 #ifdef HAVE_TAGCACHE
300 init_tagcache();
301 #endif
302 sleep(HZ/2);
303 tree_mem_init();
304 filetype_init();
305 playlist_init();
307 #if CONFIG_CODEC != SWCODEC
308 mp3_init( global_settings.volume,
309 global_settings.bass,
310 global_settings.treble,
311 global_settings.balance,
312 global_settings.loudness,
313 global_settings.avc,
314 global_settings.channel_config,
315 global_settings.stereo_width,
316 global_settings.mdb_strength,
317 global_settings.mdb_harmonics,
318 global_settings.mdb_center,
319 global_settings.mdb_shape,
320 global_settings.mdb_enable,
321 global_settings.superbass);
323 /* audio_init must to know the size of voice buffer so init voice first */
324 talk_init();
325 #endif /* CONFIG_CODEC != SWCODEC */
327 scrobbler_init();
328 cuesheet_init();
330 audio_init();
331 button_clear_queue(); /* Empty the keyboard buffer */
334 #else
336 static void init(void)
338 int rc;
339 bool mounted = false;
340 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
341 /* if nobody initialized ATA before, I consider this a cold start */
342 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
343 #endif
345 system_init();
346 kernel_init();
348 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
349 set_cpu_frequency(CPUFREQ_NORMAL);
350 #ifdef CPU_COLDFIRE
351 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
352 #endif
353 cpu_boost(true);
354 #endif
356 buffer_init();
358 settings_reset();
360 i2c_init();
362 power_init();
364 enable_irq();
365 #ifdef CPU_ARM
366 enable_fiq();
367 #endif
368 lcd_init();
369 #ifdef HAVE_REMOTE_LCD
370 lcd_remote_init();
371 #endif
372 font_init();
374 show_logo();
375 lang_init();
377 #ifdef DEBUG
378 debug_init();
379 #else
380 #if !defined(HAVE_FMADC) && !defined(HAVE_MMC)
381 serial_setup();
382 #endif
383 #endif
385 #if CONFIG_RTC
386 rtc_init();
387 #endif
388 #ifdef HAVE_RTC_RAM
389 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
390 #endif
392 adc_init();
394 usb_init();
395 #if CONFIG_USBOTG == USBOTG_ISP1362
396 isp1362_init();
397 #elif CONFIG_USBOTG == USBOTG_M5636
398 m5636_init();
399 #endif
401 backlight_init();
403 button_init();
405 powermgmt_init();
407 #if CONFIG_TUNER
408 radio_init();
409 #endif
411 /* Must be done before any code uses the multi-screen APi */
412 gui_syncstatusbar_init(&statusbars);
414 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
415 if (coldstart && charger_inserted()
416 && !global_settings.car_adapter_mode
417 #ifdef ATA_POWER_PLAYERSTYLE
418 && !ide_powered() /* relies on probing result from bootloader */
419 #endif
422 rc = charging_screen(); /* display a "charging" screen */
423 if (rc == 1) /* charger removed */
424 power_off();
425 /* "On" pressed or USB connected: proceed */
426 show_logo(); /* again, to provide better visual feedback */
428 #endif
430 rc = ata_init();
431 if(rc)
433 #ifdef HAVE_LCD_BITMAP
434 char str[32];
435 lcd_clear_display();
436 snprintf(str, 31, "ATA error: %d", rc);
437 lcd_puts(0, 1, str);
438 lcd_puts(0, 3, "Press ON to debug");
439 lcd_update();
440 while(!(button_get(true) & BUTTON_REL)); /*DO NOT CHANGE TO ACTION SYSTEM */
441 dbg_ports();
442 #endif
443 panicf("ata: %d", rc);
446 #ifdef HAVE_EEPROM_SETTINGS
447 eeprom_settings_init();
448 #endif
450 usb_start_monitoring();
451 #ifndef HAVE_USBSTACK
452 while (usb_detect() == USB_INSERTED)
454 #ifdef HAVE_EEPROM_SETTINGS
455 firmware_settings.disk_clean = false;
456 #endif
457 /* enter USB mode early, before trying to mount */
458 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
459 #ifdef HAVE_MMC
460 if (!mmc_touched() ||
461 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
462 #endif
464 usb_screen();
465 mounted = true; /* mounting done @ end of USB mode */
467 #ifdef HAVE_USB_POWER
468 if (usb_powered()) /* avoid deadlock */
469 break;
470 #endif
472 #endif
474 if (!mounted)
476 rc = disk_mount_all();
477 if (rc<=0)
479 lcd_clear_display();
480 lcd_puts(0, 0, "No partition");
481 lcd_puts(0, 1, "found.");
482 #ifdef HAVE_LCD_BITMAP
483 lcd_puts(0, 2, "Insert USB cable");
484 lcd_puts(0, 3, "and fix it.");
485 #endif
486 lcd_update();
488 while(button_get(true) != SYS_USB_CONNECTED) {};
489 usb_screen();
490 system_reboot();
494 #if defined(SETTINGS_RESET) || (CONFIG_KEYPAD == IPOD_4G_PAD) || \
495 (CONFIG_KEYPAD == IRIVER_H10_PAD)
496 #ifdef SETTINGS_RESET
497 /* Reset settings if holding the reset button. (Rec on Archos,
498 A on Gigabeat) */
499 if ((button_status() & SETTINGS_RESET) == SETTINGS_RESET)
500 #else
501 /* Reset settings if the hold button is turned on */
502 if (button_hold())
503 #endif
505 splash(HZ*2, str(LANG_RESET_DONE_CLEAR));
506 settings_reset();
508 else
509 #endif
510 settings_load(SETTINGS_ALL);
512 if (init_dircache(true) < 0)
514 #ifdef HAVE_TAGCACHE
515 remove(TAGCACHE_STATEFILE);
516 #endif
519 gui_sync_wps_init();
520 settings_apply(true);
521 init_dircache(false);
522 #ifdef HAVE_TAGCACHE
523 init_tagcache();
524 #endif
526 #ifdef HAVE_EEPROM_SETTINGS
527 if (firmware_settings.initialized)
529 /* In case we crash. */
530 firmware_settings.disk_clean = false;
531 eeprom_settings_store();
533 #endif
534 status_init();
535 playlist_init();
536 tree_mem_init();
537 filetype_init();
538 scrobbler_init();
539 cuesheet_init();
541 #if CONFIG_CODEC != SWCODEC
542 /* No buffer allocation (see buffer.c) may take place after the call to
543 audio_init() since the mpeg thread takes the rest of the buffer space */
544 mp3_init( global_settings.volume,
545 global_settings.bass,
546 global_settings.treble,
547 global_settings.balance,
548 global_settings.loudness,
549 global_settings.avc,
550 global_settings.channel_config,
551 global_settings.stereo_width,
552 global_settings.mdb_strength,
553 global_settings.mdb_harmonics,
554 global_settings.mdb_center,
555 global_settings.mdb_shape,
556 global_settings.mdb_enable,
557 global_settings.superbass);
559 /* audio_init must to know the size of voice buffer so init voice first */
560 talk_init();
561 #endif /* CONFIG_CODEC != SWCODEC */
563 audio_init();
565 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
566 pcm_rec_init();
567 #endif
569 /* runtime database has to be initialized after audio_init() */
570 cpu_boost(false);
572 #ifdef AUTOROCK
574 static const char filename[] = PLUGIN_APPS_DIR "/autostart.rock";
576 if(file_exists(filename)) /* no complaint if it doesn't exist */
578 plugin_load((char*)filename, NULL); /* start if it does */
581 #endif /* #ifdef AUTOROCK */
583 #if CONFIG_CHARGING
584 car_adapter_mode_init();
585 #endif
586 #ifdef HAVE_ACCESSORY_SUPPLY
587 accessory_supply_set(global_settings.accessory_supply);
588 #endif
591 #ifdef CPU_PP
592 void cop_main(void)
594 /* This is the entry point for the coprocessor
595 Anyone not running an upgraded bootloader will never reach this point,
596 so it should not be assumed that the coprocessor be usable even on
597 platforms which support it.
599 A kernel thread is initially setup on the coprocessor and immediately
600 destroyed for purposes of continuity. The cop sits idle until at least
601 one thread exists on it. */
603 /* 3G doesn't have Rolo or dual core support yet */
604 #if NUM_CORES > 1
605 system_init();
606 kernel_init();
607 /* This should never be reached */
608 #endif
609 while(1) {
610 sleep_core(COP);
613 #endif /* CPU_PP */
615 int main(void)
617 app_main();
619 while(1) {
620 #if (CONFIG_LED == LED_REAL)
621 led(true); sleep(HZ/10);
622 led(false); sleep(HZ/10);
623 #endif
625 return 0;
627 #endif