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 ****************************************************************************/
33 #include "appevents.h"
36 #include "lcd-remote.h"
39 #include "timefuncs.h"
44 #include "mp3_playback.h"
47 #include "ata_idle_notify.h"
50 #include "powermgmt.h"
51 #include "backlight.h"
56 #include "scrobbler.h"
62 #ifdef IPOD_ACCESSORY_PROTOCOL
66 #if (CONFIG_STORAGE & STORAGE_MMC)
70 #include "eeprom_settings.h"
71 #if defined(HAVE_RECORDING) && !defined(__PCTOOL__)
72 #include "recording.h"
74 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
77 #endif /* End HAVE_LCD_BITMAP */
78 #include "gui/gwps-common.h"
84 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
90 /* Format a large-range value for output, using the appropriate unit so that
91 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
92 * units) if possible, and 3 significant digits are shown. If a buffer is
93 * given, the result is snprintf()'d into that buffer, otherwise the result is
95 char *output_dyn_value(char *buf
, int buf_size
, int value
,
96 const unsigned char **units
, bool bin_scale
)
98 int scale
= bin_scale
? 1024 : 1000;
103 while (value
>= scale
)
105 fraction
= value
% scale
;
110 fraction
= fraction
* 1000 / 1024;
112 if (value
>= 100 || !unit_no
)
114 else if (value
>= 10)
115 snprintf(tbuf
, sizeof(tbuf
), "%01d", fraction
/ 100);
117 snprintf(tbuf
, sizeof(tbuf
), "%02d", fraction
/ 10);
122 snprintf(buf
, buf_size
, "%d%s%s%s", value
, str(LANG_POINT
),
123 tbuf
, P2STR(units
[unit_no
]));
125 snprintf(buf
, buf_size
, "%d%s", value
, P2STR(units
[unit_no
]));
129 talk_fractional(tbuf
, value
, P2ID(units
[unit_no
]));
134 /* Ask the user if they really want to erase the current dynamic playlist
135 * returns true if the playlist should be replaced */
136 bool warn_on_pl_erase(void)
138 if (global_settings
.warnon_erase_dynplaylist
&&
139 !global_settings
.party_mode
&&
140 playlist_modified(NULL
))
142 static const char *lines
[] =
143 {ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
144 static const struct text_message message
={lines
, 1};
146 return (gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
);
152 /* Read (up to) a line of text from fd into buffer and return number of bytes
153 * read (which may be larger than the number of bytes stored in buffer). If
154 * an error occurs, -1 is returned (and buffer contains whatever could be
155 * read). A line is terminated by a LF char. Neither LF nor CR chars are
158 int read_line(int fd
, char* buffer
, int buffer_size
)
165 while (count
< buffer_size
)
169 if (1 != read(fd
, &c
, 1))
183 buffer
[MIN(count
, buffer_size
- 1)] = 0;
185 return errno
? -1 : num_read
;
188 /* Performance optimized version of the previous function. */
189 int fast_readline(int fd
, char *buf
, int buf_size
, void *parameters
,
190 int (*callback
)(int n
, const char *buf
, void *parameters
))
200 rc
= read(fd
, &buf
[pos
], buf_size
- pos
- 1);
204 if ( (p
= strchr(buf
, '\r')) != NULL
)
212 if ( (p
= strchr(p
, '\n')) != NULL
)
218 rc
= callback(count
, buf
, parameters
);
225 pos
= buf_size
- ((long)next
- (long)buf
) - 1;
226 memmove(buf
, next
, pos
);
235 /* parse a line from a configuration file. the line format is:
239 Any whitespace before setting name or value (after ':') is ignored.
240 A # as first non-whitespace character discards the whole line.
241 Function sets pointers to null-terminated setting name and value.
242 Returns false if no valid config entry was found.
245 bool settings_parseline(char* line
, char** name
, char** value
)
249 line
= skip_whitespace(line
);
254 ptr
= strchr(line
, ':');
261 ptr
= skip_whitespace(ptr
);
266 static void system_flush(void)
268 scrobbler_shutdown();
271 call_storage_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
274 static void system_restore(void)
280 static bool clean_shutdown(void (*callback
)(void *), void *parameter
)
285 bookmark_autobookmark();
286 call_storage_idle_notifys(true);
292 scrobbler_poweroff();
294 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
295 if(!charger_inserted())
298 bool batt_safe
= battery_level_safe();
299 int audio_stat
= audio_status();
303 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
|= BIT_N(i
);