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"
67 #ifdef IPOD_ACCESSORY_PROTOCOL
71 #if (CONFIG_STORAGE & STORAGE_MMC)
75 #include "eeprom_settings.h"
76 #if defined(HAVE_RECORDING) && !defined(__PCTOOL__)
77 #include "recording.h"
79 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
82 #endif /* End HAVE_LCD_BITMAP */
86 #if CONFIG_CODEC == SWCODEC
87 #include "voice_thread.h"
91 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF) \
92 || defined(HAVE_HOTSWAP_STORAGE_AS_MAIN)
97 /* units used with output_dyn_value */
98 const unsigned char * const byte_units
[] =
106 const unsigned char * const * const kbyte_units
= &byte_units
[1];
108 /* Format a large-range value for output, using the appropriate unit so that
109 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
110 * units) if possible, and 3 significant digits are shown. If a buffer is
111 * given, the result is snprintf()'d into that buffer, otherwise the result is
113 char *output_dyn_value(char *buf
, int buf_size
, int value
,
114 const unsigned char * const *units
, bool bin_scale
)
116 int scale
= bin_scale
? 1024 : 1000;
121 while (value
>= scale
)
123 fraction
= value
% scale
;
128 fraction
= fraction
* 1000 / 1024;
130 if (value
>= 100 || !unit_no
)
132 else if (value
>= 10)
133 snprintf(tbuf
, sizeof(tbuf
), "%01d", fraction
/ 100);
135 snprintf(tbuf
, sizeof(tbuf
), "%02d", fraction
/ 10);
140 snprintf(buf
, buf_size
, "%d%s%s%s", value
, str(LANG_POINT
),
141 tbuf
, P2STR(units
[unit_no
]));
143 snprintf(buf
, buf_size
, "%d%s", value
, P2STR(units
[unit_no
]));
147 talk_fractional(tbuf
, value
, P2ID(units
[unit_no
]));
152 /* Ask the user if they really want to erase the current dynamic playlist
153 * returns true if the playlist should be replaced */
154 bool warn_on_pl_erase(void)
156 if (global_settings
.warnon_erase_dynplaylist
&&
157 !global_settings
.party_mode
&&
158 playlist_modified(NULL
))
160 static const char *lines
[] =
161 {ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
162 static const struct text_message message
={lines
, 1};
164 return (gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
);
171 /* Performance optimized version of the read_line() (see below) function. */
172 int fast_readline(int fd
, char *buf
, int buf_size
, void *parameters
,
173 int (*callback
)(int n
, const char *buf
, void *parameters
))
183 rc
= read(fd
, &buf
[pos
], buf_size
- pos
- 1);
187 if ( (p
= strchr(buf
, '\r')) != NULL
)
195 if ( (p
= strchr(p
, '\n')) != NULL
)
201 rc
= callback(count
, buf
, parameters
);
208 pos
= buf_size
- ((long)next
- (long)buf
) - 1;
209 memmove(buf
, next
, pos
);
218 /* parse a line from a configuration file. the line format is:
222 Any whitespace before setting name or value (after ':') is ignored.
223 A # as first non-whitespace character discards the whole line.
224 Function sets pointers to null-terminated setting name and value.
225 Returns false if no valid config entry was found.
228 bool settings_parseline(char* line
, char** name
, char** value
)
232 line
= skip_whitespace(line
);
237 ptr
= strchr(line
, ':');
244 ptr
= skip_whitespace(ptr
);
249 static void system_flush(void)
251 scrobbler_shutdown();
254 call_storage_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
257 static void system_restore(void)
263 static bool clean_shutdown(void (*callback
)(void *), void *parameter
)
268 scrobbler_poweroff();
270 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
271 if(!charger_inserted())
274 bool batt_safe
= battery_level_safe();
275 int audio_stat
= audio_status();
279 screens
[i
].clear_display();
286 if (!tagcache_prepare_shutdown())
289 splash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
293 if (battery_level() > 10)
294 splash(0, str(LANG_SHUTTINGDOWN
));
297 msg_id
= LANG_WARNING_BATTERY_LOW
;
298 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_LOW
),
299 str(LANG_SHUTTINGDOWN
));
304 msg_id
= LANG_WARNING_BATTERY_EMPTY
;
305 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_EMPTY
),
306 str(LANG_SHUTTINGDOWN
));
309 if (global_settings
.fade_on_stop
310 && (audio_stat
& AUDIO_STATUS_PLAY
))
315 if (batt_safe
) /* do not save on critical battery */
317 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
318 if (audio_stat
& AUDIO_STATUS_RECORD
)
320 rec_command(RECORDING_CMD_STOP
);
321 /* wait for stop to complete */
322 while (audio_status() & AUDIO_STATUS_RECORD
)
326 bookmark_autobookmark(false);
328 /* audio_stop_recording == audio_stop for HWCODEC */
331 if (callback
!= NULL
)
334 #if CONFIG_CODEC != SWCODEC
335 /* wait for audio_stop or audio_stop_recording to complete */
336 while (audio_status())
340 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
341 audio_close_recording();
344 if(global_settings
.talk_menu
)
346 bool enqueue
= false;
349 talk_id(msg_id
, enqueue
);
352 talk_id(LANG_SHUTTINGDOWN
, enqueue
);
353 #if CONFIG_CODEC == SWCODEC
359 #ifdef HAVE_EEPROM_SETTINGS
360 if (firmware_settings
.initialized
)
362 firmware_settings
.disk_clean
= true;
363 firmware_settings
.bl_version
= 0;
364 eeprom_settings_store();
378 bool list_stop_handler(void)
386 /* Stop the music if it is playing */
389 if (!global_settings
.party_mode
)
391 if (global_settings
.fade_on_stop
)
393 bookmark_autobookmark(true);
395 ret
= true; /* bookmarking can make a refresh necessary */
399 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
402 if (charger_inserted())
405 shutdown_screen(); /* won't return if shutdown actually happens */
407 ret
= true; /* screen is dirty, caller needs to refresh */
410 #ifndef HAVE_POWEROFF_WHILE_CHARGING
412 static long last_off
= 0;
414 if (TIME_BEFORE(current_tick
, last_off
+ HZ
/2))
416 if (charger_inserted())
419 ret
= true; /* screen is dirty, caller needs to refresh */
422 last_off
= current_tick
;
425 #endif /* CONFIG_CHARGING */
430 static bool waiting_to_resume_play
= false;
431 static long play_resume_tick
;
433 static void car_adapter_mode_processing(bool inserted
)
435 if (global_settings
.car_adapter_mode
)
440 * Just got plugged in, delay & resume if we were playing
442 if (audio_status() & AUDIO_STATUS_PAUSE
)
444 /* delay resume a bit while the engine is cranking */
445 play_resume_tick
= current_tick
+ HZ
*5;
446 waiting_to_resume_play
= true;
452 * Just got unplugged, pause if playing
454 if ((audio_status() & AUDIO_STATUS_PLAY
) &&
455 !(audio_status() & AUDIO_STATUS_PAUSE
))
457 if (global_settings
.fade_on_stop
)
462 waiting_to_resume_play
= false;
467 static void car_adapter_tick(void)
469 if (waiting_to_resume_play
)
471 if (TIME_AFTER(current_tick
, play_resume_tick
))
473 if (audio_status() & AUDIO_STATUS_PAUSE
)
475 queue_broadcast(SYS_CAR_ADAPTER_RESUME
, 0);
477 waiting_to_resume_play
= false;
482 void car_adapter_mode_init(void)
484 tick_add_task(car_adapter_tick
);
488 #ifdef HAVE_HEADPHONE_DETECTION
489 static void unplug_change(bool inserted
)
491 static bool headphone_caused_pause
= false;
493 if (global_settings
.unplug_mode
)
495 int audio_stat
= audio_status();
498 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
499 headphone_caused_pause
&&
500 global_settings
.unplug_mode
> 1 )
503 headphone_caused_pause
= false;
505 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
506 !(audio_stat
& AUDIO_STATUS_PAUSE
))
508 headphone_caused_pause
= true;
511 if (global_settings
.unplug_rw
)
513 if (audio_current_track()->elapsed
>
514 (unsigned long)(global_settings
.unplug_rw
*1000))
515 audio_ff_rewind(audio_current_track()->elapsed
-
516 (global_settings
.unplug_rw
*1000));
526 long default_event_handler_ex(long event
, void (*callback
)(void *), void *parameter
)
528 #if CONFIG_PLATFORM & (PLATFORM_ANDROID|PLATFORM_MAEMO)
529 static bool resume
= false;
534 case SYS_BATTERY_UPDATE
:
535 if(global_settings
.talk_battery_level
)
537 talk_ids(true, VOICE_PAUSE
, VOICE_PAUSE
,
539 TALK_ID(battery_level(), UNIT_PERCENT
),
541 talk_force_enqueue_next();
544 case SYS_USB_CONNECTED
:
545 if (callback
!= NULL
)
547 #if (CONFIG_STORAGE & STORAGE_MMC)
548 if (!mmc_touched() ||
549 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED
))
554 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
555 check_bootfile(false); /* gets initial size */
558 gui_usb_screen_run(false);
560 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
561 check_bootfile(true);
566 return SYS_USB_CONNECTED
;
569 if (!clean_shutdown(callback
, parameter
))
573 case SYS_CHARGER_CONNECTED
:
574 car_adapter_mode_processing(true);
575 return SYS_CHARGER_CONNECTED
;
577 case SYS_CHARGER_DISCONNECTED
:
578 car_adapter_mode_processing(false);
579 /*reset rockbox battery runtime*/
580 global_status
.runtime
= 0;
581 return SYS_CHARGER_DISCONNECTED
;
583 case SYS_CAR_ADAPTER_RESUME
:
585 return SYS_CAR_ADAPTER_RESUME
;
587 #ifdef HAVE_HOTSWAP_STORAGE_AS_MAIN
590 /* simple sanity: assume rockbox is on the first hotswappable
591 * driver, abort out if that one isn't inserted */
593 for (i
= 0; i
< NUM_DRIVES
; i
++)
595 if (storage_removable(i
) && !storage_present(i
))
596 return SYS_FS_CHANGED
;
599 check_bootfile(true); /* state gotten in main.c:init() */
602 return SYS_FS_CHANGED
;
604 #ifdef HAVE_HEADPHONE_DETECTION
605 case SYS_PHONE_PLUGGED
:
607 return SYS_PHONE_PLUGGED
;
609 case SYS_PHONE_UNPLUGGED
:
610 unplug_change(false);
611 return SYS_PHONE_UNPLUGGED
;
613 #ifdef IPOD_ACCESSORY_PROTOCOL
614 case SYS_IAP_PERIODIC
:
616 return SYS_IAP_PERIODIC
;
617 case SYS_IAP_HANDLEPKT
:
619 return SYS_IAP_HANDLEPKT
;
621 #if CONFIG_PLATFORM & (PLATFORM_ANDROID|PLATFORM_MAEMO)
622 /* stop playback if we receive a call */
623 case SYS_CALL_INCOMING
:
624 resume
= audio_status() == AUDIO_STATUS_PLAY
;
626 return SYS_CALL_INCOMING
;
627 /* resume playback if needed */
628 case SYS_CALL_HUNG_UP
:
629 if (resume
&& playlist_resume() != -1)
631 playlist_start(global_status
.resume_index
,
632 global_status
.resume_offset
);
635 return SYS_CALL_HUNG_UP
;
637 #if (CONFIG_PLATFORM & PLATFORM_HOSTED) && defined(PLATFORM_HAS_VOLUME_CHANGE)
638 case SYS_VOLUME_CHANGED
:
640 static bool firstvolume
= true;
641 int volume
= hosted_get_volume();
642 DEBUGF("SYS_VOLUME_CHANGED: %d\n", volume
);
643 if (global_settings
.volume
!= volume
) {
644 global_settings
.volume
= volume
;
653 #ifdef HAVE_MULTIMEDIA_KEYS
654 /* multimedia keys on keyboards, headsets */
655 case BUTTON_MULTIMEDIA_PLAYPAUSE
:
657 int status
= audio_status();
658 if (status
& AUDIO_STATUS_PLAY
)
660 if (status
& AUDIO_STATUS_PAUSE
)
666 if (playlist_resume() != -1)
668 playlist_start(global_status
.resume_index
,
669 global_status
.resume_offset
);
673 case BUTTON_MULTIMEDIA_NEXT
:
676 case BUTTON_MULTIMEDIA_PREV
:
679 case BUTTON_MULTIMEDIA_STOP
:
682 case BUTTON_MULTIMEDIA_REW
:
683 case BUTTON_MULTIMEDIA_FFWD
:
684 /* not supported yet, needs to be done in the WPS */
691 long default_event_handler(long event
)
693 return default_event_handler_ex(event
, NULL
, NULL
);
696 int show_logo( void )
698 #ifdef HAVE_LCD_BITMAP
702 snprintf(version
, sizeof(version
), "Ver. %s", rbversion
);
705 #if defined(SANSA_CLIP) || defined(SANSA_CLIPV2) || defined(SANSA_CLIPPLUS)
706 /* display the logo in the blue area of the screen */
707 lcd_setfont(FONT_SYSFIXED
);
708 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
709 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
710 0, (unsigned char *)version
);
711 lcd_bitmap(rockboxlogo
, 0, 16, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
713 lcd_bitmap(rockboxlogo
, 0, 10, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
714 lcd_setfont(FONT_SYSFIXED
);
715 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
716 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
717 LCD_HEIGHT
-font_h
, (unsigned char *)version
);
719 lcd_setfont(FONT_UI
);
722 char *rockbox
= " ROCKbox!";
725 lcd_double_height(true);
726 lcd_puts(0, 0, rockbox
);
727 lcd_puts_scroll(0, 1, rbversion
);
731 #ifdef HAVE_REMOTE_LCD
732 lcd_remote_clear_display();
733 lcd_remote_bitmap(remote_rockboxlogo
, 0, 10, BMPWIDTH_remote_rockboxlogo
,
734 BMPHEIGHT_remote_rockboxlogo
);
735 lcd_remote_setfont(FONT_SYSFIXED
);
736 lcd_remote_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
737 lcd_remote_putsxy((LCD_REMOTE_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
738 LCD_REMOTE_HEIGHT
-font_h
, (unsigned char *)version
);
739 lcd_remote_setfont(FONT_UI
);
747 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF) || defined(HAVE_HOTSWAP_STORAGE_AS_MAIN)
749 memorize/compare details about the BOOTFILE
750 we don't use dircache because it may not be up to date after
751 USB disconnect (scanning in the background)
753 void check_bootfile(bool do_rolo
)
755 static unsigned short wrtdate
= 0;
756 static unsigned short wrttime
= 0;
758 struct dirent
* entry
= NULL
;
760 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
761 dir
= opendir(BOOTDIR
);
763 if(!dir
) return; /* do we want an error splash? */
765 /* loop all files in BOOTDIR */
766 while(0 != (entry
= readdir(dir
)))
768 if(!strcasecmp(entry
->d_name
, BOOTFILE
))
770 struct dirinfo info
= dir_get_info(dir
, entry
);
771 /* found the bootfile */
772 if(wrtdate
&& do_rolo
)
774 if((info
.wrtdate
!= wrtdate
) ||
775 (info
.wrttime
!= wrttime
))
777 static const char *lines
[] = { ID2P(LANG_BOOT_CHANGED
),
778 ID2P(LANG_REBOOT_NOW
) };
779 static const struct text_message message
={ lines
, 2 };
780 button_clear_queue(); /* Empty the keyboard buffer */
781 if(gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
)
782 rolo_load(BOOTDIR
"/" BOOTFILE
);
785 wrtdate
= info
.wrtdate
;
786 wrttime
= info
.wrttime
;
794 /* check range, set volume and save settings */
797 const int min_vol
= sound_min(SOUND_VOLUME
);
798 const int max_vol
= sound_max(SOUND_VOLUME
);
799 if (global_settings
.volume
< min_vol
)
800 global_settings
.volume
= min_vol
;
801 if (global_settings
.volume
> max_vol
)
802 global_settings
.volume
= max_vol
;
803 sound_set_volume(global_settings
.volume
);
804 global_status
.last_volume_change
= current_tick
;
808 char* strrsplt(char* str
, int c
)
810 char* s
= strrchr(str
, c
);
825 * removes the extension of filename (if it doesn't start with a .)
826 * puts the result in buffer
828 char *strip_extension(char* buffer
, int buffer_size
, const char *filename
)
830 char *dot
= strrchr(filename
, '.');
833 if (buffer_size
<= 0)
838 buffer_size
--; /* Make room for end nil */
840 if (dot
!= 0 && filename
[0] != '.')
842 len
= dot
- filename
;
843 len
= MIN(len
, buffer_size
);
850 strlcpy(buffer
, filename
, len
+ 1);
854 #endif /* !defined(__PCTOOL__) */
856 /* Read (up to) a line of text from fd into buffer and return number of bytes
857 * read (which may be larger than the number of bytes stored in buffer). If
858 * an error occurs, -1 is returned (and buffer contains whatever could be
859 * read). A line is terminated by a LF char. Neither LF nor CR chars are
862 int read_line(int fd
, char* buffer
, int buffer_size
)
869 while (count
< buffer_size
)
873 if (1 != read(fd
, &c
, 1))
887 buffer
[MIN(count
, buffer_size
- 1)] = 0;
889 return errno
? -1 : num_read
;
893 char* skip_whitespace(char* const str
)
903 /* Format time into buf.
905 * buf - buffer to format to.
906 * buf_size - size of buffer.
907 * t - time to format, in milliseconds.
909 void format_time(char* buf
, int buf_size
, long t
)
911 int const time
= abs(t
/ 1000);
912 int const hours
= time
/ 3600;
913 int const minutes
= time
/ 60 - hours
* 60;
914 int const seconds
= time
% 60;
915 const char * const sign
= &"-"[t
< 0 ? 0 : 1];
919 snprintf(buf
, buf_size
, "%s%d:%02d", sign
, minutes
, seconds
);
923 snprintf(buf
, buf_size
, "%s%d:%02d:%02d", sign
, hours
, minutes
,
929 /** Open a UTF-8 file and set file descriptor to first byte after BOM.
930 * If no BOM is present this behaves like open().
931 * If the file is opened for writing and O_TRUNC is set, write a BOM to
932 * the opened file and leave the file pointer set after the BOM.
934 #define BOM "\xef\xbb\xbf"
937 int open_utf8(const char* pathname
, int flags
)
940 unsigned char bom
[BOM_SIZE
];
942 fd
= open(pathname
, flags
, 0666);
946 if(flags
& (O_TRUNC
| O_WRONLY
))
948 write(fd
, BOM
, BOM_SIZE
);
952 read(fd
, bom
, BOM_SIZE
);
954 if(memcmp(bom
, BOM
, BOM_SIZE
))
955 lseek(fd
, 0, SEEK_SET
);
961 #ifdef HAVE_LCD_COLOR
963 * Helper function to convert a string of 6 hex digits to a native colour
966 static int hex2dec(int c
)
968 return (((c
) >= '0' && ((c
) <= '9')) ? (c
) - '0' :
969 (toupper(c
)) - 'A' + 10);
972 int hex_to_rgb(const char* hex
, int* color
)
974 int red
, green
, blue
;
977 while ((i
< 6) && (isxdigit(hex
[i
])))
983 red
= (hex2dec(hex
[0]) << 4) | hex2dec(hex
[1]);
984 green
= (hex2dec(hex
[2]) << 4) | hex2dec(hex
[3]);
985 blue
= (hex2dec(hex
[4]) << 4) | hex2dec(hex
[5]);
987 *color
= LCD_RGBPACK(red
,green
,blue
);
991 #endif /* HAVE_LCD_COLOR */
993 #ifdef HAVE_LCD_BITMAP
994 /* '0'-'3' are ASCII 0x30 to 0x33 */
995 #define is0123(x) (((x) & 0xfc) == 0x30)
996 #if !defined(__PCTOOL__) || defined(CHECKWPS)
997 bool parse_color(enum screen_type screen
, char *text
, int *value
)
999 (void)text
; (void)value
; /* silence warnings on mono bitmap */
1002 #ifdef HAVE_LCD_COLOR
1003 if (screens
[screen
].depth
> 2)
1005 if (hex_to_rgb(text
, value
) < 0)
1012 #if LCD_DEPTH == 2 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 2)
1013 if (screens
[screen
].depth
== 2)
1015 if (text
[1] != '\0' || !is0123(*text
))
1017 *value
= *text
- '0';
1023 #endif /* !defined(__PCTOOL__) || defined(CHECKWPS) */
1025 /* only used in USB HID and set_time screen */
1026 #if defined(USB_ENABLE_HID) || (CONFIG_RTC != 0)
1027 int clamp_value_wrap(int value
, int max
, int min
)