RGB <-> HSV colorspace conversion lib
[maemo-rb.git] / apps / main.c
blob4f31cdb58a93929e8b20b73d65aa3b6dd6558c6a
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 "tagcache.h"
64 #include "tagtree.h"
65 #include "lang.h"
66 #include "string.h"
67 #include "splash.h"
69 #if (CONFIG_CODEC == SWCODEC)
70 #include "playback.h"
71 #include "pcmbuf.h"
72 #else
73 #define pcmbuf_init()
74 #endif
75 #if (defined(IRIVER_H100_SERIES) || defined(IRIVER_H300_SERIES)) && !defined(SIMULATOR)
76 #include "pcm_record.h"
77 #define SETTINGS_RESET BUTTON_REC
78 #endif
80 #ifdef CONFIG_TUNER
81 #include "radio.h"
82 #endif
83 #ifdef HAVE_MMC
84 #include "ata_mmc.h"
85 #endif
87 #ifdef HAVE_REMOTE_LCD
88 #include "lcd-remote.h"
89 #endif
91 #if CONFIG_USBOTG == USBOTG_ISP1362
92 #include "isp1362.h"
93 #endif
95 #if CONFIG_USBOTG == USBOTG_M5636
96 #include "m5636.h"
97 #endif
99 /*#define AUTOROCK*/ /* define this to check for "autostart.rock" on boot */
101 const char appsversion[]=APPSVERSION;
103 void init(void);
105 void app_main(void)
107 init();
108 browse_root();
111 #ifdef HAVE_DIRCACHE
112 void init_dircache(void)
114 int result;
115 bool clear = false;
117 dircache_init();
118 if (global_settings.dircache)
120 if (global_settings.dircache_size == 0)
122 gui_syncsplash(0, true, str(LANG_DIRCACHE_BUILDING));
123 clear = true;
126 result = dircache_build(global_settings.dircache_size);
127 if (result < 0)
128 gui_syncsplash(0, true, "Failed! Result: %d", result);
130 if (clear)
132 backlight_on();
133 show_logo();
137 #else
138 # define init_dircache(...)
139 #endif
141 void init_tagcache(void)
143 bool clear = false;
145 tagcache_init();
147 while (!tagcache_is_initialized())
149 #ifdef HAVE_LCD_CHARCELLS
150 char buf[32];
151 #endif
152 int ret = tagcache_get_commit_step();
154 if (ret > 0)
156 #ifdef HAVE_LCD_BITMAP
157 gui_syncsplash(0, true, "%s [%d/%d]",
158 str(LANG_TAGCACHE_INIT), ret, TAG_COUNT);
159 #else
160 lcd_double_height(false);
161 snprintf(buf, sizeof(buf), " TC [%d/%d]", ret, TAG_COUNT);
162 lcd_puts(0, 1, buf);
163 #endif
164 clear = true;
166 sleep(HZ/4);
168 tagtree_init();
170 if (clear)
172 backlight_on();
173 show_logo();
177 #ifdef SIMULATOR
179 void init(void)
181 init_threads();
182 buffer_init();
183 lcd_init();
184 #ifdef HAVE_REMOTE_LCD
185 lcd_remote_init();
186 #endif
187 font_init();
188 show_logo();
189 button_init();
190 backlight_init();
191 lang_init();
192 /* Must be done before any code uses the multi-screen APi */
193 screen_access_init();
194 gui_syncstatusbar_init(&statusbars);
195 settings_reset();
196 settings_calc_config_sector();
197 settings_load(SETTINGS_ALL);
198 gui_sync_wps_init();
199 settings_apply();
200 init_dircache();
201 init_tagcache();
202 sleep(HZ/2);
203 tree_init();
204 playlist_init();
205 mp3_init( global_settings.volume,
206 global_settings.bass,
207 global_settings.treble,
208 global_settings.balance,
209 global_settings.loudness,
210 global_settings.avc,
211 global_settings.channel_config,
212 global_settings.stereo_width,
213 global_settings.mdb_strength,
214 global_settings.mdb_harmonics,
215 global_settings.mdb_center,
216 global_settings.mdb_shape,
217 global_settings.mdb_enable,
218 global_settings.superbass);
220 #if CONFIG_CODEC == SWCODEC
221 audio_preinit();
222 #endif
224 /* audio_init must to know the size of voice buffer so init voice first */
225 #if CONFIG_CODEC == SWCODEC
226 talk_init();
227 #endif
229 audio_init();
230 button_clear_queue(); /* Empty the keyboard buffer */
233 #else
235 void init(void)
237 int rc;
238 bool mounted = false;
239 #if defined(CONFIG_CHARGING) && (CONFIG_CPU == SH7034)
240 /* if nobody initialized ATA before, I consider this a cold start */
241 bool coldstart = (PACR2 & 0x4000) != 0; /* starting from Flash */
242 #endif
243 system_init();
244 kernel_init();
246 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
247 set_cpu_frequency(CPUFREQ_NORMAL);
248 cpu_boost(true);
249 #endif
251 buffer_init();
253 settings_reset();
255 power_init();
257 set_irq_level(0);
258 lcd_init();
259 #ifdef HAVE_REMOTE_LCD
260 lcd_remote_init();
261 #endif
262 font_init();
263 show_logo();
264 lang_init();
266 #ifdef DEBUG
267 debug_init();
268 #else
269 #ifndef HAVE_MMC /* FIXME: This is also necessary for debug builds
270 * (do debug builds on the Ondio make sense?) */
271 serial_setup();
272 #endif
273 #endif
275 i2c_init();
277 #ifdef CONFIG_RTC
278 rtc_init();
279 #endif
280 #ifdef HAVE_RTC_RAM
281 settings_load(SETTINGS_RTC); /* early load parts of global_settings */
282 #endif
284 adc_init();
286 usb_init();
287 #if CONFIG_USBOTG == USBOTG_ISP1362
288 isp1362_init();
289 #elif CONFIG_USBOTG == USBOTG_M5636
290 m5636_init();
291 #endif
293 backlight_init();
295 button_init();
297 powermgmt_init();
299 #if CONFIG_CODEC == SWCODEC
300 audio_preinit();
301 #endif
303 #ifdef CONFIG_TUNER
304 radio_init();
305 #endif
307 /* Must be done before any code uses the multi-screen APi */
308 screen_access_init();
309 gui_syncstatusbar_init(&statusbars);
311 #if defined(CONFIG_CHARGING) && (CONFIG_CPU == SH7034)
312 if (coldstart && charger_inserted()
313 && !global_settings.car_adapter_mode
314 #ifdef ATA_POWER_PLAYERSTYLE
315 && !ide_powered() /* relies on probing result from bootloader */
316 #endif
319 rc = charging_screen(); /* display a "charging" screen */
320 if (rc == 1) /* charger removed */
321 power_off();
322 /* "On" pressed or USB connected: proceed */
323 show_logo(); /* again, to provide better visual feedback */
325 #endif
327 rc = ata_init();
328 if(rc)
330 #ifdef HAVE_LCD_BITMAP
331 char str[32];
332 lcd_clear_display();
333 snprintf(str, 31, "ATA error: %d", rc);
334 lcd_puts(0, 1, str);
335 lcd_puts(0, 3, "Press ON to debug");
336 lcd_update();
337 while(!(button_get(true) & BUTTON_REL));
338 dbg_ports();
339 #endif
340 panicf("ata: %d", rc);
343 usb_start_monitoring();
344 while (usb_detect())
345 { /* enter USB mode early, before trying to mount */
346 if (button_get_w_tmo(HZ/10) == SYS_USB_CONNECTED)
347 #ifdef HAVE_MMC
348 if (!mmc_touched() || (mmc_remove_request() == SYS_MMC_EXTRACTED))
349 #endif
351 usb_screen();
352 mounted = true; /* mounting done @ end of USB mode */
354 #ifdef HAVE_USB_POWER
355 if (usb_powered()) /* avoid deadlock */
356 break;
357 #endif
360 if (!mounted)
362 rc = disk_mount_all();
363 if (rc<=0)
365 lcd_clear_display();
366 lcd_puts(0, 0, "No partition");
367 lcd_puts(0, 1, "found.");
368 #ifdef HAVE_LCD_BITMAP
369 lcd_puts(0, 2, "Insert USB cable");
370 lcd_puts(0, 3, "and fix it.");
371 lcd_update();
372 #endif
373 while(button_get(true) != SYS_USB_CONNECTED) {};
374 usb_screen();
375 system_reboot();
379 settings_calc_config_sector();
381 #if defined(SETTINGS_RESET) || (CONFIG_KEYPAD == IPOD_4G_PAD)
382 #ifdef SETTINGS_RESET
383 /* Reset settings if holding the rec button. */
384 if ((button_status() & SETTINGS_RESET) == SETTINGS_RESET)
385 #else
386 /* Reset settings if the hold button is turned on */
387 if (button_hold())
388 #endif
390 gui_syncsplash(HZ*2, true, str(LANG_RESET_DONE_CLEAR));
391 settings_reset();
393 else
394 #endif
395 settings_load(SETTINGS_ALL);
398 gui_sync_wps_init();
399 settings_apply();
400 init_dircache();
401 init_tagcache();
403 status_init();
404 playlist_init();
405 tree_init();
407 /* No buffer allocation (see buffer.c) may take place after the call to
408 audio_init() since the mpeg thread takes the rest of the buffer space */
409 mp3_init( global_settings.volume,
410 global_settings.bass,
411 global_settings.treble,
412 global_settings.balance,
413 global_settings.loudness,
414 global_settings.avc,
415 global_settings.channel_config,
416 global_settings.stereo_width,
417 global_settings.mdb_strength,
418 global_settings.mdb_harmonics,
419 global_settings.mdb_center,
420 global_settings.mdb_shape,
421 global_settings.mdb_enable,
422 global_settings.superbass);
424 /* audio_init must to know the size of voice buffer so init voice first */
425 talk_init();
427 audio_init();
428 #if (defined(IRIVER_H100_SERIES) || defined(IRIVER_H300_SERIES)) && !defined(SIMULATOR)
429 pcm_rec_init();
430 #endif
432 /* runtime database has to be initialized after audio_init() */
433 cpu_boost(false);
435 #ifdef AUTOROCK
437 int fd;
438 static const char filename[] = PLUGIN_DIR "/autostart.rock";
440 fd = open(filename, O_RDONLY);
441 if(fd >= 0) /* no complaint if it doesn't exist */
443 close(fd);
444 plugin_load((char*)filename, NULL); /* start if it does */
447 #endif /* #ifdef AUTOROCK */
449 #ifdef CONFIG_CHARGING
450 car_adapter_mode_init();
451 #endif
454 int main(void)
456 app_main();
458 while(1) {
459 #if CONFIG_LED == LED_REAL
460 led(true); sleep(HZ/10);
461 led(false); sleep(HZ/10);
462 #endif
464 return 0;
466 #endif