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 "appevents.h"
39 #include "lcd-remote.h"
42 #include "timefuncs.h"
47 #include "mp3_playback.h"
50 #include "ata_idle_notify.h"
53 #include "powermgmt.h"
54 #include "backlight.h"
59 #include "scrobbler.h"
65 #ifdef IPOD_ACCESSORY_PROTOCOL
69 #if (CONFIG_STORAGE & STORAGE_MMC)
73 #include "eeprom_settings.h"
74 #if defined(HAVE_RECORDING) && !defined(__PCTOOL__)
75 #include "recording.h"
77 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
80 #endif /* End HAVE_LCD_BITMAP */
81 #include "gui/gwps-common.h"
87 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
93 /* Format a large-range value for output, using the appropriate unit so that
94 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
95 * units) if possible, and 3 significant digits are shown. If a buffer is
96 * given, the result is snprintf()'d into that buffer, otherwise the result is
98 char *output_dyn_value(char *buf
, int buf_size
, int value
,
99 const unsigned char **units
, bool bin_scale
)
101 int scale
= bin_scale
? 1024 : 1000;
106 while (value
>= scale
)
108 fraction
= value
% scale
;
113 fraction
= fraction
* 1000 / 1024;
115 if (value
>= 100 || !unit_no
)
117 else if (value
>= 10)
118 snprintf(tbuf
, sizeof(tbuf
), "%01d", fraction
/ 100);
120 snprintf(tbuf
, sizeof(tbuf
), "%02d", fraction
/ 10);
125 snprintf(buf
, buf_size
, "%d%s%s%s", value
, str(LANG_POINT
),
126 tbuf
, P2STR(units
[unit_no
]));
128 snprintf(buf
, buf_size
, "%d%s", value
, P2STR(units
[unit_no
]));
132 talk_fractional(tbuf
, value
, P2ID(units
[unit_no
]));
137 /* Ask the user if they really want to erase the current dynamic playlist
138 * returns true if the playlist should be replaced */
139 bool warn_on_pl_erase(void)
141 if (global_settings
.warnon_erase_dynplaylist
&&
142 !global_settings
.party_mode
&&
143 playlist_modified(NULL
))
145 static const char *lines
[] =
146 {ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
147 static const struct text_message message
={lines
, 1};
149 return (gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
);
155 /* Read (up to) a line of text from fd into buffer and return number of bytes
156 * read (which may be larger than the number of bytes stored in buffer). If
157 * an error occurs, -1 is returned (and buffer contains whatever could be
158 * read). A line is terminated by a LF char. Neither LF nor CR chars are
161 int read_line(int fd
, char* buffer
, int buffer_size
)
168 while (count
< buffer_size
)
172 if (1 != read(fd
, &c
, 1))
186 buffer
[MIN(count
, buffer_size
- 1)] = 0;
188 return errno
? -1 : num_read
;
191 /* Performance optimized version of the previous function. */
192 int fast_readline(int fd
, char *buf
, int buf_size
, void *parameters
,
193 int (*callback
)(int n
, const char *buf
, void *parameters
))
203 rc
= read(fd
, &buf
[pos
], buf_size
- pos
- 1);
207 if ( (p
= strchr(buf
, '\r')) != NULL
)
215 if ( (p
= strchr(p
, '\n')) != NULL
)
221 rc
= callback(count
, buf
, parameters
);
228 pos
= buf_size
- ((long)next
- (long)buf
) - 1;
229 memmove(buf
, next
, pos
);
238 /* parse a line from a configuration file. the line format is:
242 Any whitespace before setting name or value (after ':') is ignored.
243 A # as first non-whitespace character discards the whole line.
244 Function sets pointers to null-terminated setting name and value.
245 Returns false if no valid config entry was found.
248 bool settings_parseline(char* line
, char** name
, char** value
)
252 line
= skip_whitespace(line
);
257 ptr
= strchr(line
, ':');
264 ptr
= skip_whitespace(ptr
);
269 static void system_flush(void)
271 scrobbler_shutdown();
274 call_storage_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
277 static void system_restore(void)
283 static bool clean_shutdown(void (*callback
)(void *), void *parameter
)
288 bookmark_autobookmark();
289 call_storage_idle_notifys(true);
295 scrobbler_poweroff();
297 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
298 if(!charger_inserted())
301 bool batt_safe
= battery_level_safe();
302 int audio_stat
= audio_status();
305 screens
[i
].clear_display();
310 if (!tagcache_prepare_shutdown())
313 splash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
317 if (battery_level() > 10)
318 splash(0, str(LANG_SHUTTINGDOWN
));
321 msg_id
= LANG_WARNING_BATTERY_LOW
;
322 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_LOW
),
323 str(LANG_SHUTTINGDOWN
));
328 msg_id
= LANG_WARNING_BATTERY_EMPTY
;
329 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_EMPTY
),
330 str(LANG_SHUTTINGDOWN
));
333 if (global_settings
.fade_on_stop
334 && (audio_stat
& AUDIO_STATUS_PLAY
))
339 if (batt_safe
) /* do not save on critical battery */
341 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
342 if (audio_stat
& AUDIO_STATUS_RECORD
)
344 rec_command(RECORDING_CMD_STOP
);
345 /* wait for stop to complete */
346 while (audio_status() & AUDIO_STATUS_RECORD
)
350 bookmark_autobookmark();
352 /* audio_stop_recording == audio_stop for HWCODEC */
355 if (callback
!= NULL
)
358 #if CONFIG_CODEC != SWCODEC
359 /* wait for audio_stop or audio_stop_recording to complete */
360 while (audio_status())
364 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
365 audio_close_recording();
368 if(global_settings
.talk_menu
)
370 bool enqueue
= false;
373 talk_id(msg_id
, enqueue
);
376 talk_id(LANG_SHUTTINGDOWN
, enqueue
);
377 #if CONFIG_CODEC == SWCODEC
383 #ifdef HAVE_EEPROM_SETTINGS
384 if (firmware_settings
.initialized
)
386 firmware_settings
.disk_clean
= true;
387 firmware_settings
.bl_version
= 0;
388 eeprom_settings_store();
403 bool list_stop_handler(void)
407 /* Stop the music if it is playing */
410 if (!global_settings
.party_mode
)
412 if (global_settings
.fade_on_stop
)
414 bookmark_autobookmark();
416 ret
= true; /* bookmarking can make a refresh necessary */
420 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
423 if (charger_inserted())
426 shutdown_screen(); /* won't return if shutdown actually happens */
428 ret
= true; /* screen is dirty, caller needs to refresh */
431 #ifndef HAVE_POWEROFF_WHILE_CHARGING
433 static long last_off
= 0;
435 if (TIME_BEFORE(current_tick
, last_off
+ HZ
/2))
437 if (charger_inserted())
440 ret
= true; /* screen is dirty, caller needs to refresh */
443 last_off
= current_tick
;
446 #endif /* CONFIG_CHARGING */
451 static bool waiting_to_resume_play
= false;
452 static long play_resume_tick
;
454 static void car_adapter_mode_processing(bool inserted
)
456 if (global_settings
.car_adapter_mode
)
461 * Just got plugged in, delay & resume if we were playing
463 if (audio_status() & AUDIO_STATUS_PAUSE
)
465 /* delay resume a bit while the engine is cranking */
466 play_resume_tick
= current_tick
+ HZ
*5;
467 waiting_to_resume_play
= true;
473 * Just got unplugged, pause if playing
475 if ((audio_status() & AUDIO_STATUS_PLAY
) &&
476 !(audio_status() & AUDIO_STATUS_PAUSE
))
478 if (global_settings
.fade_on_stop
)
483 waiting_to_resume_play
= false;
488 static void car_adapter_tick(void)
490 if (waiting_to_resume_play
)
492 if (TIME_AFTER(current_tick
, play_resume_tick
))
494 if (audio_status() & AUDIO_STATUS_PAUSE
)
496 queue_broadcast(SYS_CAR_ADAPTER_RESUME
, 0);
498 waiting_to_resume_play
= false;
503 void car_adapter_mode_init(void)
505 tick_add_task(car_adapter_tick
);
509 #ifdef HAVE_HEADPHONE_DETECTION
510 static void unplug_change(bool inserted
)
512 static bool headphone_caused_pause
= false;
514 if (global_settings
.unplug_mode
)
516 int audio_stat
= audio_status();
519 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
520 headphone_caused_pause
&&
521 global_settings
.unplug_mode
> 1 )
524 headphone_caused_pause
= false;
526 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
527 !(audio_stat
& AUDIO_STATUS_PAUSE
))
529 headphone_caused_pause
= true;
532 if (global_settings
.unplug_rw
)
534 if (audio_current_track()->elapsed
>
535 (unsigned long)(global_settings
.unplug_rw
*1000))
536 audio_ff_rewind(audio_current_track()->elapsed
-
537 (global_settings
.unplug_rw
*1000));
547 long default_event_handler_ex(long event
, void (*callback
)(void *), void *parameter
)
551 case SYS_BATTERY_UPDATE
:
552 if(global_settings
.talk_battery_level
)
554 talk_ids(true, VOICE_PAUSE
, VOICE_PAUSE
,
556 TALK_ID(battery_level(), UNIT_PERCENT
),
558 talk_force_enqueue_next();
561 case SYS_USB_CONNECTED
:
562 if (callback
!= NULL
)
564 #if (CONFIG_STORAGE & STORAGE_MMC)
565 if (!mmc_touched() ||
566 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED
))
571 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
572 check_bootfile(false); /* gets initial size */
577 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
578 check_bootfile(true);
583 return SYS_USB_CONNECTED
;
585 if (!clean_shutdown(callback
, parameter
))
589 case SYS_CHARGER_CONNECTED
:
590 car_adapter_mode_processing(true);
591 return SYS_CHARGER_CONNECTED
;
593 case SYS_CHARGER_DISCONNECTED
:
594 car_adapter_mode_processing(false);
595 return SYS_CHARGER_DISCONNECTED
;
597 case SYS_CAR_ADAPTER_RESUME
:
599 return SYS_CAR_ADAPTER_RESUME
;
601 #ifdef HAVE_HEADPHONE_DETECTION
602 case SYS_PHONE_PLUGGED
:
604 return SYS_PHONE_PLUGGED
;
606 case SYS_PHONE_UNPLUGGED
:
607 unplug_change(false);
608 return SYS_PHONE_UNPLUGGED
;
610 #ifdef IPOD_ACCESSORY_PROTOCOL
611 case SYS_IAP_PERIODIC
:
613 return SYS_IAP_PERIODIC
;
614 case SYS_IAP_HANDLEPKT
:
616 return SYS_IAP_HANDLEPKT
;
622 long default_event_handler(long event
)
624 return default_event_handler_ex(event
, NULL
, NULL
);
627 int show_logo( void )
629 #ifdef HAVE_LCD_BITMAP
633 snprintf(version
, sizeof(version
), "Ver. %s", appsversion
);
636 #ifdef SANSA_CLIP /* display the logo in the blue area of the screen */
637 lcd_setfont(FONT_SYSFIXED
);
638 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
639 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
640 0, (unsigned char *)version
);
641 lcd_bitmap(rockboxlogo
, 0, 16, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
643 lcd_bitmap(rockboxlogo
, 0, 10, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
644 lcd_setfont(FONT_SYSFIXED
);
645 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
646 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
647 LCD_HEIGHT
-font_h
, (unsigned char *)version
);
649 lcd_setfont(FONT_UI
);
652 char *rockbox
= " ROCKbox!";
655 lcd_double_height(true);
656 lcd_puts(0, 0, rockbox
);
657 lcd_puts_scroll(0, 1, appsversion
);
661 #ifdef HAVE_REMOTE_LCD
662 lcd_remote_clear_display();
663 lcd_remote_bitmap(remote_rockboxlogo
, 0, 10, BMPWIDTH_remote_rockboxlogo
,
664 BMPHEIGHT_remote_rockboxlogo
);
665 lcd_remote_setfont(FONT_SYSFIXED
);
666 lcd_remote_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
667 lcd_remote_putsxy((LCD_REMOTE_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
668 LCD_REMOTE_HEIGHT
-font_h
, (unsigned char *)version
);
669 lcd_remote_setfont(FONT_UI
);
676 #if CONFIG_CODEC == SWCODEC
677 int get_replaygain_mode(bool have_track_gain
, bool have_album_gain
)
681 bool track
= ((global_settings
.replaygain_type
== REPLAYGAIN_TRACK
)
682 || ((global_settings
.replaygain_type
== REPLAYGAIN_SHUFFLE
)
683 && global_settings
.playlist_shuffle
));
685 type
= (!track
&& have_album_gain
) ? REPLAYGAIN_ALBUM
686 : have_track_gain
? REPLAYGAIN_TRACK
: -1;
693 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
695 memorize/compare details about the BOOTFILE
696 we don't use dircache because it may not be up to date after
697 USB disconnect (scanning in the background)
699 void check_bootfile(bool do_rolo
)
701 static unsigned short wrtdate
= 0;
702 static unsigned short wrttime
= 0;
704 struct dirent
* entry
= NULL
;
706 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
707 dir
= opendir(BOOTDIR
);
709 if(!dir
) return; /* do we want an error splash? */
711 /* loop all files in BOOTDIR */
712 while(0 != (entry
= readdir(dir
)))
714 if(!strcasecmp(entry
->d_name
, BOOTFILE
))
716 /* found the bootfile */
717 if(wrtdate
&& do_rolo
)
719 if((entry
->wrtdate
!= wrtdate
) ||
720 (entry
->wrttime
!= wrttime
))
722 static const char *lines
[] = { ID2P(LANG_BOOT_CHANGED
),
723 ID2P(LANG_REBOOT_NOW
) };
724 static const struct text_message message
={ lines
, 2 };
725 button_clear_queue(); /* Empty the keyboard buffer */
726 if(gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
)
727 rolo_load(BOOTDIR
"/" BOOTFILE
);
730 wrtdate
= entry
->wrtdate
;
731 wrttime
= entry
->wrttime
;
739 /* check range, set volume and save settings */
742 const int min_vol
= sound_min(SOUND_VOLUME
);
743 const int max_vol
= sound_max(SOUND_VOLUME
);
744 if (global_settings
.volume
< min_vol
)
745 global_settings
.volume
= min_vol
;
746 if (global_settings
.volume
> max_vol
)
747 global_settings
.volume
= max_vol
;
748 sound_set_volume(global_settings
.volume
);
752 char* strrsplt(char* str
, int c
)
754 char* s
= strrchr(str
, c
);
768 /* Test file existence, using dircache of possible */
769 bool file_exists(const char *file
)
773 if (!file
|| strlen(file
) <= 0)
777 if (dircache_is_enabled())
778 return (dircache_get_entry_ptr(file
) != NULL
);
781 fd
= open(file
, O_RDONLY
);
788 bool dir_exists(const char *path
)
790 DIR* d
= opendir(path
);
798 * removes the extension of filename (if it doesn't start with a .)
799 * puts the result in buffer
801 char *strip_extension(char* buffer
, int buffer_size
, const char *filename
)
803 char *dot
= strrchr(filename
, '.');
806 if (buffer_size
<= 0)
811 buffer_size
--; /* Make room for end nil */
813 if (dot
!= 0 && filename
[0] != '.')
815 len
= dot
- filename
;
816 len
= MIN(len
, buffer_size
);
817 strncpy(buffer
, filename
, len
);
822 strncpy(buffer
, filename
, buffer_size
);
829 #endif /* !defined(__PCTOOL__) */
831 char* skip_whitespace(char* const str
)
841 /* Format time into buf.
843 * buf - buffer to format to.
844 * buf_size - size of buffer.
845 * t - time to format, in milliseconds.
847 void format_time(char* buf
, int buf_size
, long t
)
851 snprintf(buf
, buf_size
, "%d:%02d",
852 (int) (t
/ 60000), (int) (t
% 60000 / 1000));
856 snprintf(buf
, buf_size
, "%d:%02d:%02d",
857 (int) (t
/ 3600000), (int) (t
% 3600000 / 60000),
858 (int) (t
% 60000 / 1000));
863 /** Open a UTF-8 file and set file descriptor to first byte after BOM.
864 * If no BOM is present this behaves like open().
865 * If the file is opened for writing and O_TRUNC is set, write a BOM to
866 * the opened file and leave the file pointer set after the BOM.
868 #define BOM "\xef\xbb\xbf"
871 int open_utf8(const char* pathname
, int flags
)
874 unsigned char bom
[BOM_SIZE
];
876 fd
= open(pathname
, flags
);
880 if(flags
& (O_TRUNC
| O_WRONLY
))
882 write(fd
, BOM
, BOM_SIZE
);
886 read(fd
, bom
, BOM_SIZE
);
888 if(memcmp(bom
, BOM
, BOM_SIZE
))
889 lseek(fd
, 0, SEEK_SET
);
895 #ifdef HAVE_LCD_COLOR
897 * Helper function to convert a string of 6 hex digits to a native colour
900 static int hex2dec(int c
)
902 return (((c
) >= '0' && ((c
) <= '9')) ? (c
) - '0' :
903 (toupper(c
)) - 'A' + 10);
906 int hex_to_rgb(const char* hex
, int* color
)
908 int red
, green
, blue
;
911 while ((i
< 6) && (isxdigit(hex
[i
])))
917 red
= (hex2dec(hex
[0]) << 4) | hex2dec(hex
[1]);
918 green
= (hex2dec(hex
[2]) << 4) | hex2dec(hex
[3]);
919 blue
= (hex2dec(hex
[4]) << 4) | hex2dec(hex
[5]);
921 *color
= LCD_RGBPACK(red
,green
,blue
);
925 #endif /* HAVE_LCD_COLOR */
927 #ifdef HAVE_LCD_BITMAP
928 /* A simplified scanf - used (at time of writing) by wps parsing functions.
930 fmt - char array specifying the format of each list option. Valid values
932 s - string (sets pointer to string, without copying)
933 c - hex colour (RGB888 - e.g. ff00ff)
934 g - greyscale "colour" (0-3)
935 set_vals - if not NULL 1 is set in the bitplace if the item was read OK
937 first item is LSB, (max 32 items! )
938 Stops parseing if an item is invalid unless the item == '-'
939 sep - list separator (e.g. ',' or '|')
940 str - string to parse, must be terminated by 0 or sep
941 ... - pointers to store the parsed values
943 return value - pointer to char after parsed data, 0 if there was an error.
947 /* '0'-'3' are ASCII 0x30 to 0x33 */
948 #define is0123(x) (((x) & 0xfc) == 0x30)
950 const char* parse_list(const char *fmt
, uint32_t *set_vals
,
951 const char sep
, const char* str
, ...)
954 const char* p
= str
, *f
= fmt
;
965 /* Check for separator, if we're not at the start */
975 case 's': /* string - return a pointer to it (not a copy) */
976 s
= va_arg(ap
, const char **);
979 while (*p
&& *p
!= sep
)
981 set
= (s
[0][0]!='-') && (s
[0][1]!=sep
) ;
985 d
= va_arg(ap
, int*);
988 if (!set_vals
|| *p
!= '-')
990 while (*p
&& *p
!= sep
)
997 *d
= (*d
* 10) + (*p
++ - '0');
1003 #ifdef HAVE_LCD_COLOR
1004 case 'c': /* colour (rrggbb - e.g. f3c1a8) */
1005 d
= va_arg(ap
, int*);
1007 if (hex_to_rgb(p
, d
) < 0)
1009 if (!set_vals
|| *p
!= '-')
1011 while (*p
&& *p
!= sep
)
1023 #if LCD_DEPTH == 2 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 2)
1024 case 'g': /* greyscale colour (0-3) */
1025 d
= va_arg(ap
, int*);
1032 else if (!set_vals
|| *p
!= '-')
1036 while (*p
&& *p
!= sep
)
1043 default: /* Unknown format type */
1047 if (set_vals
&& set
)
1048 *set_vals
|= (1<<i
);