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"
54 #include "eeprom_settings.h"
56 #ifdef HAVE_LCD_BITMAP
59 #endif /* End HAVE_LCD_BITMAP */
60 #include "gui/gwps-common.h"
72 /* Format a large-range value for output, using the appropriate unit so that
73 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
74 * units) if possible, and 3 significant digits are shown. If a buffer is
75 * given, the result is snprintf()'d into that buffer, otherwise the result is
77 char *output_dyn_value(char *buf
, int buf_size
, int value
,
78 const unsigned char **units
, bool bin_scale
)
80 int scale
= bin_scale
? 1024 : 1000;
86 while (value
>= scale
)
88 fraction
= value
% scale
;
93 fraction
= fraction
* 1000 / 1024;
95 if (value
>= 100 || !unit_no
)
98 snprintf(tbuf
, sizeof(tbuf
), "%01d", fraction
/ 100);
100 snprintf(tbuf
, sizeof(tbuf
), "%02d", fraction
/ 10);
105 snprintf(buf
, buf_size
, "%d%s%s%s", value
, str(LANG_POINT
),
106 tbuf
, P2STR(units
[unit_no
]));
108 snprintf(buf
, buf_size
, "%d%s", value
, P2STR(units
[unit_no
]));
112 /* strip trailing zeros from the fraction */
113 for (i
= strlen(tbuf
) - 1; (i
>= 0) && (tbuf
[i
] == '0'); i
--)
116 talk_number(value
, true);
119 talk_id(LANG_POINT
, true);
120 talk_spell(tbuf
, true);
122 talk_id(P2ID(units
[unit_no
]), true);
127 /* Create a filename with a number part in a way that the number is 1
128 * higher than the highest numbered file matching the same pattern.
129 * It is allowed that buffer and path point to the same memory location,
130 * saving a strcpy(). Path must always be given without trailing slash.
131 * "num" can point to an int specifying the number to use or NULL or a value
132 * less than zero to number automatically. The final number used will also
133 * be returned in *num. If *num is >= 0 then *num will be incremented by
135 char *create_numbered_filename(char *buffer
, const char *path
,
136 const char *prefix
, const char *suffix
,
137 int numberlen
IF_CNFN_NUM_(, int *num
))
140 struct dirent
*entry
;
143 int prefixlen
= strlen(prefix
);
147 strncpy(buffer
, path
, MAX_PATH
);
149 pathlen
= strlen(buffer
);
152 if (num
&& *num
>= 0)
154 /* number specified */
160 /* automatic numbering */
163 dir
= opendir(pathlen
? buffer
: "/");
167 while ((entry
= readdir(dir
)))
171 if (strncasecmp((char *)entry
->d_name
, prefix
, prefixlen
)
172 || strcasecmp((char *)entry
->d_name
+ prefixlen
+ numberlen
, suffix
))
175 curr_num
= atoi((char *)entry
->d_name
+ prefixlen
);
176 if (curr_num
> max_num
)
185 snprintf(fmtstring
, sizeof(fmtstring
), "/%%s%%0%dd%%s", numberlen
);
186 snprintf(buffer
+ pathlen
, MAX_PATH
- pathlen
, fmtstring
, prefix
,
197 /* Format time into buf.
199 * buf - buffer to format to.
200 * buf_size - size of buffer.
201 * t - time to format, in milliseconds.
203 void format_time(char* buf
, int buf_size
, long t
)
207 snprintf(buf
, buf_size
, "%d:%02d",
208 (int) (t
/ 60000), (int) (t
% 60000 / 1000));
212 snprintf(buf
, buf_size
, "%d:%02d:%02d",
213 (int) (t
/ 3600000), (int) (t
% 3600000 / 60000),
214 (int) (t
% 60000 / 1000));
219 /* Create a filename with a date+time part.
220 It is allowed that buffer and path point to the same memory location,
221 saving a strcpy(). Path must always be given without trailing slash.
222 unique_time as true makes the function wait until the current time has
224 char *create_datetime_filename(char *buffer
, const char *path
,
225 const char *prefix
, const char *suffix
,
228 struct tm
*tm
= get_time();
229 static struct tm last_tm
;
232 while (unique_time
&& !memcmp(get_time(), &last_tm
, sizeof (struct tm
)))
238 strncpy(buffer
, path
, MAX_PATH
);
240 pathlen
= strlen(buffer
);
241 snprintf(buffer
+ pathlen
, MAX_PATH
- pathlen
,
242 "/%s%02d%02d%02d-%02d%02d%02d%s", prefix
,
243 tm
->tm_year
% 100, tm
->tm_mon
+ 1, tm
->tm_mday
,
244 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
, suffix
);
248 #endif /* CONFIG_RTC */
250 /* Read (up to) a line of text from fd into buffer and return number of bytes
251 * read (which may be larger than the number of bytes stored in buffer). If
252 * an error occurs, -1 is returned (and buffer contains whatever could be
253 * read). A line is terminated by a LF char. Neither LF nor CR chars are
256 int read_line(int fd
, char* buffer
, int buffer_size
)
263 while (count
< buffer_size
)
267 if (1 != read(fd
, &c
, 1))
281 buffer
[MIN(count
, buffer_size
- 1)] = 0;
283 return errno
? -1 : num_read
;
286 /* Performance optimized version of the previous function. */
287 int fast_readline(int fd
, char *buf
, int buf_size
, void *parameters
,
288 int (*callback
)(int n
, const char *buf
, void *parameters
))
298 rc
= read(fd
, &buf
[pos
], buf_size
- pos
- 1);
302 if ( (p
= strchr(buf
, '\r')) != NULL
)
310 if ( (p
= strchr(p
, '\n')) != NULL
)
316 rc
= callback(count
, buf
, parameters
);
323 pos
= buf_size
- ((long)next
- (long)buf
) - 1;
324 memmove(buf
, next
, pos
);
333 #ifdef HAVE_LCD_BITMAP
336 #define BMP_COMPRESSION 3 /* BI_BITFIELDS */
337 #define BMP_NUMCOLORS 3
339 #define BMP_COMPRESSION 0 /* BI_RGB */
341 #define BMP_NUMCOLORS (1 << LCD_DEPTH)
343 #define BMP_NUMCOLORS 0
349 #define BMP_LINESIZE ((LCD_WIDTH/8 + 3) & ~3)
352 #define BMP_LINESIZE ((LCD_WIDTH/2 + 3) & ~3)
355 #define BMP_LINESIZE ((LCD_WIDTH + 3) & ~3)
356 #elif LCD_DEPTH <= 16
358 #define BMP_LINESIZE ((LCD_WIDTH*2 + 3) & ~3)
361 #define BMP_LINESIZE ((LCD_WIDTH*3 + 3) & ~3)
364 #define BMP_HEADERSIZE (54 + 4 * BMP_NUMCOLORS)
365 #define BMP_DATASIZE (BMP_LINESIZE * LCD_HEIGHT)
366 #define BMP_TOTALSIZE (BMP_HEADERSIZE + BMP_DATASIZE)
368 #define LE16_CONST(x) (x)&0xff, ((x)>>8)&0xff
369 #define LE32_CONST(x) (x)&0xff, ((x)>>8)&0xff, ((x)>>16)&0xff, ((x)>>24)&0xff
371 static const unsigned char bmpheader
[] =
373 0x42, 0x4d, /* 'BM' */
374 LE32_CONST(BMP_TOTALSIZE
), /* Total file size */
375 0x00, 0x00, 0x00, 0x00, /* Reserved */
376 LE32_CONST(BMP_HEADERSIZE
), /* Offset to start of pixel data */
378 0x28, 0x00, 0x00, 0x00, /* Size of (2nd) header */
379 LE32_CONST(LCD_WIDTH
), /* Width in pixels */
380 LE32_CONST(LCD_HEIGHT
), /* Height in pixels */
381 0x01, 0x00, /* Number of planes (always 1) */
382 LE16_CONST(BMP_BPP
), /* Bits per pixel 1/4/8/16/24 */
383 LE32_CONST(BMP_COMPRESSION
),/* Compression mode */
384 LE32_CONST(BMP_DATASIZE
), /* Size of bitmap data */
385 0xc4, 0x0e, 0x00, 0x00, /* Horizontal resolution (pixels/meter) */
386 0xc4, 0x0e, 0x00, 0x00, /* Vertical resolution (pixels/meter) */
387 LE32_CONST(BMP_NUMCOLORS
), /* Number of used colours */
388 LE32_CONST(BMP_NUMCOLORS
), /* Number of important colours */
391 0x90, 0xee, 0x90, 0x00, /* Colour #0 */
392 0x00, 0x00, 0x00, 0x00 /* Colour #1 */
394 0xe6, 0xd8, 0xad, 0x00, /* Colour #0 */
395 0x99, 0x90, 0x73, 0x00, /* Colour #1 */
396 0x4c, 0x48, 0x39, 0x00, /* Colour #2 */
397 0x00, 0x00, 0x00, 0x00 /* Colour #3 */
398 #elif LCD_DEPTH == 16
399 0x00, 0xf8, 0x00, 0x00, /* red bitfield mask */
400 0xe0, 0x07, 0x00, 0x00, /* green bitfield mask */
401 0x1f, 0x00, 0x00, 0x00 /* blue bitfield mask */
405 static void (*screen_dump_hook
)(int fh
) = NULL
;
407 void screen_dump(void)
410 char filename
[MAX_PATH
];
413 static unsigned char line_block
[8][BMP_LINESIZE
];
415 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
416 static unsigned char line_block
[BMP_LINESIZE
];
418 static unsigned char line_block
[4][BMP_LINESIZE
];
420 #elif LCD_DEPTH == 16
421 static unsigned short line_block
[BMP_LINESIZE
/2];
425 create_datetime_filename(filename
, "", "dump ", ".bmp", false);
427 create_numbered_filename(filename
, "", "dump_", ".bmp", 4
428 IF_CNFN_NUM_(, NULL
));
431 fh
= creat(filename
);
435 if (screen_dump_hook
)
437 screen_dump_hook(fh
);
441 write(fh
, bmpheader
, sizeof(bmpheader
));
443 /* BMP image goes bottom up */
445 for (by
= LCD_FBHEIGHT
- 1; by
>= 0; by
--)
447 unsigned char *src
= &lcd_framebuffer
[by
][0];
448 unsigned char *dst
= &line_block
[0][0];
450 memset(line_block
, 0, sizeof(line_block
));
451 for (bx
= LCD_WIDTH
/8; bx
> 0; bx
--)
453 unsigned dst_mask
= 0x80;
456 for (ix
= 8; ix
> 0; ix
--)
458 unsigned char *dst_blk
= dst
;
459 unsigned src_byte
= *src
++;
462 for (iy
= 8; iy
> 0; iy
--)
465 *dst_blk
|= dst_mask
;
467 dst_blk
+= BMP_LINESIZE
;
474 write(fh
, line_block
, sizeof(line_block
));
477 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
478 for (by
= LCD_FBHEIGHT
- 1; by
>= 0; by
--)
480 unsigned char *src
= &lcd_framebuffer
[by
][0];
481 unsigned char *dst
= line_block
;
483 memset(line_block
, 0, sizeof(line_block
));
484 for (bx
= LCD_FBWIDTH
; bx
> 0; bx
--)
486 unsigned src_byte
= *src
++;
488 *dst
++ = ((src_byte
>> 2) & 0x30) | ((src_byte
>> 4) & 0x03);
489 *dst
++ = ((src_byte
<< 2) & 0x30) | (src_byte
& 0x03);
492 write(fh
, line_block
, sizeof(line_block
));
494 #else /* VERTICAL_PACKING */
495 for (by
= LCD_FBHEIGHT
- 1; by
>= 0; by
--)
497 unsigned char *src
= &lcd_framebuffer
[by
][0];
498 unsigned char *dst
= &line_block
[3][0];
500 memset(line_block
, 0, sizeof(line_block
));
501 for (bx
= LCD_WIDTH
/2; bx
> 0; bx
--)
503 unsigned char *dst_blk
= dst
++;
504 unsigned src_byte0
= *src
++;
505 unsigned src_byte1
= *src
++;
508 for (iy
= 4; iy
> 0; iy
--)
510 *dst_blk
= ((src_byte0
& 3) << 4) | (src_byte1
& 3);
513 dst_blk
-= BMP_LINESIZE
;
517 write(fh
, line_block
, sizeof(line_block
));
520 #elif LCD_DEPTH == 16
521 for (by
= LCD_HEIGHT
- 1; by
>= 0; by
--)
523 unsigned short *src
= &lcd_framebuffer
[by
][0];
524 unsigned short *dst
= line_block
;
526 memset(line_block
, 0, sizeof(line_block
));
527 for (bx
= LCD_WIDTH
; bx
> 0; bx
--)
529 #if (LCD_PIXELFORMAT == RGB565SWAPPED)
530 /* iPod LCD data is big endian although the CPU is not */
531 *dst
++ = htobe16(*src
++);
533 *dst
++ = htole16(*src
++);
537 write(fh
, line_block
, sizeof(line_block
));
539 #endif /* LCD_DEPTH */
545 void screen_dump_set_hook(void (*hook
)(int fh
))
547 screen_dump_hook
= hook
;
550 #endif /* HAVE_LCD_BITMAP */
552 /* parse a line from a configuration file. the line format is:
556 Any whitespace before setting name or value (after ':') is ignored.
557 A # as first non-whitespace character discards the whole line.
558 Function sets pointers to null-terminated setting name and value.
559 Returns false if no valid config entry was found.
562 bool settings_parseline(char* line
, char** name
, char** value
)
566 while ( isspace(*line
) )
572 ptr
= strchr(line
, ':');
579 while (isspace(*ptr
))
585 static void system_flush(void)
588 call_ata_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
591 static void system_restore(void)
596 static bool clean_shutdown(void (*callback
)(void *), void *parameter
)
601 call_ata_idle_notifys(true);
606 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
607 if(!charger_inserted())
610 bool batt_crit
= battery_level_critical();
611 int audio_stat
= audio_status();
614 screens
[i
].clear_display();
615 #ifdef X5_BACKLIGHT_SHUTDOWN
616 x5_backlight_shutdown();
618 if (!battery_level_safe())
619 gui_syncsplash(3*HZ
, "%s %s",
620 str(LANG_WARNING_BATTERY_EMPTY
),
621 str(LANG_SHUTTINGDOWN
));
622 else if (battery_level_critical())
623 gui_syncsplash(3*HZ
, "%s %s",
624 str(LANG_WARNING_BATTERY_LOW
),
625 str(LANG_SHUTTINGDOWN
));
628 if (!tagcache_prepare_shutdown())
631 gui_syncsplash(HZ
, str(LANG_TAGCACHE_BUSY
));
635 gui_syncsplash(0, str(LANG_SHUTTINGDOWN
));
638 if (global_settings
.fade_on_stop
639 && (audio_stat
& AUDIO_STATUS_PLAY
))
644 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
645 if (!batt_crit
&& (audio_stat
& AUDIO_STATUS_RECORD
))
647 audio_stop_recording();
648 while(audio_status() & AUDIO_STATUS_RECORD
)
652 audio_close_recording();
654 /* audio_stop_recording == audio_stop for HWCODEC */
657 while (audio_status())
660 if (callback
!= NULL
)
663 if (!batt_crit
) /* do not save on critical battery */
665 #ifdef HAVE_EEPROM_SETTINGS
666 if (firmware_settings
.initialized
)
668 firmware_settings
.disk_clean
= true;
669 firmware_settings
.bl_version
= 0;
670 eeprom_settings_store();
680 static bool waiting_to_resume_play
= false;
681 static long play_resume_tick
;
683 static void car_adapter_mode_processing(bool inserted
)
685 if (global_settings
.car_adapter_mode
)
690 * Just got plugged in, delay & resume if we were playing
692 if (audio_status() & AUDIO_STATUS_PAUSE
)
694 /* delay resume a bit while the engine is cranking */
695 play_resume_tick
= current_tick
+ HZ
*5;
696 waiting_to_resume_play
= true;
702 * Just got unplugged, pause if playing
704 if ((audio_status() & AUDIO_STATUS_PLAY
) &&
705 !(audio_status() & AUDIO_STATUS_PAUSE
))
707 if (global_settings
.fade_on_stop
)
716 static void car_adapter_tick(void)
718 if (waiting_to_resume_play
)
720 if (TIME_AFTER(current_tick
, play_resume_tick
))
722 if (audio_status() & AUDIO_STATUS_PAUSE
)
726 waiting_to_resume_play
= false;
731 void car_adapter_mode_init(void)
733 tick_add_task(car_adapter_tick
);
737 #ifdef HAVE_HEADPHONE_DETECTION
738 static void unplug_change(bool inserted
)
740 if (global_settings
.unplug_mode
)
744 if ( global_settings
.unplug_mode
> 1 )
750 if (global_settings
.unplug_rw
)
752 if ( audio_current_track()->elapsed
>
753 (unsigned long)(global_settings
.unplug_rw
*1000))
754 audio_ff_rewind(audio_current_track()->elapsed
-
755 (global_settings
.unplug_rw
*1000));
764 long default_event_handler_ex(long event
, void (*callback
)(void *), void *parameter
)
768 case SYS_USB_CONNECTED
:
769 if (callback
!= NULL
)
772 if (!mmc_touched() || (mmc_remove_request() == SYS_MMC_EXTRACTED
))
775 scrobbler_flush_cache();
778 #ifndef USB_IPODSTYLE
779 check_bootfile(false); /* gets initial size */
784 #ifndef USB_IPODSTYLE
785 check_bootfile(true);
790 return SYS_USB_CONNECTED
;
792 if (!clean_shutdown(callback
, parameter
))
796 case SYS_CHARGER_CONNECTED
:
797 car_adapter_mode_processing(true);
798 return SYS_CHARGER_CONNECTED
;
800 case SYS_CHARGER_DISCONNECTED
:
801 car_adapter_mode_processing(false);
802 return SYS_CHARGER_DISCONNECTED
;
804 #ifdef HAVE_HEADPHONE_DETECTION
805 case SYS_PHONE_PLUGGED
:
807 return SYS_PHONE_PLUGGED
;
809 case SYS_PHONE_UNPLUGGED
:
810 unplug_change(false);
811 return SYS_PHONE_UNPLUGGED
;
817 long default_event_handler(long event
)
819 return default_event_handler_ex(event
, NULL
, NULL
);
822 int show_logo( void )
824 #ifdef HAVE_LCD_BITMAP
829 lcd_bitmap(rockboxlogo
, 0, 10, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
831 #ifdef HAVE_REMOTE_LCD
832 lcd_remote_clear_display();
833 lcd_remote_bitmap(remote_rockboxlogo
, 0, 10, BMPWIDTH_remote_rockboxlogo
,
834 BMPHEIGHT_remote_rockboxlogo
);
837 snprintf(version
, sizeof(version
), "Ver. %s", appsversion
);
838 lcd_setfont(FONT_SYSFIXED
);
839 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
840 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
841 LCD_HEIGHT
-font_h
, (unsigned char *)version
);
844 #ifdef HAVE_REMOTE_LCD
845 lcd_remote_setfont(FONT_SYSFIXED
);
846 lcd_remote_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
847 lcd_remote_putsxy((LCD_REMOTE_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
848 LCD_REMOTE_HEIGHT
-font_h
, (unsigned char *)version
);
853 char *rockbox
= " ROCKbox!";
855 lcd_double_height(true);
856 lcd_puts(0, 0, rockbox
);
857 lcd_puts_scroll(0, 1, appsversion
);
863 #if CONFIG_CODEC == SWCODEC
864 int get_replaygain_mode(bool have_track_gain
, bool have_album_gain
)
868 bool track
= ((global_settings
.replaygain_type
== REPLAYGAIN_TRACK
)
869 || ((global_settings
.replaygain_type
== REPLAYGAIN_SHUFFLE
)
870 && global_settings
.playlist_shuffle
));
872 type
= (!track
&& have_album_gain
) ? REPLAYGAIN_ALBUM
873 : have_track_gain
? REPLAYGAIN_TRACK
: -1;
880 #ifndef USB_IPODSTYLE
882 memorize/compare details about the BOOTFILE
883 we don't use dircache because it may not be up to date after
884 USB disconnect (scanning in the background)
886 void check_bootfile(bool do_rolo
)
888 static int boot_size
= 0;
889 static int boot_cluster
= 0;
891 struct dirent
* entry
= NULL
;
893 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
894 dir
= opendir(BOOTDIR
);
896 if(!dir
) return; /* do we want an error splash? */
898 /* loop all files in BOOTDIR */
899 while(0 != (entry
= readdir(dir
)))
901 if(!strcasecmp(entry
->d_name
, BOOTFILE
))
903 /* found the bootfile */
904 if(boot_size
&& do_rolo
)
906 if((entry
->size
!= boot_size
) ||
907 (entry
->startcluster
!= boot_cluster
))
909 char *lines
[] = { str(LANG_BOOT_CHANGED
),
910 str(LANG_REBOOT_NOW
) };
911 struct text_message message
={ lines
, 2 };
912 button_clear_queue(); /* Empty the keyboard buffer */
913 if(gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
)
914 rolo_load(BOOTDIR
"/" BOOTFILE
);
917 boot_size
= entry
->size
;
918 boot_cluster
= entry
->startcluster
;