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"
32 #include "filefuncs.h"
36 #include "lcd-remote.h"
38 #include "timefuncs.h"
40 #include "usb_screen.h"
43 #include "mp3_playback.h"
46 #include "ata_idle_notify.h"
49 #include "powermgmt.h"
50 #include "backlight.h"
55 #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 */
86 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF) \
87 || defined(HAVE_HOTSWAP_STORAGE_AS_MAIN)
92 /* units used with output_dyn_value */
93 const unsigned char * const byte_units
[] =
101 const unsigned char * const * const kbyte_units
= &byte_units
[1];
103 /* Format a large-range value for output, using the appropriate unit so that
104 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
105 * units) if possible, and 3 significant digits are shown. If a buffer is
106 * given, the result is snprintf()'d into that buffer, otherwise the result is
108 char *output_dyn_value(char *buf
, int buf_size
, int value
,
109 const unsigned char * const *units
, bool bin_scale
)
111 int scale
= bin_scale
? 1024 : 1000;
116 while (value
>= scale
)
118 fraction
= value
% scale
;
123 fraction
= fraction
* 1000 / 1024;
125 if (value
>= 100 || !unit_no
)
127 else if (value
>= 10)
128 snprintf(tbuf
, sizeof(tbuf
), "%01d", fraction
/ 100);
130 snprintf(tbuf
, sizeof(tbuf
), "%02d", fraction
/ 10);
135 snprintf(buf
, buf_size
, "%d%s%s%s", value
, str(LANG_POINT
),
136 tbuf
, P2STR(units
[unit_no
]));
138 snprintf(buf
, buf_size
, "%d%s", value
, P2STR(units
[unit_no
]));
142 talk_fractional(tbuf
, value
, P2ID(units
[unit_no
]));
147 /* Ask the user if they really want to erase the current dynamic playlist
148 * returns true if the playlist should be replaced */
149 bool warn_on_pl_erase(void)
151 if (global_settings
.warnon_erase_dynplaylist
&&
152 !global_settings
.party_mode
&&
153 playlist_modified(NULL
))
155 static const char *lines
[] =
156 {ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
157 static const struct text_message message
={lines
, 1};
159 return (gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
);
166 /* Performance optimized version of the read_line() (see below) function. */
167 int fast_readline(int fd
, char *buf
, int buf_size
, void *parameters
,
168 int (*callback
)(int n
, const char *buf
, void *parameters
))
178 rc
= read(fd
, &buf
[pos
], buf_size
- pos
- 1);
182 if ( (p
= strchr(buf
, '\r')) != NULL
)
190 if ( (p
= strchr(p
, '\n')) != NULL
)
196 rc
= callback(count
, buf
, parameters
);
203 pos
= buf_size
- ((long)next
- (long)buf
) - 1;
204 memmove(buf
, next
, pos
);
213 /* parse a line from a configuration file. the line format is:
217 Any whitespace before setting name or value (after ':') is ignored.
218 A # as first non-whitespace character discards the whole line.
219 Function sets pointers to null-terminated setting name and value.
220 Returns false if no valid config entry was found.
223 bool settings_parseline(char* line
, char** name
, char** value
)
227 line
= skip_whitespace(line
);
232 ptr
= strchr(line
, ':');
239 ptr
= skip_whitespace(ptr
);
244 static void system_flush(void)
246 scrobbler_shutdown();
249 call_storage_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
252 static void system_restore(void)
258 static bool clean_shutdown(void (*callback
)(void *), void *parameter
)
260 #if (CONFIG_PLATFORM & PLATFORM_ANDROID)
263 bookmark_autobookmark(false);
264 call_storage_idle_notifys(true);
269 scrobbler_poweroff();
271 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
272 if(!charger_inserted())
275 bool batt_safe
= battery_level_safe();
276 int audio_stat
= audio_status();
280 screens
[i
].clear_display();
287 if (!tagcache_prepare_shutdown())
290 splash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
294 if (battery_level() > 10)
295 splash(0, str(LANG_SHUTTINGDOWN
));
298 msg_id
= LANG_WARNING_BATTERY_LOW
;
299 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_LOW
),
300 str(LANG_SHUTTINGDOWN
));
305 msg_id
= LANG_WARNING_BATTERY_EMPTY
;
306 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_EMPTY
),
307 str(LANG_SHUTTINGDOWN
));
310 if (global_settings
.fade_on_stop
311 && (audio_stat
& AUDIO_STATUS_PLAY
))
316 if (batt_safe
) /* do not save on critical battery */
318 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
319 if (audio_stat
& AUDIO_STATUS_RECORD
)
321 rec_command(RECORDING_CMD_STOP
);
322 /* wait for stop to complete */
323 while (audio_status() & AUDIO_STATUS_RECORD
)
327 bookmark_autobookmark(false);
329 /* audio_stop_recording == audio_stop for HWCODEC */
332 if (callback
!= NULL
)
335 #if CONFIG_CODEC != SWCODEC
336 /* wait for audio_stop or audio_stop_recording to complete */
337 while (audio_status())
341 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
342 audio_close_recording();
345 if(global_settings
.talk_menu
)
347 bool enqueue
= false;
350 talk_id(msg_id
, enqueue
);
353 talk_id(LANG_SHUTTINGDOWN
, enqueue
);
354 #if CONFIG_CODEC == SWCODEC
360 #ifdef HAVE_EEPROM_SETTINGS
361 if (firmware_settings
.initialized
)
363 firmware_settings
.disk_clean
= true;
364 firmware_settings
.bl_version
= 0;
365 eeprom_settings_store();
380 bool list_stop_handler(void)
388 /* Stop the music if it is playing */
391 if (!global_settings
.party_mode
)
393 if (global_settings
.fade_on_stop
)
395 bookmark_autobookmark(true);
397 ret
= true; /* bookmarking can make a refresh necessary */
401 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
404 if (charger_inserted())
407 shutdown_screen(); /* won't return if shutdown actually happens */
409 ret
= true; /* screen is dirty, caller needs to refresh */
412 #ifndef HAVE_POWEROFF_WHILE_CHARGING
414 static long last_off
= 0;
416 if (TIME_BEFORE(current_tick
, last_off
+ HZ
/2))
418 if (charger_inserted())
421 ret
= true; /* screen is dirty, caller needs to refresh */
424 last_off
= current_tick
;
427 #endif /* CONFIG_CHARGING */
432 static bool waiting_to_resume_play
= false;
433 static long play_resume_tick
;
435 static void car_adapter_mode_processing(bool inserted
)
437 if (global_settings
.car_adapter_mode
)
442 * Just got plugged in, delay & resume if we were playing
444 if (audio_status() & AUDIO_STATUS_PAUSE
)
446 /* delay resume a bit while the engine is cranking */
447 play_resume_tick
= current_tick
+ HZ
*5;
448 waiting_to_resume_play
= true;
454 * Just got unplugged, pause if playing
456 if ((audio_status() & AUDIO_STATUS_PLAY
) &&
457 !(audio_status() & AUDIO_STATUS_PAUSE
))
459 if (global_settings
.fade_on_stop
)
464 waiting_to_resume_play
= false;
469 static void car_adapter_tick(void)
471 if (waiting_to_resume_play
)
473 if (TIME_AFTER(current_tick
, play_resume_tick
))
475 if (audio_status() & AUDIO_STATUS_PAUSE
)
477 queue_broadcast(SYS_CAR_ADAPTER_RESUME
, 0);
479 waiting_to_resume_play
= false;
484 void car_adapter_mode_init(void)
486 tick_add_task(car_adapter_tick
);
490 #ifdef HAVE_HEADPHONE_DETECTION
491 static void unplug_change(bool inserted
)
493 static bool headphone_caused_pause
= false;
495 if (global_settings
.unplug_mode
)
497 int audio_stat
= audio_status();
500 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
501 headphone_caused_pause
&&
502 global_settings
.unplug_mode
> 1 )
505 headphone_caused_pause
= false;
507 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
508 !(audio_stat
& AUDIO_STATUS_PAUSE
))
510 headphone_caused_pause
= true;
513 if (global_settings
.unplug_rw
)
515 if (audio_current_track()->elapsed
>
516 (unsigned long)(global_settings
.unplug_rw
*1000))
517 audio_ff_rewind(audio_current_track()->elapsed
-
518 (global_settings
.unplug_rw
*1000));
528 long default_event_handler_ex(long event
, void (*callback
)(void *), void *parameter
)
530 #if CONFIG_PLATFORM & (PLATFORM_ANDROID|PLATFORM_MAEMO)
531 static bool resume
= false;
536 case SYS_BATTERY_UPDATE
:
537 if(global_settings
.talk_battery_level
)
539 talk_ids(true, VOICE_PAUSE
, VOICE_PAUSE
,
541 TALK_ID(battery_level(), UNIT_PERCENT
),
543 talk_force_enqueue_next();
546 case SYS_USB_CONNECTED
:
547 if (callback
!= NULL
)
549 #if (CONFIG_STORAGE & STORAGE_MMC)
550 if (!mmc_touched() ||
551 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED
))
556 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
557 check_bootfile(false); /* gets initial size */
560 gui_usb_screen_run(false);
562 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
563 check_bootfile(true);
568 return SYS_USB_CONNECTED
;
571 if (!clean_shutdown(callback
, parameter
))
575 case SYS_CHARGER_CONNECTED
:
576 car_adapter_mode_processing(true);
577 return SYS_CHARGER_CONNECTED
;
579 case SYS_CHARGER_DISCONNECTED
:
580 car_adapter_mode_processing(false);
581 /*reset rockbox battery runtime*/
582 global_status
.runtime
= 0;
583 return SYS_CHARGER_DISCONNECTED
;
585 case SYS_CAR_ADAPTER_RESUME
:
587 return SYS_CAR_ADAPTER_RESUME
;
589 #ifdef HAVE_HOTSWAP_STORAGE_AS_MAIN
592 /* simple sanity: assume rockbox is on the first hotswappable
593 * driver, abort out if that one isn't inserted */
595 for (i
= 0; i
< NUM_DRIVES
; i
++)
597 if (storage_removable(i
) && !storage_present(i
))
598 return SYS_FS_CHANGED
;
601 check_bootfile(true); /* state gotten in main.c:init() */
604 return SYS_FS_CHANGED
;
606 #ifdef HAVE_HEADPHONE_DETECTION
607 case SYS_PHONE_PLUGGED
:
609 return SYS_PHONE_PLUGGED
;
611 case SYS_PHONE_UNPLUGGED
:
612 unplug_change(false);
613 return SYS_PHONE_UNPLUGGED
;
615 #ifdef IPOD_ACCESSORY_PROTOCOL
616 case SYS_IAP_PERIODIC
:
618 return SYS_IAP_PERIODIC
;
619 case SYS_IAP_HANDLEPKT
:
621 return SYS_IAP_HANDLEPKT
;
623 #if CONFIG_PLATFORM & (PLATFORM_ANDROID|PLATFORM_MAEMO)
624 /* stop playback if we receive a call */
625 case SYS_CALL_INCOMING
:
626 resume
= audio_status() == AUDIO_STATUS_PLAY
;
628 return SYS_CALL_INCOMING
;
629 /* resume playback if needed */
630 case SYS_CALL_HUNG_UP
:
631 if (resume
&& playlist_resume() != -1)
633 playlist_start(global_status
.resume_index
,
634 global_status
.resume_offset
);
637 return SYS_CALL_HUNG_UP
;
639 #ifdef HAVE_MULTIMEDIA_KEYS
640 /* multimedia keys on keyboards, headsets */
641 case BUTTON_MULTIMEDIA_PLAYPAUSE
:
643 int status
= audio_status();
644 if (status
& AUDIO_STATUS_PLAY
)
646 if (status
& AUDIO_STATUS_PAUSE
)
652 if (playlist_resume() != -1)
654 playlist_start(global_status
.resume_index
,
655 global_status
.resume_offset
);
659 case BUTTON_MULTIMEDIA_NEXT
:
662 case BUTTON_MULTIMEDIA_PREV
:
665 case BUTTON_MULTIMEDIA_STOP
:
668 case BUTTON_MULTIMEDIA_REW
:
669 case BUTTON_MULTIMEDIA_FFWD
:
670 /* not supported yet, needs to be done in the WPS */
677 long default_event_handler(long event
)
679 return default_event_handler_ex(event
, NULL
, NULL
);
682 int show_logo( void )
684 #ifdef HAVE_LCD_BITMAP
688 snprintf(version
, sizeof(version
), "Ver. %s", rbversion
);
691 #if defined(SANSA_CLIP) || defined(SANSA_CLIPV2) || defined(SANSA_CLIPPLUS)
692 /* display the logo in the blue area of the screen */
693 lcd_setfont(FONT_SYSFIXED
);
694 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
695 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
696 0, (unsigned char *)version
);
697 lcd_bitmap(rockboxlogo
, 0, 16, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
699 lcd_bitmap(rockboxlogo
, 0, 10, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
700 lcd_setfont(FONT_SYSFIXED
);
701 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
702 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
703 LCD_HEIGHT
-font_h
, (unsigned char *)version
);
705 lcd_setfont(FONT_UI
);
708 char *rockbox
= " ROCKbox!";
711 lcd_double_height(true);
712 lcd_puts(0, 0, rockbox
);
713 lcd_puts_scroll(0, 1, rbversion
);
717 #ifdef HAVE_REMOTE_LCD
718 lcd_remote_clear_display();
719 lcd_remote_bitmap(remote_rockboxlogo
, 0, 10, BMPWIDTH_remote_rockboxlogo
,
720 BMPHEIGHT_remote_rockboxlogo
);
721 lcd_remote_setfont(FONT_SYSFIXED
);
722 lcd_remote_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
723 lcd_remote_putsxy((LCD_REMOTE_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
724 LCD_REMOTE_HEIGHT
-font_h
, (unsigned char *)version
);
725 lcd_remote_setfont(FONT_UI
);
733 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF) || defined(HAVE_HOTSWAP_STORAGE_AS_MAIN)
735 memorize/compare details about the BOOTFILE
736 we don't use dircache because it may not be up to date after
737 USB disconnect (scanning in the background)
739 void check_bootfile(bool do_rolo
)
741 static unsigned short wrtdate
= 0;
742 static unsigned short wrttime
= 0;
744 struct dirent
* entry
= NULL
;
746 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
747 dir
= opendir(BOOTDIR
);
749 if(!dir
) return; /* do we want an error splash? */
751 /* loop all files in BOOTDIR */
752 while(0 != (entry
= readdir(dir
)))
754 if(!strcasecmp(entry
->d_name
, BOOTFILE
))
756 struct dirinfo info
= dir_get_info(dir
, entry
);
757 /* found the bootfile */
758 if(wrtdate
&& do_rolo
)
760 if((info
.wrtdate
!= wrtdate
) ||
761 (info
.wrttime
!= wrttime
))
763 static const char *lines
[] = { ID2P(LANG_BOOT_CHANGED
),
764 ID2P(LANG_REBOOT_NOW
) };
765 static const struct text_message message
={ lines
, 2 };
766 button_clear_queue(); /* Empty the keyboard buffer */
767 if(gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
)
768 rolo_load(BOOTDIR
"/" BOOTFILE
);
771 wrtdate
= info
.wrtdate
;
772 wrttime
= info
.wrttime
;
780 /* check range, set volume and save settings */
783 const int min_vol
= sound_min(SOUND_VOLUME
);
784 const int max_vol
= sound_max(SOUND_VOLUME
);
785 if (global_settings
.volume
< min_vol
)
786 global_settings
.volume
= min_vol
;
787 if (global_settings
.volume
> max_vol
)
788 global_settings
.volume
= max_vol
;
789 sound_set_volume(global_settings
.volume
);
790 global_status
.last_volume_change
= current_tick
;
794 char* strrsplt(char* str
, int c
)
796 char* s
= strrchr(str
, c
);
811 * removes the extension of filename (if it doesn't start with a .)
812 * puts the result in buffer
814 char *strip_extension(char* buffer
, int buffer_size
, const char *filename
)
816 char *dot
= strrchr(filename
, '.');
819 if (buffer_size
<= 0)
824 buffer_size
--; /* Make room for end nil */
826 if (dot
!= 0 && filename
[0] != '.')
828 len
= dot
- filename
;
829 len
= MIN(len
, buffer_size
);
836 strlcpy(buffer
, filename
, len
+ 1);
840 #endif /* !defined(__PCTOOL__) */
842 /* Read (up to) a line of text from fd into buffer and return number of bytes
843 * read (which may be larger than the number of bytes stored in buffer). If
844 * an error occurs, -1 is returned (and buffer contains whatever could be
845 * read). A line is terminated by a LF char. Neither LF nor CR chars are
848 int read_line(int fd
, char* buffer
, int buffer_size
)
855 while (count
< buffer_size
)
859 if (1 != read(fd
, &c
, 1))
873 buffer
[MIN(count
, buffer_size
- 1)] = 0;
875 return errno
? -1 : num_read
;
879 char* skip_whitespace(char* const str
)
889 /* Format time into buf.
891 * buf - buffer to format to.
892 * buf_size - size of buffer.
893 * t - time to format, in milliseconds.
895 void format_time(char* buf
, int buf_size
, long t
)
897 int const time
= abs(t
/ 1000);
898 int const hours
= time
/ 3600;
899 int const minutes
= time
/ 60 - hours
* 60;
900 int const seconds
= time
% 60;
901 const char * const sign
= &"-"[t
< 0 ? 0 : 1];
905 snprintf(buf
, buf_size
, "%s%d:%02d", sign
, minutes
, seconds
);
909 snprintf(buf
, buf_size
, "%s%d:%02d:%02d", sign
, hours
, minutes
,
915 /** Open a UTF-8 file and set file descriptor to first byte after BOM.
916 * If no BOM is present this behaves like open().
917 * If the file is opened for writing and O_TRUNC is set, write a BOM to
918 * the opened file and leave the file pointer set after the BOM.
920 #define BOM "\xef\xbb\xbf"
923 int open_utf8(const char* pathname
, int flags
)
926 unsigned char bom
[BOM_SIZE
];
928 fd
= open(pathname
, flags
, 0666);
932 if(flags
& (O_TRUNC
| O_WRONLY
))
934 write(fd
, BOM
, BOM_SIZE
);
938 read(fd
, bom
, BOM_SIZE
);
940 if(memcmp(bom
, BOM
, BOM_SIZE
))
941 lseek(fd
, 0, SEEK_SET
);
947 #ifdef HAVE_LCD_COLOR
949 * Helper function to convert a string of 6 hex digits to a native colour
952 static int hex2dec(int c
)
954 return (((c
) >= '0' && ((c
) <= '9')) ? (c
) - '0' :
955 (toupper(c
)) - 'A' + 10);
958 int hex_to_rgb(const char* hex
, int* color
)
960 int red
, green
, blue
;
963 while ((i
< 6) && (isxdigit(hex
[i
])))
969 red
= (hex2dec(hex
[0]) << 4) | hex2dec(hex
[1]);
970 green
= (hex2dec(hex
[2]) << 4) | hex2dec(hex
[3]);
971 blue
= (hex2dec(hex
[4]) << 4) | hex2dec(hex
[5]);
973 *color
= LCD_RGBPACK(red
,green
,blue
);
977 #endif /* HAVE_LCD_COLOR */
979 #ifdef HAVE_LCD_BITMAP
980 /* '0'-'3' are ASCII 0x30 to 0x33 */
981 #define is0123(x) (((x) & 0xfc) == 0x30)
982 #if !defined(__PCTOOL__) || defined(CHECKWPS)
983 bool parse_color(enum screen_type screen
, char *text
, int *value
)
985 (void)text
; (void)value
; /* silence warnings on mono bitmap */
988 #ifdef HAVE_LCD_COLOR
989 if (screens
[screen
].depth
> 2)
991 if (hex_to_rgb(text
, value
) < 0)
998 #if LCD_DEPTH == 2 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 2)
999 if (screens
[screen
].depth
== 2)
1001 if (text
[1] != '\0' || !is0123(*text
))
1003 *value
= *text
- '0';
1009 #endif /* !defined(__PCTOOL__) || defined(CHECKWPS) */
1011 /* only used in USB HID and set_time screen */
1012 #if defined(USB_ENABLE_HID) || (CONFIG_RTC != 0)
1013 int clamp_value_wrap(int value
, int max
, int min
)