more wps->skin moving/renaming
[kugel-rb.git] / apps / gui / skin_engine / skin_tokens.c
blobb1163aa88ab1b13a24a8a7872499d0846745bd16
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002-2007 Björn Stenberg
11 * Copyright (C) 2007-2008 Nicolas Pennequin
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
22 #include "font.h"
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include "action.h"
27 #include "system.h"
28 #include "settings.h"
29 #include "settings_list.h"
30 #include "rbunicode.h"
31 #include "timefuncs.h"
32 #include "audio.h"
33 #include "status.h"
34 #include "power.h"
35 #include "powermgmt.h"
36 #include "sound.h"
37 #include "debug.h"
38 #ifdef HAVE_LCD_CHARCELLS
39 #include "hwcompat.h"
40 #endif
41 #include "abrepeat.h"
42 #include "mp3_playback.h"
43 #include "lang.h"
44 #include "misc.h"
45 #include "led.h"
46 #ifdef HAVE_LCD_BITMAP
47 /* Image stuff */
48 #include "albumart.h"
49 #endif
50 #include "dsp.h"
51 #include "playlist.h"
52 #if CONFIG_CODEC == SWCODEC
53 #include "playback.h"
54 #endif
55 #include "viewport.h"
57 #include "wps_internals.h"
58 #include "wps.h"
60 static char* get_codectype(const struct mp3entry* id3)
62 if (id3->codectype < AFMT_NUM_CODECS) {
63 return (char*)audio_formats[id3->codectype].label;
64 } else {
65 return NULL;
69 /* Extract a part from a path.
71 * buf - buffer extract part to.
72 * buf_size - size of buffer.
73 * path - path to extract from.
74 * level - what to extract. 0 is file name, 1 is parent of file, 2 is
75 * parent of parent, etc.
77 * Returns buf if the desired level was found, NULL otherwise.
79 static char* get_dir(char* buf, int buf_size, const char* path, int level)
81 const char* sep;
82 const char* last_sep;
83 int len;
85 sep = path + strlen(path);
86 last_sep = sep;
88 while (sep > path)
90 if ('/' == *(--sep))
92 if (!level)
93 break;
95 level--;
96 last_sep = sep - 1;
100 if (level || (last_sep <= sep))
101 return NULL;
103 len = MIN(last_sep - sep, buf_size - 1);
104 strlcpy(buf, sep + 1, len + 1);
105 return buf;
108 /* Return the tag found at index i and write its value in buf.
109 The return value is buf if the tag had a value, or NULL if not.
111 intval is used with conditionals/enums: when this function is called,
112 intval should contain the number of options in the conditional/enum.
113 When this function returns, intval is -1 if the tag is non numeric or,
114 if the tag is numeric, *intval is the enum case we want to go to (between 1
115 and the original value of *intval, inclusive).
116 When not treating a conditional/enum, intval should be NULL.
118 const char *get_token_value(struct gui_wps *gwps,
119 struct wps_token *token,
120 char *buf, int buf_size,
121 int *intval)
123 if (!gwps)
124 return NULL;
126 struct wps_data *data = gwps->data;
127 struct wps_state *state = gwps->state;
129 if (!data || !state)
130 return NULL;
132 struct mp3entry *id3;
134 if (token->next)
135 id3 = state->nid3;
136 else
137 id3 = state->id3;
139 if (!id3)
140 return NULL;
142 #if CONFIG_RTC
143 struct tm* tm = NULL;
145 /* if the token is an RTC one, update the time
146 and do the necessary checks */
147 if (token->type >= WPS_TOKENS_RTC_BEGIN
148 && token->type <= WPS_TOKENS_RTC_END)
150 tm = get_time();
152 if (!valid_time(tm))
153 return NULL;
155 #endif
157 int limit = 1;
158 if (intval)
160 limit = *intval;
161 *intval = -1;
164 switch (token->type)
166 case WPS_TOKEN_CHARACTER:
167 return &(token->value.c);
169 case WPS_TOKEN_STRING:
170 return data->strings[token->value.i];
172 case WPS_TOKEN_TRACK_TIME_ELAPSED:
173 format_time(buf, buf_size,
174 id3->elapsed + state->ff_rewind_count);
175 return buf;
177 case WPS_TOKEN_TRACK_TIME_REMAINING:
178 format_time(buf, buf_size,
179 id3->length - id3->elapsed -
180 state->ff_rewind_count);
181 return buf;
183 case WPS_TOKEN_TRACK_LENGTH:
184 format_time(buf, buf_size, id3->length);
185 return buf;
187 case WPS_TOKEN_PLAYLIST_ENTRIES:
188 snprintf(buf, buf_size, "%d", playlist_amount());
189 return buf;
191 case WPS_TOKEN_PLAYLIST_NAME:
192 return playlist_name(NULL, buf, buf_size);
194 case WPS_TOKEN_PLAYLIST_POSITION:
195 snprintf(buf, buf_size, "%d", playlist_get_display_index());
196 return buf;
198 case WPS_TOKEN_PLAYLIST_SHUFFLE:
199 if ( global_settings.playlist_shuffle )
200 return "s";
201 else
202 return NULL;
203 break;
205 case WPS_TOKEN_VOLUME:
206 snprintf(buf, buf_size, "%d", global_settings.volume);
207 if (intval)
209 if (global_settings.volume == sound_min(SOUND_VOLUME))
211 *intval = 1;
213 else if (global_settings.volume == 0)
215 *intval = limit - 1;
217 else if (global_settings.volume > 0)
219 *intval = limit;
221 else
223 *intval = (limit - 3) * (global_settings.volume
224 - sound_min(SOUND_VOLUME) - 1)
225 / (-1 - sound_min(SOUND_VOLUME)) + 2;
228 return buf;
230 case WPS_TOKEN_TRACK_ELAPSED_PERCENT:
231 if (id3->length <= 0)
232 return NULL;
234 if (intval)
236 *intval = limit * (id3->elapsed + state->ff_rewind_count)
237 / id3->length + 1;
239 snprintf(buf, buf_size, "%d",
240 100*(id3->elapsed + state->ff_rewind_count) / id3->length);
241 return buf;
243 case WPS_TOKEN_METADATA_ARTIST:
244 return id3->artist;
246 case WPS_TOKEN_METADATA_COMPOSER:
247 return id3->composer;
249 case WPS_TOKEN_METADATA_ALBUM:
250 return id3->album;
252 case WPS_TOKEN_METADATA_ALBUM_ARTIST:
253 return id3->albumartist;
255 case WPS_TOKEN_METADATA_GROUPING:
256 return id3->grouping;
258 case WPS_TOKEN_METADATA_GENRE:
259 return id3->genre_string;
261 case WPS_TOKEN_METADATA_DISC_NUMBER:
262 if (id3->disc_string)
263 return id3->disc_string;
264 if (id3->discnum) {
265 snprintf(buf, buf_size, "%d", id3->discnum);
266 return buf;
268 return NULL;
270 case WPS_TOKEN_METADATA_TRACK_NUMBER:
271 if (id3->track_string)
272 return id3->track_string;
274 if (id3->tracknum) {
275 snprintf(buf, buf_size, "%d", id3->tracknum);
276 return buf;
278 return NULL;
280 case WPS_TOKEN_METADATA_TRACK_TITLE:
281 return id3->title;
283 case WPS_TOKEN_METADATA_VERSION:
284 switch (id3->id3version)
286 case ID3_VER_1_0:
287 return "1";
289 case ID3_VER_1_1:
290 return "1.1";
292 case ID3_VER_2_2:
293 return "2.2";
295 case ID3_VER_2_3:
296 return "2.3";
298 case ID3_VER_2_4:
299 return "2.4";
301 default:
302 return NULL;
305 case WPS_TOKEN_METADATA_YEAR:
306 if( id3->year_string )
307 return id3->year_string;
309 if (id3->year) {
310 snprintf(buf, buf_size, "%d", id3->year);
311 return buf;
313 return NULL;
315 case WPS_TOKEN_METADATA_COMMENT:
316 return id3->comment;
318 #ifdef HAVE_ALBUMART
319 case WPS_TOKEN_ALBUMART_DISPLAY:
320 draw_album_art(gwps, audio_current_aa_hid(), false);
321 return NULL;
323 case WPS_TOKEN_ALBUMART_FOUND:
324 if (audio_current_aa_hid() >= 0) {
325 return "C";
327 return NULL;
328 #endif
330 case WPS_TOKEN_FILE_BITRATE:
331 if(id3->bitrate)
332 snprintf(buf, buf_size, "%d", id3->bitrate);
333 else
334 return "?";
335 return buf;
337 case WPS_TOKEN_FILE_CODEC:
338 if (intval)
340 if(id3->codectype == AFMT_UNKNOWN)
341 *intval = AFMT_NUM_CODECS;
342 else
343 *intval = id3->codectype;
345 return get_codectype(id3);
347 case WPS_TOKEN_FILE_FREQUENCY:
348 snprintf(buf, buf_size, "%ld", id3->frequency);
349 return buf;
351 case WPS_TOKEN_FILE_FREQUENCY_KHZ:
352 /* ignore remainders < 100, so 22050 Hz becomes just 22k */
353 if ((id3->frequency % 1000) < 100)
354 snprintf(buf, buf_size, "%ld", id3->frequency / 1000);
355 else
356 snprintf(buf, buf_size, "%ld.%d",
357 id3->frequency / 1000,
358 (id3->frequency % 1000) / 100);
359 return buf;
361 case WPS_TOKEN_FILE_NAME:
362 if (get_dir(buf, buf_size, id3->path, 0)) {
363 /* Remove extension */
364 char* sep = strrchr(buf, '.');
365 if (NULL != sep) {
366 *sep = 0;
368 return buf;
370 else {
371 return NULL;
374 case WPS_TOKEN_FILE_NAME_WITH_EXTENSION:
375 return get_dir(buf, buf_size, id3->path, 0);
377 case WPS_TOKEN_FILE_PATH:
378 return id3->path;
380 case WPS_TOKEN_FILE_SIZE:
381 snprintf(buf, buf_size, "%ld", id3->filesize / 1024);
382 return buf;
384 case WPS_TOKEN_FILE_VBR:
385 return id3->vbr ? "(avg)" : NULL;
387 case WPS_TOKEN_FILE_DIRECTORY:
388 return get_dir(buf, buf_size, id3->path, token->value.i);
390 case WPS_TOKEN_BATTERY_PERCENT:
392 int l = battery_level();
394 if (intval)
396 limit = MAX(limit, 2);
397 if (l > -1) {
398 /* First enum is used for "unknown level". */
399 *intval = (limit - 1) * l / 100 + 2;
400 } else {
401 *intval = 1;
405 if (l > -1) {
406 snprintf(buf, buf_size, "%d", l);
407 return buf;
408 } else {
409 return "?";
413 case WPS_TOKEN_BATTERY_VOLTS:
415 unsigned int v = battery_voltage();
416 snprintf(buf, buf_size, "%d.%02d", v / 1000, (v % 1000) / 10);
417 return buf;
420 case WPS_TOKEN_BATTERY_TIME:
422 int t = battery_time();
423 if (t >= 0)
424 snprintf(buf, buf_size, "%dh %dm", t / 60, t % 60);
425 else
426 return "?h ?m";
427 return buf;
430 #if CONFIG_CHARGING
431 case WPS_TOKEN_BATTERY_CHARGER_CONNECTED:
433 if(charger_input_state==CHARGER)
434 return "p";
435 else
436 return NULL;
438 #endif
439 #if CONFIG_CHARGING >= CHARGING_MONITOR
440 case WPS_TOKEN_BATTERY_CHARGING:
442 if (charge_state == CHARGING || charge_state == TOPOFF) {
443 return "c";
444 } else {
445 return NULL;
448 #endif
449 case WPS_TOKEN_BATTERY_SLEEPTIME:
451 if (get_sleep_timer() == 0)
452 return NULL;
453 else
455 format_time(buf, buf_size, get_sleep_timer() * 1000);
456 return buf;
460 case WPS_TOKEN_PLAYBACK_STATUS:
462 int status = audio_status();
463 int mode = 1;
464 if (status == AUDIO_STATUS_PLAY)
465 mode = 2;
466 if (is_wps_fading() ||
467 (status & AUDIO_STATUS_PAUSE && !status_get_ffmode()))
468 mode = 3;
469 if (status_get_ffmode() == STATUS_FASTFORWARD)
470 mode = 4;
471 if (status_get_ffmode() == STATUS_FASTBACKWARD)
472 mode = 5;
474 if (intval) {
475 *intval = mode;
478 snprintf(buf, buf_size, "%d", mode-1);
479 return buf;
482 case WPS_TOKEN_REPEAT_MODE:
483 if (intval)
484 *intval = global_settings.repeat_mode + 1;
485 snprintf(buf, buf_size, "%d", global_settings.repeat_mode);
486 return buf;
488 case WPS_TOKEN_RTC_PRESENT:
489 #if CONFIG_RTC
490 return "c";
491 #else
492 return NULL;
493 #endif
495 #if CONFIG_RTC
496 case WPS_TOKEN_RTC_12HOUR_CFG:
497 if (intval)
498 *intval = global_settings.timeformat + 1;
499 snprintf(buf, buf_size, "%d", global_settings.timeformat);
500 return buf;
502 case WPS_TOKEN_RTC_DAY_OF_MONTH:
503 /* d: day of month (01..31) */
504 snprintf(buf, buf_size, "%02d", tm->tm_mday);
505 return buf;
507 case WPS_TOKEN_RTC_DAY_OF_MONTH_BLANK_PADDED:
508 /* e: day of month, blank padded ( 1..31) */
509 snprintf(buf, buf_size, "%2d", tm->tm_mday);
510 return buf;
512 case WPS_TOKEN_RTC_HOUR_24_ZERO_PADDED:
513 /* H: hour (00..23) */
514 snprintf(buf, buf_size, "%02d", tm->tm_hour);
515 return buf;
517 case WPS_TOKEN_RTC_HOUR_24:
518 /* k: hour ( 0..23) */
519 snprintf(buf, buf_size, "%2d", tm->tm_hour);
520 return buf;
522 case WPS_TOKEN_RTC_HOUR_12_ZERO_PADDED:
523 /* I: hour (01..12) */
524 snprintf(buf, buf_size, "%02d",
525 (tm->tm_hour % 12 == 0) ? 12 : tm->tm_hour % 12);
526 return buf;
528 case WPS_TOKEN_RTC_HOUR_12:
529 /* l: hour ( 1..12) */
530 snprintf(buf, buf_size, "%2d",
531 (tm->tm_hour % 12 == 0) ? 12 : tm->tm_hour % 12);
532 return buf;
534 case WPS_TOKEN_RTC_MONTH:
535 /* m: month (01..12) */
536 if (intval)
537 *intval = tm->tm_mon + 1;
538 snprintf(buf, buf_size, "%02d", tm->tm_mon + 1);
539 return buf;
541 case WPS_TOKEN_RTC_MINUTE:
542 /* M: minute (00..59) */
543 snprintf(buf, buf_size, "%02d", tm->tm_min);
544 return buf;
546 case WPS_TOKEN_RTC_SECOND:
547 /* S: second (00..59) */
548 snprintf(buf, buf_size, "%02d", tm->tm_sec);
549 return buf;
551 case WPS_TOKEN_RTC_YEAR_2_DIGITS:
552 /* y: last two digits of year (00..99) */
553 snprintf(buf, buf_size, "%02d", tm->tm_year % 100);
554 return buf;
556 case WPS_TOKEN_RTC_YEAR_4_DIGITS:
557 /* Y: year (1970...) */
558 snprintf(buf, buf_size, "%04d", tm->tm_year + 1900);
559 return buf;
561 case WPS_TOKEN_RTC_AM_PM_UPPER:
562 /* p: upper case AM or PM indicator */
563 return tm->tm_hour/12 == 0 ? "AM" : "PM";
565 case WPS_TOKEN_RTC_AM_PM_LOWER:
566 /* P: lower case am or pm indicator */
567 return tm->tm_hour/12 == 0 ? "am" : "pm";
569 case WPS_TOKEN_RTC_WEEKDAY_NAME:
570 /* a: abbreviated weekday name (Sun..Sat) */
571 return str(LANG_WEEKDAY_SUNDAY + tm->tm_wday);
573 case WPS_TOKEN_RTC_MONTH_NAME:
574 /* b: abbreviated month name (Jan..Dec) */
575 return str(LANG_MONTH_JANUARY + tm->tm_mon);
577 case WPS_TOKEN_RTC_DAY_OF_WEEK_START_MON:
578 /* u: day of week (1..7); 1 is Monday */
579 if (intval)
580 *intval = (tm->tm_wday == 0) ? 7 : tm->tm_wday;
581 snprintf(buf, buf_size, "%1d", tm->tm_wday + 1);
582 return buf;
584 case WPS_TOKEN_RTC_DAY_OF_WEEK_START_SUN:
585 /* w: day of week (0..6); 0 is Sunday */
586 if (intval)
587 *intval = tm->tm_wday + 1;
588 snprintf(buf, buf_size, "%1d", tm->tm_wday);
589 return buf;
590 #else
591 case WPS_TOKEN_RTC_DAY_OF_MONTH:
592 case WPS_TOKEN_RTC_DAY_OF_MONTH_BLANK_PADDED:
593 case WPS_TOKEN_RTC_HOUR_24_ZERO_PADDED:
594 case WPS_TOKEN_RTC_HOUR_24:
595 case WPS_TOKEN_RTC_HOUR_12_ZERO_PADDED:
596 case WPS_TOKEN_RTC_HOUR_12:
597 case WPS_TOKEN_RTC_MONTH:
598 case WPS_TOKEN_RTC_MINUTE:
599 case WPS_TOKEN_RTC_SECOND:
600 case WPS_TOKEN_RTC_AM_PM_UPPER:
601 case WPS_TOKEN_RTC_AM_PM_LOWER:
602 case WPS_TOKEN_RTC_YEAR_2_DIGITS:
603 return "--";
604 case WPS_TOKEN_RTC_YEAR_4_DIGITS:
605 return "----";
606 case WPS_TOKEN_RTC_WEEKDAY_NAME:
607 case WPS_TOKEN_RTC_MONTH_NAME:
608 return "---";
609 case WPS_TOKEN_RTC_DAY_OF_WEEK_START_MON:
610 case WPS_TOKEN_RTC_DAY_OF_WEEK_START_SUN:
611 return "-";
612 #endif
614 #ifdef HAVE_LCD_CHARCELLS
615 case WPS_TOKEN_PROGRESSBAR:
617 char *end = utf8encode(data->wps_progress_pat[0], buf);
618 *end = '\0';
619 return buf;
622 case WPS_TOKEN_PLAYER_PROGRESSBAR:
623 if(is_new_player())
625 /* we need 11 characters (full line) for
626 progress-bar */
627 strlcpy(buf, " ", buf_size);
629 else
631 /* Tell the user if we have an OldPlayer */
632 strlcpy(buf, " <Old LCD> ", buf_size);
634 return buf;
635 #endif
637 #ifdef HAVE_TAGCACHE
638 case WPS_TOKEN_DATABASE_PLAYCOUNT:
639 if (intval) {
640 *intval = id3->playcount + 1;
642 snprintf(buf, buf_size, "%ld", id3->playcount);
643 return buf;
645 case WPS_TOKEN_DATABASE_RATING:
646 if (intval) {
647 *intval = id3->rating + 1;
649 snprintf(buf, buf_size, "%d", id3->rating);
650 return buf;
652 case WPS_TOKEN_DATABASE_AUTOSCORE:
653 if (intval)
654 *intval = id3->score + 1;
656 snprintf(buf, buf_size, "%d", id3->score);
657 return buf;
658 #endif
660 #if (CONFIG_CODEC == SWCODEC)
661 case WPS_TOKEN_CROSSFADE:
662 if (intval)
663 *intval = global_settings.crossfade + 1;
664 snprintf(buf, buf_size, "%d", global_settings.crossfade);
665 return buf;
667 case WPS_TOKEN_REPLAYGAIN:
669 int val;
671 if (global_settings.replaygain_type == REPLAYGAIN_OFF)
672 val = 1; /* off */
673 else
675 int type =
676 get_replaygain_mode(id3->track_gain_string != NULL,
677 id3->album_gain_string != NULL);
678 if (type < 0)
679 val = 6; /* no tag */
680 else
681 val = type + 2;
683 if (global_settings.replaygain_type == REPLAYGAIN_SHUFFLE)
684 val += 2;
687 if (intval)
688 *intval = val;
690 switch (val)
692 case 1:
693 case 6:
694 return "+0.00 dB";
695 break;
696 case 2:
697 case 4:
698 strlcpy(buf, id3->track_gain_string, buf_size);
699 break;
700 case 3:
701 case 5:
702 strlcpy(buf, id3->album_gain_string, buf_size);
703 break;
705 return buf;
707 #endif /* (CONFIG_CODEC == SWCODEC) */
709 #if (CONFIG_CODEC != MAS3507D)
710 case WPS_TOKEN_SOUND_PITCH:
712 int val = sound_get_pitch();
713 snprintf(buf, buf_size, "%d.%d",
714 val / 10, val % 10);
715 return buf;
717 #endif
719 case WPS_TOKEN_MAIN_HOLD:
720 #ifdef HAS_BUTTON_HOLD
721 if (button_hold())
722 #else
723 if (is_keys_locked())
724 #endif /*hold switch or softlock*/
725 return "h";
726 else
727 return NULL;
729 #ifdef HAS_REMOTE_BUTTON_HOLD
730 case WPS_TOKEN_REMOTE_HOLD:
731 if (remote_button_hold())
732 return "r";
733 else
734 return NULL;
735 #endif
737 #if (CONFIG_LED == LED_VIRTUAL) || defined(HAVE_REMOTE_LCD)
738 case WPS_TOKEN_VLED_HDD:
739 if(led_read(HZ/2))
740 return "h";
741 else
742 return NULL;
743 #endif
744 case WPS_TOKEN_BUTTON_VOLUME:
745 if (data->button_time_volume &&
746 TIME_BEFORE(current_tick, data->button_time_volume +
747 token->value.i * TIMEOUT_UNIT))
748 return "v";
749 return NULL;
750 case WPS_TOKEN_LASTTOUCH:
751 #ifdef HAVE_TOUCHSCREEN
752 if (TIME_BEFORE(current_tick, token->value.i * TIMEOUT_UNIT +
753 touchscreen_last_touch()))
754 return "t";
755 #endif
756 return NULL;
758 case WPS_TOKEN_SETTING:
760 if (intval)
762 /* Handle contionals */
763 const struct settings_list *s = settings+token->value.i;
764 switch (s->flags&F_T_MASK)
766 case F_T_INT:
767 case F_T_UINT:
768 if (s->flags&F_RGB)
769 /* %?St|name|<#000000|#000001|...|#FFFFFF> */
770 /* shouldn't overflow since colors are stored
771 * on 16 bits ...
772 * but this is pretty useless anyway */
773 *intval = *(int*)s->setting + 1;
774 else if (s->cfg_vals == NULL)
775 /* %?St|name|<1st choice|2nd choice|...> */
776 *intval = (*(int*)s->setting-s->int_setting->min)
777 /s->int_setting->step + 1;
778 else
779 /* %?St|name|<1st choice|2nd choice|...> */
780 /* Not sure about this one. cfg_name/vals are
781 * indexed from 0 right? */
782 *intval = *(int*)s->setting + 1;
783 break;
784 case F_T_BOOL:
785 /* %?St|name|<if true|if false> */
786 *intval = *(bool*)s->setting?1:2;
787 break;
788 case F_T_CHARPTR:
789 /* %?St|name|<if non empty string|if empty>
790 * The string's emptyness discards the setting's
791 * prefix and suffix */
792 *intval = ((char*)s->setting)[0]?1:2;
793 break;
794 default:
795 /* This shouldn't happen ... but you never know */
796 *intval = -1;
797 break;
800 cfg_to_string(token->value.i,buf,buf_size);
801 return buf;
804 default:
805 return NULL;