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"
64 /* Format a large-range value for output, using the appropriate unit so that
65 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
66 * units) if possible, and 3 significant digits are shown. If a buffer is
67 * given, the result is snprintf()'d into that buffer, otherwise the result is
69 char *output_dyn_value(char *buf
, int buf_size
, int value
,
70 const unsigned char **units
, bool bin_scale
)
72 int scale
= bin_scale
? 1024 : 1000;
78 while (value
>= scale
)
80 fraction
= value
% scale
;
85 fraction
= fraction
* 1000 / 1024;
87 if (value
>= 100 || !unit_no
)
90 snprintf(tbuf
, sizeof(tbuf
), "%01d", fraction
/ 100);
92 snprintf(tbuf
, sizeof(tbuf
), "%02d", fraction
/ 10);
97 snprintf(buf
, buf_size
, "%d%s%s%s", value
, str(LANG_POINT
),
98 tbuf
, P2STR(units
[unit_no
]));
100 snprintf(buf
, buf_size
, "%d%s", value
, P2STR(units
[unit_no
]));
104 /* strip trailing zeros from the fraction */
105 for (i
= strlen(tbuf
) - 1; (i
>= 0) && (tbuf
[i
] == '0'); i
--)
108 talk_number(value
, true);
111 talk_id(LANG_POINT
, true);
112 talk_spell(tbuf
, true);
114 talk_id(P2ID(units
[unit_no
]), true);
119 /* Create a filename with a number part in a way that the number is 1
120 * higher than the highest numbered file matching the same pattern.
121 * It is allowed that buffer and path point to the same memory location,
122 * saving a strcpy(). Path must always be given without trailing slash.
123 * "num" can point to an int specifying the number to use or NULL or a value
124 * less than zero to number automatically. The final number used will also
125 * be returned in *num. If *num is >= 0 then *num will be incremented by
127 char *create_numbered_filename(char *buffer
, const char *path
,
128 const char *prefix
, const char *suffix
,
129 int numberlen
IF_CNFN_NUM_(, int *num
))
132 struct dirent
*entry
;
135 int prefixlen
= strlen(prefix
);
139 strncpy(buffer
, path
, MAX_PATH
);
141 pathlen
= strlen(buffer
);
144 if (num
&& *num
>= 0)
146 /* number specified */
152 /* automatic numbering */
155 dir
= opendir(pathlen
? buffer
: "/");
159 while ((entry
= readdir(dir
)))
163 if (strncasecmp((char *)entry
->d_name
, prefix
, prefixlen
)
164 || strcasecmp((char *)entry
->d_name
+ prefixlen
+ numberlen
, suffix
))
167 curr_num
= atoi((char *)entry
->d_name
+ prefixlen
);
168 if (curr_num
> max_num
)
177 snprintf(fmtstring
, sizeof(fmtstring
), "/%%s%%0%dd%%s", numberlen
);
178 snprintf(buffer
+ pathlen
, MAX_PATH
- pathlen
, fmtstring
, prefix
,
190 /* Create a filename with a date+time part.
191 It is allowed that buffer and path point to the same memory location,
192 saving a strcpy(). Path must always be given without trailing slash.
193 unique_time as true makes the function wait until the current time has
195 char *create_datetime_filename(char *buffer
, const char *path
,
196 const char *prefix
, const char *suffix
,
199 struct tm
*tm
= get_time();
200 static struct tm last_tm
;
203 while (unique_time
&& !memcmp(get_time(), &last_tm
, sizeof (struct tm
)))
209 strncpy(buffer
, path
, MAX_PATH
);
211 pathlen
= strlen(buffer
);
212 snprintf(buffer
+ pathlen
, MAX_PATH
- pathlen
,
213 "/%s%02d%02d%02d-%02d%02d%02d%s", prefix
,
214 tm
->tm_year
% 100, tm
->tm_mon
+ 1, tm
->tm_mday
,
215 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
, suffix
);
219 #endif /* CONFIG_RTC */
221 /* Read (up to) a line of text from fd into buffer and return number of bytes
222 * read (which may be larger than the number of bytes stored in buffer). If
223 * an error occurs, -1 is returned (and buffer contains whatever could be
224 * read). A line is terminated by a LF char. Neither LF nor CR chars are
227 int read_line(int fd
, char* buffer
, int buffer_size
)
234 while (count
< buffer_size
)
238 if (1 != read(fd
, &c
, 1))
252 buffer
[MIN(count
, buffer_size
- 1)] = 0;
254 return errno
? -1 : num_read
;
257 /* Performance optimized version of the previous function. */
258 int fast_readline(int fd
, char *buf
, int buf_size
, void *parameters
,
259 int (*callback
)(int n
, const char *buf
, void *parameters
))
269 rc
= read(fd
, &buf
[pos
], buf_size
- pos
- 1);
273 if ( (p
= strchr(buf
, '\r')) != NULL
)
281 if ( (p
= strchr(p
, '\n')) != NULL
)
287 rc
= callback(count
, buf
, parameters
);
294 pos
= buf_size
- ((long)next
- (long)buf
) - 1;
295 memmove(buf
, next
, pos
);
304 #ifdef HAVE_LCD_BITMAP
307 #define BMP_COMPRESSION 3 /* BI_BITFIELDS */
308 #define BMP_NUMCOLORS 3
310 #define BMP_COMPRESSION 0 /* BI_RGB */
312 #define BMP_NUMCOLORS (1 << LCD_DEPTH)
314 #define BMP_NUMCOLORS 0
320 #define BMP_LINESIZE ((LCD_WIDTH/8 + 3) & ~3)
323 #define BMP_LINESIZE ((LCD_WIDTH/2 + 3) & ~3)
326 #define BMP_LINESIZE ((LCD_WIDTH + 3) & ~3)
327 #elif LCD_DEPTH <= 16
329 #define BMP_LINESIZE ((LCD_WIDTH*2 + 3) & ~3)
332 #define BMP_LINESIZE ((LCD_WIDTH*3 + 3) & ~3)
335 #define BMP_HEADERSIZE (54 + 4 * BMP_NUMCOLORS)
336 #define BMP_DATASIZE (BMP_LINESIZE * LCD_HEIGHT)
337 #define BMP_TOTALSIZE (BMP_HEADERSIZE + BMP_DATASIZE)
339 #define LE16_CONST(x) (x)&0xff, ((x)>>8)&0xff
340 #define LE32_CONST(x) (x)&0xff, ((x)>>8)&0xff, ((x)>>16)&0xff, ((x)>>24)&0xff
342 static const unsigned char bmpheader
[] =
344 0x42, 0x4d, /* 'BM' */
345 LE32_CONST(BMP_TOTALSIZE
), /* Total file size */
346 0x00, 0x00, 0x00, 0x00, /* Reserved */
347 LE32_CONST(BMP_HEADERSIZE
), /* Offset to start of pixel data */
349 0x28, 0x00, 0x00, 0x00, /* Size of (2nd) header */
350 LE32_CONST(LCD_WIDTH
), /* Width in pixels */
351 LE32_CONST(LCD_HEIGHT
), /* Height in pixels */
352 0x01, 0x00, /* Number of planes (always 1) */
353 LE16_CONST(BMP_BPP
), /* Bits per pixel 1/4/8/16/24 */
354 LE32_CONST(BMP_COMPRESSION
),/* Compression mode */
355 LE32_CONST(BMP_DATASIZE
), /* Size of bitmap data */
356 0xc4, 0x0e, 0x00, 0x00, /* Horizontal resolution (pixels/meter) */
357 0xc4, 0x0e, 0x00, 0x00, /* Vertical resolution (pixels/meter) */
358 LE32_CONST(BMP_NUMCOLORS
), /* Number of used colours */
359 LE32_CONST(BMP_NUMCOLORS
), /* Number of important colours */
362 0x90, 0xee, 0x90, 0x00, /* Colour #0 */
363 0x00, 0x00, 0x00, 0x00 /* Colour #1 */
365 0xe6, 0xd8, 0xad, 0x00, /* Colour #0 */
366 0x99, 0x90, 0x73, 0x00, /* Colour #1 */
367 0x4c, 0x48, 0x39, 0x00, /* Colour #2 */
368 0x00, 0x00, 0x00, 0x00 /* Colour #3 */
369 #elif LCD_DEPTH == 16
370 0x00, 0xf8, 0x00, 0x00, /* red bitfield mask */
371 0xe0, 0x07, 0x00, 0x00, /* green bitfield mask */
372 0x1f, 0x00, 0x00, 0x00 /* blue bitfield mask */
376 static void (*screen_dump_hook
)(int fh
) = NULL
;
378 void screen_dump(void)
381 char filename
[MAX_PATH
];
384 static unsigned char line_block
[8][BMP_LINESIZE
];
386 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
387 static unsigned char line_block
[BMP_LINESIZE
];
389 static unsigned char line_block
[4][BMP_LINESIZE
];
391 #elif LCD_DEPTH == 16
392 static unsigned short line_block
[BMP_LINESIZE
/2];
396 create_datetime_filename(filename
, "", "dump ", ".bmp", false);
398 create_numbered_filename(filename
, "", "dump_", ".bmp", 4
399 IF_CNFN_NUM_(, NULL
));
402 fh
= creat(filename
, O_WRONLY
);
406 if (screen_dump_hook
)
408 screen_dump_hook(fh
);
412 write(fh
, bmpheader
, sizeof(bmpheader
));
414 /* BMP image goes bottom up */
416 for (by
= LCD_HEIGHT
/8 - 1; by
>= 0; by
--)
418 unsigned char *src
= &lcd_framebuffer
[by
][0];
419 unsigned char *dst
= &line_block
[0][0];
421 memset(line_block
, 0, sizeof(line_block
));
422 for (bx
= LCD_WIDTH
/8; bx
> 0; bx
--)
424 unsigned dst_mask
= 0x80;
427 for (ix
= 8; ix
> 0; ix
--)
429 unsigned char *dst_blk
= dst
;
430 unsigned src_byte
= *src
++;
433 for (iy
= 8; iy
> 0; iy
--)
436 *dst_blk
|= dst_mask
;
438 dst_blk
+= BMP_LINESIZE
;
445 write(fh
, line_block
, sizeof(line_block
));
448 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
449 for (by
= LCD_HEIGHT
- 1; by
>= 0; by
--)
451 unsigned char *src
= &lcd_framebuffer
[by
][0];
452 unsigned char *dst
= line_block
;
454 memset(line_block
, 0, sizeof(line_block
));
455 for (bx
= (LCD_WIDTH
+3)/4; bx
> 0; bx
--)
457 unsigned src_byte
= *src
++;
459 *dst
++ = ((src_byte
>> 2) & 0x30) | ((src_byte
>> 4) & 0x03);
460 *dst
++ = ((src_byte
<< 2) & 0x30) | (src_byte
& 0x03);
463 write(fh
, line_block
, sizeof(line_block
));
465 #else /* VERTICAL_PACKING */
466 for (by
= LCD_HEIGHT
/4 - 1; by
>= 0; by
--)
468 unsigned char *src
= &lcd_framebuffer
[by
][0];
469 unsigned char *dst
= &line_block
[3][0];
471 memset(line_block
, 0, sizeof(line_block
));
472 for (bx
= LCD_WIDTH
/2; bx
> 0; bx
--)
474 unsigned char *dst_blk
= dst
++;
475 unsigned src_byte0
= *src
++;
476 unsigned src_byte1
= *src
++;
479 for (iy
= 4; iy
> 0; iy
--)
481 *dst_blk
= ((src_byte0
& 3) << 4) | (src_byte1
& 3);
484 dst_blk
-= BMP_LINESIZE
;
488 write(fh
, line_block
, sizeof(line_block
));
491 #elif LCD_DEPTH == 16
492 for (by
= LCD_HEIGHT
- 1; by
>= 0; by
--)
494 unsigned short *src
= &lcd_framebuffer
[by
][0];
495 unsigned short *dst
= line_block
;
497 memset(line_block
, 0, sizeof(line_block
));
498 for (bx
= LCD_WIDTH
; bx
> 0; bx
--)
500 #if (LCD_PIXELFORMAT == RGB565SWAPPED)
501 /* iPod LCD data is big endian although the CPU is not */
502 *dst
++ = htobe16(*src
++);
504 *dst
++ = htole16(*src
++);
508 write(fh
, line_block
, sizeof(line_block
));
510 #endif /* LCD_DEPTH */
516 void screen_dump_set_hook(void (*hook
)(int fh
))
518 screen_dump_hook
= hook
;
521 #endif /* HAVE_LCD_BITMAP */
523 /* parse a line from a configuration file. the line format is:
527 Any whitespace before setting name or value (after ':') is ignored.
528 A # as first non-whitespace character discards the whole line.
529 Function sets pointers to null-terminated setting name and value.
530 Returns false if no valid config entry was found.
533 bool settings_parseline(char* line
, char** name
, char** value
)
537 while ( isspace(*line
) )
543 ptr
= strchr(line
, ':');
550 while (isspace(*ptr
))
556 static void system_flush(void)
558 call_ata_idle_notifys(false); /*doesnt work on usb and shutdown from ata thread */
562 static void system_restore(void)
567 static bool clean_shutdown(void (*callback
)(void *), void *parameter
)
572 call_ata_idle_notifys(false);
577 #if defined(CONFIG_CHARGING) && !defined(HAVE_POWEROFF_WHILE_CHARGING)
578 if(!charger_inserted())
582 screens
[i
].clear_display();
583 #ifdef X5_BACKLIGHT_SHUTDOWN
584 x5_backlight_shutdown();
586 if (!battery_level_safe())
587 gui_syncsplash(3*HZ
, true, "%s %s",
588 str(LANG_WARNING_BATTERY_EMPTY
),
589 str(LANG_SHUTTINGDOWN
));
590 else if (battery_level_critical())
591 gui_syncsplash(3*HZ
, true, "%s %s",
592 str(LANG_WARNING_BATTERY_LOW
),
593 str(LANG_SHUTTINGDOWN
));
596 if (!tagcache_prepare_shutdown())
599 gui_syncsplash(HZ
, true, str(LANG_TAGCACHE_BUSY
));
603 gui_syncsplash(0, true, str(LANG_SHUTTINGDOWN
));
606 if (global_settings
.fade_on_stop
607 && (audio_status() & AUDIO_STATUS_PLAY
))
613 while (audio_status())
616 if (callback
!= NULL
)
619 if (!battery_level_critical()) /* do not save on critical battery */
621 #ifdef HAVE_EEPROM_SETTINGS
622 if (firmware_settings
.initialized
)
624 firmware_settings
.disk_clean
= true;
625 firmware_settings
.bl_version
= 0;
626 eeprom_settings_store();
635 #ifdef CONFIG_CHARGING
636 static bool waiting_to_resume_play
= false;
637 static long play_resume_tick
;
639 static void car_adapter_mode_processing(bool inserted
)
641 if (global_settings
.car_adapter_mode
)
646 * Just got plugged in, delay & resume if we were playing
648 if (audio_status() & AUDIO_STATUS_PAUSE
)
650 /* delay resume a bit while the engine is cranking */
651 play_resume_tick
= current_tick
+ HZ
*5;
652 waiting_to_resume_play
= true;
658 * Just got unplugged, pause if playing
660 if ((audio_status() & AUDIO_STATUS_PLAY
) &&
661 !(audio_status() & AUDIO_STATUS_PAUSE
))
669 static void car_adapter_tick(void)
671 if (waiting_to_resume_play
)
673 if (TIME_AFTER(current_tick
, play_resume_tick
))
675 if (audio_status() & AUDIO_STATUS_PAUSE
)
679 waiting_to_resume_play
= false;
684 void car_adapter_mode_init(void)
686 tick_add_task(car_adapter_tick
);
690 #ifdef HAVE_HEADPHONE_DETECTION
691 void unplug_change(bool inserted
)
693 if (global_settings
.unplug_mode
)
697 if ( global_settings
.unplug_mode
> 1 )
703 if (global_settings
.unplug_rw
)
705 if ( audio_current_track()->elapsed
>
706 (unsigned long)(global_settings
.unplug_rw
*1000))
707 audio_ff_rewind(audio_current_track()->elapsed
-
708 (global_settings
.unplug_rw
*1000));
717 long default_event_handler_ex(long event
, void (*callback
)(void *), void *parameter
)
721 case SYS_USB_CONNECTED
:
722 if (callback
!= NULL
)
725 if (!mmc_touched() || (mmc_remove_request() == SYS_MMC_EXTRACTED
))
728 scrobbler_flush_cache();
733 return SYS_USB_CONNECTED
;
735 if (!clean_shutdown(callback
, parameter
))
738 #ifdef CONFIG_CHARGING
739 case SYS_CHARGER_CONNECTED
:
740 car_adapter_mode_processing(true);
741 return SYS_CHARGER_CONNECTED
;
743 case SYS_CHARGER_DISCONNECTED
:
744 car_adapter_mode_processing(false);
745 return SYS_CHARGER_DISCONNECTED
;
747 #ifdef HAVE_HEADPHONE_DETECTION
748 case SYS_PHONE_PLUGGED
:
750 return SYS_PHONE_PLUGGED
;
752 case SYS_PHONE_UNPLUGGED
:
753 unplug_change(false);
754 return SYS_PHONE_UNPLUGGED
;
760 long default_event_handler(long event
)
762 return default_event_handler_ex(event
, NULL
, NULL
);
765 int show_logo( void )
767 #ifdef HAVE_LCD_BITMAP
772 lcd_bitmap(rockboxlogo
, 0, 10, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
774 #ifdef HAVE_REMOTE_LCD
775 lcd_remote_clear_display();
776 lcd_remote_bitmap(remote_rockboxlogo
, 0, 10, BMPWIDTH_remote_rockboxlogo
,
777 BMPHEIGHT_remote_rockboxlogo
);
780 snprintf(version
, sizeof(version
), "Ver. %s", appsversion
);
781 lcd_setfont(FONT_SYSFIXED
);
782 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
783 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
784 LCD_HEIGHT
-font_h
, (unsigned char *)version
);
787 #ifdef HAVE_REMOTE_LCD
788 lcd_remote_setfont(FONT_SYSFIXED
);
789 lcd_remote_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
790 lcd_remote_putsxy((LCD_REMOTE_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
791 LCD_REMOTE_HEIGHT
-font_h
, (unsigned char *)version
);
796 char *rockbox
= " ROCKbox!";
798 lcd_double_height(true);
799 lcd_puts(0, 0, rockbox
);
800 lcd_puts(0, 1, appsversion
);
806 #if CONFIG_CODEC == SWCODEC
807 int get_replaygain_mode(bool have_track_gain
, bool have_album_gain
)
811 bool track
= ((global_settings
.replaygain_type
== REPLAYGAIN_TRACK
)
812 || ((global_settings
.replaygain_type
== REPLAYGAIN_SHUFFLE
)
813 && global_settings
.playlist_shuffle
));
815 type
= (!track
&& have_album_gain
) ? REPLAYGAIN_ALBUM
816 : have_track_gain
? REPLAYGAIN_TRACK
: -1;