Implement rebuffering for seeking outside of buffered data boudaries
[Rockbox.git] / apps / main.c
blob366fed95851c3201125acbda284415143fb1d49e
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 #ifdef HAVE_USBSTACK
103 #include "usbstack.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 #include "cuesheet.h"
116 /*#define AUTOROCK*/ /* define this to check for "autostart.rock" on boot */
118 const char appsversion[]=APPSVERSION;
120 static void init(void);
122 #ifdef SIMULATOR
123 void app_main(void)
124 #else
125 static void app_main(void)
126 #endif
128 init();
129 tree_gui_init();
130 root_menu();
133 static int init_dircache(bool preinit)
135 #ifdef HAVE_DIRCACHE
136 int result = 0;
137 bool clear = false;
139 if (preinit)
140 dircache_init();
142 if (!global_settings.dircache)
143 return 0;
145 # ifdef HAVE_EEPROM_SETTINGS
146 if (firmware_settings.initialized && firmware_settings.disk_clean
147 && preinit)
149 result = dircache_load();
151 if (result < 0)
153 firmware_settings.disk_clean = false;
154 if (global_status.dircache_size <= 0)
156 /* This will be in default language, settings are not
157 applied yet. Not really any easy way to fix that. */
158 gui_syncsplash(0, str(LANG_SCANNING_DISK));
159 clear = true;
162 dircache_build(global_status.dircache_size);
165 else
166 # endif
168 if (preinit)
169 return -1;
171 if (!dircache_is_enabled()
172 && !dircache_is_initializing())
174 if (global_status.dircache_size <= 0)
176 gui_syncsplash(0, str(LANG_SCANNING_DISK));
177 clear = true;
179 result = dircache_build(global_status.dircache_size);
182 if (result < 0)
184 /* Initialization of dircache failed. Manual action is
185 * necessary to enable dircache again.
187 gui_syncsplash(0, "Dircache failed, disabled. Result: %d", result);
188 global_settings.dircache = false;
193 if (clear)
195 backlight_on();
196 show_logo();
197 global_status.dircache_size = dircache_get_cache_size();
198 status_save();
201 return result;
202 #else
203 (void)preinit;
204 return 0;
205 #endif
208 #ifdef HAVE_TAGCACHE
209 static void init_tagcache(void)
211 bool clear = false;
213 tagcache_init();
215 while (!tagcache_is_initialized())
217 #ifdef HAVE_LCD_CHARCELLS
218 char buf[32];
219 #endif
220 int ret = tagcache_get_commit_step();
222 if (ret > 0)
224 #if CONFIG_CODEC == SWCODEC
225 /* hwcodec can't use voice here, as the database commit
226 * uses the audio buffer. */
227 static long talked_tick = 0;
228 if(global_settings.talk_menu
229 && (talked_tick == 0
230 || TIME_AFTER(current_tick, talked_tick+7*HZ)))
232 talked_tick = current_tick;
233 talk_id(LANG_TAGCACHE_INIT, false);
234 talk_number(ret, true);
235 talk_id(VOICE_OF, true);
236 talk_number(tagcache_get_max_commit_step(), true);
238 #endif
239 #ifdef HAVE_LCD_BITMAP
240 gui_syncsplash(0, "%s [%d/%d]",
241 str(LANG_TAGCACHE_INIT), ret,
242 tagcache_get_max_commit_step());
243 #else
244 lcd_double_height(false);
245 snprintf(buf, sizeof(buf), " DB [%d/%d]", ret,
246 tagcache_get_max_commit_step());
247 lcd_puts(0, 1, buf);
248 lcd_update();
249 #endif
250 clear = true;
252 sleep(HZ/4);
254 tagtree_init();
256 if (clear)
258 backlight_on();
259 show_logo();
262 #endif
264 #ifdef SIMULATOR
266 static void init(void)
268 init_threads();
269 buffer_init();
270 lcd_init();
271 #ifdef HAVE_REMOTE_LCD
272 lcd_remote_init();
273 #endif
274 font_init();
275 show_logo();
276 button_init();
277 backlight_init();
278 lang_init();
279 #ifdef DEBUG
280 debug_init();
281 #endif
282 /* Must be done before any code uses the multi-screen APi */
283 screen_access_init();
284 gui_syncstatusbar_init(&statusbars);
285 ata_init();
286 settings_reset();
287 settings_load(SETTINGS_ALL);
288 gui_sync_wps_init();
289 settings_apply();
290 init_dircache(true);
291 init_dircache(false);
292 #ifdef HAVE_TAGCACHE
293 init_tagcache();
294 #endif
295 sleep(HZ/2);
296 tree_mem_init();
297 filetype_init();
298 playlist_init();
300 #if CONFIG_CODEC != SWCODEC
301 mp3_init( global_settings.volume,
302 global_settings.bass,
303 global_settings.treble,
304 global_settings.balance,
305 global_settings.loudness,
306 global_settings.avc,
307 global_settings.channel_config,
308 global_settings.stereo_width,
309 global_settings.mdb_strength,
310 global_settings.mdb_harmonics,
311 global_settings.mdb_center,
312 global_settings.mdb_shape,
313 global_settings.mdb_enable,
314 global_settings.superbass);
316 /* audio_init must to know the size of voice buffer so init voice first */
317 talk_init();
318 #endif /* CONFIG_CODEC != SWCODEC */
320 scrobbler_init();
321 cuesheet_init();
323 audio_init();
324 button_clear_queue(); /* Empty the keyboard buffer */
327 #else
329 static void init(void)
331 int rc;
332 bool mounted = false;
333 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
334 /* if nobody initialized ATA before, I consider this a cold start */
335 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
336 #endif
338 system_init();
339 kernel_init();
341 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
342 set_cpu_frequency(CPUFREQ_NORMAL);
343 #ifdef CPU_COLDFIRE
344 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
345 #endif
346 cpu_boost(true);
347 #endif
349 buffer_init();
351 settings_reset();
353 power_init();
355 set_irq_level(0);
356 #ifdef CPU_ARM
357 set_fiq_status(FIQ_ENABLED);
358 #endif
359 lcd_init();
360 #ifdef HAVE_REMOTE_LCD
361 lcd_remote_init();
362 #endif
363 font_init();
365 show_logo();
366 lang_init();
368 #ifdef DEBUG
369 debug_init();
370 #else
371 #if !defined(HAVE_FMADC) && !defined(HAVE_MMC)
372 serial_setup();
373 #endif
374 #endif
376 i2c_init();
378 #if CONFIG_RTC
379 rtc_init();
380 #endif
381 #ifdef HAVE_RTC_RAM
382 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
383 #endif
385 adc_init();
387 #ifdef HAVE_USBSTACK
388 usb_stack_init();
389 #endif
390 usb_init();
391 #if CONFIG_USBOTG == USBOTG_ISP1362
392 isp1362_init();
393 #elif CONFIG_USBOTG == USBOTG_M5636
394 m5636_init();
395 #endif
397 backlight_init();
399 button_init();
401 powermgmt_init();
403 #if CONFIG_TUNER
404 radio_init();
405 #endif
407 /* Must be done before any code uses the multi-screen APi */
408 screen_access_init();
409 gui_syncstatusbar_init(&statusbars);
411 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
412 if (coldstart && charger_inserted()
413 && !global_settings.car_adapter_mode
414 #ifdef ATA_POWER_PLAYERSTYLE
415 && !ide_powered() /* relies on probing result from bootloader */
416 #endif
419 rc = charging_screen(); /* display a "charging" screen */
420 if (rc == 1) /* charger removed */
421 power_off();
422 /* "On" pressed or USB connected: proceed */
423 show_logo(); /* again, to provide better visual feedback */
425 #endif
427 ata_idle_notify_init();
428 rc = ata_init();
429 if(rc)
431 #ifdef HAVE_LCD_BITMAP
432 char str[32];
433 lcd_clear_display();
434 snprintf(str, 31, "ATA error: %d", rc);
435 lcd_puts(0, 1, str);
436 lcd_puts(0, 3, "Press ON to debug");
437 lcd_update();
438 while(!(button_get(true) & BUTTON_REL)); /*DO NOT CHANGE TO ACTION SYSTEM */
439 dbg_ports();
440 #endif
441 panicf("ata: %d", rc);
444 #ifdef HAVE_EEPROM_SETTINGS
445 eeprom_settings_init();
446 #endif
448 usb_start_monitoring();
449 while (usb_detect() == USB_INSERTED)
451 #ifdef HAVE_EEPROM_SETTINGS
452 firmware_settings.disk_clean = false;
453 #endif
454 /* enter USB mode early, before trying to mount */
455 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
456 #ifdef HAVE_MMC
457 if (!mmc_touched() ||
458 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
459 #endif
461 usb_screen();
462 mounted = true; /* mounting done @ end of USB mode */
464 #ifdef HAVE_USB_POWER
465 if (usb_powered()) /* avoid deadlock */
466 break;
467 #endif
470 if (!mounted)
472 rc = disk_mount_all();
473 if (rc<=0)
475 lcd_clear_display();
476 lcd_puts(0, 0, "No partition");
477 lcd_puts(0, 1, "found.");
478 #ifdef HAVE_LCD_BITMAP
479 lcd_puts(0, 2, "Insert USB cable");
480 lcd_puts(0, 3, "and fix it.");
481 #endif
482 lcd_update();
484 while(button_get(true) != SYS_USB_CONNECTED) {};
485 usb_screen();
486 system_reboot();
490 #if defined(SETTINGS_RESET) || (CONFIG_KEYPAD == IPOD_4G_PAD) || \
491 (CONFIG_KEYPAD == IRIVER_H10_PAD)
492 #ifdef SETTINGS_RESET
493 /* Reset settings if holding the reset button. (Rec on Archos,
494 A on Gigabeat) */
495 if ((button_status() & SETTINGS_RESET) == SETTINGS_RESET)
496 #else
497 /* Reset settings if the hold button is turned on */
498 if (button_hold())
499 #endif
501 gui_syncsplash(HZ*2, str(LANG_RESET_DONE_CLEAR));
502 settings_reset();
504 else
505 #endif
506 settings_load(SETTINGS_ALL);
508 if (init_dircache(true) < 0)
510 #ifdef HAVE_TAGCACHE
511 remove(TAGCACHE_STATEFILE);
512 #endif
515 gui_sync_wps_init();
516 settings_apply();
517 init_dircache(false);
518 #ifdef HAVE_TAGCACHE
519 init_tagcache();
520 #endif
522 #ifdef HAVE_EEPROM_SETTINGS
523 if (firmware_settings.initialized)
525 /* In case we crash. */
526 firmware_settings.disk_clean = false;
527 eeprom_settings_store();
529 #endif
530 status_init();
531 playlist_init();
532 tree_mem_init();
533 filetype_init();
534 scrobbler_init();
535 cuesheet_init();
537 #if CONFIG_CODEC != SWCODEC
538 /* No buffer allocation (see buffer.c) may take place after the call to
539 audio_init() since the mpeg thread takes the rest of the buffer space */
540 mp3_init( global_settings.volume,
541 global_settings.bass,
542 global_settings.treble,
543 global_settings.balance,
544 global_settings.loudness,
545 global_settings.avc,
546 global_settings.channel_config,
547 global_settings.stereo_width,
548 global_settings.mdb_strength,
549 global_settings.mdb_harmonics,
550 global_settings.mdb_center,
551 global_settings.mdb_shape,
552 global_settings.mdb_enable,
553 global_settings.superbass);
555 /* audio_init must to know the size of voice buffer so init voice first */
556 talk_init();
557 #endif /* CONFIG_CODEC != SWCODEC */
559 audio_init();
561 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
562 pcm_rec_init();
563 #endif
565 /* runtime database has to be initialized after audio_init() */
566 cpu_boost(false);
568 #ifdef AUTOROCK
570 int fd;
571 static const char filename[] = PLUGIN_APPS_DIR "/autostart.rock";
573 fd = open(filename, O_RDONLY);
574 if(fd >= 0) /* no complaint if it doesn't exist */
576 close(fd);
577 plugin_load((char*)filename, NULL); /* start if it does */
580 #endif /* #ifdef AUTOROCK */
582 #if CONFIG_CHARGING
583 car_adapter_mode_init();
584 #endif
587 #ifdef CPU_PP
588 void cop_main(void)
590 /* This is the entry point for the coprocessor
591 Anyone not running an upgraded bootloader will never reach this point,
592 so it should not be assumed that the coprocessor be usable even on
593 platforms which support it.
595 A kernel thread is initially setup on the coprocessor and immediately
596 destroyed for purposes of continuity. The cop sits idle until at least
597 one thread exists on it. */
599 /* 3G doesn't have Rolo or dual core support yet */
600 #if NUM_CORES > 1
601 system_init();
602 kernel_init();
603 /* This should never be reached */
604 #endif
605 while(1) {
606 COP_CTL = PROC_SLEEP;
609 #endif /* CPU_PP */
611 int main(void)
613 app_main();
615 while(1) {
616 #if (CONFIG_LED == LED_REAL)
617 led(true); sleep(HZ/10);
618 led(false); sleep(HZ/10);
619 #endif
621 return 0;
623 #endif