skin_engine: Automatically create touch regions for skin bars
[maemo-rb.git] / apps / misc.c
blob3bee0772a64bbd9aac18fd688b9c933bb94ea4d6
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 rc = callback(count, buf, parameters);
202 if (rc < 0)
203 return rc;
205 count++;
206 if (next)
208 pos = buf_size - ((long)next - (long)buf) - 1;
209 memmove(buf, next, pos);
211 else
212 break ;
215 return 0;
218 /* parse a line from a configuration file. the line format is:
220 name: value
222 Any whitespace before setting name or value (after ':') is ignored.
223 A # as first non-whitespace character discards the whole line.
224 Function sets pointers to null-terminated setting name and value.
225 Returns false if no valid config entry was found.
228 bool settings_parseline(char* line, char** name, char** value)
230 char* ptr;
232 line = skip_whitespace(line);
234 if ( *line == '#' )
235 return false;
237 ptr = strchr(line, ':');
238 if ( !ptr )
239 return false;
241 *name = line;
242 *ptr = 0;
243 ptr++;
244 ptr = skip_whitespace(ptr);
245 *value = ptr;
246 return true;
249 static void system_flush(void)
251 playlist_shutdown();
252 tree_flush();
253 call_storage_idle_notifys(true); /*doesnt work on usb and shutdown from ata thread */
256 static void system_restore(void)
258 tree_restore();
261 static bool clean_shutdown(void (*callback)(void *), void *parameter)
263 long msg_id = -1;
265 scrobbler_poweroff();
267 #if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
268 if(!charger_inserted())
269 #endif
271 bool batt_safe = battery_level_safe();
272 #if CONFIG_CODEC != SWCODEC || defined(HAVE_RECORDING)
273 int audio_stat = audio_status();
274 #endif
276 FOR_NB_SCREENS(i)
278 screens[i].clear_display();
279 screens[i].update();
282 if (batt_safe)
284 int level;
285 #ifdef HAVE_TAGCACHE
286 if (!tagcache_prepare_shutdown())
288 cancel_shutdown();
289 splash(HZ, ID2P(LANG_TAGCACHE_BUSY));
290 return false;
292 #endif
293 level = battery_level();
294 if (level > 10 || level < 0)
295 splash(0, str(LANG_SHUTTINGDOWN));
296 else
298 msg_id = LANG_WARNING_BATTERY_LOW;
299 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_LOW),
300 str(LANG_SHUTTINGDOWN));
303 else
305 msg_id = LANG_WARNING_BATTERY_EMPTY;
306 splashf(0, "%s %s", str(LANG_WARNING_BATTERY_EMPTY),
307 str(LANG_SHUTTINGDOWN));
309 #if CONFIG_CODEC != SWCODEC
310 if (global_settings.fade_on_stop
311 && (audio_stat & AUDIO_STATUS_PLAY))
313 fade(false, false);
315 #endif
317 if (batt_safe) /* do not save on critical battery */
319 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
320 if (audio_stat & AUDIO_STATUS_RECORD)
322 rec_command(RECORDING_CMD_STOP);
323 /* wait for stop to complete */
324 while (audio_status() & AUDIO_STATUS_RECORD)
325 sleep(1);
327 #endif
328 bookmark_autobookmark(false);
330 /* audio_stop_recording == audio_stop for HWCODEC */
331 audio_stop();
333 if (callback != NULL)
334 callback(parameter);
336 #if CONFIG_CODEC != SWCODEC
337 /* wait for audio_stop or audio_stop_recording to complete */
338 while (audio_status())
339 sleep(1);
340 #endif
342 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
343 audio_close_recording();
344 #endif
346 if(global_settings.talk_menu)
348 bool enqueue = false;
349 if(msg_id != -1)
351 talk_id(msg_id, enqueue);
352 enqueue = true;
354 talk_id(LANG_SHUTTINGDOWN, enqueue);
355 #if CONFIG_CODEC == SWCODEC
356 voice_wait();
357 #endif
360 system_flush();
361 #ifdef HAVE_EEPROM_SETTINGS
362 if (firmware_settings.initialized)
364 firmware_settings.disk_clean = true;
365 firmware_settings.bl_version = 0;
366 eeprom_settings_store();
368 #endif
370 #ifdef HAVE_DIRCACHE
371 else
372 dircache_disable();
373 #endif
375 shutdown_hw();
377 return false;
380 bool list_stop_handler(void)
382 bool ret = false;
384 #if CONFIG_TUNER
385 radio_stop();
386 #endif
388 /* Stop the music if it is playing */
389 if(audio_status())
391 if (!global_settings.party_mode)
393 #if CONFIG_CODEC != SWCODEC
394 if (global_settings.fade_on_stop)
395 fade(false, false);
396 #endif
397 bookmark_autobookmark(true);
398 audio_stop();
399 ret = true; /* bookmarking can make a refresh necessary */
402 #if CONFIG_CHARGING
403 #if (CONFIG_KEYPAD == RECORDER_PAD) && !defined(HAVE_SW_POWEROFF)
404 else
406 if (charger_inserted())
407 charging_splash();
408 else
409 shutdown_screen(); /* won't return if shutdown actually happens */
411 ret = true; /* screen is dirty, caller needs to refresh */
413 #endif
414 #ifndef HAVE_POWEROFF_WHILE_CHARGING
416 static long last_off = 0;
418 if (TIME_BEFORE(current_tick, last_off + HZ/2))
420 if (charger_inserted())
422 charging_splash();
423 ret = true; /* screen is dirty, caller needs to refresh */
426 last_off = current_tick;
428 #endif
429 #endif /* CONFIG_CHARGING */
430 return ret;
433 #if CONFIG_CHARGING
434 static bool waiting_to_resume_play = false;
435 static long play_resume_tick;
437 static void car_adapter_mode_processing(bool inserted)
439 if (global_settings.car_adapter_mode)
441 if(inserted)
444 * Just got plugged in, delay & resume if we were playing
446 if (audio_status() & AUDIO_STATUS_PAUSE)
448 /* delay resume a bit while the engine is cranking */
449 play_resume_tick = current_tick + HZ*5;
450 waiting_to_resume_play = true;
453 else
456 * Just got unplugged, pause if playing
458 if ((audio_status() & AUDIO_STATUS_PLAY) &&
459 !(audio_status() & AUDIO_STATUS_PAUSE))
461 pause_action(true, true);
463 waiting_to_resume_play = false;
468 static void car_adapter_tick(void)
470 if (waiting_to_resume_play)
472 if (TIME_AFTER(current_tick, play_resume_tick))
474 if (audio_status() & AUDIO_STATUS_PAUSE)
476 queue_broadcast(SYS_CAR_ADAPTER_RESUME, 0);
478 waiting_to_resume_play = false;
483 void car_adapter_mode_init(void)
485 tick_add_task(car_adapter_tick);
487 #endif
489 #ifdef HAVE_HEADPHONE_DETECTION
490 static void unplug_change(bool inserted)
492 static bool headphone_caused_pause = false;
494 if (global_settings.unplug_mode)
496 int audio_stat = audio_status();
497 if (inserted)
499 backlight_on();
500 if ((audio_stat & AUDIO_STATUS_PLAY) &&
501 headphone_caused_pause &&
502 global_settings.unplug_mode > 1 )
503 unpause_action(true, true);
504 headphone_caused_pause = false;
505 } else {
506 if ((audio_stat & AUDIO_STATUS_PLAY) &&
507 !(audio_stat & AUDIO_STATUS_PAUSE))
509 headphone_caused_pause = true;
510 pause_action(false, false);
515 #endif
517 long default_event_handler_ex(long event, void (*callback)(void *), void *parameter)
519 #if CONFIG_PLATFORM & (PLATFORM_ANDROID|PLATFORM_MAEMO)
520 static bool resume = false;
521 #endif
523 switch(event)
525 case SYS_BATTERY_UPDATE:
526 if(global_settings.talk_battery_level)
528 talk_ids(true, VOICE_PAUSE, VOICE_PAUSE,
529 LANG_BATTERY_TIME,
530 TALK_ID(battery_level(), UNIT_PERCENT),
531 VOICE_PAUSE);
532 talk_force_enqueue_next();
534 break;
535 case SYS_USB_CONNECTED:
536 if (callback != NULL)
537 callback(parameter);
538 #if (CONFIG_STORAGE & STORAGE_MMC) && (defined(ARCHOS_ONDIOSP) || defined(ARCHOS_ONDIOFM))
539 if (!mmc_touched() ||
540 (mmc_remove_request() == SYS_HOTSWAP_EXTRACTED))
541 #endif
543 system_flush();
544 #ifdef BOOTFILE
545 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
546 check_bootfile(false); /* gets initial size */
547 #endif
548 #endif
549 gui_usb_screen_run(false);
550 #ifdef BOOTFILE
551 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF)
552 check_bootfile(true);
553 #endif
554 #endif
555 system_restore();
557 return SYS_USB_CONNECTED;
559 case SYS_POWEROFF:
560 if (!clean_shutdown(callback, parameter))
561 return SYS_POWEROFF;
562 break;
563 #if CONFIG_CHARGING
564 case SYS_CHARGER_CONNECTED:
565 car_adapter_mode_processing(true);
566 return SYS_CHARGER_CONNECTED;
568 case SYS_CHARGER_DISCONNECTED:
569 car_adapter_mode_processing(false);
570 /*reset rockbox battery runtime*/
571 global_status.runtime = 0;
572 return SYS_CHARGER_DISCONNECTED;
574 case SYS_CAR_ADAPTER_RESUME:
575 unpause_action(true, true);
576 return SYS_CAR_ADAPTER_RESUME;
577 #endif
578 #ifdef HAVE_HOTSWAP_STORAGE_AS_MAIN
579 case SYS_FS_CHANGED:
581 /* simple sanity: assume rockbox is on the first hotswappable
582 * driver, abort out if that one isn't inserted */
583 int i;
584 for (i = 0; i < NUM_DRIVES; i++)
586 if (storage_removable(i) && !storage_present(i))
587 return SYS_FS_CHANGED;
589 system_flush();
590 check_bootfile(true); /* state gotten in main.c:init() */
591 system_restore();
593 return SYS_FS_CHANGED;
594 #endif
595 #ifdef HAVE_HEADPHONE_DETECTION
596 case SYS_PHONE_PLUGGED:
597 unplug_change(true);
598 return SYS_PHONE_PLUGGED;
600 case SYS_PHONE_UNPLUGGED:
601 unplug_change(false);
602 return SYS_PHONE_UNPLUGGED;
603 #endif
604 #ifdef IPOD_ACCESSORY_PROTOCOL
605 case SYS_IAP_PERIODIC:
606 iap_periodic();
607 return SYS_IAP_PERIODIC;
608 case SYS_IAP_HANDLEPKT:
609 iap_handlepkt();
610 return SYS_IAP_HANDLEPKT;
611 #endif
612 #if CONFIG_PLATFORM & (PLATFORM_ANDROID|PLATFORM_MAEMO)
613 /* stop playback if we receive a call */
614 case SYS_CALL_INCOMING:
615 resume = audio_status() == AUDIO_STATUS_PLAY;
616 list_stop_handler();
617 return SYS_CALL_INCOMING;
618 /* resume playback if needed */
619 case SYS_CALL_HUNG_UP:
620 if (resume && playlist_resume() != -1)
622 playlist_start(global_status.resume_index,
623 global_status.resume_offset);
625 resume = false;
626 return SYS_CALL_HUNG_UP;
627 #endif
628 #if (CONFIG_PLATFORM & PLATFORM_HOSTED) && defined(PLATFORM_HAS_VOLUME_CHANGE)
629 case SYS_VOLUME_CHANGED:
631 static bool firstvolume = true;
632 /* kludge: since this events go to the button_queue,
633 * event data is available in the last button data */
634 int volume = button_get_data();
635 DEBUGF("SYS_VOLUME_CHANGED: %d\n", volume);
636 if (global_settings.volume != volume) {
637 global_settings.volume = volume;
638 if (firstvolume) {
639 setvol();
640 firstvolume = false;
643 return 0;
645 #endif
646 #ifdef HAVE_MULTIMEDIA_KEYS
647 /* multimedia keys on keyboards, headsets */
648 case BUTTON_MULTIMEDIA_PLAYPAUSE:
650 int status = audio_status();
651 if (status & AUDIO_STATUS_PLAY)
653 if (status & AUDIO_STATUS_PAUSE)
654 unpause_action(true, true);
655 else
656 pause_action(true, true);
658 else
659 if (playlist_resume() != -1)
661 playlist_start(global_status.resume_index,
662 global_status.resume_offset);
664 return event;
666 case BUTTON_MULTIMEDIA_NEXT:
667 audio_next();
668 return event;
669 case BUTTON_MULTIMEDIA_PREV:
670 audio_prev();
671 return event;
672 case BUTTON_MULTIMEDIA_STOP:
673 list_stop_handler();
674 return event;
675 case BUTTON_MULTIMEDIA_REW:
676 case BUTTON_MULTIMEDIA_FFWD:
677 /* not supported yet, needs to be done in the WPS */
678 return 0;
679 #endif
681 return 0;
684 long default_event_handler(long event)
686 return default_event_handler_ex(event, NULL, NULL);
689 int show_logo( void )
691 #ifdef HAVE_LCD_BITMAP
692 char version[32];
693 int font_h, font_w;
695 snprintf(version, sizeof(version), "Ver. %s", rbversion);
697 lcd_clear_display();
698 #if defined(SANSA_CLIP) || defined(SANSA_CLIPV2) || defined(SANSA_CLIPPLUS)
699 /* display the logo in the blue area of the screen */
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 0, (unsigned char *)version);
704 lcd_bmp(&bm_rockboxlogo, (LCD_WIDTH - BMPWIDTH_rockboxlogo) / 2, 16);
705 #else
706 lcd_bmp(&bm_rockboxlogo, (LCD_WIDTH - BMPWIDTH_rockboxlogo) / 2, 10);
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 LCD_HEIGHT-font_h, (unsigned char *)version);
711 #endif
712 lcd_setfont(FONT_UI);
714 #else
715 char *rockbox = " ROCKbox!";
717 lcd_clear_display();
718 lcd_double_height(true);
719 lcd_puts(0, 0, rockbox);
720 lcd_puts_scroll(0, 1, rbversion);
721 #endif
722 lcd_update();
724 #ifdef HAVE_REMOTE_LCD
725 lcd_remote_clear_display();
726 lcd_remote_bmp(&bm_remote_rockboxlogo, 0, 10);
727 lcd_remote_setfont(FONT_SYSFIXED);
728 lcd_remote_getstringsize((unsigned char *)"A", &font_w, &font_h);
729 lcd_remote_putsxy((LCD_REMOTE_WIDTH/2) - ((strlen(version)*font_w)/2),
730 LCD_REMOTE_HEIGHT-font_h, (unsigned char *)version);
731 lcd_remote_setfont(FONT_UI);
732 lcd_remote_update();
733 #endif
735 return 0;
738 #ifdef BOOTFILE
739 #if !defined(USB_NONE) && !defined(USB_HANDLED_BY_OF) || defined(HAVE_HOTSWAP_STORAGE_AS_MAIN)
741 memorize/compare details about the BOOTFILE
742 we don't use dircache because it may not be up to date after
743 USB disconnect (scanning in the background)
745 void check_bootfile(bool do_rolo)
747 static unsigned short wrtdate = 0;
748 static unsigned short wrttime = 0;
749 DIR* dir = NULL;
750 struct dirent* entry = NULL;
752 /* 1. open BOOTDIR and find the BOOTFILE dir entry */
753 dir = opendir(BOOTDIR);
755 if(!dir) return; /* do we want an error splash? */
757 /* loop all files in BOOTDIR */
758 while(0 != (entry = readdir(dir)))
760 if(!strcasecmp(entry->d_name, BOOTFILE))
762 struct dirinfo info = dir_get_info(dir, entry);
763 /* found the bootfile */
764 if(wrtdate && do_rolo)
766 if((info.wrtdate != wrtdate) ||
767 (info.wrttime != wrttime))
769 static const char *lines[] = { ID2P(LANG_BOOT_CHANGED),
770 ID2P(LANG_REBOOT_NOW) };
771 static const struct text_message message={ lines, 2 };
772 button_clear_queue(); /* Empty the keyboard buffer */
773 if(gui_syncyesno_run(&message, NULL, NULL) == YESNO_YES)
775 audio_hard_stop();
776 rolo_load(BOOTDIR "/" BOOTFILE);
780 wrtdate = info.wrtdate;
781 wrttime = info.wrttime;
784 closedir(dir);
786 #endif
787 #endif
789 /* check range, set volume and save settings */
790 void setvol(void)
792 const int min_vol = sound_min(SOUND_VOLUME);
793 const int max_vol = sound_max(SOUND_VOLUME);
794 if (global_settings.volume < min_vol)
795 global_settings.volume = min_vol;
796 if (global_settings.volume > max_vol)
797 global_settings.volume = max_vol;
798 sound_set_volume(global_settings.volume);
799 global_status.last_volume_change = current_tick;
800 settings_save();
803 char* strrsplt(char* str, int c)
805 char* s = strrchr(str, c);
807 if (s != NULL)
809 *s++ = '\0';
811 else
813 s = str;
816 return s;
820 * removes the extension of filename (if it doesn't start with a .)
821 * puts the result in buffer
823 char *strip_extension(char* buffer, int buffer_size, const char *filename)
825 char *dot = strrchr(filename, '.');
826 int len;
828 if (buffer_size <= 0)
830 return NULL;
833 buffer_size--; /* Make room for end nil */
835 if (dot != 0 && filename[0] != '.')
837 len = dot - filename;
838 len = MIN(len, buffer_size);
840 else
842 len = buffer_size;
845 strlcpy(buffer, filename, len + 1);
847 return buffer;
850 #if CONFIG_CODEC == SWCODEC
851 /* Play a standard sound */
852 void system_sound_play(enum system_sound sound)
854 static const struct beep_params
856 int *setting;
857 unsigned short frequency;
858 unsigned short duration;
859 unsigned short amplitude;
860 } beep_params[] =
862 [SOUND_KEYCLICK] =
863 { &global_settings.keyclick,
864 4000, KEYCLICK_DURATION, 2500 },
865 [SOUND_TRACK_SKIP] =
866 { &global_settings.beep,
867 2000, 100, 2500 },
868 [SOUND_TRACK_NO_MORE] =
869 { &global_settings.beep,
870 1000, 100, 1500 },
873 const struct beep_params *params = &beep_params[sound];
875 if (*params->setting)
877 beep_play(params->frequency, params->duration,
878 params->amplitude * *params->setting);
882 static keyclick_callback keyclick_current_callback = NULL;
883 static void* keyclick_data = NULL;
884 void keyclick_set_callback(keyclick_callback cb, void* data)
886 keyclick_current_callback = cb;
887 keyclick_data = data;
890 /* Produce keyclick based upon button and global settings */
891 void keyclick_click(bool rawbutton, int action)
893 int button = action;
894 static long last_button = BUTTON_NONE;
895 bool do_beep = false;
897 if (!rawbutton)
898 get_action_statuscode(&button);
900 /* Settings filters */
901 if (
902 #ifdef HAVE_HARDWARE_CLICK
903 (global_settings.keyclick || global_settings.keyclick_hardware)
904 #else
905 global_settings.keyclick
906 #endif
909 if (global_settings.keyclick_repeats || !(button & BUTTON_REPEAT))
911 /* Button filters */
912 if (button != BUTTON_NONE && !(button & BUTTON_REL)
913 && !(button & (SYS_EVENT|BUTTON_MULTIMEDIA)) )
915 do_beep = true;
918 else if ((button & BUTTON_REPEAT) && (last_button == BUTTON_NONE))
920 do_beep = true;
922 #ifdef HAVE_SCROLLWHEEL
923 else if (button & (BUTTON_SCROLL_BACK | BUTTON_SCROLL_FWD))
925 do_beep = true;
927 #endif
929 if (button&BUTTON_REPEAT)
930 last_button = button;
931 else
932 last_button = BUTTON_NONE;
934 if (do_beep && keyclick_current_callback)
935 do_beep = keyclick_current_callback(action, keyclick_data);
936 keyclick_current_callback = NULL;
938 if (do_beep)
940 #ifdef HAVE_HARDWARE_CLICK
941 if (global_settings.keyclick)
943 system_sound_play(SOUND_KEYCLICK);
945 if (global_settings.keyclick_hardware)
947 #if !defined(SIMULATOR)
948 piezo_button_beep(false, false);
949 #endif
951 #else
952 system_sound_play(SOUND_KEYCLICK);
953 #endif
957 /* Return the ReplayGain mode adjusted by other relevant settings */
958 static int replaygain_setting_mode(int type)
960 switch (type)
962 case REPLAYGAIN_SHUFFLE:
963 type = global_settings.playlist_shuffle ?
964 REPLAYGAIN_TRACK : REPLAYGAIN_ALBUM;
965 case REPLAYGAIN_ALBUM:
966 case REPLAYGAIN_TRACK:
967 case REPLAYGAIN_OFF:
968 default:
969 break;
972 return type;
975 /* Return the ReplayGain mode adjusted for display purposes */
976 int id3_get_replaygain_mode(const struct mp3entry *id3)
978 if (!id3)
979 return -1;
981 int type = global_settings.replaygain_settings.type;
982 type = replaygain_setting_mode(type);
984 return (type != REPLAYGAIN_TRACK && id3->album_gain != 0) ?
985 REPLAYGAIN_ALBUM : (id3->track_gain != 0 ? REPLAYGAIN_TRACK : -1);
988 /* Update DSP's replaygain from global settings */
989 void replaygain_update(void)
991 struct replaygain_settings settings = global_settings.replaygain_settings;
992 settings.type = replaygain_setting_mode(settings.type);
993 dsp_replaygain_set_settings(&settings);
995 #endif /* CONFIG_CODEC == SWCODEC */
997 #endif /* !defined(__PCTOOL__) */
999 /* Read (up to) a line of text from fd into buffer and return number of bytes
1000 * read (which may be larger than the number of bytes stored in buffer). If
1001 * an error occurs, -1 is returned (and buffer contains whatever could be
1002 * read). A line is terminated by a LF char. Neither LF nor CR chars are
1003 * stored in buffer.
1005 int read_line(int fd, char* buffer, int buffer_size)
1007 int count = 0;
1008 int num_read = 0;
1010 errno = 0;
1012 while (count < buffer_size)
1014 unsigned char c;
1016 if (1 != read(fd, &c, 1))
1017 break;
1019 num_read++;
1021 if ( c == '\n' )
1022 break;
1024 if ( c == '\r' )
1025 continue;
1027 buffer[count++] = c;
1030 buffer[MIN(count, buffer_size - 1)] = 0;
1032 return errno ? -1 : num_read;
1036 char* skip_whitespace(char* const str)
1038 char *s = str;
1040 while (isspace(*s))
1041 s++;
1043 return s;
1046 /* Format time into buf.
1048 * buf - buffer to format to.
1049 * buf_size - size of buffer.
1050 * t - time to format, in milliseconds.
1052 void format_time(char* buf, int buf_size, long t)
1054 int const time = abs(t / 1000);
1055 int const hours = time / 3600;
1056 int const minutes = time / 60 - hours * 60;
1057 int const seconds = time % 60;
1058 const char * const sign = &"-"[t < 0 ? 0 : 1];
1060 if ( hours == 0 )
1062 snprintf(buf, buf_size, "%s%d:%02d", sign, minutes, seconds);
1064 else
1066 snprintf(buf, buf_size, "%s%d:%02d:%02d", sign, hours, minutes,
1067 seconds);
1072 /** Open a UTF-8 file and set file descriptor to first byte after BOM.
1073 * If no BOM is present this behaves like open().
1074 * If the file is opened for writing and O_TRUNC is set, write a BOM to
1075 * the opened file and leave the file pointer set after the BOM.
1078 int open_utf8(const char* pathname, int flags)
1080 int fd;
1081 unsigned char bom[BOM_UTF_8_SIZE];
1083 fd = open(pathname, flags, 0666);
1084 if(fd < 0)
1085 return fd;
1087 if(flags & (O_TRUNC | O_WRONLY))
1089 write(fd, BOM_UTF_8, BOM_UTF_8_SIZE);
1091 else
1093 read(fd, bom, BOM_UTF_8_SIZE);
1094 /* check for BOM */
1095 if(memcmp(bom, BOM_UTF_8, BOM_UTF_8_SIZE))
1096 lseek(fd, 0, SEEK_SET);
1098 return fd;
1102 #ifdef HAVE_LCD_COLOR
1104 * Helper function to convert a string of 6 hex digits to a native colour
1107 static int hex2dec(int c)
1109 return (((c) >= '0' && ((c) <= '9')) ? (c) - '0' :
1110 (toupper(c)) - 'A' + 10);
1113 int hex_to_rgb(const char* hex, int* color)
1115 int red, green, blue;
1116 int i = 0;
1118 while ((i < 6) && (isxdigit(hex[i])))
1119 i++;
1121 if (i < 6)
1122 return -1;
1124 red = (hex2dec(hex[0]) << 4) | hex2dec(hex[1]);
1125 green = (hex2dec(hex[2]) << 4) | hex2dec(hex[3]);
1126 blue = (hex2dec(hex[4]) << 4) | hex2dec(hex[5]);
1128 *color = LCD_RGBPACK(red,green,blue);
1130 return 0;
1132 #endif /* HAVE_LCD_COLOR */
1134 #ifdef HAVE_LCD_BITMAP
1135 /* '0'-'3' are ASCII 0x30 to 0x33 */
1136 #define is0123(x) (((x) & 0xfc) == 0x30)
1137 #if !defined(__PCTOOL__) || defined(CHECKWPS)
1138 bool parse_color(enum screen_type screen, char *text, int *value)
1140 (void)text; (void)value; /* silence warnings on mono bitmap */
1141 (void)screen;
1143 #ifdef HAVE_LCD_COLOR
1144 if (screens[screen].depth > 2)
1146 if (hex_to_rgb(text, value) < 0)
1147 return false;
1148 else
1149 return true;
1151 #endif
1153 #if LCD_DEPTH == 2 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 2)
1154 if (screens[screen].depth == 2)
1156 if (text[1] != '\0' || !is0123(*text))
1157 return false;
1158 *value = *text - '0';
1159 return true;
1161 #endif
1162 return false;
1164 #endif /* !defined(__PCTOOL__) || defined(CHECKWPS) */
1166 /* only used in USB HID and set_time screen */
1167 #if defined(USB_ENABLE_HID) || (CONFIG_RTC != 0)
1168 int clamp_value_wrap(int value, int max, int min)
1170 if (value > max)
1171 return min;
1172 if (value < min)
1173 return max;
1174 return value;
1176 #endif
1177 #endif
1178 #define MAX_ACTIVITY_DEPTH 12
1179 static enum current_activity
1180 current_activity[MAX_ACTIVITY_DEPTH] = {ACTIVITY_UNKNOWN};
1181 static int current_activity_top = 0;
1182 void push_current_activity(enum current_activity screen)
1184 current_activity[current_activity_top++] = screen;
1185 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
1186 FOR_NB_SCREENS(i)
1187 skinlist_set_cfg(i, NULL);
1188 #endif
1190 void pop_current_activity(void)
1192 current_activity_top--;
1193 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
1194 FOR_NB_SCREENS(i)
1195 skinlist_set_cfg(i, NULL);
1196 #endif
1198 enum current_activity get_current_activity(void)
1200 return current_activity[current_activity_top?current_activity_top-1:0];