fix the last of the error
[maemo-rb.git] / apps / misc.c
blob66273b6cfaf06bf40abce02be8fbbfa0367fabd8
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 /* units used with output_dyn_value */
98 const unsigned char * const byte_units[] =
100 ID2P(LANG_BYTE),
101 ID2P(LANG_KILOBYTE),
102 ID2P(LANG_MEGABYTE),
103 ID2P(LANG_GIGABYTE)
106 const unsigned char * const * const kbyte_units = &byte_units[1];
108 /* Format a large-range value for output, using the appropriate unit so that
109 * the displayed value is in the range 1 <= display < 1000 (1024 for "binary"
110 * units) if possible, and 3 significant digits are shown. If a buffer is
111 * given, the result is snprintf()'d into that buffer, otherwise the result is
112 * voiced.*/
113 char *output_dyn_value(char *buf, int buf_size, int value,
114 const unsigned char * const *units, bool bin_scale)
116 int scale = bin_scale ? 1024 : 1000;
117 int fraction = 0;
118 int unit_no = 0;
119 char tbuf[5];
121 while (value >= scale)
123 fraction = value % scale;
124 value /= scale;
125 unit_no++;
127 if (bin_scale)
128 fraction = fraction * 1000 / 1024;
130 if (value >= 100 || !unit_no)
131 tbuf[0] = '\0';
132 else if (value >= 10)
133 snprintf(tbuf, sizeof(tbuf), "%01d", fraction / 100);
134 else
135 snprintf(tbuf, sizeof(tbuf), "%02d", fraction / 10);
137 if (buf)
139 if (*tbuf)
140 snprintf(buf, buf_size, "%d%s%s%s", value, str(LANG_POINT),
141 tbuf, P2STR(units[unit_no]));
142 else
143 snprintf(buf, buf_size, "%d%s", value, P2STR(units[unit_no]));
145 else
147 talk_fractional(tbuf, value, P2ID(units[unit_no]));
149 return buf;
152 /* Ask the user if they really want to erase the current dynamic playlist
153 * returns true if the playlist should be replaced */
154 bool warn_on_pl_erase(void)
156 if (global_settings.warnon_erase_dynplaylist &&
157 !global_settings.party_mode &&
158 playlist_modified(NULL))
160 static const char *lines[] =
161 {ID2P(LANG_WARN_ERASEDYNPLAYLIST_PROMPT)};
162 static const struct text_message message={lines, 1};
164 return (gui_syncyesno_run(&message, NULL, NULL) == YESNO_YES);
166 else
167 return true;
171 /* Performance optimized version of the read_line() (see below) function. */
172 int fast_readline(int fd, char *buf, int buf_size, void *parameters,
173 int (*callback)(int n, char *buf, void *parameters))
175 char *p, *next;
176 int rc, pos = 0;
177 int count = 0;
179 while ( 1 )
181 next = NULL;
183 rc = read(fd, &buf[pos], buf_size - pos - 1);
184 if (rc >= 0)
185 buf[pos+rc] = '\0';
187 if ( (p = strchr(buf, '\n')) != NULL)
189 *p = '\0';
190 next = ++p;
193 rc = callback(count, buf, parameters);
194 if (rc < 0)
195 return rc;
197 count++;
198 if (next)
200 pos = buf_size - ((long)next - (long)buf) - 1;
201 memmove(buf, next, pos);
203 else
204 break ;
207 return 0;
210 /* parse a line from a configuration file. the line format is:
212 name: value
214 Any whitespace before setting name or value (after ':') is ignored.
215 A # as first non-whitespace character discards the whole line.
216 Function sets pointers to null-terminated setting name and value.
217 Returns false if no valid config entry was found.
220 bool settings_parseline(char* line, char** name, char** value)
222 char* ptr;
224 line = skip_whitespace(line);
226 if ( *line == '#' )
227 return false;
229 ptr = strchr(line, ':');
230 if ( !ptr )
231 return false;
233 *name = line;
234 *ptr = 0;
235 ptr++;
236 ptr = skip_whitespace(ptr);
237 *value = ptr;
238 return true;
241 static void system_flush(void)
243 playlist_shutdown();
244 tree_flush();
245 call_storage_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
248 static void system_restore(void)
250 tree_restore();
253 static bool clean_shutdown(void (*callback)(void *), void *parameter)
255 long msg_id = -1;
256 int i;
258 scrobbler_poweroff();
260 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
261 if(!charger_inserted())
262 #endif
264 bool batt_safe = battery_level_safe();
265 #if CONFIG_CODEC != SWCODEC || defined(HAVE_RECORDING)
266 int audio_stat = audio_status();
267 #endif
269 FOR_NB_SCREENS(i)
271 screens[i].clear_display();
272 screens[i].update();
275 if (batt_safe)
277 #ifdef HAVE_TAGCACHE
278 if (!tagcache_prepare_shutdown())
280 cancel_shutdown();
281 splash(HZ, ID2P(LANG_TAGCACHE_BUSY));
282 return false;
284 #endif
285 if (battery_level() > 10)
286 splash(0, str(LANG_SHUTTINGDOWN));
287 else
289 msg_id = LANG_WARNING_BATTERY_LOW;
290 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_LOW),
291 str(LANG_SHUTTINGDOWN));
294 else
296 msg_id = LANG_WARNING_BATTERY_EMPTY;
297 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_EMPTY),
298 str(LANG_SHUTTINGDOWN));
300 #if CONFIG_CODEC != SWCODEC
301 if (global_settings.fade_on_stop
302 && (audio_stat & AUDIO_STATUS_PLAY))
304 fade(false, false);
306 #endif
308 if (batt_safe) /* do not save on critical battery */
310 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
311 if (audio_stat & AUDIO_STATUS_RECORD)
313 rec_command(RECORDING_CMD_STOP);
314 /* wait for stop to complete */
315 while (audio_status() & AUDIO_STATUS_RECORD)
316 sleep(1);
318 #endif
319 bookmark_autobookmark(false);
321 /* audio_stop_recording == audio_stop for HWCODEC */
322 audio_stop();
324 if (callback != NULL)
325 callback(parameter);
327 #if CONFIG_CODEC != SWCODEC
328 /* wait for audio_stop or audio_stop_recording to complete */
329 while (audio_status())
330 sleep(1);
331 #endif
333 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
334 audio_close_recording();
335 #endif
337 if(global_settings.talk_menu)
339 bool enqueue = false;
340 if(msg_id != -1)
342 talk_id(msg_id, enqueue);
343 enqueue = true;
345 talk_id(LANG_SHUTTINGDOWN, enqueue);
346 #if CONFIG_CODEC == SWCODEC
347 voice_wait();
348 #endif
351 system_flush();
352 #ifdef HAVE_EEPROM_SETTINGS
353 if (firmware_settings.initialized)
355 firmware_settings.disk_clean = true;
356 firmware_settings.bl_version = 0;
357 eeprom_settings_store();
359 #endif
361 #ifdef HAVE_DIRCACHE
362 else
363 dircache_disable();
364 #endif
366 shutdown_hw();
368 return false;
371 bool list_stop_handler(void)
373 bool ret = false;
375 #if CONFIG_TUNER
376 radio_stop();
377 #endif
379 /* Stop the music if it is playing */
380 if(audio_status())
382 if (!global_settings.party_mode)
384 #if CONFIG_CODEC != SWCODEC
385 if (global_settings.fade_on_stop)
386 fade(false, false);
387 #endif
388 bookmark_autobookmark(true);
389 audio_stop();
390 ret = true; /* bookmarking can make a refresh necessary */
393 #if CONFIG_CHARGING
394 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
395 else
397 if (charger_inserted())
398 charging_splash();
399 else
400 shutdown_screen(); /* won't return if shutdown actually happens */
402 ret = true; /* screen is dirty, caller needs to refresh */
404 #endif
405 #ifndef HAVE_POWEROFF_WHILE_CHARGING
407 static long last_off = 0;
409 if (TIME_BEFORE(current_tick, last_off + HZ/2))
411 if (charger_inserted())
413 charging_splash();
414 ret = true; /* screen is dirty, caller needs to refresh */
417 last_off = current_tick;
419 #endif
420 #endif /* CONFIG_CHARGING */
421 return ret;
424 #if CONFIG_CHARGING
425 static bool waiting_to_resume_play = false;
426 static long play_resume_tick;
428 static void car_adapter_mode_processing(bool inserted)
430 if (global_settings.car_adapter_mode)
432 if(inserted)
435 * Just got plugged in, delay & resume if we were playing
437 if (audio_status() & AUDIO_STATUS_PAUSE)
439 /* delay resume a bit while the engine is cranking */
440 play_resume_tick = current_tick + HZ*5;
441 waiting_to_resume_play = true;
444 else
447 * Just got unplugged, pause if playing
449 if ((audio_status() & AUDIO_STATUS_PLAY) &&
450 !(audio_status() & AUDIO_STATUS_PAUSE))
452 pause_action(true, true);
454 waiting_to_resume_play = false;
459 static void car_adapter_tick(void)
461 if (waiting_to_resume_play)
463 if (TIME_AFTER(current_tick, play_resume_tick))
465 if (audio_status() & AUDIO_STATUS_PAUSE)
467 queue_broadcast(SYS_CAR_ADAPTER_RESUME, 0);
469 waiting_to_resume_play = false;
474 void car_adapter_mode_init(void)
476 tick_add_task(car_adapter_tick);
478 #endif
480 #ifdef HAVE_HEADPHONE_DETECTION
481 static void unplug_change(bool inserted)
483 static bool headphone_caused_pause = false;
485 if (global_settings.unplug_mode)
487 int audio_stat = audio_status();
488 if (inserted)
490 backlight_on();
491 if ((audio_stat & AUDIO_STATUS_PLAY) &&
492 headphone_caused_pause &&
493 global_settings.unplug_mode > 1 )
494 unpause_action(true, true);
495 headphone_caused_pause = false;
496 } else {
497 if ((audio_stat & AUDIO_STATUS_PLAY) &&
498 !(audio_stat & AUDIO_STATUS_PAUSE))
500 headphone_caused_pause = true;
501 pause_action(false, false);
506 #endif
508 long default_event_handler_ex(long event, void (*callback)(void *), void *parameter)
510 #if CONFIG_PLATFORM & (PLATFORM_ANDROID|PLATFORM_MAEMO)
511 static bool resume = false;
512 #endif
514 switch(event)
516 case SYS_BATTERY_UPDATE:
517 if(global_settings.talk_battery_level)
519 talk_ids(true, VOICE_PAUSE, VOICE_PAUSE,
520 LANG_BATTERY_TIME,
521 TALK_ID(battery_level(), UNIT_PERCENT),
522 VOICE_PAUSE);
523 talk_force_enqueue_next();
525 break;
526 case SYS_USB_CONNECTED:
527 if (callback != NULL)
528 callback(parameter);
529 #if (CONFIG_STORAGE & STORAGE_MMC) && (defined(ARCHOS_ONDIOSP) || defined(ARCHOS_ONDIOFM))
530 if (!mmc_touched() ||
531 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
532 #endif
534 system_flush();
535 #ifdef BOOTFILE
536 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
537 check_bootfile(false); /* gets initial size */
538 #endif
539 #endif
540 gui_usb_screen_run(false);
541 #ifdef BOOTFILE
542 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
543 check_bootfile(true);
544 #endif
545 #endif
546 system_restore();
548 return SYS_USB_CONNECTED;
550 case SYS_POWEROFF:
551 if (!clean_shutdown(callback, parameter))
552 return SYS_POWEROFF;
553 break;
554 #if CONFIG_CHARGING
555 case SYS_CHARGER_CONNECTED:
556 car_adapter_mode_processing(true);
557 return SYS_CHARGER_CONNECTED;
559 case SYS_CHARGER_DISCONNECTED:
560 car_adapter_mode_processing(false);
561 /*reset rockbox battery runtime*/
562 global_status.runtime = 0;
563 return SYS_CHARGER_DISCONNECTED;
565 case SYS_CAR_ADAPTER_RESUME:
566 unpause_action(true, true);
567 return SYS_CAR_ADAPTER_RESUME;
568 #endif
569 #ifdef HAVE_HOTSWAP_STORAGE_AS_MAIN
570 case SYS_FS_CHANGED:
572 /* simple sanity: assume rockbox is on the first hotswappable
573 * driver, abort out if that one isn't inserted */
574 int i;
575 for (i = 0; i < NUM_DRIVES; i++)
577 if (storage_removable(i) && !storage_present(i))
578 return SYS_FS_CHANGED;
580 system_flush();
581 check_bootfile(true); /* state gotten in main.c:init() */
582 system_restore();
584 return SYS_FS_CHANGED;
585 #endif
586 #ifdef HAVE_HEADPHONE_DETECTION
587 case SYS_PHONE_PLUGGED:
588 unplug_change(true);
589 return SYS_PHONE_PLUGGED;
591 case SYS_PHONE_UNPLUGGED:
592 unplug_change(false);
593 return SYS_PHONE_UNPLUGGED;
594 #endif
595 #ifdef IPOD_ACCESSORY_PROTOCOL
596 case SYS_IAP_PERIODIC:
597 iap_periodic();
598 return SYS_IAP_PERIODIC;
599 case SYS_IAP_HANDLEPKT:
600 iap_handlepkt();
601 return SYS_IAP_HANDLEPKT;
602 #endif
603 #if CONFIG_PLATFORM & (PLATFORM_ANDROID|PLATFORM_MAEMO)
604 /* stop playback if we receive a call */
605 case SYS_CALL_INCOMING:
606 resume = audio_status() == AUDIO_STATUS_PLAY;
607 list_stop_handler();
608 return SYS_CALL_INCOMING;
609 /* resume playback if needed */
610 case SYS_CALL_HUNG_UP:
611 if (resume && playlist_resume() != -1)
613 playlist_start(global_status.resume_index,
614 global_status.resume_offset);
616 resume = false;
617 return SYS_CALL_HUNG_UP;
618 #endif
619 #if (CONFIG_PLATFORM & PLATFORM_HOSTED) && defined(PLATFORM_HAS_VOLUME_CHANGE)
620 case SYS_VOLUME_CHANGED:
622 static bool firstvolume = true;
623 /* kludge: since this events go to the button_queue,
624 * event data is available in the last button data */
625 int volume = button_get_data();
626 DEBUGF("SYS_VOLUME_CHANGED: %d\n", volume);
627 if (global_settings.volume != volume) {
628 global_settings.volume = volume;
629 if (firstvolume) {
630 setvol();
631 firstvolume = false;
634 return 0;
636 #endif
637 #ifdef HAVE_MULTIMEDIA_KEYS
638 /* multimedia keys on keyboards, headsets */
639 case BUTTON_MULTIMEDIA_PLAYPAUSE:
641 int status = audio_status();
642 if (status & AUDIO_STATUS_PLAY)
644 if (status & AUDIO_STATUS_PAUSE)
645 unpause_action(true, true);
646 else
647 pause_action(true, true);
649 else
650 if (playlist_resume() != -1)
652 playlist_start(global_status.resume_index,
653 global_status.resume_offset);
655 return event;
657 case BUTTON_MULTIMEDIA_NEXT:
658 audio_next();
659 return event;
660 case BUTTON_MULTIMEDIA_PREV:
661 audio_prev();
662 return event;
663 case BUTTON_MULTIMEDIA_STOP:
664 list_stop_handler();
665 return event;
666 case BUTTON_MULTIMEDIA_REW:
667 case BUTTON_MULTIMEDIA_FFWD:
668 /* not supported yet, needs to be done in the WPS */
669 return 0;
670 #endif
672 return 0;
675 long default_event_handler(long event)
677 return default_event_handler_ex(event, NULL, NULL);
680 int show_logo( void )
682 #ifdef HAVE_LCD_BITMAP
683 char version[32];
684 int font_h, font_w;
686 snprintf(version, sizeof(version), "Ver. %s", rbversion);
688 lcd_clear_display();
689 #if defined(SANSA_CLIP) || defined(SANSA_CLIPV2) || defined(SANSA_CLIPPLUS)
690 /* display the logo in the blue area of the screen */
691 lcd_setfont(FONT_SYSFIXED);
692 lcd_getstringsize((unsigned char *)"A", &font_w, &font_h);
693 lcd_putsxy((LCD_WIDTH/2) - ((strlen(version)*font_w)/2),
694 0, (unsigned char *)version);
695 lcd_bitmap(rockboxlogo, (LCD_WIDTH - BMPWIDTH_rockboxlogo) / 2, 16,
696 BMPWIDTH_rockboxlogo, BMPHEIGHT_rockboxlogo);
697 #else
698 lcd_bitmap(rockboxlogo, (LCD_WIDTH - BMPWIDTH_rockboxlogo) / 2, 10,
699 BMPWIDTH_rockboxlogo, BMPHEIGHT_rockboxlogo);
700 lcd_setfont(FONT_SYSFIXED);
701 lcd_getstringsize((unsigned char *)"A", &font_w, &font_h);
702 lcd_putsxy((LCD_WIDTH/2) - ((strlen(version)*font_w)/2),
703 LCD_HEIGHT-font_h, (unsigned char *)version);
704 #endif
705 lcd_setfont(FONT_UI);
707 #else
708 char *rockbox = " ROCKbox!";
710 lcd_clear_display();
711 lcd_double_height(true);
712 lcd_puts(0, 0, rockbox);
713 lcd_puts_scroll(0, 1, rbversion);
714 #endif
715 lcd_update();
717 #ifdef HAVE_REMOTE_LCD
718 lcd_remote_clear_display();
719 lcd_remote_bitmap(remote_rockboxlogo, 0, 10, BMPWIDTH_remote_rockboxlogo,
720 BMPHEIGHT_remote_rockboxlogo);
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 /* Settings filters */
880 if (global_settings.keyclick &&
881 (global_settings.keyclick_repeats || !(button & BUTTON_REPEAT)))
883 /* Button filters */
884 if (button != BUTTON_NONE && !(button & BUTTON_REL)
885 && !(button & (SYS_EVENT|BUTTON_MULTIMEDIA)) )
887 system_sound_play(SOUND_KEYCLICK);
891 #endif /* CONFIG_CODEC == SWCODEC */
893 #endif /* !defined(__PCTOOL__) */
895 /* Read (up to) a line of text from fd into buffer and return number of bytes
896 * read (which may be larger than the number of bytes stored in buffer). If
897 * an error occurs, -1 is returned (and buffer contains whatever could be
898 * read). A line is terminated by a LF char. Neither LF nor CR chars are
899 * stored in buffer.
901 int read_line(int fd, char* buffer, int buffer_size)
903 int count = 0;
904 int num_read = 0;
906 errno = 0;
908 while (count < buffer_size)
910 unsigned char c;
912 if (1 != read(fd, &c, 1))
913 break;
915 num_read++;
917 if ( c == '\n' )
918 break;
920 if ( c == '\r' )
921 continue;
923 buffer[count++] = c;
926 buffer[MIN(count, buffer_size - 1)] = 0;
928 return errno ? -1 : num_read;
932 char* skip_whitespace(char* const str)
934 char *s = str;
936 while (isspace(*s))
937 s++;
939 return s;
942 /* Format time into buf.
944 * buf - buffer to format to.
945 * buf_size - size of buffer.
946 * t - time to format, in milliseconds.
948 void format_time(char* buf, int buf_size, long t)
950 int const time = abs(t / 1000);
951 int const hours = time / 3600;
952 int const minutes = time / 60 - hours * 60;
953 int const seconds = time % 60;
954 const char * const sign = &"-"[t < 0 ? 0 : 1];
956 if ( hours == 0 )
958 snprintf(buf, buf_size, "%s%d:%02d", sign, minutes, seconds);
960 else
962 snprintf(buf, buf_size, "%s%d:%02d:%02d", sign, hours, minutes,
963 seconds);
968 /** Open a UTF-8 file and set file descriptor to first byte after BOM.
969 * If no BOM is present this behaves like open().
970 * If the file is opened for writing and O_TRUNC is set, write a BOM to
971 * the opened file and leave the file pointer set after the BOM.
973 #define BOM "\xef\xbb\xbf"
974 #define BOM_SIZE 3
976 int open_utf8(const char* pathname, int flags)
978 int fd;
979 unsigned char bom[BOM_SIZE];
981 fd = open(pathname, flags, 0666);
982 if(fd < 0)
983 return fd;
985 if(flags & (O_TRUNC | O_WRONLY))
987 write(fd, BOM, BOM_SIZE);
989 else
991 read(fd, bom, BOM_SIZE);
992 /* check for BOM */
993 if(memcmp(bom, BOM, BOM_SIZE))
994 lseek(fd, 0, SEEK_SET);
996 return fd;
1000 #ifdef HAVE_LCD_COLOR
1002 * Helper function to convert a string of 6 hex digits to a native colour
1005 static int hex2dec(int c)
1007 return (((c) >= '0' && ((c) <= '9')) ? (c) - '0' :
1008 (toupper(c)) - 'A' + 10);
1011 int hex_to_rgb(const char* hex, int* color)
1013 int red, green, blue;
1014 int i = 0;
1016 while ((i < 6) && (isxdigit(hex[i])))
1017 i++;
1019 if (i < 6)
1020 return -1;
1022 red = (hex2dec(hex[0]) << 4) | hex2dec(hex[1]);
1023 green = (hex2dec(hex[2]) << 4) | hex2dec(hex[3]);
1024 blue = (hex2dec(hex[4]) << 4) | hex2dec(hex[5]);
1026 *color = LCD_RGBPACK(red,green,blue);
1028 return 0;
1030 #endif /* HAVE_LCD_COLOR */
1032 #ifdef HAVE_LCD_BITMAP
1033 /* '0'-'3' are ASCII 0x30 to 0x33 */
1034 #define is0123(x) (((x) & 0xfc) == 0x30)
1035 #if !defined(__PCTOOL__) || defined(CHECKWPS)
1036 bool parse_color(enum screen_type screen, char *text, int *value)
1038 (void)text; (void)value; /* silence warnings on mono bitmap */
1039 (void)screen;
1041 #ifdef HAVE_LCD_COLOR
1042 if (screens[screen].depth > 2)
1044 if (hex_to_rgb(text, value) < 0)
1045 return false;
1046 else
1047 return true;
1049 #endif
1051 #if LCD_DEPTH == 2 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 2)
1052 if (screens[screen].depth == 2)
1054 if (text[1] != '\0' || !is0123(*text))
1055 return false;
1056 *value = *text - '0';
1057 return true;
1059 #endif
1060 return false;
1062 #endif /* !defined(__PCTOOL__) || defined(CHECKWPS) */
1064 /* only used in USB HID and set_time screen */
1065 #if defined(USB_ENABLE_HID) || (CONFIG_RTC != 0)
1066 int clamp_value_wrap(int value, int max, int min)
1068 if (value > max)
1069 return min;
1070 if (value < min)
1071 return max;
1072 return value;
1074 #endif
1075 #endif
1076 #define MAX_ACTIVITY_DEPTH 12
1077 static enum current_activity
1078 current_activity[MAX_ACTIVITY_DEPTH] = {ACTIVITY_UNKNOWN};
1079 static int current_activity_top = 0;
1080 void push_current_activity(enum current_activity screen)
1082 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
1083 int i;
1084 #endif
1085 current_activity[current_activity_top++] = screen;
1086 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
1087 FOR_NB_SCREENS(i)
1088 skinlist_set_cfg(i, NULL);
1089 #endif
1091 void pop_current_activity(void)
1093 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
1094 int i;
1095 #endif
1096 current_activity_top--;
1097 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
1098 FOR_NB_SCREENS(i)
1099 skinlist_set_cfg(i, NULL);
1100 #endif
1102 enum current_activity get_current_activity(void)
1104 return current_activity[current_activity_top?current_activity_top-1:0];