Add missing "Save Sound Settings" and "Save Theme Settings" description (FS#8541...
[Rockbox.git] / apps / misc.c
blob1f8aa99e0df0b4481f82b5736eb5d34071bcf789
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 "ata_idle_notify.h"
40 #include "kernel.h"
41 #include "power.h"
42 #include "powermgmt.h"
43 #include "backlight.h"
44 #include "atoi.h"
45 #include "version.h"
46 #include "font.h"
47 #include "splash.h"
48 #include "tagcache.h"
49 #include "scrobbler.h"
50 #include "sound.h"
51 #ifdef HAVE_MMC
52 #include "ata_mmc.h"
53 #endif
54 #include "tree.h"
55 #include "eeprom_settings.h"
56 #ifdef HAVE_RECORDING
57 #include "recording.h"
58 #endif
59 #ifdef HAVE_LCD_BITMAP
60 #include "bmp.h"
61 #include "icons.h"
62 #endif /* End HAVE_LCD_BITMAP */
63 #include "gui/gwps-common.h"
64 #include "bookmark.h"
66 #include "misc.h"
67 #include "playback.h"
69 #ifdef BOOTFILE
70 #if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
71 #include "textarea.h"
72 #include "rolo.h"
73 #include "yesno.h"
74 #endif
75 #endif
77 /* Format a large-range value for output, using the appropriate unit so that
78 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
79 * units) if possible, and 3 significant digits are shown. If a buffer is
80 * given, the result is snprintf()'d into that buffer, otherwise the result is
81 * voiced.*/
82 char *output_dyn_value(char *buf, int buf_size, int value,
83 const unsigned char **units, bool bin_scale)
85 int scale = bin_scale ? 1024 : 1000;
86 int fraction = 0;
87 int unit_no = 0;
88 int i;
89 char tbuf[5];
91 while (value >= scale)
93 fraction = value % scale;
94 value /= scale;
95 unit_no++;
97 if (bin_scale)
98 fraction = fraction * 1000 / 1024;
100 if (value >= 100 || !unit_no)
101 tbuf[0] = '\0';
102 else if (value >= 10)
103 snprintf(tbuf, sizeof(tbuf), "%01d", fraction / 100);
104 else
105 snprintf(tbuf, sizeof(tbuf), "%02d", fraction / 10);
107 if (buf)
109 if (strlen(tbuf))
110 snprintf(buf, buf_size, "%d%s%s%s", value, str(LANG_POINT),
111 tbuf, P2STR(units[unit_no]));
112 else
113 snprintf(buf, buf_size, "%d%s", value, P2STR(units[unit_no]));
115 else
117 /* strip trailing zeros from the fraction */
118 for (i = strlen(tbuf) - 1; (i >= 0) && (tbuf[i] == '0'); i--)
119 tbuf[i] = '\0';
121 talk_number(value, true);
122 if (tbuf[0] != 0)
124 talk_id(LANG_POINT, true);
125 talk_spell(tbuf, true);
127 talk_id(P2ID(units[unit_no]), true);
129 return buf;
132 /* Create a filename with a number part in a way that the number is 1
133 * higher than the highest numbered file matching the same pattern.
134 * It is allowed that buffer and path point to the same memory location,
135 * saving a strcpy(). Path must always be given without trailing slash.
136 * "num" can point to an int specifying the number to use or NULL or a value
137 * less than zero to number automatically. The final number used will also
138 * be returned in *num. If *num is >= 0 then *num will be incremented by
139 * one. */
140 char *create_numbered_filename(char *buffer, const char *path,
141 const char *prefix, const char *suffix,
142 int numberlen IF_CNFN_NUM_(, int *num))
144 DIR *dir;
145 struct dirent *entry;
146 int max_num;
147 int pathlen;
148 int prefixlen = strlen(prefix);
149 char fmtstring[12];
151 if (buffer != path)
152 strncpy(buffer, path, MAX_PATH);
154 pathlen = strlen(buffer);
156 #ifdef IF_CNFN_NUM
157 if (num && *num >= 0)
159 /* number specified */
160 max_num = *num;
162 else
163 #endif
165 /* automatic numbering */
166 max_num = 0;
168 dir = opendir(pathlen ? buffer : "/");
169 if (!dir)
170 return NULL;
172 while ((entry = readdir(dir)))
174 int curr_num;
176 if (strncasecmp((char *)entry->d_name, prefix, prefixlen)
177 || strcasecmp((char *)entry->d_name + prefixlen + numberlen, suffix))
178 continue;
180 curr_num = atoi((char *)entry->d_name + prefixlen);
181 if (curr_num > max_num)
182 max_num = curr_num;
185 closedir(dir);
188 max_num++;
190 snprintf(fmtstring, sizeof(fmtstring), "/%%s%%0%dd%%s", numberlen);
191 snprintf(buffer + pathlen, MAX_PATH - pathlen, fmtstring, prefix,
192 max_num, suffix);
194 #ifdef IF_CNFN_NUM
195 if (num)
196 *num = max_num;
197 #endif
199 return buffer;
202 /* Format time into buf.
204 * buf - buffer to format to.
205 * buf_size - size of buffer.
206 * t - time to format, in milliseconds.
208 void format_time(char* buf, int buf_size, long t)
210 if ( t < 3600000 )
212 snprintf(buf, buf_size, "%d:%02d",
213 (int) (t / 60000), (int) (t % 60000 / 1000));
215 else
217 snprintf(buf, buf_size, "%d:%02d:%02d",
218 (int) (t / 3600000), (int) (t % 3600000 / 60000),
219 (int) (t % 60000 / 1000));
223 #if CONFIG_RTC
224 /* Create a filename with a date+time part.
225 It is allowed that buffer and path point to the same memory location,
226 saving a strcpy(). Path must always be given without trailing slash.
227 unique_time as true makes the function wait until the current time has
228 changed. */
229 char *create_datetime_filename(char *buffer, const char *path,
230 const char *prefix, const char *suffix,
231 bool unique_time)
233 struct tm *tm = get_time();
234 static struct tm last_tm;
235 int pathlen;
237 while (unique_time && !memcmp(get_time(), &last_tm, sizeof (struct tm)))
238 sleep(HZ/10);
240 last_tm = *tm;
242 if (buffer != path)
243 strncpy(buffer, path, MAX_PATH);
245 pathlen = strlen(buffer);
246 snprintf(buffer + pathlen, MAX_PATH - pathlen,
247 "/%s%02d%02d%02d-%02d%02d%02d%s", prefix,
248 tm->tm_year % 100, tm->tm_mon + 1, tm->tm_mday,
249 tm->tm_hour, tm->tm_min, tm->tm_sec, suffix);
251 return buffer;
253 #endif /* CONFIG_RTC */
255 /* Read (up to) a line of text from fd into buffer and return number of bytes
256 * read (which may be larger than the number of bytes stored in buffer). If
257 * an error occurs, -1 is returned (and buffer contains whatever could be
258 * read). A line is terminated by a LF char. Neither LF nor CR chars are
259 * stored in buffer.
261 int read_line(int fd, char* buffer, int buffer_size)
263 int count = 0;
264 int num_read = 0;
266 errno = 0;
268 while (count < buffer_size)
270 unsigned char c;
272 if (1 != read(fd, &c, 1))
273 break;
275 num_read++;
277 if ( c == '\n' )
278 break;
280 if ( c == '\r' )
281 continue;
283 buffer[count++] = c;
286 buffer[MIN(count, buffer_size - 1)] = 0;
288 return errno ? -1 : num_read;
291 /* Performance optimized version of the previous function. */
292 int fast_readline(int fd, char *buf, int buf_size, void *parameters,
293 int (*callback)(int n, const char *buf, void *parameters))
295 char *p, *next;
296 int rc, pos = 0;
297 int count = 0;
299 while ( 1 )
301 next = NULL;
303 rc = read(fd, &buf[pos], buf_size - pos - 1);
304 if (rc >= 0)
305 buf[pos+rc] = '\0';
307 if ( (p = strchr(buf, '\r')) != NULL)
309 *p = '\0';
310 next = ++p;
312 else
313 p = buf;
315 if ( (p = strchr(p, '\n')) != NULL)
317 *p = '\0';
318 next = ++p;
321 rc = callback(count, buf, parameters);
322 if (rc < 0)
323 return rc;
325 count++;
326 if (next)
328 pos = buf_size - ((long)next - (long)buf) - 1;
329 memmove(buf, next, pos);
331 else
332 break ;
335 return 0;
338 #ifdef HAVE_LCD_BITMAP
340 #if LCD_DEPTH == 16
341 #define BMP_COMPRESSION 3 /* BI_BITFIELDS */
342 #define BMP_NUMCOLORS 3
343 #else
344 #define BMP_COMPRESSION 0 /* BI_RGB */
345 #if LCD_DEPTH <= 8
346 #define BMP_NUMCOLORS (1 << LCD_DEPTH)
347 #else
348 #define BMP_NUMCOLORS 0
349 #endif
350 #endif
352 #if LCD_DEPTH == 1
353 #define BMP_BPP 1
354 #define BMP_LINESIZE ((LCD_WIDTH/8 + 3) & ~3)
355 #elif LCD_DEPTH <= 4
356 #define BMP_BPP 4
357 #define BMP_LINESIZE ((LCD_WIDTH/2 + 3) & ~3)
358 #elif LCD_DEPTH <= 8
359 #define BMP_BPP 8
360 #define BMP_LINESIZE ((LCD_WIDTH + 3) & ~3)
361 #elif LCD_DEPTH <= 16
362 #define BMP_BPP 16
363 #define BMP_LINESIZE ((LCD_WIDTH*2 + 3) & ~3)
364 #else
365 #define BMP_BPP 24
366 #define BMP_LINESIZE ((LCD_WIDTH*3 + 3) & ~3)
367 #endif
369 #define BMP_HEADERSIZE (54 + 4 * BMP_NUMCOLORS)
370 #define BMP_DATASIZE (BMP_LINESIZE * LCD_HEIGHT)
371 #define BMP_TOTALSIZE (BMP_HEADERSIZE + BMP_DATASIZE)
373 #define LE16_CONST(x) (x)&0xff, ((x)>>8)&0xff
374 #define LE32_CONST(x) (x)&0xff, ((x)>>8)&0xff, ((x)>>16)&0xff, ((x)>>24)&0xff
376 static const unsigned char bmpheader[] =
378 0x42, 0x4d, /* 'BM' */
379 LE32_CONST(BMP_TOTALSIZE), /* Total file size */
380 0x00, 0x00, 0x00, 0x00, /* Reserved */
381 LE32_CONST(BMP_HEADERSIZE), /* Offset to start of pixel data */
383 0x28, 0x00, 0x00, 0x00, /* Size of (2nd) header */
384 LE32_CONST(LCD_WIDTH), /* Width in pixels */
385 LE32_CONST(LCD_HEIGHT), /* Height in pixels */
386 0x01, 0x00, /* Number of planes (always 1) */
387 LE16_CONST(BMP_BPP), /* Bits per pixel 1/4/8/16/24 */
388 LE32_CONST(BMP_COMPRESSION),/* Compression mode */
389 LE32_CONST(BMP_DATASIZE), /* Size of bitmap data */
390 0xc4, 0x0e, 0x00, 0x00, /* Horizontal resolution (pixels/meter) */
391 0xc4, 0x0e, 0x00, 0x00, /* Vertical resolution (pixels/meter) */
392 LE32_CONST(BMP_NUMCOLORS), /* Number of used colours */
393 LE32_CONST(BMP_NUMCOLORS), /* Number of important colours */
395 #if LCD_DEPTH == 1
396 #ifdef MROBE_100
397 2, 2, 94, 0x00, /* Colour #0 */
398 3, 6, 241, 0x00 /* Colour #1 */
399 #else
400 0x90, 0xee, 0x90, 0x00, /* Colour #0 */
401 0x00, 0x00, 0x00, 0x00 /* Colour #1 */
402 #endif
403 #elif LCD_DEPTH == 2
404 0xe6, 0xd8, 0xad, 0x00, /* Colour #0 */
405 0x99, 0x90, 0x73, 0x00, /* Colour #1 */
406 0x4c, 0x48, 0x39, 0x00, /* Colour #2 */
407 0x00, 0x00, 0x00, 0x00 /* Colour #3 */
408 #elif LCD_DEPTH == 16
409 0x00, 0xf8, 0x00, 0x00, /* red bitfield mask */
410 0xe0, 0x07, 0x00, 0x00, /* green bitfield mask */
411 0x1f, 0x00, 0x00, 0x00 /* blue bitfield mask */
412 #endif
415 static void (*screen_dump_hook)(int fh) = NULL;
417 void screen_dump(void)
419 int fh;
420 char filename[MAX_PATH];
421 int bx, by;
422 #if LCD_DEPTH == 1
423 static unsigned char line_block[8][BMP_LINESIZE];
424 #elif LCD_DEPTH == 2
425 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
426 static unsigned char line_block[BMP_LINESIZE];
427 #else
428 static unsigned char line_block[4][BMP_LINESIZE];
429 #endif
430 #elif LCD_DEPTH == 16
431 static unsigned short line_block[BMP_LINESIZE/2];
432 #endif
434 #if CONFIG_RTC
435 create_datetime_filename(filename, "", "dump ", ".bmp", false);
436 #else
437 create_numbered_filename(filename, "", "dump_", ".bmp", 4
438 IF_CNFN_NUM_(, NULL));
439 #endif
441 fh = creat(filename);
442 if (fh < 0)
443 return;
445 if (screen_dump_hook)
447 screen_dump_hook(fh);
449 else
451 write(fh, bmpheader, sizeof(bmpheader));
453 /* BMP image goes bottom up */
454 #if LCD_DEPTH == 1
455 for (by = LCD_FBHEIGHT - 1; by >= 0; by--)
457 unsigned char *src = &lcd_framebuffer[by][0];
458 unsigned char *dst = &line_block[0][0];
460 memset(line_block, 0, sizeof(line_block));
461 for (bx = LCD_WIDTH/8; bx > 0; bx--)
463 unsigned dst_mask = 0x80;
464 int ix;
466 for (ix = 8; ix > 0; ix--)
468 unsigned char *dst_blk = dst;
469 unsigned src_byte = *src++;
470 int iy;
472 for (iy = 8; iy > 0; iy--)
474 if (src_byte & 0x80)
475 *dst_blk |= dst_mask;
476 src_byte <<= 1;
477 dst_blk += BMP_LINESIZE;
479 dst_mask >>= 1;
481 dst++;
484 write(fh, line_block, sizeof(line_block));
486 #elif LCD_DEPTH == 2
487 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
488 for (by = LCD_FBHEIGHT - 1; by >= 0; by--)
490 unsigned char *src = &lcd_framebuffer[by][0];
491 unsigned char *dst = line_block;
493 memset(line_block, 0, sizeof(line_block));
494 for (bx = LCD_FBWIDTH; bx > 0; bx--)
496 unsigned src_byte = *src++;
498 *dst++ = ((src_byte >> 2) & 0x30) | ((src_byte >> 4) & 0x03);
499 *dst++ = ((src_byte << 2) & 0x30) | (src_byte & 0x03);
502 write(fh, line_block, sizeof(line_block));
504 #else /* VERTICAL_PACKING */
505 for (by = LCD_FBHEIGHT - 1; by >= 0; by--)
507 unsigned char *src = &lcd_framebuffer[by][0];
508 unsigned char *dst = &line_block[3][0];
510 memset(line_block, 0, sizeof(line_block));
511 for (bx = LCD_WIDTH/2; bx > 0; bx--)
513 unsigned char *dst_blk = dst++;
514 unsigned src_byte0 = *src++;
515 unsigned src_byte1 = *src++;
516 int iy;
518 for (iy = 4; iy > 0; iy--)
520 *dst_blk = ((src_byte0 & 3) << 4) | (src_byte1 & 3);
521 src_byte0 >>= 2;
522 src_byte1 >>= 2;
523 dst_blk -= BMP_LINESIZE;
527 write(fh, line_block, sizeof(line_block));
529 #endif
530 #elif LCD_DEPTH == 16
531 for (by = LCD_HEIGHT - 1; by >= 0; by--)
533 unsigned short *src = &lcd_framebuffer[by][0];
534 unsigned short *dst = line_block;
536 memset(line_block, 0, sizeof(line_block));
537 for (bx = LCD_WIDTH; bx > 0; bx--)
539 #if (LCD_PIXELFORMAT == RGB565SWAPPED)
540 /* iPod LCD data is big endian although the CPU is not */
541 *dst++ = htobe16(*src++);
542 #else
543 *dst++ = htole16(*src++);
544 #endif
547 write(fh, line_block, sizeof(line_block));
549 #endif /* LCD_DEPTH */
552 close(fh);
555 void screen_dump_set_hook(void (*hook)(int fh))
557 screen_dump_hook = hook;
560 #endif /* HAVE_LCD_BITMAP */
562 /* parse a line from a configuration file. the line format is:
564 name: value
566 Any whitespace before setting name or value (after ':') is ignored.
567 A # as first non-whitespace character discards the whole line.
568 Function sets pointers to null-terminated setting name and value.
569 Returns false if no valid config entry was found.
572 bool settings_parseline(char* line, char** name, char** value)
574 char* ptr;
576 while ( isspace(*line) )
577 line++;
579 if ( *line == '#' )
580 return false;
582 ptr = strchr(line, ':');
583 if ( !ptr )
584 return false;
586 *name = line;
587 *ptr = 0;
588 ptr++;
589 while (isspace(*ptr))
590 ptr++;
591 *value = ptr;
592 return true;
595 static void system_flush(void)
597 tree_flush();
598 call_ata_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
601 static void system_restore(void)
603 tree_restore();
606 static bool clean_shutdown(void (*callback)(void *), void *parameter)
608 #ifdef SIMULATOR
609 (void)callback;
610 (void)parameter;
611 bookmark_autobookmark();
612 call_ata_idle_notifys(true);
613 exit(0);
614 #else
615 long msg_id = -1;
616 int i;
618 scrobbler_poweroff();
620 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
621 if(!charger_inserted())
622 #endif
624 bool batt_safe = battery_level_safe();
625 int audio_stat = audio_status();
627 FOR_NB_SCREENS(i)
628 screens[i].clear_display();
630 if (batt_safe)
632 #ifdef HAVE_TAGCACHE
633 if (!tagcache_prepare_shutdown())
635 cancel_shutdown();
636 gui_syncsplash(HZ, ID2P(LANG_TAGCACHE_BUSY));
637 return false;
639 #endif
640 if (battery_level() > 10)
641 gui_syncsplash(0, str(LANG_SHUTTINGDOWN));
642 else
644 msg_id = LANG_WARNING_BATTERY_LOW;
645 gui_syncsplash(0, "%s %s",
646 str(LANG_WARNING_BATTERY_LOW),
647 str(LANG_SHUTTINGDOWN));
650 else
652 msg_id = LANG_WARNING_BATTERY_EMPTY;
653 gui_syncsplash(0, "%s %s",
654 str(LANG_WARNING_BATTERY_EMPTY),
655 str(LANG_SHUTTINGDOWN));
658 if (global_settings.fade_on_stop
659 && (audio_stat & AUDIO_STATUS_PLAY))
661 fade(0);
664 if (batt_safe) /* do not save on critical battery */
666 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
667 if (audio_stat & AUDIO_STATUS_RECORD)
669 rec_command(RECORDING_CMD_STOP);
670 /* wait for stop to complete */
671 while (audio_status() & AUDIO_STATUS_RECORD)
672 sleep(1);
674 #endif
675 bookmark_autobookmark();
677 /* audio_stop_recording == audio_stop for HWCODEC */
678 audio_stop();
680 if (callback != NULL)
681 callback(parameter);
683 #if CONFIG_CODEC != SWCODEC
684 /* wait for audio_stop or audio_stop_recording to complete */
685 while (audio_status())
686 sleep(1);
687 #endif
689 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
690 audio_close_recording();
691 #endif
693 if(global_settings.talk_menu)
695 bool enqueue = false;
696 if(msg_id != -1)
698 talk_id(msg_id, enqueue);
699 enqueue = true;
701 talk_id(LANG_SHUTTINGDOWN, enqueue);
702 #if CONFIG_CODEC == SWCODEC
703 voice_wait();
704 #endif
707 system_flush();
708 #ifdef HAVE_EEPROM_SETTINGS
709 if (firmware_settings.initialized)
711 firmware_settings.disk_clean = true;
712 firmware_settings.bl_version = 0;
713 eeprom_settings_store();
715 #endif
717 #ifdef HAVE_DIRCACHE
718 else
719 dircache_disable();
720 #endif
722 shutdown_hw();
724 #endif
725 return false;
728 bool list_stop_handler(void)
730 bool ret = false;
732 /* Stop the music if it is playing */
733 if(audio_status())
735 if (!global_settings.party_mode)
737 if (global_settings.fade_on_stop)
738 fade(0);
739 bookmark_autobookmark();
740 audio_stop();
741 ret = true; /* bookmarking can make a refresh necessary */
744 #if CONFIG_CHARGING
745 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
746 else
748 if (charger_inserted())
749 charging_splash();
750 else
751 shutdown_screen(); /* won't return if shutdown actually happens */
753 ret = true; /* screen is dirty, caller needs to refresh */
755 #endif
756 #ifndef HAVE_POWEROFF_WHILE_CHARGING
758 static long last_off = 0;
760 if (TIME_BEFORE(current_tick, last_off + HZ/2))
762 if (charger_inserted())
764 charging_splash();
765 ret = true; /* screen is dirty, caller needs to refresh */
768 last_off = current_tick;
770 #endif
771 #endif /* CONFIG_CHARGING */
772 return ret;
775 #if CONFIG_CHARGING
776 static bool waiting_to_resume_play = false;
777 static long play_resume_tick;
779 static void car_adapter_mode_processing(bool inserted)
781 if (global_settings.car_adapter_mode)
783 if(inserted)
786 * Just got plugged in, delay & resume if we were playing
788 if (audio_status() & AUDIO_STATUS_PAUSE)
790 /* delay resume a bit while the engine is cranking */
791 play_resume_tick = current_tick + HZ*5;
792 waiting_to_resume_play = true;
795 else
798 * Just got unplugged, pause if playing
800 if ((audio_status() & AUDIO_STATUS_PLAY) &&
801 !(audio_status() & AUDIO_STATUS_PAUSE))
803 if (global_settings.fade_on_stop)
804 fade(0);
805 else
806 audio_pause();
808 waiting_to_resume_play = false;
813 static void car_adapter_tick(void)
815 if (waiting_to_resume_play)
817 if (TIME_AFTER(current_tick, play_resume_tick))
819 if (audio_status() & AUDIO_STATUS_PAUSE)
821 queue_broadcast(SYS_CAR_ADAPTER_RESUME, 0);
823 waiting_to_resume_play = false;
828 void car_adapter_mode_init(void)
830 tick_add_task(car_adapter_tick);
832 #endif
834 #ifdef HAVE_HEADPHONE_DETECTION
835 static void unplug_change(bool inserted)
837 if (global_settings.unplug_mode)
839 if (inserted)
841 if ( global_settings.unplug_mode > 1 )
842 audio_resume();
843 backlight_on();
844 } else {
845 audio_pause();
847 if (global_settings.unplug_rw)
849 if ( audio_current_track()->elapsed >
850 (unsigned long)(global_settings.unplug_rw*1000))
851 audio_ff_rewind(audio_current_track()->elapsed -
852 (global_settings.unplug_rw*1000));
853 else
854 audio_ff_rewind(0);
859 #endif
861 long default_event_handler_ex(long event, void (*callback)(void *), void *parameter)
863 switch(event)
865 case SYS_BATTERY_UPDATE:
866 if(global_settings.talk_battery_level)
868 talk_ids(true, VOICE_PAUSE, VOICE_PAUSE,
869 LANG_BATTERY_TIME,
870 TALK_ID(battery_level(), UNIT_PERCENT),
871 VOICE_PAUSE);
872 talk_force_enqueue_next();
874 break;
875 case SYS_USB_CONNECTED:
876 if (callback != NULL)
877 callback(parameter);
878 #ifdef HAVE_MMC
879 if (!mmc_touched() ||
880 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
881 #endif
883 scrobbler_flush_cache();
884 system_flush();
885 #ifdef BOOTFILE
886 #if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
887 check_bootfile(false); /* gets initial size */
888 #endif
889 #endif
890 usb_screen();
891 #ifdef BOOTFILE
892 #if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
893 check_bootfile(true);
894 #endif
895 #endif
896 system_restore();
898 return SYS_USB_CONNECTED;
899 case SYS_POWEROFF:
900 if (!clean_shutdown(callback, parameter))
901 return SYS_POWEROFF;
902 break;
903 #if CONFIG_CHARGING
904 case SYS_CHARGER_CONNECTED:
905 car_adapter_mode_processing(true);
906 return SYS_CHARGER_CONNECTED;
908 case SYS_CHARGER_DISCONNECTED:
909 car_adapter_mode_processing(false);
910 return SYS_CHARGER_DISCONNECTED;
912 case SYS_CAR_ADAPTER_RESUME:
913 audio_resume();
914 return SYS_CAR_ADAPTER_RESUME;
915 #endif
916 #ifdef HAVE_HEADPHONE_DETECTION
917 case SYS_PHONE_PLUGGED:
918 unplug_change(true);
919 return SYS_PHONE_PLUGGED;
921 case SYS_PHONE_UNPLUGGED:
922 unplug_change(false);
923 return SYS_PHONE_UNPLUGGED;
924 #endif
926 return 0;
929 long default_event_handler(long event)
931 return default_event_handler_ex(event, NULL, NULL);
934 int show_logo( void )
936 #ifdef HAVE_LCD_BITMAP
937 char version[32];
938 int font_h, font_w;
940 snprintf(version, sizeof(version), "Ver. %s", appsversion);
942 lcd_clear_display();
943 lcd_bitmap(rockboxlogo, 0, 10, BMPWIDTH_rockboxlogo, BMPHEIGHT_rockboxlogo);
944 lcd_setfont(FONT_SYSFIXED);
945 lcd_getstringsize((unsigned char *)"A", &font_w, &font_h);
946 lcd_putsxy((LCD_WIDTH/2) - ((strlen(version)*font_w)/2),
947 LCD_HEIGHT-font_h, (unsigned char *)version);
948 lcd_setfont(FONT_UI);
950 #else
951 char *rockbox = " ROCKbox!";
953 lcd_clear_display();
954 lcd_double_height(true);
955 lcd_puts(0, 0, rockbox);
956 lcd_puts_scroll(0, 1, appsversion);
957 #endif
958 lcd_update();
960 #ifdef HAVE_REMOTE_LCD
961 lcd_remote_clear_display();
962 lcd_remote_bitmap(remote_rockboxlogo, 0, 10, BMPWIDTH_remote_rockboxlogo,
963 BMPHEIGHT_remote_rockboxlogo);
964 lcd_remote_setfont(FONT_SYSFIXED);
965 lcd_remote_getstringsize((unsigned char *)"A", &font_w, &font_h);
966 lcd_remote_putsxy((LCD_REMOTE_WIDTH/2) - ((strlen(version)*font_w)/2),
967 LCD_REMOTE_HEIGHT-font_h, (unsigned char *)version);
968 lcd_remote_setfont(FONT_UI);
969 lcd_remote_update();
970 #endif
972 return 0;
975 #if CONFIG_CODEC == SWCODEC
976 int get_replaygain_mode(bool have_track_gain, bool have_album_gain)
978 int type;
980 bool track = ((global_settings.replaygain_type == REPLAYGAIN_TRACK)
981 || ((global_settings.replaygain_type == REPLAYGAIN_SHUFFLE)
982 && global_settings.playlist_shuffle));
984 type = (!track && have_album_gain) ? REPLAYGAIN_ALBUM
985 : have_track_gain ? REPLAYGAIN_TRACK : -1;
987 return type;
989 #endif
991 #ifdef BOOTFILE
992 #if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
994 memorize/compare details about the BOOTFILE
995 we don't use dircache because it may not be up to date after
996 USB disconnect (scanning in the background)
998 void check_bootfile(bool do_rolo)
1000 static unsigned short wrtdate = 0;
1001 static unsigned short wrttime = 0;
1002 DIR* dir = NULL;
1003 struct dirent* entry = NULL;
1005 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
1006 dir = opendir(BOOTDIR);
1008 if(!dir) return; /* do we want an error splash? */
1010 /* loop all files in BOOTDIR */
1011 while(0 != (entry = readdir(dir)))
1013 if(!strcasecmp(entry->d_name, BOOTFILE))
1015 /* found the bootfile */
1016 if(wrtdate && do_rolo)
1018 if((entry->wrtdate != wrtdate) ||
1019 (entry->wrttime != wrttime))
1021 char *lines[] = { ID2P(LANG_BOOT_CHANGED),
1022 ID2P(LANG_REBOOT_NOW) };
1023 struct text_message message={ lines, 2 };
1024 button_clear_queue(); /* Empty the keyboard buffer */
1025 if(gui_syncyesno_run(&message, NULL, NULL) == YESNO_YES)
1026 rolo_load(BOOTDIR "/" BOOTFILE);
1029 wrtdate = entry->wrtdate;
1030 wrttime = entry->wrttime;
1033 closedir(dir);
1035 #endif
1036 #endif
1038 /* check range, set volume and save settings */
1039 void setvol(void)
1041 const int min_vol = sound_min(SOUND_VOLUME);
1042 const int max_vol = sound_max(SOUND_VOLUME);
1043 if (global_settings.volume < min_vol)
1044 global_settings.volume = min_vol;
1045 if (global_settings.volume > max_vol)
1046 global_settings.volume = max_vol;
1047 sound_set_volume(global_settings.volume);
1048 settings_save();
1051 #ifdef HAVE_LCD_COLOR
1053 * Helper function to convert a string of 6 hex digits to a native colour
1056 #define hex2dec(c) (((c) >= '0' && ((c) <= '9')) ? (toupper(c)) - '0' : \
1057 (toupper(c)) - 'A' + 10)
1059 int hex_to_rgb(const char* hex)
1060 { int ok = 1;
1061 int i;
1062 int red, green, blue;
1064 if (strlen(hex) == 6) {
1065 for (i=0; i < 6; i++ ) {
1066 if (!isxdigit(hex[i])) {
1067 ok=0;
1068 break;
1072 if (ok) {
1073 red = (hex2dec(hex[0]) << 4) | hex2dec(hex[1]);
1074 green = (hex2dec(hex[2]) << 4) | hex2dec(hex[3]);
1075 blue = (hex2dec(hex[4]) << 4) | hex2dec(hex[5]);
1076 return LCD_RGBPACK(red,green,blue);
1080 return 0;
1082 #endif /* HAVE_LCD_COLOR */
1084 char* strrsplt(char* str, int c)
1086 char* s = strrchr(str, c);
1088 if (s != NULL)
1090 *s++ = '\0';
1092 else
1094 s = str;
1097 return s;
1100 /* Test file existence, using dircache of possible */
1101 bool file_exists(const char *file)
1103 int fd;
1105 if (!file || strlen(file) <= 0)
1106 return false;
1108 #ifdef HAVE_DIRCACHE
1109 if (dircache_is_enabled())
1110 return (dircache_get_entry_ptr(file) != NULL);
1111 #endif
1113 fd = open(file, O_RDONLY);
1114 if (fd < 0)
1115 return false;
1116 close(fd);
1117 return true;
1120 bool dir_exists(const char *path)
1122 DIR* d = opendir(path);
1123 if (!d)
1124 return false;
1125 closedir(d);
1126 return true;
1130 * removes the extension of filename (if it doesn't start with a .)
1131 * puts the result in buffer
1133 char *strip_extension(char* buffer, int buffer_size, const char *filename)
1135 char *dot = strrchr(filename, '.');
1136 int len;
1138 if (buffer_size <= 0)
1140 return NULL;
1143 buffer_size--; /* Make room for end nil */
1145 if (dot != 0 && filename[0] != '.')
1147 len = dot - filename;
1148 len = MIN(len, buffer_size);
1149 strncpy(buffer, filename, len);
1151 else
1153 len = buffer_size;
1154 strncpy(buffer, filename, buffer_size);
1157 buffer[len] = 0;
1159 return buffer;