make the dialog box asking about replacing an up-to-date bootloader less confusing.
[Rockbox.git] / apps / misc.c
blob3c6ffd30321172e81b054deacdd9c0cc1916b4ae
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 0x90, 0xee, 0x90, 0x00, /* Colour #0 */
397 0x00, 0x00, 0x00, 0x00 /* Colour #1 */
398 #elif LCD_DEPTH == 2
399 0xe6, 0xd8, 0xad, 0x00, /* Colour #0 */
400 0x99, 0x90, 0x73, 0x00, /* Colour #1 */
401 0x4c, 0x48, 0x39, 0x00, /* Colour #2 */
402 0x00, 0x00, 0x00, 0x00 /* Colour #3 */
403 #elif LCD_DEPTH == 16
404 0x00, 0xf8, 0x00, 0x00, /* red bitfield mask */
405 0xe0, 0x07, 0x00, 0x00, /* green bitfield mask */
406 0x1f, 0x00, 0x00, 0x00 /* blue bitfield mask */
407 #endif
410 static void (*screen_dump_hook)(int fh) = NULL;
412 void screen_dump(void)
414 int fh;
415 char filename[MAX_PATH];
416 int bx, by;
417 #if LCD_DEPTH == 1
418 static unsigned char line_block[8][BMP_LINESIZE];
419 #elif LCD_DEPTH == 2
420 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
421 static unsigned char line_block[BMP_LINESIZE];
422 #else
423 static unsigned char line_block[4][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 #else /* 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++;
510 unsigned src_byte1 = *src++;
511 int iy;
513 for (iy = 4; iy > 0; iy--)
515 *dst_blk = ((src_byte0 & 3) << 4) | (src_byte1 & 3);
516 src_byte0 >>= 2;
517 src_byte1 >>= 2;
518 dst_blk -= BMP_LINESIZE;
522 write(fh, line_block, sizeof(line_block));
524 #endif
525 #elif LCD_DEPTH == 16
526 for (by = LCD_HEIGHT - 1; by >= 0; by--)
528 unsigned short *src = &lcd_framebuffer[by][0];
529 unsigned short *dst = line_block;
531 memset(line_block, 0, sizeof(line_block));
532 for (bx = LCD_WIDTH; bx > 0; bx--)
534 #if (LCD_PIXELFORMAT == RGB565SWAPPED)
535 /* iPod LCD data is big endian although the CPU is not */
536 *dst++ = htobe16(*src++);
537 #else
538 *dst++ = htole16(*src++);
539 #endif
542 write(fh, line_block, sizeof(line_block));
544 #endif /* LCD_DEPTH */
547 close(fh);
550 void screen_dump_set_hook(void (*hook)(int fh))
552 screen_dump_hook = hook;
555 #endif /* HAVE_LCD_BITMAP */
557 /* parse a line from a configuration file. the line format is:
559 name: value
561 Any whitespace before setting name or value (after ':') is ignored.
562 A # as first non-whitespace character discards the whole line.
563 Function sets pointers to null-terminated setting name and value.
564 Returns false if no valid config entry was found.
567 bool settings_parseline(char* line, char** name, char** value)
569 char* ptr;
571 while ( isspace(*line) )
572 line++;
574 if ( *line == '#' )
575 return false;
577 ptr = strchr(line, ':');
578 if ( !ptr )
579 return false;
581 *name = line;
582 *ptr = 0;
583 ptr++;
584 while (isspace(*ptr))
585 ptr++;
586 *value = ptr;
587 return true;
590 static void system_flush(void)
592 tree_flush();
593 call_ata_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
596 static void system_restore(void)
598 tree_restore();
601 static bool clean_shutdown(void (*callback)(void *), void *parameter)
603 #ifdef SIMULATOR
604 (void)callback;
605 (void)parameter;
606 bookmark_autobookmark();
607 call_ata_idle_notifys(true);
608 exit(0);
609 #else
610 long msg_id = -1;
611 int i;
613 scrobbler_poweroff();
615 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
616 if(!charger_inserted())
617 #endif
619 bool batt_safe = battery_level_safe();
620 int audio_stat = audio_status();
622 FOR_NB_SCREENS(i)
623 screens[i].clear_display();
625 if (batt_safe)
627 #ifdef HAVE_TAGCACHE
628 if (!tagcache_prepare_shutdown())
630 cancel_shutdown();
631 gui_syncsplash(HZ, ID2P(LANG_TAGCACHE_BUSY));
632 return false;
634 #endif
635 if (battery_level() > 10)
636 gui_syncsplash(0, str(LANG_SHUTTINGDOWN));
637 else
639 msg_id = LANG_WARNING_BATTERY_LOW;
640 gui_syncsplash(0, "%s %s",
641 str(LANG_WARNING_BATTERY_LOW),
642 str(LANG_SHUTTINGDOWN));
645 else
647 msg_id = LANG_WARNING_BATTERY_EMPTY;
648 gui_syncsplash(0, "%s %s",
649 str(LANG_WARNING_BATTERY_EMPTY),
650 str(LANG_SHUTTINGDOWN));
653 if (global_settings.fade_on_stop
654 && (audio_stat & AUDIO_STATUS_PLAY))
656 fade(0);
659 if (batt_safe) /* do not save on critical battery */
661 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
662 if (audio_stat & AUDIO_STATUS_RECORD)
664 rec_command(RECORDING_CMD_STOP);
665 /* wait for stop to complete */
666 while (audio_status() & AUDIO_STATUS_RECORD)
667 sleep(1);
669 #endif
670 bookmark_autobookmark();
672 /* audio_stop_recording == audio_stop for HWCODEC */
673 audio_stop();
675 if (callback != NULL)
676 callback(parameter);
678 #if CONFIG_CODEC != SWCODEC
679 /* wait for audio_stop or audio_stop_recording to complete */
680 while (audio_status())
681 sleep(1);
682 #endif
684 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
685 audio_close_recording();
686 #endif
688 if(global_settings.talk_menu)
690 bool enqueue = false;
691 if(msg_id != -1)
693 talk_id(msg_id, enqueue);
694 enqueue = true;
696 talk_id(LANG_SHUTTINGDOWN, enqueue);
697 #if CONFIG_CODEC == SWCODEC
698 voice_wait();
699 #endif
702 system_flush();
703 #ifdef HAVE_EEPROM_SETTINGS
704 if (firmware_settings.initialized)
706 firmware_settings.disk_clean = true;
707 firmware_settings.bl_version = 0;
708 eeprom_settings_store();
710 #endif
712 #ifdef HAVE_DIRCACHE
713 else
714 dircache_disable();
715 #endif
717 shutdown_hw();
719 #endif
720 return false;
723 bool list_stop_handler(void)
725 bool ret = false;
727 /* Stop the music if it is playing */
728 if(audio_status())
730 if (!global_settings.party_mode)
732 if (global_settings.fade_on_stop)
733 fade(0);
734 bookmark_autobookmark();
735 audio_stop();
736 ret = true; /* bookmarking can make a refresh necessary */
739 #if CONFIG_CHARGING
740 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
741 else
743 if (charger_inserted())
744 charging_splash();
745 else
746 shutdown_screen(); /* won't return if shutdown actually happens */
748 ret = true; /* screen is dirty, caller needs to refresh */
750 #endif
751 #ifndef HAVE_POWEROFF_WHILE_CHARGING
753 static long last_off = 0;
755 if (TIME_BEFORE(current_tick, last_off + HZ/2))
757 if (charger_inserted())
759 charging_splash();
760 ret = true; /* screen is dirty, caller needs to refresh */
763 last_off = current_tick;
765 #endif
766 #endif /* CONFIG_CHARGING */
767 return ret;
770 #if CONFIG_CHARGING
771 static bool waiting_to_resume_play = false;
772 static long play_resume_tick;
774 static void car_adapter_mode_processing(bool inserted)
776 if (global_settings.car_adapter_mode)
778 if(inserted)
781 * Just got plugged in, delay & resume if we were playing
783 if (audio_status() & AUDIO_STATUS_PAUSE)
785 /* delay resume a bit while the engine is cranking */
786 play_resume_tick = current_tick + HZ*5;
787 waiting_to_resume_play = true;
790 else
793 * Just got unplugged, pause if playing
795 if ((audio_status() & AUDIO_STATUS_PLAY) &&
796 !(audio_status() & AUDIO_STATUS_PAUSE))
798 if (global_settings.fade_on_stop)
799 fade(0);
800 else
801 audio_pause();
803 waiting_to_resume_play = false;
808 static void car_adapter_tick(void)
810 if (waiting_to_resume_play)
812 if (TIME_AFTER(current_tick, play_resume_tick))
814 if (audio_status() & AUDIO_STATUS_PAUSE)
816 queue_broadcast(SYS_CAR_ADAPTER_RESUME, 0);
818 waiting_to_resume_play = false;
823 void car_adapter_mode_init(void)
825 tick_add_task(car_adapter_tick);
827 #endif
829 #ifdef HAVE_HEADPHONE_DETECTION
830 static void unplug_change(bool inserted)
832 if (global_settings.unplug_mode)
834 if (inserted)
836 if ( global_settings.unplug_mode > 1 )
837 audio_resume();
838 backlight_on();
839 } else {
840 audio_pause();
842 if (global_settings.unplug_rw)
844 if ( audio_current_track()->elapsed >
845 (unsigned long)(global_settings.unplug_rw*1000))
846 audio_ff_rewind(audio_current_track()->elapsed -
847 (global_settings.unplug_rw*1000));
848 else
849 audio_ff_rewind(0);
854 #endif
856 long default_event_handler_ex(long event, void (*callback)(void *), void *parameter)
858 switch(event)
860 case SYS_USB_CONNECTED:
861 if (callback != NULL)
862 callback(parameter);
863 #ifdef HAVE_MMC
864 if (!mmc_touched() ||
865 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
866 #endif
868 scrobbler_flush_cache();
869 system_flush();
870 #ifdef BOOTFILE
871 #if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
872 check_bootfile(false); /* gets initial size */
873 #endif
874 #endif
875 usb_screen();
876 #ifdef BOOTFILE
877 #if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
878 check_bootfile(true);
879 #endif
880 #endif
881 system_restore();
883 return SYS_USB_CONNECTED;
884 case SYS_POWEROFF:
885 if (!clean_shutdown(callback, parameter))
886 return SYS_POWEROFF;
887 break;
888 #if CONFIG_CHARGING
889 case SYS_CHARGER_CONNECTED:
890 car_adapter_mode_processing(true);
891 return SYS_CHARGER_CONNECTED;
893 case SYS_CHARGER_DISCONNECTED:
894 car_adapter_mode_processing(false);
895 return SYS_CHARGER_DISCONNECTED;
897 case SYS_CAR_ADAPTER_RESUME:
898 audio_resume();
899 return SYS_CAR_ADAPTER_RESUME;
900 #endif
901 #ifdef HAVE_HEADPHONE_DETECTION
902 case SYS_PHONE_PLUGGED:
903 unplug_change(true);
904 return SYS_PHONE_PLUGGED;
906 case SYS_PHONE_UNPLUGGED:
907 unplug_change(false);
908 return SYS_PHONE_UNPLUGGED;
909 #endif
911 return 0;
914 long default_event_handler(long event)
916 return default_event_handler_ex(event, NULL, NULL);
919 int show_logo( void )
921 #ifdef HAVE_LCD_BITMAP
922 char version[32];
923 int font_h, font_w;
925 snprintf(version, sizeof(version), "Ver. %s", appsversion);
927 lcd_clear_display();
928 lcd_bitmap(rockboxlogo, 0, 10, BMPWIDTH_rockboxlogo, BMPHEIGHT_rockboxlogo);
929 lcd_setfont(FONT_SYSFIXED);
930 lcd_getstringsize((unsigned char *)"A", &font_w, &font_h);
931 lcd_putsxy((LCD_WIDTH/2) - ((strlen(version)*font_w)/2),
932 LCD_HEIGHT-font_h, (unsigned char *)version);
933 lcd_setfont(FONT_UI);
935 #else
936 char *rockbox = " ROCKbox!";
938 lcd_clear_display();
939 lcd_double_height(true);
940 lcd_puts(0, 0, rockbox);
941 lcd_puts_scroll(0, 1, appsversion);
942 #endif
943 lcd_update();
945 #ifdef HAVE_REMOTE_LCD
946 lcd_remote_clear_display();
947 lcd_remote_bitmap(remote_rockboxlogo, 0, 10, BMPWIDTH_remote_rockboxlogo,
948 BMPHEIGHT_remote_rockboxlogo);
949 lcd_remote_setfont(FONT_SYSFIXED);
950 lcd_remote_getstringsize((unsigned char *)"A", &font_w, &font_h);
951 lcd_remote_putsxy((LCD_REMOTE_WIDTH/2) - ((strlen(version)*font_w)/2),
952 LCD_REMOTE_HEIGHT-font_h, (unsigned char *)version);
953 lcd_remote_setfont(FONT_UI);
954 lcd_remote_update();
955 #endif
957 return 0;
960 #if CONFIG_CODEC == SWCODEC
961 int get_replaygain_mode(bool have_track_gain, bool have_album_gain)
963 int type;
965 bool track = ((global_settings.replaygain_type == REPLAYGAIN_TRACK)
966 || ((global_settings.replaygain_type == REPLAYGAIN_SHUFFLE)
967 && global_settings.playlist_shuffle));
969 type = (!track && have_album_gain) ? REPLAYGAIN_ALBUM
970 : have_track_gain ? REPLAYGAIN_TRACK : -1;
972 return type;
974 #endif
976 #ifdef BOOTFILE
977 #if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
979 memorize/compare details about the BOOTFILE
980 we don't use dircache because it may not be up to date after
981 USB disconnect (scanning in the background)
983 void check_bootfile(bool do_rolo)
985 static unsigned short wrtdate = 0;
986 static unsigned short wrttime = 0;
987 DIR* dir = NULL;
988 struct dirent* entry = NULL;
990 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
991 dir = opendir(BOOTDIR);
993 if(!dir) return; /* do we want an error splash? */
995 /* loop all files in BOOTDIR */
996 while(0 != (entry = readdir(dir)))
998 if(!strcasecmp(entry->d_name, BOOTFILE))
1000 /* found the bootfile */
1001 if(wrtdate && do_rolo)
1003 if((entry->wrtdate != wrtdate) ||
1004 (entry->wrttime != wrttime))
1006 char *lines[] = { ID2P(LANG_BOOT_CHANGED),
1007 ID2P(LANG_REBOOT_NOW) };
1008 struct text_message message={ lines, 2 };
1009 button_clear_queue(); /* Empty the keyboard buffer */
1010 if(gui_syncyesno_run(&message, NULL, NULL) == YESNO_YES)
1011 rolo_load(BOOTDIR "/" BOOTFILE);
1014 wrtdate = entry->wrtdate;
1015 wrttime = entry->wrttime;
1018 closedir(dir);
1020 #endif
1021 #endif
1023 /* check range, set volume and save settings */
1024 void setvol(void)
1026 const int min_vol = sound_min(SOUND_VOLUME);
1027 const int max_vol = sound_max(SOUND_VOLUME);
1028 if (global_settings.volume < min_vol)
1029 global_settings.volume = min_vol;
1030 if (global_settings.volume > max_vol)
1031 global_settings.volume = max_vol;
1032 sound_set_volume(global_settings.volume);
1033 settings_save();
1036 #ifdef HAVE_LCD_COLOR
1038 * Helper function to convert a string of 6 hex digits to a native colour
1041 #define hex2dec(c) (((c) >= '0' && ((c) <= '9')) ? (toupper(c)) - '0' : \
1042 (toupper(c)) - 'A' + 10)
1044 int hex_to_rgb(const char* hex)
1045 { int ok = 1;
1046 int i;
1047 int red, green, blue;
1049 if (strlen(hex) == 6) {
1050 for (i=0; i < 6; i++ ) {
1051 if (!isxdigit(hex[i])) {
1052 ok=0;
1053 break;
1057 if (ok) {
1058 red = (hex2dec(hex[0]) << 4) | hex2dec(hex[1]);
1059 green = (hex2dec(hex[2]) << 4) | hex2dec(hex[3]);
1060 blue = (hex2dec(hex[4]) << 4) | hex2dec(hex[5]);
1061 return LCD_RGBPACK(red,green,blue);
1065 return 0;
1067 #endif /* HAVE_LCD_COLOR */