Patch #1352575 - Shorten codec from the ffmpeg project. Rockbox implementation by...
[Rockbox.git] / apps / misc.c
blob1c19c155d26575de7ea70c935fc1d1b792816c21
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 #ifdef HAVE_MMC
47 #include "ata_mmc.h"
48 #endif
49 #include "tree.h"
51 #ifdef HAVE_LCD_BITMAP
52 #include "bmp.h"
53 #include "icons.h"
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
60 * voiced.*/
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;
65 int fraction = 0;
66 int unit_no = 0;
67 int i;
68 char tbuf[5];
70 while (value >= scale)
72 fraction = value % scale;
73 value /= scale;
74 unit_no++;
76 if (bin_scale)
77 fraction = fraction * 1000 / 1024;
79 if (value >= 100 || !unit_no)
80 tbuf[0] = '\0';
81 else if (value >= 10)
82 snprintf(tbuf, sizeof(tbuf), "%01d", fraction / 100);
83 else
84 snprintf(tbuf, sizeof(tbuf), "%02d", fraction / 10);
86 if (buf)
88 if (strlen(tbuf))
89 snprintf(buf, buf_size, "%d%s%s%s", value, str(LANG_POINT),
90 tbuf, P2STR(units[unit_no]));
91 else
92 snprintf(buf, buf_size, "%d%s", value, P2STR(units[unit_no]));
94 else
96 /* strip trailing zeros from the fraction */
97 for (i = strlen(tbuf) - 1; (i >= 0) && (tbuf[i] == '0'); i--)
98 tbuf[i] = '\0';
100 talk_number(value, true);
101 if (tbuf[0] != 0)
103 talk_id(LANG_POINT, true);
104 talk_spell(tbuf, true);
106 talk_id(P2ID(units[unit_no]), true);
108 return buf;
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,
117 int numberlen)
119 DIR *dir;
120 struct dirent *entry;
121 int max_num = 0;
122 int pathlen;
123 int prefixlen = strlen(prefix);
124 char fmtstring[12];
126 if (buffer != path)
127 strncpy(buffer, path, MAX_PATH);
129 pathlen = strlen(buffer);
131 dir = opendir(pathlen ? buffer : "/");
132 if (!dir)
133 return NULL;
135 while ((entry = readdir(dir)))
137 int curr_num;
139 if (strncasecmp(entry->d_name, prefix, prefixlen)
140 || strcasecmp(entry->d_name + prefixlen + numberlen, suffix))
141 continue;
143 curr_num = atoi(entry->d_name + prefixlen);
144 if (curr_num > max_num)
145 max_num = curr_num;
147 closedir(dir);
149 snprintf(fmtstring, sizeof(fmtstring), "/%%s%%0%dd%%s", numberlen);
150 snprintf(buffer + pathlen, MAX_PATH - pathlen, fmtstring, prefix,
151 max_num + 1, suffix);
153 return buffer;
156 #ifdef HAVE_RTC
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();
164 int pathlen;
166 if (buffer != path)
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);
175 return buffer;
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
183 * stored in buffer.
185 int read_line(int fd, char* buffer, int buffer_size)
187 int count = 0;
188 int num_read = 0;
190 errno = 0;
192 while (count < buffer_size)
194 unsigned char c;
196 if (1 != read(fd, &c, 1))
197 break;
199 num_read++;
201 if ( c == '\n' )
202 break;
204 if ( c == '\r' )
205 continue;
207 buffer[count++] = c;
210 buffer[MIN(count, buffer_size - 1)] = 0;
212 return errno ? -1 : num_read;
215 #ifdef HAVE_LCD_BITMAP
217 #if LCD_DEPTH <= 8
218 #define BMP_NUMCOLORS (1 << LCD_DEPTH)
219 #else
220 #define BMP_NUMCOLORS 0
221 #endif
223 #if LCD_DEPTH == 1
224 #define BMP_BPP 1
225 #define BMP_LINESIZE ((LCD_WIDTH/8 + 3) & ~3)
226 #elif LCD_DEPTH <= 4
227 #define BMP_BPP 4
228 #define BMP_LINESIZE ((LCD_WIDTH/2 + 3) & ~3)
229 #elif LCD_DEPTH <= 8
230 #define BMP_BPP 8
231 #define BMP_LINESIZE ((LCD_WIDTH + 3) & ~3)
232 #elif LCD_DEPTH <= 16
233 #define BMP_BPP 16
234 #define BMP_LINESIZE ((LCD_WIDTH*2 + 3) & ~3)
235 #else
236 #define BMP_BPP 24
237 #define BMP_LINESIZE ((LCD_WIDTH*3 + 3) & ~3)
238 #endif
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 */
266 #if LCD_DEPTH == 1
267 0x90, 0xee, 0x90, 0x00, /* Colour #0 */
268 0x00, 0x00, 0x00, 0x00 /* Colour #1 */
269 #elif LCD_DEPTH == 2
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 */
274 #endif
277 static void (*screen_dump_hook)(int fh) = NULL;
279 void screen_dump(void)
281 int fh;
282 int bx, by, iy;
283 int src_byte;
284 char filename[MAX_PATH];
285 #if LCD_DEPTH == 1
286 int ix, src_mask, dst_mask;
287 static unsigned char line_block[8][BMP_LINESIZE];
288 #elif LCD_DEPTH == 2
289 int src_byte2;
290 static unsigned char line_block[4][BMP_LINESIZE];
291 #endif
293 #ifdef HAVE_RTC
294 create_datetime_filename(filename, "", "dump ", ".bmp");
295 #else
296 create_numbered_filename(filename, "", "dump_", ".bmp", 4);
297 #endif
299 fh = creat(filename, O_WRONLY);
300 if (fh < 0)
301 return;
303 if (screen_dump_hook)
305 screen_dump_hook(fh);
307 else
309 write(fh, bmpheader, sizeof(bmpheader));
311 /* BMP image goes bottom up */
312 #if LCD_DEPTH == 1
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++)
319 dst_mask = 0x01;
320 for (ix = 7; ix >= 0; ix--)
322 src_byte = lcd_framebuffer[by][8*bx+ix];
323 src_mask = 0x01;
324 for (iy = 7; iy >= 0; iy--)
326 if (src_byte & src_mask)
327 line_block[iy][bx] |= dst_mask;
328 src_mask <<= 1;
330 dst_mask <<= 1;
334 write(fh, &line_block[0][0], sizeof(line_block));
336 #elif LCD_DEPTH == 2
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);
348 src_byte >>= 2;
349 src_byte2 >>= 2;
353 write(fh, &line_block[0][0], sizeof(line_block));
355 #endif /* LCD_DEPTH */
358 close(fh);
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:
370 name: value
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)
380 char* ptr;
382 while ( isspace(*line) )
383 line++;
385 if ( *line == '#' )
386 return false;
388 ptr = strchr(line, ':');
389 if ( !ptr )
390 return false;
392 *name = line;
393 *ptr = 0;
394 ptr++;
395 while (isspace(*ptr))
396 ptr++;
397 *value = ptr;
398 return true;
401 static void system_flush(void)
403 tree_flush();
406 static void system_restore(void)
408 tree_restore();
411 static bool clean_shutdown(void (*callback)(void *), void *parameter)
413 #ifdef SIMULATOR
414 (void)callback;
415 (void)parameter;
416 exit(0);
417 #else
418 #ifndef HAVE_POWEROFF_WHILE_CHARGING
419 if(!charger_inserted())
420 #endif
422 lcd_clear_display();
423 splash(0, true, str(LANG_SHUTTINGDOWN));
424 if (callback != NULL)
425 callback(parameter);
427 system_flush();
429 shutdown_hw();
431 #endif
432 return false;
435 #ifdef HAVE_CHARGING
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)
443 if(inserted)
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;
455 else
458 * Just got unplugged, pause if playing
460 if ((audio_status() & AUDIO_STATUS_PLAY) &&
461 !(audio_status() & AUDIO_STATUS_PAUSE))
463 audio_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)
477 audio_resume();
479 waiting_to_resume_play = false;
484 void car_adapter_mode_init(void)
486 tick_add_task(car_adapter_tick);
488 #endif
490 long default_event_handler_ex(long event, void (*callback)(void *), void *parameter)
492 switch(event)
494 case SYS_USB_CONNECTED:
495 if (callback != NULL)
496 callback(parameter);
497 #ifdef HAVE_MMC
498 if (!mmc_touched() || (mmc_remove_request() == SYS_MMC_EXTRACTED))
499 #endif
501 system_flush();
502 usb_screen();
503 system_restore();
505 return SYS_USB_CONNECTED;
506 case SYS_POWEROFF:
507 if (!clean_shutdown(callback, parameter))
508 return SYS_POWEROFF;
509 break;
510 #ifdef HAVE_CHARGING
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;
518 #endif
520 return 0;
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
531 char version[32];
532 int font_h, font_w;
534 lcd_clear_display();
535 #if LCD_WIDTH == 112 || LCD_WIDTH == 128
536 lcd_bitmap(rockbox112x37, 0, 10, 112, 37);
537 #endif
538 #if LCD_WIDTH >= 160
539 lcd_bitmap(rockbox160x53x2, 0, 10, 160, 53);
540 #endif
542 #ifdef HAVE_REMOTE_LCD
543 lcd_remote_clear_display();
544 lcd_remote_bitmap(rockbox112x37,10,14,112,37);
545 #endif
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);
552 lcd_update();
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);
559 lcd_remote_update();
560 #endif
562 #else
563 char *rockbox = " ROCKbox!";
564 lcd_clear_display();
565 lcd_double_height(true);
566 lcd_puts(0, 0, rockbox);
567 lcd_puts(0, 1, appsversion);
568 #endif
570 return 0;