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 ****************************************************************************/
36 #include "appevents.h"
40 #include "lcd-remote.h"
43 #include "timefuncs.h"
48 #include "mp3_playback.h"
51 #include "ata_idle_notify.h"
54 #include "powermgmt.h"
55 #include "backlight.h"
60 #include "scrobbler.h"
66 #ifdef IPOD_ACCESSORY_PROTOCOL
70 #if (CONFIG_STORAGE & STORAGE_MMC)
74 #include "eeprom_settings.h"
75 #if defined(HAVE_RECORDING) && !defined(__PCTOOL__)
76 #include "recording.h"
78 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
81 #endif /* End HAVE_LCD_BITMAP */
82 #include "gui/gwps-common.h"
88 #if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
94 /* Format a large-range value for output, using the appropriate unit so that
95 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
96 * units) if possible, and 3 significant digits are shown. If a buffer is
97 * given, the result is snprintf()'d into that buffer, otherwise the result is
99 char *output_dyn_value(char *buf
, int buf_size
, int value
,
100 const unsigned char **units
, bool bin_scale
)
102 int scale
= bin_scale
? 1024 : 1000;
107 while (value
>= scale
)
109 fraction
= value
% scale
;
114 fraction
= fraction
* 1000 / 1024;
116 if (value
>= 100 || !unit_no
)
118 else if (value
>= 10)
119 snprintf(tbuf
, sizeof(tbuf
), "%01d", fraction
/ 100);
121 snprintf(tbuf
, sizeof(tbuf
), "%02d", fraction
/ 10);
126 snprintf(buf
, buf_size
, "%d%s%s%s", value
, str(LANG_POINT
),
127 tbuf
, P2STR(units
[unit_no
]));
129 snprintf(buf
, buf_size
, "%d%s", value
, P2STR(units
[unit_no
]));
133 talk_fractional(tbuf
, value
, P2ID(units
[unit_no
]));
138 /* Ask the user if they really want to erase the current dynamic playlist
139 * returns true if the playlist should be replaced */
140 bool warn_on_pl_erase(void)
142 if (global_settings
.warnon_erase_dynplaylist
&&
143 !global_settings
.party_mode
&&
144 playlist_modified(NULL
))
146 static const char *lines
[] =
147 {ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
148 static const struct text_message message
={lines
, 1};
150 return (gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
);
156 /* Read (up to) a line of text from fd into buffer and return number of bytes
157 * read (which may be larger than the number of bytes stored in buffer). If
158 * an error occurs, -1 is returned (and buffer contains whatever could be
159 * read). A line is terminated by a LF char. Neither LF nor CR chars are
162 int read_line(int fd
, char* buffer
, int buffer_size
)
169 while (count
< buffer_size
)
173 if (1 != read(fd
, &c
, 1))
187 buffer
[MIN(count
, buffer_size
- 1)] = 0;
189 return errno
? -1 : num_read
;
192 /* Performance optimized version of the previous function. */
193 int fast_readline(int fd
, char *buf
, int buf_size
, void *parameters
,
194 int (*callback
)(int n
, const char *buf
, void *parameters
))
204 rc
= read(fd
, &buf
[pos
], buf_size
- pos
- 1);
208 if ( (p
= strchr(buf
, '\r')) != NULL
)
216 if ( (p
= strchr(p
, '\n')) != NULL
)
222 rc
= callback(count
, buf
, parameters
);
229 pos
= buf_size
- ((long)next
- (long)buf
) - 1;
230 memmove(buf
, next
, pos
);
239 /* parse a line from a configuration file. the line format is:
243 Any whitespace before setting name or value (after ':') is ignored.
244 A # as first non-whitespace character discards the whole line.
245 Function sets pointers to null-terminated setting name and value.
246 Returns false if no valid config entry was found.
249 bool settings_parseline(char* line
, char** name
, char** value
)
253 line
= skip_whitespace(line
);
258 ptr
= strchr(line
, ':');
265 ptr
= skip_whitespace(ptr
);
270 static void system_flush(void)
272 scrobbler_shutdown();
275 call_storage_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
278 static void system_restore(void)
284 static bool clean_shutdown(void (*callback
)(void *), void *parameter
)
289 bookmark_autobookmark();
290 call_storage_idle_notifys(true);
296 scrobbler_poweroff();
298 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
299 if(!charger_inserted())
302 bool batt_safe
= battery_level_safe();
303 int audio_stat
= audio_status();
306 screens
[i
].clear_display();
311 if (!tagcache_prepare_shutdown())
314 splash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
318 if (battery_level() > 10)
319 splash(0, str(LANG_SHUTTINGDOWN
));
322 msg_id
= LANG_WARNING_BATTERY_LOW
;
323 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_LOW
),
324 str(LANG_SHUTTINGDOWN
));
329 msg_id
= LANG_WARNING_BATTERY_EMPTY
;
330 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_EMPTY
),
331 str(LANG_SHUTTINGDOWN
));
334 if (global_settings
.fade_on_stop
335 && (audio_stat
& AUDIO_STATUS_PLAY
))
340 if (batt_safe
) /* do not save on critical battery */
342 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
343 if (audio_stat
& AUDIO_STATUS_RECORD
)
345 rec_command(RECORDING_CMD_STOP
);
346 /* wait for stop to complete */
347 while (audio_status() & AUDIO_STATUS_RECORD
)
351 bookmark_autobookmark();
353 /* audio_stop_recording == audio_stop for HWCODEC */
356 if (callback
!= NULL
)
359 #if CONFIG_CODEC != SWCODEC
360 /* wait for audio_stop or audio_stop_recording to complete */
361 while (audio_status())
365 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
366 audio_close_recording();
369 if(global_settings
.talk_menu
)
371 bool enqueue
= false;
374 talk_id(msg_id
, enqueue
);
377 talk_id(LANG_SHUTTINGDOWN
, enqueue
);
378 #if CONFIG_CODEC == SWCODEC
384 #ifdef HAVE_EEPROM_SETTINGS
385 if (firmware_settings
.initialized
)
387 firmware_settings
.disk_clean
= true;
388 firmware_settings
.bl_version
= 0;
389 eeprom_settings_store();
404 bool list_stop_handler(void)
408 /* Stop the music if it is playing */
411 if (!global_settings
.party_mode
)
413 if (global_settings
.fade_on_stop
)
415 bookmark_autobookmark();
417 ret
= true; /* bookmarking can make a refresh necessary */
421 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
424 if (charger_inserted())
427 shutdown_screen(); /* won't return if shutdown actually happens */
429 ret
= true; /* screen is dirty, caller needs to refresh */
432 #ifndef HAVE_POWEROFF_WHILE_CHARGING
434 static long last_off
= 0;
436 if (TIME_BEFORE(current_tick
, last_off
+ HZ
/2))
438 if (charger_inserted())
441 ret
= true; /* screen is dirty, caller needs to refresh */
444 last_off
= current_tick
;
447 #endif /* CONFIG_CHARGING */
452 static bool waiting_to_resume_play
= false;
453 static long play_resume_tick
;
455 static void car_adapter_mode_processing(bool inserted
)
457 if (global_settings
.car_adapter_mode
)
462 * Just got plugged in, delay & resume if we were playing
464 if (audio_status() & AUDIO_STATUS_PAUSE
)
466 /* delay resume a bit while the engine is cranking */
467 play_resume_tick
= current_tick
+ HZ
*5;
468 waiting_to_resume_play
= true;
474 * Just got unplugged, pause if playing
476 if ((audio_status() & AUDIO_STATUS_PLAY
) &&
477 !(audio_status() & AUDIO_STATUS_PAUSE
))
479 if (global_settings
.fade_on_stop
)
484 waiting_to_resume_play
= false;
489 static void car_adapter_tick(void)
491 if (waiting_to_resume_play
)
493 if (TIME_AFTER(current_tick
, play_resume_tick
))
495 if (audio_status() & AUDIO_STATUS_PAUSE
)
497 queue_broadcast(SYS_CAR_ADAPTER_RESUME
, 0);
499 waiting_to_resume_play
= false;
504 void car_adapter_mode_init(void)
506 tick_add_task(car_adapter_tick
);
510 #ifdef HAVE_HEADPHONE_DETECTION
511 static void unplug_change(bool inserted
)
513 static bool headphone_caused_pause
= false;
515 if (global_settings
.unplug_mode
)
517 int audio_stat
= audio_status();
520 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
521 headphone_caused_pause
&&
522 global_settings
.unplug_mode
> 1 )
525 headphone_caused_pause
= false;
527 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
528 !(audio_stat
& AUDIO_STATUS_PAUSE
))
530 headphone_caused_pause
= true;
533 if (global_settings
.unplug_rw
)
535 if (audio_current_track()->elapsed
>
536 (unsigned long)(global_settings
.unplug_rw
*1000))
537 audio_ff_rewind(audio_current_track()->elapsed
-
538 (global_settings
.unplug_rw
*1000));
548 long default_event_handler_ex(long event
, void (*callback
)(void *), void *parameter
)
552 case SYS_BATTERY_UPDATE
:
553 if(global_settings
.talk_battery_level
)
555 talk_ids(true, VOICE_PAUSE
, VOICE_PAUSE
,
557 TALK_ID(battery_level(), UNIT_PERCENT
),
559 talk_force_enqueue_next();
562 case SYS_USB_CONNECTED
:
563 if (callback
!= NULL
)
565 #if (CONFIG_STORAGE & STORAGE_MMC)
566 if (!mmc_touched() ||
567 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED
))
572 #if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
573 check_bootfile(false); /* gets initial size */
578 #if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
579 check_bootfile(true);
584 return SYS_USB_CONNECTED
;
586 if (!clean_shutdown(callback
, parameter
))
590 case SYS_CHARGER_CONNECTED
:
591 car_adapter_mode_processing(true);
592 return SYS_CHARGER_CONNECTED
;
594 case SYS_CHARGER_DISCONNECTED
:
595 car_adapter_mode_processing(false);
596 return SYS_CHARGER_DISCONNECTED
;
598 case SYS_CAR_ADAPTER_RESUME
:
600 return SYS_CAR_ADAPTER_RESUME
;
602 #ifdef HAVE_HEADPHONE_DETECTION
603 case SYS_PHONE_PLUGGED
:
605 return SYS_PHONE_PLUGGED
;
607 case SYS_PHONE_UNPLUGGED
:
608 unplug_change(false);
609 return SYS_PHONE_UNPLUGGED
;
611 #ifdef IPOD_ACCESSORY_PROTOCOL
612 case SYS_IAP_PERIODIC
:
614 return SYS_IAP_PERIODIC
;
615 case SYS_IAP_HANDLEPKT
:
617 return SYS_IAP_HANDLEPKT
;
623 long default_event_handler(long event
)
625 return default_event_handler_ex(event
, NULL
, NULL
);
628 int show_logo( void )
630 #ifdef HAVE_LCD_BITMAP
634 snprintf(version
, sizeof(version
), "Ver. %s", appsversion
);
637 #ifdef SANSA_CLIP /* display the logo in the blue area of the screen */
638 lcd_setfont(FONT_SYSFIXED
);
639 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
640 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
641 0, (unsigned char *)version
);
642 lcd_bitmap(rockboxlogo
, 0, 16, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
644 lcd_bitmap(rockboxlogo
, 0, 10, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
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 LCD_HEIGHT
-font_h
, (unsigned char *)version
);
650 lcd_setfont(FONT_UI
);
653 char *rockbox
= " ROCKbox!";
656 lcd_double_height(true);
657 lcd_puts(0, 0, rockbox
);
658 lcd_puts_scroll(0, 1, appsversion
);
662 #ifdef HAVE_REMOTE_LCD
663 lcd_remote_clear_display();
664 lcd_remote_bitmap(remote_rockboxlogo
, 0, 10, BMPWIDTH_remote_rockboxlogo
,
665 BMPHEIGHT_remote_rockboxlogo
);
666 lcd_remote_setfont(FONT_SYSFIXED
);
667 lcd_remote_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
668 lcd_remote_putsxy((LCD_REMOTE_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
669 LCD_REMOTE_HEIGHT
-font_h
, (unsigned char *)version
);
670 lcd_remote_setfont(FONT_UI
);
677 #if CONFIG_CODEC == SWCODEC
678 int get_replaygain_mode(bool have_track_gain
, bool have_album_gain
)
682 bool track
= ((global_settings
.replaygain_type
== REPLAYGAIN_TRACK
)
683 || ((global_settings
.replaygain_type
== REPLAYGAIN_SHUFFLE
)
684 && global_settings
.playlist_shuffle
));
686 type
= (!track
&& have_album_gain
) ? REPLAYGAIN_ALBUM
687 : have_track_gain
? REPLAYGAIN_TRACK
: -1;
694 #if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
696 memorize/compare details about the BOOTFILE
697 we don't use dircache because it may not be up to date after
698 USB disconnect (scanning in the background)
700 void check_bootfile(bool do_rolo
)
702 static unsigned short wrtdate
= 0;
703 static unsigned short wrttime
= 0;
705 struct dirent
* entry
= NULL
;
707 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
708 dir
= opendir(BOOTDIR
);
710 if(!dir
) return; /* do we want an error splash? */
712 /* loop all files in BOOTDIR */
713 while(0 != (entry
= readdir(dir
)))
715 if(!strcasecmp(entry
->d_name
, BOOTFILE
))
717 /* found the bootfile */
718 if(wrtdate
&& do_rolo
)
720 if((entry
->wrtdate
!= wrtdate
) ||
721 (entry
->wrttime
!= wrttime
))
723 static const char *lines
[] = { ID2P(LANG_BOOT_CHANGED
),
724 ID2P(LANG_REBOOT_NOW
) };
725 static const struct text_message message
={ lines
, 2 };
726 button_clear_queue(); /* Empty the keyboard buffer */
727 if(gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
)
728 rolo_load(BOOTDIR
"/" BOOTFILE
);
731 wrtdate
= entry
->wrtdate
;
732 wrttime
= entry
->wrttime
;
740 /* check range, set volume and save settings */
743 const int min_vol
= sound_min(SOUND_VOLUME
);
744 const int max_vol
= sound_max(SOUND_VOLUME
);
745 if (global_settings
.volume
< min_vol
)
746 global_settings
.volume
= min_vol
;
747 if (global_settings
.volume
> max_vol
)
748 global_settings
.volume
= max_vol
;
749 sound_set_volume(global_settings
.volume
);
753 char* strrsplt(char* str
, int c
)
755 char* s
= strrchr(str
, c
);
769 char* skip_whitespace(char* const str
)
779 /* Test file existence, using dircache of possible */
780 bool file_exists(const char *file
)
784 if (!file
|| strlen(file
) <= 0)
788 if (dircache_is_enabled())
789 return (dircache_get_entry_ptr(file
) != NULL
);
792 fd
= open(file
, O_RDONLY
);
799 bool dir_exists(const char *path
)
801 DIR* d
= opendir(path
);
809 * removes the extension of filename (if it doesn't start with a .)
810 * puts the result in buffer
812 char *strip_extension(char* buffer
, int buffer_size
, const char *filename
)
814 char *dot
= strrchr(filename
, '.');
817 if (buffer_size
<= 0)
822 buffer_size
--; /* Make room for end nil */
824 if (dot
!= 0 && filename
[0] != '.')
826 len
= dot
- filename
;
827 len
= MIN(len
, buffer_size
);
828 strncpy(buffer
, filename
, len
);
833 strncpy(buffer
, filename
, buffer_size
);
840 #endif /* !defined(__PCTOOL__) */
842 /* Format time into buf.
844 * buf - buffer to format to.
845 * buf_size - size of buffer.
846 * t - time to format, in milliseconds.
848 void format_time(char* buf
, int buf_size
, long t
)
852 snprintf(buf
, buf_size
, "%d:%02d",
853 (int) (t
/ 60000), (int) (t
% 60000 / 1000));
857 snprintf(buf
, buf_size
, "%d:%02d:%02d",
858 (int) (t
/ 3600000), (int) (t
% 3600000 / 60000),
859 (int) (t
% 60000 / 1000));
864 /** Open a UTF-8 file and set file descriptor to first byte after BOM.
865 * If no BOM is present this behaves like open().
866 * If the file is opened for writing and O_TRUNC is set, write a BOM to
867 * the opened file and leave the file pointer set after the BOM.
869 #define BOM "\xef\xbb\xbf"
872 int open_utf8(const char* pathname
, int flags
)
875 unsigned char bom
[BOM_SIZE
];
877 fd
= open(pathname
, flags
);
881 if(flags
& (O_TRUNC
| O_WRONLY
))
883 write(fd
, BOM
, BOM_SIZE
);
887 read(fd
, bom
, BOM_SIZE
);
889 if(memcmp(bom
, BOM
, BOM_SIZE
))
890 lseek(fd
, 0, SEEK_SET
);
896 #ifdef HAVE_LCD_COLOR
898 * Helper function to convert a string of 6 hex digits to a native colour
901 static int hex2dec(int c
)
903 return (((c
) >= '0' && ((c
) <= '9')) ? (c
) - '0' :
904 (toupper(c
)) - 'A' + 10);
907 int hex_to_rgb(const char* hex
, int* color
)
909 int red
, green
, blue
;
912 while ((i
< 6) && (isxdigit(hex
[i
])))
918 red
= (hex2dec(hex
[0]) << 4) | hex2dec(hex
[1]);
919 green
= (hex2dec(hex
[2]) << 4) | hex2dec(hex
[3]);
920 blue
= (hex2dec(hex
[4]) << 4) | hex2dec(hex
[5]);
922 *color
= LCD_RGBPACK(red
,green
,blue
);
926 #endif /* HAVE_LCD_COLOR */
928 #ifdef HAVE_LCD_BITMAP
929 /* A simplified scanf - used (at time of writing) by wps parsing functions.
931 fmt - char array specifying the format of each list option. Valid values
933 s - string (sets pointer to string, without copying)
934 c - hex colour (RGB888 - e.g. ff00ff)
935 g - greyscale "colour" (0-3)
936 set_vals - if not NULL 1 is set in the bitplace if the item was read OK
938 first item is LSB, (max 32 items! )
939 Stops parseing if an item is invalid unless the item == '-'
940 sep - list separator (e.g. ',' or '|')
941 str - string to parse, must be terminated by 0 or sep
942 ... - pointers to store the parsed values
944 return value - pointer to char after parsed data, 0 if there was an error.
948 /* '0'-'3' are ASCII 0x30 to 0x33 */
949 #define is0123(x) (((x) & 0xfc) == 0x30)
951 const char* parse_list(const char *fmt
, uint32_t *set_vals
,
952 const char sep
, const char* str
, ...)
955 const char* p
= str
, *f
= fmt
;
966 /* Check for separator, if we're not at the start */
976 case 's': /* string - return a pointer to it (not a copy) */
977 s
= va_arg(ap
, const char **);
980 while (*p
&& *p
!= sep
)
982 set
= (s
[0][0]!='-') && (s
[0][1]!=sep
) ;
986 d
= va_arg(ap
, int*);
989 if (!set_vals
|| *p
!= '-')
991 while (*p
&& *p
!= sep
)
998 *d
= (*d
* 10) + (*p
++ - '0');
1004 #ifdef HAVE_LCD_COLOR
1005 case 'c': /* colour (rrggbb - e.g. f3c1a8) */
1006 d
= va_arg(ap
, int*);
1008 if (hex_to_rgb(p
, d
) < 0)
1010 if (!set_vals
|| *p
!= '-')
1012 while (*p
&& *p
!= sep
)
1024 #if LCD_DEPTH == 2 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 2)
1025 case 'g': /* greyscale colour (0-3) */
1026 d
= va_arg(ap
, int*);
1033 else if (!set_vals
|| *p
!= '-')
1037 while (*p
&& *p
!= sep
)
1044 default: /* Unknown format type */
1048 if (set_vals
&& set
)
1049 *set_vals
|= (1<<i
);