Update several codec Makefiles so that the codec libs build again on Coldfire targets...
[Rockbox.git] / apps / misc.c
blobc36d61914b6a5a1a8bd073c932deb3b7e9145dd8
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 #include "tagcache.h"
48 #include "scrobbler.h"
49 #ifdef HAVE_MMC
50 #include "ata_mmc.h"
51 #endif
52 #include "tree.h"
53 #include "eeprom_settings.h"
55 #ifdef HAVE_LCD_BITMAP
56 #include "bmp.h"
57 #include "icons.h"
58 #endif /* End HAVE_LCD_BITMAP */
59 #include "gui/gwps-common.h"
61 /* Format a large-range value for output, using the appropriate unit so that
62 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
63 * units) if possible, and 3 significant digits are shown. If a buffer is
64 * given, the result is snprintf()'d into that buffer, otherwise the result is
65 * voiced.*/
66 char *output_dyn_value(char *buf, int buf_size, int value,
67 const unsigned char **units, bool bin_scale)
69 int scale = bin_scale ? 1024 : 1000;
70 int fraction = 0;
71 int unit_no = 0;
72 int i;
73 char tbuf[5];
75 while (value >= scale)
77 fraction = value % scale;
78 value /= scale;
79 unit_no++;
81 if (bin_scale)
82 fraction = fraction * 1000 / 1024;
84 if (value >= 100 || !unit_no)
85 tbuf[0] = '\0';
86 else if (value >= 10)
87 snprintf(tbuf, sizeof(tbuf), "%01d", fraction / 100);
88 else
89 snprintf(tbuf, sizeof(tbuf), "%02d", fraction / 10);
91 if (buf)
93 if (strlen(tbuf))
94 snprintf(buf, buf_size, "%d%s%s%s", value, str(LANG_POINT),
95 tbuf, P2STR(units[unit_no]));
96 else
97 snprintf(buf, buf_size, "%d%s", value, P2STR(units[unit_no]));
99 else
101 /* strip trailing zeros from the fraction */
102 for (i = strlen(tbuf) - 1; (i >= 0) && (tbuf[i] == '0'); i--)
103 tbuf[i] = '\0';
105 talk_number(value, true);
106 if (tbuf[0] != 0)
108 talk_id(LANG_POINT, true);
109 talk_spell(tbuf, true);
111 talk_id(P2ID(units[unit_no]), true);
113 return buf;
116 /* Create a filename with a number part in a way that the number is 1
117 higher than the highest numbered file matching the same pattern.
118 It is allowed that buffer and path point to the same memory location,
119 saving a strcpy(). Path must always be given without trailing slash,. */
120 char *create_numbered_filename(char *buffer, const char *path,
121 const char *prefix, const char *suffix,
122 int numberlen)
124 DIR *dir;
125 struct dirent *entry;
126 int max_num = 0;
127 int pathlen;
128 int prefixlen = strlen(prefix);
129 char fmtstring[12];
131 if (buffer != path)
132 strncpy(buffer, path, MAX_PATH);
134 pathlen = strlen(buffer);
136 dir = opendir(pathlen ? buffer : "/");
137 if (!dir)
138 return NULL;
140 while ((entry = readdir(dir)))
142 int curr_num;
144 if (strncasecmp((char *)entry->d_name, prefix, prefixlen)
145 || strcasecmp((char *)entry->d_name + prefixlen + numberlen, suffix))
146 continue;
148 curr_num = atoi((char *)entry->d_name + prefixlen);
149 if (curr_num > max_num)
150 max_num = curr_num;
152 closedir(dir);
154 snprintf(fmtstring, sizeof(fmtstring), "/%%s%%0%dd%%s", numberlen);
155 snprintf(buffer + pathlen, MAX_PATH - pathlen, fmtstring, prefix,
156 max_num + 1, suffix);
158 return buffer;
161 #ifdef CONFIG_RTC
162 /* Create a filename with a date+time part.
163 It is allowed that buffer and path point to the same memory location,
164 saving a strcpy(). Path must always be given without trailing slash. */
165 char *create_datetime_filename(char *buffer, const char *path,
166 const char *prefix, const char *suffix)
168 struct tm *tm = get_time();
169 int pathlen;
171 if (buffer != path)
172 strncpy(buffer, path, MAX_PATH);
174 pathlen = strlen(buffer);
175 snprintf(buffer + pathlen, MAX_PATH - pathlen,
176 "/%s%02d%02d%02d-%02d%02d%02d%s", prefix,
177 tm->tm_year % 100, tm->tm_mon + 1, tm->tm_mday,
178 tm->tm_hour, tm->tm_min, tm->tm_sec, suffix);
180 return buffer;
182 #endif /* CONFIG_RTC */
184 /* Read (up to) a line of text from fd into buffer and return number of bytes
185 * read (which may be larger than the number of bytes stored in buffer). If
186 * an error occurs, -1 is returned (and buffer contains whatever could be
187 * read). A line is terminated by a LF char. Neither LF nor CR chars are
188 * stored in buffer.
190 int read_line(int fd, char* buffer, int buffer_size)
192 int count = 0;
193 int num_read = 0;
195 errno = 0;
197 while (count < buffer_size)
199 unsigned char c;
201 if (1 != read(fd, &c, 1))
202 break;
204 num_read++;
206 if ( c == '\n' )
207 break;
209 if ( c == '\r' )
210 continue;
212 buffer[count++] = c;
215 buffer[MIN(count, buffer_size - 1)] = 0;
217 return errno ? -1 : num_read;
220 /* Performance optimized version of the previous function. */
221 int fast_readline(int fd, char *buf, int buf_size, void *parameters,
222 int (*callback)(int n, const char *buf, void *parameters))
224 char *p, *next;
225 int rc, pos = 0;
226 int count = 0;
228 while ( 1 )
230 next = NULL;
232 rc = read(fd, &buf[pos], buf_size - pos - 1);
233 if (rc >= 0)
234 buf[pos+rc] = '\0';
236 if ( (p = strchr(buf, '\r')) != NULL)
238 *p = '\0';
239 next = ++p;
241 else
242 p = buf;
244 if ( (p = strchr(p, '\n')) != NULL)
246 *p = '\0';
247 next = ++p;
250 rc = callback(count, buf, parameters);
251 if (rc < 0)
252 return rc;
254 count++;
255 if (next)
257 pos = buf_size - ((long)next - (long)buf) - 1;
258 memmove(buf, next, pos);
260 else
261 break ;
264 return 0;
267 #ifdef HAVE_LCD_BITMAP
269 #if LCD_DEPTH == 16
270 #define BMP_COMPRESSION 3 /* BI_BITFIELDS */
271 #define BMP_NUMCOLORS 3
272 #else
273 #define BMP_COMPRESSION 0 /* BI_RGB */
274 #if LCD_DEPTH <= 8
275 #define BMP_NUMCOLORS (1 << LCD_DEPTH)
276 #else
277 #define BMP_NUMCOLORS 0
278 #endif
279 #endif
281 #if LCD_DEPTH == 1
282 #define BMP_BPP 1
283 #define BMP_LINESIZE ((LCD_WIDTH/8 + 3) & ~3)
284 #elif LCD_DEPTH <= 4
285 #define BMP_BPP 4
286 #define BMP_LINESIZE ((LCD_WIDTH/2 + 3) & ~3)
287 #elif LCD_DEPTH <= 8
288 #define BMP_BPP 8
289 #define BMP_LINESIZE ((LCD_WIDTH + 3) & ~3)
290 #elif LCD_DEPTH <= 16
291 #define BMP_BPP 16
292 #define BMP_LINESIZE ((LCD_WIDTH*2 + 3) & ~3)
293 #else
294 #define BMP_BPP 24
295 #define BMP_LINESIZE ((LCD_WIDTH*3 + 3) & ~3)
296 #endif
298 #define BMP_HEADERSIZE (54 + 4 * BMP_NUMCOLORS)
299 #define BMP_DATASIZE (BMP_LINESIZE * LCD_HEIGHT)
300 #define BMP_TOTALSIZE (BMP_HEADERSIZE + BMP_DATASIZE)
302 #define LE16_CONST(x) (x)&0xff, ((x)>>8)&0xff
303 #define LE32_CONST(x) (x)&0xff, ((x)>>8)&0xff, ((x)>>16)&0xff, ((x)>>24)&0xff
305 static const unsigned char bmpheader[] =
307 0x42, 0x4d, /* 'BM' */
308 LE32_CONST(BMP_TOTALSIZE), /* Total file size */
309 0x00, 0x00, 0x00, 0x00, /* Reserved */
310 LE32_CONST(BMP_HEADERSIZE), /* Offset to start of pixel data */
312 0x28, 0x00, 0x00, 0x00, /* Size of (2nd) header */
313 LE32_CONST(LCD_WIDTH), /* Width in pixels */
314 LE32_CONST(LCD_HEIGHT), /* Height in pixels */
315 0x01, 0x00, /* Number of planes (always 1) */
316 LE16_CONST(BMP_BPP), /* Bits per pixel 1/4/8/16/24 */
317 LE32_CONST(BMP_COMPRESSION),/* Compression mode */
318 LE32_CONST(BMP_DATASIZE), /* Size of bitmap data */
319 0xc4, 0x0e, 0x00, 0x00, /* Horizontal resolution (pixels/meter) */
320 0xc4, 0x0e, 0x00, 0x00, /* Vertical resolution (pixels/meter) */
321 LE32_CONST(BMP_NUMCOLORS), /* Number of used colours */
322 LE32_CONST(BMP_NUMCOLORS), /* Number of important colours */
324 #if LCD_DEPTH == 1
325 0x90, 0xee, 0x90, 0x00, /* Colour #0 */
326 0x00, 0x00, 0x00, 0x00 /* Colour #1 */
327 #elif LCD_DEPTH == 2
328 0xe6, 0xd8, 0xad, 0x00, /* Colour #0 */
329 0x99, 0x90, 0x73, 0x00, /* Colour #1 */
330 0x4c, 0x48, 0x39, 0x00, /* Colour #2 */
331 0x00, 0x00, 0x00, 0x00 /* Colour #3 */
332 #elif LCD_DEPTH == 16
333 0x00, 0xf8, 0x00, 0x00, /* red bitfield mask */
334 0xe0, 0x07, 0x00, 0x00, /* green bitfield mask */
335 0x1f, 0x00, 0x00, 0x00 /* blue bitfield mask */
336 #endif
339 static void (*screen_dump_hook)(int fh) = NULL;
341 void screen_dump(void)
343 int fh;
344 char filename[MAX_PATH];
345 int bx, by;
346 #if LCD_DEPTH == 1
347 static unsigned char line_block[8][BMP_LINESIZE];
348 #elif LCD_DEPTH == 2
349 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
350 static unsigned char line_block[BMP_LINESIZE];
351 #else
352 static unsigned char line_block[4][BMP_LINESIZE];
353 #endif
354 #elif LCD_DEPTH == 16
355 static unsigned short line_block[BMP_LINESIZE/2];
356 #endif
358 #ifdef CONFIG_RTC
359 create_datetime_filename(filename, "", "dump ", ".bmp");
360 #else
361 create_numbered_filename(filename, "", "dump_", ".bmp", 4);
362 #endif
364 fh = creat(filename, O_WRONLY);
365 if (fh < 0)
366 return;
368 if (screen_dump_hook)
370 screen_dump_hook(fh);
372 else
374 write(fh, bmpheader, sizeof(bmpheader));
376 /* BMP image goes bottom up */
377 #if LCD_DEPTH == 1
378 for (by = LCD_HEIGHT/8 - 1; by >= 0; by--)
380 unsigned char *src = &lcd_framebuffer[by][0];
381 unsigned char *dst = &line_block[0][0];
383 memset(line_block, 0, sizeof(line_block));
384 for (bx = LCD_WIDTH/8; bx > 0; bx--)
386 unsigned dst_mask = 0x80;
387 int ix;
389 for (ix = 8; ix > 0; ix--)
391 unsigned char *dst_blk = dst;
392 unsigned src_byte = *src++;
393 int iy;
395 for (iy = 8; iy > 0; iy--)
397 if (src_byte & 0x80)
398 *dst_blk |= dst_mask;
399 src_byte <<= 1;
400 dst_blk += BMP_LINESIZE;
402 dst_mask >>= 1;
404 dst++;
407 write(fh, line_block, sizeof(line_block));
409 #elif LCD_DEPTH == 2
410 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
411 for (by = LCD_HEIGHT - 1; by >= 0; by--)
413 unsigned char *src = &lcd_framebuffer[by][0];
414 unsigned char *dst = line_block;
416 memset(line_block, 0, sizeof(line_block));
417 for (bx = (LCD_WIDTH+3)/4; bx > 0; bx--)
419 unsigned src_byte = *src++;
421 *dst++ = ((src_byte >> 2) & 0x30) | ((src_byte >> 4) & 0x03);
422 *dst++ = ((src_byte << 2) & 0x30) | (src_byte & 0x03);
425 write(fh, line_block, sizeof(line_block));
427 #else /* VERTICAL_PACKING */
428 for (by = LCD_HEIGHT/4 - 1; by >= 0; by--)
430 unsigned char *src = &lcd_framebuffer[by][0];
431 unsigned char *dst = &line_block[3][0];
433 memset(line_block, 0, sizeof(line_block));
434 for (bx = LCD_WIDTH/2; bx > 0; bx--)
436 unsigned char *dst_blk = dst++;
437 unsigned src_byte0 = *src++;
438 unsigned src_byte1 = *src++;
439 int iy;
441 for (iy = 4; iy > 0; iy--)
443 *dst_blk = ((src_byte0 & 3) << 4) | (src_byte1 & 3);
444 src_byte0 >>= 2;
445 src_byte1 >>= 2;
446 dst_blk -= BMP_LINESIZE;
450 write(fh, line_block, sizeof(line_block));
452 #endif
453 #elif LCD_DEPTH == 16
454 for (by = LCD_HEIGHT - 1; by >= 0; by--)
456 unsigned short *src = &lcd_framebuffer[by][0];
457 unsigned short *dst = line_block;
459 memset(line_block, 0, sizeof(line_block));
460 for (bx = LCD_WIDTH; bx > 0; bx--)
462 #if (LCD_PIXELFORMAT == RGB565SWAPPED)
463 /* iPod LCD data is big endian although the CPU is not */
464 *dst++ = htobe16(*src++);
465 #else
466 *dst++ = htole16(*src++);
467 #endif
470 write(fh, line_block, sizeof(line_block));
472 #endif /* LCD_DEPTH */
475 close(fh);
478 void screen_dump_set_hook(void (*hook)(int fh))
480 screen_dump_hook = hook;
483 #endif /* HAVE_LCD_BITMAP */
485 /* parse a line from a configuration file. the line format is:
487 name: value
489 Any whitespace before setting name or value (after ':') is ignored.
490 A # as first non-whitespace character discards the whole line.
491 Function sets pointers to null-terminated setting name and value.
492 Returns false if no valid config entry was found.
495 bool settings_parseline(char* line, char** name, char** value)
497 char* ptr;
499 while ( isspace(*line) )
500 line++;
502 if ( *line == '#' )
503 return false;
505 ptr = strchr(line, ':');
506 if ( !ptr )
507 return false;
509 *name = line;
510 *ptr = 0;
511 ptr++;
512 while (isspace(*ptr))
513 ptr++;
514 *value = ptr;
515 return true;
518 static void system_flush(void)
520 tree_flush();
523 static void system_restore(void)
525 tree_restore();
528 static bool clean_shutdown(void (*callback)(void *), void *parameter)
530 #ifdef SIMULATOR
531 (void)callback;
532 (void)parameter;
533 exit(0);
534 #else
535 int i;
537 #if defined(CONFIG_CHARGING) && !defined(HAVE_POWEROFF_WHILE_CHARGING)
538 if(!charger_inserted())
539 #endif
541 FOR_NB_SCREENS(i)
542 screens[i].clear_display();
543 #ifdef X5_BACKLIGHT_SHUTDOWN
544 x5_backlight_shutdown();
545 #endif
546 #ifdef HAVE_TAGCACHE
547 if (!tagcache_prepare_shutdown())
549 cancel_shutdown();
550 gui_syncsplash(HZ, true, str(LANG_TAGCACHE_BUSY));
551 return false;
553 #endif
555 gui_syncsplash(0, true, str(LANG_SHUTTINGDOWN));
557 if (global_settings.fade_on_stop
558 && (audio_status() & AUDIO_STATUS_PLAY))
560 fade(0);
563 audio_stop();
564 while (audio_status())
565 sleep(1);
567 if (callback != NULL)
568 callback(parameter);
570 system_flush();
571 #ifdef HAVE_EEPROM_SETTINGS
572 if (firmware_settings.initialized)
574 firmware_settings.disk_clean = true;
575 firmware_settings.bl_version = 0;
576 eeprom_settings_store();
578 #endif
579 shutdown_hw();
581 #endif
582 return false;
585 #ifdef CONFIG_CHARGING
586 static bool waiting_to_resume_play = false;
587 static long play_resume_tick;
589 static void car_adapter_mode_processing(bool inserted)
591 if (global_settings.car_adapter_mode)
593 if(inserted)
596 * Just got plugged in, delay & resume if we were playing
598 if (audio_status() & AUDIO_STATUS_PAUSE)
600 /* delay resume a bit while the engine is cranking */
601 play_resume_tick = current_tick + HZ*5;
602 waiting_to_resume_play = true;
605 else
608 * Just got unplugged, pause if playing
610 if ((audio_status() & AUDIO_STATUS_PLAY) &&
611 !(audio_status() & AUDIO_STATUS_PAUSE))
613 audio_pause();
619 static void car_adapter_tick(void)
621 if (waiting_to_resume_play)
623 if (TIME_AFTER(current_tick, play_resume_tick))
625 if (audio_status() & AUDIO_STATUS_PAUSE)
627 audio_resume();
629 waiting_to_resume_play = false;
634 void car_adapter_mode_init(void)
636 tick_add_task(car_adapter_tick);
638 #endif
640 #ifdef HAVE_HEADPHONE_DETECTION
641 void unplug_change(bool inserted)
643 if (global_settings.unplug_mode)
645 if (inserted)
647 if ( global_settings.unplug_mode > 1 )
648 audio_resume();
649 backlight_on();
650 } else {
651 audio_pause();
653 if (global_settings.unplug_rw)
655 if ( audio_current_track()->elapsed >
656 (unsigned long)(global_settings.unplug_rw*1000))
657 audio_ff_rewind(audio_current_track()->elapsed -
658 (global_settings.unplug_rw*1000));
659 else
660 audio_ff_rewind(0);
665 #endif
667 long default_event_handler_ex(long event, void (*callback)(void *), void *parameter)
669 switch(event)
671 case SYS_USB_CONNECTED:
672 if (callback != NULL)
673 callback(parameter);
674 #ifdef HAVE_MMC
675 if (!mmc_touched() || (mmc_remove_request() == SYS_MMC_EXTRACTED))
676 #endif
678 scrobbler_flush_cache();
679 system_flush();
680 usb_screen();
681 system_restore();
683 return SYS_USB_CONNECTED;
684 case SYS_POWEROFF:
685 if (!clean_shutdown(callback, parameter))
686 return SYS_POWEROFF;
687 break;
688 #ifdef CONFIG_CHARGING
689 case SYS_CHARGER_CONNECTED:
690 car_adapter_mode_processing(true);
691 return SYS_CHARGER_CONNECTED;
693 case SYS_CHARGER_DISCONNECTED:
694 car_adapter_mode_processing(false);
695 return SYS_CHARGER_DISCONNECTED;
696 #endif
697 #ifdef HAVE_HEADPHONE_DETECTION
698 case SYS_PHONE_PLUGGED:
699 unplug_change(true);
700 return SYS_PHONE_PLUGGED;
702 case SYS_PHONE_UNPLUGGED:
703 unplug_change(false);
704 return SYS_PHONE_UNPLUGGED;
705 #endif
707 return 0;
710 long default_event_handler(long event)
712 return default_event_handler_ex(event, NULL, NULL);
715 int show_logo( void )
717 #ifdef HAVE_LCD_BITMAP
718 char version[32];
719 int font_h, font_w;
721 lcd_clear_display();
722 lcd_bitmap(rockboxlogo, 0, 10, BMPWIDTH_rockboxlogo, BMPHEIGHT_rockboxlogo);
724 #ifdef HAVE_REMOTE_LCD
725 lcd_remote_clear_display();
726 lcd_remote_bitmap(remote_rockboxlogo, 0, 10, BMPWIDTH_remote_rockboxlogo,
727 BMPHEIGHT_remote_rockboxlogo);
728 #endif
730 snprintf(version, sizeof(version), "Ver. %s", appsversion);
731 lcd_setfont(FONT_SYSFIXED);
732 lcd_getstringsize((unsigned char *)"A", &font_w, &font_h);
733 lcd_putsxy((LCD_WIDTH/2) - ((strlen(version)*font_w)/2),
734 LCD_HEIGHT-font_h, (unsigned char *)version);
735 lcd_update();
737 #ifdef HAVE_REMOTE_LCD
738 lcd_remote_setfont(FONT_SYSFIXED);
739 lcd_remote_getstringsize((unsigned char *)"A", &font_w, &font_h);
740 lcd_remote_putsxy((LCD_REMOTE_WIDTH/2) - ((strlen(version)*font_w)/2),
741 LCD_REMOTE_HEIGHT-font_h, (unsigned char *)version);
742 lcd_remote_update();
743 #endif
745 #else
746 char *rockbox = " ROCKbox!";
747 lcd_clear_display();
748 lcd_double_height(true);
749 lcd_puts(0, 0, rockbox);
750 lcd_puts(0, 1, appsversion);
751 #endif
753 return 0;