bump version to m1.0.5 release
[Rockbox.git] / apps / misc.c
blobf6e5e6b8801b288d9feca6cb72c86f9838f86327
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 "config.h"
22 #include "lcd.h"
23 #include "file.h"
24 #ifdef __PCTOOL__
25 #include <stdarg.h>
26 #else
27 #include "sprintf.h"
28 #include "lang.h"
29 #include "string.h"
30 #include "dir.h"
31 #include "lcd-remote.h"
32 #include "errno.h"
33 #include "system.h"
34 #include "timefuncs.h"
35 #include "screens.h"
36 #include "talk.h"
37 #include "mpeg.h"
38 #include "audio.h"
39 #include "mp3_playback.h"
40 #include "settings.h"
41 #include "ata.h"
42 #include "ata_idle_notify.h"
43 #include "kernel.h"
44 #include "power.h"
45 #include "powermgmt.h"
46 #include "backlight.h"
47 #include "atoi.h"
48 #include "version.h"
49 #include "font.h"
50 #include "splash.h"
51 #include "tagcache.h"
52 #include "scrobbler.h"
53 #include "sound.h"
55 #ifdef HAVE_MMC
56 #include "ata_mmc.h"
57 #endif
58 #include "tree.h"
59 #include "eeprom_settings.h"
60 #if defined(HAVE_RECORDING) && !defined(__PCTOOL__)
61 #include "recording.h"
62 #endif
63 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
64 #include "bmp.h"
65 #include "icons.h"
66 #endif /* End HAVE_LCD_BITMAP */
67 #include "gui/gwps-common.h"
68 #include "bookmark.h"
70 #include "misc.h"
71 #include "playback.h"
73 #ifdef BOOTFILE
74 #if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
75 #include "textarea.h"
76 #include "rolo.h"
77 #include "yesno.h"
78 #endif
79 #endif
81 /* Format a large-range value for output, using the appropriate unit so that
82 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
83 * units) if possible, and 3 significant digits are shown. If a buffer is
84 * given, the result is snprintf()'d into that buffer, otherwise the result is
85 * voiced.*/
86 char *output_dyn_value(char *buf, int buf_size, int value,
87 const unsigned char **units, bool bin_scale)
89 int scale = bin_scale ? 1024 : 1000;
90 int fraction = 0;
91 int unit_no = 0;
92 char tbuf[5];
94 while (value >= scale)
96 fraction = value % scale;
97 value /= scale;
98 unit_no++;
100 if (bin_scale)
101 fraction = fraction * 1000 / 1024;
103 if (value >= 100 || !unit_no)
104 tbuf[0] = '\0';
105 else if (value >= 10)
106 snprintf(tbuf, sizeof(tbuf), "%01d", fraction / 100);
107 else
108 snprintf(tbuf, sizeof(tbuf), "%02d", fraction / 10);
110 if (buf)
112 if (strlen(tbuf))
113 snprintf(buf, buf_size, "%d%s%s%s", value, str(LANG_POINT),
114 tbuf, P2STR(units[unit_no]));
115 else
116 snprintf(buf, buf_size, "%d%s", value, P2STR(units[unit_no]));
118 else
120 talk_fractional(tbuf, value, P2ID(units[unit_no]));
122 return buf;
125 /* Create a filename with a number part in a way that the number is 1
126 * higher than the highest numbered file matching the same pattern.
127 * It is allowed that buffer and path point to the same memory location,
128 * saving a strcpy(). Path must always be given without trailing slash.
129 * "num" can point to an int specifying the number to use or NULL or a value
130 * less than zero to number automatically. The final number used will also
131 * be returned in *num. If *num is >= 0 then *num will be incremented by
132 * one. */
133 char *create_numbered_filename(char *buffer, const char *path,
134 const char *prefix, const char *suffix,
135 int numberlen IF_CNFN_NUM_(, int *num))
137 DIR *dir;
138 struct dirent *entry;
139 int max_num;
140 int pathlen;
141 int prefixlen = strlen(prefix);
142 char fmtstring[12];
144 if (buffer != path)
145 strncpy(buffer, path, MAX_PATH);
147 pathlen = strlen(buffer);
149 #ifdef IF_CNFN_NUM
150 if (num && *num >= 0)
152 /* number specified */
153 max_num = *num;
155 else
156 #endif
158 /* automatic numbering */
159 max_num = 0;
161 dir = opendir(pathlen ? buffer : "/");
162 if (!dir)
163 return NULL;
165 while ((entry = readdir(dir)))
167 int curr_num;
169 if (strncasecmp((char *)entry->d_name, prefix, prefixlen)
170 || strcasecmp((char *)entry->d_name + prefixlen + numberlen, suffix))
171 continue;
173 curr_num = atoi((char *)entry->d_name + prefixlen);
174 if (curr_num > max_num)
175 max_num = curr_num;
178 closedir(dir);
181 max_num++;
183 snprintf(fmtstring, sizeof(fmtstring), "/%%s%%0%dd%%s", numberlen);
184 snprintf(buffer + pathlen, MAX_PATH - pathlen, fmtstring, prefix,
185 max_num, suffix);
187 #ifdef IF_CNFN_NUM
188 if (num)
189 *num = max_num;
190 #endif
192 return buffer;
195 /* Format time into buf.
197 * buf - buffer to format to.
198 * buf_size - size of buffer.
199 * t - time to format, in milliseconds.
201 void format_time(char* buf, int buf_size, long t)
203 if ( t < 3600000 )
205 snprintf(buf, buf_size, "%d:%02d",
206 (int) (t / 60000), (int) (t % 60000 / 1000));
208 else
210 snprintf(buf, buf_size, "%d:%02d:%02d",
211 (int) (t / 3600000), (int) (t % 3600000 / 60000),
212 (int) (t % 60000 / 1000));
216 #if CONFIG_RTC
217 /* Create a filename with a date+time part.
218 It is allowed that buffer and path point to the same memory location,
219 saving a strcpy(). Path must always be given without trailing slash.
220 unique_time as true makes the function wait until the current time has
221 changed. */
222 char *create_datetime_filename(char *buffer, const char *path,
223 const char *prefix, const char *suffix,
224 bool unique_time)
226 struct tm *tm = get_time();
227 static struct tm last_tm;
228 int pathlen;
230 while (unique_time && !memcmp(get_time(), &last_tm, sizeof (struct tm)))
231 sleep(HZ/10);
233 last_tm = *tm;
235 if (buffer != path)
236 strncpy(buffer, path, MAX_PATH);
238 pathlen = strlen(buffer);
239 snprintf(buffer + pathlen, MAX_PATH - pathlen,
240 "/%s%02d%02d%02d-%02d%02d%02d%s", prefix,
241 tm->tm_year % 100, tm->tm_mon + 1, tm->tm_mday,
242 tm->tm_hour, tm->tm_min, tm->tm_sec, suffix);
244 return buffer;
246 #endif /* CONFIG_RTC */
248 /* Read (up to) a line of text from fd into buffer and return number of bytes
249 * read (which may be larger than the number of bytes stored in buffer). If
250 * an error occurs, -1 is returned (and buffer contains whatever could be
251 * read). A line is terminated by a LF char. Neither LF nor CR chars are
252 * stored in buffer.
254 int read_line(int fd, char* buffer, int buffer_size)
256 int count = 0;
257 int num_read = 0;
259 errno = 0;
261 while (count < buffer_size)
263 unsigned char c;
265 if (1 != read(fd, &c, 1))
266 break;
268 num_read++;
270 if ( c == '\n' )
271 break;
273 if ( c == '\r' )
274 continue;
276 buffer[count++] = c;
279 buffer[MIN(count, buffer_size - 1)] = 0;
281 return errno ? -1 : num_read;
284 /* Performance optimized version of the previous function. */
285 int fast_readline(int fd, char *buf, int buf_size, void *parameters,
286 int (*callback)(int n, const char *buf, void *parameters))
288 char *p, *next;
289 int rc, pos = 0;
290 int count = 0;
292 while ( 1 )
294 next = NULL;
296 rc = read(fd, &buf[pos], buf_size - pos - 1);
297 if (rc >= 0)
298 buf[pos+rc] = '\0';
300 if ( (p = strchr(buf, '\r')) != NULL)
302 *p = '\0';
303 next = ++p;
305 else
306 p = buf;
308 if ( (p = strchr(p, '\n')) != NULL)
310 *p = '\0';
311 next = ++p;
314 rc = callback(count, buf, parameters);
315 if (rc < 0)
316 return rc;
318 count++;
319 if (next)
321 pos = buf_size - ((long)next - (long)buf) - 1;
322 memmove(buf, next, pos);
324 else
325 break ;
328 return 0;
331 #ifdef HAVE_LCD_BITMAP
333 #if LCD_DEPTH == 16
334 #define BMP_COMPRESSION 3 /* BI_BITFIELDS */
335 #define BMP_NUMCOLORS 3
336 #else
337 #define BMP_COMPRESSION 0 /* BI_RGB */
338 #if LCD_DEPTH <= 8
339 #define BMP_NUMCOLORS (1 << LCD_DEPTH)
340 #else
341 #define BMP_NUMCOLORS 0
342 #endif
343 #endif
345 #if LCD_DEPTH == 1
346 #define BMP_BPP 1
347 #define BMP_LINESIZE ((LCD_WIDTH/8 + 3) & ~3)
348 #elif LCD_DEPTH <= 4
349 #define BMP_BPP 4
350 #define BMP_LINESIZE ((LCD_WIDTH/2 + 3) & ~3)
351 #elif LCD_DEPTH <= 8
352 #define BMP_BPP 8
353 #define BMP_LINESIZE ((LCD_WIDTH + 3) & ~3)
354 #elif LCD_DEPTH <= 16
355 #define BMP_BPP 16
356 #define BMP_LINESIZE ((LCD_WIDTH*2 + 3) & ~3)
357 #else
358 #define BMP_BPP 24
359 #define BMP_LINESIZE ((LCD_WIDTH*3 + 3) & ~3)
360 #endif
362 #define BMP_HEADERSIZE (54 + 4 * BMP_NUMCOLORS)
363 #define BMP_DATASIZE (BMP_LINESIZE * LCD_HEIGHT)
364 #define BMP_TOTALSIZE (BMP_HEADERSIZE + BMP_DATASIZE)
366 #define LE16_CONST(x) (x)&0xff, ((x)>>8)&0xff
367 #define LE32_CONST(x) (x)&0xff, ((x)>>8)&0xff, ((x)>>16)&0xff, ((x)>>24)&0xff
369 static const unsigned char bmpheader[] =
371 0x42, 0x4d, /* 'BM' */
372 LE32_CONST(BMP_TOTALSIZE), /* Total file size */
373 0x00, 0x00, 0x00, 0x00, /* Reserved */
374 LE32_CONST(BMP_HEADERSIZE), /* Offset to start of pixel data */
376 0x28, 0x00, 0x00, 0x00, /* Size of (2nd) header */
377 LE32_CONST(LCD_WIDTH), /* Width in pixels */
378 LE32_CONST(LCD_HEIGHT), /* Height in pixels */
379 0x01, 0x00, /* Number of planes (always 1) */
380 LE16_CONST(BMP_BPP), /* Bits per pixel 1/4/8/16/24 */
381 LE32_CONST(BMP_COMPRESSION),/* Compression mode */
382 LE32_CONST(BMP_DATASIZE), /* Size of bitmap data */
383 0xc4, 0x0e, 0x00, 0x00, /* Horizontal resolution (pixels/meter) */
384 0xc4, 0x0e, 0x00, 0x00, /* Vertical resolution (pixels/meter) */
385 LE32_CONST(BMP_NUMCOLORS), /* Number of used colours */
386 LE32_CONST(BMP_NUMCOLORS), /* Number of important colours */
388 #if LCD_DEPTH == 1
389 #ifdef MROBE_100
390 2, 2, 94, 0x00, /* Colour #0 */
391 3, 6, 241, 0x00 /* Colour #1 */
392 #else
393 0x90, 0xee, 0x90, 0x00, /* Colour #0 */
394 0x00, 0x00, 0x00, 0x00 /* Colour #1 */
395 #endif
396 #elif LCD_DEPTH == 2
397 0xe6, 0xd8, 0xad, 0x00, /* Colour #0 */
398 0x99, 0x90, 0x73, 0x00, /* Colour #1 */
399 0x4c, 0x48, 0x39, 0x00, /* Colour #2 */
400 0x00, 0x00, 0x00, 0x00 /* Colour #3 */
401 #elif LCD_DEPTH == 16
402 0x00, 0xf8, 0x00, 0x00, /* red bitfield mask */
403 0xe0, 0x07, 0x00, 0x00, /* green bitfield mask */
404 0x1f, 0x00, 0x00, 0x00 /* blue bitfield mask */
405 #endif
408 static void (*screen_dump_hook)(int fh) = NULL;
410 void screen_dump(void)
412 int fh;
413 char filename[MAX_PATH];
414 int bx, by;
415 #if LCD_DEPTH == 1
416 static unsigned char line_block[8][BMP_LINESIZE];
417 #elif LCD_DEPTH == 2
418 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
419 static unsigned char line_block[BMP_LINESIZE];
420 #elif LCD_PIXELFORMAT == VERTICAL_PACKING
421 static unsigned char line_block[4][BMP_LINESIZE];
422 #elif LCD_PIXELFORMAT == VERTICAL_INTERLEAVED
423 static unsigned char line_block[8][BMP_LINESIZE];
424 #endif
425 #elif LCD_DEPTH == 16
426 static unsigned short line_block[BMP_LINESIZE/2];
427 #endif
429 #if CONFIG_RTC
430 create_datetime_filename(filename, "", "dump ", ".bmp", false);
431 #else
432 create_numbered_filename(filename, "", "dump_", ".bmp", 4
433 IF_CNFN_NUM_(, NULL));
434 #endif
436 fh = creat(filename);
437 if (fh < 0)
438 return;
440 if (screen_dump_hook)
442 screen_dump_hook(fh);
444 else
446 write(fh, bmpheader, sizeof(bmpheader));
448 /* BMP image goes bottom up */
449 #if LCD_DEPTH == 1
450 for (by = LCD_FBHEIGHT - 1; by >= 0; by--)
452 unsigned char *src = &lcd_framebuffer[by][0];
453 unsigned char *dst = &line_block[0][0];
455 memset(line_block, 0, sizeof(line_block));
456 for (bx = LCD_WIDTH/8; bx > 0; bx--)
458 unsigned dst_mask = 0x80;
459 int ix;
461 for (ix = 8; ix > 0; ix--)
463 unsigned char *dst_blk = dst;
464 unsigned src_byte = *src++;
465 int iy;
467 for (iy = 8; iy > 0; iy--)
469 if (src_byte & 0x80)
470 *dst_blk |= dst_mask;
471 src_byte <<= 1;
472 dst_blk += BMP_LINESIZE;
474 dst_mask >>= 1;
476 dst++;
479 write(fh, line_block, sizeof(line_block));
481 #elif LCD_DEPTH == 2
482 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
483 for (by = LCD_FBHEIGHT - 1; by >= 0; by--)
485 unsigned char *src = &lcd_framebuffer[by][0];
486 unsigned char *dst = line_block;
488 memset(line_block, 0, sizeof(line_block));
489 for (bx = LCD_FBWIDTH; bx > 0; bx--)
491 unsigned src_byte = *src++;
493 *dst++ = ((src_byte >> 2) & 0x30) | ((src_byte >> 4) & 0x03);
494 *dst++ = ((src_byte << 2) & 0x30) | (src_byte & 0x03);
497 write(fh, line_block, sizeof(line_block));
499 #elif LCD_PIXELFORMAT == VERTICAL_PACKING
500 for (by = LCD_FBHEIGHT - 1; by >= 0; by--)
502 unsigned char *src = &lcd_framebuffer[by][0];
503 unsigned char *dst = &line_block[3][0];
505 memset(line_block, 0, sizeof(line_block));
506 for (bx = LCD_WIDTH/2; bx > 0; bx--)
508 unsigned char *dst_blk = dst++;
509 unsigned src_byte0 = *src++ << 4;
510 unsigned src_byte1 = *src++;
511 int iy;
513 for (iy = 4; iy > 0; iy--)
515 *dst_blk = (src_byte0 & 0x30) | (src_byte1 & 0x03);
516 src_byte0 >>= 2;
517 src_byte1 >>= 2;
518 dst_blk -= BMP_LINESIZE;
522 write(fh, line_block, sizeof(line_block));
524 #elif LCD_PIXELFORMAT == VERTICAL_INTERLEAVED
525 for (by = LCD_FBHEIGHT - 1; by >= 0; by--)
527 const fb_data *src = &lcd_framebuffer[by][0];
528 unsigned char *dst = &line_block[7][0];
530 memset(line_block, 0, sizeof(line_block));
531 for (bx = LCD_WIDTH/2; bx > 0; bx--)
533 unsigned char *dst_blk = dst++;
534 unsigned src_data0 = *src++ << 4;
535 unsigned src_data1 = *src++;
536 int iy;
538 for (iy = 8; iy > 0; iy--)
540 *dst_blk = (src_data0 & 0x10) | (src_data1 & 0x01)
541 | ((src_data0 & 0x1000) | (src_data1 & 0x0100)) >> 7;
542 src_data0 >>= 1;
543 src_data1 >>= 1;
544 dst_blk -= BMP_LINESIZE;
548 write(fh, line_block, sizeof(line_block));
550 #endif
551 #elif LCD_DEPTH == 16
552 for (by = LCD_HEIGHT - 1; by >= 0; by--)
554 unsigned short *src = &lcd_framebuffer[by][0];
555 unsigned short *dst = line_block;
557 memset(line_block, 0, sizeof(line_block));
558 for (bx = LCD_WIDTH; bx > 0; bx--)
560 #if (LCD_PIXELFORMAT == RGB565SWAPPED)
561 /* iPod LCD data is big endian although the CPU is not */
562 *dst++ = htobe16(*src++);
563 #else
564 *dst++ = htole16(*src++);
565 #endif
568 write(fh, line_block, sizeof(line_block));
570 #endif /* LCD_DEPTH */
573 close(fh);
576 void screen_dump_set_hook(void (*hook)(int fh))
578 screen_dump_hook = hook;
581 #endif /* HAVE_LCD_BITMAP */
583 /* parse a line from a configuration file. the line format is:
585 name: value
587 Any whitespace before setting name or value (after ':') is ignored.
588 A # as first non-whitespace character discards the whole line.
589 Function sets pointers to null-terminated setting name and value.
590 Returns false if no valid config entry was found.
593 bool settings_parseline(char* line, char** name, char** value)
595 char* ptr;
597 while ( isspace(*line) )
598 line++;
600 if ( *line == '#' )
601 return false;
603 ptr = strchr(line, ':');
604 if ( !ptr )
605 return false;
607 *name = line;
608 *ptr = 0;
609 ptr++;
610 while (isspace(*ptr))
611 ptr++;
612 *value = ptr;
613 return true;
616 static void system_flush(void)
618 tree_flush();
619 call_ata_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
622 static void system_restore(void)
624 tree_restore();
627 static bool clean_shutdown(void (*callback)(void *), void *parameter)
629 #ifdef SIMULATOR
630 (void)callback;
631 (void)parameter;
632 bookmark_autobookmark();
633 call_ata_idle_notifys(true);
634 exit(0);
635 #else
636 long msg_id = -1;
637 int i;
639 scrobbler_poweroff();
641 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
642 if(!charger_inserted())
643 #endif
645 bool batt_safe = battery_level_safe();
646 int audio_stat = audio_status();
648 FOR_NB_SCREENS(i)
649 screens[i].clear_display();
651 if (batt_safe)
653 #ifdef HAVE_TAGCACHE
654 if (!tagcache_prepare_shutdown())
656 cancel_shutdown();
657 gui_syncsplash(HZ, ID2P(LANG_TAGCACHE_BUSY));
658 return false;
660 #endif
661 if (battery_level() > 10)
662 gui_syncsplash(0, str(LANG_SHUTTINGDOWN));
663 else
665 msg_id = LANG_WARNING_BATTERY_LOW;
666 gui_syncsplash(0, "%s %s",
667 str(LANG_WARNING_BATTERY_LOW),
668 str(LANG_SHUTTINGDOWN));
671 else
673 msg_id = LANG_WARNING_BATTERY_EMPTY;
674 gui_syncsplash(0, "%s %s",
675 str(LANG_WARNING_BATTERY_EMPTY),
676 str(LANG_SHUTTINGDOWN));
679 if (global_settings.fade_on_stop
680 && (audio_stat & AUDIO_STATUS_PLAY))
682 fade(0);
685 if (batt_safe) /* do not save on critical battery */
687 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
688 if (audio_stat & AUDIO_STATUS_RECORD)
690 rec_command(RECORDING_CMD_STOP);
691 /* wait for stop to complete */
692 while (audio_status() & AUDIO_STATUS_RECORD)
693 sleep(1);
695 #endif
696 bookmark_autobookmark();
698 /* audio_stop_recording == audio_stop for HWCODEC */
699 audio_stop();
701 if (callback != NULL)
702 callback(parameter);
704 #if CONFIG_CODEC != SWCODEC
705 /* wait for audio_stop or audio_stop_recording to complete */
706 while (audio_status())
707 sleep(1);
708 #endif
710 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
711 audio_close_recording();
712 #endif
714 if(global_settings.talk_menu)
716 bool enqueue = false;
717 if(msg_id != -1)
719 talk_id(msg_id, enqueue);
720 enqueue = true;
722 talk_id(LANG_SHUTTINGDOWN, enqueue);
723 #if CONFIG_CODEC == SWCODEC
724 voice_wait();
725 #endif
728 system_flush();
729 #ifdef HAVE_EEPROM_SETTINGS
730 if (firmware_settings.initialized)
732 firmware_settings.disk_clean = true;
733 firmware_settings.bl_version = 0;
734 eeprom_settings_store();
736 #endif
738 #ifdef HAVE_DIRCACHE
739 else
740 dircache_disable();
741 #endif
743 shutdown_hw();
745 #endif
746 return false;
749 bool list_stop_handler(void)
751 bool ret = false;
753 /* Stop the music if it is playing */
754 if(audio_status())
756 if (!global_settings.party_mode)
758 if (global_settings.fade_on_stop)
759 fade(0);
760 bookmark_autobookmark();
761 audio_stop();
762 ret = true; /* bookmarking can make a refresh necessary */
765 #if CONFIG_CHARGING
766 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
767 else
769 if (charger_inserted())
770 charging_splash();
771 else
772 shutdown_screen(); /* won't return if shutdown actually happens */
774 ret = true; /* screen is dirty, caller needs to refresh */
776 #endif
777 #ifndef HAVE_POWEROFF_WHILE_CHARGING
779 static long last_off = 0;
781 if (TIME_BEFORE(current_tick, last_off + HZ/2))
783 if (charger_inserted())
785 charging_splash();
786 ret = true; /* screen is dirty, caller needs to refresh */
789 last_off = current_tick;
791 #endif
792 #endif /* CONFIG_CHARGING */
793 return ret;
796 #if CONFIG_CHARGING
797 static bool waiting_to_resume_play = false;
798 static long play_resume_tick;
800 static void car_adapter_mode_processing(bool inserted)
802 if (global_settings.car_adapter_mode)
804 if(inserted)
807 * Just got plugged in, delay & resume if we were playing
809 if (audio_status() & AUDIO_STATUS_PAUSE)
811 /* delay resume a bit while the engine is cranking */
812 play_resume_tick = current_tick + HZ*5;
813 waiting_to_resume_play = true;
816 else
819 * Just got unplugged, pause if playing
821 if ((audio_status() & AUDIO_STATUS_PLAY) &&
822 !(audio_status() & AUDIO_STATUS_PAUSE))
824 if (global_settings.fade_on_stop)
825 fade(0);
826 else
827 audio_pause();
829 waiting_to_resume_play = false;
834 static void car_adapter_tick(void)
836 if (waiting_to_resume_play)
838 if (TIME_AFTER(current_tick, play_resume_tick))
840 if (audio_status() & AUDIO_STATUS_PAUSE)
842 queue_broadcast(SYS_CAR_ADAPTER_RESUME, 0);
844 waiting_to_resume_play = false;
849 void car_adapter_mode_init(void)
851 tick_add_task(car_adapter_tick);
853 #endif
855 #ifdef HAVE_HEADPHONE_DETECTION
856 static void unplug_change(bool inserted)
858 static bool headphone_caused_pause = false;
860 if (global_settings.unplug_mode)
862 int audio_stat = audio_status();
863 if (inserted)
865 if ((audio_stat & AUDIO_STATUS_PLAY) &&
866 headphone_caused_pause &&
867 global_settings.unplug_mode > 1 )
868 audio_resume();
869 backlight_on();
870 headphone_caused_pause = false;
871 } else {
872 if ((audio_stat & AUDIO_STATUS_PLAY) &&
873 !(audio_stat & AUDIO_STATUS_PAUSE))
875 headphone_caused_pause = true;
876 audio_pause();
878 if (global_settings.unplug_rw)
880 if (audio_current_track()->elapsed >
881 (unsigned long)(global_settings.unplug_rw*1000))
882 audio_ff_rewind(audio_current_track()->elapsed -
883 (global_settings.unplug_rw*1000));
884 else
885 audio_ff_rewind(0);
891 #endif
893 long default_event_handler_ex(long event, void (*callback)(void *), void *parameter)
895 switch(event)
897 case SYS_BATTERY_UPDATE:
898 if(global_settings.talk_battery_level)
900 talk_ids(true, VOICE_PAUSE, VOICE_PAUSE,
901 LANG_BATTERY_TIME,
902 TALK_ID(battery_level(), UNIT_PERCENT),
903 VOICE_PAUSE);
904 talk_force_enqueue_next();
906 break;
907 case SYS_USB_CONNECTED:
908 if (callback != NULL)
909 callback(parameter);
910 #ifdef HAVE_MMC
911 if (!mmc_touched() ||
912 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
913 #endif
915 scrobbler_flush_cache();
916 system_flush();
917 #ifdef BOOTFILE
918 #if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
919 check_bootfile(false); /* gets initial size */
920 #endif
921 #endif
922 usb_screen();
923 #ifdef BOOTFILE
924 #if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
925 check_bootfile(true);
926 #endif
927 #endif
928 system_restore();
930 return SYS_USB_CONNECTED;
931 case SYS_POWEROFF:
932 if (!clean_shutdown(callback, parameter))
933 return SYS_POWEROFF;
934 break;
935 #if CONFIG_CHARGING
936 case SYS_CHARGER_CONNECTED:
937 car_adapter_mode_processing(true);
938 return SYS_CHARGER_CONNECTED;
940 case SYS_CHARGER_DISCONNECTED:
941 car_adapter_mode_processing(false);
942 return SYS_CHARGER_DISCONNECTED;
944 case SYS_CAR_ADAPTER_RESUME:
945 audio_resume();
946 return SYS_CAR_ADAPTER_RESUME;
947 #endif
948 #ifdef HAVE_HEADPHONE_DETECTION
949 case SYS_PHONE_PLUGGED:
950 unplug_change(true);
951 return SYS_PHONE_PLUGGED;
953 case SYS_PHONE_UNPLUGGED:
954 unplug_change(false);
955 return SYS_PHONE_UNPLUGGED;
956 #endif
958 return 0;
961 long default_event_handler(long event)
963 return default_event_handler_ex(event, NULL, NULL);
966 int show_logo( void )
968 #ifdef HAVE_LCD_BITMAP
969 char version[32];
970 int font_h, font_w;
972 snprintf(version, sizeof(version), "Ver. %s", appsversion);
974 lcd_clear_display();
975 lcd_bitmap(rockboxlogo, 0, 10, BMPWIDTH_rockboxlogo, BMPHEIGHT_rockboxlogo);
976 lcd_setfont(FONT_SYSFIXED);
977 lcd_getstringsize((unsigned char *)"A", &font_w, &font_h);
978 lcd_putsxy((LCD_WIDTH/2) - ((strlen(version)*font_w)/2),
979 LCD_HEIGHT-font_h, (unsigned char *)version);
980 lcd_setfont(FONT_UI);
982 #else
983 char *rockbox = " ROCKbox!";
985 lcd_clear_display();
986 lcd_double_height(true);
987 lcd_puts(0, 0, rockbox);
988 lcd_puts_scroll(0, 1, appsversion);
989 #endif
990 lcd_update();
992 #ifdef HAVE_REMOTE_LCD
993 lcd_remote_clear_display();
994 lcd_remote_bitmap(remote_rockboxlogo, 0, 10, BMPWIDTH_remote_rockboxlogo,
995 BMPHEIGHT_remote_rockboxlogo);
996 lcd_remote_setfont(FONT_SYSFIXED);
997 lcd_remote_getstringsize((unsigned char *)"A", &font_w, &font_h);
998 lcd_remote_putsxy((LCD_REMOTE_WIDTH/2) - ((strlen(version)*font_w)/2),
999 LCD_REMOTE_HEIGHT-font_h, (unsigned char *)version);
1000 lcd_remote_setfont(FONT_UI);
1001 lcd_remote_update();
1002 #endif
1004 return 0;
1007 #if CONFIG_CODEC == SWCODEC
1008 int get_replaygain_mode(bool have_track_gain, bool have_album_gain)
1010 int type;
1012 bool track = ((global_settings.replaygain_type == REPLAYGAIN_TRACK)
1013 || ((global_settings.replaygain_type == REPLAYGAIN_SHUFFLE)
1014 && global_settings.playlist_shuffle));
1016 type = (!track && have_album_gain) ? REPLAYGAIN_ALBUM
1017 : have_track_gain ? REPLAYGAIN_TRACK : -1;
1019 return type;
1021 #endif
1023 #ifdef BOOTFILE
1024 #if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
1026 memorize/compare details about the BOOTFILE
1027 we don't use dircache because it may not be up to date after
1028 USB disconnect (scanning in the background)
1030 void check_bootfile(bool do_rolo)
1032 static unsigned short wrtdate = 0;
1033 static unsigned short wrttime = 0;
1034 DIR* dir = NULL;
1035 struct dirent* entry = NULL;
1037 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
1038 dir = opendir(BOOTDIR);
1040 if(!dir) return; /* do we want an error splash? */
1042 /* loop all files in BOOTDIR */
1043 while(0 != (entry = readdir(dir)))
1045 if(!strcasecmp(entry->d_name, BOOTFILE))
1047 /* found the bootfile */
1048 if(wrtdate && do_rolo)
1050 if((entry->wrtdate != wrtdate) ||
1051 (entry->wrttime != wrttime))
1053 char *lines[] = { ID2P(LANG_BOOT_CHANGED),
1054 ID2P(LANG_REBOOT_NOW) };
1055 struct text_message message={ lines, 2 };
1056 button_clear_queue(); /* Empty the keyboard buffer */
1057 if(gui_syncyesno_run(&message, NULL, NULL) == YESNO_YES)
1058 rolo_load(BOOTDIR "/" BOOTFILE);
1061 wrtdate = entry->wrtdate;
1062 wrttime = entry->wrttime;
1065 closedir(dir);
1067 #endif
1068 #endif
1070 /* check range, set volume and save settings */
1071 void setvol(void)
1073 const int min_vol = sound_min(SOUND_VOLUME);
1074 const int max_vol = sound_max(SOUND_VOLUME);
1075 if (global_settings.volume < min_vol)
1076 global_settings.volume = min_vol;
1077 if (global_settings.volume > max_vol)
1078 global_settings.volume = max_vol;
1079 sound_set_volume(global_settings.volume);
1080 settings_save();
1083 char* strrsplt(char* str, int c)
1085 char* s = strrchr(str, c);
1087 if (s != NULL)
1089 *s++ = '\0';
1091 else
1093 s = str;
1096 return s;
1099 /* Test file existence, using dircache of possible */
1100 bool file_exists(const char *file)
1102 int fd;
1104 if (!file || strlen(file) <= 0)
1105 return false;
1107 #ifdef HAVE_DIRCACHE
1108 if (dircache_is_enabled())
1109 return (dircache_get_entry_ptr(file) != NULL);
1110 #endif
1112 fd = open(file, O_RDONLY);
1113 if (fd < 0)
1114 return false;
1115 close(fd);
1116 return true;
1119 bool dir_exists(const char *path)
1121 DIR* d = opendir(path);
1122 if (!d)
1123 return false;
1124 closedir(d);
1125 return true;
1129 * removes the extension of filename (if it doesn't start with a .)
1130 * puts the result in buffer
1132 char *strip_extension(char* buffer, int buffer_size, const char *filename)
1134 char *dot = strrchr(filename, '.');
1135 int len;
1137 if (buffer_size <= 0)
1139 return NULL;
1142 buffer_size--; /* Make room for end nil */
1144 if (dot != 0 && filename[0] != '.')
1146 len = dot - filename;
1147 len = MIN(len, buffer_size);
1148 strncpy(buffer, filename, len);
1150 else
1152 len = buffer_size;
1153 strncpy(buffer, filename, buffer_size);
1156 buffer[len] = 0;
1158 return buffer;
1160 #endif /* !defined(__PCTOOL__) */
1162 #ifdef HAVE_LCD_COLOR
1164 * Helper function to convert a string of 6 hex digits to a native colour
1167 static int hex2dec(int c)
1169 return (((c) >= '0' && ((c) <= '9')) ? (c) - '0' :
1170 (toupper(c)) - 'A' + 10);
1173 int hex_to_rgb(const char* hex, int* color)
1175 int red, green, blue;
1176 int i = 0;
1178 while ((i < 6) && (isxdigit(hex[i])))
1179 i++;
1181 if (i < 6)
1182 return -1;
1184 red = (hex2dec(hex[0]) << 4) | hex2dec(hex[1]);
1185 green = (hex2dec(hex[2]) << 4) | hex2dec(hex[3]);
1186 blue = (hex2dec(hex[4]) << 4) | hex2dec(hex[5]);
1188 *color = LCD_RGBPACK(red,green,blue);
1190 return 0;
1192 #endif /* HAVE_LCD_COLOR */
1194 #ifdef HAVE_LCD_BITMAP
1195 /* A simplified scanf - used (at time of writing) by wps parsing functions.
1197 fmt - char array specifying the format of each list option. Valid values
1198 are: d - int
1199 s - string (sets pointer to string, without copying)
1200 c - hex colour (RGB888 - e.g. ff00ff)
1201 g - greyscale "colour" (0-3)
1203 sep - list separator (e.g. ',' or '|')
1204 str - string to parse, must be terminated by 0 or sep
1205 ... - pointers to store the parsed values
1207 return value - pointer to char after parsed data, 0 if there was an error.
1211 /* '0'-'3' are ASCII 0x30 to 0x33 */
1212 #define is0123(x) (((x) & 0xfc) == 0x30)
1214 const char* parse_list(const char *fmt, const char sep, const char* str, ...)
1216 va_list ap;
1217 const char* p = str;
1218 const char** s;
1219 int* d;
1221 va_start(ap, str);
1223 while (*fmt)
1225 /* Check for separator, if we're not at the start */
1226 if (p != str)
1228 if (*p != sep)
1229 goto err;
1230 p++;
1233 switch (*fmt++)
1235 case 's': /* string - return a pointer to it (not a copy) */
1236 s = va_arg(ap, const char **);
1238 *s = p;
1239 while (*p && *p != sep)
1240 p++;
1242 break;
1244 case 'd': /* int */
1245 d = va_arg(ap, int*);
1246 if (!isdigit(*p))
1247 goto err;
1249 *d = *p++ - '0';
1251 while (isdigit(*p))
1252 *d = (*d * 10) + (*p++ - '0');
1254 break;
1256 #ifdef HAVE_LCD_COLOR
1257 case 'c': /* colour (rrggbb - e.g. f3c1a8) */
1258 d = va_arg(ap, int*);
1260 if (hex_to_rgb(p, d) < 0)
1261 goto err;
1263 p += 6;
1265 break;
1266 #endif
1268 #if LCD_DEPTH == 2 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 2)
1269 case 'g': /* greyscale colour (0-3) */
1270 d = va_arg(ap, int*);
1272 if (is0123(*p))
1273 *d = *p++ - '0';
1274 else
1275 goto err;
1277 break;
1278 #endif
1280 default: /* Unknown format type */
1281 goto err;
1282 break;
1286 va_end(ap);
1287 return p;
1289 err:
1290 va_end(ap);
1291 return 0;
1293 #endif