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 ****************************************************************************/
35 #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"
61 #ifdef IPOD_ACCESSORY_PROTOCOL
65 #if (CONFIG_STORAGE & STORAGE_MMC)
69 #include "eeprom_settings.h"
70 #if defined(HAVE_RECORDING) && !defined(__PCTOOL__)
71 #include "recording.h"
73 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
76 #endif /* End HAVE_LCD_BITMAP */
82 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
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
);
160 /* Read (up to) a line of text from fd into buffer and return number of bytes
161 * read (which may be larger than the number of bytes stored in buffer). If
162 * an error occurs, -1 is returned (and buffer contains whatever could be
163 * read). A line is terminated by a LF char. Neither LF nor CR chars are
166 int read_line(int fd
, char* buffer
, int buffer_size
)
173 while (count
< buffer_size
)
177 if (1 != read(fd
, &c
, 1))
191 buffer
[MIN(count
, buffer_size
- 1)] = 0;
193 return errno
? -1 : num_read
;
196 /* Performance optimized version of the previous function. */
197 int fast_readline(int fd
, char *buf
, int buf_size
, void *parameters
,
198 int (*callback
)(int n
, const char *buf
, void *parameters
))
208 rc
= read(fd
, &buf
[pos
], buf_size
- pos
- 1);
212 if ( (p
= strchr(buf
, '\r')) != NULL
)
220 if ( (p
= strchr(p
, '\n')) != NULL
)
226 rc
= callback(count
, buf
, parameters
);
233 pos
= buf_size
- ((long)next
- (long)buf
) - 1;
234 memmove(buf
, next
, pos
);
243 /* parse a line from a configuration file. the line format is:
247 Any whitespace before setting name or value (after ':') is ignored.
248 A # as first non-whitespace character discards the whole line.
249 Function sets pointers to null-terminated setting name and value.
250 Returns false if no valid config entry was found.
253 bool settings_parseline(char* line
, char** name
, char** value
)
257 line
= skip_whitespace(line
);
262 ptr
= strchr(line
, ':');
269 ptr
= skip_whitespace(ptr
);
274 static void system_flush(void)
276 scrobbler_shutdown();
279 call_storage_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
282 static void system_restore(void)
288 static bool clean_shutdown(void (*callback
)(void *), void *parameter
)
293 bookmark_autobookmark();
294 call_storage_idle_notifys(true);
300 scrobbler_poweroff();
302 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
303 if(!charger_inserted())
306 bool batt_safe
= battery_level_safe();
307 int audio_stat
= audio_status();
311 screens
[i
].clear_display();
318 if (!tagcache_prepare_shutdown())
321 splash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
325 if (battery_level() > 10)
326 splash(0, str(LANG_SHUTTINGDOWN
));
329 msg_id
= LANG_WARNING_BATTERY_LOW
;
330 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_LOW
),
331 str(LANG_SHUTTINGDOWN
));
336 msg_id
= LANG_WARNING_BATTERY_EMPTY
;
337 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_EMPTY
),
338 str(LANG_SHUTTINGDOWN
));
341 if (global_settings
.fade_on_stop
342 && (audio_stat
& AUDIO_STATUS_PLAY
))
347 if (batt_safe
) /* do not save on critical battery */
349 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
350 if (audio_stat
& AUDIO_STATUS_RECORD
)
352 rec_command(RECORDING_CMD_STOP
);
353 /* wait for stop to complete */
354 while (audio_status() & AUDIO_STATUS_RECORD
)
358 bookmark_autobookmark();
360 /* audio_stop_recording == audio_stop for HWCODEC */
363 if (callback
!= NULL
)
366 #if CONFIG_CODEC != SWCODEC
367 /* wait for audio_stop or audio_stop_recording to complete */
368 while (audio_status())
372 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
373 audio_close_recording();
376 if(global_settings
.talk_menu
)
378 bool enqueue
= false;
381 talk_id(msg_id
, enqueue
);
384 talk_id(LANG_SHUTTINGDOWN
, enqueue
);
385 #if CONFIG_CODEC == SWCODEC
391 #ifdef HAVE_EEPROM_SETTINGS
392 if (firmware_settings
.initialized
)
394 firmware_settings
.disk_clean
= true;
395 firmware_settings
.bl_version
= 0;
396 eeprom_settings_store();
411 bool list_stop_handler(void)
415 /* Stop the music if it is playing */
418 if (!global_settings
.party_mode
)
420 if (global_settings
.fade_on_stop
)
422 bookmark_autobookmark();
424 ret
= true; /* bookmarking can make a refresh necessary */
428 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
431 if (charger_inserted())
434 shutdown_screen(); /* won't return if shutdown actually happens */
436 ret
= true; /* screen is dirty, caller needs to refresh */
439 #ifndef HAVE_POWEROFF_WHILE_CHARGING
441 static long last_off
= 0;
443 if (TIME_BEFORE(current_tick
, last_off
+ HZ
/2))
445 if (charger_inserted())
448 ret
= true; /* screen is dirty, caller needs to refresh */
451 last_off
= current_tick
;
454 #endif /* CONFIG_CHARGING */
459 static bool waiting_to_resume_play
= false;
460 static long play_resume_tick
;
462 static void car_adapter_mode_processing(bool inserted
)
464 if (global_settings
.car_adapter_mode
)
469 * Just got plugged in, delay & resume if we were playing
471 if (audio_status() & AUDIO_STATUS_PAUSE
)
473 /* delay resume a bit while the engine is cranking */
474 play_resume_tick
= current_tick
+ HZ
*5;
475 waiting_to_resume_play
= true;
481 * Just got unplugged, pause if playing
483 if ((audio_status() & AUDIO_STATUS_PLAY
) &&
484 !(audio_status() & AUDIO_STATUS_PAUSE
))
486 if (global_settings
.fade_on_stop
)
491 waiting_to_resume_play
= false;
496 static void car_adapter_tick(void)
498 if (waiting_to_resume_play
)
500 if (TIME_AFTER(current_tick
, play_resume_tick
))
502 if (audio_status() & AUDIO_STATUS_PAUSE
)
504 queue_broadcast(SYS_CAR_ADAPTER_RESUME
, 0);
506 waiting_to_resume_play
= false;
511 void car_adapter_mode_init(void)
513 tick_add_task(car_adapter_tick
);
517 #ifdef HAVE_HEADPHONE_DETECTION
518 static void unplug_change(bool inserted
)
520 static bool headphone_caused_pause
= false;
522 if (global_settings
.unplug_mode
)
524 int audio_stat
= audio_status();
527 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
528 headphone_caused_pause
&&
529 global_settings
.unplug_mode
> 1 )
532 headphone_caused_pause
= false;
534 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
535 !(audio_stat
& AUDIO_STATUS_PAUSE
))
537 headphone_caused_pause
= true;
540 if (global_settings
.unplug_rw
)
542 if (audio_current_track()->elapsed
>
543 (unsigned long)(global_settings
.unplug_rw
*1000))
544 audio_ff_rewind(audio_current_track()->elapsed
-
545 (global_settings
.unplug_rw
*1000));
555 long default_event_handler_ex(long event
, void (*callback
)(void *), void *parameter
)
559 case SYS_BATTERY_UPDATE
:
560 if(global_settings
.talk_battery_level
)
562 talk_ids(true, VOICE_PAUSE
, VOICE_PAUSE
,
564 TALK_ID(battery_level(), UNIT_PERCENT
),
566 talk_force_enqueue_next();
569 case SYS_USB_CONNECTED
:
570 if (callback
!= NULL
)
572 #if (CONFIG_STORAGE & STORAGE_MMC)
573 if (!mmc_touched() ||
574 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED
))
579 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
580 check_bootfile(false); /* gets initial size */
583 gui_usb_screen_run();
585 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
586 check_bootfile(true);
591 return SYS_USB_CONNECTED
;
593 if (!clean_shutdown(callback
, parameter
))
597 case SYS_CHARGER_CONNECTED
:
598 car_adapter_mode_processing(true);
599 return SYS_CHARGER_CONNECTED
;
601 case SYS_CHARGER_DISCONNECTED
:
602 car_adapter_mode_processing(false);
603 return SYS_CHARGER_DISCONNECTED
;
605 case SYS_CAR_ADAPTER_RESUME
:
607 return SYS_CAR_ADAPTER_RESUME
;
609 #ifdef HAVE_HEADPHONE_DETECTION
610 case SYS_PHONE_PLUGGED
:
612 return SYS_PHONE_PLUGGED
;
614 case SYS_PHONE_UNPLUGGED
:
615 unplug_change(false);
616 return SYS_PHONE_UNPLUGGED
;
618 #ifdef IPOD_ACCESSORY_PROTOCOL
619 case SYS_IAP_PERIODIC
:
621 return SYS_IAP_PERIODIC
;
622 case SYS_IAP_HANDLEPKT
:
624 return SYS_IAP_HANDLEPKT
;
630 long default_event_handler(long event
)
632 return default_event_handler_ex(event
, NULL
, NULL
);
635 int show_logo( void )
637 #ifdef HAVE_LCD_BITMAP
641 snprintf(version
, sizeof(version
), "Ver. %s", appsversion
);
644 #ifdef SANSA_CLIP /* display the logo in the blue area of the screen */
645 lcd_setfont(FONT_SYSFIXED
);
646 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
647 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
648 0, (unsigned char *)version
);
649 lcd_bitmap(rockboxlogo
, 0, 16, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
651 lcd_bitmap(rockboxlogo
, 0, 10, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
652 lcd_setfont(FONT_SYSFIXED
);
653 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
654 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
655 LCD_HEIGHT
-font_h
, (unsigned char *)version
);
657 lcd_setfont(FONT_UI
);
660 char *rockbox
= " ROCKbox!";
663 lcd_double_height(true);
664 lcd_puts(0, 0, rockbox
);
665 lcd_puts_scroll(0, 1, appsversion
);
669 #ifdef HAVE_REMOTE_LCD
670 lcd_remote_clear_display();
671 lcd_remote_bitmap(remote_rockboxlogo
, 0, 10, BMPWIDTH_remote_rockboxlogo
,
672 BMPHEIGHT_remote_rockboxlogo
);
673 lcd_remote_setfont(FONT_SYSFIXED
);
674 lcd_remote_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
675 lcd_remote_putsxy((LCD_REMOTE_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
676 LCD_REMOTE_HEIGHT
-font_h
, (unsigned char *)version
);
677 lcd_remote_setfont(FONT_UI
);
685 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
687 memorize/compare details about the BOOTFILE
688 we don't use dircache because it may not be up to date after
689 USB disconnect (scanning in the background)
691 void check_bootfile(bool do_rolo
)
693 static unsigned short wrtdate
= 0;
694 static unsigned short wrttime
= 0;
696 struct dirent
* entry
= NULL
;
698 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
699 dir
= opendir(BOOTDIR
);
701 if(!dir
) return; /* do we want an error splash? */
703 /* loop all files in BOOTDIR */
704 while(0 != (entry
= readdir(dir
)))
706 if(!strcasecmp(entry
->d_name
, BOOTFILE
))
708 /* found the bootfile */
709 if(wrtdate
&& do_rolo
)
711 if((entry
->wrtdate
!= wrtdate
) ||
712 (entry
->wrttime
!= wrttime
))
714 static const char *lines
[] = { ID2P(LANG_BOOT_CHANGED
),
715 ID2P(LANG_REBOOT_NOW
) };
716 static const struct text_message message
={ lines
, 2 };
717 button_clear_queue(); /* Empty the keyboard buffer */
718 if(gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
)
719 rolo_load(BOOTDIR
"/" BOOTFILE
);
722 wrtdate
= entry
->wrtdate
;
723 wrttime
= entry
->wrttime
;
731 /* check range, set volume and save settings */
734 const int min_vol
= sound_min(SOUND_VOLUME
);
735 const int max_vol
= sound_max(SOUND_VOLUME
);
736 if (global_settings
.volume
< min_vol
)
737 global_settings
.volume
= min_vol
;
738 if (global_settings
.volume
> max_vol
)
739 global_settings
.volume
= max_vol
;
740 sound_set_volume(global_settings
.volume
);
741 global_status
.last_volume_change
= current_tick
;
745 char* strrsplt(char* str
, int c
)
747 char* s
= strrchr(str
, c
);
761 /* Test file existence, using dircache of possible */
762 bool file_exists(const char *file
)
766 if (!file
|| strlen(file
) <= 0)
770 if (dircache_is_enabled())
771 return (dircache_get_entry_ptr(file
) != NULL
);
774 fd
= open(file
, O_RDONLY
);
781 bool dir_exists(const char *path
)
783 DIR* d
= opendir(path
);
791 * removes the extension of filename (if it doesn't start with a .)
792 * puts the result in buffer
794 char *strip_extension(char* buffer
, int buffer_size
, const char *filename
)
796 char *dot
= strrchr(filename
, '.');
799 if (buffer_size
<= 0)
804 buffer_size
--; /* Make room for end nil */
806 if (dot
!= 0 && filename
[0] != '.')
808 len
= dot
- filename
;
809 len
= MIN(len
, buffer_size
);
816 strlcpy(buffer
, filename
, len
+ 1);
820 #endif /* !defined(__PCTOOL__) */
822 char* skip_whitespace(char* const str
)
832 /* Format time into buf.
834 * buf - buffer to format to.
835 * buf_size - size of buffer.
836 * t - time to format, in milliseconds.
838 void format_time(char* buf
, int buf_size
, long t
)
842 snprintf(buf
, buf_size
, "%d:%02d",
843 (int) (t
/ 60000), (int) (t
% 60000 / 1000));
847 snprintf(buf
, buf_size
, "%d:%02d:%02d",
848 (int) (t
/ 3600000), (int) (t
% 3600000 / 60000),
849 (int) (t
% 60000 / 1000));
854 /** Open a UTF-8 file and set file descriptor to first byte after BOM.
855 * If no BOM is present this behaves like open().
856 * If the file is opened for writing and O_TRUNC is set, write a BOM to
857 * the opened file and leave the file pointer set after the BOM.
859 #define BOM "\xef\xbb\xbf"
862 int open_utf8(const char* pathname
, int flags
)
865 unsigned char bom
[BOM_SIZE
];
867 fd
= open(pathname
, flags
);
871 if(flags
& (O_TRUNC
| O_WRONLY
))
873 write(fd
, BOM
, BOM_SIZE
);
877 read(fd
, bom
, BOM_SIZE
);
879 if(memcmp(bom
, BOM
, BOM_SIZE
))
880 lseek(fd
, 0, SEEK_SET
);
886 #ifdef HAVE_LCD_COLOR
888 * Helper function to convert a string of 6 hex digits to a native colour
891 static int hex2dec(int c
)
893 return (((c
) >= '0' && ((c
) <= '9')) ? (c
) - '0' :
894 (toupper(c
)) - 'A' + 10);
897 int hex_to_rgb(const char* hex
, int* color
)
899 int red
, green
, blue
;
902 while ((i
< 6) && (isxdigit(hex
[i
])))
908 red
= (hex2dec(hex
[0]) << 4) | hex2dec(hex
[1]);
909 green
= (hex2dec(hex
[2]) << 4) | hex2dec(hex
[3]);
910 blue
= (hex2dec(hex
[4]) << 4) | hex2dec(hex
[5]);
912 *color
= LCD_RGBPACK(red
,green
,blue
);
916 #endif /* HAVE_LCD_COLOR */
918 #ifdef HAVE_LCD_BITMAP
919 /* A simplified scanf - used (at time of writing) by wps parsing functions.
921 fmt - char array specifying the format of each list option. Valid values
923 s - string (sets pointer to string, without copying)
924 c - hex colour (RGB888 - e.g. ff00ff)
925 g - greyscale "colour" (0-3)
926 set_vals - if not NULL 1 is set in the bitplace if the item was read OK
928 first item is LSB, (max 32 items! )
929 Stops parseing if an item is invalid unless the item == '-'
930 sep - list separator (e.g. ',' or '|')
931 str - string to parse, must be terminated by 0 or sep
932 ... - pointers to store the parsed values
934 return value - pointer to char after parsed data, 0 if there was an error.
938 /* '0'-'3' are ASCII 0x30 to 0x33 */
939 #define is0123(x) (((x) & 0xfc) == 0x30)
941 const char* parse_list(const char *fmt
, uint32_t *set_vals
,
942 const char sep
, const char* str
, ...)
945 const char* p
= str
, *f
= fmt
;
948 bool set
, is_negative
;
956 /* Check for separator, if we're not at the start */
966 case 's': /* string - return a pointer to it (not a copy) */
967 s
= va_arg(ap
, const char **);
970 while (*p
&& *p
!= sep
)
972 set
= (s
[0][0]!='-') && (s
[0][1]!=sep
) ;
977 d
= va_arg(ap
, int*);
978 if (*p
== '-' && isdigit(*(p
+1)))
985 if (!set_vals
|| *p
!= '-')
987 while (*p
&& *p
!= sep
)
994 *d
= (*d
* 10) + (*p
++ - '0');
1002 #ifdef HAVE_LCD_COLOR
1003 case 'c': /* colour (rrggbb - e.g. f3c1a8) */
1004 d
= va_arg(ap
, int*);
1006 if (hex_to_rgb(p
, d
) < 0)
1008 if (!set_vals
|| *p
!= '-')
1010 while (*p
&& *p
!= sep
)
1022 #if LCD_DEPTH == 2 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 2)
1023 case 'g': /* greyscale colour (0-3) */
1024 d
= va_arg(ap
, int*);
1031 else if (!set_vals
|| *p
!= '-')
1035 while (*p
&& *p
!= sep
)
1042 default: /* Unknown format type */
1046 if (set_vals
&& set
)
1047 *set_vals
|= BIT_N(i
);
1059 /* only used in USB HID and set_time screen */
1060 #if defined(USB_ENABLE_HID) || (CONFIG_RTC != 0)
1061 int clamp_value_wrap(int value
, int max
, int min
)