Greek language update by Ioannis Koutoulakis - needs an increase of the language...
[Rockbox.git] / apps / main.c
blob4249fd0b8bef1c0316448dadeeea838f64e83499
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by 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 "disk.h"
23 #include "fat.h"
24 #include "lcd.h"
25 #include "rtc.h"
26 #include "debug.h"
27 #include "led.h"
28 #include "kernel.h"
29 #include "button.h"
30 #include "tree.h"
31 #include "panic.h"
32 #include "menu.h"
33 #include "system.h"
34 #include "usb.h"
35 #include "powermgmt.h"
36 #include "adc.h"
37 #include "i2c.h"
38 #ifndef DEBUG
39 #include "serial.h"
40 #endif
41 #include "audio.h"
42 #include "mp3_playback.h"
43 #include "thread.h"
44 #include "settings.h"
45 #include "backlight.h"
46 #include "status.h"
47 #include "debug_menu.h"
48 #include "version.h"
49 #include "sprintf.h"
50 #include "font.h"
51 #include "language.h"
52 #include "gwps.h"
53 #include "playlist.h"
54 #include "buffer.h"
55 #include "rolo.h"
56 #include "screens.h"
57 #include "power.h"
58 #include "talk.h"
59 #include "plugin.h"
60 #include "misc.h"
61 #include "database.h"
62 #include "dircache.h"
63 #ifdef HAVE_TAGCACHE
64 #include "tagcache.h"
65 #include "tagtree.h"
66 #endif
67 #include "lang.h"
68 #include "string.h"
69 #include "splash.h"
70 #include "eeprom_settings.h"
71 #include "scrobbler.h"
73 #if (CONFIG_CODEC == SWCODEC)
74 #include "playback.h"
75 #include "pcmbuf.h"
76 #else
77 #define pcmbuf_init()
78 #endif
79 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
80 #include "pcm_record.h"
81 #define SETTINGS_RESET BUTTON_REC
82 #endif
84 #ifdef CONFIG_TUNER
85 #include "radio.h"
86 #endif
87 #ifdef HAVE_MMC
88 #include "ata_mmc.h"
89 #endif
91 #ifdef HAVE_REMOTE_LCD
92 #include "lcd-remote.h"
93 #endif
95 #if CONFIG_USBOTG == USBOTG_ISP1362
96 #include "isp1362.h"
97 #endif
99 #if CONFIG_USBOTG == USBOTG_M5636
100 #include "m5636.h"
101 #endif
103 /*#define AUTOROCK*/ /* define this to check for "autostart.rock" on boot */
105 const char appsversion[]=APPSVERSION;
107 void init(void);
109 void app_main(void)
111 init();
112 browse_root();
115 int init_dircache(bool preinit)
117 #ifdef HAVE_DIRCACHE
118 int result = 0;
119 bool clear = false;
121 if (preinit)
122 dircache_init();
124 if (global_settings.dircache)
126 # ifdef HAVE_EEPROM_SETTINGS
127 if (firmware_settings.initialized && firmware_settings.disk_clean
128 && preinit)
130 result = dircache_load(DIRCACHE_FILE);
131 remove(DIRCACHE_FILE);
132 if (result < 0)
134 firmware_settings.disk_clean = false;
135 if (global_settings.dircache_size >= 0)
136 dircache_build(global_settings.dircache_size);
139 return result;
141 # endif
143 if (preinit)
144 return -1;
146 if (!dircache_is_enabled()
147 && !dircache_is_initializing())
149 if (global_settings.dircache_size <= 0)
151 gui_syncsplash(0, true, str(LANG_DIRCACHE_BUILDING));
152 clear = true;
154 result = dircache_build(global_settings.dircache_size);
157 if (result < 0)
158 gui_syncsplash(0, true, "Failed! Result: %d", result);
160 if (clear)
162 backlight_on();
163 show_logo();
167 return result;
168 #else
169 (void)preinit;
170 return 0;
171 #endif
174 #ifdef HAVE_TAGCACHE
175 void init_tagcache(void)
177 bool clear = false;
179 tagcache_init();
181 while (!tagcache_is_initialized())
183 #ifdef HAVE_LCD_CHARCELLS
184 char buf[32];
185 #endif
186 int ret = tagcache_get_commit_step();
188 if (ret > 0)
190 #ifdef HAVE_LCD_BITMAP
191 gui_syncsplash(0, true, "%s [%d/%d]",
192 str(LANG_TAGCACHE_INIT), ret, 7);
193 #else
194 lcd_double_height(false);
195 snprintf(buf, sizeof(buf), " TC [%d/%d]", ret, 7);
196 lcd_puts(0, 1, buf);
197 #endif
198 clear = true;
200 sleep(HZ/4);
202 tagtree_init();
204 if (clear)
206 backlight_on();
207 show_logo();
210 #endif
212 #ifdef SIMULATOR
214 void init(void)
216 init_threads();
217 buffer_init();
218 lcd_init();
219 #ifdef HAVE_REMOTE_LCD
220 lcd_remote_init();
221 #endif
222 font_init();
223 show_logo();
224 button_init();
225 backlight_init();
226 lang_init();
227 /* Must be done before any code uses the multi-screen APi */
228 screen_access_init();
229 gui_syncstatusbar_init(&statusbars);
230 settings_reset();
231 settings_calc_config_sector();
232 settings_load(SETTINGS_ALL);
233 gui_sync_wps_init();
234 settings_apply();
235 init_dircache(true);
236 init_dircache(false);
237 #ifdef HAVE_TAGCACHE
238 init_tagcache();
239 #endif
240 sleep(HZ/2);
241 tree_init();
242 playlist_init();
243 mp3_init( global_settings.volume,
244 global_settings.bass,
245 global_settings.treble,
246 global_settings.balance,
247 global_settings.loudness,
248 global_settings.avc,
249 global_settings.channel_config,
250 global_settings.stereo_width,
251 global_settings.mdb_strength,
252 global_settings.mdb_harmonics,
253 global_settings.mdb_center,
254 global_settings.mdb_shape,
255 global_settings.mdb_enable,
256 global_settings.superbass);
258 #if CONFIG_CODEC == SWCODEC
259 audio_preinit();
260 #endif
262 scrobbler_init();
264 /* audio_init must to know the size of voice buffer so init voice first */
265 #if CONFIG_CODEC == SWCODEC
266 talk_init();
267 #endif
269 audio_init();
270 button_clear_queue(); /* Empty the keyboard buffer */
273 #else
275 void init(void)
277 int rc;
278 bool mounted = false;
279 #if defined(CONFIG_CHARGING) && (CONFIG_CPU == SH7034)
280 /* if nobody initialized ATA before, I consider this a cold start */
281 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
282 #endif
283 system_init();
284 kernel_init();
286 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
287 set_cpu_frequency(CPUFREQ_NORMAL);
288 cpu_boost_id(true, CPUBOOSTID_MAININIT);
289 #endif
291 buffer_init();
293 settings_reset();
295 power_init();
297 set_irq_level(0);
298 lcd_init();
299 #ifdef HAVE_REMOTE_LCD
300 lcd_remote_init();
301 #endif
302 font_init();
303 show_logo();
304 lang_init();
306 #ifdef DEBUG
307 debug_init();
308 #else
309 #ifndef HAVE_MMC /* FIXME: This is also necessary for debug builds
310 * (do debug builds on the Ondio make sense?) */
311 serial_setup();
312 #endif
313 #endif
315 i2c_init();
317 #ifdef CONFIG_RTC
318 rtc_init();
319 #endif
320 #ifdef HAVE_RTC_RAM
321 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
322 #endif
324 adc_init();
326 usb_init();
327 #if CONFIG_USBOTG == USBOTG_ISP1362
328 isp1362_init();
329 #elif CONFIG_USBOTG == USBOTG_M5636
330 m5636_init();
331 #endif
333 backlight_init();
335 button_init();
337 powermgmt_init();
339 #if CONFIG_CODEC == SWCODEC
340 audio_preinit();
341 #endif
343 #ifdef CONFIG_TUNER
344 radio_init();
345 #endif
347 /* Must be done before any code uses the multi-screen APi */
348 screen_access_init();
349 gui_syncstatusbar_init(&statusbars);
351 #if defined(CONFIG_CHARGING) && (CONFIG_CPU == SH7034)
352 if (coldstart && charger_inserted()
353 && !global_settings.car_adapter_mode
354 #ifdef ATA_POWER_PLAYERSTYLE
355 && !ide_powered() /* relies on probing result from bootloader */
356 #endif
359 rc = charging_screen(); /* display a "charging" screen */
360 if (rc == 1) /* charger removed */
361 power_off();
362 /* "On" pressed or USB connected: proceed */
363 show_logo(); /* again, to provide better visual feedback */
365 #endif
367 rc = ata_init();
368 if(rc)
370 #ifdef HAVE_LCD_BITMAP
371 char str[32];
372 lcd_clear_display();
373 snprintf(str, 31, "ATA error: %d", rc);
374 lcd_puts(0, 1, str);
375 lcd_puts(0, 3, "Press ON to debug");
376 lcd_update();
377 while(!(button_get(true) & BUTTON_REL)); /*DO NOT CHANGE TO ACTION SYSTEM */
378 dbg_ports();
379 #endif
380 panicf("ata: %d", rc);
383 #ifdef HAVE_EEPROM_SETTINGS
384 eeprom_settings_init();
385 #endif
387 usb_start_monitoring();
388 while (usb_detect())
390 #ifdef HAVE_EEPROM_SETTINGS
391 firmware_settings.disk_clean = false;
392 #endif
393 /* enter USB mode early, before trying to mount */
394 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
395 #ifdef HAVE_MMC
396 if (!mmc_touched() || (mmc_remove_request() == SYS_MMC_EXTRACTED))
397 #endif
399 usb_screen();
400 mounted = true; /* mounting done @ end of USB mode */
402 #ifdef HAVE_USB_POWER
403 if (usb_powered()) /* avoid deadlock */
404 break;
405 #endif
408 if (!mounted)
410 rc = disk_mount_all();
411 if (rc<=0)
413 lcd_clear_display();
414 lcd_puts(0, 0, "No partition");
415 lcd_puts(0, 1, "found.");
416 #ifdef HAVE_LCD_BITMAP
417 lcd_puts(0, 2, "Insert USB cable");
418 lcd_puts(0, 3, "and fix it.");
419 lcd_update();
420 #endif
421 while(button_get(true) != SYS_USB_CONNECTED) {};
422 usb_screen();
423 system_reboot();
427 settings_calc_config_sector();
429 #if defined(SETTINGS_RESET) || (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IRIVER_H10_PAD)
430 #ifdef SETTINGS_RESET
431 /* Reset settings if holding the rec button. */
432 if ((button_status() & SETTINGS_RESET) == SETTINGS_RESET)
433 #else
434 /* Reset settings if the hold button is turned on */
435 if (button_hold())
436 #endif
438 gui_syncsplash(HZ*2, true, str(LANG_RESET_DONE_CLEAR));
439 settings_reset();
441 else
442 #endif
443 settings_load(SETTINGS_ALL);
445 if (init_dircache(true) < 0)
447 #ifdef HAVE_TAGCACHE
448 remove(TAGCACHE_STATEFILE);
449 #endif
452 gui_sync_wps_init();
453 settings_apply();
454 init_dircache(false);
455 #ifdef HAVE_TAGCACHE
456 init_tagcache();
457 #endif
459 #ifdef HAVE_EEPROM_SETTINGS
460 if (firmware_settings.initialized)
462 /* In case we crash. */
463 firmware_settings.disk_clean = false;
464 eeprom_settings_store();
466 #endif
467 status_init();
468 playlist_init();
469 tree_init();
470 scrobbler_init();
472 /* No buffer allocation (see buffer.c) may take place after the call to
473 audio_init() since the mpeg thread takes the rest of the buffer space */
474 mp3_init( global_settings.volume,
475 global_settings.bass,
476 global_settings.treble,
477 global_settings.balance,
478 global_settings.loudness,
479 global_settings.avc,
480 global_settings.channel_config,
481 global_settings.stereo_width,
482 global_settings.mdb_strength,
483 global_settings.mdb_harmonics,
484 global_settings.mdb_center,
485 global_settings.mdb_shape,
486 global_settings.mdb_enable,
487 global_settings.superbass);
489 /* audio_init must to know the size of voice buffer so init voice first */
490 talk_init();
492 audio_init();
493 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
494 pcm_rec_init();
495 #endif
497 /* runtime database has to be initialized after audio_init() */
498 cpu_boost_id(false, CPUBOOSTID_MAININIT);
500 #ifdef AUTOROCK
502 int fd;
503 static const char filename[] = PLUGIN_DIR "/autostart.rock";
505 fd = open(filename, O_RDONLY);
506 if(fd >= 0) /* no complaint if it doesn't exist */
508 close(fd);
509 plugin_load((char*)filename, NULL); /* start if it does */
512 #endif /* #ifdef AUTOROCK */
514 #ifdef CONFIG_CHARGING
515 car_adapter_mode_init();
516 #endif
519 #ifdef CPU_PP
520 void cop_main(void)
522 /* This is the entry point for the coprocessor
523 Anyone not running an upgraded bootloader will never reach this point,
524 so it should not be assumed that the coprocessor be usable even on
525 platforms which support it.
527 At present all we do is send the COP to sleep if anything wakes it. */
528 while(1) {
529 COP_CTL = PROC_SLEEP;
532 #endif
534 int main(void)
536 app_main();
538 while(1) {
539 #if CONFIG_LED == LED_REAL
540 led(true); sleep(HZ/10);
541 led(false); sleep(HZ/10);
542 #endif
544 return 0;
546 #endif