rbutil: Add quessing of the Drive letter via Windows API, also restructuring for...
[kugel-rb.git] / apps / main.c
blob3dfab06280ec3ad5c4db9d98c9024ea70e71817c
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 #if CONFIG_USBOTG == USBOTG_ISP1362
103 #include "isp1362.h"
104 #endif
106 #if CONFIG_USBOTG == USBOTG_M5636
107 #include "m5636.h"
108 #endif
110 #include "cuesheet.h"
112 /*#define AUTOROCK*/ /* define this to check for "autostart.rock" on boot */
114 const char appsversion[]=APPSVERSION;
116 static void init(void);
118 #ifdef SIMULATOR
119 void app_main(void)
120 #else
121 static void app_main(void)
122 #endif
124 init();
125 tree_gui_init();
126 root_menu();
129 static int init_dircache(bool preinit)
131 #ifdef HAVE_DIRCACHE
132 int result = 0;
133 bool clear = false;
135 if (preinit)
136 dircache_init();
138 if (!global_settings.dircache)
139 return 0;
141 # ifdef HAVE_EEPROM_SETTINGS
142 if (firmware_settings.initialized && firmware_settings.disk_clean
143 && preinit)
145 result = dircache_load();
147 if (result < 0)
149 firmware_settings.disk_clean = false;
150 if (global_status.dircache_size <= 0)
152 /* This will be in default language, settings are not
153 applied yet. Not really any easy way to fix that. */
154 gui_syncsplash(0, str(LANG_DIRCACHE_BUILDING));
155 clear = true;
158 dircache_build(global_status.dircache_size);
161 else
162 # endif
164 if (preinit)
165 return -1;
167 if (!dircache_is_enabled()
168 && !dircache_is_initializing())
170 if (global_status.dircache_size <= 0)
172 gui_syncsplash(0, str(LANG_DIRCACHE_BUILDING));
173 clear = true;
175 result = dircache_build(global_status.dircache_size);
178 if (result < 0)
180 /* Initialization of dircache failed. Manual action is
181 * necessary to enable dircache again.
183 gui_syncsplash(0, "Dircache failed, disabled. Result: %d", result);
184 global_settings.dircache = false;
189 if (clear)
191 backlight_on();
192 show_logo();
193 global_status.dircache_size = dircache_get_cache_size();
194 status_save();
197 return result;
198 #else
199 (void)preinit;
200 return 0;
201 #endif
204 #ifdef HAVE_TAGCACHE
205 static void init_tagcache(void)
207 bool clear = false;
209 tagcache_init();
211 while (!tagcache_is_initialized())
213 #ifdef HAVE_LCD_CHARCELLS
214 char buf[32];
215 #endif
216 int ret = tagcache_get_commit_step();
218 if (ret > 0)
220 #ifdef HAVE_LCD_BITMAP
221 gui_syncsplash(0, "%s [%d/%d]",
222 str(LANG_TAGCACHE_INIT), ret,
223 tagcache_get_max_commit_step());
224 #else
225 lcd_double_height(false);
226 snprintf(buf, sizeof(buf), " DB [%d/%d]", ret,
227 tagcache_get_max_commit_step());
228 lcd_puts(0, 1, buf);
229 lcd_update();
230 #endif
231 clear = true;
233 sleep(HZ/4);
235 tagtree_init();
237 if (clear)
239 backlight_on();
240 show_logo();
243 #endif
245 #ifdef SIMULATOR
247 static void init(void)
249 init_threads();
250 buffer_init();
251 lcd_init();
252 #ifdef HAVE_REMOTE_LCD
253 lcd_remote_init();
254 #endif
255 font_init();
256 show_logo();
257 button_init();
258 backlight_init();
259 lang_init();
260 #ifdef DEBUG
261 debug_init();
262 #endif
263 /* Must be done before any code uses the multi-screen APi */
264 screen_access_init();
265 gui_syncstatusbar_init(&statusbars);
266 settings_reset();
267 settings_load(SETTINGS_ALL);
268 gui_sync_wps_init();
269 settings_apply();
270 init_dircache(true);
271 init_dircache(false);
272 #ifdef HAVE_TAGCACHE
273 init_tagcache();
274 #endif
275 sleep(HZ/2);
276 tree_mem_init();
277 filetype_init();
278 playlist_init();
280 #if CONFIG_CODEC != SWCODEC
281 mp3_init( global_settings.volume,
282 global_settings.bass,
283 global_settings.treble,
284 global_settings.balance,
285 global_settings.loudness,
286 global_settings.avc,
287 global_settings.channel_config,
288 global_settings.stereo_width,
289 global_settings.mdb_strength,
290 global_settings.mdb_harmonics,
291 global_settings.mdb_center,
292 global_settings.mdb_shape,
293 global_settings.mdb_enable,
294 global_settings.superbass);
296 /* audio_init must to know the size of voice buffer so init voice first */
297 talk_init();
298 #endif /* CONFIG_CODEC != SWCODEC */
300 scrobbler_init();
301 cuesheet_init();
303 audio_init();
304 button_clear_queue(); /* Empty the keyboard buffer */
307 #else
309 static void init(void)
311 int rc;
312 bool mounted = false;
313 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
314 /* if nobody initialized ATA before, I consider this a cold start */
315 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
316 #endif
317 #ifdef CPU_PP
318 COP_CTL = PROC_WAKE;
319 #endif
320 system_init();
321 kernel_init();
323 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
324 set_cpu_frequency(CPUFREQ_NORMAL);
325 #ifdef CPU_COLDFIRE
326 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
327 #endif
328 cpu_boost(true);
329 #endif
331 buffer_init();
333 settings_reset();
335 power_init();
337 set_irq_level(0);
338 lcd_init();
339 #ifdef HAVE_REMOTE_LCD
340 lcd_remote_init();
341 #endif
342 font_init();
344 show_logo();
345 lang_init();
347 #ifdef DEBUG
348 debug_init();
349 #else
350 #if !defined(HAVE_FMADC) && !defined(HAVE_MMC)
351 serial_setup();
352 #endif
353 #endif
355 i2c_init();
357 #if CONFIG_RTC
358 rtc_init();
359 #endif
360 #ifdef HAVE_RTC_RAM
361 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
362 #endif
364 adc_init();
366 usb_init();
367 #if CONFIG_USBOTG == USBOTG_ISP1362
368 isp1362_init();
369 #elif CONFIG_USBOTG == USBOTG_M5636
370 m5636_init();
371 #endif
373 backlight_init();
375 button_init();
377 powermgmt_init();
379 #if CONFIG_TUNER
380 radio_init();
381 #endif
383 /* Must be done before any code uses the multi-screen APi */
384 screen_access_init();
385 gui_syncstatusbar_init(&statusbars);
387 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
388 if (coldstart && charger_inserted()
389 && !global_settings.car_adapter_mode
390 #ifdef ATA_POWER_PLAYERSTYLE
391 && !ide_powered() /* relies on probing result from bootloader */
392 #endif
395 rc = charging_screen(); /* display a "charging" screen */
396 if (rc == 1) /* charger removed */
397 power_off();
398 /* "On" pressed or USB connected: proceed */
399 show_logo(); /* again, to provide better visual feedback */
401 #endif
403 ata_idle_notify_init();
404 rc = ata_init();
405 if(rc)
407 #ifdef HAVE_LCD_BITMAP
408 char str[32];
409 lcd_clear_display();
410 snprintf(str, 31, "ATA error: %d", rc);
411 lcd_puts(0, 1, str);
412 lcd_puts(0, 3, "Press ON to debug");
413 lcd_update();
414 while(!(button_get(true) & BUTTON_REL)); /*DO NOT CHANGE TO ACTION SYSTEM */
415 dbg_ports();
416 #endif
417 panicf("ata: %d", rc);
420 #ifdef HAVE_EEPROM_SETTINGS
421 eeprom_settings_init();
422 #endif
424 usb_start_monitoring();
425 while (usb_detect())
427 #ifdef HAVE_EEPROM_SETTINGS
428 firmware_settings.disk_clean = false;
429 #endif
430 /* enter USB mode early, before trying to mount */
431 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
432 #ifdef HAVE_MMC
433 if (!mmc_touched() || (mmc_remove_request() == SYS_MMC_EXTRACTED))
434 #endif
436 usb_screen();
437 mounted = true; /* mounting done @ end of USB mode */
439 #ifdef HAVE_USB_POWER
440 if (usb_powered()) /* avoid deadlock */
441 break;
442 #endif
445 if (!mounted)
447 rc = disk_mount_all();
448 if (rc<=0)
450 lcd_clear_display();
451 lcd_puts(0, 0, "No partition");
452 lcd_puts(0, 1, "found.");
453 #ifdef HAVE_LCD_BITMAP
454 lcd_puts(0, 2, "Insert USB cable");
455 lcd_puts(0, 3, "and fix it.");
456 #endif
457 lcd_update();
459 while(button_get(true) != SYS_USB_CONNECTED) {};
460 usb_screen();
461 system_reboot();
465 #if defined(SETTINGS_RESET) || (CONFIG_KEYPAD == IPOD_4G_PAD) || \
466 (CONFIG_KEYPAD == IRIVER_H10_PAD)
467 #ifdef SETTINGS_RESET
468 /* Reset settings if holding the reset button. (Rec on Archos,
469 A on Gigabeat) */
470 if ((button_status() & SETTINGS_RESET) == SETTINGS_RESET)
471 #else
472 /* Reset settings if the hold button is turned on */
473 if (button_hold())
474 #endif
476 gui_syncsplash(HZ*2, str(LANG_RESET_DONE_CLEAR));
477 settings_reset();
479 else
480 #endif
481 settings_load(SETTINGS_ALL);
483 if (init_dircache(true) < 0)
485 #ifdef HAVE_TAGCACHE
486 remove(TAGCACHE_STATEFILE);
487 #endif
490 gui_sync_wps_init();
491 settings_apply();
492 init_dircache(false);
493 #ifdef HAVE_TAGCACHE
494 init_tagcache();
495 #endif
497 #ifdef HAVE_EEPROM_SETTINGS
498 if (firmware_settings.initialized)
500 /* In case we crash. */
501 firmware_settings.disk_clean = false;
502 eeprom_settings_store();
504 #endif
505 status_init();
506 playlist_init();
507 tree_mem_init();
508 filetype_init();
509 scrobbler_init();
510 cuesheet_init();
512 #if CONFIG_CODEC != SWCODEC
513 /* No buffer allocation (see buffer.c) may take place after the call to
514 audio_init() since the mpeg thread takes the rest of the buffer space */
515 mp3_init( global_settings.volume,
516 global_settings.bass,
517 global_settings.treble,
518 global_settings.balance,
519 global_settings.loudness,
520 global_settings.avc,
521 global_settings.channel_config,
522 global_settings.stereo_width,
523 global_settings.mdb_strength,
524 global_settings.mdb_harmonics,
525 global_settings.mdb_center,
526 global_settings.mdb_shape,
527 global_settings.mdb_enable,
528 global_settings.superbass);
530 /* audio_init must to know the size of voice buffer so init voice first */
531 talk_init();
532 #endif /* CONFIG_CODEC != SWCODEC */
534 audio_init();
536 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
537 pcm_rec_init();
538 #endif
540 /* runtime database has to be initialized after audio_init() */
541 cpu_boost(false);
543 #ifdef AUTOROCK
545 int fd;
546 static const char filename[] = PLUGIN_DIR "/autostart.rock";
548 fd = open(filename, O_RDONLY);
549 if(fd >= 0) /* no complaint if it doesn't exist */
551 close(fd);
552 plugin_load((char*)filename, NULL); /* start if it does */
555 #endif /* #ifdef AUTOROCK */
557 #if CONFIG_CHARGING
558 car_adapter_mode_init();
559 #endif
562 #ifdef CPU_PP
563 void cop_main(void)
565 /* This is the entry point for the coprocessor
566 Anyone not running an upgraded bootloader will never reach this point,
567 so it should not be assumed that the coprocessor be usable even on
568 platforms which support it.
570 A kernel thread runs on the coprocessor which waits for other threads to be
571 added, and gracefully handles RoLo */
573 #if CONFIG_CPU == PP5002
574 /* 3G doesn't have Rolo or dual core support yet */
575 while(1) {
576 COP_CTL = PROC_SLEEP;
578 #else
579 extern volatile unsigned char cpu_message;
581 system_init();
582 kernel_init();
584 while(cpu_message != COP_REBOOT) {
585 sleep(HZ);
587 rolo_restart_cop();
588 #endif /* PP5002 */
590 #endif /* CPU_PP */
592 int main(void)
594 app_main();
596 while(1) {
597 #if (CONFIG_LED == LED_REAL)
598 led(true); sleep(HZ/10);
599 led(false); sleep(HZ/10);
600 #endif
602 return 0;
604 #endif