1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Daniel Stenberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
27 #include "lcd-remote.h"
31 #include "timefuncs.h"
36 #include "mp3_playback.h"
39 #include "ata_idle_notify.h"
42 #include "powermgmt.h"
43 #include "backlight.h"
49 #include "scrobbler.h"
55 #include "eeprom_settings.h"
57 #ifdef HAVE_LCD_BITMAP
60 #endif /* End HAVE_LCD_BITMAP */
61 #include "gui/gwps-common.h"
74 /* Format a large-range value for output, using the appropriate unit so that
75 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
76 * units) if possible, and 3 significant digits are shown. If a buffer is
77 * given, the result is snprintf()'d into that buffer, otherwise the result is
79 char *output_dyn_value(char *buf
, int buf_size
, int value
,
80 const unsigned char **units
, bool bin_scale
)
82 int scale
= bin_scale
? 1024 : 1000;
88 while (value
>= scale
)
90 fraction
= value
% scale
;
95 fraction
= fraction
* 1000 / 1024;
97 if (value
>= 100 || !unit_no
)
100 snprintf(tbuf
, sizeof(tbuf
), "%01d", fraction
/ 100);
102 snprintf(tbuf
, sizeof(tbuf
), "%02d", fraction
/ 10);
107 snprintf(buf
, buf_size
, "%d%s%s%s", value
, str(LANG_POINT
),
108 tbuf
, P2STR(units
[unit_no
]));
110 snprintf(buf
, buf_size
, "%d%s", value
, P2STR(units
[unit_no
]));
114 /* strip trailing zeros from the fraction */
115 for (i
= strlen(tbuf
) - 1; (i
>= 0) && (tbuf
[i
] == '0'); i
--)
118 talk_number(value
, true);
121 talk_id(LANG_POINT
, true);
122 talk_spell(tbuf
, true);
124 talk_id(P2ID(units
[unit_no
]), true);
129 /* Create a filename with a number part in a way that the number is 1
130 * higher than the highest numbered file matching the same pattern.
131 * It is allowed that buffer and path point to the same memory location,
132 * saving a strcpy(). Path must always be given without trailing slash.
133 * "num" can point to an int specifying the number to use or NULL or a value
134 * less than zero to number automatically. The final number used will also
135 * be returned in *num. If *num is >= 0 then *num will be incremented by
137 char *create_numbered_filename(char *buffer
, const char *path
,
138 const char *prefix
, const char *suffix
,
139 int numberlen
IF_CNFN_NUM_(, int *num
))
142 struct dirent
*entry
;
145 int prefixlen
= strlen(prefix
);
149 strncpy(buffer
, path
, MAX_PATH
);
151 pathlen
= strlen(buffer
);
154 if (num
&& *num
>= 0)
156 /* number specified */
162 /* automatic numbering */
165 dir
= opendir(pathlen
? buffer
: "/");
169 while ((entry
= readdir(dir
)))
173 if (strncasecmp((char *)entry
->d_name
, prefix
, prefixlen
)
174 || strcasecmp((char *)entry
->d_name
+ prefixlen
+ numberlen
, suffix
))
177 curr_num
= atoi((char *)entry
->d_name
+ prefixlen
);
178 if (curr_num
> max_num
)
187 snprintf(fmtstring
, sizeof(fmtstring
), "/%%s%%0%dd%%s", numberlen
);
188 snprintf(buffer
+ pathlen
, MAX_PATH
- pathlen
, fmtstring
, prefix
,
199 /* Format time into buf.
201 * buf - buffer to format to.
202 * buf_size - size of buffer.
203 * t - time to format, in milliseconds.
205 void format_time(char* buf
, int buf_size
, long t
)
209 snprintf(buf
, buf_size
, "%d:%02d",
210 (int) (t
/ 60000), (int) (t
% 60000 / 1000));
214 snprintf(buf
, buf_size
, "%d:%02d:%02d",
215 (int) (t
/ 3600000), (int) (t
% 3600000 / 60000),
216 (int) (t
% 60000 / 1000));
221 /* Create a filename with a date+time part.
222 It is allowed that buffer and path point to the same memory location,
223 saving a strcpy(). Path must always be given without trailing slash.
224 unique_time as true makes the function wait until the current time has
226 char *create_datetime_filename(char *buffer
, const char *path
,
227 const char *prefix
, const char *suffix
,
230 struct tm
*tm
= get_time();
231 static struct tm last_tm
;
234 while (unique_time
&& !memcmp(get_time(), &last_tm
, sizeof (struct tm
)))
240 strncpy(buffer
, path
, MAX_PATH
);
242 pathlen
= strlen(buffer
);
243 snprintf(buffer
+ pathlen
, MAX_PATH
- pathlen
,
244 "/%s%02d%02d%02d-%02d%02d%02d%s", prefix
,
245 tm
->tm_year
% 100, tm
->tm_mon
+ 1, tm
->tm_mday
,
246 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
, suffix
);
250 #endif /* CONFIG_RTC */
252 /* Read (up to) a line of text from fd into buffer and return number of bytes
253 * read (which may be larger than the number of bytes stored in buffer). If
254 * an error occurs, -1 is returned (and buffer contains whatever could be
255 * read). A line is terminated by a LF char. Neither LF nor CR chars are
258 int read_line(int fd
, char* buffer
, int buffer_size
)
265 while (count
< buffer_size
)
269 if (1 != read(fd
, &c
, 1))
283 buffer
[MIN(count
, buffer_size
- 1)] = 0;
285 return errno
? -1 : num_read
;
288 /* Performance optimized version of the previous function. */
289 int fast_readline(int fd
, char *buf
, int buf_size
, void *parameters
,
290 int (*callback
)(int n
, const char *buf
, void *parameters
))
300 rc
= read(fd
, &buf
[pos
], buf_size
- pos
- 1);
304 if ( (p
= strchr(buf
, '\r')) != NULL
)
312 if ( (p
= strchr(p
, '\n')) != NULL
)
318 rc
= callback(count
, buf
, parameters
);
325 pos
= buf_size
- ((long)next
- (long)buf
) - 1;
326 memmove(buf
, next
, pos
);
335 #ifdef HAVE_LCD_BITMAP
338 #define BMP_COMPRESSION 3 /* BI_BITFIELDS */
339 #define BMP_NUMCOLORS 3
341 #define BMP_COMPRESSION 0 /* BI_RGB */
343 #define BMP_NUMCOLORS (1 << LCD_DEPTH)
345 #define BMP_NUMCOLORS 0
351 #define BMP_LINESIZE ((LCD_WIDTH/8 + 3) & ~3)
354 #define BMP_LINESIZE ((LCD_WIDTH/2 + 3) & ~3)
357 #define BMP_LINESIZE ((LCD_WIDTH + 3) & ~3)
358 #elif LCD_DEPTH <= 16
360 #define BMP_LINESIZE ((LCD_WIDTH*2 + 3) & ~3)
363 #define BMP_LINESIZE ((LCD_WIDTH*3 + 3) & ~3)
366 #define BMP_HEADERSIZE (54 + 4 * BMP_NUMCOLORS)
367 #define BMP_DATASIZE (BMP_LINESIZE * LCD_HEIGHT)
368 #define BMP_TOTALSIZE (BMP_HEADERSIZE + BMP_DATASIZE)
370 #define LE16_CONST(x) (x)&0xff, ((x)>>8)&0xff
371 #define LE32_CONST(x) (x)&0xff, ((x)>>8)&0xff, ((x)>>16)&0xff, ((x)>>24)&0xff
373 static const unsigned char bmpheader
[] =
375 0x42, 0x4d, /* 'BM' */
376 LE32_CONST(BMP_TOTALSIZE
), /* Total file size */
377 0x00, 0x00, 0x00, 0x00, /* Reserved */
378 LE32_CONST(BMP_HEADERSIZE
), /* Offset to start of pixel data */
380 0x28, 0x00, 0x00, 0x00, /* Size of (2nd) header */
381 LE32_CONST(LCD_WIDTH
), /* Width in pixels */
382 LE32_CONST(LCD_HEIGHT
), /* Height in pixels */
383 0x01, 0x00, /* Number of planes (always 1) */
384 LE16_CONST(BMP_BPP
), /* Bits per pixel 1/4/8/16/24 */
385 LE32_CONST(BMP_COMPRESSION
),/* Compression mode */
386 LE32_CONST(BMP_DATASIZE
), /* Size of bitmap data */
387 0xc4, 0x0e, 0x00, 0x00, /* Horizontal resolution (pixels/meter) */
388 0xc4, 0x0e, 0x00, 0x00, /* Vertical resolution (pixels/meter) */
389 LE32_CONST(BMP_NUMCOLORS
), /* Number of used colours */
390 LE32_CONST(BMP_NUMCOLORS
), /* Number of important colours */
393 0x90, 0xee, 0x90, 0x00, /* Colour #0 */
394 0x00, 0x00, 0x00, 0x00 /* Colour #1 */
396 0xe6, 0xd8, 0xad, 0x00, /* Colour #0 */
397 0x99, 0x90, 0x73, 0x00, /* Colour #1 */
398 0x4c, 0x48, 0x39, 0x00, /* Colour #2 */
399 0x00, 0x00, 0x00, 0x00 /* Colour #3 */
400 #elif LCD_DEPTH == 16
401 0x00, 0xf8, 0x00, 0x00, /* red bitfield mask */
402 0xe0, 0x07, 0x00, 0x00, /* green bitfield mask */
403 0x1f, 0x00, 0x00, 0x00 /* blue bitfield mask */
407 static void (*screen_dump_hook
)(int fh
) = NULL
;
409 void screen_dump(void)
412 char filename
[MAX_PATH
];
415 static unsigned char line_block
[8][BMP_LINESIZE
];
417 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
418 static unsigned char line_block
[BMP_LINESIZE
];
420 static unsigned char line_block
[4][BMP_LINESIZE
];
422 #elif LCD_DEPTH == 16
423 static unsigned short line_block
[BMP_LINESIZE
/2];
427 create_datetime_filename(filename
, "", "dump ", ".bmp", false);
429 create_numbered_filename(filename
, "", "dump_", ".bmp", 4
430 IF_CNFN_NUM_(, NULL
));
433 fh
= creat(filename
);
437 if (screen_dump_hook
)
439 screen_dump_hook(fh
);
443 write(fh
, bmpheader
, sizeof(bmpheader
));
445 /* BMP image goes bottom up */
447 for (by
= LCD_FBHEIGHT
- 1; by
>= 0; by
--)
449 unsigned char *src
= &lcd_framebuffer
[by
][0];
450 unsigned char *dst
= &line_block
[0][0];
452 memset(line_block
, 0, sizeof(line_block
));
453 for (bx
= LCD_WIDTH
/8; bx
> 0; bx
--)
455 unsigned dst_mask
= 0x80;
458 for (ix
= 8; ix
> 0; ix
--)
460 unsigned char *dst_blk
= dst
;
461 unsigned src_byte
= *src
++;
464 for (iy
= 8; iy
> 0; iy
--)
467 *dst_blk
|= dst_mask
;
469 dst_blk
+= BMP_LINESIZE
;
476 write(fh
, line_block
, sizeof(line_block
));
479 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
480 for (by
= LCD_FBHEIGHT
- 1; by
>= 0; by
--)
482 unsigned char *src
= &lcd_framebuffer
[by
][0];
483 unsigned char *dst
= line_block
;
485 memset(line_block
, 0, sizeof(line_block
));
486 for (bx
= LCD_FBWIDTH
; bx
> 0; bx
--)
488 unsigned src_byte
= *src
++;
490 *dst
++ = ((src_byte
>> 2) & 0x30) | ((src_byte
>> 4) & 0x03);
491 *dst
++ = ((src_byte
<< 2) & 0x30) | (src_byte
& 0x03);
494 write(fh
, line_block
, sizeof(line_block
));
496 #else /* VERTICAL_PACKING */
497 for (by
= LCD_FBHEIGHT
- 1; by
>= 0; by
--)
499 unsigned char *src
= &lcd_framebuffer
[by
][0];
500 unsigned char *dst
= &line_block
[3][0];
502 memset(line_block
, 0, sizeof(line_block
));
503 for (bx
= LCD_WIDTH
/2; bx
> 0; bx
--)
505 unsigned char *dst_blk
= dst
++;
506 unsigned src_byte0
= *src
++;
507 unsigned src_byte1
= *src
++;
510 for (iy
= 4; iy
> 0; iy
--)
512 *dst_blk
= ((src_byte0
& 3) << 4) | (src_byte1
& 3);
515 dst_blk
-= BMP_LINESIZE
;
519 write(fh
, line_block
, sizeof(line_block
));
522 #elif LCD_DEPTH == 16
523 for (by
= LCD_HEIGHT
- 1; by
>= 0; by
--)
525 unsigned short *src
= &lcd_framebuffer
[by
][0];
526 unsigned short *dst
= line_block
;
528 memset(line_block
, 0, sizeof(line_block
));
529 for (bx
= LCD_WIDTH
; bx
> 0; bx
--)
531 #if (LCD_PIXELFORMAT == RGB565SWAPPED)
532 /* iPod LCD data is big endian although the CPU is not */
533 *dst
++ = htobe16(*src
++);
535 *dst
++ = htole16(*src
++);
539 write(fh
, line_block
, sizeof(line_block
));
541 #endif /* LCD_DEPTH */
547 void screen_dump_set_hook(void (*hook
)(int fh
))
549 screen_dump_hook
= hook
;
552 #endif /* HAVE_LCD_BITMAP */
554 /* parse a line from a configuration file. the line format is:
558 Any whitespace before setting name or value (after ':') is ignored.
559 A # as first non-whitespace character discards the whole line.
560 Function sets pointers to null-terminated setting name and value.
561 Returns false if no valid config entry was found.
564 bool settings_parseline(char* line
, char** name
, char** value
)
568 while ( isspace(*line
) )
574 ptr
= strchr(line
, ':');
581 while (isspace(*ptr
))
587 static void system_flush(void)
590 call_ata_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
593 static void system_restore(void)
598 static bool clean_shutdown(void (*callback
)(void *), void *parameter
)
603 call_ata_idle_notifys(true);
608 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
609 if(!charger_inserted())
612 bool batt_crit
= battery_level_critical();
613 int audio_stat
= audio_status();
616 screens
[i
].clear_display();
617 #ifdef X5_BACKLIGHT_SHUTDOWN
618 x5_backlight_shutdown();
620 if (!battery_level_safe())
621 gui_syncsplash(3*HZ
, "%s %s",
622 str(LANG_WARNING_BATTERY_EMPTY
),
623 str(LANG_SHUTTINGDOWN
));
624 else if (battery_level_critical())
625 gui_syncsplash(3*HZ
, "%s %s",
626 str(LANG_WARNING_BATTERY_LOW
),
627 str(LANG_SHUTTINGDOWN
));
630 if (!tagcache_prepare_shutdown())
633 gui_syncsplash(HZ
, str(LANG_TAGCACHE_BUSY
));
637 gui_syncsplash(0, str(LANG_SHUTTINGDOWN
));
640 if (global_settings
.fade_on_stop
641 && (audio_stat
& AUDIO_STATUS_PLAY
))
646 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
647 if (!batt_crit
&& (audio_stat
& AUDIO_STATUS_RECORD
))
649 audio_stop_recording();
650 while(audio_status() & AUDIO_STATUS_RECORD
)
654 audio_close_recording();
656 /* audio_stop_recording == audio_stop for HWCODEC */
659 while (audio_status())
662 if (callback
!= NULL
)
665 if (!batt_crit
) /* do not save on critical battery */
667 #ifdef HAVE_EEPROM_SETTINGS
668 if (firmware_settings
.initialized
)
670 firmware_settings
.disk_clean
= true;
671 firmware_settings
.bl_version
= 0;
672 eeprom_settings_store();
681 bool list_stop_handler(void)
685 /* Stop the music if it is playing */
688 if (!global_settings
.party_mode
)
690 if (global_settings
.fade_on_stop
)
692 bookmark_autobookmark();
697 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
700 if (charger_inserted())
703 shutdown_screen(); /* won't return if shutdown actually happens */
705 ret
= true; /* screen is dirty, caller needs to refresh */
708 #ifndef HAVE_POWEROFF_WHILE_CHARGING
710 static long last_off
= 0;
712 if (TIME_BEFORE(current_tick
, last_off
+ HZ
/2))
714 if (charger_inserted())
717 ret
= true; /* screen is dirty, caller needs to refresh */
720 last_off
= current_tick
;
723 #endif /* CONFIG_CHARGING */
728 static bool waiting_to_resume_play
= false;
729 static long play_resume_tick
;
731 static void car_adapter_mode_processing(bool inserted
)
733 if (global_settings
.car_adapter_mode
)
738 * Just got plugged in, delay & resume if we were playing
740 if (audio_status() & AUDIO_STATUS_PAUSE
)
742 /* delay resume a bit while the engine is cranking */
743 play_resume_tick
= current_tick
+ HZ
*5;
744 waiting_to_resume_play
= true;
750 * Just got unplugged, pause if playing
752 if ((audio_status() & AUDIO_STATUS_PLAY
) &&
753 !(audio_status() & AUDIO_STATUS_PAUSE
))
755 if (global_settings
.fade_on_stop
)
764 static void car_adapter_tick(void)
766 if (waiting_to_resume_play
)
768 if (TIME_AFTER(current_tick
, play_resume_tick
))
770 if (audio_status() & AUDIO_STATUS_PAUSE
)
774 waiting_to_resume_play
= false;
779 void car_adapter_mode_init(void)
781 tick_add_task(car_adapter_tick
);
785 #ifdef HAVE_HEADPHONE_DETECTION
786 static void unplug_change(bool inserted
)
788 if (global_settings
.unplug_mode
)
792 if ( global_settings
.unplug_mode
> 1 )
798 if (global_settings
.unplug_rw
)
800 if ( audio_current_track()->elapsed
>
801 (unsigned long)(global_settings
.unplug_rw
*1000))
802 audio_ff_rewind(audio_current_track()->elapsed
-
803 (global_settings
.unplug_rw
*1000));
812 long default_event_handler_ex(long event
, void (*callback
)(void *), void *parameter
)
816 case SYS_USB_CONNECTED
:
817 if (callback
!= NULL
)
820 if (!mmc_touched() || (mmc_remove_request() == SYS_MMC_EXTRACTED
))
823 scrobbler_flush_cache();
826 #ifndef USB_IPODSTYLE
827 check_bootfile(false); /* gets initial size */
832 #ifndef USB_IPODSTYLE
833 check_bootfile(true);
838 return SYS_USB_CONNECTED
;
840 if (!clean_shutdown(callback
, parameter
))
844 case SYS_CHARGER_CONNECTED
:
845 car_adapter_mode_processing(true);
846 return SYS_CHARGER_CONNECTED
;
848 case SYS_CHARGER_DISCONNECTED
:
849 car_adapter_mode_processing(false);
850 return SYS_CHARGER_DISCONNECTED
;
852 #ifdef HAVE_HEADPHONE_DETECTION
853 case SYS_PHONE_PLUGGED
:
855 return SYS_PHONE_PLUGGED
;
857 case SYS_PHONE_UNPLUGGED
:
858 unplug_change(false);
859 return SYS_PHONE_UNPLUGGED
;
865 long default_event_handler(long event
)
867 return default_event_handler_ex(event
, NULL
, NULL
);
870 int show_logo( void )
872 #ifdef HAVE_LCD_BITMAP
876 snprintf(version
, sizeof(version
), "Ver. %s", appsversion
);
879 lcd_bitmap(rockboxlogo
, 0, 10, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
880 lcd_setfont(FONT_SYSFIXED
);
881 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
882 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
883 LCD_HEIGHT
-font_h
, (unsigned char *)version
);
884 lcd_setfont(FONT_UI
);
887 char *rockbox
= " ROCKbox!";
890 lcd_double_height(true);
891 lcd_puts(0, 0, rockbox
);
892 lcd_puts_scroll(0, 1, appsversion
);
896 #ifdef HAVE_REMOTE_LCD
897 lcd_remote_clear_display();
898 lcd_remote_bitmap(remote_rockboxlogo
, 0, 10, BMPWIDTH_remote_rockboxlogo
,
899 BMPHEIGHT_remote_rockboxlogo
);
900 lcd_remote_setfont(FONT_SYSFIXED
);
901 lcd_remote_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
902 lcd_remote_putsxy((LCD_REMOTE_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
903 LCD_REMOTE_HEIGHT
-font_h
, (unsigned char *)version
);
904 lcd_remote_setfont(FONT_UI
);
911 #if CONFIG_CODEC == SWCODEC
912 int get_replaygain_mode(bool have_track_gain
, bool have_album_gain
)
916 bool track
= ((global_settings
.replaygain_type
== REPLAYGAIN_TRACK
)
917 || ((global_settings
.replaygain_type
== REPLAYGAIN_SHUFFLE
)
918 && global_settings
.playlist_shuffle
));
920 type
= (!track
&& have_album_gain
) ? REPLAYGAIN_ALBUM
921 : have_track_gain
? REPLAYGAIN_TRACK
: -1;
928 #ifndef USB_IPODSTYLE
930 memorize/compare details about the BOOTFILE
931 we don't use dircache because it may not be up to date after
932 USB disconnect (scanning in the background)
934 void check_bootfile(bool do_rolo
)
936 static unsigned short wrtdate
= 0;
937 static unsigned short wrttime
= 0;
939 struct dirent
* entry
= NULL
;
941 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
942 dir
= opendir(BOOTDIR
);
944 if(!dir
) return; /* do we want an error splash? */
946 /* loop all files in BOOTDIR */
947 while(0 != (entry
= readdir(dir
)))
949 if(!strcasecmp(entry
->d_name
, BOOTFILE
))
951 /* found the bootfile */
952 if(wrtdate
&& do_rolo
)
954 if((entry
->wrtdate
!= wrtdate
) ||
955 (entry
->wrttime
!= wrttime
))
957 char *lines
[] = { str(LANG_BOOT_CHANGED
),
958 str(LANG_REBOOT_NOW
) };
959 struct text_message message
={ lines
, 2 };
960 button_clear_queue(); /* Empty the keyboard buffer */
961 if(gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
)
962 rolo_load(BOOTDIR
"/" BOOTFILE
);
965 wrtdate
= entry
->wrtdate
;
966 wrttime
= entry
->wrttime
;
974 /* check range, set volume and save settings */
977 const int min_vol
= sound_min(SOUND_VOLUME
);
978 const int max_vol
= sound_max(SOUND_VOLUME
);
979 if (global_settings
.volume
< min_vol
)
980 global_settings
.volume
= min_vol
;
981 if (global_settings
.volume
> max_vol
)
982 global_settings
.volume
= max_vol
;
983 sound_set_volume(global_settings
.volume
);