lots of old and new edits
[kugel-rb.git] / apps / main.c
blobd84aa25d0ff77c0cb04d58cc3ed33eeb023ea736
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 #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 gui_sync_data_wps_init();
142 gui_sync_wps_init();
143 settings_apply();
144 init_dircache();
145 sleep(HZ/2);
146 tree_init();
147 playlist_init();
148 mp3_init( global_settings.volume,
149 global_settings.bass,
150 global_settings.treble,
151 global_settings.balance,
152 global_settings.loudness,
153 global_settings.avc,
154 global_settings.channel_config,
155 global_settings.stereo_width,
156 global_settings.mdb_strength,
157 global_settings.mdb_harmonics,
158 global_settings.mdb_center,
159 global_settings.mdb_shape,
160 global_settings.mdb_enable,
161 global_settings.superbass);
162 audio_init();
163 button_clear_queue(); /* Empty the keyboard buffer */
164 #if CONFIG_CODEC == SWCODEC
165 talk_init();
166 #endif
169 #else
171 void init(void)
173 int rc;
174 bool mounted = false;
175 #if defined(HAVE_CHARGING) && (CONFIG_CPU == SH7034)
176 /* if nobody initialized ATA before, I consider this a cold start */
177 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
178 #endif
179 system_init();
180 kernel_init();
182 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
183 set_cpu_frequency(CPUFREQ_NORMAL);
184 #endif
186 buffer_init();
188 settings_reset();
190 lcd_init();
191 #ifdef HAVE_REMOTE_LCD
192 lcd_remote_init();
193 #endif
194 font_init();
195 show_logo();
196 lang_init();
198 set_irq_level(0);
199 #ifdef DEBUG
200 debug_init();
201 #else
202 #ifndef HAVE_MMC /* FIXME: This is also necessary for debug builds
203 * (do debug builds on the Ondio make sense?) */
204 serial_setup();
205 #endif
206 #endif
208 i2c_init();
210 #ifdef HAVE_RTC
211 rtc_init();
212 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
213 #endif
215 adc_init();
217 usb_init();
219 backlight_init();
221 button_init();
223 powermgmt_init();
225 #ifdef CONFIG_TUNER
226 radio_init();
227 #endif
229 #if defined(HAVE_CHARGING) && (CONFIG_CPU == SH7034)
230 if (coldstart && charger_inserted()
231 && !global_settings.car_adapter_mode
232 #ifdef ATA_POWER_PLAYERSTYLE
233 && !ide_powered() /* relies on probing result from bootloader */
234 #endif
237 rc = charging_screen(); /* display a "charging" screen */
238 if (rc == 1) /* charger removed */
239 power_off();
240 /* "On" pressed or USB connected: proceed */
241 show_logo(); /* again, to provide better visual feedback */
243 #endif
245 rc = ata_init();
246 if(rc)
248 #ifdef HAVE_LCD_BITMAP
249 char str[32];
250 lcd_clear_display();
251 snprintf(str, 31, "ATA error: %d", rc);
252 lcd_puts(0, 1, str);
253 lcd_puts(0, 3, "Press ON to debug");
254 lcd_update();
255 while(!(button_get(true) & BUTTON_REL));
256 dbg_ports();
257 #endif
258 panicf("ata: %d", rc);
261 usb_start_monitoring();
262 while (usb_detect())
263 { /* enter USB mode early, before trying to mount */
264 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
265 #ifdef HAVE_MMC
266 if (!mmc_touched() || (mmc_remove_request() == SYS_MMC_EXTRACTED))
267 #endif
269 usb_screen();
270 mounted = true; /* mounting done @ end of USB mode */
272 #ifdef HAVE_USB_POWER
273 if (usb_powered()) /* avoid deadlock */
274 break;
275 #endif
278 if (!mounted)
280 rc = disk_mount_all();
281 if (rc<=0)
283 lcd_clear_display();
284 lcd_puts(0, 0, "No partition");
285 lcd_puts(0, 1, "found.");
286 #ifdef HAVE_LCD_BITMAP
287 lcd_puts(0, 2, "Insert USB cable");
288 lcd_puts(0, 3, "and fix it.");
289 lcd_update();
290 #endif
291 while(button_get(true) != SYS_USB_CONNECTED) {};
292 usb_screen();
293 system_reboot();
297 settings_calc_config_sector();
298 settings_load(SETTINGS_ALL);
299 init_dircache();
300 gui_sync_data_wps_init();
301 gui_sync_wps_init();
302 settings_apply();
304 status_init();
305 playlist_init();
306 tree_init();
308 /* No buffer allocation (see buffer.c) may take place after the call to
309 audio_init() since the mpeg thread takes the rest of the buffer space */
310 mp3_init( global_settings.volume,
311 global_settings.bass,
312 global_settings.treble,
313 global_settings.balance,
314 global_settings.loudness,
315 global_settings.avc,
316 global_settings.channel_config,
317 global_settings.stereo_width,
318 global_settings.mdb_strength,
319 global_settings.mdb_harmonics,
320 global_settings.mdb_center,
321 global_settings.mdb_shape,
322 global_settings.mdb_enable,
323 global_settings.superbass);
324 audio_init();
325 #if (CONFIG_CODEC == SWCODEC)
326 sound_settings_apply();
327 #endif
328 #if defined(IRIVER_H100_SERIES) && !defined(SIMULATOR)
329 pcm_rec_init();
330 #endif
331 talk_init();
332 /* runtime database has to be initialized after audio_init() */
333 rundb_init();
336 #ifdef AUTOROCK
338 int fd;
339 static const char filename[] = PLUGIN_DIR "/autostart.rock";
341 fd = open(filename, O_RDONLY);
342 if(fd >= 0) /* no complaint if it doesn't exist */
344 close(fd);
345 plugin_load((char*)filename, NULL); /* start if it does */
348 #endif /* #ifdef AUTOROCK */
350 #ifdef HAVE_CHARGING
351 car_adapter_mode_init();
352 #endif
355 int main(void)
357 app_main();
359 while(1) {
360 #if CONFIG_LED == LED_REAL
361 led(true); sleep(HZ/10);
362 led(false); sleep(HZ/10);
363 #endif
365 return 0;
367 #endif