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 */
79 #include "music_screen.h"
83 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
89 /* Format a large-range value for output, using the appropriate unit so that
90 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
91 * units) if possible, and 3 significant digits are shown. If a buffer is
92 * given, the result is snprintf()'d into that buffer, otherwise the result is
94 char *output_dyn_value(char *buf
, int buf_size
, int value
,
95 const unsigned char **units
, bool bin_scale
)
97 int scale
= bin_scale
? 1024 : 1000;
102 while (value
>= scale
)
104 fraction
= value
% scale
;
109 fraction
= fraction
* 1000 / 1024;
111 if (value
>= 100 || !unit_no
)
113 else if (value
>= 10)
114 snprintf(tbuf
, sizeof(tbuf
), "%01d", fraction
/ 100);
116 snprintf(tbuf
, sizeof(tbuf
), "%02d", fraction
/ 10);
121 snprintf(buf
, buf_size
, "%d%s%s%s", value
, str(LANG_POINT
),
122 tbuf
, P2STR(units
[unit_no
]));
124 snprintf(buf
, buf_size
, "%d%s", value
, P2STR(units
[unit_no
]));
128 talk_fractional(tbuf
, value
, P2ID(units
[unit_no
]));
133 /* Ask the user if they really want to erase the current dynamic playlist
134 * returns true if the playlist should be replaced */
135 bool warn_on_pl_erase(void)
137 if (global_settings
.warnon_erase_dynplaylist
&&
138 !global_settings
.party_mode
&&
139 playlist_modified(NULL
))
141 static const char *lines
[] =
142 {ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
143 static const struct text_message message
={lines
, 1};
145 return (gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
);
151 /* Read (up to) a line of text from fd into buffer and return number of bytes
152 * read (which may be larger than the number of bytes stored in buffer). If
153 * an error occurs, -1 is returned (and buffer contains whatever could be
154 * read). A line is terminated by a LF char. Neither LF nor CR chars are
157 int read_line(int fd
, char* buffer
, int buffer_size
)
164 while (count
< buffer_size
)
168 if (1 != read(fd
, &c
, 1))
182 buffer
[MIN(count
, buffer_size
- 1)] = 0;
184 return errno
? -1 : num_read
;
187 /* Performance optimized version of the previous function. */
188 int fast_readline(int fd
, char *buf
, int buf_size
, void *parameters
,
189 int (*callback
)(int n
, const char *buf
, void *parameters
))
199 rc
= read(fd
, &buf
[pos
], buf_size
- pos
- 1);
203 if ( (p
= strchr(buf
, '\r')) != NULL
)
211 if ( (p
= strchr(p
, '\n')) != NULL
)
217 rc
= callback(count
, buf
, parameters
);
224 pos
= buf_size
- ((long)next
- (long)buf
) - 1;
225 memmove(buf
, next
, pos
);
234 /* parse a line from a configuration file. the line format is:
238 Any whitespace before setting name or value (after ':') is ignored.
239 A # as first non-whitespace character discards the whole line.
240 Function sets pointers to null-terminated setting name and value.
241 Returns false if no valid config entry was found.
244 bool settings_parseline(char* line
, char** name
, char** value
)
248 line
= skip_whitespace(line
);
253 ptr
= strchr(line
, ':');
260 ptr
= skip_whitespace(ptr
);
265 static void system_flush(void)
267 scrobbler_shutdown();
270 call_storage_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
273 static void system_restore(void)
279 static bool clean_shutdown(void (*callback
)(void *), void *parameter
)
284 bookmark_autobookmark();
285 call_storage_idle_notifys(true);
291 scrobbler_poweroff();
293 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
294 if(!charger_inserted())
297 bool batt_safe
= battery_level_safe();
298 int audio_stat
= audio_status();
302 screens
[i
].clear_display();
309 if (!tagcache_prepare_shutdown())
312 splash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
316 if (battery_level() > 10)
317 splash(0, str(LANG_SHUTTINGDOWN
));
320 msg_id
= LANG_WARNING_BATTERY_LOW
;
321 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_LOW
),
322 str(LANG_SHUTTINGDOWN
));
327 msg_id
= LANG_WARNING_BATTERY_EMPTY
;
328 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_EMPTY
),
329 str(LANG_SHUTTINGDOWN
));
332 if (global_settings
.fade_on_stop
333 && (audio_stat
& AUDIO_STATUS_PLAY
))
338 if (batt_safe
) /* do not save on critical battery */
340 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
341 if (audio_stat
& AUDIO_STATUS_RECORD
)
343 rec_command(RECORDING_CMD_STOP
);
344 /* wait for stop to complete */
345 while (audio_status() & AUDIO_STATUS_RECORD
)
349 bookmark_autobookmark();
351 /* audio_stop_recording == audio_stop for HWCODEC */
354 if (callback
!= NULL
)
357 #if CONFIG_CODEC != SWCODEC
358 /* wait for audio_stop or audio_stop_recording to complete */
359 while (audio_status())
363 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
364 audio_close_recording();
367 if(global_settings
.talk_menu
)
369 bool enqueue
= false;
372 talk_id(msg_id
, enqueue
);
375 talk_id(LANG_SHUTTINGDOWN
, enqueue
);
376 #if CONFIG_CODEC == SWCODEC
382 #ifdef HAVE_EEPROM_SETTINGS
383 if (firmware_settings
.initialized
)
385 firmware_settings
.disk_clean
= true;
386 firmware_settings
.bl_version
= 0;
387 eeprom_settings_store();
402 bool list_stop_handler(void)
406 /* Stop the music if it is playing */
409 if (!global_settings
.party_mode
)
411 if (global_settings
.fade_on_stop
)
413 bookmark_autobookmark();
415 ret
= true; /* bookmarking can make a refresh necessary */
419 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
422 if (charger_inserted())
425 shutdown_screen(); /* won't return if shutdown actually happens */
427 ret
= true; /* screen is dirty, caller needs to refresh */
430 #ifndef HAVE_POWEROFF_WHILE_CHARGING
432 static long last_off
= 0;
434 if (TIME_BEFORE(current_tick
, last_off
+ HZ
/2))
436 if (charger_inserted())
439 ret
= true; /* screen is dirty, caller needs to refresh */
442 last_off
= current_tick
;
445 #endif /* CONFIG_CHARGING */
450 static bool waiting_to_resume_play
= false;
451 static long play_resume_tick
;
453 static void car_adapter_mode_processing(bool inserted
)
455 if (global_settings
.car_adapter_mode
)
460 * Just got plugged in, delay & resume if we were playing
462 if (audio_status() & AUDIO_STATUS_PAUSE
)
464 /* delay resume a bit while the engine is cranking */
465 play_resume_tick
= current_tick
+ HZ
*5;
466 waiting_to_resume_play
= true;
472 * Just got unplugged, pause if playing
474 if ((audio_status() & AUDIO_STATUS_PLAY
) &&
475 !(audio_status() & AUDIO_STATUS_PAUSE
))
477 if (global_settings
.fade_on_stop
)
482 waiting_to_resume_play
= false;
487 static void car_adapter_tick(void)
489 if (waiting_to_resume_play
)
491 if (TIME_AFTER(current_tick
, play_resume_tick
))
493 if (audio_status() & AUDIO_STATUS_PAUSE
)
495 queue_broadcast(SYS_CAR_ADAPTER_RESUME
, 0);
497 waiting_to_resume_play
= false;
502 void car_adapter_mode_init(void)
504 tick_add_task(car_adapter_tick
);
508 #ifdef HAVE_HEADPHONE_DETECTION
509 static void unplug_change(bool inserted
)
511 static bool headphone_caused_pause
= false;
513 if (global_settings
.unplug_mode
)
515 int audio_stat
= audio_status();
518 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
519 headphone_caused_pause
&&
520 global_settings
.unplug_mode
> 1 )
523 headphone_caused_pause
= false;
525 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
526 !(audio_stat
& AUDIO_STATUS_PAUSE
))
528 headphone_caused_pause
= true;
531 if (global_settings
.unplug_rw
)
533 if (audio_current_track()->elapsed
>
534 (unsigned long)(global_settings
.unplug_rw
*1000))
535 audio_ff_rewind(audio_current_track()->elapsed
-
536 (global_settings
.unplug_rw
*1000));
546 long default_event_handler_ex(long event
, void (*callback
)(void *), void *parameter
)
550 case SYS_BATTERY_UPDATE
:
551 if(global_settings
.talk_battery_level
)
553 talk_ids(true, VOICE_PAUSE
, VOICE_PAUSE
,
555 TALK_ID(battery_level(), UNIT_PERCENT
),
557 talk_force_enqueue_next();
560 case SYS_USB_CONNECTED
:
561 if (callback
!= NULL
)
563 #if (CONFIG_STORAGE & STORAGE_MMC)
564 if (!mmc_touched() ||
565 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED
))
570 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
571 check_bootfile(false); /* gets initial size */
576 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
577 check_bootfile(true);
582 return SYS_USB_CONNECTED
;
584 if (!clean_shutdown(callback
, parameter
))
588 case SYS_CHARGER_CONNECTED
:
589 car_adapter_mode_processing(true);
590 return SYS_CHARGER_CONNECTED
;
592 case SYS_CHARGER_DISCONNECTED
:
593 car_adapter_mode_processing(false);
594 return SYS_CHARGER_DISCONNECTED
;
596 case SYS_CAR_ADAPTER_RESUME
:
598 return SYS_CAR_ADAPTER_RESUME
;
600 #ifdef HAVE_HEADPHONE_DETECTION
601 case SYS_PHONE_PLUGGED
:
603 return SYS_PHONE_PLUGGED
;
605 case SYS_PHONE_UNPLUGGED
:
606 unplug_change(false);
607 return SYS_PHONE_UNPLUGGED
;
609 #ifdef IPOD_ACCESSORY_PROTOCOL
610 case SYS_IAP_PERIODIC
:
612 return SYS_IAP_PERIODIC
;
613 case SYS_IAP_HANDLEPKT
:
615 return SYS_IAP_HANDLEPKT
;
621 long default_event_handler(long event
)
623 return default_event_handler_ex(event
, NULL
, NULL
);
626 int show_logo( void )
628 #ifdef HAVE_LCD_BITMAP
632 snprintf(version
, sizeof(version
), "Ver. %s", appsversion
);
635 #ifdef SANSA_CLIP /* display the logo in the blue area of the screen */
636 lcd_setfont(FONT_SYSFIXED
);
637 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
638 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
639 0, (unsigned char *)version
);
640 lcd_bitmap(rockboxlogo
, 0, 16, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
642 lcd_bitmap(rockboxlogo
, 0, 10, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
643 lcd_setfont(FONT_SYSFIXED
);
644 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
645 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
646 LCD_HEIGHT
-font_h
, (unsigned char *)version
);
648 lcd_setfont(FONT_UI
);
651 char *rockbox
= " ROCKbox!";
654 lcd_double_height(true);
655 lcd_puts(0, 0, rockbox
);
656 lcd_puts_scroll(0, 1, appsversion
);
660 #ifdef HAVE_REMOTE_LCD
661 lcd_remote_clear_display();
662 lcd_remote_bitmap(remote_rockboxlogo
, 0, 10, BMPWIDTH_remote_rockboxlogo
,
663 BMPHEIGHT_remote_rockboxlogo
);
664 lcd_remote_setfont(FONT_SYSFIXED
);
665 lcd_remote_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
666 lcd_remote_putsxy((LCD_REMOTE_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
667 LCD_REMOTE_HEIGHT
-font_h
, (unsigned char *)version
);
668 lcd_remote_setfont(FONT_UI
);
675 #if CONFIG_CODEC == SWCODEC
676 int get_replaygain_mode(bool have_track_gain
, bool have_album_gain
)
680 bool track
= ((global_settings
.replaygain_type
== REPLAYGAIN_TRACK
)
681 || ((global_settings
.replaygain_type
== REPLAYGAIN_SHUFFLE
)
682 && global_settings
.playlist_shuffle
));
684 type
= (!track
&& have_album_gain
) ? REPLAYGAIN_ALBUM
685 : have_track_gain
? REPLAYGAIN_TRACK
: -1;
692 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
694 memorize/compare details about the BOOTFILE
695 we don't use dircache because it may not be up to date after
696 USB disconnect (scanning in the background)
698 void check_bootfile(bool do_rolo
)
700 static unsigned short wrtdate
= 0;
701 static unsigned short wrttime
= 0;
703 struct dirent
* entry
= NULL
;
705 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
706 dir
= opendir(BOOTDIR
);
708 if(!dir
) return; /* do we want an error splash? */
710 /* loop all files in BOOTDIR */
711 while(0 != (entry
= readdir(dir
)))
713 if(!strcasecmp(entry
->d_name
, BOOTFILE
))
715 /* found the bootfile */
716 if(wrtdate
&& do_rolo
)
718 if((entry
->wrtdate
!= wrtdate
) ||
719 (entry
->wrttime
!= wrttime
))
721 static const char *lines
[] = { ID2P(LANG_BOOT_CHANGED
),
722 ID2P(LANG_REBOOT_NOW
) };
723 static const struct text_message message
={ lines
, 2 };
724 button_clear_queue(); /* Empty the keyboard buffer */
725 if(gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
)
726 rolo_load(BOOTDIR
"/" BOOTFILE
);
729 wrtdate
= entry
->wrtdate
;
730 wrttime
= entry
->wrttime
;
738 /* check range, set volume and save settings */
741 const int min_vol
= sound_min(SOUND_VOLUME
);
742 const int max_vol
= sound_max(SOUND_VOLUME
);
743 if (global_settings
.volume
< min_vol
)
744 global_settings
.volume
= min_vol
;
745 if (global_settings
.volume
> max_vol
)
746 global_settings
.volume
= max_vol
;
747 sound_set_volume(global_settings
.volume
);
751 char* strrsplt(char* str
, int c
)
753 char* s
= strrchr(str
, c
);
767 /* Test file existence, using dircache of possible */
768 bool file_exists(const char *file
)
772 if (!file
|| strlen(file
) <= 0)
776 if (dircache_is_enabled())
777 return (dircache_get_entry_ptr(file
) != NULL
);
780 fd
= open(file
, O_RDONLY
);
787 bool dir_exists(const char *path
)
789 DIR* d
= opendir(path
);
797 * removes the extension of filename (if it doesn't start with a .)
798 * puts the result in buffer
800 char *strip_extension(char* buffer
, int buffer_size
, const char *filename
)
802 char *dot
= strrchr(filename
, '.');
805 if (buffer_size
<= 0)
810 buffer_size
--; /* Make room for end nil */
812 if (dot
!= 0 && filename
[0] != '.')
814 len
= dot
- filename
;
815 len
= MIN(len
, buffer_size
);
822 strlcpy(buffer
, filename
, len
+ 1);
826 #endif /* !defined(__PCTOOL__) */
828 char* skip_whitespace(char* const str
)
838 /* Format time into buf.
840 * buf - buffer to format to.
841 * buf_size - size of buffer.
842 * t - time to format, in milliseconds.
844 void format_time(char* buf
, int buf_size
, long t
)
848 snprintf(buf
, buf_size
, "%d:%02d",
849 (int) (t
/ 60000), (int) (t
% 60000 / 1000));
853 snprintf(buf
, buf_size
, "%d:%02d:%02d",
854 (int) (t
/ 3600000), (int) (t
% 3600000 / 60000),
855 (int) (t
% 60000 / 1000));
860 /** Open a UTF-8 file and set file descriptor to first byte after BOM.
861 * If no BOM is present this behaves like open().
862 * If the file is opened for writing and O_TRUNC is set, write a BOM to
863 * the opened file and leave the file pointer set after the BOM.
865 #define BOM "\xef\xbb\xbf"
868 int open_utf8(const char* pathname
, int flags
)
871 unsigned char bom
[BOM_SIZE
];
873 fd
= open(pathname
, flags
);
877 if(flags
& (O_TRUNC
| O_WRONLY
))
879 write(fd
, BOM
, BOM_SIZE
);
883 read(fd
, bom
, BOM_SIZE
);
885 if(memcmp(bom
, BOM
, BOM_SIZE
))
886 lseek(fd
, 0, SEEK_SET
);
892 #ifdef HAVE_LCD_COLOR
894 * Helper function to convert a string of 6 hex digits to a native colour
897 static int hex2dec(int c
)
899 return (((c
) >= '0' && ((c
) <= '9')) ? (c
) - '0' :
900 (toupper(c
)) - 'A' + 10);
903 int hex_to_rgb(const char* hex
, int* color
)
905 int red
, green
, blue
;
908 while ((i
< 6) && (isxdigit(hex
[i
])))
914 red
= (hex2dec(hex
[0]) << 4) | hex2dec(hex
[1]);
915 green
= (hex2dec(hex
[2]) << 4) | hex2dec(hex
[3]);
916 blue
= (hex2dec(hex
[4]) << 4) | hex2dec(hex
[5]);
918 *color
= LCD_RGBPACK(red
,green
,blue
);
922 #endif /* HAVE_LCD_COLOR */
924 #ifdef HAVE_LCD_BITMAP
925 /* A simplified scanf - used (at time of writing) by wps parsing functions.
927 fmt - char array specifying the format of each list option. Valid values
929 s - string (sets pointer to string, without copying)
930 c - hex colour (RGB888 - e.g. ff00ff)
931 g - greyscale "colour" (0-3)
932 set_vals - if not NULL 1 is set in the bitplace if the item was read OK
934 first item is LSB, (max 32 items! )
935 Stops parseing if an item is invalid unless the item == '-'
936 sep - list separator (e.g. ',' or '|')
937 str - string to parse, must be terminated by 0 or sep
938 ... - pointers to store the parsed values
940 return value - pointer to char after parsed data, 0 if there was an error.
944 /* '0'-'3' are ASCII 0x30 to 0x33 */
945 #define is0123(x) (((x) & 0xfc) == 0x30)
947 const char* parse_list(const char *fmt
, uint32_t *set_vals
,
948 const char sep
, const char* str
, ...)
951 const char* p
= str
, *f
= fmt
;
962 /* Check for separator, if we're not at the start */
972 case 's': /* string - return a pointer to it (not a copy) */
973 s
= va_arg(ap
, const char **);
976 while (*p
&& *p
!= sep
)
978 set
= (s
[0][0]!='-') && (s
[0][1]!=sep
) ;
982 d
= va_arg(ap
, int*);
985 if (!set_vals
|| *p
!= '-')
987 while (*p
&& *p
!= sep
)
994 *d
= (*d
* 10) + (*p
++ - '0');
1000 #ifdef HAVE_LCD_COLOR
1001 case 'c': /* colour (rrggbb - e.g. f3c1a8) */
1002 d
= va_arg(ap
, int*);
1004 if (hex_to_rgb(p
, d
) < 0)
1006 if (!set_vals
|| *p
!= '-')
1008 while (*p
&& *p
!= sep
)
1020 #if LCD_DEPTH == 2 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 2)
1021 case 'g': /* greyscale colour (0-3) */
1022 d
= va_arg(ap
, int*);
1029 else if (!set_vals
|| *p
!= '-')
1033 while (*p
&& *p
!= sep
)
1040 default: /* Unknown format type */
1044 if (set_vals
&& set
)
1045 *set_vals
|= BIT_N(i
);