fix compiler error, missed action.h
[maemo-rb.git] / apps / misc.c
blob381512ca5621e4a931d2e850889c23033c2487f5
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 #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 "mp3_playback.h"
46 #include "settings.h"
47 #include "storage.h"
48 #include "ata_idle_notify.h"
49 #include "kernel.h"
50 #include "power.h"
51 #include "powermgmt.h"
52 #include "backlight.h"
53 #include "version.h"
54 #include "font.h"
55 #include "splash.h"
56 #include "tagcache.h"
57 #include "scrobbler.h"
58 #include "sound.h"
59 #include "playlist.h"
60 #include "yesno.h"
61 #include "viewport.h"
62 #include "list.h"
64 #include "debug.h"
66 #if CONFIG_TUNER
67 #include "radio.h"
68 #endif
70 #ifdef IPOD_ACCESSORY_PROTOCOL
71 #include "iap.h"
72 #endif
74 #if (CONFIG_STORAGE & STORAGE_MMC)
75 #include "ata_mmc.h"
76 #endif
77 #include "tree.h"
78 #include "eeprom_settings.h"
79 #if defined(HAVE_RECORDING) && !defined(__PCTOOL__)
80 #include "recording.h"
81 #endif
82 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
83 #include "bmp.h"
84 #include "icons.h"
85 #endif /* End HAVE_LCD_BITMAP */
86 #include "bookmark.h"
87 #include "wps.h"
88 #include "playback.h"
89 #if CONFIG_CODEC == SWCODEC
90 #include "voice_thread.h"
91 #endif
93 #ifdef BOOTFILE
94 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF) \
95 || defined(HAVE_HOTSWAP_STORAGE_AS_MAIN)
96 #include "rolo.h"
97 #endif
98 #endif
100 #ifdef HAVE_HARDWARE_CLICK
101 #include "piezo.h"
102 #endif
104 /* units used with output_dyn_value */
105 const unsigned char * const byte_units[] =
107 ID2P(LANG_BYTE),
108 ID2P(LANG_KILOBYTE),
109 ID2P(LANG_MEGABYTE),
110 ID2P(LANG_GIGABYTE)
113 const unsigned char * const * const kbyte_units = &byte_units[1];
115 /* Format a large-range value for output, using the appropriate unit so that
116 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
117 * units) if possible, and 3 significant digits are shown. If a buffer is
118 * given, the result is snprintf()'d into that buffer, otherwise the result is
119 * voiced.*/
120 char *output_dyn_value(char *buf, int buf_size, int value,
121 const unsigned char * const *units, bool bin_scale)
123 int scale = bin_scale ? 1024 : 1000;
124 int fraction = 0;
125 int unit_no = 0;
126 char tbuf[5];
128 while (value >= scale)
130 fraction = value % scale;
131 value /= scale;
132 unit_no++;
134 if (bin_scale)
135 fraction = fraction * 1000 / 1024;
137 if (value >= 100 || !unit_no)
138 tbuf[0] = '\0';
139 else if (value >= 10)
140 snprintf(tbuf, sizeof(tbuf), "%01d", fraction / 100);
141 else
142 snprintf(tbuf, sizeof(tbuf), "%02d", fraction / 10);
144 if (buf)
146 if (*tbuf)
147 snprintf(buf, buf_size, "%d%s%s%s", value, str(LANG_POINT),
148 tbuf, P2STR(units[unit_no]));
149 else
150 snprintf(buf, buf_size, "%d%s", value, P2STR(units[unit_no]));
152 else
154 talk_fractional(tbuf, value, P2ID(units[unit_no]));
156 return buf;
159 /* Ask the user if they really want to erase the current dynamic playlist
160 * returns true if the playlist should be replaced */
161 bool warn_on_pl_erase(void)
163 if (global_settings.warnon_erase_dynplaylist &&
164 !global_settings.party_mode &&
165 playlist_modified(NULL))
167 static const char *lines[] =
168 {ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
169 static const struct text_message message={lines, 1};
171 return (gui_syncyesno_run(&message, NULL, NULL) == YESNO_YES);
173 else
174 return true;
178 /* Performance optimized version of the read_line() (see below) function. */
179 int fast_readline(int fd, char *buf, int buf_size, void *parameters,
180 int (*callback)(int n, char *buf, void *parameters))
182 char *p, *next;
183 int rc, pos = 0;
184 int count = 0;
186 while ( 1 )
188 next = NULL;
190 rc = read(fd, &buf[pos], buf_size - pos - 1);
191 if (rc >= 0)
192 buf[pos+rc] = '\0';
194 if ( (p = strchr(buf, '\n')) != NULL)
196 *p = '\0';
197 next = ++p;
200 rc = callback(count, buf, parameters);
201 if (rc < 0)
202 return rc;
204 count++;
205 if (next)
207 pos = buf_size - ((long)next - (long)buf) - 1;
208 memmove(buf, next, pos);
210 else
211 break ;
214 return 0;
217 /* parse a line from a configuration file. the line format is:
219 name: value
221 Any whitespace before setting name or value (after ':') is ignored.
222 A # as first non-whitespace character discards the whole line.
223 Function sets pointers to null-terminated setting name and value.
224 Returns false if no valid config entry was found.
227 bool settings_parseline(char* line, char** name, char** value)
229 char* ptr;
231 line = skip_whitespace(line);
233 if ( *line == '#' )
234 return false;
236 ptr = strchr(line, ':');
237 if ( !ptr )
238 return false;
240 *name = line;
241 *ptr = 0;
242 ptr++;
243 ptr = skip_whitespace(ptr);
244 *value = ptr;
245 return true;
248 static void system_flush(void)
250 playlist_shutdown();
251 tree_flush();
252 call_storage_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
255 static void system_restore(void)
257 tree_restore();
260 static bool clean_shutdown(void (*callback)(void *), void *parameter)
262 long msg_id = -1;
264 scrobbler_poweroff();
266 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
267 if(!charger_inserted())
268 #endif
270 bool batt_safe = battery_level_safe();
271 #if CONFIG_CODEC != SWCODEC || defined(HAVE_RECORDING)
272 int audio_stat = audio_status();
273 #endif
275 FOR_NB_SCREENS(i)
277 screens[i].clear_display();
278 screens[i].update();
281 if (batt_safe)
283 int level;
284 #ifdef HAVE_TAGCACHE
285 if (!tagcache_prepare_shutdown())
287 cancel_shutdown();
288 splash(HZ, ID2P(LANG_TAGCACHE_BUSY));
289 return false;
291 #endif
292 level = battery_level();
293 if (level > 10 || level < 0)
294 splash(0, str(LANG_SHUTTINGDOWN));
295 else
297 msg_id = LANG_WARNING_BATTERY_LOW;
298 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_LOW),
299 str(LANG_SHUTTINGDOWN));
302 else
304 msg_id = LANG_WARNING_BATTERY_EMPTY;
305 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_EMPTY),
306 str(LANG_SHUTTINGDOWN));
308 #if CONFIG_CODEC != SWCODEC
309 if (global_settings.fade_on_stop
310 && (audio_stat & AUDIO_STATUS_PLAY))
312 fade(false, false);
314 #endif
316 if (batt_safe) /* do not save on critical battery */
318 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
319 if (audio_stat & AUDIO_STATUS_RECORD)
321 rec_command(RECORDING_CMD_STOP);
322 /* wait for stop to complete */
323 while (audio_status() & AUDIO_STATUS_RECORD)
324 sleep(1);
326 #endif
327 bookmark_autobookmark(false);
329 /* audio_stop_recording == audio_stop for HWCODEC */
330 audio_stop();
332 if (callback != NULL)
333 callback(parameter);
335 #if CONFIG_CODEC != SWCODEC
336 /* wait for audio_stop or audio_stop_recording to complete */
337 while (audio_status())
338 sleep(1);
339 #endif
341 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
342 audio_close_recording();
343 #endif
345 if(global_settings.talk_menu)
347 bool enqueue = false;
348 if(msg_id != -1)
350 talk_id(msg_id, enqueue);
351 enqueue = true;
353 talk_id(LANG_SHUTTINGDOWN, enqueue);
354 #if CONFIG_CODEC == SWCODEC
355 voice_wait();
356 #endif
359 system_flush();
360 #ifdef HAVE_EEPROM_SETTINGS
361 if (firmware_settings.initialized)
363 firmware_settings.disk_clean = true;
364 firmware_settings.bl_version = 0;
365 eeprom_settings_store();
367 #endif
369 #ifdef HAVE_DIRCACHE
370 else
371 dircache_disable();
372 #endif
374 shutdown_hw();
376 return false;
379 bool list_stop_handler(void)
381 bool ret = false;
383 #if CONFIG_TUNER
384 radio_stop();
385 #endif
387 /* Stop the music if it is playing */
388 if(audio_status())
390 if (!global_settings.party_mode)
392 #if CONFIG_CODEC != SWCODEC
393 if (global_settings.fade_on_stop)
394 fade(false, false);
395 #endif
396 bookmark_autobookmark(true);
397 audio_stop();
398 ret = true; /* bookmarking can make a refresh necessary */
401 #if CONFIG_CHARGING
402 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
403 else
405 if (charger_inserted())
406 charging_splash();
407 else
408 shutdown_screen(); /* won't return if shutdown actually happens */
410 ret = true; /* screen is dirty, caller needs to refresh */
412 #endif
413 #ifndef HAVE_POWEROFF_WHILE_CHARGING
415 static long last_off = 0;
417 if (TIME_BEFORE(current_tick, last_off + HZ/2))
419 if (charger_inserted())
421 charging_splash();
422 ret = true; /* screen is dirty, caller needs to refresh */
425 last_off = current_tick;
427 #endif
428 #endif /* CONFIG_CHARGING */
429 return ret;
432 #if CONFIG_CHARGING
433 static bool waiting_to_resume_play = false;
434 static long play_resume_tick;
436 static void car_adapter_mode_processing(bool inserted)
438 if (global_settings.car_adapter_mode)
440 if(inserted)
443 * Just got plugged in, delay & resume if we were playing
445 if (audio_status() & AUDIO_STATUS_PAUSE)
447 /* delay resume a bit while the engine is cranking */
448 play_resume_tick = current_tick + HZ*5;
449 waiting_to_resume_play = true;
452 else
455 * Just got unplugged, pause if playing
457 if ((audio_status() & AUDIO_STATUS_PLAY) &&
458 !(audio_status() & AUDIO_STATUS_PAUSE))
460 pause_action(true, true);
462 waiting_to_resume_play = false;
467 static void car_adapter_tick(void)
469 if (waiting_to_resume_play)
471 if (TIME_AFTER(current_tick, play_resume_tick))
473 if (audio_status() & AUDIO_STATUS_PAUSE)
475 queue_broadcast(SYS_CAR_ADAPTER_RESUME, 0);
477 waiting_to_resume_play = false;
482 void car_adapter_mode_init(void)
484 tick_add_task(car_adapter_tick);
486 #endif
488 #ifdef HAVE_HEADPHONE_DETECTION
489 static void unplug_change(bool inserted)
491 static bool headphone_caused_pause = false;
493 if (global_settings.unplug_mode)
495 int audio_stat = audio_status();
496 if (inserted)
498 backlight_on();
499 if ((audio_stat & AUDIO_STATUS_PLAY) &&
500 headphone_caused_pause &&
501 global_settings.unplug_mode > 1 )
502 unpause_action(true, true);
503 headphone_caused_pause = false;
504 } else {
505 if ((audio_stat & AUDIO_STATUS_PLAY) &&
506 !(audio_stat & AUDIO_STATUS_PAUSE))
508 headphone_caused_pause = true;
509 pause_action(false, false);
514 #endif
516 long default_event_handler_ex(long event, void (*callback)(void *), void *parameter)
518 #if CONFIG_PLATFORM & (PLATFORM_ANDROID|PLATFORM_MAEMO)
519 static bool resume = false;
520 #endif
522 switch(event)
524 case SYS_BATTERY_UPDATE:
525 if(global_settings.talk_battery_level)
527 talk_ids(true, VOICE_PAUSE, VOICE_PAUSE,
528 LANG_BATTERY_TIME,
529 TALK_ID(battery_level(), UNIT_PERCENT),
530 VOICE_PAUSE);
531 talk_force_enqueue_next();
533 break;
534 case SYS_USB_CONNECTED:
535 if (callback != NULL)
536 callback(parameter);
537 #if (CONFIG_STORAGE & STORAGE_MMC) && (defined(ARCHOS_ONDIOSP) || defined(ARCHOS_ONDIOFM))
538 if (!mmc_touched() ||
539 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
540 #endif
542 system_flush();
543 #ifdef BOOTFILE
544 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
545 check_bootfile(false); /* gets initial size */
546 #endif
547 #endif
548 gui_usb_screen_run(false);
549 #ifdef BOOTFILE
550 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
551 check_bootfile(true);
552 #endif
553 #endif
554 system_restore();
556 return SYS_USB_CONNECTED;
558 case SYS_POWEROFF:
559 if (!clean_shutdown(callback, parameter))
560 return SYS_POWEROFF;
561 break;
562 #if CONFIG_CHARGING
563 case SYS_CHARGER_CONNECTED:
564 car_adapter_mode_processing(true);
565 return SYS_CHARGER_CONNECTED;
567 case SYS_CHARGER_DISCONNECTED:
568 car_adapter_mode_processing(false);
569 /*reset rockbox battery runtime*/
570 global_status.runtime = 0;
571 return SYS_CHARGER_DISCONNECTED;
573 case SYS_CAR_ADAPTER_RESUME:
574 unpause_action(true, true);
575 return SYS_CAR_ADAPTER_RESUME;
576 #endif
577 #ifdef HAVE_HOTSWAP_STORAGE_AS_MAIN
578 case SYS_FS_CHANGED:
580 /* simple sanity: assume rockbox is on the first hotswappable
581 * driver, abort out if that one isn't inserted */
582 int i;
583 for (i = 0; i < NUM_DRIVES; i++)
585 if (storage_removable(i) && !storage_present(i))
586 return SYS_FS_CHANGED;
588 system_flush();
589 check_bootfile(true); /* state gotten in main.c:init() */
590 system_restore();
592 return SYS_FS_CHANGED;
593 #endif
594 #ifdef HAVE_HEADPHONE_DETECTION
595 case SYS_PHONE_PLUGGED:
596 unplug_change(true);
597 return SYS_PHONE_PLUGGED;
599 case SYS_PHONE_UNPLUGGED:
600 unplug_change(false);
601 return SYS_PHONE_UNPLUGGED;
602 #endif
603 #ifdef IPOD_ACCESSORY_PROTOCOL
604 case SYS_IAP_PERIODIC:
605 iap_periodic();
606 return SYS_IAP_PERIODIC;
607 case SYS_IAP_HANDLEPKT:
608 iap_handlepkt();
609 return SYS_IAP_HANDLEPKT;
610 #endif
611 #if CONFIG_PLATFORM & (PLATFORM_ANDROID|PLATFORM_MAEMO)
612 /* stop playback if we receive a call */
613 case SYS_CALL_INCOMING:
614 resume = audio_status() == AUDIO_STATUS_PLAY;
615 list_stop_handler();
616 return SYS_CALL_INCOMING;
617 /* resume playback if needed */
618 case SYS_CALL_HUNG_UP:
619 if (resume && playlist_resume() != -1)
621 playlist_start(global_status.resume_index,
622 global_status.resume_offset);
624 resume = false;
625 return SYS_CALL_HUNG_UP;
626 #endif
627 #if (CONFIG_PLATFORM & PLATFORM_HOSTED) && defined(PLATFORM_HAS_VOLUME_CHANGE)
628 case SYS_VOLUME_CHANGED:
630 static bool firstvolume = true;
631 /* kludge: since this events go to the button_queue,
632 * event data is available in the last button data */
633 int volume = button_get_data();
634 DEBUGF("SYS_VOLUME_CHANGED: %d\n", volume);
635 if (global_settings.volume != volume) {
636 global_settings.volume = volume;
637 if (firstvolume) {
638 setvol();
639 firstvolume = false;
642 return 0;
644 #endif
645 #ifdef HAVE_MULTIMEDIA_KEYS
646 /* multimedia keys on keyboards, headsets */
647 case BUTTON_MULTIMEDIA_PLAYPAUSE:
649 int status = audio_status();
650 if (status & AUDIO_STATUS_PLAY)
652 if (status & AUDIO_STATUS_PAUSE)
653 unpause_action(true, true);
654 else
655 pause_action(true, true);
657 else
658 if (playlist_resume() != -1)
660 playlist_start(global_status.resume_index,
661 global_status.resume_offset);
663 return event;
665 case BUTTON_MULTIMEDIA_NEXT:
666 audio_next();
667 return event;
668 case BUTTON_MULTIMEDIA_PREV:
669 audio_prev();
670 return event;
671 case BUTTON_MULTIMEDIA_STOP:
672 list_stop_handler();
673 return event;
674 case BUTTON_MULTIMEDIA_REW:
675 case BUTTON_MULTIMEDIA_FFWD:
676 /* not supported yet, needs to be done in the WPS */
677 return 0;
678 #endif
680 return 0;
683 long default_event_handler(long event)
685 return default_event_handler_ex(event, NULL, NULL);
688 int show_logo( void )
690 #ifdef HAVE_LCD_BITMAP
691 char version[32];
692 int font_h, font_w;
694 snprintf(version, sizeof(version), "Ver. %s", rbversion);
696 lcd_clear_display();
697 #if defined(SANSA_CLIP) || defined(SANSA_CLIPV2) || defined(SANSA_CLIPPLUS)
698 /* display the logo in the blue area of the screen */
699 lcd_setfont(FONT_SYSFIXED);
700 lcd_getstringsize((unsigned char *)"A", &font_w, &font_h);
701 lcd_putsxy((LCD_WIDTH/2) - ((strlen(version)*font_w)/2),
702 0, (unsigned char *)version);
703 lcd_bmp(&bm_rockboxlogo, (LCD_WIDTH - BMPWIDTH_rockboxlogo) / 2, 16);
704 #else
705 lcd_bmp(&bm_rockboxlogo, (LCD_WIDTH - BMPWIDTH_rockboxlogo) / 2, 10);
706 lcd_setfont(FONT_SYSFIXED);
707 lcd_getstringsize((unsigned char *)"A", &font_w, &font_h);
708 lcd_putsxy((LCD_WIDTH/2) - ((strlen(version)*font_w)/2),
709 LCD_HEIGHT-font_h, (unsigned char *)version);
710 #endif
711 lcd_setfont(FONT_UI);
713 #else
714 char *rockbox = " ROCKbox!";
716 lcd_clear_display();
717 lcd_double_height(true);
718 lcd_puts(0, 0, rockbox);
719 lcd_puts_scroll(0, 1, rbversion);
720 #endif
721 lcd_update();
723 #ifdef HAVE_REMOTE_LCD
724 lcd_remote_clear_display();
725 lcd_remote_bmp(&bm_remote_rockboxlogo, 0, 10);
726 lcd_remote_setfont(FONT_SYSFIXED);
727 lcd_remote_getstringsize((unsigned char *)"A", &font_w, &font_h);
728 lcd_remote_putsxy((LCD_REMOTE_WIDTH/2) - ((strlen(version)*font_w)/2),
729 LCD_REMOTE_HEIGHT-font_h, (unsigned char *)version);
730 lcd_remote_setfont(FONT_UI);
731 lcd_remote_update();
732 #endif
734 return 0;
737 #ifdef BOOTFILE
738 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF) || defined(HAVE_HOTSWAP_STORAGE_AS_MAIN)
740 memorize/compare details about the BOOTFILE
741 we don't use dircache because it may not be up to date after
742 USB disconnect (scanning in the background)
744 void check_bootfile(bool do_rolo)
746 static unsigned short wrtdate = 0;
747 static unsigned short wrttime = 0;
748 DIR* dir = NULL;
749 struct dirent* entry = NULL;
751 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
752 dir = opendir(BOOTDIR);
754 if(!dir) return; /* do we want an error splash? */
756 /* loop all files in BOOTDIR */
757 while(0 != (entry = readdir(dir)))
759 if(!strcasecmp(entry->d_name, BOOTFILE))
761 struct dirinfo info = dir_get_info(dir, entry);
762 /* found the bootfile */
763 if(wrtdate && do_rolo)
765 if((info.wrtdate != wrtdate) ||
766 (info.wrttime != wrttime))
768 static const char *lines[] = { ID2P(LANG_BOOT_CHANGED),
769 ID2P(LANG_REBOOT_NOW) };
770 static const struct text_message message={ lines, 2 };
771 button_clear_queue(); /* Empty the keyboard buffer */
772 if(gui_syncyesno_run(&message, NULL, NULL) == YESNO_YES)
774 audio_hard_stop();
775 rolo_load(BOOTDIR "/" BOOTFILE);
779 wrtdate = info.wrtdate;
780 wrttime = info.wrttime;
783 closedir(dir);
785 #endif
786 #endif
788 /* check range, set volume and save settings */
789 void setvol(void)
791 const int min_vol = sound_min(SOUND_VOLUME);
792 const int max_vol = sound_max(SOUND_VOLUME);
793 if (global_settings.volume < min_vol)
794 global_settings.volume = min_vol;
795 if (global_settings.volume > max_vol)
796 global_settings.volume = max_vol;
797 sound_set_volume(global_settings.volume);
798 global_status.last_volume_change = current_tick;
799 settings_save();
802 char* strrsplt(char* str, int c)
804 char* s = strrchr(str, c);
806 if (s != NULL)
808 *s++ = '\0';
810 else
812 s = str;
815 return s;
819 * removes the extension of filename (if it doesn't start with a .)
820 * puts the result in buffer
822 char *strip_extension(char* buffer, int buffer_size, const char *filename)
824 char *dot = strrchr(filename, '.');
825 int len;
827 if (buffer_size <= 0)
829 return NULL;
832 buffer_size--; /* Make room for end nil */
834 if (dot != 0 && filename[0] != '.')
836 len = dot - filename;
837 len = MIN(len, buffer_size);
839 else
841 len = buffer_size;
844 strlcpy(buffer, filename, len + 1);
846 return buffer;
849 #if CONFIG_CODEC == SWCODEC
850 /* Play a standard sound */
851 void system_sound_play(enum system_sound sound)
853 static const struct beep_params
855 int *setting;
856 unsigned short frequency;
857 unsigned short duration;
858 unsigned short amplitude;
859 } beep_params[] =
861 [SOUND_KEYCLICK] =
862 { &global_settings.keyclick,
863 4000, KEYCLICK_DURATION, 2500 },
864 [SOUND_TRACK_SKIP] =
865 { &global_settings.beep,
866 2000, 100, 2500 },
867 [SOUND_TRACK_NO_MORE] =
868 { &global_settings.beep,
869 1000, 100, 1500 },
872 const struct beep_params *params = &beep_params[sound];
874 if (*params->setting)
876 beep_play(params->frequency, params->duration,
877 params->amplitude * *params->setting);
881 static keyclick_callback keyclick_current_callback = NULL;
882 static void* keyclick_data = NULL;
883 void keyclick_set_callback(keyclick_callback cb, void* data)
885 keyclick_current_callback = cb;
886 keyclick_data = data;
889 /* Produce keyclick based upon button and global settings */
890 void keyclick_click(int action)
892 int button;
893 static long last_button = BUTTON_NONE;
894 bool do_beep = false;
896 get_action_statuscode(&button);
897 /* Settings filters */
898 if (
899 #ifdef HAVE_HARDWARE_CLICK
900 (global_settings.keyclick || global_settings.keyclick_hardware)
901 #else
902 global_settings.keyclick
903 #endif
906 if (global_settings.keyclick_repeats || !(button & BUTTON_REPEAT))
908 /* Button filters */
909 if (button != BUTTON_NONE && !(button & BUTTON_REL)
910 && !(button & (SYS_EVENT|BUTTON_MULTIMEDIA)) )
912 do_beep = true;
915 else if ((button & BUTTON_REPEAT) && (last_button == BUTTON_NONE))
917 do_beep = true;
919 #ifdef HAVE_SCROLLWHEEL
920 else if (button & (BUTTON_SCROLL_BACK | BUTTON_SCROLL_FWD))
922 do_beep = true;
924 #endif
926 if (button&BUTTON_REPEAT)
927 last_button = button;
928 else
929 last_button = BUTTON_NONE;
931 if (do_beep && keyclick_current_callback)
932 do_beep = keyclick_current_callback(action, keyclick_data);
933 keyclick_current_callback = NULL;
935 if (do_beep)
937 #ifdef HAVE_HARDWARE_CLICK
938 if (global_settings.keyclick)
940 system_sound_play(SOUND_KEYCLICK);
942 if (global_settings.keyclick_hardware)
944 #if !defined(SIMULATOR)
945 piezo_button_beep(false, false);
946 #endif
948 #else
949 system_sound_play(SOUND_KEYCLICK);
950 #endif
953 #endif /* CONFIG_CODEC == SWCODEC */
955 #endif /* !defined(__PCTOOL__) */
957 /* Read (up to) a line of text from fd into buffer and return number of bytes
958 * read (which may be larger than the number of bytes stored in buffer). If
959 * an error occurs, -1 is returned (and buffer contains whatever could be
960 * read). A line is terminated by a LF char. Neither LF nor CR chars are
961 * stored in buffer.
963 int read_line(int fd, char* buffer, int buffer_size)
965 int count = 0;
966 int num_read = 0;
968 errno = 0;
970 while (count < buffer_size)
972 unsigned char c;
974 if (1 != read(fd, &c, 1))
975 break;
977 num_read++;
979 if ( c == '\n' )
980 break;
982 if ( c == '\r' )
983 continue;
985 buffer[count++] = c;
988 buffer[MIN(count, buffer_size - 1)] = 0;
990 return errno ? -1 : num_read;
994 char* skip_whitespace(char* const str)
996 char *s = str;
998 while (isspace(*s))
999 s++;
1001 return s;
1004 /* Format time into buf.
1006 * buf - buffer to format to.
1007 * buf_size - size of buffer.
1008 * t - time to format, in milliseconds.
1010 void format_time(char* buf, int buf_size, long t)
1012 int const time = abs(t / 1000);
1013 int const hours = time / 3600;
1014 int const minutes = time / 60 - hours * 60;
1015 int const seconds = time % 60;
1016 const char * const sign = &"-"[t < 0 ? 0 : 1];
1018 if ( hours == 0 )
1020 snprintf(buf, buf_size, "%s%d:%02d", sign, minutes, seconds);
1022 else
1024 snprintf(buf, buf_size, "%s%d:%02d:%02d", sign, hours, minutes,
1025 seconds);
1030 /** Open a UTF-8 file and set file descriptor to first byte after BOM.
1031 * If no BOM is present this behaves like open().
1032 * If the file is opened for writing and O_TRUNC is set, write a BOM to
1033 * the opened file and leave the file pointer set after the BOM.
1036 int open_utf8(const char* pathname, int flags)
1038 int fd;
1039 unsigned char bom[BOM_UTF_8_SIZE];
1041 fd = open(pathname, flags, 0666);
1042 if(fd < 0)
1043 return fd;
1045 if(flags & (O_TRUNC | O_WRONLY))
1047 write(fd, BOM_UTF_8, BOM_UTF_8_SIZE);
1049 else
1051 read(fd, bom, BOM_UTF_8_SIZE);
1052 /* check for BOM */
1053 if(memcmp(bom, BOM_UTF_8, BOM_UTF_8_SIZE))
1054 lseek(fd, 0, SEEK_SET);
1056 return fd;
1060 #ifdef HAVE_LCD_COLOR
1062 * Helper function to convert a string of 6 hex digits to a native colour
1065 static int hex2dec(int c)
1067 return (((c) >= '0' && ((c) <= '9')) ? (c) - '0' :
1068 (toupper(c)) - 'A' + 10);
1071 int hex_to_rgb(const char* hex, int* color)
1073 int red, green, blue;
1074 int i = 0;
1076 while ((i < 6) && (isxdigit(hex[i])))
1077 i++;
1079 if (i < 6)
1080 return -1;
1082 red = (hex2dec(hex[0]) << 4) | hex2dec(hex[1]);
1083 green = (hex2dec(hex[2]) << 4) | hex2dec(hex[3]);
1084 blue = (hex2dec(hex[4]) << 4) | hex2dec(hex[5]);
1086 *color = LCD_RGBPACK(red,green,blue);
1088 return 0;
1090 #endif /* HAVE_LCD_COLOR */
1092 #ifdef HAVE_LCD_BITMAP
1093 /* '0'-'3' are ASCII 0x30 to 0x33 */
1094 #define is0123(x) (((x) & 0xfc) == 0x30)
1095 #if !defined(__PCTOOL__) || defined(CHECKWPS)
1096 bool parse_color(enum screen_type screen, char *text, int *value)
1098 (void)text; (void)value; /* silence warnings on mono bitmap */
1099 (void)screen;
1101 #ifdef HAVE_LCD_COLOR
1102 if (screens[screen].depth > 2)
1104 if (hex_to_rgb(text, value) < 0)
1105 return false;
1106 else
1107 return true;
1109 #endif
1111 #if LCD_DEPTH == 2 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 2)
1112 if (screens[screen].depth == 2)
1114 if (text[1] != '\0' || !is0123(*text))
1115 return false;
1116 *value = *text - '0';
1117 return true;
1119 #endif
1120 return false;
1122 #endif /* !defined(__PCTOOL__) || defined(CHECKWPS) */
1124 /* only used in USB HID and set_time screen */
1125 #if defined(USB_ENABLE_HID) || (CONFIG_RTC != 0)
1126 int clamp_value_wrap(int value, int max, int min)
1128 if (value > max)
1129 return min;
1130 if (value < min)
1131 return max;
1132 return value;
1134 #endif
1135 #endif
1136 #define MAX_ACTIVITY_DEPTH 12
1137 static enum current_activity
1138 current_activity[MAX_ACTIVITY_DEPTH] = {ACTIVITY_UNKNOWN};
1139 static int current_activity_top = 0;
1140 void push_current_activity(enum current_activity screen)
1142 current_activity[current_activity_top++] = screen;
1143 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
1144 FOR_NB_SCREENS(i)
1145 skinlist_set_cfg(i, NULL);
1146 #endif
1148 void pop_current_activity(void)
1150 current_activity_top--;
1151 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
1152 FOR_NB_SCREENS(i)
1153 skinlist_set_cfg(i, NULL);
1154 #endif
1156 enum current_activity get_current_activity(void)
1158 return current_activity[current_activity_top?current_activity_top-1:0];