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"
52 #ifdef HAVE_LCD_BITMAP
55 #endif /* End HAVE_LCD_BITMAP */
57 /* Format a large-range value for output, using the appropriate unit so that
58 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
59 * units) if possible, and 3 significant digits are shown. If a buffer is
60 * given, the result is snprintf()'d into that buffer, otherwise the result is
62 char *output_dyn_value(char *buf
, int buf_size
, int value
,
63 const unsigned char **units
, bool bin_scale
)
65 int scale
= bin_scale
? 1024 : 1000;
71 while (value
>= scale
)
73 fraction
= value
% scale
;
78 fraction
= fraction
* 1000 / 1024;
80 if (value
>= 100 || !unit_no
)
83 snprintf(tbuf
, sizeof(tbuf
), "%01d", fraction
/ 100);
85 snprintf(tbuf
, sizeof(tbuf
), "%02d", fraction
/ 10);
90 snprintf(buf
, buf_size
, "%d%s%s%s", value
, str(LANG_POINT
),
91 tbuf
, P2STR(units
[unit_no
]));
93 snprintf(buf
, buf_size
, "%d%s", value
, P2STR(units
[unit_no
]));
97 /* strip trailing zeros from the fraction */
98 for (i
= strlen(tbuf
) - 1; (i
>= 0) && (tbuf
[i
] == '0'); i
--)
101 talk_number(value
, true);
104 talk_id(LANG_POINT
, true);
105 talk_spell(tbuf
, true);
107 talk_id(P2ID(units
[unit_no
]), true);
112 /* Create a filename with a number part in a way that the number is 1
113 higher than the highest numbered file matching the same pattern.
114 It is allowed that buffer and path point to the same memory location,
115 saving a strcpy(). Path must always be given without trailing slash,. */
116 char *create_numbered_filename(char *buffer
, const char *path
,
117 const char *prefix
, const char *suffix
,
121 struct dirent
*entry
;
124 int prefixlen
= strlen(prefix
);
128 strncpy(buffer
, path
, MAX_PATH
);
130 pathlen
= strlen(buffer
);
132 dir
= opendir(pathlen
? buffer
: "/");
136 while ((entry
= readdir(dir
)))
140 if (strncasecmp((char *)entry
->d_name
, prefix
, prefixlen
)
141 || strcasecmp((char *)entry
->d_name
+ prefixlen
+ numberlen
, suffix
))
144 curr_num
= atoi((char *)entry
->d_name
+ prefixlen
);
145 if (curr_num
> max_num
)
150 snprintf(fmtstring
, sizeof(fmtstring
), "/%%s%%0%dd%%s", numberlen
);
151 snprintf(buffer
+ pathlen
, MAX_PATH
- pathlen
, fmtstring
, prefix
,
152 max_num
+ 1, suffix
);
158 /* Create a filename with a date+time part.
159 It is allowed that buffer and path point to the same memory location,
160 saving a strcpy(). Path must always be given without trailing slash. */
161 char *create_datetime_filename(char *buffer
, const char *path
,
162 const char *prefix
, const char *suffix
)
164 struct tm
*tm
= get_time();
168 strncpy(buffer
, path
, MAX_PATH
);
170 pathlen
= strlen(buffer
);
171 snprintf(buffer
+ pathlen
, MAX_PATH
- pathlen
,
172 "/%s%02d%02d%02d-%02d%02d%02d%s", prefix
,
173 tm
->tm_year
% 100, tm
->tm_mon
+ 1, tm
->tm_mday
,
174 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
, suffix
);
178 #endif /* CONFIG_RTC */
180 /* Read (up to) a line of text from fd into buffer and return number of bytes
181 * read (which may be larger than the number of bytes stored in buffer). If
182 * an error occurs, -1 is returned (and buffer contains whatever could be
183 * read). A line is terminated by a LF char. Neither LF nor CR chars are
186 int read_line(int fd
, char* buffer
, int buffer_size
)
193 while (count
< buffer_size
)
197 if (1 != read(fd
, &c
, 1))
211 buffer
[MIN(count
, buffer_size
- 1)] = 0;
213 return errno
? -1 : num_read
;
216 #ifdef HAVE_LCD_BITMAP
219 #define BMP_COMPRESSION 3 /* BI_BITFIELDS */
220 #define BMP_NUMCOLORS 3
222 #define BMP_COMPRESSION 0 /* BI_RGB */
224 #define BMP_NUMCOLORS (1 << LCD_DEPTH)
226 #define BMP_NUMCOLORS 0
232 #define BMP_LINESIZE ((LCD_WIDTH/8 + 3) & ~3)
235 #define BMP_LINESIZE ((LCD_WIDTH/2 + 3) & ~3)
238 #define BMP_LINESIZE ((LCD_WIDTH + 3) & ~3)
239 #elif LCD_DEPTH <= 16
241 #define BMP_LINESIZE ((LCD_WIDTH*2 + 3) & ~3)
244 #define BMP_LINESIZE ((LCD_WIDTH*3 + 3) & ~3)
247 #define BMP_HEADERSIZE (54 + 4 * BMP_NUMCOLORS)
248 #define BMP_DATASIZE (BMP_LINESIZE * LCD_HEIGHT)
249 #define BMP_TOTALSIZE (BMP_HEADERSIZE + BMP_DATASIZE)
251 #define LE16_CONST(x) (x)&0xff, ((x)>>8)&0xff
252 #define LE32_CONST(x) (x)&0xff, ((x)>>8)&0xff, ((x)>>16)&0xff, ((x)>>24)&0xff
254 static const unsigned char bmpheader
[] =
256 0x42, 0x4d, /* 'BM' */
257 LE32_CONST(BMP_TOTALSIZE
), /* Total file size */
258 0x00, 0x00, 0x00, 0x00, /* Reserved */
259 LE32_CONST(BMP_HEADERSIZE
), /* Offset to start of pixel data */
261 0x28, 0x00, 0x00, 0x00, /* Size of (2nd) header */
262 LE32_CONST(LCD_WIDTH
), /* Width in pixels */
263 LE32_CONST(LCD_HEIGHT
), /* Height in pixels */
264 0x01, 0x00, /* Number of planes (always 1) */
265 LE16_CONST(BMP_BPP
), /* Bits per pixel 1/4/8/16/24 */
266 LE32_CONST(BMP_COMPRESSION
),/* Compression mode */
267 LE32_CONST(BMP_DATASIZE
), /* Size of bitmap data */
268 0xc4, 0x0e, 0x00, 0x00, /* Horizontal resolution (pixels/meter) */
269 0xc4, 0x0e, 0x00, 0x00, /* Vertical resolution (pixels/meter) */
270 LE32_CONST(BMP_NUMCOLORS
), /* Number of used colours */
271 LE32_CONST(BMP_NUMCOLORS
), /* Number of important colours */
274 0x90, 0xee, 0x90, 0x00, /* Colour #0 */
275 0x00, 0x00, 0x00, 0x00 /* Colour #1 */
277 0xe6, 0xd8, 0xad, 0x00, /* Colour #0 */
278 0x99, 0x90, 0x73, 0x00, /* Colour #1 */
279 0x4c, 0x48, 0x39, 0x00, /* Colour #2 */
280 0x00, 0x00, 0x00, 0x00 /* Colour #3 */
281 #elif LCD_DEPTH == 16
282 0x00, 0xf8, 0x00, 0x00, /* red bitfield mask */
283 0xe0, 0x07, 0x00, 0x00, /* green bitfield mask */
284 0x1f, 0x00, 0x00, 0x00 /* blue bitfield mask */
288 static void (*screen_dump_hook
)(int fh
) = NULL
;
290 void screen_dump(void)
293 char filename
[MAX_PATH
];
296 static unsigned char line_block
[8][BMP_LINESIZE
];
298 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
299 static unsigned char line_block
[BMP_LINESIZE
];
301 static unsigned char line_block
[4][BMP_LINESIZE
];
303 #elif LCD_DEPTH == 16
304 static unsigned short line_block
[BMP_LINESIZE
/2];
308 create_datetime_filename(filename
, "", "dump ", ".bmp");
310 create_numbered_filename(filename
, "", "dump_", ".bmp", 4);
313 fh
= creat(filename
, O_WRONLY
);
317 if (screen_dump_hook
)
319 screen_dump_hook(fh
);
323 write(fh
, bmpheader
, sizeof(bmpheader
));
325 /* BMP image goes bottom up */
327 for (by
= LCD_HEIGHT
/8 - 1; by
>= 0; by
--)
329 unsigned char *src
= &lcd_framebuffer
[by
][0];
330 unsigned char *dst
= &line_block
[0][0];
332 memset(line_block
, 0, sizeof(line_block
));
333 for (bx
= LCD_WIDTH
/8; bx
> 0; bx
--)
335 unsigned dst_mask
= 0x80;
338 for (ix
= 8; ix
> 0; ix
--)
340 unsigned char *dst_blk
= dst
;
341 unsigned src_byte
= *src
++;
344 for (iy
= 8; iy
> 0; iy
--)
347 *dst_blk
|= dst_mask
;
349 dst_blk
+= BMP_LINESIZE
;
356 write(fh
, line_block
, sizeof(line_block
));
359 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
360 for (by
= LCD_HEIGHT
- 1; by
>= 0; by
--)
362 unsigned char *src
= &lcd_framebuffer
[by
][0];
363 unsigned char *dst
= line_block
;
365 memset(line_block
, 0, sizeof(line_block
));
366 for (bx
= LCD_WIDTH
/4; bx
> 0; bx
--)
368 unsigned src_byte
= *src
++;
373 *dst
++ = (tmp
<< 4) | (src_byte
& 3);
377 *dst
++ = (tmp
<< 4) | (src_byte
& 3);
380 write(fh
, line_block
, sizeof(line_block
));
382 #else /* VERTICAL_PACKING */
383 for (by
= LCD_HEIGHT
/4 - 1; by
>= 0; by
--)
385 unsigned char *src
= &lcd_framebuffer
[by
][0];
386 unsigned char *dst
= &line_block
[3][0];
388 memset(line_block
, 0, sizeof(line_block
));
389 for (bx
= LCD_WIDTH
/2; bx
> 0; bx
--)
391 unsigned char *dst_blk
= dst
++;
392 unsigned src_byte0
= *src
++;
393 unsigned src_byte1
= *src
++;
396 for (iy
= 4; iy
> 0; iy
--)
398 *dst_blk
= ((src_byte0
& 3) << 4) | (src_byte1
& 3);
401 dst_blk
-= BMP_LINESIZE
;
405 write(fh
, line_block
, sizeof(line_block
));
408 #elif LCD_DEPTH == 16
409 for (by
= LCD_HEIGHT
- 1; by
>= 0; by
--)
411 unsigned short *src
= &lcd_framebuffer
[by
][0];
412 unsigned short *dst
= line_block
;
414 memset(line_block
, 0, sizeof(line_block
));
415 for (bx
= LCD_WIDTH
; bx
> 0; bx
--)
417 #if (LCD_PIXELFORMAT == RGB565SWAPPED)
418 /* iPod LCD data is big endian although the CPU is not */
419 *dst
++ = htobe16(*src
++);
421 *dst
++ = htole16(*src
++);
425 write(fh
, line_block
, sizeof(line_block
));
427 #endif /* LCD_DEPTH */
433 void screen_dump_set_hook(void (*hook
)(int fh
))
435 screen_dump_hook
= hook
;
438 #endif /* HAVE_LCD_BITMAP */
440 /* parse a line from a configuration file. the line format is:
444 Any whitespace before setting name or value (after ':') is ignored.
445 A # as first non-whitespace character discards the whole line.
446 Function sets pointers to null-terminated setting name and value.
447 Returns false if no valid config entry was found.
450 bool settings_parseline(char* line
, char** name
, char** value
)
454 while ( isspace(*line
) )
460 ptr
= strchr(line
, ':');
467 while (isspace(*ptr
))
473 static void system_flush(void)
478 static void system_restore(void)
483 static bool clean_shutdown(void (*callback
)(void *), void *parameter
)
490 #if defined(HAVE_CHARGING) && !defined(HAVE_POWEROFF_WHILE_CHARGING)
491 if(!charger_inserted())
495 gui_syncsplash(0, true, str(LANG_SHUTTINGDOWN
));
496 if (callback
!= NULL
)
508 static bool waiting_to_resume_play
= false;
509 static long play_resume_tick
;
511 static void car_adapter_mode_processing(bool inserted
)
513 if (global_settings
.car_adapter_mode
)
518 * Just got plugged in, delay & resume if we were playing
520 if (audio_status() & AUDIO_STATUS_PAUSE
)
522 /* delay resume a bit while the engine is cranking */
523 play_resume_tick
= current_tick
+ HZ
*5;
524 waiting_to_resume_play
= true;
530 * Just got unplugged, pause if playing
532 if ((audio_status() & AUDIO_STATUS_PLAY
) &&
533 !(audio_status() & AUDIO_STATUS_PAUSE
))
541 static void car_adapter_tick(void)
543 if (waiting_to_resume_play
)
545 if (TIME_AFTER(current_tick
, play_resume_tick
))
547 if (audio_status() & AUDIO_STATUS_PAUSE
)
551 waiting_to_resume_play
= false;
556 void car_adapter_mode_init(void)
558 tick_add_task(car_adapter_tick
);
562 long default_event_handler_ex(long event
, void (*callback
)(void *), void *parameter
)
566 case SYS_USB_CONNECTED
:
567 if (callback
!= NULL
)
570 if (!mmc_touched() || (mmc_remove_request() == SYS_MMC_EXTRACTED
))
577 return SYS_USB_CONNECTED
;
579 if (!clean_shutdown(callback
, parameter
))
583 case SYS_CHARGER_CONNECTED
:
584 car_adapter_mode_processing(true);
585 return SYS_CHARGER_CONNECTED
;
587 case SYS_CHARGER_DISCONNECTED
:
588 car_adapter_mode_processing(false);
589 return SYS_CHARGER_DISCONNECTED
;
595 long default_event_handler(long event
)
597 return default_event_handler_ex(event
, NULL
, NULL
);
600 int show_logo( void )
602 #ifdef HAVE_LCD_BITMAP
607 lcd_bitmap(rockboxlogo
, 0, 10, BMPWIDTH_rockboxlogo
, BMPHEIGHT_rockboxlogo
);
609 #ifdef HAVE_REMOTE_LCD
610 lcd_remote_clear_display();
611 lcd_remote_bitmap(remote_rockboxlogo
,10,14,BMPWIDTH_remote_rockboxlogo
,
612 BMPHEIGHT_remote_rockboxlogo
);
615 snprintf(version
, sizeof(version
), "Ver. %s", appsversion
);
616 lcd_setfont(FONT_SYSFIXED
);
617 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
618 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
619 LCD_HEIGHT
-font_h
, (unsigned char *)version
);
622 #ifdef HAVE_REMOTE_LCD
623 lcd_remote_setfont(FONT_SYSFIXED
);
624 lcd_remote_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
625 lcd_remote_putsxy((LCD_REMOTE_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
626 LCD_REMOTE_HEIGHT
-font_h
, (unsigned char *)version
);
631 char *rockbox
= " ROCKbox!";
633 lcd_double_height(true);
634 lcd_puts(0, 0, rockbox
);
635 lcd_puts(0, 1, appsversion
);