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"
41 #include "powermgmt.h"
42 #include "backlight.h"
48 #include "scrobbler.h"
53 #include "eeprom_settings.h"
55 #ifdef HAVE_LCD_BITMAP
58 #endif /* End HAVE_LCD_BITMAP */
59 #include "gui/gwps-common.h"
61 /* Format a large-range value for output, using the appropriate unit so that
62 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
63 * units) if possible, and 3 significant digits are shown. If a buffer is
64 * given, the result is snprintf()'d into that buffer, otherwise the result is
66 char *output_dyn_value(char *buf
, int buf_size
, int value
,
67 const unsigned char **units
, bool bin_scale
)
69 int scale
= bin_scale
? 1024 : 1000;
75 while (value
>= scale
)
77 fraction
= value
% scale
;
82 fraction
= fraction
* 1000 / 1024;
84 if (value
>= 100 || !unit_no
)
87 snprintf(tbuf
, sizeof(tbuf
), "%01d", fraction
/ 100);
89 snprintf(tbuf
, sizeof(tbuf
), "%02d", fraction
/ 10);
94 snprintf(buf
, buf_size
, "%d%s%s%s", value
, str(LANG_POINT
),
95 tbuf
, P2STR(units
[unit_no
]));
97 snprintf(buf
, buf_size
, "%d%s", value
, P2STR(units
[unit_no
]));
101 /* strip trailing zeros from the fraction */
102 for (i
= strlen(tbuf
) - 1; (i
>= 0) && (tbuf
[i
] == '0'); i
--)
105 talk_number(value
, true);
108 talk_id(LANG_POINT
, true);
109 talk_spell(tbuf
, true);
111 talk_id(P2ID(units
[unit_no
]), true);
116 /* Create a filename with a number part in a way that the number is 1
117 higher than the highest numbered file matching the same pattern.
118 It is allowed that buffer and path point to the same memory location,
119 saving a strcpy(). Path must always be given without trailing slash,. */
120 char *create_numbered_filename(char *buffer
, const char *path
,
121 const char *prefix
, const char *suffix
,
125 struct dirent
*entry
;
128 int prefixlen
= strlen(prefix
);
132 strncpy(buffer
, path
, MAX_PATH
);
134 pathlen
= strlen(buffer
);
136 dir
= opendir(pathlen
? buffer
: "/");
140 while ((entry
= readdir(dir
)))
144 if (strncasecmp((char *)entry
->d_name
, prefix
, prefixlen
)
145 || strcasecmp((char *)entry
->d_name
+ prefixlen
+ numberlen
, suffix
))
148 curr_num
= atoi((char *)entry
->d_name
+ prefixlen
);
149 if (curr_num
> max_num
)
154 snprintf(fmtstring
, sizeof(fmtstring
), "/%%s%%0%dd%%s", numberlen
);
155 snprintf(buffer
+ pathlen
, MAX_PATH
- pathlen
, fmtstring
, prefix
,
156 max_num
+ 1, suffix
);
162 /* Create a filename with a date+time part.
163 It is allowed that buffer and path point to the same memory location,
164 saving a strcpy(). Path must always be given without trailing slash. */
165 char *create_datetime_filename(char *buffer
, const char *path
,
166 const char *prefix
, const char *suffix
)
168 struct tm
*tm
= get_time();
172 strncpy(buffer
, path
, MAX_PATH
);
174 pathlen
= strlen(buffer
);
175 snprintf(buffer
+ pathlen
, MAX_PATH
- pathlen
,
176 "/%s%02d%02d%02d-%02d%02d%02d%s", prefix
,
177 tm
->tm_year
% 100, tm
->tm_mon
+ 1, tm
->tm_mday
,
178 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
, suffix
);
182 #endif /* CONFIG_RTC */
184 /* Read (up to) a line of text from fd into buffer and return number of bytes
185 * read (which may be larger than the number of bytes stored in buffer). If
186 * an error occurs, -1 is returned (and buffer contains whatever could be
187 * read). A line is terminated by a LF char. Neither LF nor CR chars are
190 int read_line(int fd
, char* buffer
, int buffer_size
)
197 while (count
< buffer_size
)
201 if (1 != read(fd
, &c
, 1))
215 buffer
[MIN(count
, buffer_size
- 1)] = 0;
217 return errno
? -1 : num_read
;
220 /* Performance optimized version of the previous function. */
221 int fast_readline(int fd
, char *buf
, int buf_size
, void *parameters
,
222 int (*callback
)(int n
, const char *buf
, void *parameters
))
232 rc
= read(fd
, &buf
[pos
], buf_size
- pos
- 1);
236 if ( (p
= strchr(buf
, '\r')) != NULL
)
244 if ( (p
= strchr(p
, '\n')) != NULL
)
250 rc
= callback(count
, buf
, parameters
);
257 pos
= buf_size
- ((long)next
- (long)buf
) - 1;
258 memmove(buf
, next
, pos
);
267 #ifdef HAVE_LCD_BITMAP
270 #define BMP_COMPRESSION 3 /* BI_BITFIELDS */
271 #define BMP_NUMCOLORS 3
273 #define BMP_COMPRESSION 0 /* BI_RGB */
275 #define BMP_NUMCOLORS (1 << LCD_DEPTH)
277 #define BMP_NUMCOLORS 0
283 #define BMP_LINESIZE ((LCD_WIDTH/8 + 3) & ~3)
286 #define BMP_LINESIZE ((LCD_WIDTH/2 + 3) & ~3)
289 #define BMP_LINESIZE ((LCD_WIDTH + 3) & ~3)
290 #elif LCD_DEPTH <= 16
292 #define BMP_LINESIZE ((LCD_WIDTH*2 + 3) & ~3)
295 #define BMP_LINESIZE ((LCD_WIDTH*3 + 3) & ~3)
298 #define BMP_HEADERSIZE (54 + 4 * BMP_NUMCOLORS)
299 #define BMP_DATASIZE (BMP_LINESIZE * LCD_HEIGHT)
300 #define BMP_TOTALSIZE (BMP_HEADERSIZE + BMP_DATASIZE)
302 #define LE16_CONST(x) (x)&0xff, ((x)>>8)&0xff
303 #define LE32_CONST(x) (x)&0xff, ((x)>>8)&0xff, ((x)>>16)&0xff, ((x)>>24)&0xff
305 static const unsigned char bmpheader
[] =
307 0x42, 0x4d, /* 'BM' */
308 LE32_CONST(BMP_TOTALSIZE
), /* Total file size */
309 0x00, 0x00, 0x00, 0x00, /* Reserved */
310 LE32_CONST(BMP_HEADERSIZE
), /* Offset to start of pixel data */
312 0x28, 0x00, 0x00, 0x00, /* Size of (2nd) header */
313 LE32_CONST(LCD_WIDTH
), /* Width in pixels */
314 LE32_CONST(LCD_HEIGHT
), /* Height in pixels */
315 0x01, 0x00, /* Number of planes (always 1) */
316 LE16_CONST(BMP_BPP
), /* Bits per pixel 1/4/8/16/24 */
317 LE32_CONST(BMP_COMPRESSION
),/* Compression mode */
318 LE32_CONST(BMP_DATASIZE
), /* Size of bitmap data */
319 0xc4, 0x0e, 0x00, 0x00, /* Horizontal resolution (pixels/meter) */
320 0xc4, 0x0e, 0x00, 0x00, /* Vertical resolution (pixels/meter) */
321 LE32_CONST(BMP_NUMCOLORS
), /* Number of used colours */
322 LE32_CONST(BMP_NUMCOLORS
), /* Number of important colours */
325 0x90, 0xee, 0x90, 0x00, /* Colour #0 */
326 0x00, 0x00, 0x00, 0x00 /* Colour #1 */
328 0xe6, 0xd8, 0xad, 0x00, /* Colour #0 */
329 0x99, 0x90, 0x73, 0x00, /* Colour #1 */
330 0x4c, 0x48, 0x39, 0x00, /* Colour #2 */
331 0x00, 0x00, 0x00, 0x00 /* Colour #3 */
332 #elif LCD_DEPTH == 16
333 0x00, 0xf8, 0x00, 0x00, /* red bitfield mask */
334 0xe0, 0x07, 0x00, 0x00, /* green bitfield mask */
335 0x1f, 0x00, 0x00, 0x00 /* blue bitfield mask */
339 static void (*screen_dump_hook
)(int fh
) = NULL
;
341 void screen_dump(void)
344 char filename
[MAX_PATH
];
347 static unsigned char line_block
[8][BMP_LINESIZE
];
349 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
350 static unsigned char line_block
[BMP_LINESIZE
];
352 static unsigned char line_block
[4][BMP_LINESIZE
];
354 #elif LCD_DEPTH == 16
355 static unsigned short line_block
[BMP_LINESIZE
/2];
359 create_datetime_filename(filename
, "", "dump ", ".bmp");
361 create_numbered_filename(filename
, "", "dump_", ".bmp", 4);
364 fh
= creat(filename
, O_WRONLY
);
368 if (screen_dump_hook
)
370 screen_dump_hook(fh
);
374 write(fh
, bmpheader
, sizeof(bmpheader
));
376 /* BMP image goes bottom up */
378 for (by
= LCD_HEIGHT
/8 - 1; by
>= 0; by
--)
380 unsigned char *src
= &lcd_framebuffer
[by
][0];
381 unsigned char *dst
= &line_block
[0][0];
383 memset(line_block
, 0, sizeof(line_block
));
384 for (bx
= LCD_WIDTH
/8; bx
> 0; bx
--)
386 unsigned dst_mask
= 0x80;
389 for (ix
= 8; ix
> 0; ix
--)
391 unsigned char *dst_blk
= dst
;
392 unsigned src_byte
= *src
++;
395 for (iy
= 8; iy
> 0; iy
--)
398 *dst_blk
|= dst_mask
;
400 dst_blk
+= BMP_LINESIZE
;
407 write(fh
, line_block
, sizeof(line_block
));
410 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
411 for (by
= LCD_HEIGHT
- 1; by
>= 0; by
--)
413 unsigned char *src
= &lcd_framebuffer
[by
][0];
414 unsigned char *dst
= line_block
;
416 memset(line_block
, 0, sizeof(line_block
));
417 for (bx
= (LCD_WIDTH
+3)/4; bx
> 0; bx
--)
419 unsigned src_byte
= *src
++;
421 *dst
++ = ((src_byte
>> 2) & 0x30) | ((src_byte
>> 4) & 0x03);
422 *dst
++ = ((src_byte
<< 2) & 0x30) | (src_byte
& 0x03);
425 write(fh
, line_block
, sizeof(line_block
));
427 #else /* VERTICAL_PACKING */
428 for (by
= LCD_HEIGHT
/4 - 1; by
>= 0; by
--)
430 unsigned char *src
= &lcd_framebuffer
[by
][0];
431 unsigned char *dst
= &line_block
[3][0];
433 memset(line_block
, 0, sizeof(line_block
));
434 for (bx
= LCD_WIDTH
/2; bx
> 0; bx
--)
436 unsigned char *dst_blk
= dst
++;
437 unsigned src_byte0
= *src
++;
438 unsigned src_byte1
= *src
++;
441 for (iy
= 4; iy
> 0; iy
--)
443 *dst_blk
= ((src_byte0
& 3) << 4) | (src_byte1
& 3);
446 dst_blk
-= BMP_LINESIZE
;
450 write(fh
, line_block
, sizeof(line_block
));
453 #elif LCD_DEPTH == 16
454 for (by
= LCD_HEIGHT
- 1; by
>= 0; by
--)
456 unsigned short *src
= &lcd_framebuffer
[by
][0];
457 unsigned short *dst
= line_block
;
459 memset(line_block
, 0, sizeof(line_block
));
460 for (bx
= LCD_WIDTH
; bx
> 0; bx
--)
462 #if (LCD_PIXELFORMAT == RGB565SWAPPED)
463 /* iPod LCD data is big endian although the CPU is not */
464 *dst
++ = htobe16(*src
++);
466 *dst
++ = htole16(*src
++);
470 write(fh
, line_block
, sizeof(line_block
));
472 #endif /* LCD_DEPTH */
478 void screen_dump_set_hook(void (*hook
)(int fh
))
480 screen_dump_hook
= hook
;
483 #endif /* HAVE_LCD_BITMAP */
485 /* parse a line from a configuration file. the line format is:
489 Any whitespace before setting name or value (after ':') is ignored.
490 A # as first non-whitespace character discards the whole line.
491 Function sets pointers to null-terminated setting name and value.
492 Returns false if no valid config entry was found.
495 bool settings_parseline(char* line
, char** name
, char** value
)
499 while ( isspace(*line
) )
505 ptr
= strchr(line
, ':');
512 while (isspace(*ptr
))
518 static void system_flush(void)
523 static void system_restore(void)
528 static bool clean_shutdown(void (*callback
)(void *), void *parameter
)
537 #if defined(CONFIG_CHARGING) && !defined(HAVE_POWEROFF_WHILE_CHARGING)
538 if(!charger_inserted())
542 screens
[i
].clear_display();
543 #ifdef X5_BACKLIGHT_SHUTDOWN
544 x5_backlight_shutdown();
547 if (!tagcache_prepare_shutdown())
550 gui_syncsplash(HZ
, true, str(LANG_TAGCACHE_BUSY
));
555 gui_syncsplash(0, true, str(LANG_SHUTTINGDOWN
));
557 if (global_settings
.fade_on_stop
558 && (audio_status() & AUDIO_STATUS_PLAY
))
564 while (audio_status())
567 if (callback
!= NULL
)
571 #ifdef HAVE_EEPROM_SETTINGS
572 if (firmware_settings
.initialized
)
574 firmware_settings
.disk_clean
= true;
575 firmware_settings
.bl_version
= 0;
576 eeprom_settings_store();
585 #ifdef CONFIG_CHARGING
586 static bool waiting_to_resume_play
= false;
587 static long play_resume_tick
;
589 static void car_adapter_mode_processing(bool inserted
)
591 if (global_settings
.car_adapter_mode
)
596 * Just got plugged in, delay & resume if we were playing
598 if (audio_status() & AUDIO_STATUS_PAUSE
)
600 /* delay resume a bit while the engine is cranking */
601 play_resume_tick
= current_tick
+ HZ
*5;
602 waiting_to_resume_play
= true;
608 * Just got unplugged, pause if playing
610 if ((audio_status() & AUDIO_STATUS_PLAY
) &&
611 !(audio_status() & AUDIO_STATUS_PAUSE
))
619 static void car_adapter_tick(void)
621 if (waiting_to_resume_play
)
623 if (TIME_AFTER(current_tick
, play_resume_tick
))
625 if (audio_status() & AUDIO_STATUS_PAUSE
)
629 waiting_to_resume_play
= false;
634 void car_adapter_mode_init(void)
636 tick_add_task(car_adapter_tick
);
640 #ifdef HAVE_HEADPHONE_DETECTION
641 void unplug_change(bool inserted
)
643 if (global_settings
.unplug_mode
)
647 if ( global_settings
.unplug_mode
> 1 )
653 if (global_settings
.unplug_rw
)
655 if ( audio_current_track()->elapsed
>
656 (unsigned long)(global_settings
.unplug_rw
*1000))
657 audio_ff_rewind(audio_current_track()->elapsed
-
658 (global_settings
.unplug_rw
*1000));
667 long default_event_handler_ex(long event
, void (*callback
)(void *), void *parameter
)
671 case SYS_USB_CONNECTED
:
672 if (callback
!= NULL
)
675 if (!mmc_touched() || (mmc_remove_request() == SYS_MMC_EXTRACTED
))
678 scrobbler_flush_cache();
683 return SYS_USB_CONNECTED
;
685 if (!clean_shutdown(callback
, parameter
))
688 #ifdef CONFIG_CHARGING
689 case SYS_CHARGER_CONNECTED
:
690 car_adapter_mode_processing(true);
691 return SYS_CHARGER_CONNECTED
;
693 case SYS_CHARGER_DISCONNECTED
:
694 car_adapter_mode_processing(false);
695 return SYS_CHARGER_DISCONNECTED
;
697 #ifdef HAVE_HEADPHONE_DETECTION
698 case SYS_PHONE_PLUGGED
:
700 return SYS_PHONE_PLUGGED
;
702 case SYS_PHONE_UNPLUGGED
:
703 unplug_change(false);
704 return SYS_PHONE_UNPLUGGED
;
710 long default_event_handler(long event
)
712 return default_event_handler_ex(event
, NULL
, NULL
);
715 int show_logo( void )
717 #ifdef HAVE_LCD_BITMAP
722 lcd_bitmap(rockboxlogo
, 0, 10, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
724 #ifdef HAVE_REMOTE_LCD
725 lcd_remote_clear_display();
726 lcd_remote_bitmap(remote_rockboxlogo
, 0, 10, BMPWIDTH_remote_rockboxlogo
,
727 BMPHEIGHT_remote_rockboxlogo
);
730 snprintf(version
, sizeof(version
), "Ver. %s", appsversion
);
731 lcd_setfont(FONT_SYSFIXED
);
732 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
733 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
734 LCD_HEIGHT
-font_h
, (unsigned char *)version
);
737 #ifdef HAVE_REMOTE_LCD
738 lcd_remote_setfont(FONT_SYSFIXED
);
739 lcd_remote_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
740 lcd_remote_putsxy((LCD_REMOTE_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
741 LCD_REMOTE_HEIGHT
-font_h
, (unsigned char *)version
);
746 char *rockbox
= " ROCKbox!";
748 lcd_double_height(true);
749 lcd_puts(0, 0, rockbox
);
750 lcd_puts(0, 1, appsversion
);