1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Daniel Stenberg
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
26 #include "string-extra.h"
33 #include "filefuncs.h"
36 #ifdef HAVE_REMOTE_LCD
37 #include "lcd-remote.h"
40 #include "timefuncs.h"
42 #include "usb_screen.h"
47 #include "ata_idle_notify.h"
50 #include "powermgmt.h"
51 #include "backlight.h"
56 #include "scrobbler.h"
69 #ifdef IPOD_ACCESSORY_PROTOCOL
73 #if (CONFIG_STORAGE & STORAGE_MMC)
77 #include "eeprom_settings.h"
78 #if defined(HAVE_RECORDING) && !defined(__PCTOOL__)
79 #include "recording.h"
81 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
84 #endif /* End HAVE_LCD_BITMAP */
88 #if CONFIG_CODEC == SWCODEC
89 #include "voice_thread.h"
91 #include "mp3_playback.h"
95 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF) \
96 || defined(HAVE_HOTSWAP_STORAGE_AS_MAIN)
101 #ifdef HAVE_HARDWARE_CLICK
105 /* units used with output_dyn_value */
106 const unsigned char * const byte_units
[] =
114 const unsigned char * const * const kbyte_units
= &byte_units
[1];
116 /* Format a large-range value for output, using the appropriate unit so that
117 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
118 * units) if possible, and 3 significant digits are shown. If a buffer is
119 * given, the result is snprintf()'d into that buffer, otherwise the result is
121 char *output_dyn_value(char *buf
, int buf_size
, int value
,
122 const unsigned char * const *units
, bool bin_scale
)
124 int scale
= bin_scale
? 1024 : 1000;
129 while (value
>= scale
)
131 fraction
= value
% scale
;
136 fraction
= fraction
* 1000 / 1024;
138 if (value
>= 100 || !unit_no
)
140 else if (value
>= 10)
141 snprintf(tbuf
, sizeof(tbuf
), "%01d", fraction
/ 100);
143 snprintf(tbuf
, sizeof(tbuf
), "%02d", fraction
/ 10);
148 snprintf(buf
, buf_size
, "%d%s%s%s", value
, str(LANG_POINT
),
149 tbuf
, P2STR(units
[unit_no
]));
151 snprintf(buf
, buf_size
, "%d%s", value
, P2STR(units
[unit_no
]));
155 talk_fractional(tbuf
, value
, P2ID(units
[unit_no
]));
160 /* Ask the user if they really want to erase the current dynamic playlist
161 * returns true if the playlist should be replaced */
162 bool warn_on_pl_erase(void)
164 if (global_settings
.warnon_erase_dynplaylist
&&
165 !global_settings
.party_mode
&&
166 playlist_modified(NULL
))
168 static const char *lines
[] =
169 {ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT
)};
170 static const struct text_message message
={lines
, 1};
172 return (gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
);
179 /* Performance optimized version of the read_line() (see below) function. */
180 int fast_readline(int fd
, char *buf
, int buf_size
, void *parameters
,
181 int (*callback
)(int n
, char *buf
, void *parameters
))
191 rc
= read(fd
, &buf
[pos
], buf_size
- pos
- 1);
195 if ( (p
= strchr(buf
, '\n')) != NULL
)
201 if ( (p
= strchr(buf
, '\r')) != NULL
)
208 rc
= callback(count
, buf
, parameters
);
215 pos
= buf_size
- ((long)next
- (long)buf
) - 1;
216 memmove(buf
, next
, pos
);
225 /* parse a line from a configuration file. the line format is:
229 Any whitespace before setting name or value (after ':') is ignored.
230 A # as first non-whitespace character discards the whole line.
231 Function sets pointers to null-terminated setting name and value.
232 Returns false if no valid config entry was found.
235 bool settings_parseline(char* line
, char** name
, char** value
)
239 line
= skip_whitespace(line
);
244 ptr
= strchr(line
, ':');
251 ptr
= skip_whitespace(ptr
);
256 static void system_flush(void)
260 call_storage_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
263 static void system_restore(void)
268 static bool clean_shutdown(void (*callback
)(void *), void *parameter
)
272 scrobbler_poweroff();
274 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
275 if(!charger_inserted())
278 bool batt_safe
= battery_level_safe();
279 #if CONFIG_CODEC != SWCODEC || defined(HAVE_RECORDING)
280 int audio_stat
= audio_status();
285 screens
[i
].clear_display();
293 if (!tagcache_prepare_shutdown())
296 splash(HZ
, ID2P(LANG_TAGCACHE_BUSY
));
300 level
= battery_level();
301 if (level
> 10 || level
< 0)
302 splash(0, str(LANG_SHUTTINGDOWN
));
305 msg_id
= LANG_WARNING_BATTERY_LOW
;
306 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_LOW
),
307 str(LANG_SHUTTINGDOWN
));
312 msg_id
= LANG_WARNING_BATTERY_EMPTY
;
313 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_EMPTY
),
314 str(LANG_SHUTTINGDOWN
));
316 #if CONFIG_CODEC != SWCODEC
317 if (global_settings
.fade_on_stop
318 && (audio_stat
& AUDIO_STATUS_PLAY
))
324 if (batt_safe
) /* do not save on critical battery */
326 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
327 if (audio_stat
& AUDIO_STATUS_RECORD
)
329 rec_command(RECORDING_CMD_STOP
);
330 /* wait for stop to complete */
331 while (audio_status() & AUDIO_STATUS_RECORD
)
335 bookmark_autobookmark(false);
337 /* audio_stop_recording == audio_stop for HWCODEC */
340 if (callback
!= NULL
)
343 #if CONFIG_CODEC != SWCODEC
344 /* wait for audio_stop or audio_stop_recording to complete */
345 while (audio_status())
349 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
350 audio_close_recording();
353 if(global_settings
.talk_menu
)
355 bool enqueue
= false;
358 talk_id(msg_id
, enqueue
);
361 talk_id(LANG_SHUTTINGDOWN
, enqueue
);
362 #if CONFIG_CODEC == SWCODEC
368 #ifdef HAVE_EEPROM_SETTINGS
369 if (firmware_settings
.initialized
)
371 firmware_settings
.disk_clean
= true;
372 firmware_settings
.bl_version
= 0;
373 eeprom_settings_store();
387 bool list_stop_handler(void)
395 /* Stop the music if it is playing */
398 if (!global_settings
.party_mode
)
400 #if CONFIG_CODEC != SWCODEC
401 if (global_settings
.fade_on_stop
)
404 bookmark_autobookmark(true);
406 ret
= true; /* bookmarking can make a refresh necessary */
410 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
413 if (charger_inserted())
416 shutdown_screen(); /* won't return if shutdown actually happens */
418 ret
= true; /* screen is dirty, caller needs to refresh */
421 #ifndef HAVE_POWEROFF_WHILE_CHARGING
423 static long last_off
= 0;
425 if (TIME_BEFORE(current_tick
, last_off
+ HZ
/2))
427 if (charger_inserted())
430 ret
= true; /* screen is dirty, caller needs to refresh */
433 last_off
= current_tick
;
436 #endif /* CONFIG_CHARGING */
441 static bool waiting_to_resume_play
= false;
442 static long play_resume_tick
;
444 static void car_adapter_mode_processing(bool inserted
)
446 if (global_settings
.car_adapter_mode
)
451 * Just got plugged in, delay & resume if we were playing
453 if (audio_status() & AUDIO_STATUS_PAUSE
)
455 /* delay resume a bit while the engine is cranking */
456 play_resume_tick
= current_tick
+ HZ
*5;
457 waiting_to_resume_play
= true;
463 * Just got unplugged, pause if playing
465 if ((audio_status() & AUDIO_STATUS_PLAY
) &&
466 !(audio_status() & AUDIO_STATUS_PAUSE
))
468 pause_action(true, true);
470 waiting_to_resume_play
= false;
475 static void car_adapter_tick(void)
477 if (waiting_to_resume_play
)
479 if (TIME_AFTER(current_tick
, play_resume_tick
))
481 if (audio_status() & AUDIO_STATUS_PAUSE
)
483 queue_broadcast(SYS_CAR_ADAPTER_RESUME
, 0);
485 waiting_to_resume_play
= false;
490 void car_adapter_mode_init(void)
492 tick_add_task(car_adapter_tick
);
496 #ifdef HAVE_HEADPHONE_DETECTION
497 static void unplug_change(bool inserted
)
499 static bool headphone_caused_pause
= false;
501 if (global_settings
.unplug_mode
)
503 int audio_stat
= audio_status();
507 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
508 headphone_caused_pause
&&
509 global_settings
.unplug_mode
> 1 )
510 unpause_action(true, true);
511 headphone_caused_pause
= false;
513 if ((audio_stat
& AUDIO_STATUS_PLAY
) &&
514 !(audio_stat
& AUDIO_STATUS_PAUSE
))
516 headphone_caused_pause
= true;
517 pause_action(false, false);
524 long default_event_handler_ex(long event
, void (*callback
)(void *), void *parameter
)
526 #if CONFIG_PLATFORM & (PLATFORM_ANDROID|PLATFORM_MAEMO)
527 static bool resume
= false;
532 case SYS_BATTERY_UPDATE
:
533 if(global_settings
.talk_battery_level
)
535 talk_ids(true, VOICE_PAUSE
, VOICE_PAUSE
,
537 TALK_ID(battery_level(), UNIT_PERCENT
),
539 talk_force_enqueue_next();
542 case SYS_USB_CONNECTED
:
543 if (callback
!= NULL
)
545 #if (CONFIG_STORAGE & STORAGE_MMC) && (defined(ARCHOS_ONDIOSP) || defined(ARCHOS_ONDIOFM))
546 if (!mmc_touched() ||
547 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED
))
552 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
553 check_bootfile(false); /* gets initial size */
556 gui_usb_screen_run(false);
558 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
559 check_bootfile(true);
564 return SYS_USB_CONNECTED
;
567 if (!clean_shutdown(callback
, parameter
))
571 case SYS_CHARGER_CONNECTED
:
572 car_adapter_mode_processing(true);
573 return SYS_CHARGER_CONNECTED
;
575 case SYS_CHARGER_DISCONNECTED
:
576 car_adapter_mode_processing(false);
577 /*reset rockbox battery runtime*/
578 global_status
.runtime
= 0;
579 return SYS_CHARGER_DISCONNECTED
;
581 case SYS_CAR_ADAPTER_RESUME
:
582 unpause_action(true, true);
583 return SYS_CAR_ADAPTER_RESUME
;
585 #ifdef HAVE_HOTSWAP_STORAGE_AS_MAIN
588 /* simple sanity: assume rockbox is on the first hotswappable
589 * driver, abort out if that one isn't inserted */
591 for (i
= 0; i
< NUM_DRIVES
; i
++)
593 if (storage_removable(i
) && !storage_present(i
))
594 return SYS_FS_CHANGED
;
597 check_bootfile(true); /* state gotten in main.c:init() */
600 return SYS_FS_CHANGED
;
602 #ifdef HAVE_HEADPHONE_DETECTION
603 case SYS_PHONE_PLUGGED
:
605 return SYS_PHONE_PLUGGED
;
607 case SYS_PHONE_UNPLUGGED
:
608 unplug_change(false);
609 return SYS_PHONE_UNPLUGGED
;
611 #ifdef IPOD_ACCESSORY_PROTOCOL
612 case SYS_IAP_PERIODIC
:
614 return SYS_IAP_PERIODIC
;
615 case SYS_IAP_HANDLEPKT
:
617 return SYS_IAP_HANDLEPKT
;
619 #if CONFIG_PLATFORM & (PLATFORM_ANDROID|PLATFORM_MAEMO)
620 /* stop playback if we receive a call */
621 case SYS_CALL_INCOMING
:
622 resume
= audio_status() == AUDIO_STATUS_PLAY
;
624 return SYS_CALL_INCOMING
;
625 /* resume playback if needed */
626 case SYS_CALL_HUNG_UP
:
627 if (resume
&& playlist_resume() != -1)
629 playlist_start(global_status
.resume_index
,
630 global_status
.resume_offset
);
633 return SYS_CALL_HUNG_UP
;
635 #if (CONFIG_PLATFORM & PLATFORM_HOSTED) && defined(PLATFORM_HAS_VOLUME_CHANGE)
636 case SYS_VOLUME_CHANGED
:
638 static bool firstvolume
= true;
639 /* kludge: since this events go to the button_queue,
640 * event data is available in the last button data */
641 int volume
= button_get_data();
642 DEBUGF("SYS_VOLUME_CHANGED: %d\n", volume
);
643 if (global_settings
.volume
!= volume
) {
644 global_settings
.volume
= volume
;
653 #ifdef HAVE_MULTIMEDIA_KEYS
654 /* multimedia keys on keyboards, headsets */
655 case BUTTON_MULTIMEDIA_PLAYPAUSE
:
657 int status
= audio_status();
658 if (status
& AUDIO_STATUS_PLAY
)
660 if (status
& AUDIO_STATUS_PAUSE
)
661 unpause_action(true, true);
663 pause_action(true, true);
666 if (playlist_resume() != -1)
668 playlist_start(global_status
.resume_index
,
669 global_status
.resume_offset
);
673 case BUTTON_MULTIMEDIA_NEXT
:
676 case BUTTON_MULTIMEDIA_PREV
:
679 case BUTTON_MULTIMEDIA_STOP
:
682 case BUTTON_MULTIMEDIA_REW
:
683 case BUTTON_MULTIMEDIA_FFWD
:
684 /* not supported yet, needs to be done in the WPS */
691 long default_event_handler(long event
)
693 return default_event_handler_ex(event
, NULL
, NULL
);
696 int show_logo( void )
698 #ifdef HAVE_LCD_BITMAP
702 snprintf(version
, sizeof(version
), "Ver. %s", rbversion
);
705 #if defined(SANSA_CLIP) || defined(SANSA_CLIPV2) || defined(SANSA_CLIPPLUS)
706 /* display the logo in the blue area of the screen */
707 lcd_setfont(FONT_SYSFIXED
);
708 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
709 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
710 0, (unsigned char *)version
);
711 lcd_bmp(&bm_rockboxlogo
, (LCD_WIDTH
- BMPWIDTH_rockboxlogo
) / 2, 16);
713 lcd_bmp(&bm_rockboxlogo
, (LCD_WIDTH
- BMPWIDTH_rockboxlogo
) / 2, 10);
714 lcd_setfont(FONT_SYSFIXED
);
715 lcd_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
716 lcd_putsxy((LCD_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
717 LCD_HEIGHT
-font_h
, (unsigned char *)version
);
719 lcd_setfont(FONT_UI
);
722 char *rockbox
= " ROCKbox!";
725 lcd_double_height(true);
726 lcd_puts(0, 0, rockbox
);
727 lcd_puts_scroll(0, 1, rbversion
);
731 #ifdef HAVE_REMOTE_LCD
732 lcd_remote_clear_display();
733 lcd_remote_bmp(&bm_remote_rockboxlogo
, 0, 10);
734 lcd_remote_setfont(FONT_SYSFIXED
);
735 lcd_remote_getstringsize((unsigned char *)"A", &font_w
, &font_h
);
736 lcd_remote_putsxy((LCD_REMOTE_WIDTH
/2) - ((strlen(version
)*font_w
)/2),
737 LCD_REMOTE_HEIGHT
-font_h
, (unsigned char *)version
);
738 lcd_remote_setfont(FONT_UI
);
746 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF) || defined(HAVE_HOTSWAP_STORAGE_AS_MAIN)
748 memorize/compare details about the BOOTFILE
749 we don't use dircache because it may not be up to date after
750 USB disconnect (scanning in the background)
752 void check_bootfile(bool do_rolo
)
754 static unsigned short wrtdate
= 0;
755 static unsigned short wrttime
= 0;
757 struct dirent
* entry
= NULL
;
759 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
760 dir
= opendir(BOOTDIR
);
762 if(!dir
) return; /* do we want an error splash? */
764 /* loop all files in BOOTDIR */
765 while(0 != (entry
= readdir(dir
)))
767 if(!strcasecmp(entry
->d_name
, BOOTFILE
))
769 struct dirinfo info
= dir_get_info(dir
, entry
);
770 /* found the bootfile */
771 if(wrtdate
&& do_rolo
)
773 if((info
.wrtdate
!= wrtdate
) ||
774 (info
.wrttime
!= wrttime
))
776 static const char *lines
[] = { ID2P(LANG_BOOT_CHANGED
),
777 ID2P(LANG_REBOOT_NOW
) };
778 static const struct text_message message
={ lines
, 2 };
779 button_clear_queue(); /* Empty the keyboard buffer */
780 if(gui_syncyesno_run(&message
, NULL
, NULL
) == YESNO_YES
)
783 rolo_load(BOOTDIR
"/" BOOTFILE
);
787 wrtdate
= info
.wrtdate
;
788 wrttime
= info
.wrttime
;
796 /* check range, set volume and save settings */
799 const int min_vol
= sound_min(SOUND_VOLUME
);
800 const int max_vol
= sound_max(SOUND_VOLUME
);
801 if (global_settings
.volume
< min_vol
)
802 global_settings
.volume
= min_vol
;
803 if (global_settings
.volume
> max_vol
)
804 global_settings
.volume
= max_vol
;
805 sound_set_volume(global_settings
.volume
);
806 global_status
.last_volume_change
= current_tick
;
810 char* strrsplt(char* str
, int c
)
812 char* s
= strrchr(str
, c
);
827 * removes the extension of filename (if it doesn't start with a .)
828 * puts the result in buffer
830 char *strip_extension(char* buffer
, int buffer_size
, const char *filename
)
832 char *dot
= strrchr(filename
, '.');
835 if (buffer_size
<= 0)
840 buffer_size
--; /* Make room for end nil */
842 if (dot
!= 0 && filename
[0] != '.')
844 len
= dot
- filename
;
845 len
= MIN(len
, buffer_size
);
852 strlcpy(buffer
, filename
, len
+ 1);
857 #if CONFIG_CODEC == SWCODEC
858 /* Play a standard sound */
859 void system_sound_play(enum system_sound sound
)
861 static const struct beep_params
864 unsigned short frequency
;
865 unsigned short duration
;
866 unsigned short amplitude
;
870 { &global_settings
.keyclick
,
871 4000, KEYCLICK_DURATION
, 2500 },
873 { &global_settings
.beep
,
875 [SOUND_TRACK_NO_MORE
] =
876 { &global_settings
.beep
,
880 const struct beep_params
*params
= &beep_params
[sound
];
882 if (*params
->setting
)
884 beep_play(params
->frequency
, params
->duration
,
885 params
->amplitude
* *params
->setting
);
889 static keyclick_callback keyclick_current_callback
= NULL
;
890 static void* keyclick_data
= NULL
;
891 void keyclick_set_callback(keyclick_callback cb
, void* data
)
893 keyclick_current_callback
= cb
;
894 keyclick_data
= data
;
897 /* Produce keyclick based upon button and global settings */
898 void keyclick_click(bool rawbutton
, int action
)
901 static long last_button
= BUTTON_NONE
;
902 bool do_beep
= false;
905 get_action_statuscode(&button
);
907 /* Settings filters */
909 #ifdef HAVE_HARDWARE_CLICK
910 (global_settings
.keyclick
|| global_settings
.keyclick_hardware
)
912 global_settings
.keyclick
916 if (global_settings
.keyclick_repeats
|| !(button
& BUTTON_REPEAT
))
919 if (button
!= BUTTON_NONE
&& !(button
& BUTTON_REL
)
920 && !(button
& (SYS_EVENT
|BUTTON_MULTIMEDIA
)) )
925 else if ((button
& BUTTON_REPEAT
) && (last_button
== BUTTON_NONE
))
929 #ifdef HAVE_SCROLLWHEEL
930 else if (button
& (BUTTON_SCROLL_BACK
| BUTTON_SCROLL_FWD
))
936 if (button
&BUTTON_REPEAT
)
937 last_button
= button
;
939 last_button
= BUTTON_NONE
;
941 if (do_beep
&& keyclick_current_callback
)
942 do_beep
= keyclick_current_callback(action
, keyclick_data
);
943 keyclick_current_callback
= NULL
;
947 #ifdef HAVE_HARDWARE_CLICK
948 if (global_settings
.keyclick
)
950 system_sound_play(SOUND_KEYCLICK
);
952 if (global_settings
.keyclick_hardware
)
954 #if !defined(SIMULATOR)
955 piezo_button_beep(false, false);
959 system_sound_play(SOUND_KEYCLICK
);
964 /* Return the ReplayGain mode adjusted by other relevant settings */
965 static int replaygain_setting_mode(int type
)
969 case REPLAYGAIN_SHUFFLE
:
970 type
= global_settings
.playlist_shuffle
?
971 REPLAYGAIN_TRACK
: REPLAYGAIN_ALBUM
;
972 case REPLAYGAIN_ALBUM
:
973 case REPLAYGAIN_TRACK
:
982 /* Return the ReplayGain mode adjusted for display purposes */
983 int id3_get_replaygain_mode(const struct mp3entry
*id3
)
988 int type
= global_settings
.replaygain_settings
.type
;
989 type
= replaygain_setting_mode(type
);
991 return (type
!= REPLAYGAIN_TRACK
&& id3
->album_gain
!= 0) ?
992 REPLAYGAIN_ALBUM
: (id3
->track_gain
!= 0 ? REPLAYGAIN_TRACK
: -1);
995 /* Update DSP's replaygain from global settings */
996 void replaygain_update(void)
998 struct replaygain_settings settings
= global_settings
.replaygain_settings
;
999 settings
.type
= replaygain_setting_mode(settings
.type
);
1000 dsp_replaygain_set_settings(&settings
);
1002 #endif /* CONFIG_CODEC == SWCODEC */
1004 #endif /* !defined(__PCTOOL__) */
1006 /* Read (up to) a line of text from fd into buffer and return number of bytes
1007 * read (which may be larger than the number of bytes stored in buffer). If
1008 * an error occurs, -1 is returned (and buffer contains whatever could be
1009 * read). A line is terminated by a LF char. Neither LF nor CR chars are
1012 int read_line(int fd
, char* buffer
, int buffer_size
)
1019 while (count
< buffer_size
)
1023 if (1 != read(fd
, &c
, 1))
1034 buffer
[count
++] = c
;
1037 buffer
[MIN(count
, buffer_size
- 1)] = 0;
1039 return errno
? -1 : num_read
;
1043 char* skip_whitespace(char* const str
)
1053 /* Format time into buf.
1055 * buf - buffer to format to.
1056 * buf_size - size of buffer.
1057 * t - time to format, in milliseconds.
1059 void format_time(char* buf
, int buf_size
, long t
)
1061 int const time
= abs(t
/ 1000);
1062 int const hours
= time
/ 3600;
1063 int const minutes
= time
/ 60 - hours
* 60;
1064 int const seconds
= time
% 60;
1065 const char * const sign
= &"-"[t
< 0 ? 0 : 1];
1069 snprintf(buf
, buf_size
, "%s%d:%02d", sign
, minutes
, seconds
);
1073 snprintf(buf
, buf_size
, "%s%d:%02d:%02d", sign
, hours
, minutes
,
1079 * Splits str at each occurence of split_char and puts the substrings into vector,
1080 * but at most vector_lenght items. Empty substrings are ignored.
1082 * Modifies str by replacing each split_char following a substring with nul
1084 * Returns the number of substrings found, i.e. the number of valid strings
1087 int split_string(char *str
, const char split_char
, char *vector
[], const int vector_length
)
1092 /* skip leading splitters */
1093 while(*p
== split_char
) p
++;
1095 /* *p in the condition takes care of trailing splitters */
1096 for(i
= 0; p
&& *p
&& i
< vector_length
; i
++)
1099 if ((p
= strchr(p
, split_char
)))
1102 while(*p
== split_char
) p
++; /* skip successive splitters */
1110 /** Open a UTF-8 file and set file descriptor to first byte after BOM.
1111 * If no BOM is present this behaves like open().
1112 * If the file is opened for writing and O_TRUNC is set, write a BOM to
1113 * the opened file and leave the file pointer set after the BOM.
1116 int open_utf8(const char* pathname
, int flags
)
1119 unsigned char bom
[BOM_UTF_8_SIZE
];
1121 fd
= open(pathname
, flags
, 0666);
1125 if(flags
& (O_TRUNC
| O_WRONLY
))
1127 write(fd
, BOM_UTF_8
, BOM_UTF_8_SIZE
);
1131 read(fd
, bom
, BOM_UTF_8_SIZE
);
1133 if(memcmp(bom
, BOM_UTF_8
, BOM_UTF_8_SIZE
))
1134 lseek(fd
, 0, SEEK_SET
);
1140 #ifdef HAVE_LCD_COLOR
1142 * Helper function to convert a string of 6 hex digits to a native colour
1145 static int hex2dec(int c
)
1147 return (((c
) >= '0' && ((c
) <= '9')) ? (c
) - '0' :
1148 (toupper(c
)) - 'A' + 10);
1151 int hex_to_rgb(const char* hex
, int* color
)
1153 int red
, green
, blue
;
1156 while ((i
< 6) && (isxdigit(hex
[i
])))
1162 red
= (hex2dec(hex
[0]) << 4) | hex2dec(hex
[1]);
1163 green
= (hex2dec(hex
[2]) << 4) | hex2dec(hex
[3]);
1164 blue
= (hex2dec(hex
[4]) << 4) | hex2dec(hex
[5]);
1166 *color
= LCD_RGBPACK(red
,green
,blue
);
1170 #endif /* HAVE_LCD_COLOR */
1172 #ifdef HAVE_LCD_BITMAP
1173 /* '0'-'3' are ASCII 0x30 to 0x33 */
1174 #define is0123(x) (((x) & 0xfc) == 0x30)
1175 #if !defined(__PCTOOL__) || defined(CHECKWPS)
1176 bool parse_color(enum screen_type screen
, char *text
, int *value
)
1178 (void)text
; (void)value
; /* silence warnings on mono bitmap */
1181 #ifdef HAVE_LCD_COLOR
1182 if (screens
[screen
].depth
> 2)
1184 if (hex_to_rgb(text
, value
) < 0)
1191 #if LCD_DEPTH == 2 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 2)
1192 if (screens
[screen
].depth
== 2)
1194 if (text
[1] != '\0' || !is0123(*text
))
1196 *value
= *text
- '0';
1202 #endif /* !defined(__PCTOOL__) || defined(CHECKWPS) */
1204 /* only used in USB HID and set_time screen */
1205 #if defined(USB_ENABLE_HID) || (CONFIG_RTC != 0)
1206 int clamp_value_wrap(int value
, int max
, int min
)
1216 #define MAX_ACTIVITY_DEPTH 12
1217 static enum current_activity
1218 current_activity
[MAX_ACTIVITY_DEPTH
] = {ACTIVITY_UNKNOWN
};
1219 static int current_activity_top
= 0;
1220 void push_current_activity(enum current_activity screen
)
1222 current_activity
[current_activity_top
++] = screen
;
1223 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
1225 skinlist_set_cfg(i
, NULL
);
1228 void pop_current_activity(void)
1230 current_activity_top
--;
1231 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
1233 skinlist_set_cfg(i
, NULL
);
1236 enum current_activity
get_current_activity(void)
1238 return current_activity
[current_activity_top
?current_activity_top
-1:0];