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) \
83 || defined(HAVE_HOTSWAP_STORAGE_AS_MAIN)
88 /* units used with output_dyn_value */
89 const unsigned char * const byte_units
[] =
97 const unsigned char * const * const kbyte_units
= &byte_units
[1];
99 /* Format a large-range value for output, using the appropriate unit so that
100 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
101 * units) if possible, and 3 significant digits are shown. If a buffer is
102 * given, the result is snprintf()'d into that buffer, otherwise the result is
104 char *output_dyn_value(char *buf
, int buf_size
, int value
,
105 const unsigned char * const *units
, bool bin_scale
)
107 int scale
= bin_scale
? 1024 : 1000;
112 while (value
>= scale
)
114 fraction
= value
% scale
;
119 fraction
= fraction
* 1000 / 1024;
121 if (value
>= 100 || !unit_no
)
123 else if (value
>= 10)
124 snprintf(tbuf
, sizeof(tbuf
), "%01d", fraction
/ 100);
126 snprintf(tbuf
, sizeof(tbuf
), "%02d", fraction
/ 10);
131 snprintf(buf
, buf_size
, "%d%s%s%s", value
, str(LANG_POINT
),
132 tbuf
, P2STR(units
[unit_no
]));
134 snprintf(buf
, buf_size
, "%d%s", value
, P2STR(units
[unit_no
]));
138 talk_fractional(tbuf
, value
, P2ID(units
[unit_no
]));
143 /* Ask the user if they really want to erase the current dynamic playlist
144 * returns true if the playlist should be replaced */
145 bool warn_on_pl_erase(void)
147 if (global_settings
.warnon_erase_dynplaylist
&&
148 !global_settings
.party_mode
&&
149 playlist_modified(NULL
))
151 static const char *lines
[] =
152 {ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
153 static const struct text_message message
={lines
, 1};
155 return (gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
);
161 /* Read (up to) a line of text from fd into buffer and return number of bytes
162 * read (which may be larger than the number of bytes stored in buffer). If
163 * an error occurs, -1 is returned (and buffer contains whatever could be
164 * read). A line is terminated by a LF char. Neither LF nor CR chars are
167 int read_line(int fd
, char* buffer
, int buffer_size
)
174 while (count
< buffer_size
)
178 if (1 != read(fd
, &c
, 1))
192 buffer
[MIN(count
, buffer_size
- 1)] = 0;
194 return errno
? -1 : num_read
;
197 /* Performance optimized version of the previous function. */
198 int fast_readline(int fd
, char *buf
, int buf_size
, void *parameters
,
199 int (*callback
)(int n
, const char *buf
, void *parameters
))
209 rc
= read(fd
, &buf
[pos
], buf_size
- pos
- 1);
213 if ( (p
= strchr(buf
, '\r')) != NULL
)
221 if ( (p
= strchr(p
, '\n')) != NULL
)
227 rc
= callback(count
, buf
, parameters
);
234 pos
= buf_size
- ((long)next
- (long)buf
) - 1;
235 memmove(buf
, next
, pos
);
244 /* parse a line from a configuration file. the line format is:
248 Any whitespace before setting name or value (after ':') is ignored.
249 A # as first non-whitespace character discards the whole line.
250 Function sets pointers to null-terminated setting name and value.
251 Returns false if no valid config entry was found.
254 bool settings_parseline(char* line
, char** name
, char** value
)
258 line
= skip_whitespace(line
);
263 ptr
= strchr(line
, ':');
270 ptr
= skip_whitespace(ptr
);
275 static void system_flush(void)
277 scrobbler_shutdown();
280 call_storage_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
283 static void system_restore(void)
289 static bool clean_shutdown(void (*callback
)(void *), void *parameter
)
294 bookmark_autobookmark();
295 call_storage_idle_notifys(true);
301 scrobbler_poweroff();
303 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
304 if(!charger_inserted())
307 bool batt_safe
= battery_level_safe();
308 int audio_stat
= audio_status();
312 screens
[i
].clear_display();
319 if (!tagcache_prepare_shutdown())
322 splash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
326 if (battery_level() > 10)
327 splash(0, str(LANG_SHUTTINGDOWN
));
330 msg_id
= LANG_WARNING_BATTERY_LOW
;
331 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_LOW
),
332 str(LANG_SHUTTINGDOWN
));
337 msg_id
= LANG_WARNING_BATTERY_EMPTY
;
338 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_EMPTY
),
339 str(LANG_SHUTTINGDOWN
));
342 if (global_settings
.fade_on_stop
343 && (audio_stat
& AUDIO_STATUS_PLAY
))
348 if (batt_safe
) /* do not save on critical battery */
350 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
351 if (audio_stat
& AUDIO_STATUS_RECORD
)
353 rec_command(RECORDING_CMD_STOP
);
354 /* wait for stop to complete */
355 while (audio_status() & AUDIO_STATUS_RECORD
)
359 bookmark_autobookmark();
361 /* audio_stop_recording == audio_stop for HWCODEC */
364 if (callback
!= NULL
)
367 #if CONFIG_CODEC != SWCODEC
368 /* wait for audio_stop or audio_stop_recording to complete */
369 while (audio_status())
373 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
374 audio_close_recording();
377 if(global_settings
.talk_menu
)
379 bool enqueue
= false;
382 talk_id(msg_id
, enqueue
);
385 talk_id(LANG_SHUTTINGDOWN
, enqueue
);
386 #if CONFIG_CODEC == SWCODEC
392 #ifdef HAVE_EEPROM_SETTINGS
393 if (firmware_settings
.initialized
)
395 firmware_settings
.disk_clean
= true;
396 firmware_settings
.bl_version
= 0;
397 eeprom_settings_store();
412 bool list_stop_handler(void)
416 /* Stop the music if it is playing */
419 if (!global_settings
.party_mode
)
421 if (global_settings
.fade_on_stop
)
423 bookmark_autobookmark();
425 ret
= true; /* bookmarking can make a refresh necessary */
429 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
432 if (charger_inserted())
435 shutdown_screen(); /* won't return if shutdown actually happens */
437 ret
= true; /* screen is dirty, caller needs to refresh */
440 #ifndef HAVE_POWEROFF_WHILE_CHARGING
442 static long last_off
= 0;
444 if (TIME_BEFORE(current_tick
, last_off
+ HZ
/2))
446 if (charger_inserted())
449 ret
= true; /* screen is dirty, caller needs to refresh */
452 last_off
= current_tick
;
455 #endif /* CONFIG_CHARGING */
460 static bool waiting_to_resume_play
= false;
461 static long play_resume_tick
;
463 static void car_adapter_mode_processing(bool inserted
)
465 if (global_settings
.car_adapter_mode
)
470 * Just got plugged in, delay & resume if we were playing
472 if (audio_status() & AUDIO_STATUS_PAUSE
)
474 /* delay resume a bit while the engine is cranking */
475 play_resume_tick
= current_tick
+ HZ
*5;
476 waiting_to_resume_play
= true;
482 * Just got unplugged, pause if playing
484 if ((audio_status() & AUDIO_STATUS_PLAY
) &&
485 !(audio_status() & AUDIO_STATUS_PAUSE
))
487 if (global_settings
.fade_on_stop
)
492 waiting_to_resume_play
= false;
497 static void car_adapter_tick(void)
499 if (waiting_to_resume_play
)
501 if (TIME_AFTER(current_tick
, play_resume_tick
))
503 if (audio_status() & AUDIO_STATUS_PAUSE
)
505 queue_broadcast(SYS_CAR_ADAPTER_RESUME
, 0);
507 waiting_to_resume_play
= false;
512 void car_adapter_mode_init(void)
514 tick_add_task(car_adapter_tick
);
518 #ifdef HAVE_HEADPHONE_DETECTION
519 static void unplug_change(bool inserted
)
521 static bool headphone_caused_pause
= false;
523 if (global_settings
.unplug_mode
)
525 int audio_stat
= audio_status();
528 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
529 headphone_caused_pause
&&
530 global_settings
.unplug_mode
> 1 )
533 headphone_caused_pause
= false;
535 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
536 !(audio_stat
& AUDIO_STATUS_PAUSE
))
538 headphone_caused_pause
= true;
541 if (global_settings
.unplug_rw
)
543 if (audio_current_track()->elapsed
>
544 (unsigned long)(global_settings
.unplug_rw
*1000))
545 audio_ff_rewind(audio_current_track()->elapsed
-
546 (global_settings
.unplug_rw
*1000));
556 long default_event_handler_ex(long event
, void (*callback
)(void *), void *parameter
)
560 case SYS_BATTERY_UPDATE
:
561 if(global_settings
.talk_battery_level
)
563 talk_ids(true, VOICE_PAUSE
, VOICE_PAUSE
,
565 TALK_ID(battery_level(), UNIT_PERCENT
),
567 talk_force_enqueue_next();
570 case SYS_USB_CONNECTED
:
571 if (callback
!= NULL
)
573 #if (CONFIG_STORAGE & STORAGE_MMC)
574 if (!mmc_touched() ||
575 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED
))
580 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
581 check_bootfile(false); /* gets initial size */
584 gui_usb_screen_run();
586 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
587 check_bootfile(true);
592 return SYS_USB_CONNECTED
;
594 if (!clean_shutdown(callback
, parameter
))
598 case SYS_CHARGER_CONNECTED
:
599 car_adapter_mode_processing(true);
600 return SYS_CHARGER_CONNECTED
;
602 case SYS_CHARGER_DISCONNECTED
:
603 car_adapter_mode_processing(false);
604 return SYS_CHARGER_DISCONNECTED
;
606 case SYS_CAR_ADAPTER_RESUME
:
608 return SYS_CAR_ADAPTER_RESUME
;
610 #ifdef HAVE_HOTSWAP_STORAGE_AS_MAIN
613 /* simple sanity: assume rockbox is on the first hotswappable
614 * driver, abort out if that one isn't inserted */
616 for (i
= 0; i
< NUM_DRIVES
; i
++)
618 if (storage_removable(i
) && !storage_present(i
))
619 return SYS_FS_CHANGED
;
622 check_bootfile(true); /* state gotten in main.c:init() */
625 return SYS_FS_CHANGED
;
627 #ifdef HAVE_HEADPHONE_DETECTION
628 case SYS_PHONE_PLUGGED
:
630 return SYS_PHONE_PLUGGED
;
632 case SYS_PHONE_UNPLUGGED
:
633 unplug_change(false);
634 return SYS_PHONE_UNPLUGGED
;
636 #ifdef IPOD_ACCESSORY_PROTOCOL
637 case SYS_IAP_PERIODIC
:
639 return SYS_IAP_PERIODIC
;
640 case SYS_IAP_HANDLEPKT
:
642 return SYS_IAP_HANDLEPKT
;
648 long default_event_handler(long event
)
650 return default_event_handler_ex(event
, NULL
, NULL
);
653 int show_logo( void )
655 #ifdef HAVE_LCD_BITMAP
659 snprintf(version
, sizeof(version
), "Ver. %s", appsversion
);
662 #ifdef SANSA_CLIP /* display the logo in the blue area of the screen */
663 lcd_setfont(FONT_SYSFIXED
);
664 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
665 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
666 0, (unsigned char *)version
);
667 lcd_bitmap(rockboxlogo
, 0, 16, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
669 lcd_bitmap(rockboxlogo
, 0, 10, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
670 lcd_setfont(FONT_SYSFIXED
);
671 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
672 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
673 LCD_HEIGHT
-font_h
, (unsigned char *)version
);
675 lcd_setfont(FONT_UI
);
678 char *rockbox
= " ROCKbox!";
681 lcd_double_height(true);
682 lcd_puts(0, 0, rockbox
);
683 lcd_puts_scroll(0, 1, appsversion
);
687 #ifdef HAVE_REMOTE_LCD
688 lcd_remote_clear_display();
689 lcd_remote_bitmap(remote_rockboxlogo
, 0, 10, BMPWIDTH_remote_rockboxlogo
,
690 BMPHEIGHT_remote_rockboxlogo
);
691 lcd_remote_setfont(FONT_SYSFIXED
);
692 lcd_remote_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
693 lcd_remote_putsxy((LCD_REMOTE_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
694 LCD_REMOTE_HEIGHT
-font_h
, (unsigned char *)version
);
695 lcd_remote_setfont(FONT_UI
);
703 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF) || defined(HAVE_HOTSWAP_STORAGE_AS_MAIN)
705 memorize/compare details about the BOOTFILE
706 we don't use dircache because it may not be up to date after
707 USB disconnect (scanning in the background)
709 void check_bootfile(bool do_rolo
)
711 static unsigned short wrtdate
= 0;
712 static unsigned short wrttime
= 0;
714 struct dirent
* entry
= NULL
;
716 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
717 dir
= opendir(BOOTDIR
);
719 if(!dir
) return; /* do we want an error splash? */
721 /* loop all files in BOOTDIR */
722 while(0 != (entry
= readdir(dir
)))
724 if(!strcasecmp(entry
->d_name
, BOOTFILE
))
726 /* found the bootfile */
727 if(wrtdate
&& do_rolo
)
729 if((entry
->wrtdate
!= wrtdate
) ||
730 (entry
->wrttime
!= wrttime
))
732 static const char *lines
[] = { ID2P(LANG_BOOT_CHANGED
),
733 ID2P(LANG_REBOOT_NOW
) };
734 static const struct text_message message
={ lines
, 2 };
735 button_clear_queue(); /* Empty the keyboard buffer */
736 if(gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
)
737 rolo_load(BOOTDIR
"/" BOOTFILE
);
740 wrtdate
= entry
->wrtdate
;
741 wrttime
= entry
->wrttime
;
749 /* check range, set volume and save settings */
752 const int min_vol
= sound_min(SOUND_VOLUME
);
753 const int max_vol
= sound_max(SOUND_VOLUME
);
754 if (global_settings
.volume
< min_vol
)
755 global_settings
.volume
= min_vol
;
756 if (global_settings
.volume
> max_vol
)
757 global_settings
.volume
= max_vol
;
758 sound_set_volume(global_settings
.volume
);
759 global_status
.last_volume_change
= current_tick
;
763 char* strrsplt(char* str
, int c
)
765 char* s
= strrchr(str
, c
);
779 /* Test file existence, using dircache of possible */
780 bool file_exists(const char *file
)
784 if (!file
|| strlen(file
) <= 0)
788 if (dircache_is_enabled())
789 return (dircache_get_entry_ptr(file
) != NULL
);
792 fd
= open(file
, O_RDONLY
);
799 bool dir_exists(const char *path
)
801 DIR* d
= opendir(path
);
809 * removes the extension of filename (if it doesn't start with a .)
810 * puts the result in buffer
812 char *strip_extension(char* buffer
, int buffer_size
, const char *filename
)
814 char *dot
= strrchr(filename
, '.');
817 if (buffer_size
<= 0)
822 buffer_size
--; /* Make room for end nil */
824 if (dot
!= 0 && filename
[0] != '.')
826 len
= dot
- filename
;
827 len
= MIN(len
, buffer_size
);
834 strlcpy(buffer
, filename
, len
+ 1);
838 #endif /* !defined(__PCTOOL__) */
840 char* skip_whitespace(char* const str
)
850 /* Format time into buf.
852 * buf - buffer to format to.
853 * buf_size - size of buffer.
854 * t - time to format, in milliseconds.
856 void format_time(char* buf
, int buf_size
, long t
)
860 snprintf(buf
, buf_size
, "%d:%02d",
861 (int) (t
/ 60000), (int) (t
% 60000 / 1000));
865 snprintf(buf
, buf_size
, "%d:%02d:%02d",
866 (int) (t
/ 3600000), (int) (t
% 3600000 / 60000),
867 (int) (t
% 60000 / 1000));
872 /** Open a UTF-8 file and set file descriptor to first byte after BOM.
873 * If no BOM is present this behaves like open().
874 * If the file is opened for writing and O_TRUNC is set, write a BOM to
875 * the opened file and leave the file pointer set after the BOM.
877 #define BOM "\xef\xbb\xbf"
880 int open_utf8(const char* pathname
, int flags
)
883 unsigned char bom
[BOM_SIZE
];
885 fd
= open(pathname
, flags
);
889 if(flags
& (O_TRUNC
| O_WRONLY
))
891 write(fd
, BOM
, BOM_SIZE
);
895 read(fd
, bom
, BOM_SIZE
);
897 if(memcmp(bom
, BOM
, BOM_SIZE
))
898 lseek(fd
, 0, SEEK_SET
);
904 #ifdef HAVE_LCD_COLOR
906 * Helper function to convert a string of 6 hex digits to a native colour
909 static int hex2dec(int c
)
911 return (((c
) >= '0' && ((c
) <= '9')) ? (c
) - '0' :
912 (toupper(c
)) - 'A' + 10);
915 int hex_to_rgb(const char* hex
, int* color
)
917 int red
, green
, blue
;
920 while ((i
< 6) && (isxdigit(hex
[i
])))
926 red
= (hex2dec(hex
[0]) << 4) | hex2dec(hex
[1]);
927 green
= (hex2dec(hex
[2]) << 4) | hex2dec(hex
[3]);
928 blue
= (hex2dec(hex
[4]) << 4) | hex2dec(hex
[5]);
930 *color
= LCD_RGBPACK(red
,green
,blue
);
934 #endif /* HAVE_LCD_COLOR */
936 #ifdef HAVE_LCD_BITMAP
937 /* A simplified scanf - used (at time of writing) by wps parsing functions.
939 fmt - char array specifying the format of each list option. Valid values
941 s - string (sets pointer to string, without copying)
942 c - hex colour (RGB888 - e.g. ff00ff)
943 g - greyscale "colour" (0-3)
944 set_vals - if not NULL 1 is set in the bitplace if the item was read OK
946 first item is LSB, (max 32 items! )
947 Stops parseing if an item is invalid unless the item == '-'
948 sep - list separator (e.g. ',' or '|')
949 str - string to parse, must be terminated by 0 or sep
950 ... - pointers to store the parsed values
952 return value - pointer to char after parsed data, 0 if there was an error.
956 /* '0'-'3' are ASCII 0x30 to 0x33 */
957 #define is0123(x) (((x) & 0xfc) == 0x30)
959 const char* parse_list(const char *fmt
, uint32_t *set_vals
,
960 const char sep
, const char* str
, ...)
963 const char* p
= str
, *f
= fmt
;
966 bool set
, is_negative
;
974 /* Check for separator, if we're not at the start */
984 case 's': /* string - return a pointer to it (not a copy) */
985 s
= va_arg(ap
, const char **);
988 while (*p
&& *p
!= sep
)
990 set
= (s
[0][0]!='-') && (s
[0][1]!=sep
) ;
995 d
= va_arg(ap
, int*);
996 if (*p
== '-' && isdigit(*(p
+1)))
1003 if (!set_vals
|| *p
!= '-')
1005 while (*p
&& *p
!= sep
)
1012 *d
= (*d
* 10) + (*p
++ - '0');
1020 #ifdef HAVE_LCD_COLOR
1021 case 'c': /* colour (rrggbb - e.g. f3c1a8) */
1022 d
= va_arg(ap
, int*);
1024 if (hex_to_rgb(p
, d
) < 0)
1026 if (!set_vals
|| *p
!= '-')
1028 while (*p
&& *p
!= sep
)
1040 #if LCD_DEPTH == 2 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 2)
1041 case 'g': /* greyscale colour (0-3) */
1042 d
= va_arg(ap
, int*);
1049 else if (!set_vals
|| *p
!= '-')
1053 while (*p
&& *p
!= sep
)
1060 default: /* Unknown format type */
1064 if (set_vals
&& set
)
1065 *set_vals
|= BIT_N(i
);
1077 /* only used in USB HID and set_time screen */
1078 #if defined(USB_ENABLE_HID) || (CONFIG_RTC != 0)
1079 int clamp_value_wrap(int value
, int max
, int min
)