FS#8457 - convert the list drawing code to use viewports. This does not include any...
[maemo-rb.git] / apps / main.c
blobbb29eb73dc6502a019a1ecc570e8b4790ee91eb5
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 #ifdef SIMULATOR
113 #include "system-sdl.h"
114 #endif
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 int i;
129 init();
130 FOR_NB_SCREENS(i)
132 screens[i].clear_display();
133 screens[i].update();
135 tree_gui_init();
136 root_menu();
139 static int init_dircache(bool preinit)
141 #ifdef HAVE_DIRCACHE
142 int result = 0;
143 bool clear = false;
145 if (preinit)
146 dircache_init();
148 if (!global_settings.dircache)
149 return 0;
151 # ifdef HAVE_EEPROM_SETTINGS
152 if (firmware_settings.initialized && firmware_settings.disk_clean
153 && preinit)
155 result = dircache_load();
157 if (result < 0)
159 firmware_settings.disk_clean = false;
160 if (global_status.dircache_size <= 0)
162 /* This will be in default language, settings are not
163 applied yet. Not really any easy way to fix that. */
164 gui_syncsplash(0, str(LANG_SCANNING_DISK));
165 clear = true;
168 dircache_build(global_status.dircache_size);
171 else
172 # endif
174 if (preinit)
175 return -1;
177 if (!dircache_is_enabled()
178 && !dircache_is_initializing())
180 if (global_status.dircache_size <= 0)
182 gui_syncsplash(0, str(LANG_SCANNING_DISK));
183 clear = true;
185 result = dircache_build(global_status.dircache_size);
188 if (result < 0)
190 /* Initialization of dircache failed. Manual action is
191 * necessary to enable dircache again.
193 gui_syncsplash(0, "Dircache failed, disabled. Result: %d", result);
194 global_settings.dircache = false;
199 if (clear)
201 backlight_on();
202 show_logo();
203 global_status.dircache_size = dircache_get_cache_size();
204 status_save();
207 return result;
208 #else
209 (void)preinit;
210 return 0;
211 #endif
214 #ifdef HAVE_TAGCACHE
215 static void init_tagcache(void)
217 bool clear = false;
219 tagcache_init();
221 while (!tagcache_is_initialized())
223 #ifdef HAVE_LCD_CHARCELLS
224 char buf[32];
225 #endif
226 int ret = tagcache_get_commit_step();
228 if (ret > 0)
230 #if CONFIG_CODEC == SWCODEC
231 /* hwcodec can't use voice here, as the database commit
232 * uses the audio buffer. */
233 static long talked_tick = 0;
234 if(global_settings.talk_menu
235 && (talked_tick == 0
236 || TIME_AFTER(current_tick, talked_tick+7*HZ)))
238 talked_tick = current_tick;
239 talk_id(LANG_TAGCACHE_INIT, false);
240 talk_number(ret, true);
241 talk_id(VOICE_OF, true);
242 talk_number(tagcache_get_max_commit_step(), true);
244 #endif
245 #ifdef HAVE_LCD_BITMAP
246 gui_syncsplash(0, "%s [%d/%d]",
247 str(LANG_TAGCACHE_INIT), ret,
248 tagcache_get_max_commit_step());
249 #else
250 lcd_double_height(false);
251 snprintf(buf, sizeof(buf), " DB [%d/%d]", ret,
252 tagcache_get_max_commit_step());
253 lcd_puts(0, 1, buf);
254 lcd_update();
255 #endif
256 clear = true;
258 sleep(HZ/4);
260 tagtree_init();
262 if (clear)
264 backlight_on();
265 show_logo();
268 #endif
270 #ifdef SIMULATOR
272 static void init(void)
274 init_threads();
275 buffer_init();
276 set_irq_level(0);
277 lcd_init();
278 #ifdef HAVE_REMOTE_LCD
279 lcd_remote_init();
280 #endif
281 font_init();
282 show_logo();
283 button_init();
284 backlight_init();
285 lang_init();
286 #ifdef DEBUG
287 debug_init();
288 #endif
289 /* Must be done before any code uses the multi-screen APi */
290 screen_access_init();
291 gui_syncstatusbar_init(&statusbars);
292 ata_init();
293 settings_reset();
294 settings_load(SETTINGS_ALL);
295 gui_sync_wps_init();
296 settings_apply();
297 init_dircache(true);
298 init_dircache(false);
299 #ifdef HAVE_TAGCACHE
300 init_tagcache();
301 #endif
302 sleep(HZ/2);
303 tree_mem_init();
304 filetype_init();
305 playlist_init();
307 #if CONFIG_CODEC != SWCODEC
308 mp3_init( global_settings.volume,
309 global_settings.bass,
310 global_settings.treble,
311 global_settings.balance,
312 global_settings.loudness,
313 global_settings.avc,
314 global_settings.channel_config,
315 global_settings.stereo_width,
316 global_settings.mdb_strength,
317 global_settings.mdb_harmonics,
318 global_settings.mdb_center,
319 global_settings.mdb_shape,
320 global_settings.mdb_enable,
321 global_settings.superbass);
323 /* audio_init must to know the size of voice buffer so init voice first */
324 talk_init();
325 #endif /* CONFIG_CODEC != SWCODEC */
327 scrobbler_init();
328 cuesheet_init();
330 audio_init();
331 button_clear_queue(); /* Empty the keyboard buffer */
334 #else
336 static void init(void)
338 int rc;
339 bool mounted = false;
340 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
341 /* if nobody initialized ATA before, I consider this a cold start */
342 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
343 #endif
345 system_init();
346 kernel_init();
348 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
349 set_cpu_frequency(CPUFREQ_NORMAL);
350 #ifdef CPU_COLDFIRE
351 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
352 #endif
353 cpu_boost(true);
354 #endif
356 buffer_init();
358 settings_reset();
360 i2c_init();
362 power_init();
364 set_irq_level(0);
365 #ifdef CPU_ARM
366 set_fiq_status(FIQ_ENABLED);
367 #endif
368 lcd_init();
369 #ifdef HAVE_REMOTE_LCD
370 lcd_remote_init();
371 #endif
372 font_init();
374 show_logo();
375 lang_init();
377 #ifdef DEBUG
378 debug_init();
379 #else
380 #if !defined(HAVE_FMADC) && !defined(HAVE_MMC)
381 serial_setup();
382 #endif
383 #endif
385 #if CONFIG_RTC
386 rtc_init();
387 #endif
388 #ifdef HAVE_RTC_RAM
389 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
390 #endif
392 adc_init();
394 usb_init();
395 #if CONFIG_USBOTG == USBOTG_ISP1362
396 isp1362_init();
397 #elif CONFIG_USBOTG == USBOTG_M5636
398 m5636_init();
399 #endif
401 backlight_init();
403 button_init();
405 powermgmt_init();
407 #if CONFIG_TUNER
408 radio_init();
409 #endif
411 /* Must be done before any code uses the multi-screen APi */
412 screen_access_init();
413 gui_syncstatusbar_init(&statusbars);
415 #if CONFIG_CHARGING && (CONFIG_CPU == SH7034)
416 if (coldstart && charger_inserted()
417 && !global_settings.car_adapter_mode
418 #ifdef ATA_POWER_PLAYERSTYLE
419 && !ide_powered() /* relies on probing result from bootloader */
420 #endif
423 rc = charging_screen(); /* display a "charging" screen */
424 if (rc == 1) /* charger removed */
425 power_off();
426 /* "On" pressed or USB connected: proceed */
427 show_logo(); /* again, to provide better visual feedback */
429 #endif
431 ata_idle_notify_init();
432 rc = ata_init();
433 if(rc)
435 #ifdef HAVE_LCD_BITMAP
436 char str[32];
437 lcd_clear_display();
438 snprintf(str, 31, "ATA error: %d", rc);
439 lcd_puts(0, 1, str);
440 lcd_puts(0, 3, "Press ON to debug");
441 lcd_update();
442 while(!(button_get(true) & BUTTON_REL)); /*DO NOT CHANGE TO ACTION SYSTEM */
443 dbg_ports();
444 #endif
445 panicf("ata: %d", rc);
448 #ifdef HAVE_EEPROM_SETTINGS
449 eeprom_settings_init();
450 #endif
452 usb_start_monitoring();
453 while (usb_detect() == USB_INSERTED)
455 #ifdef HAVE_EEPROM_SETTINGS
456 firmware_settings.disk_clean = false;
457 #endif
458 /* enter USB mode early, before trying to mount */
459 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
460 #ifdef HAVE_MMC
461 if (!mmc_touched() ||
462 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
463 #endif
465 usb_screen();
466 #ifndef HAVE_USBSTACK
467 mounted = true; /* mounting done @ end of USB mode */
468 #endif
470 #ifdef HAVE_USB_POWER
471 if (usb_powered()) /* avoid deadlock */
472 break;
473 #endif
476 if (!mounted)
478 rc = disk_mount_all();
479 if (rc<=0)
481 lcd_clear_display();
482 lcd_puts(0, 0, "No partition");
483 lcd_puts(0, 1, "found.");
484 #ifdef HAVE_LCD_BITMAP
485 lcd_puts(0, 2, "Insert USB cable");
486 lcd_puts(0, 3, "and fix it.");
487 #endif
488 lcd_update();
490 while(button_get(true) != SYS_USB_CONNECTED) {};
491 usb_screen();
492 system_reboot();
496 #if defined(SETTINGS_RESET) || (CONFIG_KEYPAD == IPOD_4G_PAD) || \
497 (CONFIG_KEYPAD == IRIVER_H10_PAD)
498 #ifdef SETTINGS_RESET
499 /* Reset settings if holding the reset button. (Rec on Archos,
500 A on Gigabeat) */
501 if ((button_status() & SETTINGS_RESET) == SETTINGS_RESET)
502 #else
503 /* Reset settings if the hold button is turned on */
504 if (button_hold())
505 #endif
507 gui_syncsplash(HZ*2, str(LANG_RESET_DONE_CLEAR));
508 settings_reset();
510 else
511 #endif
512 settings_load(SETTINGS_ALL);
514 if (init_dircache(true) < 0)
516 #ifdef HAVE_TAGCACHE
517 remove(TAGCACHE_STATEFILE);
518 #endif
521 gui_sync_wps_init();
522 settings_apply();
523 init_dircache(false);
524 #ifdef HAVE_TAGCACHE
525 init_tagcache();
526 #endif
528 #ifdef HAVE_EEPROM_SETTINGS
529 if (firmware_settings.initialized)
531 /* In case we crash. */
532 firmware_settings.disk_clean = false;
533 eeprom_settings_store();
535 #endif
536 status_init();
537 playlist_init();
538 tree_mem_init();
539 filetype_init();
540 scrobbler_init();
541 cuesheet_init();
543 #if CONFIG_CODEC != SWCODEC
544 /* No buffer allocation (see buffer.c) may take place after the call to
545 audio_init() since the mpeg thread takes the rest of the buffer space */
546 mp3_init( global_settings.volume,
547 global_settings.bass,
548 global_settings.treble,
549 global_settings.balance,
550 global_settings.loudness,
551 global_settings.avc,
552 global_settings.channel_config,
553 global_settings.stereo_width,
554 global_settings.mdb_strength,
555 global_settings.mdb_harmonics,
556 global_settings.mdb_center,
557 global_settings.mdb_shape,
558 global_settings.mdb_enable,
559 global_settings.superbass);
561 /* audio_init must to know the size of voice buffer so init voice first */
562 talk_init();
563 #endif /* CONFIG_CODEC != SWCODEC */
565 audio_init();
567 #if (CONFIG_CODEC == SWCODEC) && defined(HAVE_RECORDING) && !defined(SIMULATOR)
568 pcm_rec_init();
569 #endif
571 /* runtime database has to be initialized after audio_init() */
572 cpu_boost(false);
574 #ifdef AUTOROCK
576 int fd;
577 static const char filename[] = PLUGIN_APPS_DIR "/autostart.rock";
579 fd = open(filename, O_RDONLY);
580 if(fd >= 0) /* no complaint if it doesn't exist */
582 close(fd);
583 plugin_load((char*)filename, NULL); /* start if it does */
586 #endif /* #ifdef AUTOROCK */
588 #if CONFIG_CHARGING
589 car_adapter_mode_init();
590 #endif
593 #ifdef CPU_PP
594 void cop_main(void)
596 /* This is the entry point for the coprocessor
597 Anyone not running an upgraded bootloader will never reach this point,
598 so it should not be assumed that the coprocessor be usable even on
599 platforms which support it.
601 A kernel thread is initially setup on the coprocessor and immediately
602 destroyed for purposes of continuity. The cop sits idle until at least
603 one thread exists on it. */
605 /* 3G doesn't have Rolo or dual core support yet */
606 #if NUM_CORES > 1
607 system_init();
608 kernel_init();
609 /* This should never be reached */
610 #endif
611 while(1) {
612 COP_CTL = PROC_SLEEP;
615 #endif /* CPU_PP */
617 int main(void)
619 app_main();
621 while(1) {
622 #if (CONFIG_LED == LED_REAL)
623 led(true); sleep(HZ/10);
624 led(false); sleep(HZ/10);
625 #endif
627 return 0;
629 #endif