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"
51 #ifdef HAVE_LCD_BITMAP
54 #endif /* End HAVE_LCD_BITMAP */
56 /* Format a large-range value for output, using the appropriate unit so that
57 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
58 * units) if possible, and 3 significant digits are shown. If a buffer is
59 * given, the result is snprintf()'d into that buffer, otherwise the result is
61 char *output_dyn_value(char *buf
, int buf_size
, int value
,
62 const unsigned char **units
, bool bin_scale
)
64 int scale
= bin_scale
? 1024 : 1000;
70 while (value
>= scale
)
72 fraction
= value
% scale
;
77 fraction
= fraction
* 1000 / 1024;
79 if (value
>= 100 || !unit_no
)
82 snprintf(tbuf
, sizeof(tbuf
), "%01d", fraction
/ 100);
84 snprintf(tbuf
, sizeof(tbuf
), "%02d", fraction
/ 10);
89 snprintf(buf
, buf_size
, "%d%s%s%s", value
, str(LANG_POINT
),
90 tbuf
, P2STR(units
[unit_no
]));
92 snprintf(buf
, buf_size
, "%d%s", value
, P2STR(units
[unit_no
]));
96 /* strip trailing zeros from the fraction */
97 for (i
= strlen(tbuf
) - 1; (i
>= 0) && (tbuf
[i
] == '0'); i
--)
100 talk_number(value
, true);
103 talk_id(LANG_POINT
, true);
104 talk_spell(tbuf
, true);
106 talk_id(P2ID(units
[unit_no
]), true);
111 /* Create a filename with a number part in a way that the number is 1
112 higher than the highest numbered file matching the same pattern.
113 It is allowed that buffer and path point to the same memory location,
114 saving a strcpy(). Path must always be given without trailing slash,. */
115 char *create_numbered_filename(char *buffer
, const char *path
,
116 const char *prefix
, const char *suffix
,
120 struct dirent
*entry
;
123 int prefixlen
= strlen(prefix
);
127 strncpy(buffer
, path
, MAX_PATH
);
129 pathlen
= strlen(buffer
);
131 dir
= opendir(pathlen
? buffer
: "/");
135 while ((entry
= readdir(dir
)))
139 if (strncasecmp(entry
->d_name
, prefix
, prefixlen
)
140 || strcasecmp(entry
->d_name
+ prefixlen
+ numberlen
, suffix
))
143 curr_num
= atoi(entry
->d_name
+ prefixlen
);
144 if (curr_num
> max_num
)
149 snprintf(fmtstring
, sizeof(fmtstring
), "/%%s%%0%dd%%s", numberlen
);
150 snprintf(buffer
+ pathlen
, MAX_PATH
- pathlen
, fmtstring
, prefix
,
151 max_num
+ 1, suffix
);
157 /* Create a filename with a date+time part.
158 It is allowed that buffer and path point to the same memory location,
159 saving a strcpy(). Path must always be given without trailing slash. */
160 char *create_datetime_filename(char *buffer
, const char *path
,
161 const char *prefix
, const char *suffix
)
163 struct tm
*tm
= get_time();
167 strncpy(buffer
, path
, MAX_PATH
);
169 pathlen
= strlen(buffer
);
170 snprintf(buffer
+ pathlen
, MAX_PATH
- pathlen
,
171 "/%s%02d%02d%02d-%02d%02d%02d%s", prefix
,
172 tm
->tm_year
% 100, tm
->tm_mon
+ 1, tm
->tm_mday
,
173 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
, suffix
);
177 #endif /* HAVE_RTC */
179 /* Read (up to) a line of text from fd into buffer and return number of bytes
180 * read (which may be larger than the number of bytes stored in buffer). If
181 * an error occurs, -1 is returned (and buffer contains whatever could be
182 * read). A line is terminated by a LF char. Neither LF nor CR chars are
185 int read_line(int fd
, char* buffer
, int buffer_size
)
192 while (count
< buffer_size
)
196 if (1 != read(fd
, &c
, 1))
210 buffer
[MIN(count
, buffer_size
- 1)] = 0;
212 return errno
? -1 : num_read
;
215 #ifdef HAVE_LCD_BITMAP
218 #define BMP_NUMCOLORS (1 << LCD_DEPTH)
220 #define BMP_NUMCOLORS 0
225 #define BMP_LINESIZE ((LCD_WIDTH/8 + 3) & ~3)
228 #define BMP_LINESIZE ((LCD_WIDTH/2 + 3) & ~3)
231 #define BMP_LINESIZE ((LCD_WIDTH + 3) & ~3)
232 #elif LCD_DEPTH <= 16
234 #define BMP_LINESIZE ((LCD_WIDTH*2 + 3) & ~3)
237 #define BMP_LINESIZE ((LCD_WIDTH*3 + 3) & ~3)
240 #define BMP_HEADERSIZE (54 + 4 * BMP_NUMCOLORS)
241 #define BMP_DATASIZE (BMP_LINESIZE * LCD_HEIGHT)
242 #define BMP_TOTALSIZE (BMP_HEADERSIZE + BMP_DATASIZE)
244 #define LE16_CONST(x) (x)&0xff, ((x)>>8)&0xff
245 #define LE32_CONST(x) (x)&0xff, ((x)>>8)&0xff, ((x)>>16)&0xff, ((x)>>24)&0xff
247 static const unsigned char bmpheader
[] =
249 0x42, 0x4d, /* 'BM' */
250 LE32_CONST(BMP_TOTALSIZE
), /* Total file size */
251 0x00, 0x00, 0x00, 0x00, /* Reserved */
252 LE32_CONST(BMP_HEADERSIZE
), /* Offset to start of pixel data */
254 0x28, 0x00, 0x00, 0x00, /* Size of (2nd) header */
255 LE32_CONST(LCD_WIDTH
), /* Width in pixels */
256 LE32_CONST(LCD_HEIGHT
), /* Height in pixels */
257 0x01, 0x00, /* Number of planes (always 1) */
258 LE16_CONST(BMP_BPP
), /* Bits per pixel 1/4/8/16/24 */
259 0x00, 0x00, 0x00, 0x00, /* Compression mode, 0 = none */
260 LE32_CONST(BMP_DATASIZE
), /* Size of bitmap data */
261 0xc4, 0x0e, 0x00, 0x00, /* Horizontal resolution (pixels/meter) */
262 0xc4, 0x0e, 0x00, 0x00, /* Vertical resolution (pixels/meter) */
263 LE32_CONST(BMP_NUMCOLORS
), /* Number of used colours */
264 LE32_CONST(BMP_NUMCOLORS
), /* Number of important colours */
267 0x90, 0xee, 0x90, 0x00, /* Colour #0 */
268 0x00, 0x00, 0x00, 0x00 /* Colour #1 */
270 0xe6, 0xd8, 0xad, 0x00, /* Colour #0 */
271 0x99, 0x90, 0x73, 0x00, /* Colour #1 */
272 0x4c, 0x48, 0x39, 0x00, /* Colour #2 */
273 0x00, 0x00, 0x00, 0x00 /* Colour #3 */
277 static void (*screen_dump_hook
)(int fh
) = NULL
;
279 void screen_dump(void)
284 char filename
[MAX_PATH
];
286 int ix
, src_mask
, dst_mask
;
287 static unsigned char line_block
[8][BMP_LINESIZE
];
290 static unsigned char line_block
[4][BMP_LINESIZE
];
294 create_datetime_filename(filename
, "", "dump ", ".bmp");
296 create_numbered_filename(filename
, "", "dump_", ".bmp", 4);
299 fh
= creat(filename
, O_WRONLY
);
303 if (screen_dump_hook
)
305 screen_dump_hook(fh
);
309 write(fh
, bmpheader
, sizeof(bmpheader
));
311 /* BMP image goes bottom up */
313 for (by
= LCD_HEIGHT
/8 - 1; by
>= 0; by
--)
315 memset(&line_block
[0][0], 0, sizeof(line_block
));
317 for (bx
= 0; bx
< LCD_WIDTH
/8; bx
++)
320 for (ix
= 7; ix
>= 0; ix
--)
322 src_byte
= lcd_framebuffer
[by
][8*bx
+ix
];
324 for (iy
= 7; iy
>= 0; iy
--)
326 if (src_byte
& src_mask
)
327 line_block
[iy
][bx
] |= dst_mask
;
334 write(fh
, &line_block
[0][0], sizeof(line_block
));
337 for (by
= LCD_HEIGHT
/4 - 1; by
>= 0; by
--)
339 memset(&line_block
[0][0], 0, sizeof(line_block
));
341 for (bx
= 0; bx
< LCD_WIDTH
/2; bx
++)
343 src_byte
= lcd_framebuffer
[by
][2*bx
];
344 src_byte2
= lcd_framebuffer
[by
][2*bx
+1];
345 for (iy
= 3; iy
>= 0; iy
--)
347 line_block
[iy
][bx
] = ((src_byte
& 3) << 4) | (src_byte2
& 3);
353 write(fh
, &line_block
[0][0], sizeof(line_block
));
355 #endif /* LCD_DEPTH */
361 void screen_dump_set_hook(void (*hook
)(int fh
))
363 screen_dump_hook
= hook
;
366 #endif /* HAVE_LCD_BITMAP */
368 /* parse a line from a configuration file. the line format is:
372 Any whitespace before setting name or value (after ':') is ignored.
373 A # as first non-whitespace character discards the whole line.
374 Function sets pointers to null-terminated setting name and value.
375 Returns false if no valid config entry was found.
378 bool settings_parseline(char* line
, char** name
, char** value
)
382 while ( isspace(*line
) )
388 ptr
= strchr(line
, ':');
395 while (isspace(*ptr
))
401 static void system_flush(void)
406 static void system_restore(void)
411 static bool clean_shutdown(void (*callback
)(void *), void *parameter
)
418 #ifndef HAVE_POWEROFF_WHILE_CHARGING
419 if(!charger_inserted())
423 splash(0, true, str(LANG_SHUTTINGDOWN
));
424 if (callback
!= NULL
)
436 static bool waiting_to_resume_play
= false;
437 static long play_resume_tick
;
439 static void car_adapter_mode_processing(bool inserted
)
441 if (global_settings
.car_adapter_mode
)
446 * Just got plugged in, delay & resume if we were playing
448 if (audio_status() & AUDIO_STATUS_PAUSE
)
450 /* delay resume a bit while the engine is cranking */
451 play_resume_tick
= current_tick
+ HZ
*5;
452 waiting_to_resume_play
= true;
458 * Just got unplugged, pause if playing
460 if ((audio_status() & AUDIO_STATUS_PLAY
) &&
461 !(audio_status() & AUDIO_STATUS_PAUSE
))
469 static void car_adapter_tick(void)
471 if (waiting_to_resume_play
)
473 if (TIME_AFTER(current_tick
, play_resume_tick
))
475 if (audio_status() & AUDIO_STATUS_PAUSE
)
479 waiting_to_resume_play
= false;
484 void car_adapter_mode_init(void)
486 tick_add_task(car_adapter_tick
);
490 long default_event_handler_ex(long event
, void (*callback
)(void *), void *parameter
)
494 case SYS_USB_CONNECTED
:
495 if (callback
!= NULL
)
498 if (!mmc_touched() || (mmc_remove_request() == SYS_MMC_EXTRACTED
))
505 return SYS_USB_CONNECTED
;
507 if (!clean_shutdown(callback
, parameter
))
511 case SYS_CHARGER_CONNECTED
:
512 car_adapter_mode_processing(true);
513 return SYS_CHARGER_CONNECTED
;
515 case SYS_CHARGER_DISCONNECTED
:
516 car_adapter_mode_processing(false);
517 return SYS_CHARGER_DISCONNECTED
;
523 long default_event_handler(long event
)
525 return default_event_handler_ex(event
, NULL
, NULL
);
528 int show_logo( void )
530 #ifdef HAVE_LCD_BITMAP
535 #if LCD_WIDTH == 112 || LCD_WIDTH == 128
536 lcd_bitmap(rockbox112x37
, 0, 10, 112, 37);
539 lcd_bitmap(rockbox160x53x2
, 0, 10, 160, 53);
542 #ifdef HAVE_REMOTE_LCD
543 lcd_remote_clear_display();
544 lcd_remote_bitmap(rockbox112x37
,10,14,112,37);
547 snprintf(version
, sizeof(version
), "Ver. %s", appsversion
);
548 lcd_setfont(FONT_SYSFIXED
);
549 lcd_getstringsize("A", &font_w
, &font_h
);
550 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
551 LCD_HEIGHT
-font_h
, version
);
554 #ifdef HAVE_REMOTE_LCD
555 lcd_remote_setfont(FONT_SYSFIXED
);
556 lcd_remote_getstringsize("A", &font_w
, &font_h
);
557 lcd_remote_putsxy((LCD_REMOTE_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
558 LCD_REMOTE_HEIGHT
-font_h
, version
);
563 char *rockbox
= " ROCKbox!";
565 lcd_double_height(true);
566 lcd_puts(0, 0, rockbox
);
567 lcd_puts(0, 1, appsversion
);