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 ****************************************************************************/
23 #include "string-extra.h"
36 #include "lcd-remote.h"
39 #include "timefuncs.h"
41 #include "usb_screen.h"
44 #include "mp3_playback.h"
47 #include "ata_idle_notify.h"
50 #include "powermgmt.h"
51 #include "backlight.h"
56 #include "scrobbler.h"
62 #ifdef IPOD_ACCESSORY_PROTOCOL
66 #if (CONFIG_STORAGE & STORAGE_MMC)
70 #include "eeprom_settings.h"
71 #if defined(HAVE_RECORDING) && !defined(__PCTOOL__)
72 #include "recording.h"
74 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
77 #endif /* End HAVE_LCD_BITMAP */
83 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF) \
84 || defined(HAVE_HOTSWAP_STORAGE_AS_MAIN)
89 /* units used with output_dyn_value */
90 const unsigned char * const byte_units
[] =
98 const unsigned char * const * const kbyte_units
= &byte_units
[1];
100 /* Format a large-range value for output, using the appropriate unit so that
101 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
102 * units) if possible, and 3 significant digits are shown. If a buffer is
103 * given, the result is snprintf()'d into that buffer, otherwise the result is
105 char *output_dyn_value(char *buf
, int buf_size
, int value
,
106 const unsigned char * const *units
, bool bin_scale
)
108 int scale
= bin_scale
? 1024 : 1000;
113 while (value
>= scale
)
115 fraction
= value
% scale
;
120 fraction
= fraction
* 1000 / 1024;
122 if (value
>= 100 || !unit_no
)
124 else if (value
>= 10)
125 snprintf(tbuf
, sizeof(tbuf
), "%01d", fraction
/ 100);
127 snprintf(tbuf
, sizeof(tbuf
), "%02d", fraction
/ 10);
132 snprintf(buf
, buf_size
, "%d%s%s%s", value
, str(LANG_POINT
),
133 tbuf
, P2STR(units
[unit_no
]));
135 snprintf(buf
, buf_size
, "%d%s", value
, P2STR(units
[unit_no
]));
139 talk_fractional(tbuf
, value
, P2ID(units
[unit_no
]));
144 /* Ask the user if they really want to erase the current dynamic playlist
145 * returns true if the playlist should be replaced */
146 bool warn_on_pl_erase(void)
148 if (global_settings
.warnon_erase_dynplaylist
&&
149 !global_settings
.party_mode
&&
150 playlist_modified(NULL
))
152 static const char *lines
[] =
153 {ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
154 static const struct text_message message
={lines
, 1};
156 return (gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
);
162 /* Read (up to) a line of text from fd into buffer and return number of bytes
163 * read (which may be larger than the number of bytes stored in buffer). If
164 * an error occurs, -1 is returned (and buffer contains whatever could be
165 * read). A line is terminated by a LF char. Neither LF nor CR chars are
168 int read_line(int fd
, char* buffer
, int buffer_size
)
175 while (count
< buffer_size
)
179 if (1 != read(fd
, &c
, 1))
193 buffer
[MIN(count
, buffer_size
- 1)] = 0;
195 return errno
? -1 : num_read
;
198 /* Performance optimized version of the previous function. */
199 int fast_readline(int fd
, char *buf
, int buf_size
, void *parameters
,
200 int (*callback
)(int n
, const char *buf
, void *parameters
))
210 rc
= read(fd
, &buf
[pos
], buf_size
- pos
- 1);
214 if ( (p
= strchr(buf
, '\r')) != NULL
)
222 if ( (p
= strchr(p
, '\n')) != NULL
)
228 rc
= callback(count
, buf
, parameters
);
235 pos
= buf_size
- ((long)next
- (long)buf
) - 1;
236 memmove(buf
, next
, pos
);
245 /* parse a line from a configuration file. the line format is:
249 Any whitespace before setting name or value (after ':') is ignored.
250 A # as first non-whitespace character discards the whole line.
251 Function sets pointers to null-terminated setting name and value.
252 Returns false if no valid config entry was found.
255 bool settings_parseline(char* line
, char** name
, char** value
)
259 line
= skip_whitespace(line
);
264 ptr
= strchr(line
, ':');
271 ptr
= skip_whitespace(ptr
);
276 static void system_flush(void)
278 scrobbler_shutdown();
281 call_storage_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
284 static void system_restore(void)
290 static bool clean_shutdown(void (*callback
)(void *), void *parameter
)
295 bookmark_autobookmark(false);
296 call_storage_idle_notifys(true);
302 scrobbler_poweroff();
304 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
305 if(!charger_inserted())
308 bool batt_safe
= battery_level_safe();
309 int audio_stat
= audio_status();
313 screens
[i
].clear_display();
320 if (!tagcache_prepare_shutdown())
323 splash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
327 if (battery_level() > 10)
328 splash(0, str(LANG_SHUTTINGDOWN
));
331 msg_id
= LANG_WARNING_BATTERY_LOW
;
332 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_LOW
),
333 str(LANG_SHUTTINGDOWN
));
338 msg_id
= LANG_WARNING_BATTERY_EMPTY
;
339 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_EMPTY
),
340 str(LANG_SHUTTINGDOWN
));
343 if (global_settings
.fade_on_stop
344 && (audio_stat
& AUDIO_STATUS_PLAY
))
349 if (batt_safe
) /* do not save on critical battery */
351 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
352 if (audio_stat
& AUDIO_STATUS_RECORD
)
354 rec_command(RECORDING_CMD_STOP
);
355 /* wait for stop to complete */
356 while (audio_status() & AUDIO_STATUS_RECORD
)
360 bookmark_autobookmark(false);
362 /* audio_stop_recording == audio_stop for HWCODEC */
365 if (callback
!= NULL
)
368 #if CONFIG_CODEC != SWCODEC
369 /* wait for audio_stop or audio_stop_recording to complete */
370 while (audio_status())
374 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
375 audio_close_recording();
378 if(global_settings
.talk_menu
)
380 bool enqueue
= false;
383 talk_id(msg_id
, enqueue
);
386 talk_id(LANG_SHUTTINGDOWN
, enqueue
);
387 #if CONFIG_CODEC == SWCODEC
393 #ifdef HAVE_EEPROM_SETTINGS
394 if (firmware_settings
.initialized
)
396 firmware_settings
.disk_clean
= true;
397 firmware_settings
.bl_version
= 0;
398 eeprom_settings_store();
413 bool list_stop_handler(void)
417 /* Stop the music if it is playing */
420 if (!global_settings
.party_mode
)
422 if (global_settings
.fade_on_stop
)
424 bookmark_autobookmark(true);
426 ret
= true; /* bookmarking can make a refresh necessary */
430 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
433 if (charger_inserted())
436 shutdown_screen(); /* won't return if shutdown actually happens */
438 ret
= true; /* screen is dirty, caller needs to refresh */
441 #ifndef HAVE_POWEROFF_WHILE_CHARGING
443 static long last_off
= 0;
445 if (TIME_BEFORE(current_tick
, last_off
+ HZ
/2))
447 if (charger_inserted())
450 ret
= true; /* screen is dirty, caller needs to refresh */
453 last_off
= current_tick
;
456 #endif /* CONFIG_CHARGING */
461 static bool waiting_to_resume_play
= false;
462 static long play_resume_tick
;
464 static void car_adapter_mode_processing(bool inserted
)
466 if (global_settings
.car_adapter_mode
)
471 * Just got plugged in, delay & resume if we were playing
473 if (audio_status() & AUDIO_STATUS_PAUSE
)
475 /* delay resume a bit while the engine is cranking */
476 play_resume_tick
= current_tick
+ HZ
*5;
477 waiting_to_resume_play
= true;
483 * Just got unplugged, pause if playing
485 if ((audio_status() & AUDIO_STATUS_PLAY
) &&
486 !(audio_status() & AUDIO_STATUS_PAUSE
))
488 if (global_settings
.fade_on_stop
)
493 waiting_to_resume_play
= false;
498 static void car_adapter_tick(void)
500 if (waiting_to_resume_play
)
502 if (TIME_AFTER(current_tick
, play_resume_tick
))
504 if (audio_status() & AUDIO_STATUS_PAUSE
)
506 queue_broadcast(SYS_CAR_ADAPTER_RESUME
, 0);
508 waiting_to_resume_play
= false;
513 void car_adapter_mode_init(void)
515 tick_add_task(car_adapter_tick
);
519 #ifdef HAVE_HEADPHONE_DETECTION
520 static void unplug_change(bool inserted
)
522 static bool headphone_caused_pause
= false;
524 if (global_settings
.unplug_mode
)
526 int audio_stat
= audio_status();
529 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
530 headphone_caused_pause
&&
531 global_settings
.unplug_mode
> 1 )
534 headphone_caused_pause
= false;
536 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
537 !(audio_stat
& AUDIO_STATUS_PAUSE
))
539 headphone_caused_pause
= true;
542 if (global_settings
.unplug_rw
)
544 if (audio_current_track()->elapsed
>
545 (unsigned long)(global_settings
.unplug_rw
*1000))
546 audio_ff_rewind(audio_current_track()->elapsed
-
547 (global_settings
.unplug_rw
*1000));
557 long default_event_handler_ex(long event
, void (*callback
)(void *), void *parameter
)
561 case SYS_BATTERY_UPDATE
:
562 if(global_settings
.talk_battery_level
)
564 talk_ids(true, VOICE_PAUSE
, VOICE_PAUSE
,
566 TALK_ID(battery_level(), UNIT_PERCENT
),
568 talk_force_enqueue_next();
571 case SYS_USB_CONNECTED
:
572 if (callback
!= NULL
)
574 #if (CONFIG_STORAGE & STORAGE_MMC)
575 if (!mmc_touched() ||
576 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED
))
581 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
582 check_bootfile(false); /* gets initial size */
585 gui_usb_screen_run();
587 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
588 check_bootfile(true);
593 return SYS_USB_CONNECTED
;
596 if (!clean_shutdown(callback
, parameter
))
600 case SYS_CHARGER_CONNECTED
:
601 car_adapter_mode_processing(true);
602 return SYS_CHARGER_CONNECTED
;
604 case SYS_CHARGER_DISCONNECTED
:
605 car_adapter_mode_processing(false);
606 /*reset rockbox battery runtime*/
607 global_status
.runtime
= 0;
608 return SYS_CHARGER_DISCONNECTED
;
610 case SYS_CAR_ADAPTER_RESUME
:
612 return SYS_CAR_ADAPTER_RESUME
;
614 #ifdef HAVE_HOTSWAP_STORAGE_AS_MAIN
617 /* simple sanity: assume rockbox is on the first hotswappable
618 * driver, abort out if that one isn't inserted */
620 for (i
= 0; i
< NUM_DRIVES
; i
++)
622 if (storage_removable(i
) && !storage_present(i
))
623 return SYS_FS_CHANGED
;
626 check_bootfile(true); /* state gotten in main.c:init() */
629 return SYS_FS_CHANGED
;
631 #ifdef HAVE_HEADPHONE_DETECTION
632 case SYS_PHONE_PLUGGED
:
634 return SYS_PHONE_PLUGGED
;
636 case SYS_PHONE_UNPLUGGED
:
637 unplug_change(false);
638 return SYS_PHONE_UNPLUGGED
;
640 #ifdef IPOD_ACCESSORY_PROTOCOL
641 case SYS_IAP_PERIODIC
:
643 return SYS_IAP_PERIODIC
;
644 case SYS_IAP_HANDLEPKT
:
646 return SYS_IAP_HANDLEPKT
;
652 long default_event_handler(long event
)
654 return default_event_handler_ex(event
, NULL
, NULL
);
657 int show_logo( void )
659 #ifdef HAVE_LCD_BITMAP
663 snprintf(version
, sizeof(version
), "Ver. %s", appsversion
);
666 #if defined(SANSA_CLIP) || defined(SANSA_CLIPV2) || defined(SANSA_CLIPPLUS)
667 /* display the logo in the blue area of the screen */
668 lcd_setfont(FONT_SYSFIXED
);
669 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
670 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
671 0, (unsigned char *)version
);
672 lcd_bitmap(rockboxlogo
, 0, 16, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
674 lcd_bitmap(rockboxlogo
, 0, 10, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
675 lcd_setfont(FONT_SYSFIXED
);
676 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
677 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
678 LCD_HEIGHT
-font_h
, (unsigned char *)version
);
680 lcd_setfont(FONT_UI
);
683 char *rockbox
= " ROCKbox!";
686 lcd_double_height(true);
687 lcd_puts(0, 0, rockbox
);
688 lcd_puts_scroll(0, 1, appsversion
);
692 #ifdef HAVE_REMOTE_LCD
693 lcd_remote_clear_display();
694 lcd_remote_bitmap(remote_rockboxlogo
, 0, 10, BMPWIDTH_remote_rockboxlogo
,
695 BMPHEIGHT_remote_rockboxlogo
);
696 lcd_remote_setfont(FONT_SYSFIXED
);
697 lcd_remote_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
698 lcd_remote_putsxy((LCD_REMOTE_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
699 LCD_REMOTE_HEIGHT
-font_h
, (unsigned char *)version
);
700 lcd_remote_setfont(FONT_UI
);
708 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF) || defined(HAVE_HOTSWAP_STORAGE_AS_MAIN)
710 memorize/compare details about the BOOTFILE
711 we don't use dircache because it may not be up to date after
712 USB disconnect (scanning in the background)
714 void check_bootfile(bool do_rolo
)
716 static unsigned short wrtdate
= 0;
717 static unsigned short wrttime
= 0;
719 struct dirent
* entry
= NULL
;
721 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
722 dir
= opendir(BOOTDIR
);
724 if(!dir
) return; /* do we want an error splash? */
726 /* loop all files in BOOTDIR */
727 while(0 != (entry
= readdir(dir
)))
729 if(!strcasecmp(entry
->d_name
, BOOTFILE
))
731 /* found the bootfile */
732 if(wrtdate
&& do_rolo
)
734 if((entry
->wrtdate
!= wrtdate
) ||
735 (entry
->wrttime
!= wrttime
))
737 static const char *lines
[] = { ID2P(LANG_BOOT_CHANGED
),
738 ID2P(LANG_REBOOT_NOW
) };
739 static const struct text_message message
={ lines
, 2 };
740 button_clear_queue(); /* Empty the keyboard buffer */
741 if(gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
)
742 rolo_load(BOOTDIR
"/" BOOTFILE
);
745 wrtdate
= entry
->wrtdate
;
746 wrttime
= entry
->wrttime
;
754 /* check range, set volume and save settings */
757 const int min_vol
= sound_min(SOUND_VOLUME
);
758 const int max_vol
= sound_max(SOUND_VOLUME
);
759 if (global_settings
.volume
< min_vol
)
760 global_settings
.volume
= min_vol
;
761 if (global_settings
.volume
> max_vol
)
762 global_settings
.volume
= max_vol
;
763 sound_set_volume(global_settings
.volume
);
764 global_status
.last_volume_change
= current_tick
;
768 char* strrsplt(char* str
, int c
)
770 char* s
= strrchr(str
, c
);
784 /* Test file existence, using dircache of possible */
785 bool file_exists(const char *file
)
789 if (!file
|| strlen(file
) <= 0)
793 if (dircache_is_enabled())
794 return (dircache_get_entry_ptr(file
) != NULL
);
797 fd
= open(file
, O_RDONLY
);
804 bool dir_exists(const char *path
)
806 DIR* d
= opendir(path
);
814 * removes the extension of filename (if it doesn't start with a .)
815 * puts the result in buffer
817 char *strip_extension(char* buffer
, int buffer_size
, const char *filename
)
819 char *dot
= strrchr(filename
, '.');
822 if (buffer_size
<= 0)
827 buffer_size
--; /* Make room for end nil */
829 if (dot
!= 0 && filename
[0] != '.')
831 len
= dot
- filename
;
832 len
= MIN(len
, buffer_size
);
839 strlcpy(buffer
, filename
, len
+ 1);
843 #endif /* !defined(__PCTOOL__) */
845 char* skip_whitespace(char* const str
)
855 /* Format time into buf.
857 * buf - buffer to format to.
858 * buf_size - size of buffer.
859 * t - time to format, in milliseconds.
861 void format_time(char* buf
, int buf_size
, long t
)
865 snprintf(buf
, buf_size
, "%d:%02d",
866 (int) (t
/ 60000), (int) (t
% 60000 / 1000));
870 snprintf(buf
, buf_size
, "%d:%02d:%02d",
871 (int) (t
/ 3600000), (int) (t
% 3600000 / 60000),
872 (int) (t
% 60000 / 1000));
877 /** Open a UTF-8 file and set file descriptor to first byte after BOM.
878 * If no BOM is present this behaves like open().
879 * If the file is opened for writing and O_TRUNC is set, write a BOM to
880 * the opened file and leave the file pointer set after the BOM.
882 #define BOM "\xef\xbb\xbf"
885 int open_utf8(const char* pathname
, int flags
)
888 unsigned char bom
[BOM_SIZE
];
890 fd
= open(pathname
, flags
);
894 if(flags
& (O_TRUNC
| O_WRONLY
))
896 write(fd
, BOM
, BOM_SIZE
);
900 read(fd
, bom
, BOM_SIZE
);
902 if(memcmp(bom
, BOM
, BOM_SIZE
))
903 lseek(fd
, 0, SEEK_SET
);
909 #ifdef HAVE_LCD_COLOR
911 * Helper function to convert a string of 6 hex digits to a native colour
914 static int hex2dec(int c
)
916 return (((c
) >= '0' && ((c
) <= '9')) ? (c
) - '0' :
917 (toupper(c
)) - 'A' + 10);
920 int hex_to_rgb(const char* hex
, int* color
)
922 int red
, green
, blue
;
925 while ((i
< 6) && (isxdigit(hex
[i
])))
931 red
= (hex2dec(hex
[0]) << 4) | hex2dec(hex
[1]);
932 green
= (hex2dec(hex
[2]) << 4) | hex2dec(hex
[3]);
933 blue
= (hex2dec(hex
[4]) << 4) | hex2dec(hex
[5]);
935 *color
= LCD_RGBPACK(red
,green
,blue
);
939 #endif /* HAVE_LCD_COLOR */
941 #ifdef HAVE_LCD_BITMAP
942 /* A simplified scanf - used (at time of writing) by wps parsing functions.
944 fmt - char array specifying the format of each list option. Valid values
946 s - string (sets pointer to string, without copying)
947 c - hex colour (RGB888 - e.g. ff00ff)
948 g - greyscale "colour" (0-3)
949 set_vals - if not NULL 1 is set in the bitplace if the item was read OK
951 first item is LSB, (max 32 items! )
952 Stops parseing if an item is invalid unless the item == '-'
953 sep - list separator (e.g. ',' or '|')
954 str - string to parse, must be terminated by 0 or sep
955 ... - pointers to store the parsed values
957 return value - pointer to char after parsed data, 0 if there was an error.
961 /* '0'-'3' are ASCII 0x30 to 0x33 */
962 #define is0123(x) (((x) & 0xfc) == 0x30)
964 const char* parse_list(const char *fmt
, uint32_t *set_vals
,
965 const char sep
, const char* str
, ...)
968 const char* p
= str
, *f
= fmt
;
971 bool set
, is_negative
;
979 /* Check for separator, if we're not at the start */
989 case 's': /* string - return a pointer to it (not a copy) */
990 s
= va_arg(ap
, const char **);
993 while (*p
&& *p
!= sep
)
995 set
= (s
[0][0]!='-') && (s
[0][1]!=sep
) ;
1000 d
= va_arg(ap
, int*);
1001 if (*p
== '-' && isdigit(*(p
+1)))
1008 if (!set_vals
|| *p
!= '-')
1010 while (*p
&& *p
!= sep
)
1017 *d
= (*d
* 10) + (*p
++ - '0');
1025 #ifdef HAVE_LCD_COLOR
1026 case 'c': /* colour (rrggbb - e.g. f3c1a8) */
1027 d
= va_arg(ap
, int*);
1029 if (hex_to_rgb(p
, d
) < 0)
1031 if (!set_vals
|| *p
!= '-')
1033 while (*p
&& *p
!= sep
)
1045 #if LCD_DEPTH == 2 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 2)
1046 case 'g': /* greyscale colour (0-3) */
1047 d
= va_arg(ap
, int*);
1054 else if (!set_vals
|| *p
!= '-')
1058 while (*p
&& *p
!= sep
)
1065 default: /* Unknown format type */
1069 if (set_vals
&& set
)
1070 *set_vals
|= BIT_N(i
);
1082 /* only used in USB HID and set_time screen */
1083 #if defined(USB_ENABLE_HID) || (CONFIG_RTC != 0)
1084 int clamp_value_wrap(int value
, int max
, int min
)