Enable replaygain for the new FLAC decoder
[Rockbox.git] / apps / main.c
blob296b3fc35185bcc569ad3f330309de4c59e4aeb3
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 "wps-display.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 #include "lang.h"
64 #include "string.h"
66 #if (CONFIG_CODEC == SWCODEC)
67 #include "pcmbuf.h"
68 #else
69 #define pcmbuf_init()
70 #endif
71 #if defined(IRIVER_H100_SERIES) && !defined(SIMULATOR)
72 #include "pcm_record.h"
73 #endif
75 #ifdef CONFIG_TUNER
76 #include "radio.h"
77 #endif
78 #ifdef HAVE_MMC
79 #include "ata_mmc.h"
80 #endif
82 #ifdef HAVE_REMOTE_LCD
83 #include "lcd-remote.h"
84 #endif
86 /*#define AUTOROCK*/ /* define this to check for "autostart.rock" on boot */
88 const char appsversion[]=APPSVERSION;
90 void init(void);
92 void app_main(void)
94 init();
95 browse_root();
98 #ifdef HAVE_DIRCACHE
99 void init_dircache(void)
101 int font_w, font_h;
103 dircache_init();
104 if (global_settings.dircache)
106 /* Print "Scanning disk..." to the display. */
107 lcd_getstringsize("A", &font_w, &font_h);
108 lcd_putsxy((LCD_WIDTH/2) - ((strlen(str(LANG_DIRCACHE_BUILDING))*font_w)/2),
109 LCD_HEIGHT-font_h*3, str(LANG_DIRCACHE_BUILDING));
110 lcd_update();
112 dircache_build(global_settings.dircache_size);
114 /* Clean the text when we are done. */
115 lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
116 lcd_fillrect(0, LCD_HEIGHT-font_h*3, LCD_WIDTH, font_h);
117 lcd_set_drawmode(DRMODE_SOLID);
118 lcd_update();
121 #else
122 # define init_dircache(...)
123 #endif
125 #ifdef SIMULATOR
127 void init(void)
129 init_threads();
130 buffer_init();
131 lcd_init();
132 #ifdef HAVE_REMOTE_LCD
133 lcd_remote_init();
134 #endif
135 font_init();
136 show_logo();
137 lang_init();
138 settings_reset();
139 settings_calc_config_sector();
140 settings_load(SETTINGS_ALL);
141 settings_apply();
142 init_dircache();
143 sleep(HZ/2);
144 tree_init();
145 playlist_init();
146 mp3_init( global_settings.volume,
147 global_settings.bass,
148 global_settings.treble,
149 global_settings.balance,
150 global_settings.loudness,
151 global_settings.avc,
152 global_settings.channel_config,
153 global_settings.stereo_width,
154 global_settings.mdb_strength,
155 global_settings.mdb_harmonics,
156 global_settings.mdb_center,
157 global_settings.mdb_shape,
158 global_settings.mdb_enable,
159 global_settings.superbass);
160 audio_init();
161 button_clear_queue(); /* Empty the keyboard buffer */
162 #if CONFIG_CODEC == SWCODEC
163 talk_init();
164 #endif
167 #else
169 void init(void)
171 int rc;
172 bool mounted = false;
173 #if defined(HAVE_CHARGING) && (CONFIG_CPU == SH7034)
174 /* if nobody initialized ATA before, I consider this a cold start */
175 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
176 #endif
177 system_init();
178 kernel_init();
180 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
181 set_cpu_frequency(CPUFREQ_NORMAL);
182 #endif
184 buffer_init();
186 settings_reset();
188 lcd_init();
189 #ifdef HAVE_REMOTE_LCD
190 lcd_remote_init();
191 #endif
192 font_init();
193 show_logo();
194 lang_init();
196 set_irq_level(0);
197 #ifdef DEBUG
198 debug_init();
199 #else
200 #ifndef HAVE_MMC /* FIXME: This is also necessary for debug builds
201 * (do debug builds on the Ondio make sense?) */
202 serial_setup();
203 #endif
204 #endif
206 i2c_init();
208 #ifdef HAVE_RTC
209 rtc_init();
210 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
211 #endif
213 adc_init();
215 usb_init();
217 backlight_init();
219 button_init();
221 powermgmt_init();
223 #ifdef CONFIG_TUNER
224 radio_init();
225 #endif
227 #if defined(HAVE_CHARGING) && (CONFIG_CPU == SH7034)
228 if (coldstart && charger_inserted()
229 && !global_settings.car_adapter_mode
230 #ifdef ATA_POWER_PLAYERSTYLE
231 && !ide_powered() /* relies on probing result from bootloader */
232 #endif
235 rc = charging_screen(); /* display a "charging" screen */
236 if (rc == 1) /* charger removed */
237 power_off();
238 /* "On" pressed or USB connected: proceed */
239 show_logo(); /* again, to provide better visual feedback */
241 #endif
243 rc = ata_init();
244 if(rc)
246 #ifdef HAVE_LCD_BITMAP
247 char str[32];
248 lcd_clear_display();
249 snprintf(str, 31, "ATA error: %d", rc);
250 lcd_puts(0, 1, str);
251 lcd_puts(0, 3, "Press ON to debug");
252 lcd_update();
253 while(!(button_get(true) & BUTTON_REL));
254 dbg_ports();
255 #endif
256 panicf("ata: %d", rc);
259 usb_start_monitoring();
260 while (usb_detect())
261 { /* enter USB mode early, before trying to mount */
262 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
263 #ifdef HAVE_MMC
264 if (!mmc_touched() || (mmc_remove_request() == SYS_MMC_EXTRACTED))
265 #endif
267 usb_screen();
268 mounted = true; /* mounting done @ end of USB mode */
270 #ifdef HAVE_USB_POWER
271 if (usb_powered()) /* avoid deadlock */
272 break;
273 #endif
276 if (!mounted)
278 rc = disk_mount_all();
279 if (rc<=0)
281 lcd_clear_display();
282 lcd_puts(0, 0, "No partition");
283 lcd_puts(0, 1, "found.");
284 #ifdef HAVE_LCD_BITMAP
285 lcd_puts(0, 2, "Insert USB cable");
286 lcd_puts(0, 3, "and fix it.");
287 lcd_update();
288 #endif
289 while(button_get(true) != SYS_USB_CONNECTED) {};
290 usb_screen();
291 system_reboot();
295 settings_calc_config_sector();
296 settings_load(SETTINGS_ALL);
297 init_dircache();
299 settings_apply();
301 status_init();
302 playlist_init();
303 tree_init();
305 /* No buffer allocation (see buffer.c) may take place after the call to
306 audio_init() since the mpeg thread takes the rest of the buffer space */
307 mp3_init( global_settings.volume,
308 global_settings.bass,
309 global_settings.treble,
310 global_settings.balance,
311 global_settings.loudness,
312 global_settings.avc,
313 global_settings.channel_config,
314 global_settings.stereo_width,
315 global_settings.mdb_strength,
316 global_settings.mdb_harmonics,
317 global_settings.mdb_center,
318 global_settings.mdb_shape,
319 global_settings.mdb_enable,
320 global_settings.superbass);
321 audio_init();
322 #if (CONFIG_CODEC == SWCODEC)
323 sound_settings_apply();
324 #endif
325 #if defined(IRIVER_H100_SERIES) && !defined(SIMULATOR)
326 pcm_init_recording();
327 #endif
328 talk_init();
329 /* runtime database has to be initialized after audio_init() */
330 rundb_init();
333 #ifdef AUTOROCK
335 int fd;
336 static const char filename[] = PLUGIN_DIR "/autostart.rock";
338 fd = open(filename, O_RDONLY);
339 if(fd >= 0) /* no complaint if it doesn't exist */
341 close(fd);
342 plugin_load((char*)filename, NULL); /* start if it does */
345 #endif /* #ifdef AUTOROCK */
347 #ifdef HAVE_CHARGING
348 car_adapter_mode_init();
349 #endif
352 int main(void)
354 app_main();
356 while(1) {
357 #if CONFIG_LED == LED_REAL
358 led(true); sleep(HZ/10);
359 led(false); sleep(HZ/10);
360 #endif
362 return 0;
364 #endif