w32: implement "system proxy" settings. This uses the IE setting by reading its value...
[Rockbox.git] / apps / main.c
bloba27998168c017ccc5a12cf1839e8d3b264b0d537
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(talk_menus_enabled()
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
337 #ifdef CPU_PP
338 COP_CTL = PROC_WAKE;
339 #endif
340 system_init();
341 kernel_init();
343 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
344 set_cpu_frequency(CPUFREQ_NORMAL);
345 #ifdef CPU_COLDFIRE
346 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
347 #endif
348 cpu_boost(true);
349 #endif
351 buffer_init();
353 settings_reset();
355 power_init();
357 set_irq_level(0);
358 lcd_init();
359 #ifdef HAVE_REMOTE_LCD
360 lcd_remote_init();
361 #endif
362 font_init();
364 show_logo();
365 lang_init();
367 #ifdef DEBUG
368 debug_init();
369 #else
370 #if !defined(HAVE_FMADC) && !defined(HAVE_MMC)
371 serial_setup();
372 #endif
373 #endif
375 i2c_init();
377 #if CONFIG_RTC
378 rtc_init();
379 #endif
380 #ifdef HAVE_RTC_RAM
381 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
382 #endif
384 adc_init();
386 #ifdef HAVE_USBSTACK
387 usb_stack_init();
388 #endif
389 usb_init();
390 #if CONFIG_USBOTG == USBOTG_ISP1362
391 isp1362_init();
392 #elif CONFIG_USBOTG == USBOTG_M5636
393 m5636_init();
394 #endif
396 backlight_init();
398 button_init();
400 powermgmt_init();
402 #if CONFIG_TUNER
403 radio_init();
404 #endif
406 /* Must be done before any code uses the multi-screen APi */
407 screen_access_init();
408 gui_syncstatusbar_init(&statusbars);
410 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
411 if (coldstart && charger_inserted()
412 && !global_settings.car_adapter_mode
413 #ifdef ATA_POWER_PLAYERSTYLE
414 && !ide_powered() /* relies on probing result from bootloader */
415 #endif
418 rc = charging_screen(); /* display a "charging" screen */
419 if (rc == 1) /* charger removed */
420 power_off();
421 /* "On" pressed or USB connected: proceed */
422 show_logo(); /* again, to provide better visual feedback */
424 #endif
426 ata_idle_notify_init();
427 rc = ata_init();
428 if(rc)
430 #ifdef HAVE_LCD_BITMAP
431 char str[32];
432 lcd_clear_display();
433 snprintf(str, 31, "ATA error: %d", rc);
434 lcd_puts(0, 1, str);
435 lcd_puts(0, 3, "Press ON to debug");
436 lcd_update();
437 while(!(button_get(true) & BUTTON_REL)); /*DO NOT CHANGE TO ACTION SYSTEM */
438 dbg_ports();
439 #endif
440 panicf("ata: %d", rc);
443 #ifdef HAVE_EEPROM_SETTINGS
444 eeprom_settings_init();
445 #endif
447 usb_start_monitoring();
448 while (usb_detect() == USB_INSERTED)
450 #ifdef HAVE_EEPROM_SETTINGS
451 firmware_settings.disk_clean = false;
452 #endif
453 /* enter USB mode early, before trying to mount */
454 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
455 #ifdef HAVE_MMC
456 if (!mmc_touched() ||
457 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
458 #endif
460 usb_screen();
461 mounted = true; /* mounting done @ end of USB mode */
463 #ifdef HAVE_USB_POWER
464 if (usb_powered()) /* avoid deadlock */
465 break;
466 #endif
469 if (!mounted)
471 rc = disk_mount_all();
472 if (rc<=0)
474 lcd_clear_display();
475 lcd_puts(0, 0, "No partition");
476 lcd_puts(0, 1, "found.");
477 #ifdef HAVE_LCD_BITMAP
478 lcd_puts(0, 2, "Insert USB cable");
479 lcd_puts(0, 3, "and fix it.");
480 #endif
481 lcd_update();
483 while(button_get(true) != SYS_USB_CONNECTED) {};
484 usb_screen();
485 system_reboot();
489 #if defined(SETTINGS_RESET) || (CONFIG_KEYPAD == IPOD_4G_PAD) || \
490 (CONFIG_KEYPAD == IRIVER_H10_PAD)
491 #ifdef SETTINGS_RESET
492 /* Reset settings if holding the reset button. (Rec on Archos,
493 A on Gigabeat) */
494 if ((button_status() & SETTINGS_RESET) == SETTINGS_RESET)
495 #else
496 /* Reset settings if the hold button is turned on */
497 if (button_hold())
498 #endif
500 gui_syncsplash(HZ*2, str(LANG_RESET_DONE_CLEAR));
501 settings_reset();
503 else
504 #endif
505 settings_load(SETTINGS_ALL);
507 if (init_dircache(true) < 0)
509 #ifdef HAVE_TAGCACHE
510 remove(TAGCACHE_STATEFILE);
511 #endif
514 gui_sync_wps_init();
515 settings_apply();
516 init_dircache(false);
517 #ifdef HAVE_TAGCACHE
518 init_tagcache();
519 #endif
521 #ifdef HAVE_EEPROM_SETTINGS
522 if (firmware_settings.initialized)
524 /* In case we crash. */
525 firmware_settings.disk_clean = false;
526 eeprom_settings_store();
528 #endif
529 status_init();
530 playlist_init();
531 tree_mem_init();
532 filetype_init();
533 scrobbler_init();
534 cuesheet_init();
536 #if CONFIG_CODEC != SWCODEC
537 /* No buffer allocation (see buffer.c) may take place after the call to
538 audio_init() since the mpeg thread takes the rest of the buffer space */
539 mp3_init( global_settings.volume,
540 global_settings.bass,
541 global_settings.treble,
542 global_settings.balance,
543 global_settings.loudness,
544 global_settings.avc,
545 global_settings.channel_config,
546 global_settings.stereo_width,
547 global_settings.mdb_strength,
548 global_settings.mdb_harmonics,
549 global_settings.mdb_center,
550 global_settings.mdb_shape,
551 global_settings.mdb_enable,
552 global_settings.superbass);
554 /* audio_init must to know the size of voice buffer so init voice first */
555 talk_init();
556 #endif /* CONFIG_CODEC != SWCODEC */
558 audio_init();
560 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
561 pcm_rec_init();
562 #endif
564 /* runtime database has to be initialized after audio_init() */
565 cpu_boost(false);
567 #ifdef AUTOROCK
569 int fd;
570 static const char filename[] = PLUGIN_APPS_DIR "/autostart.rock";
572 fd = open(filename, O_RDONLY);
573 if(fd >= 0) /* no complaint if it doesn't exist */
575 close(fd);
576 plugin_load((char*)filename, NULL); /* start if it does */
579 #endif /* #ifdef AUTOROCK */
581 #if CONFIG_CHARGING
582 car_adapter_mode_init();
583 #endif
586 #ifdef CPU_PP
587 void cop_main(void)
589 /* This is the entry point for the coprocessor
590 Anyone not running an upgraded bootloader will never reach this point,
591 so it should not be assumed that the coprocessor be usable even on
592 platforms which support it.
594 A kernel thread runs on the coprocessor which waits for other threads to be
595 added, and gracefully handles RoLo */
597 #if CONFIG_CPU == PP5002
598 /* 3G doesn't have Rolo or dual core support yet */
599 while(1) {
600 COP_CTL = PROC_SLEEP;
602 #else
603 extern volatile unsigned char cpu_message;
605 system_init();
606 kernel_init();
608 while(cpu_message != COP_REBOOT) {
609 sleep(HZ);
611 rolo_restart_cop();
612 #endif /* PP5002 */
614 #endif /* CPU_PP */
616 int main(void)
618 app_main();
620 while(1) {
621 #if (CONFIG_LED == LED_REAL)
622 led(true); sleep(HZ/10);
623 led(false); sleep(HZ/10);
624 #endif
626 return 0;
628 #endif