Added old.t
[kugel-rb.git] / apps / misc.c
blob6493e7b46fccae00f26d9cd0e9e2076b7507d27e
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 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
299 static unsigned char line_block[BMP_LINESIZE];
300 #else
301 static unsigned char line_block[4][BMP_LINESIZE];
302 #endif
303 #elif LCD_DEPTH == 16
304 static unsigned short line_block[BMP_LINESIZE/2];
305 #endif
307 #ifdef CONFIG_RTC
308 create_datetime_filename(filename, "", "dump ", ".bmp");
309 #else
310 create_numbered_filename(filename, "", "dump_", ".bmp", 4);
311 #endif
313 fh = creat(filename, O_WRONLY);
314 if (fh < 0)
315 return;
317 if (screen_dump_hook)
319 screen_dump_hook(fh);
321 else
323 write(fh, bmpheader, sizeof(bmpheader));
325 /* BMP image goes bottom up */
326 #if LCD_DEPTH == 1
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;
336 int ix;
338 for (ix = 8; ix > 0; ix--)
340 unsigned char *dst_blk = dst;
341 unsigned src_byte = *src++;
342 int iy;
344 for (iy = 8; iy > 0; iy--)
346 if (src_byte & 0x80)
347 *dst_blk |= dst_mask;
348 src_byte <<= 1;
349 dst_blk += BMP_LINESIZE;
351 dst_mask >>= 1;
353 dst++;
356 write(fh, line_block, sizeof(line_block));
358 #elif LCD_DEPTH == 2
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++;
369 unsigned tmp;
371 tmp = src_byte & 3;
372 src_byte >>= 2;
373 *dst++ = (tmp << 4) | (src_byte & 3);
374 src_byte >>= 2;
375 tmp = src_byte & 3;
376 src_byte >>= 2;
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++;
394 int iy;
396 for (iy = 4; iy > 0; iy--)
398 *dst_blk = ((src_byte0 & 3) << 4) | (src_byte1 & 3);
399 src_byte0 >>= 2;
400 src_byte1 >>= 2;
401 dst_blk -= BMP_LINESIZE;
405 write(fh, line_block, sizeof(line_block));
407 #endif
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++);
420 #else
421 *dst++ = htole16(*src++);
422 #endif
425 write(fh, line_block, sizeof(line_block));
427 #endif /* LCD_DEPTH */
430 close(fh);
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:
442 name: value
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)
452 char* ptr;
454 while ( isspace(*line) )
455 line++;
457 if ( *line == '#' )
458 return false;
460 ptr = strchr(line, ':');
461 if ( !ptr )
462 return false;
464 *name = line;
465 *ptr = 0;
466 ptr++;
467 while (isspace(*ptr))
468 ptr++;
469 *value = ptr;
470 return true;
473 static void system_flush(void)
475 tree_flush();
478 static void system_restore(void)
480 tree_restore();
483 static bool clean_shutdown(void (*callback)(void *), void *parameter)
485 #ifdef SIMULATOR
486 (void)callback;
487 (void)parameter;
488 exit(0);
489 #else
490 #if defined(HAVE_CHARGING) && !defined(HAVE_POWEROFF_WHILE_CHARGING)
491 if(!charger_inserted())
492 #endif
494 lcd_clear_display();
495 gui_syncsplash(0, true, str(LANG_SHUTTINGDOWN));
496 if (callback != NULL)
497 callback(parameter);
499 system_flush();
501 shutdown_hw();
503 #endif
504 return false;
507 #ifdef HAVE_CHARGING
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)
515 if(inserted)
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;
527 else
530 * Just got unplugged, pause if playing
532 if ((audio_status() & AUDIO_STATUS_PLAY) &&
533 !(audio_status() & AUDIO_STATUS_PAUSE))
535 audio_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)
549 audio_resume();
551 waiting_to_resume_play = false;
556 void car_adapter_mode_init(void)
558 tick_add_task(car_adapter_tick);
560 #endif
562 long default_event_handler_ex(long event, void (*callback)(void *), void *parameter)
564 switch(event)
566 case SYS_USB_CONNECTED:
567 if (callback != NULL)
568 callback(parameter);
569 #ifdef HAVE_MMC
570 if (!mmc_touched() || (mmc_remove_request() == SYS_MMC_EXTRACTED))
571 #endif
573 system_flush();
574 usb_screen();
575 system_restore();
577 return SYS_USB_CONNECTED;
578 case SYS_POWEROFF:
579 if (!clean_shutdown(callback, parameter))
580 return SYS_POWEROFF;
581 break;
582 #ifdef HAVE_CHARGING
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;
590 #endif
592 return 0;
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
603 char version[32];
604 int font_h, font_w;
606 lcd_clear_display();
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);
613 #endif
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);
620 lcd_update();
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);
627 lcd_remote_update();
628 #endif
630 #else
631 char *rockbox = " ROCKbox!";
632 lcd_clear_display();
633 lcd_double_height(true);
634 lcd_puts(0, 0, rockbox);
635 lcd_puts(0, 1, appsversion);
636 #endif
638 return 0;