Oops.
[Rockbox.git] / apps / misc.c
blobbd42ca9e3428784f88c7aa6bf928c52e9ab416c8
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Daniel 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 <stdlib.h>
20 #include <ctype.h>
21 #include "lang.h"
22 #include "string.h"
23 #include "config.h"
24 #include "file.h"
25 #include "dir.h"
26 #include "lcd.h"
27 #include "lcd-remote.h"
28 #include "sprintf.h"
29 #include "errno.h"
30 #include "system.h"
31 #include "timefuncs.h"
32 #include "screens.h"
33 #include "talk.h"
34 #include "mpeg.h"
35 #include "audio.h"
36 #include "mp3_playback.h"
37 #include "settings.h"
38 #include "ata.h"
39 #include "ata_idle_notify.h"
40 #include "kernel.h"
41 #include "power.h"
42 #include "powermgmt.h"
43 #include "backlight.h"
44 #include "atoi.h"
45 #include "version.h"
46 #include "font.h"
47 #include "splash.h"
48 #include "tagcache.h"
49 #include "scrobbler.h"
50 #include "sound.h"
51 #ifdef HAVE_MMC
52 #include "ata_mmc.h"
53 #endif
54 #include "tree.h"
55 #include "eeprom_settings.h"
57 #ifdef HAVE_LCD_BITMAP
58 #include "bmp.h"
59 #include "icons.h"
60 #endif /* End HAVE_LCD_BITMAP */
61 #include "gui/gwps-common.h"
62 #include "bookmark.h"
64 #include "misc.h"
65 #include "playback.h"
67 #ifdef BOOTFILE
68 #if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
69 #include "textarea.h"
70 #include "rolo.h"
71 #include "yesno.h"
72 #endif
73 #endif
75 /* Format a large-range value for output, using the appropriate unit so that
76 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
77 * units) if possible, and 3 significant digits are shown. If a buffer is
78 * given, the result is snprintf()'d into that buffer, otherwise the result is
79 * voiced.*/
80 char *output_dyn_value(char *buf, int buf_size, int value,
81 const unsigned char **units, bool bin_scale)
83 int scale = bin_scale ? 1024 : 1000;
84 int fraction = 0;
85 int unit_no = 0;
86 int i;
87 char tbuf[5];
89 while (value >= scale)
91 fraction = value % scale;
92 value /= scale;
93 unit_no++;
95 if (bin_scale)
96 fraction = fraction * 1000 / 1024;
98 if (value >= 100 || !unit_no)
99 tbuf[0] = '\0';
100 else if (value >= 10)
101 snprintf(tbuf, sizeof(tbuf), "%01d", fraction / 100);
102 else
103 snprintf(tbuf, sizeof(tbuf), "%02d", fraction / 10);
105 if (buf)
107 if (strlen(tbuf))
108 snprintf(buf, buf_size, "%d%s%s%s", value, str(LANG_POINT),
109 tbuf, P2STR(units[unit_no]));
110 else
111 snprintf(buf, buf_size, "%d%s", value, P2STR(units[unit_no]));
113 else
115 /* strip trailing zeros from the fraction */
116 for (i = strlen(tbuf) - 1; (i >= 0) && (tbuf[i] == '0'); i--)
117 tbuf[i] = '\0';
119 talk_number(value, true);
120 if (tbuf[0] != 0)
122 talk_id(LANG_POINT, true);
123 talk_spell(tbuf, true);
125 talk_id(P2ID(units[unit_no]), true);
127 return buf;
130 /* Create a filename with a number part in a way that the number is 1
131 * higher than the highest numbered file matching the same pattern.
132 * It is allowed that buffer and path point to the same memory location,
133 * saving a strcpy(). Path must always be given without trailing slash.
134 * "num" can point to an int specifying the number to use or NULL or a value
135 * less than zero to number automatically. The final number used will also
136 * be returned in *num. If *num is >= 0 then *num will be incremented by
137 * one. */
138 char *create_numbered_filename(char *buffer, const char *path,
139 const char *prefix, const char *suffix,
140 int numberlen IF_CNFN_NUM_(, int *num))
142 DIR *dir;
143 struct dirent *entry;
144 int max_num;
145 int pathlen;
146 int prefixlen = strlen(prefix);
147 char fmtstring[12];
149 if (buffer != path)
150 strncpy(buffer, path, MAX_PATH);
152 pathlen = strlen(buffer);
154 #ifdef IF_CNFN_NUM
155 if (num && *num >= 0)
157 /* number specified */
158 max_num = *num;
160 else
161 #endif
163 /* automatic numbering */
164 max_num = 0;
166 dir = opendir(pathlen ? buffer : "/");
167 if (!dir)
168 return NULL;
170 while ((entry = readdir(dir)))
172 int curr_num;
174 if (strncasecmp((char *)entry->d_name, prefix, prefixlen)
175 || strcasecmp((char *)entry->d_name + prefixlen + numberlen, suffix))
176 continue;
178 curr_num = atoi((char *)entry->d_name + prefixlen);
179 if (curr_num > max_num)
180 max_num = curr_num;
183 closedir(dir);
186 max_num++;
188 snprintf(fmtstring, sizeof(fmtstring), "/%%s%%0%dd%%s", numberlen);
189 snprintf(buffer + pathlen, MAX_PATH - pathlen, fmtstring, prefix,
190 max_num, suffix);
192 #ifdef IF_CNFN_NUM
193 if (num)
194 *num = max_num;
195 #endif
197 return buffer;
200 /* Format time into buf.
202 * buf - buffer to format to.
203 * buf_size - size of buffer.
204 * t - time to format, in milliseconds.
206 void format_time(char* buf, int buf_size, long t)
208 if ( t < 3600000 )
210 snprintf(buf, buf_size, "%d:%02d",
211 (int) (t / 60000), (int) (t % 60000 / 1000));
213 else
215 snprintf(buf, buf_size, "%d:%02d:%02d",
216 (int) (t / 3600000), (int) (t % 3600000 / 60000),
217 (int) (t % 60000 / 1000));
221 #if CONFIG_RTC
222 /* Create a filename with a date+time part.
223 It is allowed that buffer and path point to the same memory location,
224 saving a strcpy(). Path must always be given without trailing slash.
225 unique_time as true makes the function wait until the current time has
226 changed. */
227 char *create_datetime_filename(char *buffer, const char *path,
228 const char *prefix, const char *suffix,
229 bool unique_time)
231 struct tm *tm = get_time();
232 static struct tm last_tm;
233 int pathlen;
235 while (unique_time && !memcmp(get_time(), &last_tm, sizeof (struct tm)))
236 sleep(HZ/10);
238 last_tm = *tm;
240 if (buffer != path)
241 strncpy(buffer, path, MAX_PATH);
243 pathlen = strlen(buffer);
244 snprintf(buffer + pathlen, MAX_PATH - pathlen,
245 "/%s%02d%02d%02d-%02d%02d%02d%s", prefix,
246 tm->tm_year % 100, tm->tm_mon + 1, tm->tm_mday,
247 tm->tm_hour, tm->tm_min, tm->tm_sec, suffix);
249 return buffer;
251 #endif /* CONFIG_RTC */
253 /* Read (up to) a line of text from fd into buffer and return number of bytes
254 * read (which may be larger than the number of bytes stored in buffer). If
255 * an error occurs, -1 is returned (and buffer contains whatever could be
256 * read). A line is terminated by a LF char. Neither LF nor CR chars are
257 * stored in buffer.
259 int read_line(int fd, char* buffer, int buffer_size)
261 int count = 0;
262 int num_read = 0;
264 errno = 0;
266 while (count < buffer_size)
268 unsigned char c;
270 if (1 != read(fd, &c, 1))
271 break;
273 num_read++;
275 if ( c == '\n' )
276 break;
278 if ( c == '\r' )
279 continue;
281 buffer[count++] = c;
284 buffer[MIN(count, buffer_size - 1)] = 0;
286 return errno ? -1 : num_read;
289 /* Performance optimized version of the previous function. */
290 int fast_readline(int fd, char *buf, int buf_size, void *parameters,
291 int (*callback)(int n, const char *buf, void *parameters))
293 char *p, *next;
294 int rc, pos = 0;
295 int count = 0;
297 while ( 1 )
299 next = NULL;
301 rc = read(fd, &buf[pos], buf_size - pos - 1);
302 if (rc >= 0)
303 buf[pos+rc] = '\0';
305 if ( (p = strchr(buf, '\r')) != NULL)
307 *p = '\0';
308 next = ++p;
310 else
311 p = buf;
313 if ( (p = strchr(p, '\n')) != NULL)
315 *p = '\0';
316 next = ++p;
319 rc = callback(count, buf, parameters);
320 if (rc < 0)
321 return rc;
323 count++;
324 if (next)
326 pos = buf_size - ((long)next - (long)buf) - 1;
327 memmove(buf, next, pos);
329 else
330 break ;
333 return 0;
336 #ifdef HAVE_LCD_BITMAP
338 #if LCD_DEPTH == 16
339 #define BMP_COMPRESSION 3 /* BI_BITFIELDS */
340 #define BMP_NUMCOLORS 3
341 #else
342 #define BMP_COMPRESSION 0 /* BI_RGB */
343 #if LCD_DEPTH <= 8
344 #define BMP_NUMCOLORS (1 << LCD_DEPTH)
345 #else
346 #define BMP_NUMCOLORS 0
347 #endif
348 #endif
350 #if LCD_DEPTH == 1
351 #define BMP_BPP 1
352 #define BMP_LINESIZE ((LCD_WIDTH/8 + 3) & ~3)
353 #elif LCD_DEPTH <= 4
354 #define BMP_BPP 4
355 #define BMP_LINESIZE ((LCD_WIDTH/2 + 3) & ~3)
356 #elif LCD_DEPTH <= 8
357 #define BMP_BPP 8
358 #define BMP_LINESIZE ((LCD_WIDTH + 3) & ~3)
359 #elif LCD_DEPTH <= 16
360 #define BMP_BPP 16
361 #define BMP_LINESIZE ((LCD_WIDTH*2 + 3) & ~3)
362 #else
363 #define BMP_BPP 24
364 #define BMP_LINESIZE ((LCD_WIDTH*3 + 3) & ~3)
365 #endif
367 #define BMP_HEADERSIZE (54 + 4 * BMP_NUMCOLORS)
368 #define BMP_DATASIZE (BMP_LINESIZE * LCD_HEIGHT)
369 #define BMP_TOTALSIZE (BMP_HEADERSIZE + BMP_DATASIZE)
371 #define LE16_CONST(x) (x)&0xff, ((x)>>8)&0xff
372 #define LE32_CONST(x) (x)&0xff, ((x)>>8)&0xff, ((x)>>16)&0xff, ((x)>>24)&0xff
374 static const unsigned char bmpheader[] =
376 0x42, 0x4d, /* 'BM' */
377 LE32_CONST(BMP_TOTALSIZE), /* Total file size */
378 0x00, 0x00, 0x00, 0x00, /* Reserved */
379 LE32_CONST(BMP_HEADERSIZE), /* Offset to start of pixel data */
381 0x28, 0x00, 0x00, 0x00, /* Size of (2nd) header */
382 LE32_CONST(LCD_WIDTH), /* Width in pixels */
383 LE32_CONST(LCD_HEIGHT), /* Height in pixels */
384 0x01, 0x00, /* Number of planes (always 1) */
385 LE16_CONST(BMP_BPP), /* Bits per pixel 1/4/8/16/24 */
386 LE32_CONST(BMP_COMPRESSION),/* Compression mode */
387 LE32_CONST(BMP_DATASIZE), /* Size of bitmap data */
388 0xc4, 0x0e, 0x00, 0x00, /* Horizontal resolution (pixels/meter) */
389 0xc4, 0x0e, 0x00, 0x00, /* Vertical resolution (pixels/meter) */
390 LE32_CONST(BMP_NUMCOLORS), /* Number of used colours */
391 LE32_CONST(BMP_NUMCOLORS), /* Number of important colours */
393 #if LCD_DEPTH == 1
394 0x90, 0xee, 0x90, 0x00, /* Colour #0 */
395 0x00, 0x00, 0x00, 0x00 /* Colour #1 */
396 #elif LCD_DEPTH == 2
397 0xe6, 0xd8, 0xad, 0x00, /* Colour #0 */
398 0x99, 0x90, 0x73, 0x00, /* Colour #1 */
399 0x4c, 0x48, 0x39, 0x00, /* Colour #2 */
400 0x00, 0x00, 0x00, 0x00 /* Colour #3 */
401 #elif LCD_DEPTH == 16
402 0x00, 0xf8, 0x00, 0x00, /* red bitfield mask */
403 0xe0, 0x07, 0x00, 0x00, /* green bitfield mask */
404 0x1f, 0x00, 0x00, 0x00 /* blue bitfield mask */
405 #endif
408 static void (*screen_dump_hook)(int fh) = NULL;
410 void screen_dump(void)
412 int fh;
413 char filename[MAX_PATH];
414 int bx, by;
415 #if LCD_DEPTH == 1
416 static unsigned char line_block[8][BMP_LINESIZE];
417 #elif LCD_DEPTH == 2
418 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
419 static unsigned char line_block[BMP_LINESIZE];
420 #else
421 static unsigned char line_block[4][BMP_LINESIZE];
422 #endif
423 #elif LCD_DEPTH == 16
424 static unsigned short line_block[BMP_LINESIZE/2];
425 #endif
427 #if CONFIG_RTC
428 create_datetime_filename(filename, "", "dump ", ".bmp", false);
429 #else
430 create_numbered_filename(filename, "", "dump_", ".bmp", 4
431 IF_CNFN_NUM_(, NULL));
432 #endif
434 fh = creat(filename);
435 if (fh < 0)
436 return;
438 if (screen_dump_hook)
440 screen_dump_hook(fh);
442 else
444 write(fh, bmpheader, sizeof(bmpheader));
446 /* BMP image goes bottom up */
447 #if LCD_DEPTH == 1
448 for (by = LCD_FBHEIGHT - 1; by >= 0; by--)
450 unsigned char *src = &lcd_framebuffer[by][0];
451 unsigned char *dst = &line_block[0][0];
453 memset(line_block, 0, sizeof(line_block));
454 for (bx = LCD_WIDTH/8; bx > 0; bx--)
456 unsigned dst_mask = 0x80;
457 int ix;
459 for (ix = 8; ix > 0; ix--)
461 unsigned char *dst_blk = dst;
462 unsigned src_byte = *src++;
463 int iy;
465 for (iy = 8; iy > 0; iy--)
467 if (src_byte & 0x80)
468 *dst_blk |= dst_mask;
469 src_byte <<= 1;
470 dst_blk += BMP_LINESIZE;
472 dst_mask >>= 1;
474 dst++;
477 write(fh, line_block, sizeof(line_block));
479 #elif LCD_DEPTH == 2
480 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
481 for (by = LCD_FBHEIGHT - 1; by >= 0; by--)
483 unsigned char *src = &lcd_framebuffer[by][0];
484 unsigned char *dst = line_block;
486 memset(line_block, 0, sizeof(line_block));
487 for (bx = LCD_FBWIDTH; bx > 0; bx--)
489 unsigned src_byte = *src++;
491 *dst++ = ((src_byte >> 2) & 0x30) | ((src_byte >> 4) & 0x03);
492 *dst++ = ((src_byte << 2) & 0x30) | (src_byte & 0x03);
495 write(fh, line_block, sizeof(line_block));
497 #else /* VERTICAL_PACKING */
498 for (by = LCD_FBHEIGHT - 1; by >= 0; by--)
500 unsigned char *src = &lcd_framebuffer[by][0];
501 unsigned char *dst = &line_block[3][0];
503 memset(line_block, 0, sizeof(line_block));
504 for (bx = LCD_WIDTH/2; bx > 0; bx--)
506 unsigned char *dst_blk = dst++;
507 unsigned src_byte0 = *src++;
508 unsigned src_byte1 = *src++;
509 int iy;
511 for (iy = 4; iy > 0; iy--)
513 *dst_blk = ((src_byte0 & 3) << 4) | (src_byte1 & 3);
514 src_byte0 >>= 2;
515 src_byte1 >>= 2;
516 dst_blk -= BMP_LINESIZE;
520 write(fh, line_block, sizeof(line_block));
522 #endif
523 #elif LCD_DEPTH == 16
524 for (by = LCD_HEIGHT - 1; by >= 0; by--)
526 unsigned short *src = &lcd_framebuffer[by][0];
527 unsigned short *dst = line_block;
529 memset(line_block, 0, sizeof(line_block));
530 for (bx = LCD_WIDTH; bx > 0; bx--)
532 #if (LCD_PIXELFORMAT == RGB565SWAPPED)
533 /* iPod LCD data is big endian although the CPU is not */
534 *dst++ = htobe16(*src++);
535 #else
536 *dst++ = htole16(*src++);
537 #endif
540 write(fh, line_block, sizeof(line_block));
542 #endif /* LCD_DEPTH */
545 close(fh);
548 void screen_dump_set_hook(void (*hook)(int fh))
550 screen_dump_hook = hook;
553 #endif /* HAVE_LCD_BITMAP */
555 /* parse a line from a configuration file. the line format is:
557 name: value
559 Any whitespace before setting name or value (after ':') is ignored.
560 A # as first non-whitespace character discards the whole line.
561 Function sets pointers to null-terminated setting name and value.
562 Returns false if no valid config entry was found.
565 bool settings_parseline(char* line, char** name, char** value)
567 char* ptr;
569 while ( isspace(*line) )
570 line++;
572 if ( *line == '#' )
573 return false;
575 ptr = strchr(line, ':');
576 if ( !ptr )
577 return false;
579 *name = line;
580 *ptr = 0;
581 ptr++;
582 while (isspace(*ptr))
583 ptr++;
584 *value = ptr;
585 return true;
588 static void system_flush(void)
590 tree_flush();
591 call_ata_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
594 static void system_restore(void)
596 tree_restore();
599 static bool clean_shutdown(void (*callback)(void *), void *parameter)
601 #ifdef SIMULATOR
602 (void)callback;
603 (void)parameter;
604 call_ata_idle_notifys(true);
605 exit(0);
606 #else
607 long msg_id = -1;
608 int i;
610 scrobbler_poweroff();
612 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
613 if(!charger_inserted())
614 #endif
616 bool batt_safe = battery_level_safe();
617 int audio_stat = audio_status();
619 FOR_NB_SCREENS(i)
620 screens[i].clear_display();
621 #ifdef X5_BACKLIGHT_SHUTDOWN
622 x5_backlight_shutdown();
623 #endif
624 if (batt_safe)
626 #ifdef HAVE_TAGCACHE
627 if (!tagcache_prepare_shutdown())
629 cancel_shutdown();
630 gui_syncsplash(HZ, ID2P(LANG_TAGCACHE_BUSY));
631 return false;
633 #endif
634 if (battery_level() > 10)
635 gui_syncsplash(0, str(LANG_SHUTTINGDOWN));
636 else
638 msg_id = LANG_WARNING_BATTERY_LOW;
639 gui_syncsplash(0, "%s %s",
640 str(LANG_WARNING_BATTERY_LOW),
641 str(LANG_SHUTTINGDOWN));
644 else
646 msg_id = LANG_WARNING_BATTERY_EMPTY;
647 gui_syncsplash(0, "%s %s",
648 str(LANG_WARNING_BATTERY_EMPTY),
649 str(LANG_SHUTTINGDOWN));
652 if (global_settings.fade_on_stop
653 && (audio_stat & AUDIO_STATUS_PLAY))
655 fade(0);
658 if (batt_safe) /* do not save on critical battery */
660 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
661 if (audio_stat & AUDIO_STATUS_RECORD)
663 audio_stop_recording();
664 /* wait for stop to complete */
665 while (audio_status() & AUDIO_STATUS_RECORD)
666 sleep(1);
668 #endif
669 /* audio_stop_recording == audio_stop for HWCODEC */
670 audio_stop();
672 if (callback != NULL)
673 callback(parameter);
675 #if CONFIG_CODEC != SWCODEC
676 /* wait for audio_stop or audio_stop_recording to complete */
677 while (audio_status())
678 sleep(1);
679 #endif
681 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
682 audio_close_recording();
683 #endif
685 if(talk_menus_enabled())
687 bool enqueue = false;
688 if(msg_id != -1)
690 talk_id(msg_id, enqueue);
691 enqueue = true;
693 talk_id(LANG_SHUTTINGDOWN, enqueue);
694 #if CONFIG_CODEC == SWCODEC
695 voice_wait();
696 #endif
699 system_flush();
700 #ifdef HAVE_EEPROM_SETTINGS
701 if (firmware_settings.initialized)
703 firmware_settings.disk_clean = true;
704 firmware_settings.bl_version = 0;
705 eeprom_settings_store();
707 #endif
709 #ifdef HAVE_DIRCACHE
710 else
711 dircache_disable();
712 #endif
714 shutdown_hw();
716 #endif
717 return false;
720 bool list_stop_handler(void)
722 bool ret = false;
724 /* Stop the music if it is playing */
725 if(audio_status())
727 if (!global_settings.party_mode)
729 if (global_settings.fade_on_stop)
730 fade(0);
731 bookmark_autobookmark();
732 audio_stop();
733 ret = true; /* bookmarking can make a refresh necessary */
736 #if CONFIG_CHARGING
737 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
738 else
740 if (charger_inserted())
741 charging_splash();
742 else
743 shutdown_screen(); /* won't return if shutdown actually happens */
745 ret = true; /* screen is dirty, caller needs to refresh */
747 #endif
748 #ifndef HAVE_POWEROFF_WHILE_CHARGING
750 static long last_off = 0;
752 if (TIME_BEFORE(current_tick, last_off + HZ/2))
754 if (charger_inserted())
756 charging_splash();
757 ret = true; /* screen is dirty, caller needs to refresh */
760 last_off = current_tick;
762 #endif
763 #endif /* CONFIG_CHARGING */
764 return ret;
767 #if CONFIG_CHARGING
768 static bool waiting_to_resume_play = false;
769 static long play_resume_tick;
771 static void car_adapter_mode_processing(bool inserted)
773 if (global_settings.car_adapter_mode)
775 if(inserted)
778 * Just got plugged in, delay & resume if we were playing
780 if (audio_status() & AUDIO_STATUS_PAUSE)
782 /* delay resume a bit while the engine is cranking */
783 play_resume_tick = current_tick + HZ*5;
784 waiting_to_resume_play = true;
787 else
790 * Just got unplugged, pause if playing
792 if ((audio_status() & AUDIO_STATUS_PLAY) &&
793 !(audio_status() & AUDIO_STATUS_PAUSE))
795 if (global_settings.fade_on_stop)
796 fade(0);
797 else
798 audio_pause();
804 static void car_adapter_tick(void)
806 if (waiting_to_resume_play)
808 if (TIME_AFTER(current_tick, play_resume_tick))
810 if (audio_status() & AUDIO_STATUS_PAUSE)
812 audio_resume();
814 waiting_to_resume_play = false;
819 void car_adapter_mode_init(void)
821 tick_add_task(car_adapter_tick);
823 #endif
825 #ifdef HAVE_HEADPHONE_DETECTION
826 static void unplug_change(bool inserted)
828 if (global_settings.unplug_mode)
830 if (inserted)
832 if ( global_settings.unplug_mode > 1 )
833 audio_resume();
834 backlight_on();
835 } else {
836 audio_pause();
838 if (global_settings.unplug_rw)
840 if ( audio_current_track()->elapsed >
841 (unsigned long)(global_settings.unplug_rw*1000))
842 audio_ff_rewind(audio_current_track()->elapsed -
843 (global_settings.unplug_rw*1000));
844 else
845 audio_ff_rewind(0);
850 #endif
852 long default_event_handler_ex(long event, void (*callback)(void *), void *parameter)
854 switch(event)
856 case SYS_USB_CONNECTED:
857 if (callback != NULL)
858 callback(parameter);
859 #ifdef HAVE_MMC
860 if (!mmc_touched() ||
861 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
862 #endif
864 scrobbler_flush_cache();
865 system_flush();
866 #ifdef BOOTFILE
867 #if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
868 check_bootfile(false); /* gets initial size */
869 #endif
870 #endif
871 usb_screen();
872 #ifdef BOOTFILE
873 #if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
874 check_bootfile(true);
875 #endif
876 #endif
877 system_restore();
879 return SYS_USB_CONNECTED;
880 case SYS_POWEROFF:
881 if (!clean_shutdown(callback, parameter))
882 return SYS_POWEROFF;
883 break;
884 #if CONFIG_CHARGING
885 case SYS_CHARGER_CONNECTED:
886 car_adapter_mode_processing(true);
887 return SYS_CHARGER_CONNECTED;
889 case SYS_CHARGER_DISCONNECTED:
890 car_adapter_mode_processing(false);
891 return SYS_CHARGER_DISCONNECTED;
892 #endif
893 #ifdef HAVE_HEADPHONE_DETECTION
894 case SYS_PHONE_PLUGGED:
895 unplug_change(true);
896 return SYS_PHONE_PLUGGED;
898 case SYS_PHONE_UNPLUGGED:
899 unplug_change(false);
900 return SYS_PHONE_UNPLUGGED;
901 #endif
903 return 0;
906 long default_event_handler(long event)
908 return default_event_handler_ex(event, NULL, NULL);
911 int show_logo( void )
913 #ifdef HAVE_LCD_BITMAP
914 char version[32];
915 int font_h, font_w;
917 snprintf(version, sizeof(version), "Ver. %s", appsversion);
919 lcd_clear_display();
920 lcd_bitmap(rockboxlogo, 0, 10, BMPWIDTH_rockboxlogo, BMPHEIGHT_rockboxlogo);
921 lcd_setfont(FONT_SYSFIXED);
922 lcd_getstringsize((unsigned char *)"A", &font_w, &font_h);
923 lcd_putsxy((LCD_WIDTH/2) - ((strlen(version)*font_w)/2),
924 LCD_HEIGHT-font_h, (unsigned char *)version);
925 lcd_setfont(FONT_UI);
927 #else
928 char *rockbox = " ROCKbox!";
930 lcd_clear_display();
931 lcd_double_height(true);
932 lcd_puts(0, 0, rockbox);
933 lcd_puts_scroll(0, 1, appsversion);
934 #endif
935 lcd_update();
937 #ifdef HAVE_REMOTE_LCD
938 lcd_remote_clear_display();
939 lcd_remote_bitmap(remote_rockboxlogo, 0, 10, BMPWIDTH_remote_rockboxlogo,
940 BMPHEIGHT_remote_rockboxlogo);
941 lcd_remote_setfont(FONT_SYSFIXED);
942 lcd_remote_getstringsize((unsigned char *)"A", &font_w, &font_h);
943 lcd_remote_putsxy((LCD_REMOTE_WIDTH/2) - ((strlen(version)*font_w)/2),
944 LCD_REMOTE_HEIGHT-font_h, (unsigned char *)version);
945 lcd_remote_setfont(FONT_UI);
946 lcd_remote_update();
947 #endif
949 return 0;
952 #if CONFIG_CODEC == SWCODEC
953 int get_replaygain_mode(bool have_track_gain, bool have_album_gain)
955 int type;
957 bool track = ((global_settings.replaygain_type == REPLAYGAIN_TRACK)
958 || ((global_settings.replaygain_type == REPLAYGAIN_SHUFFLE)
959 && global_settings.playlist_shuffle));
961 type = (!track && have_album_gain) ? REPLAYGAIN_ALBUM
962 : have_track_gain ? REPLAYGAIN_TRACK : -1;
964 return type;
966 #endif
968 #ifdef BOOTFILE
969 #if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
971 memorize/compare details about the BOOTFILE
972 we don't use dircache because it may not be up to date after
973 USB disconnect (scanning in the background)
975 void check_bootfile(bool do_rolo)
977 static unsigned short wrtdate = 0;
978 static unsigned short wrttime = 0;
979 DIR* dir = NULL;
980 struct dirent* entry = NULL;
982 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
983 dir = opendir(BOOTDIR);
985 if(!dir) return; /* do we want an error splash? */
987 /* loop all files in BOOTDIR */
988 while(0 != (entry = readdir(dir)))
990 if(!strcasecmp(entry->d_name, BOOTFILE))
992 /* found the bootfile */
993 if(wrtdate && do_rolo)
995 if((entry->wrtdate != wrtdate) ||
996 (entry->wrttime != wrttime))
998 char *lines[] = { ID2P(LANG_BOOT_CHANGED),
999 ID2P(LANG_REBOOT_NOW) };
1000 struct text_message message={ lines, 2 };
1001 button_clear_queue(); /* Empty the keyboard buffer */
1002 if(gui_syncyesno_run(&message, NULL, NULL) == YESNO_YES)
1003 rolo_load(BOOTDIR "/" BOOTFILE);
1006 wrtdate = entry->wrtdate;
1007 wrttime = entry->wrttime;
1010 closedir(dir);
1012 #endif
1013 #endif
1015 /* check range, set volume and save settings */
1016 void setvol(void)
1018 const int min_vol = sound_min(SOUND_VOLUME);
1019 const int max_vol = sound_max(SOUND_VOLUME);
1020 if (global_settings.volume < min_vol)
1021 global_settings.volume = min_vol;
1022 if (global_settings.volume > max_vol)
1023 global_settings.volume = max_vol;
1024 sound_set_volume(global_settings.volume);
1025 settings_save();
1028 #ifdef HAVE_LCD_COLOR
1030 * Helper function to convert a string of 6 hex digits to a native colour
1033 #define hex2dec(c) (((c) >= '0' && ((c) <= '9')) ? (toupper(c)) - '0' : \
1034 (toupper(c)) - 'A' + 10)
1036 int hex_to_rgb(const char* hex)
1037 { int ok = 1;
1038 int i;
1039 int red, green, blue;
1041 if (strlen(hex) == 6) {
1042 for (i=0; i < 6; i++ ) {
1043 if (!isxdigit(hex[i])) {
1044 ok=0;
1045 break;
1049 if (ok) {
1050 red = (hex2dec(hex[0]) << 4) | hex2dec(hex[1]);
1051 green = (hex2dec(hex[2]) << 4) | hex2dec(hex[3]);
1052 blue = (hex2dec(hex[4]) << 4) | hex2dec(hex[5]);
1053 return LCD_RGBPACK(red,green,blue);
1057 return 0;
1059 #endif /* HAVE_LCD_COLOR */