1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Daniel Stenberg
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
35 #include "lcd-remote.h"
38 #include "timefuncs.h"
43 #include "mp3_playback.h"
46 #include "ata_idle_notify.h"
49 #include "powermgmt.h"
50 #include "backlight.h"
55 #include "scrobbler.h"
61 #ifdef IPOD_ACCESSORY_PROTOCOL
65 #if (CONFIG_STORAGE & STORAGE_MMC)
69 #include "eeprom_settings.h"
70 #if defined(HAVE_RECORDING) && !defined(__PCTOOL__)
71 #include "recording.h"
73 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
76 #endif /* End HAVE_LCD_BITMAP */
82 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
87 /* units used with output_dyn_value */
88 const unsigned char * const byte_units
[] =
96 const unsigned char * const * const kbyte_units
= &byte_units
[1];
98 /* Format a large-range value for output, using the appropriate unit so that
99 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
100 * units) if possible, and 3 significant digits are shown. If a buffer is
101 * given, the result is snprintf()'d into that buffer, otherwise the result is
103 char *output_dyn_value(char *buf
, int buf_size
, int value
,
104 const unsigned char * const *units
, bool bin_scale
)
106 int scale
= bin_scale
? 1024 : 1000;
111 while (value
>= scale
)
113 fraction
= value
% scale
;
118 fraction
= fraction
* 1000 / 1024;
120 if (value
>= 100 || !unit_no
)
122 else if (value
>= 10)
123 snprintf(tbuf
, sizeof(tbuf
), "%01d", fraction
/ 100);
125 snprintf(tbuf
, sizeof(tbuf
), "%02d", fraction
/ 10);
130 snprintf(buf
, buf_size
, "%d%s%s%s", value
, str(LANG_POINT
),
131 tbuf
, P2STR(units
[unit_no
]));
133 snprintf(buf
, buf_size
, "%d%s", value
, P2STR(units
[unit_no
]));
137 talk_fractional(tbuf
, value
, P2ID(units
[unit_no
]));
142 /* Ask the user if they really want to erase the current dynamic playlist
143 * returns true if the playlist should be replaced */
144 bool warn_on_pl_erase(void)
146 if (global_settings
.warnon_erase_dynplaylist
&&
147 !global_settings
.party_mode
&&
148 playlist_modified(NULL
))
150 static const char *lines
[] =
151 {ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
152 static const struct text_message message
={lines
, 1};
154 return (gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
);
160 /* Read (up to) a line of text from fd into buffer and return number of bytes
161 * read (which may be larger than the number of bytes stored in buffer). If
162 * an error occurs, -1 is returned (and buffer contains whatever could be
163 * read). A line is terminated by a LF char. Neither LF nor CR chars are
166 int read_line(int fd
, char* buffer
, int buffer_size
)
173 while (count
< buffer_size
)
177 if (1 != read(fd
, &c
, 1))
191 buffer
[MIN(count
, buffer_size
- 1)] = 0;
193 return errno
? -1 : num_read
;
196 /* Performance optimized version of the previous function. */
197 int fast_readline(int fd
, char *buf
, int buf_size
, void *parameters
,
198 int (*callback
)(int n
, const char *buf
, void *parameters
))
208 rc
= read(fd
, &buf
[pos
], buf_size
- pos
- 1);
212 if ( (p
= strchr(buf
, '\r')) != NULL
)
220 if ( (p
= strchr(p
, '\n')) != NULL
)
226 rc
= callback(count
, buf
, parameters
);
233 pos
= buf_size
- ((long)next
- (long)buf
) - 1;
234 memmove(buf
, next
, pos
);
243 /* parse a line from a configuration file. the line format is:
247 Any whitespace before setting name or value (after ':') is ignored.
248 A # as first non-whitespace character discards the whole line.
249 Function sets pointers to null-terminated setting name and value.
250 Returns false if no valid config entry was found.
253 bool settings_parseline(char* line
, char** name
, char** value
)
257 line
= skip_whitespace(line
);
262 ptr
= strchr(line
, ':');
269 ptr
= skip_whitespace(ptr
);
274 static void system_flush(void)
276 scrobbler_shutdown();
279 call_storage_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
282 static void system_restore(void)
288 static bool clean_shutdown(void (*callback
)(void *), void *parameter
)
293 bookmark_autobookmark();
294 call_storage_idle_notifys(true);
300 scrobbler_poweroff();
302 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
303 if(!charger_inserted())
306 bool batt_safe
= battery_level_safe();
307 int audio_stat
= audio_status();
311 screens
[i
].clear_display();
318 if (!tagcache_prepare_shutdown())
321 splash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
325 if (battery_level() > 10)
326 splash(0, str(LANG_SHUTTINGDOWN
));
329 msg_id
= LANG_WARNING_BATTERY_LOW
;
330 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_LOW
),
331 str(LANG_SHUTTINGDOWN
));
336 msg_id
= LANG_WARNING_BATTERY_EMPTY
;
337 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_EMPTY
),
338 str(LANG_SHUTTINGDOWN
));
341 if (global_settings
.fade_on_stop
342 && (audio_stat
& AUDIO_STATUS_PLAY
))
347 if (batt_safe
) /* do not save on critical battery */
349 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
350 if (audio_stat
& AUDIO_STATUS_RECORD
)
352 rec_command(RECORDING_CMD_STOP
);
353 /* wait for stop to complete */
354 while (audio_status() & AUDIO_STATUS_RECORD
)
358 bookmark_autobookmark();
360 /* audio_stop_recording == audio_stop for HWCODEC */
363 if (callback
!= NULL
)
366 #if CONFIG_CODEC != SWCODEC
367 /* wait for audio_stop or audio_stop_recording to complete */
368 while (audio_status())
372 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
373 audio_close_recording();
376 if(global_settings
.talk_menu
)
378 bool enqueue
= false;
381 talk_id(msg_id
, enqueue
);
384 talk_id(LANG_SHUTTINGDOWN
, enqueue
);
385 #if CONFIG_CODEC == SWCODEC
391 #ifdef HAVE_EEPROM_SETTINGS
392 if (firmware_settings
.initialized
)
394 firmware_settings
.disk_clean
= true;
395 firmware_settings
.bl_version
= 0;
396 eeprom_settings_store();
411 bool list_stop_handler(void)
415 /* Stop the music if it is playing */
418 if (!global_settings
.party_mode
)
420 if (global_settings
.fade_on_stop
)
422 bookmark_autobookmark();
424 ret
= true; /* bookmarking can make a refresh necessary */
428 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
431 if (charger_inserted())
434 shutdown_screen(); /* won't return if shutdown actually happens */
436 ret
= true; /* screen is dirty, caller needs to refresh */
439 #ifndef HAVE_POWEROFF_WHILE_CHARGING
441 static long last_off
= 0;
443 if (TIME_BEFORE(current_tick
, last_off
+ HZ
/2))
445 if (charger_inserted())
448 ret
= true; /* screen is dirty, caller needs to refresh */
451 last_off
= current_tick
;
454 #endif /* CONFIG_CHARGING */
459 static bool waiting_to_resume_play
= false;
460 static long play_resume_tick
;
462 static void car_adapter_mode_processing(bool inserted
)
464 if (global_settings
.car_adapter_mode
)
469 * Just got plugged in, delay & resume if we were playing
471 if (audio_status() & AUDIO_STATUS_PAUSE
)
473 /* delay resume a bit while the engine is cranking */
474 play_resume_tick
= current_tick
+ HZ
*5;
475 waiting_to_resume_play
= true;
481 * Just got unplugged, pause if playing
483 if ((audio_status() & AUDIO_STATUS_PLAY
) &&
484 !(audio_status() & AUDIO_STATUS_PAUSE
))
486 if (global_settings
.fade_on_stop
)
491 waiting_to_resume_play
= false;
496 static void car_adapter_tick(void)
498 if (waiting_to_resume_play
)
500 if (TIME_AFTER(current_tick
, play_resume_tick
))
502 if (audio_status() & AUDIO_STATUS_PAUSE
)
504 queue_broadcast(SYS_CAR_ADAPTER_RESUME
, 0);
506 waiting_to_resume_play
= false;
511 void car_adapter_mode_init(void)
513 tick_add_task(car_adapter_tick
);
517 #ifdef HAVE_HEADPHONE_DETECTION
518 static void unplug_change(bool inserted
)
520 static bool headphone_caused_pause
= false;
522 if (global_settings
.unplug_mode
)
524 int audio_stat
= audio_status();
527 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
528 headphone_caused_pause
&&
529 global_settings
.unplug_mode
> 1 )
532 headphone_caused_pause
= false;
534 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
535 !(audio_stat
& AUDIO_STATUS_PAUSE
))
537 headphone_caused_pause
= true;
540 if (global_settings
.unplug_rw
)
542 if (audio_current_track()->elapsed
>
543 (unsigned long)(global_settings
.unplug_rw
*1000))
544 audio_ff_rewind(audio_current_track()->elapsed
-
545 (global_settings
.unplug_rw
*1000));
555 long default_event_handler_ex(long event
, void (*callback
)(void *), void *parameter
)
559 case SYS_BATTERY_UPDATE
:
560 if(global_settings
.talk_battery_level
)
562 talk_ids(true, VOICE_PAUSE
, VOICE_PAUSE
,
564 TALK_ID(battery_level(), UNIT_PERCENT
),
566 talk_force_enqueue_next();
569 case SYS_USB_CONNECTED
:
570 if (callback
!= NULL
)
572 #if (CONFIG_STORAGE & STORAGE_MMC)
573 if (!mmc_touched() ||
574 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED
))
579 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
580 check_bootfile(false); /* gets initial size */
585 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
586 check_bootfile(true);
591 return SYS_USB_CONNECTED
;
593 if (!clean_shutdown(callback
, parameter
))
597 case SYS_CHARGER_CONNECTED
:
598 car_adapter_mode_processing(true);
599 return SYS_CHARGER_CONNECTED
;
601 case SYS_CHARGER_DISCONNECTED
:
602 car_adapter_mode_processing(false);
603 return SYS_CHARGER_DISCONNECTED
;
605 case SYS_CAR_ADAPTER_RESUME
:
607 return SYS_CAR_ADAPTER_RESUME
;
609 #ifdef HAVE_HEADPHONE_DETECTION
610 case SYS_PHONE_PLUGGED
:
612 return SYS_PHONE_PLUGGED
;
614 case SYS_PHONE_UNPLUGGED
:
615 unplug_change(false);
616 return SYS_PHONE_UNPLUGGED
;
618 #ifdef IPOD_ACCESSORY_PROTOCOL
619 case SYS_IAP_PERIODIC
:
621 return SYS_IAP_PERIODIC
;
622 case SYS_IAP_HANDLEPKT
:
624 return SYS_IAP_HANDLEPKT
;
630 long default_event_handler(long event
)
632 return default_event_handler_ex(event
, NULL
, NULL
);
635 int show_logo( void )
637 #ifdef HAVE_LCD_BITMAP
641 snprintf(version
, sizeof(version
), "Ver. %s", appsversion
);
644 #ifdef SANSA_CLIP /* display the logo in the blue area of the screen */
645 lcd_setfont(FONT_SYSFIXED
);
646 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
647 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
648 0, (unsigned char *)version
);
649 lcd_bitmap(rockboxlogo
, 0, 16, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
651 lcd_bitmap(rockboxlogo
, 0, 10, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
652 lcd_setfont(FONT_SYSFIXED
);
653 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
654 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
655 LCD_HEIGHT
-font_h
, (unsigned char *)version
);
657 lcd_setfont(FONT_UI
);
660 char *rockbox
= " ROCKbox!";
663 lcd_double_height(true);
664 lcd_puts(0, 0, rockbox
);
665 lcd_puts_scroll(0, 1, appsversion
);
669 #ifdef HAVE_REMOTE_LCD
670 lcd_remote_clear_display();
671 lcd_remote_bitmap(remote_rockboxlogo
, 0, 10, BMPWIDTH_remote_rockboxlogo
,
672 BMPHEIGHT_remote_rockboxlogo
);
673 lcd_remote_setfont(FONT_SYSFIXED
);
674 lcd_remote_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
675 lcd_remote_putsxy((LCD_REMOTE_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
676 LCD_REMOTE_HEIGHT
-font_h
, (unsigned char *)version
);
677 lcd_remote_setfont(FONT_UI
);
684 #if CONFIG_CODEC == SWCODEC
685 int get_replaygain_mode(bool have_track_gain
, bool have_album_gain
)
689 bool track
= ((global_settings
.replaygain_type
== REPLAYGAIN_TRACK
)
690 || ((global_settings
.replaygain_type
== REPLAYGAIN_SHUFFLE
)
691 && global_settings
.playlist_shuffle
));
693 type
= (!track
&& have_album_gain
) ? REPLAYGAIN_ALBUM
694 : have_track_gain
? REPLAYGAIN_TRACK
: -1;
701 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
703 memorize/compare details about the BOOTFILE
704 we don't use dircache because it may not be up to date after
705 USB disconnect (scanning in the background)
707 void check_bootfile(bool do_rolo
)
709 static unsigned short wrtdate
= 0;
710 static unsigned short wrttime
= 0;
712 struct dirent
* entry
= NULL
;
714 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
715 dir
= opendir(BOOTDIR
);
717 if(!dir
) return; /* do we want an error splash? */
719 /* loop all files in BOOTDIR */
720 while(0 != (entry
= readdir(dir
)))
722 if(!strcasecmp(entry
->d_name
, BOOTFILE
))
724 /* found the bootfile */
725 if(wrtdate
&& do_rolo
)
727 if((entry
->wrtdate
!= wrtdate
) ||
728 (entry
->wrttime
!= wrttime
))
730 static const char *lines
[] = { ID2P(LANG_BOOT_CHANGED
),
731 ID2P(LANG_REBOOT_NOW
) };
732 static const struct text_message message
={ lines
, 2 };
733 button_clear_queue(); /* Empty the keyboard buffer */
734 if(gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
)
735 rolo_load(BOOTDIR
"/" BOOTFILE
);
738 wrtdate
= entry
->wrtdate
;
739 wrttime
= entry
->wrttime
;
747 /* check range, set volume and save settings */
750 const int min_vol
= sound_min(SOUND_VOLUME
);
751 const int max_vol
= sound_max(SOUND_VOLUME
);
752 if (global_settings
.volume
< min_vol
)
753 global_settings
.volume
= min_vol
;
754 if (global_settings
.volume
> max_vol
)
755 global_settings
.volume
= max_vol
;
756 sound_set_volume(global_settings
.volume
);
760 char* strrsplt(char* str
, int c
)
762 char* s
= strrchr(str
, c
);
776 /* Test file existence, using dircache of possible */
777 bool file_exists(const char *file
)
781 if (!file
|| strlen(file
) <= 0)
785 if (dircache_is_enabled())
786 return (dircache_get_entry_ptr(file
) != NULL
);
789 fd
= open(file
, O_RDONLY
);
796 bool dir_exists(const char *path
)
798 DIR* d
= opendir(path
);
806 * removes the extension of filename (if it doesn't start with a .)
807 * puts the result in buffer
809 char *strip_extension(char* buffer
, int buffer_size
, const char *filename
)
811 char *dot
= strrchr(filename
, '.');
814 if (buffer_size
<= 0)
819 buffer_size
--; /* Make room for end nil */
821 if (dot
!= 0 && filename
[0] != '.')
823 len
= dot
- filename
;
824 len
= MIN(len
, buffer_size
);
831 strlcpy(buffer
, filename
, len
+ 1);
835 #endif /* !defined(__PCTOOL__) */
837 char* skip_whitespace(char* const str
)
847 /* Format time into buf.
849 * buf - buffer to format to.
850 * buf_size - size of buffer.
851 * t - time to format, in milliseconds.
853 void format_time(char* buf
, int buf_size
, long t
)
857 snprintf(buf
, buf_size
, "%d:%02d",
858 (int) (t
/ 60000), (int) (t
% 60000 / 1000));
862 snprintf(buf
, buf_size
, "%d:%02d:%02d",
863 (int) (t
/ 3600000), (int) (t
% 3600000 / 60000),
864 (int) (t
% 60000 / 1000));
869 /** Open a UTF-8 file and set file descriptor to first byte after BOM.
870 * If no BOM is present this behaves like open().
871 * If the file is opened for writing and O_TRUNC is set, write a BOM to
872 * the opened file and leave the file pointer set after the BOM.
874 #define BOM "\xef\xbb\xbf"
877 int open_utf8(const char* pathname
, int flags
)
880 unsigned char bom
[BOM_SIZE
];
882 fd
= open(pathname
, flags
);
886 if(flags
& (O_TRUNC
| O_WRONLY
))
888 write(fd
, BOM
, BOM_SIZE
);
892 read(fd
, bom
, BOM_SIZE
);
894 if(memcmp(bom
, BOM
, BOM_SIZE
))
895 lseek(fd
, 0, SEEK_SET
);
901 #ifdef HAVE_LCD_COLOR
903 * Helper function to convert a string of 6 hex digits to a native colour
906 static int hex2dec(int c
)
908 return (((c
) >= '0' && ((c
) <= '9')) ? (c
) - '0' :
909 (toupper(c
)) - 'A' + 10);
912 int hex_to_rgb(const char* hex
, int* color
)
914 int red
, green
, blue
;
917 while ((i
< 6) && (isxdigit(hex
[i
])))
923 red
= (hex2dec(hex
[0]) << 4) | hex2dec(hex
[1]);
924 green
= (hex2dec(hex
[2]) << 4) | hex2dec(hex
[3]);
925 blue
= (hex2dec(hex
[4]) << 4) | hex2dec(hex
[5]);
927 *color
= LCD_RGBPACK(red
,green
,blue
);
931 #endif /* HAVE_LCD_COLOR */
933 #ifdef HAVE_LCD_BITMAP
934 /* A simplified scanf - used (at time of writing) by wps parsing functions.
936 fmt - char array specifying the format of each list option. Valid values
938 s - string (sets pointer to string, without copying)
939 c - hex colour (RGB888 - e.g. ff00ff)
940 g - greyscale "colour" (0-3)
941 set_vals - if not NULL 1 is set in the bitplace if the item was read OK
943 first item is LSB, (max 32 items! )
944 Stops parseing if an item is invalid unless the item == '-'
945 sep - list separator (e.g. ',' or '|')
946 str - string to parse, must be terminated by 0 or sep
947 ... - pointers to store the parsed values
949 return value - pointer to char after parsed data, 0 if there was an error.
953 /* '0'-'3' are ASCII 0x30 to 0x33 */
954 #define is0123(x) (((x) & 0xfc) == 0x30)
956 const char* parse_list(const char *fmt
, uint32_t *set_vals
,
957 const char sep
, const char* str
, ...)
960 const char* p
= str
, *f
= fmt
;
971 /* Check for separator, if we're not at the start */
981 case 's': /* string - return a pointer to it (not a copy) */
982 s
= va_arg(ap
, const char **);
985 while (*p
&& *p
!= sep
)
987 set
= (s
[0][0]!='-') && (s
[0][1]!=sep
) ;
991 d
= va_arg(ap
, int*);
994 if (!set_vals
|| *p
!= '-')
996 while (*p
&& *p
!= sep
)
1003 *d
= (*d
* 10) + (*p
++ - '0');
1009 #ifdef HAVE_LCD_COLOR
1010 case 'c': /* colour (rrggbb - e.g. f3c1a8) */
1011 d
= va_arg(ap
, int*);
1013 if (hex_to_rgb(p
, d
) < 0)
1015 if (!set_vals
|| *p
!= '-')
1017 while (*p
&& *p
!= sep
)
1029 #if LCD_DEPTH == 2 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 2)
1030 case 'g': /* greyscale colour (0-3) */
1031 d
= va_arg(ap
, int*);
1038 else if (!set_vals
|| *p
!= '-')
1042 while (*p
&& *p
!= sep
)
1049 default: /* Unknown format type */
1053 if (set_vals
&& set
)
1054 *set_vals
|= BIT_N(i
);