Added old.t
[kugel-rb.git] / apps / main.c
blob09c1e4bea553e443e1ddc65076668c957578c785
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"
65 #include "splash.h"
67 #if (CONFIG_CODEC == SWCODEC)
68 #include "playback.h"
69 #include "pcmbuf.h"
70 #else
71 #define pcmbuf_init()
72 #endif
73 #if (defined(IRIVER_H100_SERIES) || defined(IRIVER_H300_SERIES)) && !defined(SIMULATOR)
74 #include "pcm_record.h"
75 #define SETTINGS_RESET BUTTON_REC
76 #endif
78 #ifdef CONFIG_TUNER
79 #include "radio.h"
80 #endif
81 #ifdef HAVE_MMC
82 #include "ata_mmc.h"
83 #endif
85 #ifdef HAVE_REMOTE_LCD
86 #include "lcd-remote.h"
87 #endif
89 /*#define AUTOROCK*/ /* define this to check for "autostart.rock" on boot */
91 const char appsversion[]=APPSVERSION;
93 void init(void);
95 void app_main(void)
97 init();
98 browse_root();
101 #ifdef HAVE_DIRCACHE
102 void init_dircache(void)
104 int font_w, font_h;
105 int result;
106 char buf[32];
108 dircache_init();
109 if (global_settings.dircache)
111 /* Print "Scanning disk..." to the display. */
112 lcd_getstringsize("A", &font_w, &font_h);
113 lcd_putsxy((LCD_WIDTH/2) - ((strlen(str(LANG_DIRCACHE_BUILDING))*font_w)/2),
114 LCD_HEIGHT-font_h*3, str(LANG_DIRCACHE_BUILDING));
115 lcd_update();
117 result = dircache_build(global_settings.dircache_size);
118 if (result < 0)
120 snprintf(buf, sizeof(buf),
121 "Failed! Result: %d",
122 result);
123 lcd_getstringsize("A", &font_w, &font_h);
124 lcd_putsxy((LCD_WIDTH/2) - ((strlen(buf)*font_w)/2),
125 LCD_HEIGHT-font_h*2, buf);
127 else
129 /* Clean the text when we are done. */
130 lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
131 lcd_fillrect(0, LCD_HEIGHT-font_h*3, LCD_WIDTH, font_h);
132 lcd_set_drawmode(DRMODE_SOLID);
133 lcd_update();
137 #else
138 # define init_dircache(...)
139 #endif
141 #ifdef SIMULATOR
143 void init(void)
145 init_threads();
146 buffer_init();
147 lcd_init();
148 #ifdef HAVE_REMOTE_LCD
149 lcd_remote_init();
150 #endif
151 font_init();
152 show_logo();
153 button_init();
154 backlight_init();
155 lang_init();
156 /* Must be done before any code uses the multi-screen APi */
157 screen_access_init();
158 gui_syncstatusbar_init(&statusbars);
159 settings_reset();
160 settings_calc_config_sector();
161 settings_load(SETTINGS_ALL);
162 gui_sync_wps_init();
163 settings_apply();
164 init_dircache();
165 sleep(HZ/2);
166 tree_init();
167 playlist_init();
168 mp3_init( global_settings.volume,
169 global_settings.bass,
170 global_settings.treble,
171 global_settings.balance,
172 global_settings.loudness,
173 global_settings.avc,
174 global_settings.channel_config,
175 global_settings.stereo_width,
176 global_settings.mdb_strength,
177 global_settings.mdb_harmonics,
178 global_settings.mdb_center,
179 global_settings.mdb_shape,
180 global_settings.mdb_enable,
181 global_settings.superbass);
182 #if CONFIG_CODEC == SWCODEC
183 audio_preinit();
184 #endif
185 audio_init();
186 button_clear_queue(); /* Empty the keyboard buffer */
187 #if CONFIG_CODEC == SWCODEC
188 talk_init();
189 #endif
192 #else
194 void init(void)
196 int rc;
197 bool mounted = false;
198 #if defined(HAVE_CHARGING) && (CONFIG_CPU == SH7034)
199 /* if nobody initialized ATA before, I consider this a cold start */
200 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
201 #endif
202 system_init();
203 kernel_init();
205 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
206 set_cpu_frequency(CPUFREQ_NORMAL);
207 cpu_boost(true);
208 #endif
210 buffer_init();
212 settings_reset();
214 power_init();
216 lcd_init();
217 #ifdef HAVE_REMOTE_LCD
218 lcd_remote_init();
219 #endif
220 font_init();
221 show_logo();
222 lang_init();
224 set_irq_level(0);
225 #ifdef DEBUG
226 debug_init();
227 #else
228 #ifndef HAVE_MMC /* FIXME: This is also necessary for debug builds
229 * (do debug builds on the Ondio make sense?) */
230 serial_setup();
231 #endif
232 #endif
234 i2c_init();
236 #ifdef CONFIG_RTC
237 rtc_init();
238 #endif
239 #ifdef HAVE_RTC_RAM
240 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
241 #endif
243 adc_init();
245 usb_init();
247 backlight_init();
249 button_init();
251 powermgmt_init();
253 #if CONFIG_CODEC == SWCODEC
254 audio_preinit();
255 #endif
257 #ifdef CONFIG_TUNER
258 radio_init();
259 #endif
261 /* Must be done before any code uses the multi-screen APi */
262 screen_access_init();
263 gui_syncstatusbar_init(&statusbars);
265 #if defined(HAVE_CHARGING) && (CONFIG_CPU == SH7034)
266 if (coldstart && charger_inserted()
267 && !global_settings.car_adapter_mode
268 #ifdef ATA_POWER_PLAYERSTYLE
269 && !ide_powered() /* relies on probing result from bootloader */
270 #endif
273 rc = charging_screen(); /* display a "charging" screen */
274 if (rc == 1) /* charger removed */
275 power_off();
276 /* "On" pressed or USB connected: proceed */
277 show_logo(); /* again, to provide better visual feedback */
279 #endif
281 rc = ata_init();
282 if(rc)
284 #ifdef HAVE_LCD_BITMAP
285 char str[32];
286 lcd_clear_display();
287 snprintf(str, 31, "ATA error: %d", rc);
288 lcd_puts(0, 1, str);
289 lcd_puts(0, 3, "Press ON to debug");
290 lcd_update();
291 while(!(button_get(true) & BUTTON_REL));
292 dbg_ports();
293 #endif
294 panicf("ata: %d", rc);
297 usb_start_monitoring();
298 while (usb_detect())
299 { /* enter USB mode early, before trying to mount */
300 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
301 #ifdef HAVE_MMC
302 if (!mmc_touched() || (mmc_remove_request() == SYS_MMC_EXTRACTED))
303 #endif
305 usb_screen();
306 mounted = true; /* mounting done @ end of USB mode */
308 #ifdef HAVE_USB_POWER
309 if (usb_powered()) /* avoid deadlock */
310 break;
311 #endif
314 if (!mounted)
316 rc = disk_mount_all();
317 if (rc<=0)
319 lcd_clear_display();
320 lcd_puts(0, 0, "No partition");
321 lcd_puts(0, 1, "found.");
322 #ifdef HAVE_LCD_BITMAP
323 lcd_puts(0, 2, "Insert USB cable");
324 lcd_puts(0, 3, "and fix it.");
325 lcd_update();
326 #endif
327 while(button_get(true) != SYS_USB_CONNECTED) {};
328 usb_screen();
329 system_reboot();
333 settings_calc_config_sector();
335 #if defined(SETTINGS_RESET) || (CONFIG_KEYPAD == IPOD_4G_PAD)
336 #ifdef SETTINGS_RESET
337 /* Reset settings if holding the rec button. */
338 if ((button_status() & SETTINGS_RESET) == SETTINGS_RESET)
339 #else
340 /* Reset settings if the hold button is turned on */
341 if (button_hold())
342 #endif
344 gui_syncsplash(HZ*2, true, str(LANG_RESET_DONE_CLEAR));
345 settings_reset();
347 else
348 #endif
349 settings_load(SETTINGS_ALL);
352 init_dircache();
353 gui_sync_wps_init();
354 settings_apply();
356 status_init();
357 playlist_init();
358 tree_init();
360 /* No buffer allocation (see buffer.c) may take place after the call to
361 audio_init() since the mpeg thread takes the rest of the buffer space */
362 mp3_init( global_settings.volume,
363 global_settings.bass,
364 global_settings.treble,
365 global_settings.balance,
366 global_settings.loudness,
367 global_settings.avc,
368 global_settings.channel_config,
369 global_settings.stereo_width,
370 global_settings.mdb_strength,
371 global_settings.mdb_harmonics,
372 global_settings.mdb_center,
373 global_settings.mdb_shape,
374 global_settings.mdb_enable,
375 global_settings.superbass);
376 audio_init();
377 #if (defined(IRIVER_H100_SERIES) || defined(IRIVER_H300_SERIES)) && !defined(SIMULATOR)
378 pcm_rec_init();
379 #endif
380 talk_init();
381 /* runtime database has to be initialized after audio_init() */
382 rundb_init();
383 cpu_boost(false);
385 #ifdef AUTOROCK
387 int fd;
388 static const char filename[] = PLUGIN_DIR "/autostart.rock";
390 fd = open(filename, O_RDONLY);
391 if(fd >= 0) /* no complaint if it doesn't exist */
393 close(fd);
394 plugin_load((char*)filename, NULL); /* start if it does */
397 #endif /* #ifdef AUTOROCK */
399 #ifdef HAVE_CHARGING
400 car_adapter_mode_init();
401 #endif
404 int main(void)
406 app_main();
408 while(1) {
409 #if CONFIG_LED == LED_REAL
410 led(true); sleep(HZ/10);
411 led(false); sleep(HZ/10);
412 #endif
414 return 0;
416 #endif