Initial attempt at using bmp2rb in the build system. Don't forget to re-run configure
[Rockbox.git] / apps / misc.c
blobe8bc8f9345a215d224445760c2c13e130a7f4a03
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
19 #include <stdlib.h>
20 #include <ctype.h>
21 #include "lang.h"
22 #include "string.h"
23 #include "config.h"
24 #include "file.h"
25 #include "dir.h"
26 #include "lcd.h"
27 #include "lcd-remote.h"
28 #include "sprintf.h"
29 #include "errno.h"
30 #include "system.h"
31 #include "timefuncs.h"
32 #include "screens.h"
33 #include "talk.h"
34 #include "mpeg.h"
35 #include "audio.h"
36 #include "mp3_playback.h"
37 #include "settings.h"
38 #include "ata.h"
39 #include "kernel.h"
40 #include "power.h"
41 #include "powermgmt.h"
42 #include "backlight.h"
43 #include "atoi.h"
44 #include "version.h"
45 #include "font.h"
46 #include "splash.h"
47 #ifdef HAVE_MMC
48 #include "ata_mmc.h"
49 #endif
50 #include "tree.h"
52 #ifdef HAVE_LCD_BITMAP
53 #include "bmp.h"
54 #include "icons.h"
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
61 * voiced.*/
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;
66 int fraction = 0;
67 int unit_no = 0;
68 int i;
69 char tbuf[5];
71 while (value >= scale)
73 fraction = value % scale;
74 value /= scale;
75 unit_no++;
77 if (bin_scale)
78 fraction = fraction * 1000 / 1024;
80 if (value >= 100 || !unit_no)
81 tbuf[0] = '\0';
82 else if (value >= 10)
83 snprintf(tbuf, sizeof(tbuf), "%01d", fraction / 100);
84 else
85 snprintf(tbuf, sizeof(tbuf), "%02d", fraction / 10);
87 if (buf)
89 if (strlen(tbuf))
90 snprintf(buf, buf_size, "%d%s%s%s", value, str(LANG_POINT),
91 tbuf, P2STR(units[unit_no]));
92 else
93 snprintf(buf, buf_size, "%d%s", value, P2STR(units[unit_no]));
95 else
97 /* strip trailing zeros from the fraction */
98 for (i = strlen(tbuf) - 1; (i >= 0) && (tbuf[i] == '0'); i--)
99 tbuf[i] = '\0';
101 talk_number(value, true);
102 if (tbuf[0] != 0)
104 talk_id(LANG_POINT, true);
105 talk_spell(tbuf, true);
107 talk_id(P2ID(units[unit_no]), true);
109 return buf;
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,
118 int numberlen)
120 DIR *dir;
121 struct dirent *entry;
122 int max_num = 0;
123 int pathlen;
124 int prefixlen = strlen(prefix);
125 char fmtstring[12];
127 if (buffer != path)
128 strncpy(buffer, path, MAX_PATH);
130 pathlen = strlen(buffer);
132 dir = opendir(pathlen ? buffer : "/");
133 if (!dir)
134 return NULL;
136 while ((entry = readdir(dir)))
138 int curr_num;
140 if (strncasecmp((char *)entry->d_name, prefix, prefixlen)
141 || strcasecmp((char *)entry->d_name + prefixlen + numberlen, suffix))
142 continue;
144 curr_num = atoi((char *)entry->d_name + prefixlen);
145 if (curr_num > max_num)
146 max_num = curr_num;
148 closedir(dir);
150 snprintf(fmtstring, sizeof(fmtstring), "/%%s%%0%dd%%s", numberlen);
151 snprintf(buffer + pathlen, MAX_PATH - pathlen, fmtstring, prefix,
152 max_num + 1, suffix);
154 return buffer;
157 #ifdef CONFIG_RTC
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();
165 int pathlen;
167 if (buffer != path)
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);
176 return buffer;
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
184 * stored in buffer.
186 int read_line(int fd, char* buffer, int buffer_size)
188 int count = 0;
189 int num_read = 0;
191 errno = 0;
193 while (count < buffer_size)
195 unsigned char c;
197 if (1 != read(fd, &c, 1))
198 break;
200 num_read++;
202 if ( c == '\n' )
203 break;
205 if ( c == '\r' )
206 continue;
208 buffer[count++] = c;
211 buffer[MIN(count, buffer_size - 1)] = 0;
213 return errno ? -1 : num_read;
216 #ifdef HAVE_LCD_BITMAP
218 #if LCD_DEPTH == 16
219 #define BMP_COMPRESSION 3 /* BI_BITFIELDS */
220 #define BMP_NUMCOLORS 3
221 #else
222 #define BMP_COMPRESSION 0 /* BI_RGB */
223 #if LCD_DEPTH <= 8
224 #define BMP_NUMCOLORS (1 << LCD_DEPTH)
225 #else
226 #define BMP_NUMCOLORS 0
227 #endif
228 #endif
230 #if LCD_DEPTH == 1
231 #define BMP_BPP 1
232 #define BMP_LINESIZE ((LCD_WIDTH/8 + 3) & ~3)
233 #elif LCD_DEPTH <= 4
234 #define BMP_BPP 4
235 #define BMP_LINESIZE ((LCD_WIDTH/2 + 3) & ~3)
236 #elif LCD_DEPTH <= 8
237 #define BMP_BPP 8
238 #define BMP_LINESIZE ((LCD_WIDTH + 3) & ~3)
239 #elif LCD_DEPTH <= 16
240 #define BMP_BPP 16
241 #define BMP_LINESIZE ((LCD_WIDTH*2 + 3) & ~3)
242 #else
243 #define BMP_BPP 24
244 #define BMP_LINESIZE ((LCD_WIDTH*3 + 3) & ~3)
245 #endif
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 */
273 #if LCD_DEPTH == 1
274 0x90, 0xee, 0x90, 0x00, /* Colour #0 */
275 0x00, 0x00, 0x00, 0x00 /* Colour #1 */
276 #elif LCD_DEPTH == 2
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 */
285 #endif
288 static void (*screen_dump_hook)(int fh) = NULL;
290 void screen_dump(void)
292 int fh;
293 char filename[MAX_PATH];
294 int bx, by;
295 #if LCD_DEPTH == 1
296 static unsigned char line_block[8][BMP_LINESIZE];
297 #elif LCD_DEPTH == 2
298 static unsigned char line_block[4][BMP_LINESIZE];
299 #elif LCD_DEPTH == 16
300 static unsigned short line_block[BMP_LINESIZE/2];
301 #endif
303 #ifdef CONFIG_RTC
304 create_datetime_filename(filename, "", "dump ", ".bmp");
305 #else
306 create_numbered_filename(filename, "", "dump_", ".bmp", 4);
307 #endif
309 fh = creat(filename, O_WRONLY);
310 if (fh < 0)
311 return;
313 if (screen_dump_hook)
315 screen_dump_hook(fh);
317 else
319 write(fh, bmpheader, sizeof(bmpheader));
321 /* BMP image goes bottom up */
322 #if LCD_DEPTH == 1
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;
332 int ix;
334 for (ix = 8; ix > 0; ix--)
336 unsigned char *dst_blk = dst;
337 unsigned src_byte = *src++;
338 int iy;
340 for (iy = 8; iy > 0; iy--)
342 if (src_byte & 0x80)
343 *dst_blk |= dst_mask;
344 src_byte <<= 1;
345 dst_blk += BMP_LINESIZE;
347 dst_mask >>= 1;
349 dst++;
352 write(fh, line_block, sizeof(line_block));
354 #elif LCD_DEPTH == 2
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++;
366 int iy;
368 for (iy = 4; iy > 0; iy--)
370 *dst_blk = ((src_byte0 & 3) << 4) | (src_byte1 & 3);
371 src_byte0 >>= 2;
372 src_byte1 >>= 2;
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++);
391 #else
392 *dst++ = htole16(*src++);
393 #endif
396 write(fh, line_block, sizeof(line_block));
398 #endif /* LCD_DEPTH */
401 close(fh);
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:
413 name: value
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)
423 char* ptr;
425 while ( isspace(*line) )
426 line++;
428 if ( *line == '#' )
429 return false;
431 ptr = strchr(line, ':');
432 if ( !ptr )
433 return false;
435 *name = line;
436 *ptr = 0;
437 ptr++;
438 while (isspace(*ptr))
439 ptr++;
440 *value = ptr;
441 return true;
444 static void system_flush(void)
446 tree_flush();
449 static void system_restore(void)
451 tree_restore();
454 static bool clean_shutdown(void (*callback)(void *), void *parameter)
456 #ifdef SIMULATOR
457 (void)callback;
458 (void)parameter;
459 exit(0);
460 #else
461 #if defined(HAVE_CHARGING) && !defined(HAVE_POWEROFF_WHILE_CHARGING)
462 if(!charger_inserted())
463 #endif
465 lcd_clear_display();
466 gui_syncsplash(0, true, str(LANG_SHUTTINGDOWN));
467 if (callback != NULL)
468 callback(parameter);
470 system_flush();
472 shutdown_hw();
474 #endif
475 return false;
478 #ifdef HAVE_CHARGING
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)
486 if(inserted)
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;
498 else
501 * Just got unplugged, pause if playing
503 if ((audio_status() & AUDIO_STATUS_PLAY) &&
504 !(audio_status() & AUDIO_STATUS_PAUSE))
506 audio_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)
520 audio_resume();
522 waiting_to_resume_play = false;
527 void car_adapter_mode_init(void)
529 tick_add_task(car_adapter_tick);
531 #endif
533 long default_event_handler_ex(long event, void (*callback)(void *), void *parameter)
535 switch(event)
537 case SYS_USB_CONNECTED:
538 if (callback != NULL)
539 callback(parameter);
540 #ifdef HAVE_MMC
541 if (!mmc_touched() || (mmc_remove_request() == SYS_MMC_EXTRACTED))
542 #endif
544 system_flush();
545 usb_screen();
546 system_restore();
548 return SYS_USB_CONNECTED;
549 case SYS_POWEROFF:
550 if (!clean_shutdown(callback, parameter))
551 return SYS_POWEROFF;
552 break;
553 #ifdef HAVE_CHARGING
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;
561 #endif
563 return 0;
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
574 char version[32];
575 int font_h, font_w;
577 lcd_clear_display();
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);
584 #endif
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);
591 lcd_update();
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);
598 lcd_remote_update();
599 #endif
601 #else
602 char *rockbox = " ROCKbox!";
603 lcd_clear_display();
604 lcd_double_height(true);
605 lcd_puts(0, 0, rockbox);
606 lcd_puts(0, 1, appsversion);
607 #endif
609 return 0;