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 ****************************************************************************/
26 #include "string-extra.h"
31 #include "filefuncs.h"
35 #include "lcd-remote.h"
37 #include "timefuncs.h"
39 #include "usb_screen.h"
42 #include "mp3_playback.h"
45 #include "ata_idle_notify.h"
48 #include "powermgmt.h"
49 #include "backlight.h"
54 #include "scrobbler.h"
60 #ifdef IPOD_ACCESSORY_PROTOCOL
64 #if (CONFIG_STORAGE & STORAGE_MMC)
68 #include "eeprom_settings.h"
69 #if defined(HAVE_RECORDING) && !defined(__PCTOOL__)
70 #include "recording.h"
72 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
75 #endif /* End HAVE_LCD_BITMAP */
81 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF) \
82 || defined(HAVE_HOTSWAP_STORAGE_AS_MAIN)
87 /* units used with output_dyn_value */
88 const unsigned char * const byte_units
[] =
96 const unsigned char * const * const kbyte_units
= &byte_units
[1];
98 /* Format a large-range value for output, using the appropriate unit so that
99 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
100 * units) if possible, and 3 significant digits are shown. If a buffer is
101 * given, the result is snprintf()'d into that buffer, otherwise the result is
103 char *output_dyn_value(char *buf
, int buf_size
, int value
,
104 const unsigned char * const *units
, bool bin_scale
)
106 int scale
= bin_scale
? 1024 : 1000;
111 while (value
>= scale
)
113 fraction
= value
% scale
;
118 fraction
= fraction
* 1000 / 1024;
120 if (value
>= 100 || !unit_no
)
122 else if (value
>= 10)
123 snprintf(tbuf
, sizeof(tbuf
), "%01d", fraction
/ 100);
125 snprintf(tbuf
, sizeof(tbuf
), "%02d", fraction
/ 10);
130 snprintf(buf
, buf_size
, "%d%s%s%s", value
, str(LANG_POINT
),
131 tbuf
, P2STR(units
[unit_no
]));
133 snprintf(buf
, buf_size
, "%d%s", value
, P2STR(units
[unit_no
]));
137 talk_fractional(tbuf
, value
, P2ID(units
[unit_no
]));
142 /* Ask the user if they really want to erase the current dynamic playlist
143 * returns true if the playlist should be replaced */
144 bool warn_on_pl_erase(void)
146 if (global_settings
.warnon_erase_dynplaylist
&&
147 !global_settings
.party_mode
&&
148 playlist_modified(NULL
))
150 static const char *lines
[] =
151 {ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
152 static const struct text_message message
={lines
, 1};
154 return (gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
);
161 /* Performance optimized version of the read_line() (see below) function. */
162 int fast_readline(int fd
, char *buf
, int buf_size
, void *parameters
,
163 int (*callback
)(int n
, const char *buf
, void *parameters
))
173 rc
= read(fd
, &buf
[pos
], buf_size
- pos
- 1);
177 if ( (p
= strchr(buf
, '\r')) != NULL
)
185 if ( (p
= strchr(p
, '\n')) != NULL
)
191 rc
= callback(count
, buf
, parameters
);
198 pos
= buf_size
- ((long)next
- (long)buf
) - 1;
199 memmove(buf
, next
, pos
);
208 /* parse a line from a configuration file. the line format is:
212 Any whitespace before setting name or value (after ':') is ignored.
213 A # as first non-whitespace character discards the whole line.
214 Function sets pointers to null-terminated setting name and value.
215 Returns false if no valid config entry was found.
218 bool settings_parseline(char* line
, char** name
, char** value
)
222 line
= skip_whitespace(line
);
227 ptr
= strchr(line
, ':');
234 ptr
= skip_whitespace(ptr
);
239 static void system_flush(void)
241 scrobbler_shutdown();
244 call_storage_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
247 static void system_restore(void)
253 static bool clean_shutdown(void (*callback
)(void *), void *parameter
)
255 #if (CONFIG_PLATFORM & PLATFORM_HOSTED)
258 bookmark_autobookmark(false);
259 call_storage_idle_notifys(true);
264 scrobbler_poweroff();
266 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
267 if(!charger_inserted())
270 bool batt_safe
= battery_level_safe();
271 int audio_stat
= audio_status();
275 screens
[i
].clear_display();
282 if (!tagcache_prepare_shutdown())
285 splash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
289 if (battery_level() > 10)
290 splash(0, str(LANG_SHUTTINGDOWN
));
293 msg_id
= LANG_WARNING_BATTERY_LOW
;
294 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_LOW
),
295 str(LANG_SHUTTINGDOWN
));
300 msg_id
= LANG_WARNING_BATTERY_EMPTY
;
301 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_EMPTY
),
302 str(LANG_SHUTTINGDOWN
));
305 if (global_settings
.fade_on_stop
306 && (audio_stat
& AUDIO_STATUS_PLAY
))
311 if (batt_safe
) /* do not save on critical battery */
313 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
314 if (audio_stat
& AUDIO_STATUS_RECORD
)
316 rec_command(RECORDING_CMD_STOP
);
317 /* wait for stop to complete */
318 while (audio_status() & AUDIO_STATUS_RECORD
)
322 bookmark_autobookmark(false);
324 /* audio_stop_recording == audio_stop for HWCODEC */
327 if (callback
!= NULL
)
330 #if CONFIG_CODEC != SWCODEC
331 /* wait for audio_stop or audio_stop_recording to complete */
332 while (audio_status())
336 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
337 audio_close_recording();
340 if(global_settings
.talk_menu
)
342 bool enqueue
= false;
345 talk_id(msg_id
, enqueue
);
348 talk_id(LANG_SHUTTINGDOWN
, enqueue
);
349 #if CONFIG_CODEC == SWCODEC
355 #ifdef HAVE_EEPROM_SETTINGS
356 if (firmware_settings
.initialized
)
358 firmware_settings
.disk_clean
= true;
359 firmware_settings
.bl_version
= 0;
360 eeprom_settings_store();
375 bool list_stop_handler(void)
379 /* Stop the music if it is playing */
382 if (!global_settings
.party_mode
)
384 if (global_settings
.fade_on_stop
)
386 bookmark_autobookmark(true);
388 ret
= true; /* bookmarking can make a refresh necessary */
392 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
395 if (charger_inserted())
398 shutdown_screen(); /* won't return if shutdown actually happens */
400 ret
= true; /* screen is dirty, caller needs to refresh */
403 #ifndef HAVE_POWEROFF_WHILE_CHARGING
405 static long last_off
= 0;
407 if (TIME_BEFORE(current_tick
, last_off
+ HZ
/2))
409 if (charger_inserted())
412 ret
= true; /* screen is dirty, caller needs to refresh */
415 last_off
= current_tick
;
418 #endif /* CONFIG_CHARGING */
423 static bool waiting_to_resume_play
= false;
424 static long play_resume_tick
;
426 static void car_adapter_mode_processing(bool inserted
)
428 if (global_settings
.car_adapter_mode
)
433 * Just got plugged in, delay & resume if we were playing
435 if (audio_status() & AUDIO_STATUS_PAUSE
)
437 /* delay resume a bit while the engine is cranking */
438 play_resume_tick
= current_tick
+ HZ
*5;
439 waiting_to_resume_play
= true;
445 * Just got unplugged, pause if playing
447 if ((audio_status() & AUDIO_STATUS_PLAY
) &&
448 !(audio_status() & AUDIO_STATUS_PAUSE
))
450 if (global_settings
.fade_on_stop
)
455 waiting_to_resume_play
= false;
460 static void car_adapter_tick(void)
462 if (waiting_to_resume_play
)
464 if (TIME_AFTER(current_tick
, play_resume_tick
))
466 if (audio_status() & AUDIO_STATUS_PAUSE
)
468 queue_broadcast(SYS_CAR_ADAPTER_RESUME
, 0);
470 waiting_to_resume_play
= false;
475 void car_adapter_mode_init(void)
477 tick_add_task(car_adapter_tick
);
481 #ifdef HAVE_HEADPHONE_DETECTION
482 static void unplug_change(bool inserted
)
484 static bool headphone_caused_pause
= false;
486 if (global_settings
.unplug_mode
)
488 int audio_stat
= audio_status();
491 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
492 headphone_caused_pause
&&
493 global_settings
.unplug_mode
> 1 )
496 headphone_caused_pause
= false;
498 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
499 !(audio_stat
& AUDIO_STATUS_PAUSE
))
501 headphone_caused_pause
= true;
504 if (global_settings
.unplug_rw
)
506 if (audio_current_track()->elapsed
>
507 (unsigned long)(global_settings
.unplug_rw
*1000))
508 audio_ff_rewind(audio_current_track()->elapsed
-
509 (global_settings
.unplug_rw
*1000));
519 long default_event_handler_ex(long event
, void (*callback
)(void *), void *parameter
)
521 #if CONFIG_PLATFORM & PLATFORM_ANDROID
522 static bool resume
= false;
526 case SYS_BATTERY_UPDATE
:
527 if(global_settings
.talk_battery_level
)
529 talk_ids(true, VOICE_PAUSE
, VOICE_PAUSE
,
531 TALK_ID(battery_level(), UNIT_PERCENT
),
533 talk_force_enqueue_next();
536 case SYS_USB_CONNECTED
:
537 if (callback
!= NULL
)
539 #if (CONFIG_STORAGE & STORAGE_MMC)
540 if (!mmc_touched() ||
541 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED
))
546 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
547 check_bootfile(false); /* gets initial size */
550 gui_usb_screen_run(false);
552 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
553 check_bootfile(true);
558 return SYS_USB_CONNECTED
;
561 if (!clean_shutdown(callback
, parameter
))
565 case SYS_CHARGER_CONNECTED
:
566 car_adapter_mode_processing(true);
567 return SYS_CHARGER_CONNECTED
;
569 case SYS_CHARGER_DISCONNECTED
:
570 car_adapter_mode_processing(false);
571 /*reset rockbox battery runtime*/
572 global_status
.runtime
= 0;
573 return SYS_CHARGER_DISCONNECTED
;
575 case SYS_CAR_ADAPTER_RESUME
:
577 return SYS_CAR_ADAPTER_RESUME
;
579 #ifdef HAVE_HOTSWAP_STORAGE_AS_MAIN
582 /* simple sanity: assume rockbox is on the first hotswappable
583 * driver, abort out if that one isn't inserted */
585 for (i
= 0; i
< NUM_DRIVES
; i
++)
587 if (storage_removable(i
) && !storage_present(i
))
588 return SYS_FS_CHANGED
;
591 check_bootfile(true); /* state gotten in main.c:init() */
594 return SYS_FS_CHANGED
;
596 #ifdef HAVE_HEADPHONE_DETECTION
597 case SYS_PHONE_PLUGGED
:
599 return SYS_PHONE_PLUGGED
;
601 case SYS_PHONE_UNPLUGGED
:
602 unplug_change(false);
603 return SYS_PHONE_UNPLUGGED
;
605 #ifdef IPOD_ACCESSORY_PROTOCOL
606 case SYS_IAP_PERIODIC
:
608 return SYS_IAP_PERIODIC
;
609 case SYS_IAP_HANDLEPKT
:
611 return SYS_IAP_HANDLEPKT
;
613 #if CONFIG_PLATFORM & PLATFORM_ANDROID
614 /* stop playback if we receive a call */
615 case SYS_CALL_INCOMING
:
616 resume
= (audio_status() & AUDIO_STATUS_PLAY
) != 0;
618 return SYS_CALL_INCOMING
;
619 /* resume playback if needed */
620 case SYS_CALL_HUNG_UP
:
621 if (resume
&& playlist_resume() != -1)
623 playlist_start(global_status
.resume_index
,
624 global_status
.resume_offset
);
627 return SYS_CALL_HUNG_UP
;
633 long default_event_handler(long event
)
635 return default_event_handler_ex(event
, NULL
, NULL
);
638 int show_logo( void )
640 #ifdef HAVE_LCD_BITMAP
644 snprintf(version
, sizeof(version
), "Ver. %s", rbversion
);
647 #if defined(SANSA_CLIP) || defined(SANSA_CLIPV2) || defined(SANSA_CLIPPLUS)
648 /* display the logo in the blue area of the screen */
649 lcd_setfont(FONT_SYSFIXED
);
650 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
651 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
652 0, (unsigned char *)version
);
653 lcd_bitmap(rockboxlogo
, 0, 16, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
655 lcd_bitmap(rockboxlogo
, 0, 10, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
656 lcd_setfont(FONT_SYSFIXED
);
657 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
658 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
659 LCD_HEIGHT
-font_h
, (unsigned char *)version
);
661 lcd_setfont(FONT_UI
);
664 char *rockbox
= " ROCKbox!";
667 lcd_double_height(true);
668 lcd_puts(0, 0, rockbox
);
669 lcd_puts_scroll(0, 1, rbversion
);
673 #ifdef HAVE_REMOTE_LCD
674 lcd_remote_clear_display();
675 lcd_remote_bitmap(remote_rockboxlogo
, 0, 10, BMPWIDTH_remote_rockboxlogo
,
676 BMPHEIGHT_remote_rockboxlogo
);
677 lcd_remote_setfont(FONT_SYSFIXED
);
678 lcd_remote_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
679 lcd_remote_putsxy((LCD_REMOTE_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
680 LCD_REMOTE_HEIGHT
-font_h
, (unsigned char *)version
);
681 lcd_remote_setfont(FONT_UI
);
689 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF) || defined(HAVE_HOTSWAP_STORAGE_AS_MAIN)
691 memorize/compare details about the BOOTFILE
692 we don't use dircache because it may not be up to date after
693 USB disconnect (scanning in the background)
695 void check_bootfile(bool do_rolo
)
697 static unsigned short wrtdate
= 0;
698 static unsigned short wrttime
= 0;
700 struct dirent
* entry
= NULL
;
702 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
703 dir
= opendir(BOOTDIR
);
705 if(!dir
) return; /* do we want an error splash? */
707 /* loop all files in BOOTDIR */
708 while(0 != (entry
= readdir(dir
)))
710 if(!strcasecmp(entry
->d_name
, BOOTFILE
))
712 struct dirinfo info
= dir_get_info(dir
, entry
);
713 /* found the bootfile */
714 if(wrtdate
&& do_rolo
)
716 if((info
.wrtdate
!= wrtdate
) ||
717 (info
.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
= info
.wrtdate
;
728 wrttime
= info
.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
);
746 global_status
.last_volume_change
= current_tick
;
750 char* strrsplt(char* str
, int c
)
752 char* s
= strrchr(str
, c
);
767 * removes the extension of filename (if it doesn't start with a .)
768 * puts the result in buffer
770 char *strip_extension(char* buffer
, int buffer_size
, const char *filename
)
772 char *dot
= strrchr(filename
, '.');
775 if (buffer_size
<= 0)
780 buffer_size
--; /* Make room for end nil */
782 if (dot
!= 0 && filename
[0] != '.')
784 len
= dot
- filename
;
785 len
= MIN(len
, buffer_size
);
792 strlcpy(buffer
, filename
, len
+ 1);
796 #endif /* !defined(__PCTOOL__) */
798 /* Read (up to) a line of text from fd into buffer and return number of bytes
799 * read (which may be larger than the number of bytes stored in buffer). If
800 * an error occurs, -1 is returned (and buffer contains whatever could be
801 * read). A line is terminated by a LF char. Neither LF nor CR chars are
804 int read_line(int fd
, char* buffer
, int buffer_size
)
811 while (count
< buffer_size
)
815 if (1 != read(fd
, &c
, 1))
829 buffer
[MIN(count
, buffer_size
- 1)] = 0;
831 return errno
? -1 : num_read
;
835 char* skip_whitespace(char* const str
)
845 /* Format time into buf.
847 * buf - buffer to format to.
848 * buf_size - size of buffer.
849 * t - time to format, in milliseconds.
851 void format_time(char* buf
, int buf_size
, long t
)
855 snprintf(buf
, buf_size
, "%d:%02d",
856 (int) (t
/ 60000), (int) (t
% 60000 / 1000));
860 snprintf(buf
, buf_size
, "%d:%02d:%02d",
861 (int) (t
/ 3600000), (int) (t
% 3600000 / 60000),
862 (int) (t
% 60000 / 1000));
867 /** Open a UTF-8 file and set file descriptor to first byte after BOM.
868 * If no BOM is present this behaves like open().
869 * If the file is opened for writing and O_TRUNC is set, write a BOM to
870 * the opened file and leave the file pointer set after the BOM.
872 #define BOM "\xef\xbb\xbf"
875 int open_utf8(const char* pathname
, int flags
)
878 unsigned char bom
[BOM_SIZE
];
880 fd
= open(pathname
, flags
);
884 if(flags
& (O_TRUNC
| O_WRONLY
))
886 write(fd
, BOM
, BOM_SIZE
);
890 read(fd
, bom
, BOM_SIZE
);
892 if(memcmp(bom
, BOM
, BOM_SIZE
))
893 lseek(fd
, 0, SEEK_SET
);
899 #ifdef HAVE_LCD_COLOR
901 * Helper function to convert a string of 6 hex digits to a native colour
904 static int hex2dec(int c
)
906 return (((c
) >= '0' && ((c
) <= '9')) ? (c
) - '0' :
907 (toupper(c
)) - 'A' + 10);
910 int hex_to_rgb(const char* hex
, int* color
)
912 int red
, green
, blue
;
915 while ((i
< 6) && (isxdigit(hex
[i
])))
921 red
= (hex2dec(hex
[0]) << 4) | hex2dec(hex
[1]);
922 green
= (hex2dec(hex
[2]) << 4) | hex2dec(hex
[3]);
923 blue
= (hex2dec(hex
[4]) << 4) | hex2dec(hex
[5]);
925 *color
= LCD_RGBPACK(red
,green
,blue
);
929 #endif /* HAVE_LCD_COLOR */
931 #ifdef HAVE_LCD_BITMAP
932 /* '0'-'3' are ASCII 0x30 to 0x33 */
933 #define is0123(x) (((x) & 0xfc) == 0x30)
934 #if !defined(__PCTOOL__) || defined(CHECKWPS)
935 bool parse_color(enum screen_type screen
, char *text
, int *value
)
937 (void)text
; (void)value
; /* silence warnings on mono bitmap */
940 #ifdef HAVE_LCD_COLOR
941 if (screens
[screen
].depth
> 2)
943 if (hex_to_rgb(text
, value
) < 0)
950 #if LCD_DEPTH == 2 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 2)
951 if (screens
[screen
].depth
== 2)
953 if (text
[1] != '\0' || !is0123(*text
))
955 *value
= *text
- '0';
961 #endif /* !defined(__PCTOOL__) || defined(CHECKWPS) */
963 /* only used in USB HID and set_time screen */
964 #if defined(USB_ENABLE_HID) || (CONFIG_RTC != 0)
965 int clamp_value_wrap(int value
, int max
, int min
)