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 */
88 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF) \
89 || defined(HAVE_HOTSWAP_STORAGE_AS_MAIN)
94 /* units used with output_dyn_value */
95 const unsigned char * const byte_units
[] =
103 const unsigned char * const * const kbyte_units
= &byte_units
[1];
105 /* Format a large-range value for output, using the appropriate unit so that
106 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
107 * units) if possible, and 3 significant digits are shown. If a buffer is
108 * given, the result is snprintf()'d into that buffer, otherwise the result is
110 char *output_dyn_value(char *buf
, int buf_size
, int value
,
111 const unsigned char * const *units
, bool bin_scale
)
113 int scale
= bin_scale
? 1024 : 1000;
118 while (value
>= scale
)
120 fraction
= value
% scale
;
125 fraction
= fraction
* 1000 / 1024;
127 if (value
>= 100 || !unit_no
)
129 else if (value
>= 10)
130 snprintf(tbuf
, sizeof(tbuf
), "%01d", fraction
/ 100);
132 snprintf(tbuf
, sizeof(tbuf
), "%02d", fraction
/ 10);
137 snprintf(buf
, buf_size
, "%d%s%s%s", value
, str(LANG_POINT
),
138 tbuf
, P2STR(units
[unit_no
]));
140 snprintf(buf
, buf_size
, "%d%s", value
, P2STR(units
[unit_no
]));
144 talk_fractional(tbuf
, value
, P2ID(units
[unit_no
]));
149 /* Ask the user if they really want to erase the current dynamic playlist
150 * returns true if the playlist should be replaced */
151 bool warn_on_pl_erase(void)
153 if (global_settings
.warnon_erase_dynplaylist
&&
154 !global_settings
.party_mode
&&
155 playlist_modified(NULL
))
157 static const char *lines
[] =
158 {ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
159 static const struct text_message message
={lines
, 1};
161 return (gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
);
168 /* Performance optimized version of the read_line() (see below) function. */
169 int fast_readline(int fd
, char *buf
, int buf_size
, void *parameters
,
170 int (*callback
)(int n
, const char *buf
, void *parameters
))
180 rc
= read(fd
, &buf
[pos
], buf_size
- pos
- 1);
184 if ( (p
= strchr(buf
, '\r')) != NULL
)
192 if ( (p
= strchr(p
, '\n')) != NULL
)
198 rc
= callback(count
, buf
, parameters
);
205 pos
= buf_size
- ((long)next
- (long)buf
) - 1;
206 memmove(buf
, next
, pos
);
215 /* parse a line from a configuration file. the line format is:
219 Any whitespace before setting name or value (after ':') is ignored.
220 A # as first non-whitespace character discards the whole line.
221 Function sets pointers to null-terminated setting name and value.
222 Returns false if no valid config entry was found.
225 bool settings_parseline(char* line
, char** name
, char** value
)
229 line
= skip_whitespace(line
);
234 ptr
= strchr(line
, ':');
241 ptr
= skip_whitespace(ptr
);
246 static void system_flush(void)
248 scrobbler_shutdown();
251 call_storage_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
254 static void system_restore(void)
260 static bool clean_shutdown(void (*callback
)(void *), void *parameter
)
265 scrobbler_poweroff();
267 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
268 if(!charger_inserted())
271 bool batt_safe
= battery_level_safe();
272 int audio_stat
= audio_status();
276 screens
[i
].clear_display();
283 if (!tagcache_prepare_shutdown())
286 splash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
290 if (battery_level() > 10)
291 splash(0, str(LANG_SHUTTINGDOWN
));
294 msg_id
= LANG_WARNING_BATTERY_LOW
;
295 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_LOW
),
296 str(LANG_SHUTTINGDOWN
));
301 msg_id
= LANG_WARNING_BATTERY_EMPTY
;
302 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_EMPTY
),
303 str(LANG_SHUTTINGDOWN
));
306 if (global_settings
.fade_on_stop
307 && (audio_stat
& AUDIO_STATUS_PLAY
))
312 if (batt_safe
) /* do not save on critical battery */
314 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
315 if (audio_stat
& AUDIO_STATUS_RECORD
)
317 rec_command(RECORDING_CMD_STOP
);
318 /* wait for stop to complete */
319 while (audio_status() & AUDIO_STATUS_RECORD
)
323 bookmark_autobookmark(false);
325 /* audio_stop_recording == audio_stop for HWCODEC */
328 if (callback
!= NULL
)
331 #if CONFIG_CODEC != SWCODEC
332 /* wait for audio_stop or audio_stop_recording to complete */
333 while (audio_status())
337 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
338 audio_close_recording();
341 if(global_settings
.talk_menu
)
343 bool enqueue
= false;
346 talk_id(msg_id
, enqueue
);
349 talk_id(LANG_SHUTTINGDOWN
, enqueue
);
350 #if CONFIG_CODEC == SWCODEC
356 #ifdef HAVE_EEPROM_SETTINGS
357 if (firmware_settings
.initialized
)
359 firmware_settings
.disk_clean
= true;
360 firmware_settings
.bl_version
= 0;
361 eeprom_settings_store();
375 bool list_stop_handler(void)
383 /* Stop the music if it is playing */
386 if (!global_settings
.party_mode
)
388 if (global_settings
.fade_on_stop
)
390 bookmark_autobookmark(true);
392 ret
= true; /* bookmarking can make a refresh necessary */
396 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
399 if (charger_inserted())
402 shutdown_screen(); /* won't return if shutdown actually happens */
404 ret
= true; /* screen is dirty, caller needs to refresh */
407 #ifndef HAVE_POWEROFF_WHILE_CHARGING
409 static long last_off
= 0;
411 if (TIME_BEFORE(current_tick
, last_off
+ HZ
/2))
413 if (charger_inserted())
416 ret
= true; /* screen is dirty, caller needs to refresh */
419 last_off
= current_tick
;
422 #endif /* CONFIG_CHARGING */
427 static bool waiting_to_resume_play
= false;
428 static long play_resume_tick
;
430 static void car_adapter_mode_processing(bool inserted
)
432 if (global_settings
.car_adapter_mode
)
437 * Just got plugged in, delay & resume if we were playing
439 if (audio_status() & AUDIO_STATUS_PAUSE
)
441 /* delay resume a bit while the engine is cranking */
442 play_resume_tick
= current_tick
+ HZ
*5;
443 waiting_to_resume_play
= true;
449 * Just got unplugged, pause if playing
451 if ((audio_status() & AUDIO_STATUS_PLAY
) &&
452 !(audio_status() & AUDIO_STATUS_PAUSE
))
454 if (global_settings
.fade_on_stop
)
459 waiting_to_resume_play
= false;
464 static void car_adapter_tick(void)
466 if (waiting_to_resume_play
)
468 if (TIME_AFTER(current_tick
, play_resume_tick
))
470 if (audio_status() & AUDIO_STATUS_PAUSE
)
472 queue_broadcast(SYS_CAR_ADAPTER_RESUME
, 0);
474 waiting_to_resume_play
= false;
479 void car_adapter_mode_init(void)
481 tick_add_task(car_adapter_tick
);
485 #ifdef HAVE_HEADPHONE_DETECTION
486 static void unplug_change(bool inserted
)
488 static bool headphone_caused_pause
= false;
490 if (global_settings
.unplug_mode
)
492 int audio_stat
= audio_status();
495 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
496 headphone_caused_pause
&&
497 global_settings
.unplug_mode
> 1 )
500 headphone_caused_pause
= false;
502 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
503 !(audio_stat
& AUDIO_STATUS_PAUSE
))
505 headphone_caused_pause
= true;
508 if (global_settings
.unplug_rw
)
510 if (audio_current_track()->elapsed
>
511 (unsigned long)(global_settings
.unplug_rw
*1000))
512 audio_ff_rewind(audio_current_track()->elapsed
-
513 (global_settings
.unplug_rw
*1000));
523 long default_event_handler_ex(long event
, void (*callback
)(void *), void *parameter
)
525 #if CONFIG_PLATFORM & (PLATFORM_ANDROID|PLATFORM_MAEMO)
526 static bool resume
= false;
531 case SYS_BATTERY_UPDATE
:
532 if(global_settings
.talk_battery_level
)
534 talk_ids(true, VOICE_PAUSE
, VOICE_PAUSE
,
536 TALK_ID(battery_level(), UNIT_PERCENT
),
538 talk_force_enqueue_next();
541 case SYS_USB_CONNECTED
:
542 if (callback
!= NULL
)
544 #if (CONFIG_STORAGE & STORAGE_MMC)
545 if (!mmc_touched() ||
546 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED
))
551 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
552 check_bootfile(false); /* gets initial size */
555 gui_usb_screen_run(false);
557 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
558 check_bootfile(true);
563 return SYS_USB_CONNECTED
;
566 if (!clean_shutdown(callback
, parameter
))
570 case SYS_CHARGER_CONNECTED
:
571 car_adapter_mode_processing(true);
572 return SYS_CHARGER_CONNECTED
;
574 case SYS_CHARGER_DISCONNECTED
:
575 car_adapter_mode_processing(false);
576 /*reset rockbox battery runtime*/
577 global_status
.runtime
= 0;
578 return SYS_CHARGER_DISCONNECTED
;
580 case SYS_CAR_ADAPTER_RESUME
:
582 return SYS_CAR_ADAPTER_RESUME
;
584 #ifdef HAVE_HOTSWAP_STORAGE_AS_MAIN
587 /* simple sanity: assume rockbox is on the first hotswappable
588 * driver, abort out if that one isn't inserted */
590 for (i
= 0; i
< NUM_DRIVES
; i
++)
592 if (storage_removable(i
) && !storage_present(i
))
593 return SYS_FS_CHANGED
;
596 check_bootfile(true); /* state gotten in main.c:init() */
599 return SYS_FS_CHANGED
;
601 #ifdef HAVE_HEADPHONE_DETECTION
602 case SYS_PHONE_PLUGGED
:
604 return SYS_PHONE_PLUGGED
;
606 case SYS_PHONE_UNPLUGGED
:
607 unplug_change(false);
608 return SYS_PHONE_UNPLUGGED
;
610 #ifdef IPOD_ACCESSORY_PROTOCOL
611 case SYS_IAP_PERIODIC
:
613 return SYS_IAP_PERIODIC
;
614 case SYS_IAP_HANDLEPKT
:
616 return SYS_IAP_HANDLEPKT
;
618 #if CONFIG_PLATFORM & (PLATFORM_ANDROID|PLATFORM_MAEMO)
619 /* stop playback if we receive a call */
620 case SYS_CALL_INCOMING
:
621 resume
= audio_status() == AUDIO_STATUS_PLAY
;
623 return SYS_CALL_INCOMING
;
624 /* resume playback if needed */
625 case SYS_CALL_HUNG_UP
:
626 if (resume
&& playlist_resume() != -1)
628 playlist_start(global_status
.resume_index
,
629 global_status
.resume_offset
);
632 return SYS_CALL_HUNG_UP
;
634 #if (CONFIG_PLATFORM & PLATFORM_HOSTED) && defined(PLATFORM_HAS_VOLUME_CHANGE)
635 case SYS_VOLUME_CHANGED
:
637 static bool firstvolume
= true;
638 int volume
= hosted_get_volume();
639 DEBUGF("SYS_VOLUME_CHANGED: %d\n", volume
);
640 if (global_settings
.volume
!= volume
) {
641 global_settings
.volume
= volume
;
650 #ifdef HAVE_MULTIMEDIA_KEYS
651 /* multimedia keys on keyboards, headsets */
652 case BUTTON_MULTIMEDIA_PLAYPAUSE
:
654 int status
= audio_status();
655 if (status
& AUDIO_STATUS_PLAY
)
657 if (status
& AUDIO_STATUS_PAUSE
)
663 if (playlist_resume() != -1)
665 playlist_start(global_status
.resume_index
,
666 global_status
.resume_offset
);
670 case BUTTON_MULTIMEDIA_NEXT
:
673 case BUTTON_MULTIMEDIA_PREV
:
676 case BUTTON_MULTIMEDIA_STOP
:
679 case BUTTON_MULTIMEDIA_REW
:
680 case BUTTON_MULTIMEDIA_FFWD
:
681 /* not supported yet, needs to be done in the WPS */
688 long default_event_handler(long event
)
690 return default_event_handler_ex(event
, NULL
, NULL
);
693 int show_logo( void )
695 #ifdef HAVE_LCD_BITMAP
699 snprintf(version
, sizeof(version
), "Ver. %s", rbversion
);
702 #if defined(SANSA_CLIP) || defined(SANSA_CLIPV2) || defined(SANSA_CLIPPLUS)
703 /* display the logo in the blue area of the screen */
704 lcd_setfont(FONT_SYSFIXED
);
705 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
706 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
707 0, (unsigned char *)version
);
708 lcd_bitmap(rockboxlogo
, 0, 16, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
710 lcd_bitmap(rockboxlogo
, 0, 10, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
711 lcd_setfont(FONT_SYSFIXED
);
712 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
713 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
714 LCD_HEIGHT
-font_h
, (unsigned char *)version
);
716 lcd_setfont(FONT_UI
);
719 char *rockbox
= " ROCKbox!";
722 lcd_double_height(true);
723 lcd_puts(0, 0, rockbox
);
724 lcd_puts_scroll(0, 1, rbversion
);
728 #ifdef HAVE_REMOTE_LCD
729 lcd_remote_clear_display();
730 lcd_remote_bitmap(remote_rockboxlogo
, 0, 10, BMPWIDTH_remote_rockboxlogo
,
731 BMPHEIGHT_remote_rockboxlogo
);
732 lcd_remote_setfont(FONT_SYSFIXED
);
733 lcd_remote_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
734 lcd_remote_putsxy((LCD_REMOTE_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
735 LCD_REMOTE_HEIGHT
-font_h
, (unsigned char *)version
);
736 lcd_remote_setfont(FONT_UI
);
744 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF) || defined(HAVE_HOTSWAP_STORAGE_AS_MAIN)
746 memorize/compare details about the BOOTFILE
747 we don't use dircache because it may not be up to date after
748 USB disconnect (scanning in the background)
750 void check_bootfile(bool do_rolo
)
752 static unsigned short wrtdate
= 0;
753 static unsigned short wrttime
= 0;
755 struct dirent
* entry
= NULL
;
757 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
758 dir
= opendir(BOOTDIR
);
760 if(!dir
) return; /* do we want an error splash? */
762 /* loop all files in BOOTDIR */
763 while(0 != (entry
= readdir(dir
)))
765 if(!strcasecmp(entry
->d_name
, BOOTFILE
))
767 struct dirinfo info
= dir_get_info(dir
, entry
);
768 /* found the bootfile */
769 if(wrtdate
&& do_rolo
)
771 if((info
.wrtdate
!= wrtdate
) ||
772 (info
.wrttime
!= wrttime
))
774 static const char *lines
[] = { ID2P(LANG_BOOT_CHANGED
),
775 ID2P(LANG_REBOOT_NOW
) };
776 static const struct text_message message
={ lines
, 2 };
777 button_clear_queue(); /* Empty the keyboard buffer */
778 if(gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
)
779 rolo_load(BOOTDIR
"/" BOOTFILE
);
782 wrtdate
= info
.wrtdate
;
783 wrttime
= info
.wrttime
;
791 /* check range, set volume and save settings */
794 const int min_vol
= sound_min(SOUND_VOLUME
);
795 const int max_vol
= sound_max(SOUND_VOLUME
);
796 if (global_settings
.volume
< min_vol
)
797 global_settings
.volume
= min_vol
;
798 if (global_settings
.volume
> max_vol
)
799 global_settings
.volume
= max_vol
;
800 sound_set_volume(global_settings
.volume
);
801 global_status
.last_volume_change
= current_tick
;
805 char* strrsplt(char* str
, int c
)
807 char* s
= strrchr(str
, c
);
822 * removes the extension of filename (if it doesn't start with a .)
823 * puts the result in buffer
825 char *strip_extension(char* buffer
, int buffer_size
, const char *filename
)
827 char *dot
= strrchr(filename
, '.');
830 if (buffer_size
<= 0)
835 buffer_size
--; /* Make room for end nil */
837 if (dot
!= 0 && filename
[0] != '.')
839 len
= dot
- filename
;
840 len
= MIN(len
, buffer_size
);
847 strlcpy(buffer
, filename
, len
+ 1);
851 #endif /* !defined(__PCTOOL__) */
853 /* Read (up to) a line of text from fd into buffer and return number of bytes
854 * read (which may be larger than the number of bytes stored in buffer). If
855 * an error occurs, -1 is returned (and buffer contains whatever could be
856 * read). A line is terminated by a LF char. Neither LF nor CR chars are
859 int read_line(int fd
, char* buffer
, int buffer_size
)
866 while (count
< buffer_size
)
870 if (1 != read(fd
, &c
, 1))
884 buffer
[MIN(count
, buffer_size
- 1)] = 0;
886 return errno
? -1 : num_read
;
890 char* skip_whitespace(char* const str
)
900 /* Format time into buf.
902 * buf - buffer to format to.
903 * buf_size - size of buffer.
904 * t - time to format, in milliseconds.
906 void format_time(char* buf
, int buf_size
, long t
)
908 int const time
= abs(t
/ 1000);
909 int const hours
= time
/ 3600;
910 int const minutes
= time
/ 60 - hours
* 60;
911 int const seconds
= time
% 60;
912 const char * const sign
= &"-"[t
< 0 ? 0 : 1];
916 snprintf(buf
, buf_size
, "%s%d:%02d", sign
, minutes
, seconds
);
920 snprintf(buf
, buf_size
, "%s%d:%02d:%02d", sign
, hours
, minutes
,
926 /** Open a UTF-8 file and set file descriptor to first byte after BOM.
927 * If no BOM is present this behaves like open().
928 * If the file is opened for writing and O_TRUNC is set, write a BOM to
929 * the opened file and leave the file pointer set after the BOM.
931 #define BOM "\xef\xbb\xbf"
934 int open_utf8(const char* pathname
, int flags
)
937 unsigned char bom
[BOM_SIZE
];
939 fd
= open(pathname
, flags
, 0666);
943 if(flags
& (O_TRUNC
| O_WRONLY
))
945 write(fd
, BOM
, BOM_SIZE
);
949 read(fd
, bom
, BOM_SIZE
);
951 if(memcmp(bom
, BOM
, BOM_SIZE
))
952 lseek(fd
, 0, SEEK_SET
);
958 #ifdef HAVE_LCD_COLOR
960 * Helper function to convert a string of 6 hex digits to a native colour
963 static int hex2dec(int c
)
965 return (((c
) >= '0' && ((c
) <= '9')) ? (c
) - '0' :
966 (toupper(c
)) - 'A' + 10);
969 int hex_to_rgb(const char* hex
, int* color
)
971 int red
, green
, blue
;
974 while ((i
< 6) && (isxdigit(hex
[i
])))
980 red
= (hex2dec(hex
[0]) << 4) | hex2dec(hex
[1]);
981 green
= (hex2dec(hex
[2]) << 4) | hex2dec(hex
[3]);
982 blue
= (hex2dec(hex
[4]) << 4) | hex2dec(hex
[5]);
984 *color
= LCD_RGBPACK(red
,green
,blue
);
988 #endif /* HAVE_LCD_COLOR */
990 #ifdef HAVE_LCD_BITMAP
991 /* '0'-'3' are ASCII 0x30 to 0x33 */
992 #define is0123(x) (((x) & 0xfc) == 0x30)
993 #if !defined(__PCTOOL__) || defined(CHECKWPS)
994 bool parse_color(enum screen_type screen
, char *text
, int *value
)
996 (void)text
; (void)value
; /* silence warnings on mono bitmap */
999 #ifdef HAVE_LCD_COLOR
1000 if (screens
[screen
].depth
> 2)
1002 if (hex_to_rgb(text
, value
) < 0)
1009 #if LCD_DEPTH == 2 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 2)
1010 if (screens
[screen
].depth
== 2)
1012 if (text
[1] != '\0' || !is0123(*text
))
1014 *value
= *text
- '0';
1020 #endif /* !defined(__PCTOOL__) || defined(CHECKWPS) */
1022 /* only used in USB HID and set_time screen */
1023 #if defined(USB_ENABLE_HID) || (CONFIG_RTC != 0)
1024 int clamp_value_wrap(int value
, int max
, int min
)