stfm1000: comment static unused function
[maemo-rb.git] / apps / misc.c
blobd7d4bdd2f9fff090fb42a8d6a78d3d6124ec7916
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
21 #include <stdlib.h>
22 #include <ctype.h>
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <errno.h>
26 #include "string-extra.h"
27 #include "config.h"
28 #include "misc.h"
29 #include "system.h"
30 #include "lcd.h"
31 #include "file.h"
32 #ifndef __PCTOOL__
33 #include "filefuncs.h"
34 #include "lang.h"
35 #include "dir.h"
36 #ifdef HAVE_REMOTE_LCD
37 #include "lcd-remote.h"
38 #endif
39 #include "action.h"
40 #include "timefuncs.h"
41 #include "screens.h"
42 #include "usb_screen.h"
43 #include "talk.h"
44 #include "audio.h"
45 #include "settings.h"
46 #include "storage.h"
47 #include "ata_idle_notify.h"
48 #include "kernel.h"
49 #include "power.h"
50 #include "powermgmt.h"
51 #include "backlight.h"
52 #include "version.h"
53 #include "font.h"
54 #include "splash.h"
55 #include "tagcache.h"
56 #include "scrobbler.h"
57 #include "sound.h"
58 #include "playlist.h"
59 #include "yesno.h"
60 #include "viewport.h"
61 #include "list.h"
63 #include "debug.h"
65 #if CONFIG_TUNER
66 #include "radio.h"
67 #endif
69 #ifdef IPOD_ACCESSORY_PROTOCOL
70 #include "iap.h"
71 #endif
73 #if (CONFIG_STORAGE & STORAGE_MMC)
74 #include "ata_mmc.h"
75 #endif
76 #include "tree.h"
77 #include "eeprom_settings.h"
78 #if defined(HAVE_RECORDING) && !defined(__PCTOOL__)
79 #include "recording.h"
80 #endif
81 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
82 #include "bmp.h"
83 #include "icons.h"
84 #endif /* End HAVE_LCD_BITMAP */
85 #include "bookmark.h"
86 #include "wps.h"
87 #include "playback.h"
88 #if CONFIG_CODEC == SWCODEC
89 #include "voice_thread.h"
90 #else
91 #include "mp3_playback.h"
92 #endif
94 #ifdef BOOTFILE
95 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF) \
96 || defined(HAVE_HOTSWAP_STORAGE_AS_MAIN)
97 #include "rolo.h"
98 #endif
99 #endif
101 #ifdef HAVE_HARDWARE_CLICK
102 #include "piezo.h"
103 #endif
105 /* units used with output_dyn_value */
106 const unsigned char * const byte_units[] =
108 ID2P(LANG_BYTE),
109 ID2P(LANG_KILOBYTE),
110 ID2P(LANG_MEGABYTE),
111 ID2P(LANG_GIGABYTE)
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
120 * voiced.*/
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;
125 int fraction = 0;
126 int unit_no = 0;
127 char tbuf[5];
129 while (value >= scale)
131 fraction = value % scale;
132 value /= scale;
133 unit_no++;
135 if (bin_scale)
136 fraction = fraction * 1000 / 1024;
138 if (value >= 100 || !unit_no)
139 tbuf[0] = '\0';
140 else if (value >= 10)
141 snprintf(tbuf, sizeof(tbuf), "%01d", fraction / 100);
142 else
143 snprintf(tbuf, sizeof(tbuf), "%02d", fraction / 10);
145 if (buf)
147 if (*tbuf)
148 snprintf(buf, buf_size, "%d%s%s%s", value, str(LANG_POINT),
149 tbuf, P2STR(units[unit_no]));
150 else
151 snprintf(buf, buf_size, "%d%s", value, P2STR(units[unit_no]));
153 else
155 talk_fractional(tbuf, value, P2ID(units[unit_no]));
157 return buf;
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);
174 else
175 return true;
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))
183 char *p, *next;
184 int rc, pos = 0;
185 int count = 0;
187 while ( 1 )
189 next = NULL;
191 rc = read(fd, &buf[pos], buf_size - pos - 1);
192 if (rc >= 0)
193 buf[pos+rc] = '\0';
195 if ( (p = strchr(buf, '\n')) != NULL)
197 *p = '\0';
198 next = ++p;
201 if ( (p = strchr(buf, '\r')) != NULL)
203 *p = '\0';
204 if (!next)
205 next = ++p;
208 rc = callback(count, buf, parameters);
209 if (rc < 0)
210 return rc;
212 count++;
213 if (next)
215 pos = buf_size - ((long)next - (long)buf) - 1;
216 memmove(buf, next, pos);
218 else
219 break ;
222 return 0;
225 /* parse a line from a configuration file. the line format is:
227 name: value
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)
237 char* ptr;
239 line = skip_whitespace(line);
241 if ( *line == '#' )
242 return false;
244 ptr = strchr(line, ':');
245 if ( !ptr )
246 return false;
248 *name = line;
249 *ptr = 0;
250 ptr++;
251 ptr = skip_whitespace(ptr);
252 *value = ptr;
253 return true;
256 static void system_flush(void)
258 playlist_shutdown();
259 tree_flush();
260 call_storage_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
263 static void system_restore(void)
265 tree_restore();
268 static bool clean_shutdown(void (*callback)(void *), void *parameter)
270 long msg_id = -1;
272 scrobbler_poweroff();
274 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
275 if(!charger_inserted())
276 #endif
278 bool batt_safe = battery_level_safe();
279 #if CONFIG_CODEC != SWCODEC || defined(HAVE_RECORDING)
280 int audio_stat = audio_status();
281 #endif
283 FOR_NB_SCREENS(i)
285 screens[i].clear_display();
286 screens[i].update();
289 if (batt_safe)
291 int level;
292 #ifdef HAVE_TAGCACHE
293 if (!tagcache_prepare_shutdown())
295 cancel_shutdown();
296 splash(HZ, ID2P(LANG_TAGCACHE_BUSY));
297 return false;
299 #endif
300 level = battery_level();
301 if (level > 10 || level < 0)
302 splash(0, str(LANG_SHUTTINGDOWN));
303 else
305 msg_id = LANG_WARNING_BATTERY_LOW;
306 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_LOW),
307 str(LANG_SHUTTINGDOWN));
310 else
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))
320 fade(false, false);
322 #endif
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)
332 sleep(1);
334 #endif
335 bookmark_autobookmark(false);
337 /* audio_stop_recording == audio_stop for HWCODEC */
338 audio_stop();
340 if (callback != NULL)
341 callback(parameter);
343 #if CONFIG_CODEC != SWCODEC
344 /* wait for audio_stop or audio_stop_recording to complete */
345 while (audio_status())
346 sleep(1);
347 #endif
349 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
350 audio_close_recording();
351 #endif
353 if(global_settings.talk_menu)
355 bool enqueue = false;
356 if(msg_id != -1)
358 talk_id(msg_id, enqueue);
359 enqueue = true;
361 talk_id(LANG_SHUTTINGDOWN, enqueue);
362 #if CONFIG_CODEC == SWCODEC
363 voice_wait();
364 #endif
367 system_flush();
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();
375 #endif
377 #ifdef HAVE_DIRCACHE
378 else
379 dircache_disable();
380 #endif
382 shutdown_hw();
384 return false;
387 bool list_stop_handler(void)
389 bool ret = false;
391 #if CONFIG_TUNER
392 radio_stop();
393 #endif
395 /* Stop the music if it is playing */
396 if(audio_status())
398 if (!global_settings.party_mode)
400 #if CONFIG_CODEC != SWCODEC
401 if (global_settings.fade_on_stop)
402 fade(false, false);
403 #endif
404 bookmark_autobookmark(true);
405 audio_stop();
406 ret = true; /* bookmarking can make a refresh necessary */
409 #if CONFIG_CHARGING
410 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
411 else
413 if (charger_inserted())
414 charging_splash();
415 else
416 shutdown_screen(); /* won't return if shutdown actually happens */
418 ret = true; /* screen is dirty, caller needs to refresh */
420 #endif
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())
429 charging_splash();
430 ret = true; /* screen is dirty, caller needs to refresh */
433 last_off = current_tick;
435 #endif
436 #endif /* CONFIG_CHARGING */
437 return ret;
440 #if 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)
448 if(inserted)
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;
460 else
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);
494 #endif
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();
504 if (inserted)
506 backlight_on();
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;
512 } else {
513 if ((audio_stat & AUDIO_STATUS_PLAY) &&
514 !(audio_stat & AUDIO_STATUS_PAUSE))
516 headphone_caused_pause = true;
517 pause_action(false, false);
522 #endif
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;
528 #endif
530 switch(event)
532 case SYS_BATTERY_UPDATE:
533 if(global_settings.talk_battery_level)
535 talk_ids(true, VOICE_PAUSE, VOICE_PAUSE,
536 LANG_BATTERY_TIME,
537 TALK_ID(battery_level(), UNIT_PERCENT),
538 VOICE_PAUSE);
539 talk_force_enqueue_next();
541 break;
542 case SYS_USB_CONNECTED:
543 if (callback != NULL)
544 callback(parameter);
545 #if (CONFIG_STORAGE & STORAGE_MMC) && (defined(ARCHOS_ONDIOSP) || defined(ARCHOS_ONDIOFM))
546 if (!mmc_touched() ||
547 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
548 #endif
550 system_flush();
551 #ifdef BOOTFILE
552 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
553 check_bootfile(false); /* gets initial size */
554 #endif
555 #endif
556 gui_usb_screen_run(false);
557 #ifdef BOOTFILE
558 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
559 check_bootfile(true);
560 #endif
561 #endif
562 system_restore();
564 return SYS_USB_CONNECTED;
566 case SYS_POWEROFF:
567 if (!clean_shutdown(callback, parameter))
568 return SYS_POWEROFF;
569 break;
570 #if CONFIG_CHARGING
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;
584 #endif
585 #ifdef HAVE_HOTSWAP_STORAGE_AS_MAIN
586 case SYS_FS_CHANGED:
588 /* simple sanity: assume rockbox is on the first hotswappable
589 * driver, abort out if that one isn't inserted */
590 int i;
591 for (i = 0; i < NUM_DRIVES; i++)
593 if (storage_removable(i) && !storage_present(i))
594 return SYS_FS_CHANGED;
596 system_flush();
597 check_bootfile(true); /* state gotten in main.c:init() */
598 system_restore();
600 return SYS_FS_CHANGED;
601 #endif
602 #ifdef HAVE_HEADPHONE_DETECTION
603 case SYS_PHONE_PLUGGED:
604 unplug_change(true);
605 return SYS_PHONE_PLUGGED;
607 case SYS_PHONE_UNPLUGGED:
608 unplug_change(false);
609 return SYS_PHONE_UNPLUGGED;
610 #endif
611 #ifdef IPOD_ACCESSORY_PROTOCOL
612 case SYS_IAP_PERIODIC:
613 iap_periodic();
614 return SYS_IAP_PERIODIC;
615 case SYS_IAP_HANDLEPKT:
616 iap_handlepkt();
617 return SYS_IAP_HANDLEPKT;
618 #endif
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;
623 list_stop_handler();
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);
632 resume = false;
633 return SYS_CALL_HUNG_UP;
634 #endif
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;
645 if (firstvolume) {
646 setvol();
647 firstvolume = false;
650 return 0;
652 #endif
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);
662 else
663 pause_action(true, true);
665 else
666 if (playlist_resume() != -1)
668 playlist_start(global_status.resume_index,
669 global_status.resume_offset);
671 return event;
673 case BUTTON_MULTIMEDIA_NEXT:
674 audio_next();
675 return event;
676 case BUTTON_MULTIMEDIA_PREV:
677 audio_prev();
678 return event;
679 case BUTTON_MULTIMEDIA_STOP:
680 list_stop_handler();
681 return event;
682 case BUTTON_MULTIMEDIA_REW:
683 case BUTTON_MULTIMEDIA_FFWD:
684 /* not supported yet, needs to be done in the WPS */
685 return 0;
686 #endif
688 return 0;
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
699 char version[32];
700 int font_h, font_w;
702 snprintf(version, sizeof(version), "Ver. %s", rbversion);
704 lcd_clear_display();
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);
712 #else
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);
718 #endif
719 lcd_setfont(FONT_UI);
721 #else
722 char *rockbox = " ROCKbox!";
724 lcd_clear_display();
725 lcd_double_height(true);
726 lcd_puts(0, 0, rockbox);
727 lcd_puts_scroll(0, 1, rbversion);
728 #endif
729 lcd_update();
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);
739 lcd_remote_update();
740 #endif
742 return 0;
745 #ifdef BOOTFILE
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;
756 DIR* dir = NULL;
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)
782 audio_hard_stop();
783 rolo_load(BOOTDIR "/" BOOTFILE);
787 wrtdate = info.wrtdate;
788 wrttime = info.wrttime;
791 closedir(dir);
793 #endif
794 #endif
796 /* check range, set volume and save settings */
797 void setvol(void)
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;
807 settings_save();
810 char* strrsplt(char* str, int c)
812 char* s = strrchr(str, c);
814 if (s != NULL)
816 *s++ = '\0';
818 else
820 s = str;
823 return s;
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, '.');
833 int len;
835 if (buffer_size <= 0)
837 return NULL;
840 buffer_size--; /* Make room for end nil */
842 if (dot != 0 && filename[0] != '.')
844 len = dot - filename;
845 len = MIN(len, buffer_size);
847 else
849 len = buffer_size;
852 strlcpy(buffer, filename, len + 1);
854 return buffer;
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
863 int *setting;
864 unsigned short frequency;
865 unsigned short duration;
866 unsigned short amplitude;
867 } beep_params[] =
869 [SOUND_KEYCLICK] =
870 { &global_settings.keyclick,
871 4000, KEYCLICK_DURATION, 2500 },
872 [SOUND_TRACK_SKIP] =
873 { &global_settings.beep,
874 2000, 100, 2500 },
875 [SOUND_TRACK_NO_MORE] =
876 { &global_settings.beep,
877 1000, 100, 1500 },
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)
900 int button = action;
901 static long last_button = BUTTON_NONE;
902 bool do_beep = false;
904 if (!rawbutton)
905 get_action_statuscode(&button);
907 /* Settings filters */
908 if (
909 #ifdef HAVE_HARDWARE_CLICK
910 (global_settings.keyclick || global_settings.keyclick_hardware)
911 #else
912 global_settings.keyclick
913 #endif
916 if (global_settings.keyclick_repeats || !(button & BUTTON_REPEAT))
918 /* Button filters */
919 if (button != BUTTON_NONE && !(button & BUTTON_REL)
920 && !(button & (SYS_EVENT|BUTTON_MULTIMEDIA)) )
922 do_beep = true;
925 else if ((button & BUTTON_REPEAT) && (last_button == BUTTON_NONE))
927 do_beep = true;
929 #ifdef HAVE_SCROLLWHEEL
930 else if (button & (BUTTON_SCROLL_BACK | BUTTON_SCROLL_FWD))
932 do_beep = true;
934 #endif
936 if (button&BUTTON_REPEAT)
937 last_button = button;
938 else
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;
945 if (do_beep)
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);
956 #endif
958 #else
959 system_sound_play(SOUND_KEYCLICK);
960 #endif
964 /* Return the ReplayGain mode adjusted by other relevant settings */
965 static int replaygain_setting_mode(int type)
967 switch (type)
969 case REPLAYGAIN_SHUFFLE:
970 type = global_settings.playlist_shuffle ?
971 REPLAYGAIN_TRACK : REPLAYGAIN_ALBUM;
972 case REPLAYGAIN_ALBUM:
973 case REPLAYGAIN_TRACK:
974 case REPLAYGAIN_OFF:
975 default:
976 break;
979 return type;
982 /* Return the ReplayGain mode adjusted for display purposes */
983 int id3_get_replaygain_mode(const struct mp3entry *id3)
985 if (!id3)
986 return -1;
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
1010 * stored in buffer.
1012 int read_line(int fd, char* buffer, int buffer_size)
1014 int count = 0;
1015 int num_read = 0;
1017 errno = 0;
1019 while (count < buffer_size)
1021 unsigned char c;
1023 if (1 != read(fd, &c, 1))
1024 break;
1026 num_read++;
1028 if ( c == '\n' )
1029 break;
1031 if ( c == '\r' )
1032 continue;
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)
1045 char *s = str;
1047 while (isspace(*s))
1048 s++;
1050 return s;
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];
1067 if ( hours == 0 )
1069 snprintf(buf, buf_size, "%s%d:%02d", sign, minutes, seconds);
1071 else
1073 snprintf(buf, buf_size, "%s%d:%02d:%02d", sign, hours, minutes,
1074 seconds);
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
1085 * in vector
1087 int split_string(char *str, const char split_char, char *vector[], const int vector_length)
1089 int i;
1090 char *p = str;
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++)
1098 vector[i] = p;
1099 if ((p = strchr(p, split_char)))
1101 *p++ = '\0';
1102 while(*p == split_char) p++; /* skip successive splitters */
1106 return i;
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)
1118 int fd;
1119 unsigned char bom[BOM_UTF_8_SIZE];
1121 fd = open(pathname, flags, 0666);
1122 if(fd < 0)
1123 return fd;
1125 if(flags & (O_TRUNC | O_WRONLY))
1127 write(fd, BOM_UTF_8, BOM_UTF_8_SIZE);
1129 else
1131 read(fd, bom, BOM_UTF_8_SIZE);
1132 /* check for BOM */
1133 if(memcmp(bom, BOM_UTF_8, BOM_UTF_8_SIZE))
1134 lseek(fd, 0, SEEK_SET);
1136 return fd;
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;
1154 int i = 0;
1156 while ((i < 6) && (isxdigit(hex[i])))
1157 i++;
1159 if (i < 6)
1160 return -1;
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);
1168 return 0;
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 */
1179 (void)screen;
1181 #ifdef HAVE_LCD_COLOR
1182 if (screens[screen].depth > 2)
1184 if (hex_to_rgb(text, value) < 0)
1185 return false;
1186 else
1187 return true;
1189 #endif
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))
1195 return false;
1196 *value = *text - '0';
1197 return true;
1199 #endif
1200 return false;
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)
1208 if (value > max)
1209 return min;
1210 if (value < min)
1211 return max;
1212 return value;
1214 #endif
1215 #endif
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__)
1224 FOR_NB_SCREENS(i)
1225 skinlist_set_cfg(i, NULL);
1226 #endif
1228 void pop_current_activity(void)
1230 current_activity_top--;
1231 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
1232 FOR_NB_SCREENS(i)
1233 skinlist_set_cfg(i, NULL);
1234 #endif
1236 enum current_activity get_current_activity(void)
1238 return current_activity[current_activity_top?current_activity_top-1:0];