i.MX31: Implement asynchronous version of I2C driver.
[maemo-rb.git] / apps / misc.c
blobb1def596abf0f164c5df47ccaa6d1da766e6ef91
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 #include "filefuncs.h"
33 #ifndef __PCTOOL__
34 #include "lang.h"
35 #include "dir.h"
36 #include "lcd-remote.h"
37 #include "timefuncs.h"
38 #include "screens.h"
39 #include "usb_screen.h"
40 #include "talk.h"
41 #include "audio.h"
42 #include "mp3_playback.h"
43 #include "settings.h"
44 #include "storage.h"
45 #include "ata_idle_notify.h"
46 #include "kernel.h"
47 #include "power.h"
48 #include "powermgmt.h"
49 #include "backlight.h"
50 #include "version.h"
51 #include "font.h"
52 #include "splash.h"
53 #include "tagcache.h"
54 #include "scrobbler.h"
55 #include "sound.h"
56 #include "playlist.h"
57 #include "yesno.h"
58 #include "viewport.h"
59 #include "list.h"
61 #include "debug.h"
63 #if CONFIG_TUNER
64 #include "radio.h"
65 #endif
67 #ifdef IPOD_ACCESSORY_PROTOCOL
68 #include "iap.h"
69 #endif
71 #if (CONFIG_STORAGE & STORAGE_MMC)
72 #include "ata_mmc.h"
73 #endif
74 #include "tree.h"
75 #include "eeprom_settings.h"
76 #if defined(HAVE_RECORDING) && !defined(__PCTOOL__)
77 #include "recording.h"
78 #endif
79 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
80 #include "bmp.h"
81 #include "icons.h"
82 #endif /* End HAVE_LCD_BITMAP */
83 #include "bookmark.h"
84 #include "wps.h"
85 #include "playback.h"
86 #if CONFIG_CODEC == SWCODEC
87 #include "voice_thread.h"
88 #endif
90 #ifdef BOOTFILE
91 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF) \
92 || defined(HAVE_HOTSWAP_STORAGE_AS_MAIN)
93 #include "rolo.h"
94 #endif
95 #endif
97 #ifdef HAVE_HARDWARE_CLICK
98 #include "piezo.h"
99 #endif
101 /* units used with output_dyn_value */
102 const unsigned char * const byte_units[] =
104 ID2P(LANG_BYTE),
105 ID2P(LANG_KILOBYTE),
106 ID2P(LANG_MEGABYTE),
107 ID2P(LANG_GIGABYTE)
110 const unsigned char * const * const kbyte_units = &byte_units[1];
112 /* Format a large-range value for output, using the appropriate unit so that
113 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
114 * units) if possible, and 3 significant digits are shown. If a buffer is
115 * given, the result is snprintf()'d into that buffer, otherwise the result is
116 * voiced.*/
117 char *output_dyn_value(char *buf, int buf_size, int value,
118 const unsigned char * const *units, bool bin_scale)
120 int scale = bin_scale ? 1024 : 1000;
121 int fraction = 0;
122 int unit_no = 0;
123 char tbuf[5];
125 while (value >= scale)
127 fraction = value % scale;
128 value /= scale;
129 unit_no++;
131 if (bin_scale)
132 fraction = fraction * 1000 / 1024;
134 if (value >= 100 || !unit_no)
135 tbuf[0] = '\0';
136 else if (value >= 10)
137 snprintf(tbuf, sizeof(tbuf), "%01d", fraction / 100);
138 else
139 snprintf(tbuf, sizeof(tbuf), "%02d", fraction / 10);
141 if (buf)
143 if (*tbuf)
144 snprintf(buf, buf_size, "%d%s%s%s", value, str(LANG_POINT),
145 tbuf, P2STR(units[unit_no]));
146 else
147 snprintf(buf, buf_size, "%d%s", value, P2STR(units[unit_no]));
149 else
151 talk_fractional(tbuf, value, P2ID(units[unit_no]));
153 return buf;
156 /* Ask the user if they really want to erase the current dynamic playlist
157 * returns true if the playlist should be replaced */
158 bool warn_on_pl_erase(void)
160 if (global_settings.warnon_erase_dynplaylist &&
161 !global_settings.party_mode &&
162 playlist_modified(NULL))
164 static const char *lines[] =
165 {ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
166 static const struct text_message message={lines, 1};
168 return (gui_syncyesno_run(&message, NULL, NULL) == YESNO_YES);
170 else
171 return true;
175 /* Performance optimized version of the read_line() (see below) function. */
176 int fast_readline(int fd, char *buf, int buf_size, void *parameters,
177 int (*callback)(int n, char *buf, void *parameters))
179 char *p, *next;
180 int rc, pos = 0;
181 int count = 0;
183 while ( 1 )
185 next = NULL;
187 rc = read(fd, &buf[pos], buf_size - pos - 1);
188 if (rc >= 0)
189 buf[pos+rc] = '\0';
191 if ( (p = strchr(buf, '\n')) != NULL)
193 *p = '\0';
194 next = ++p;
197 rc = callback(count, buf, parameters);
198 if (rc < 0)
199 return rc;
201 count++;
202 if (next)
204 pos = buf_size - ((long)next - (long)buf) - 1;
205 memmove(buf, next, pos);
207 else
208 break ;
211 return 0;
214 /* parse a line from a configuration file. the line format is:
216 name: value
218 Any whitespace before setting name or value (after ':') is ignored.
219 A # as first non-whitespace character discards the whole line.
220 Function sets pointers to null-terminated setting name and value.
221 Returns false if no valid config entry was found.
224 bool settings_parseline(char* line, char** name, char** value)
226 char* ptr;
228 line = skip_whitespace(line);
230 if ( *line == '#' )
231 return false;
233 ptr = strchr(line, ':');
234 if ( !ptr )
235 return false;
237 *name = line;
238 *ptr = 0;
239 ptr++;
240 ptr = skip_whitespace(ptr);
241 *value = ptr;
242 return true;
245 static void system_flush(void)
247 playlist_shutdown();
248 tree_flush();
249 call_storage_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
252 static void system_restore(void)
254 tree_restore();
257 static bool clean_shutdown(void (*callback)(void *), void *parameter)
259 long msg_id = -1;
261 scrobbler_poweroff();
263 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
264 if(!charger_inserted())
265 #endif
267 bool batt_safe = battery_level_safe();
268 #if CONFIG_CODEC != SWCODEC || defined(HAVE_RECORDING)
269 int audio_stat = audio_status();
270 #endif
272 FOR_NB_SCREENS(i)
274 screens[i].clear_display();
275 screens[i].update();
278 if (batt_safe)
280 #ifdef HAVE_TAGCACHE
281 if (!tagcache_prepare_shutdown())
283 cancel_shutdown();
284 splash(HZ, ID2P(LANG_TAGCACHE_BUSY));
285 return false;
287 #endif
288 if (battery_level() > 10)
289 splash(0, str(LANG_SHUTTINGDOWN));
290 else
292 msg_id = LANG_WARNING_BATTERY_LOW;
293 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_LOW),
294 str(LANG_SHUTTINGDOWN));
297 else
299 msg_id = LANG_WARNING_BATTERY_EMPTY;
300 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_EMPTY),
301 str(LANG_SHUTTINGDOWN));
303 #if CONFIG_CODEC != SWCODEC
304 if (global_settings.fade_on_stop
305 && (audio_stat & AUDIO_STATUS_PLAY))
307 fade(false, false);
309 #endif
311 if (batt_safe) /* do not save on critical battery */
313 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
314 if (audio_stat & AUDIO_STATUS_RECORD)
316 rec_command(RECORDING_CMD_STOP);
317 /* wait for stop to complete */
318 while (audio_status() & AUDIO_STATUS_RECORD)
319 sleep(1);
321 #endif
322 bookmark_autobookmark(false);
324 /* audio_stop_recording == audio_stop for HWCODEC */
325 audio_stop();
327 if (callback != NULL)
328 callback(parameter);
330 #if CONFIG_CODEC != SWCODEC
331 /* wait for audio_stop or audio_stop_recording to complete */
332 while (audio_status())
333 sleep(1);
334 #endif
336 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
337 audio_close_recording();
338 #endif
340 if(global_settings.talk_menu)
342 bool enqueue = false;
343 if(msg_id != -1)
345 talk_id(msg_id, enqueue);
346 enqueue = true;
348 talk_id(LANG_SHUTTINGDOWN, enqueue);
349 #if CONFIG_CODEC == SWCODEC
350 voice_wait();
351 #endif
354 system_flush();
355 #ifdef HAVE_EEPROM_SETTINGS
356 if (firmware_settings.initialized)
358 firmware_settings.disk_clean = true;
359 firmware_settings.bl_version = 0;
360 eeprom_settings_store();
362 #endif
364 #ifdef HAVE_DIRCACHE
365 else
366 dircache_disable();
367 #endif
369 shutdown_hw();
371 return false;
374 bool list_stop_handler(void)
376 bool ret = false;
378 #if CONFIG_TUNER
379 radio_stop();
380 #endif
382 /* Stop the music if it is playing */
383 if(audio_status())
385 if (!global_settings.party_mode)
387 #if CONFIG_CODEC != SWCODEC
388 if (global_settings.fade_on_stop)
389 fade(false, false);
390 #endif
391 bookmark_autobookmark(true);
392 audio_stop();
393 ret = true; /* bookmarking can make a refresh necessary */
396 #if CONFIG_CHARGING
397 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
398 else
400 if (charger_inserted())
401 charging_splash();
402 else
403 shutdown_screen(); /* won't return if shutdown actually happens */
405 ret = true; /* screen is dirty, caller needs to refresh */
407 #endif
408 #ifndef HAVE_POWEROFF_WHILE_CHARGING
410 static long last_off = 0;
412 if (TIME_BEFORE(current_tick, last_off + HZ/2))
414 if (charger_inserted())
416 charging_splash();
417 ret = true; /* screen is dirty, caller needs to refresh */
420 last_off = current_tick;
422 #endif
423 #endif /* CONFIG_CHARGING */
424 return ret;
427 #if CONFIG_CHARGING
428 static bool waiting_to_resume_play = false;
429 static long play_resume_tick;
431 static void car_adapter_mode_processing(bool inserted)
433 if (global_settings.car_adapter_mode)
435 if(inserted)
438 * Just got plugged in, delay & resume if we were playing
440 if (audio_status() & AUDIO_STATUS_PAUSE)
442 /* delay resume a bit while the engine is cranking */
443 play_resume_tick = current_tick + HZ*5;
444 waiting_to_resume_play = true;
447 else
450 * Just got unplugged, pause if playing
452 if ((audio_status() & AUDIO_STATUS_PLAY) &&
453 !(audio_status() & AUDIO_STATUS_PAUSE))
455 pause_action(true, true);
457 waiting_to_resume_play = false;
462 static void car_adapter_tick(void)
464 if (waiting_to_resume_play)
466 if (TIME_AFTER(current_tick, play_resume_tick))
468 if (audio_status() & AUDIO_STATUS_PAUSE)
470 queue_broadcast(SYS_CAR_ADAPTER_RESUME, 0);
472 waiting_to_resume_play = false;
477 void car_adapter_mode_init(void)
479 tick_add_task(car_adapter_tick);
481 #endif
483 #ifdef HAVE_HEADPHONE_DETECTION
484 static void unplug_change(bool inserted)
486 static bool headphone_caused_pause = false;
488 if (global_settings.unplug_mode)
490 int audio_stat = audio_status();
491 if (inserted)
493 backlight_on();
494 if ((audio_stat & AUDIO_STATUS_PLAY) &&
495 headphone_caused_pause &&
496 global_settings.unplug_mode > 1 )
497 unpause_action(true, true);
498 headphone_caused_pause = false;
499 } else {
500 if ((audio_stat & AUDIO_STATUS_PLAY) &&
501 !(audio_stat & AUDIO_STATUS_PAUSE))
503 headphone_caused_pause = true;
504 pause_action(false, false);
509 #endif
511 long default_event_handler_ex(long event, void (*callback)(void *), void *parameter)
513 #if CONFIG_PLATFORM & (PLATFORM_ANDROID|PLATFORM_MAEMO)
514 static bool resume = false;
515 #endif
517 switch(event)
519 case SYS_BATTERY_UPDATE:
520 if(global_settings.talk_battery_level)
522 talk_ids(true, VOICE_PAUSE, VOICE_PAUSE,
523 LANG_BATTERY_TIME,
524 TALK_ID(battery_level(), UNIT_PERCENT),
525 VOICE_PAUSE);
526 talk_force_enqueue_next();
528 break;
529 case SYS_USB_CONNECTED:
530 if (callback != NULL)
531 callback(parameter);
532 #if (CONFIG_STORAGE & STORAGE_MMC) && (defined(ARCHOS_ONDIOSP) || defined(ARCHOS_ONDIOFM))
533 if (!mmc_touched() ||
534 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
535 #endif
537 system_flush();
538 #ifdef BOOTFILE
539 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
540 check_bootfile(false); /* gets initial size */
541 #endif
542 #endif
543 gui_usb_screen_run(false);
544 #ifdef BOOTFILE
545 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
546 check_bootfile(true);
547 #endif
548 #endif
549 system_restore();
551 return SYS_USB_CONNECTED;
553 case SYS_POWEROFF:
554 if (!clean_shutdown(callback, parameter))
555 return SYS_POWEROFF;
556 break;
557 #if CONFIG_CHARGING
558 case SYS_CHARGER_CONNECTED:
559 car_adapter_mode_processing(true);
560 return SYS_CHARGER_CONNECTED;
562 case SYS_CHARGER_DISCONNECTED:
563 car_adapter_mode_processing(false);
564 /*reset rockbox battery runtime*/
565 global_status.runtime = 0;
566 return SYS_CHARGER_DISCONNECTED;
568 case SYS_CAR_ADAPTER_RESUME:
569 unpause_action(true, true);
570 return SYS_CAR_ADAPTER_RESUME;
571 #endif
572 #ifdef HAVE_HOTSWAP_STORAGE_AS_MAIN
573 case SYS_FS_CHANGED:
575 /* simple sanity: assume rockbox is on the first hotswappable
576 * driver, abort out if that one isn't inserted */
577 int i;
578 for (i = 0; i < NUM_DRIVES; i++)
580 if (storage_removable(i) && !storage_present(i))
581 return SYS_FS_CHANGED;
583 system_flush();
584 check_bootfile(true); /* state gotten in main.c:init() */
585 system_restore();
587 return SYS_FS_CHANGED;
588 #endif
589 #ifdef HAVE_HEADPHONE_DETECTION
590 case SYS_PHONE_PLUGGED:
591 unplug_change(true);
592 return SYS_PHONE_PLUGGED;
594 case SYS_PHONE_UNPLUGGED:
595 unplug_change(false);
596 return SYS_PHONE_UNPLUGGED;
597 #endif
598 #ifdef IPOD_ACCESSORY_PROTOCOL
599 case SYS_IAP_PERIODIC:
600 iap_periodic();
601 return SYS_IAP_PERIODIC;
602 case SYS_IAP_HANDLEPKT:
603 iap_handlepkt();
604 return SYS_IAP_HANDLEPKT;
605 #endif
606 #if CONFIG_PLATFORM & (PLATFORM_ANDROID|PLATFORM_MAEMO)
607 /* stop playback if we receive a call */
608 case SYS_CALL_INCOMING:
609 resume = audio_status() == AUDIO_STATUS_PLAY;
610 list_stop_handler();
611 return SYS_CALL_INCOMING;
612 /* resume playback if needed */
613 case SYS_CALL_HUNG_UP:
614 if (resume && playlist_resume() != -1)
616 playlist_start(global_status.resume_index,
617 global_status.resume_offset);
619 resume = false;
620 return SYS_CALL_HUNG_UP;
621 #endif
622 #if (CONFIG_PLATFORM & PLATFORM_HOSTED) && defined(PLATFORM_HAS_VOLUME_CHANGE)
623 case SYS_VOLUME_CHANGED:
625 static bool firstvolume = true;
626 /* kludge: since this events go to the button_queue,
627 * event data is available in the last button data */
628 int volume = button_get_data();
629 DEBUGF("SYS_VOLUME_CHANGED: %d\n", volume);
630 if (global_settings.volume != volume) {
631 global_settings.volume = volume;
632 if (firstvolume) {
633 setvol();
634 firstvolume = false;
637 return 0;
639 #endif
640 #ifdef HAVE_MULTIMEDIA_KEYS
641 /* multimedia keys on keyboards, headsets */
642 case BUTTON_MULTIMEDIA_PLAYPAUSE:
644 int status = audio_status();
645 if (status & AUDIO_STATUS_PLAY)
647 if (status & AUDIO_STATUS_PAUSE)
648 unpause_action(true, true);
649 else
650 pause_action(true, true);
652 else
653 if (playlist_resume() != -1)
655 playlist_start(global_status.resume_index,
656 global_status.resume_offset);
658 return event;
660 case BUTTON_MULTIMEDIA_NEXT:
661 audio_next();
662 return event;
663 case BUTTON_MULTIMEDIA_PREV:
664 audio_prev();
665 return event;
666 case BUTTON_MULTIMEDIA_STOP:
667 list_stop_handler();
668 return event;
669 case BUTTON_MULTIMEDIA_REW:
670 case BUTTON_MULTIMEDIA_FFWD:
671 /* not supported yet, needs to be done in the WPS */
672 return 0;
673 #endif
675 return 0;
678 long default_event_handler(long event)
680 return default_event_handler_ex(event, NULL, NULL);
683 int show_logo( void )
685 #ifdef HAVE_LCD_BITMAP
686 char version[32];
687 int font_h, font_w;
689 snprintf(version, sizeof(version), "Ver. %s", rbversion);
691 lcd_clear_display();
692 #if defined(SANSA_CLIP) || defined(SANSA_CLIPV2) || defined(SANSA_CLIPPLUS)
693 /* display the logo in the blue area of the screen */
694 lcd_setfont(FONT_SYSFIXED);
695 lcd_getstringsize((unsigned char *)"A", &font_w, &font_h);
696 lcd_putsxy((LCD_WIDTH/2) - ((strlen(version)*font_w)/2),
697 0, (unsigned char *)version);
698 lcd_bmp(&bm_rockboxlogo, (LCD_WIDTH - BMPWIDTH_rockboxlogo) / 2, 16);
699 #else
700 lcd_bmp(&bm_rockboxlogo, (LCD_WIDTH - BMPWIDTH_rockboxlogo) / 2, 10);
701 lcd_setfont(FONT_SYSFIXED);
702 lcd_getstringsize((unsigned char *)"A", &font_w, &font_h);
703 lcd_putsxy((LCD_WIDTH/2) - ((strlen(version)*font_w)/2),
704 LCD_HEIGHT-font_h, (unsigned char *)version);
705 #endif
706 lcd_setfont(FONT_UI);
708 #else
709 char *rockbox = " ROCKbox!";
711 lcd_clear_display();
712 lcd_double_height(true);
713 lcd_puts(0, 0, rockbox);
714 lcd_puts_scroll(0, 1, rbversion);
715 #endif
716 lcd_update();
718 #ifdef HAVE_REMOTE_LCD
719 lcd_remote_clear_display();
720 lcd_remote_bmp(&bm_remote_rockboxlogo, 0, 10);
721 lcd_remote_setfont(FONT_SYSFIXED);
722 lcd_remote_getstringsize((unsigned char *)"A", &font_w, &font_h);
723 lcd_remote_putsxy((LCD_REMOTE_WIDTH/2) - ((strlen(version)*font_w)/2),
724 LCD_REMOTE_HEIGHT-font_h, (unsigned char *)version);
725 lcd_remote_setfont(FONT_UI);
726 lcd_remote_update();
727 #endif
729 return 0;
732 #ifdef BOOTFILE
733 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF) || defined(HAVE_HOTSWAP_STORAGE_AS_MAIN)
735 memorize/compare details about the BOOTFILE
736 we don't use dircache because it may not be up to date after
737 USB disconnect (scanning in the background)
739 void check_bootfile(bool do_rolo)
741 static unsigned short wrtdate = 0;
742 static unsigned short wrttime = 0;
743 DIR* dir = NULL;
744 struct dirent* entry = NULL;
746 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
747 dir = opendir(BOOTDIR);
749 if(!dir) return; /* do we want an error splash? */
751 /* loop all files in BOOTDIR */
752 while(0 != (entry = readdir(dir)))
754 if(!strcasecmp(entry->d_name, BOOTFILE))
756 struct dirinfo info = dir_get_info(dir, entry);
757 /* found the bootfile */
758 if(wrtdate && do_rolo)
760 if((info.wrtdate != wrtdate) ||
761 (info.wrttime != wrttime))
763 static const char *lines[] = { ID2P(LANG_BOOT_CHANGED),
764 ID2P(LANG_REBOOT_NOW) };
765 static const struct text_message message={ lines, 2 };
766 button_clear_queue(); /* Empty the keyboard buffer */
767 if(gui_syncyesno_run(&message, NULL, NULL) == YESNO_YES)
769 audio_hard_stop();
770 rolo_load(BOOTDIR "/" BOOTFILE);
774 wrtdate = info.wrtdate;
775 wrttime = info.wrttime;
778 closedir(dir);
780 #endif
781 #endif
783 /* check range, set volume and save settings */
784 void setvol(void)
786 const int min_vol = sound_min(SOUND_VOLUME);
787 const int max_vol = sound_max(SOUND_VOLUME);
788 if (global_settings.volume < min_vol)
789 global_settings.volume = min_vol;
790 if (global_settings.volume > max_vol)
791 global_settings.volume = max_vol;
792 sound_set_volume(global_settings.volume);
793 global_status.last_volume_change = current_tick;
794 settings_save();
797 char* strrsplt(char* str, int c)
799 char* s = strrchr(str, c);
801 if (s != NULL)
803 *s++ = '\0';
805 else
807 s = str;
810 return s;
814 * removes the extension of filename (if it doesn't start with a .)
815 * puts the result in buffer
817 char *strip_extension(char* buffer, int buffer_size, const char *filename)
819 char *dot = strrchr(filename, '.');
820 int len;
822 if (buffer_size <= 0)
824 return NULL;
827 buffer_size--; /* Make room for end nil */
829 if (dot != 0 && filename[0] != '.')
831 len = dot - filename;
832 len = MIN(len, buffer_size);
834 else
836 len = buffer_size;
839 strlcpy(buffer, filename, len + 1);
841 return buffer;
844 #if CONFIG_CODEC == SWCODEC
845 /* Play a standard sound */
846 void system_sound_play(enum system_sound sound)
848 static const struct beep_params
850 int *setting;
851 unsigned short frequency;
852 unsigned short duration;
853 unsigned short amplitude;
854 } beep_params[] =
856 [SOUND_KEYCLICK] =
857 { &global_settings.keyclick,
858 4000, KEYCLICK_DURATION, 2500 },
859 [SOUND_TRACK_SKIP] =
860 { &global_settings.beep,
861 2000, 100, 2500 },
862 [SOUND_TRACK_NO_MORE] =
863 { &global_settings.beep,
864 1000, 100, 1500 },
867 const struct beep_params *params = &beep_params[sound];
869 if (*params->setting)
871 beep_play(params->frequency, params->duration,
872 params->amplitude * *params->setting);
876 /* Produce keyclick based upon button and global settings */
877 void keyclick_click(int button)
879 static long last_button = BUTTON_NONE;
880 bool do_beep = false;
881 /* Settings filters */
882 if (
883 #ifdef HAVE_HARDWARE_CLICK
884 (global_settings.keyclick || global_settings.keyclick_hardware)
885 #else
886 global_settings.keyclick
887 #endif
890 if (global_settings.keyclick_repeats || !(button & BUTTON_REPEAT))
892 /* Button filters */
893 if (button != BUTTON_NONE && !(button & BUTTON_REL)
894 && !(button & (SYS_EVENT|BUTTON_MULTIMEDIA)) )
896 do_beep = true;
899 else if ((button & BUTTON_REPEAT) && (last_button == BUTTON_NONE))
901 do_beep = true;
903 #ifdef HAVE_SCROLLWHEEL
904 else if (button & (BUTTON_SCROLL_BACK | BUTTON_SCROLL_FWD))
906 do_beep = true;
908 #endif
910 if (button&BUTTON_REPEAT)
911 last_button = button;
912 else
913 last_button = BUTTON_NONE;
914 if (do_beep)
916 #ifdef HAVE_HARDWARE_CLICK
917 if (global_settings.keyclick)
919 system_sound_play(SOUND_KEYCLICK);
921 if (global_settings.keyclick_hardware)
923 #if !defined(SIMULATOR)
924 piezo_button_beep(false, false);
925 #endif
927 #else
928 system_sound_play(SOUND_KEYCLICK);
929 #endif
932 #endif /* CONFIG_CODEC == SWCODEC */
934 #endif /* !defined(__PCTOOL__) */
936 /* Read (up to) a line of text from fd into buffer and return number of bytes
937 * read (which may be larger than the number of bytes stored in buffer). If
938 * an error occurs, -1 is returned (and buffer contains whatever could be
939 * read). A line is terminated by a LF char. Neither LF nor CR chars are
940 * stored in buffer.
942 int read_line(int fd, char* buffer, int buffer_size)
944 int count = 0;
945 int num_read = 0;
947 errno = 0;
949 while (count < buffer_size)
951 unsigned char c;
953 if (1 != read(fd, &c, 1))
954 break;
956 num_read++;
958 if ( c == '\n' )
959 break;
961 if ( c == '\r' )
962 continue;
964 buffer[count++] = c;
967 buffer[MIN(count, buffer_size - 1)] = 0;
969 return errno ? -1 : num_read;
973 char* skip_whitespace(char* const str)
975 char *s = str;
977 while (isspace(*s))
978 s++;
980 return s;
983 /* Format time into buf.
985 * buf - buffer to format to.
986 * buf_size - size of buffer.
987 * t - time to format, in milliseconds.
989 void format_time(char* buf, int buf_size, long t)
991 int const time = abs(t / 1000);
992 int const hours = time / 3600;
993 int const minutes = time / 60 - hours * 60;
994 int const seconds = time % 60;
995 const char * const sign = &"-"[t < 0 ? 0 : 1];
997 if ( hours == 0 )
999 snprintf(buf, buf_size, "%s%d:%02d", sign, minutes, seconds);
1001 else
1003 snprintf(buf, buf_size, "%s%d:%02d:%02d", sign, hours, minutes,
1004 seconds);
1009 /** Open a UTF-8 file and set file descriptor to first byte after BOM.
1010 * If no BOM is present this behaves like open().
1011 * If the file is opened for writing and O_TRUNC is set, write a BOM to
1012 * the opened file and leave the file pointer set after the BOM.
1015 int open_utf8(const char* pathname, int flags)
1017 int fd;
1018 unsigned char bom[BOM_UTF_8_SIZE];
1020 fd = open(pathname, flags, 0666);
1021 if(fd < 0)
1022 return fd;
1024 if(flags & (O_TRUNC | O_WRONLY))
1026 write(fd, BOM_UTF_8, BOM_UTF_8_SIZE);
1028 else
1030 read(fd, bom, BOM_UTF_8_SIZE);
1031 /* check for BOM */
1032 if(memcmp(bom, BOM_UTF_8, BOM_UTF_8_SIZE))
1033 lseek(fd, 0, SEEK_SET);
1035 return fd;
1039 #ifdef HAVE_LCD_COLOR
1041 * Helper function to convert a string of 6 hex digits to a native colour
1044 static int hex2dec(int c)
1046 return (((c) >= '0' && ((c) <= '9')) ? (c) - '0' :
1047 (toupper(c)) - 'A' + 10);
1050 int hex_to_rgb(const char* hex, int* color)
1052 int red, green, blue;
1053 int i = 0;
1055 while ((i < 6) && (isxdigit(hex[i])))
1056 i++;
1058 if (i < 6)
1059 return -1;
1061 red = (hex2dec(hex[0]) << 4) | hex2dec(hex[1]);
1062 green = (hex2dec(hex[2]) << 4) | hex2dec(hex[3]);
1063 blue = (hex2dec(hex[4]) << 4) | hex2dec(hex[5]);
1065 *color = LCD_RGBPACK(red,green,blue);
1067 return 0;
1069 #endif /* HAVE_LCD_COLOR */
1071 #ifdef HAVE_LCD_BITMAP
1072 /* '0'-'3' are ASCII 0x30 to 0x33 */
1073 #define is0123(x) (((x) & 0xfc) == 0x30)
1074 #if !defined(__PCTOOL__) || defined(CHECKWPS)
1075 bool parse_color(enum screen_type screen, char *text, int *value)
1077 (void)text; (void)value; /* silence warnings on mono bitmap */
1078 (void)screen;
1080 #ifdef HAVE_LCD_COLOR
1081 if (screens[screen].depth > 2)
1083 if (hex_to_rgb(text, value) < 0)
1084 return false;
1085 else
1086 return true;
1088 #endif
1090 #if LCD_DEPTH == 2 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 2)
1091 if (screens[screen].depth == 2)
1093 if (text[1] != '\0' || !is0123(*text))
1094 return false;
1095 *value = *text - '0';
1096 return true;
1098 #endif
1099 return false;
1101 #endif /* !defined(__PCTOOL__) || defined(CHECKWPS) */
1103 /* only used in USB HID and set_time screen */
1104 #if defined(USB_ENABLE_HID) || (CONFIG_RTC != 0)
1105 int clamp_value_wrap(int value, int max, int min)
1107 if (value > max)
1108 return min;
1109 if (value < min)
1110 return max;
1111 return value;
1113 #endif
1114 #endif
1115 #define MAX_ACTIVITY_DEPTH 12
1116 static enum current_activity
1117 current_activity[MAX_ACTIVITY_DEPTH] = {ACTIVITY_UNKNOWN};
1118 static int current_activity_top = 0;
1119 void push_current_activity(enum current_activity screen)
1121 current_activity[current_activity_top++] = screen;
1122 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
1123 FOR_NB_SCREENS(i)
1124 skinlist_set_cfg(i, NULL);
1125 #endif
1127 void pop_current_activity(void)
1129 current_activity_top--;
1130 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
1131 FOR_NB_SCREENS(i)
1132 skinlist_set_cfg(i, NULL);
1133 #endif
1135 enum current_activity get_current_activity(void)
1137 return current_activity[current_activity_top?current_activity_top-1:0];