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 static unsigned char line_block
[4][BMP_LINESIZE
];
299 #elif LCD_DEPTH == 16
300 static unsigned short line_block
[BMP_LINESIZE
/2];
304 create_datetime_filename(filename
, "", "dump ", ".bmp");
306 create_numbered_filename(filename
, "", "dump_", ".bmp", 4);
309 fh
= creat(filename
, O_WRONLY
);
313 if (screen_dump_hook
)
315 screen_dump_hook(fh
);
319 write(fh
, bmpheader
, sizeof(bmpheader
));
321 /* BMP image goes bottom up */
323 for (by
= LCD_HEIGHT
/8 - 1; by
>= 0; by
--)
325 unsigned char *src
= &lcd_framebuffer
[by
][0];
326 unsigned char *dst
= &line_block
[0][0];
328 memset(line_block
, 0, sizeof(line_block
));
329 for (bx
= LCD_WIDTH
/8; bx
> 0; bx
--)
331 unsigned dst_mask
= 0x80;
334 for (ix
= 8; ix
> 0; ix
--)
336 unsigned char *dst_blk
= dst
;
337 unsigned src_byte
= *src
++;
340 for (iy
= 8; iy
> 0; iy
--)
343 *dst_blk
|= dst_mask
;
345 dst_blk
+= BMP_LINESIZE
;
352 write(fh
, line_block
, sizeof(line_block
));
355 for (by
= LCD_HEIGHT
/4 - 1; by
>= 0; by
--)
357 unsigned char *src
= &lcd_framebuffer
[by
][0];
358 unsigned char *dst
= &line_block
[3][0];
360 memset(line_block
, 0, sizeof(line_block
));
361 for (bx
= LCD_WIDTH
/2; bx
> 0; bx
--)
363 unsigned char *dst_blk
= dst
++;
364 unsigned src_byte0
= *src
++;
365 unsigned src_byte1
= *src
++;
368 for (iy
= 4; iy
> 0; iy
--)
370 *dst_blk
= ((src_byte0
& 3) << 4) | (src_byte1
& 3);
373 dst_blk
-= BMP_LINESIZE
;
377 write(fh
, line_block
, sizeof(line_block
));
379 #elif LCD_DEPTH == 16
380 for (by
= LCD_HEIGHT
- 1; by
>= 0; by
--)
382 unsigned short *src
= &lcd_framebuffer
[by
][0];
383 unsigned short *dst
= line_block
;
385 memset(line_block
, 0, sizeof(line_block
));
386 for (bx
= LCD_WIDTH
; bx
> 0; bx
--)
388 #if (LCD_PIXELFORMAT == RGB565SWAPPED)
389 /* iPod LCD data is big endian although the CPU is not */
390 *dst
++ = htobe16(*src
++);
392 *dst
++ = htole16(*src
++);
396 write(fh
, line_block
, sizeof(line_block
));
398 #endif /* LCD_DEPTH */
404 void screen_dump_set_hook(void (*hook
)(int fh
))
406 screen_dump_hook
= hook
;
409 #endif /* HAVE_LCD_BITMAP */
411 /* parse a line from a configuration file. the line format is:
415 Any whitespace before setting name or value (after ':') is ignored.
416 A # as first non-whitespace character discards the whole line.
417 Function sets pointers to null-terminated setting name and value.
418 Returns false if no valid config entry was found.
421 bool settings_parseline(char* line
, char** name
, char** value
)
425 while ( isspace(*line
) )
431 ptr
= strchr(line
, ':');
438 while (isspace(*ptr
))
444 static void system_flush(void)
449 static void system_restore(void)
454 static bool clean_shutdown(void (*callback
)(void *), void *parameter
)
461 #if defined(HAVE_CHARGING) && !defined(HAVE_POWEROFF_WHILE_CHARGING)
462 if(!charger_inserted())
466 gui_syncsplash(0, true, str(LANG_SHUTTINGDOWN
));
467 if (callback
!= NULL
)
479 static bool waiting_to_resume_play
= false;
480 static long play_resume_tick
;
482 static void car_adapter_mode_processing(bool inserted
)
484 if (global_settings
.car_adapter_mode
)
489 * Just got plugged in, delay & resume if we were playing
491 if (audio_status() & AUDIO_STATUS_PAUSE
)
493 /* delay resume a bit while the engine is cranking */
494 play_resume_tick
= current_tick
+ HZ
*5;
495 waiting_to_resume_play
= true;
501 * Just got unplugged, pause if playing
503 if ((audio_status() & AUDIO_STATUS_PLAY
) &&
504 !(audio_status() & AUDIO_STATUS_PAUSE
))
512 static void car_adapter_tick(void)
514 if (waiting_to_resume_play
)
516 if (TIME_AFTER(current_tick
, play_resume_tick
))
518 if (audio_status() & AUDIO_STATUS_PAUSE
)
522 waiting_to_resume_play
= false;
527 void car_adapter_mode_init(void)
529 tick_add_task(car_adapter_tick
);
533 long default_event_handler_ex(long event
, void (*callback
)(void *), void *parameter
)
537 case SYS_USB_CONNECTED
:
538 if (callback
!= NULL
)
541 if (!mmc_touched() || (mmc_remove_request() == SYS_MMC_EXTRACTED
))
548 return SYS_USB_CONNECTED
;
550 if (!clean_shutdown(callback
, parameter
))
554 case SYS_CHARGER_CONNECTED
:
555 car_adapter_mode_processing(true);
556 return SYS_CHARGER_CONNECTED
;
558 case SYS_CHARGER_DISCONNECTED
:
559 car_adapter_mode_processing(false);
560 return SYS_CHARGER_DISCONNECTED
;
566 long default_event_handler(long event
)
568 return default_event_handler_ex(event
, NULL
, NULL
);
571 int show_logo( void )
573 #ifdef HAVE_LCD_BITMAP
578 lcd_bitmap(rockboxlogo
, 0, 10, ROCKBOXLOGO_WIDTH
, ROCKBOXLOGO_HEIGHT
);
580 #ifdef HAVE_REMOTE_LCD
581 lcd_remote_clear_display();
582 lcd_remote_bitmap(remote_rockboxlogo
,10,14,REMOTE_ROCKBOXLOGO_WIDTH
,
583 REMOTE_ROCKBOXLOGO_HEIGHT
);
586 snprintf(version
, sizeof(version
), "Ver. %s", appsversion
);
587 lcd_setfont(FONT_SYSFIXED
);
588 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
589 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
590 LCD_HEIGHT
-font_h
, (unsigned char *)version
);
593 #ifdef HAVE_REMOTE_LCD
594 lcd_remote_setfont(FONT_SYSFIXED
);
595 lcd_remote_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
596 lcd_remote_putsxy((LCD_REMOTE_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
597 LCD_REMOTE_HEIGHT
-font_h
, (unsigned char *)version
);
602 char *rockbox
= " ROCKbox!";
604 lcd_double_height(true);
605 lcd_puts(0, 0, rockbox
);
606 lcd_puts(0, 1, appsversion
);