simple fix for FS#7274 - selected item might not be shown when a list is drawn in...
[Rockbox.git] / apps / misc.c
blob0113825ac409c8de84620878c679b3350542478e
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 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
609 if(!charger_inserted())
610 #endif
612 bool batt_crit = battery_level_critical();
613 int audio_stat = audio_status();
615 FOR_NB_SCREENS(i)
616 screens[i].clear_display();
617 #ifdef X5_BACKLIGHT_SHUTDOWN
618 x5_backlight_shutdown();
619 #endif
620 if (!battery_level_safe())
621 gui_syncsplash(3*HZ, "%s %s",
622 str(LANG_WARNING_BATTERY_EMPTY),
623 str(LANG_SHUTTINGDOWN));
624 else if (battery_level_critical())
625 gui_syncsplash(3*HZ, "%s %s",
626 str(LANG_WARNING_BATTERY_LOW),
627 str(LANG_SHUTTINGDOWN));
628 else {
629 #ifdef HAVE_TAGCACHE
630 if (!tagcache_prepare_shutdown())
632 cancel_shutdown();
633 gui_syncsplash(HZ, str(LANG_TAGCACHE_BUSY));
634 return false;
636 #endif
637 gui_syncsplash(0, str(LANG_SHUTTINGDOWN));
640 if (global_settings.fade_on_stop
641 && (audio_stat & AUDIO_STATUS_PLAY))
643 fade(0);
646 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
647 if (!batt_crit && (audio_stat & AUDIO_STATUS_RECORD))
649 audio_stop_recording();
650 while(audio_status() & AUDIO_STATUS_RECORD)
651 sleep(1);
654 audio_close_recording();
655 #endif
656 /* audio_stop_recording == audio_stop for HWCODEC */
658 audio_stop();
659 while (audio_status())
660 sleep(1);
662 if (callback != NULL)
663 callback(parameter);
665 if (!batt_crit) /* do not save on critical battery */
666 system_flush();
667 #ifdef HAVE_EEPROM_SETTINGS
668 if (firmware_settings.initialized)
670 firmware_settings.disk_clean = true;
671 firmware_settings.bl_version = 0;
672 eeprom_settings_store();
674 #endif
675 shutdown_hw();
677 #endif
678 return false;
681 bool list_stop_handler(void)
683 bool ret = false;
685 /* Stop the music if it is playing */
686 if(audio_status())
688 if (!global_settings.party_mode)
690 if (global_settings.fade_on_stop)
691 fade(0);
692 bookmark_autobookmark();
693 audio_stop();
696 #if CONFIG_CHARGING
697 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
698 else
700 if (charger_inserted())
701 charging_splash();
702 else
703 shutdown_screen(); /* won't return if shutdown actually happens */
705 ret = true; /* screen is dirty, caller needs to refresh */
707 #endif
708 #ifndef HAVE_POWEROFF_WHILE_CHARGING
710 static long last_off = 0;
712 if (TIME_BEFORE(current_tick, last_off + HZ/2))
714 if (charger_inserted())
716 charging_splash();
717 ret = true; /* screen is dirty, caller needs to refresh */
720 last_off = current_tick;
722 #endif
723 #endif /* CONFIG_CHARGING */
724 return ret;
727 #if CONFIG_CHARGING
728 static bool waiting_to_resume_play = false;
729 static long play_resume_tick;
731 static void car_adapter_mode_processing(bool inserted)
733 if (global_settings.car_adapter_mode)
735 if(inserted)
738 * Just got plugged in, delay & resume if we were playing
740 if (audio_status() & AUDIO_STATUS_PAUSE)
742 /* delay resume a bit while the engine is cranking */
743 play_resume_tick = current_tick + HZ*5;
744 waiting_to_resume_play = true;
747 else
750 * Just got unplugged, pause if playing
752 if ((audio_status() & AUDIO_STATUS_PLAY) &&
753 !(audio_status() & AUDIO_STATUS_PAUSE))
755 if (global_settings.fade_on_stop)
756 fade(0);
757 else
758 audio_pause();
764 static void car_adapter_tick(void)
766 if (waiting_to_resume_play)
768 if (TIME_AFTER(current_tick, play_resume_tick))
770 if (audio_status() & AUDIO_STATUS_PAUSE)
772 audio_resume();
774 waiting_to_resume_play = false;
779 void car_adapter_mode_init(void)
781 tick_add_task(car_adapter_tick);
783 #endif
785 #ifdef HAVE_HEADPHONE_DETECTION
786 static void unplug_change(bool inserted)
788 if (global_settings.unplug_mode)
790 if (inserted)
792 if ( global_settings.unplug_mode > 1 )
793 audio_resume();
794 backlight_on();
795 } else {
796 audio_pause();
798 if (global_settings.unplug_rw)
800 if ( audio_current_track()->elapsed >
801 (unsigned long)(global_settings.unplug_rw*1000))
802 audio_ff_rewind(audio_current_track()->elapsed -
803 (global_settings.unplug_rw*1000));
804 else
805 audio_ff_rewind(0);
810 #endif
812 long default_event_handler_ex(long event, void (*callback)(void *), void *parameter)
814 switch(event)
816 case SYS_USB_CONNECTED:
817 if (callback != NULL)
818 callback(parameter);
819 #ifdef HAVE_MMC
820 if (!mmc_touched() || (mmc_remove_request() == SYS_MMC_EXTRACTED))
821 #endif
823 scrobbler_flush_cache();
824 system_flush();
825 #ifdef BOOTFILE
826 #ifndef USB_IPODSTYLE
827 check_bootfile(false); /* gets initial size */
828 #endif
829 #endif
830 usb_screen();
831 #ifdef BOOTFILE
832 #ifndef USB_IPODSTYLE
833 check_bootfile(true);
834 #endif
835 #endif
836 system_restore();
838 return SYS_USB_CONNECTED;
839 case SYS_POWEROFF:
840 if (!clean_shutdown(callback, parameter))
841 return SYS_POWEROFF;
842 break;
843 #if CONFIG_CHARGING
844 case SYS_CHARGER_CONNECTED:
845 car_adapter_mode_processing(true);
846 return SYS_CHARGER_CONNECTED;
848 case SYS_CHARGER_DISCONNECTED:
849 car_adapter_mode_processing(false);
850 return SYS_CHARGER_DISCONNECTED;
851 #endif
852 #ifdef HAVE_HEADPHONE_DETECTION
853 case SYS_PHONE_PLUGGED:
854 unplug_change(true);
855 return SYS_PHONE_PLUGGED;
857 case SYS_PHONE_UNPLUGGED:
858 unplug_change(false);
859 return SYS_PHONE_UNPLUGGED;
860 #endif
862 return 0;
865 long default_event_handler(long event)
867 return default_event_handler_ex(event, NULL, NULL);
870 int show_logo( void )
872 #ifdef HAVE_LCD_BITMAP
873 char version[32];
874 int font_h, font_w;
876 snprintf(version, sizeof(version), "Ver. %s", appsversion);
878 lcd_clear_display();
879 lcd_bitmap(rockboxlogo, 0, 10, BMPWIDTH_rockboxlogo, BMPHEIGHT_rockboxlogo);
880 lcd_setfont(FONT_SYSFIXED);
881 lcd_getstringsize((unsigned char *)"A", &font_w, &font_h);
882 lcd_putsxy((LCD_WIDTH/2) - ((strlen(version)*font_w)/2),
883 LCD_HEIGHT-font_h, (unsigned char *)version);
884 lcd_setfont(FONT_UI);
886 #else
887 char *rockbox = " ROCKbox!";
889 lcd_clear_display();
890 lcd_double_height(true);
891 lcd_puts(0, 0, rockbox);
892 lcd_puts_scroll(0, 1, appsversion);
893 #endif
894 lcd_update();
896 #ifdef HAVE_REMOTE_LCD
897 lcd_remote_clear_display();
898 lcd_remote_bitmap(remote_rockboxlogo, 0, 10, BMPWIDTH_remote_rockboxlogo,
899 BMPHEIGHT_remote_rockboxlogo);
900 lcd_remote_setfont(FONT_SYSFIXED);
901 lcd_remote_getstringsize((unsigned char *)"A", &font_w, &font_h);
902 lcd_remote_putsxy((LCD_REMOTE_WIDTH/2) - ((strlen(version)*font_w)/2),
903 LCD_REMOTE_HEIGHT-font_h, (unsigned char *)version);
904 lcd_remote_setfont(FONT_UI);
905 lcd_remote_update();
906 #endif
908 return 0;
911 #if CONFIG_CODEC == SWCODEC
912 int get_replaygain_mode(bool have_track_gain, bool have_album_gain)
914 int type;
916 bool track = ((global_settings.replaygain_type == REPLAYGAIN_TRACK)
917 || ((global_settings.replaygain_type == REPLAYGAIN_SHUFFLE)
918 && global_settings.playlist_shuffle));
920 type = (!track && have_album_gain) ? REPLAYGAIN_ALBUM
921 : have_track_gain ? REPLAYGAIN_TRACK : -1;
923 return type;
925 #endif
927 #ifdef BOOTFILE
928 #ifndef USB_IPODSTYLE
930 memorize/compare details about the BOOTFILE
931 we don't use dircache because it may not be up to date after
932 USB disconnect (scanning in the background)
934 void check_bootfile(bool do_rolo)
936 static unsigned short wrtdate = 0;
937 static unsigned short wrttime = 0;
938 DIR* dir = NULL;
939 struct dirent* entry = NULL;
941 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
942 dir = opendir(BOOTDIR);
944 if(!dir) return; /* do we want an error splash? */
946 /* loop all files in BOOTDIR */
947 while(0 != (entry = readdir(dir)))
949 if(!strcasecmp(entry->d_name, BOOTFILE))
951 /* found the bootfile */
952 if(wrtdate && do_rolo)
954 if((entry->wrtdate != wrtdate) ||
955 (entry->wrttime != wrttime))
957 char *lines[] = { str(LANG_BOOT_CHANGED),
958 str(LANG_REBOOT_NOW) };
959 struct text_message message={ lines, 2 };
960 button_clear_queue(); /* Empty the keyboard buffer */
961 if(gui_syncyesno_run(&message, NULL, NULL) == YESNO_YES)
962 rolo_load(BOOTDIR "/" BOOTFILE);
965 wrtdate = entry->wrtdate;
966 wrttime = entry->wrttime;
969 closedir(dir);
971 #endif
972 #endif
974 /* check range, set volume and save settings */
975 void setvol(void)
977 const int min_vol = sound_min(SOUND_VOLUME);
978 const int max_vol = sound_max(SOUND_VOLUME);
979 if (global_settings.volume < min_vol)
980 global_settings.volume = min_vol;
981 if (global_settings.volume > max_vol)
982 global_settings.volume = max_vol;
983 sound_set_volume(global_settings.volume);
984 settings_save();