Use size_t type for the buffer_size parameter to formatter functions, static two...
[Rockbox.git] / apps / misc.c
blobe4149581f5f0fb3e1a97b2b870f5dbd4cc58b8ce
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"
65 #include "playback.h"
67 #ifdef BOOTFILE
68 #if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
69 #include "textarea.h"
70 #include "rolo.h"
71 #include "yesno.h"
72 #endif
73 #endif
75 /* Format a large-range value for output, using the appropriate unit so that
76 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
77 * units) if possible, and 3 significant digits are shown. If a buffer is
78 * given, the result is snprintf()'d into that buffer, otherwise the result is
79 * voiced.*/
80 char *output_dyn_value(char *buf, int buf_size, int value,
81 const unsigned char **units, bool bin_scale)
83 int scale = bin_scale ? 1024 : 1000;
84 int fraction = 0;
85 int unit_no = 0;
86 int i;
87 char tbuf[5];
89 while (value >= scale)
91 fraction = value % scale;
92 value /= scale;
93 unit_no++;
95 if (bin_scale)
96 fraction = fraction * 1000 / 1024;
98 if (value >= 100 || !unit_no)
99 tbuf[0] = '\0';
100 else if (value >= 10)
101 snprintf(tbuf, sizeof(tbuf), "%01d", fraction / 100);
102 else
103 snprintf(tbuf, sizeof(tbuf), "%02d", fraction / 10);
105 if (buf)
107 if (strlen(tbuf))
108 snprintf(buf, buf_size, "%d%s%s%s", value, str(LANG_POINT),
109 tbuf, P2STR(units[unit_no]));
110 else
111 snprintf(buf, buf_size, "%d%s", value, P2STR(units[unit_no]));
113 else
115 /* strip trailing zeros from the fraction */
116 for (i = strlen(tbuf) - 1; (i >= 0) && (tbuf[i] == '0'); i--)
117 tbuf[i] = '\0';
119 talk_number(value, true);
120 if (tbuf[0] != 0)
122 talk_id(LANG_POINT, true);
123 talk_spell(tbuf, true);
125 talk_id(P2ID(units[unit_no]), true);
127 return buf;
130 /* Create a filename with a number part in a way that the number is 1
131 * higher than the highest numbered file matching the same pattern.
132 * It is allowed that buffer and path point to the same memory location,
133 * saving a strcpy(). Path must always be given without trailing slash.
134 * "num" can point to an int specifying the number to use or NULL or a value
135 * less than zero to number automatically. The final number used will also
136 * be returned in *num. If *num is >= 0 then *num will be incremented by
137 * one. */
138 char *create_numbered_filename(char *buffer, const char *path,
139 const char *prefix, const char *suffix,
140 int numberlen IF_CNFN_NUM_(, int *num))
142 DIR *dir;
143 struct dirent *entry;
144 int max_num;
145 int pathlen;
146 int prefixlen = strlen(prefix);
147 char fmtstring[12];
149 if (buffer != path)
150 strncpy(buffer, path, MAX_PATH);
152 pathlen = strlen(buffer);
154 #ifdef IF_CNFN_NUM
155 if (num && *num >= 0)
157 /* number specified */
158 max_num = *num;
160 else
161 #endif
163 /* automatic numbering */
164 max_num = 0;
166 dir = opendir(pathlen ? buffer : "/");
167 if (!dir)
168 return NULL;
170 while ((entry = readdir(dir)))
172 int curr_num;
174 if (strncasecmp((char *)entry->d_name, prefix, prefixlen)
175 || strcasecmp((char *)entry->d_name + prefixlen + numberlen, suffix))
176 continue;
178 curr_num = atoi((char *)entry->d_name + prefixlen);
179 if (curr_num > max_num)
180 max_num = curr_num;
183 closedir(dir);
186 max_num++;
188 snprintf(fmtstring, sizeof(fmtstring), "/%%s%%0%dd%%s", numberlen);
189 snprintf(buffer + pathlen, MAX_PATH - pathlen, fmtstring, prefix,
190 max_num, suffix);
192 #ifdef IF_CNFN_NUM
193 if (num)
194 *num = max_num;
195 #endif
197 return buffer;
200 /* Format time into buf.
202 * buf - buffer to format to.
203 * buf_size - size of buffer.
204 * t - time to format, in milliseconds.
206 void format_time(char* buf, int buf_size, long t)
208 if ( t < 3600000 )
210 snprintf(buf, buf_size, "%d:%02d",
211 (int) (t / 60000), (int) (t % 60000 / 1000));
213 else
215 snprintf(buf, buf_size, "%d:%02d:%02d",
216 (int) (t / 3600000), (int) (t % 3600000 / 60000),
217 (int) (t % 60000 / 1000));
221 #if CONFIG_RTC
222 /* Create a filename with a date+time part.
223 It is allowed that buffer and path point to the same memory location,
224 saving a strcpy(). Path must always be given without trailing slash.
225 unique_time as true makes the function wait until the current time has
226 changed. */
227 char *create_datetime_filename(char *buffer, const char *path,
228 const char *prefix, const char *suffix,
229 bool unique_time)
231 struct tm *tm = get_time();
232 static struct tm last_tm;
233 int pathlen;
235 while (unique_time && !memcmp(get_time(), &last_tm, sizeof (struct tm)))
236 sleep(HZ/10);
238 last_tm = *tm;
240 if (buffer != path)
241 strncpy(buffer, path, MAX_PATH);
243 pathlen = strlen(buffer);
244 snprintf(buffer + pathlen, MAX_PATH - pathlen,
245 "/%s%02d%02d%02d-%02d%02d%02d%s", prefix,
246 tm->tm_year % 100, tm->tm_mon + 1, tm->tm_mday,
247 tm->tm_hour, tm->tm_min, tm->tm_sec, suffix);
249 return buffer;
251 #endif /* CONFIG_RTC */
253 /* Read (up to) a line of text from fd into buffer and return number of bytes
254 * read (which may be larger than the number of bytes stored in buffer). If
255 * an error occurs, -1 is returned (and buffer contains whatever could be
256 * read). A line is terminated by a LF char. Neither LF nor CR chars are
257 * stored in buffer.
259 int read_line(int fd, char* buffer, int buffer_size)
261 int count = 0;
262 int num_read = 0;
264 errno = 0;
266 while (count < buffer_size)
268 unsigned char c;
270 if (1 != read(fd, &c, 1))
271 break;
273 num_read++;
275 if ( c == '\n' )
276 break;
278 if ( c == '\r' )
279 continue;
281 buffer[count++] = c;
284 buffer[MIN(count, buffer_size - 1)] = 0;
286 return errno ? -1 : num_read;
289 /* Performance optimized version of the previous function. */
290 int fast_readline(int fd, char *buf, int buf_size, void *parameters,
291 int (*callback)(int n, const char *buf, void *parameters))
293 char *p, *next;
294 int rc, pos = 0;
295 int count = 0;
297 while ( 1 )
299 next = NULL;
301 rc = read(fd, &buf[pos], buf_size - pos - 1);
302 if (rc >= 0)
303 buf[pos+rc] = '\0';
305 if ( (p = strchr(buf, '\r')) != NULL)
307 *p = '\0';
308 next = ++p;
310 else
311 p = buf;
313 if ( (p = strchr(p, '\n')) != NULL)
315 *p = '\0';
316 next = ++p;
319 rc = callback(count, buf, parameters);
320 if (rc < 0)
321 return rc;
323 count++;
324 if (next)
326 pos = buf_size - ((long)next - (long)buf) - 1;
327 memmove(buf, next, pos);
329 else
330 break ;
333 return 0;
336 #ifdef HAVE_LCD_BITMAP
338 #if LCD_DEPTH == 16
339 #define BMP_COMPRESSION 3 /* BI_BITFIELDS */
340 #define BMP_NUMCOLORS 3
341 #else
342 #define BMP_COMPRESSION 0 /* BI_RGB */
343 #if LCD_DEPTH <= 8
344 #define BMP_NUMCOLORS (1 << LCD_DEPTH)
345 #else
346 #define BMP_NUMCOLORS 0
347 #endif
348 #endif
350 #if LCD_DEPTH == 1
351 #define BMP_BPP 1
352 #define BMP_LINESIZE ((LCD_WIDTH/8 + 3) & ~3)
353 #elif LCD_DEPTH <= 4
354 #define BMP_BPP 4
355 #define BMP_LINESIZE ((LCD_WIDTH/2 + 3) & ~3)
356 #elif LCD_DEPTH <= 8
357 #define BMP_BPP 8
358 #define BMP_LINESIZE ((LCD_WIDTH + 3) & ~3)
359 #elif LCD_DEPTH <= 16
360 #define BMP_BPP 16
361 #define BMP_LINESIZE ((LCD_WIDTH*2 + 3) & ~3)
362 #else
363 #define BMP_BPP 24
364 #define BMP_LINESIZE ((LCD_WIDTH*3 + 3) & ~3)
365 #endif
367 #define BMP_HEADERSIZE (54 + 4 * BMP_NUMCOLORS)
368 #define BMP_DATASIZE (BMP_LINESIZE * LCD_HEIGHT)
369 #define BMP_TOTALSIZE (BMP_HEADERSIZE + BMP_DATASIZE)
371 #define LE16_CONST(x) (x)&0xff, ((x)>>8)&0xff
372 #define LE32_CONST(x) (x)&0xff, ((x)>>8)&0xff, ((x)>>16)&0xff, ((x)>>24)&0xff
374 static const unsigned char bmpheader[] =
376 0x42, 0x4d, /* 'BM' */
377 LE32_CONST(BMP_TOTALSIZE), /* Total file size */
378 0x00, 0x00, 0x00, 0x00, /* Reserved */
379 LE32_CONST(BMP_HEADERSIZE), /* Offset to start of pixel data */
381 0x28, 0x00, 0x00, 0x00, /* Size of (2nd) header */
382 LE32_CONST(LCD_WIDTH), /* Width in pixels */
383 LE32_CONST(LCD_HEIGHT), /* Height in pixels */
384 0x01, 0x00, /* Number of planes (always 1) */
385 LE16_CONST(BMP_BPP), /* Bits per pixel 1/4/8/16/24 */
386 LE32_CONST(BMP_COMPRESSION),/* Compression mode */
387 LE32_CONST(BMP_DATASIZE), /* Size of bitmap data */
388 0xc4, 0x0e, 0x00, 0x00, /* Horizontal resolution (pixels/meter) */
389 0xc4, 0x0e, 0x00, 0x00, /* Vertical resolution (pixels/meter) */
390 LE32_CONST(BMP_NUMCOLORS), /* Number of used colours */
391 LE32_CONST(BMP_NUMCOLORS), /* Number of important colours */
393 #if LCD_DEPTH == 1
394 0x90, 0xee, 0x90, 0x00, /* Colour #0 */
395 0x00, 0x00, 0x00, 0x00 /* Colour #1 */
396 #elif LCD_DEPTH == 2
397 0xe6, 0xd8, 0xad, 0x00, /* Colour #0 */
398 0x99, 0x90, 0x73, 0x00, /* Colour #1 */
399 0x4c, 0x48, 0x39, 0x00, /* Colour #2 */
400 0x00, 0x00, 0x00, 0x00 /* Colour #3 */
401 #elif LCD_DEPTH == 16
402 0x00, 0xf8, 0x00, 0x00, /* red bitfield mask */
403 0xe0, 0x07, 0x00, 0x00, /* green bitfield mask */
404 0x1f, 0x00, 0x00, 0x00 /* blue bitfield mask */
405 #endif
408 static void (*screen_dump_hook)(int fh) = NULL;
410 void screen_dump(void)
412 int fh;
413 char filename[MAX_PATH];
414 int bx, by;
415 #if LCD_DEPTH == 1
416 static unsigned char line_block[8][BMP_LINESIZE];
417 #elif LCD_DEPTH == 2
418 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
419 static unsigned char line_block[BMP_LINESIZE];
420 #else
421 static unsigned char line_block[4][BMP_LINESIZE];
422 #endif
423 #elif LCD_DEPTH == 16
424 static unsigned short line_block[BMP_LINESIZE/2];
425 #endif
427 #if CONFIG_RTC
428 create_datetime_filename(filename, "", "dump ", ".bmp", false);
429 #else
430 create_numbered_filename(filename, "", "dump_", ".bmp", 4
431 IF_CNFN_NUM_(, NULL));
432 #endif
434 fh = creat(filename);
435 if (fh < 0)
436 return;
438 if (screen_dump_hook)
440 screen_dump_hook(fh);
442 else
444 write(fh, bmpheader, sizeof(bmpheader));
446 /* BMP image goes bottom up */
447 #if LCD_DEPTH == 1
448 for (by = LCD_FBHEIGHT - 1; by >= 0; by--)
450 unsigned char *src = &lcd_framebuffer[by][0];
451 unsigned char *dst = &line_block[0][0];
453 memset(line_block, 0, sizeof(line_block));
454 for (bx = LCD_WIDTH/8; bx > 0; bx--)
456 unsigned dst_mask = 0x80;
457 int ix;
459 for (ix = 8; ix > 0; ix--)
461 unsigned char *dst_blk = dst;
462 unsigned src_byte = *src++;
463 int iy;
465 for (iy = 8; iy > 0; iy--)
467 if (src_byte & 0x80)
468 *dst_blk |= dst_mask;
469 src_byte <<= 1;
470 dst_blk += BMP_LINESIZE;
472 dst_mask >>= 1;
474 dst++;
477 write(fh, line_block, sizeof(line_block));
479 #elif LCD_DEPTH == 2
480 #if LCD_PIXELFORMAT == HORIZONTAL_PACKING
481 for (by = LCD_FBHEIGHT - 1; by >= 0; by--)
483 unsigned char *src = &lcd_framebuffer[by][0];
484 unsigned char *dst = line_block;
486 memset(line_block, 0, sizeof(line_block));
487 for (bx = LCD_FBWIDTH; bx > 0; bx--)
489 unsigned src_byte = *src++;
491 *dst++ = ((src_byte >> 2) & 0x30) | ((src_byte >> 4) & 0x03);
492 *dst++ = ((src_byte << 2) & 0x30) | (src_byte & 0x03);
495 write(fh, line_block, sizeof(line_block));
497 #else /* VERTICAL_PACKING */
498 for (by = LCD_FBHEIGHT - 1; by >= 0; by--)
500 unsigned char *src = &lcd_framebuffer[by][0];
501 unsigned char *dst = &line_block[3][0];
503 memset(line_block, 0, sizeof(line_block));
504 for (bx = LCD_WIDTH/2; bx > 0; bx--)
506 unsigned char *dst_blk = dst++;
507 unsigned src_byte0 = *src++;
508 unsigned src_byte1 = *src++;
509 int iy;
511 for (iy = 4; iy > 0; iy--)
513 *dst_blk = ((src_byte0 & 3) << 4) | (src_byte1 & 3);
514 src_byte0 >>= 2;
515 src_byte1 >>= 2;
516 dst_blk -= BMP_LINESIZE;
520 write(fh, line_block, sizeof(line_block));
522 #endif
523 #elif LCD_DEPTH == 16
524 for (by = LCD_HEIGHT - 1; by >= 0; by--)
526 unsigned short *src = &lcd_framebuffer[by][0];
527 unsigned short *dst = line_block;
529 memset(line_block, 0, sizeof(line_block));
530 for (bx = LCD_WIDTH; bx > 0; bx--)
532 #if (LCD_PIXELFORMAT == RGB565SWAPPED)
533 /* iPod LCD data is big endian although the CPU is not */
534 *dst++ = htobe16(*src++);
535 #else
536 *dst++ = htole16(*src++);
537 #endif
540 write(fh, line_block, sizeof(line_block));
542 #endif /* LCD_DEPTH */
545 close(fh);
548 void screen_dump_set_hook(void (*hook)(int fh))
550 screen_dump_hook = hook;
553 #endif /* HAVE_LCD_BITMAP */
555 /* parse a line from a configuration file. the line format is:
557 name: value
559 Any whitespace before setting name or value (after ':') is ignored.
560 A # as first non-whitespace character discards the whole line.
561 Function sets pointers to null-terminated setting name and value.
562 Returns false if no valid config entry was found.
565 bool settings_parseline(char* line, char** name, char** value)
567 char* ptr;
569 while ( isspace(*line) )
570 line++;
572 if ( *line == '#' )
573 return false;
575 ptr = strchr(line, ':');
576 if ( !ptr )
577 return false;
579 *name = line;
580 *ptr = 0;
581 ptr++;
582 while (isspace(*ptr))
583 ptr++;
584 *value = ptr;
585 return true;
588 static void system_flush(void)
590 tree_flush();
591 call_ata_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
594 static void system_restore(void)
596 tree_restore();
599 static bool clean_shutdown(void (*callback)(void *), void *parameter)
601 #ifdef SIMULATOR
602 (void)callback;
603 (void)parameter;
604 call_ata_idle_notifys(true);
605 exit(0);
606 #else
607 long msg_id = -1;
608 int i;
610 scrobbler_poweroff();
612 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
613 if(!charger_inserted())
614 #endif
616 bool batt_safe = battery_level_safe();
617 int audio_stat = audio_status();
619 FOR_NB_SCREENS(i)
620 screens[i].clear_display();
622 if (batt_safe)
624 #ifdef HAVE_TAGCACHE
625 if (!tagcache_prepare_shutdown())
627 cancel_shutdown();
628 gui_syncsplash(HZ, ID2P(LANG_TAGCACHE_BUSY));
629 return false;
631 #endif
632 if (battery_level() > 10)
633 gui_syncsplash(0, str(LANG_SHUTTINGDOWN));
634 else
636 msg_id = LANG_WARNING_BATTERY_LOW;
637 gui_syncsplash(0, "%s %s",
638 str(LANG_WARNING_BATTERY_LOW),
639 str(LANG_SHUTTINGDOWN));
642 else
644 msg_id = LANG_WARNING_BATTERY_EMPTY;
645 gui_syncsplash(0, "%s %s",
646 str(LANG_WARNING_BATTERY_EMPTY),
647 str(LANG_SHUTTINGDOWN));
650 if (global_settings.fade_on_stop
651 && (audio_stat & AUDIO_STATUS_PLAY))
653 fade(0);
656 if (batt_safe) /* do not save on critical battery */
658 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
659 if (audio_stat & AUDIO_STATUS_RECORD)
661 audio_stop_recording();
662 /* wait for stop to complete */
663 while (audio_status() & AUDIO_STATUS_RECORD)
664 sleep(1);
666 #endif
667 /* audio_stop_recording == audio_stop for HWCODEC */
668 audio_stop();
670 if (callback != NULL)
671 callback(parameter);
673 #if CONFIG_CODEC != SWCODEC
674 /* wait for audio_stop or audio_stop_recording to complete */
675 while (audio_status())
676 sleep(1);
677 #endif
679 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
680 audio_close_recording();
681 #endif
683 if(talk_menus_enabled())
685 bool enqueue = false;
686 if(msg_id != -1)
688 talk_id(msg_id, enqueue);
689 enqueue = true;
691 talk_id(LANG_SHUTTINGDOWN, enqueue);
692 #if CONFIG_CODEC == SWCODEC
693 voice_wait();
694 #endif
697 system_flush();
698 #ifdef HAVE_EEPROM_SETTINGS
699 if (firmware_settings.initialized)
701 firmware_settings.disk_clean = true;
702 firmware_settings.bl_version = 0;
703 eeprom_settings_store();
705 #endif
707 #ifdef HAVE_DIRCACHE
708 else
709 dircache_disable();
710 #endif
712 shutdown_hw();
714 #endif
715 return false;
718 bool list_stop_handler(void)
720 bool ret = false;
722 /* Stop the music if it is playing */
723 if(audio_status())
725 if (!global_settings.party_mode)
727 if (global_settings.fade_on_stop)
728 fade(0);
729 bookmark_autobookmark();
730 audio_stop();
731 ret = true; /* bookmarking can make a refresh necessary */
734 #if CONFIG_CHARGING
735 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
736 else
738 if (charger_inserted())
739 charging_splash();
740 else
741 shutdown_screen(); /* won't return if shutdown actually happens */
743 ret = true; /* screen is dirty, caller needs to refresh */
745 #endif
746 #ifndef HAVE_POWEROFF_WHILE_CHARGING
748 static long last_off = 0;
750 if (TIME_BEFORE(current_tick, last_off + HZ/2))
752 if (charger_inserted())
754 charging_splash();
755 ret = true; /* screen is dirty, caller needs to refresh */
758 last_off = current_tick;
760 #endif
761 #endif /* CONFIG_CHARGING */
762 return ret;
765 #if CONFIG_CHARGING
766 static bool waiting_to_resume_play = false;
767 static long play_resume_tick;
769 static void car_adapter_mode_processing(bool inserted)
771 if (global_settings.car_adapter_mode)
773 if(inserted)
776 * Just got plugged in, delay & resume if we were playing
778 if (audio_status() & AUDIO_STATUS_PAUSE)
780 /* delay resume a bit while the engine is cranking */
781 play_resume_tick = current_tick + HZ*5;
782 waiting_to_resume_play = true;
785 else
788 * Just got unplugged, pause if playing
790 if ((audio_status() & AUDIO_STATUS_PLAY) &&
791 !(audio_status() & AUDIO_STATUS_PAUSE))
793 if (global_settings.fade_on_stop)
794 fade(0);
795 else
796 audio_pause();
802 static void car_adapter_tick(void)
804 if (waiting_to_resume_play)
806 if (TIME_AFTER(current_tick, play_resume_tick))
808 if (audio_status() & AUDIO_STATUS_PAUSE)
810 audio_resume();
812 waiting_to_resume_play = false;
817 void car_adapter_mode_init(void)
819 tick_add_task(car_adapter_tick);
821 #endif
823 #ifdef HAVE_HEADPHONE_DETECTION
824 static void unplug_change(bool inserted)
826 if (global_settings.unplug_mode)
828 if (inserted)
830 if ( global_settings.unplug_mode > 1 )
831 audio_resume();
832 backlight_on();
833 } else {
834 audio_pause();
836 if (global_settings.unplug_rw)
838 if ( audio_current_track()->elapsed >
839 (unsigned long)(global_settings.unplug_rw*1000))
840 audio_ff_rewind(audio_current_track()->elapsed -
841 (global_settings.unplug_rw*1000));
842 else
843 audio_ff_rewind(0);
848 #endif
850 long default_event_handler_ex(long event, void (*callback)(void *), void *parameter)
852 switch(event)
854 case SYS_USB_CONNECTED:
855 if (callback != NULL)
856 callback(parameter);
857 #ifdef HAVE_MMC
858 if (!mmc_touched() ||
859 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
860 #endif
862 scrobbler_flush_cache();
863 system_flush();
864 #ifdef BOOTFILE
865 #if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
866 check_bootfile(false); /* gets initial size */
867 #endif
868 #endif
869 usb_screen();
870 #ifdef BOOTFILE
871 #if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
872 check_bootfile(true);
873 #endif
874 #endif
875 system_restore();
877 return SYS_USB_CONNECTED;
878 case SYS_POWEROFF:
879 if (!clean_shutdown(callback, parameter))
880 return SYS_POWEROFF;
881 break;
882 #if CONFIG_CHARGING
883 case SYS_CHARGER_CONNECTED:
884 car_adapter_mode_processing(true);
885 return SYS_CHARGER_CONNECTED;
887 case SYS_CHARGER_DISCONNECTED:
888 car_adapter_mode_processing(false);
889 return SYS_CHARGER_DISCONNECTED;
890 #endif
891 #ifdef HAVE_HEADPHONE_DETECTION
892 case SYS_PHONE_PLUGGED:
893 unplug_change(true);
894 return SYS_PHONE_PLUGGED;
896 case SYS_PHONE_UNPLUGGED:
897 unplug_change(false);
898 return SYS_PHONE_UNPLUGGED;
899 #endif
901 return 0;
904 long default_event_handler(long event)
906 return default_event_handler_ex(event, NULL, NULL);
909 int show_logo( void )
911 #ifdef HAVE_LCD_BITMAP
912 char version[32];
913 int font_h, font_w;
915 snprintf(version, sizeof(version), "Ver. %s", appsversion);
917 lcd_clear_display();
918 lcd_bitmap(rockboxlogo, 0, 10, BMPWIDTH_rockboxlogo, BMPHEIGHT_rockboxlogo);
919 lcd_setfont(FONT_SYSFIXED);
920 lcd_getstringsize((unsigned char *)"A", &font_w, &font_h);
921 lcd_putsxy((LCD_WIDTH/2) - ((strlen(version)*font_w)/2),
922 LCD_HEIGHT-font_h, (unsigned char *)version);
923 lcd_setfont(FONT_UI);
925 #else
926 char *rockbox = " ROCKbox!";
928 lcd_clear_display();
929 lcd_double_height(true);
930 lcd_puts(0, 0, rockbox);
931 lcd_puts_scroll(0, 1, appsversion);
932 #endif
933 lcd_update();
935 #ifdef HAVE_REMOTE_LCD
936 lcd_remote_clear_display();
937 lcd_remote_bitmap(remote_rockboxlogo, 0, 10, BMPWIDTH_remote_rockboxlogo,
938 BMPHEIGHT_remote_rockboxlogo);
939 lcd_remote_setfont(FONT_SYSFIXED);
940 lcd_remote_getstringsize((unsigned char *)"A", &font_w, &font_h);
941 lcd_remote_putsxy((LCD_REMOTE_WIDTH/2) - ((strlen(version)*font_w)/2),
942 LCD_REMOTE_HEIGHT-font_h, (unsigned char *)version);
943 lcd_remote_setfont(FONT_UI);
944 lcd_remote_update();
945 #endif
947 return 0;
950 #if CONFIG_CODEC == SWCODEC
951 int get_replaygain_mode(bool have_track_gain, bool have_album_gain)
953 int type;
955 bool track = ((global_settings.replaygain_type == REPLAYGAIN_TRACK)
956 || ((global_settings.replaygain_type == REPLAYGAIN_SHUFFLE)
957 && global_settings.playlist_shuffle));
959 type = (!track && have_album_gain) ? REPLAYGAIN_ALBUM
960 : have_track_gain ? REPLAYGAIN_TRACK : -1;
962 return type;
964 #endif
966 #ifdef BOOTFILE
967 #if !defined(USB_NONE) && !defined(USB_IPODSTYLE)
969 memorize/compare details about the BOOTFILE
970 we don't use dircache because it may not be up to date after
971 USB disconnect (scanning in the background)
973 void check_bootfile(bool do_rolo)
975 static unsigned short wrtdate = 0;
976 static unsigned short wrttime = 0;
977 DIR* dir = NULL;
978 struct dirent* entry = NULL;
980 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
981 dir = opendir(BOOTDIR);
983 if(!dir) return; /* do we want an error splash? */
985 /* loop all files in BOOTDIR */
986 while(0 != (entry = readdir(dir)))
988 if(!strcasecmp(entry->d_name, BOOTFILE))
990 /* found the bootfile */
991 if(wrtdate && do_rolo)
993 if((entry->wrtdate != wrtdate) ||
994 (entry->wrttime != wrttime))
996 char *lines[] = { ID2P(LANG_BOOT_CHANGED),
997 ID2P(LANG_REBOOT_NOW) };
998 struct text_message message={ lines, 2 };
999 button_clear_queue(); /* Empty the keyboard buffer */
1000 if(gui_syncyesno_run(&message, NULL, NULL) == YESNO_YES)
1001 rolo_load(BOOTDIR "/" BOOTFILE);
1004 wrtdate = entry->wrtdate;
1005 wrttime = entry->wrttime;
1008 closedir(dir);
1010 #endif
1011 #endif
1013 /* check range, set volume and save settings */
1014 void setvol(void)
1016 const int min_vol = sound_min(SOUND_VOLUME);
1017 const int max_vol = sound_max(SOUND_VOLUME);
1018 if (global_settings.volume < min_vol)
1019 global_settings.volume = min_vol;
1020 if (global_settings.volume > max_vol)
1021 global_settings.volume = max_vol;
1022 sound_set_volume(global_settings.volume);
1023 settings_save();
1026 #ifdef HAVE_LCD_COLOR
1028 * Helper function to convert a string of 6 hex digits to a native colour
1031 #define hex2dec(c) (((c) >= '0' && ((c) <= '9')) ? (toupper(c)) - '0' : \
1032 (toupper(c)) - 'A' + 10)
1034 int hex_to_rgb(const char* hex)
1035 { int ok = 1;
1036 int i;
1037 int red, green, blue;
1039 if (strlen(hex) == 6) {
1040 for (i=0; i < 6; i++ ) {
1041 if (!isxdigit(hex[i])) {
1042 ok=0;
1043 break;
1047 if (ok) {
1048 red = (hex2dec(hex[0]) << 4) | hex2dec(hex[1]);
1049 green = (hex2dec(hex[2]) << 4) | hex2dec(hex[3]);
1050 blue = (hex2dec(hex[4]) << 4) | hex2dec(hex[5]);
1051 return LCD_RGBPACK(red,green,blue);
1055 return 0;
1057 #endif /* HAVE_LCD_COLOR */