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 /* Format a large-range value for output, using the appropriate unit so that
88 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
89 * units) if possible, and 3 significant digits are shown. If a buffer is
90 * given, the result is snprintf()'d into that buffer, otherwise the result is
92 char *output_dyn_value(char *buf
, int buf_size
, int value
,
93 const unsigned char **units
, bool bin_scale
)
95 int scale
= bin_scale
? 1024 : 1000;
100 while (value
>= scale
)
102 fraction
= value
% scale
;
107 fraction
= fraction
* 1000 / 1024;
109 if (value
>= 100 || !unit_no
)
111 else if (value
>= 10)
112 snprintf(tbuf
, sizeof(tbuf
), "%01d", fraction
/ 100);
114 snprintf(tbuf
, sizeof(tbuf
), "%02d", fraction
/ 10);
119 snprintf(buf
, buf_size
, "%d%s%s%s", value
, str(LANG_POINT
),
120 tbuf
, P2STR(units
[unit_no
]));
122 snprintf(buf
, buf_size
, "%d%s", value
, P2STR(units
[unit_no
]));
126 talk_fractional(tbuf
, value
, P2ID(units
[unit_no
]));
131 /* Ask the user if they really want to erase the current dynamic playlist
132 * returns true if the playlist should be replaced */
133 bool warn_on_pl_erase(void)
135 if (global_settings
.warnon_erase_dynplaylist
&&
136 !global_settings
.party_mode
&&
137 playlist_modified(NULL
))
139 static const char *lines
[] =
140 {ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
141 static const struct text_message message
={lines
, 1};
143 return (gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
);
149 /* Read (up to) a line of text from fd into buffer and return number of bytes
150 * read (which may be larger than the number of bytes stored in buffer). If
151 * an error occurs, -1 is returned (and buffer contains whatever could be
152 * read). A line is terminated by a LF char. Neither LF nor CR chars are
155 int read_line(int fd
, char* buffer
, int buffer_size
)
162 while (count
< buffer_size
)
166 if (1 != read(fd
, &c
, 1))
180 buffer
[MIN(count
, buffer_size
- 1)] = 0;
182 return errno
? -1 : num_read
;
185 /* Performance optimized version of the previous function. */
186 int fast_readline(int fd
, char *buf
, int buf_size
, void *parameters
,
187 int (*callback
)(int n
, const char *buf
, void *parameters
))
197 rc
= read(fd
, &buf
[pos
], buf_size
- pos
- 1);
201 if ( (p
= strchr(buf
, '\r')) != NULL
)
209 if ( (p
= strchr(p
, '\n')) != NULL
)
215 rc
= callback(count
, buf
, parameters
);
222 pos
= buf_size
- ((long)next
- (long)buf
) - 1;
223 memmove(buf
, next
, pos
);
232 /* parse a line from a configuration file. the line format is:
236 Any whitespace before setting name or value (after ':') is ignored.
237 A # as first non-whitespace character discards the whole line.
238 Function sets pointers to null-terminated setting name and value.
239 Returns false if no valid config entry was found.
242 bool settings_parseline(char* line
, char** name
, char** value
)
246 line
= skip_whitespace(line
);
251 ptr
= strchr(line
, ':');
258 ptr
= skip_whitespace(ptr
);
263 static void system_flush(void)
265 scrobbler_shutdown();
268 call_storage_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
271 static void system_restore(void)
277 static bool clean_shutdown(void (*callback
)(void *), void *parameter
)
282 bookmark_autobookmark();
283 call_storage_idle_notifys(true);
289 scrobbler_poweroff();
291 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
292 if(!charger_inserted())
295 bool batt_safe
= battery_level_safe();
296 int audio_stat
= audio_status();
300 screens
[i
].clear_display();
307 if (!tagcache_prepare_shutdown())
310 splash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
314 if (battery_level() > 10)
315 splash(0, str(LANG_SHUTTINGDOWN
));
318 msg_id
= LANG_WARNING_BATTERY_LOW
;
319 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_LOW
),
320 str(LANG_SHUTTINGDOWN
));
325 msg_id
= LANG_WARNING_BATTERY_EMPTY
;
326 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_EMPTY
),
327 str(LANG_SHUTTINGDOWN
));
330 if (global_settings
.fade_on_stop
331 && (audio_stat
& AUDIO_STATUS_PLAY
))
336 if (batt_safe
) /* do not save on critical battery */
338 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
339 if (audio_stat
& AUDIO_STATUS_RECORD
)
341 rec_command(RECORDING_CMD_STOP
);
342 /* wait for stop to complete */
343 while (audio_status() & AUDIO_STATUS_RECORD
)
347 bookmark_autobookmark();
349 /* audio_stop_recording == audio_stop for HWCODEC */
352 if (callback
!= NULL
)
355 #if CONFIG_CODEC != SWCODEC
356 /* wait for audio_stop or audio_stop_recording to complete */
357 while (audio_status())
361 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
362 audio_close_recording();
365 if(global_settings
.talk_menu
)
367 bool enqueue
= false;
370 talk_id(msg_id
, enqueue
);
373 talk_id(LANG_SHUTTINGDOWN
, enqueue
);
374 #if CONFIG_CODEC == SWCODEC
380 #ifdef HAVE_EEPROM_SETTINGS
381 if (firmware_settings
.initialized
)
383 firmware_settings
.disk_clean
= true;
384 firmware_settings
.bl_version
= 0;
385 eeprom_settings_store();
400 bool list_stop_handler(void)
404 /* Stop the music if it is playing */
407 if (!global_settings
.party_mode
)
409 if (global_settings
.fade_on_stop
)
411 bookmark_autobookmark();
413 ret
= true; /* bookmarking can make a refresh necessary */
417 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
420 if (charger_inserted())
423 shutdown_screen(); /* won't return if shutdown actually happens */
425 ret
= true; /* screen is dirty, caller needs to refresh */
428 #ifndef HAVE_POWEROFF_WHILE_CHARGING
430 static long last_off
= 0;
432 if (TIME_BEFORE(current_tick
, last_off
+ HZ
/2))
434 if (charger_inserted())
437 ret
= true; /* screen is dirty, caller needs to refresh */
440 last_off
= current_tick
;
443 #endif /* CONFIG_CHARGING */
448 static bool waiting_to_resume_play
= false;
449 static long play_resume_tick
;
451 static void car_adapter_mode_processing(bool inserted
)
453 if (global_settings
.car_adapter_mode
)
458 * Just got plugged in, delay & resume if we were playing
460 if (audio_status() & AUDIO_STATUS_PAUSE
)
462 /* delay resume a bit while the engine is cranking */
463 play_resume_tick
= current_tick
+ HZ
*5;
464 waiting_to_resume_play
= true;
470 * Just got unplugged, pause if playing
472 if ((audio_status() & AUDIO_STATUS_PLAY
) &&
473 !(audio_status() & AUDIO_STATUS_PAUSE
))
475 if (global_settings
.fade_on_stop
)
480 waiting_to_resume_play
= false;
485 static void car_adapter_tick(void)
487 if (waiting_to_resume_play
)
489 if (TIME_AFTER(current_tick
, play_resume_tick
))
491 if (audio_status() & AUDIO_STATUS_PAUSE
)
493 queue_broadcast(SYS_CAR_ADAPTER_RESUME
, 0);
495 waiting_to_resume_play
= false;
500 void car_adapter_mode_init(void)
502 tick_add_task(car_adapter_tick
);
506 #ifdef HAVE_HEADPHONE_DETECTION
507 static void unplug_change(bool inserted
)
509 static bool headphone_caused_pause
= false;
511 if (global_settings
.unplug_mode
)
513 int audio_stat
= audio_status();
516 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
517 headphone_caused_pause
&&
518 global_settings
.unplug_mode
> 1 )
521 headphone_caused_pause
= false;
523 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
524 !(audio_stat
& AUDIO_STATUS_PAUSE
))
526 headphone_caused_pause
= true;
529 if (global_settings
.unplug_rw
)
531 if (audio_current_track()->elapsed
>
532 (unsigned long)(global_settings
.unplug_rw
*1000))
533 audio_ff_rewind(audio_current_track()->elapsed
-
534 (global_settings
.unplug_rw
*1000));
544 long default_event_handler_ex(long event
, void (*callback
)(void *), void *parameter
)
548 case SYS_BATTERY_UPDATE
:
549 if(global_settings
.talk_battery_level
)
551 talk_ids(true, VOICE_PAUSE
, VOICE_PAUSE
,
553 TALK_ID(battery_level(), UNIT_PERCENT
),
555 talk_force_enqueue_next();
558 case SYS_USB_CONNECTED
:
559 if (callback
!= NULL
)
561 #if (CONFIG_STORAGE & STORAGE_MMC)
562 if (!mmc_touched() ||
563 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED
))
568 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
569 check_bootfile(false); /* gets initial size */
574 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
575 check_bootfile(true);
580 return SYS_USB_CONNECTED
;
582 if (!clean_shutdown(callback
, parameter
))
586 case SYS_CHARGER_CONNECTED
:
587 car_adapter_mode_processing(true);
588 return SYS_CHARGER_CONNECTED
;
590 case SYS_CHARGER_DISCONNECTED
:
591 car_adapter_mode_processing(false);
592 return SYS_CHARGER_DISCONNECTED
;
594 case SYS_CAR_ADAPTER_RESUME
:
596 return SYS_CAR_ADAPTER_RESUME
;
598 #ifdef HAVE_HEADPHONE_DETECTION
599 case SYS_PHONE_PLUGGED
:
601 return SYS_PHONE_PLUGGED
;
603 case SYS_PHONE_UNPLUGGED
:
604 unplug_change(false);
605 return SYS_PHONE_UNPLUGGED
;
607 #ifdef IPOD_ACCESSORY_PROTOCOL
608 case SYS_IAP_PERIODIC
:
610 return SYS_IAP_PERIODIC
;
611 case SYS_IAP_HANDLEPKT
:
613 return SYS_IAP_HANDLEPKT
;
619 long default_event_handler(long event
)
621 return default_event_handler_ex(event
, NULL
, NULL
);
624 int show_logo( void )
626 #ifdef HAVE_LCD_BITMAP
630 snprintf(version
, sizeof(version
), "Ver. %s", appsversion
);
633 #ifdef SANSA_CLIP /* display the logo in the blue area of the screen */
634 lcd_setfont(FONT_SYSFIXED
);
635 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
636 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
637 0, (unsigned char *)version
);
638 lcd_bitmap(rockboxlogo
, 0, 16, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
640 lcd_bitmap(rockboxlogo
, 0, 10, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
641 lcd_setfont(FONT_SYSFIXED
);
642 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
643 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
644 LCD_HEIGHT
-font_h
, (unsigned char *)version
);
646 lcd_setfont(FONT_UI
);
649 char *rockbox
= " ROCKbox!";
652 lcd_double_height(true);
653 lcd_puts(0, 0, rockbox
);
654 lcd_puts_scroll(0, 1, appsversion
);
658 #ifdef HAVE_REMOTE_LCD
659 lcd_remote_clear_display();
660 lcd_remote_bitmap(remote_rockboxlogo
, 0, 10, BMPWIDTH_remote_rockboxlogo
,
661 BMPHEIGHT_remote_rockboxlogo
);
662 lcd_remote_setfont(FONT_SYSFIXED
);
663 lcd_remote_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
664 lcd_remote_putsxy((LCD_REMOTE_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
665 LCD_REMOTE_HEIGHT
-font_h
, (unsigned char *)version
);
666 lcd_remote_setfont(FONT_UI
);
673 #if CONFIG_CODEC == SWCODEC
674 int get_replaygain_mode(bool have_track_gain
, bool have_album_gain
)
678 bool track
= ((global_settings
.replaygain_type
== REPLAYGAIN_TRACK
)
679 || ((global_settings
.replaygain_type
== REPLAYGAIN_SHUFFLE
)
680 && global_settings
.playlist_shuffle
));
682 type
= (!track
&& have_album_gain
) ? REPLAYGAIN_ALBUM
683 : have_track_gain
? REPLAYGAIN_TRACK
: -1;
690 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
692 memorize/compare details about the BOOTFILE
693 we don't use dircache because it may not be up to date after
694 USB disconnect (scanning in the background)
696 void check_bootfile(bool do_rolo
)
698 static unsigned short wrtdate
= 0;
699 static unsigned short wrttime
= 0;
701 struct dirent
* entry
= NULL
;
703 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
704 dir
= opendir(BOOTDIR
);
706 if(!dir
) return; /* do we want an error splash? */
708 /* loop all files in BOOTDIR */
709 while(0 != (entry
= readdir(dir
)))
711 if(!strcasecmp(entry
->d_name
, BOOTFILE
))
713 /* found the bootfile */
714 if(wrtdate
&& do_rolo
)
716 if((entry
->wrtdate
!= wrtdate
) ||
717 (entry
->wrttime
!= wrttime
))
719 static const char *lines
[] = { ID2P(LANG_BOOT_CHANGED
),
720 ID2P(LANG_REBOOT_NOW
) };
721 static const struct text_message message
={ lines
, 2 };
722 button_clear_queue(); /* Empty the keyboard buffer */
723 if(gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
)
724 rolo_load(BOOTDIR
"/" BOOTFILE
);
727 wrtdate
= entry
->wrtdate
;
728 wrttime
= entry
->wrttime
;
736 /* check range, set volume and save settings */
739 const int min_vol
= sound_min(SOUND_VOLUME
);
740 const int max_vol
= sound_max(SOUND_VOLUME
);
741 if (global_settings
.volume
< min_vol
)
742 global_settings
.volume
= min_vol
;
743 if (global_settings
.volume
> max_vol
)
744 global_settings
.volume
= max_vol
;
745 sound_set_volume(global_settings
.volume
);
749 char* strrsplt(char* str
, int c
)
751 char* s
= strrchr(str
, c
);
765 /* Test file existence, using dircache of possible */
766 bool file_exists(const char *file
)
770 if (!file
|| strlen(file
) <= 0)
774 if (dircache_is_enabled())
775 return (dircache_get_entry_ptr(file
) != NULL
);
778 fd
= open(file
, O_RDONLY
);
785 bool dir_exists(const char *path
)
787 DIR* d
= opendir(path
);
795 * removes the extension of filename (if it doesn't start with a .)
796 * puts the result in buffer
798 char *strip_extension(char* buffer
, int buffer_size
, const char *filename
)
800 char *dot
= strrchr(filename
, '.');
803 if (buffer_size
<= 0)
808 buffer_size
--; /* Make room for end nil */
810 if (dot
!= 0 && filename
[0] != '.')
812 len
= dot
- filename
;
813 len
= MIN(len
, buffer_size
);
820 strlcpy(buffer
, filename
, len
+ 1);
824 #endif /* !defined(__PCTOOL__) */
826 char* skip_whitespace(char* const str
)
836 /* Format time into buf.
838 * buf - buffer to format to.
839 * buf_size - size of buffer.
840 * t - time to format, in milliseconds.
842 void format_time(char* buf
, int buf_size
, long t
)
846 snprintf(buf
, buf_size
, "%d:%02d",
847 (int) (t
/ 60000), (int) (t
% 60000 / 1000));
851 snprintf(buf
, buf_size
, "%d:%02d:%02d",
852 (int) (t
/ 3600000), (int) (t
% 3600000 / 60000),
853 (int) (t
% 60000 / 1000));
858 /** Open a UTF-8 file and set file descriptor to first byte after BOM.
859 * If no BOM is present this behaves like open().
860 * If the file is opened for writing and O_TRUNC is set, write a BOM to
861 * the opened file and leave the file pointer set after the BOM.
863 #define BOM "\xef\xbb\xbf"
866 int open_utf8(const char* pathname
, int flags
)
869 unsigned char bom
[BOM_SIZE
];
871 fd
= open(pathname
, flags
);
875 if(flags
& (O_TRUNC
| O_WRONLY
))
877 write(fd
, BOM
, BOM_SIZE
);
881 read(fd
, bom
, BOM_SIZE
);
883 if(memcmp(bom
, BOM
, BOM_SIZE
))
884 lseek(fd
, 0, SEEK_SET
);
890 #ifdef HAVE_LCD_COLOR
892 * Helper function to convert a string of 6 hex digits to a native colour
895 static int hex2dec(int c
)
897 return (((c
) >= '0' && ((c
) <= '9')) ? (c
) - '0' :
898 (toupper(c
)) - 'A' + 10);
901 int hex_to_rgb(const char* hex
, int* color
)
903 int red
, green
, blue
;
906 while ((i
< 6) && (isxdigit(hex
[i
])))
912 red
= (hex2dec(hex
[0]) << 4) | hex2dec(hex
[1]);
913 green
= (hex2dec(hex
[2]) << 4) | hex2dec(hex
[3]);
914 blue
= (hex2dec(hex
[4]) << 4) | hex2dec(hex
[5]);
916 *color
= LCD_RGBPACK(red
,green
,blue
);
920 #endif /* HAVE_LCD_COLOR */
922 #ifdef HAVE_LCD_BITMAP
923 /* A simplified scanf - used (at time of writing) by wps parsing functions.
925 fmt - char array specifying the format of each list option. Valid values
927 s - string (sets pointer to string, without copying)
928 c - hex colour (RGB888 - e.g. ff00ff)
929 g - greyscale "colour" (0-3)
930 set_vals - if not NULL 1 is set in the bitplace if the item was read OK
932 first item is LSB, (max 32 items! )
933 Stops parseing if an item is invalid unless the item == '-'
934 sep - list separator (e.g. ',' or '|')
935 str - string to parse, must be terminated by 0 or sep
936 ... - pointers to store the parsed values
938 return value - pointer to char after parsed data, 0 if there was an error.
942 /* '0'-'3' are ASCII 0x30 to 0x33 */
943 #define is0123(x) (((x) & 0xfc) == 0x30)
945 const char* parse_list(const char *fmt
, uint32_t *set_vals
,
946 const char sep
, const char* str
, ...)
949 const char* p
= str
, *f
= fmt
;
960 /* Check for separator, if we're not at the start */
970 case 's': /* string - return a pointer to it (not a copy) */
971 s
= va_arg(ap
, const char **);
974 while (*p
&& *p
!= sep
)
976 set
= (s
[0][0]!='-') && (s
[0][1]!=sep
) ;
980 d
= va_arg(ap
, int*);
983 if (!set_vals
|| *p
!= '-')
985 while (*p
&& *p
!= sep
)
992 *d
= (*d
* 10) + (*p
++ - '0');
998 #ifdef HAVE_LCD_COLOR
999 case 'c': /* colour (rrggbb - e.g. f3c1a8) */
1000 d
= va_arg(ap
, int*);
1002 if (hex_to_rgb(p
, d
) < 0)
1004 if (!set_vals
|| *p
!= '-')
1006 while (*p
&& *p
!= sep
)
1018 #if LCD_DEPTH == 2 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 2)
1019 case 'g': /* greyscale colour (0-3) */
1020 d
= va_arg(ap
, int*);
1027 else if (!set_vals
|| *p
!= '-')
1031 while (*p
&& *p
!= sep
)
1038 default: /* Unknown format type */
1042 if (set_vals
&& set
)
1043 *set_vals
|= BIT_N(i
);