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
, '\n')) != NULL
)
193 rc
= callback(count
, buf
, parameters
);
200 pos
= buf_size
- ((long)next
- (long)buf
) - 1;
201 memmove(buf
, next
, pos
);
210 /* parse a line from a configuration file. the line format is:
214 Any whitespace before setting name or value (after ':') is ignored.
215 A # as first non-whitespace character discards the whole line.
216 Function sets pointers to null-terminated setting name and value.
217 Returns false if no valid config entry was found.
220 bool settings_parseline(char* line
, char** name
, char** value
)
224 line
= skip_whitespace(line
);
229 ptr
= strchr(line
, ':');
236 ptr
= skip_whitespace(ptr
);
241 static void system_flush(void)
243 scrobbler_shutdown();
246 call_storage_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
249 static void system_restore(void)
255 static bool clean_shutdown(void (*callback
)(void *), void *parameter
)
260 scrobbler_poweroff();
262 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
263 if(!charger_inserted())
266 bool batt_safe
= battery_level_safe();
267 int audio_stat
= audio_status();
271 screens
[i
].clear_display();
278 if (!tagcache_prepare_shutdown())
281 splash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
285 if (battery_level() > 10)
286 splash(0, str(LANG_SHUTTINGDOWN
));
289 msg_id
= LANG_WARNING_BATTERY_LOW
;
290 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_LOW
),
291 str(LANG_SHUTTINGDOWN
));
296 msg_id
= LANG_WARNING_BATTERY_EMPTY
;
297 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_EMPTY
),
298 str(LANG_SHUTTINGDOWN
));
301 if (global_settings
.fade_on_stop
302 && (audio_stat
& AUDIO_STATUS_PLAY
))
307 if (batt_safe
) /* do not save on critical battery */
309 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
310 if (audio_stat
& AUDIO_STATUS_RECORD
)
312 rec_command(RECORDING_CMD_STOP
);
313 /* wait for stop to complete */
314 while (audio_status() & AUDIO_STATUS_RECORD
)
318 bookmark_autobookmark(false);
320 /* audio_stop_recording == audio_stop for HWCODEC */
323 if (callback
!= NULL
)
326 #if CONFIG_CODEC != SWCODEC
327 /* wait for audio_stop or audio_stop_recording to complete */
328 while (audio_status())
332 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
333 audio_close_recording();
336 if(global_settings
.talk_menu
)
338 bool enqueue
= false;
341 talk_id(msg_id
, enqueue
);
344 talk_id(LANG_SHUTTINGDOWN
, enqueue
);
345 #if CONFIG_CODEC == SWCODEC
351 #ifdef HAVE_EEPROM_SETTINGS
352 if (firmware_settings
.initialized
)
354 firmware_settings
.disk_clean
= true;
355 firmware_settings
.bl_version
= 0;
356 eeprom_settings_store();
370 bool list_stop_handler(void)
378 /* Stop the music if it is playing */
381 if (!global_settings
.party_mode
)
383 if (global_settings
.fade_on_stop
)
385 bookmark_autobookmark(true);
387 ret
= true; /* bookmarking can make a refresh necessary */
391 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
394 if (charger_inserted())
397 shutdown_screen(); /* won't return if shutdown actually happens */
399 ret
= true; /* screen is dirty, caller needs to refresh */
402 #ifndef HAVE_POWEROFF_WHILE_CHARGING
404 static long last_off
= 0;
406 if (TIME_BEFORE(current_tick
, last_off
+ HZ
/2))
408 if (charger_inserted())
411 ret
= true; /* screen is dirty, caller needs to refresh */
414 last_off
= current_tick
;
417 #endif /* CONFIG_CHARGING */
422 static bool waiting_to_resume_play
= false;
423 static long play_resume_tick
;
425 static void car_adapter_mode_processing(bool inserted
)
427 if (global_settings
.car_adapter_mode
)
432 * Just got plugged in, delay & resume if we were playing
434 if (audio_status() & AUDIO_STATUS_PAUSE
)
436 /* delay resume a bit while the engine is cranking */
437 play_resume_tick
= current_tick
+ HZ
*5;
438 waiting_to_resume_play
= true;
444 * Just got unplugged, pause if playing
446 if ((audio_status() & AUDIO_STATUS_PLAY
) &&
447 !(audio_status() & AUDIO_STATUS_PAUSE
))
449 pause_action(true, true);
451 waiting_to_resume_play
= false;
456 static void car_adapter_tick(void)
458 if (waiting_to_resume_play
)
460 if (TIME_AFTER(current_tick
, play_resume_tick
))
462 if (audio_status() & AUDIO_STATUS_PAUSE
)
464 queue_broadcast(SYS_CAR_ADAPTER_RESUME
, 0);
466 waiting_to_resume_play
= false;
471 void car_adapter_mode_init(void)
473 tick_add_task(car_adapter_tick
);
477 #ifdef HAVE_HEADPHONE_DETECTION
478 static void unplug_change(bool inserted
)
480 static bool headphone_caused_pause
= false;
482 if (global_settings
.unplug_mode
)
484 int audio_stat
= audio_status();
488 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
489 headphone_caused_pause
&&
490 global_settings
.unplug_mode
> 1 )
491 unpause_action(true, true);
492 headphone_caused_pause
= false;
494 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
495 !(audio_stat
& AUDIO_STATUS_PAUSE
))
497 headphone_caused_pause
= true;
498 pause_action(false, false);
505 long default_event_handler_ex(long event
, void (*callback
)(void *), void *parameter
)
507 #if CONFIG_PLATFORM & (PLATFORM_ANDROID|PLATFORM_MAEMO)
508 static bool resume
= false;
513 case SYS_BATTERY_UPDATE
:
514 if(global_settings
.talk_battery_level
)
516 talk_ids(true, VOICE_PAUSE
, VOICE_PAUSE
,
518 TALK_ID(battery_level(), UNIT_PERCENT
),
520 talk_force_enqueue_next();
523 case SYS_USB_CONNECTED
:
524 if (callback
!= NULL
)
526 #if (CONFIG_STORAGE & STORAGE_MMC)
527 if (!mmc_touched() ||
528 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED
))
533 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
534 check_bootfile(false); /* gets initial size */
537 gui_usb_screen_run(false);
539 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
540 check_bootfile(true);
545 return SYS_USB_CONNECTED
;
548 if (!clean_shutdown(callback
, parameter
))
552 case SYS_CHARGER_CONNECTED
:
553 car_adapter_mode_processing(true);
554 return SYS_CHARGER_CONNECTED
;
556 case SYS_CHARGER_DISCONNECTED
:
557 car_adapter_mode_processing(false);
558 /*reset rockbox battery runtime*/
559 global_status
.runtime
= 0;
560 return SYS_CHARGER_DISCONNECTED
;
562 case SYS_CAR_ADAPTER_RESUME
:
563 unpause_action(true, true);
564 return SYS_CAR_ADAPTER_RESUME
;
566 #ifdef HAVE_HOTSWAP_STORAGE_AS_MAIN
569 /* simple sanity: assume rockbox is on the first hotswappable
570 * driver, abort out if that one isn't inserted */
572 for (i
= 0; i
< NUM_DRIVES
; i
++)
574 if (storage_removable(i
) && !storage_present(i
))
575 return SYS_FS_CHANGED
;
578 check_bootfile(true); /* state gotten in main.c:init() */
581 return SYS_FS_CHANGED
;
583 #ifdef HAVE_HEADPHONE_DETECTION
584 case SYS_PHONE_PLUGGED
:
586 return SYS_PHONE_PLUGGED
;
588 case SYS_PHONE_UNPLUGGED
:
589 unplug_change(false);
590 return SYS_PHONE_UNPLUGGED
;
592 #ifdef IPOD_ACCESSORY_PROTOCOL
593 case SYS_IAP_PERIODIC
:
595 return SYS_IAP_PERIODIC
;
596 case SYS_IAP_HANDLEPKT
:
598 return SYS_IAP_HANDLEPKT
;
600 #if CONFIG_PLATFORM & (PLATFORM_ANDROID|PLATFORM_MAEMO)
601 /* stop playback if we receive a call */
602 case SYS_CALL_INCOMING
:
603 resume
= audio_status() == AUDIO_STATUS_PLAY
;
605 return SYS_CALL_INCOMING
;
606 /* resume playback if needed */
607 case SYS_CALL_HUNG_UP
:
608 if (resume
&& playlist_resume() != -1)
610 playlist_start(global_status
.resume_index
,
611 global_status
.resume_offset
);
614 return SYS_CALL_HUNG_UP
;
616 #if (CONFIG_PLATFORM & PLATFORM_HOSTED) && defined(PLATFORM_HAS_VOLUME_CHANGE)
617 case SYS_VOLUME_CHANGED
:
619 static bool firstvolume
= true;
620 int volume
= hosted_get_volume();
621 DEBUGF("SYS_VOLUME_CHANGED: %d\n", volume
);
622 if (global_settings
.volume
!= volume
) {
623 global_settings
.volume
= volume
;
632 #ifdef HAVE_MULTIMEDIA_KEYS
633 /* multimedia keys on keyboards, headsets */
634 case BUTTON_MULTIMEDIA_PLAYPAUSE
:
636 int status
= audio_status();
637 if (status
& AUDIO_STATUS_PLAY
)
639 if (status
& AUDIO_STATUS_PAUSE
)
640 unpause_action(true, true);
642 pause_action(true, true);
645 if (playlist_resume() != -1)
647 playlist_start(global_status
.resume_index
,
648 global_status
.resume_offset
);
652 case BUTTON_MULTIMEDIA_NEXT
:
655 case BUTTON_MULTIMEDIA_PREV
:
658 case BUTTON_MULTIMEDIA_STOP
:
661 case BUTTON_MULTIMEDIA_REW
:
662 case BUTTON_MULTIMEDIA_FFWD
:
663 /* not supported yet, needs to be done in the WPS */
670 long default_event_handler(long event
)
672 return default_event_handler_ex(event
, NULL
, NULL
);
675 int show_logo( void )
677 #ifdef HAVE_LCD_BITMAP
681 snprintf(version
, sizeof(version
), "Ver. %s", rbversion
);
684 #if defined(SANSA_CLIP) || defined(SANSA_CLIPV2) || defined(SANSA_CLIPPLUS)
685 /* display the logo in the blue area of the screen */
686 lcd_setfont(FONT_SYSFIXED
);
687 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
688 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
689 0, (unsigned char *)version
);
690 lcd_bitmap(rockboxlogo
, (LCD_WIDTH
- BMPWIDTH_rockboxlogo
) / 2, 16,
691 BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
693 lcd_bitmap(rockboxlogo
, (LCD_WIDTH
- BMPWIDTH_rockboxlogo
) / 2, 10,
694 BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
695 lcd_setfont(FONT_SYSFIXED
);
696 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
697 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
698 LCD_HEIGHT
-font_h
, (unsigned char *)version
);
700 lcd_setfont(FONT_UI
);
703 char *rockbox
= " ROCKbox!";
706 lcd_double_height(true);
707 lcd_puts(0, 0, rockbox
);
708 lcd_puts_scroll(0, 1, rbversion
);
712 #ifdef HAVE_REMOTE_LCD
713 lcd_remote_clear_display();
714 lcd_remote_bitmap(remote_rockboxlogo
, 0, 10, BMPWIDTH_remote_rockboxlogo
,
715 BMPHEIGHT_remote_rockboxlogo
);
716 lcd_remote_setfont(FONT_SYSFIXED
);
717 lcd_remote_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
718 lcd_remote_putsxy((LCD_REMOTE_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
719 LCD_REMOTE_HEIGHT
-font_h
, (unsigned char *)version
);
720 lcd_remote_setfont(FONT_UI
);
728 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF) || defined(HAVE_HOTSWAP_STORAGE_AS_MAIN)
730 memorize/compare details about the BOOTFILE
731 we don't use dircache because it may not be up to date after
732 USB disconnect (scanning in the background)
734 void check_bootfile(bool do_rolo
)
736 static unsigned short wrtdate
= 0;
737 static unsigned short wrttime
= 0;
739 struct dirent
* entry
= NULL
;
741 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
742 dir
= opendir(BOOTDIR
);
744 if(!dir
) return; /* do we want an error splash? */
746 /* loop all files in BOOTDIR */
747 while(0 != (entry
= readdir(dir
)))
749 if(!strcasecmp(entry
->d_name
, BOOTFILE
))
751 struct dirinfo info
= dir_get_info(dir
, entry
);
752 /* found the bootfile */
753 if(wrtdate
&& do_rolo
)
755 if((info
.wrtdate
!= wrtdate
) ||
756 (info
.wrttime
!= wrttime
))
758 static const char *lines
[] = { ID2P(LANG_BOOT_CHANGED
),
759 ID2P(LANG_REBOOT_NOW
) };
760 static const struct text_message message
={ lines
, 2 };
761 button_clear_queue(); /* Empty the keyboard buffer */
762 if(gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
)
763 rolo_load(BOOTDIR
"/" BOOTFILE
);
766 wrtdate
= info
.wrtdate
;
767 wrttime
= info
.wrttime
;
775 /* check range, set volume and save settings */
778 const int min_vol
= sound_min(SOUND_VOLUME
);
779 const int max_vol
= sound_max(SOUND_VOLUME
);
780 if (global_settings
.volume
< min_vol
)
781 global_settings
.volume
= min_vol
;
782 if (global_settings
.volume
> max_vol
)
783 global_settings
.volume
= max_vol
;
784 sound_set_volume(global_settings
.volume
);
785 global_status
.last_volume_change
= current_tick
;
789 char* strrsplt(char* str
, int c
)
791 char* s
= strrchr(str
, c
);
806 * removes the extension of filename (if it doesn't start with a .)
807 * puts the result in buffer
809 char *strip_extension(char* buffer
, int buffer_size
, const char *filename
)
811 char *dot
= strrchr(filename
, '.');
814 if (buffer_size
<= 0)
819 buffer_size
--; /* Make room for end nil */
821 if (dot
!= 0 && filename
[0] != '.')
823 len
= dot
- filename
;
824 len
= MIN(len
, buffer_size
);
831 strlcpy(buffer
, filename
, len
+ 1);
835 #endif /* !defined(__PCTOOL__) */
837 /* Read (up to) a line of text from fd into buffer and return number of bytes
838 * read (which may be larger than the number of bytes stored in buffer). If
839 * an error occurs, -1 is returned (and buffer contains whatever could be
840 * read). A line is terminated by a LF char. Neither LF nor CR chars are
843 int read_line(int fd
, char* buffer
, int buffer_size
)
850 while (count
< buffer_size
)
854 if (1 != read(fd
, &c
, 1))
868 buffer
[MIN(count
, buffer_size
- 1)] = 0;
870 return errno
? -1 : num_read
;
874 char* skip_whitespace(char* const str
)
884 /* Format time into buf.
886 * buf - buffer to format to.
887 * buf_size - size of buffer.
888 * t - time to format, in milliseconds.
890 void format_time(char* buf
, int buf_size
, long t
)
892 int const time
= abs(t
/ 1000);
893 int const hours
= time
/ 3600;
894 int const minutes
= time
/ 60 - hours
* 60;
895 int const seconds
= time
% 60;
896 const char * const sign
= &"-"[t
< 0 ? 0 : 1];
900 snprintf(buf
, buf_size
, "%s%d:%02d", sign
, minutes
, seconds
);
904 snprintf(buf
, buf_size
, "%s%d:%02d:%02d", sign
, hours
, minutes
,
910 /** Open a UTF-8 file and set file descriptor to first byte after BOM.
911 * If no BOM is present this behaves like open().
912 * If the file is opened for writing and O_TRUNC is set, write a BOM to
913 * the opened file and leave the file pointer set after the BOM.
915 #define BOM "\xef\xbb\xbf"
918 int open_utf8(const char* pathname
, int flags
)
921 unsigned char bom
[BOM_SIZE
];
923 fd
= open(pathname
, flags
, 0666);
927 if(flags
& (O_TRUNC
| O_WRONLY
))
929 write(fd
, BOM
, BOM_SIZE
);
933 read(fd
, bom
, BOM_SIZE
);
935 if(memcmp(bom
, BOM
, BOM_SIZE
))
936 lseek(fd
, 0, SEEK_SET
);
942 #ifdef HAVE_LCD_COLOR
944 * Helper function to convert a string of 6 hex digits to a native colour
947 static int hex2dec(int c
)
949 return (((c
) >= '0' && ((c
) <= '9')) ? (c
) - '0' :
950 (toupper(c
)) - 'A' + 10);
953 int hex_to_rgb(const char* hex
, int* color
)
955 int red
, green
, blue
;
958 while ((i
< 6) && (isxdigit(hex
[i
])))
964 red
= (hex2dec(hex
[0]) << 4) | hex2dec(hex
[1]);
965 green
= (hex2dec(hex
[2]) << 4) | hex2dec(hex
[3]);
966 blue
= (hex2dec(hex
[4]) << 4) | hex2dec(hex
[5]);
968 *color
= LCD_RGBPACK(red
,green
,blue
);
972 #endif /* HAVE_LCD_COLOR */
974 #ifdef HAVE_LCD_BITMAP
975 /* '0'-'3' are ASCII 0x30 to 0x33 */
976 #define is0123(x) (((x) & 0xfc) == 0x30)
977 #if !defined(__PCTOOL__) || defined(CHECKWPS)
978 bool parse_color(enum screen_type screen
, char *text
, int *value
)
980 (void)text
; (void)value
; /* silence warnings on mono bitmap */
983 #ifdef HAVE_LCD_COLOR
984 if (screens
[screen
].depth
> 2)
986 if (hex_to_rgb(text
, value
) < 0)
993 #if LCD_DEPTH == 2 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 2)
994 if (screens
[screen
].depth
== 2)
996 if (text
[1] != '\0' || !is0123(*text
))
998 *value
= *text
- '0';
1004 #endif /* !defined(__PCTOOL__) || defined(CHECKWPS) */
1006 /* only used in USB HID and set_time screen */
1007 #if defined(USB_ENABLE_HID) || (CONFIG_RTC != 0)
1008 int clamp_value_wrap(int value
, int max
, int min
)
1018 #define MAX_ACTIVITY_DEPTH 12
1019 static enum current_activity
1020 current_activity
[MAX_ACTIVITY_DEPTH
] = {ACTIVITY_UNKNOWN
};
1021 static int current_activity_top
= 0;
1022 void push_current_activity(enum current_activity screen
)
1024 current_activity
[current_activity_top
++] = screen
;
1026 void pop_current_activity(void)
1028 current_activity_top
--;
1030 enum current_activity
get_current_activity(void)
1032 return current_activity
[current_activity_top
?current_activity_top
-1:0];