A patch by Robert Keevil that's been in the tracker way to long, fixes FS #6213:...
[Rockbox.git] / apps / misc.c
blob08e699e78180c6b627c1f858e49799935f787cf6
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"
57 #ifdef HAVE_LCD_BITMAP
58 #include "bmp.h"
59 #include "icons.h"
60 #endif /* End HAVE_LCD_BITMAP */
61 #include "gui/gwps-common.h"
62 #include "bookmark.h"
64 #include "misc.h"
66 #ifdef BOOTFILE
67 #ifndef USB_IPODSTYLE
68 #include "textarea.h"
69 #include "rolo.h"
70 #include "yesno.h"
71 #endif
72 #endif
74 /* Format a large-range value for output, using the appropriate unit so that
75 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
76 * units) if possible, and 3 significant digits are shown. If a buffer is
77 * given, the result is snprintf()'d into that buffer, otherwise the result is
78 * voiced.*/
79 char *output_dyn_value(char *buf, int buf_size, int value,
80 const unsigned char **units, bool bin_scale)
82 int scale = bin_scale ? 1024 : 1000;
83 int fraction = 0;
84 int unit_no = 0;
85 int i;
86 char tbuf[5];
88 while (value >= scale)
90 fraction = value % scale;
91 value /= scale;
92 unit_no++;
94 if (bin_scale)
95 fraction = fraction * 1000 / 1024;
97 if (value >= 100 || !unit_no)
98 tbuf[0] = '\0';
99 else if (value >= 10)
100 snprintf(tbuf, sizeof(tbuf), "%01d", fraction / 100);
101 else
102 snprintf(tbuf, sizeof(tbuf), "%02d", fraction / 10);
104 if (buf)
106 if (strlen(tbuf))
107 snprintf(buf, buf_size, "%d%s%s%s", value, str(LANG_POINT),
108 tbuf, P2STR(units[unit_no]));
109 else
110 snprintf(buf, buf_size, "%d%s", value, P2STR(units[unit_no]));
112 else
114 /* strip trailing zeros from the fraction */
115 for (i = strlen(tbuf) - 1; (i >= 0) && (tbuf[i] == '0'); i--)
116 tbuf[i] = '\0';
118 talk_number(value, true);
119 if (tbuf[0] != 0)
121 talk_id(LANG_POINT, true);
122 talk_spell(tbuf, true);
124 talk_id(P2ID(units[unit_no]), true);
126 return buf;
129 /* Create a filename with a number part in a way that the number is 1
130 * higher than the highest numbered file matching the same pattern.
131 * It is allowed that buffer and path point to the same memory location,
132 * saving a strcpy(). Path must always be given without trailing slash.
133 * "num" can point to an int specifying the number to use or NULL or a value
134 * less than zero to number automatically. The final number used will also
135 * be returned in *num. If *num is >= 0 then *num will be incremented by
136 * one. */
137 char *create_numbered_filename(char *buffer, const char *path,
138 const char *prefix, const char *suffix,
139 int numberlen IF_CNFN_NUM_(, int *num))
141 DIR *dir;
142 struct dirent *entry;
143 int max_num;
144 int pathlen;
145 int prefixlen = strlen(prefix);
146 char fmtstring[12];
148 if (buffer != path)
149 strncpy(buffer, path, MAX_PATH);
151 pathlen = strlen(buffer);
153 #ifdef IF_CNFN_NUM
154 if (num && *num >= 0)
156 /* number specified */
157 max_num = *num;
159 else
160 #endif
162 /* automatic numbering */
163 max_num = 0;
165 dir = opendir(pathlen ? buffer : "/");
166 if (!dir)
167 return NULL;
169 while ((entry = readdir(dir)))
171 int curr_num;
173 if (strncasecmp((char *)entry->d_name, prefix, prefixlen)
174 || strcasecmp((char *)entry->d_name + prefixlen + numberlen, suffix))
175 continue;
177 curr_num = atoi((char *)entry->d_name + prefixlen);
178 if (curr_num > max_num)
179 max_num = curr_num;
182 closedir(dir);
185 max_num++;
187 snprintf(fmtstring, sizeof(fmtstring), "/%%s%%0%dd%%s", numberlen);
188 snprintf(buffer + pathlen, MAX_PATH - pathlen, fmtstring, prefix,
189 max_num, suffix);
191 #ifdef IF_CNFN_NUM
192 if (num)
193 *num = max_num;
194 #endif
196 return buffer;
199 /* Format time into buf.
201 * buf - buffer to format to.
202 * buf_size - size of buffer.
203 * t - time to format, in milliseconds.
205 void format_time(char* buf, int buf_size, long t)
207 if ( t < 3600000 )
209 snprintf(buf, buf_size, "%d:%02d",
210 (int) (t / 60000), (int) (t % 60000 / 1000));
212 else
214 snprintf(buf, buf_size, "%d:%02d:%02d",
215 (int) (t / 3600000), (int) (t % 3600000 / 60000),
216 (int) (t % 60000 / 1000));
220 #if CONFIG_RTC
221 /* Create a filename with a date+time part.
222 It is allowed that buffer and path point to the same memory location,
223 saving a strcpy(). Path must always be given without trailing slash.
224 unique_time as true makes the function wait until the current time has
225 changed. */
226 char *create_datetime_filename(char *buffer, const char *path,
227 const char *prefix, const char *suffix,
228 bool unique_time)
230 struct tm *tm = get_time();
231 static struct tm last_tm;
232 int pathlen;
234 while (unique_time && !memcmp(get_time(), &last_tm, sizeof (struct tm)))
235 sleep(HZ/10);
237 last_tm = *tm;
239 if (buffer != path)
240 strncpy(buffer, path, MAX_PATH);
242 pathlen = strlen(buffer);
243 snprintf(buffer + pathlen, MAX_PATH - pathlen,
244 "/%s%02d%02d%02d-%02d%02d%02d%s", prefix,
245 tm->tm_year % 100, tm->tm_mon + 1, tm->tm_mday,
246 tm->tm_hour, tm->tm_min, tm->tm_sec, suffix);
248 return buffer;
250 #endif /* CONFIG_RTC */
252 /* Read (up to) a line of text from fd into buffer and return number of bytes
253 * read (which may be larger than the number of bytes stored in buffer). If
254 * an error occurs, -1 is returned (and buffer contains whatever could be
255 * read). A line is terminated by a LF char. Neither LF nor CR chars are
256 * stored in buffer.
258 int read_line(int fd, char* buffer, int buffer_size)
260 int count = 0;
261 int num_read = 0;
263 errno = 0;
265 while (count < buffer_size)
267 unsigned char c;
269 if (1 != read(fd, &c, 1))
270 break;
272 num_read++;
274 if ( c == '\n' )
275 break;
277 if ( c == '\r' )
278 continue;
280 buffer[count++] = c;
283 buffer[MIN(count, buffer_size - 1)] = 0;
285 return errno ? -1 : num_read;
288 /* Performance optimized version of the previous function. */
289 int fast_readline(int fd, char *buf, int buf_size, void *parameters,
290 int (*callback)(int n, const char *buf, void *parameters))
292 char *p, *next;
293 int rc, pos = 0;
294 int count = 0;
296 while ( 1 )
298 next = NULL;
300 rc = read(fd, &buf[pos], buf_size - pos - 1);
301 if (rc >= 0)
302 buf[pos+rc] = '\0';
304 if ( (p = strchr(buf, '\r')) != NULL)
306 *p = '\0';
307 next = ++p;
309 else
310 p = buf;
312 if ( (p = strchr(p, '\n')) != NULL)
314 *p = '\0';
315 next = ++p;
318 rc = callback(count, buf, parameters);
319 if (rc < 0)
320 return rc;
322 count++;
323 if (next)
325 pos = buf_size - ((long)next - (long)buf) - 1;
326 memmove(buf, next, pos);
328 else
329 break ;
332 return 0;
335 #ifdef HAVE_LCD_BITMAP
337 #if LCD_DEPTH == 16
338 #define BMP_COMPRESSION 3 /* BI_BITFIELDS */
339 #define BMP_NUMCOLORS 3
340 #else
341 #define BMP_COMPRESSION 0 /* BI_RGB */
342 #if LCD_DEPTH <= 8
343 #define BMP_NUMCOLORS (1 << LCD_DEPTH)
344 #else
345 #define BMP_NUMCOLORS 0
346 #endif
347 #endif
349 #if LCD_DEPTH == 1
350 #define BMP_BPP 1
351 #define BMP_LINESIZE ((LCD_WIDTH/8 + 3) & ~3)
352 #elif LCD_DEPTH <= 4
353 #define BMP_BPP 4
354 #define BMP_LINESIZE ((LCD_WIDTH/2 + 3) & ~3)
355 #elif LCD_DEPTH <= 8
356 #define BMP_BPP 8
357 #define BMP_LINESIZE ((LCD_WIDTH + 3) & ~3)
358 #elif LCD_DEPTH <= 16
359 #define BMP_BPP 16
360 #define BMP_LINESIZE ((LCD_WIDTH*2 + 3) & ~3)
361 #else
362 #define BMP_BPP 24
363 #define BMP_LINESIZE ((LCD_WIDTH*3 + 3) & ~3)
364 #endif
366 #define BMP_HEADERSIZE (54 + 4 * BMP_NUMCOLORS)
367 #define BMP_DATASIZE (BMP_LINESIZE * LCD_HEIGHT)
368 #define BMP_TOTALSIZE (BMP_HEADERSIZE + BMP_DATASIZE)
370 #define LE16_CONST(x) (x)&0xff, ((x)>>8)&0xff
371 #define LE32_CONST(x) (x)&0xff, ((x)>>8)&0xff, ((x)>>16)&0xff, ((x)>>24)&0xff
373 static const unsigned char bmpheader[] =
375 0x42, 0x4d, /* 'BM' */
376 LE32_CONST(BMP_TOTALSIZE), /* Total file size */
377 0x00, 0x00, 0x00, 0x00, /* Reserved */
378 LE32_CONST(BMP_HEADERSIZE), /* Offset to start of pixel data */
380 0x28, 0x00, 0x00, 0x00, /* Size of (2nd) header */
381 LE32_CONST(LCD_WIDTH), /* Width in pixels */
382 LE32_CONST(LCD_HEIGHT), /* Height in pixels */
383 0x01, 0x00, /* Number of planes (always 1) */
384 LE16_CONST(BMP_BPP), /* Bits per pixel 1/4/8/16/24 */
385 LE32_CONST(BMP_COMPRESSION),/* Compression mode */
386 LE32_CONST(BMP_DATASIZE), /* Size of bitmap data */
387 0xc4, 0x0e, 0x00, 0x00, /* Horizontal resolution (pixels/meter) */
388 0xc4, 0x0e, 0x00, 0x00, /* Vertical resolution (pixels/meter) */
389 LE32_CONST(BMP_NUMCOLORS), /* Number of used colours */
390 LE32_CONST(BMP_NUMCOLORS), /* Number of important colours */
392 #if LCD_DEPTH == 1
393 0x90, 0xee, 0x90, 0x00, /* Colour #0 */
394 0x00, 0x00, 0x00, 0x00 /* Colour #1 */
395 #elif LCD_DEPTH == 2
396 0xe6, 0xd8, 0xad, 0x00, /* Colour #0 */
397 0x99, 0x90, 0x73, 0x00, /* Colour #1 */
398 0x4c, 0x48, 0x39, 0x00, /* Colour #2 */
399 0x00, 0x00, 0x00, 0x00 /* Colour #3 */
400 #elif LCD_DEPTH == 16
401 0x00, 0xf8, 0x00, 0x00, /* red bitfield mask */
402 0xe0, 0x07, 0x00, 0x00, /* green bitfield mask */
403 0x1f, 0x00, 0x00, 0x00 /* blue bitfield mask */
404 #endif
407 static void (*screen_dump_hook)(int fh) = NULL;
409 void screen_dump(void)
411 int fh;
412 char filename[MAX_PATH];
413 int bx, by;
414 #if LCD_DEPTH == 1
415 static unsigned char line_block[8][BMP_LINESIZE];
416 #elif LCD_DEPTH == 2
417 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
418 static unsigned char line_block[BMP_LINESIZE];
419 #else
420 static unsigned char line_block[4][BMP_LINESIZE];
421 #endif
422 #elif LCD_DEPTH == 16
423 static unsigned short line_block[BMP_LINESIZE/2];
424 #endif
426 #if CONFIG_RTC
427 create_datetime_filename(filename, "", "dump ", ".bmp", false);
428 #else
429 create_numbered_filename(filename, "", "dump_", ".bmp", 4
430 IF_CNFN_NUM_(, NULL));
431 #endif
433 fh = creat(filename);
434 if (fh < 0)
435 return;
437 if (screen_dump_hook)
439 screen_dump_hook(fh);
441 else
443 write(fh, bmpheader, sizeof(bmpheader));
445 /* BMP image goes bottom up */
446 #if LCD_DEPTH == 1
447 for (by = LCD_FBHEIGHT - 1; by >= 0; by--)
449 unsigned char *src = &lcd_framebuffer[by][0];
450 unsigned char *dst = &line_block[0][0];
452 memset(line_block, 0, sizeof(line_block));
453 for (bx = LCD_WIDTH/8; bx > 0; bx--)
455 unsigned dst_mask = 0x80;
456 int ix;
458 for (ix = 8; ix > 0; ix--)
460 unsigned char *dst_blk = dst;
461 unsigned src_byte = *src++;
462 int iy;
464 for (iy = 8; iy > 0; iy--)
466 if (src_byte & 0x80)
467 *dst_blk |= dst_mask;
468 src_byte <<= 1;
469 dst_blk += BMP_LINESIZE;
471 dst_mask >>= 1;
473 dst++;
476 write(fh, line_block, sizeof(line_block));
478 #elif LCD_DEPTH == 2
479 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
480 for (by = LCD_FBHEIGHT - 1; by >= 0; by--)
482 unsigned char *src = &lcd_framebuffer[by][0];
483 unsigned char *dst = line_block;
485 memset(line_block, 0, sizeof(line_block));
486 for (bx = LCD_FBWIDTH; bx > 0; bx--)
488 unsigned src_byte = *src++;
490 *dst++ = ((src_byte >> 2) & 0x30) | ((src_byte >> 4) & 0x03);
491 *dst++ = ((src_byte << 2) & 0x30) | (src_byte & 0x03);
494 write(fh, line_block, sizeof(line_block));
496 #else /* VERTICAL_PACKING */
497 for (by = LCD_FBHEIGHT - 1; by >= 0; by--)
499 unsigned char *src = &lcd_framebuffer[by][0];
500 unsigned char *dst = &line_block[3][0];
502 memset(line_block, 0, sizeof(line_block));
503 for (bx = LCD_WIDTH/2; bx > 0; bx--)
505 unsigned char *dst_blk = dst++;
506 unsigned src_byte0 = *src++;
507 unsigned src_byte1 = *src++;
508 int iy;
510 for (iy = 4; iy > 0; iy--)
512 *dst_blk = ((src_byte0 & 3) << 4) | (src_byte1 & 3);
513 src_byte0 >>= 2;
514 src_byte1 >>= 2;
515 dst_blk -= BMP_LINESIZE;
519 write(fh, line_block, sizeof(line_block));
521 #endif
522 #elif LCD_DEPTH == 16
523 for (by = LCD_HEIGHT - 1; by >= 0; by--)
525 unsigned short *src = &lcd_framebuffer[by][0];
526 unsigned short *dst = line_block;
528 memset(line_block, 0, sizeof(line_block));
529 for (bx = LCD_WIDTH; bx > 0; bx--)
531 #if (LCD_PIXELFORMAT == RGB565SWAPPED)
532 /* iPod LCD data is big endian although the CPU is not */
533 *dst++ = htobe16(*src++);
534 #else
535 *dst++ = htole16(*src++);
536 #endif
539 write(fh, line_block, sizeof(line_block));
541 #endif /* LCD_DEPTH */
544 close(fh);
547 void screen_dump_set_hook(void (*hook)(int fh))
549 screen_dump_hook = hook;
552 #endif /* HAVE_LCD_BITMAP */
554 /* parse a line from a configuration file. the line format is:
556 name: value
558 Any whitespace before setting name or value (after ':') is ignored.
559 A # as first non-whitespace character discards the whole line.
560 Function sets pointers to null-terminated setting name and value.
561 Returns false if no valid config entry was found.
564 bool settings_parseline(char* line, char** name, char** value)
566 char* ptr;
568 while ( isspace(*line) )
569 line++;
571 if ( *line == '#' )
572 return false;
574 ptr = strchr(line, ':');
575 if ( !ptr )
576 return false;
578 *name = line;
579 *ptr = 0;
580 ptr++;
581 while (isspace(*ptr))
582 ptr++;
583 *value = ptr;
584 return true;
587 static void system_flush(void)
589 tree_flush();
590 call_ata_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
593 static void system_restore(void)
595 tree_restore();
598 static bool clean_shutdown(void (*callback)(void *), void *parameter)
600 #ifdef SIMULATOR
601 (void)callback;
602 (void)parameter;
603 call_ata_idle_notifys(true);
604 exit(0);
605 #else
606 int i;
608 scrobbler_poweroff();
610 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
611 if(!charger_inserted())
612 #endif
614 bool batt_crit = battery_level_critical();
615 int audio_stat = audio_status();
617 FOR_NB_SCREENS(i)
618 screens[i].clear_display();
619 #ifdef X5_BACKLIGHT_SHUTDOWN
620 x5_backlight_shutdown();
621 #endif
622 if (!battery_level_safe())
623 gui_syncsplash(3*HZ, "%s %s",
624 str(LANG_WARNING_BATTERY_EMPTY),
625 str(LANG_SHUTTINGDOWN));
626 else if (battery_level_critical())
627 gui_syncsplash(3*HZ, "%s %s",
628 str(LANG_WARNING_BATTERY_LOW),
629 str(LANG_SHUTTINGDOWN));
630 else {
631 #ifdef HAVE_TAGCACHE
632 if (!tagcache_prepare_shutdown())
634 cancel_shutdown();
635 gui_syncsplash(HZ, str(LANG_TAGCACHE_BUSY));
636 return false;
638 #endif
639 gui_syncsplash(0, str(LANG_SHUTTINGDOWN));
642 if (global_settings.fade_on_stop
643 && (audio_stat & AUDIO_STATUS_PLAY))
645 fade(0);
648 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
649 if (!batt_crit && (audio_stat & AUDIO_STATUS_RECORD))
651 audio_stop_recording();
652 while(audio_status() & AUDIO_STATUS_RECORD)
653 sleep(1);
656 audio_close_recording();
657 #endif
658 /* audio_stop_recording == audio_stop for HWCODEC */
660 audio_stop();
661 while (audio_status())
662 sleep(1);
664 if (callback != NULL)
665 callback(parameter);
667 if (!batt_crit) /* do not save on critical battery */
668 system_flush();
669 #ifdef HAVE_EEPROM_SETTINGS
670 if (firmware_settings.initialized)
672 firmware_settings.disk_clean = true;
673 firmware_settings.bl_version = 0;
674 eeprom_settings_store();
676 #endif
677 shutdown_hw();
679 #endif
680 return false;
683 bool list_stop_handler(void)
685 bool ret = false;
687 /* Stop the music if it is playing */
688 if(audio_status())
690 if (!global_settings.party_mode)
692 if (global_settings.fade_on_stop)
693 fade(0);
694 bookmark_autobookmark();
695 audio_stop();
698 #if CONFIG_CHARGING
699 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
700 else
702 if (charger_inserted())
703 charging_splash();
704 else
705 shutdown_screen(); /* won't return if shutdown actually happens */
707 ret = true; /* screen is dirty, caller needs to refresh */
709 #endif
710 #ifndef HAVE_POWEROFF_WHILE_CHARGING
712 static long last_off = 0;
714 if (TIME_BEFORE(current_tick, last_off + HZ/2))
716 if (charger_inserted())
718 charging_splash();
719 ret = true; /* screen is dirty, caller needs to refresh */
722 last_off = current_tick;
724 #endif
725 #endif /* CONFIG_CHARGING */
726 return ret;
729 #if CONFIG_CHARGING
730 static bool waiting_to_resume_play = false;
731 static long play_resume_tick;
733 static void car_adapter_mode_processing(bool inserted)
735 if (global_settings.car_adapter_mode)
737 if(inserted)
740 * Just got plugged in, delay & resume if we were playing
742 if (audio_status() & AUDIO_STATUS_PAUSE)
744 /* delay resume a bit while the engine is cranking */
745 play_resume_tick = current_tick + HZ*5;
746 waiting_to_resume_play = true;
749 else
752 * Just got unplugged, pause if playing
754 if ((audio_status() & AUDIO_STATUS_PLAY) &&
755 !(audio_status() & AUDIO_STATUS_PAUSE))
757 if (global_settings.fade_on_stop)
758 fade(0);
759 else
760 audio_pause();
766 static void car_adapter_tick(void)
768 if (waiting_to_resume_play)
770 if (TIME_AFTER(current_tick, play_resume_tick))
772 if (audio_status() & AUDIO_STATUS_PAUSE)
774 audio_resume();
776 waiting_to_resume_play = false;
781 void car_adapter_mode_init(void)
783 tick_add_task(car_adapter_tick);
785 #endif
787 #ifdef HAVE_HEADPHONE_DETECTION
788 static void unplug_change(bool inserted)
790 if (global_settings.unplug_mode)
792 if (inserted)
794 if ( global_settings.unplug_mode > 1 )
795 audio_resume();
796 backlight_on();
797 } else {
798 audio_pause();
800 if (global_settings.unplug_rw)
802 if ( audio_current_track()->elapsed >
803 (unsigned long)(global_settings.unplug_rw*1000))
804 audio_ff_rewind(audio_current_track()->elapsed -
805 (global_settings.unplug_rw*1000));
806 else
807 audio_ff_rewind(0);
812 #endif
814 long default_event_handler_ex(long event, void (*callback)(void *), void *parameter)
816 switch(event)
818 case SYS_USB_CONNECTED:
819 if (callback != NULL)
820 callback(parameter);
821 #ifdef HAVE_MMC
822 if (!mmc_touched() || (mmc_remove_request() == SYS_MMC_EXTRACTED))
823 #endif
825 scrobbler_flush_cache();
826 system_flush();
827 #ifdef BOOTFILE
828 #ifndef USB_IPODSTYLE
829 check_bootfile(false); /* gets initial size */
830 #endif
831 #endif
832 usb_screen();
833 #ifdef BOOTFILE
834 #ifndef USB_IPODSTYLE
835 check_bootfile(true);
836 #endif
837 #endif
838 system_restore();
840 return SYS_USB_CONNECTED;
841 case SYS_POWEROFF:
842 if (!clean_shutdown(callback, parameter))
843 return SYS_POWEROFF;
844 break;
845 #if CONFIG_CHARGING
846 case SYS_CHARGER_CONNECTED:
847 car_adapter_mode_processing(true);
848 return SYS_CHARGER_CONNECTED;
850 case SYS_CHARGER_DISCONNECTED:
851 car_adapter_mode_processing(false);
852 return SYS_CHARGER_DISCONNECTED;
853 #endif
854 #ifdef HAVE_HEADPHONE_DETECTION
855 case SYS_PHONE_PLUGGED:
856 unplug_change(true);
857 return SYS_PHONE_PLUGGED;
859 case SYS_PHONE_UNPLUGGED:
860 unplug_change(false);
861 return SYS_PHONE_UNPLUGGED;
862 #endif
864 return 0;
867 long default_event_handler(long event)
869 return default_event_handler_ex(event, NULL, NULL);
872 int show_logo( void )
874 #ifdef HAVE_LCD_BITMAP
875 char version[32];
876 int font_h, font_w;
878 snprintf(version, sizeof(version), "Ver. %s", appsversion);
880 lcd_clear_display();
881 lcd_bitmap(rockboxlogo, 0, 10, BMPWIDTH_rockboxlogo, BMPHEIGHT_rockboxlogo);
882 lcd_setfont(FONT_SYSFIXED);
883 lcd_getstringsize((unsigned char *)"A", &font_w, &font_h);
884 lcd_putsxy((LCD_WIDTH/2) - ((strlen(version)*font_w)/2),
885 LCD_HEIGHT-font_h, (unsigned char *)version);
886 lcd_setfont(FONT_UI);
888 #else
889 char *rockbox = " ROCKbox!";
891 lcd_clear_display();
892 lcd_double_height(true);
893 lcd_puts(0, 0, rockbox);
894 lcd_puts_scroll(0, 1, appsversion);
895 #endif
896 lcd_update();
898 #ifdef HAVE_REMOTE_LCD
899 lcd_remote_clear_display();
900 lcd_remote_bitmap(remote_rockboxlogo, 0, 10, BMPWIDTH_remote_rockboxlogo,
901 BMPHEIGHT_remote_rockboxlogo);
902 lcd_remote_setfont(FONT_SYSFIXED);
903 lcd_remote_getstringsize((unsigned char *)"A", &font_w, &font_h);
904 lcd_remote_putsxy((LCD_REMOTE_WIDTH/2) - ((strlen(version)*font_w)/2),
905 LCD_REMOTE_HEIGHT-font_h, (unsigned char *)version);
906 lcd_remote_setfont(FONT_UI);
907 lcd_remote_update();
908 #endif
910 return 0;
913 #if CONFIG_CODEC == SWCODEC
914 int get_replaygain_mode(bool have_track_gain, bool have_album_gain)
916 int type;
918 bool track = ((global_settings.replaygain_type == REPLAYGAIN_TRACK)
919 || ((global_settings.replaygain_type == REPLAYGAIN_SHUFFLE)
920 && global_settings.playlist_shuffle));
922 type = (!track && have_album_gain) ? REPLAYGAIN_ALBUM
923 : have_track_gain ? REPLAYGAIN_TRACK : -1;
925 return type;
927 #endif
929 #ifdef BOOTFILE
930 #ifndef USB_IPODSTYLE
932 memorize/compare details about the BOOTFILE
933 we don't use dircache because it may not be up to date after
934 USB disconnect (scanning in the background)
936 void check_bootfile(bool do_rolo)
938 static unsigned short wrtdate = 0;
939 static unsigned short wrttime = 0;
940 DIR* dir = NULL;
941 struct dirent* entry = NULL;
943 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
944 dir = opendir(BOOTDIR);
946 if(!dir) return; /* do we want an error splash? */
948 /* loop all files in BOOTDIR */
949 while(0 != (entry = readdir(dir)))
951 if(!strcasecmp(entry->d_name, BOOTFILE))
953 /* found the bootfile */
954 if(wrtdate && do_rolo)
956 if((entry->wrtdate != wrtdate) ||
957 (entry->wrttime != wrttime))
959 char *lines[] = { str(LANG_BOOT_CHANGED),
960 str(LANG_REBOOT_NOW) };
961 struct text_message message={ lines, 2 };
962 button_clear_queue(); /* Empty the keyboard buffer */
963 if(gui_syncyesno_run(&message, NULL, NULL) == YESNO_YES)
964 rolo_load(BOOTDIR "/" BOOTFILE);
967 wrtdate = entry->wrtdate;
968 wrttime = entry->wrttime;
971 closedir(dir);
973 #endif
974 #endif
976 /* check range, set volume and save settings */
977 void setvol(void)
979 const int min_vol = sound_min(SOUND_VOLUME);
980 const int max_vol = sound_max(SOUND_VOLUME);
981 if (global_settings.volume < min_vol)
982 global_settings.volume = min_vol;
983 if (global_settings.volume > max_vol)
984 global_settings.volume = max_vol;
985 sound_set_volume(global_settings.volume);
986 settings_save();
989 #ifdef HAVE_LCD_COLOR
991 * Helper function to convert a string of 6 hex digits to a native colour
994 #define hex2dec(c) (((c) >= '0' && ((c) <= '9')) ? (toupper(c)) - '0' : \
995 (toupper(c)) - 'A' + 10)
997 int hex_to_rgb(const char* hex)
998 { int ok = 1;
999 int i;
1000 int red, green, blue;
1002 if (strlen(hex) == 6) {
1003 for (i=0; i < 6; i++ ) {
1004 if (!isxdigit(hex[i])) {
1005 ok=0;
1006 break;
1010 if (ok) {
1011 red = (hex2dec(hex[0]) << 4) | hex2dec(hex[1]);
1012 green = (hex2dec(hex[2]) << 4) | hex2dec(hex[3]);
1013 blue = (hex2dec(hex[4]) << 4) | hex2dec(hex[5]);
1014 return LCD_RGBPACK(red,green,blue);
1018 return 0;
1020 #endif /* HAVE_LCD_COLOR */