Build doom on clipv2 and clip+
[kugel-rb.git] / apps / plugins / lrcplayer.c
blobfdd54b636642e60acfd718fc58c928d303d0f972
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2008-2009 Teruaki Kawashima
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 "plugin.h"
22 #include "lib/playback_control.h"
23 #include "lib/configfile.h"
24 #include "lib/helper.h"
25 #include <ctype.h>
27 PLUGIN_HEADER
29 #define MAX_LINE_LEN 256
30 #define LRC_BUFFER_SIZE 0x3000 /* 12 kiB */
31 #if PLUGIN_BUFFER_SIZE >= 0x10000 /* no id3 support for low mem targets */
32 /* define this to read lyrics in id3 tag */
33 #define LRC_SUPPORT_ID3
34 #endif
35 /* define this to show debug info in menu */
36 /* #define LRC_DEBUG */
38 enum lrc_screen {
39 PLUGIN_OTHER = 10,
40 LRC_GOTO_MAIN,
41 LRC_GOTO_MENU,
42 LRC_GOTO_EDITOR,
45 struct lrc_word {
46 long time_start;
47 short count;
48 short width;
49 unsigned char *word;
52 struct lrc_brpos {
53 short count;
54 short width;
57 struct lrc_line {
58 long time_start;
59 long old_time_start;
60 off_t file_offset; /* offset of time tag in file */
61 short nword;
62 short width;
63 short nline[NB_SCREENS];
64 struct lrc_line *next;
65 struct lrc_word *words;
68 struct preferences {
69 /* display settings */
70 #if LCD_DEPTH > 1
71 unsigned active_color;
72 unsigned inactive_color;
73 #endif
74 #ifdef HAVE_LCD_BITMAP
75 bool wrap;
76 bool wipe;
77 bool active_one_line;
78 int align; /* 0: left, 1: center, 2: right */
79 bool statusbar_on;
80 bool display_title;
81 #endif
82 bool display_time;
83 bool backlight_on;
85 /* file settings */
86 char lrc_directory[64];
87 int encoding;
88 #ifdef LRC_SUPPORT_ID3
89 bool read_id3;
90 #endif
93 static struct preferences prefs, old_prefs;
94 static unsigned char *lrc_buffer;
95 static size_t lrc_buffer_size;
96 static size_t lrc_buffer_used, lrc_buffer_end;
97 enum extention_types {LRC, LRC8, SNC, TXT, NUM_TYPES, ID3_SYLT, ID3_USLT};
98 static const char *extentions[NUM_TYPES] = {
99 ".lrc", ".lrc8", ".snc", ".txt",
101 static struct lrc_info {
102 struct mp3entry *id3;
103 long elapsed;
104 long length;
105 long ff_rewind;
106 int audio_status;
107 char mp3_file[MAX_PATH];
108 char lrc_file[MAX_PATH];
109 char *title; /* use lrc_buffer */
110 char *artist; /* use lrc_buffer */
111 enum extention_types type;
112 long offset; /* msec */
113 off_t offset_file_offset; /* offset of offset tag in file */
114 int nlrcbrpos;
115 int nlrcline;
116 struct lrc_line *ll_head, **ll_tail;
117 bool found_lrc;
118 bool loaded_lrc;
119 bool changed_lrc;
120 bool too_many_lines; /* true if nlrcline >= max_lrclines after calc pos */
121 #ifdef HAVE_LCD_BITMAP
122 bool wipe; /* false if lyrics is unsynched */
123 #endif
124 } current;
125 static char temp_buf[MAX(MAX_LINE_LEN,MAX_PATH)];
126 #ifdef HAVE_LCD_BITMAP
127 static int font_ui_height = 1;
128 static struct viewport vp_info[NB_SCREENS];
129 #endif
130 static struct viewport vp_lyrics[NB_SCREENS];
132 #define AUDIO_PAUSE (current.audio_status & AUDIO_STATUS_PAUSE)
133 #define AUDIO_PLAY (current.audio_status & AUDIO_STATUS_PLAY)
134 #define AUDIO_STOP (!(current.audio_status & AUDIO_STATUS_PLAY))
136 /*******************************
137 * lrc_set_time
138 *******************************/
139 #define LST_SET_MSEC 0x00010000
140 #define LST_SET_SEC 0x00020000
141 #define LST_SET_MIN 0x00040000
142 #define LST_SET_HOUR 0x00080000
144 #include "lib/pluginlib_actions.h"
145 #define LST_SET_TIME (LST_SET_MSEC|LST_SET_SEC|LST_SET_MIN|LST_SET_HOUR)
146 #ifdef HAVE_LCD_CHARCELLS
147 #define LST_OFF_Y 0
148 #else /* HAVE_LCD_BITMAP */
149 #define LST_OFF_Y 1
150 #endif
151 int lrc_set_time(const char *title, const char *unit, long *pval,
152 int step, int min, int max, int flags)
154 const struct button_mapping *lst_contexts[] = {
155 pla_main_ctx,
156 #ifdef HAVE_REMOTE_LCD
157 pla_remote_ctx,
158 #endif
160 /* how many */
161 const unsigned char formats[4][8] = {"%03ld.", "%02ld.", "%02ld:", "%02ld:"};
162 const unsigned int maxs[4] = {1000, 60, 60, 24};
163 const unsigned int scls[4] = {1, 1000, 60*1000, 60*60*1000};
164 char buffer[32];
165 long value = *pval, scl_step = step, i = 0;
166 int pos = 0, last_pos = 0, pos_min = 3, pos_max = 0;
167 int x = 0, y = 0, p_start = 0, p_end = 0;
168 int ret = 10;
170 if (!(flags&LST_SET_TIME))
171 return -1;
173 for (i = 0; i < 4; i++)
175 if (flags&(LST_SET_MSEC<<i))
177 if (pos_min > i) pos_min = i;
178 if (pos_max < i) pos_max = i;
181 pos = pos_min;
183 rb->button_clear_queue();
184 rb->lcd_clear_display();
185 rb->lcd_puts_scroll(0, LST_OFF_Y, title);
186 while (ret == 10)
188 int len = 0;
189 long abs_val = value;
190 long segvals[4] = {-1, -1, -1, -1};
191 /* show negative value like -00:01 but 00:-1 */
192 if (value < 0)
194 buffer[len++] = '-';
195 abs_val = -value;
197 buffer[len] = 0;
198 /* calc value of each segments */
199 for (i = pos_min; i <= pos_max; i++)
201 segvals[i] = abs_val % maxs[i];
202 abs_val /= maxs[i];
204 segvals[i-1] += abs_val * maxs[i-1];
205 for (i = pos_max; i >= pos_min; i--)
207 if (pos == i)
209 rb->lcd_getstringsize(buffer, &x, &y);
210 p_start = len;
212 rb->snprintf(&buffer[len], 32-len, formats[i], segvals[i]);
213 len += rb->strlen(&buffer[len]);
214 if (pos == i)
215 p_end = len;
217 buffer[len-1] = 0; /* remove last separater */
218 if (unit != NULL)
220 rb->snprintf(&buffer[len], 32-len, " (%s)", unit);
222 rb->lcd_puts(0, LST_OFF_Y+1, buffer);
223 if (pos_min != pos_max)
225 /* draw cursor */
226 buffer[p_end-1] = 0;
227 #ifdef HAVE_LCD_BITMAP
228 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
229 rb->lcd_putsxy(x, y*(1+LST_OFF_Y), &buffer[p_start]);
230 rb->lcd_set_drawmode(DRMODE_SOLID);
231 #else
232 rb->lcd_put_cursor(x+rb->utf8length(&buffer[p_start])-1, y, 0x7F);
233 #endif
235 rb->lcd_update();
236 int button = pluginlib_getaction(TIMEOUT_BLOCK, lst_contexts, ARRAYLEN(lst_contexts));
237 int mult = 1;
238 #ifdef HAVE_LCD_CHARCELLS
239 if (pos_min != pos_max)
240 rb->lcd_remove_cursor();
241 #endif
242 switch (button)
244 case PLA_UP_REPEAT:
245 case PLA_DOWN_REPEAT:
246 mult *= 10;
247 case PLA_DOWN:
248 case PLA_UP:
249 if (button == PLA_DOWN_REPEAT || button == PLA_DOWN)
250 mult *= -1;
251 if (pos != last_pos)
253 scl_step = ((scls[pos]/scls[pos_min]+step-1)/step) * step;
254 last_pos = pos;
256 value += scl_step * mult;
257 if (value > max)
258 value = max;
259 if (value < min)
260 value = min;
261 break;
262 case PLA_LEFT:
263 case PLA_LEFT_REPEAT:
264 if (++pos > pos_max)
265 pos = pos_min;
266 break;
267 case PLA_RIGHT:
268 case PLA_RIGHT_REPEAT:
269 if (--pos < pos_min)
270 pos = pos_max;
271 break;
272 case PLA_SELECT:
273 *pval = value;
274 ret = 0;
275 break;
276 case PLA_CANCEL:
277 case PLA_EXIT:
278 rb->splash(HZ, "Cancelled");
279 ret = -1;
280 break;
281 default:
282 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
283 ret = 1;
284 break;
287 rb->lcd_clear_display();
288 rb->lcd_update();
289 return ret;
292 /*******************************
293 * misc stuff
294 *******************************/
295 static void reset_current_data(void)
297 current.title = NULL;
298 current.artist = NULL;
299 current.offset = 0;
300 current.offset_file_offset = -1;
301 current.nlrcbrpos = 0;
302 current.nlrcline = 0;
303 current.ll_head = NULL;
304 current.ll_tail = &current.ll_head;
305 current.loaded_lrc = false;
306 current.changed_lrc = false;
307 current.too_many_lines = false;
308 lrc_buffer_used = 0;
309 lrc_buffer_end = lrc_buffer_size;
312 /* check space and add str to lrc_buffer.
313 * return NULL if there is not enough buffer. */
314 static char *lrcbufadd(const char*str, bool join)
316 if (join) lrc_buffer_used--;
317 size_t siz = rb->strlen(str)+1;
318 char *pos = &lrc_buffer[lrc_buffer_used];
319 if (lrc_buffer_used + siz > lrc_buffer_end)
320 return NULL;
321 rb->strcpy(pos, str);
322 lrc_buffer_used += siz;
323 return pos;
325 static void *alloc_buf(size_t siz)
327 siz = (siz+3) & ~3;
328 if (lrc_buffer_used + siz > lrc_buffer_end)
329 return NULL;
330 lrc_buffer_end -= siz;
331 return &lrc_buffer[lrc_buffer_end];
333 static void *new_lrc_word(long time_start, char *word, bool join)
335 struct lrc_word *lrc_word;
336 if ((lrc_word = alloc_buf(sizeof(struct lrc_word))) == NULL)
337 return NULL;
338 if ((lrc_word->word = lrcbufadd(word, join)) == NULL)
339 return NULL;
340 lrc_word->time_start = time_start;
341 return lrc_word;
343 static bool add_lrc_line(struct lrc_line *lrc_line, char *word)
345 lrc_line->nword = 0;
346 lrc_line->next = NULL;
347 lrc_line->words = NULL;
348 if (word)
350 if ((lrc_line->words = new_lrc_word(-1, word, false)) == NULL)
351 return false;
352 lrc_line->nword++;
354 *current.ll_tail = lrc_line;
355 current.ll_tail = &(lrc_line->next);
356 current.nlrcline++;
357 return true;
359 static struct lrc_line *get_lrc_line(int idx)
361 static struct lrc_line *lrc_line = NULL;
362 static int n = 0;
363 if (idx < n)
365 lrc_line = current.ll_head;
366 n = 0;
368 while (n < idx && lrc_line)
370 lrc_line = lrc_line->next;
371 n++;
373 return lrc_line;
375 static char *get_lrc_str(struct lrc_line *lrc_line)
377 return lrc_line->words[lrc_line->nword-1].word;
379 static long get_time_start(struct lrc_line *lrc_line)
381 if (!lrc_line) return current.length+20;
382 long time = lrc_line->time_start + current.offset;
383 return time < 0? 0: time;
385 static void set_time_start(struct lrc_line *lrc_line, long time_start)
387 time_start -= current.offset;
388 time_start -= time_start%10;
389 if (lrc_line->time_start != time_start)
391 lrc_line->time_start = time_start;
392 current.changed_lrc = true;
395 #define get_word_time_start(x) get_time_start((struct lrc_line *)(x))
396 #define set_word_time_start(x, t) set_time_start((struct lrc_line *)(x), (t))
398 static int format_time_tag(char *buf, long t)
400 return rb->snprintf(buf, 16, "%02ld:%02ld.%02ld",
401 t/60000, (t/1000)%60, (t/10)%100);
403 /* find start of next line */
404 static const char *lrc_skip_space(const char *str)
406 #ifdef HAVE_LCD_BITMAP
407 if (prefs.wrap)
409 while (*str && *str != '\n' && isspace(*str))
410 str++;
412 #endif
413 if (*str == '\n')
414 str++;
415 return str;
418 /* calculate how many lines is needed to display and store it.
419 * create cache if there is enough space in lrc_buffer. */
420 static struct lrc_brpos *calc_brpos(struct lrc_line *lrc_line, int i)
422 struct lrc_brpos *lrc_brpos;
423 struct lrc_word *lrc_word;
424 int nlrcbrpos = 0, max_lrcbrpos;
425 #ifdef HAVE_LCD_BITMAP
426 struct font* pf = rb->font_get(FONT_UI);
427 unsigned short ch;
428 #endif
429 struct snap {
430 int count, width;
431 int nword;
432 int word_count, word_width;
433 const unsigned char *str;
434 } sp, cr;
436 lrc_buffer_used = (lrc_buffer_used+3)&~3; /* 4 bytes aligned */
437 lrc_brpos = (struct lrc_brpos *) &lrc_buffer[lrc_buffer_used];
438 max_lrcbrpos = (lrc_buffer_end-lrc_buffer_used) / sizeof(struct lrc_brpos);
440 if (!lrc_line)
442 /* calc info for all lrcs and store them if possible */
443 size_t buffer_used = lrc_buffer_used;
444 bool too_many_lines = false;
445 current.too_many_lines = true;
446 for (lrc_line = current.ll_head; lrc_line; lrc_line = lrc_line->next)
448 FOR_NB_SCREENS(i)
450 lrc_brpos = calc_brpos(lrc_line, i);
451 if (!too_many_lines)
453 lrc_buffer_used += lrc_line->nline[i]*sizeof(struct lrc_brpos);
454 if (nlrcbrpos + lrc_line->nline[i] >= max_lrcbrpos)
456 too_many_lines = true;
457 lrc_buffer_used = buffer_used;
458 calc_brpos(lrc_line, i);
461 nlrcbrpos += lrc_line->nline[i];
464 current.too_many_lines = too_many_lines;
465 lrc_buffer_used = buffer_used;
466 current.nlrcbrpos = nlrcbrpos;
467 return NULL;
470 if (!current.too_many_lines)
472 /* use stored infos. */
473 struct lrc_line *temp_lrc = current.ll_head;
474 for (; temp_lrc != lrc_line; temp_lrc = temp_lrc->next)
476 lrc_brpos += temp_lrc->nline[SCREEN_MAIN];
477 #ifdef HAVE_REMOTE_LCD
478 lrc_brpos += temp_lrc->nline[SCREEN_REMOTE];
479 #endif
481 #if NB_SCREENS >= 2
482 while (i)
483 lrc_brpos += lrc_line->nline[--i];
484 #endif
485 return lrc_brpos;
488 /* calculate number of lines, line width and char count for each line. */
489 lrc_line->width = 0;
490 cr.nword = lrc_line->nword;
491 lrc_word = lrc_line->words+cr.nword;
492 cr.str = (lrc_word-1)->word;
493 sp.word_count = 0;
494 sp.word_width = 0;
495 sp.nword = 0;
496 sp.count = 0;
497 sp.width = 0;
498 do {
499 cr.count = 0;
500 cr.width = 0;
501 sp.str = NULL;
503 while (1)
505 while(cr.nword > 0 && cr.str >= (lrc_word-1)->word)
507 cr.nword--;
508 lrc_word--;
509 lrc_word->count = 0;
510 lrc_word->width = 0;
512 if (*cr.str == 0 || *cr.str == '\n')
513 break;
515 int c, w;
516 #ifdef HAVE_LCD_CHARCELLS
517 c = rb->utf8seek(cr.str, 1);
518 w = 1;
519 #else
520 c = ((long)rb->utf8decode(cr.str, &ch) - (long)cr.str);
521 w = rb->font_get_width(pf, ch);
522 if (cr.count && isspace(*cr.str))
524 /* remember position of last space */
525 rb->memcpy(&sp, &cr, sizeof(struct snap));
526 sp.word_count = lrc_word->count;
527 sp.word_width = lrc_word->width;
529 if (cr.count && cr.width+w > vp_lyrics[i].width)
531 if (prefs.wrap && sp.str != NULL) /* wrap */
533 rb->memcpy(&cr, &sp, sizeof(struct snap));
534 lrc_word = lrc_line->words+cr.nword;
535 lrc_word->count = sp.word_count;
536 lrc_word->width = sp.word_width;
538 break;
540 #endif
541 cr.count += c;
542 cr.width += w;
543 lrc_word->count += c;
544 lrc_word->width += w;
545 cr.str += c;
547 lrc_line->width += cr.width;
548 lrc_brpos->count = cr.count;
549 lrc_brpos->width = cr.width;
550 nlrcbrpos++;
551 lrc_brpos++;
552 cr.str = lrc_skip_space(cr.str);
553 } while (*cr.str && nlrcbrpos < max_lrcbrpos);
554 lrc_line->nline[i] = nlrcbrpos;
556 while (cr.nword > 0)
558 cr.nword--;
559 lrc_word--;
560 lrc_word->count = 0;
561 lrc_word->width = 0;
563 return lrc_brpos-nlrcbrpos;
566 /* sort lyrics by time using stable sort. */
567 static void sort_lrcs(void)
569 struct lrc_line *p = current.ll_head, **q = NULL, *t;
570 long time_max = 0;
572 current.ll_head = NULL;
573 current.ll_tail = &current.ll_head;
574 while (p != NULL)
576 t = p->next;
577 /* remove problematic lrc_lines.
578 * it would cause problem in display_lrc_line() if nword is 0. */
579 if (p->nword)
581 q = p->time_start >= time_max? current.ll_tail: &current.ll_head;
582 while ((*q) && (*q)->time_start <= p->time_start)
583 q = &((*q)->next);
584 p->next = *q;
585 *q = p;
586 if (!p->next)
588 time_max = p->time_start;
589 current.ll_tail = &p->next;
592 p = t;
594 if (!current.too_many_lines)
595 calc_brpos(NULL, 0); /* stored data depends on order of lrcs if exist */
597 static void init_time_tag(void)
599 struct lrc_line *lrc_line = current.ll_head;
600 int nline = 0;
601 if (current.type == TXT || current.type == ID3_USLT)
603 /* set time tag according to length of audio and total line count
604 * for not synched lyrics, so that scroll speed is almost constant. */
605 for (; lrc_line; lrc_line = lrc_line->next)
607 lrc_line->time_start = nline * current.length / current.nlrcbrpos;
608 lrc_line->time_start -= lrc_line->time_start%10;
609 lrc_line->old_time_start = -1;
610 nline += lrc_line->nline[SCREEN_MAIN];
611 #ifdef HAVE_REMOTE_LCD
612 nline += lrc_line->nline[SCREEN_REMOTE];
613 #endif
616 else
618 /* reset timetags to the value read from file */
619 for (; lrc_line; lrc_line = lrc_line->next)
621 lrc_line->time_start = lrc_line->old_time_start;
623 sort_lrcs();
625 current.changed_lrc = false;
628 /*******************************
629 * Serch lrc file.
630 *******************************/
632 /* search in same or parent directries of playing file.
633 * assume playing file is /aaa/bbb/ccc/ddd.mp3,
634 * this function searchs lrc file following order.
635 * /aaa/bbb/ccc/ddd.lrc
636 * /aaa/bbb/ddd.lrc
637 * /aaa/ddd.lrc
638 * /ddd.lrc
641 /* taken from apps/recorder/albumart.c */
642 static void fix_filename(char* name)
644 static const char invalid_chars[] = "*/:<>?\\|";
646 while (1)
648 if (*name == 0)
649 return;
650 if (*name == '"')
651 *name = '\'';
652 else if (rb->strchr(invalid_chars, *name))
653 *name = '_';
654 name++;
657 static bool find_lrc_file_helper(const char *base_dir)
659 char fname[MAX_PATH];
660 char *names[3] = {NULL, NULL, NULL};
661 char *p, *dir;
662 int i, len;
663 /* /aaa/bbb/ccc/ddd.mp3
664 * dir <--q names[0]
667 /* assuming file name starts with '/' */
668 rb->strcpy(temp_buf, current.mp3_file);
669 /* get file name and remove extension */
670 names[0] = rb->strrchr(temp_buf, '/')+1;
671 if ((p = rb->strrchr(names[0], '.')) != NULL)
672 *p = 0;
673 if (current.id3->title && rb->strcmp(names[0], current.id3->title))
675 rb->strlcpy(fname, current.id3->title, sizeof(fname));
676 fix_filename(fname);
677 names[1] = fname;
680 dir = temp_buf;
681 p = names[0]-1;
682 do {
683 int n;
684 *p = 0;
685 for (n = 0; ; n++)
687 if (n == 0)
689 len = rb->snprintf(current.lrc_file, MAX_PATH, "%s%s/",
690 base_dir, dir);
692 else if (n == 1)
694 /* check file in subfolder named prefs.lrc_directory
695 * in the directory of mp3 file. */
696 if (prefs.lrc_directory[0] == '/')
698 len = rb->snprintf(current.lrc_file, MAX_PATH, "%s%s/",
699 dir, prefs.lrc_directory);
701 else
702 continue;
704 else
705 break;
706 DEBUGF("check file in %s\n", current.lrc_file);
707 if (!rb->dir_exists(current.lrc_file))
708 continue;
709 for (current.type = 0; current.type < NUM_TYPES; current.type++)
711 for (i = 0; names[i] != NULL; i++)
713 rb->snprintf(&current.lrc_file[len], MAX_PATH-len,
714 "%s%s", names[i], extentions[current.type]);
715 if (rb->file_exists(current.lrc_file))
717 DEBUGF("found: `%s'\n", current.lrc_file);
718 return true;
723 } while ((p = rb->strrchr(dir, '/')) != NULL);
724 return false;
727 /* return true if a lrc file is found */
728 static bool find_lrc_file(void)
730 reset_current_data();
732 DEBUGF("find lrc file for `%s'\n", current.mp3_file);
733 /* find .lrc file */
734 if (find_lrc_file_helper(""))
735 return true;
736 if (prefs.lrc_directory[0] == '/' && rb->dir_exists(prefs.lrc_directory))
738 if (find_lrc_file_helper(prefs.lrc_directory))
739 return true;
742 current.lrc_file[0] = 0;
743 return false;
746 /*******************************
747 * Load file.
748 *******************************/
750 /* check tag format and calculate value of the tag.
751 * supported tag: ti, ar, offset
752 * supported format of time tag: [mm:ss], [mm:ss.xx], [mm:ss.xxx]
753 * returns value of timega if tag is time tag, -1 if tag is supported tag,
754 * -10 otherwise.
756 static char *parse_int(char *ptr, int *val)
758 *val = rb->atoi(ptr);
759 while (isdigit(*ptr)) ptr++;
760 return ptr;
762 static long get_time_value(char *tag, bool read_id_tags, off_t file_offset)
764 long time;
765 char *ptr;
766 int val;
768 if (read_id_tags)
770 if (!rb->strncmp(tag, "ti:", 3))
772 if (!current.id3->title || rb->strcmp(&tag[3], current.id3->title))
773 current.title = lrcbufadd(&tag[3], false);
774 return -1;
776 if (!rb->strncmp(tag, "ar:", 3))
778 if (!current.id3->artist || rb->strcmp(&tag[3], current.id3->artist))
779 current.artist = lrcbufadd(&tag[3], false);
780 return -1;
782 if (!rb->strncmp(tag, "offset:", 7))
784 current.offset = rb->atoi(&tag[7]);
785 current.offset_file_offset = file_offset;
786 return -1;
790 /* minute */
791 ptr = parse_int(tag, &val);
792 if (ptr-tag < 1 || ptr-tag > 2 || *ptr != ':')
793 return -10;
794 time = val * 60000;
795 /* second */
796 tag = ptr+1;
797 ptr = parse_int(tag, &val);
798 if (ptr-tag != 2 || (*ptr != '.' && *ptr != ':' && *ptr != '\0'))
799 return -10;
800 time += val * 1000;
802 if (*ptr != '\0')
804 /* milliseccond */
805 tag = ptr+1;
806 ptr = parse_int(tag, &val);
807 if (ptr-tag < 2 || ptr-tag > 3 || *ptr != '\0')
808 return -10;
809 time += ((ptr-tag)==3 ?val: val*10);
812 return time;
815 /* format:
816 * [time tag]line
817 * [time tag]...[time tag]line
818 * [time tag]<word time tag>word<word time tag>...<word time tag>
820 static bool parse_lrc_line(char *line, off_t file_offset)
822 struct lrc_line *lrc_line = NULL, *first_lrc_line = NULL;
823 long time, time_start;
824 char *str, *tagstart, *tagend;
825 struct lrc_word *lrc_word;
826 int nword = 0;
828 /* parse [time tag]...[time tag] type tags */
829 str = line;
830 while (1)
832 if (*str != '[') break;
833 tagend = rb->strchr(str, ']');
834 if (tagend == NULL) break;
835 *tagend = 0;
836 time = get_time_value(str+1, !lrc_line, file_offset);
837 *tagend++ = ']';
838 if (time < 0)
839 break;
840 lrc_line = alloc_buf(sizeof(struct lrc_line));
841 if (lrc_line == NULL)
842 return false;
843 if (!first_lrc_line)
844 first_lrc_line = lrc_line;
845 lrc_line->file_offset = file_offset;
846 lrc_line->time_start = (time/10)*10;
847 lrc_line->old_time_start = lrc_line->time_start;
848 add_lrc_line(lrc_line, NULL);
849 file_offset += (long)tagend - (long)str;
850 str = tagend;
852 if (!first_lrc_line)
853 return true; /* no time tag in line */
855 lrc_line = first_lrc_line;
856 if (lrcbufadd("", false) == NULL)
857 return false;
859 /* parse <word time tag>...<word time tag> type tags */
860 /* [time tag]...[time tag]line type tags share lrc_line->words and can't
861 * use lrc_line->words->timestart. use lrc_line->time_start instead. */
862 time_start = -1;
863 tagstart = str;
864 while (*tagstart)
866 tagstart = rb->strchr(tagstart, '<');
867 if (!tagstart) break;
868 tagend = rb->strchr(tagstart, '>');
869 if (!tagend) break;
870 *tagend = 0;
871 time = get_time_value(tagstart+1, false,
872 file_offset + ((long)tagstart - (long)str));
873 *tagend++ = '>';
874 if (time < 0)
876 tagstart++;
877 continue;
879 *tagstart = 0;
880 /* found word time tag. */
881 if (*str || time_start != -1)
883 if ((lrc_word = new_lrc_word(time_start, str, true)) == NULL)
884 return false;
885 nword++;
887 file_offset += (long)tagend - (long)str;
888 tagstart = str = tagend;
889 time_start = time;
891 if ((lrc_word = new_lrc_word(time_start, str, true)) == NULL)
892 return false;
893 nword++;
895 /* duplicate lrc_lines */
896 while (lrc_line)
898 lrc_line->nword = nword;
899 lrc_line->words = lrc_word;
900 lrc_line = lrc_line->next;
903 return true;
906 /* format:
907 * \xa2\xe2hhmmssxx\xa2\xd0
908 * line 1
909 * line 2
910 * \xa2\xe2hhmmssxx\xa2\xd0
911 * line 3
912 * ...
914 static bool parse_snc_line(char *line, off_t file_offset)
916 #define SNC_TAG_START "\xa2\xe2"
917 #define SNC_TAG_END "\xa2\xd0"
919 /* SNC_TAG can be dencoded, so use
920 * temp_buf which contains native data */
921 if (!rb->memcmp(temp_buf, SNC_TAG_START, 2)
922 && !rb->memcmp(temp_buf+10, SNC_TAG_END, 2)) /* time tag */
924 const char *pos = temp_buf+2; /* skip SNC_TAG_START */
925 int hh, mm, ss, xx;
927 hh = (pos[0]-'0')*10+(pos[1]-'0'); pos += 2;
928 mm = (pos[0]-'0')*10+(pos[1]-'0'); pos += 2;
929 ss = (pos[0]-'0')*10+(pos[1]-'0'); pos += 2;
930 xx = (pos[0]-'0')*10+(pos[1]-'0'); pos += 2;
931 pos += 2; /* skip SNC_TAG_END */
933 /* initialize */
934 struct lrc_line *lrc_line = alloc_buf(sizeof(struct lrc_line));
935 if (lrc_line == NULL)
936 return false;
937 lrc_line->file_offset = file_offset+2;
938 lrc_line->time_start = hh*3600000+mm*60000+ss*1000+xx*10;
939 lrc_line->old_time_start = lrc_line->time_start;
940 if (!add_lrc_line(lrc_line, ""))
941 return false;
942 if (pos[0]==0)
943 return true;
945 /* encode rest of line and add to buffer */
946 rb->iso_decode(pos, line, prefs.encoding, rb->strlen(pos)+1);
948 if (current.ll_head)
950 rb->strcat(line, "\n");
951 if (lrcbufadd(line, true) == NULL)
952 return false;
954 return true;
957 static bool parse_txt_line(char *line, off_t file_offset)
959 /* initialize */
960 struct lrc_line *lrc_line = alloc_buf(sizeof(struct lrc_line));
961 if (lrc_line == NULL)
962 return false;
963 lrc_line->file_offset = file_offset;
964 lrc_line->time_start = 0;
965 lrc_line->old_time_start = -1;
966 if (!add_lrc_line(lrc_line, line))
967 return false;
968 return true;
971 static void load_lrc_file(void)
973 char utf8line[MAX_LINE_LEN*3];
974 int fd;
975 int encoding = prefs.encoding;
976 bool (*line_parser)(char *line, off_t) = NULL;
977 off_t file_offset, readsize;
979 switch(current.type)
981 case LRC8:
982 encoding = UTF_8; /* .lrc8 is utf8 */
983 /* fall through */
984 case LRC:
985 line_parser = parse_lrc_line;
986 break;
987 case SNC:
988 line_parser = parse_snc_line;
989 break;
990 case TXT:
991 line_parser = parse_txt_line;
992 break;
993 default:
994 return;
997 fd = rb->open(current.lrc_file, O_RDONLY);
998 if (fd < 0) return;
1001 /* check encoding */
1002 #define BOM "\xef\xbb\xbf"
1003 #define BOM_SIZE 3
1004 unsigned char header[BOM_SIZE];
1005 unsigned char* (*utf_decode)(const unsigned char *,
1006 unsigned char *, int) = NULL;
1007 rb->read(fd, header, BOM_SIZE);
1008 if (!rb->memcmp(header, BOM, BOM_SIZE)) /* UTF-8 */
1010 encoding = UTF_8;
1012 else if (!rb->memcmp(header, "\xff\xfe", 2)) /* UTF-16LE */
1014 utf_decode = rb->utf16LEdecode;
1016 else if (!rb->memcmp(header, "\xfe\xff", 2)) /* UTF-16BE */
1018 utf_decode = rb->utf16BEdecode;
1020 else
1022 rb->lseek(fd, 0, SEEK_SET);
1025 if (utf_decode)
1027 /* convert encoding of file from UTF-16 to UTF-8 */
1028 char temp_file[MAX_PATH];
1029 int fe;
1030 rb->lseek(fd, 2, SEEK_SET);
1031 rb->snprintf(temp_file, MAX_PATH, "%s~", current.lrc_file);
1032 fe = rb->creat(temp_file, 0666);
1033 if (fe < 0)
1035 rb->close(fd);
1036 return;
1038 rb->write(fe, BOM, BOM_SIZE);
1039 while ((readsize = rb->read(fd, temp_buf, MAX_LINE_LEN)) > 0)
1041 char *end = utf_decode(temp_buf, utf8line, readsize/2);
1042 rb->write(fe, utf8line, end-utf8line);
1044 rb->close(fe);
1045 rb->close(fd);
1046 rb->remove(current.lrc_file);
1047 rb->rename(temp_file, current.lrc_file);
1048 fd = rb->open(current.lrc_file, O_RDONLY);
1049 if (fd < 0) return;
1050 rb->lseek(fd, BOM_SIZE, SEEK_SET); /* skip bom */
1051 encoding = UTF_8;
1055 file_offset = rb->lseek(fd, 0, SEEK_CUR); /* used in line_parser */
1056 while ((readsize = rb->read_line(fd, temp_buf, MAX_LINE_LEN)) > 0)
1058 /* note: parse_snc_line() reads temp_buf for native data. */
1059 rb->iso_decode(temp_buf, utf8line, encoding, readsize+1);
1060 if (!line_parser(utf8line, file_offset))
1061 break;
1062 file_offset += readsize;
1064 rb->close(fd);
1066 current.loaded_lrc = true;
1067 calc_brpos(NULL, 0);
1068 init_time_tag();
1070 return;
1073 #ifdef LRC_SUPPORT_ID3
1074 /*******************************
1075 * read lyrics from id3
1076 *******************************/
1077 /* taken from apps/metadata/mp3.c */
1078 static unsigned long unsync(unsigned long b0, unsigned long b1,
1079 unsigned long b2, unsigned long b3)
1081 return (((long)(b0 & 0x7F) << (3*7)) |
1082 ((long)(b1 & 0x7F) << (2*7)) |
1083 ((long)(b2 & 0x7F) << (1*7)) |
1084 ((long)(b3 & 0x7F) << (0*7)));
1087 static unsigned long bytes2int(unsigned long b0, unsigned long b1,
1088 unsigned long b2, unsigned long b3)
1090 return (((long)(b0 & 0xFF) << (3*8)) |
1091 ((long)(b1 & 0xFF) << (2*8)) |
1092 ((long)(b2 & 0xFF) << (1*8)) |
1093 ((long)(b3 & 0xFF) << (0*8)));
1096 static int unsynchronize(char* tag, int len, bool *ff_found)
1098 int i;
1099 unsigned char c;
1100 unsigned char *rp, *wp;
1101 bool _ff_found = false;
1102 if(ff_found) _ff_found = *ff_found;
1104 wp = rp = (unsigned char *)tag;
1106 rp = (unsigned char *)tag;
1107 for(i = 0; i<len; i++) {
1108 /* Read the next byte and write it back, but don't increment the
1109 write pointer */
1110 c = *rp++;
1111 *wp = c;
1112 if(_ff_found) {
1113 /* Increment the write pointer if it isn't an unsynch pattern */
1114 if(c != 0)
1115 wp++;
1116 _ff_found = false;
1117 } else {
1118 if(c == 0xff)
1119 _ff_found = true;
1120 wp++;
1123 if(ff_found) *ff_found = _ff_found;
1124 return (long)wp - (long)tag;
1127 static int read_unsynched(int fd, void *buf, int len, bool *ff_found)
1129 int i;
1130 int rc;
1131 int remaining = len;
1132 char *wp;
1134 wp = buf;
1136 while(remaining) {
1137 rc = rb->read(fd, wp, remaining);
1138 if(rc <= 0)
1139 return rc;
1141 i = unsynchronize(wp, remaining, ff_found);
1142 remaining -= i;
1143 wp += i;
1146 return len;
1149 static unsigned char* utf8cpy(const unsigned char *src,
1150 unsigned char *dst, int count)
1152 rb->strlcpy(dst, src, count+1);
1153 return dst+rb->strlen(dst);
1156 static void parse_id3v2(int fd)
1158 int minframesize;
1159 int size;
1160 long framelen;
1161 char header[10];
1162 char tmp[8];
1163 unsigned char version;
1164 int bytesread = 0;
1165 unsigned char global_flags;
1166 int flags;
1167 int skip;
1168 bool global_unsynch = false;
1169 bool global_ff_found = false;
1170 bool unsynch = false;
1171 int rc;
1172 enum {NOLT, SYLT, USLT} type = NOLT;
1174 /* Bail out if the tag is shorter than 10 bytes */
1175 if(current.id3->id3v2len < 10)
1176 return;
1178 /* Read the ID3 tag version from the header */
1179 if(10 != rb->read(fd, header, 10))
1180 return;
1182 /* Get the total ID3 tag size */
1183 size = current.id3->id3v2len - 10;
1185 version = current.id3->id3version;
1186 switch ( version )
1188 case ID3_VER_2_2:
1189 minframesize = 8;
1190 break;
1192 case ID3_VER_2_3:
1193 minframesize = 12;
1194 break;
1196 case ID3_VER_2_4:
1197 minframesize = 12;
1198 break;
1200 default:
1201 /* unsupported id3 version */
1202 return;
1205 global_flags = header[5];
1207 /* Skip the extended header if it is present */
1208 if(global_flags & 0x40) {
1209 skip = 0;
1211 if(version == ID3_VER_2_3) {
1212 if(10 != rb->read(fd, header, 10))
1213 return;
1214 /* The 2.3 extended header size doesn't include the header size
1215 field itself. Also, it is not unsynched. */
1216 framelen =
1217 bytes2int(header[0], header[1], header[2], header[3]) + 4;
1219 /* Skip the rest of the header */
1220 rb->lseek(fd, framelen - 10, SEEK_CUR);
1223 if(version >= ID3_VER_2_4) {
1224 if(4 != rb->read(fd, header, 4))
1225 return;
1227 /* The 2.4 extended header size does include the entire header,
1228 so here we can just skip it. This header is unsynched. */
1229 framelen = unsync(header[0], header[1],
1230 header[2], header[3]);
1232 rb->lseek(fd, framelen - 4, SEEK_CUR);
1236 /* Is unsynchronization applied? */
1237 if(global_flags & 0x80) {
1238 global_unsynch = true;
1241 /* We must have at least minframesize bytes left for the
1242 * remaining frames to be interesting */
1243 while (size >= minframesize) {
1244 flags = 0;
1246 /* Read frame header and check length */
1247 if(version >= ID3_VER_2_3) {
1248 if(global_unsynch && version <= ID3_VER_2_3)
1249 rc = read_unsynched(fd, header, 10, &global_ff_found);
1250 else
1251 rc = rb->read(fd, header, 10);
1252 if(rc != 10)
1253 return;
1254 /* Adjust for the 10 bytes we read */
1255 size -= 10;
1257 flags = bytes2int(0, 0, header[8], header[9]);
1259 if (version >= ID3_VER_2_4) {
1260 framelen = unsync(header[4], header[5],
1261 header[6], header[7]);
1262 } else {
1263 /* version .3 files don't use synchsafe ints for
1264 * size */
1265 framelen = bytes2int(header[4], header[5],
1266 header[6], header[7]);
1268 } else {
1269 if(6 != rb->read(fd, header, 6))
1270 return;
1271 /* Adjust for the 6 bytes we read */
1272 size -= 6;
1274 framelen = bytes2int(0, header[3], header[4], header[5]);
1277 if(framelen == 0){
1278 if (header[0] == 0 && header[1] == 0 && header[2] == 0)
1279 return;
1280 else
1281 continue;
1284 unsynch = false;
1286 if(flags)
1288 if (version >= ID3_VER_2_4) {
1289 if(flags & 0x0040) { /* Grouping identity */
1290 rb->lseek(fd, 1, SEEK_CUR); /* Skip 1 byte */
1291 framelen--;
1293 } else {
1294 if(flags & 0x0020) { /* Grouping identity */
1295 rb->lseek(fd, 1, SEEK_CUR); /* Skip 1 byte */
1296 framelen--;
1300 if(flags & 0x000c) /* Compression or encryption */
1302 /* Skip it */
1303 size -= framelen;
1304 rb->lseek(fd, framelen, SEEK_CUR);
1305 continue;
1308 if(flags & 0x0002) /* Unsynchronization */
1309 unsynch = true;
1311 if (version >= ID3_VER_2_4) {
1312 if(flags & 0x0001) { /* Data length indicator */
1313 if(4 != rb->read(fd, tmp, 4))
1314 return;
1316 /* We don't need the data length */
1317 framelen -= 4;
1322 if (framelen == 0)
1323 continue;
1325 if (framelen < 0)
1326 return;
1328 if(!rb->memcmp( header, "SLT", 3 ) ||
1329 !rb->memcmp( header, "SYLT", 4 ))
1331 /* found a supported tag */
1332 type = SYLT;
1333 break;
1335 else if(!rb->memcmp( header, "ULT", 3 ) ||
1336 !rb->memcmp( header, "USLT", 4 ))
1338 /* found a supported tag */
1339 type = USLT;
1340 break;
1342 else
1344 /* not a supported tag*/
1345 if(global_unsynch && version <= ID3_VER_2_3) {
1346 size -= read_unsynched(fd, lrc_buffer, framelen, &global_ff_found);
1347 } else {
1348 size -= framelen;
1349 if( rb->lseek(fd, framelen, SEEK_CUR) == -1 )
1350 return;
1354 if(type == NOLT)
1355 return;
1357 int encoding = 0, chsiz;
1358 char *tag, *p, utf8line[MAX_LINE_LEN*3];
1359 unsigned char* (*utf_decode)(const unsigned char *,
1360 unsigned char *, int) = NULL;
1361 /* use middle of lrc_buffer to store tag data. */
1362 if(framelen >= LRC_BUFFER_SIZE/3)
1363 framelen = LRC_BUFFER_SIZE/3-1;
1364 tag = lrc_buffer+LRC_BUFFER_SIZE*2/3-framelen-1;
1365 if(global_unsynch && version <= ID3_VER_2_3)
1366 bytesread = read_unsynched(fd, tag, framelen, &global_ff_found);
1367 else
1368 bytesread = rb->read(fd, tag, framelen);
1370 if( bytesread != framelen )
1371 return;
1373 if(unsynch || (global_unsynch && version >= ID3_VER_2_4))
1374 bytesread = unsynchronize(tag, bytesread, NULL);
1376 tag[bytesread] = 0;
1377 encoding = tag[0];
1378 p = tag;
1379 /* skip some data */
1380 if(type == SYLT) {
1381 p += 6;
1382 } else {
1383 p += 4;
1386 /* check encoding and skip content descriptor */
1387 switch (encoding) {
1388 case 0x01: /* Unicode with or without BOM */
1389 case 0x02:
1391 /* Now check if there is a BOM
1392 (zero-width non-breaking space, 0xfeff)
1393 and if it is in little or big endian format */
1394 if(!rb->memcmp(p, "\xff\xfe", 2)) { /* Little endian? */
1395 utf_decode = rb->utf16LEdecode;
1396 } else if(!rb->memcmp(p, "\xfe\xff", 2)) { /* Big endian? */
1397 utf_decode = rb->utf16BEdecode;
1398 } else
1399 utf_decode = NULL;
1401 encoding = NUM_CODEPAGES;
1402 do {
1403 size = p[0] | p[1];
1404 p += 2;
1405 } while(size);
1406 chsiz = 2;
1407 break;
1409 default:
1410 utf_decode = utf8cpy;
1411 if(encoding == 0x03) /* UTF-8 encoded string */
1412 encoding = UTF_8;
1413 else
1414 encoding = prefs.encoding;
1415 p += rb->strlen(p)+1;
1416 chsiz = 1;
1417 break;
1419 if(encoding == NUM_CODEPAGES)
1421 /* check if there is a BOM */
1422 if(!rb->memcmp(p, "\xff\xfe", 2)) { /* Little endian? */
1423 utf_decode = rb->utf16LEdecode;
1424 p += 2;
1425 } else if(!rb->memcmp(p, "\xfe\xff", 2)) { /* Big endian? */
1426 utf_decode = rb->utf16BEdecode;
1427 p += 2;
1428 } else if(!utf_decode) {
1429 /* If there is no BOM (which is a specification violation),
1430 let's try to guess it. If one of the bytes is 0x00, it is
1431 probably the most significant one. */
1432 if(p[1] == 0)
1433 utf_decode = rb->utf16LEdecode;
1434 else
1435 utf_decode = rb->utf16BEdecode;
1438 bytesread -= (long)p - (long)tag;
1439 tag = p;
1441 while ( bytesread > 0
1442 && lrc_buffer_used+bytesread < LRC_BUFFER_SIZE*2/3
1443 && LRC_BUFFER_SIZE*2/3 < lrc_buffer_end)
1445 bool is_crlf = false;
1446 struct lrc_line *lrc_line = alloc_buf(sizeof(struct lrc_line));
1447 if(!lrc_line)
1448 break;
1449 lrc_line->file_offset = -1;
1450 if(type == USLT)
1452 /* replace 0x0a and 0x0d with 0x00 */
1453 p = tag;
1454 while(1) {
1455 utf_decode(p, tmp, 2);
1456 if(!tmp[0]) break;
1457 if(tmp[0] == 0x0d || tmp[0] == 0x0a)
1459 if(tmp[0] == 0x0d && tmp[1] == 0x0a)
1460 is_crlf = true;
1461 p[0] = 0;
1462 p[chsiz-1] = 0;
1463 break;
1465 p += chsiz;
1468 if(encoding == NUM_CODEPAGES)
1470 unsigned char* utf8 = utf8line;
1471 p = tag;
1472 do {
1473 utf8 = utf_decode(p, utf8, 1);
1474 p += 2;
1475 } while(*(utf8-1));
1477 else
1479 size = rb->strlen(tag)+1;
1480 rb->iso_decode(tag, utf8line, encoding, size);
1481 p = tag+size;
1484 if(type == SYLT) { /* timestamp */
1485 lrc_line->time_start = bytes2int(p[0], p[1], p[2], p[3]);
1486 lrc_line->old_time_start = lrc_line->time_start;
1487 p += 4;
1488 utf_decode(p, tmp, 1);
1489 if(tmp[0] == 0x0a)
1490 p += chsiz;
1491 } else { /* USLT */
1492 lrc_line->time_start = 0;
1493 lrc_line->old_time_start = -1;
1494 if(is_crlf) p += chsiz;
1496 bytesread -= (long)p - (long)tag;
1497 tag = p;
1498 if(!add_lrc_line(lrc_line, utf8line))
1499 break;
1502 current.type = ID3_SYLT-SYLT+type;
1503 rb->strcpy(current.lrc_file, current.mp3_file);
1505 current.loaded_lrc = true;
1506 calc_brpos(NULL, 0);
1507 init_time_tag();
1509 return;
1512 static bool read_id3(void)
1514 int fd;
1516 if(current.id3->codectype != AFMT_MPA_L1
1517 && current.id3->codectype != AFMT_MPA_L2
1518 && current.id3->codectype != AFMT_MPA_L3)
1519 return false;
1521 fd = rb->open(current.mp3_file, O_RDONLY);
1522 if(fd < 0) return false;
1523 current.loaded_lrc = false;
1524 parse_id3v2(fd);
1525 rb->close(fd);
1526 return current.loaded_lrc;
1528 #endif /* LRC_SUPPORT_ID3 */
1530 /*******************************
1531 * Display information
1532 *******************************/
1533 static void display_state(void)
1535 const char *str = NULL;
1537 if (AUDIO_STOP)
1538 str = "Audio Stopped";
1539 else if (current.found_lrc)
1541 if (!current.loaded_lrc)
1542 str = "Loading lrc";
1543 else if (!current.ll_head)
1544 str = "No lyrics";
1547 #ifdef HAVE_LCD_BITMAP
1548 const char *info = NULL;
1550 if (AUDIO_PLAY && prefs.display_title)
1552 char *title = (current.title? current.title: current.id3->title);
1553 char *artist = (current.artist? current.artist: current.id3->artist);
1555 if (artist != NULL && title != NULL)
1557 rb->snprintf(temp_buf, MAX_LINE_LEN, "%s/%s", title, artist);
1558 info = temp_buf;
1560 else if (title != NULL)
1561 info = title;
1562 else if (current.mp3_file[0] == '/')
1563 info = rb->strrchr(current.mp3_file, '/')+1;
1564 else
1565 info = "(no info)";
1568 int i, w, h;
1569 struct screen* display;
1570 FOR_NB_SCREENS(i)
1572 display = rb->screens[i];
1573 display->set_viewport(&vp_info[i]);
1574 display->clear_viewport();
1575 if (info)
1576 display->puts_scroll(0, 0, info);
1577 if (str)
1579 display->set_viewport(&vp_lyrics[i]);
1580 display->clear_viewport();
1581 display->getstringsize(str, &w, &h);
1582 if (vp_lyrics[i].width - w < 0)
1583 display->puts_scroll(0, vp_lyrics[i].height/font_ui_height/2,
1584 str);
1585 else
1586 display->putsxy((vp_lyrics[i].width - w)*prefs.align/2,
1587 (vp_lyrics[i].height-font_ui_height)/2, str);
1588 display->set_viewport(&vp_info[i]);
1590 display->update_viewport();
1591 display->set_viewport(NULL);
1593 #else
1594 /* there is no place to display title or artist. */
1595 rb->lcd_clear_display();
1596 if (str)
1597 rb->lcd_puts_scroll(0, 0, str);
1598 rb->lcd_update();
1599 #endif /* HAVE_LCD_BITMAP */
1602 static void display_time(void)
1604 rb->snprintf(temp_buf, MAX_LINE_LEN, "%ld:%02ld/%ld:%02ld",
1605 current.elapsed/60000, (current.elapsed/1000)%60,
1606 current.length/60000, (current.length)/1000%60);
1607 #ifdef HAVE_LCD_BITMAP
1608 int y = (prefs.display_title? font_ui_height:0), i;
1609 FOR_NB_SCREENS(i)
1611 struct screen* display = rb->screens[i];
1612 display->set_viewport(&vp_info[i]);
1613 display->setfont(FONT_SYSFIXED);
1614 display->putsxy(0, y, temp_buf);
1615 rb->gui_scrollbar_draw(display, 0, y+SYSFONT_HEIGHT+1,
1616 vp_info[i].width, SYSFONT_HEIGHT-2,
1617 current.length, 0, current.elapsed, HORIZONTAL);
1618 display->update_viewport_rect(0, y, vp_info[i].width, SYSFONT_HEIGHT*2);
1619 display->setfont(FONT_UI);
1620 display->set_viewport(NULL);
1622 #else
1623 rb->lcd_puts(0, 0, temp_buf);
1624 rb->lcd_update();
1625 #endif /* HAVE_LCD_BITMAP */
1628 /*******************************
1629 * Display lyrics
1630 *******************************/
1631 #ifdef HAVE_LCD_BITMAP
1632 static inline void set_to_default(struct screen *display)
1634 #if (LCD_DEPTH > 1)
1635 #ifdef HAVE_LCD_REMOTE
1636 if (display->screen_type != SCREEN_REMOTE)
1637 #endif
1638 display->set_foreground(prefs.active_color);
1639 #endif
1640 display->set_drawmode(DRMODE_SOLID);
1642 static inline void set_to_active(struct screen *display)
1644 #if (LCD_DEPTH > 1)
1645 #ifdef HAVE_LCD_REMOTE
1646 if (display->screen_type == SCREEN_REMOTE)
1647 display->set_drawmode(DRMODE_INVERSEVID);
1648 else
1649 #endif
1651 display->set_foreground(prefs.active_color);
1652 display->set_drawmode(DRMODE_SOLID);
1654 #else /* LCD_DEPTH == 1 */
1655 display->set_drawmode(DRMODE_INVERSEVID);
1656 #endif
1658 static inline void set_to_inactive(struct screen *display)
1660 #if (LCD_DEPTH > 1)
1661 #ifdef HAVE_LCD_REMOTE
1662 if (display->screen_type != SCREEN_REMOTE)
1663 #endif
1664 display->set_foreground(prefs.inactive_color);
1665 #endif
1666 display->set_drawmode(DRMODE_SOLID);
1669 static int display_lrc_line(struct lrc_line *lrc_line, int ypos, int i)
1671 struct screen *display = rb->screens[i];
1672 struct lrc_word *lrc_word;
1673 struct lrc_brpos *lrc_brpos;
1674 long time_start, time_end, elapsed;
1675 int count, width, nword;
1676 int xpos;
1677 const char *str;
1678 bool active_line;
1680 time_start = get_time_start(lrc_line);
1681 time_end = get_time_start(lrc_line->next);
1682 active_line = (time_start <= current.elapsed
1683 && time_end > current.elapsed);
1685 if (!lrc_line->width)
1687 /* empty line. draw bar during interval. */
1688 long rin = current.elapsed - time_start;
1689 long len = time_end - time_start;
1690 if (current.wipe && active_line && len >= 3500)
1692 elapsed = rin * vp_lyrics[i].width / len;
1693 set_to_active(display);
1694 display->drawrect(0, ypos+font_ui_height/4,
1695 vp_lyrics[i].width, font_ui_height/2);
1696 display->fillrect(1, ypos+font_ui_height/4+1,
1697 elapsed-1, font_ui_height/2-2);
1698 set_to_inactive(display);
1699 display->fillrect(elapsed, ypos+font_ui_height/4+1,
1700 vp_lyrics[i].width-elapsed-1, font_ui_height/2-2);
1701 set_to_default(display);
1703 return ypos + font_ui_height;
1706 lrc_brpos = calc_brpos(lrc_line, i);
1707 /* initialize line */
1708 xpos = (vp_lyrics[i].width - lrc_brpos->width)*prefs.align/2;
1709 count = 0;
1710 width = 0;
1712 active_line = active_line || !prefs.active_one_line;
1713 nword = lrc_line->nword-1;
1714 lrc_word = lrc_line->words + nword;
1715 str = lrc_word->word;
1716 /* only time_start of first word could be -1 */
1717 if (lrc_word->time_start != -1)
1718 time_end = get_word_time_start(lrc_word);
1719 else
1720 time_end = time_start;
1721 do {
1722 time_start = time_end;
1723 if (nword > 0)
1724 time_end = get_word_time_start(lrc_word-1);
1725 else
1726 time_end = get_time_start(lrc_line->next);
1728 if (time_start > current.elapsed || !active_line)
1730 /* inactive */
1731 elapsed = 0;
1733 else if (!current.wipe || time_end <= current.elapsed)
1735 /* active whole word */
1736 elapsed = lrc_word->width;
1738 else
1740 /* wipe word */
1741 long rin = current.elapsed - time_start;
1742 long len = time_end - time_start;
1743 elapsed = rin * lrc_word->width / len;
1746 int word_count = lrc_word->count;
1747 int word_width = lrc_word->width;
1748 set_to_active(display);
1749 while (word_count > 0 && word_width > 0)
1751 int c = lrc_brpos->count - count;
1752 int w = lrc_brpos->width - width;
1753 if (c > word_count || w > word_width)
1755 c = word_count;
1756 w = word_width;
1758 if (elapsed <= 0)
1760 set_to_inactive(display);
1762 else if (elapsed < w)
1764 /* wipe text */
1765 display->fillrect(xpos, ypos, elapsed, font_ui_height);
1766 set_to_inactive(display);
1767 display->fillrect(xpos+elapsed, ypos,
1768 w-elapsed, font_ui_height);
1769 #if (LCD_DEPTH > 1)
1770 #ifdef HAVE_LCD_REMOTE
1771 if (display->screen_type == SCREEN_REMOTE)
1772 display->set_drawmode(DRMODE_INVERSEVID);
1773 else
1774 #endif
1775 display->set_drawmode(DRMODE_BG);
1776 #else
1777 display->set_drawmode(DRMODE_INVERSEVID);
1778 #endif
1780 rb->strlcpy(temp_buf, str, c+1);
1781 display->putsxy(xpos, ypos, temp_buf);
1782 str += c;
1783 xpos += w;
1784 count += c;
1785 width += w;
1786 word_count -= c;
1787 word_width -= w;
1788 elapsed -= w;
1789 if (count >= lrc_brpos->count || width >= lrc_brpos->width)
1791 /* prepare for next line */
1792 lrc_brpos++;
1793 str = lrc_skip_space(str);
1794 xpos = (vp_lyrics[i].width - lrc_brpos->width)*prefs.align/2;
1795 ypos += font_ui_height;
1796 count = 0;
1797 width = 0;
1800 lrc_word--;
1801 } while (nword--);
1802 set_to_default(display);
1803 return ypos;
1805 #endif /* HAVE_LCD_BITMAP */
1807 static void display_lrcs(void)
1809 long time_start, time_end, rin, len;
1810 int i, nline[NB_SCREENS] = {0};
1811 struct lrc_line *lrc_center = current.ll_head;
1813 if (!lrc_center) return;
1815 while (get_time_start(lrc_center->next) <= current.elapsed)
1817 nline[SCREEN_MAIN] += lrc_center->nline[SCREEN_MAIN];
1818 #ifdef HAVE_REMOTE_LCD
1819 nline[SCREEN_REMOTE] += lrc_center->nline[SCREEN_REMOTE];
1820 #endif
1821 lrc_center = lrc_center->next;
1824 time_start = get_time_start(lrc_center);
1825 time_end = get_time_start(lrc_center->next);
1826 rin = current.elapsed - time_start;
1827 len = time_end - time_start;
1829 struct screen *display;
1830 FOR_NB_SCREENS(i)
1832 display = rb->screens[i];
1833 /* display current line at the center of the viewport */
1834 display->set_viewport(&vp_lyrics[i]);
1835 display->clear_viewport();
1836 #ifdef HAVE_LCD_BITMAP
1837 struct lrc_line *lrc_line;
1838 int y, ypos = 0, nblines = vp_lyrics[i].height/font_ui_height;
1839 y = (nblines-1)/2;
1840 if (rin < 0)
1842 /* current.elapsed < time of first lrc */
1843 if (!current.wipe)
1844 ypos = (time_start - current.elapsed)
1845 * font_ui_height / time_start;
1846 else
1847 y++;
1849 else if (len > 0)
1851 if (!current.wipe)
1852 ypos = - rin * lrc_center->nline[i] * font_ui_height / len;
1853 else
1855 long elapsed = rin * lrc_center->width / len;
1856 struct lrc_brpos *lrc_brpos = calc_brpos(lrc_center, i);
1857 while (elapsed > lrc_brpos->width)
1859 elapsed -= lrc_brpos->width;
1860 y--;
1861 lrc_brpos++;
1866 /* find first line to display */
1867 y -= nline[i];
1868 lrc_line = current.ll_head;
1869 while (y < -lrc_line->nline[i])
1871 y += lrc_line->nline[i];
1872 lrc_line = lrc_line->next;
1875 ypos += y*font_ui_height;
1876 while (lrc_line && ypos < vp_lyrics[i].height)
1878 ypos = display_lrc_line(lrc_line, ypos, i);
1879 lrc_line = lrc_line->next;
1881 if (!lrc_line && ypos < vp_lyrics[i].height)
1882 display->putsxy(0, ypos, "[end]");
1883 #else /* HAVE_LCD_CHARCELLS */
1884 struct lrc_line *lrc_line = lrc_center;
1885 struct lrc_brpos *lrc_brpos = calc_brpos(lrc_line, i);
1886 long elapsed = 0;
1887 const char *str = get_lrc_str(lrc_line);
1888 int x = vp_lyrics[i].width/2, y = 0;
1890 if (rin >= 0 && len > 0)
1892 elapsed = rin * lrc_center->width / len;
1893 while (elapsed > lrc_brpos->width)
1895 elapsed -= lrc_brpos->width;
1896 str = lrc_skip_space(str+lrc_brpos->count);
1897 lrc_brpos++;
1900 rb->strlcpy(temp_buf, str, lrc_brpos->count+1);
1902 x -= elapsed;
1903 if (x < 0)
1904 display->puts(0, y, temp_buf + rb->utf8seek(temp_buf, -x));
1905 else
1906 display->puts(x, y, temp_buf);
1907 x += rb->utf8length(temp_buf)+1;
1908 lrc_line = lrc_line->next;
1909 if (!lrc_line && x < vp_lyrics[i].width)
1911 if (x < vp_lyrics[i].width/2)
1912 x = vp_lyrics[i].width/2;
1913 display->puts(x, y, "[end]");
1915 #endif /* HAVE_LCD_BITMAP */
1916 display->update_viewport();
1917 display->set_viewport(NULL);
1921 /*******************************
1922 * Browse lyrics and edit time.
1923 *******************************/
1924 /* point playing line in lyrics */
1925 static enum themable_icons get_icon(int selected, void * data)
1927 (void) data;
1928 struct lrc_line *lrc_line = get_lrc_line(selected);
1929 if (lrc_line)
1931 long time_start = get_time_start(lrc_line);
1932 long time_end = get_time_start(lrc_line->next);
1933 long elapsed = current.id3->elapsed;
1934 if (time_start <= elapsed && time_end > elapsed)
1935 return Icon_Moving;
1937 return Icon_NOICON;
1939 static const char *get_lrc_timeline(int selected, void *data,
1940 char *buffer, size_t buffer_len)
1942 (void) data;
1943 struct lrc_line *lrc_line = get_lrc_line(selected);
1944 if (lrc_line)
1946 format_time_tag(temp_buf, get_time_start(lrc_line));
1947 rb->snprintf(buffer, buffer_len, "[%s]%s",
1948 temp_buf, get_lrc_str(lrc_line));
1949 return buffer;
1951 return NULL;
1954 static void save_changes(void)
1956 char new_file[MAX_PATH], *p;
1957 bool success = false;
1958 int fd, fe;
1959 if (!current.changed_lrc)
1960 return;
1961 rb->splash(HZ/2, "Saving changes...");
1962 if (current.type > NUM_TYPES)
1964 /* save changes to new .lrc8 file */
1965 rb->strcpy(new_file, current.lrc_file);
1966 p = rb->strrchr(new_file, '.');
1967 rb->strcpy(p, extentions[LRC8]);
1969 else if (current.type == TXT)
1971 /* save changes to new .lrc file */
1972 rb->strcpy(new_file, current.lrc_file);
1973 p = rb->strrchr(new_file, '.');
1974 rb->strcpy(p, extentions[LRC]);
1976 else
1978 /* file already exists. use temp file. */
1979 rb->snprintf(new_file, MAX_PATH, "%s~", current.lrc_file);
1981 fd = rb->creat(new_file, 0666);
1982 fe = rb->open(current.lrc_file, O_RDONLY);
1983 if (fd >= 0 && fe >= 0)
1985 struct lrc_line *lrc_line, *temp_lrc;
1986 off_t curr = 0, next = 0, size = 0, offset = 0;
1987 for (lrc_line = current.ll_head; lrc_line;
1988 lrc_line = lrc_line->next)
1990 /* apply offset and set old_time_start -1 to indicate
1991 that time tag is not saved yet. */
1992 lrc_line->time_start = get_time_start(lrc_line);
1993 lrc_line->old_time_start = -1;
1995 current.offset = 0;
1996 if (current.type > NUM_TYPES)
1997 curr = -1;
1998 else
1999 size = rb->filesize(fe);
2000 while (curr < size)
2002 /* find offset of next tag */
2003 lrc_line = NULL;
2004 for (temp_lrc = current.ll_head, next = size;
2005 temp_lrc; temp_lrc = temp_lrc->next)
2007 offset = temp_lrc->file_offset;
2008 if (offset == -1 ||
2009 (offset < next && temp_lrc->old_time_start == -1))
2011 lrc_line = temp_lrc;
2012 next = offset;
2013 if (offset <= curr) break;
2016 offset = current.offset_file_offset;
2017 if (offset >= curr && offset < next)
2019 lrc_line = NULL;
2020 next = offset;
2021 current.offset_file_offset = -1;
2023 if (next > curr)
2025 if (curr == -1) curr = 0;
2026 /* copy before the next tag */
2027 while (curr < next)
2029 ssize_t count = next-curr;
2030 if (count > MAX_LINE_LEN)
2031 count = MAX_LINE_LEN;
2032 if (rb->read(fe, temp_buf, count)!=count)
2033 break;
2034 rb->write(fd, temp_buf, count);
2035 curr += count;
2037 if (curr < next || curr >= size) break;
2039 /* write tag to new file and skip tag in backup */
2040 if (lrc_line != NULL)
2042 lrc_line->file_offset = rb->lseek(fd, 0, SEEK_CUR);
2043 lrc_line->old_time_start = lrc_line->time_start;
2044 long t = lrc_line->time_start;
2045 if (current.type == SNC)
2047 rb->fdprintf(fd, "%02ld%02ld%02ld%02ld", (t/3600000)%100,
2048 (t/60000)%60, (t/1000)%60, (t/10)%100);
2049 /* skip time tag */
2050 curr += rb->read(fe, temp_buf, 8);
2052 else /* LRC || LRC8 */
2054 format_time_tag(temp_buf, t);
2055 rb->fdprintf(fd, "[%s]", temp_buf);
2057 if (next == -1)
2059 rb->fdprintf(fd, "%s\n", get_lrc_str(lrc_line));
2062 if (current.type == LRC || current.type == LRC8)
2064 /* skip both time tag and offset tag */
2065 while (curr++<size && rb->read(fe, temp_buf, 1)==1)
2066 if (temp_buf[0]==']') break;
2069 success = (curr>=size);
2071 if (fe >= 0) rb->close(fe);
2072 if (fd >= 0) rb->close(fd);
2074 if (success)
2076 if (current.type == TXT)
2078 current.type = LRC;
2079 rb->strcpy(current.lrc_file, new_file);
2081 else if (current.type > NUM_TYPES)
2083 current.type = LRC8;
2084 rb->strcpy(current.lrc_file, new_file);
2086 else
2088 rb->remove(current.lrc_file);
2089 rb->rename(new_file, current.lrc_file);
2092 else
2094 rb->remove(new_file);
2095 rb->splash(HZ, "Could not save changes.");
2097 rb->reload_directory();
2098 current.changed_lrc = false;
2100 static int timetag_editor(void)
2102 struct gui_synclist gui_editor;
2103 struct lrc_line *lrc_line;
2104 bool exit = false;
2105 int button, idx, selected = 0;
2107 if (current.id3 == NULL || !current.ll_head)
2109 rb->splash(HZ, "No lyrics");
2110 return LRC_GOTO_MAIN;
2113 get_lrc_line(-1); /* initialize static variables */
2115 for (lrc_line = current.ll_head, idx = 0;
2116 lrc_line; lrc_line = lrc_line->next, idx++)
2118 long time_start = get_time_start(lrc_line);
2119 long time_end = get_time_start(lrc_line->next);
2120 long elapsed = current.id3->elapsed;
2121 if (time_start <= elapsed && time_end > elapsed)
2122 selected = idx;
2125 rb->gui_synclist_init(&gui_editor, &get_lrc_timeline, NULL, false, 1, NULL);
2126 rb->gui_synclist_set_nb_items(&gui_editor, current.nlrcline);
2127 rb->gui_synclist_set_icon_callback(&gui_editor, get_icon);
2128 rb->gui_synclist_set_title(&gui_editor, "Timetag Editor",
2129 Icon_Menu_functioncall);
2130 rb->gui_synclist_select_item(&gui_editor, selected);
2131 rb->gui_synclist_draw(&gui_editor);
2133 while (!exit)
2135 button = rb->get_action(CONTEXT_TREE, TIMEOUT_BLOCK);
2136 if (rb->gui_synclist_do_button(&gui_editor, &button,
2137 LIST_WRAP_UNLESS_HELD))
2138 continue;
2140 switch (button)
2142 case ACTION_STD_OK:
2143 idx = rb->gui_synclist_get_sel_pos(&gui_editor);
2144 lrc_line = get_lrc_line(idx);
2145 if (lrc_line)
2147 set_time_start(lrc_line, current.id3->elapsed-500);
2148 rb->gui_synclist_draw(&gui_editor);
2150 break;
2151 case ACTION_STD_CONTEXT:
2152 idx = rb->gui_synclist_get_sel_pos(&gui_editor);
2153 lrc_line = get_lrc_line(idx);
2154 if (lrc_line)
2156 long temp_time = get_time_start(lrc_line);
2157 if (lrc_set_time(get_lrc_str(lrc_line), NULL,
2158 &temp_time, 10, 0, current.length,
2159 LST_SET_MSEC|LST_SET_SEC|LST_SET_MIN) == 1)
2160 return PLUGIN_USB_CONNECTED;
2161 set_time_start(lrc_line, temp_time);
2162 rb->gui_synclist_draw(&gui_editor);
2164 break;
2165 case ACTION_TREE_STOP:
2166 case ACTION_STD_CANCEL:
2167 exit = true;
2168 break;
2169 default:
2170 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
2171 return PLUGIN_USB_CONNECTED;
2172 break;
2176 FOR_NB_SCREENS(idx)
2177 rb->screens[idx]->stop_scroll();
2179 if (current.changed_lrc)
2181 MENUITEM_STRINGLIST(save_menu, "Save Changes?", NULL,
2182 "Yes", "No (save later)", "Discard All Changes")
2183 button = 0;
2184 exit = false;
2185 while (!exit)
2187 switch (rb->do_menu(&save_menu, &button, NULL, false))
2189 case 0:
2190 sort_lrcs();
2191 save_changes();
2192 exit = true;
2193 break;
2194 case 1:
2195 sort_lrcs();
2196 exit = true;
2197 break;
2198 case 2:
2199 init_time_tag();
2200 exit = true;
2201 break;
2202 case MENU_ATTACHED_USB:
2203 return PLUGIN_USB_CONNECTED;
2207 return LRC_GOTO_MAIN;
2210 /*******************************
2211 * Settings.
2212 *******************************/
2213 static void load_or_save_settings(bool save)
2215 static const char config_file[] = "lrcplayer.cfg";
2216 static struct configdata config[] = {
2217 #ifdef HAVE_LCD_COLOR
2218 { TYPE_INT, 0, 0xffffff, { .int_p = &prefs.inactive_color },
2219 "inactive color", NULL },
2220 #endif
2221 #ifdef HAVE_LCD_BITMAP
2222 { TYPE_BOOL, 0, 1, { .bool_p = &prefs.wrap }, "wrap", NULL },
2223 { TYPE_BOOL, 0, 1, { .bool_p = &prefs.wipe }, "wipe", NULL },
2224 { TYPE_BOOL, 0, 1, { .bool_p = &prefs.active_one_line },
2225 "active one line", NULL },
2226 { TYPE_INT, 0, 2, { .int_p = &prefs.align }, "align", NULL },
2227 { TYPE_BOOL, 0, 1, { .bool_p = &prefs.statusbar_on },
2228 "statusbar on", NULL },
2229 { TYPE_BOOL, 0, 1, { .bool_p = &prefs.display_title },
2230 "display title", NULL },
2231 #endif
2232 { TYPE_BOOL, 0, 1, { .bool_p = &prefs.display_time },
2233 "display time", NULL },
2234 { TYPE_BOOL, 0, 1, { .bool_p = &prefs.backlight_on },
2235 "backlight on", NULL },
2237 { TYPE_STRING, 0, sizeof(prefs.lrc_directory),
2238 { .string = prefs.lrc_directory }, "lrc directory", NULL },
2239 { TYPE_INT, -1, NUM_CODEPAGES-1, { .int_p = &prefs.encoding },
2240 "encoding", NULL },
2241 #ifdef LRC_SUPPORT_ID3
2242 { TYPE_BOOL, 0, 1, { .bool_p = &prefs.read_id3 }, "read id3", NULL },
2243 #endif
2246 if (!save)
2248 /* initialize setting */
2249 #if LCD_DEPTH > 1
2250 prefs.active_color = rb->lcd_get_foreground();
2251 prefs.inactive_color = LCD_LIGHTGRAY;
2252 #endif
2253 #ifdef HAVE_LCD_BITMAP
2254 prefs.wrap = true;
2255 prefs.wipe = true;
2256 prefs.active_one_line = false;
2257 prefs.align = 1; /* center */
2258 prefs.statusbar_on = false;
2259 prefs.display_title = true;
2260 #endif
2261 prefs.display_time = true;
2262 prefs.backlight_on = false;
2263 #ifdef LRC_SUPPORT_ID3
2264 prefs.read_id3 = true;
2265 #endif
2266 rb->strcpy(prefs.lrc_directory, "/Lyrics");
2267 prefs.encoding = -1; /* default codepage */
2269 configfile_load(config_file, config, ARRAYLEN(config), 0);
2271 else if (rb->memcmp(&old_prefs, &prefs, sizeof(prefs)))
2273 rb->splash(0, "Saving Settings");
2274 configfile_save(config_file, config, ARRAYLEN(config), 0);
2276 rb->memcpy(&old_prefs, &prefs, sizeof(prefs));
2279 static bool lrc_theme_menu(void)
2281 enum {
2282 #ifdef HAVE_LCD_BITMAP
2283 LRC_MENU_STATUSBAR,
2284 LRC_MENU_DISP_TITLE,
2285 #endif
2286 LRC_MENU_DISP_TIME,
2287 #ifdef HAVE_LCD_COLOR
2288 LRC_MENU_INACTIVE_COLOR,
2289 #endif
2290 LRC_MENU_BACKLIGHT,
2293 int selected = 0;
2294 bool exit = false, usb = false;
2296 MENUITEM_STRINGLIST(menu, "Theme Settings", NULL,
2297 #ifdef HAVE_LCD_BITMAP
2298 "Show Statusbar", "Display Title",
2299 #endif
2300 "Display Time",
2301 #ifdef HAVE_LCD_COLOR
2302 "Inactive Color",
2303 #endif
2304 "Backlight Force On");
2306 while (!exit && !usb)
2308 switch (rb->do_menu(&menu, &selected, NULL, false))
2310 #ifdef HAVE_LCD_BITMAP
2311 case LRC_MENU_STATUSBAR:
2312 usb = rb->set_bool("Show Statusbar", &prefs.statusbar_on);
2313 break;
2314 case LRC_MENU_DISP_TITLE:
2315 usb = rb->set_bool("Display Title", &prefs.display_title);
2316 break;
2317 #endif
2318 case LRC_MENU_DISP_TIME:
2319 usb = rb->set_bool("Display Time", &prefs.display_time);
2320 break;
2321 #ifdef HAVE_LCD_COLOR
2322 case LRC_MENU_INACTIVE_COLOR:
2323 usb = rb->set_color(NULL, "Inactive Color",
2324 &prefs.inactive_color, -1);
2325 break;
2326 #endif
2327 case LRC_MENU_BACKLIGHT:
2328 usb = rb->set_bool("Backlight Force On", &prefs.backlight_on);
2329 break;
2330 case MENU_ATTACHED_USB:
2331 usb = true;
2332 break;
2333 default:
2334 exit = true;
2335 break;
2339 return usb;
2342 #ifdef HAVE_LCD_BITMAP
2343 static bool lrc_display_menu(void)
2345 enum {
2346 LRC_MENU_WRAP,
2347 LRC_MENU_WIPE,
2348 LRC_MENU_ALIGN,
2349 LRC_MENU_LINE_MODE,
2352 int selected = 0;
2353 bool exit = false, usb = false;
2355 MENUITEM_STRINGLIST(menu, "Display Settings", NULL,
2356 "Wrap", "Wipe", "Align",
2357 "Activate Only Current Line");
2359 struct opt_items align_names[] = {
2360 {"Left", -1}, {"Center", -1}, {"Right", -1},
2363 while (!exit && !usb)
2365 switch (rb->do_menu(&menu, &selected, NULL, false))
2367 case LRC_MENU_WRAP:
2368 usb = rb->set_bool("Wrap", &prefs.wrap);
2369 break;
2370 case LRC_MENU_WIPE:
2371 usb = rb->set_bool("Wipe", &prefs.wipe);
2372 break;
2373 case LRC_MENU_ALIGN:
2374 usb = rb->set_option("Align", &prefs.align, INT,
2375 align_names, 3, NULL);
2376 break;
2377 case LRC_MENU_LINE_MODE:
2378 usb = rb->set_bool("Activate Only Current Line",
2379 &prefs.active_one_line);
2380 break;
2381 case MENU_ATTACHED_USB:
2382 usb = true;
2383 break;
2384 default:
2385 exit = true;
2386 break;
2390 return usb;
2392 #endif /* HAVE_LCD_BITMAP */
2394 static bool lrc_lyrics_menu(void)
2396 enum {
2397 LRC_MENU_ENCODING,
2398 #ifdef LRC_SUPPORT_ID3
2399 LRC_MENU_READ_ID3,
2400 #endif
2401 LRC_MENU_LRC_DIR,
2404 int selected = 0;
2405 bool exit = false, usb = false;
2407 struct opt_items cp_names[NUM_CODEPAGES+1];
2408 int old_val;
2410 MENUITEM_STRINGLIST(menu, "Lyrics Settings", NULL,
2411 "Encoding",
2412 #ifdef LRC_SUPPORT_ID3
2413 "Read ID3 tag",
2414 #endif
2415 "Lrc Directry");
2417 cp_names[0].string = "Use default codepage";
2418 cp_names[0].voice_id = -1;
2419 for (old_val = 1; old_val < NUM_CODEPAGES+1; old_val++)
2421 cp_names[old_val].string = rb->get_codepage_name(old_val-1);
2422 cp_names[old_val].voice_id = -1;
2425 while (!exit && !usb)
2427 switch (rb->do_menu(&menu, &selected, NULL, false))
2429 case LRC_MENU_ENCODING:
2430 prefs.encoding++;
2431 old_val = prefs.encoding;
2432 usb = rb->set_option("Encoding", &prefs.encoding, INT,
2433 cp_names, NUM_CODEPAGES+1, NULL);
2434 if (prefs.encoding != old_val)
2436 save_changes();
2437 if (current.type < NUM_TYPES)
2439 /* let reload lrc file to apply encoding setting */
2440 reset_current_data();
2443 prefs.encoding--;
2444 break;
2445 #ifdef LRC_SUPPORT_ID3
2446 case LRC_MENU_READ_ID3:
2447 usb = rb->set_bool("Read ID3 tag", &prefs.read_id3);
2448 break;
2449 #endif
2450 case LRC_MENU_LRC_DIR:
2451 rb->strcpy(temp_buf, prefs.lrc_directory);
2452 if (!rb->kbd_input(temp_buf, sizeof(prefs.lrc_directory)))
2453 rb->strcpy(prefs.lrc_directory, temp_buf);
2454 break;
2455 case MENU_ATTACHED_USB:
2456 usb = true;
2457 break;
2458 default:
2459 exit = true;
2460 break;
2464 return usb;
2467 #ifdef LRC_DEBUG
2468 static const char* lrc_debug_data(int selected, void * data,
2469 char * buffer, size_t buffer_len)
2471 (void)data;
2472 switch (selected)
2474 case 0:
2475 rb->strlcpy(buffer, current.mp3_file, buffer_len);
2476 break;
2477 case 1:
2478 rb->strlcpy(buffer, current.lrc_file, buffer_len);
2479 break;
2480 case 2:
2481 rb->snprintf(buffer, buffer_len, "buf usage: %d,%d/%d",
2482 (int)lrc_buffer_used, (int)lrc_buffer_end,
2483 (int)lrc_buffer_size);
2484 break;
2485 case 3:
2486 rb->snprintf(buffer, buffer_len, "line count: %d,%d",
2487 current.nlrcline, current.nlrcbrpos);
2488 break;
2489 case 4:
2490 rb->snprintf(buffer, buffer_len, "loaded lrc? %s",
2491 current.loaded_lrc?"yes":"no");
2492 break;
2493 case 5:
2494 rb->snprintf(buffer, buffer_len, "too many lines? %s",
2495 current.too_many_lines?"yes":"no");
2496 break;
2497 default:
2498 return NULL;
2500 return buffer;
2503 static bool lrc_debug_menu(void)
2505 struct simplelist_info info;
2506 rb->simplelist_info_init(&info, "Debug Menu", 6, NULL);
2507 info.hide_selection = true;
2508 info.scroll_all = true;
2509 info.get_name = lrc_debug_data;
2510 return rb->simplelist_show_list(&info);
2512 #endif
2514 /* returns one of enum lrc_screen or enum plugin_status */
2515 static int lrc_menu(void)
2517 enum {
2518 LRC_MENU_THEME,
2519 #ifdef HAVE_LCD_BITMAP
2520 LRC_MENU_DISPLAY,
2521 #endif
2522 LRC_MENU_LYRICS,
2523 LRC_MENU_PLAYBACK,
2524 #ifdef LRC_DEBUG
2525 LRC_MENU_DEBUG,
2526 #endif
2527 LRC_MENU_OFFSET,
2528 LRC_MENU_TIMETAG_EDITOR,
2529 LRC_MENU_QUIT,
2532 MENUITEM_STRINGLIST(menu, "Lrcplayer Menu", NULL,
2533 "Theme Settings",
2534 #ifdef HAVE_LCD_BITMAP
2535 "Display Settings",
2536 #endif
2537 "Lyrics Settings",
2538 "Playback Control",
2539 #ifdef LRC_DEBUG
2540 "Debug Menu",
2541 #endif
2542 "Time Offset", "Timetag Editor",
2543 "Quit");
2544 int selected = 0, ret = LRC_GOTO_MENU;
2545 bool usb = false;
2547 while (ret == LRC_GOTO_MENU)
2549 switch (rb->do_menu(&menu, &selected, NULL, false))
2551 case LRC_MENU_THEME:
2552 usb = lrc_theme_menu();
2553 break;
2554 #ifdef HAVE_LCD_BITMAP
2555 case LRC_MENU_DISPLAY:
2556 usb = lrc_display_menu();
2557 break;
2558 #endif
2559 case LRC_MENU_LYRICS:
2560 usb = lrc_lyrics_menu();
2561 break;
2562 case LRC_MENU_PLAYBACK:
2563 usb = playback_control(NULL);
2564 ret = LRC_GOTO_MAIN;
2565 break;
2566 #ifdef LRC_DEBUG
2567 case LRC_MENU_DEBUG:
2568 usb = lrc_debug_menu();
2569 ret = LRC_GOTO_MAIN;
2570 break;
2571 #endif
2572 case LRC_MENU_OFFSET:
2573 usb = (lrc_set_time("Time Offset", "sec", &current.offset,
2574 10, -60*1000, 60*1000,
2575 LST_SET_MSEC|LST_SET_SEC) == 1);
2576 ret = LRC_GOTO_MAIN;
2577 break;
2578 case LRC_MENU_TIMETAG_EDITOR:
2579 ret = LRC_GOTO_EDITOR;
2580 break;
2581 case LRC_MENU_QUIT:
2582 ret = PLUGIN_OK;
2583 break;
2584 case MENU_ATTACHED_USB:
2585 usb = true;
2586 break;
2587 default:
2588 ret = LRC_GOTO_MAIN;
2589 break;
2591 if (usb)
2592 ret = PLUGIN_USB_CONNECTED;
2594 return ret;
2597 /*******************************
2598 * Main.
2599 *******************************/
2600 /* returns true if song has changed to know when to load new lyrics. */
2601 static bool check_audio_status(void)
2603 static int last_audio_status = 0;
2604 if (current.ff_rewind == -1)
2605 current.audio_status = rb->audio_status();
2606 current.id3 = rb->audio_current_track();
2607 if ((last_audio_status^current.audio_status)&AUDIO_STATUS_PLAY)
2609 last_audio_status = current.audio_status;
2610 return true;
2612 if (AUDIO_STOP || current.id3 == NULL)
2613 return false;
2614 if (rb->strcmp(current.mp3_file, current.id3->path))
2616 return true;
2618 return false;
2620 static void ff_rewind(long time, bool resume)
2622 if (AUDIO_PLAY)
2624 if (!AUDIO_PAUSE)
2626 resume = true;
2627 rb->audio_pause();
2629 rb->audio_ff_rewind(time);
2630 rb->sleep(HZ/10); /* take affect seeking */
2631 if (resume)
2632 rb->audio_resume();
2636 static int handle_button(void)
2638 int ret = LRC_GOTO_MAIN;
2639 static int step = 0;
2640 int limit, button = rb->get_action(CONTEXT_WPS, HZ/10);
2641 switch (button)
2643 case ACTION_WPS_BROWSE:
2644 #if CONFIG_KEYPAD == ONDIO_PAD
2645 /* ondio doesn't have ACTION_WPS_MENU,
2646 so use ACTION_WPS_BROWSE for menu */
2647 ret = LRC_GOTO_MENU;
2648 break;
2649 #endif
2650 case ACTION_WPS_STOP:
2651 save_changes();
2652 ret = PLUGIN_OK;
2653 break;
2654 case ACTION_WPS_PLAY:
2655 if (AUDIO_STOP && rb->global_status->resume_index != -1)
2657 if (rb->playlist_resume() != -1)
2659 rb->playlist_start(rb->global_status->resume_index,
2660 rb->global_status->resume_offset);
2663 else if (AUDIO_PAUSE)
2664 rb->audio_resume();
2665 else
2666 rb->audio_pause();
2667 break;
2668 case ACTION_WPS_SEEKFWD:
2669 case ACTION_WPS_SEEKBACK:
2670 if (AUDIO_STOP)
2671 break;
2672 if (current.ff_rewind > -1)
2674 if (button == ACTION_WPS_SEEKFWD)
2675 /* fast forwarding, calc max step relative to end */
2676 limit = (current.length - current.ff_rewind) * 3 / 100;
2677 else
2678 /* rewinding, calc max step relative to start */
2679 limit = (current.ff_rewind) * 3 / 100;
2680 limit = MAX(limit, 500);
2682 if (step > limit)
2683 step = limit;
2685 if (button == ACTION_WPS_SEEKFWD)
2686 current.ff_rewind += step;
2687 else
2688 current.ff_rewind -= step;
2690 if (current.ff_rewind > current.length-100)
2691 current.ff_rewind = current.length-100;
2692 if (current.ff_rewind < 0)
2693 current.ff_rewind = 0;
2695 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
2696 step += step >> (rb->global_settings->ff_rewind_accel + 3);
2698 else
2700 current.ff_rewind = current.elapsed;
2701 if (!AUDIO_PAUSE)
2702 rb->audio_pause();
2703 step = 1000 * rb->global_settings->ff_rewind_min_step;
2705 break;
2706 case ACTION_WPS_STOPSEEK:
2707 if (current.ff_rewind == -1)
2708 break;
2709 ff_rewind(current.ff_rewind, !AUDIO_PAUSE);
2710 current.elapsed = current.ff_rewind;
2711 current.ff_rewind = -1;
2712 break;
2713 case ACTION_WPS_SKIPNEXT:
2714 rb->audio_next();
2715 break;
2716 case ACTION_WPS_SKIPPREV:
2717 if (current.elapsed < 3000)
2718 rb->audio_prev();
2719 else
2720 ff_rewind(0, false);
2721 break;
2722 case ACTION_WPS_VOLDOWN:
2723 limit = rb->sound_min(SOUND_VOLUME);
2724 if (--rb->global_settings->volume < limit)
2725 rb->global_settings->volume = limit;
2726 rb->sound_set(SOUND_VOLUME, rb->global_settings->volume);
2727 break;
2728 case ACTION_WPS_VOLUP:
2729 limit = rb->sound_max(SOUND_VOLUME);
2730 if (++rb->global_settings->volume > limit)
2731 rb->global_settings->volume = limit;
2732 rb->sound_set(SOUND_VOLUME, rb->global_settings->volume);
2733 break;
2734 case ACTION_WPS_CONTEXT:
2735 ret = LRC_GOTO_EDITOR;
2736 break;
2737 case ACTION_WPS_MENU:
2738 ret = LRC_GOTO_MENU;
2739 break;
2740 default:
2741 if(rb->default_event_handler(button) == SYS_USB_CONNECTED)
2742 ret = PLUGIN_USB_CONNECTED;
2743 break;
2745 return ret;
2748 static int lrc_main(void)
2750 int ret = LRC_GOTO_MAIN;
2751 int i;
2752 long id3_timeout = 0;
2753 bool update_display_state = true;
2755 #ifdef HAVE_LCD_BITMAP
2756 /* y offset of vp_lyrics */
2757 int h = (prefs.display_title?font_ui_height:0)+
2758 (prefs.display_time?SYSFONT_HEIGHT*2:0);
2760 #endif
2762 FOR_NB_SCREENS(i)
2764 #ifdef HAVE_LCD_BITMAP
2765 rb->viewportmanager_theme_enable(i, prefs.statusbar_on, &vp_info[i]);
2766 vp_lyrics[i] = vp_info[i];
2767 vp_lyrics[i].flags &= ~VP_FLAG_ALIGNMENT_MASK;
2768 vp_lyrics[i].y += h;
2769 vp_lyrics[i].height -= h;
2770 #else
2771 rb->viewport_set_defaults(&vp_lyrics[i], i);
2772 if (prefs.display_time)
2774 vp_lyrics[i].y += 1; /* time */
2775 vp_lyrics[i].height -= 1;
2777 #endif
2780 if (prefs.backlight_on)
2781 backlight_force_on();
2783 #ifdef HAVE_LCD_BITMAP
2784 /* in case settings that may affect break position
2785 * are changed (statusbar_on and wrap). */
2786 if (!current.too_many_lines)
2787 calc_brpos(NULL, 0);
2788 #endif
2790 while (ret == LRC_GOTO_MAIN)
2792 if (check_audio_status())
2794 update_display_state = true;
2795 if (AUDIO_STOP)
2797 current.id3 = NULL;
2798 id3_timeout = 0;
2800 else if (rb->strcmp(current.mp3_file, current.id3->path))
2802 save_changes();
2803 reset_current_data();
2804 rb->strcpy(current.mp3_file, current.id3->path);
2805 id3_timeout = *rb->current_tick+HZ*3;
2806 current.found_lrc = false;
2809 if (current.id3 && current.id3->length)
2811 if (current.ff_rewind == -1)
2813 long di = current.id3->elapsed - current.elapsed;
2814 if (di < -250 || di > 0)
2815 current.elapsed = current.id3->elapsed;
2817 else
2818 current.elapsed = current.ff_rewind;
2819 current.length = current.id3->length;
2820 if (current.elapsed > current.length)
2821 current.elapsed = current.length;
2823 else
2825 current.elapsed = 0;
2826 current.length = 1;
2829 if (current.id3 && id3_timeout &&
2830 (TIME_AFTER(*rb->current_tick, id3_timeout) ||
2831 current.id3->artist))
2833 update_display_state = true;
2834 id3_timeout = 0;
2836 current.found_lrc = find_lrc_file();
2837 #ifdef LRC_SUPPORT_ID3
2838 if (!current.found_lrc && prefs.read_id3)
2840 /* no lyrics file found. try to read from id3 tag. */
2841 current.found_lrc = read_id3();
2843 #endif
2845 else if (current.found_lrc && !current.loaded_lrc)
2847 /* current.loaded_lrc is false after changing encode setting */
2848 update_display_state = true;
2849 display_state();
2850 load_lrc_file();
2852 if (update_display_state)
2854 #ifdef HAVE_LCD_BITMAP
2855 if (current.type == TXT || current.type == ID3_USLT)
2856 current.wipe = false;
2857 else
2858 current.wipe = prefs.wipe;
2859 #endif
2860 display_state();
2861 update_display_state = false;
2863 if (AUDIO_PLAY)
2865 if (prefs.display_time)
2866 display_time();
2867 if (!id3_timeout)
2868 display_lrcs();
2871 ret = handle_button();
2874 #ifdef HAVE_LCD_BITMAP
2875 FOR_NB_SCREENS(i)
2876 rb->viewportmanager_theme_undo(i, false);
2877 #endif
2878 if (prefs.backlight_on)
2879 backlight_use_settings();
2881 return ret;
2884 /* this is the plugin entry point */
2885 enum plugin_status plugin_start(const void* parameter)
2887 int ret = LRC_GOTO_MAIN;
2889 /* initialize settings. */
2890 load_or_save_settings(false);
2892 #ifdef HAVE_LCD_BITMAP
2893 rb->lcd_getstringsize("O", NULL, &font_ui_height);
2894 #endif
2896 lrc_buffer = rb->plugin_get_buffer(&lrc_buffer_size);
2897 lrc_buffer = (void *)(((long)lrc_buffer+3)&~3); /* 4 bytes aligned */
2898 lrc_buffer_size = (lrc_buffer_size - 4)&~3;
2900 reset_current_data();
2901 current.id3 = NULL;
2902 current.mp3_file[0] = 0;
2903 current.lrc_file[0] = 0;
2904 current.ff_rewind = -1;
2905 current.found_lrc = false;
2906 if (parameter && check_audio_status())
2908 const char *ext;
2909 rb->strcpy(current.mp3_file, current.id3->path);
2910 /* use passed parameter as lrc file. */
2911 rb->strcpy(current.lrc_file, parameter);
2912 if (!rb->file_exists(current.lrc_file))
2914 rb->splash(HZ, "Specified file dose not exist.");
2915 return PLUGIN_ERROR;
2917 ext = rb->strrchr(current.lrc_file, '.');
2918 for (current.type = 0; ext && current.type < NUM_TYPES; current.type++)
2920 if (!rb->strcmp(ext, extentions[current.type]))
2921 break;
2923 if (!ext || current.type == NUM_TYPES)
2925 rb->splashf(HZ, "%s is not supported", ext? ext: current.lrc_file);
2926 return PLUGIN_ERROR;
2928 current.found_lrc = true;
2931 while (ret >= PLUGIN_OTHER)
2933 switch (ret)
2935 case LRC_GOTO_MAIN:
2936 ret = lrc_main();
2937 break;
2938 case LRC_GOTO_MENU:
2939 ret = lrc_menu();
2940 break;
2941 case LRC_GOTO_EDITOR:
2942 ret = timetag_editor();
2943 break;
2944 default:
2945 ret = PLUGIN_ERROR;
2946 break;
2950 load_or_save_settings(true);
2951 return ret;